Skip to content

Commit 32bf08a

Browse files
Alexei Starovoitovdavem330
authored andcommitted
bpf: fix bug in eBPF verifier
while comparing for verifier state equivalency the comparison was missing a check for uninitialized register. Make sure it does so and add a testcase. Fixes: f1bca82 ("bpf: add search pruning optimization to verifier") Cc: Hannes Frederic Sowa <hannes@stressinduktion.org> Signed-off-by: Alexei Starovoitov <ast@plumgrid.com> Acked-by: Hannes Frederic Sowa <hannes@stressinduktion.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 78fd1d0 commit 32bf08a

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

kernel/bpf/verifier.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1409,7 +1409,8 @@ static bool states_equal(struct verifier_state *old, struct verifier_state *cur)
14091409
if (memcmp(&old->regs[i], &cur->regs[i],
14101410
sizeof(old->regs[0])) != 0) {
14111411
if (old->regs[i].type == NOT_INIT ||
1412-
old->regs[i].type == UNKNOWN_VALUE)
1412+
(old->regs[i].type == UNKNOWN_VALUE &&
1413+
cur->regs[i].type != NOT_INIT))
14131414
continue;
14141415
return false;
14151416
}

samples/bpf/test_verifier.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,17 @@ static struct bpf_test tests[] = {
208208
.errstr = "R0 !read_ok",
209209
.result = REJECT,
210210
},
211+
{
212+
"program doesn't init R0 before exit in all branches",
213+
.insns = {
214+
BPF_JMP_IMM(BPF_JGE, BPF_REG_1, 0, 2),
215+
BPF_MOV64_IMM(BPF_REG_0, 1),
216+
BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 2),
217+
BPF_EXIT_INSN(),
218+
},
219+
.errstr = "R0 !read_ok",
220+
.result = REJECT,
221+
},
211222
{
212223
"stack out of bounds",
213224
.insns = {

0 commit comments

Comments
 (0)