Refuse CO-RE relocations for a signed program
Subsystems (4)
Kumar Kartikeya Dwivedi
maintainercover Refuse CO-RE relocations for a signed program
The signature of a BPF program is checked over the insns as submitted, but the loader may also hand in CO-RE relocation records, which the kernel applies to those insns afterwards with values the loader picks through its BTF. Whoever holds a signed program can so change its constants once the signature has passed. Refuse relocations on a signed load, before the signature is examined, and test it.
Found by Sashiko on the "Fix some arena paper cuts" series:
https://lore.kernel.org/bpf/[email protected]/
Kumar Kartikeya Dwivedi (2):
bpf: Refuse CO-RE relocations for a signed program selftests/bpf: Test that a signed program with CO-RE relocations is
refused
kernel/bpf/verifier.c | 9 ++++ .../selftests/bpf/prog_tests/signed_loader.c | 46 +++++++++++++++++++ 2 files changed, 55 insertions(+)
base-commit: 25bb03f0a47b622b9025a1558bf2095ec1f55d26
Signature · 1 line
2.53.0-Meta
Kumar Kartikeya Dwivedi
maintainerPATCH 1/2 bpf: Refuse CO-RE relocations for a signed program
The signature of a program covers its insns as submitted, along with the frozen metadata maps of a signed loader, and bpf_prog_verify_signature() checks it before the verifier does anything else with the program. CO-RE relocations are the one input besides the insns that changes them: the loader hands them in with the program, and bpf_check_core_relo() applies them after the signature check, rewriting the immediate or the offset of every insn a record names with a value the loader chooses, a field offset, a type size or id or an enum value of the kernel BTF, or a type id of the BTF the loader supplies itself. Whoever holds a signed program can so change its constants once the signature has passed, the offsets a signed loader reads its metadata map with for instance, and run a program the signer never saw. The exclusive metadata map of a signed loader does not make up for it: the hash that binds the map to a program is chosen by whoever creates the map, so it can name the relocated program as well.
Refuse a signed load that carries relocations, before the signature is examined, like the other checks on the shape of a signed load. A signed loader has none to lose: the programs it embeds are relocated by the loader itself through bpf_core_apply(), and nothing relocates the loader. The BTF of the program itself can stay: without relocations it steers only the verification, through func_info, line_info and the annotations of global function arguments, and rewrites nothing.
--- kernel/bpf/verifier.c | 9 +++++++++ 1 file changed, 9 insertions(+)
kernel/bpf/verifier.c +9 -0
@@ -23401,6 +23401,15 @@ static int bpf_prog_verify_signature(struct bpf_verifier_env *env,u64 data_sz;int err =0 ; +/* +* The signature covers the insns as submitted. CO-RE relocations would +* have the kernel rewrite them afterwards, with values the loader picks +* through the BTF it supplies, see bpf_check_core_relo(). +*/ +if (attr->core_relo_cnt) { +verbose (env,"signed program cannot carry CO-RE relocations\n" ); +return -EACCES ; + }if (!attr->signature_size || attr->signature_size >BPF_PROG_MAX_SIGNATURE_SIZE )return -EINVAL ;
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewRe: [PATCH bpf-next v1 1/2] bpf: Refuse CO-RE relocations for a signed program
TL;DR: Commit message may be inaccurate: unsigned program BTF still drives post-signature insn patching (bpf_obj_new size, struct_meta, exception callback), so signed programs may still change…
Quoted 1 line
bpf: Refuse CO-RE relocations for a signed program
TL;DR: Commit message may be inaccurate: unsigned program BTF still drives post-signature insn patching (bpf_obj_new size, struct_meta, exception callback), so signed programs may still change; check itself looks correct.
Quoted 26 lines
bpf: Refuse CO-RE relocations for a signed program
The signature of a program covers its insns as submitted, along with the frozen metadata maps of a signed loader, and
bpf_prog_verify_signature()checks it before the verifier does anything else with the program. CO-RE relocations are the one input besides the insns that changes them: the loader hands them in with the program, andbpf_check_core_relo()applies them after the signature check, rewriting the immediate or the offset of every insn a record names with a value the loader chooses, a field offset, a type size or id or an enum value of the kernel BTF, or a type id of the BTF the loader supplies itself. Whoever holds a signed program can so change its constants once the signature has passed, the offsets a signed loader reads its metadata map with for instance, and run a program the signer never saw. The exclusive metadata map of a signed loader does not make up for it: the hash that binds the map to a program is chosen by whoever creates the map, so it can name the relocated program as well.Refuse a signed load that carries relocations, before the signature is examined, like the other checks on the shape of a signed load. A signed loader has none to lose: the programs it embeds are relocated by the loader itself through
bpf_core_apply(), and nothing relocates the loader. The BTF of the program itself can stay: without relocations it steers only the verification, throughfunc_info,line_infoand the annotations of global function arguments, and rewrites nothing.
Quoted 3 lines
Fixes 349271568303 ("bpf: Implement signature verification for BPF programs")Cc KP Singh <[email protected]>Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>
The commit message says CO-RE relocations "are the one input besides the insns that changes them", and that the BTF of the program "steers only the verification ... and rewrites nothing".
Are both statements accurate?
The program BTF (prog_btf_fd, installed into prog->aux->btf by bpf_prepare_btf_info()) is not part of the signed payload. bpf_prog_verify_signature() hashes only the insns followed by the frozen maps, and it runs before the BTF is read.
The verifier still patches the insns using values taken from that BTF. In check_special_kfunc(), for bpf_obj_new_impl() and bpf_percpu_obj_new_impl():
ret_btf = env->prog->aux->btf;
...
insn_aux->obj_new_size = ret_t->size;
insn_aux->kptr_struct_meta = struct_meta;and later bpf_fixup_kfunc_call() emits:
insn_buf[0] = BPF_MOV64_IMM(BPF_REG_1, obj_new_size);
followed by a BPF_LD_IMM64 of the struct_meta pointer into R2. The same BTF-derived struct_meta is patched in for bpf_obj_drop and bpf_refcount_acquire, and for the list push and rbtree add kfuncs through __fixup_collection_insert_kfunc().
These kfuncs are registered for BPF_PROG_TYPE_SYSCALL (the signed loader type) as well as TRACING, SCHED_CLS, XDP, STRUCT_OPS and CGROUP_SKB, and nothing in the signed load path refuses them.
Can a holder of a signed program still have the kernel patch in an allocation size and struct_meta pointer derived from a BTF type the signer never saw, after the signature check has passed? That looks like the same class of post-signature constant change this patch sets out to close.
Similarly, bpf_find_exception_callback_insn_off() picks the exception callback subprog from an "exception_callback:" decl tag in the same unsigned BTF, which changes run-time control flow and not only the verification.
The memory safety impact looks limited, since the verifier checks accesses against the same BTF it then patches in. This is mainly a question of the message's accuracy, and of the scope of the hardening it describes.
Separately, the statement that the embedded programs "are relocated by the loader itself through bpf_core_apply()" seems imprecise. The loader program only issues a nested BPF_PROG_LOAD carrying core_relo_cnt (bpf_gen__prog_load() in tools/lib/bpf/gen_loader.c), and the kernel applies the relocations in bpf_check_core_relo() -> bpf_core_apply() on that nested, unsigned load.
Could the message be narrowed so it does not claim the program BTF "rewrites nothing", or could it acknowledge the bpf_obj_new, list and rbtree fixups and the exception_callback decl tag? Should those paths also be refused for a signed program?
The check itself looks correct: core_relo_cnt is zero for the signed loader (bpf_load_and_run() in skel_internal.h memsets attr), the early return leaks nothing, and the err_prep path in bpf_check() cleans up.
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/39273821952
Re: [PATCH bpf-next v1 1/2] bpf: Refuse CO-RE relocations for a signed program
In reply to Kumar Kartikeya DwivediQuoted 24 lines · Kumar Kartikeya Dwivedi
The signature of a program covers its insns as submitted, along with the frozen metadata maps of a signed loader, and
bpf_prog_verify_signature()checks it before the verifier does anything else with the program. CO-RE relocations are the one input besides the insns that changes them: the loader hands them in with the program, andbpf_check_core_relo()applies them after the signature check, rewriting the immediate or the offset of every insn a record names with a value the loader chooses, a field offset, a type size or id or an enum value of the kernel BTF, or a type id of the BTF the loader supplies itself. Whoever holds a signed program can so change its constants once the signature has passed, the offsets a signed loader reads its metadata map with for instance, and run a program the signer never saw. The exclusive metadata map of a signed loader does not make up for it: the hash that binds the map to a program is chosen by whoever creates the map, so it can name the relocated program as well.Refuse a signed load that carries relocations, before the signature is examined, like the other checks on the shape of a signed load. A signed loader has none to lose: the programs it embeds are relocated by the loader itself through
bpf_core_apply(), and nothing relocates the loader. The BTF of the program itself can stay: without relocations it steers only the verification, throughfunc_info,line_infoand the annotations of global function arguments, and rewrites nothing.
Quoted 3 lines
Fixes 349271568303 ("bpf: Implement signature verification for BPF programs")Cc KP Singh <[email protected]>Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>
Kumar Kartikeya Dwivedi
maintainerPATCH 2/2 selftests/bpf: Test that a signed program with CO-RE relocations is refused
In reply to Kumar Kartikeya DwivediLoad a two insn program with a junk signature and one CO-RE relocation record, and check that the kernel refuses it with -EACCES and the message naming the relocations, before it examines the signature. Without the check the same load gets as far as the signature and fails on it with -EBADMSG.
--- .../selftests/bpf/prog_tests/signed_loader.c | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+)
tools/testing/selftests/bpf/prog_tests/signed_loader.c +46 -0
@@ -379,6 +379,51 @@ static void signature_enforced(void)gen_loader_fixture_fini (&f); } +/* +* CO-RE relocations would have the kernel rewrite the insns after the +* signature was checked over them, with values the loader picks through the +* BTF it supplies. A signed load that carries any is rejected before the +* signature is examined, so junk serves as one. +*/ +static void signed_core_relo_rejected (void ) +{ +static const __u8 junk[64 ] = {0x30 ,0x42 ,0x13 ,0x37 , }; +struct bpf_insn insns[] = { +BPF_MOV64_IMM (BPF_REG_0 ,0 ), +BPF_EXIT_INSN (), + }; +struct bpf_core_relo relo = { + .insn_off =0 , + .type_id =1 , + .kind =BPF_CORE_TYPE_ID_LOCAL , + }; +char log_buf[1024 ] = {}; +union bpf_attr attr; +int fd; + +memset (&attr,0 ,sizeof (attr)); + attr.prog_type =BPF_PROG_TYPE_SYSCALL ; + attr.insns =ptr_to_u64 (insns); + attr.insn_cnt =ARRAY_SIZE (insns); + attr.license =ptr_to_u64 ("Dual BSD/GPL" ); + attr.prog_flags =BPF_F_SLEEPABLE ; + attr.signature =ptr_to_u64 (junk); + attr.signature_size =sizeof (junk); + attr.keyring_id =KEY_SPEC_SESSION_KEYRING ; + attr.core_relos =ptr_to_u64 (&relo); + attr.core_relo_cnt =1 ; + attr.core_relo_rec_size =sizeof (relo); + attr.log_level =1 ; + attr.log_buf =ptr_to_u64 (log_buf); + attr.log_size =sizeof (log_buf); + fd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, offsetofend(union bpf_attr, keyring_id)); + fd = fd <0 ? -errno : fd; +ASSERT_EQ (fd, -EACCES ,"signed load with CO-RE relocations rejected" ); +ASSERT_HAS_SUBSTR (log_buf,"signed program cannot carry CO-RE relocations" ,"log" ); +if (fd >=0 ) +close (fd); +} +static void signed_nonexcl_fd_array_rejected (void ) {static const __u8 junk[64 ] = {0x30 ,0x42 ,0x13 ,0x37 , };
@@ -2187,6 +2232,7 @@ static const struct { { "signed_unfrozen_fd_array_rejected", signed_unfrozen_fd_array_rejected, BOOT_SEALED }, { "signed_nonarray_fd_array_rejected", signed_nonarray_fd_array_rejected, BOOT_SEALED }, { "signed_btf_fd_array_rejected", signed_btf_fd_array_rejected, BOOT_ANY }, + { "signed_core_relo_rejected", signed_core_relo_rejected, BOOT_ANY }, { "signed_module_kfunc_rejected", signed_module_kfunc_rejected, BOOT_SEALED }, { "signature_failure_logs", signature_failure_logs, BOOT_SEALED }, { "signature_too_large", signature_too_large, BOOT_ANY },
Signature · 1 line
2.53.0-Meta
Re: [PATCH bpf-next v1 2/2] selftests/bpf: Test that a signed program with CO-RE relocations is refused
Quoted 7 lines · Kumar Kartikeya Dwivedi
Load a two insn program with a junk signature and one CO-RE relocation record, and check that the kernel refuses it with
-EACCESand the message naming the relocations, before it examines the signature. Without the check the same load gets as far as the signature and fails on it with-EBADMSG.Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 8 lines · On Mon, 19 Oct 2026 16:48:48 +0200 you wrote:
The signature of a BPF program is checked over the insns as submitted, but the loader may also hand in CO-RE relocation records, which the kernel applies to those insns afterwards with values the loader picks through its BTF. Whoever holds a signed program can so change its constants once the signature has passed. Refuse relocations on a signed load, before the signature is examined, and test it.
[...]
Here is the summary with links:
- [bpf-next,v1,1/2] bpf: Refuse CO-RE relocations for a signed program
https://git.kernel.org/bpf/bpf-next/c/19a17b5b2b01 - [bpf-next,v1,2/2] selftests/bpf: Test that a signed program with CO-RE relocations is refused https://git.kernel.org/bpf/bpf-next/c/25a2e54999b0
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html