Merge pull request #5810 from vmarmol/drop-caps

Change libcontainer to drop all capabilities by default.
This commit is contained in:
Victor Marmol 2014-05-16 11:51:41 -07:00
commit fa7e4d6946
2 changed files with 25 additions and 18 deletions

View file

@ -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...)
if err := c.Apply(capability.CAPS | capability.BOUNDS); err != nil {
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
}

View file

@ -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},
}
)