Skip to content

Commit de36adc

Browse files
foxirainAlexei Starovoitov
authored andcommitted
bpf: reject overlarge global subprog argument sizes
Global subprogram argument checking derives generic pointer sizes from BTF and passes the resolved size to check_mem_reg() as a u32. The access-size validation path then uses a signed int, and stack pointers negate the value before calling check_helper_mem_access(). This creates a wrap when BTF describes a pointee size larger than S32_MAX. For example, a global subprogram argument of type: int (*p)[0x3fffffff] has a BTF-resolved pointee size of 0xfffffffc bytes. At a call site the caller can pass a pointer to a 4-byte stack slot at fp-4. The current PTR_TO_STACK path computes: size = -(int)mem_size so 0xfffffffc becomes -4 as a signed int and the negation validates only a 4-byte stack range. That range is covered by the caller's stack slot, so the call is accepted. The callee is then verified independently with R1 as PTR_TO_MEM and mem_size 0xfffffffc. A small instruction such as: r0 = *(u32 *)(r1 + 4) is accepted as being inside that BTF-described memory region. At run time, however, the actual argument value is still fp-4, so r1 + 4 addresses fp+0, outside the 4-byte object that the caller provided. Reject sizes that cannot be represented by the verifier's signed access-size API before the stack-specific negation. Add a verifier regression test for the oversized BTF argument. Fixes: 2cb2715 ("bpf: poison dead stack slots") Signed-off-by: Taegu Ha <hataegu0826@gmail.com> Acked-by: Yonghong Song <yonghong.song@linux.dev> Link: https://lore.kernel.org/r/20260528062155.3988156-1-hataegu0826@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 44cee8d commit de36adc

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

kernel/bpf/verifier.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6927,6 +6927,12 @@ static int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg
69276927
if (bpf_register_is_null(reg))
69286928
return 0;
69296929

6930+
if (mem_size > S32_MAX) {
6931+
verbose(env, "%s memory size %u is too large\n",
6932+
reg_arg_name(env, argno), mem_size);
6933+
return -EACCES;
6934+
}
6935+
69306936
/* Assuming that the register contains a value check if the memory
69316937
* access is safe. Temporarily save and restore the register's state as
69326938
* the conversion shouldn't be visible to a caller.

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,23 @@ int anon_user_mem_valid(void *ctx)
152152
return subprog_user_anon_mem(&t);
153153
}
154154

155+
__noinline __weak int subprog_user_anon_mem_huge(int (*p)[0x3fffffff])
156+
{
157+
return p ? (*p)[1] : 0;
158+
}
159+
160+
SEC("?tracepoint")
161+
__failure __log_level(2)
162+
__msg("R1 memory size 4294967292 is too large")
163+
int anon_user_mem_huge_size_invalid(void *ctx)
164+
{
165+
int (*p)[0x3fffffff];
166+
int tiny = 42;
167+
168+
p = (void *)&tiny;
169+
return subprog_user_anon_mem_huge(p) + tiny;
170+
}
171+
155172
__noinline __weak int subprog_nonnull_ptr_good(int *p1 __arg_nonnull, int *p2 __arg_nonnull)
156173
{
157174
return (*p1) * (*p2); /* good, no need for NULL checks */

0 commit comments

Comments
 (0)