-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathhelpers_test.go
More file actions
199 lines (179 loc) · 6.26 KB
/
Copy pathhelpers_test.go
File metadata and controls
199 lines (179 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package libbpfgo
import (
"fmt"
"strings"
"syscall"
"testing"
"kernel.org/pub/linux/libs/security/libcap/cap"
)
// Reset only effective capabilites
func resetEffectiveCapabilities() error {
// current capability
existing := cap.GetProc()
// Clear all effective capabilites
if err := existing.ClearFlag(cap.Effective); err != nil {
return fmt.Errorf("error cleaning effective capabilites %w", err)
}
// set updated capabilitis to current process
if err := existing.SetProc(); err != nil {
return fmt.Errorf("error during update capabilites %w", err)
}
return nil
}
// Enforce effective capabilites only
func enforceEffectiveCapabilities(newCap []string) error {
existing := cap.GetProc()
// create a new empty capabilities
enforce := cap.NewSet()
// copy all/only permitted flags to new cap
if err := enforce.FillFlag(cap.Permitted, existing, cap.Permitted); err != nil {
return fmt.Errorf("failed to copy permitted capability flags: %w", err)
}
values := []cap.Value{}
for _, name := range newCap {
value, err := cap.FromName(name)
if err != nil {
return fmt.Errorf("error getting capability %q: %w", name, err)
}
values = append(values, value)
}
// only set the given effetive capabilities
if err := enforce.SetFlag(cap.Effective, true, values...); err != nil {
return fmt.Errorf("error setting effective capabilities: %w", err)
}
if err := enforce.SetProc(); err != nil {
return fmt.Errorf("failed to drop capabilities: %q -> %q: %w", existing, enforce, err)
}
return nil
}
func TestFuncSupportbyType(t *testing.T) {
tt := []struct {
progType BPFProgType
funcId BPFFunc
supported bool
capability []string
errMsg error
}{
// func available but not enough permission (permission denied)
// May return success (`true`) even if the BPF program load would fail due to permission issues (EPERM).
// Check BPFHelperIsSupported for more info.
{
progType: BPFProgTypeKprobe,
funcId: BPFFuncGetCurrentUidGid,
supported: true,
capability: []string{},
errMsg: syscall.EPERM,
},
// func available and enough permission
{
progType: BPFProgTypeKprobe,
funcId: BPFFuncGetCurrentUidGid,
supported: true,
capability: []string{"cap_bpf", "cap_perfmon"},
errMsg: nil,
},
// func unavailable and enough permission
// When the function is unavailable, BPF returns "Invalid Argument".
// Therefore, ignore the error and proceed with validation.
// May return success (`true`) even if the BPF program load would fail due to permission issues (EPERM).
// Check BPFHelperIsSupported for more info.
{
progType: BPFProgTypeSkLookup,
funcId: BPFFuncGetCurrentCgroupId,
supported: true,
capability: []string{},
errMsg: syscall.EPERM,
},
{
progType: BPFProgTypeKprobe,
funcId: BPFFuncKtimeGetNs,
supported: true,
capability: []string{"cap_bpf", "cap_perfmon"},
errMsg: nil,
},
{
progType: BPFProgTypeKprobe,
funcId: BPFFuncKtimeGetNs,
supported: true,
capability: []string{"cap_sys_admin"},
errMsg: nil,
},
{
progType: BPFProgTypeKprobe,
funcId: BPFFuncSysBpf,
supported: false,
capability: []string{"cap_bpf", "cap_perfmon"},
errMsg: syscall.EINVAL,
},
{
progType: BPFProgTypeSyscall,
funcId: BPFFuncGetCgroupClassid,
supported: false,
capability: []string{"cap_bpf"},
errMsg: syscall.EINVAL,
},
// Not able to probe helpers for some types (even with permission)
// https://github.com/libbpf/libbpf/blob/c1a6c770c46c6e78ad6755bf596c23a4e6f6b216/src/libbpf_probes.c#L430-L441
{
progType: BPFProgTypeLsm,
funcId: BPFFuncGetCurrentCgroupId,
supported: false,
capability: []string{"cap_bpf", "cap_perfmon"},
errMsg: syscall.EOPNOTSUPP,
},
{
progType: BPFProgTypeLsm,
funcId: BPFFuncGetCurrentCgroupId,
supported: false,
capability: []string{},
errMsg: syscall.EOPNOTSUPP,
},
{
progType: BPFProgTypeKprobe,
funcId: BPFFuncSockMapUpdate,
supported: false,
capability: []string{"cap_sys_admin"},
errMsg: syscall.EINVAL,
},
}
for _, tc := range tt {
// reset all current effective capabilities
if err := resetEffectiveCapabilities(); err != nil {
t.Fatalf("failed to reset effective capabilities: %v", err)
}
if tc.capability != nil {
if err := enforceEffectiveCapabilities(tc.capability); err != nil {
t.Fatalf("failed to enforce effective capabilities: %v", err)
}
}
support, err := BPFHelperIsSupported(tc.progType, tc.funcId)
// Helper support is kernel-version dependent. Newer kernels regularly add
// support for helpers that were previously unavailable for a given program
// type. Treat such forward changes (expected unsupported -> now supported)
// as a warning, since they are expected on newer kernels and CI runners.
// A regression in the other direction (expected supported -> now unsupported)
// is still treated as a hard failure.
if support != tc.supported {
if support && !tc.supported {
t.Logf("warning: expected %s to be unsupported for %s, but the kernel now reports it as supported; skipping error check", tc.funcId.String(), tc.progType.String())
continue
}
t.Errorf("expected support=%v for %s (%s), got %v (err: %v)", tc.supported, tc.funcId.String(), tc.progType.String(), support, err)
}
if tc.errMsg == nil {
if err != nil {
t.Errorf("expected no error for %s, got %v", tc.funcId.String(), err)
}
} else if err == nil || !strings.Contains(err.Error(), tc.errMsg.Error()) {
// The expected errno is not guaranteed across kernels. For an unsupported
// helper on a probeable program type (e.g. kprobe/syscall), libbpf returns
// 0 and the EINVAL we observe is the stale errno from the verifier-rejected
// bpf(BPF_PROG_LOAD) call surfaced via cgo. EOPNOTSUPP is returned by libbpf
// for non-probeable program types (tracing/ext/lsm/struct_ops), not based on
// kernel version. When a kernel gains support for the helper, the probe load
// succeeds and no error is returned at all. So we only warn on an errno
// mismatch here, as long as the support result matched the expectation above.
t.Logf("warning: expected error containing %q for %s, got %v", tc.errMsg.Error(), tc.funcId.String(), err)
}
}
}