当前位置:网站首页>What is the process of switching c read / write files from user mode to kernel mode?
What is the process of switching c read / write files from user mode to kernel mode?
2022-07-28 11:30:00 【biyusr】
One : background
1. A curious question
We are learning C# In the process of , I always hear a word called Kernel mode , For example, with C# Read and write files , It will involve code from User mode To Kernel mode Handoff , use HttpClient Get remote data , It will also involve User mode To Kernel mode Handoff , What kind of interaction process is this ? After all, our program is unmanageable Kernel mode , Today we will explore together .
Two : Explore the interaction process of two states
1. Where is the junction of the two states
We know the boundary between the world and the underworld Gate of hell , Same thing User mode and Kernel mode At the junction of ntdll.dll layer , Draw a picture like this :

The operating system is for protection Kernel mode Code for , It is definitely not possible to use a pointer directly in the user mode , After all, one is ring 3, In a ring 0, and cpu Hardware protection is also provided , How do you get in ? For the convenience of research , Let's start with a small example .
2. A simple file read
We use File.ReadAllLines() Realize file reading , The code is as follows :
internal class Program
{
public static object lockMe = new object();
static void Main(string[] args)
{
var txt= File.ReadAllLines(@"D:\1.txt");
Console.WriteLine(txt);
Console.ReadLine();
}
}
stay Windows On the platform , The external entry of all kernel functions is Win32 Api , Between the lines , This file also needs to be used for reading , Can be in WinDbg Use in bp ntdll!NtReadFile stay Gate of hell Intercept at .
0:000> bp ntdll!NtReadFile
breakpoint 0 redefined
0:000> g
ModLoad: 00007ffe`fdb20000 00007ffe`fdb50000 C:\Windows\System32\IMM32.DLL
ModLoad: 00007ffe`e2660000 00007ffe`e26bf000 C:\Program Files\dotnet\host\fxr\6.0.5\hostfxr.dll
Breakpoint 0 hit
ntdll!NtReadFile:
00007ffe`fe24c060 4c8bd1 mov r10,rcx
ha-ha , Successfully intercepted , Next use uf ntdll!NtReadFile Display the assembly code of the method body .
0:000> uf ntdll!NtReadFile
ntdll!NtReadFile:
00007ffe`fe24c060 mov r10,rcx
00007ffe`fe24c063 mov eax,6
00007ffe`fe24c068 test byte ptr [SharedUserData+0x308 (00000000`7ffe0308)],1
00007ffe`fe24c070 jne ntdll!NtReadFile+0x15 (00007ffe`fe24c075)
00007ffe`fe24c072 syscall
00007ffe`fe24c074 ret
00007ffe`fe24c075 int 2Eh
00007ffe`fe24c077 ret
From the perspective of assembly code , The logic is very simple , It's just one. if Judge , Decide whether to go syscall still int 2Eh, It is obvious that no matter which way you take, you can enter Kernel mode , Let's talk about it one by one .
3. int 2Eh Entry method
I believe no one in the debugging world does not know int do , After all, I have seen it countless times int 3, In essence , In the kernel layer, a Interrupt vector table , Each number is mapped to a piece of function code , When you turn on the computer and get windows Taking over also relies on Interrupt vector table , Okay , Next, let's take a brief look at how to find 3 Corresponding function code .
windbg There is one of them. !idt The command is used to find the function code corresponding to the number .
lkd> !idt 3
Dumping IDT: fffff804347e1000
03: fffff80438000f00 nt!KiBreakpointTrap
You can see , It corresponds to the kernel level nt!KiBreakpointTrap function , In the same way, let's look at 2E.
lkd> !idt 2E
Dumping IDT: fffff804347e1000
2e: fffff804380065c0 nt!KiSystemService
Now it's finally clear , The first way to enter the kernel state is KiSystemService, From the name , It is a similar general method , The next step is how to get into the kernel state Read the file In the way ?
To find the answer , You can look back at the assembly code mov eax,6 , there 6 Is the method number to which the kernel state needs to be routed , ha-ha , Which method does it correspond to ? because windows Closed source of , We don't know , Fortunately github Someone on the has made a list :https://j00ru.vexillium.org/syscalls/nt/64/ , Corresponding to my machine is .

As you can see from the picture, it is actually nt!NtReadFile , Here I think the truth should be revealed , Let's talk about syscall.
4. syscall How to walk
syscall yes CPU A special feature , be called The system quickly calls , Between the lines , It relies on a set of MSR register Help code quickly from User mode Cut to Kernel mode , Efficiency is far better than walking Interrupt routing table It's much faster , This is why the code has if Judge , In fact, it's judgment cpu Is this feature supported .
Just now I said that it has the help of MSR register , One of the registers MSR_LSTAR The address of the kernel state entry function is stored , We can use rdmsr c0000082 Take a look at .
lkd> rdmsr c0000082
msr[c0000082] = fffff804`38006cc0
lkd> uf fffff804`38006cc0
nt!KiSystemCall64:
fffff804`38006cc0 0f01f8 swapgs
fffff804`38006cc3 654889242510000000 mov qword ptr gs:[10h],rsp
fffff804`38006ccc 65488b2425a8010000 mov rsp,qword ptr gs:[1A8h]
...
You can see that in the code , It enters nt!KiSystemCall64 function , And then execute the following 6 Corresponding nt!NtReadFile Complete the business logic , Finally, it is nt!KiSystemCall64 complete Kernel mode To User mode Handoff .
Knowing these two ways , Next, you can patch up the picture a little , increase syscall and int xxx Two ways to enter the customs .

3、 ... and : summary
Through assembly code analysis , We finally know User mode To Kernel mode Switching principle of , There are two ways , One is int 2e, One is syscall , Deepen our understanding of C# Read the file A deeper understanding of .
边栏推荐
- mysql的左连接和右连接(内连接和自然连接的区别)
- What is WordPress
- Generation and use of Lib library files in keil and IAR
- R语言-用于非平衡数据集的一些度量指标
- 「Node学习笔记」Koa框架学习
- Full version of H5 social chat platform source code [complete database + complete document tutorial]
- 目标检测领域必看的6篇论文
- Flutter教程之带有 GoRouter 的 Flutter Navigator 2.0,使用 go_router 包了解 Flutter 中的声明式路由机制(教程含源码)
- Google Earth Engine——使用geetool批量下载单景影像以Landsat 8 反演后的NDSI结果
- No swagger, what do I use?
猜你喜欢

Why should coding and modulation be carried out before transmission

Technology sharing | quick intercom integrated dispatching system
![Full version of H5 social chat platform source code [complete database + complete document tutorial]](/img/3f/03239c1b4d6906766348d545a4f234.png)
Full version of H5 social chat platform source code [complete database + complete document tutorial]

技术分享| 快对讲综合调度系统

Office2013 input mathematical formula above

Jupiter、spyder、Anaconda Prompt 、navigator 快捷键消失的解决办法

使用 Terraform 在 AWS 上快速部署 MQTT 集群

GIS数据漫谈(五)— 地理坐标系统
![Leetcode:1300. the sum of the array closest to the target value after transforming the array [dichotomy]](/img/da/51c4051464d52eae10d93e881cfdf5.png)
Leetcode:1300. the sum of the array closest to the target value after transforming the array [dichotomy]

本地化、低时延、绿色低碳:阿里云正式启用福州数据中心
随机推荐
Software designers ask 20 questions before the exam, pay attention!!
Function of interface test
几个数据库的相关概念
[极客大挑战 2019]BabySQL-1|SQL注入
完整版H5社交聊天平台源码[完整数据库+完整文档教程]
Build a quick development ide: visualsvn + sublime + Visual Studio 2013 + quickeasyftpserver
抖音程序员表白专用代码教程(如何玩抖音)
字节一面:如何用 UDP 实现可靠传输?
苹果手机iCloud钥匙串的加密缺陷
outlook突然变得很慢很卡怎么解决
1331. 数组序号转换
对话庄表伟:开源第一课
Flutter教程之带有 GoRouter 的 Flutter Navigator 2.0,使用 go_router 包了解 Flutter 中的声明式路由机制(教程含源码)
Cvpr2021 pedestrian re identification /person re identification paper + summary of open source code
[JS advanced] JS functions, overloads, anonymous functions, scopes and scope chains_ 03
Ec20/ec25 4G module at instruction development summary
五面阿里技术专家岗,已拿offer,这些面试题你能答出多少
迭代法求行列式(线性代数公式)
什么是WordPress
[FPGA tutorial case 41] image case 1 - reading pictures through Verilog