BPF indirect jumps
From: | Anton Protopopov <a.s.protopopov-AT-gmail.com> | |
To: | bpf-AT-vger.kernel.org, Alexei Starovoitov <ast-AT-kernel.org>, Andrii Nakryiko <andrii-AT-kernel.org>, Anton Protopopov <aspsk-AT-isovalent.com>, Daniel Borkmann <daniel-AT-iogearbox.net>, Eduard Zingerman <eddyz87-AT-gmail.com>, Quentin Monnet <qmo-AT-kernel.org>, Yonghong Song <yonghong.song-AT-linux.dev> | |
Subject: | [PATCH v4 bpf-next 00/15] BPF indirect jumps | |
Date: | Tue, 30 Sep 2025 10:55:08 +0000 | |
Message-ID: | <20250930105523.1014140-1-a.s.protopopov@gmail.com> | |
Cc: | Anton Protopopov <a.s.protopopov-AT-gmail.com> | |
Archive-link: | Article |
This patchset implements a new type of map, instruction set, and uses it to build support for indirect branches in BPF (on x86). (The same map will be later used to provide support for indirect calls and static keys.) See [1], [2] for more context. Short table of contents: * Patches 1-7 implement the new map of type BPF_MAP_TYPE_INSN_SET and corresponding selftests. This map can be used to track the "original -> xlated -> jitted mapping" for a given program. Patches 6,7 add support for "blinded" variant. * Patches 8,9,10,11 implement the support for indirect jumps * Patches 12,13,14,15 add support for LLVM-compiled programs containing indirect jumps. Since the v3 the jump table support was merged to LLVM and now can be enabled with v4. See [3] for the PR which added the support. There is still a list of TBDs for selftests, mainly, to test all the error conditions introduce by the patch "bpf, x86: add support for indirect jumps" in a new "verifier_indirect_jumps" style, but I wanted to see the kernel side calming down before doing this. See individual patches for more details on the implementation details. v3 -> v4 (this series): * instruction arrays: * add copyright (Alexei) * remove mutexes, add frozen back (Alexei) * setup 1:1 prog-map correspondence using atomic_xchg * do not copy/paste array_map_get_next_key, add a common helper (Alexei) * misc minor code cleanups (Alexei) * indirect jumps, kernel side: * remove jt_allocated, just check if insn is gotox (Eduard) * use copy_register_state instead of individual copies (Eduard) * in push_stack is_speculative should be inherited (Eduard) * a few cleanups for insn_successors, including omitting error path (Eduard) * check if reserved fields are used when considering `gotox` instruction (Eduard) * read size and alignment of read from insn_array should be 8 (Eduard) * put buffer for sorting in subfun info and realloc to grow as needed (Eduard) * properly do `jump_point` / `prune_point` from `push_gotox_edge` (Eduard) * use range_within to check states (Eduard) * some minor cleanups and fix commit message (Eduard) * indirect jumps, libbpf side: * close map_fd in some error paths in create_jt_map (Andrii) * maps for jump tables are actually not closed at all, fix this (Andrii) * rename map from `jt` to `.jumptables` (Andrii) * use `errstr` in an error message (Andrii) * rephrase error message to look more standard (Andrii) * misc other minor renames and cleanups (Andrii) * selftests: * add the frozen selftest back * add a selftest for two jumps loading same table * some other changes: * rebase and split insn_successor changes into separate patch * use PTR_ERR_OR_ZERO in the push stack patch (Eduard) * indirect jumps on x86: properly re-read *pprog (Eduard) v2 -> v3 (https://lore.kernel.org/bpf/20250918093850.455051-1-a.s.p...): * fix build failure when CONFIG_BPF_SYSCALL is not set (kbuild-bot) * reformat bpftool help messages (Quentin) v1 -> v2 (https://lore.kernel.org/bpf/20250913193922.1910480-1-a.s....): * push_stack changes: * sanitize_speculative_path should just return int (Eduard) * return code from sanitize_speculative_path, not EFAULT (Eduard) * when BPF_COMPLEXITY_LIMIT_JMP_SEQ is reached, return E2BIG (Eduard) * indirect jumps: * omit support for .imm=fd in gotox, as we're not using it for now (Eduard) * struct jt -> struct bpf_iarray (Eduard) * insn_successors: rewrite the interface to just return a pointer (Eduard) * remove min_index/max_index, use umin_value/umax_value instead (Alexei, Eduard) * move emit_indirect_jump args change to the previous patch (Eduard) * add a comment to map_mem_size() (Eduard) * use verifier_bug for some error cases in check_indirect_jump (Eduard) * clear_insn_aux_data: use start,len instead of start,end (Eduard) * make regs[insn->dst_reg].type = PTR_TO_INSN part of check_mem_access (Eduard) * constant blinding changes: * make subprog_start adjustment better readable (Eduard) * do not set subprog len, it is already set (Eduard) * libbpf: * remove check that relocations from .rodata are ok (Anton) * do not freeze the map, it is not necessary anymore (Anton) * rename the goto_x -> gotox everywhere (Anton) * use u64 when parsing LLVM jump tables (Eduard) * split patch in two due to spaces->tabs change (Eduard) * split bpftool changes to bpftool patch (Andrii) * make sym_size it a union with ext_idx (Andrii) * properly copy/free the jumptables_data section from elf (Andrii) * a few cosmetic changes around create_jt_map (Andrii) * fix some comments + rewrite patch description (Andrii) * inline bpf_prog__append_subprog_offsets (Andrii) * subprog_sec_offst -> subprog_sec_off (Andrii) * !strcmp -> strcmp() == 0 (Andrii) * make some function names more readable (Andrii) * allocate table of subfunc offsets via libbpf_reallocarray (Andrii) * selftests: * squash insn_array* tests together (Anton) * fixed build warnings (kernel test robot) RFC -> v1 (https://lore.kernel.org/bpf/20250816180631.952085-1-a.s.p...): * I've tried to address all the comments provided by Alexei and Eduard in RFC. Will try to list the most important of them below. * One big change: move from older LLVM version [5] to newer [4]. Now LLVM generates jump tables as symbols in the new special section ".jumptables". Another part of this change is that libbpf now doesn't try to link map load and goto *rX, as 1) this is absolutely not reliable 2) for some use cases this is impossible (namely, when more than one jump table can be used in the same gotox instruction). * Added insn_successors() support (Alexei, Eduard). This includes getting rid of the ugly bpf_insn_set_iter_xlated_offset() interface (Eduard). * Removed hack for the unreachable instruction, as new LLVM thank to Eduard doesn't generate it. * Set mem_size for direct map access properly instead of hacking. Remove off>0 check. (Alexei) * Do not allocate new memory for min_index/max_index (Alexei, Eduard) * Information required during check_cfg is now cached to be reused later (Alexei + general logic for supporting multiple JT per jump) * Properly compare registers in regsafe (Alexei, Eduard) * Remove support for JMP32 (Eduard) * Better checks in adjust_ptr_min_max_vals (Eduard) * More selftests were added (but still there's room for more) which directly use gotox (Alexei) * More checks and verbose messages added * "unique pointers" are no more in the map Links: 1. https://lpc.events/event/18/contributions/1941/ 2. https://lwn.net/Articles/1017439/ 3. https://github.com/llvm/llvm-project/pull/149715 4. https://github.com/llvm/llvm-project/pull/149715#issuecom... 6. rfc: https://lore.kernel.org/bpf/20250615085943.3871208-1-a.s.... Anton Protopopov (15): bpf: fix the return value of push_stack bpf: save the start of functions in bpf_prog_aux bpf: generalize and export map_get_next_key for arrays bpf, x86: add new map type: instructions array selftests/bpf: add selftests for new insn_array map bpf: support instructions arrays with constants blinding selftests/bpf: test instructions arrays with blinding bpf, x86: allow indirect jumps to r8...r15 bpf: make bpf_insn_successors to return a pointer bpf, x86: add support for indirect jumps bpf: disasm: add support for BPF_JMP|BPF_JA|BPF_X libbpf: fix formatting of bpf_object__append_subprog_code libbpf: support llvm-generated indirect jumps bpftool: Recognize insn_array map type selftests/bpf: add selftests for indirect jumps arch/x86/net/bpf_jit_comp.c | 39 +- include/linux/bpf.h | 46 ++ include/linux/bpf_types.h | 1 + include/linux/bpf_verifier.h | 27 +- include/uapi/linux/bpf.h | 11 + kernel/bpf/Makefile | 2 +- kernel/bpf/arraymap.c | 19 +- kernel/bpf/bpf_insn_array.c | 300 +++++++++ kernel/bpf/core.c | 21 + kernel/bpf/disasm.c | 9 + kernel/bpf/liveness.c | 39 +- kernel/bpf/log.c | 1 + kernel/bpf/syscall.c | 22 + kernel/bpf/verifier.c | 596 ++++++++++++++++-- .../bpf/bpftool/Documentation/bpftool-map.rst | 3 +- tools/bpf/bpftool/map.c | 3 +- tools/include/uapi/linux/bpf.h | 11 + tools/lib/bpf/libbpf.c | 263 +++++++- tools/lib/bpf/libbpf_probes.c | 4 + tools/lib/bpf/linker.c | 10 +- tools/testing/selftests/bpf/Makefile | 4 +- .../selftests/bpf/prog_tests/bpf_gotox.c | 177 ++++++ .../selftests/bpf/prog_tests/bpf_insn_array.c | 574 +++++++++++++++++ tools/testing/selftests/bpf/progs/bpf_gotox.c | 411 ++++++++++++ 24 files changed, 2466 insertions(+), 127 deletions(-) create mode 100644 kernel/bpf/bpf_insn_array.c create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_gotox.c create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_insn_array.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_gotox.c -- 2.34.1