Allow fixed offsets for PTR_TO_CTX
Subsystems (4)
Kumar Kartikeya Dwivedi
maintainercover Allow fixed offsets for PTR_TO_CTX
Enable pointer modification with constant offsets accumulated in the register for PTR_TO_CTX for programs where it won't be rewritten. See patches for details.
Kumar Kartikeya Dwivedi (2):
bpf: Relax fixed offset check for PTR_TO_CTX selftests/bpf: Add tests for ctx fixed offset support
kernel/bpf/verifier.c | 13 +++- .../selftests/bpf/progs/verifier_ctx.c | 76 +++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-)
base-commit: f620af11c27b8ec9994a39fe968aa778112d1566
Signature · 1 line
2.47.3
Kumar Kartikeya Dwivedi
maintainerPATCH 1/2 bpf: Relax fixed offset check for PTR_TO_CTX
The limitation on fixed offsets stems from the fact that certain program types rewrite the accesses to the context structure and translate them to accesses to the real underlying type. Technically, in the past, we could have stashed the register offset in insn_aux and made rewrites work, but we've never needed it in the past since the offset for such context structures easily fit in the s16 signed instruction offset.
Regardless, the consequence is that for program types where the program type's verifier ops doesn't supply a convert_ctx_access callback, we unnecessarily reject accesses with a modified ctx pointer (i.e., one whose offset has been shifted) in check_ptr_off_reg. Make an exception for such program types (like syscall, tracepoint, raw_tp, etc.).
Pass in fixed_off_ok as true to __check_ptr_off_reg for such cases, and accumulate the register offset into insn->off passed to check_ctx_access. In particular, the accumulation is critical since we need to correctly track the max_ctx_offset which is used for bounds checking the buffer for syscall programs at runtime.
--- kernel/bpf/verifier.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +12 -1
@@ -7722,6 +7722,11 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regnif (!err && value_regno >=0 && (t ==BPF_READ || rdonly_mem))mark_reg_unknown (env, regs, value_regno); }else if (reg->type ==PTR_TO_CTX ) { +/* +* Program types that don't rewrite ctx accesses can safely +* dereference ctx pointers with fixed offsets. +*/ +bool fixed_off_ok = !env->ops->convert_ctx_access;struct bpf_retval_range range;struct bpf_insn_access_aux info = { .reg_type = SCALAR_VALUE,
@@ -7735,10 +7740,16 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regnreturn -EACCES ; } - err =check_ptr_off_reg (env, reg, regno); + err =__check_ptr_off_reg (env, reg, regno, fixed_off_ok);if (err <0 )return err; +/* +* Fold the register's constant offset into the insn offset so +* that is_valid_access() sees the true effective offset. +*/ +if (fixed_off_ok) + off += reg->var_off.value; err =check_ctx_access (env, insn_idx, off, size, t, &info);if (err)verbose_linfo (env, insn_idx,"; " );
Signature · 1 line
2.47.3
Re: [PATCH bpf-next v1 1/2] bpf: Relax fixed offset check for PTR_TO_CTX
Quoted 19 lines · Kumar Kartikeya Dwivedi
The limitation on fixed offsets stems from the fact that certain program types rewrite the accesses to the context structure and translate them to accesses to the real underlying type. Technically, in the past, we could have stashed the register offset in
insn_auxand made rewrites work, but we've never needed it in the past since the offset for such context structures easily fit in the s16 signed instruction offset.Regardless, the consequence is that for program types where the program type's verifier ops doesn't supply a
convert_ctx_accesscallback, we unnecessarily reject accesses with a modified ctx pointer (i.e., one whose offset has been shifted) incheck_ptr_off_reg. Make an exception for such program types (like syscall, tracepoint,raw_tp, etc.).Pass in
fixed_off_okas true to__check_ptr_off_regfor such cases, and accumulate the register offset intoinsn->offpassed tocheck_ctx_access. In particular, the accumulation is critical since we need to correctly track themax_ctx_offsetwhich is used for bounds checking the buffer for syscall programs at runtime.
Quoted 4 lines
Reported-by Tejun Heo <[email protected]>Reported-by Dan Schatzberg <[email protected]>Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>---
Quoted 37 lines
kernel/bpf/verifier.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)kernel/bpf/verifier.c +12
-1@@ -7722,6 +7722,11 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regnif (!err && value_regno >=0 && (t ==BPF_READ || rdonly_mem))mark_reg_unknown (env, regs, value_regno); }else if (reg->type ==PTR_TO_CTX ) { +/* +* Program types that don't rewrite ctx accesses can safely +* dereference ctx pointers with fixed offsets. +*/ +bool fixed_off_ok = !env->ops->convert_ctx_access;struct bpf_retval_range range;struct bpf_insn_access_aux info = { .reg_type = SCALAR_VALUE,@@ -7735,10 +7740,16 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regnreturn -EACCES ; } - err =check_ptr_off_reg (env, reg, regno); + err =__check_ptr_off_reg (env, reg, regno, fixed_off_ok);if (err <0 )return err; +/* +* Fold the register's constant offset into the insn offset so +* that is_valid_access() sees the true effective offset. +*/ +if (fixed_off_ok) + off += reg->var_off.value; err =check_ctx_access (env, insn_idx, off, size, t, &info);if (err)verbose_linfo (env, insn_idx,"; " );
Kumar Kartikeya Dwivedi
maintainerPATCH 2/2 selftests/bpf: Add tests for ctx fixed offset support
In reply to Kumar Kartikeya DwivediAdd tests to ensure PTR_TO_CTX supports fixed offsets for program types that don't rewrite accesses to it. Ensure that variable offsets and negative offsets are still rejected. An extra test also checks writing into ctx with modified offset for syscall progs. Other program types do not support writes (notably, writable tracepoints offer a pointer for a writable buffer through ctx, but don't allow writing to the ctx itself).
Before the fix made in the previous commit, these tests do not succeed, except the ones testing for failures regardless of the change.
--- .../selftests/bpf/progs/verifier_ctx.c | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+)
tools/testing/selftests/bpf/progs/verifier_ctx.c +76 -0
@@ -292,4 +292,80 @@ padding_access("cgroup/post_bind4", bpf_sock, dst_port, 2); __failure __msg("invalid bpf_context access") padding_access("sk_reuseport", sk_reuseport_md, hash, 4); +SEC("syscall") +__description("syscall: write to ctx with fixed offset") +__success +__naked void syscall_ctx_fixed_off_write (void ) +{ + asmvolatile (" \ +r0 = 0; \ +*(u32*)(r1 + 0) = r0; \ +r1 += 4; \ +*(u32*)(r1 + 0) = r0; \ +exit; \ +" ::: __clobber_all); +} + +/* +* Test that program types without convert_ctx_access can dereference +* their ctx pointer after adding a fixed offset. Variable and negative +* offsets should still be rejected. +*/ +#define no_rewrite_ctx_access (type, name, off, ld_op) \ + SEC(type) \ + __description(type ": read ctx at fixed offset") \ + __success \ + __naked void no_rewrite_##name##_fixed(void) \ + { \ + asm volatile (" \ + r1 += %[__off]; \ + r0 = *(" #ld_op " *)(r1 + 0); \ + r0 = 0; \ + exit;" \ + : \ + : __imm_const(__off, off) \ + : __clobber_all); \ + } \ + SEC(type) \ + __description(type ": reject variable offset ctx access") \ + __failure __msg("variable ctx access var_off=") \ + __naked void no_rewrite_##name##_var(void) \ + { \ + asm volatile (" \ + r6 = r1; \ + call %[bpf_get_prandom_u32]; \ + r1 = r6; \ + r0 &= 4; \ + r1 += r0; \ + r0 = *(" #ld_op " *)(r1 + 0); \ + r0 = 0; \ + exit;" \ + : \ + : __imm(bpf_get_prandom_u32) \ + : __clobber_all); \ + } \ + SEC(type) \ + __description(type ": reject negative offset ctx access") \ + __failure __msg("negative offset ctx ptr") \ + __naked void no_rewrite_##name##_neg(void) \ + { \ + asm volatile (" \ + r1 += %[__neg_off]; \ + r0 = *(" #ld_op " *)(r1 + 0); \ + r0 = 0; \ + exit;" \ + : \ + : __imm_const(__neg_off, -(off)) \ + : __clobber_all); \ + } + +no_rewrite_ctx_access ("syscall" , syscall,4 , u32); +no_rewrite_ctx_access ("kprobe" , kprobe,8 , u64); +no_rewrite_ctx_access ("tracepoint" , tp,8 , u64); +no_rewrite_ctx_access ("raw_tp" , raw_tp,8 , u64); +no_rewrite_ctx_access ("raw_tracepoint.w" , raw_tp_w,8 , u64); +no_rewrite_ctx_access ("fentry/bpf_modify_return_test" , fentry,8 , u64); +no_rewrite_ctx_access ("cgroup/dev" , cgroup_dev,4 , u32); +no_rewrite_ctx_access ("netfilter" , netfilter, offsetof(struct bpf_nf_ctx , skb), u64); + char _license[] SEC("license") = "GPL";
Signature · 1 line
2.47.3
Re: [PATCH bpf-next v1 2/2] selftests/bpf: Add tests for ctx fixed offset support
Quoted 7 lines · Kumar Kartikeya Dwivedi
Add tests to ensure
PTR_TO_CTXsupports fixed offsets for program types that don't rewrite accesses to it. Ensure that variable offsets and negative offsets are still rejected. An extra test also checks writing into ctx with modified offset for syscall progs. Other program types do not support writes (notably, writable tracepoints offer a pointer for a writable buffer through ctx, but don't allow writing to the ctx itself).
Quoted 5 lines
Before the fix made in the previous commit, these tests do not succeed, except the ones testing for failures regardless of the change.
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>---
Quoted 88 lines
.../selftests/bpf/progs/verifier_ctx.c | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+)tools/testing/selftests/bpf/progs/verifier_ctx.c +76
-0@@ -292,4 +292,80 @@ padding_access("cgroup/post_bind4", bpf_sock, dst_port, 2); __failure __msg("invalid bpf_context access") padding_access("sk_reuseport", sk_reuseport_md, hash, 4); +SEC("syscall") +__description("syscall: write to ctx with fixed offset") +__success +__naked void syscall_ctx_fixed_off_write (void ) +{ + asmvolatile (" \ +r0 = 0; \ +*(u32*)(r1 + 0) = r0; \ +r1 += 4; \ +*(u32*)(r1 + 0) = r0; \ +exit; \ +" ::: __clobber_all); +} + +/* +* Test that program types without convert_ctx_access can dereference +* their ctx pointer after adding a fixed offset. Variable and negative +* offsets should still be rejected. +*/ +#define no_rewrite_ctx_access (type, name, off, ld_op) \ + SEC(type) \ + __description(type ": read ctx at fixed offset") \ + __success \ + __naked void no_rewrite_##name##_fixed(void) \ + { \ + asm volatile (" \ + r1 += %[__off]; \ + r0 = *(" #ld_op " *)(r1 + 0); \ + r0 = 0; \ + exit;" \ + : \ + : __imm_const(__off, off) \ + : __clobber_all); \ + } \ + SEC(type) \ + __description(type ": reject variable offset ctx access") \ + __failure __msg("variable ctx access var_off=") \ + __naked void no_rewrite_##name##_var(void) \ + { \ + asm volatile (" \ + r6 = r1; \ + call %[bpf_get_prandom_u32]; \ + r1 = r6; \ + r0 &= 4; \ + r1 += r0; \ + r0 = *(" #ld_op " *)(r1 + 0); \ + r0 = 0; \ + exit;" \ + : \ + : __imm(bpf_get_prandom_u32) \ + : __clobber_all); \ + } \ + SEC(type) \ + __description(type ": reject negative offset ctx access") \ + __failure __msg("negative offset ctx ptr") \ + __naked void no_rewrite_##name##_neg(void) \ + { \ + asm volatile (" \ + r1 += %[__neg_off]; \ + r0 = *(" #ld_op " *)(r1 + 0); \ + r0 = 0; \ + exit;" \ + : \ + : __imm_const(__neg_off, -(off)) \ + : __clobber_all); \ + } + +no_rewrite_ctx_access ("syscall" , syscall,4 , u32); +no_rewrite_ctx_access ("kprobe" , kprobe,8 , u64); +no_rewrite_ctx_access ("tracepoint" , tp,8 , u64); +no_rewrite_ctx_access ("raw_tp" , raw_tp,8 , u64); +no_rewrite_ctx_access ("raw_tracepoint.w" , raw_tp_w,8 , u64); +no_rewrite_ctx_access ("fentry/bpf_modify_return_test" , fentry,8 , u64); +no_rewrite_ctx_access ("cgroup/dev" , cgroup_dev,4 , u32); +no_rewrite_ctx_access ("netfilter" , netfilter, offsetof(struct bpf_nf_ctx , skb), u64); + char _license[] SEC("license") = "GPL";
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 9 lines · On Thu, 26 Feb 2026 16:57:23 -0800 you wrote:
Enable pointer modification with constant offsets accumulated in the register for
PTR_TO_CTXfor programs where it won't be rewritten. See patches for details.Kumar Kartikeya Dwivedi (2):
bpf: Relax fixed offset check forPTR_TO_CTXselftests/bpf: Add tests for ctx fixed offset support[...]
Here is the summary with links:
- [bpf-next,v1,1/2] bpf: Relax fixed offset check for PTR_TO_CTX
https://git.kernel.org/bpf/bpf-next/c/de6c7d99f898 - [bpf-next,v1,2/2] selftests/bpf: Add tests for ctx fixed offset support https://git.kernel.org/bpf/bpf-next/c/f6312e71759d
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html