用户态程序调用系统态程序-快速系统调用
在調(diào)試程序中,經(jīng)常發(fā)現(xiàn)程序最后會(huì)調(diào)用到系統(tǒng)態(tài)的程序。這個(gè)過(guò)程是怎樣的?用戶空間的程序怎樣進(jìn)行系統(tǒng)調(diào)用,在此過(guò)程中是怎樣進(jìn)入和退出內(nèi)核的。
根據(jù)運(yùn)行狀態(tài)和執(zhí)行代碼所在的內(nèi)存空間的不同,CPU既可以運(yùn)行于非特權(quán)的“用戶態(tài)”,也可以運(yùn)行于特權(quán)的“系統(tǒng)態(tài)”。CPU從系統(tǒng)空間裝入用戶空間是容易的,CPU可以通過(guò)一些特權(quán)指令改變其運(yùn)行狀態(tài),反過(guò)來(lái)要從用戶空間轉(zhuǎn)到系統(tǒng)空間就不容易了,因?yàn)檫\(yùn)行于用戶態(tài)的CPU不能執(zhí)行特權(quán)指令。
一般而言有三種方式可以使運(yùn)行于用戶態(tài)的CPU轉(zhuǎn)入到系統(tǒng)態(tài),下面只說(shuō)一下Windows是如何利用IA-32處理器的SYSENTER、SYSEXIT指令實(shí)現(xiàn)快速系統(tǒng)調(diào)用的。
Windows 2000或之前的Windows不支持快速系統(tǒng)調(diào)用,它們只能使用INT 2E的方式進(jìn)行系統(tǒng)調(diào)研。在Windows XP和Windows Server 2003或以后版本在啟動(dòng)時(shí)會(huì)通過(guò)CPUID指令檢測(cè)是否支持快速系統(tǒng)調(diào)用。如果支持,那么Windows會(huì)使用新的方式進(jìn)行系統(tǒng)調(diào)用。
使用快速系統(tǒng)調(diào)用需要如下工作:
以下以Notepad為例,看看Notepad在調(diào)用NtWriteFile時(shí)的CPU從用戶態(tài)到系統(tǒng)態(tài)的轉(zhuǎn)換過(guò)程。
使用Windbg掛著VWare用運(yùn)行的Windows XP系統(tǒng),在Notepad的主線程上設(shè)置ntdll!NtWriteFile的斷點(diǎn)。
?
kd> !process 0 3 notepad.exe
PROCESS 81bd4658 SessionId: 0 Cid: 07a8 Peb: 7ffdf000 ParentCid: 0590
DirBase: 1a8a3000 ObjectTable: e1ae2678 HandleCount: 38.
Image: notepad.exe
VadRoot 81e1ef30 Vads 62 Clone 0 Private 169. Modified 13. Locked 0.
DeviceMap e17b2e98
Token e1b8ece8
ElapsedTime 00:01:50.327
UserTime 00:00:00.062
KernelTime 00:00:01.968
QuotaPoolUsage[PagedPool] 32228
QuotaPoolUsage[NonPagedPool] 2480
Working Set Sizes (now,min,max) (1267, 50, 345) (5068KB, 200KB, 1380KB)
PeakWorkingSetSize 1267
VirtualSize 33 Mb
PeakVirtualSize 34 Mb
PageFaultCount 1330
MemoryPriority BACKGROUND
BasePriority 8
CommitCharge 479
THREAD 81e068e8 Cid 07a8.0340 Teb: 7ffde000 Win32Thread: e176ad58 RUNNING on processor 0
kd> bp /t 81e068e8 ntdll!ntwritefile
?
如上設(shè)置斷點(diǎn)后,在Windows XP中使用Notepad保存文件就會(huì)斷在ntdll!ZwWriteFile。
kd> kb
ChildEBP RetAddr Args to Child
0006fa04 77e673b1 0000057c 00000000 00000000 ntdll!ZwWriteFile
0006fa64 010048e8 0000057c 00092c40 000000b8 kernel32!WriteFile+0xf7
0006fa9c 01004cb4 0000057c 000004e4 00000400 notepad!___PchSym_+0x6e8
0006fad8 01002a1f 000701c0 01009900 00099570 notepad!___PchSym_+0xab4
0006fafc 00000000 00000000 00000000 00000000 notepad!ServiceStarter+0x6b
反匯編ntdll!ZwWriteFile可以看到調(diào)用了SystemCallStub
ntdll!ZwWriteFile:
001b:77f3f010 b812010000 mov eax,112h
001b:77f3f015 ba0003fe7f mov edx,offset SharedUserData!SystemCallStub (7ffe0300)
001b:77f3f01a ffd2 call edx
001b:77f3f01c c22400 ret 24h
001b:77f3f01f 90 nop?
反匯編SharedUserData!SystemCallStub查看它的代碼,它代碼作用是:把棧指針(ESP)放到EDX中,可以傳遞參數(shù),也作為從內(nèi)核模式返回時(shí)的棧地址。再執(zhí)行SYSENTER指令。
SharedUserData!SystemCallStub:
7ffe0300 8bd4 mov edx,esp
7ffe0302 0f34 sysenter
7ffe0304 c3 ret?
當(dāng)執(zhí)行SYSENTER指令時(shí),CPU進(jìn)入系統(tǒng)態(tài),并且:
- 把寄存器SYSENTER_CS_MSR的內(nèi)容復(fù)制到段寄存器CS中。
- 把寄存器SYSENTER_EIP_MSR的內(nèi)容復(fù)制到寄存器EIP中。
- 把寄存器SYSENTER_CS_MSR的內(nèi)容+8寫(xiě)入堆棧段寄存器SS中。
- 把寄存器SYSENTER_ESP_MSR的內(nèi)容復(fù)制到堆棧指針ESP中。
這樣,只要預(yù)先設(shè)置好三個(gè)MSR寄存器的內(nèi)容中,CPU在執(zhí)行SYSENTER后就可以進(jìn)入系統(tǒng)空間并從預(yù)定的地址執(zhí)行程序,同時(shí)開(kāi)始使用系統(tǒng)空間的堆棧。通過(guò)WinDbg中rdmsr命令可以查看到MSR寄存器的內(nèi)容,可以看到SYSENTER_EIP_MSR指向nt!KiFastCallEntry,這就是系統(tǒng)空間中快速系統(tǒng)調(diào)用的總?cè)肟凇t!KiFastCallEntry中再進(jìn)入nt!KiSystemService。
kd> rdmsr 174
msr[174] = 00000000`00000008
kd> rdmsr 175
msr[175] = 00000000`00000000
kd> rdmsr 176
msr[176] = 00000000`80599770
kd> u 80599770
nt!KiFastCallEntry:?
從nt!KeServiceDescriptorTable中可以看到第112h是對(duì)應(yīng)著nt!NtWriteFile。
kd> dds poi(nt!KeServiceDescriptorTable) + 112*4
8050f0f4 805e81cc nt!NtWriteFile
8050f0f8 805e8a94 nt!NtWriteFileGather
8050f0fc 80635c50 nt!NtWriteRequestData
8050f100 8065ff93 nt!NtWriteVirtualMemory?
在nt!NtWriteFile設(shè)置斷點(diǎn)就可以斷下,在Windbg中使用如下命令就可以看到結(jié)果。
kd> bp /t 81e068e8 nt!NtWriteFile
kd> k
ChildEBP RetAddr
f59347ac 8059994c nt!NtWriteFile
f59347ac 7ffe0304 nt!KiSystemService+0x13b
0006fa00 77f3f01c SharedUserData!SystemCallStub+0x4
0006fa04 77e673b1 ntdll!ZwWriteFile+0xc
0006fa64 010048e8 kernel32!WriteFile+0xf7
0006fa9c 01004cb4 notepad!___PchSym_+0x6e8
0006fad8 01002a1f notepad!___PchSym_+0xab4
0006fafc 00000000 notepad!ServiceStarter+0x6b?
上面列出了程序從用戶空間轉(zhuǎn)到系統(tǒng)空間的過(guò)程,那么怎么從系統(tǒng)空間中返回呢?堆棧指針保存在哪里?
在nt!KiFastCallEntry中在進(jìn)入nt!KiSystemServic前會(huì)保存0x7ffe0304到ECX,并壓入堆棧。0x7ffe0304就是用戶態(tài)的返回地址,對(duì)應(yīng)著SharedUserData!SystemCallStub函數(shù)中的Ret指令。
在調(diào)用返回時(shí),會(huì)把堆棧POP到ECX中,再調(diào)用SYSEXIT。當(dāng)CPU執(zhí)行SYSEXIT指令時(shí),CPU回到用戶態(tài),并且:
- 把CS設(shè)置成(SYSENTER_CS_MSR的內(nèi)容+16),這實(shí)際上是KGDT_R3_CODE。
- 把寄存器EDX的內(nèi)容復(fù)制到EIP。
- 把SS設(shè)置成(SYSENTER_CS_MSR的內(nèi)容+24),這實(shí)際上是KGDT_R3_DATA.
- 把寄存器ECX的內(nèi)容復(fù)制到ESP。
kd> u nt!KiSystemCallExit2
nt!KiSystemCallExit2:
80599ae0 5a pop edx
80599ae1 83c408 add esp,8
80599ae4 59 pop ecx
80599ae5 fb sti
80599ae6 0f35 sysexit
轉(zhuǎn)載于:https://www.cnblogs.com/Quincy/archive/2010/03/27/1698604.html
總結(jié)
以上是生活随笔為你收集整理的用户态程序调用系统态程序-快速系统调用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: VBS递归遍历文件夹
- 下一篇: EDM HTML编写建议和规范