2014-02-19 08:56:11 +08:00
|
|
|
package capabilities
|
|
|
|
|
|
|
|
import (
|
2014-05-06 03:34:21 +08:00
|
|
|
"os"
|
|
|
|
|
2014-06-10 23:14:16 +08:00
|
|
|
"github.com/docker/libcontainer"
|
2014-02-19 08:56:11 +08:00
|
|
|
"github.com/syndtr/gocapability/capability"
|
|
|
|
)
|
|
|
|
|
2014-05-15 02:29:08 +08:00
|
|
|
const allCapabilityTypes = capability.CAPS | capability.BOUNDS
|
|
|
|
|
2014-05-28 22:41:48 +08: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-15 02:29:08 +08:00
|
|
|
// DropCapabilities drops all capabilities for the current process expect those specified in the container configuration.
|
2014-02-19 08:56:11 +08:00
|
|
|
func DropCapabilities(container *libcontainer.Container) error {
|
2014-05-15 02:29:08 +08:00
|
|
|
c, err := capability.NewPid(os.Getpid())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-19 08:56:11 +08:00
|
|
|
|
2014-05-15 02:29:08 +08:00
|
|
|
keep := getEnabledCapabilities(container)
|
|
|
|
c.Clear(allCapabilityTypes)
|
|
|
|
c.Set(allCapabilityTypes, keep...)
|
|
|
|
|
|
|
|
if err := c.Apply(allCapabilityTypes); err != nil {
|
|
|
|
return err
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-17 08:44:10 +08:00
|
|
|
// getEnabledCapabilities returns the capabilities that should not be dropped by the container.
|
2014-05-15 02:29:08 +08:00
|
|
|
func getEnabledCapabilities(container *libcontainer.Container) []capability.Cap {
|
|
|
|
keep := []capability.Cap{}
|
2014-05-17 08:44:10 +08:00
|
|
|
for _, capability := range container.Capabilities {
|
|
|
|
if c := libcontainer.GetCapability(capability); c != nil {
|
|
|
|
keep = append(keep, c.Value)
|
2014-03-21 08:10:24 +08:00
|
|
|
}
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|
2014-05-15 02:29:08 +08:00
|
|
|
return keep
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|