Skip to contentSkip to conversation

bpf: fix verifier memory corruption

PATCH In review Alexei Starovoitov 4 participants 6 messages Apr 14, 2015 – Apr 16, 2015

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: fix verifier memory corruption

Due to missing bounds check the DAG pass of the BPF verifier can corrupt the memory which can cause random crashes during program loading:

[8.449451] BUG: unable to handle kernel paging request at ffffffffffffffff
[8.451293] IP: [<ffffffff811de33d>] kmem_cache_alloc_trace+0x8d/0x2f0
[8.452329] Oops: 0000 [#1] SMP
[8.452329] Call Trace:
[8.452329]  [<ffffffff8116cc82>] bpf_check+0x852/0x2000
[8.452329]  [<ffffffff8116b7e4>] bpf_prog_load+0x1e4/0x310
[8.452329]  [<ffffffff811b190f>] ? might_fault+0x5f/0xb0
[8.452329]  [<ffffffff8116c206>] SyS_bpf+0x806/0xa30
Fixes f1bca824dabb ("bpf: add search pruning optimization to verifier")

---
Many things need to align for this crash to be seen, yet I managed to hit it. In my case JA was last insn, 't' was 255 and explored_states array had 256 elements. I've double checked other similar paths and all seems clean.

 kernel/bpf/verifier.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +2 -1
index a28e09c7825d..36508e69e92a 100644
@@ -1380,7 +1380,8 @@ peek_stack:
 			/* tell verifier to check for equivalent states
 			 * after every call and jump
 			 */
-			env->explored_states[t + 1] = STATE_LIST_MARK;
+			if (t + 1 < insn_cnt)
+				env->explored_states[t + 1] = STATE_LIST_MARK;
 		} else {
 			/* conditional jump with two edges */
 			ret = push_insn(t, t + 1, FALLTHROUGH, env);
Signature · 1 line
1.7.9.5
#1 lore raw
Quoted 27 lines · Alexei Starovoitov

Due to missing bounds check the DAG pass of the BPF verifier can corrupt the memory which can cause random crashes during program loading:

[8.449451] BUG: unable to handle kernel paging request at ffffffffffffffff
[8.451293] IP: [<ffffffff811de33d>] kmem_cache_alloc_trace+0x8d/0x2f0
[8.452329] Oops: 0000 [#1] SMP
[8.452329] Call Trace:
[8.452329]  [<ffffffff8116cc82>] bpf_check+0x852/0x2000
[8.452329]  [<ffffffff8116b7e4>] bpf_prog_load+0x1e4/0x310
[8.452329]  [<ffffffff811b190f>] ? might_fault+0x5f/0xb0
[8.452329]  [<ffffffff8116c206>] SyS_bpf+0x806/0xa30
Fixes f1bca824dabb ("bpf: add search pruning optimization to verifier")

---
Many things need to align for this crash to be seen, yet I managed to hit it. In my case JA was last insn, 't' was 255 and explored_states array had 256 elements. I've double checked other similar paths and all seems clean.

 kernel/bpf/verifier.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +0 -0
index a28e09c7825d..36508e69e92a 100644

kernel/bpf/verifier.c peek_stack: · line 1387

Quoted 9 lines
kernel/bpf/verifier.c +2 -1
@@ -1380,7 +1380,8 @@ peek_stack:
 			/* tell verifier to check for equivalent states
 			 * after every call and jump
 			 */
-			env->explored_states[t + 1] = STATE_LIST_MARK;
+			if (t + 1 < insn_cnt)
+				env->explored_states[t + 1] = STATE_LIST_MARK;
 		} else {
 			/* conditional jump with two edges */
 			ret = push_insn(t, t + 1, FALLTHROUGH, env);

Quick review:

We have env->explored_states[t+1] access in the

                } else {
                        /* conditional jump with two edges */
                        ret = push_insn(t, t + 1, FALLTHROUGH, env);
                        if (ret == 1)
                                goto peek_stack;
                        else if (ret < 0)
                                goto err_free;
Quoted 1 line
Quoted 1 line
Quoted 1 line
                    ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
                        if (ret == 1)
                                goto peek_stack;
                        else if (ret < 0)
                                goto err_free;
                }
        } else {

push_insn call. At this point insn[t].off could be 0, no?

Thanks,
Hannes

#2 lore raw
Quoted 59 lines · Hannes Frederic Sowa
Quoted 27 lines · Alexei Starovoitov

Due to missing bounds check the DAG pass of the BPF verifier can corrupt the memory which can cause random crashes during program loading:

[8.449451] BUG: unable to handle kernel paging request at ffffffffffffffff
[8.451293] IP: [<ffffffff811de33d>] kmem_cache_alloc_trace+0x8d/0x2f0
[8.452329] Oops: 0000 [#1] SMP
[8.452329] Call Trace:
[8.452329]  [<ffffffff8116cc82>] bpf_check+0x852/0x2000
[8.452329]  [<ffffffff8116b7e4>] bpf_prog_load+0x1e4/0x310
[8.452329]  [<ffffffff811b190f>] ? might_fault+0x5f/0xb0
[8.452329]  [<ffffffff8116c206>] SyS_bpf+0x806/0xa30
Fixes f1bca824dabb ("bpf: add search pruning optimization to verifier")

---
Many things need to align for this crash to be seen, yet I managed to hit it. In my case JA was last insn, 't' was 255 and explored_states array had 256 elements. I've double checked other similar paths and all seems clean.

  kernel/bpf/verifier.c |    3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +0 -0
index a28e09c7825d..36508e69e92a 100644

kernel/bpf/verifier.c peek_stack: · line 1387

Quoted 9 lines
kernel/bpf/verifier.c +2 -1
@@ -1380,7 +1380,8 @@ peek_stack:
  			/* tell verifier to check for equivalent states
  			 * after every call and jump
  			 */
-			env->explored_states[t + 1] = STATE_LIST_MARK;
+			if (t + 1 < insn_cnt)
+				env->explored_states[t + 1] = STATE_LIST_MARK;
  		} else {
  			/* conditional jump with two edges */
  			ret = push_insn(t, t + 1, FALLTHROUGH, env);

Quick review:

We have env->explored_states[t+1] access in the

                 } else {
                         /* conditional jump with two edges */
                         ret = push_insn(t, t + 1, FALLTHROUGH, env);
                         if (ret == 1)
                                 goto peek_stack;
                         else if (ret < 0)
                                 goto err_free;
Quoted 1 line
Quoted 1 line
Quoted 1 line
                     ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
                         if (ret == 1)
                                 goto peek_stack;
                         else if (ret < 0)
                                 goto err_free;
                 }
         } else {
Quoted 1 line

push_insn call. At this point insn[t].off could be 0, no?

insn[t].off can be anything, but the first thing that push_insn() checks is:
if (w < 0 || w >= env->prog->len)
only then it does:
env->explored_states[w] = STATE_LIST_MARK;
so we're good there.
Though thanks for triple checking :)

#3 lore raw
Quoted 62 lines · Alexei Starovoitov
Quoted 59 lines · Hannes Frederic Sowa
Quoted 27 lines · Alexei Starovoitov

Due to missing bounds check the DAG pass of the BPF verifier can corrupt the memory which can cause random crashes during program loading:

[8.449451] BUG: unable to handle kernel paging request at ffffffffffffffff
[8.451293] IP: [<ffffffff811de33d>] kmem_cache_alloc_trace+0x8d/0x2f0
[8.452329] Oops: 0000 [#1] SMP
[8.452329] Call Trace:
[8.452329]  [<ffffffff8116cc82>] bpf_check+0x852/0x2000
[8.452329]  [<ffffffff8116b7e4>] bpf_prog_load+0x1e4/0x310
[8.452329]  [<ffffffff811b190f>] ? might_fault+0x5f/0xb0
[8.452329]  [<ffffffff8116c206>] SyS_bpf+0x806/0xa30
Fixes f1bca824dabb ("bpf: add search pruning optimization to verifier")

---
Many things need to align for this crash to be seen, yet I managed to hit it. In my case JA was last insn, 't' was 255 and explored_states array had 256 elements. I've double checked other similar paths and all seems clean.

  kernel/bpf/verifier.c |    3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +0 -0
index a28e09c7825d..36508e69e92a 100644

kernel/bpf/verifier.c peek_stack: · line 1387

Quoted 9 lines
kernel/bpf/verifier.c +2 -1
@@ -1380,7 +1380,8 @@ peek_stack:
  			/* tell verifier to check for equivalent states
  			 * after every call and jump
  			 */
-			env->explored_states[t + 1] = STATE_LIST_MARK;
+			if (t + 1 < insn_cnt)
+				env->explored_states[t + 1] = STATE_LIST_MARK;
  		} else {
  			/* conditional jump with two edges */
  			ret = push_insn(t, t + 1, FALLTHROUGH, env);

Quick review:

We have env->explored_states[t+1] access in the

                 } else {
                         /* conditional jump with two edges */
                         ret = push_insn(t, t + 1, FALLTHROUGH, env);
                         if (ret == 1)
                                 goto peek_stack;
                         else if (ret < 0)
                                 goto err_free;
Quoted 1 line
Quoted 1 line
Quoted 1 line
                     ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
                         if (ret == 1)
                                 goto peek_stack;
                         else if (ret < 0)
                                 goto err_free;
                 }
         } else {
Quoted 1 line

push_insn call. At this point insn[t].off could be 0, no?

Quoted 7 lines

insn[t].off can be anything, but the first thing that push_insn() checks is:
if (w < 0 || w >= env->prog->len)
only then it does:
env->explored_states[w] = STATE_LIST_MARK;
so we're good there.
Though thanks for triple checking :)

