Skip to content
DoS Write-up Advanced

Facebook Fizz integer overflow vulnerability (CVE-2019-3560)

An integer overflow in Facebook's Fizz TLS library allowed an unauthenticated remote attacker to send a crafted message that caused an infinite loop, making the server unresponsive. The attacker could disrupt service with a cheap denial-of-service attack.

Kevin Backhouse Follow

Published on securitylab.github.com

Bounty$10,000 View original

This post is about a denial of service vulnerability which I found in Facebook’s Fizz project, using a CodeQL query. The vulnerability is an infinite loop which can be triggered by an unauthenticated remote attacker.

kevinbackhouse, on securitylab.github.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 8 min read.

Context

Fizz is Facebook's TLS implementation, the code that handles the "https:" part of https://facebook.com. In a blog post about Fizz published on 2018-08-06, Facebook engineers Kyle Nekritz, Subodh Iyengar and Alex Guzman described how widely it is deployed:

We have deployed Fizz and TLS 1.3 globally in our mobile apps, Proxygen, our load balancers, our internal services, and even our QUIC library, mvfst. More than 50 percent of our internet traffic is now secured with TLS 1.3.

Fizz is open source, so other projects and organisations are likely to use it as well. The bug described here is an integer overflow in the record layer that turns into an infinite loop, reachable by an unauthenticated remote attacker over plain TCP. It was found with a CodeQL query and is tracked as CVE-2019-3560.

Setup

The vulnerable code is shown at commit [redacted]. and the proof of concept was tested only against the demo server application that ships with the Fizz source tree. No real websites were tested. The analysis tooling was CodeQL, run either through the CodeQL plugin for Eclipse with a snapshot of Fizz, or through the free CodeQL extension for Visual Studio Code. The exploit itself is a small C program that opens a TCP socket to a server and writes a payload.

Finding it

The vulnerable code is in PlaintextRecordLayer.cpp, around lines 39 to 44:

C
auto length = cursor.readBE<uint16_t>();
if (buf.chainLength() < (cursor - buf.front()) + length) {
  return folly::none;
}
length +=
    sizeof(ContentType) + sizeof(ProtocolVersion) + sizeof(uint16_t);
buf.trimStart(length);
continue;

The length value is read from the incoming packet as a uint16_t, so it is attacker-controlled. The if on line 39 looks like a bounds check but only confirms that enough data has arrived to keep parsing. The += on line 42 adds the sizes of the content type, protocol version and length field to that attacker-controlled value. Because length is a uint16_t, the addition can wrap.

The researcher's point is that the bug is obvious once you are looking at line 42; the hard part is knowing which line to look at. Fuzzing, which Facebook also runs on Fizz, depends on randomly generated inputs and struggles when the probability of reaching a particular code path by chance is very low. CodeQL is not constrained that way: it can search for every potential integer overflow regardless of how hard it is to trigger. The trade-off is precision, since developers cannot be asked to fix thousands of hypothetical bugs, so the query has to return only results where an attacker could plausibly trigger the overflow.

The query used here is an improved version written by the researcher's colleague Jonas Jensen. It relies on the new C++ intermediate representation (IR), which collapses multiple source syntaxes for the same operation into one node. For example, these three lines all do the same thing:

Text
x = x+1;
x += 1;
x++;

Without IR a query for additions needs a separate clause per syntax; with IR it needs one clause for AddInstruction.

The first step is a query for all conversions from a larger type to a smaller type, which are the conversions that might overflow:

QL
import cpp
import semmle.code.cpp.ir.IR

from ConvertInstruction conv
where conv.getResultSize() < conv.getUnary().getResultSize()
select conv

That returns a lot of narrowing conversions, so the next step is to restrict them to values that may depend on untrusted input. The untrusted input in Fizz arrives through another Facebook library, Folly, which places the data in an IOBuf that Fizz then reads. One way to model the source would be to find all uses of IOBuf in Fizz, but the query takes a simpler and less project-specific route: data sent over a socket is usually in network byte order and has to be converted to host byte order, typically with ntohs or ntohl, so those functions are good proxies for untrusted input. Fizz does not use ntohs or ntohl; it uses Folly's Endian class. This QL class identifies the Endian method that converts network byte order to host:

QL
class EndianConvert extends Function {
  EndianConvert() {
    this.getName() = "big" and
    this.getDeclaringType().getName().matches("Endian")
  }
}

Putting it together, the full query uses the IR taint tracking library to find narrowing conversions of expressions that may depend on untrusted input:

