fix: manual stack manipulation in sleaping()#7
Open
ccoskrnl wants to merge 1 commit into
Open
Conversation
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.
PAGE BOUNDARY CHECK (Manual Stack Extension Fix)
Problem Description
In the x64 calling convention, the first four arguments are passed via registers (
RCX,RDX,R8,R9), while arguments 5 and 6 must be placed on the stack atRSP+40(0x28) andRSP+48(0x30).A critical issue arises when the
RSP(Stack Pointer) is located near the upper boundary of a committed memory page. For example, ifRSPis0x3C2E8FFFF8, calculatingRSP + 40results in0x3C2E900000. This new address crosses into the subsequent virtual memory page.Under normal execution, the CPU hardware and Windows kernel manage stack growth automatically using Guard Pages. When a standard
pushinstruction or a function's prologue hits a Guard Page, the OS intercepts the fault and commits more memory. However, when we perform manual stack manipulation (via direct pointer dereferencing), we bypass this mechanism. If the target address (RSP + 40) resides in a page that is still marked asMEM_RESERVEor is aPAGE_GUARDthat hasn't been officially tripped by the OS, the CPU cannot perform the write operation. This results in an immediateEXCEPTION_ACCESS_VIOLATION(C0000005).Solution
To ensure stability during manual thread context manipulation, we implement a Page Boundary Check. Before attempting to write arguments to the stack, the code calculates whether the target addresses (
RSP + 40orRSP + 48) reside on a different memory page than the currentRSP.If a page crossing is detected, we proactively call the
AllocateStackfunction. This function ensures the target memory region is transitioned from aRESERVEDorGUARDstate to a fullyCOMMITTEDstate withPAGE_READWRITEpermissions. By manually "backing" the virtual address with physical memory before the write occurs, we prevent the access violation and ensure the hijacked thread can resume execution safely.