[bl] Uninitialized memory exposure via negative .consume()
A negative argument to the BufferList consume() method corrupts internal state, causing slice() to return uninitialized memory. An attacker who controls the consume() argument could expose sensitive data from process memory.
- Triaged in 3 days
- Fixed in 3 days
- Disclosed after 3 days
Module
module name: bl
version: 4.0.2
npm page: https://www.npmjs.com/package/bl
Module Description
A Node.js Buffer list collector, reader and streamer thingy.
Module Stats
8 660 595 weekly downloads
Vulnerability
Vulnerability Description
If user input (even typed) ends up in consume() argument and can become negative,
BufferList state can be corrupted, tricking it into exposing uninitialized memory via
regular .slice() calls.
Steps To Reproduce:
const { BufferList } = require('bl')
const secret = require('crypto').randomBytes(256)
for (let i = 0; i < 1e6; i++) {
const clone = Buffer.from(secret)
const bl = new BufferList()
bl.append(Buffer.from('a'))
bl.consume(-1024)
const buf = bl.slice(1)
if (buf.indexOf(clone) !== -1) {
console.error(`Match (at ${i})`, buf)
}
}
Patch
First component (more important):
In BufferList.prototype.copy, before the last return dst:
if (dst.length !== bufoff) return dst.slice(0, bufoff)
Second component:
Check .consume() argument to be a non-negative integer.
Supporting Material/References:
- Node.js v14.8.0
Wrap up
- I contacted the maintainer to let them know: Y
- I opened an issue in the related repository: N
Impact
In case if the argument of consume() is attacker controlled:
- Expose uninitialized memory, containing source code, passwords, network traffic, etc.
- Cause invalid data in slices (low control)
- Cause DoS by allocating a large buffer this way (with a large negative number before a slice/toString call is performed).
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the consume() method does not validate that its argument is a non-negative integer, and the copy() method does not correctly handle a buffer offset that exceeds the destination length.
Impact
If the consume() argument is attacker-controlled, uninitialized memory containing source code, passwords, or network traffic could be exposed; invalid data may appear in slices, and a large negative number before a slice or toString call could cause a denial of service by allocating a large buffer.
What the attacker needed
Attacker controls consume() argument
The fix
The copy() method should truncate the destination to the buffer offset, and consume() should reject negative or non-integer arguments.
How it was found
- Create a BufferList and append a single byte.
- Call consume() with a large negative number.
- Call slice() starting at index 1.
- Check whether the returned buffer contains the secret data.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A library for handling buffers (chunks of binary data) has a method that can be given a negative number. This confuses the library's internal bookkeeping, so when you later ask for a slice of the data, it hands back memory that was never properly initialised, which may contain leftover secrets from other parts of the program.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.