Skip to contentSkip to conversation

libbpf: Reject non-exclusive metadata maps in the signed loader

PATCH In review Daniel Borkmann 2 participants 6 messages Jun 1

Subsystems (3)
lore
Share & tools
lore

Daniel Borkmann

author maintainer
#0 lore raw

PATCH 1/3 libbpf: Reject non-exclusive metadata maps in the signed loader

From: KP Singh <[email protected]>

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.

While the map is frozen, the /signed loader/ must also ensure the map is exclusive, as, without exclusivity (which a hostile host could just omit when loading the loader), another BPF program with map access can mutate the contents afterwards, so the check passes on stale data.

With the extra check as part of the signed loader, it now refuses to move on with map->sha validation if the host set it up wrongly.

Fixes fb2b0e290147 ("libbpf: Update light skeleton for signing")
---
 include/linux/bpf.h        |  1 +
 kernel/bpf/syscall.c       |  7 +++++++
 tools/lib/bpf/gen_loader.c | 17 +++++++++++++++++
 3 files changed, 25 insertions(+)
include/linux/bpf.h +1 -0
index 1c6863ce89e0..6191c0c51fea 100644
@@ -296,6 +296,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 +7 -0
index 2aafd2131983..0f53ec4bacc6 100644
@@ -1588,6 +1588,13 @@ static int map_create_alloc(union bpf_attr *attr, bpfptr_t uattr, struct bpf_ver
 			err = -EFAULT;
 			goto free_map;
 		}
+
+		/* See libbpf: emit_signature_match() */
+		BUILD_BUG_ON(offsetof(struct bpf_map, excl) != SHA256_DIGEST_SIZE);
+		BUILD_BUG_ON(!__same_type(map->excl, u32));
+		BUILD_BUG_ON(offsetof(struct bpf_map, sha)  != 0);
+		BUILD_BUG_ON(!__same_type(map->sha, u8[SHA256_DIGEST_SIZE]));
+		map->excl = 1;
 	} else if (attr->excl_prog_hash_size) {
 		bpf_log(log, "Invalid excl_prog_hash_size.\n");
 		err = -EINVAL;
tools/lib/bpf/gen_loader.c +17 -0
index 3702c5944bc0..66a02039da8c 100644
@@ -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
author maintainer
#1 lore raw

PATCH 2/3 libbpf: Skip initial_value override on signed loaders

bpf_gen__map_update_elem() emits code that, when the host-supplied loader ctx provides a non-NULL map_desc[idx].initial_value, overwrites the blob value with bytes read from the host (bpf_copy_from_user / bpf_probe_read_kernel) before the BPF_MAP_UPDATE_ELEM that populates the program's .data/.rodata/.bss maps.

This override runs after emit_signature_match() has validated map->sha[], and initial_value is part of neither the signed loader instructions nor the hashed data blob. For a signed loader this lets an untrusted host substitute global-variable contents into a program whose code carries a valid signature, thus weakening what the signature attests to.

The blob already contains the signer-provided value (added via add_data() and covered by the embedded, signed hash), so simply skip emitting the override for signed loaders (gen_hash). Runtime initialization stays available for the unsigned light-skeleton path as before. The jump offsets within the override block are internal to it, so guarding the whole block leaves them unchanged.

Fixes ea923080c145 ("libbpf: Embed and verify the metadata hash in the loader")
---
 tools/lib/bpf/gen_loader.c | 39 +++++++++++++++++++++++---------------
 1 file changed, 24 insertions(+), 15 deletions(-)
tools/lib/bpf/gen_loader.c +24 -15
index 66a02039da8c..a5d9c7a5261b 100644
@@ -1187,27 +1187,36 @@ void bpf_gen__map_update_elem(struct bpf_gen *gen, int map_idx, void *pvalue,
 	value = add_data(gen, pvalue, value_size);
 	key = add_data(gen, &zero, sizeof(zero));
 
-	/* if (map_desc[map_idx].initial_value) {
+	/*
+	 * if (map_desc[map_idx].initial_value) {
 	 *    if (ctx->flags & BPF_SKEL_KERNEL)
 	 *        bpf_probe_read_kernel(value, value_size, initial_value);
 	 *    else
 	 *        bpf_copy_from_user(value, value_size, initial_value);
 	 * }
+	 *
+	 * The runtime initial_value comes from the host-supplied loader
+	 * ctx and would overwrite the blob value after emit_signature_match()
+	 * has already validated map->sha[]. For a signed loader (gen_hash)
+	 * the attested blob value must be authoritative, so skip the override
+	 * and leave the hashed value in place.
 	 */
-	emit(gen, BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_6,
-			      sizeof(struct bpf_loader_ctx) +
-			      sizeof(struct bpf_map_desc) * map_idx +
-			      offsetof(struct bpf_map_desc, initial_value)));
-	emit(gen, BPF_JMP_IMM(BPF_JEQ, BPF_REG_3, 0, 8));
-	emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE,
-					 0, 0, 0, value));
-	emit(gen, BPF_MOV64_IMM(BPF_REG_2, value_size));
-	emit(gen, BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_6,
-			      offsetof(struct bpf_loader_ctx, flags)));
-	emit(gen, BPF_JMP_IMM(BPF_JSET, BPF_REG_0, BPF_SKEL_KERNEL, 2));
-	emit(gen, BPF_EMIT_CALL(BPF_FUNC_copy_from_user));
-	emit(gen, BPF_JMP_IMM(BPF_JA, 0, 0, 1));
-	emit(gen, BPF_EMIT_CALL(BPF_FUNC_probe_read_kernel));
+	if (!OPTS_GET(gen->opts, gen_hash, false)) {
+		emit(gen, BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_6,
+				      sizeof(struct bpf_loader_ctx) +
+				      sizeof(struct bpf_map_desc) * map_idx +
+				      offsetof(struct bpf_map_desc, initial_value)));
+		emit(gen, BPF_JMP_IMM(BPF_JEQ, BPF_REG_3, 0, 8));
+		emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE,
+						 0, 0, 0, value));
+		emit(gen, BPF_MOV64_IMM(BPF_REG_2, value_size));
+		emit(gen, BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_6,
+				      offsetof(struct bpf_loader_ctx, flags)));
+		emit(gen, BPF_JMP_IMM(BPF_JSET, BPF_REG_0, BPF_SKEL_KERNEL, 2));
+		emit(gen, BPF_EMIT_CALL(BPF_FUNC_copy_from_user));
+		emit(gen, BPF_JMP_IMM(BPF_JA, 0, 0, 1));
+		emit(gen, BPF_EMIT_CALL(BPF_FUNC_probe_read_kernel));
+	}
 
 	map_update_attr = add_data(gen, &attr, attr_size);
 	pr_debug("gen: map_update_elem: idx %d, value: off %d size %d, attr: off %d size %d\n",
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH 3/3 libbpf: Skip max_entries override on signed loaders

In reply to Daniel Borkmann

bpf_gen__map_create() lets the host-supplied loader ctx override a map's max_entries at runtime (map_desc[idx].max_entries, when non-zero). This is how the light skeleton sizes maps to the target machine, but it happens after emit_signature_match() and is covered by neither the signed loader instructions nor the hashed blob.

For a signed loader this means an untrusted host can re-dimension the program's maps, outside what the signature attests to. Gate the override on gen_hash so signed loaders use the signer-provided max_entries baked into the blob.

Fixes ea923080c145 ("libbpf: Embed and verify the metadata hash in the loader")
---
 tools/lib/bpf/gen_loader.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
tools/lib/bpf/gen_loader.c +11 -2
index a5d9c7a5261b..66e13566bc31 100644
@@ -546,13 +546,22 @@ void bpf_gen__map_create(struct bpf_gen *gen,
 	default:
 		break;
 	}
-	/* conditionally update max_entries */
-	if (map_idx >= 0)
+
+	/*
+	 * Conditionally update max_entries from the host-supplied loader
+	 * ctx. This sizes the map at runtime, but for a signed loader
+	 * (gen_hash) it would let an untrusted host re-dimension the
+	 * program's maps after emit_signature_match(), outside what the
+	 * signature attests to. Keep the signer-provided max_entries
+	 * baked into the blob in that case.
+	 */
+	if (map_idx >= 0 && !OPTS_GET(gen->opts, gen_hash, false))
 		move_ctx2blob(gen, attr_field(map_create_attr, max_entries), 4,
 			      sizeof(struct bpf_loader_ctx) +
 			      sizeof(struct bpf_map_desc) * map_idx +
 			      offsetof(struct bpf_map_desc, max_entries),
 			      true /* check that max_entries != 0 */);
+
 	/* emit MAP_CREATE command */
 	emit_sys_bpf(gen, BPF_MAP_CREATE, map_create_attr, attr_size);
 	debug_ret(gen, "map_create %s idx %d type %d value_size %d value_btf_id %d",
Signature · 1 line
2.43.0
#3 lore raw
In reply to Daniel Borkmann
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [Critical] The BPF map exclusivity feature (excl_prog_sha) can be bypassed via ARRAY_OF_MAPS, allowing a malicious BPF program to mutate a frozen exclusive metadata map and defeating the signed loader's TOCTOU defense.

Pre-existing issues:
- [High] Concurrent bpf_map_get_info_by_fd() calls on a frozen array map trigger an unprotected data race and undefined behavior due to overlapping memcpy(). --

commit c6a3102cfade62f836db60603418dd29d86695ef

Author: KP Singh <[email protected]>
Date:   Mon Jun 1 10:03:04 2026 +0000

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.

While the map is frozen, the /signed loader/ must also ensure the map is exclusive, as, without exclusivity (which a hostile host could just omit when loading the loader), another BPF program with map access can mutate the contents afterwards, so the check passes on stale data.

With the extra check as part of the signed loader, it now refuses to move on with map->sha validation if the host set it up wrongly.

include/linux/bpf.h struct bpf_map_owner { · line 299

Quoted 9 lines
include/linux/bpf.h +1 -0
index 1c6863ce89e0c..6191c0c51fea3 100644
@@ -296,6 +296,7 @@ struct bpf_map_owner {
 
 struct bpf_map {
 	u8 sha[SHA256_DIGEST_SIZE];
+	u32 excl;

[Severity: High]
This is a pre-existing issue, but does concurrent execution of bpf_map_get_info_by_fd() on a frozen array map trigger an unprotected data race and undefined behavior?

In bpf_map_get_info_by_fd(), map->sha is passed directly as the hash_buf without holding any locks:

kernel/bpf/syscall.c:bpf_map_get_info_by_fd() {
    ...
    err = map->ops->map_get_hash(map, SHA256_DIGEST_SIZE, map->sha);
    ...
}

For array maps, array_map_get_hash() uses sha256() to write directly to hash_buf, meaning concurrent calls could race while writing to map->sha.

Additionally, array_map_get_hash() finishes with this copy:

kernel/bpf/arraymap.c:array_map_get_hash() {
    ...
    memcpy(array->map.sha, hash_buf, sizeof(array->map.sha));
    ...
}

Because hash_buf is exactly map->sha, this evaluates to memcpy(map->sha, map->sha, 32). Can this overlapping memcpy() cause undefined behavior?

[ ... ]

Quoted 11 lines
tools/lib/bpf/gen_loader.c +3 -0
index 3702c5944bc02..66a02039da8ce 100644
@@ -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

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 +10 -0
+	 * 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));

[Severity: Critical]
Does this exclusivity check fully prevent TOCTOU attacks, or can the exclusivity be bypassed via an array of maps?

If an attacker creates an exclusive metadata map and inserts it into an array of maps, bpf_map_meta_equal() appears to ignore excl and excl_prog_sha during validation:

kernel/bpf/map_in_map.c:bpf_map_meta_equal() {
    return meta0->map_type == meta1->map_type &&
           meta0->key_size == meta1->key_size &&
           meta0->value_size == meta1->value_size &&
           meta0->map_flags == meta1->map_flags &&
           btf_record_equal(meta0->record, meta1->record);
}

After freezing the inner metadata map and passing it to the signed loader to pass this check, couldn't a malicious BPF program bind to the outer map and mutate the inner map?

When the malicious program calls bpf_map_update_elem() on the inner map, it routes to array_map_update_elem(), which does not enforce map->frozen or map->excl_prog_sha at runtime.

Does this allow a BPF program to successfully overwrite the frozen metadata map's contents after its hash was verified, defeating the signed loader's exclusivity defense?

Quoted 3 lines
tools/lib/bpf/gen_loader.c +3 -0
+	} else {
+		gen->error = -ERANGE;
+	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#4 lore raw
Quoted 7 lines · On 6/1/26 12:32 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [Critical] The BPF map exclusivity feature (excl_prog_sha) can be bypassed via ARRAY_OF_MAPS, allowing a malicious BPF program to mutate a frozen exclusive metadata map and defeating the signed loader's TOCTOU defense.

Pre-existing issues:
- [High] Concurrent bpf_map_get_info_by_fd() calls on a frozen array map trigger an unprotected data race and undefined behavior due to overlapping memcpy().

Will take a look into the new findings.

author maintainer
#5 lore raw
Quoted 9 lines · Daniel Borkmann
Quoted 7 lines · On 6/1/26 12:32 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [Critical] The BPF map exclusivity feature (excl_prog_sha) can be bypassed via ARRAY_OF_MAPS, allowing a malicious BPF program to mutate a frozen exclusive metadata map and defeating the signed loader's TOCTOU defense.

Pre-existing issues:
- [High] Concurrent bpf_map_get_info_by_fd() calls on a frozen array map trigger an unprotected data race and undefined behavior due to overlapping memcpy().

Will take a look into the new findings.

Both findings are reasonable, will expand in a v2 and also fix up the selftest.