oci.go 2.5 KB

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