Skip to content

Commit f1f5d6f

Browse files
eddyz87Alexei Starovoitov
authored andcommitted
selftests/bpf: ptr_to_btf_id struct walk ending with primitive pointer
Validate that reading a PTR_TO_BTF_ID field produces a value of type PTR_TO_MEM|MEM_RDONLY|PTR_UNTRUSTED, if field is a pointer to a primitive type. Acked-by: Kumar Kartikeya Dwivedi <[email protected]> Signed-off-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 2d5c91e commit f1f5d6f

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,37 @@
55
#include "bpf_misc.h"
66
#include "../test_kmods/bpf_testmod_kfunc.h"
77

8+
SEC("tp_btf/sys_enter")
9+
__success
10+
__log_level(2)
11+
__msg("r8 = *(u64 *)(r7 +0) ; R7_w=ptr_nameidata(off={{[0-9]+}}) R8_w=rdonly_untrusted_mem(sz=0)")
12+
__msg("r9 = *(u8 *)(r8 +0) ; R8_w=rdonly_untrusted_mem(sz=0) R9_w=scalar")
13+
int btf_id_to_ptr_mem(void *ctx)
14+
{
15+
struct task_struct *task;
16+
struct nameidata *idata;
17+
u64 ret, off;
18+
19+
task = bpf_get_current_task_btf();
20+
idata = task->nameidata;
21+
off = bpf_core_field_offset(struct nameidata, pathname);
22+
/*
23+
* asm block to have reliable match target for __msg, equivalent of:
24+
* ret = task->nameidata->pathname[0];
25+
*/
26+
asm volatile (
27+
"r7 = %[idata];"
28+
"r7 += %[off];"
29+
"r8 = *(u64 *)(r7 + 0);"
30+
"r9 = *(u8 *)(r8 + 0);"
31+
"%[ret] = r9;"
32+
: [ret]"=r"(ret)
33+
: [idata]"r"(idata),
34+
[off]"r"(off)
35+
: "r7", "r8", "r9");
36+
return ret;
37+
}
38+
839
SEC("socket")
940
__success
1041
__retval(0)

0 commit comments

Comments
 (0)