Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix use-after-free bug in registry_watcher#554

Open
Royar13 wants to merge 2 commits intomicrosoft:master from
Royar13:user/royar13/reg-watcher-uaf
Open

Fix use-after-free bug in registry_watcher #554
Royar13 wants to merge 2 commits intomicrosoft:master from
Royar13:user/royar13/reg-watcher-uaf

Conversation

@Royar13
Copy link

@Royar13 Royar13 commented Oct 28, 2025

Fix the following scenario:

  1. (Thread A) registry_watcher_t::callback() starts (before reaching TryAddRef), m_refCount = 1.
  2. (Thread B) registry_watcher_state object is deleted (Release() method).
  3. (Thread B) m_refCount is destroyed.
  4. (Thread A) TryAddRef() is reached and accesses destroyed member m_refCount -> Use-after-free, UNDEFINED BEHAVIOR, can corrupt other members (which happened in production code using WIL).

NN--- reacted with eyes emoji
Copy link
Collaborator

dmachaj commented Oct 29, 2025

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Member

looks good. do you have a synthetic test that shows the problem and validates the fix?

// Sleep(1); // Enable for testing to find use after free bugs.
}
else if (rearm)
else if (rearm && refCount > 0)
Copy link
Member

Choose a reason for hiding this comment

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

This suggests you are guarding against negative values. If you've seen this actually happen, we'd be very interested to learn more since that would suggest there's a mis-match of AddRef and Release.

Copy link
Member

dunhor commented Oct 30, 2025

Something seems fishy here. unique_threadpool_wait calls WaitForThreadpoolWaitCallbacks in its destructor, so scenario doesn't seem possible, assuming everything is working as designed. In particular, registry_watcher_state cannot be fully deleted during an invocation of the callback.

You do mention:

TryAddRef() is reached and accesses destroyed member m_refCount

which may technically have some merit to it since m_refCount is declared after m_threadPoolWait in the definition of registry_watcher_state. That said, longs are trivially destructible, so this is only a "technically wrong" thing and unlikely to be observable in practice, outside of something like a Debug build or ASan/UBSan etc.

I'll second Chris' question - I'd like to know more about the scenario where you've seen issues.

ChrisGuzak reacted with thumbs up emoji

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

Reviewers

@dunhor dunhor dunhor left review comments

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Comments

AltStyle によって変換されたページ (->オリジナル) /