Skip to content

Commit 762f851

Browse files
olsajiriAlexei Starovoitov
authored andcommitted
selftests/bpf: Add verifier test for d_path helper
Adding verifier test for attaching tracing program and calling d_path helper from within and testing that it's allowed for dentry_open function and denied for 'd_path' function with appropriate error. Signed-off-by: Jiri Olsa <jolsa@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Andrii Nakryiko <andriin@fb.com> Link: https://lore.kernel.org/bpf/20200825192124.710397-13-jolsa@kernel.org
1 parent 68a26bc commit 762f851

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

tools/testing/selftests/bpf/test_verifier.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ struct bpf_test {
114114
bpf_testdata_struct_t retvals[MAX_TEST_RUNS];
115115
};
116116
enum bpf_attach_type expected_attach_type;
117+
const char *kfunc;
117118
};
118119

119120
/* Note we want this to be 64 bit aligned so that the end of our array is
@@ -984,8 +985,24 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
984985
attr.log_level = 4;
985986
attr.prog_flags = pflags;
986987

988+
if (prog_type == BPF_PROG_TYPE_TRACING && test->kfunc) {
989+
attr.attach_btf_id = libbpf_find_vmlinux_btf_id(test->kfunc,
990+
attr.expected_attach_type);
991+
if (attr.attach_btf_id < 0) {
992+
printf("FAIL\nFailed to find BTF ID for '%s'!\n",
993+
test->kfunc);
994+
(*errors)++;
995+
return;
996+
}
997+
}
998+
987999
fd_prog = bpf_load_program_xattr(&attr, bpf_vlog, sizeof(bpf_vlog));
988-
if (fd_prog < 0 && !bpf_probe_prog_type(prog_type, 0)) {
1000+
1001+
/* BPF_PROG_TYPE_TRACING requires more setup and
1002+
* bpf_probe_prog_type won't give correct answer
1003+
*/
1004+
if (fd_prog < 0 && prog_type != BPF_PROG_TYPE_TRACING &&
1005+
!bpf_probe_prog_type(prog_type, 0)) {
9891006
printf("SKIP (unsupported program type %d)\n", prog_type);
9901007
skips++;
9911008
goto close_fds;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"d_path accept",
3+
.insns = {
4+
BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_1, 0),
5+
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
6+
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
7+
BPF_MOV64_IMM(BPF_REG_6, 0),
8+
BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_6, 0),
9+
BPF_LD_IMM64(BPF_REG_3, 8),
10+
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_d_path),
11+
BPF_MOV64_IMM(BPF_REG_0, 0),
12+
BPF_EXIT_INSN(),
13+
},
14+
.result = ACCEPT,
15+
.prog_type = BPF_PROG_TYPE_TRACING,
16+
.expected_attach_type = BPF_TRACE_FENTRY,
17+
.kfunc = "dentry_open",
18+
},
19+
{
20+
"d_path reject",
21+
.insns = {
22+
BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_1, 0),
23+
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
24+
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
25+
BPF_MOV64_IMM(BPF_REG_6, 0),
26+
BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_6, 0),
27+
BPF_LD_IMM64(BPF_REG_3, 8),
28+
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_d_path),
29+
BPF_MOV64_IMM(BPF_REG_0, 0),
30+
BPF_EXIT_INSN(),
31+
},
32+
.errstr = "helper call is not allowed in probe",
33+
.result = REJECT,
34+
.prog_type = BPF_PROG_TYPE_TRACING,
35+
.expected_attach_type = BPF_TRACE_FENTRY,
36+
.kfunc = "d_path",
37+
},

0 commit comments

Comments
 (0)