0:000> u ntdll!KiFastSystemCallntdll!KiFastSystemCall:7c92eb8b 8bd4 mov edx,esp7c92eb8d 0f34 sysenterntdll!KiFastSystemCallRet:7c92eb8f 90 nop7c92eb90 90 nop7c92eb91 90 nop7c92eb92 90 nop7c92eb93 90 nopntdll!KiFastSystemCallRet:7c92eb94 c3 ret
The result obtained through dump disasm
0x77d1a146 : FF 15 24 14 D1 77 call dword ptr [0x77d11424]【ntdll.dll.[.text].NtCallbackReturn】0x7c92d51d : B8 14 00 00 00 mov eax, 0x140x7c92d522 : BA 00 03 FE 7F mov edx, 0x7ffe03000x7c92d527 : FF 12 call dword ptr [edx]
【ntdll.dll.[.text].KiFastSystemCall】 0x7c92eb8b : 8B D4 mov edx, esp 0x7c92eb8d : 0F 34 sysenter 0x7c92eb94 : C3 ret
0x77ef67d4 : C3 ret
According to the above results, kifastsystemcall should have been returned to the next command 0x7c92d527 at the end of execution, that is, 0x7c92d529, but why should it be returned to 0x77ef67d4?
View the call stack
Call [0x0012f43c]: 0x7c92d527 --> 0x7c92eb8b
Return [0x0012f458]: 0x77ef67d4 <-- 0x7c92eb94
Thread 38:
[# 0] [0x0012fb20]:[0x7c941739 - 0x7c94173e] ==> [0x7c93c9e4] ntdll.dll.[.text].LdrFindResourceDirectory_U_0x000000af [# 1] [0x0012fa18]:[0x7c93cba6 - 0x7c93cbab] ==> [0x7c921193] ntdll.dll.[.text].LdrInitializeThunk_0x00000015 [# 2] [0x0012f9f8]:[0x7c9211a4 - 0x7c9211a7] ==> [0x77d1f518] USER32.dll.[.text].UserClientDllInitialize [# 3] [0x0012f46c]:[0x77d1f76f - 0x77d1f774] ==> [0x77d1f791] USER32.dll.[.text].UserClientDllInitialize_0x00000279 [# 4] [0x0012f460]:[0x77ef655e - 0x77ef6563] ==> [0x77ef67c8] GDI32.dll.[.text].GdiProcessSetup_0x000001ac [# 5] [0x0012f45c]:[0x77ef67d2 - 0x77ef67d4] ==> [0x7c92eb8b] ntdll.dll.[.text].KiFastSystemCall [# 6] [0x0012f450]:[0x7c92eae0 - 0x7c92eae3] ==> [0x77d1a12e] USER32.dll.[.text].ClientThreadSetup_0x00000134 [# 7] [0x0012f440]:[0x77d1a146 - 0x77d1a14c] ==> [0x7c92d51d] ntdll.dll.[.text].NtCallbackReturn--> [# 8] [0x0012f43c]:[0x7c92d527 - 0x7c92d529] ==> [0x7c92eb8b] ntdll.dll.[.text].KiFastSystemCall
It can be seen that 0x77ef67d4 is the return address stored on the stack when kifastsystemcall is called for the first time. At that time, the stack position was 0x0012f45c (when the return address was not pressed to the stack );
The stack is at 0x0012f458, so it is returned to the location where kifastsystemcall was called for the first time.
If so, what is the problem with our shadow call stack?
We know that when the user State program enters the kernel state for execution (via sysenter), the kernel may schedule the execution of callback functions such as APC in the user State, the execution initiators of these callback functions are kernel-state code, so they need to return the execution process to the kernel code;
Windows provides a mechanism. When user-mode callback functions do not provide explicit Code returned to the kernel, Windows automatically executes the code returned to the kernel by default, this code is also completed through the kifastsystemcall mechanism (because kifastsystemcall is the only entry to the kernel state from the user State). This mechanism is called ntcallbackreturn.
Therefore, we suspect that the ntcallbackreturn mechanism isolates user-state code and does not meet the stack balance because ntcallbackreturn's kernel service program will adjust the user-state stack, to conceal traces of asynchronous callback function execution.
Therefore, we believe that the existence of ntcallbackreturn makes the shadow call stack established simply based on the call/RET command of the user state unbalanced.
The solution is to add three equivalent RET effects after calling ntcallbackreturn, which can solve the imbalance of the Shadow stack.
It's not over yet, because we don't know which system calls will disrupt the balance of user State stacks.
For example, ntcontinue is very suspicious (Based on the implementation results). How can the user-state shadow call stack ensure its correctness?
The more common practice is to judge based on kifastsystemcallret. It can be paired with kifastsystemcall to eliminate unnecessary items in the middle.