Browse code

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>

Sebastiaan van Stijn authored on 2021/07/16 22:42:56
Showing 2 changed files
... ...
@@ -1473,7 +1473,7 @@ func (s *DockerDaemonSuite) TestRunSeccompJSONNoNameAndNames(c *testing.T) {
1473 1473
 
1474 1474
 	out, err := s.d.Cmd("run", "--security-opt", "seccomp="+tmpFile.Name(), "busybox", "chmod", "777", ".")
1475 1475
 	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'"))
1477 1477
 }
1478 1478
 
1479 1479
 func (s *DockerDaemonSuite) TestRunSeccompJSONNoArchAndArchMap(c *testing.T) {
... ...
@@ -1510,7 +1510,7 @@ func (s *DockerDaemonSuite) TestRunSeccompJSONNoArchAndArchMap(c *testing.T) {
1510 1510
 
1511 1511
 	out, err := s.d.Cmd("run", "--security-opt", "seccomp="+tmpFile.Name(), "busybox", "chmod", "777", ".")
1512 1512
 	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'"))
1514 1514
 }
1515 1515
 
1516 1516
 func (s *DockerDaemonSuite) TestRunWithDaemonDefaultSeccompProfile(c *testing.T) {
... ...
@@ -85,7 +85,7 @@ func setupSeccomp(config *Seccomp, rs *specs.Spec) (*specs.LinuxSeccomp, error)
85 85
 	newConfig := &specs.LinuxSeccomp{}
86 86
 
87 87
 	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'")
89 89
 	}
90 90
 
91 91
 	// 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)
94 94
 	}
95 95
 
96 96
 	arch := goToNative[runtime.GOARCH]
97
-	seccompArch, archExists := nativeToSeccomp[arch]
98
-
99
-	if len(config.ArchMap) != 0 && archExists {
97
+	if seccompArch, ok := nativeToSeccomp[arch]; ok {
100 98
 		for _, a := range config.ArchMap {
101 99
 			if a.Arch == seccompArch {
102 100
 				newConfig.Architectures = append(newConfig.Architectures, a.Arch)
... ...
@@ -112,8 +110,14 @@ func setupSeccomp(config *Seccomp, rs *specs.Spec) (*specs.LinuxSeccomp, error)
112 112
 	newConfig.ListenerMetadata = config.ListenerMetadata
113 113
 
114 114
 Loop:
115
-	// Loop through all syscall blocks and convert them to libcontainer format after filtering them
115
+	// Convert Syscall to OCI runtimes-spec specs.LinuxSyscall after filtering them.
116 116
 	for _, call := range config.Syscalls {
117
+		if call.Name != "" {
118
+			if len(call.Names) != 0 {
119
+				return nil, errors.New("both 'name' and 'names' are specified in the seccomp profile, use either 'name' or 'names'")
120
+			}
121
+			call.Names = []string{call.Name}
122
+		}
117 123
 		if call.Excludes != nil {
118 124
 			if len(call.Excludes.Arches) > 0 {
119 125
 				if inSlice(call.Excludes.Arches, arch) {
... ...
@@ -156,14 +160,6 @@ Loop:
156 156
 				}
157 157
 			}
158 158
 		}
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
-
167 159
 		newConfig.Syscalls = append(newConfig.Syscalls, call.LinuxSyscall)
168 160
 	}
169 161