Skip to content

Commit 5640952

Browse files
Fix potential verification failure for opensnoop.py (#5364)
When testing #5362, I found the following verification failure for fentry progs: ... 44: (b7) r0 = 0 ; R0_w=0 refs=3 45: (95) exit Unreleased reference id=3 alloc_insn=10 The reason is missing `ringbuf_discard` for certain conditionals for fentry progs since it used `ringbuf_reserve`. Adding `ringbuf_discard` in proper places can fix verification failure.
1 parent 0ae562c commit 5640952

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

tools/opensnoop.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@
227227
u32 tid = id; // Cast and get the lower part
228228
u32 uid = bpf_get_current_uid_gid();
229229
230-
PID_TID_FILTER
231-
UID_FILTER
232-
FLAGS_FILTER
230+
KPROBE_PID_TID_FILTER
231+
KPROBE_UID_FILTER
232+
KPROBE_FLAGS_FILTER
233233
234234
if (container_should_be_filtered()) {
235235
return 0;
@@ -327,10 +327,11 @@
327327
if (!data)
328328
return 0;
329329
330-
PID_TID_FILTER
331-
UID_FILTER
332-
FLAGS_FILTER
330+
KFUNC_PID_TID_FILTER
331+
KFUNC_UID_FILTER
332+
KFUNC_FLAGS_FILTER
333333
if (container_should_be_filtered()) {
334+
events.ringbuf_discard(data, 0);
334335
return 0;
335336
}
336337
@@ -389,28 +390,39 @@
389390
bpf_text += bpf_text_kprobe_body
390391

391392
if args.tid: # TID trumps PID
392-
bpf_text = bpf_text.replace('PID_TID_FILTER',
393+
bpf_text = bpf_text.replace('KPROBE_PID_TID_FILTER',
393394
'if (tid != %s) { return 0; }' % args.tid)
395+
bpf_text = bpf_text.replace('KFUNC_PID_TID_FILTER',
396+
'if (tid != %s) { events.ringbuf_discard(data, 0); return 0; }' % args.tid)
394397
elif args.pid:
395-
bpf_text = bpf_text.replace('PID_TID_FILTER',
398+
bpf_text = bpf_text.replace('KPROBE_PID_TID_FILTER',
396399
'if (pid != %s) { return 0; }' % args.pid)
400+
bpf_text = bpf_text.replace('KFUNC_PID_TID_FILTER',
401+
'if (pid != %s) { events.ringbuf_discard(data, 0); return 0; }' % args.pid)
397402
else:
398-
bpf_text = bpf_text.replace('PID_TID_FILTER', '')
403+
bpf_text = bpf_text.replace('KPROBE_PID_TID_FILTER', '')
404+
bpf_text = bpf_text.replace('KFUNC_PID_TID_FILTER', '')
399405
if args.uid:
400-
bpf_text = bpf_text.replace('UID_FILTER',
406+
bpf_text = bpf_text.replace('KPROBE_UID_FILTER',
401407
'if (uid != %s) { return 0; }' % args.uid)
408+
bpf_text = bpf_text.replace('KFUNC_UID_FILTER',
409+
'if (uid != %s) { events.ringbuf_discard(data, 0); return 0; }' % args.uid)
402410
else:
403-
bpf_text = bpf_text.replace('UID_FILTER', '')
411+
bpf_text = bpf_text.replace('KPROBE_UID_FILTER', '')
412+
bpf_text = bpf_text.replace('KFUNC_UID_FILTER', '')
404413
if args.buffer_pages:
405414
bpf_text = bpf_text.replace('BUFFER_PAGES', '%s' % args.buffer_pages)
406415
else:
407416
bpf_text = bpf_text.replace('BUFFER_PAGES', '%d' % 64)
408417
bpf_text = filter_by_containers(args) + bpf_text
409418
if args.flag_filter:
410-
bpf_text = bpf_text.replace('FLAGS_FILTER',
419+
bpf_text = bpf_text.replace('KPROBE_FLAGS_FILTER',
411420
'if (!(flags & %d)) { return 0; }' % flag_filter_mask)
421+
bpf_text = bpf_text.replace('KFUNC_FLAGS_FILTER',
422+
'if (!(flags & %d)) { events.ringbuf_discard(data, 0); return 0; }' % flag_filter_mask)
412423
else:
413-
bpf_text = bpf_text.replace('FLAGS_FILTER', '')
424+
bpf_text = bpf_text.replace('KPROBE_FLAGS_FILTER', '')
425+
bpf_text = bpf_text.replace('KFUNC_FLAGS_FILTER', '')
414426
if not (args.extended_fields or args.flag_filter):
415427
bpf_text = '\n'.join(x for x in bpf_text.split('\n')
416428
if 'EXTENDED_STRUCT_MEMBER' not in x)

0 commit comments

Comments
 (0)