Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Out of bounds memory access #81

Open
devongovett opened this issue Feb 18, 2023 · 1 comment
Open

Out of bounds memory access #81

devongovett opened this issue Feb 18, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@devongovett
Copy link

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.

@guybedford
Copy link
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.

@guybedford guybedford added the bug Something isn't working label Feb 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants