Skip to contentSkip to conversation

bpf: Check variable-offset accesses against special fields

PATCH In review Shihuang Liu 3 participants 3 messages Nov 18 – Nov 19 BPF

lore
Share & tools
lore
#0 lore raw

PATCH v4 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.

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")

---
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(-)
kernel/bpf/btf.c +13 -5
index e95ca39f990c6e..ffdf12db1f87cd 100644
@@ -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;
 	}
Signature · 1 line
2.43.0
#1 lore raw
TL;DR: Commit message only: it cites ___mark_reg_known(), which doesn't exist in this tree. The review asks whether __mark_reg_known() or scalar_step_reset() was meant. No code issues raised.
Quoted 1 line

bpf: Check variable-offset accesses against special fields

TL;DR: Commit message only: it cites ___mark_reg_known(), which doesn't exist in this tree. The review asks whether __mark_reg_known() or scalar_step_reset() was meant. No code issues raised.

Quoted 22 lines

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.
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".

Quoted 4 lines
Fixes 89e72f81a455 ("bpf: Varying offset access support for PTR_TO_BTF_ID pointers")

The commit message says "reg->step is never 0 (___mark_reg_known() sets it to 1)", but there is no ___mark_reg_known() in this tree. Is this meant to be __mark_reg_known()?

The step is set to 1 by scalar_step_reset(), which scalar_set_const() calls, and scalar_set_const() is called from __mark_reg_known():

  static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
  {
  	...
  	scalar_set_const(scalar_of(reg), imm);
  }

  static void scalar_step_reset(struct scalar *s)
  {
  	s->base = 0;
  	s->step = 1;
  }

Could the changelog name __mark_reg_known()/scalar_step_reset() instead?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/44351194274

#2 lore raw
In reply to Shihuang Liu
Quoted 3 lines · Shihuang Liu

Changes since v3:
- Rebased on bpf-next to resolve the merge conflict with ca4395aade31a ("bpf: Walk each type once when looking for special fields").

It's fixed already. This patch doesn't resolve the conflict either.

Just wait for bpf tree to get into bpf-next.

pw-bot: cr