Debian Codegraph @

internal/testutils/cap.go

raw symbols SCIP
1 package testutils
2
3 import (
4 "runtime"
5 "testing"
6
7 "github.com/cilium/ebpf/internal/platform"
8 "github.com/cilium/ebpf/internal/unix"
9 )
10
11 type Capability int
12
13 // Mirrors of constants from x/sys/unix
14 const (
15 CAP_DAC_READ_SEARCH Capability = 2
16 CAP_NET_ADMIN Capability = 12
17 CAP_SYS_ADMIN Capability = 21
18 CAP_SYS_RESOURCE Capability = 24
19 CAP_PERFMON Capability = 38
20 CAP_BPF Capability = 39
21 )
22
23 // WithCapabilities runs `f` with only the given capabilities
24 // in the effective set. This allows us to assert that certain operations
25 // only require specific capabilities.
26 //
27 // The code in `f` and any code called by `f` must NOT call [runtime.LockOSThread],
28 // as this could leave the current goroutine permanently pinned to an OS thread.
29 // It must also not create any goroutines of its own, as that will result in a new
30 // OS thread being created that may or may not inherit the new capabilities of its
31 // parent, and will later be released into the schedulable pool of threads available
32 // for goroutine scheduling.
33 //
34 // Warning: on non-linux platforms, this function calls through to `f` without
35 // side effects.
36 func WithCapabilities(tb testing.TB, caps []Capability, f func()) {
37 tb.Helper()
38
39 if !platform.IsLinux {
40 f()
41 return
42 }
43
44 runtime.LockOSThread()
45 defer runtime.UnlockOSThread()
46
47 orig, err := capget()
48 if err != nil {
49 tb.Fatal("Can't get capabilities:", err)
50 }
51
52 var set capUserData
53 for _, cap := range caps {
54 set.Effective |= 1 << uint(cap)
55 }
56 set.Permitted = orig.Permitted
57
58 if err := capset(set); err != nil {
59 tb.Fatal("Can't set capabilities:", err)
60 }
61
62 // Defer here so that we restore the original capabilities even if `f` panics or calls `tb.Fatal`/`tb.Skip`.
63 defer func() {
64 if err := capset(orig); err != nil {
65 tb.Fatal("Can't restore capabilities:", err)
66 }
67 }()
68
69 f()
70 }
71
72 type capUserData struct {
73 Effective uint64
74 Permitted uint64
75 Inheritable uint64
76 }
77
78 func capget() (capUserData, error) {
79 var hdr = &unix.CapUserHeader{
80 Version: unix.LINUX_CAPABILITY_VERSION_3,
81 }
82
83 var data [2]unix.CapUserData
84 err := unix.Capget(hdr, &data[0])
85 if err != nil {
86 return capUserData{}, err
87 }
88
89 return capUserData{
90 Effective: uint64(data[0].Effective) | uint64(data[1].Effective)<<32,
91 Permitted: uint64(data[0].Permitted) | uint64(data[1].Permitted)<<32,
92 Inheritable: uint64(data[0].Inheritable) | uint64(data[1].Inheritable)<<32,
93 }, err
94 }
95
96 func capset(data capUserData) error {
97 var hdr = &unix.CapUserHeader{
98 Version: unix.LINUX_CAPABILITY_VERSION_3,
99 }
100
101 var linuxData [2]unix.CapUserData
102 linuxData[0].Effective = uint32(data.Effective & 0xFFFFFFFF)
103 linuxData[0].Permitted = uint32(data.Permitted & 0xFFFFFFFF)
104 linuxData[0].Inheritable = uint32(data.Inheritable & 0xFFFFFFFF)
105 linuxData[1].Effective = uint32(data.Effective >> 32)
106 linuxData[1].Permitted = uint32(data.Permitted >> 32)
107 linuxData[1].Inheritable = uint32(data.Inheritable >> 32)
108
109 return unix.Capset(hdr, &linuxData[0])
110 }