Skip to content

fix: prevent use-after-free in PowerMonitor via dangling OS callbacks - #50045

Merged
ckerr merged 1 commit into
mainfrom
pm-uaf
Mar 5, 2026
Merged

ckerr merged 1 commit into
mainfrom
pm-uaf

Conversation

@codebytere

@codebytere codebytere commented Mar 3, 2026 •

Copy link
Copy Markdown
Member

Description of Change

PowerMonitor registered OS-level callbacks (HWND UserData and WTS/suspend notifications on Windows, shutdown handler and lock-screen observer on macOS) but never cleaned them up in its destructor. The JS layer also only held the native object in a closure-local variable, allowing GC to reclaim it while those registrations still referenced freed memory.

To fix this we retain the native PowerMonitor at module level in power-monitor.ts so it cannot be garbage-collected, and add DestroyPlatformSpecificMonitors() to properly tear down OS registrations on destruction. On Windows, unregister WTS and suspend notifications, clear GWLP_USERDATA, and destroy the HWND; on macOS, remove the emitter from the global MacLockMonitor and reset the Browser shutdown handler.

Checklist

Release Notes

Notes: none

@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Mar 3, 2026
PowerMonitor registered OS-level callbacks (HWND UserData and
WTS/suspend notifications on Windows, shutdown handler and lock-screen
observer on macOS) but never cleaned them up in its destructor. The JS
layer also only held the native object in a closure-local variable,
allowing GC to reclaim it while those registrations still referenced
freed memory.

Retain the native PowerMonitor at module level in power-monitor.ts so
it cannot be garbage-collected. Add DestroyPlatformSpecificMonitors()
to properly tear down OS registrations on destruction: on Windows,
unregister WTS and suspend notifications, clear GWLP_USERDATA, and
destroy the HWND; on macOS, remove the emitter from the global
MacLockMonitor and reset the Browser shutdown handler.
@codebytere codebytere added semver/patch backwards-compatible bug fixes target/39-x-y PR should also be added to the "39-x-y" branch. target/40-x-y PR should also be added to the "40-x-y" branch. target/41-x-y PR should also be added to the "41-x-y" branch. labels Mar 3, 2026
@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Mar 4, 2026
@codebytere
codebytere marked this pull request as ready for review March 4, 2026 09:27
@codebytere codebytere added target/37-x-y PR should also be added to the "37-x-y" branch. target/38-x-y PR should also be added to the "38-x-y" branch. labels Mar 4, 2026
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Mar 4, 2026
@codebytere codebytere removed the target/37-x-y PR should also be added to the "37-x-y" branch. label Mar 4, 2026
@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Mar 5, 2026
@ckerr
ckerr merged commit 7c0cb61 into main Mar 5, 2026
180 of 184 checks passed
@ckerr
ckerr deleted the pm-uaf branch March 5, 2026 15:00
@release-clerk

release-clerk Bot commented Mar 5, 2026

Copy link
Copy Markdown

No Release Notes

@trop

trop Bot commented Mar 5, 2026

Copy link
Copy Markdown
Contributor

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

@trop trop Bot added the in-flight/41-x-y label Mar 5, 2026
@trop

trop Bot commented Mar 5, 2026

Copy link
Copy Markdown
Contributor

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

@trop trop Bot removed the target/41-x-y PR should also be added to the "41-x-y" branch. label Mar 5, 2026
@trop

trop Bot commented Mar 5, 2026

Copy link
Copy Markdown
Contributor

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

@trop trop Bot added the in-flight/40-x-y label Mar 5, 2026
@trop

trop Bot commented Mar 5, 2026

Copy link
Copy Markdown
Contributor

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

@trop trop Bot added in-flight/39-x-y in-flight/38-x-y merged/41-x-y PR was merged to the "41-x-y" branch. merged/38-x-y PR was merged to the "38-x-y" branch. merged/39-x-y PR was merged to the "39-x-y" branch. merged/40-x-y PR was merged to the "40-x-y" branch. and removed target/40-x-y PR should also be added to the "40-x-y" branch. target/39-x-y PR should also be added to the "39-x-y" branch. target/38-x-y PR should also be added to the "38-x-y" branch. in-flight/41-x-y in-flight/38-x-y in-flight/39-x-y in-flight/40-x-y labels Mar 5, 2026
// while this module is alive. The C++ side registers OS-level callbacks (HWND
// user-data on Windows, shutdown handler on macOS, notification observers) that
// prevent safe collection of the C++ wrapper while those registrations exist.
let pm: any;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something is wrong here, the self keep alive is meant to prevent it. Can you share a test case that confirms this ?

MarshallOfSound added a commit that referenced this pull request Sep 25, 2026
…it ignores

#47162 removed the WM_POWERBROADCAST handling from powerMonitor's message
window (suspend/resume come from base::PowerMonitor), but left the
RegisterSuspendResumeNotification() call behind. That registration no
longer fed anything and its unregistration is the ARM64 shutdown crash
that #50045, #50878 and #51198 have been chasing.

Drop the private message window entirely and take session lock/unlock
from ui::SessionChangeObserver, which registers WTS notifications on the
UI thread's singleton window and already filters to the current session.
MarshallOfSound added a commit that referenced this pull request Sep 25, 2026
…it ignores (#54352)

* fix: stop registering powerMonitor for Windows suspend notifications it ignores

#47162 removed the WM_POWERBROADCAST handling from powerMonitor's message
window (suspend/resume come from base::PowerMonitor), but left the
RegisterSuspendResumeNotification() call behind. That registration no
longer fed anything and its unregistration is the ARM64 shutdown crash
that #50045, #50878 and #51198 have been chasing.

Drop the private message window entirely and take session lock/unlock
from ui::SessionChangeObserver, which registers WTS notifications on the
UI thread's singleton window and already filters to the current session.

* fix: complete ui::SessionChangeObserver where PowerMonitor is destroyed and emit directly

~PowerMonitor() is defined in electron_api_power_monitor.cc, which only saw
the forward declaration, so the unique_ptr member did not compile on
Windows. The session change callback already runs on the UI thread, so emit
there instead of posting an Unretained task that nothing can cancel.
MarshallOfSound added a commit that referenced this pull request Sep 25, 2026
…it ignores (#54384)

* fix: stop registering powerMonitor for Windows suspend notifications it ignores

#47162 removed the WM_POWERBROADCAST handling from powerMonitor's message
window (suspend/resume come from base::PowerMonitor), but left the
RegisterSuspendResumeNotification() call behind. That registration no
longer fed anything and its unregistration is the ARM64 shutdown crash
that #50045, #50878 and #51198 have been chasing.

Drop the private message window entirely and take session lock/unlock
from ui::SessionChangeObserver, which registers WTS notifications on the
UI thread's singleton window and already filters to the current session.

Co-authored-by: Sam Attard <[email protected]>

* fix: complete ui::SessionChangeObserver where PowerMonitor is destroyed and emit directly

~PowerMonitor() is defined in electron_api_power_monitor.cc, which only saw
the forward declaration, so the unique_ptr member did not compile on
Windows. The session change callback already runs on the UI thread, so emit
there instead of posting an Unretained task that nothing can cancel.

Co-authored-by: Sam Attard <[email protected]>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Sam Attard <[email protected]>
MarshallOfSound added a commit that referenced this pull request Sep 25, 2026
…it ignores (44-x-y) (#54408)

fix: stop registering powerMonitor for Windows suspend notifications it ignores (#54352)

* fix: stop registering powerMonitor for Windows suspend notifications it ignores

window (suspend/resume come from base::PowerMonitor), but left the
RegisterSuspendResumeNotification() call behind. That registration no
longer fed anything and its unregistration is the ARM64 shutdown crash
that #50045, #50878 and #51198 have been chasing.

Drop the private message window entirely and take session lock/unlock
from ui::SessionChangeObserver, which registers WTS notifications on the
UI thread's singleton window and already filters to the current session.

* fix: complete ui::SessionChangeObserver where PowerMonitor is destroyed and emit directly

~PowerMonitor() is defined in electron_api_power_monitor.cc, which only saw
the forward declaration, so the unique_ptr member did not compile on
Windows. The session change callback already runs on the UI thread, so emit
there instead of posting an Unretained task that nothing can cancel.

(cherry picked from commit 98704f6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged/38-x-y PR was merged to the "38-x-y" branch. merged/39-x-y PR was merged to the "39-x-y" branch. merged/40-x-y PR was merged to the "40-x-y" branch. merged/41-x-y PR was merged to the "41-x-y" branch. semver/patch backwards-compatible bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants