BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next v4] bpf: Check variable-offset accesses against special fields
@ 2026-11-18 16:21 Shihuang Liu
  2026-11-18 17:16 ` bot+bpf-ci
  2026-11-19  1:09 ` Alexei Starovoitov
  0 siblings, 2 replies; 3+ messages in thread
From: Shihuang Liu @ 2026-11-18 16:21 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann
  Cc: Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
	Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
	Emil Tsalapatis, Ihor Solodrai, Mark Brown, bpf, Shihuang Liu

btf_struct_access() rejects direct access to special fields (kptr, ...)
of program allocated objects, but for a variable offset it only checks
the access at min_off.  This is enough for compiler generated BTF where
members don't overlap, but btf_struct_check_meta() allows overlapping
members in hand-written BTF:

  struct node {
          int arr[4];             /* offset 0, size 16 */
          struct foo __kptr *p;   /* offset 8 */
  };

A 4-byte store to arr[i] passes the check at offset 0 and overwrites
p at run time for i == 2 or 3; bpf_obj_free_fields() then frees the
corrupted pointer.

btf_record_first_field() returns the first field that ends after
min_off: it is where the check has to start, not where it can end:
every field up to max_off + size can be reached.  Check the offsets the
register can really take, min_off + k * step.  No separate constant
offset path is needed: reg->step is never 0 (___mark_reg_known() sets
it to 1), so a constant offset is k == 0.

Verified with hand-written BTF: a variable-offset store to arr[i] is
now rejected with "direct access to kptr is disallowed".

Fixes: 89e72f81a455 ("bpf: Varying offset access support for PTR_TO_BTF_ID pointers")
Link: https://lore.kernel.org/bpf/20261109094723.14614-1-shlomojune6@gmail.com/
Link: https://lore.kernel.org/all/avzIz70qnJIA2t3x@sirena.org.uk/
Signed-off-by: Shihuang Liu <shlomojune6@gmail.com>

---
Changes since v3:
- Rebased on bpf-next to resolve the merge conflict with ca4395aade31a
  ("bpf: Walk each type once when looking for special fields").
---
 kernel/bpf/btf.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index e95ca39f990c6e..ffdf12db1f87cd 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -8126,11 +8126,19 @@ int btf_struct_access(struct bpf_verifier_log *log,
 		if (!meta)
 			break;
 		rec = meta->record;
-		i = btf_record_first_field(rec, off);
-		if (i < rec->cnt && rec->fields[i].offset < off + size) {
-			bpf_log(log, "direct access to %s is disallowed\n",
-				btf_field_type_name(rec->fields[i].type));
-			return -EACCES;
+		for (i = btf_record_first_field(rec, min_off);
+		     i < rec->cnt && rec->fields[i].offset < max_off + size; i++) {
+			struct btf_field *field = &rec->fields[i];
+			/* An access at 'lo' or above ends after the start of the field. */
+			s64 lo = max(min_off, (s64)field->offset - size + 1);
+
+			/* Round up to min_off + k * step. The step is never 0. */
+			lo += imod(min_off - lo, reg->step);
+			if (lo <= max_off && lo < field->offset + field->size) {
+				bpf_log(log, "direct access to %s is disallowed\n",
+					btf_field_type_name(field->type));
+				return -EACCES;
+			}
 		}
 		break;
 	}
-- 
2.43.0


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

end of thread, other threads:[~2026-11-19  1:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 16:21 [PATCH bpf-next v4] bpf: Check variable-offset accesses against special fields Shihuang Liu
2026-11-18 17:16 ` bot+bpf-ci
2026-11-19  1:09 ` Alexei Starovoitov

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