Vulnerability23 Feb 2017

Cloudbleed

An "==" that should have been ">=" let Cloudflare's parser leak private data into web pages, some later cached by search engines.

Severity
Front-page news
Difficulty
intermediate
Companies
Cloudflare, Google Project Zero
Tech
C, Ragel, NGINX, HTML parsing, Memory safety
Root cause
memory safety, latent bug

The story

Quick (1 min)

In February 2017, Tavis Ormandy of Google's Project Zero noticed something strange: some web pages served through Cloudflare contained random chunks of other people's data, such as cookies, login tokens and form submissions.

Cloudflare edits web pages on the fly for some features, like hiding email addresses from spam bots. Its old HTML parser had a bug: when a page ended in a certain broken way, the code stepped past the end of its memory buffer and kept reading. Whatever was next in memory got inserted into the page.

The bug had been sitting in the old parser for years. It only started leaking after Cloudflare introduced a new parser that changed how memory buffers were passed around. Search engines had already cached some leaked pages, so Cloudflare worked with Google, Bing and others to scrub them before announcing.

The lesson: in C, one wrong comparison can leak secrets. Use safe bounds checks, and remember that changing one component can wake up bugs in another.

Keep reading to the end to earn XP.Read. XP earned.

Engineer (2 min)

Background. Cloudflare's edge modified HTML for features like email obfuscation, Server-Side Excludes and Automatic HTTPS Rewrites. The original parser was written in Ragel, which generates C code that moves pointers through the document. A newer parser, cf-html, was gradually replacing it. Both ran as NGINX filter modules.

The bug. Generated end-of-buffer checks looked like if ( ++p == pe ) goto _test_eof;. In one state, used when consuming an unterminated attribute in a <script> tag, the parser ran out of characters with p == pe, then jumped to a state that incremented p again without stepping back. Now p was past pe, so the == check never fired, and parsing continued into adjacent memory. Cloudflare: "Had the check been done using >= instead of == jumping over the buffer end would have been caught."

Why it started leaking in 2016. The flaw had been in the Ragel parser "for many years". Introducing cf-html changed the buffering between NGINX filter modules, which made the overrun leak memory into responses. cf-html itself had no bug.

Exposure. Earliest possible leak: 22 September 2016. Peak exposure: 13–18 February 2017, at about 1 in every 3,300,000 HTTP requests (0.00003%). Leaked data included HTTP cookies, authentication tokens and POST bodies. SSL private keys were not leaked, because TLS terminated in a separate NGINX instance.

Response. Reported at 00:32 UTC on 18 February. Email Obfuscation was globally killed at 01:19 (47 minutes), Automatic HTTPS Rewrites at 04:24, and a kill switch for cf-html was deployed worldwide by 07:22. Server-Side Excludes was so old it predated global kill switches, so one had to be built. Cloudflare found 770 cached URIs across 161 domains and had them purged before disclosure.

Interview takeaway: use >= bounds checks and memory-safe parsers, give every feature a kill switch, and clean up leaked data, not just the bug.

Keep reading to the end to earn XP.Read. XP earned.

Deep dive (2 min)

