Skip to content

Commit 4711642

Browse files
committed
[test] Switch to github.com/moby/sys/capability
The github.com/moby/sys/capability package is a fork of the original one, which apparently is no longer maintained. Note that "workaround for RHEL6" is removed for a number of reasons. Feel free to choose the one you like the most, either is sufficient: 1. /proc/sys/kernel/cap_last_cap is available since RHEL 6.7 (kernel 2.6.32-573.el6), released 9 years ago (2015-07-22). 2. It incorrectly returns CAP_BLOCK_SUSPEND (36), which was only added in kernel v3.5 and was never backported to RHEL6 kernels. The correct value for RHEL6 would be CAP_MAC_ADMIN (33). 3. As far as upstream kernels go, /proc/sys/kernel/cap_last_cap was added in kernel v3.2, and a correct value depends on the kernel version. It could be CAP_WAKE_ALARM (35), added to kernel v3.0, or CAP_SYSLOG (34), added to kernel v2.6.38, or possibly a lesser value for even older kernels. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 486baf8 commit 4711642

File tree

24 files changed

+355
-336
lines changed

24 files changed

+355
-336
lines changed

cmd/runtimetest/main.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import (
1515
"syscall"
1616

1717
"github.com/mndrix/tap-go"
18+
"github.com/moby/sys/capability"
1819
rspec "github.com/opencontainers/runtime-spec/specs-go"
1920
"github.com/sirupsen/logrus"
20-
"github.com/syndtr/gocapability/capability"
2121
"github.com/urfave/cli"
2222

2323
"github.com/opencontainers/runtime-tools/cmd/runtimetest/mount"
@@ -265,10 +265,9 @@ func (c *complianceTester) validateCapabilities(spec *rspec.Spec) error {
265265
return nil
266266
}
267267

268-
last := capability.CAP_LAST_CAP
269-
// workaround for RHEL6 which has no /proc/sys/kernel/cap_last_cap
270-
if last == capability.Cap(63) {
271-
last = capability.CAP_BLOCK_SUSPEND
268+
supportedCaps, err := capability.ListSupported()
269+
if err != nil {
270+
return err
272271
}
273272

274273
processCaps, err := capability.NewPid2(0)
@@ -309,11 +308,7 @@ func (c *complianceTester) validateCapabilities(spec *rspec.Spec) error {
309308
expectedCaps[ec] = true
310309
}
311310

312-
for _, cap := range capability.List() {
313-
if cap > last {
314-
continue
315-
}
316-
311+
for _, cap := range supportedCaps {
317312
capKey := fmt.Sprintf("CAP_%s", strings.ToUpper(cap.String()))
318313
expectedSet := expectedCaps[capKey]
319314
actuallySet := processCaps.Get(capType.capType, cap)

generate/generate.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
"os"
99
"strings"
1010

11+
"github.com/moby/sys/capability"
1112
rspec "github.com/opencontainers/runtime-spec/specs-go"
1213
"github.com/opencontainers/runtime-tools/generate/seccomp"
1314
capsCheck "github.com/opencontainers/runtime-tools/validate/capabilities"
14-
"github.com/syndtr/gocapability/capability"
1515
)
1616

1717
var (
@@ -1135,10 +1135,11 @@ func (g *Generator) ClearMounts() {
11351135
func (g *Generator) SetupPrivileged(privileged bool) {
11361136
if privileged { // Add all capabilities in privileged mode.
11371137
var finalCapList []string
1138-
for _, cap := range capability.List() {
1139-
if g.HostSpecific && cap > capsCheck.LastCap() {
1140-
continue
1141-
}
1138+
capList := capability.ListKnown()
1139+
if g.HostSpecific {
1140+
capList, _ = capability.ListSupported()
1141+
}
1142+
for _, cap := range capList {
11421143
finalCapList = append(finalCapList, fmt.Sprintf("CAP_%s", strings.ToUpper(cap.String())))
11431144
}
11441145
g.initConfigLinux()

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
module github.com/opencontainers/runtime-tools
22

3-
go 1.19
3+
go 1.21
44

55
require (
66
github.com/blang/semver/v4 v4.0.0
77
github.com/google/uuid v1.3.0
88
github.com/hashicorp/go-multierror v1.1.1
99
github.com/mndrix/tap-go v0.0.0-20171203230836-629fa407e90b
10+
github.com/moby/sys/capability v0.2.1-0.20240925213336-aaea55a66478
1011
github.com/mrunalp/fileutils v0.5.0
1112
github.com/opencontainers/runtime-spec v1.0.3-0.20220825212826-86290f6a00fb
1213
github.com/opencontainers/selinux v1.9.1
1314
github.com/sirupsen/logrus v1.8.1
1415
github.com/stretchr/testify v1.3.0
15-
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635
1616
github.com/urfave/cli v1.19.1
1717
github.com/xeipuuv/gojsonschema v1.2.0
1818
golang.org/x/sys v0.1.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+l
1111
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
1212
github.com/mndrix/tap-go v0.0.0-20171203230836-629fa407e90b h1:Ga1nclDSe8gOw37MVLMhfu2QKWtD6gvtQ298zsKVh8g=
1313
github.com/mndrix/tap-go v0.0.0-20171203230836-629fa407e90b/go.mod h1:pzzDgJWZ34fGzaAZGFW22KVZDfyrYW+QABMrWnJBnSs=
14+
github.com/moby/sys/capability v0.2.1-0.20240925213336-aaea55a66478 h1:L1of2hA2QHy7I07JaRddpvaDL6D72xYzRLkJp8OibzA=
15+
github.com/moby/sys/capability v0.2.1-0.20240925213336-aaea55a66478/go.mod h1:4g9IK291rVkms3LKCDOoYlnV8xKwoDTpIrNEE35Wq0I=
1416
github.com/mrunalp/fileutils v0.5.0 h1:NKzVxiH7eSk+OQ4M+ZYW1K6h27RUV3MI6NUTsHhU6Z4=
1517
github.com/mrunalp/fileutils v0.5.0/go.mod h1:M1WthSahJixYnrXQl/DFQuteStB1weuxD2QJNHXfbSQ=
1618
github.com/opencontainers/runtime-spec v1.0.3-0.20220825212826-86290f6a00fb h1:1xSVPOd7/UA+39/hXEGnBJ13p6JFB0E1EvQFlrRDOXI=
@@ -25,8 +27,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
2527
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
2628
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
2729
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
28-
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 h1:kdXcSzyDtseVEc4yCz2qF8ZrQvIDBJLl4S1c3GCXmoI=
29-
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
3030
github.com/urfave/cli v1.19.1 h1:0mKm4ZoB74PxYmZVua162y1dGt1qc10MyymYRBf3lb8=
3131
github.com/urfave/cli v1.19.1/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
3232
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=

validate/capabilities/lastcap.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package capabilities
2+
3+
import (
4+
"github.com/moby/sys/capability"
5+
)
6+
7+
// LastCap return last cap of system
8+
//
9+
// Deprecated: use github.com/moby/sys/capability.LastCap instead.
10+
func LastCap() capability.Cap {
11+
last, err := capability.LastCap()
12+
if err != nil {
13+
return -1
14+
}
15+
return last
16+
}

validate/capabilities/validate.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,43 @@ package capabilities
33
import (
44
"fmt"
55
"strings"
6+
"sync"
67

7-
"github.com/syndtr/gocapability/capability"
8+
"github.com/moby/sys/capability"
89
)
910

10-
// CapValid checks whether a capability is valid
11+
// CapValid checks whether a capability is valid. If hostSpecific is set,
12+
// it also checks that the capability is supported on the current host.
1113
func CapValid(c string, hostSpecific bool) error {
12-
isValid := false
13-
1414
if !strings.HasPrefix(c, "CAP_") {
1515
return fmt.Errorf("capability %s must start with CAP_", c)
1616
}
17-
for _, cap := range capability.List() {
18-
if c == fmt.Sprintf("CAP_%s", strings.ToUpper(cap.String())) {
19-
if hostSpecific && cap > LastCap() {
20-
return fmt.Errorf("%s is not supported on the current host", c)
21-
}
22-
isValid = true
23-
break
24-
}
25-
}
2617

27-
if !isValid {
18+
if _, ok := knownCaps()[c]; !ok {
2819
return fmt.Errorf("invalid capability: %s", c)
2920
}
21+
if !hostSpecific {
22+
return nil
23+
}
24+
if _, ok := supportedCaps()[c]; !ok {
25+
return fmt.Errorf("%s is not supported on the current host", c)
26+
}
3027
return nil
3128
}
29+
30+
func capSet(list []capability.Cap) map[string]struct{} {
31+
m := make(map[string]struct{}, len(list))
32+
for _, c := range list {
33+
m["CAP_"+strings.ToUpper(c.String())] = struct{}{}
34+
}
35+
return m
36+
}
37+
38+
var knownCaps = sync.OnceValue(func() map[string]struct{} {
39+
return capSet(capability.ListKnown())
40+
})
41+
42+
var supportedCaps = sync.OnceValue(func() map[string]struct{} {
43+
list, _ := capability.ListSupported()
44+
return capSet(list)
45+
})

validate/capabilities/validate_linux.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

validate/capabilities/validate_unsupported.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

validate/validate.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,11 @@ func CapValid(c string, hostSpecific bool) error {
692692
return capsCheck.CapValid(c, hostSpecific)
693693
}
694694

695+
// LastCap return last cap of system
696+
//
697+
// Deprecated: use github.com/moby/sys/capability.LastCap directly.
698+
var LastCap = capsCheck.LastCap
699+
695700
func envValid(env string) bool {
696701
items := strings.Split(env, "=")
697702
if len(items) < 2 {

validate/validate_linux.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,10 @@ import (
1515
rspec "github.com/opencontainers/runtime-spec/specs-go"
1616
osFilepath "github.com/opencontainers/runtime-tools/filepath"
1717
"github.com/opencontainers/runtime-tools/specerror"
18-
capsCheck "github.com/opencontainers/runtime-tools/validate/capabilities"
1918
"github.com/opencontainers/selinux/go-selinux/label"
2019
"github.com/sirupsen/logrus"
2120
)
2221

23-
// LastCap return last cap of system
24-
//
25-
// Deprecated: use github.com/opencontainers/runtime-tools/validate/capabilities.LastCap directly.
26-
var LastCap = capsCheck.LastCap
27-
2822
func deviceValid(d rspec.LinuxDevice) bool {
2923
switch d.Type {
3024
case "b", "c", "u":

0 commit comments

Comments
 (0)