Closed Bug 2042966 Opened 6 months ago Closed 6 months ago

Crash [@ InnerModuleEvaluation] - stack overflow on deep module graph

Categories

(Core :: JavaScript Engine, defect)

x86_64
Linux
defect

Tracking

()

RESOLVED DUPLICATE of bug 2042305

People

(Reporter: decoder, Unassigned)

References

Details

(4 keywords)

Attachments

(1 file)

Summary

InnerModuleEvaluation (js/src/vm/Modules.cpp:2146), the recursive helper that drives module-graph evaluation from ModuleEvaluate (js/src/vm/Modules.cpp:2028), performs no recursion-limit check before recursing into a module's dependencies. Its two sibling graph-walk helpers in the same file — InnerModuleLoading (js/src/vm/Modules.cpp:1587) and InnerModuleLinking (js/src/vm/Modules.cpp:1909) — both begin with an AutoCheckRecursionLimit / recursion.check(cx) guard. The guard was omitted in the evaluation phase.

Evaluating a sufficiently deep module dependency graph therefore drives unbounded native C++ recursion and exhausts the stack, producing an uncontrolled AddressSanitizer: stack-overflow (SIGSEGV) instead of the catchable InternalError: too much recursion that the loading and linking phases raise for the same kind of input.

Root cause

The recursive descent over the requested modules happens at js/src/vm/Modules.cpp:2206-2228:

// Step 11. For each ModuleRequest Record required of module.[[RequestedModules]], do:
Rooted<ModuleRequestObject*> required(cx);
Rooted<ModuleObject*> requiredModule(cx);
for (const RequestedModule& request : module->requestedModules()) {   // 2208
  required = request.moduleRequest();
  if (required->phase() != ImportPhase::Evaluation) {
    continue;
  }
  requiredModule = GetImportedModule(cx, module, required);
  ...
  // Step 11.b.i Set index to ? InnerModuleEvaluation(requiredModule, stack, index).
  if (!InnerModuleEvaluation(cx, requiredModule, stack, index, &index)) {  // 2226
    return false;
  }
  ...
}

There is no recursion.check(cx) at the top of InnerModuleEvaluation, unlike InnerModuleLinking (js/src/vm/Modules.cpp:1909):

AutoCheckRecursionLimit recursion(cx);
if (!recursion.check(cx)) {
  return false;
}

and InnerModuleLoading (js/src/vm/Modules.cpp:1587), which has the same guard.

Reachability / why loading & linking don't already prevent it

The loading and linking phases are guarded, so importing a single deep graph fails cleanly at link time. The PoC sidesteps that by linking the chain incrementally, bottom-up:

  • The chain is m0 <- m1 <- ... <- m(D-1) where mi imports m(i-1).
  • moduleLink(mi) is called in increasing i order. By the time mi is linked, m(i-1) is already Linked, so InnerModuleLoading / InnerModuleLinking for mi short-circuit after one level (InnerModuleLinking step 2 returns early for already-linked modules). The guarded phases therefore never recurse deeply and all succeed; the whole chain reaches Linked status.
  • A single moduleEvaluate(m(D-1)) then walks the full depth D through the unguarded InnerModuleEvaluation and overflows the stack.

Note that this incremental, per-module linking relies on the JS shell test functions registerModule / parseModule / moduleLink / moduleEvaluate, which are not exposed to web content; a browser embedding links the whole graph from the root, which would trip the guarded InnerModuleLinking recursion limit and fail before evaluation. The defect itself (a missing recursion guard relative to the sibling phases) is nonetheless real and is rated sec-audit pending a determination of any non-shell reachable path. Compare bug 2013604 (shell-loader-reachable missing-guard stack overflow, sec-other) and bug 2027274 (AsyncModuleExecutionRejected stack overflow, sec-moderate).

Reproduction

Testcase: poc.js (in the finding directory).

const D = 100000;
let mods = new Array(D);
mods[0] = registerModule('m0', parseModule('0;'));
for (let i = 1; i < D; i++) {
  mods[i] = registerModule('m' + i, parseModule("import 'm" + (i - 1) + "';"));
}
for (let i = 0; i < D; i++) {
  moduleLink(mods[i]);          // shallow, guarded phases succeed
}
moduleEvaluate(mods[D - 1]);    // unguarded deep recursion -> stack overflow

Run: js --fuzzing-safe poc.js (no special prefs required).

Crash signature

Debug ASan build:

==687779==ERROR: AddressSanitizer: stack-overflow on address 0x7fff44df0e78 ...
    #1 ... in InnerModuleEvaluation(...) js/src/vm/Modules.cpp:2208:32
    #2 ... in InnerModuleEvaluation(...) js/src/vm/Modules.cpp:2226:10
    #3 ... in InnerModuleEvaluation(...) js/src/vm/Modules.cpp:2226:10
    ...   (hundreds of identical InnerModuleEvaluation frames)
SUMMARY: AddressSanitizer: stack-overflow ... Modules.cpp:2208:32 in InnerModuleEvaluation(...)

Opt ASan build prints all linked; root=Linked first (proving loading/linking completed), then overflows; the top frame happens to be a GC write-barrier helper (MapAllocToTraceKind) inlined into the deep InnerModuleEvaluation recursion, with the unbounded self-call still at Modules.cpp:2226.

Both builds abort with exit status 1.

Suggested fix

Add the same guard the sibling helpers use at the top of InnerModuleEvaluation (js/src/vm/Modules.cpp:2146):

static bool InnerModuleEvaluation(JSContext* cx, Handle<ModuleObject*> module,
                                  MutableHandle<ModuleVector> stack,
                                  size_t index, size_t* indexOut) {
  AutoCheckRecursionLimit recursion(cx);
  if (!recursion.check(cx)) {
    return false;
  }
  ...

This converts the crash into a catchable InternalError: too much recursion, matching the loading and linking phases.

Status: NEW → RESOLVED
Closed: 6 months ago
Duplicate of bug: 2042305
Resolution: --- → DUPLICATE
Group: javascript-core-security
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: