Skip to content

Commit 9d6875d

Browse files
committed
Change libcontainer to drop all capabilities by default. Only keeps
those that were specified in the config. This commit also explicitly adds a set of capabilities that we were silently not dropping and were assumed by the tests. Docker-DCO-1.1-Signed-off-by: Victor Marmol <[email protected]> (github: vmarmol)
1 parent b561f03 commit 9d6875d

3 files changed

Lines changed: 29 additions & 17 deletions

File tree

daemon/execdriver/native/template/default_template.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ func New() *libcontainer.Container {
2626
"NET_ADMIN": false,
2727
"MKNOD": true,
2828
"SYSLOG": false,
29+
"SETUID": true,
30+
"SETGID": true,
31+
"CHOWN": true,
32+
"NET_RAW": true,
33+
"DAC_OVERRIDE": true,
2934
},
3035
Namespaces: map[string]bool{
3136
"NEWNS": true,

pkg/libcontainer/security/capabilities/capabilities.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,34 @@ import (
77
"github.com/syndtr/gocapability/capability"
88
)
99

10-
// DropCapabilities drops capabilities for the current process based
11-
// on the container's configuration.
10+
const allCapabilityTypes = capability.CAPS | capability.BOUNDS
11+
12+
// DropCapabilities drops all capabilities for the current process expect those specified in the container configuration.
1213
func DropCapabilities(container *libcontainer.Container) error {
13-
if drop := getCapabilitiesMask(container); len(drop) > 0 {
14-
c, err := capability.NewPid(os.Getpid())
15-
if err != nil {
16-
return err
17-
}
18-
c.Unset(capability.CAPS|capability.BOUNDS, drop...)
14+
c, err := capability.NewPid(os.Getpid())
15+
if err != nil {
16+
return err
17+
}
1918

20-
if err := c.Apply(capability.CAPS | capability.BOUNDS); err != nil {
21-
return err
22-
}
19+
keep := getEnabledCapabilities(container)
20+
c.Clear(allCapabilityTypes)
21+
c.Set(allCapabilityTypes, keep...)
22+
23+
if err := c.Apply(allCapabilityTypes); err != nil {
24+
return err
2325
}
2426
return nil
2527
}
2628

27-
// getCapabilitiesMask returns the specific cap mask values for the libcontainer types
28-
func getCapabilitiesMask(container *libcontainer.Container) []capability.Cap {
29-
drop := []capability.Cap{}
29+
// getCapabilitiesMask returns the capabilities that should not be dropped by the container.
30+
func getEnabledCapabilities(container *libcontainer.Container) []capability.Cap {
31+
keep := []capability.Cap{}
3032
for key, enabled := range container.CapabilitiesMask {
31-
if !enabled {
33+
if enabled {
3234
if c := libcontainer.GetCapability(key); c != nil {
33-
drop = append(drop, c.Value)
35+
keep = append(keep, c.Value)
3436
}
3537
}
3638
}
37-
return drop
39+
return keep
3840
}

pkg/libcontainer/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ var (
5555
{Key: "MAC_ADMIN", Value: capability.CAP_MAC_ADMIN},
5656
{Key: "NET_ADMIN", Value: capability.CAP_NET_ADMIN},
5757
{Key: "SYSLOG", Value: capability.CAP_SYSLOG},
58+
{Key: "SETUID", Value: capability.CAP_SETUID},
59+
{Key: "SETGID", Value: capability.CAP_SETGID},
60+
{Key: "CHOWN", Value: capability.CAP_CHOWN},
61+
{Key: "NET_RAW", Value: capability.CAP_NET_RAW},
62+
{Key: "DAC_OVERRIDE", Value: capability.CAP_DAC_OVERRIDE},
5863
}
5964
)
6065

0 commit comments

Comments
 (0)