-
Notifications
You must be signed in to change notification settings - Fork 94
/
helpers_test.go
182 lines (161 loc) · 4.51 KB
/
helpers_test.go
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
package libbpfgo
import (
"errors"
"fmt"
"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
enforce.FillFlag(cap.Permitted, existing, cap.Permitted)
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)
{
progType: BPFProgTypeKprobe,
funcId: BPFFuncGetCurrentUidGid,
supported: false,
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.
{
progType: BPFProgTypeSkLookup,
funcId: BPFFuncGetCurrentCgroupId,
supported: false,
capability: []string{"cap_sys_admin"},
errMsg: syscall.EINVAL,
},
{
progType: BPFProgTypeSkLookup,
funcId: BPFFuncGetCurrentCgroupId,
supported: false,
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
resetEffectiveCapabilities()
if tc.capability != nil {
enforceEffectiveCapabilities(tc.capability)
}
support, err := BPFHelperIsSupported(tc.progType, tc.funcId)
if tc.errMsg == nil {
if err != nil {
t.Errorf("expected no error, got %v", err)
}
} else {
if !errors.Is(err, tc.errMsg) {
t.Errorf("expected error %v, got %v", tc.errMsg, err)
}
}
// This may fail if the bpf helper support for a specific program changes in future.
if support != tc.supported {
t.Errorf("expected %v, got %v", tc.supported, support)
}
}
}