QL
/**
 * @name Fizz Overflow
 * @description Narrowing conversions on untrusted data could enable
 *              an attacker to trigger an integer overflow.
 * @kind path-problem
 * @problem.severity warning
 */

import cpp
import semmle.code.cpp.ir.dataflow.TaintTracking
import semmle.code.cpp.ir.IR
import DataFlow::PathGraph

/**
 * The endianness conversion function `Endian::big()`.
 * It is Folly's replacement for `ntohs` and `ntohl`.
 */
class EndianConvert extends Function {
  EndianConvert() {
    this.getName() = "big" and
    this.getDeclaringType().getName().matches("Endian")
  }
}

/**
 * Holds if `i` is an endianness conversion.
 * (A telltale sign of network data.)
 */
predicate isNetworkData(Instruction i) {
  i.(CallInstruction).getCallTarget().(FunctionInstruction).getFunctionSymbol() instanceof
    EndianConvert
}

/** Holds if `i` is a narrowing conversion. */
predicate isNarrowingConversion(ConvertInstruction i) {
  i.getResultSize() < i.getUnary().getResultSize()
}

class Cfg extends TaintTracking::Configuration {
  Cfg() { this = "FizzOverflowIR" }

  /**
   * Holds if `source` is network data.
   */
  override predicate isSource(DataFlow::Node source) { isNetworkData(source.asInstruction()) }

  /** Holds if `sink` is a narrowing conversion. */
  override predicate isSink(DataFlow::Node sink) { isNarrowingConversion(sink.asInstruction()) }
}

from
  Cfg cfg, DataFlow::PathNode source, DataFlow::PathNode sink, ConvertInstruction conv,
  Type inputType, Type outputType
where
  cfg.hasFlowPath(source, sink) and
  conv = sink.getNode().asInstruction() and
  inputType = conv.getUnary().getResultType() and
  outputType = conv.getResultType()
select sink, source, sink,
  "Conversion of untrusted data from " + inputType + " to " + outputType + "."

The query has exactly one result, the vulnerability described above, and zero results on newer revisions that include the fix.

Exploitation

Step 1: send a payload just over 64KB

The proof of concept is a simple C program that opens a TCP socket to the server and sends a malicious payload just over 64KB in size. It closes the socket as soon as the payload has been sent, but the server does not notice because it is already stuck in an infinite loop. The source code of the exploit was held back for a couple of weeks so that other Fizz users had time to upgrade, and the full details of how to construct a message that reaches the vulnerable line were held back for the same reason.

Step 2: set the length field to 0xFFFB

The exploit works by setting length = 0xFFFB. After the += on line 42, the value of length is 0. The call to trimStart on line 43 therefore consumes no data, so no progress is made before the next iteration of the loop, and the loop never terminates.

Step 3: satisfy the size check first

The reason the payload has to be just over 64KB is that the code does not reach the integer overflow on line 42 until it has received at least length bytes. The if on line 39 has to pass before the addition happens.

Root cause

The mistake is the use of a uint16_t to hold a value that is then incremented by the sizes of the content type, protocol version and length field. The attacker controls the starting value, so choosing 0xFFFB makes the addition wrap to zero. The subsequent trimStart(0) consumes nothing and the parsing loop spins forever. The if on line 39 does not prevent this because it is a check that enough data has been received to continue parsing, not a check on the arithmetic that follows.

The fix is simple: compute the addition in a type larger than uint16_t, so that the integer overflow is impossible.

As for why the existing defences did not catch it, the researcher notes that Fizz is a high quality project: modern C++ style, no manual memory management, so it is far less likely to suffer from something like a buffer overflow, and Facebook have said they use fuzzing on Fizz and have done a security review with an external consultancy. Fuzzing is based on randomly generated inputs and does not work well when the probability of hitting certain code paths by chance is very low, and the hardest part of writing the exploit was constructing an input that reaches the vulnerable line. A static query over the IR is not limited in that way.

Impact

An attacker can send a malicious message via TCP to any server that uses Fizz and trigger an infinite loop on that server, which can make it unresponsive to other clients. The vulnerability is a denial of service: it lets an attacker disrupt the service but not gain unauthorised access. The message is just over 64KB, so the attack is extremely cheap for the attacker and crippling for the server. A single computer with an unexceptional domestic-grade internet connection (1Mbps upload speed) could send two of these messages per second, and since each message knocks out one CPU core, a small botnet could quickly debilitate an entire datacentre.

The researcher did not test the payload on any real websites, only on the demo server application included with the Fizz source code. Because the vulnerability is in the core of the Fizz library rather than the demo application, the researcher believes https://facebook.com was at risk until the report was made. Facebook patched their own servers within hours on 2019-02-20.

Fix

Facebook pushed the fix to GitHub on 2019-02-25, in commit [redacted], and the vulnerability was fixed in version v2019.02.25.00. The change computes the addition in a larger type than uint16_t so that the overflow cannot occur. The researcher is not aware of any mitigation other than upgrading Fizz, and recommends that all Fizz users upgrade as soon as possible.

Variants to test

  1. Other length-prefixed parsers in TLS and QUIC stacks: look for a 16-bit length read from the wire that is then incremented by fixed header sizes before being used to advance a cursor. A positive result is a length value near 0xFFFF that makes the advance wrap to zero and the parse loop make no progress.
  2. Other uses of Folly's Endian::big() in code that feeds a narrowing conversion: run the taint query above against a different codebase that uses Folly, and treat any path from Endian::big() to a smaller result type as worth manual review.
  3. Other endianness helpers in place of ntohs/ntohl: adapt the EndianConvert class to the project's own byte-order conversion functions, then check whether the query returns paths that reach arithmetic on the converted value.
  4. Loops that call trimStart or an equivalent cursor advance: check whether the advance can be zero for an attacker-chosen input while the loop condition still holds. A positive result is a request that keeps a worker busy without consuming input.
  5. Fuzzing gaps around deep parse paths: for code where reaching a branch requires a large, structured input, compare what coverage-guided fuzzing finds with what a static narrowing-conversion query reports. A positive result is a reachable overflow that fuzzing has not surfaced.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 23 min read.

What happened, in plain words

A security researcher found a bug in a piece of software called Fizz, which Facebook uses to run the secure part of its website. The bug lets anyone on the internet send a message of just over 64KB to a server and make it get stuck in an infinite loop, occupying one CPU core. Facebook fixed it within hours of being told, paid a $10,000 bounty, and the bug was later given the public identifier CVE-2019-3560.

The researcher found the bug not by guessing or by hand, but by writing a question for a tool called CodeQL, which reads source code the way a search engine reads the web. The question was: "show me every place where a number coming from the network is squeezed into a smaller box than it needs." The tool answered with exactly one result, and that result was the vulnerability.

This article walks through the whole story slowly, explaining each idea from scratch: what Fizz is, what an integer overflow is, what an infinite loop is, what a denial of service is, how CodeQL works, and how the researcher turned the finding into a working attack. No prior security knowledge is assumed.

Context

What Fizz is

When you type https://facebook.com into your browser, the https part means the traffic between your browser and Facebook's servers is encrypted. Encryption turns readable data into scrambled data so that anyone listening on the network (a café Wi-Fi operator, your internet provider, a government) sees only gibberish. The set of rules that two computers follow to set up that encrypted channel is called TLS, short for Transport Layer Security.

Fizz is Facebook's own implementation of TLS. "Implementation" means Facebook did not just use an off-the-shelf TLS library written by someone else; its engineers wrote their own version of the code that performs the TLS handshake and encrypts and decrypts the traffic. Fizz is the code that handles the "https:" part of https://facebook.com.

In a blog post about Fizz published on 2018-08-06, Facebook engineers Kyle Nekritz, Subodh Iyengar and Alex Guzman described how widely it is deployed:

We have deployed Fizz and TLS 1.3 globally in our mobile apps, Proxygen, our load balancers, our internal services, and even our QUIC library, mvfst. More than 50 percent of our internet traffic is now secured with TLS 1.3.

In plain words: Fizz is not a side project. It runs in Facebook's phone apps, in the software that distributes incoming traffic across its servers (load balancers), in its internal services, and in its QUIC library (QUIC is a newer transport protocol that sits alongside TLS). More than half of all Facebook's internet traffic at the time was protected by the version of TLS that Fizz implements.

Fizz is also open source, meaning its source code is published publicly on GitHub for anyone to read, use and modify. That is good for transparency, but it also means other projects and organisations are likely to use Fizz as well, so a bug in Fizz is not only Facebook's problem.

What the bug is, in one sentence

The bug described here is an integer overflow in the part of Fizz that reads incoming messages (the "record layer"), and that overflow turns into an infinite loop. It can be reached by an unauthenticated remote attacker over plain TCP. Each of those bolded terms is explained below. It was found with a CodeQL query and is tracked as CVE-2019-3560.

  • Integer overflow: computers store numbers in fixed-size boxes. If you add to a number and the result is too big for the box, the extra digits fall off and the number wraps around to a small value, like a car odometer rolling from 99999 back to 00000. That wrap-around is an integer overflow.
  • Infinite loop: a loop is a piece of code that repeats. An infinite loop is one that never stops because the condition that should end it is never met. The program sits there forever, doing nothing useful.
  • Unauthenticated: the attacker does not need an account, a password or any login. Anyone who can reach the server over the network can try.
  • Remote: the attacker does not need to be sitting at the server or have any special access. They can be anywhere on the internet.
  • TCP: the basic protocol that carries most internet traffic. When your browser connects to a website, it opens a TCP connection first. It is like a phone call between two computers: once the line is open, either side can send bytes.

Setup

What the researcher worked with

To find and confirm the bug, the researcher needed three things:

  1. The Fizz source code, at a specific revision identified by the commit hash [redacted]. A commit hash is a long string of letters and numbers that uniquely identifies one exact snapshot of a codebase, like a fingerprint. It matters because code changes over time; saying "the bug is on line 42" only makes sense if you say which version of the file you mean.
  2. A test server. The researcher tested the exploit only against the demo server application that ships with the Fizz source tree. A demo application is a small example program included with a library so that developers can try it out. No real websites were tested.
  3. CodeQL, the analysis tool. CodeQL is a system that lets you ask questions about source code in a query language, and it answers by pointing at the exact lines that match. The report suggests running the query with the CodeQL plugin for Eclipse and a snapshot of Fizz, or with the free CodeQL extension for Visual Studio Code. A snapshot here means a prepared, indexed copy of the code that the tool can search quickly.

The exploit itself is a small C program that opens a TCP socket to a server and writes a payload. A socket is the programming object that represents one end of a network connection; opening a socket is how a program dials another computer. A payload is the data you send over that connection.

Finding it

The vulnerable code

The bug lives in a file called PlaintextRecordLayer.cpp, around lines 39 to 44. Here is that code exactly as it appears:

C
auto length = cursor.readBE<uint16_t>();
if (buf.chainLength() < (cursor - buf.front()) + length) {
  return folly::none;
}
length +=
    sizeof(ContentType) + sizeof(ProtocolVersion) + sizeof(uint16_t);
buf.trimStart(length);
continue;

Let us go through it line by line, in plain words.

  • auto length = cursor.readBE<uint16_t>(); reads a number from the incoming network packet and stores it in a variable called length. The uint16_t part says the number is stored in a 16-bit box, which can hold values from 0 to 65535. readBE means "read in big-endian order", which is the byte order used on networks. Crucially, this number comes straight from whoever sent the packet, so it is attacker-controlled: the attacker chooses its value.
  • if (buf.chainLength() < (cursor - buf.front()) + length) { checks whether the amount of data received so far is less than the position we have reached plus the length we were told to expect. If so, there is not enough data yet.
  • return folly::none; means "stop for now, nothing to parse yet". folly is another Facebook library, and folly::none is its way of saying "no value".
  • length += sizeof(ContentType) + sizeof(ProtocolVersion) + sizeof(uint16_t); adds the sizes of three fixed header fields (the content type, the protocol version and the length field itself) to length. sizeof asks the compiler how many bytes each of those types occupies. This is the line with the bug.
  • buf.trimStart(length); advances the read position by length bytes, effectively saying "we have consumed this much of the buffer".
  • continue; jumps back to the top of the loop to parse the next record.

The length value is read from the incoming packet as a uint16_t, so it is attacker-controlled. The if on line 39 looks like a bounds check but only confirms that enough data has arrived to keep parsing. The += on line 42 adds the sizes of the content type, protocol version and length field to that attacker-controlled value. Because length is a uint16_t, the addition can wrap.

Why the bug is hard to find by eye

The researcher's point is that the bug is obvious once you are looking at line 42; the hard part is knowing which line to look at. A large codebase has thousands of lines, and a human reading through them will not spot every arithmetic mistake.

Facebook also runs fuzzing on Fizz. Fuzzing means feeding a program huge numbers of randomly generated or mutated inputs and watching for crashes. It is a powerful technique, but it depends on randomly generated inputs and struggles when the probability of reaching a particular code path by chance is very low. If a bug only triggers when the input has a very specific structure, random inputs will almost never produce it.

CodeQL is not constrained that way: it can search for every potential integer overflow regardless of how hard it is to trigger. The trade-off is precision, since developers cannot be asked to fix thousands of hypothetical bugs, so the query has to return only results where an attacker could plausibly trigger the overflow.

The improved query

The query used here is an improved version written by the researcher's colleague Jonas Jensen. It relies on the new C++ intermediate representation (IR), which collapses multiple source syntaxes for the same operation into one node. An intermediate representation is a simplified, standardised form of the code that the analysis tool works on, so that the tool does not have to care about every way a programmer might have written the same thing.

For example, these three lines all do the same thing:

Text
x = x+1;
x += 1;
x++;
  • x = x+1; takes the current value of x, adds one, and stores the result back in x.
  • x += 1; is shorthand for the same thing.
  • x++; is also shorthand for the same thing.

Without IR a query for additions needs a separate clause per syntax; with IR it needs one clause for AddInstruction. In other words, the tool sees all three as the same underlying "add one" operation, so the query author writes one rule instead of three.

Step 1: find every narrowing conversion

The first step is a query for all conversions from a larger type to a smaller type, which are the conversions that might overflow:

QL
import cpp
import semmle.code.cpp.ir.IR

from ConvertInstruction conv
where conv.getResultSize() < conv.getUnary().getResultSize()
select conv

Line by line:

  • import cpp loads the part of CodeQL that understands C and C++ code.
  • import semmle.code.cpp.ir.IR loads the part that understands the intermediate representation, so the query can talk about IR instructions.
  • from ConvertInstruction conv says: look at every conversion instruction in the code, and call each one conv.
  • where conv.getResultSize() < conv.getUnary().getResultSize() keeps only those conversions where the result is stored in fewer bits than the input. That is what "narrowing" means: squeezing a big number into a small box.
  • select conv outputs each matching conversion.

A narrowing conversion is exactly the kind of operation that can silently lose information, and therefore the kind that can overflow.

Step 2: restrict to untrusted input

That returns a lot of narrowing conversions, so the next step is to restrict them to values that may depend on untrusted input. Untrusted input means any data that comes from outside the program and that an attacker could control: a network packet, a file, a form field.

The untrusted input in Fizz arrives through another Facebook library, Folly, which places the data in an IOBuf that Fizz then reads. An IOBuf is Folly's object for holding a chunk of bytes, typically data read from or about to be written to a network connection.

One way to model the source would be to find all uses of IOBuf in Fizz, but the query takes a simpler and less project-specific route: data sent over a socket is usually in network byte order and has to be converted to host byte order, typically with ntohs or ntohl, so those functions are good proxies for untrusted input.

What does that mean? Computers can store a multi-byte number in two orders: big-endian (most significant byte first) or little-endian (least significant byte first). Different machines use different orders. To make sure both ends agree, network protocols specify one order, called network byte order. When a program receives a number from the network, it must convert it to whatever order its own machine uses. The C functions ntohs ("network to host, short") and ntohl ("network to host, long") do exactly that. So if you see a call to ntohs or ntohl, you know the value came from the network, and therefore from a potential attacker.

Fizz does not use ntohs or ntohl; it uses Folly's Endian class. This QL class identifies the Endian method that converts network byte order to host:

QL
class EndianConvert extends Function {
  EndianConvert() {
    this.getName() = "big" and
    this.getDeclaringType().getName().matches("Endian")
  }
}

Line by line:

  • class EndianConvert extends Function { defines a new category called EndianConvert, which is a kind of Function (a function in the code).
  • EndianConvert() { is the condition that a function must satisfy to belong to this category.
  • this.getName() = "big" requires the function to be named big.
  • this.getDeclaringType().getName().matches("Endian") requires the function to belong to a type whose name matches Endian.
  • The closing braces end the definition.

So this class captures exactly the function Fizz uses to read a number from the network: Endian::big().

Step 3: the full query

Putting it together, the full query uses the IR taint tracking library to find narrowing conversions of expressions that may depend on untrusted input. Taint tracking is the idea that you mark certain values as "tainted" (coming from an attacker) and then follow them through the program, through every assignment, function call and arithmetic operation, to see whether they ever reach a dangerous place. It is the same idea as tracing a drop of dye through a pipe system.

QL
/**
 * @name Fizz Overflow
 * @description Narrowing conversions on untrusted data could enable
 *              an attacker to trigger an integer overflow.
 * @kind path-problem
 * @problem.severity warning
 */

import cpp
import semmle.code.cpp.ir.dataflow.TaintTracking
import semmle.code.cpp.ir.IR
import DataFlow::PathGraph

/**
 * The endianness conversion function `Endian::big()`.
 * It is Folly's replacement for `ntohs` and `ntohl`.
 */
class EndianConvert extends Function {
  EndianConvert() {
    this.getName() = "big" and
    this.getDeclaringType().getName().matches("Endian")
  }
}

/**
 * Holds if `i` is an endianness conversion.
 * (A telltale sign of network data.)
 */
predicate isNetworkData(Instruction i) {
  i.(CallInstruction).getCallTarget().(FunctionInstruction).getFunctionSymbol() instanceof
    EndianConvert
}

/** Holds if `i` is a narrowing conversion. */
predicate isNarrowingConversion(ConvertInstruction i) {
  i.getResultSize() < i.getUnary().getResultSize()
}

class Cfg extends TaintTracking::Configuration {
  Cfg() { this = "FizzOverflowIR" }

  /**
   * Holds if `source` is network data.
   */
  override predicate isSource(DataFlow::Node source) { isNetworkData(source.asInstruction()) }

  /** Holds if `sink` is a narrowing conversion. */
  override predicate isSink(DataFlow::Node sink) { isNarrowingConversion(sink.asInstruction()) }
}

from
  Cfg cfg, DataFlow::PathNode source, DataFlow::PathNode sink, ConvertInstruction conv,
  Type inputType, Type outputType
where
  cfg.hasFlowPath(source, sink) and
  conv = sink.getNode().asInstruction() and
  inputType = conv.getUnary().getResultType() and
  outputType = conv.getResultType()
select sink, source, sink,
  "Conversion of untrusted data from " + inputType + " to " + outputType + "."

Line by line:

  • The block at the top between /** and */ is metadata: a name for the query, a description, the kind of result (a path problem, meaning it reports a path from source to sink), and a severity (warning).
  • import cpp loads the C/C++ support.
  • import semmle.code.cpp.ir.dataflow.TaintTracking loads the taint tracking machinery.
  • import semmle.code.cpp.ir.IR loads the intermediate representation support.
  • import DataFlow::PathGraph loads the code that can display a path from source to sink as a graph.
  • The EndianConvert class is the same as before: it identifies Endian::big().
  • predicate isNetworkData(Instruction i) defines a rule that says an instruction i is network data if it is a call to a function that belongs to EndianConvert. The body i.(CallInstruction).getCallTarget().(FunctionInstruction).getFunctionSymbol() instanceof EndianConvert walks from the instruction to the function it calls and checks whether that function is an EndianConvert.
  • predicate isNarrowingConversion(ConvertInstruction i) defines a rule that says a conversion is narrowing if its result is smaller than its input.
  • class Cfg extends TaintTracking::Configuration defines a taint tracking configuration, which is the set of rules that tell the tracker where taint starts and where it ends.
  • Cfg() { this = "FizzOverflowIR" } gives the configuration a name.
  • override predicate isSource(DataFlow::Node source) { isNetworkData(source.asInstruction()) } says: taint starts at any node that is network data.
  • override predicate isSink(DataFlow::Node sink) { isNarrowingConversion(sink.asInstruction()) } says: taint ends at any node that is a narrowing conversion.
  • The from clause declares the variables the query will use: the configuration, a source node, a sink node, the conversion instruction, and the input and output types.
  • The where clause requires that there is a flow path from source to sink, that the sink is the conversion, and that the input and output types are those of the conversion.
  • The select clause outputs the sink, the source, the sink again, and a message saying "Conversion of untrusted data from X to Y."

The query has exactly one result, the vulnerability described above, and zero results on newer revisions that include the fix.

Exploitation

Step 1: send a payload just over 64KB

The proof of concept is a simple C program that opens a TCP socket to the server and sends a malicious payload just over 64KB in size. It closes the socket as soon as the payload has been sent, but the server does not notice because it is already stuck in an infinite loop. The source code of the exploit was held back for a couple of weeks so that other Fizz users had time to upgrade, and the full details of how to construct a message that reaches the vulnerable line were held back for the same reason.

Why hold back? The report says the source code and full details were held back for a couple of weeks so that other Fizz users had time to upgrade. Delaying publication gives defenders time to update.

Step 2: set the length field to 0xFFFB

The exploit works by setting length = 0xFFFB. After the += on line 42, the value of length is 0. The call to trimStart on line 43 therefore consumes no data, so no progress is made before the next iteration of the loop, and the loop never terminates.

Why does 0xFFFB become 0? 0xFFFB in hexadecimal is 65531 in decimal. The += adds the sizes of the content type, protocol version and length field. 65531 + 5 = 65536, which is one more than the largest value a 16-bit box can hold (65535). So the result wraps around to 0. trimStart(0) then consumes nothing, the loop goes back to the top, reads the same bytes again, computes the same zero, and repeats forever.

Step 3: satisfy the size check first

The reason the payload has to be just over 64KB is that the code does not reach the integer overflow on line 42 until it has received at least length bytes. The if on line 39 has to pass before the addition happens. Since length is 65531, the server must have received at least 65531 bytes before it will proceed past the if. The code does not reach the integer overflow on line 42 until it has received at least length bytes.

Root cause

The mistake is the use of a uint16_t to hold a value that is then incremented by the sizes of the content type, protocol version and length field. The attacker controls the starting value, so choosing 0xFFFB makes the addition wrap to zero. The subsequent trimStart(0) consumes nothing and the parsing loop spins forever. The if on line 39 does not prevent this because it is a check that enough data has been received to continue parsing, not a check on the arithmetic that follows.

The fix is simple: compute the addition in a type larger than uint16_t, so that the integer overflow is impossible. The fix uses a larger type than uint16_t to compute the addition, so that an integer overflow is impossible.

As for why the existing defences did not catch it, the researcher notes that Fizz is a high quality project: modern C++ style, no manual memory management, so it is far less likely to suffer from something like a buffer overflow, and Facebook have said they use fuzzing on Fizz and have done a security review with an external consultancy. Fuzzing is based on randomly generated inputs and does not work well when the probability of hitting certain code paths by chance is very low, and the hardest part of writing the exploit was constructing an input that reaches the vulnerable line. A static query over the IR is not limited in that way.

Impact

An attacker can send a malicious message via TCP to any server that uses Fizz and trigger an infinite loop on that server, which can make it unresponsive to other clients. The vulnerability is a denial of service: it lets an attacker disrupt the service but not gain unauthorised access. In other words, the attacker cannot read other people's data or take over the server; they can only make it stop working.

The message is just over 64KB, so the attack is extremely cheap for the attacker and crippling for the server. A single computer with an unexceptional domestic-grade internet connection (1Mbps upload speed) could send two of these messages per second, and since each message knocks out one CPU core, a small botnet could quickly debilitate an entire datacentre. A CPU core is one of the processing units inside a computer; a server with, say, 16 cores can handle 16 things at once, and each stuck loop occupies one core permanently. A botnet is a collection of compromised computers controlled by one attacker, which can be used to send traffic from many places at once.

The researcher did not test the payload on any real websites, only on the demo server application included with the Fizz source code. Because the vulnerability is in the core of the Fizz library rather than the demo application, the researcher believes https://facebook.com was at risk until the report was made. Facebook patched their own servers within hours on 2019-02-20.

Fix

Facebook pushed the fix to GitHub on 2019-02-25, in commit [redacted], and the vulnerability was fixed in version v2019.02.25.00. The change computes the addition in a larger type than uint16_t so that the overflow cannot occur. The researcher is not aware of any mitigation other than upgrading Fizz, and recommends that all Fizz users upgrade as soon as possible.

A mitigation is a workaround that reduces the risk without fixing the underlying bug, such as a firewall rule or a configuration change. The researcher knew of none here, so the only safe action was to upgrade to the fixed version.

Variants to test

These are ideas for other researchers who want to look for similar bugs elsewhere. Each one is a way to generalise the finding.

  1. Other length-prefixed parsers in TLS and QUIC stacks: look for a 16-bit length read from the wire that is then incremented by fixed header sizes before being used to advance a cursor. A positive result is a length value near 0xFFFF that makes the advance wrap to zero and the parse loop make no progress.
  2. Other uses of Folly's Endian::big() in code that feeds a narrowing conversion: run the taint query above against a different codebase that uses Folly, and treat any path from Endian::big() to a smaller result type as worth manual review.
  3. Other endianness helpers in place of ntohs/ntohl: adapt the EndianConvert class to the project's own byte-order conversion functions, then check whether the query returns paths that reach arithmetic on the converted value.
  4. Loops that call trimStart or an equivalent cursor advance: check whether the advance can be zero for an attacker-chosen input while the loop condition still holds. A positive result is a request that keeps a worker busy without consuming input.
  5. Fuzzing gaps around deep parse paths: for code where reaching a branch requires a large, structured input, compare what coverage-guided fuzzing finds with what a static narrowing-conversion query reports. A positive result is a reachable overflow that fuzzing has not surfaced.

Glossary

  • API: Application Programming Interface, a defined way for one piece of software to talk to another. Not used in this report, but a common term in security write-ups.
  • Attacker-controlled: a value that an attacker can choose, because it comes from outside the program (a network packet, a file, a form field).
  • Big-endian: a byte order where the most significant byte of a number comes first.
  • Botnet: a collection of compromised computers controlled by one attacker, often used to send traffic from many places at once.
  • Bounds check: code that verifies a value is within an allowed range before using it.
  • Buffer: a region of memory used to hold data temporarily, often data read from or about to be written to a network connection.
  • Buffer overflow: a bug where a program writes more data into a buffer than it can hold, corrupting nearby memory.
  • C: a programming language widely used for system software.
  • C++: a programming language that extends C with features such as classes and templates.
  • CodeQL: a tool that lets you ask questions about source code in a query language and returns the lines that match.
  • Commit hash: a long string of letters and numbers that uniquely identifies one exact snapshot of a codebase.
  • CPU core: one of the processing units inside a computer; a server with many cores can handle many things at once.
  • CVE: Common Vulnerabilities and Exposures, a public catalogue that gives each known security bug a unique identifier such as CVE-2019-3560.
  • Denial of service (DoS): an attack that makes a service unavailable to legitimate users, without gaining unauthorised access.
  • Demo application: a small example program included with a library so that developers can try it out.
  • Eclipse: a code editor and development environment.
  • Endianness: the order in which a computer stores the bytes of a multi-byte number (big-endian or little-endian).
  • Exploit: code or a technique that takes advantage of a vulnerability.
  • Fizz: Facebook's own implementation of TLS, the code that handles the "https:" part of https://facebook.com.
  • Folly: a Facebook C++ library that Fizz uses, including its IOBuf and Endian classes.
  • Fuzzing: feeding a program huge numbers of randomly generated or mutated inputs and watching for crashes.
  • Host byte order: the byte order that the local machine uses natively.
  • Infinite loop: a loop that never terminates because the condition that should end it is never met.
  • Integer overflow: a bug where an arithmetic result is too big for the fixed-size box that holds it, so it wraps around to a small value.
  • Intermediate representation (IR): a simplified, standardised form of code that an analysis tool works on, so it does not have to care about every way a programmer might have written the same thing.
  • IOBuf: Folly's object for holding a chunk of bytes, typically data read from or about to be written to a network connection.
  • Little-endian: a byte order where the least significant byte of a number comes first.
  • Load balancer: software that distributes incoming traffic across multiple servers.
  • Mitigation: a workaround that reduces the risk of a vulnerability without fixing the underlying bug.
  • Narrowing conversion: a conversion that stores a value in a smaller box than it came from, which can silently lose information.
  • Network byte order: the byte order that network protocols use, so that both ends agree regardless of their native order.
  • ntohs / ntohl: C functions that convert a number from network byte order to host byte order ("network to host, short" and "network to host, long").
  • Open source: software whose source code is published publicly for anyone to read, use and modify.
  • Payload: the data you send over a network connection.
  • QUIC: a newer transport protocol that sits alongside TLS.
  • Remote: reachable from another computer over a network, not requiring physical access.
  • Snapshot: a prepared, indexed copy of a codebase that an analysis tool can search quickly.
  • Socket: the programming object that represents one end of a network connection.
  • Source code: the human-readable text of a program, before it is compiled into something the computer runs.
  • Static analysis: examining code without running it, to find patterns or bugs.
  • Taint tracking: marking certain values as coming from an attacker and following them through the program to see whether they reach a dangerous place.
  • TCP: the basic protocol that carries most internet traffic; a connection-oriented stream of bytes between two computers.
  • TLS: Transport Layer Security, the set of rules that two computers follow to set up an encrypted channel.
  • trimStart: a function that advances a read position by a given number of bytes, effectively consuming that much of a buffer.
  • uint16_t: a C/C++ type that stores an unsigned 16-bit integer, holding values from 0 to 65535.
  • Unauthenticated: not requiring an account, password or any login.
  • Untrusted input: any data that comes from outside the program and that an attacker could control.
  • Visual Studio Code: a code editor and development environment.
  • Vulnerability: a weakness in software that an attacker can exploit to cause harm.

More from Meta

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.