* [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* Re: [PATCH bpf-next v4] bpf: Check variable-offset accesses against special fields
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
1 sibling, 0 replies; 3+ messages in thread
From: bot+bpf-ci @ 2026-11-18 17:16 UTC (permalink / raw)
To: shlomojune6, ast, daniel
Cc: andrii, eddyz87, memxor, martin.lau, song, yonghong.song, jolsa,
emil, ihor.solodrai, broonie, bpf, shlomojune6, ast, andrii,
daniel, martin.lau, eddyz87, yonghong.song, mason, ihor.solodrai
[-- Attachment #1: Type: text/plain, Size: 2700 bytes --]
> 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.
> 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")
> 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>
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
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH bpf-next v4] bpf: Check variable-offset accesses against special fields
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
1 sibling, 0 replies; 3+ messages in thread
From: Alexei Starovoitov @ 2026-11-19 1:09 UTC (permalink / raw)
To: Shihuang Liu, 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
On Wed, Nov 18, 2026 at 08:21 AM Shihuang Liu <shlomojune6@gmail.com> wrote:
> 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
^ permalink raw reply [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