bpf: fix bug in eBPF verifier
authorAlexei Starovoitov <ast@plumgrid.com>
Mon, 20 Oct 2014 21:54:57 +0000 (14:54 -0700)
committerDavid S. Miller <davem@davemloft.net>
Wed, 22 Oct 2014 01:43:46 +0000 (21:43 -0400)
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: f1bca824dabb ("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>
kernel/bpf/verifier.c
samples/bpf/test_verifier.c

index 801f5f3b9307cc5cbe8ed24f13e2aa1cbab53dc6..9f81818f294185988c9cc181274ed38789774871 100644 (file)
@@ -1409,7 +1409,8 @@ static bool states_equal(struct verifier_state *old, struct verifier_state *cur)
                if (memcmp(&old->regs[i], &cur->regs[i],
                           sizeof(old->regs[0])) != 0) {
                        if (old->regs[i].type == NOT_INIT ||
-                           old->regs[i].type == UNKNOWN_VALUE)
+                           (old->regs[i].type == UNKNOWN_VALUE &&
+                            cur->regs[i].type != NOT_INIT))
                                continue;
                        return false;
                }
index f44ef11f65a787a26fe99791b24b2b2606321a0e..eb4bec0ad8afd2da3c8a52c61f1937425b082665 100644 (file)
@@ -208,6 +208,17 @@ static struct bpf_test tests[] = {
                .errstr = "R0 !read_ok",
                .result = REJECT,
        },
+       {
+               "program doesn't init R0 before exit in all branches",
+               .insns = {
+                       BPF_JMP_IMM(BPF_JGE, BPF_REG_1, 0, 2),
+                       BPF_MOV64_IMM(BPF_REG_0, 1),
+                       BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 2),
+                       BPF_EXIT_INSN(),
+               },
+               .errstr = "R0 !read_ok",
+               .result = REJECT,
+       },
        {
                "stack out of bounds",
                .insns = {