github.com/jfrazelle/docker@v1.1.2-0.20210712172922-bf78e25fe508/oci/oci.go (about)

     1  package oci // import "github.com/docker/docker/oci"
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strconv"
     7  
     8  	specs "github.com/opencontainers/runtime-spec/specs-go"
     9  )
    10  
    11  // TODO verify if this regex is correct for "a" (all); the docs (https://github.com/torvalds/linux/blob/v5.10/Documentation/admin-guide/cgroup-v1/devices.rst) describe:
    12  //      "'all' means it applies to all types and all major and minor numbers", and shows an example
    13  //      that *only* passes `a` as value: `echo a > /sys/fs/cgroup/1/devices.allow, which would be
    14  //      the "implicit" equivalent of "a *:* rwm". Source-code also looks to confirm this, and returns
    15  //      early for "a" (all); https://github.com/torvalds/linux/blob/v5.10/security/device_cgroup.c#L614-L642
    16  //nolint: gosimple
    17  var deviceCgroupRuleRegex = regexp.MustCompile("^([acb]) ([0-9]+|\\*):([0-9]+|\\*) ([rwm]{1,3})$")
    18  
    19  // SetCapabilities sets the provided capabilities on the spec
    20  // All capabilities are added if privileged is true
    21  func SetCapabilities(s *specs.Spec, caplist []string) error {
    22  	s.Process.Capabilities.Effective = caplist
    23  	s.Process.Capabilities.Bounding = caplist
    24  	s.Process.Capabilities.Permitted = caplist
    25  	s.Process.Capabilities.Inheritable = caplist
    26  	// setUser has already been executed here
    27  	// if non root drop capabilities in the way execve does
    28  	if s.Process.User.UID != 0 {
    29  		s.Process.Capabilities.Effective = []string{}
    30  		s.Process.Capabilities.Permitted = []string{}
    31  	}
    32  	return nil
    33  }
    34  
    35  // AppendDevicePermissionsFromCgroupRules takes rules for the devices cgroup to append to the default set
    36  func AppendDevicePermissionsFromCgroupRules(devPermissions []specs.LinuxDeviceCgroup, rules []string) ([]specs.LinuxDeviceCgroup, error) {
    37  	for _, deviceCgroupRule := range rules {
    38  		ss := deviceCgroupRuleRegex.FindAllStringSubmatch(deviceCgroupRule, -1)
    39  		if len(ss) == 0 || len(ss[0]) != 5 {
    40  			return nil, fmt.Errorf("invalid device cgroup rule format: '%s'", deviceCgroupRule)
    41  		}
    42  		matches := ss[0]
    43  
    44  		dPermissions := specs.LinuxDeviceCgroup{
    45  			Allow:  true,
    46  			Type:   matches[1],
    47  			Access: matches[4],
    48  		}
    49  		if matches[2] == "*" {
    50  			major := int64(-1)
    51  			dPermissions.Major = &major
    52  		} else {
    53  			major, err := strconv.ParseInt(matches[2], 10, 64)
    54  			if err != nil {
    55  				return nil, fmt.Errorf("invalid major value in device cgroup rule format: '%s'", deviceCgroupRule)
    56  			}
    57  			dPermissions.Major = &major
    58  		}
    59  		if matches[3] == "*" {
    60  			minor := int64(-1)
    61  			dPermissions.Minor = &minor
    62  		} else {
    63  			minor, err := strconv.ParseInt(matches[3], 10, 64)
    64  			if err != nil {
    65  				return nil, fmt.Errorf("invalid minor value in device cgroup rule format: '%s'", deviceCgroupRule)
    66  			}
    67  			dPermissions.Minor = &minor
    68  		}
    69  		devPermissions = append(devPermissions, dPermissions)
    70  	}
    71  	return devPermissions, nil
    72  }