Skip to content

Commit

Permalink
Merge pull request #164 from lifubang/ambient-clearall-first
Browse files Browse the repository at this point in the history
capability: use pr_CAP_AMBIENT_CLEAR_ALL to reduce pr_CAP_AMBIENT_LOWER syscall
  • Loading branch information
kolyshkin authored Oct 10, 2024
2 parents aa7aee5 + 97c671e commit da487b0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
12 changes: 8 additions & 4 deletions capability/capability_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,16 @@ func (c *capsV3) Apply(kind CapType) (err error) {
}

if kind&AMBS == AMBS {
err = prctl(pr_CAP_AMBIENT, pr_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0)
if err != nil && err != syscall.EINVAL { //nolint:errorlint // Errors from syscall are bare.
// Ignore EINVAL as not supported on kernels before 4.3
return
}
for i := Cap(0); i <= last; i++ {
action := pr_CAP_AMBIENT_LOWER
if c.Get(AMBIENT, i) {
action = pr_CAP_AMBIENT_RAISE
if !c.Get(AMBIENT, i) {
continue
}
err = prctl(pr_CAP_AMBIENT, action, uintptr(i), 0, 0)
err = prctl(pr_CAP_AMBIENT, pr_CAP_AMBIENT_RAISE, uintptr(i), 0, 0)
if err != nil {
// Ignore EINVAL as not supported on kernels before 4.3
if err == syscall.EINVAL { //nolint:errorlint // Errors from syscall are bare.
Expand Down
63 changes: 63 additions & 0 deletions capability/capability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ const (
maxLastCap = CAP_CHECKPOINT_RESTORE
)

func requirePCapSet(t *testing.T) {
pid, err := NewPid(0)
if err != nil {
t.Fatal(err)
}
if !pid.Get(EFFECTIVE, CAP_SETPCAP) {
t.Skip("The test needs `CAP_SETPCAP`.")
}
}

func TestLastCap(t *testing.T) {
last, err := LastCap()
switch runtime.GOOS {
Expand Down Expand Up @@ -91,3 +101,56 @@ func TestNewPid2Load(t *testing.T) {
t.Fatal("loaded bounding set: want non-empty, got empty")
}
}

func TestAmbientCapSet(t *testing.T) {
if runtime.GOOS != "linux" {
return
}
requirePCapSet(t)

capBounding := []Cap{CAP_KILL, CAP_CHOWN, CAP_SYSLOG}
capPermitted := []Cap{CAP_KILL, CAP_CHOWN}
capEffective := []Cap{CAP_KILL}
capInheritable := []Cap{CAP_KILL, CAP_CHOWN}
capAmbient := []Cap{CAP_KILL, CAP_CHOWN}

pid, err := newPid(0)
if err != nil {
t.Fatal(err)
}
pid.Set(BOUNDING, capBounding...)
pid.Set(PERMITTED, capPermitted...)
pid.Set(EFFECTIVE, capEffective...)
pid.Set(INHERITABLE, capInheritable...)
pid.Set(AMBIENT, capAmbient...)
if err = pid.Apply(CAPS | BOUNDING | AMBIENT); err != nil {
t.Fatal(err)
}

// Restore the cap set data from current process
if err = pid.Load(); err != nil {
t.Fatal(err)
}
for _, cap := range capAmbient {
if !pid.Get(AMBIENT, cap) {
t.Fatalf("expected ambient cap(%d) to be set but it's not", cap)
}
}

// Remove a ambient cap, to check `PR_CAP_AMBIENT_CLEAR_ALL` work.
pid.Clear(AMBIENT)
pid.Set(AMBIENT, capAmbient[0])
if err = pid.Apply(CAPS | BOUNDING | AMBIENT); err != nil {
t.Fatal(err)
}

if err = pid.Load(); err != nil {
t.Fatal(err)
}
if !pid.Get(AMBIENT, capAmbient[0]) {
t.Fatalf("expected ambient cap(%d) to be set but it's not", capAmbient[0])
}
if pid.Get(AMBIENT, capAmbient[1]) {
t.Fatalf("expected ambient cap(%d) not to be set but it has been set", capAmbient[1])
}
}

0 comments on commit da487b0

Please sign in to comment.