Skip to content

fix: Use Correct ptr Type to Avoid UAF - #433

Merged
whoisj merged 4 commits into
mainfrom
jwyman/fix-ptr-ownership
Mar 19, 2026
Merged

whoisj merged 4 commits into
mainfrom
jwyman/fix-ptr-ownership

Conversation

@whoisj

@whoisj whoisj commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

This change changes the managed pointer type used to manage the lifecyle of the stub instance from unique to shared. This avoids the dereference of the oft used unique pointer reference after the unique pointer has been free / deallocated.

CI Pipeline ID: 46362355

This change changes the managed pointer type used to manage the lifecyle of the stub instance from unique to shared.
This avoids the dereference of the oft used unique pointer reference after the unique pointer has been free / deallocated.
@whoisj
whoisj requested a review from yinggeh March 17, 2026 18:23
@whoisj whoisj changed the title fix: Use Correct ptr Type fix: Use Correct ptr Type to Avoid UAF Mar 17, 2026
Comment thread src/pb_stub.cc
Comment thread src/pb_stub.cc
Adding a destroy instance function to stub to force invalidate the shared ptr.
@whoisj
whoisj requested a review from yinggeh March 17, 2026 22:40
Comment thread src/pb_stub.cc
Comment thread src/pb_stub.h Outdated
@yinggeh

yinggeh commented Mar 18, 2026

Copy link
Copy Markdown
Contributor

Copyrights

@whoisj

whoisj commented Mar 18, 2026

Copy link
Copy Markdown
Contributor Author

Copyrights

Good catch, thanks. Fixed up.

@whoisj
whoisj requested a review from mudit-eng March 18, 2026 22:34
@whoisj
whoisj merged commit 47f9e03 into main Mar 19, 2026
3 checks passed
@whoisj
whoisj deleted the jwyman/fix-ptr-ownership branch March 19, 2026 15:42
@mrpre

mrpre commented Jul 21, 2026 •

Copy link
Copy Markdown

@whoisj
Hi, can you confirm whether this patch is intended to fix the following crash pattern?

In our old python_backend version, Stub::GetOrCreateInstance() returns std::unique_ptr&. The main thread runs stub->RunCommand(), while the background health thread captures
&stub and calls stub.reset() when the parent process is detected dead.

Our core shows:

Thread 1:
__new_sem_wait_slow
-> Stub::PopMessage()
-> Stub::RunCommand()

The faulting instruction is lock add QWORD PTR [rbx], rax, with rbx == si_addr, si_code == SEGV_MAPERR. The address is the semaphore inside the shared memory message queue.

Another thread:

main::{lambda()#1}
-> Stub::~Stub()
-> SharedMemoryManager::~SharedMemoryManager()

So it looks like the health thread destroys Stub/SharedMemoryManager and unmaps shm while the main thread is still blocked in sem_wait on mq_shm_ptr_->sem_full.

Does this commit’s change from unique_ptr& to shared_ptr by value cover this exact use-after-free / use-after-unmap scenario?

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants