Skip to content

Commit

Permalink
Add Supported function to test availability of seccomp (#8)
Browse files Browse the repository at this point in the history
The `seccomp.Supported()` function can be used to test if seccomp is supported
by the kernel prior to calling `LoadFilter`.
  • Loading branch information
andrewkroh authored and ruflin committed May 17, 2018
1 parent d09d03a commit 5bed103
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions defs_constants_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ const prSetNoNewPrivs = C.PR_SET_NO_NEW_PRIVS
// Valid operations for seccomp syscall.
// https://github.com/torvalds/linux/blob/v4.16/include/uapi/linux/seccomp.h#L14-L17
const (
// Seccomp filter mode where only system calls that the calling thread is
// permitted to make are read(2), write(2), _exit(2) (but not
// exit_group(2)), and sigreturn(2). Flags must be 0.
seccompSetModeStrict = C.SECCOMP_SET_MODE_STRICT

// Seccomp filter mode where a BPF filter defines what system calls are
// allowed.
seccompSetModeFilter = C.SECCOMP_SET_MODE_FILTER
Expand Down
11 changes: 11 additions & 0 deletions seccomp_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ import (
"golang.org/x/sys/unix"
)

// Supported returns true if the seccomp syscall is supported.
func Supported() bool {
// Strict mode requires that flags be set to 0, but we are sending 1 so
// this will return EINVAL if the syscall exists and is allowed.
if err := seccomp(seccompSetModeStrict, 1, nil); err == syscall.EINVAL {
return true
}

return false
}

// SetNoNewPrivs will use prctl to set the calling thread's no_new_privs bit to
// 1 (true). Once set, this bit cannot be unset.
func SetNoNewPrivs() error {
Expand Down
4 changes: 4 additions & 0 deletions seccomp_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import (
)

func TestLoadFilter(t *testing.T) {
if !Supported() {
t.Skip("seccomp not supported by kernel")
}

var policy Policy

switch runtime.GOARCH {
Expand Down
7 changes: 7 additions & 0 deletions seccomp_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@

package seccomp

// Supported returns true if the seccomp syscall is supported.
//
// This is a stub for non-Linux systems. It always returns false.
func Supported() bool {
return false
}

// SetNoNewPrivs will use prctl to set the calling thread's no_new_privs bit to
// 1 (true). Once set, this bit cannot be unset.
//
Expand Down
2 changes: 2 additions & 0 deletions zconstants.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5bed103

Please sign in to comment.