Skip to content

Commit f261e83

Browse files
authored
Merge pull request #174 from kolyshkin/fix-apply
capability: Apply: deny for another process
2 parents 59f321c + 8e47bad commit f261e83

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

Diff for: capability/capability_linux.go

+3
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@ func (c *capsV3) Load() (err error) {
328328
}
329329

330330
func (c *capsV3) Apply(kind CapType) (err error) {
331+
if c.hdr.pid != 0 {
332+
return errors.New("unable to modify capabilities of another process")
333+
}
331334
last, err := LastCap()
332335
if err != nil {
333336
return err

Diff for: capability/capability_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"os/exec"
1111
"runtime"
12+
"strings"
1213
"testing"
1314

1415
. "github.com/moby/sys/capability"
@@ -193,3 +194,36 @@ func childAmbientCapSet() {
193194
}
194195
os.Exit(0)
195196
}
197+
198+
// https://github.com/moby/sys/issues/168
199+
func TestApplyOtherProcess(t *testing.T) {
200+
if runtime.GOOS != "linux" {
201+
return
202+
}
203+
requirePCapSet(t)
204+
205+
cmd := exec.Command("sleep", "infinity")
206+
if err := cmd.Start(); err != nil {
207+
t.Fatal(err)
208+
}
209+
t.Cleanup(func() {
210+
_ = cmd.Process.Kill()
211+
_, _ = cmd.Process.Wait()
212+
})
213+
214+
pid, err := NewPid2(cmd.Process.Pid)
215+
if err != nil {
216+
t.Fatal(err)
217+
}
218+
pid.Clear(CAPS | BOUNDS | AMBS)
219+
220+
// See (*capsV3).Apply.
221+
expErr := "unable to modify capabilities of another process"
222+
223+
for _, arg := range []CapType{CAPS, BOUNDS, AMBS} {
224+
err = pid.Apply(arg)
225+
if !strings.Contains(err.Error(), expErr) {
226+
t.Errorf("Apply(%q): want error to contain %q; got %v", arg, expErr, err)
227+
}
228+
}
229+
}

0 commit comments

Comments
 (0)