src: fix use-after-free in CleanupHookThunkRun - #65196
Open
sreehariannam wants to merge 1 commit into
Open
Conversation
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
Author
|
cc @addaleax (you wrote both #63642 and #63985, whose interaction causes this) and @legendecas (reviewed #63642) — would appreciate a look when you have a chance. |
Author
|
cc @nsavoire — thanks for the thorough root-cause writeup and repro in the issue, it made this a straightforward fix to apply. If you get a chance to re-run your valgrind repro (test/addons/worker-addon-exit) against this branch, that would help confirm it before a maintainer looks at it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
CleanupHookThunkRun()readthunk->isolate/thunk->fun/thunk->argfrom the
CleanupHookThunkafter invokingthunk->fun(). For everynode::ObjectWrapstill alive at teardown,thunk->funisObjectWrap::CleanupHook, which deletes the wrap. Since #63642,~ObjectWrap()callsRemoveEnvironmentCleanupHook()itself, which erasesthe
CleanupHookThunkfromcleanup_hook_registryand frees the node itlives in. The subsequent read to make the (now redundant) second
RemoveEnvironmentCleanupHook()call was therefore a use-after-free — thisis now the ordinary teardown path for every
ObjectWrap-based addon, not anedge case.
The fix caches
isolate/fun/argbefore invoking the hook, so nothing isread from
thunkonce it may have already been freed.Root cause and fix as diagnosed in the issue.
Fixes: #65195
Test plan
test/addons/worker-addon-exit(built withnode-gyp, run undervalgrind) is the existing repro described in the issue.