Skip to content

Commit 2e3f066

Browse files
borkmannAlexei Starovoitov
authored andcommitted
selftests/bpf: Add a test case to write strtol result into .rodata
Add a test case which attempts to write into .rodata section of the BPF program, and for comparison this adds test cases also for .bss and .data section. Before fix: # ./vmtest.sh -- ./test_progs -t verifier_const [...] ./test_progs -t verifier_const tester_init:PASS:tester_log_buf 0 nsec process_subtest:PASS:obj_open_mem 0 nsec process_subtest:PASS:specs_alloc 0 nsec run_subtest:PASS:obj_open_mem 0 nsec run_subtest:FAIL:unexpected_load_success unexpected success: 0 torvalds#465/1 verifier_const/rodata: write rejected:FAIL torvalds#465/2 verifier_const/bss: write accepted:OK torvalds#465/3 verifier_const/data: write accepted:OK torvalds#465 verifier_const:FAIL [...] After fix: # ./vmtest.sh -- ./test_progs -t verifier_const [...] ./test_progs -t verifier_const torvalds#465/1 verifier_const/rodata: write rejected:OK torvalds#465/2 verifier_const/bss: write accepted:OK torvalds#465/3 verifier_const/data: write accepted:OK torvalds#465 verifier_const:OK [...] Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Shung-Hsi Yu <shung-hsi.yu@suse.com> Acked-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/r/20240913191754.13290-8-daniel@iogearbox.net Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent b073b82 commit 2e3f066

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "verifier_cgroup_inv_retcode.skel.h"
2222
#include "verifier_cgroup_skb.skel.h"
2323
#include "verifier_cgroup_storage.skel.h"
24+
#include "verifier_const.skel.h"
2425
#include "verifier_const_or.skel.h"
2526
#include "verifier_ctx.skel.h"
2627
#include "verifier_ctx_sk_msg.skel.h"
@@ -146,6 +147,7 @@ void test_verifier_cfg(void) { RUN(verifier_cfg); }
146147
void test_verifier_cgroup_inv_retcode(void) { RUN(verifier_cgroup_inv_retcode); }
147148
void test_verifier_cgroup_skb(void) { RUN(verifier_cgroup_skb); }
148149
void test_verifier_cgroup_storage(void) { RUN(verifier_cgroup_storage); }
150+
void test_verifier_const(void) { RUN(verifier_const); }
149151
void test_verifier_const_or(void) { RUN(verifier_const_or); }
150152
void test_verifier_ctx(void) { RUN(verifier_ctx); }
151153
void test_verifier_ctx_sk_msg(void) { RUN(verifier_ctx_sk_msg); }
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2024 Isovalent */
3+
4+
#include <linux/bpf.h>
5+
#include <bpf/bpf_helpers.h>
6+
#include "bpf_misc.h"
7+
8+
const volatile long foo = 42;
9+
long bar;
10+
long bart = 96;
11+
12+
SEC("tc/ingress")
13+
__description("rodata: write rejected")
14+
__failure __msg("write into map forbidden")
15+
int tcx1(struct __sk_buff *skb)
16+
{
17+
char buff[] = { '8', '4', '\0' };
18+
bpf_strtol(buff, sizeof(buff), 0, (long *)&foo);
19+
return TCX_PASS;
20+
}
21+
22+
SEC("tc/ingress")
23+
__description("bss: write accepted")
24+
__success
25+
int tcx2(struct __sk_buff *skb)
26+
{
27+
char buff[] = { '8', '4', '\0' };
28+
bpf_strtol(buff, sizeof(buff), 0, &bar);
29+
return TCX_PASS;
30+
}
31+
32+
SEC("tc/ingress")
33+
__description("data: write accepted")
34+
__success
35+
int tcx3(struct __sk_buff *skb)
36+
{
37+
char buff[] = { '8', '4', '\0' };
38+
bpf_strtol(buff, sizeof(buff), 0, &bart);
39+
return TCX_PASS;
40+
}
41+
42+
char LICENSE[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)