Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
Change libcontainer to drop all capabilities by default. Only keeps
Browse files Browse the repository at this point in the history
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 <vmarmol@google.com> (github: vmarmol)
  • Loading branch information
vmarmol committed May 16, 2014
1 parent 06b6ff0 commit e0f356a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
36 changes: 19 additions & 17 deletions security/capabilities/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,34 @@ import (
"github.com/syndtr/gocapability/capability"
)

// DropCapabilities drops capabilities for the current process based
// on the container's configuration.
const allCapabilityTypes = capability.CAPS | capability.BOUNDS

// DropCapabilities drops all capabilities for the current process expect those specified in the container configuration.
func DropCapabilities(container *libcontainer.Container) error {
if drop := getCapabilitiesMask(container); len(drop) > 0 {
c, err := capability.NewPid(os.Getpid())
if err != nil {
return err
}
c.Unset(capability.CAPS|capability.BOUNDS, drop...)
c, err := capability.NewPid(os.Getpid())
if err != nil {
return err
}

if err := c.Apply(capability.CAPS | capability.BOUNDS); err != nil {
return err
}
keep := getEnabledCapabilities(container)
c.Clear(allCapabilityTypes)
c.Set(allCapabilityTypes, keep...)

if err := c.Apply(allCapabilityTypes); err != nil {
return err
}
return nil
}

// getCapabilitiesMask returns the specific cap mask values for the libcontainer types
func getCapabilitiesMask(container *libcontainer.Container) []capability.Cap {
drop := []capability.Cap{}
// getCapabilitiesMask returns the capabilities that should not be dropped by the container.
func getEnabledCapabilities(container *libcontainer.Container) []capability.Cap {
keep := []capability.Cap{}
for key, enabled := range container.CapabilitiesMask {
if !enabled {
if enabled {
if c := libcontainer.GetCapability(key); c != nil {
drop = append(drop, c.Value)
keep = append(keep, c.Value)
}
}
}
return drop
return keep
}
5 changes: 5 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ var (
{Key: "MAC_ADMIN", Value: capability.CAP_MAC_ADMIN},
{Key: "NET_ADMIN", Value: capability.CAP_NET_ADMIN},
{Key: "SYSLOG", Value: capability.CAP_SYSLOG},
{Key: "SETUID", Value: capability.CAP_SETUID},
{Key: "SETGID", Value: capability.CAP_SETGID},
{Key: "CHOWN", Value: capability.CAP_CHOWN},
{Key: "NET_RAW", Value: capability.CAP_NET_RAW},
{Key: "DAC_OVERRIDE", Value: capability.CAP_DAC_OVERRIDE},
}
)

Expand Down

0 comments on commit e0f356a

Please sign in to comment.