Skip to contentSkip to conversation

bpf: Fix error checks against bpf_get_btf_vmlinux().

PATCH Applied [email protected] 2 participants 2 messages Jan 26, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 bpf: Fix error checks against bpf_get_btf_vmlinux().

From: Kui-Feng Lee <[email protected]>

Check whether the returned pointer is NULL. Previously, it was assumed that an error code would be returned if BTF is not available or fails to parse. However, it actually returns NULL if BTF is disabled.

In the function check_struct_ops_btf_id(), we have stopped using btf_vmlinux as a backup because attach_btf is never null when attach_btf_id is set. However, the function test_libbpf_probe_prog_types() in libbpf_probes.c does not set both attach_btf_obj_fd and attach_btf_id, resulting in attach_btf being null, and it expects ENOTSUPP as a result. So, if attach_btf_id is not set, it will return ENOTSUPP.

Changes from v1:

- Remove an unnecessary NULL check in check_struct_ops_btf_id()

Reported-by [email protected]
Reported-by [email protected]
Fixes fcc2c1fb0651 ("bpf: pass attached BTF to the bpf_struct_ops subsystem")
---
 kernel/bpf/bpf_struct_ops.c | 2 ++
 kernel/bpf/verifier.c       | 5 ++++-
 2 files changed, 6 insertions(+), 1 deletion(-)
kernel/bpf/bpf_struct_ops.c +2 -0
index defc052e4622..0decd862dfe0 100644
@@ -669,6 +669,8 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
 		btf = bpf_get_btf_vmlinux();
 		if (IS_ERR(btf))
 			return ERR_CAST(btf);
+		if (!btf)
+			return ERR_PTR(-ENOTSUPP);
 	}
 
 	st_ops_desc = bpf_struct_ops_find_value(btf, attr->btf_vmlinux_value_type_id);
kernel/bpf/verifier.c +4 -1
index fe833e831cb6..c5d68a9d8acc 100644
@@ -20298,7 +20298,10 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env)
 		return -EINVAL;
 	}
 
-	btf = prog->aux->attach_btf ?: bpf_get_btf_vmlinux();
+	if (!prog->aux->attach_btf_id)
+		return -ENOTSUPP;
+
+	btf = prog->aux->attach_btf;
 	if (btf_is_module(btf)) {
 		/* Make sure st_ops is valid through the lifetime of env */
 		env->attach_btf_mod = btf_try_get_module(btf);
Signature · 1 line
2.34.1
#1 lore raw
This patch was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:

Quoted 6 lines · On Thu, 25 Jan 2024 18:31:13 -0800 you wrote:

From: Kui-Feng Lee <[email protected]>

Check whether the returned pointer is NULL. Previously, it was assumed that an error code would be returned if BTF is not available or fails to parse. However, it actually returns NULL if BTF is disabled.

Quoted 8 lines

In the function check_struct_ops_btf_id(), we have stopped using btf_vmlinux as a backup because attach_btf is never null when attach_btf_id is set. However, the function test_libbpf_probe_prog_types() in libbpf_probes.c does not set both attach_btf_obj_fd and attach_btf_id, resulting in attach_btf being null, and it expects ENOTSUPP as a result. So, if attach_btf_id is not set, it will return ENOTSUPP.

[...]

Here is the summary with links:
- [bpf-next,v2] bpf: Fix error checks against bpf_get_btf_vmlinux().

    https://git.kernel.org/bpf/bpf-next/c/e6be8cd5d3cf

You are awesome, thank you!

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