diff --git a/daemon/execdriver/utils.go b/daemon/execdriver/utils.go index 4188b9bf07..c3e856e32e 100644 --- a/daemon/execdriver/utils.go +++ b/daemon/execdriver/utils.go @@ -9,6 +9,11 @@ import ( func TweakCapabilities(basics, adds, drops []string) []string { var caps []string + + if utils.StringsContainsNoCase(adds, "all") { + basics = capabilities.GetAllCapabilities() + } + if !utils.StringsContainsNoCase(drops, "all") { for _, cap := range basics { if !utils.StringsContainsNoCase(drops, cap) { @@ -19,8 +24,7 @@ func TweakCapabilities(basics, adds, drops []string) []string { for _, cap := range adds { if strings.ToLower(cap) == "all" { - caps = capabilities.GetAllCapabilities() - break + continue } if !utils.StringsContainsNoCase(caps, cap) { caps = append(caps, cap) diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index d4832638b7..32af41f4e7 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -813,6 +813,21 @@ func TestCapDropALLCannotMknod(t *testing.T) { logDone("run - test --cap-drop=ALL cannot mknod") } +func TestCapDropALLAddMknodCannotMknod(t *testing.T) { + cmd := exec.Command(dockerBinary, "run", "--cap-drop=ALL --cap-add=MKNOD", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok") + out, _, err := runCommandWithOutput(cmd) + if err != nil { + t.Fatal(err, out) + } + + if actual := strings.Trim(out, "\r\n"); actual != "ok" { + t.Fatalf("expected output ok received %s", actual) + } + deleteAllContainers() + + logDone("run - test --cap-drop=ALL --cap-add=MKNOD can mknod") +} + func TestCapAddCanDownInterface(t *testing.T) { cmd := exec.Command(dockerBinary, "run", "--cap-add=NET_ADMIN", "busybox", "sh", "-c", "ip link set eth0 down && echo ok") out, _, err := runCommandWithOutput(cmd) @@ -843,6 +858,21 @@ func TestCapAddALLCanDownInterface(t *testing.T) { logDone("run - test --cap-add=ALL can set eth0 down") } +func TestCapAddALLDropNetAdminCanDownInterface(t *testing.T) { + cmd := exec.Command(dockerBinary, "run", "--cap-add=ALL --cap-drop=NET_ADMIN", "busybox", "sh", "-c", "ip link set eth0 down && echo ok") + out, _, err := runCommandWithOutput(cmd) + if err == nil { + t.Fatal(err, out) + } + + if actual := strings.Trim(out, "\r\n"); actual == "ok" { + t.Fatalf("expected output not ok received %s", actual) + } + deleteAllContainers() + + logDone("run - test --cap-add=ALL --cap-drop=NET_ADMIN cannot set eth0 down") +} + func TestPrivilegedCanMount(t *testing.T) { cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")