bpf_convert_ctx_accesses() can patch an insn twice: a barrier goes in front of it, and then its ctx access replaces the copy of the insn behind the barrier. bpf_patch_list_add() merges the two into one patch. No test has a second patch with a jump in it, so nothing checks that the jumps of the second patch survive the merge. The barrier needs a prog without CAP_PERFMON, and "nospec, store, nospec" in verifier_unpriv.c has no jump. Load skb->ifindex, which becomes a load, a jump and another load, at an insn that a mispredicted path reaches with a scalar instead of the ctx. Without CAP_PERFMON the verifier puts a barrier in front of it. Check that the jump still skips one insn. Reported-by: Alexei Starovoitov Closes: https://lore.kernel.org/bpf/DMKHZS5B5CNO.186F6YH1T6YU4@gmail.com/ Signed-off-by: Qiliang Yuan --- .../selftests/bpf/progs/verifier_patch_list.c | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tools/testing/selftests/bpf/progs/verifier_patch_list.c b/tools/testing/selftests/bpf/progs/verifier_patch_list.c index 045081936d73d..287b33e17b730 100644 --- a/tools/testing/selftests/bpf/progs/verifier_patch_list.c +++ b/tools/testing/selftests/bpf/progs/verifier_patch_list.c @@ -423,4 +423,43 @@ __naked void patch_list_goto_over_nop_and_insn(void) ::: __clobber_all); } +/* + * Load skb->ifindex where a mispredicted path through l1 has a scalar instead + * of the ctx, so that the verifier puts a barrier in front of the load. The + * load becomes a second patch of 3 insns behind the barrier, and its jump must + * still skip one insn. Only a prog without CAP_PERFMON gets the barrier. + */ +SEC("socket") +__description("patch list: second patch with a jump behind a barrier") +__success __success_unpriv +__caps_unpriv(CAP_BPF) +#ifdef SPEC_V1 +__xlated_unpriv("nospec") +__xlated_unpriv("r0 = *(u64 *)(r2 +{{[0-9]+}})") +__xlated_unpriv("if r0 == 0x0 goto pc+1") +__xlated_unpriv("r0 = *(u32 *)(r0 +{{[0-9]+}})") +__xlated_unpriv("exit") +#endif +__naked void patch_list_second_patch_jump(void) +{ + asm volatile (" \ + r8 = 0; \ + r8 ^= r8; \ + r9 = 0; \ + r9 ^= r9; \ + r0 = 0; \ + r2 = r1; \ + if r8 != 0 goto l1_%=; \ + goto l2_%=; \ +l1_%=: if r9 == 0 goto l3_%=; \ + r2 = r0; \ +l2_%=: r0 = *(u32 *)(r2 + %[ifindex]); \ + goto l4_%=; \ +l3_%=: r0 = 0; \ +l4_%=: exit; \ +" : + : __imm_const(ifindex, offsetof(struct __sk_buff, ifindex)) + : __clobber_all); +} + char _license[] SEC("license") = "GPL"; -- 2.43.0