Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf, x86: Fall back to interpreter mode when extra pass fails

Extra pass for subprog jit may fail (e.g. due to bpf_jit_harden race),
but bpf_func is not cleared for the subprog and jit_subprogs will
succeed. The running of the bpf program may lead to oops because the
memory for the jited subprog image has already been freed.

So fall back to interpreter mode by clearing bpf_func/jited/jited_len
when extra pass fails.

Signed-off-by: Hou Tao <houtao1@huawei.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20220309123321.2400262-2-houtao1@huawei.com

authored by

Hou Tao and committed by
Alexei Starovoitov
73e14451 aaccdf9c

+9 -2
+9 -2
arch/x86/net/bpf_jit_comp.c
··· 2335 2335 sizeof(rw_header->size)); 2336 2336 bpf_jit_binary_pack_free(header, rw_header); 2337 2337 } 2338 + /* Fall back to interpreter mode */ 2338 2339 prog = orig_prog; 2340 + if (extra_pass) { 2341 + prog->bpf_func = NULL; 2342 + prog->jited = 0; 2343 + prog->jited_len = 0; 2344 + } 2339 2345 goto out_addrs; 2340 2346 } 2341 2347 if (image) { ··· 2390 2384 * Both cases are serious bugs and justify WARN_ON. 2391 2385 */ 2392 2386 if (WARN_ON(bpf_jit_binary_pack_finalize(prog, header, rw_header))) { 2393 - prog = orig_prog; 2394 - goto out_addrs; 2387 + /* header has been freed */ 2388 + header = NULL; 2389 + goto out_image; 2395 2390 } 2396 2391 2397 2392 bpf_tail_call_direct_fixup(prog);