@@ -5,7 +5,6 @@ package seccomp
5
5
import (
6
6
"errors"
7
7
"fmt"
8
- "os"
9
8
10
9
libseccomp "github.com/seccomp/libseccomp-golang"
11
10
"github.com/sirupsen/logrus"
@@ -27,24 +26,25 @@ const (
27
26
)
28
27
29
28
// 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 ) {
33
33
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" )
35
35
}
36
36
37
37
defaultAction , err := getAction (config .DefaultAction , config .DefaultErrnoRet )
38
38
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" )
40
40
}
41
41
42
42
// Ignore the error since pre-2.4 libseccomp is treated as API level 0.
43
43
apiLevel , _ := libseccomp .GetAPI ()
44
44
for _ , call := range config .Syscalls {
45
45
if call .Action == configs .Notify {
46
46
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 )
48
48
}
49
49
50
50
// 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) {
60
60
// agent allows those syscalls to proceed, initialization works just fine and the agent can
61
61
// handle future read()/close() syscalls as it wanted.
62
62
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" )
64
64
}
65
65
}
66
66
}
67
67
68
68
// See comment on why write is not allowed. The same reason applies, as this can mean handling write too.
69
69
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" )
71
71
}
72
72
73
73
filter , err := libseccomp .NewFilter (defaultAction )
74
74
if err != nil {
75
- return nil , fmt .Errorf ("error creating filter: %w" , err )
75
+ return - 1 , fmt .Errorf ("error creating filter: %w" , err )
76
76
}
77
77
78
78
// Add extra architectures
79
79
for _ , arch := range config .Architectures {
80
80
scmpArch , err := libseccomp .GetArchFromString (arch )
81
81
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 )
83
83
}
84
84
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 )
86
86
}
87
87
}
88
88
89
89
// Add extra flags.
90
90
for _ , flag := range config .Flags {
91
91
if err := setFlag (filter , flag ); err != nil {
92
- return nil , err
92
+ return - 1 , err
93
93
}
94
94
}
95
95
@@ -109,24 +109,25 @@ func InitSeccomp(config *configs.Seccomp) (*os.File, error) {
109
109
110
110
// Unset no new privs bit
111
111
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 )
113
113
}
114
114
115
115
// Add a rule for each syscall
116
116
for _ , call := range config .Syscalls {
117
117
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" )
119
119
}
120
120
121
121
if err := matchCall (filter , call , defaultAction ); err != nil {
122
- return nil , err
122
+ return - 1 , err
123
123
}
124
124
}
125
125
126
126
seccompFd , err := patchbpf .PatchAndLoad (config , filter )
127
127
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 )
129
129
}
130
+
130
131
return seccompFd , nil
131
132
}
132
133
0 commit comments