devices.go 1.6 KB

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