xref: /linux/tools/testing/selftests/bpf/verifier/pseudo_func.c (revision 14c5eb685cdefbd32e73d2723071ecbd8effbce9)
1 /*
2  * Buggy verifier accepted the program below while not patching BPF_PSEUDO_FUNC
3  * load instruction to contain a real address. Which resulted in a function call
4  * to a bogus address.
5  */
6 {
7 	"BPF_PSEUDO_FUNC reference to the main program",
8 	.insns = {
9 	/* r6 = bpf_map_lookup_elem(&timer_map, &(int){0}); */
10 	BPF_ST_MEM(BPF_W, BPF_REG_10, -4, 0),
11 	BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
12 	BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
13 	BPF_LD_MAP_FD(BPF_REG_1, 0),
14 	BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),
15 	BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 10),
16 	BPF_MOV64_REG(BPF_REG_6, BPF_REG_0),
17 	/* bpf_timer_init(r6, &timer_map, 0); */
18 	BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
19 	BPF_LD_MAP_FD(BPF_REG_2, 0),
20 	BPF_MOV64_IMM(BPF_REG_3, 0),
21 	BPF_EMIT_CALL(BPF_FUNC_timer_init),
22 	/* bpf_timer_set_callback(r6, <insn #0>); */
23 	BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
24 	BPF_RAW_INSN(BPF_LD | BPF_IMM | BPF_DW, BPF_REG_2, BPF_PSEUDO_FUNC, 0, -15),
25 	BPF_RAW_INSN(0, 0, 0, 0, 0),
26 	BPF_EMIT_CALL(BPF_FUNC_timer_set_callback),
27 	BPF_MOV64_IMM(BPF_REG_0, 0),
28 	BPF_EXIT_INSN(),
29 	},
30 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
31 	.fixup_map_timer = { 3, 9 },
32 	.result = REJECT,
33 	.errstr = "callback function cannot be the main program",
34 	.func_info = { { 0, 4 /* main_prog */ } },
35 	.func_info_cnt = 1,
36 	.btf_strings = "\0int\0ctx\0main_prog",
37 	.btf_types = {
38 	/* 1: int            */ BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4),
39 	/* 2: void*          */ BTF_PTR_ENC(0),
40 	/* 3: int __(void *) */ BTF_FUNC_PROTO_ENC(1, 1),
41 				BTF_FUNC_PROTO_ARG_ENC(5, 2),
42 	/* 4: main_prog      */ BTF_FUNC_ENC(9, 3),
43 	BTF_END_RAW
44 	}
45 },
46