Skip to contentSkip to conversation

bpf: Propagate btf_add_type() errors in btf_check_all_metas()

PATCH Applied Feng Yang 4 participants 4 messages Oct 22 – Oct 24 BPF

lore
Share & tools
lore

Feng Yang

author
#0 lore raw

PATCH 1/1 bpf: Propagate btf_add_type() errors in btf_check_all_metas()

From: Feng Yang <[email protected]>

btf_check_all_metas() ignores the return value of btf_add_type(), so a type that could not be registered is silently dropped while the loop keeps walking over the type section.

Both failure modes are reachable:

- -E2BIG. Once the types array reaches its capacity (BTF_MAX_TYPE), every following btf_add_type() fails the same way, so btf_check_all_metas() still returns 0 and the BTF object is created with btf->nr_types capped while hdr->type_len claims more records.

- -ENOMEM. btf->nr_types is not incremented and the array is not grown, so the next type is recorded at the slot of the dropped one and btf->types[] stays off by one with respect to the type ids for all remaining types. btf_type_by_id() then returns the wrong type for those ids for the whole lifetime of the object, and the last type is silently left unresolved.

Propagate the error so that the object is rejected instead.

Fixes 69b693f0aefa ("bpf: btf: Introduce BPF Type Format (BTF)")
---
 kernel/bpf/btf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/bpf/btf.c +5 -1
index d870bc5e50bc..80b6f96263f1 100644
@@ -5459,6 +5459,7 @@ static int btf_check_all_metas(struct btf_verifier_env *env)
 	struct btf *btf = env->btf;
 	struct btf_header *hdr;
 	void *cur, *end;
+	int err;
 
 	hdr = &btf->hdr;
 	cur = btf->nohdr_data + hdr->type_off;
@@ -5473,7 +5474,10 @@ static int btf_check_all_metas(struct btf_verifier_env *env)
 		if (meta_size < 0)
 			return meta_size;
 
-		btf_add_type(env, t);
+		err = btf_add_type(env, t);
+		if (err)
+			return err;
+
 		cur += meta_size;
 		env->log_type_id++;
 	}
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 20 lines · Feng Yang

From: Feng Yang <[email protected]>

btf_check_all_metas() ignores the return value of btf_add_type(), so a type that could not be registered is silently dropped while the loop keeps walking over the type section.

Both failure modes are reachable:

- -E2BIG. Once the types array reaches its capacity (BTF_MAX_TYPE), every following btf_add_type() fails the same way, so btf_check_all_metas() still returns 0 and the BTF object is created with btf->nr_types capped while hdr->type_len claims more records.

- -ENOMEM. btf->nr_types is not incremented and the array is not grown, so the next type is recorded at the slot of the dropped one and btf->types[] stays off by one with respect to the type ids for all remaining types. btf_type_by_id() then returns the wrong type for those ids for the whole lifetime of the object, and the last type is silently left unresolved.

Quoted 4 lines

Propagate the error so that the object is rejected instead.

Fixes 69b693f0aefa ("bpf: btf: Introduce BPF Type Format (BTF)")

As sashiko pointed out, also makes sense in the context of this other
patch: bpf: Fix out-of-bounds btf->types write for split BTF[^1].

It seems it dates to the original introduction of this codebase with no good reasons given the two points above listed by Feng.

[^1]: https://lore.kernel.org/bpf/[email protected]/

Quoted 31 lines
---
 kernel/bpf/btf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/bpf/btf.c +5 -1
index d870bc5e50bc..80b6f96263f1 100644
@@ -5459,6 +5459,7 @@ static int btf_check_all_metas(struct btf_verifier_env *env)
 	struct btf *btf = env->btf;
 	struct btf_header *hdr;
 	void *cur, *end;
+	int err;
 
 	hdr = &btf->hdr;
 	cur = btf->nohdr_data + hdr->type_off;
@@ -5473,7 +5474,10 @@ static int btf_check_all_metas(struct btf_verifier_env *env)
 		if (meta_size < 0)
 			return meta_size;
 
-		btf_add_type(env, t);
+		err = btf_add_type(env, t);
+		if (err)
+			return err;
+
 		cur += meta_size;
 		env->log_type_id++;
 	}
Signature · 1 line
2.25.1
Acked-by
#2 lore raw
Quoted 21 lines · Mahe Tardy
Quoted 20 lines · Feng Yang

From: Feng Yang <[email protected]>

btf_check_all_metas() ignores the return value of btf_add_type(), so a type that could not be registered is silently dropped while the loop keeps walking over the type section.

Both failure modes are reachable:

- -E2BIG. Once the types array reaches its capacity (BTF_MAX_TYPE), every following btf_add_type() fails the same way, so btf_check_all_metas() still returns 0 and the BTF object is created with btf->nr_types capped while hdr->type_len claims more records.

- -ENOMEM. btf->nr_types is not incremented and the array is not grown, so the next type is recorded at the slot of the dropped one and btf->types[] stays off by one with respect to the type ids for all remaining types. btf_type_by_id() then returns the wrong type for those ids for the whole lifetime of the object, and the last type is silently left unresolved.

Quoted 7 lines
Quoted 4 lines

Propagate the error so that the object is rejected instead.

Fixes 69b693f0aefa ("bpf: btf: Introduce BPF Type Format (BTF)")

Thanks for the fix!

Quoted 40 lines

As sashiko pointed out, also makes sense in the context of this other
patch: bpf: Fix out-of-bounds btf->types write for split BTF[^1].

It seems it dates to the original introduction of this codebase with no good reasons given the two points above listed by Feng.

[^1]: https://lore.kernel.org/bpf/[email protected]/

Quoted 31 lines
---
 kernel/bpf/btf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/bpf/btf.c +5 -1
index d870bc5e50bc..80b6f96263f1 100644
@@ -5459,6 +5459,7 @@ static int btf_check_all_metas(struct btf_verifier_env *env)
 	struct btf *btf = env->btf;
 	struct btf_header *hdr;
 	void *cur, *end;
+	int err;
 
 	hdr = &btf->hdr;
 	cur = btf->nohdr_data + hdr->type_off;
@@ -5473,7 +5474,10 @@ static int btf_check_all_metas(struct btf_verifier_env *env)
 		if (meta_size < 0)
 			return meta_size;
 
-		btf_add_type(env, t);
+		err = btf_add_type(env, t);
+		if (err)
+			return err;
+
 		cur += meta_size;
 		env->log_type_id++;
 	}
Signature · 1 line
2.25.1
#3 lore raw
In reply to Feng Yang
This patch was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf.git (master)
by Alexei Starovoitov <[email protected]>:

Quoted 9 lines · On Thu, 22 Oct 2026 14:47:27 +0800 you wrote:

From: Feng Yang <[email protected]>

btf_check_all_metas() ignores the return value of btf_add_type(), so a type that could not be registered is silently dropped while the loop keeps walking over the type section.

Both failure modes are reachable:

[...]

Here is the summary with links:
- [bpf-next] bpf: Propagate btf_add_type() errors in btf_check_all_metas()

    https://git.kernel.org/bpf/bpf/c/fb2b27d214c3

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html