Skip to content

Commit fd4cfa8

Browse files
ahjf-07eddyz87
authored andcommitted
bpf: Reject negative const offsets for buffer pointers
The verifier rejects variable offsets for PTR_TO_TP_BUFFER and PTR_TO_BUF accesses, but it currently accepts a constant negative offset produced by pointer arithmetic. Commit 022ac07 ("bpf: use reg->var_off instead of reg->off for pointers") moved constant pointer offsets from reg->off to reg->var_off. However, __check_buffer_access() continued to check only the instruction offset. An access with reg->var_off equal to -8 and an instruction offset of zero therefore passes verification. For writable raw tracepoints, the access end is also calculated from the unsigned reg->var_off.value. An eight-byte access starting at -8 wraps the calculated end to zero, allowing the program to load and attach without increasing max_tp_access. After ensuring that reg->var_off is constant, calculate the effective access start using signed arithmetic and reject it when it is negative. Use the validated start to calculate the access end for both PTR_TO_TP_BUFFER and PTR_TO_BUF. Fixes: 022ac07 ("bpf: use reg->var_off instead of reg->off for pointers") Signed-off-by: Sun Jian <sun.jian.kdev@gmail.com> Acked-by: Shung-Hsi Yu <shung-hsi.yu@suse.com> Cc: stable@vger.kernel.org # 5.2.0 Link: https://patch.msgid.link/20260714093846.18159-2-sun.jian.kdev@gmail.com Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
1 parent 2d8af4e commit fd4cfa8

1 file changed

Lines changed: 19 additions & 12 deletions

File tree

kernel/bpf/verifier.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5326,14 +5326,11 @@ static int check_max_stack_depth(struct bpf_verifier_env *env)
53265326
static int __check_buffer_access(struct bpf_verifier_env *env,
53275327
const char *buf_info,
53285328
const struct bpf_reg_state *reg,
5329-
argno_t argno, int off, int size)
5329+
argno_t argno, int off, int size,
5330+
u32 *access_end)
53305331
{
5331-
if (off < 0) {
5332-
verbose(env,
5333-
"%s invalid %s buffer access: off=%d, size=%d\n",
5334-
reg_arg_name(env, argno), buf_info, off, size);
5335-
return -EACCES;
5336-
}
5332+
s64 start;
5333+
53375334
if (!tnum_is_const(reg->var_off)) {
53385335
char tn_buf[48];
53395336

@@ -5344,21 +5341,30 @@ static int __check_buffer_access(struct bpf_verifier_env *env,
53445341
return -EACCES;
53455342
}
53465343

5344+
start = (s64)reg->var_off.value + off;
5345+
if (start < 0) {
5346+
verbose(env,
5347+
"%s invalid negative %s buffer offset: off=%d, var_off=%lld\n",
5348+
reg_arg_name(env, argno), buf_info, off, (s64)reg->var_off.value);
5349+
return -EACCES;
5350+
}
5351+
5352+
*access_end = start + size;
53475353
return 0;
53485354
}
53495355

53505356
static int check_tp_buffer_access(struct bpf_verifier_env *env,
53515357
const struct bpf_reg_state *reg,
53525358
argno_t argno, int off, int size)
53535359
{
5360+
u32 access_end;
53545361
int err;
53555362

5356-
err = __check_buffer_access(env, "tracepoint", reg, argno, off, size);
5363+
err = __check_buffer_access(env, "tracepoint", reg, argno, off, size, &access_end);
53575364
if (err)
53585365
return err;
53595366

5360-
env->prog->aux->max_tp_access = max(reg->var_off.value + off + size,
5361-
env->prog->aux->max_tp_access);
5367+
env->prog->aux->max_tp_access = max(access_end, env->prog->aux->max_tp_access);
53625368

53635369
return 0;
53645370
}
@@ -5370,13 +5376,14 @@ static int check_buffer_access(struct bpf_verifier_env *env,
53705376
u32 *max_access)
53715377
{
53725378
const char *buf_info = type_is_rdonly_mem(reg->type) ? "rdonly" : "rdwr";
5379+
u32 access_end;
53735380
int err;
53745381

5375-
err = __check_buffer_access(env, buf_info, reg, argno, off, size);
5382+
err = __check_buffer_access(env, buf_info, reg, argno, off, size, &access_end);
53765383
if (err)
53775384
return err;
53785385

5379-
*max_access = max(reg->var_off.value + off + size, *max_access);
5386+
*max_access = max(access_end, *max_access);
53805387

53815388
return 0;
53825389
}

0 commit comments

Comments
 (0)