devices.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 != nil {
  40. if *cgroup.Resources.AllowAllDevices == false {
  41. if err := writeFile(path, "devices.deny", "a"); err != nil {
  42. return err
  43. }
  44. for _, dev := range cgroup.Resources.AllowedDevices {
  45. if err := writeFile(path, "devices.allow", dev.CgroupString()); err != nil {
  46. return err
  47. }
  48. }
  49. return nil
  50. }
  51. if err := writeFile(path, "devices.allow", "a"); err != nil {
  52. return err
  53. }
  54. }
  55. for _, dev := range cgroup.Resources.DeniedDevices {
  56. if err := writeFile(path, "devices.deny", dev.CgroupString()); err != nil {
  57. return err
  58. }
  59. }
  60. return nil
  61. }
  62. func (s *DevicesGroup) Remove(d *cgroupData) error {
  63. return removePath(d.path("devices"))
  64. }
  65. func (s *DevicesGroup) GetStats(path string, stats *cgroups.Stats) error {
  66. return nil
  67. }