Skip to content

Commit 8772c4d

Browse files
author
Mrunal Patel
authored
Merge pull request opencontainers#3109 from kolyshkin/seccomp
seccomp: skip redundant rules
2 parents 64b3fe9 + 5dd92fd commit 8772c4d

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

libcontainer/seccomp/config.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ func ConvertStringToOperator(in string) (configs.Operator, error) {
5656
}
5757

5858
// ConvertStringToAction converts a string into a Seccomp rule match action.
59-
// Actions use the names they are assigned in Libseccomp's header, though some
60-
// (notable, SCMP_ACT_TRACE) are not available in this implementation and will
61-
// return errors.
59+
// Actions use the names they are assigned in Libseccomp's header.
6260
// Attempting to convert a string that is not a valid action results in an
6361
// error.
6462
func ConvertStringToAction(in string) (configs.Action, error) {

libcontainer/seccomp/seccomp_linux.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import (
66
"errors"
77
"fmt"
88

9-
"github.com/opencontainers/runc/libcontainer/configs"
10-
"github.com/opencontainers/runc/libcontainer/seccomp/patchbpf"
11-
129
libseccomp "github.com/seccomp/libseccomp-golang"
10+
"github.com/sirupsen/logrus"
1311
"golang.org/x/sys/unix"
12+
13+
"github.com/opencontainers/runc/libcontainer/configs"
14+
"github.com/opencontainers/runc/libcontainer/seccomp/patchbpf"
1415
)
1516

1617
var (
@@ -67,7 +68,7 @@ func InitSeccomp(config *configs.Seccomp) error {
6768
if call == nil {
6869
return errors.New("encountered nil syscall while initializing Seccomp")
6970
}
70-
if err := matchCall(filter, call); err != nil {
71+
if err := matchCall(filter, call, defaultAction); err != nil {
7172
return err
7273
}
7374
}
@@ -142,7 +143,7 @@ func getCondition(arg *configs.Arg) (libseccomp.ScmpCondition, error) {
142143
}
143144

144145
// Add a rule to match a single syscall
145-
func matchCall(filter *libseccomp.ScmpFilter, call *configs.Syscall) error {
146+
func matchCall(filter *libseccomp.ScmpFilter, call *configs.Syscall, defAct libseccomp.ScmpAction) error {
146147
if call == nil || filter == nil {
147148
return errors.New("cannot use nil as syscall to block")
148149
}
@@ -151,17 +152,23 @@ func matchCall(filter *libseccomp.ScmpFilter, call *configs.Syscall) error {
151152
return errors.New("empty string is not a valid syscall")
152153
}
153154

154-
// If we can't resolve the syscall, assume it's not supported on this kernel
155-
// Ignore it, don't error out
156-
callNum, err := libseccomp.GetSyscallFromName(call.Name)
155+
// Convert the call's action to the libseccomp equivalent
156+
callAct, err := getAction(call.Action, call.ErrnoRet)
157157
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.
158163
return nil
159164
}
160165

161-
// Convert the call's action to the libseccomp equivalent
162-
callAct, err := getAction(call.Action, call.ErrnoRet)
166+
// If we can't resolve the syscall, assume it is not supported
167+
// by this kernel. Warn about it, don't error out.
168+
callNum, err := libseccomp.GetSyscallFromName(call.Name)
163169
if err != nil {
164-
return fmt.Errorf("action in seccomp profile is invalid: %w", err)
170+
logrus.Debugf("unknown seccomp syscall %q ignored", call.Name)
171+
return nil
165172
}
166173

167174
// Unconditional match - just add the rule

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)