Skip to content

Cloudbleed

On 18 February 2017, Tavis Ormandy of Google's Project Zero contacted Cloudflare: some of its edge servers were returning chunks of uninitialized memory inside ordinary web pages. Other sites' cookies, authentication tokens and private messages were turning up in responses, and in search engines' caches. Cloudflare's incident report traced it to the HTML parser behind three features (email obfuscation, Server-side Excludes and Automatic HTTPS Rewrites). At its peak, around 1 in every 3,300,000 HTTP requests through Cloudflare could leak memory, and the earliest date memory could have leaked was 2016-09-22.

The root cause, in the report's words: "reaching the end of a buffer was checked using the equality operator and a pointer was able to step past the end of the buffer."

This page is a literate Pia file, docs/cases/cloudbleed.pia.md. The pia blocks are the fixed scanner, which the test suite proves, and the other code blocks are the broken versions, each checked to fail exactly as shown.

The bug

The parser was generated by Ragel, which turns a grammar into a state machine in C. The generated code walks a pointer p through the buffer and stops when it reaches the end pointer pe:

/* generated code */
if ( ++p == pe )
    goto _test_eof;

That test is correct as long as p only ever moves one byte at a time. Cloudflare's grammar had an error action, for an attribute left unfinished at the end of a buffer (an HTML page ending in <script type=, say), that jumped to another state without first stepping p back. So p was already at pe when the next state ran ++p. Now p was one past the end, p == pe was never true again, and the parser kept reading whatever memory came next. As the report says: "Had the check been done using >= instead of == jumping over the buffer end would have been caught."

The bug was not in Ragel, the report says, but in Cloudflare's use of it. A C compiler had no way to know that p and pe point into the same buffer, let alone that p must never pass pe.

The same shape in Pia

Pia has no pointers into buffers, but an index does the same job. This scanner is not Cloudflare's parser; it is the shape of the bug. It counts the attributes in some HTML, and when it sees an = it steps over the character after it too, the opening quote of the value. The loop stops when p equals the end, as the generated C did. The invariant says what the C code silently assumed, that p never passes the end:

let count_attrs (html : [u8]) : usize =
  let p : mutable usize = 0 in
  let n : mutable usize = 0 in
  while p <> len html
    invariant p <= len html && n <= p
    decreases len html - p
  do
    if html.[p] = 61 then (n <- n + 1; p <- p + 2)
    else p <- p + 1
  done;
  n

61 is =. piac will not compile it:

$ piac --verify scan.pia
...
scan.pia:8:47: failed: '+' does not overflow usize
  counterexample: len html = 18446744073709551615, result = 18446744073709551616
...
scan.pia:5:5: failed: the loop invariant holds after each iteration
  counterexample: len html = 18446462598732840960
...
5 proved, 2 failed, 0 skipped

The invariant failure is Cloudbleed. Counterexamples do not show the contents of a slice yet, only its length, but the case the solver found is the one that triggered the leak: p at the last byte, and that byte an =, an attribute cut off at the end of the buffer. The step takes p to one past the end, and the loop, testing only for equality, never stops. (The first failure is the same step from the other side: on a buffer of 2^64 - 1 bytes, p + 2 does not even fit in a usize.)

Without the invariant

Leaving the invariant out does not help. Then the verifier knows nothing about p inside the loop, and the read itself is the goal that fails:

let count_attrs (html : [u8]) : usize =
  let p : mutable usize = 0 in
  let n : mutable usize = 0 in
  while p <> len html
    decreases len html - p
  do
    if html.[p] = 61 then (n <- n +| 1; p <- p +| 2)
    else p <- p +| 1
  done;
  n
$ piac --verify scan_no_invariant.pia
scan_no_invariant.pia:7:8: failed: the index is within bounds
  counterexample: len html = 12583104, index = 18446744073696968511, length = 12583104
...
1 proved, 1 failed, 0 skipped

Either way, the program does not compile. In C, both compiled.

The fix, proved

The fix the report names is to test with >= instead of ==: in Pia, loop while p < len html. Here that is only half the fix:

let count_attrs (html : [u8]) : usize =
  let p : mutable usize = 0 in
  let n : mutable usize = 0 in
  while p < len html
    invariant p <= len html + 1 && n <= p && n <= len html
    decreases len html + 1 - p
  do
    if html.[p] = 61 then (n <- n + 1; p <- p + 2)
    else p <- p + 1
  done;
  n
$ piac --verify scan_lt.pia
...
scan_lt.pia:8:47: failed: '+' does not overflow usize
  counterexample: len html = 18446744073709551615, result = 18446744073709551616
...
scan_lt.pia:5:5: proved: the loop invariant holds after each iteration [smt]
...
6 proved, 1 failed, 0 skipped

The loop now stops, and every read is proved in bounds, but the step past the end is still there: on the largest possible buffer, p + 2 overflows. A C program would wrap the pointer around and carry on. The other half of the fix is not to step over a byte that is not there. The scanner skips the quote only when the buffer has one, so p never passes the end, and the loop test and the invariant say so. The postcondition is a small theorem about the result: there are never more attributes than bytes.

let count_attrs (html : [u8]) : usize
  ensures result <= len html
=
  let p : mutable usize = 0 in
  let n : mutable usize = 0 in
  while p < len html
    invariant p <= len html && n <= p
    decreases len html - p
  do
    if html.[p] = 61 && p + 1 < len html then (n <- n + 1; p <- p + 2)
    else p <- p + 1
  done;
  n

Every goal on this page is proved:

$ piac --verify cloudbleed.pia.md
...
cloudbleed.pia.md:173:5: proved: the loop invariant holds after each iteration [smt]
...
cloudbleed.pia.md:168:3: proved: the postcondition holds [smt]
9 proved, 0 failed, 0 skipped

The deeper fix is the one Pia makes the default. In the C code, the fact that p stays inside the buffer lived in the programmer's head. In Pia it is a goal at every read, so a grammar action that breaks it is a compile error at the line that breaks it, with the input that breaks it.

Sources