Codebot.Cross icon indicating copy to clipboard operation
Codebot.Cross copied to clipboard

TUniqueInstance - Race condition can cause a use-after-free bug

Open WayneSherman opened this issue 3 years ago • 0 comments

Reading through the code, I found what looks like a race condition which can cause a "use-after-free".

When TUniqueInstance is destroyed, FThread is asked to terminate and FSocket is freed: https://github.com/sysrpl/Codebot.Cross/blob/master/source/codebot/codebot.unique.pas#L77-L79

But the FThread can still be running and attempt to access FSocket after it is freed. https://github.com/sysrpl/Codebot.Cross/blob/master/source/codebot/codebot.unique.pas#L99

(Imagine the thread checks Terminated and a context switch happens right after that. The main thread then calls TUniqueInstance.Destroy and FSocket is freed. When the thread continues to execute it calls FSocket.Accept(Client), but FSocket has already been freed.)

WayneSherman avatar Jul 02 '22 15:07 WayneSherman