Skip to content

Commit 5dd92fd

Browse files
committed
libct/seccomp: skip redundant rules
This fixes using runc with podman on my system (Fedora 34). > $ podman --runtime `pwd`/runc run --rm --memory 4M fedora echo it works > Error: unable to start container process: error adding seccomp filter rule for syscall bdflush: permission denied: OCI permission denied The problem is, libseccomp returns EPERM when a redundant rule (i.e. the rule with the same action as the default one) is added, and podman (on my machine) sets the following rules in config.json: <....> "seccomp": { "defaultAction": "SCMP_ACT_ERRNO", "architectures": [ "SCMP_ARCH_X86_64", "SCMP_ARCH_X86", "SCMP_ARCH_X32" ], "syscalls": [ { "names": [ "bdflush", "io_pgetevents", <....> ], "action": "SCMP_ACT_ERRNO", "errnoRet": 1 }, <....> (Note that defaultErrnoRet is not set, but it defaults to 1). With this commit, it works: > $ podman --runtime `pwd`/runc run --memory 4M fedora echo it works > it works Add an integration test (that fails without the fix). Similar crun commit: * containers/crun@08229f3fb904c5ea19a7d9 Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent e44bee1 commit 5dd92fd

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

libcontainer/seccomp/seccomp_linux.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func InitSeccomp(config *configs.Seccomp) error {
6868
if call == nil {
6969
return errors.New("encountered nil syscall while initializing Seccomp")
7070
}
71-
if err := matchCall(filter, call); err != nil {
71+
if err := matchCall(filter, call, defaultAction); err != nil {
7272
return err
7373
}
7474
}
@@ -143,7 +143,7 @@ func getCondition(arg *configs.Arg) (libseccomp.ScmpCondition, error) {
143143
}
144144

145145
// Add a rule to match a single syscall
146-
func matchCall(filter *libseccomp.ScmpFilter, call *configs.Syscall) error {
146+
func matchCall(filter *libseccomp.ScmpFilter, call *configs.Syscall, defAct libseccomp.ScmpAction) error {
147147
if call == nil || filter == nil {
148148
return errors.New("cannot use nil as syscall to block")
149149
}
@@ -152,6 +152,17 @@ func matchCall(filter *libseccomp.ScmpFilter, call *configs.Syscall) error {
152152
return errors.New("empty string is not a valid syscall")
153153
}
154154

155+
// Convert the call's action to the libseccomp equivalent
156+
callAct, err := getAction(call.Action, call.ErrnoRet)
157+
if err != nil {
158+
return fmt.Errorf("action in seccomp profile is invalid: %w", err)
159+
}
160+
if callAct == defAct {
161+
// This rule is redundant, silently skip it
162+
// to avoid error from AddRule.
163+
return nil
164+
}
165+
155166
// If we can't resolve the syscall, assume it is not supported
156167
// by this kernel. Warn about it, don't error out.
157168
callNum, err := libseccomp.GetSyscallFromName(call.Name)
@@ -160,12 +171,6 @@ func matchCall(filter *libseccomp.ScmpFilter, call *configs.Syscall) error {
160171
return nil
161172
}
162173

163-
// Convert the call's action to the libseccomp equivalent
164-
callAct, err := getAction(call.Action, call.ErrnoRet)
165-
if err != nil {
166-
return fmt.Errorf("action in seccomp profile is invalid: %w", err)
167-
}
168-
169174
// Unconditional match - just add the rule
170175
if len(call.Args) == 0 {
171176
if err := filter.AddRule(callNum, callAct); err != nil {

tests/integration/start_hello.bats

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,15 @@ function teardown() {
7676
runc run test_hello
7777
[ "$status" -eq 0 ]
7878
}
79+
80+
@test "runc run [redundant seccomp rules]" {
81+
update_config ' .linux.seccomp = {
82+
"defaultAction": "SCMP_ACT_ALLOW",
83+
"syscalls": [{
84+
"names": ["bdflush"],
85+
"action": "SCMP_ACT_ALLOW",
86+
}]
87+
}'
88+
runc run test_hello
89+
[ "$status" -eq 0 ]
90+
}

0 commit comments

Comments
 (0)