Skip to content

Commit

Permalink
Check that rsp is within stack limits when redirecting a thread. (#48433
Browse files Browse the repository at this point in the history
)

* Check that rsp is within stack limits when redirecting a thread.

* move the check after/if SetThreadContext has failed

* just return  FALSE after failed SetThreadContext

Co-authored-by: Jan Kotas <jkotas@microsoft.com>

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
  • Loading branch information
VSadov and jkotas committed Feb 18, 2021
1 parent e7afcda commit bc9dc0e
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/coreclr/vm/threadsuspend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2952,7 +2952,26 @@ BOOL Thread::RedirectThreadAtHandledJITCase(PFN_REDIRECTTARGET pTgt)
this, this->GetThreadId(), dwOrigEip, pTgt);

bRes = EESetThreadContext(this, pCtx);
_ASSERTE(bRes && "Failed to SetThreadContext in RedirectThreadAtHandledJITCase - aborting redirect.");
if (!bRes)
{
#ifdef _DEBUG
// In some rare cases the stack pointer may be outside the stack limits.
// SetThreadContext would fail assuming that we are trying to bypass CFG.
//
// NB: the check here is slightly more strict than what OS requires,
// but it is simple and uses only documented parts of TEB
auto pTeb = this->GetTEB();
void* stackPointer = (void*)GetSP(pCtx);
if ((stackPointer < pTeb->StackLimit) || (stackPointer > pTeb->StackBase))
{
return (FALSE);
}

_ASSERTE(!"Failed to SetThreadContext in RedirectThreadAtHandledJITCase - aborting redirect.");
#endif

return FALSE;
}

// Restore original IP
SetIP(pCtx, dwOrigEip);
Expand Down

0 comments on commit bc9dc0e

Please sign in to comment.