Skip to content

Commit

Permalink
fix(build): add syscall validation before creating profile (alegrey91#35
Browse files Browse the repository at this point in the history
)

* fix(build): add syscall validation before creating profile

Signed-off-by: Alessio Greggi <[email protected]>

---------

Signed-off-by: Alessio Greggi <[email protected]>
Co-authored-by: ccoVeille <[email protected]>
  • Loading branch information
alegrey91 and ccoVeille authored Aug 14, 2024
1 parent ea08fde commit 4e5c47e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
4 changes: 3 additions & 1 deletion cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ var buildCmd = &cobra.Command{
scanner := bufio.NewScanner(file)
for scanner.Scan() {
syscall := scanner.Text()
syscallList[string(syscall)]++
if seccomp.IsValidSyscall(syscall) {
syscallList[string(syscall)]++
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package syscalls
package seccomputils

import (
"fmt"
Expand All @@ -21,3 +21,10 @@ func Print(writer io.Writer, syscalls []uint32) error {
}
return nil
}

// IsValidSyscall returns true if a valid system call was passed to the function.
// Returns false otherwise.
func IsValidSyscall(syscall string) bool {
_, err := seccomp.GetSyscallFromName(syscall)
return err == nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package syscalls
package seccomputils

import (
"bytes"
Expand Down Expand Up @@ -46,3 +46,36 @@ func TestPrint(t *testing.T) {
})
}
}

func TestIsValidSyscall(t *testing.T) {
type args struct {
syscall string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "syscall is valid",
args: args{
syscall: "openat",
},
want: true,
},
{
name: "syscall is not valid",
args: args{
syscall: "openatx",
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsValidSyscall(tt.args.syscall); got != tt.want {
t.Errorf("IsValidSyscall() = %v, want %v", got, tt.want)
}
})
}
}
6 changes: 3 additions & 3 deletions internal/writer/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"path"

"github.com/alegrey91/harpoon/internal/archiver"
syscallsw "github.com/alegrey91/harpoon/internal/syscallswriter"
"github.com/alegrey91/harpoon/internal/seccomputils"
)

type WriteOptions struct {
Expand All @@ -32,10 +32,10 @@ func Write(syscalls []uint32, functionSymbol string, opts WriteOptions) error {
return fmt.Errorf("error setting permissions to %s: %v", file.Name(), err)
}
// write to file
errOut = syscallsw.Print(file, syscalls)
errOut = seccomputils.Print(file, syscalls)
} else {
// write to stdout
errOut = syscallsw.Print(os.Stdout, syscalls)
errOut = seccomputils.Print(os.Stdout, syscalls)
}
if errOut != nil {
return fmt.Errorf("error printing out system calls: %v", errOut)
Expand Down

0 comments on commit 4e5c47e

Please sign in to comment.