Skip to content

Commit 3f00e4a

Browse files
Martin KaFai Lauborkmann
authored andcommitted
selftests/bpf: Test racing between bpf_timer_cancel_and_free and bpf_timer_cancel
This selftest is based on a Alexei's test adopted from an internal user to troubleshoot another bug. During this exercise, a separate racing bug was discovered between bpf_timer_cancel_and_free and bpf_timer_cancel. The details can be found in the previous patch. This patch is to add a selftest that can trigger the bug. I can trigger the UAF everytime in my qemu setup with KASAN. The idea is to have multiple user space threads running in a tight loop to exercise both bpf_map_update_elem (which calls into bpf_timer_cancel_and_free) and bpf_timer_cancel. Signed-off-by: Martin KaFai Lau <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Hou Tao <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 0281b91 commit 3f00e4a

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

tools/testing/selftests/bpf/prog_tests/timer.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,29 @@
44
#include "timer.skel.h"
55
#include "timer_failure.skel.h"
66

7+
#define NUM_THR 8
8+
9+
static void *spin_lock_thread(void *arg)
10+
{
11+
int i, err, prog_fd = *(int *)arg;
12+
LIBBPF_OPTS(bpf_test_run_opts, topts);
13+
14+
for (i = 0; i < 10000; i++) {
15+
err = bpf_prog_test_run_opts(prog_fd, &topts);
16+
if (!ASSERT_OK(err, "test_run_opts err") ||
17+
!ASSERT_OK(topts.retval, "test_run_opts retval"))
18+
break;
19+
}
20+
21+
pthread_exit(arg);
22+
}
23+
724
static int timer(struct timer *timer_skel)
825
{
9-
int err, prog_fd;
26+
int i, err, prog_fd;
1027
LIBBPF_OPTS(bpf_test_run_opts, topts);
28+
pthread_t thread_id[NUM_THR];
29+
void *ret;
1130

1231
err = timer__attach(timer_skel);
1332
if (!ASSERT_OK(err, "timer_attach"))
@@ -43,6 +62,20 @@ static int timer(struct timer *timer_skel)
4362
/* check that code paths completed */
4463
ASSERT_EQ(timer_skel->bss->ok, 1 | 2 | 4, "ok");
4564

65+
prog_fd = bpf_program__fd(timer_skel->progs.race);
66+
for (i = 0; i < NUM_THR; i++) {
67+
err = pthread_create(&thread_id[i], NULL,
68+
&spin_lock_thread, &prog_fd);
69+
if (!ASSERT_OK(err, "pthread_create"))
70+
break;
71+
}
72+
73+
while (i) {
74+
err = pthread_join(thread_id[--i], &ret);
75+
if (ASSERT_OK(err, "pthread_join"))
76+
ASSERT_EQ(ret, (void *)&prog_fd, "pthread_join");
77+
}
78+
4679
return 0;
4780
}
4881

tools/testing/selftests/bpf/progs/timer.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ struct {
5151
__uint(max_entries, 1);
5252
__type(key, int);
5353
__type(value, struct elem);
54-
} abs_timer SEC(".maps"), soft_timer_pinned SEC(".maps"), abs_timer_pinned SEC(".maps");
54+
} abs_timer SEC(".maps"), soft_timer_pinned SEC(".maps"), abs_timer_pinned SEC(".maps"),
55+
race_array SEC(".maps");
5556

5657
__u64 bss_data;
5758
__u64 abs_data;
@@ -390,3 +391,34 @@ int BPF_PROG2(test5, int, a)
390391

391392
return 0;
392393
}
394+
395+
static int race_timer_callback(void *race_array, int *race_key, struct bpf_timer *timer)
396+
{
397+
bpf_timer_start(timer, 1000000, 0);
398+
return 0;
399+
}
400+
401+
SEC("syscall")
402+
int race(void *ctx)
403+
{
404+
struct bpf_timer *timer;
405+
int err, race_key = 0;
406+
struct elem init;
407+
408+
__builtin_memset(&init, 0, sizeof(struct elem));
409+
bpf_map_update_elem(&race_array, &race_key, &init, BPF_ANY);
410+
411+
timer = bpf_map_lookup_elem(&race_array, &race_key);
412+
if (!timer)
413+
return 1;
414+
415+
err = bpf_timer_init(timer, &race_array, CLOCK_MONOTONIC);
416+
if (err && err != -EBUSY)
417+
return 1;
418+
419+
bpf_timer_set_callback(timer, race_timer_callback);
420+
bpf_timer_start(timer, 0, 0);
421+
bpf_timer_cancel(timer);
422+
423+
return 0;
424+
}

0 commit comments

Comments
 (0)