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