Skip to content

Commit c2dadba

Browse files
committed
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. Also, bump Go to 1.21 as this is a minimally supported version for github.com/moby/sys/capability, and update CI accordingly. 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 260e151 commit c2dadba

File tree

24 files changed

+622
-451
lines changed

24 files changed

+622
-451
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
strategy:
4545
fail-fast: false
4646
matrix:
47-
go-version: [1.19.x, 1.22.x, 1.23.x]
47+
go-version: [1.21.x, 1.22.x, 1.23.x]
4848
race: ["-race", ""]
4949

5050
steps:

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/moby/sys/mountinfo"
@@ -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,19 +1,19 @@
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.4.0
1011
github.com/moby/sys/mountinfo v0.7.2
1112
github.com/mrunalp/fileutils v0.5.0
1213
github.com/opencontainers/runtime-spec v1.1.0
1314
github.com/opencontainers/selinux v1.9.1
1415
github.com/sirupsen/logrus v1.8.1
1516
github.com/stretchr/testify v1.3.0
16-
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635
1717
github.com/urfave/cli v1.19.1
1818
github.com/xeipuuv/gojsonschema v1.2.0
1919
golang.org/x/sys v0.1.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ github.com/mndrix/tap-go v0.0.0-20171203230836-629fa407e90b h1:Ga1nclDSe8gOw37MV
1313
github.com/mndrix/tap-go v0.0.0-20171203230836-629fa407e90b/go.mod h1:pzzDgJWZ34fGzaAZGFW22KVZDfyrYW+QABMrWnJBnSs=
1414
github.com/moby/sys/mountinfo v0.7.2 h1:1shs6aH5s4o5H2zQLn796ADW1wMrIwHsyJ2v9KouLrg=
1515
github.com/moby/sys/mountinfo v0.7.2/go.mod h1:1YOa8w8Ih7uW0wALDUgT1dTTSBrZ+HiBLGws92L2RU4=
16+
github.com/moby/sys/capability v0.4.0 h1:4D4mI6KlNtWMCM1Z/K0i7RV1FkX+DBDHKVJpCndZoHk=
17+
github.com/moby/sys/capability v0.4.0/go.mod h1:4g9IK291rVkms3LKCDOoYlnV8xKwoDTpIrNEE35Wq0I=
1618
github.com/mrunalp/fileutils v0.5.0 h1:NKzVxiH7eSk+OQ4M+ZYW1K6h27RUV3MI6NUTsHhU6Z4=
1719
github.com/mrunalp/fileutils v0.5.0/go.mod h1:M1WthSahJixYnrXQl/DFQuteStB1weuxD2QJNHXfbSQ=
1820
github.com/opencontainers/runtime-spec v1.1.0 h1:HHUyrt9mwHUjtasSbXSMvs4cyFxh+Bll4AjJ9odEGpg=
@@ -27,8 +29,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
2729
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
2830
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
2931
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
30-
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 h1:kdXcSzyDtseVEc4yCz2qF8ZrQvIDBJLl4S1c3GCXmoI=
31-
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
3232
github.com/urfave/cli v1.19.1 h1:0mKm4ZoB74PxYmZVua162y1dGt1qc10MyymYRBf3lb8=
3333
github.com/urfave/cli v1.19.1/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
3434
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 returns 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 returns 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 {

0 commit comments

Comments
 (0)