Skip to content

Commit 2083bb9

Browse files
committed
selftests/bpf: Add tests with stack ptr register in conditional jmp
JIRA: https://issues.redhat.com/browse/RHEL-110274 commit 5ffb537 Author: Yonghong Song <yonghong.song@linux.dev> Date: Fri May 23 21:13:40 2025 -0700 selftests/bpf: Add tests with stack ptr register in conditional jmp Add two tests: - one test has 'rX <op> r10' where rX is not r10, and - another test has 'rX <op> rY' where rX and rY are not r10 but there is an early insn 'rX = r10'. Without previous verifier change, both tests will fail. Signed-off-by: Yonghong Song <yonghong.song@linux.dev> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20250524041340.4046304-1-yonghong.song@linux.dev Signed-off-by: Viktor Malik <vmalik@redhat.com>
1 parent 217714b commit 2083bb9

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

kernel/bpf/verifier.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15556,6 +15556,8 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
1555615556

1555715557
if (src_reg->type == PTR_TO_STACK)
1555815558
insn_flags |= INSN_F_SRC_REG_STACK;
15559+
if (dst_reg->type == PTR_TO_STACK)
15560+
insn_flags |= INSN_F_DST_REG_STACK;
1555915561
} else {
1556015562
if (insn->src_reg != BPF_REG_0) {
1556115563
verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
@@ -15565,10 +15567,11 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
1556515567
memset(src_reg, 0, sizeof(*src_reg));
1556615568
src_reg->type = SCALAR_VALUE;
1556715569
__mark_reg_known(src_reg, insn->imm);
15570+
15571+
if (dst_reg->type == PTR_TO_STACK)
15572+
insn_flags |= INSN_F_DST_REG_STACK;
1556815573
}
1556915574

15570-
if (dst_reg->type == PTR_TO_STACK)
15571-
insn_flags |= INSN_F_DST_REG_STACK;
1557215575
if (insn_flags) {
1557315576
err = push_jmp_history(env, this_branch, insn_flags, 0);
1557415577
if (err)

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,57 @@ __naked int state_loop_first_last_equal(void)
130130
);
131131
}
132132

133+
__used __naked static void __bpf_cond_op_r10(void)
134+
{
135+
asm volatile (
136+
"r2 = 2314885393468386424 ll;"
137+
"goto +0;"
138+
"if r2 <= r10 goto +3;"
139+
"if r1 >= -1835016 goto +0;"
140+
"if r2 <= 8 goto +0;"
141+
"if r3 <= 0 goto +0;"
142+
"exit;"
143+
::: __clobber_all);
144+
}
145+
146+
SEC("?raw_tp")
147+
__success __log_level(2)
148+
__msg("8: (bd) if r2 <= r10 goto pc+3")
149+
__msg("9: (35) if r1 >= 0xffe3fff8 goto pc+0")
150+
__msg("10: (b5) if r2 <= 0x8 goto pc+0")
151+
__msg("mark_precise: frame1: last_idx 10 first_idx 0 subseq_idx -1")
152+
__msg("mark_precise: frame1: regs=r2 stack= before 9: (35) if r1 >= 0xffe3fff8 goto pc+0")
153+
__msg("mark_precise: frame1: regs=r2 stack= before 8: (bd) if r2 <= r10 goto pc+3")
154+
__msg("mark_precise: frame1: regs=r2 stack= before 7: (05) goto pc+0")
155+
__naked void bpf_cond_op_r10(void)
156+
{
157+
asm volatile (
158+
"r3 = 0 ll;"
159+
"call __bpf_cond_op_r10;"
160+
"r0 = 0;"
161+
"exit;"
162+
::: __clobber_all);
163+
}
164+
165+
SEC("?raw_tp")
166+
__success __log_level(2)
167+
__msg("3: (bf) r3 = r10")
168+
__msg("4: (bd) if r3 <= r2 goto pc+1")
169+
__msg("5: (b5) if r2 <= 0x8 goto pc+2")
170+
__msg("mark_precise: frame0: last_idx 5 first_idx 0 subseq_idx -1")
171+
__msg("mark_precise: frame0: regs=r2 stack= before 4: (bd) if r3 <= r2 goto pc+1")
172+
__msg("mark_precise: frame0: regs=r2 stack= before 3: (bf) r3 = r10")
173+
__naked void bpf_cond_op_not_r10(void)
174+
{
175+
asm volatile (
176+
"r0 = 0;"
177+
"r2 = 2314885393468386424 ll;"
178+
"r3 = r10;"
179+
"if r3 <= r2 goto +1;"
180+
"if r2 <= 8 goto +2;"
181+
"r0 = 2 ll;"
182+
"exit;"
183+
::: __clobber_all);
184+
}
185+
133186
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)