ebpf.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cgroup2
  14. import (
  15. "fmt"
  16. "github.com/cilium/ebpf"
  17. "github.com/cilium/ebpf/asm"
  18. "github.com/cilium/ebpf/link"
  19. "github.com/opencontainers/runtime-spec/specs-go"
  20. "golang.org/x/sys/unix"
  21. )
  22. // LoadAttachCgroupDeviceFilter installs eBPF device filter program to /sys/fs/cgroup/<foo> directory.
  23. //
  24. // Requires the system to be running in cgroup2 unified-mode with kernel >= 4.15 .
  25. //
  26. // https://github.com/torvalds/linux/commit/ebc614f687369f9df99828572b1d85a7c2de3d92
  27. func LoadAttachCgroupDeviceFilter(insts asm.Instructions, license string, dirFD int) (func() error, error) {
  28. nilCloser := func() error {
  29. return nil
  30. }
  31. spec := &ebpf.ProgramSpec{
  32. Type: ebpf.CGroupDevice,
  33. Instructions: insts,
  34. License: license,
  35. }
  36. prog, err := ebpf.NewProgram(spec)
  37. if err != nil {
  38. return nilCloser, err
  39. }
  40. err = link.RawAttachProgram(link.RawAttachProgramOptions{
  41. Target: dirFD,
  42. Program: prog,
  43. Attach: ebpf.AttachCGroupDevice,
  44. Flags: unix.BPF_F_ALLOW_MULTI,
  45. })
  46. if err != nil {
  47. return nilCloser, fmt.Errorf("failed to call BPF_PROG_ATTACH (BPF_CGROUP_DEVICE, BPF_F_ALLOW_MULTI): %w", err)
  48. }
  49. closer := func() error {
  50. err = link.RawDetachProgram(link.RawDetachProgramOptions{
  51. Target: dirFD,
  52. Program: prog,
  53. Attach: ebpf.AttachCGroupDevice,
  54. })
  55. if err != nil {
  56. return fmt.Errorf("failed to call BPF_PROG_DETACH (BPF_CGROUP_DEVICE): %w", err)
  57. }
  58. return nil
  59. }
  60. return closer, nil
  61. }
  62. func isRWM(cgroupPermissions string) bool {
  63. r := false
  64. w := false
  65. m := false
  66. for _, rn := range cgroupPermissions {
  67. switch rn {
  68. case 'r':
  69. r = true
  70. case 'w':
  71. w = true
  72. case 'm':
  73. m = true
  74. }
  75. }
  76. return r && w && m
  77. }
  78. // the logic is from runc
  79. // https://github.com/opencontainers/runc/blob/master/libcontainer/cgroups/fs/devices_v2.go#L44
  80. func canSkipEBPFError(devices []specs.LinuxDeviceCgroup) bool {
  81. for _, dev := range devices {
  82. if dev.Allow || !isRWM(dev.Access) {
  83. return false
  84. }
  85. }
  86. return true
  87. }