Timeline (UTC, from Cloudflare's incident report)

  • 22 Sep 2016: Automatic HTTP Rewrites enabled using the new parser, the earliest date memory could have leaked.
  • 30 Jan 2017: Server-Side Excludes migrated to the new parser.
  • 13 Feb 2017: Email Obfuscation partially migrated, the start of the greatest exposure.
  • 18 Feb, 00:11: Tavis Ormandy tweets asking for a Cloudflare security contact.
  • 00:32: Cloudflare receives the details from Google.
  • 00:40: a cross-functional team assembles in San Francisco; London joins at 01:22.
  • 01:19: Email Obfuscation disabled worldwide (global kill, 47 minutes after the report).
  • 04:24: Automatic HTTPS Rewrites disabled worldwide.
  • 07:22: patch with a kill switch for the cf-html parser deployed worldwide. Fully mitigated in under 7 hours.
  • 18–23 Feb: search engine caches scrubbed with Google, Yahoo, Bing and others.
  • 23 Feb 2017: public incident report.

Impact

  • About 1 in 3,300,000 requests during the peak window.
  • 770 unique URIs across 161 domains found in search caches containing leaked memory, all purged.
  • Leaked memory could contain HTTP cookies, authentication tokens, POST bodies and other sensitive data.
  • "Cloudflare customer SSL private keys were not leaked."
  • "We have also not discovered any evidence of malicious exploits of the bug or other reports of its existence."

Why "==" wasn't enough

"The root cause of the bug was that 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."

An equality check only catches a pointer that lands exactly on the end. Any code path that advances twice skips right over it.

The disclosure choice

Cloudflare delayed announcing until search caches were scrubbed: "we felt we had a duty of care to ensure that search engine caches were scrubbed before a public announcement."

Kill switches matter

Every Cloudflare feature had a global kill flag, except Server-Side Excludes, which predated the mechanism. Building one and patching took about three hours, the slowest part of the response.

Keep reading to the end to earn XP.Read. XP earned.

Failure replay

Step by step, what broke and what it hit next.

  1. Years earlier
    Ragel HTML parserdegraded

    Generated C code checks the buffer end with "++p == pe". One state can step p past the end, so the check never fires.

    Affects: NGINX buffers

  2. Sep 22, 2016
    cf-html parserhealthy

    A new parser is introduced. It's correct itself, but it changes how buffers flow between NGINX modules.

    Affects: NGINX buffers

  3. Sep 2016 – Feb 2017
    NGINX bufferscompromised

    On pages with an unterminated script attribute, the old parser runs past the buffer and copies neighbouring memory into the response.

    Affects: Web pages served

  4. Feb 13–18, 2017
    Web pages servedcompromised

    About 1 in 3.3 million responses contains other users' cookies, tokens or POST data. Crawlers cache some of them.

    Affects: Search engine caches

  5. Feb 18, 00:32 UTC
    Project Zerohealthy

    Tavis Ormandy reports corrupted pages containing private data.

    Affects: Web pages served

  6. 01:19–07:22 UTC
    Web pages servedrecovering

    Global kill switches disable the affected features. A cf-html kill switch is deployed worldwide in under 7 hours.

  7. Feb 18–23
    Search engine cachesrecovering

    770 cached URIs across 161 domains are purged before public disclosure.

Impact

Who was affected
Visitors to sites using Cloudflare features that ran through the affected parser (email obfuscation, Server-Side Excludes, Automatic HTTPS Rewrites); about 1 in 3.3 million requests at peak.
How long
Leaks possible from 22 September 2016 to 18 February 2017, with peak exposure 13–18 February; mitigated within about 7 hours of the report.
Cost
not disclosed

Lessons

  • Use >= (or safe abstractions) for bounds checks; == misses any pointer that skips the end.
  • Changing one component can activate dormant bugs in another; test the combination.
  • Every feature should have a global kill switch, including old ones.
  • Clean up leaked data (search caches), not just the code.
  • Memory-safe languages remove this whole class of bug.

Quiz

  1. What was the root cause of Cloudbleed?

    Check answer

    An end-of-buffer check using == let a pointer step past the end of the buffer. Cloudflare said a >= check would have caught the pointer jumping over the buffer end.

  2. Why did the old bug start leaking only in 2016?

    Check answer

    Introducing the new cf-html parser changed NGINX buffering, which exposed the dormant bug. cf-html itself was fine, but its buffering changes enabled the leak in the old Ragel-based parser.

  3. Were Cloudflare customers' SSL private keys leaked?

    Check answer

    No; TLS was terminated by a separate NGINX instance not affected by the bug. Cloudflare stated private keys were not leaked because SSL termination was isolated.

  4. What did Cloudflare do before announcing publicly?

    Check answer

    Worked with search engines to purge 770 cached URIs containing leaked memory. It scrubbed search caches first so third parties couldn't hunt for leaked data.

Interview prep

Try answering out loud before you open the model answer.

  1. security

    Explain how Cloudbleed leaked memory and why changing == to >= would have prevented it.

    Show model answer

    The Ragel-generated parser advanced a pointer p through the buffer and checked "if (++p == pe)" to detect the end. In one error state it reached the end and then incremented p again without stepping back, so p went past pe. An equality check only catches p landing exactly on pe, so parsing continued into adjacent heap memory, which was copied into HTML responses. A ">=" check would have caught any pointer at or beyond the end. More broadly: use safe bounds checks, fuzz parsers, and prefer memory-safe languages for parsing untrusted input.

    A strong answer hits:

    • Pointer stepped past the end
    • == only catches an exact match; >= catches overshoot
    • Adjacent memory copied into responses
    • Fuzzing and memory-safe parsing
  2. system design

    How did Cloudflare contain Cloudbleed within hours, and what design features made that possible?

    Show model answer

    Global kill switches per feature let it disable Email Obfuscation in 47 minutes and Automatic HTTPS Rewrites shortly after, without a full deploy. A fast global deployment pipeline shipped a cf-html kill switch worldwide by 07:22. The one feature without a kill switch (Server-Side Excludes) slowed things down. Design lessons: give every feature a runtime kill flag, keep a fast and safe emergency deploy path, and keep an inventory of which features use which components.

    A strong answer hits:

    • Per-feature global kill switches
    • Fast emergency deploy path
    • Inventory of component dependencies
  3. behavioural

    Your service leaked sensitive data into publicly cached pages. Do you announce immediately or wait to clean up first?

    Show model answer

    Weigh harm. Announcing immediately tells users, but it also tells attackers to go search caches for secrets. Cloudflare chose to scrub search engine caches first (770 URIs across 161 domains) with Google, Bing and Yahoo, then disclose about five days later with full detail. I'd stop the leak at once, work urgently with cache operators, notify affected customers directly where possible, and disclose publicly as soon as cleanup makes it safe, while being transparent about the timeline afterwards.

    A strong answer hits:

    • Stop the leak immediately
    • Weigh disclosure against the risk of attackers hunting caches
    • Coordinate cleanup with third parties
    • Transparent timeline afterwards

Go deeper