Skip to content

Commit 9220c3e

Browse files
Jinghao JiaAlexei Starovoitov
authored andcommitted
samples/bpf: syscall_tp_user: Fix array out-of-bound access
Commit 06744f2 ("samples/bpf: Add openat2() enter/exit tracepoint to syscall_tp sample") added two more eBPF programs to support the openat2() syscall. However, it did not increase the size of the array that holds the corresponding bpf_links. This leads to an out-of-bound access on that array in the bpf_object__for_each_program loop and could corrupt other variables on the stack. On our testing QEMU, it corrupts the map1_fds array and causes the sample to fail: # ./syscall_tp prog #0: map ids 4 5 verify map:4 val: 5 map_lookup failed: Bad file descriptor Dynamically allocate the array based on the number of programs reported by libbpf to prevent similar inconsistencies in the future Fixes: 06744f2 ("samples/bpf: Add openat2() enter/exit tracepoint to syscall_tp sample") Signed-off-by: Jinghao Jia <jinghao@linux.ibm.com> Signed-off-by: Ruowen Qin <ruowenq2@illinois.edu> Signed-off-by: Jinghao Jia <jinghao7@illinois.edu> Link: https://lore.kernel.org/r/20230917214220.637721-4-jinghao7@illinois.edu Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 0ee352f commit 9220c3e

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

samples/bpf/syscall_tp_user.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static void verify_map(int map_id)
4848
static int test(char *filename, int nr_tests)
4949
{
5050
int map0_fds[nr_tests], map1_fds[nr_tests], fd, i, j = 0;
51-
struct bpf_link *links[nr_tests * 4];
51+
struct bpf_link **links = NULL;
5252
struct bpf_object *objs[nr_tests];
5353
struct bpf_program *prog;
5454

@@ -60,6 +60,19 @@ static int test(char *filename, int nr_tests)
6060
goto cleanup;
6161
}
6262

63+
/* One-time initialization */
64+
if (!links) {
65+
int nr_progs = 0;
66+
67+
bpf_object__for_each_program(prog, objs[i])
68+
nr_progs += 1;
69+
70+
links = calloc(nr_progs * nr_tests, sizeof(struct bpf_link *));
71+
72+
if (!links)
73+
goto cleanup;
74+
}
75+
6376
/* load BPF program */
6477
if (bpf_object__load(objs[i])) {
6578
fprintf(stderr, "loading BPF object file failed\n");
@@ -107,8 +120,12 @@ static int test(char *filename, int nr_tests)
107120
}
108121

109122
cleanup:
110-
for (j--; j >= 0; j--)
111-
bpf_link__destroy(links[j]);
123+
if (links) {
124+
for (j--; j >= 0; j--)
125+
bpf_link__destroy(links[j]);
126+
127+
free(links);
128+
}
112129

113130
for (i--; i >= 0; i--)
114131
bpf_object__close(objs[i]);

0 commit comments

Comments
 (0)