BPF List
 help / color / mirror / Atom feed
* [PATCH] bpf: fix special field check with variable offset access
@ 2026-11-07 13:54 Shihuang Liu
  2026-11-07 14:35 ` bot+bpf-ci
  2026-11-07 16:58 ` Alexei Starovoitov
  0 siblings, 2 replies; 4+ messages in thread
From: Shihuang Liu @ 2026-11-07 13:54 UTC (permalink / raw)
  To: bpf
  Cc: ast, daniel, andrii, eddyz87, memxor, martin.lau, song,
	yonghong.song, jolsa, emil, ihor.solodrai, Shihuang Liu

btf_struct_access() rejects direct access to special fields (kptr,
spin_lock, list/rbtree nodes, ...) of program allocated objects, but
for variable offset access the check is done at min_off only, while
the whole [min_off, max_off] range can be reached at runtime. A read
leaks the pointer; a write corrupts the refcounted pointer and crashes
the release path (bpf_obj_free_fields -> bpf_mem_free_rcu).

Check the whole [min_off, max_off + size) range against the record,
like check_map_access() already does for map values.

Fixes: 89e72f81a455 ("bpf: Varying offset access support for PTR_TO_BTF_ID pointers")
Signed-off-by: Shihuang Liu <shlomojune6@gmail.com>
---
 kernel/bpf/btf.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 8bf39098ba97a..47a64a57b370d 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -8094,7 +8094,8 @@ int btf_struct_access(struct bpf_verifier_log *log,
 		for (i = 0; i < rec->cnt; i++) {
 			struct btf_field *field = &rec->fields[i];
 			u32 offset = field->offset;
-			if (off < offset + field->size && offset < off + size) {
+			if (min_off < (s64)offset + field->size &&
+			    (s64)offset < max_off + size) {
 				bpf_log(log,
 					"direct access to %s is disallowed\n",
 					btf_field_type_name(field->type));
-- 
2.43.0

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-08 15:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-07 13:54 [PATCH] bpf: fix special field check with variable offset access Shihuang Liu
2026-11-07 14:35 ` bot+bpf-ci
2026-11-07 16:58 ` Alexei Starovoitov
2026-11-08 15:29   ` J4f J4f

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox