Skip to content

fix: use-after-free of a freed Realm by cppgc wrapper destructors at exit - #53560

Merged
MarshallOfSound merged 1 commit into
mainfrom
fix/node-cppgc-wrapper-realm-uaf
Sep 24, 2026
Merged

MarshallOfSound merged 1 commit into
mainfrom
fix/node-cppgc-wrapper-realm-uaf

Conversation

@MarshallOfSound

@MarshallOfSound MarshallOfSound commented Sep 5, 2026 •

Copy link
Copy Markdown
Member

Description of Change

Cherry-picks nodejs/node#65778 (landed upstream as nodejs/node@c909c63, not yet in a v24.x release) as a Node patch. The plan is to drop the patch once it rolls in with a Node release.

  • Realm::RunCleanup() detaches the cppgc-managed wrappers it tracks (vm.Script, vm contexts) through cppgc::WeakPersistents. The GC clears those in the atomic pause that finds a wrapper dead, but with lazy/concurrent sweeping the wrapper's destructor can run much later. A wrapper collected shortly before FreeEnvironment() and swept after it was skipped by cleanup, kept its Realm*, and ~CppgcMixin() then wrote into the freed Realm.
  • In Electron the main process frees its node::Environment in PostMainMessageLoopRun and then deletes the isolate, whose teardown finishes any pending cppgc sweep, so this surfaced as an ASan heap-use-after-free in ~ContextifyScript at exit on the linux-x64-asan shard (run, and still recurring, e.g. 45-x-y) after all tests had passed.
  • The fix moves the Realm pointer into the off-heap CppgcWrapperListNode, which the wrapper owns; cleanup clears it for every entry, live or already collected, so a late destructor sees no Realm rather than a freed one. The purge flag, its GC epilogue callback and PurgeEmpty() go away, which also stops the list nodes of wrappers still alive at FreeEnvironment() from leaking. Upstream's cctests are included in the patch.
  • The only conflict against our tree was context in cppgc_helpers-inl.h from upstream's embedder-data-tag change, which our Node does not have.

Verified locally on an ASan build with instrumentation: before, every run left hundreds of collected-but-unswept vm.Script wrappers holding a stale Realm* past RunCleanup(); after, none do.

Checklist

  • PR description included
  • npm test passes

Release Notes

Notes: Fixed a possible crash during process exit when vm.Script or vm contexts created in the main process had been garbage collected shortly before quitting.

@MarshallOfSound MarshallOfSound added semver/patch backwards-compatible bug fixes target/43-x-y PR should also be added to the "43-x-y" branch. target/44-x-y PR should also be added to the "44-x-y" branch. target/45-x-y PR should also be added to the "45-x-y" branch. labels Sep 5, 2026
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Sep 5, 2026
@MarshallOfSound MarshallOfSound self-assigned this Sep 5, 2026
@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Sep 6, 2026
@deepak1556

Copy link
Copy Markdown
Member

nodejs/node#65778 exposes a valid memory safety issue but that also voids the behavior of clean when the wrapper is unreachable and realm shutdown happens, today there are no custom implementation of clean. What if we have something like

GC wrapper
    |
    | owns
    v
CppgcWrapperListNode <── Realm wrapper list
    |
    +── nullable Realm pointer
    +── WeakPersistent<CppgcMixin>
    +── unique_ptr<RealmCleanupState>
                         |
                         +── native realm specific resource cleanup

instead of a clean override on the mixin, it provides a cleanup object. This guarantees a cleanup hook when realm shutdown or wrapper destruction whichever happens first, the cleanup object however cannot or should not access an on heap objects.

class RealmCleanupState {
 public:
  virtual ~RealmCleanupState() = default;

  void RunOnce(Realm& realm) noexcept {
    if (status_ == Completed)
      return;

    CHECK(status_ == Pending);
    status_ = Running;

    Clean(realm);

    status_ = Completed;
  }

 protected:
  // Contract:
  // - No wrapper pointer, GC objects, or V8 handles.
  // - No JavaScript, GC triggering operations, or nested event loops.
  // - Must not destroy this state, its node, or the Realm reentrantly.
  // - Required Realm state must still be available.
  virtual void Clean(Realm& realm) = 0;

 private:
  enum Status { Pending, Running, Completed };
  Status status_ = Pending;
};

…exit

Cherry-picks nodejs/node#65778 (landed upstream as nodejs/node@c909c63be3).

Realm::RunCleanup() detaches the cppgc wrappers it tracks (vm.Script,
vm contexts) through weak persistents, which the GC clears as soon as it
finds a wrapper unreachable. With lazy/concurrent sweeping the wrapper's
destructor can run much later, so one collected shortly before
FreeEnvironment() and swept after it kept its Realm pointer, and
~CppgcMixin() wrote into the freed Realm. In Electron this showed up as
an ASan heap-use-after-free in ~ContextifyScript during
JavascriptEnvironment teardown at process exit.

The Realm pointer now lives in the off-heap list node, which cleanup
clears for every entry, so a late destructor sees no Realm instead of a
freed one.
@MarshallOfSound
MarshallOfSound force-pushed the fix/node-cppgc-wrapper-realm-uaf branch from 88b23fd to 1f99699 Compare September 23, 2026 22:59
@MarshallOfSound
MarshallOfSound marked this pull request as ready for review September 23, 2026 23:01
@MarshallOfSound
MarshallOfSound requested a review from a team as a code owner September 23, 2026 23:01
@MarshallOfSound
MarshallOfSound enabled auto-merge (squash) September 23, 2026 23:01
@MarshallOfSound
MarshallOfSound merged commit c07f124 into main Sep 24, 2026
83 checks passed
@MarshallOfSound
MarshallOfSound deleted the fix/node-cppgc-wrapper-realm-uaf branch September 24, 2026 00:30
@release-clerk

release-clerk Bot commented Sep 24, 2026

Copy link
Copy Markdown

Release Notes Persisted

Fixed a possible crash during process exit when vm.Script or vm contexts created in the main process had been garbage collected shortly before quitting.

@trop

trop Bot commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "44-x-y", please check out #54266

@trop

trop Bot commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "45-x-y", please check out #54267

@trop trop Bot removed the target/44-x-y PR should also be added to the "44-x-y" branch. label Sep 24, 2026
@trop

trop Bot commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "43-x-y", please check out #54268

@trop trop Bot added in-flight/45-x-y in-flight/43-x-y merged/44-x-y PR was merged to the "44-x-y" branch. merged/45-x-y PR was merged to the "45-x-y" branch. merged/43-x-y PR was merged to the "43-x-y" branch. and removed target/45-x-y PR should also be added to the "45-x-y" branch. target/43-x-y PR should also be added to the "43-x-y" branch. in-flight/44-x-y in-flight/45-x-y in-flight/43-x-y labels Sep 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

46-x-y merged/43-x-y PR was merged to the "43-x-y" branch. merged/44-x-y PR was merged to the "44-x-y" branch. merged/45-x-y PR was merged to the "45-x-y" branch. semver/patch backwards-compatible bug fixes

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants