Skip to contentSkip to conversation

bpf: Check variable-offset accesses against special fields

PATCH Applied Shihuang Liu 2 participants 2 messages Nov 9 – Nov 12 BPF

v3 of 4 Applied 1/1 Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 bpf: Check variable-offset accesses against special fields

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.

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", while a store to arr[i].data with step == sizeof(elem) still loads.

Fixes 89e72f81a455 ("bpf: Varying offset access support for PTR_TO_BTF_ID pointers")
---
 kernel/bpf/btf.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/bpf/btf.c +6 -1
index 8bf39098ba97a1..2265362d8342f1 100644
@@ -8094,7 +8094,12 @@ 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) {
+			/* An access at 'lo' or above ends after the start of the field. */
+			s64 lo = max(min_off, (s64)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 < offset + field->size) {
 				bpf_log(log,
 					"direct access to %s is disallowed\n",
 					btf_field_type_name(field->type));
Signature · 1 line
2.43.0
#1 lore raw
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 6 lines · On Mon, 9 Nov 2026 17:47:23 +0800 you wrote:

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:

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

[...]

Here is the summary with links:
- [bpf,v3] bpf: Check variable-offset accesses against special fields

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

You are awesome, thank you!

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