Skip to content

Commit 464120a

Browse files
Yonghong Songintel-lab-lkp
authored andcommitted
bpf: Do not include stack ptr register in precision backtracking bookkeeping
Yi Lai reported an issue ([1]) where the following warning appears in kernel dmesg: [ 60.643604] verifier backtracking bug [ 60.643635] WARNING: CPU: 10 PID: 2315 at kernel/bpf/verifier.c:4302 __mark_chain_precision+0x3a6c/0x3e10 [ 60.648428] Modules linked in: bpf_testmod(OE) [ 60.650471] CPU: 10 UID: 0 PID: 2315 Comm: test_progs Tainted: G OE 6.15.0-rc4-gef11287f8289-dirty torvalds#327 PREEMPT(full) [ 60.654385] Tainted: [O]=OOT_MODULE, [E]=UNSIGNED_MODULE [ 60.656682] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.14.0-0-g155821a1990b-prebuilt.qemu.org 04/01/2014 [ 60.660475] RIP: 0010:__mark_chain_precision+0x3a6c/0x3e10 [ 60.662814] Code: 5a 30 84 89 ea e8 c4 d9 01 00 80 3d 3e 7d d8 04 00 0f 85 60 fa ff ff c6 05 31 7d d8 04 01 48 c7 c7 00 58 30 84 e8 c4 06 a5 ff <0f> 0b e9 46 fa ff ff 48 ... [ 60.668720] RSP: 0018:ffff888116cc7298 EFLAGS: 00010246 [ 60.671075] RAX: 54d70e82dfd31900 RBX: ffff888115b65e20 RCX: 0000000000000000 [ 60.673659] RDX: 0000000000000001 RSI: 0000000000000004 RDI: 00000000ffffffff [ 60.676241] RBP: 0000000000000400 R08: ffff8881f6f23bd3 R09: 1ffff1103ede477a [ 60.678787] R10: dffffc0000000000 R11: ffffed103ede477b R12: ffff888115b60ae8 [ 60.681420] R13: 1ffff11022b6cbc4 R14: 00000000fffffff2 R15: 0000000000000001 [ 60.684030] FS: 00007fc2aedd80c0(0000) GS:ffff88826fa8a000(0000) knlGS:0000000000000000 [ 60.686837] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 60.689027] CR2: 000056325369e000 CR3: 000000011088b002 CR4: 0000000000370ef0 [ 60.691623] Call Trace: [ 60.692821] <TASK> [ 60.693960] ? __pfx_verbose+0x10/0x10 [ 60.695656] ? __pfx_disasm_kfunc_name+0x10/0x10 [ 60.697495] check_cond_jmp_op+0x16f7/0x39b0 [ 60.699237] do_check+0x58fa/0xab10 ... Further analysis shows the warning is at line 4302 as below: 4294 /* static subprog call instruction, which 4295 * means that we are exiting current subprog, 4296 * so only r1-r5 could be still requested as 4297 * precise, r0 and r6-r10 or any stack slot in 4298 * the current frame should be zero by now 4299 */ 4300 if (bt_reg_mask(bt) & ~BPF_REGMASK_ARGS) { 4301 verbose(env, "BUG regs %x\n", bt_reg_mask(bt)); 4302 WARN_ONCE(1, "verifier backtracking bug"); 4303 return -EFAULT; 4304 } With the below test (also in the next patch): __used __naked static void __bpf_jmp_r10(void) { asm volatile ( "r2 = 2314885393468386424 ll;" "goto +0;" "if r2 <= r10 goto +3;" "if r1 >= -1835016 goto +0;" "if r2 <= 8 goto +0;" "if r3 <= 0 goto +0;" "exit;" ::: __clobber_all); } SEC("?raw_tp") __naked void bpf_jmp_r10(void) { asm volatile ( "r3 = 0 ll;" "call __bpf_jmp_r10;" "r0 = 0;" "exit;" ::: __clobber_all); } The following is the verifier failure log: 0: (18) r3 = 0x0 ; R3_w=0 2: (85) call pc+2 caller: R10=fp0 callee: frame1: R1=ctx() R3_w=0 R10=fp0 5: frame1: R1=ctx() R3_w=0 R10=fp0 ; asm volatile (" \ @ verifier_precision.c:184 5: (18) r2 = 0x20202000256c6c78 ; frame1: R2_w=0x20202000256c6c78 7: (05) goto pc+0 8: (bd) if r2 <= r10 goto pc+3 ; frame1: R2_w=0x20202000256c6c78 R10=fp0 9: (35) if r1 >= 0xffe3fff8 goto pc+0 ; frame1: R1=ctx() 10: (b5) if r2 <= 0x8 goto pc+0 mark_precise: frame1: last_idx 10 first_idx 0 subseq_idx -1 mark_precise: frame1: regs=r2 stack= before 9: (35) if r1 >= 0xffe3fff8 goto pc+0 mark_precise: frame1: regs=r2 stack= before 8: (bd) if r2 <= r10 goto pc+3 mark_precise: frame1: regs=r2,r10 stack= before 7: (05) goto pc+0 mark_precise: frame1: regs=r2,r10 stack= before 5: (18) r2 = 0x20202000256c6c78 mark_precise: frame1: regs=r10 stack= before 2: (85) call pc+2 BUG regs 400 The main failure reason is due to r10 in precision backtracking bookkeeping. Actually r10 is always precise and there is no need to add it for the precision backtracking bookkeeping. One way to fix the issue is to prevent bt_set_reg() if any src/dst reg is r10. Andrii suggested to go with push_insn_history() approach to avoid explicitly checking r10 in backtrack_insn(). This patch added push_insn_history() support for cond_jmp like 'rX <op> rY' operations. In check_cond_jmp_op(), if any of rX or rY is a stack pointer, push_insn_history() will record such information, and later backtrack_insn() will do bt_set_reg() properly for those register(s). [1] https://lore.kernel.org/bpf/Z%2F8q3xzpU59CIYQE@ly-workstation/ Reported by: Yi Lai <[email protected]> Fixes: 407958a ("bpf: encapsulate precision backtracking bookkeeping") Signed-off-by: Yonghong Song <[email protected]>
1 parent 25b6d5d commit 464120a

