@@ -5,7 +5,6 @@ package seccomp
55import (
66 "errors"
77 "fmt"
8- "os"
98
109 libseccomp "github.com/seccomp/libseccomp-golang"
1110 "github.com/sirupsen/logrus"
@@ -27,24 +26,25 @@ const (
2726)
2827
2928// InitSeccomp installs the seccomp filters to be used in the container as
30- // specified in config. Returns the seccomp file descriptor if any of the
31- // filters include a SCMP_ACT_NOTIFY action.
32- func InitSeccomp (config * configs.Seccomp ) (* os.File , error ) {
29+ // specified in config.
30+ // Returns the seccomp file descriptor if any of the filters include a
31+ // SCMP_ACT_NOTIFY action, otherwise returns -1.
32+ func InitSeccomp (config * configs.Seccomp ) (int , error ) {
3333 if config == nil {
34- return nil , errors .New ("cannot initialize Seccomp - nil config passed" )
34+ return - 1 , errors .New ("cannot initialize Seccomp - nil config passed" )
3535 }
3636
3737 defaultAction , err := getAction (config .DefaultAction , config .DefaultErrnoRet )
3838 if err != nil {
39- return nil , errors .New ("error initializing seccomp - invalid default action" )
39+ return - 1 , errors .New ("error initializing seccomp - invalid default action" )
4040 }
4141
4242 // Ignore the error since pre-2.4 libseccomp is treated as API level 0.
4343 apiLevel , _ := libseccomp .GetAPI ()
4444 for _ , call := range config .Syscalls {
4545 if call .Action == configs .Notify {
4646 if apiLevel < 6 {
47- return nil , fmt .Errorf ("seccomp notify unsupported: API level: got %d, want at least 6. Please try with libseccomp >= 2.5.0 and Linux >= 5.7" , apiLevel )
47+ return - 1 , fmt .Errorf ("seccomp notify unsupported: API level: got %d, want at least 6. Please try with libseccomp >= 2.5.0 and Linux >= 5.7" , apiLevel )
4848 }
4949
5050 // We can't allow the write syscall to notify to the seccomp agent.
@@ -60,36 +60,36 @@ func InitSeccomp(config *configs.Seccomp) (*os.File, error) {
6060 // agent allows those syscalls to proceed, initialization works just fine and the agent can
6161 // handle future read()/close() syscalls as it wanted.
6262 if call .Name == "write" {
63- return nil , errors .New ("SCMP_ACT_NOTIFY cannot be used for the write syscall" )
63+ return - 1 , errors .New ("SCMP_ACT_NOTIFY cannot be used for the write syscall" )
6464 }
6565 }
6666 }
6767
6868 // See comment on why write is not allowed. The same reason applies, as this can mean handling write too.
6969 if defaultAction == libseccomp .ActNotify {
70- return nil , errors .New ("SCMP_ACT_NOTIFY cannot be used as default action" )
70+ return - 1 , errors .New ("SCMP_ACT_NOTIFY cannot be used as default action" )
7171 }
7272
7373 filter , err := libseccomp .NewFilter (defaultAction )
7474 if err != nil {
75- return nil , fmt .Errorf ("error creating filter: %w" , err )
75+ return - 1 , fmt .Errorf ("error creating filter: %w" , err )
7676 }
7777
7878 // Add extra architectures
7979 for _ , arch := range config .Architectures {
8080 scmpArch , err := libseccomp .GetArchFromString (arch )
8181 if err != nil {
82- return nil , fmt .Errorf ("error validating Seccomp architecture: %w" , err )
82+ return - 1 , fmt .Errorf ("error validating Seccomp architecture: %w" , err )
8383 }
8484 if err := filter .AddArch (scmpArch ); err != nil {
85- return nil , fmt .Errorf ("error adding architecture to seccomp filter: %w" , err )
85+ return - 1 , fmt .Errorf ("error adding architecture to seccomp filter: %w" , err )
8686 }
8787 }
8888
8989 // Add extra flags.
9090 for _ , flag := range config .Flags {
9191 if err := setFlag (filter , flag ); err != nil {
92- return nil , err
92+ return - 1 , err
9393 }
9494 }
9595
@@ -109,24 +109,25 @@ func InitSeccomp(config *configs.Seccomp) (*os.File, error) {
109109
110110 // Unset no new privs bit
111111 if err := filter .SetNoNewPrivsBit (false ); err != nil {
112- return nil , fmt .Errorf ("error setting no new privileges: %w" , err )
112+ return - 1 , fmt .Errorf ("error setting no new privileges: %w" , err )
113113 }
114114
115115 // Add a rule for each syscall
116116 for _ , call := range config .Syscalls {
117117 if call == nil {
118- return nil , errors .New ("encountered nil syscall while initializing Seccomp" )
118+ return - 1 , errors .New ("encountered nil syscall while initializing Seccomp" )
119119 }
120120
121121 if err := matchCall (filter , call , defaultAction ); err != nil {
122- return nil , err
122+ return - 1 , err
123123 }
124124 }
125125
126126 seccompFd , err := patchbpf .PatchAndLoad (config , filter )
127127 if err != nil {
128- return nil , fmt .Errorf ("error loading seccomp filter into kernel: %w" , err )
128+ return - 1 , fmt .Errorf ("error loading seccomp filter into kernel: %w" , err )
129129 }
130+
130131 return seccompFd , nil
131132}
132133
0 commit comments