Skip to content

Commit bfd4b64

Browse files
committed
seccomp: setupSeccomp(): update errors and remove redundant check
Make the error message slightly more informative, and remove the redundant `len(config.ArchMap) != 0` check, as iterating over an empty, or 'nil' slice is a no-op already. This allows to use a slightly more idiomatic "if ok := xx; ok" condition. Also move validation to the start of the loop (early return), and explicitly create a new slice for "names" if the legacy "Name" field is used. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent c815b86 commit bfd4b64

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

integration-cli/docker_cli_run_unix_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,7 @@ func (s *DockerDaemonSuite) TestRunSeccompJSONNoNameAndNames(c *testing.T) {
14731473

14741474
out, err := s.d.Cmd("run", "--security-opt", "seccomp="+tmpFile.Name(), "busybox", "chmod", "777", ".")
14751475
assert.ErrorContains(c, err, "")
1476-
assert.Assert(c, strings.Contains(out, "'name' and 'names' were specified in the seccomp profile, use either 'name' or 'names'"))
1476+
assert.Assert(c, strings.Contains(out, "use either 'name' or 'names'"))
14771477
}
14781478

14791479
func (s *DockerDaemonSuite) TestRunSeccompJSONNoArchAndArchMap(c *testing.T) {
@@ -1510,7 +1510,7 @@ func (s *DockerDaemonSuite) TestRunSeccompJSONNoArchAndArchMap(c *testing.T) {
15101510

15111511
out, err := s.d.Cmd("run", "--security-opt", "seccomp="+tmpFile.Name(), "busybox", "chmod", "777", ".")
15121512
assert.ErrorContains(c, err, "")
1513-
assert.Assert(c, strings.Contains(out, "'architectures' and 'archMap' were specified in the seccomp profile, use either 'architectures' or 'archMap'"))
1513+
assert.Assert(c, strings.Contains(out, "use either 'architectures' or 'archMap'"))
15141514
}
15151515

15161516
func (s *DockerDaemonSuite) TestRunWithDaemonDefaultSeccompProfile(c *testing.T) {

profiles/seccomp/seccomp_linux.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func setupSeccomp(config *Seccomp, rs *specs.Spec) (*specs.LinuxSeccomp, error)
8585
newConfig := &specs.LinuxSeccomp{}
8686

8787
if len(config.Architectures) != 0 && len(config.ArchMap) != 0 {
88-
return nil, errors.New("'architectures' and 'archMap' were specified in the seccomp profile, use either 'architectures' or 'archMap'")
88+
return nil, errors.New("both 'architectures' and 'archMap' are specified in the seccomp profile, use either 'architectures' or 'archMap'")
8989
}
9090

9191
// if config.Architectures == 0 then libseccomp will figure out the architecture to use
@@ -94,9 +94,7 @@ func setupSeccomp(config *Seccomp, rs *specs.Spec) (*specs.LinuxSeccomp, error)
9494
}
9595

9696
arch := goToNative[runtime.GOARCH]
97-
seccompArch, archExists := nativeToSeccomp[arch]
98-
99-
if len(config.ArchMap) != 0 && archExists {
97+
if seccompArch, ok := nativeToSeccomp[arch]; ok {
10098
for _, a := range config.ArchMap {
10199
if a.Arch == seccompArch {
102100
newConfig.Architectures = append(newConfig.Architectures, a.Arch)
@@ -112,8 +110,14 @@ func setupSeccomp(config *Seccomp, rs *specs.Spec) (*specs.LinuxSeccomp, error)
112110
newConfig.ListenerMetadata = config.ListenerMetadata
113111

114112
Loop:
115-
// Loop through all syscall blocks and convert them to libcontainer format after filtering them
113+
// Convert Syscall to OCI runtimes-spec specs.LinuxSyscall after filtering them.
116114
for _, call := range config.Syscalls {
115+
if call.Name != "" {
116+
if len(call.Names) != 0 {
117+
return nil, errors.New("both 'name' and 'names' are specified in the seccomp profile, use either 'name' or 'names'")
118+
}
119+
call.Names = []string{call.Name}
120+
}
117121
if call.Excludes != nil {
118122
if len(call.Excludes.Arches) > 0 {
119123
if inSlice(call.Excludes.Arches, arch) {
@@ -156,14 +160,6 @@ Loop:
156160
}
157161
}
158162
}
159-
160-
if call.Name != "" {
161-
if len(call.Names) != 0 {
162-
return nil, errors.New("'name' and 'names' were specified in the seccomp profile, use either 'name' or 'names'")
163-
}
164-
call.Names = append(call.Names, call.Name)
165-
}
166-
167163
newConfig.Syscalls = append(newConfig.Syscalls, call.LinuxSyscall)
168164
}
169165

0 commit comments

Comments
 (0)