File tree

2 files changed

+70
-10
lines changed

2 files changed

+70
-10
lines changed

include/linux/bpf_verifier.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,10 @@ enum {
357357
INSN_F_SPI_SHIFT = 3, /* shifted 3 bits to the left */
358358

359359
INSN_F_STACK_ACCESS = BIT(9), /* we need 10 bits total */
360+
361+
INSN_F_DST_REG_STACK = BIT(10), /* dst_reg is PTR_TO_STACK */
362+
INSN_F_SRC_REG_STACK = BIT(11), /* src_reg is PTR_TO_STACK */
363+
/* total 12 bits are used now. */
360364
};
361365

362366
static_assert(INSN_F_FRAMENO_MASK + 1 >= MAX_CALL_FRAMES);
@@ -365,9 +369,11 @@ static_assert(INSN_F_SPI_MASK + 1 >= MAX_BPF_STACK / 8);
365369
struct bpf_insn_hist_entry {
366370
u32 idx;
367371
/* insn idx can't be bigger than 1 million */
368-
u32 prev_idx : 22;
369-
/* special flags, e.g., whether insn is doing register stack spill/load */
370-
u32 flags : 10;
372+
u32 prev_idx : 20;
373+
/* special flags, e.g., whether insn is doing register stack spill/load,
374+
* whether dst/src register is PTR_TO_STACK.
375+
*/
376+
u32 flags : 12;
371377
/* additional registers that need precision tracking when this
372378
* jump is backtracked, vector of six 10-bit records
373379
*/

kernel/bpf/verifier.c

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3739,6 +3739,22 @@ static int check_reg_arg(struct bpf_verifier_env *env, u32 regno,
37393739
return __check_reg_arg(env, state->regs, regno, t);
37403740
}
37413741

3742+
static int insn_reg_access_flags(bool dreg_stack_ptr, bool sreg_stack_ptr)
3743+
{
3744+
return (dreg_stack_ptr ? INSN_F_DST_REG_STACK : 0) |
3745+
(sreg_stack_ptr ? INSN_F_SRC_REG_STACK : 0);
3746+
}
3747+
3748+
static bool insn_dreg_stack_ptr(int insn_flags)
3749+
{
3750+
return !!(insn_flags & INSN_F_DST_REG_STACK);
3751+
}
3752+
3753+
static bool insn_sreg_stack_ptr(int insn_flags)
3754+
{
3755+
return !!(insn_flags & INSN_F_SRC_REG_STACK);
3756+
}
3757+
37423758
static int insn_stack_access_flags(int frameno, int spi)
37433759
{
37443760
return INSN_F_STACK_ACCESS | (spi << INSN_F_SPI_SHIFT) | frameno;
@@ -4402,6 +4418,8 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,
44024418
*/
44034419
return 0;
44044420
} else if (BPF_SRC(insn->code) == BPF_X) {
4421+
bool dreg_precise, sreg_precise;
4422+
44054423
if (!bt_is_reg_set(bt, dreg) && !bt_is_reg_set(bt, sreg))
44064424
return 0;
44074425
/* dreg <cond> sreg
@@ -4410,8 +4428,16 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,
44104428
* before it would be equally necessary to
44114429
* propagate it to dreg.
44124430
*/
4413-
bt_set_reg(bt, dreg);
4414-
bt_set_reg(bt, sreg);
4431+
if (!hist)
4432+
return 0;
4433+
dreg_precise = !insn_dreg_stack_ptr(hist->flags);
4434+
sreg_precise = !insn_sreg_stack_ptr(hist->flags);
4435+
if (!dreg_precise && !sreg_precise)
4436+
return 0;
4437+
if (dreg_precise)
4438+
bt_set_reg(bt, dreg);
4439+
if (sreg_precise)
4440+
bt_set_reg(bt, sreg);
44154441
} else if (BPF_SRC(insn->code) == BPF_K) {
44164442
/* dreg <cond> K
44174443
* Only dreg still needs precision before
@@ -16397,6 +16423,29 @@ static void sync_linked_regs(struct bpf_verifier_state *vstate, struct bpf_reg_s
1639716423
}
1639816424
}
1639916425

16426+
static int push_cond_jmp_history(struct bpf_verifier_env *env, struct bpf_verifier_state *state,
16427+
struct bpf_reg_state *dst_reg, struct bpf_reg_state *src_reg,
16428+
u64 linked_regs)
16429+
{
16430+
bool dreg_stack_ptr, sreg_stack_ptr;
16431+
int insn_flags;
16432+
16433+
if (!src_reg) {
16434+
if (linked_regs)
16435+
return push_insn_history(env, state, 0, linked_regs);
16436+
return 0;
16437+
}
16438+
16439+
dreg_stack_ptr = dst_reg->type == PTR_TO_STACK;
16440+
sreg_stack_ptr = src_reg->type == PTR_TO_STACK;
16441+
16442+
if (!dreg_stack_ptr && !sreg_stack_ptr && !linked_regs)
16443+
return 0;
16444+
16445+
insn_flags = insn_reg_access_flags(dreg_stack_ptr, sreg_stack_ptr);
16446+
return push_insn_history(env, state, insn_flags, linked_regs);
16447+
}
16448+
1640016449
static int check_cond_jmp_op(struct bpf_verifier_env *env,
1640116450
struct bpf_insn *insn, int *insn_idx)
1640216451
{
@@ -16500,6 +16549,9 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
1650016549
!sanitize_speculative_path(env, insn, *insn_idx + 1,
1650116550
*insn_idx))
1650216551
return -EFAULT;
16552+
err = push_cond_jmp_history(env, this_branch, dst_reg, src_reg, 0);
16553+
if (err)
16554+
return err;
1650316555
if (env->log.level & BPF_LOG_LEVEL)
1650416556
print_insn_state(env, this_branch, this_branch->curframe);
1650516557
*insn_idx += insn->off;
@@ -16514,6 +16566,9 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
1651416566
*insn_idx + insn->off + 1,
1651516567
*insn_idx))
1651616568
return -EFAULT;
16569+
err = push_cond_jmp_history(env, this_branch, dst_reg, src_reg, 0);
16570+
if (err)
16571+
return err;
1651716572
if (env->log.level & BPF_LOG_LEVEL)
1651816573
print_insn_state(env, this_branch, this_branch->curframe);
1651916574
return 0;
@@ -16528,11 +16583,10 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
1652816583
collect_linked_regs(this_branch, src_reg->id, &linked_regs);
1652916584
if (dst_reg->type == SCALAR_VALUE && dst_reg->id)
1653016585
collect_linked_regs(this_branch, dst_reg->id, &linked_regs);
16531-
if (linked_regs.cnt > 1) {
16532-
err = push_insn_history(env, this_branch, 0, linked_regs_pack(&linked_regs));
16533-
if (err)
16534-
return err;
16535-
}
16586+
err = push_cond_jmp_history(env, this_branch, dst_reg, src_reg,
16587+
linked_regs.cnt > 1 ? linked_regs_pack(&linked_regs) : 0);
16588+
if (err)
16589+
return err;
1653616590

1653716591
other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx,
1653816592
false);

0 commit comments

Comments
 (0)