Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libct/cap: switch to moby/sys/capability #4418

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ go 1.22
// Note that toolchain does not impose a requirement on other modules using runc.
toolchain go1.22.4

replace github.com/moby/sys/capability v0.3.0 => github.com/lifubang/moby_sys/capability v0.0.0-20241013102214-92ccf7035c8d
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which PR? This?

moby/sys#165


require (
github.com/checkpoint-restore/go-criu/v6 v6.3.0
github.com/cilium/ebpf v0.16.0
Expand All @@ -15,6 +17,7 @@ require (
github.com/cyphar/filepath-securejoin v0.3.4
github.com/docker/go-units v0.5.0
github.com/godbus/dbus/v5 v5.1.0
github.com/moby/sys/capability v0.3.0
github.com/moby/sys/mountinfo v0.7.1
github.com/moby/sys/user v0.3.0
github.com/moby/sys/userns v0.1.0
Expand All @@ -23,7 +26,6 @@ require (
github.com/opencontainers/selinux v1.11.0
github.com/seccomp/libseccomp-golang v0.10.0
github.com/sirupsen/logrus v1.9.3
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635
github.com/urfave/cli v1.22.14
github.com/vishvananda/netlink v1.1.0
golang.org/x/net v0.24.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lifubang/moby_sys/capability v0.0.0-20241013102214-92ccf7035c8d h1:EhSBja/Vl3FnEgJEvQ7B2dPrv9PjRmq9+DnWSbtlLDI=
github.com/lifubang/moby_sys/capability v0.0.0-20241013102214-92ccf7035c8d/go.mod h1:4g9IK291rVkms3LKCDOoYlnV8xKwoDTpIrNEE35Wq0I=
github.com/mdlayher/netlink v1.7.2 h1:/UtM3ofJap7Vl4QWCPDGXY8d3GIY2UGSDbK+QWmY8/g=
github.com/mdlayher/netlink v1.7.2/go.mod h1:xraEF7uJbxLhc5fpHL4cPe221LI2bdttWlU+ZGLfQSw=
github.com/mdlayher/socket v0.4.1 h1:eM9y2/jlbs1M615oshPQOHZzj6R6wMT7bX5NPiQvn2U=
Expand Down Expand Up @@ -71,8 +73,6 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 h1:kdXcSzyDtseVEc4yCz2qF8ZrQvIDBJLl4S1c3GCXmoI=
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/urfave/cli v1.22.14 h1:ebbhrRiGK2i4naQJr+1Xj92HXZCrK7MsyTS/ob3HnAk=
github.com/urfave/cli v1.22.14/go.mod h1:X0eDS6pD6Exaclxm99NJ3FiCDRED7vIHpx2mDOHLvkA=
github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJH8j0=
Expand Down
40 changes: 32 additions & 8 deletions libcontainer/capabilities/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"sort"
"strings"

"github.com/moby/sys/capability"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/sirupsen/logrus"
"github.com/syndtr/gocapability/capability"
)

const allCapabilityTypes = capability.CAPS | capability.BOUNDING | capability.AMBIENT
Expand All @@ -25,19 +25,23 @@ var (
)

func init() {
capabilityMap = make(map[string]capability.Cap, capability.CAP_LAST_CAP+1)
for _, c := range capability.List() {
if c > capability.CAP_LAST_CAP {
continue
}
list, err := capability.ListSupported()
if err != nil {
return
}
capabilityMap = make(map[string]capability.Cap, len(list))
for _, c := range list {
capabilityMap["CAP_"+strings.ToUpper(c.String())] = c
}
}

// KnownCapabilities returns the list of the known capabilities.
// Used by `runc features`.
func KnownCapabilities() []string {
list := capability.List()
list, err := capability.ListSupported()
if err != nil {
return nil
}
res := make([]string, len(list))
for i, c := range list {
res[i] = "CAP_" + strings.ToUpper(c.String())
Expand Down Expand Up @@ -118,5 +122,25 @@ func (c *Caps) ApplyCaps() error {
for _, g := range capTypes {
c.pid.Set(g, c.caps[g]...)
}
return c.pid.Apply(allCapabilityTypes)
if err := c.pid.Apply(capability.BOUNDING | capability.CAPS); err != nil {
return err
}
// As there was a bug for ambient implementation in package capability,
// the error of raise/lower ambient caps has been masked. Please see:
// https://github.com/kolyshkin/capability/pull/3
// Though the bug has been fixed in v0.4.0, but we should have a
// compatibility for ambient cap set.
ambientCaps := c.caps[capability.AMBIENT]
err := capability.AmbientClearAll()
if err != nil {
logrus.Warnf("can't lower all ambient caps: %v", err)
return nil
}
for _, ambient := range ambientCaps {
err = capability.AmbientRaise(ambient)
if err != nil {
logrus.Warnf("can't raise ambient cap(%s): %v", ambient.String(), err)
}
}
return nil
}
2 changes: 1 addition & 1 deletion libcontainer/capabilities/capabilities_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"os"
"testing"

"github.com/moby/sys/capability"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/syndtr/gocapability/capability"
)

func TestNew(t *testing.T) {
Expand Down
20 changes: 20 additions & 0 deletions tests/integration/capabilities.bats
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,23 @@ function teardown() {
[[ "${output}" == *"CapBnd: 0000000400000021"* ]]
[[ "${output}" == *"CapAmb: 0000000400000001"* ]]
}

@test "runc run with ambient not in permitted" {
update_config '.process.capabilities.inheritable = ["CAP_CHOWN", "CAP_SYSLOG"]
| .process.capabilities.permitted = ["CAP_KILL"]
| .process.capabilities.effective = ["CAP_KILL"]
| .process.capabilities.bounding = ["CAP_KILL", "CAP_CHOWN", "CAP_SYSLOG"]
| .process.capabilities.ambient = ["CAP_CHOWN"]'
runc run test_some_caps
[ "$status" -eq 0 ]
# Check that ambient cap set is null.
#
# CAP_CHOWN is 0, the bit mask is 0x1 (1 << 0)
# CAP_KILL is 5, the bit mask is 0x20 (1 << 5).
# CAP_SYSLOG is 34, the bit mask is 0x400000000 (1 << 34).
[[ "${output}" == *"CapInh: 0000000400000001"* ]]
[[ "${output}" == *"CapPrm: 0000000000000020"* ]]
[[ "${output}" == *"CapEff: 0000000000000020"* ]]
[[ "${output}" == *"CapBnd: 0000000400000021"* ]]
[[ "${output}" == *"CapAmb: 0000000000000000"* ]]
}
3 changes: 3 additions & 0 deletions vendor/github.com/moby/sys/capability/.codespellrc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 90 additions & 0 deletions vendor/github.com/moby/sys/capability/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions vendor/github.com/moby/sys/capability/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading