The Pointer That Ran Past the Buffer

Medium

· Backend

Incident

SEV-1

BUG-CB17

Priority: Critical (data leak) · reported by security

Time to fix: 20 min

Failing checks on the shipped build

  • a region without a terminator stops at its own end

    must not read past the owned length: expected [1,2,3] but got [1,2,3,44,55,66]

How to work this incident

  1. Read the report: write down the expected behaviour and the actual one in a sentence each.
  2. Reproduce it: run the visible tests and find the failing check that matches the report.
  3. Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
  4. Follow the request through the handler, the validation and the service it calls; find the first step whose output disagrees with the report.
  5. Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.

Inspired by

Cloudflare · Cloudbleed 2017

Modelled on Cloudbleed (Cloudflare, February 2017): an HTML rewriter checked for the end of its buffer with == instead of >=. When a page ended in just the wrong way the pointer stepped past the terminator, and the parser kept reading — spraying adjacent memory, including other customers' cookies and private messages, into public responses.

scanner.js walks a buffer looking for a terminator byte and returns everything before it. Its end check has the same flaw.

Fix scan so it can never read past the end of the buffer.

Code map

// Reads owned bytes up to (but not including) the terminator.
exports.scan = function (arena, length, terminator) {
  var out = [];
  var p = 0;
  // Stop when we reach the end of the backing store.
  while (p < arena.length) {
    if (arena[p] === terminator) return out;
    out.push(arena[p]);
    p++;
  }
  return out;
};

Sign in to edit the file, run the tests and submit a fix

A free account — no card, no trial clock. You come straight back here.

The Pointer That Ran Past the Buffer — JavaScript Bug Hunt

Modelled on Cloudbleed (Cloudflare, February 2017): an HTML rewriter checked for the end of its buffer with == instead of >=.

  • Language: JavaScript
  • Layer: Backend
  • Difficulty: Medium
  • Concepts: Memory Safety, Off-by-One
  • Modelled on: Cloudflare · Cloudbleed 2017
  • Visible tests: stops at the terminator; a region without a terminator stops at its own end
  • Reward: 50 XP for a complete fix

Incident

  • Severity: SEV-1 (priority: Critical (data leak))
  • Ticket: BUG-CB17
  • Reported by: security
  • Time to fix: 20 minutes, the target for a medium incident

Failing checks on the shipped build

  • a region without a terminator stops at its own end — must not read past the owned length: expected [1,2,3] but got [1,2,3,44,55,66]

How to work this incident

  1. Read the report: write down the expected behaviour and the actual one in a sentence each.
  2. Reproduce it: run the visible tests and find the failing check that matches the report.
  3. Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
  4. Follow the request through the handler, the validation and the service it calls; find the first step whose output disagrees with the report.
  5. Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.

Briefing

Modelled on Cloudbleed (Cloudflare, February 2017): an HTML rewriter checked for the end of its buffer with == instead of >=. When a page ended in just the wrong way the pointer stepped past the terminator, and the parser kept reading — spraying adjacent memory, including other customers' cookies and private messages, into public responses.

scanner.js walks a buffer looking for a terminator byte and returns everything before it. Its end check has the same flaw.

Fix scan so it can never read past the end of the buffer.

Bug report

BUG-CB17 · Priority: Critical (data leak) · Reported by: security

scan(arena, length, terminator):

  • arena is a shared backing store; this request owns only its first length bytes, and everything after that belongs to other requests
  • returns the owned bytes before the first terminator
  • if the terminator is absent within the owned region, returns all length of them

Observed: the loop bounds itself on the ARENA size rather than on length, so a request whose region has no terminator keeps reading into the next request's bytes and returns them.

Logs

[rewriter] scan overran: length=3 returned=6
[rewriter] response contained bytes from an unrelated request

The code as shipped

src/parser/scanner.js (editable)

// Reads owned bytes up to (but not including) the terminator.
exports.scan = function (arena, length, terminator) {
  var out = [];
  var p = 0;
  // Stop when we reach the end of the backing store.
  while (p < arena.length) {
    if (arena[p] === terminator) return out;
    out.push(arena[p]);
    p++;
  }
  return out;
};

Read-only context: src/parser/README.js.

Open the hunt to start the incident clock, edit the files, run the visible tests and submit against the hidden ones, then write the root cause down. More JavaScript bug hunts.