devices.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // +build linux
  2. package fs
  3. import (
  4. "github.com/opencontainers/runc/libcontainer/cgroups"
  5. "github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
  6. "github.com/opencontainers/runc/libcontainer/configs"
  7. "github.com/opencontainers/runc/libcontainer/system"
  8. )
  9. type DevicesGroup struct {
  10. }
  11. func (s *DevicesGroup) Name() string {
  12. return "devices"
  13. }
  14. func (s *DevicesGroup) Apply(d *cgroupData) error {
  15. _, err := d.join("devices")
  16. if err != nil {
  17. // We will return error even it's `not found` error, devices
  18. // cgroup is hard requirement for container's security.
  19. return err
  20. }
  21. return nil
  22. }
  23. func (s *DevicesGroup) Set(path string, cgroup *configs.Cgroup) error {
  24. if system.RunningInUserNS() {
  25. return nil
  26. }
  27. devices := cgroup.Resources.Devices
  28. if len(devices) > 0 {
  29. for _, dev := range devices {
  30. file := "devices.deny"
  31. if dev.Allow {
  32. file = "devices.allow"
  33. }
  34. if err := fscommon.WriteFile(path, file, dev.CgroupString()); err != nil {
  35. return err
  36. }
  37. }
  38. return nil
  39. }
  40. if cgroup.Resources.AllowAllDevices != nil {
  41. if *cgroup.Resources.AllowAllDevices == false {
  42. if err := fscommon.WriteFile(path, "devices.deny", "a"); err != nil {
  43. return err
  44. }
  45. for _, dev := range cgroup.Resources.AllowedDevices {
  46. if err := fscommon.WriteFile(path, "devices.allow", dev.CgroupString()); err != nil {
  47. return err
  48. }
  49. }
  50. return nil
  51. }
  52. if err := fscommon.WriteFile(path, "devices.allow", "a"); err != nil {
  53. return err
  54. }
  55. }
  56. for _, dev := range cgroup.Resources.DeniedDevices {
  57. if err := fscommon.WriteFile(path, "devices.deny", dev.CgroupString()); err != nil {
  58. return err
  59. }
  60. }
  61. return nil
  62. }
  63. func (s *DevicesGroup) Remove(d *cgroupData) error {
  64. return removePath(d.path("devices"))
  65. }
  66. func (s *DevicesGroup) GetStats(path string, stats *cgroups.Stats) error {
  67. return nil
  68. }