Skip to content

Commit 4e99d11

Browse files
committed
Merge branch 'RISC-V selftest/bpf fixes'
Björn Töpel says: ==================== This series contain some fixes for selftests/bpf when building/running on a RISC-V host. Details can be found in each individual commit. v2: Makefile cosmetics. (Andrii) Simplified unpriv check and added comment. (Andrii) ==================== Signed-off-by: Andrii Nakryiko <[email protected]>
2 parents d055126 + 6007b23 commit 4e99d11

File tree

9 files changed

+67
-19
lines changed

9 files changed

+67
-19
lines changed

tools/testing/selftests/bpf/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ $(RESOLVE_BTFIDS): $(BPFOBJ) | $(BUILD_DIR)/resolve_btfids \
219219
# build would have failed anyways.
220220
define get_sys_includes
221221
$(shell $(1) -v -E - </dev/null 2>&1 \
222-
| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')
222+
| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
223+
$(shell $(1) -dM -E - </dev/null | grep '#define __riscv_xlen ' | sed 's/#define /-D/' | sed 's/ /=/')
223224
endef
224225

225226
# Determine target endianness.

tools/testing/selftests/bpf/test_verifier.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,19 @@ static void get_unpriv_disabled()
11521152

11531153
static bool test_as_unpriv(struct bpf_test *test)
11541154
{
1155+
#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1156+
/* Some architectures have strict alignment requirements. In
1157+
* that case, the BPF verifier detects if a program has
1158+
* unaligned accesses and rejects them. A user can pass
1159+
* BPF_F_ANY_ALIGNMENT to a program to override this
1160+
* check. That, however, will only work when a privileged user
1161+
* loads a program. An unprivileged user loading a program
1162+
* with this flag will be rejected prior entering the
1163+
* verifier.
1164+
*/
1165+
if (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS)
1166+
return false;
1167+
#endif
11551168
return !test->prog_type ||
11561169
test->prog_type == BPF_PROG_TYPE_SOCKET_FILTER ||
11571170
test->prog_type == BPF_PROG_TYPE_CGROUP_SKB;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@
266266
.result = REJECT,
267267
.prog_type = BPF_PROG_TYPE_SK_LOOKUP,
268268
.expected_attach_type = BPF_SK_LOOKUP,
269+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
269270
},
270271
{
271272
"invalid 8-byte read from bpf_sk_lookup remote_ip4 field",
@@ -292,6 +293,7 @@
292293
.result = REJECT,
293294
.prog_type = BPF_PROG_TYPE_SK_LOOKUP,
294295
.expected_attach_type = BPF_SK_LOOKUP,
296+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
295297
},
296298
{
297299
"invalid 8-byte read from bpf_sk_lookup remote_port field",
@@ -305,6 +307,7 @@
305307
.result = REJECT,
306308
.prog_type = BPF_PROG_TYPE_SK_LOOKUP,
307309
.expected_attach_type = BPF_SK_LOOKUP,
310+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
308311
},
309312
{
310313
"invalid 8-byte read from bpf_sk_lookup local_ip4 field",
@@ -331,6 +334,7 @@
331334
.result = REJECT,
332335
.prog_type = BPF_PROG_TYPE_SK_LOOKUP,
333336
.expected_attach_type = BPF_SK_LOOKUP,
337+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
334338
},
335339
{
336340
"invalid 8-byte read from bpf_sk_lookup local_port field",
@@ -344,6 +348,7 @@
344348
.result = REJECT,
345349
.prog_type = BPF_PROG_TYPE_SK_LOOKUP,
346350
.expected_attach_type = BPF_SK_LOOKUP,
351+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
347352
},
348353
/* invalid 1,2,4-byte reads from 8-byte fields in bpf_sk_lookup */
349354
{
@@ -410,6 +415,7 @@
410415
.result = REJECT,
411416
.prog_type = BPF_PROG_TYPE_SK_LOOKUP,
412417
.expected_attach_type = BPF_SK_LOOKUP,
418+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
413419
},
414420
{
415421
"invalid 4-byte unaligned read from bpf_sk_lookup at even offset",
@@ -422,6 +428,7 @@
422428
.result = REJECT,
423429
.prog_type = BPF_PROG_TYPE_SK_LOOKUP,
424430
.expected_attach_type = BPF_SK_LOOKUP,
431+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
425432
},
426433
/* in-bound and out-of-bound writes to bpf_sk_lookup */
427434
{

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
.fixup_map_array_48b = { 1 },
7070
.result = REJECT,
7171
.errstr = "R1 min value is outside of the allowed memory range",
72+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
7273
},
7374
{
7475
"direct map access, write test 7",
@@ -195,6 +196,7 @@
195196
.fixup_map_array_48b = { 1, 3 },
196197
.result = REJECT,
197198
.errstr = "invalid access to map value, value_size=48 off=47 size=2",
199+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
198200
},
199201
{
200202
"direct map access, write test 17",
@@ -209,6 +211,7 @@
209211
.fixup_map_array_48b = { 1, 3 },
210212
.result = REJECT,
211213
.errstr = "invalid access to map value, value_size=48 off=47 size=2",
214+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
212215
},
213216
{
214217
"direct map access, write test 18",

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
.errstr_unpriv = "bpf_array access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN",
4545
.result = REJECT,
4646
.errstr = "cannot access ptr member ops with moff 0 in struct bpf_map with off 1 size 4",
47+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
4748
},
4849
{
4950
"bpf_map_ptr: read ops field accepted",

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@
3131
.fixup_map_hash_8b = { 1, },
3232
.prog_type = BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE,
3333
.errstr = "R6 invalid variable buffer offset: off=0, var_off=(0x0; 0xffffffff)",
34+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
3435
},

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@
675675
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
676676
.result = REJECT,
677677
.errstr = "invalid mem access",
678+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
678679
},
679680
{
680681
"reference tracking: use ptr from bpf_sk_fullsock() after release",
@@ -698,6 +699,7 @@
698699
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
699700
.result = REJECT,
700701
.errstr = "invalid mem access",
702+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
701703
},
702704
{
703705
"reference tracking: use ptr from bpf_sk_fullsock(tp) after release",
@@ -725,6 +727,7 @@
725727
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
726728
.result = REJECT,
727729
.errstr = "invalid mem access",
730+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
728731
},
729732
{
730733
"reference tracking: use sk after bpf_sk_release(tp)",
@@ -747,6 +750,7 @@
747750
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
748751
.result = REJECT,
749752
.errstr = "invalid mem access",
753+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
750754
},
751755
{
752756
"reference tracking: use ptr from bpf_get_listener_sock() after bpf_sk_release(sk)",

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
.fixup_map_hash_48b = { 4 },
2222
.result = ACCEPT,
2323
.prog_type = BPF_PROG_TYPE_TRACEPOINT,
24+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
2425
},
2526
{
2627
"regalloc negative",
@@ -71,6 +72,7 @@
7172
.fixup_map_hash_48b = { 4 },
7273
.result = ACCEPT,
7374
.prog_type = BPF_PROG_TYPE_TRACEPOINT,
75+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
7476
},
7577
{
7678
"regalloc src_reg negative",
@@ -97,6 +99,7 @@
9799
.result = REJECT,
98100
.errstr = "invalid access to map value, value_size=48 off=44 size=8",
99101
.prog_type = BPF_PROG_TYPE_TRACEPOINT,
102+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
100103
},
101104
{
102105
"regalloc and spill",
@@ -126,6 +129,7 @@
126129
.fixup_map_hash_48b = { 4 },
127130
.result = ACCEPT,
128131
.prog_type = BPF_PROG_TYPE_TRACEPOINT,
132+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
129133
},
130134
{
131135
"regalloc and spill negative",
@@ -156,6 +160,7 @@
156160
.result = REJECT,
157161
.errstr = "invalid access to map value, value_size=48 off=48 size=8",
158162
.prog_type = BPF_PROG_TYPE_TRACEPOINT,
163+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
159164
},
160165
{
161166
"regalloc three regs",
@@ -182,6 +187,7 @@
182187
.fixup_map_hash_48b = { 4 },
183188
.result = ACCEPT,
184189
.prog_type = BPF_PROG_TYPE_TRACEPOINT,
190+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
185191
},
186192
{
187193
"regalloc after call",
@@ -210,6 +216,7 @@
210216
.fixup_map_hash_48b = { 4 },
211217
.result = ACCEPT,
212218
.prog_type = BPF_PROG_TYPE_TRACEPOINT,
219+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
213220
},
214221
{
215222
"regalloc in callee",
@@ -240,6 +247,7 @@
240247
.fixup_map_hash_48b = { 4 },
241248
.result = ACCEPT,
242249
.prog_type = BPF_PROG_TYPE_TRACEPOINT,
250+
.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
243251
},
244252
{
245253
"regalloc, spill, JEQ",
Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#define BPF_SOCK_ADDR_STORE(field, off, res, err) \
1+
#define BPF_SOCK_ADDR_STORE(field, off, res, err, flgs) \
22
{ \
33
"wide store to bpf_sock_addr." #field "[" #off "]", \
44
.insns = { \
@@ -11,31 +11,36 @@
1111
.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR, \
1212
.expected_attach_type = BPF_CGROUP_UDP6_SENDMSG, \
1313
.errstr = err, \
14+
.flags = flgs, \
1415
}
1516

1617
/* user_ip6[0] is u64 aligned */
1718
BPF_SOCK_ADDR_STORE(user_ip6, 0, ACCEPT,
18-
NULL),
19+
NULL, 0),
1920
BPF_SOCK_ADDR_STORE(user_ip6, 1, REJECT,
20-
"invalid bpf_context access off=12 size=8"),
21+
"invalid bpf_context access off=12 size=8",
22+
F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
2123
BPF_SOCK_ADDR_STORE(user_ip6, 2, ACCEPT,
22-
NULL),
24+
NULL, 0),
2325
BPF_SOCK_ADDR_STORE(user_ip6, 3, REJECT,
24-
"invalid bpf_context access off=20 size=8"),
26+
"invalid bpf_context access off=20 size=8",
27+
F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
2528

