Skip to content

Commit f1a660b

Browse files
committed
Merge branch 'bpf-enforce-btf-pointer-write-checks-for-global-args'
Nuoqi Gui says: ==================== bpf: Enforce BTF pointer write checks for global args check_mem_reg() verifies both read and write access when a caller passes memory into a global subprogram. For PTR_TO_BTF_ID callers, check_helper_mem_access() currently always checks the access as BPF_READ. That lets a tracing program pass a task_struct field pointer to a global subprogram argument typed as writable memory. The direct field store is rejected with "only read is supported", but the callee is validated with a generic writable PTR_TO_MEM argument and can store through it. Forward the requested access type into the PTR_TO_BTF_ID helper-access path and add verifier coverage for the global-subprogram argument case. Validation (tested on bpf-next 8496d90): Without this series: direct BTF field store rejected with "only read is supported"; global-subprogram candidate loaded, attached, and runtime-confirmed. With this series applied: direct BTF field store rejected with "only read is supported"; global-subprogram candidate rejected with "only read is supported". Signed-off-by: Nuoqi Gui <gnq25@mails.tsinghua.edu.cn> --- ==================== Link: https://patch.msgid.link/20260609-f01-04-btf-writable-arg-v1-0-f449cd970669@mails.tsinghua.edu.cn Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
2 parents dd0f968 + af8c3f1 commit f1a660b

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

kernel/bpf/verifier.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6777,7 +6777,7 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, struct bpf_reg_
67776777
zero_size_allowed, access_type, meta);
67786778
case PTR_TO_BTF_ID:
67796779
return check_ptr_to_btf_access(env, regs, reg, argno, 0,
6780-
access_size, BPF_READ, -1);
6780+
access_size, access_type, -1);
67816781
case PTR_TO_CTX:
67826782
/* Only permit reading or writing syscall context using helper calls. */
67836783
if (is_var_ctx_off_allowed(env->prog)) {

tools/testing/selftests/bpf/progs/verifier_global_ptr_args.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,25 @@ int trusted_to_untrusted_mem(void *ctx)
287287
return subprog_void_untrusted(bpf_get_current_task_btf());
288288
}
289289

290+
__weak int subprog_write_mem_arg(int *p)
291+
{
292+
if (!p)
293+
return 0;
294+
295+
*p = 42;
296+
return 0;
297+
}
298+
299+
SEC("?tp_btf/task_newtask")
300+
__failure
301+
__msg("only read is supported")
302+
int trusted_btf_field_to_writable_mem(void *ctx)
303+
{
304+
struct task_struct *task = bpf_get_current_task_btf();
305+
306+
return subprog_write_mem_arg(&task->prio);
307+
}
308+
290309
SEC("tp_btf/sys_enter")
291310
__success
292311
int anything_to_untrusted_mem(void *ctx)

0 commit comments

Comments
 (0)