Skip to content

Test cleanup hook uaf regression 65196 - #65546

Closed
everett1992 wants to merge 2 commits into
nodejs:mainfrom
everett1992:test-cleanup-hook-uaf-regression-65196
Closed

Test cleanup hook uaf regression 65196#65546
everett1992 wants to merge 2 commits into
nodejs:mainfrom
everett1992:test-cleanup-hook-uaf-regression-65196

Conversation

@everett1992

Copy link
Copy Markdown
Contributor

This PR is a continuation of @sreehariannam's work in

#65196

I've added a regression test and fixed the lint issue. I'm only opening this because there's no activity on that PR, and this bug is blocking the backport of #65042 which fixes a crash with better-sqlite3 on node v29.19.0

sreehariannam and others added 2 commits August 10, 2026 10:58
CleanupHookThunkRun() read thunk->isolate/fun/arg from the
CleanupHookThunk after invoking thunk->fun(). For every
node::ObjectWrap alive at teardown, thunk->fun is
ObjectWrap::CleanupHook, which deletes the wrap; ~ObjectWrap() calls
RemoveEnvironmentCleanupHook() itself, erasing the CleanupHookThunk
from the registry and freeing the node it lives in. The subsequent
read of thunk->isolate/fun/arg to make the (now redundant) second
RemoveEnvironmentCleanupHook() call was therefore a use-after-free.

Cache the fields before running the hook so nothing is read from
`thunk` once it may have been freed.

Fixes: nodejs#65195
Add a cctest that registers an environment cleanup hook which removes
itself while the cleanup queue is drained -- the ordinary teardown path
for every node::ObjectWrap still alive at exit since nodejs#63642. It
exercises CleanupHookThunkRun(), which must not read the
CleanupHookThunk after invoking the hook, because the hook has already
erased and freed it.

The use-after-free is silent in ordinary builds and is caught by the
ASan/Valgrind CI, which is how the original assertion (nodejs#63923)
surfaced.

Refs: nodejs#65195
Co-authored-by: Sreehari Annam <sreehari.annam@gmail.com>
Co-authored-by: nsavoire <19255994+nsavoire@users.noreply.github.com>
Signed-off-by: Caleb Everett <everett.caleb@gmail.com>
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Aug 25, 2026
@everett1992
everett1992 marked this pull request as draft August 25, 2026 16:25
@codecov

codecov Bot commented Aug 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.14%. Comparing base (404b0cf) to head (5cf34f7).
⚠️ Report is 267 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #65546      +/-   ##
==========================================
- Coverage   90.32%   90.14%   -0.19%     
==========================================
  Files         760      751       -9     
  Lines      248525   253638    +5113     
  Branches    46894    47791     +897     
==========================================
+ Hits       224488   228636    +4148     
- Misses      15469    16254     +785     
- Partials     8568     8748     +180     
Files with missing lines Coverage Δ
src/api/hooks.cc 87.57% <100.00%> (-1.67%) ⬇️

... and 212 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants