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
|
|
|
|
|
2014-05-28 14:41:48 +00:00
|
|
|
// DropBoundingSet drops the capability bounding set to those specified in the
|
|
|
|
// container configuration.
|
|
|
|
func DropBoundingSet(container *libcontainer.Container) error {
|
|
|
|
c, err := capability.NewPid(os.Getpid())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
keep := getEnabledCapabilities(container)
|
|
|
|
c.Clear(capability.BOUNDS)
|
|
|
|
c.Set(capability.BOUNDS, keep...)
|
|
|
|
|
|
|
|
if err := c.Apply(capability.BOUNDS); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-14 18:29:08 +00:00
|
|
|
// 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-17 00:44:10 +00:00
|
|
|
// getEnabledCapabilities returns the capabilities that should not be dropped by the container.
|
2014-05-14 18:29:08 +00:00
|
|
|
func getEnabledCapabilities(container *libcontainer.Container) []capability.Cap {
|
|
|
|
keep := []capability.Cap{}
|
2014-05-17 00:44:10 +00:00
|
|
|
for _, capability := range container.Capabilities {
|
|
|
|
if c := libcontainer.GetCapability(capability); c != nil {
|
|
|
|
keep = append(keep, c.Value)
|
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
|
|
|
}
|