2629
/* msg_src_ip6[0] is _not_ u64 aligned */
2730
BPF_SOCK_ADDR_STORE(msg_src_ip6, 0, REJECT,
28-
"invalid bpf_context access off=44 size=8"),
31+
"invalid bpf_context access off=44 size=8",
32+
F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
2933
BPF_SOCK_ADDR_STORE(msg_src_ip6, 1, ACCEPT,
30-
NULL),
34+
NULL, 0),
3135
BPF_SOCK_ADDR_STORE(msg_src_ip6, 2, REJECT,
32-
"invalid bpf_context access off=52 size=8"),
36+
"invalid bpf_context access off=52 size=8",
37+
F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
3338
BPF_SOCK_ADDR_STORE(msg_src_ip6, 3, REJECT,
34-
"invalid bpf_context access off=56 size=8"),
39+
"invalid bpf_context access off=56 size=8", 0),
3540

3641
#undef BPF_SOCK_ADDR_STORE
3742

38-
#define BPF_SOCK_ADDR_LOAD(field, off, res, err) \
43+
#define BPF_SOCK_ADDR_LOAD(field, off, res, err, flgs) \
3944
{ \
4045
"wide load from bpf_sock_addr." #field "[" #off "]", \
4146
.insns = { \
@@ -48,26 +53,31 @@ BPF_SOCK_ADDR_STORE(msg_src_ip6, 3, REJECT,
4853
.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR, \
4954
.expected_attach_type = BPF_CGROUP_UDP6_SENDMSG, \
5055
.errstr = err, \
56+
.flags = flgs, \
5157
}
5258

5359
/* user_ip6[0] is u64 aligned */
5460
BPF_SOCK_ADDR_LOAD(user_ip6, 0, ACCEPT,
55-
NULL),
61+
NULL, 0),
5662
BPF_SOCK_ADDR_LOAD(user_ip6, 1, REJECT,
57-
"invalid bpf_context access off=12 size=8"),
63+
"invalid bpf_context access off=12 size=8",
64+
F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
5865
BPF_SOCK_ADDR_LOAD(user_ip6, 2, ACCEPT,
59-
NULL),
66+
NULL, 0),
6067
BPF_SOCK_ADDR_LOAD(user_ip6, 3, REJECT,
61-
"invalid bpf_context access off=20 size=8"),
68+
"invalid bpf_context access off=20 size=8",
69+
F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
6270

6371
/* msg_src_ip6[0] is _not_ u64 aligned */
6472
BPF_SOCK_ADDR_LOAD(msg_src_ip6, 0, REJECT,
65-
"invalid bpf_context access off=44 size=8"),
73+
"invalid bpf_context access off=44 size=8",
74+
F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
6675
BPF_SOCK_ADDR_LOAD(msg_src_ip6, 1, ACCEPT,
67-
NULL),
76+
NULL, 0),
6877
BPF_SOCK_ADDR_LOAD(msg_src_ip6, 2, REJECT,
69-
"invalid bpf_context access off=52 size=8"),
78+
"invalid bpf_context access off=52 size=8",
79+
F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
7080
BPF_SOCK_ADDR_LOAD(msg_src_ip6, 3, REJECT,
71-
"invalid bpf_context access off=56 size=8"),
81+
"invalid bpf_context access off=56 size=8", 0),
7282

7383
#undef BPF_SOCK_ADDR_LOAD

0 commit comments

Comments
 (0)