2014-02-19 00:56:11 +00:00
|
|
|
package capabilities
|
|
|
|
|
|
|
|
import (
|
2014-05-05 19:34:21 +00:00
|
|
|
"os"
|
|
|
|
|
2014-02-19 00:56:11 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
|
|
|
"github.com/syndtr/gocapability/capability"
|
|
|
|
)
|
|
|
|
|
2014-05-14 18:29:08 +00:00
|
|
|
const allCapabilityTypes = capability.CAPS | capability.BOUNDS
|
|
|
|
|
|
|
|
// DropCapabilities drops all capabilities for the current process expect those specified in the container configuration.
|
2014-02-19 00:56:11 +00:00
|
|
|
func DropCapabilities(container *libcontainer.Container) error {
|
2014-05-14 18:29:08 +00:00
|
|
|
c, err := capability.NewPid(os.Getpid())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-19 00:56:11 +00:00
|
|
|
|
2014-05-14 18:29:08 +00:00
|
|
|
keep := getEnabledCapabilities(container)
|
|
|
|
c.Clear(allCapabilityTypes)
|
|
|
|
c.Set(allCapabilityTypes, keep...)
|
|
|
|
|
|
|
|
if err := c.Apply(allCapabilityTypes); err != nil {
|
|
|
|
return err
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-14 18:29:08 +00:00
|
|
|
// getCapabilitiesMask returns the capabilities that should not be dropped by the container.
|
|
|
|
func getEnabledCapabilities(container *libcontainer.Container) []capability.Cap {
|
|
|
|
keep := []capability.Cap{}
|
2014-05-05 19:34:21 +00:00
|
|
|
for key, enabled := range container.CapabilitiesMask {
|
2014-05-14 18:29:08 +00:00
|
|
|
if enabled {
|
2014-05-05 19:34:21 +00:00
|
|
|
if c := libcontainer.GetCapability(key); c != nil {
|
2014-05-14 18:29:08 +00:00
|
|
|
keep = append(keep, c.Value)
|
2014-05-05 19:34:21 +00:00
|
|
|
}
|
2014-03-21 00:10:24 +00:00
|
|
|
}
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
2014-05-14 18:29:08 +00:00
|
|
|
return keep
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|