gen_loader fixes
Subsystems (6)
v3 cover gen_loader fixes
# v2 -> v3
defer fixing 32 bit.
Fix off-by-one in the signed loader's cleanup jump and reject non-exclusive metadata maps.
Patch 1 fixes a pre-existing bug: the cleanup-label offset in emit_signature_match() does not account for the MOV insn emitted before the JMP.
Patch 2 adds an exclusivity check so the loader bails if the metadata map lacks excl_prog_hash, preventing stale-hash attacks from concurrent map writers.
KP Singh (2):
libbpf: fix off-by-one in emit_signature_match jump offset bpf, libbpf: reject non-exclusive metadata maps in the signed loader
include/linux/bpf.h | 2 +- tools/lib/bpf/gen_loader.c | 20 +++++++++++++++++-- .../selftests/bpf/progs/verifier_map_ptr.c | 8 +++++--- 3 files changed, 24 insertions(+), 6 deletions(-)
Signature · 1 line
2.53.0
PATCH v3 1/2 libbpf: fix off-by-one in emit_signature_match jump offset
The offset for the cleanup-label jump is computed before the MOV R7 instruction is emitted, but the JMP lands after it. Account for the extra insn in the offset calculation (-2 instead of -1). Drop the redundant self-loop in the else branch; gen->error = -ERANGE already marks the generation as failed.
--- tools/lib/bpf/gen_loader.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
tools/lib/bpf/gen_loader.c +1 -2
@@ -592,13 +592,12 @@ static void emit_signature_match(struct bpf_gen *gen) gen->hash_insn_offset[i] = gen->insn_cur - gen->insn_start;emit2 (gen,BPF_LD_IMM64_RAW_FULL (BPF_REG_3 ,0 ,0 ,0 ,0 ,0 )); - off = -(gen->insn_cur - gen->insn_start - gen->cleanup_label) /8 -1 ; + off = -(gen->insn_cur - gen->insn_start - gen->cleanup_label) /8 -2 ;if (is_simm16 (off)) {emit (gen,BPF_MOV64_IMM (BPF_REG_7 , -EINVAL ));emit (gen,BPF_JMP_REG (BPF_JNE ,BPF_REG_2 ,BPF_REG_3 , off)); }else { gen->error = -ERANGE ; -emit (gen,BPF_JMP_IMM (BPF_JA ,0 ,0 ,-1 )); } } }
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH bpf v3 1/2] libbpf: fix off-by-one in emit_signature_match jump offset
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Unconditional execution of compute_sha_update_offsets in bpf_gen__finish leads to NULL pointer dereference, Heap Out-Of-Bounds Write, or Use-After-Free on error paths. --
commit 7b5c3c90020ee6b64fbfb4b7121cda7da5a9c379
Author: KP Singh <[email protected]>
libbpf: fix off-by-one in emit_signature_match jump offset
This commit corrects a jump offset calculation in emit_signature_match() by accounting for the previously emitted MOV instruction. It also removes a redundant self-loop when the offset is out of range, relying on gen->error to flag the failure.
Quoted 6 lines
tools/lib/bpf/gen_loader.c +0
-0@@ -592,13 +592,12 @@ static void emit_signature_match(struct bpf_gen *gen) gen->hash_insn_offset[i] = gen->insn_cur - gen->insn_start;
tools/lib/bpf/gen_loader.c static void emit_signature_match(struct bpf_gen *gen) · line 601
Quoted 10 lines
tools/lib/bpf/gen_loader.c +1
-2emit2 (gen,BPF_LD_IMM64_RAW_FULL (BPF_REG_3 ,0 ,0 ,0 ,0 ,0 )); - off = -(gen->insn_cur - gen->insn_start - gen->cleanup_label) /8 -1 ; + off = -(gen->insn_cur - gen->insn_start - gen->cleanup_label) /8 -2 ;if (is_simm16 (off)) {emit (gen,BPF_MOV64_IMM (BPF_REG_7 , -EINVAL ));emit (gen,BPF_JMP_REG (BPF_JNE ,BPF_REG_2 ,BPF_REG_3 , off)); }else { gen->error = -ERANGE ; -emit (gen,BPF_JMP_IMM (BPF_JA ,0 ,0 ,-1 ));
[Severity: High]
This is a pre-existing issue, but I wanted to ask about how gen->error is handled once it is set to -ERANGE here.
Since the emit functions become no-ops when gen->error is set, gen->insn_cur stops advancing. However, gen_loader functions like bpf_gen__init() and bpf_gen__prog_load() return void and do not propagate this error back to the libbpf load paths.
Because of this, does the loading process continue and eventually execute bpf_gen__finish() unconditionally?
If bpf_gen__finish() calls compute_sha_update_offsets() without checking if gen->error is set, could this cause memory corruption? With gen->insn_cur stalled, gen->hash_insn_offset could point exactly to the end of the insn_start buffer.
When compute_sha_update_offsets() then writes to insn[0].imm and insn[1].imm, could this lead to a heap out-of-bounds write?
Additionally, if a memory allocation failed earlier setting gen->error to -ENOMEM, realloc_insn_buf() might have freed gen->insn_start and set it to NULL. Would the same unconditional execution path then lead to a NULL pointer dereference or use-after-free?
Quoted 3 lines
tools/lib/bpf/gen_loader.c +0
-0} } }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH bpf v3 1/2] libbpf: fix off-by-one in emit_signature_match jump offset
In reply to KP SinghDaniel Borkmann reviewed v2 and pointed out that the Fixes tag references the wrong commit. The commit fb2b0e290147 only modified tools/lib/bpf/skel_internal.h and did not touch…
Quoted 1 line
Fixes fb2b0e290147 ("libbpf: Update light skeleton for signing")
Daniel Borkmann reviewed v2 and pointed out that the Fixes tag references the wrong commit. The commit fb2b0e290147 only modified tools/lib/bpf/skel_internal.h and did not touch emit_signature_match. He suggested using ea923080c145 ("libbpf: Embed and verify the metadata hash in the loader") instead.
Reference: https://lore.kernel.org/bpf/[email protected]/
The search for the correct Fixes tag confirmed that fb2b0e290147 did not introduce the emit_signature_match function or the buggy jump offset calculation in tools/lib/bpf/gen_loader.c. The true origin commit could not be determined in this repository's history.
Should the Fixes tag be updated to point to the commit that actually introduced the emit_signature_match function with the buggy offset calculation?
Quoted 9 lines
tools/lib/bpf/gen_loader.c +0
-1@@ -592,13 +592,12 @@ static void emit_signature_match(struct bpf_gen *gen) gen->hash_insn_offset[i] = gen->insn_cur - gen->insn_start;emit2 (gen,BPF_LD_IMM64_RAW_FULL (BPF_REG_3 ,0 ,0 ,0 ,0 ,0 )); - off = -(gen->insn_cur - gen->insn_start - gen->cleanup_label) /8 -1 ;
tools/lib/bpf/gen_loader.c static void emit_signature_match(struct bpf_gen *gen) · line 603
Quoted 10 lines
tools/lib/bpf/gen_loader.c +1
-1+ off = -(gen->insn_cur - gen->insn_start - gen->cleanup_label) /8 -2 ;if (is_simm16 (off)) {emit (gen,BPF_MOV64_IMM (BPF_REG_7 , -EINVAL ));emit (gen,BPF_JMP_REG (BPF_JNE ,BPF_REG_2 ,BPF_REG_3 , off)); }else { gen->error = -ERANGE ; -emit (gen,BPF_JMP_IMM (BPF_JA ,0 ,0 ,-1 )); } } }
---
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/26314489328
PATCH v3 2/2 bpf, libbpf: reject non-exclusive metadata maps in the signed loader
In reply to KP SinghThe loader verifies map->sha against the metadata hash in its instructions. map->sha is calculated when BPF_OBJ_GET_INFO_BY_FD is called on the frozen map.
While the map is frozen, the loader must also ensure the map is exclusive, as, without exclusivity, another BPF program with map access can mutate the contents afterwards, so the check passes on stale data.
Place excl_prog_sha right after sha[] in struct bpf_map and have gen_loader bail with -EINVAL when it is NULL, via BPF_PSEUDO_MAP_IDX at fixed offset 32. The 8-byte read of the pointer field limits this to 64-bit kernels; gen_loader needs target pointer size tracking to emit the right sized read on 32-bit (follow-up).
--- include/linux/bpf.h | 2 +- tools/lib/bpf/gen_loader.c | 17 +++++++++++++++++ .../selftests/bpf/progs/verifier_map_ptr.c | 8 +++++--- 3 files changed, 23 insertions(+), 4 deletions(-)
include/linux/bpf.h +1 -1
@@ -295,6 +295,7 @@ struct bpf_map_owner {struct bpf_map {u8 sha[SHA256_DIGEST_SIZE ]; +char *excl_prog_sha;const struct bpf_map_ops *ops;struct bpf_map *inner_map_meta;#ifdef CONFIG_SECURITY
@@ -335,7 +336,6 @@ struct bpf_map {atomic64_t sleepable_refcnt;s64 __percpu *elem_count;u64 cookie;/* write-once */ -char *excl_prog_sha; };static inline const char *btf_field_type_name (enum btf_field_type type)
tools/lib/bpf/gen_loader.c +17 -0
@@ -600,6 +600,23 @@ static void emit_signature_match(struct bpf_gen *gen) gen->error = -ERANGE ; } } + +/* +* Reject if the metadata map is not exclusive. Without exclusivity +* the cached map->sha[] verified above can be stale: another BPF +* program with map access could have mutated the contents between +* BPF_OBJ_GET_INFO_BY_FD and loader execution. +*/ +emit2 (gen,BPF_LD_IMM64_RAW_FULL (BPF_REG_1 ,BPF_PSEUDO_MAP_IDX , +0 ,0 ,0 ,0 )); +emit (gen,BPF_LDX_MEM (BPF_DW ,BPF_REG_2 ,BPF_REG_1 ,SHA256_DWORD_SIZE *sizeof (__u64))); + off = -(gen->insn_cur - gen->insn_start - gen->cleanup_label) /8 -2 ; +if (is_simm16 (off)) { +emit (gen,BPF_MOV64_IMM (BPF_REG_7 , -EINVAL )); +emit (gen,BPF_JMP_IMM (BPF_JEQ ,BPF_REG_2 ,0 , off)); + }else { + gen->error = -ERANGE ; + } } void bpf_gen__record_attach_target(struct bpf_gen *gen, const char *attach_name,
tools/testing/selftests/bpf/progs/verifier_map_ptr.c +5 -3
@@ -70,13 +70,15 @@ __naked void bpf_map_ptr_write_rejected(void) : __clobber_all); } -/* The first element of struct bpf_map is a SHA256 hash of 32 bytes, accessing -* into this array is valid. The opts field is now at offset 33. +/* +* The first 32 bytes of struct bpf_map are the SHA256 hash; accessing into +* this array is valid. excl_prog_sha follows at offset 32, reading at +* offset 33 crosses into the pointer and must be rejected. */ SEC("socket") __description("bpf_map_ptr: read non-existent field rejected") __failure -__msg("cannot access ptr member ops with moff 32 in struct bpf_map with off 33 size 4") +__msg("cannot access ptr member excl_prog_sha with moff 32 in struct bpf_map with off 33 size 4") __failure_unpriv __msg_unpriv("access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN")__flag (BPF_F_ANY_ALIGNMENT )
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH bpf v3 2/2] bpf, libbpf: reject non-exclusive metadata maps in the signed loader
Daniel Borkmann suggested adding BUILD_BUG_ON checks to validate the struct layout assumptions that gen_loader relies on. Specifically:
include/linux/bpf.h struct bpf_map_owner { · line 298
Quoted 9 lines
include/linux/bpf.h +1
-0@@ -295,6 +295,7 @@ struct bpf_map_owner {struct bpf_map {u8 sha[SHA256_DIGEST_SIZE ]; +char *excl_prog_sha;
Daniel Borkmann suggested adding BUILD_BUG_ON checks to validate the struct layout assumptions that gen_loader relies on. Specifically:
BUILD_BUG_ON(offsetof(struct bpf_map, sha) != 0); BUILD_BUG_ON(offsetof(struct bpf_map, excl_prog_sha) != SHA256_DIGEST_SIZE); BUILD_BUG_ON(sizeof_field(struct bpf_map, excl_prog_sha) != sizeof(__u64));
Would this make the code more robust against future struct layout changes?
Quoted 3 lines
include/linux/bpf.h +0
-0const struct bpf_map_ops *ops;struct bpf_map *inner_map_meta;#ifdef CONFIG_SECURITY
[ ... ]
Quoted 8 lines
tools/lib/bpf/gen_loader.c +0
-0@@ -600,6 +600,23 @@ static void emit_signature_match(struct bpf_gen *gen) gen->error = -ERANGE ; } }
tools/lib/bpf/gen_loader.c static void emit_signature_match(struct bpf_gen *gen) · line 612
Quoted 10 lines
tools/lib/bpf/gen_loader.c +10
-0+ +/* +* Reject if the metadata map is not exclusive. Without exclusivity +* the cached map->sha[] verified above can be stale: another BPF +* program with map access could have mutated the contents between +* BPF_OBJ_GET_INFO_BY_FD and loader execution. +*/ +emit2 (gen,BPF_LD_IMM64_RAW_FULL (BPF_REG_1 ,BPF_PSEUDO_MAP_IDX , +0 ,0 ,0 ,0 )); +emit (gen,BPF_LDX_MEM (BPF_DW ,BPF_REG_2 ,BPF_REG_1 ,SHA256_DWORD_SIZE *sizeof (__u64)));
Will this BPF_DW (8-byte) read pass the BPF verifier on 32-bit architectures?
Sashiko AI flagged this in v2, and you confirmed through testing on a 32-bit kernel that the verifier rejects this with 'cannot access ptr member excl_prog_sha with moff 0 in struct (anon) with off 0 size 8'. The issue is that __bpf_md_ptr doesn't emit padding in BTF; the kernel's BTF represents the pointer member with a size of 4 bytes on 32-bit systems, while btf_struct_walk() enforces exact size matching.
The commit message acknowledges this: 'The 8-byte read of the pointer field limits this to 64-bit kernels; gen_loader needs target pointer size tracking to emit the right sized read on 32-bit (follow-up).'
This means the security check will be silently bypassed on 32-bit systems until the follow-up is implemented.
Could SHA256_DIGEST_SIZE be used here instead of 'SHA256_DWORD_SIZE * sizeof(__u64)'? Daniel Borkmann pointed out that SHA256_DWORD_SIZE is already SHA256_DIGEST_SIZE / sizeof(__u64), so the multiplication essentially reconstructs SHA256_DIGEST_SIZE.
tools/lib/bpf/gen_loader.c static void emit_signature_match(struct bpf_gen *gen) · line 620
Quoted 8 lines
tools/lib/bpf/gen_loader.c +7
-0+ off = -(gen->insn_cur - gen->insn_start - gen->cleanup_label) /8 -2 ; +if (is_simm16 (off)) { +emit (gen,BPF_MOV64_IMM (BPF_REG_7 , -EINVAL )); +emit (gen,BPF_JMP_IMM (BPF_JEQ ,BPF_REG_2 ,0 , off)); + }else { + gen->error = -ERANGE ; + } }
[ ... ]
---
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/26314489328
[email protected]
AI reviewRe: [PATCH bpf v3 2/2] bpf, libbpf: reject non-exclusive metadata maps in the signed loader
In reply to KP SinghThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Critical] Hardcoded fixed offsets for kernel-internal `struct bpf_map` in userspace-generated BPF code…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] Hardcoded fixed offsets for kernel-internal struct bpf_map in userspace-generated BPF code without CO-RE relocations.
- [High] The loader program unconditionally emits an 8-byte read for the excl_prog_sha pointer field, causing a guaranteed verifier rejection on 32-bit kernels.
- [High] The metadata map exclusivity check can be bypassed via map-in-map, reintroducing the TOCTOU vulnerability.
--
commit afb36fdfc7cc8ff5eb04ebacf8fda203221aa679
Author: KP Singh <[email protected]>
bpf, libbpf: reject non-exclusive metadata maps in the signed loader
This commit adds a check in the gen_loader to verify that the metadata map is exclusive before allowing it to be used. It places excl_prog_sha right after the sha field in struct bpf_map and reads it at a fixed offset to ensure another program cannot mutate the contents after signature validation.
include/linux/bpf.h struct bpf_map_owner { · line 300
Quoted 11 lines
include/linux/bpf.h +1
-0@@ -295,6 +295,7 @@ struct bpf_map_owner {struct bpf_map {u8 sha[SHA256_DIGEST_SIZE ]; +char *excl_prog_sha;const struct bpf_map_ops *ops;struct bpf_map *inner_map_meta;
[Severity: High]
Does this exclusivity check handle map-in-map scenarios correctly? Since bpf_map_meta_alloc() does not copy excl_prog_sha into the inner_map_meta, could an attacker load a program against an inner_map_meta (which has a NULL excl_prog_sha), and then insert the exclusive frozen map into the map-in-map at runtime? It appears this might allow a malicious program to dynamically look up the inner map and mutate the frozen map using bpf_map_update_elem(), bypassing the intended security guarantee.
[ ... ]
Quoted 4 lines
tools/lib/bpf/gen_loader.c +0
-0
[ ... ]
Quoted 4 lines
tools/lib/bpf/gen_loader.c +0
-0@@ -600,6 +600,23 @@ static void emit_signature_match(struct bpf_gen *gen) gen->error = -ERANGE ; } }
tools/lib/bpf/gen_loader.c static void emit_signature_match(struct bpf_gen *gen) · line 612
Quoted 10 lines
tools/lib/bpf/gen_loader.c +10
-0+ +/* +* Reject if the metadata map is not exclusive. Without exclusivity +* the cached map->sha[] verified above can be stale: another BPF +* program with map access could have mutated the contents between +* BPF_OBJ_GET_INFO_BY_FD and loader execution. +*/ +emit2 (gen,BPF_LD_IMM64_RAW_FULL (BPF_REG_1 ,BPF_PSEUDO_MAP_IDX , +0 ,0 ,0 ,0 )); +emit (gen,BPF_LDX_MEM (BPF_DW ,BPF_REG_2 ,BPF_REG_1 ,SHA256_DWORD_SIZE *sizeof (__u64)));
[Severity: Critical]
Does this hardcode a fixed offset (32) for a kernel-internal structure without using CO-RE? Since light skeleton headers are distributed in userspace, if struct bpf_map changes in future kernel releases, won't these older generated skeletons be rejected by the verifier or silently read incorrect data? This seems to unintentionally freeze the internal ABI of struct bpf_map for light skeletons.
[Severity: High]
Will this unconditionally emit an 8-byte read for the excl_prog_sha pointer field, even on 32-bit systems? Since pointers are 4 bytes on 32-bit architectures, won't the BPF verifier strictly reject this 8-byte read against a 4-byte BTF pointer field due to the size mismatch? The commit message mentions target pointer size tracking is needed as a follow-up, but does this mean 32-bit skeletons will be guaranteed to fail loading until that follow-up is merged?
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH bpf v3 2/2] bpf, libbpf: reject non-exclusive metadata maps in the signed loader
In reply to KP SinghQuoted 8 lines · KP Singh
The loader verifies
map->shaagainst the metadata hash in its instructions.map->shais calculated whenBPF_OBJ_GET_INFO_BY_FDis called on the frozen map.While the map is frozen, the loader must also ensure the map is exclusive, as, without exclusivity, another BPF program with map access can mutate the contents afterwards, so the check passes on stale data.
Hold on. How is this an issue? excl_prog_sha guarantees that only loader prog can use this map.
Are you saying the same loader prog will use the same map for the 2nd time. Ok. I still don't see a problem.
Quoted 5 lines
Place
excl_prog_sharight after sha[] in structbpf_mapand havegen_loaderbail with-EINVALwhen it is NULL, viaBPF_PSEUDO_MAP_IDXat fixed offset 32. The 8-byte read of the pointer field limits this to 64-bit kernels;gen_loaderneeds target pointer size tracking to emit the right sized read on 32-bit (follow-up).
I don't think we can go from maybe-racy to certainly-broken-on-32-bit. So only applied patch 1.
Daniel Borkmann
maintainerRe: [PATCH bpf v3 2/2] bpf, libbpf: reject non-exclusive metadata maps in the signed loader
Quoted 15 lines · Alexei Starovoitov
Quoted 8 lines · KP Singh
The loader verifies
map->shaagainst the metadata hash in its instructions.map->shais calculated whenBPF_OBJ_GET_INFO_BY_FDis called on the frozen map.While the map is frozen, the loader must also ensure the map is exclusive, as, without exclusivity, another BPF program with map access can mutate the contents afterwards, so the check passes on stale data.
Hold on. How is this an issue?
excl_prog_shaguarantees that only loader prog can use this map.
Are you saying the same loader prog will use the same map for the 2nd time. Ok. I still don't see a problem.
Quoted 8 lines
Quoted 5 lines
Place
excl_prog_sharight after sha[] in structbpf_mapand havegen_loaderbail with-EINVALwhen it is NULL, viaBPF_PSEUDO_MAP_IDXat fixed offset 32. The 8-byte read of the pointer field limits this to 64-bit kernels;gen_loaderneeds target pointer size tracking to emit the right sized read on 32-bit (follow-up).I don't think we can go from maybe-racy to certainly-broken-on-32-bit. So only applied patch 1.
I've looked a bit more into it with regards to above question from Alexei as well as the __bpf_md_ptr issue.
Imho, KP is correct that the extra check/enforcement is needed. So Alice as a trusted signer generates the loader program (loader_insns + data_blob) and signs it. The loader program contains the below enforcement to reject if the metadata map was not exclusive.
Now the (untrusted) host that wants to load the program, it holds a signed loader where they can't change a byte of it without breaking the signature.
However, it could simply omit excl_prog_hash on BPF_MAP_CREATE for the data map (which would "normally" be bound exclusively to the loader).
Then check_map_prog_compatibility() enforcement is skipped on verifier side given excl_prog_sha is not set. The loader loads fine, the fingerprint check can then pass against a stale snapshot while a different program mangled the data_blob underneath.
Regarding __bpf_md_ptr, I would solve it differently via fixed size, see below together with the excl check coming before the signature check in the loader and the build bug assertions, and a jmp not eq to 1.
include/linux/bpf.h | 1 + kernel/bpf/syscall.c | 5 +++++ tools/lib/bpf/gen_loader.c | 17 +++++++++++++++++ 3 files changed, 23 insertions(+)
include/linux/bpf.h +1 -0
@@ -295,6 +295,7 @@ struct bpf_map_owner {struct bpf_map {u8 sha[SHA256_DIGEST_SIZE ]; +u32 excl;const struct bpf_map_ops *ops;struct bpf_map *inner_map_meta;#ifdef CONFIG_SECURITY
kernel/bpf/syscall.c +5 -0
@@ -1572,6 +1572,11 @@ static int map_create(union bpf_attr *attr, bpfptr_t uattr) err = -EFAULT ; goto free_map; } + +/* See libbpf: emit_signature_match() */ +BUILD_BUG_ON (offsetof(struct bpf_map , excl) !=SHA256_DIGEST_SIZE ); +BUILD_BUG_ON (offsetof(struct bpf_map , sha) !=0 ); + map->excl =1 ; }else if (attr->excl_prog_hash_size) { err = -EINVAL ; goto free_map;
tools/lib/bpf/gen_loader.c +17 -0
@@ -586,6 +586,23 @@ static void emit_signature_match(struct bpf_gen *gen)__s64 off;int i; +/* +* Reject if the metadata map is not exclusive. Without exclusivity +* the cached map->sha[] verified above can be stale: another BPF +* program with map access could have mutated the contents between +* BPF_OBJ_GET_INFO_BY_FD and loader execution. +*/ +emit2 (gen,BPF_LD_IMM64_RAW_FULL (BPF_REG_1 ,BPF_PSEUDO_MAP_IDX , +0 ,0 ,0 ,0 )); +emit (gen,BPF_LDX_MEM (BPF_W ,BPF_REG_2 ,BPF_REG_1 ,SHA256_DIGEST_LENGTH )); + off = -(gen->insn_cur - gen->insn_start - gen->cleanup_label) /8 -2 ; +if (is_simm16 (off)) { +emit (gen,BPF_MOV64_IMM (BPF_REG_7 , -EINVAL )); +emit (gen,BPF_JMP_IMM (BPF_JNE ,BPF_REG_2 ,1 , off)); + }else { + gen->error = -ERANGE ; + } +for (i =0 ; i <SHA256_DWORD_SIZE ; i++) {emit2 (gen,BPF_LD_IMM64_RAW_FULL (BPF_REG_1 ,BPF_PSEUDO_MAP_IDX ,0 ,0 ,0 ,0 ));
Signature · 1 line
2.43.0
Re: [PATCH bpf v3 2/2] bpf, libbpf: reject non-exclusive metadata maps in the signed loader
Quoted 96 lines · Daniel Borkmann
Quoted 15 lines · Alexei Starovoitov
Quoted 8 lines · KP Singh
The loader verifies
map->shaagainst the metadata hash in its instructions.map->shais calculated whenBPF_OBJ_GET_INFO_BY_FDis called on the frozen map.While the map is frozen, the loader must also ensure the map is exclusive, as, without exclusivity, another BPF program with map access can mutate the contents afterwards, so the check passes on stale data.
Hold on. How is this an issue?
excl_prog_shaguarantees that only loader prog can use this map.
Are you saying the same loader prog will use the same map for the 2nd time. Ok. I still don't see a problem.Quoted 8 lines
Quoted 5 lines
Place
excl_prog_sharight after sha[] in structbpf_mapand havegen_loaderbail with-EINVALwhen it is NULL, viaBPF_PSEUDO_MAP_IDXat fixed offset 32. The 8-byte read of the pointer field limits this to 64-bit kernels;gen_loaderneeds target pointer size tracking to emit the right sized read on 32-bit (follow-up).I don't think we can go from maybe-racy to certainly-broken-on-32-bit. So only applied patch 1.
I've looked a bit more into it with regards to above question from Alexei as well as the
__bpf_md_ptrissue.Imho, KP is correct that the extra check/enforcement is needed. So Alice as a trusted signer generates the loader program (
loader_insns+data_blob) and signs it. The loader program contains the below enforcement to reject if the metadata map was not exclusive.Now the (untrusted) host that wants to load the program, it holds a signed loader where they can't change a byte of it without breaking the signature.
However, it could simply omit
excl_prog_hashonBPF_MAP_CREATEfor the data map (which would "normally" be bound exclusively to the loader).Then
check_map_prog_compatibility()enforcement is skipped on verifier side givenexcl_prog_shais not set. The loader loads fine, the fingerprint check can then pass against a stale snapshot while a different program mangled thedata_blobunderneath.Regarding
__bpf_md_ptr, I would solve it differently via fixed size, see below together with the excl check coming before the signature check in the loader and the build bug assertions, and a jmp not eq to 1.include/linux/bpf.h | 1 + kernel/bpf/syscall.c | 5 +++++ tools/lib/bpf/gen_loader.c | 17 +++++++++++++++++ 3 files changed, 23 insertions(+)include/linux/bpf.h +1
-0@@ -295,6 +295,7 @@ struct bpf_map_owner {struct bpf_map {u8 sha[SHA256_DIGEST_SIZE ]; +u32 excl;const struct bpf_map_ops *ops;struct bpf_map *inner_map_meta;#ifdef CONFIG_SECURITY kernel/bpf/syscall.c +5
-0@@ -1572,6 +1572,11 @@ static int map_create(union bpf_attr *attr, bpfptr_t uattr) err = -EFAULT ; goto free_map; } + +/* See libbpf: emit_signature_match() */ +BUILD_BUG_ON (offsetof(struct bpf_map , excl) !=SHA256_DIGEST_SIZE ); +BUILD_BUG_ON (offsetof(struct bpf_map , sha) !=0 ); + map->excl =1 ; }else if (attr->excl_prog_hash_size) { err = -EINVAL ; goto free_map;tools/lib/bpf/gen_loader.c +6
-0@@ -586,6 +586,23 @@ static void emit_signature_match(struct bpf_gen *gen)__s64 off;int i; +/* +* Reject if the metadata map is not exclusive. Without exclusivity +* the cached map->sha[] verified above can be stale: another BPF +* program with map access could have mutated the contents between +* BPF_OBJ_GET_INFO_BY_FD and loader execution. +*/
tools/lib/bpf/gen_loader.c static void emit_signature_match(struct bpf_gen *gen) · line 604
Quoted 10 lines
tools/lib/bpf/gen_loader.c +10
-0+emit2 (gen,BPF_LD_IMM64_RAW_FULL (BPF_REG_1 ,BPF_PSEUDO_MAP_IDX , +0 ,0 ,0 ,0 )); +emit (gen,BPF_LDX_MEM (BPF_W ,BPF_REG_2 ,BPF_REG_1 ,SHA256_DIGEST_LENGTH )); + off = -(gen->insn_cur - gen->insn_start - gen->cleanup_label) /8 -2 ; +if (is_simm16 (off)) { +emit (gen,BPF_MOV64_IMM (BPF_REG_7 , -EINVAL )); +emit (gen,BPF_JMP_IMM (BPF_JNE ,BPF_REG_2 ,1 , off)); + }else { + gen->error = -ERANGE ; + }
yeah. much cleaner. ship it.
This series was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf.git (master)
by Alexei Starovoitov <[email protected]>:
Quoted 8 lines · On Fri, 22 May 2026 23:53:35 +0200 you wrote:
# v2 -> v3defer fixing 32 bit.
Fix off-by-one in the signed loader's cleanup jump and reject non-exclusive metadata maps.
[...]
Here is the summary with links:
- [bpf,v3,1/2] libbpf: fix off-by-one in emit_signature_match jump offset
https://git.kernel.org/bpf/bpf/c/7dd62566e0d1
- [bpf,v3,2/2] bpf, libbpf: reject non-exclusive metadata maps in the signed loader (no matching commit)
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html