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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
package internal
import (
"testing"
"ior/internal/event"
"ior/internal/file"
"ior/internal/globalfilter"
"ior/internal/types"
)
func TestHandleSocketExitTracksReturnedFd(t *testing.T) {
el := mustNewEventLoop(t, eventLoopConfig{})
enter := &types.SocketEvent{
EventType: types.ENTER_SOCKET_EVENT,
TraceId: types.SYS_ENTER_SOCKET,
Time: 100,
Pid: 42,
Tid: 43,
Family: 1,
Type: 2,
Protocol: 0,
}
exit := &types.RetEvent{
EventType: types.EXIT_SOCKET_EVENT,
TraceId: types.SYS_EXIT_SOCKET,
Time: 200,
Ret: 55,
Pid: 42,
Tid: 43,
}
ep := &event.Pair{EnterEv: enter, ExitEv: exit}
if ok := el.handleSocketExit(ep, enter); !ok {
t.Fatal("handleSocketExit returned false")
}
verifyFileDescriptor(t, el, 55, "socket:1:2:0")
}
func TestHandleSocketExitAppliesPairFilter(t *testing.T) {
el := mustNewEventLoop(t, eventLoopConfig{
filter: globalfilter.Filter{
Syscall: &globalfilter.StringFilter{Pattern: "openat"},
},
})
enter := &types.SocketEvent{
EventType: types.ENTER_SOCKET_EVENT,
TraceId: types.SYS_ENTER_SOCKET,
Time: 100,
Pid: 42,
Tid: 43,
Family: 1,
Type: 2,
Protocol: 0,
}
exit := &types.RetEvent{
EventType: types.EXIT_SOCKET_EVENT,
TraceId: types.SYS_EXIT_SOCKET,
Time: 200,
Ret: 55,
Pid: 42,
Tid: 43,
}
ep := &event.Pair{EnterEv: enter, ExitEv: exit}
if ok := el.handleSocketExit(ep, enter); ok {
t.Fatal("handleSocketExit should reject pair due to filter mismatch")
}
}
func TestHandleSocketpairExitTracksReturnedFdsFromExitEvent(t *testing.T) {
el := mustNewEventLoop(t, eventLoopConfig{})
enter := &types.SocketpairEvent{
EventType: types.ENTER_SOCKETPAIR_EVENT,
TraceId: types.SYS_ENTER_SOCKETPAIR,
Time: 100,
Pid: 77,
Tid: 78,
Family: 1,
Type: 1,
Protocol: 0,
Sv0: -1,
Sv1: -1,
Ret: 0,
}
exit := &types.SocketpairEvent{
EventType: types.EXIT_SOCKETPAIR_EVENT,
TraceId: types.SYS_EXIT_SOCKETPAIR,
Time: 200,
Pid: 77,
Tid: 78,
Family: 1,
Type: 1,
Protocol: 0,
Sv0: 61,
Sv1: 62,
Ret: 0,
}
ep := &event.Pair{EnterEv: enter, ExitEv: exit}
if ok := el.handleSocketpairExit(ep, enter); !ok {
t.Fatal("handleSocketpairExit returned false")
}
verifyFileDescriptor(t, el, 61, "socket:1:1:0")
verifyFileDescriptor(t, el, 62, "socket:1:1:0")
}
// TestHandleSocketpairExitDoesNotTrackDomainAsFd is a regression lock-in for the
// socketpair(2) audit (task c00). socketpair's first argument (args[0]) is the
// address-family/domain constant (e.g. AF_UNIX, AF_INET6), NOT a file
// descriptor: the two created fds are written by the kernel into the OUTPUT
// array sv[2] (args[3]) and are only valid AFTER the call returns. KindSocketpair
// captures sv0/sv1 from that output buffer at exit; it must never register the
// domain integer as an fd. This test pins that invariant by using a Family value
// (AF_INET6 == 10) that is numerically distinct from the returned fds and
// asserting fd 10 is never tracked.
func TestHandleSocketpairExitDoesNotTrackDomainAsFd(t *testing.T) {
el := mustNewEventLoop(t, eventLoopConfig{})
const afInet6 = 10
enter := &types.SocketpairEvent{
EventType: types.ENTER_SOCKETPAIR_EVENT,
TraceId: types.SYS_ENTER_SOCKETPAIR,
Time: 100,
Pid: 77,
Tid: 78,
Family: afInet6,
Type: 1,
Protocol: 0,
Sv0: -1,
Sv1: -1,
Ret: 0,
}
exit := &types.SocketpairEvent{
EventType: types.EXIT_SOCKETPAIR_EVENT,
TraceId: types.SYS_EXIT_SOCKETPAIR,
Time: 200,
Pid: 77,
Tid: 78,
Family: afInet6,
Type: 1,
Protocol: 0,
Sv0: 3,
Sv1: 4,
Ret: 0,
}
ep := &event.Pair{EnterEv: enter, ExitEv: exit}
if ok := el.handleSocketpairExit(ep, enter); !ok {
t.Fatal("handleSocketpairExit returned false")
}
// Only the output fds sv[2] are tracked.
verifyFileDescriptor(t, el, 3, "socket:10:1:0")
verifyFileDescriptor(t, el, 4, "socket:10:1:0")
// The domain constant (AF_INET6 == 10) must NOT have been captured as an fd.
verifyFdNotTracked(t, el, afInet6)
}
// TestHandleSocketpairExitDropsFdsOnError pins that a failed socketpair(2)
// (ret != 0) tracks no descriptors: the sv[2] output buffer is undefined on
// error, so the BPF exit handler leaves sv0/sv1 at the -1 sentinel and the
// userspace handler must not register anything.
func TestHandleSocketpairExitDropsFdsOnError(t *testing.T) {
el := mustNewEventLoop(t, eventLoopConfig{})
enter := &types.SocketpairEvent{
EventType: types.ENTER_SOCKETPAIR_EVENT,
TraceId: types.SYS_ENTER_SOCKETPAIR,
Time: 100,
Pid: 77,
Tid: 78,
Family: 1,
Type: 1,
Protocol: 0,
Sv0: -1,
Sv1: -1,
Ret: 0,
}
exit := &types.SocketpairEvent{
EventType: types.EXIT_SOCKETPAIR_EVENT,
TraceId: types.SYS_EXIT_SOCKETPAIR,
Time: 200,
Pid: 77,
Tid: 78,
Family: 1,
Type: 1,
Protocol: 0,
Sv0: -1,
Sv1: -1,
Ret: -24, // -EMFILE
}
ep := &event.Pair{EnterEv: enter, ExitEv: exit}
if ok := el.handleSocketpairExit(ep, enter); !ok {
t.Fatal("handleSocketpairExit returned false")
}
verifyFdNotTracked(t, el, 1)
verifyFdNotTracked(t, el, -1)
}
func TestHandleAcceptExitTracksAcceptedFd(t *testing.T) {
el := mustNewEventLoop(t, eventLoopConfig{})
el.fdState().set(11, file.NewFd(11, "socket:1:1:0", -1))
enter := &types.AcceptEvent{
EventType: types.ENTER_ACCEPT_EVENT,
TraceId: types.SYS_ENTER_ACCEPT4,
Time: 100,
Pid: 91,
Tid: 92,
Fd: 11,
Ret: -1,
}
exit := &types.AcceptEvent{
EventType: types.EXIT_ACCEPT_EVENT,
TraceId: types.SYS_EXIT_ACCEPT4,
Time: 200,
Pid: 91,
Tid: 92,
Fd: -1,
Ret: 77,
}
ep := &event.Pair{EnterEv: enter, ExitEv: exit}
if ok := el.handleAcceptExit(ep, enter); !ok {
t.Fatal("handleAcceptExit returned false")
}
verifyFileDescriptor(t, el, 77, "socket:1:1:0")
}
func TestHandleAcceptExitAppliesPairFilter(t *testing.T) {
el := mustNewEventLoop(t, eventLoopConfig{
filter: globalfilter.Filter{
Syscall: &globalfilter.StringFilter{Pattern: "openat"},
},
})
el.fdState().set(11, file.NewFd(11, "socket:1:1:0", -1))
enter := &types.AcceptEvent{
EventType: types.ENTER_ACCEPT_EVENT,
TraceId: types.SYS_ENTER_ACCEPT,
Time: 100,
Pid: 91,
Tid: 92,
Fd: 11,
Ret: -1,
}
exit := &types.AcceptEvent{
EventType: types.EXIT_ACCEPT_EVENT,
TraceId: types.SYS_EXIT_ACCEPT,
Time: 200,
Pid: 91,
Tid: 92,
Fd: -1,
Ret: 77,
}
ep := &event.Pair{EnterEv: enter, ExitEv: exit}
if ok := el.handleAcceptExit(ep, enter); ok {
t.Fatal("handleAcceptExit should reject pair due to filter mismatch")
}
}
func TestInitRawHandlersRegistersSocketEvents(t *testing.T) {
el := mustNewEventLoop(t, eventLoopConfig{})
if _, ok := el.rawHandlers[types.ENTER_SOCKET_EVENT]; !ok {
t.Fatal("ENTER_SOCKET_EVENT handler is not registered")
}
if _, ok := el.rawHandlers[types.ENTER_SOCKETPAIR_EVENT]; !ok {
t.Fatal("ENTER_SOCKETPAIR_EVENT handler is not registered")
}
if _, ok := el.rawHandlers[types.EXIT_SOCKETPAIR_EVENT]; !ok {
t.Fatal("EXIT_SOCKETPAIR_EVENT handler is not registered")
}
if _, ok := el.rawHandlers[types.ENTER_ACCEPT_EVENT]; !ok {
t.Fatal("ENTER_ACCEPT_EVENT handler is not registered")
}
if _, ok := el.rawHandlers[types.EXIT_ACCEPT_EVENT]; !ok {
t.Fatal("EXIT_ACCEPT_EVENT handler is not registered")
}
}
|