Skip to content

Commit 37c8d48

Browse files
committed
bpf, selftests: Add ringbuf memory type confusion test
Add two tests, one which asserts that ring buffer memory can be passed to other helpers for populating its entry area, and another one where verifier rejects different type of memory passed to bpf_ringbuf_submit(). Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: John Fastabend <[email protected]> Acked-by: Alexei Starovoitov <[email protected]>
1 parent 722e4db commit 37c8d48

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "test_d_path.skel.h"
1212
#include "test_d_path_check_rdonly_mem.skel.h"
13+
#include "test_d_path_check_types.skel.h"
1314

1415
static int duration;
1516

@@ -167,11 +168,24 @@ static void test_d_path_check_rdonly_mem(void)
167168
test_d_path_check_rdonly_mem__destroy(skel);
168169
}
169170

171+
static void test_d_path_check_types(void)
172+
{
173+
struct test_d_path_check_types *skel;
174+
175+
skel = test_d_path_check_types__open_and_load();
176+
ASSERT_ERR_PTR(skel, "unexpected_load_passing_wrong_type");
177+
178+
test_d_path_check_types__destroy(skel);
179+
}
180+
170181
void test_d_path(void)
171182
{
172183
if (test__start_subtest("basic"))
173184
test_d_path_basic();
174185

175186
if (test__start_subtest("check_rdonly_mem"))
176187
test_d_path_check_rdonly_mem();
188+
189+
if (test__start_subtest("check_alloc_mem"))
190+
test_d_path_check_types();
177191
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include "vmlinux.h"
4+
#include <bpf/bpf_helpers.h>
5+
#include <bpf/bpf_tracing.h>
6+
7+
extern const int bpf_prog_active __ksym;
8+
9+
struct {
10+
__uint(type, BPF_MAP_TYPE_RINGBUF);
11+
__uint(max_entries, 1 << 12);
12+
} ringbuf SEC(".maps");
13+
14+
SEC("fentry/security_inode_getattr")
15+
int BPF_PROG(d_path_check_rdonly_mem, struct path *path, struct kstat *stat,
16+
__u32 request_mask, unsigned int query_flags)
17+
{
18+
void *active;
19+
u32 cpu;
20+
21+
cpu = bpf_get_smp_processor_id();
22+
active = (void *)bpf_per_cpu_ptr(&bpf_prog_active, cpu);
23+
if (active) {
24+
/* FAIL here! 'active' points to 'regular' memory. It
25+
* cannot be submitted to ring buffer.
26+
*/
27+
bpf_ringbuf_submit(active, 0);
28+
}
29+
return 0;
30+
}
31+
32+
char _license[] SEC("license") = "GPL";

tools/testing/selftests/bpf/verifier/ringbuf.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
.fixup_map_ringbuf = { 1 },
3030
.result = REJECT,
31-
.errstr = "dereference of modified mem ptr R1",
31+
.errstr = "dereference of modified alloc_mem ptr R1",
3232
},
3333
{
3434
"ringbuf: invalid reservation offset 2",
@@ -62,3 +62,34 @@
6262
.result = REJECT,
6363
.errstr = "R7 min value is outside of the allowed memory range",
6464
},
65+
{
66+
"ringbuf: check passing rb mem to helpers",
67+
.insns = {
68+
BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
69+
/* reserve 8 byte ringbuf memory */
70+
BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
71+
BPF_LD_MAP_FD(BPF_REG_1, 0),
72+
BPF_MOV64_IMM(BPF_REG_2, 8),
73+
BPF_MOV64_IMM(BPF_REG_3, 0),
74+
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_ringbuf_reserve),
75+
BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
76+
/* check whether the reservation was successful */
77+
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
78+
BPF_EXIT_INSN(),
79+
/* pass allocated ring buffer memory to fib lookup */
80+
BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
81+
BPF_MOV64_REG(BPF_REG_2, BPF_REG_0),
82+
BPF_MOV64_IMM(BPF_REG_3, 8),
83+
BPF_MOV64_IMM(BPF_REG_4, 0),
84+
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_fib_lookup),
85+
/* submit the ringbuf memory */
86+
BPF_MOV64_REG(BPF_REG_1, BPF_REG_7),
87+
BPF_MOV64_IMM(BPF_REG_2, 0),
88+
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_ringbuf_submit),
89+
BPF_MOV64_IMM(BPF_REG_0, 0),
90+
BPF_EXIT_INSN(),
91+
},
92+
.fixup_map_ringbuf = { 2 },
93+
.prog_type = BPF_PROG_TYPE_XDP,
94+
.result = ACCEPT,
95+
},

tools/testing/selftests/bpf/verifier/spill_fill.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
},
8585
.fixup_map_ringbuf = { 1 },
8686
.result = REJECT,
87-
.errstr = "R0 pointer arithmetic on mem_or_null prohibited",
87+
.errstr = "R0 pointer arithmetic on alloc_mem_or_null prohibited",
8888
},
8989
{
9090
"check corrupted spill/fill",

0 commit comments

Comments
 (0)