gen_loader fixes
Subsystems (6)
v2 cover gen_loader fixes
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 | 19 +++++++++++++++++-- .../selftests/bpf/progs/verifier_map_ptr.c | 10 ++++++---- 3 files changed, 24 insertions(+), 7 deletions(-)
Signature · 1 line
2.53.0
PATCH v2 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
PATCH v2 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. Declare excl_prog_sha with __bpf_md_ptr so the 8-byte BPF_LDX_MEM read works on 32-bit kernels.
--- include/linux/bpf.h | 2 +- tools/lib/bpf/gen_loader.c | 16 ++++++++++++++++ .../selftests/bpf/progs/verifier_map_ptr.c | 10 ++++++---- 3 files changed, 23 insertions(+), 5 deletions(-)
include/linux/bpf.h +1 -1
@@ -295,6 +295,7 @@ struct bpf_map_owner {struct bpf_map {u8 sha[SHA256_DIGEST_SIZE ]; + __bpf_md_ptr(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 +16 -0
@@ -600,6 +600,22 @@ 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 +6 -4
@@ -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 (8 bytes via +* __bpf_md_ptr), then ops at offset 40. Reading at offset 41 crosses into +* the ops 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 ops with moff 40 in struct bpf_map with off 41 size 4") __failure_unpriv __msg_unpriv("access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN")__flag (BPF_F_ANY_ALIGNMENT )
@@ -85,7 +87,7 @@ __naked void read_non_existent_field_rejected(void) asmvolatile (" \ r6 = 0; \ r1 = %[map_array_48b] ll; \ -r6 = *(u32*)(r1 + 33); \ +r6 = *(u32*)(r1 + 41); \ r0 = 1; \ exit; \ " :
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v2 1/2] libbpf: fix off-by-one in emit_signature_match jump offset
In reply to KP SinghThe Fixes: tag points to commit fb2b0e290147 ("libbpf: Update light skeleton for signing"), which does not exist in this repository. The bug was actually introduced by commit b3beebbe6154 ("adding…
Quoted 5 lines
commit
4eb2c1ff205ef1d11b97cef9c13092a8b13ba5e1
Author: KP Singh <[email protected]>libbpf: fix off-by-one in
emit_signature_matchjump offset
Quoted 8 lines
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=-ERANGEalready marks the generation as failed.Fixes fb2b0e290147 ("libbpf: Update light skeleton for signing")Signed-off-by KP Singh <[email protected]>
The Fixes: tag points to commit fb2b0e290147 ("libbpf: Update light skeleton for signing"), which does not exist in this repository. The bug was actually introduced by commit b3beebbe6154 ("adding ci files"), which added the gen_loader.c file containing the emit_signature_match function with the off-by-one error in the jump offset calculation. Should the Fixes: tag be:
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/26236267798
[email protected]
AI reviewRe: [PATCH v2 2/2] bpf, libbpf: reject non-exclusive metadata maps in the signed loader
In reply to KP SinghThis isn't a bug, but the multi-line comment opening should follow the BPF subsystem style with /* on its own line.
Quoted 18 lines
include/linux/bpf.h +1
-1@@ -295,6 +295,7 @@ struct bpf_map_owner {struct bpf_map {u8 sha[SHA256_DIGEST_SIZE ]; + __bpf_md_ptr(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; };
[ ... ]
Quoted 5 lines
tools/lib/bpf/gen_loader.c +0
-0
tools/lib/bpf/gen_loader.c static void emit_signature_match(struct bpf_gen *gen) · line 608
Quoted 9 lines
tools/lib/bpf/gen_loader.c +6
-0@@ -600,6 +600,22 @@ 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. +*/
This isn't a bug, but the multi-line comment opening should follow the BPF subsystem style with /* on its own line.
Quoted 11 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_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 ; + } }
[ ... ]
Quoted 24 lines
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 (8 bytes via +* __bpf_md_ptr), then ops at offset 40. Reading at offset 41 crosses into +* the ops 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 ops with moff 40 in struct bpf_map with off 41 size 4") __failure_unpriv __msg_unpriv("access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN")__flag (BPF_F_ANY_ALIGNMENT )
tools/testing/selftests/bpf/progs/verifier_map_ptr.c __naked void read_non_existent_field_rejected(void) · line 93
Quoted 8 lines
tools/testing/selftests/bpf/progs/verifier_map_ptr.c +1
-1@@ -85,7 +87,7 @@ __naked void read_non_existent_field_rejected(void) asmvolatile (" \ r6 = 0; \ r1 = %[map_array_48b] ll; \ -r6 = *(u32*)(r1 + 33); \ +r6 = *(u32*)(r1 + 41); \ r0 = 1; \ exit; \ " :
---
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/26236267798
[email protected]
AI reviewRe: [PATCH v2 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 2 potential issue(s) to consider: - [High] The BPF verifier will reject the `gen_loader` program on 32-bit architectures because it accesses…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The BPF verifier will reject the gen_loader program on 32-bit architectures because it accesses a 4-byte pointer using an 8-byte read.
- [Low] Multi-line comments do not follow the mandatory BPF subsystem standard.
--
commit 878a81f0d9571d5734eccfbe01c3dfe58050f954
Author: KP Singh <[email protected]>
bpf, libbpf: reject non-exclusive metadata maps in the signed loader
The 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.
Quoted 4 lines
tools/lib/bpf/gen_loader.c +0
-0
[ ... ]
tools/lib/bpf/gen_loader.c static void emit_signature_match(struct bpf_gen *gen) · line 608
Quoted 10 lines
tools/lib/bpf/gen_loader.c +6
-0@@ -600,6 +600,22 @@ 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. +*/
[Severity: Low]
This isn't a bug, but does this multi-line comment follow the BPF subsystem style guidelines? It looks like the opening /* should be on its own line.
tools/lib/bpf/gen_loader.c static void emit_signature_match(struct bpf_gen *gen) · line 611
Quoted 3 lines
tools/lib/bpf/gen_loader.c +3
-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_DW ,BPF_REG_2 ,BPF_REG_1 ,SHA256_DWORD_SIZE *sizeof (__u64)));
[Severity: High]
Will this 8-byte read pass the verifier on 32-bit architectures?
While __bpf_md_ptr pads the C struct field to 8 bytes, unnamed bitfields are not emitted in BTF. The kernel's BTF will represent the pointer member with a size of 4 bytes on 32-bit systems.
Since btf_struct_walk() enforces that the memory access size exactly matches the BTF pointer size, it seems the verifier will reject this BPF_DW access with a cannot access ptr member error on 32-bit architectures.
Quoted 4 lines
tools/testing/selftests/bpf/progs/verifier_map_ptr.c +0
-0
[ ... ]
tools/testing/selftests/bpf/progs/verifier_map_ptr.c __naked void bpf_map_ptr_write_rejected(void) · line 77
Quoted 11 lines
tools/testing/selftests/bpf/progs/verifier_map_ptr.c +4
-2@@ -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 (8 bytes via +* __bpf_md_ptr), then ops at offset 40. Reading at offset 41 crosses into +* the ops pointer and must be rejected. */
[Severity: Low]
This isn't a bug, but should this multi-line comment also have the opening /* on a standalone line according to the BPF style guidelines?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v2 2/2] bpf, libbpf: reject non-exclusive metadata maps in the signed loader
Quoted 10 lines · On Thu, May 21, 2026 at 6:52 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The BPF verifier will reject thegen_loaderprogram on 32-bit architectures because it accesses a 4-byte pointer using an 8-byte read.
- [Low] Multi-line comments do not follow the mandatory BPF subsystem standard.
--commit
878a81f0d9571d5734eccfbe01c3dfe58050f954
Author: KP Singh <[email protected]>bpf, libbpf: reject non-exclusive metadata maps in the signed loader
[...]
Quoted 10 lines
tools/lib/bpf/gen_loader.c static void emit_signature_match(struct bpf_gen *gen) · line 608
Quoted 5 lines
tools/lib/bpf/gen_loader.c +5
-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. +*/ [Severity: Low]
This isn't a bug, but does this multi-line comment follow the BPF subsystem style guidelines? It looks like the opening /* should be on its own line.
Sashiko, this is how we comment in BPF.
Quoted 8 lines
tools/lib/bpf/gen_loader.c static void emit_signature_match(struct bpf_gen *gen) · line 611
Quoted 4 lines
tools/lib/bpf/gen_loader.c +3
-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_DW ,BPF_REG_2 ,BPF_REG_1 ,SHA256_DWORD_SIZE *sizeof (__u64)));[Severity: High]
Will this 8-byte read pass the verifier on 32-bit architectures?
Quoted 8 lines
While
__bpf_md_ptrpads the C struct field to 8 bytes, unnamed bitfields are not emitted in BTF. The kernel's BTF will represent the pointer member with a size of 4 bytes on 32-bit systems.Since
btf_struct_walk()enforces that the memory access size exactly matches the BTF pointer size, it seems the verifier will reject thisBPF_DWaccess with a cannot access ptr member error on 32-bit architectures.
Good find Sashiko!
__bpf_md_ptr doesn't help — unnamed bitfields are stripped
from BTF:
$ bpftool btf dump file vmlinux | grep -A1 '^\[1462\]'
[1462] UNION '(anon)' size=8 vlen=1
'excl_prog_sha' type_id=17 bits_offset=0vlen=1, no padding. On 32-bit the pointer is 4 bytes, BPF_DW is 8, btf_struct_walk rejects (msize != size). BPF_W doesn't work on 64-bit either (4 != 8).
gen_loader needs target pointer size tracking (like swapped_endian) to emit the right sized read. Will revert __bpf_md_ptr and do that As a follow-up, I need to re-spin anyway since I messed up PATCH "bpf" and probably confused the CI.
- KP
Quoted 3 lines
Quoted 3 lines
tools/testing/selftests/bpf/progs/verifier_map_ptr.c +0
-1--- a/tools/testing/selftests/bpf/progs/verifier_map_ptr.c
[...]
Quoted 2 lines
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v2 2/2] bpf, libbpf: reject non-exclusive metadata maps in the signed loader
Quoted 21 lines · KP Singh
Quoted 10 lines · On Thu, May 21, 2026 at 6:52 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The BPF verifier will reject thegen_loaderprogram on 32-bit architectures because it accesses a 4-byte pointer using an 8-byte read.
- [Low] Multi-line comments do not follow the mandatory BPF subsystem standard.
--commit
878a81f0d9571d5734eccfbe01c3dfe58050f954
Author: KP Singh <[email protected]>bpf, libbpf: reject non-exclusive metadata maps in the signed loader
[...]
Quoted 6 lines
Quoted 5 lines
tools/lib/bpf/gen_loader.c +5
-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. +*/
Quoted 6 lines
Quoted 4 lines
[Severity: Low]
This isn't a bug, but does this multi-line comment follow the BPF subsystem style guidelines? It looks like the opening /* should be on its own line.Sashiko, this is how we comment in BPF.
No. Sashiko is correct. bpf doesn't use the old networking style anymore.
Quoted 26 lines
Quoted 8 lines
tools/lib/bpf/gen_loader.c static void emit_signature_match(struct bpf_gen *gen) · line 611
Quoted 4 lines
tools/lib/bpf/gen_loader.c +3
-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_DW ,BPF_REG_2 ,BPF_REG_1 ,SHA256_DWORD_SIZE *sizeof (__u64)));[Severity: High]
Will this 8-byte read pass the verifier on 32-bit architectures?Quoted 8 lines
While
__bpf_md_ptrpads the C struct field to 8 bytes, unnamed bitfields are not emitted in BTF. The kernel's BTF will represent the pointer member with a size of 4 bytes on 32-bit systems.Since
btf_struct_walk()enforces that the memory access size exactly matches the BTF pointer size, it seems the verifier will reject thisBPF_DWaccess with a cannot access ptr member error on 32-bit architectures.Good find Sashiko!
__bpf_md_ptrdoesn't help — unnamed bitfields are stripped
from BTF:$ bpftool btf dump file vmlinux | grep -A1 '^\[1462\]' [1462] UNION '(anon)' size=8 vlen=1 'excl_prog_sha' type_id=17 bits_offset=0
Quoted 8 lines
vlen=1, no padding. On 32-bit the pointer is 4 bytes,
BPF_DWis 8,btf_struct_walkrejects (msize != size).BPF_Wdoesn't work on 64-bit either (4 != 8).
gen_loaderneeds target pointer size tracking (likeswapped_endian) to emit the right sized read. Will revert__bpf_md_ptrand do that As a follow-up, I need to re-spin anyway since I messed up PATCH "bpf" and probably confused the CI.
hmm. I think AI is wrong here. size=8 is all we need.
I think BPF_DW should work. Pls double check. Don't rely on AI guesses.
Re: [PATCH v2 2/2] bpf, libbpf: reject non-exclusive metadata maps in the signed loader
Quoted 62 lines · Alexei Starovoitov
Quoted 21 lines · KP Singh
Quoted 10 lines · On Thu, May 21, 2026 at 6:52 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The BPF verifier will reject thegen_loaderprogram on 32-bit architectures because it accesses a 4-byte pointer using an 8-byte read.
- [Low] Multi-line comments do not follow the mandatory BPF subsystem standard.
--commit
878a81f0d9571d5734eccfbe01c3dfe58050f954
Author: KP Singh <[email protected]>bpf, libbpf: reject non-exclusive metadata maps in the signed loader
[...]
Quoted 6 lines
Quoted 5 lines
(quoted diff) +5
-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. + */Quoted 6 lines
Quoted 4 lines
[Severity: Low]
This isn't a bug, but does this multi-line comment follow the BPF subsystem style guidelines? It looks like the opening /* should be on its own line.Sashiko, this is how we comment in BPF.
No. Sashiko is correct. bpf doesn't use the old networking style anymore.
Quoted 30 lines
Quoted 8 lines
Review
Quoted 4 lines
(quoted diff) +3
-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_DW, BPF_REG_2, BPF_REG_1, SHA256_DWORD_SIZE * sizeof(__u64)));[Severity: High]
Will this 8-byte read pass the verifier on 32-bit architectures?Quoted 8 lines
While
__bpf_md_ptrpads the C struct field to 8 bytes, unnamed bitfields are not emitted in BTF. The kernel's BTF will represent the pointer member with a size of 4 bytes on 32-bit systems.Since
btf_struct_walk()enforces that the memory access size exactly matches the BTF pointer size, it seems the verifier will reject thisBPF_DWaccess with a cannot access ptr member error on 32-bit architectures.Good find Sashiko!
__bpf_md_ptrdoesn't help — unnamed bitfields are stripped
from BTF:$ bpftool btf dump file vmlinux | grep -A1 '^\[1462\]' [1462] UNION '(anon)' size=8 vlen=1 'excl_prog_sha' type_id=17 bits_offset=0vlen=1, no padding. On 32-bit the pointer is 4 bytes,
BPF_DWis 8,btf_struct_walkrejects (msize != size).BPF_Wdoesn't work on 64-bit either (4 != 8).
Quoted 7 lines
Quoted 4 lines
gen_loaderneeds target pointer size tracking (likeswapped_endian) to emit the right sized read. Will revert__bpf_md_ptrand do that As a follow-up, I need to re-spin anyway since I messed up PATCH "bpf" and probably confused the CI.hmm. I think AI is wrong here. size=8 is all we need.
I thinkBPF_DWshould work. Pls double check. Don't rely on AI guesses.
I wrestled with the AI to create some test code, it still does seem to fail, anything missing in this diff?
https://raw.githubusercontent.com/sinkap/linux/bpf-md-ptr-32bit-test/test_bpf_32bit.sh
commit 13fa71a6e16b6f024da481bb7a733c312974ed32 (HEAD)
Author: KP Singh <[email protected]> Date: Fri May 22 05:18:32 2026 +0200
bpf: use __bpf_md_ptr for excl_prog_sha in struct bpf_map
include/linux/bpf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
commit 13fa71a6e16b6f024da481bb7a733c312974ed32 (HEAD)
Author: KP Singh <[email protected]> Date: Fri May 22 05:18:32 2026 +0200
bpf: use __bpf_md_ptr for excl_prog_sha in struct bpf_map
include/linux/bpf.h +1 -1
@@ -295,7 +295,7 @@ struct bpf_map_owner {struct bpf_map {u8 sha[SHA256_DIGEST_SIZE ]; -char *excl_prog_sha; + __bpf_md_ptr(char *, excl_prog_sha);const struct bpf_map_ops *ops;struct bpf_map *inner_map_meta;#ifdef CONFIG_SECURITY
Building (deps, selftests, 32-bit kernel, rootfs)... Full log: /tmp/bpf32-build.log
done.
=== 32-bit BTF ===
[79128] STRUCT 'bpf_map' size=224 vlen=33
'sha' type_id=70 bits_offset=0
'(anon)' type_id=1349 bits_offset=256
'ops' type_id=1351 bits_offset=320
'inner_map_meta' type_id=1265 bits_offset=352
'security' type_id=30 bits_offset=384
'map_type' type_id=79193 bits_offset=416
'key_size' type_id=139067 bits_offset=448
'value_size' type_id=139067 bits_offset=480
--
=== Boot 32-bit QEMU ===
qemu-system-i386: terminating on signal 15 from pid 4116468 (/bin/bash)
=== QEMU serial output ===
[ 0.000000] Linux version 7.1.0-rc3-g13fa71a6e16b (kpsingh@kpsingh-genoa) (gcc (Ubuntu 15.2.0-16ubuntu1) 15.2.0, GNU ld (GNU Binutils for Ubuntu) 2.46) #3 SMP PREEMPT_DYNAMIC Fri May 22 05:25:14 CEST 2026 [ 0.000000] x86/CPU: Model not found in latest microcode list [ 0.000000] BIOS-provided physical RAM map: [ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] Sys
[...]
=== Running atomics signed lskel on 32-bit ===
REJECTED: load: Permission denied (errno 13)
LOG: 0: R1=ctx() R10=fp0
0: (bf) r6 = r1 ; R1=ctx() R6=ctx() 1: (bf) r1 = r10 ; R1=fp0 R10=fp0 2: (07) r1 += -136 ; R1=fp-136 3: (b7) r2 = 136 ; R2=136 4: (b7) r3 = 0 ; R3=0
5: (85) call bpf_probe_read_kernel#113 ; fp-104=ppppmmmm
fp-112=mmmmmmmm fp-120=mmmmmmmm fp-128=mmmmmmmm fp-136=mmmmmmmm 6: (05) goto pc+41
48: (18) r1 = 0xc1258000 ;
R1=map_ptr(map=__loader.map,ks=4,vs=9976)
50: (79) r2 = *(u64 *)(r1 +0) ;
R1=map_ptr(map=__loader.map,ks=4,vs=9976) R2=scalar()
51: (18) r3 = 0x687c37c5f0d1a81f ; R3=0x687c37c5f0d1a81f 53: (b7) r7 = -22 ; R7=-22 54: (5d) if r2 != r3 goto pc-48 ; R2=0x687c37c5f0d1a81f
R3=0x687c37c5f0d1a81f
55: (18) r1 = 0xc1258000 ;
R1=map_ptr(map=__loader.map,ks=4,vs=9976)
57: (79) r2 = *(u64 *)(r1 +8) ;
R1=map_ptr(map=__loader.map,ks=4,vs=9976) R2=scalar()
58: (18) r3 = 0x50bbf46171f6280f ; R3=0x50bbf46171f6280f 60: (b7) r7 = -22 ; R7=-22 61: (5d) if r2 != r3 goto pc-55 ; R2=0x50bbf46171f6280f
R3=0x50bbf46171f6280f
62: (18) r1 = 0xc1258000 ;
R1=map_ptr(map=__loader.map,ks=4,vs=9976)
64: (79) r2 = *(u64 *)(r1 +16) ;
R1=map_ptr(map=__loader.map,ks=4,vs=9976) R2=scalar()
65: (18) r3 = 0x62d310438c86b5f6 ; R3=0x62d310438c86b5f6 67: (b7) r7 = -22 ; R7=-22 68: (5d) if r2 != r3 goto pc-62 ; R2=0x62d310438c86b5f6
R3=0x62d310438c86b5f6
69: (18) r1 = 0xc1258000 ;
R1=map_ptr(map=__loader.map,ks=4,vs=9976)
71: (79) r2 = *(u64 *)(r1 +24) ;
R1=map_ptr(map=__loader.map,ks=4,vs=9976) R2=scalar()
72: (18) r3 = 0xc5a92d04886f9627 ; R3=0xc5a92d04886f9627 74: (b7) r7 = -22 ; R7=-22 75: (5d) if r2 != r3 goto pc-69 ; R2=0xc5a92d04886f9627
R3=0xc5a92d04886f9627
76: (18) r1 = 0xc1258000 ;
R1=map_ptr(map=__loader.map,ks=4,vs=9976)
78: (79) r2 = *(u64 *)(r1 +32)
cannot access ptr member excl_prog_sha with moff 0 in struct (anon) with off 0 size 8
processed 29 insns (limit 1000000) max_states_per_insn 0 total_states 2 peak_states 2 mark_read 0
Re: [PATCH v2 2/2] bpf, libbpf: reject non-exclusive metadata maps in the signed loader
Quoted 26 lines · KP Singh
Quoted 23 lines · Alexei Starovoitov
Quoted 21 lines · KP Singh
Quoted 10 lines · On Thu, May 21, 2026 at 6:52 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The BPF verifier will reject thegen_loaderprogram on 32-bit architectures because it accesses a 4-byte pointer using an 8-byte read.
- [Low] Multi-line comments do not follow the mandatory BPF subsystem standard.
--commit
878a81f0d9571d5734eccfbe01c3dfe58050f954
Author: KP Singh <[email protected]>bpf, libbpf: reject non-exclusive metadata maps in the signed loader
[...]
Quoted 6 lines
Quoted 5 lines
(quoted diff) +5
-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. + */
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
[Severity: Low]
This isn't a bug, but does this multi-line comment follow the BPF subsystem style guidelines? It looks like the opening /* should be on its own line.Sashiko, this is how we comment in BPF.
No. Sashiko is correct. bpf doesn't use the old networking style anymore.
Okay. TIL, I will fix it.
Quoted 1 line
Quoted 1 line
[...]
Quoted 1 line
2
peak_states2mark_read0
Re: [PATCH v2 2/2] bpf, libbpf: reject non-exclusive metadata maps in the signed loader
In reply to KP SinghQuoted 5 lines · KP Singh
Quoted 2 lines · Alexei Starovoitov
On Thu, May 21, 2026 at 9:09 PM KP Singh <[email protected]> wrote:
[...]
Quoted 20 lines
https://raw.githubusercontent.com/sinkap/linux/bpf-md-ptr-32bit-test/test_bpf_32bit.sh
commit
13fa71a6e16b6f024da481bb7a733c312974ed32(HEAD)Author: KP Singh <[email protected]> Date: Fri May 22 05:18:32 2026 +0200bpf: use
__bpf_md_ptrforexcl_prog_shain structbpf_mapinclude/linux/bpf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)commit
13fa71a6e16b6f024da481bb7a733c312974ed32(HEAD)Author: KP Singh <[email protected]> Date: Fri May 22 05:18:32 2026 +0200bpf: use
__bpf_md_ptrforexcl_prog_shain structbpf_mapinclude/linux/bpf.h +0
-0
[...]
Quoted 17 lines
=== Boot 32-bit QEMU ===
qemu-system-i386: terminating on signal 15 from pid 4116468 (/bin/bash)=== QEMU serial output ===
[ 0.000000] Linux version 7.1.0-rc3-g13fa71a6e16b (kpsingh@kpsingh-genoa) (gcc (Ubuntu 15.2.0-16ubuntu1) 15.2.0, GNU ld (GNU Binutils for Ubuntu) 2.46) #3 SMP PREEMPT_DYNAMIC Fri May 22 05:25:14 CEST 2026 [ 0.000000] x86/CPU: Model not found in latest microcode list [ 0.000000] BIOS-provided physical RAM map: [ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] Sys[...]
=== Running atomics signed lskel on 32-bit ===
REJECTED: load: Permission denied (errno 13)
LOG: 0: R1=ctx() R10=fp0
[...]
Quoted 3 lines
R1=map_ptr(map=__loader.map,ks=4,vs=9976)
78: (79) r2 = *(u64 *)(r1 +32)
cannot access ptr memberexcl_prog_shawith moff 0 in struct (anon)
I think we can address this as a follow-up and I'm sending v3.
We don't test it in the CI and I spent way too much time proving /
debugging despite help from our AI friends.
- KP
Quoted 3 lines
with off 0 size 8
processed 29 insns (limit 1000000)max_states_per_insn0total_states2peak_states2mark_read0
Re: [PATCH v2 1/2] libbpf: fix off-by-one in emit_signature_match jump offset
In reply to KP SinghQuoted 7 lines · KP Singh
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=-ERANGEalready marks the generation as failed.Fixes fb2b0e290147 ("libbpf: Update light skeleton for signing")
The fb2b0e290147 does not touch emit_signature_match as pointed out.
Quoted 1 line
Signed-off-by KP Singh <[email protected]>
Other than that, lgtm:
Quoted 10 lines
--- tools/lib/bpf/gen_loader.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)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 ));
It would make sense to move this into a separate commit, and then also clean up the same in emit_check_err() in one go?
Quoted 3 lines
tools/lib/bpf/gen_loader.c +0
-0} } }
Daniel Borkmann
maintainerRe: [PATCH v2 2/2] bpf, libbpf: reject non-exclusive metadata maps in the signed loader
In reply to KP SinghQuoted 46 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.
Place
excl_prog_sharight after sha[] in structbpf_mapand havegen_loaderbail with-EINVALwhen it is NULL, viaBPF_PSEUDO_MAP_IDXat fixed offset 32. Declareexcl_prog_shawith__bpf_md_ptrso the 8-byteBPF_LDX_MEMread works on 32-bit kernels.Fixes fb2b0e290147 ("libbpf: Update light skeleton for signing")Signed-off-by KP Singh <[email protected]>--- include/linux/bpf.h | 2 +- tools/lib/bpf/gen_loader.c | 16 ++++++++++++++++ .../selftests/bpf/progs/verifier_map_ptr.c | 10 ++++++---- 3 files changed, 23 insertions(+), 5 deletions(-)include/linux/bpf.h +1
-1@@ -295,6 +295,7 @@ struct bpf_map_owner {struct bpf_map {u8 sha[SHA256_DIGEST_SIZE ]; + __bpf_md_ptr(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 +0
-0
tools/lib/bpf/gen_loader.c static void emit_signature_match(struct bpf_gen *gen) · line 608
Quoted 9 lines
tools/lib/bpf/gen_loader.c +6
-0@@ -600,6 +600,22 @@ 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. +*/
We could probably add sth like this somewhere in the bpf kernel side :
/* See: emit_signature_match() in tools/lib/bpf/gen_loader.c */
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));tools/lib/bpf/gen_loader.c static void emit_signature_match(struct bpf_gen *gen) · line 611
Quoted 3 lines
tools/lib/bpf/gen_loader.c +3
-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_DW ,BPF_REG_2 ,BPF_REG_1 ,SHA256_DWORD_SIZE *sizeof (__u64)));
nit: SHA256_DWORD_SIZE is SHA256_DIGEST_LENGTH / sizeof(__u64), can we just use
SHA256_DIGEST_LENGTH ?
Quoted 10 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 ; + } } void bpf_gen__record_attach_target(struct bpf_gen *gen, const char *attach_name,
Daniel Borkmann
maintainerRe: [PATCH v2 2/2] bpf, libbpf: reject non-exclusive metadata maps in the signed loader
Quoted 46 lines · Daniel Borkmann
Quoted 45 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.
Place
excl_prog_sharight after sha[] in structbpf_mapand havegen_loaderbail with-EINVALwhen it is NULL, viaBPF_PSEUDO_MAP_IDXat fixed offset 32. Declareexcl_prog_shawith__bpf_md_ptrso the 8-byteBPF_LDX_MEMread works on 32-bit kernels.Fixes fb2b0e290147 ("libbpf: Update light skeleton for signing")Signed-off-by KP Singh <[email protected]>---
include/linux/bpf.h | 2 +- tools/lib/bpf/gen_loader.c | 16 ++++++++++++++++ .../selftests/bpf/progs/verifier_map_ptr.c | 10 ++++++----3 files changed, 23 insertions(+), 5 deletions(-)include/linux/bpf.h +1
-0@@ -295,6 +295,7 @@ struct bpf_map_owner { struct bpf_map { u8 sha[SHA256_DIGEST_SIZE]; + __bpf_md_ptr(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(enumbtf_field_typetype)tools/lib/bpf/gen_loader.c +0
-0@@ -600,6 +600,22 @@ static void emit_signature_match(struct bpf_gen *gen) gen->error = -ERANGE;
Quoted 8 lines
tools/lib/bpf/gen_loader.c static void emit_signature_match(struct bpf_gen *gen) · line 608
Quoted 8 lines
tools/lib/bpf/gen_loader.c +6
-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. +*/
(One other small nit: it probably makes sense to first check the exclusivity before we compare the map->sha[], so this could be reordered inside the emit_signature_match.)
Quoted 24 lines
We could probably add sth like this somewhere in the bpf kernel side :
/* See:
emit_signature_match()intools/lib/bpf/gen_loader.c*/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));tools/lib/bpf/gen_loader.c static void emit_signature_match(struct bpf_gen *gen) · line 611
Quoted 3 lines
tools/lib/bpf/gen_loader.c +3
-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_DW, BPF_REG_2, BPF_REG_1, SHA256_DWORD_SIZE * sizeof(__u64)));nit:
SHA256_DWORD_SIZEisSHA256_DIGEST_LENGTH/sizeof(__u64), can we just use
SHA256_DIGEST_LENGTH?Quoted 9 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; + } } void bpf_gen__record_attach_target(struct bpf_gen *gen, const char *attach_name,