Sorry, yes. That check was too obvious to me. ;)

Bye,
Hannes

maintainer Acked-by on v1 1/1
#4 lore raw
In reply to Alexei Starovoitov
Quoted 12 lines · Alexei Starovoitov

Due to missing bounds check the DAG pass of the BPF verifier can corrupt the memory which can cause random crashes during program loading:

[8.449451] BUG: unable to handle kernel paging request at ffffffffffffffff
[8.451293] IP: [<ffffffff811de33d>] kmem_cache_alloc_trace+0x8d/0x2f0
[8.452329] Oops: 0000 [#1] SMP
[8.452329] Call Trace:
[8.452329]  [<ffffffff8116cc82>] bpf_check+0x852/0x2000
[8.452329]  [<ffffffff8116b7e4>] bpf_prog_load+0x1e4/0x310
[8.452329]  [<ffffffff811b190f>] ? might_fault+0x5f/0xb0
[8.452329]  [<ffffffff8116c206>] SyS_bpf+0x806/0xa30
Quoted 2 lines
Fixes f1bca824dabb ("bpf: add search pruning optimization to verifier")

As far as I can tell, looks good to me. Any other access to a next instruction elsewhere would be blocked from push_insn() with an error.

maintainer
#5 lore raw
In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>
Date: Tue, 14 Apr 2015 15:57:13 -0700

Quoted 12 lines

Due to missing bounds check the DAG pass of the BPF verifier can corrupt the memory which can cause random crashes during program loading:

[8.449451] BUG: unable to handle kernel paging request at ffffffffffffffff
[8.451293] IP: [<ffffffff811de33d>] kmem_cache_alloc_trace+0x8d/0x2f0
[8.452329] Oops: 0000 [#1] SMP
[8.452329] Call Trace:
[8.452329]  [<ffffffff8116cc82>] bpf_check+0x852/0x2000
[8.452329]  [<ffffffff8116b7e4>] bpf_prog_load+0x1e4/0x310
[8.452329]  [<ffffffff811b190f>] ? might_fault+0x5f/0xb0
[8.452329]  [<ffffffff8116c206>] SyS_bpf+0x806/0xa30
Quoted 2 lines
Fixes f1bca824dabb ("bpf: add search pruning optimization to verifier")

Applied and queued up for -stable, thanks.