2018-12-10 20:40:40 +00:00
|
|
|
package caps // import "github.com/docker/docker/oci/caps"
|
2016-03-18 18:50:19 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2018-12-16 15:11:37 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2016-03-18 18:50:19 +00:00
|
|
|
"github.com/syndtr/gocapability/capability"
|
|
|
|
)
|
|
|
|
|
|
|
|
var capabilityList Capabilities
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
last := capability.CAP_LAST_CAP
|
|
|
|
// hack for RHEL6 which has no /proc/sys/kernel/cap_last_cap
|
|
|
|
if last == capability.Cap(63) {
|
|
|
|
last = capability.CAP_BLOCK_SUSPEND
|
|
|
|
}
|
2020-10-16 14:38:48 +00:00
|
|
|
if last > capability.CAP_AUDIT_READ {
|
|
|
|
// Prevents docker from setting CAP_PERFMON, CAP_BPF, and CAP_CHECKPOINT_RESTORE
|
|
|
|
// capabilities on privileged (or CAP_ALL) containers on Kernel 5.8 and up.
|
|
|
|
// While these kernels support these capabilities, the current release of
|
|
|
|
// runc ships with an older version of /gocapability/capability, and does
|
|
|
|
// not know about them, causing an error to be produced.
|
|
|
|
//
|
|
|
|
// FIXME remove once https://github.com/opencontainers/runc/commit/6dfbe9b80707b1ca188255e8def15263348e0f9a
|
|
|
|
// is included in a runc release and once we stop supporting containerd 1.3.x
|
|
|
|
// (which ships with runc v1.0.0-rc92)
|
|
|
|
last = capability.CAP_AUDIT_READ
|
|
|
|
}
|
2016-03-18 18:50:19 +00:00
|
|
|
for _, cap := range capability.List() {
|
|
|
|
if cap > last {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
capabilityList = append(capabilityList,
|
|
|
|
&CapabilityMapping{
|
|
|
|
Key: "CAP_" + strings.ToUpper(cap.String()),
|
|
|
|
Value: cap,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
// CapabilityMapping maps linux capability name to its value of capability.Cap type
|
|
|
|
// Capabilities is one of the security systems in Linux Security Module (LSM)
|
|
|
|
// framework provided by the kernel.
|
|
|
|
// For more details on capabilities, see http://man7.org/linux/man-pages/man7/capabilities.7.html
|
|
|
|
CapabilityMapping struct {
|
|
|
|
Key string `json:"key,omitempty"`
|
|
|
|
Value capability.Cap `json:"value,omitempty"`
|
|
|
|
}
|
|
|
|
// Capabilities contains all CapabilityMapping
|
|
|
|
Capabilities []*CapabilityMapping
|
|
|
|
)
|
|
|
|
|
|
|
|
// String returns <key> of CapabilityMapping
|
|
|
|
func (c *CapabilityMapping) String() string {
|
|
|
|
return c.Key
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCapability returns CapabilityMapping which contains specific key
|
|
|
|
func GetCapability(key string) *CapabilityMapping {
|
|
|
|
for _, capp := range capabilityList {
|
|
|
|
if capp.Key == key {
|
|
|
|
cpy := *capp
|
|
|
|
return &cpy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllCapabilities returns all of the capabilities
|
|
|
|
func GetAllCapabilities() []string {
|
|
|
|
output := make([]string, len(capabilityList))
|
|
|
|
for i, capability := range capabilityList {
|
|
|
|
output[i] = capability.String()
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2017-11-10 05:18:48 +00:00
|
|
|
// inSlice tests whether a string is contained in a slice of strings or not.
|
|
|
|
func inSlice(slice []string, s string) bool {
|
|
|
|
for _, ss := range slice {
|
2018-12-16 15:11:37 +00:00
|
|
|
if s == ss {
|
2017-11-10 05:18:48 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-12-16 15:11:37 +00:00
|
|
|
const allCapabilities = "ALL"
|
2016-03-18 18:50:19 +00:00
|
|
|
|
2018-12-16 15:11:37 +00:00
|
|
|
// NormalizeLegacyCapabilities normalizes, and validates CapAdd/CapDrop capabilities
|
|
|
|
// by upper-casing them, and adding a CAP_ prefix (if not yet present).
|
|
|
|
//
|
|
|
|
// This function also accepts the "ALL" magic-value, that's used by CapAdd/CapDrop.
|
|
|
|
func NormalizeLegacyCapabilities(caps []string) ([]string, error) {
|
|
|
|
var normalized []string
|
2016-03-18 18:50:19 +00:00
|
|
|
|
2018-12-16 15:11:37 +00:00
|
|
|
valids := GetAllCapabilities()
|
|
|
|
for _, c := range caps {
|
|
|
|
c = strings.ToUpper(c)
|
|
|
|
if c == allCapabilities {
|
|
|
|
normalized = append(normalized, c)
|
2016-03-18 18:50:19 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-12-16 15:11:37 +00:00
|
|
|
if !strings.HasPrefix(c, "CAP_") {
|
|
|
|
c = "CAP_" + c
|
|
|
|
}
|
|
|
|
if !inSlice(valids, c) {
|
|
|
|
return nil, errdefs.InvalidParameter(fmt.Errorf("unknown capability: %q", c))
|
2016-03-18 18:50:19 +00:00
|
|
|
}
|
2018-12-16 15:11:37 +00:00
|
|
|
normalized = append(normalized, c)
|
2016-03-18 18:50:19 +00:00
|
|
|
}
|
2018-12-16 15:11:37 +00:00
|
|
|
return normalized, nil
|
|
|
|
}
|
2016-03-18 18:50:19 +00:00
|
|
|
|
2018-12-16 15:11:37 +00:00
|
|
|
// ValidateCapabilities validates if caps only contains valid capabilities
|
|
|
|
func ValidateCapabilities(caps []string) error {
|
|
|
|
valids := GetAllCapabilities()
|
|
|
|
for _, c := range caps {
|
|
|
|
if !inSlice(valids, c) {
|
|
|
|
return errdefs.InvalidParameter(fmt.Errorf("unknown capability: %q", c))
|
|
|
|
}
|
2016-03-18 18:50:19 +00:00
|
|
|
}
|
2018-12-16 15:11:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-03-18 18:50:19 +00:00
|
|
|
|
2018-12-16 15:11:37 +00:00
|
|
|
// TweakCapabilities tweaks capabilities by adding, dropping, or overriding
|
|
|
|
// capabilities in the basics capabilities list.
|
2020-07-23 18:03:15 +00:00
|
|
|
func TweakCapabilities(basics, adds, drops []string, privileged bool) ([]string, error) {
|
2018-12-16 15:11:37 +00:00
|
|
|
switch {
|
|
|
|
case privileged:
|
|
|
|
// Privileged containers get all capabilities
|
|
|
|
return GetAllCapabilities(), nil
|
|
|
|
case len(adds) == 0 && len(drops) == 0:
|
|
|
|
// Nothing to tweak; we're done
|
|
|
|
return basics, nil
|
2016-03-18 18:50:19 +00:00
|
|
|
}
|
|
|
|
|
2018-12-16 15:11:37 +00:00
|
|
|
capDrop, err := NormalizeLegacyCapabilities(drops)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
capAdd, err := NormalizeLegacyCapabilities(adds)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-18 18:50:19 +00:00
|
|
|
|
2018-12-16 15:11:37 +00:00
|
|
|
var caps []string
|
2016-03-18 18:50:19 +00:00
|
|
|
|
2018-12-16 15:11:37 +00:00
|
|
|
switch {
|
|
|
|
case inSlice(capAdd, allCapabilities):
|
|
|
|
// Add all capabilities except ones on capDrop
|
|
|
|
for _, c := range GetAllCapabilities() {
|
|
|
|
if !inSlice(capDrop, c) {
|
|
|
|
caps = append(caps, c)
|
|
|
|
}
|
2016-03-18 18:50:19 +00:00
|
|
|
}
|
2018-12-16 15:11:37 +00:00
|
|
|
case inSlice(capDrop, allCapabilities):
|
|
|
|
// "Drop" all capabilities; use what's in capAdd instead
|
|
|
|
caps = capAdd
|
|
|
|
default:
|
|
|
|
// First drop some capabilities
|
|
|
|
for _, c := range basics {
|
|
|
|
if !inSlice(capDrop, c) {
|
|
|
|
caps = append(caps, c)
|
|
|
|
}
|
2016-03-18 18:50:19 +00:00
|
|
|
}
|
2018-12-16 15:11:37 +00:00
|
|
|
// Then add the list of capabilities from capAdd
|
|
|
|
caps = append(caps, capAdd...)
|
2016-03-18 18:50:19 +00:00
|
|
|
}
|
2018-12-16 15:11:37 +00:00
|
|
|
return caps, nil
|
2016-03-18 18:50:19 +00:00
|
|
|
}
|