Skip to content

Commit 0f4860e

Browse files
committed
selftests/bpf: check operations on untrusted ro pointers to mem
The following cases are tested: - it is ok to load memory at any offset from rdonly_untrusted_mem; - rdonly_untrusted_mem offset/bounds are not tracked; - writes into rdonly_untrusted_mem are forbidden; - atomic operations on rdonly_untrusted_mem are forbidden; - rdonly_untrusted_mem can't be passed as a memory argument of a helper of kfunc; - it is ok to use PTR_TO_MEM and PTR_TO_BTF_ID in a same load instruction. Signed-off-by: Eduard Zingerman <[email protected]>
1 parent 8d60da4 commit 0f4860e

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-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
@@ -57,6 +57,7 @@
5757
#include "verifier_may_goto_1.skel.h"
5858
#include "verifier_may_goto_2.skel.h"
5959
#include "verifier_meta_access.skel.h"
60+
#include "verifier_mem_rdonly_untrusted.skel.h"
6061
#include "verifier_movsx.skel.h"
6162
#include "verifier_mtu.skel.h"
6263
#include "verifier_netfilter_ctx.skel.h"
@@ -205,6 +206,7 @@ void test_verifier_prevent_map_lookup(void) { RUN(verifier_prevent_map_lookup)
205206
void test_verifier_private_stack(void) { RUN(verifier_private_stack); }
206207
void test_verifier_raw_stack(void) { RUN(verifier_raw_stack); }
207208
void test_verifier_raw_tp_writable(void) { RUN(verifier_raw_tp_writable); }
209+
void test_verifier_mem_rdonly_untrusted(void) { RUN_FULL_CAPS(verifier_mem_rdonly_untrusted); }
208210
void test_verifier_reg_equal(void) { RUN(verifier_reg_equal); }
209211
void test_verifier_ref_tracking(void) { RUN(verifier_ref_tracking); }
210212
void test_verifier_regalloc(void) { RUN(verifier_regalloc); }
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <vmlinux.h>
4+
#include <bpf/bpf_core_read.h>
5+
#include "bpf_misc.h"
6+
#include "../test_kmods/bpf_testmod_kfunc.h"
7+
8+
SEC("socket")
9+
__success
10+
__retval(0)
11+
int ldx_is_ok_bad_addr(void *ctx)
12+
{
13+
char *p;
14+
15+
if (!bpf_core_enum_value_exists(enum bpf_features, BPF_FEAT_RDONLY_CAST_TO_VOID))
16+
return 42;
17+
18+
p = bpf_rdonly_cast(0, 0);
19+
return p[0x7fff];
20+
}
21+
22+
SEC("socket")
23+
__success
24+
__retval(1)
25+
int ldx_is_ok_good_addr(void *ctx)
26+
{
27+
int v, *p;
28+
29+
v = 1;
30+
p = bpf_rdonly_cast(&v, 0);
31+
return *p;
32+
}
33+
34+
SEC("socket")
35+
__success
36+
int offset_not_tracked(void *ctx)
37+
{
38+
int *p, i, s;
39+
40+
p = bpf_rdonly_cast(0, 0);
41+
s = 0;
42+
bpf_for(i, 0, 1000 * 1000 * 1000) {
43+
p++;
44+
s += *p;
45+
}
46+
return s;
47+
}
48+
49+
SEC("socket")
50+
__failure
51+
__msg("cannot write into rdonly_untrusted_mem")
52+
int stx_not_ok(void *ctx)
53+
{
54+
int v, *p;
55+
56+
p = bpf_rdonly_cast(&v, 0);
57+
*p = 1;
58+
return 0;
59+
}
60+
61+
SEC("socket")
62+
__failure
63+
__msg("cannot write into rdonly_untrusted_mem")
64+
int atomic_not_ok(void *ctx)
65+
{
66+
int v, *p;
67+
68+
p = bpf_rdonly_cast(&v, 0);
69+
__sync_fetch_and_add(p, 1);
70+
return 0;
71+
}
72+
73+
SEC("socket")
74+
__failure
75+
__msg("cannot write into rdonly_untrusted_mem")
76+
int atomic_rmw_not_ok(void *ctx)
77+
{
78+
long v, *p;
79+
80+
p = bpf_rdonly_cast(&v, 0);
81+
return __sync_val_compare_and_swap(p, 0, 42);
82+
}
83+
84+
SEC("socket")
85+
__failure
86+
__msg("invalid access to memory, mem_size=0 off=0 size=4")
87+
__msg("R1 min value is outside of the allowed memory range")
88+
int kfunc_param_not_ok(void *ctx)
89+
{
90+
int *p;
91+
92+
p = bpf_rdonly_cast(0, 0);
93+
bpf_kfunc_trusted_num_test(p);
94+
return 0;
95+
}
96+
97+
SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
98+
__failure
99+
__msg("R1 type=rdonly_untrusted_mem expected=")
100+
int helper_param_not_ok(void *ctx)
101+
{
102+
char *p;
103+
104+
p = bpf_rdonly_cast(0, 0);
105+
/*
106+
* Any helper with ARG_CONST_SIZE_OR_ZERO constraint will do,
107+
* the most permissive constraint
108+
*/
109+
bpf_copy_from_user(p, 0, (void *)42);
110+
return 0;
111+
}
112+
113+
static __noinline u64 *get_some_addr(void)
114+
{
115+
if (bpf_get_prandom_u32())
116+
return bpf_rdonly_cast(0, bpf_core_type_id_kernel(struct sock));
117+
else
118+
return bpf_rdonly_cast(0, 0);
119+
}
120+
121+
SEC("socket")
122+
__success
123+
__retval(0)
124+
int mixed_mem_type(void *ctx)
125+
{
126+
u64 *p;
127+
128+
/* Try to avoid compiler hoisting load to if branches by using __noinline func. */
129+
p = get_some_addr();
130+
return *p;
131+
}
132+
133+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)