Skip to content

Out of bounds memory access #81

Closed

Description

@devongovett

cjs-module-lexer has a stack depth of 2048:

#define STACK_DEPTH 2048

this creates two stacks:

uint16_t templateStack_[STACK_DEPTH];
uint16_t* openTokenPosStack_[STACK_DEPTH];

However, when pushing to these stacks, there is no bounds check performed. For example:

openTokenPosStack[openTokenDepth++] = lastTokenPos;

If you run with a maliciously crafted input, you can overflow the stack and cause memory corruption.

import { parse, init } from 'cjs-module-lexer';

await init();
console.log(parse('exports.foo = 2;\n' + '{'.repeat(3069) + '}'.repeat(3069)));

Running this causes a Bus error: 10 crash.

This can also cause a crash of node itself by writing the above contents to a file and importing it via ESM.

Activity

  1. guybedford commented on Feb 19, 2023

    @guybedford
    Collaborator

    Thanks for digging into this. The expectation for protection during development was always that it would only crash the Wasm process and therefore we'd wrap that in a JS error and simply catch it. Wasm errors causing process errors is not something I expected though, in which case we need to introduce these protections carefully.

    We can PR the stack checks in the right places and backport across all active release lines as it's a patch. Would be nice to ensure we catch everything at once if possible, but we can also do it gradually. I'll try and put a Sunday afternoon into this soon when I can. PRs very welcome too.

  2. added
    bug
    on Feb 19, 2023
  3. added a commit that references this issue on Sep 7, 2026
  4. added a commit that references this issue on Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions