[windows內核]段權限檢查

之前我們有提到過RPL和DPL的概念,RPL在段選擇子裏,DPL在段描述符裏
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
可以看到在Intle32手冊裏是這樣解釋他們的,但是關於段權限還有一個新的概念要提,也就CPL(當前特權級)
在這裏插入圖片描述
簡單理解就是特指CS段選擇子的第1和第二位描述了當前運行的進程的特權級,舉一個簡單的例子
使用OD打開一個程序,查看CS的段選擇子
在這裏插入圖片描述
拆開就是‭00011011‬ 後兩位就是11=3 也就是特權級別爲3
我們再使用內核調試器,中斷下來
在這裏插入圖片描述
可以看到我們的cs是8 拆開也就是‭1000‬ 後兩位爲00=0 也就是特權級別爲0
爲什麼會這樣呢,這就引出了cpu另外一個機制,CPU分級
在這裏插入圖片描述
在windows下只使用了R3和R0,也就是我們常說的三環和零環,三環對應的應用層,零環對應的系統層,也就是內核層CLP表明了當前執行代碼的權限,這個應該很好理解,我們剛纔使用應用層的調試器去調試應用層的程序,所以CPL爲3,我們中斷在內核調試內核程序,所以我們的CPL爲0

下面我們再來用例子解釋清楚這三者的關聯性
我們先看DPL在Intle32手冊中的解釋

DPL (descriptor privilege level) field
Specifies the privilege level of the segment. The privilege level can range from 0 to 3, with 0 being
the most privileged level. The DPL is used to control access to the segment. See Section 5.5, “Privilege Levels”, for a description of the relationship of the DPL to the CPL of the executing code
segment and the RPL of a segment selector

通俗的理解就是如果你想訪問我這個段的內容你應該具備什麼權限
舉一個簡單的例子

mov ds,ex;

如果ax指向的段DPL=0,而當前程序的CPL=3 那麼這句指令就不會執行成功

我們接下來看RPL的解釋

Requested privilege level (RPL) — The RPL is an override privilege level that is assigned to segment
selectors. It is stored in bits 0 and 1 of the segment selector. The processor checks the RPL along with the CPL
to determine if access to a segment is allowed. Even if the program or task requesting access to a segment has
sufficient privilege to access the segment, access is denied if the RPL is not of sufficient privilege level. That is,
if the RPL of a segment selector is numerically greater than the CPL, the RPL overrides the CPL, and vice versa.
The RPL can be used to insure that privileged code does not access a segment on behalf of an application
program unless the program itself has access privileges for that segment. See Section 5.10.4, “Checking Caller
Access Privileges (ARPL Instruction),” for a detailed description of the purpose and typical use of the RPL.
Privilege levels are checked when the segment selector of a segment descriptor is loaded into a segment register.
The checks used for data access differ from those used for transfers of program control among code segments;
therefore, the two kinds of accesses are considered separately in the following sections.

在這裏插入圖片描述
RPL是針對段選擇子而言的,每個段的選擇子都有自己的RPL,理論上來說RPL我們是可以自己更改的,但是能不能成功還需要配合其他兩個
例如:

Mov ax,0008;   Mov ax,000B;      //段選擇子
Mov ds,ax;     Mov ds,ax;        //寫入段描述

指向的是同一個段描述符,但RPL是不一樣的,這個可以自己拆一下,第二步的成功與否,都與DPL和CPL有關係.

下面來說一下數據段的權限檢查(注意這裏說的是數據段,如果是代碼段或者系統段的檢查方式是不同的)
具體的解釋可以看手冊的這一節
在這裏插入圖片描述
看着有點生澀難懂,舉一個簡單的例子就明白了
例如:
比如當前程序處於0環,也就是說CPL=0

Mov ax,000B  //1011 RPL = 3
Mov ds,ax    //ax指向的段描述符的DPL = 0

數據段的權限檢查:
CPL <= DPL 並且 RPL <= DPL (數值上的比較,數值越大權限越低)

那麼我們來分析一下這句執行成功沒有
第一個比較 CPL <= DPL, 0<=0 成功
第二個比較 RPL <= DPL ,3<=0 失敗
所以這句Mov ds,ax 執行的結果爲失敗

簡單的總結一下
CPL CPU當前的權限級別
DPL 如果你想訪問我,你應該具備什麼樣的權限
RPL 用什麼權限去訪問一個段

那麼最後可能有個疑問既然有CPL爲啥要有RPL?這樣不是概念重疊或者衝突了嗎?在英特爾的白皮書裏有多處解釋CPL和PRL對應不同的作用
例如:
在這裏插入圖片描述
但最精髓的應該還是這段
在這裏插入圖片描述
爲了細分權限和防止錯誤
例如:
我們本可以用“讀寫”的權限去打開一個文件,但爲了避免出錯,有些時候我們使用“只讀”的權限去打開。

下面貼一張由:shayi1983end大佬解釋翻譯的圖來強化一下對段描述符的印象
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章