Skip to content

Commit c28aa4e

Browse files
committed
Switch to github.com/moby/sys/capability v0.4.0
This is an example to explain how to keep the behavior or runc after we repalce the package capability. Signed-off-by: lfbzhm <[email protected]>
1 parent 69b9cd8 commit c28aa4e

File tree

16 files changed

+403
-282
lines changed

16 files changed

+403
-282
lines changed

go.mod

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ go 1.22
77
// Note that toolchain does not impose a requirement on other modules using runc.
88
toolchain go1.22.4
99

10+
replace github.com/moby/sys/capability v0.3.0 => github.com/lifubang/moby_sys/capability v0.0.0-20241013102214-92ccf7035c8d
11+
1012
require (
1113
github.com/checkpoint-restore/go-criu/v6 v6.3.0
1214
github.com/cilium/ebpf v0.16.0
@@ -15,6 +17,7 @@ require (
1517
github.com/cyphar/filepath-securejoin v0.3.4
1618
github.com/docker/go-units v0.5.0
1719
github.com/godbus/dbus/v5 v5.1.0
20+
github.com/moby/sys/capability v0.3.0
1821
github.com/moby/sys/mountinfo v0.7.1
1922
github.com/moby/sys/user v0.3.0
2023
github.com/moby/sys/userns v0.1.0
@@ -23,7 +26,6 @@ require (
2326
github.com/opencontainers/selinux v1.11.0
2427
github.com/seccomp/libseccomp-golang v0.10.0
2528
github.com/sirupsen/logrus v1.9.3
26-
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635
2729
github.com/urfave/cli v1.22.14
2830
github.com/vishvananda/netlink v1.1.0
2931
golang.org/x/net v0.24.0

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
3434
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
3535
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
3636
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
37+
github.com/lifubang/moby_sys/capability v0.0.0-20241013102214-92ccf7035c8d h1:EhSBja/Vl3FnEgJEvQ7B2dPrv9PjRmq9+DnWSbtlLDI=
38+
github.com/lifubang/moby_sys/capability v0.0.0-20241013102214-92ccf7035c8d/go.mod h1:4g9IK291rVkms3LKCDOoYlnV8xKwoDTpIrNEE35Wq0I=
3739
github.com/mdlayher/netlink v1.7.2 h1:/UtM3ofJap7Vl4QWCPDGXY8d3GIY2UGSDbK+QWmY8/g=
3840
github.com/mdlayher/netlink v1.7.2/go.mod h1:xraEF7uJbxLhc5fpHL4cPe221LI2bdttWlU+ZGLfQSw=
3941
github.com/mdlayher/socket v0.4.1 h1:eM9y2/jlbs1M615oshPQOHZzj6R6wMT7bX5NPiQvn2U=
@@ -71,8 +73,6 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
7173
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
7274
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
7375
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
74-
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 h1:kdXcSzyDtseVEc4yCz2qF8ZrQvIDBJLl4S1c3GCXmoI=
75-
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
7676
github.com/urfave/cli v1.22.14 h1:ebbhrRiGK2i4naQJr+1Xj92HXZCrK7MsyTS/ob3HnAk=
7777
github.com/urfave/cli v1.22.14/go.mod h1:X0eDS6pD6Exaclxm99NJ3FiCDRED7vIHpx2mDOHLvkA=
7878
github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJH8j0=

libcontainer/capabilities/capabilities.go

+32-8
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"sort"
77
"strings"
88

9+
"github.com/moby/sys/capability"
910
"github.com/opencontainers/runc/libcontainer/configs"
1011
"github.com/sirupsen/logrus"
11-
"github.com/syndtr/gocapability/capability"
1212
)
1313

1414
const allCapabilityTypes = capability.CAPS | capability.BOUNDING | capability.AMBIENT
@@ -25,19 +25,23 @@ var (
2525
)
2626

2727
func init() {
28-
capabilityMap = make(map[string]capability.Cap, capability.CAP_LAST_CAP+1)
29-
for _, c := range capability.List() {
30-
if c > capability.CAP_LAST_CAP {
31-
continue
32-
}
28+
list, err := capability.ListSupported()
29+
if err != nil {
30+
return
31+
}
32+
capabilityMap = make(map[string]capability.Cap, len(list))
33+
for _, c := range list {
3334
capabilityMap["CAP_"+strings.ToUpper(c.String())] = c
3435
}
3536
}
3637

3738
// KnownCapabilities returns the list of the known capabilities.
3839
// Used by `runc features`.
3940
func KnownCapabilities() []string {
40-
list := capability.List()
41+
list, err := capability.ListSupported()
42+
if err != nil {
43+
return nil
44+
}
4145
res := make([]string, len(list))
4246
for i, c := range list {
4347
res[i] = "CAP_" + strings.ToUpper(c.String())
@@ -118,5 +122,25 @@ func (c *Caps) ApplyCaps() error {
118122
for _, g := range capTypes {
119123
c.pid.Set(g, c.caps[g]...)
120124
}
121-
return c.pid.Apply(allCapabilityTypes)
125+
if err := c.pid.Apply(capability.BOUNDING | capability.CAPS); err != nil {
126+
return err
127+
}
128+
// As there was a bug for ambient implementation in package capability,
129+
// the error of raise/lower ambient caps has been masked. Please see:
130+
// https://github.com/kolyshkin/capability/pull/3
131+
// Though the bug has been fixed in v0.4.0, but we should have a
132+
// compatibility for ambient cap set.
133+
ambientCaps := c.caps[capability.AMBIENT]
134+
err := capability.AmbientClearAll()
135+
if err != nil {
136+
logrus.Warnf("can't lower all ambient caps: %v", err)
137+
return nil
138+
}
139+
for _, ambient := range ambientCaps {
140+
err = capability.AmbientRaise(ambient)
141+
if err != nil {
142+
logrus.Warnf("can't raise ambient cap(%s): %v", ambient.String(), err)
143+
}
144+
}
145+
return nil
122146
}

libcontainer/capabilities/capabilities_linux_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"os"
66
"testing"
77

8+
"github.com/moby/sys/capability"
89
"github.com/opencontainers/runc/libcontainer/configs"
910
"github.com/sirupsen/logrus"
1011
"github.com/sirupsen/logrus/hooks/test"
11-
"github.com/syndtr/gocapability/capability"
1212
)
1313

1414
func TestNew(t *testing.T) {

vendor/github.com/moby/sys/capability/.codespellrc

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/moby/sys/capability/CHANGELOG.md

+90
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/syndtr/gocapability/LICENSE vendor/github.com/moby/sys/capability/LICENSE

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/moby/sys/capability/README.md

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/syndtr/gocapability/capability/capability.go vendor/github.com/moby/sys/capability/capability.go

+62-36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)