深入特权级转移(中)

处理器通过什么规则来判断资源请求或代码跳转是否合法?

数据段的访问规则(数据段无可执行属性)

  • 访问者权限(CPL) 高于或等于数据段特权级(DPL)
  • 请求特权级(RPL)高于或等于数据段特权级(DPL)
  • 即 : (CPL <= DPL) && (RPL <= DPL)
%include "inc.asm"

org 0x9000

jmp ENTRY_SEGMENT

[section .gdt]
; GDT definition
;                                 段基址,       段界限,       段属性
GDT_ENTRY       :     Descriptor    0,            0,           0
CODE32_DESC     :     Descriptor    0,    Code32SegLen - 1,    DA_C + DA_32 + DA_DPL0
VIDEO_DESC      :     Descriptor 0xB8000,     0x07FFF,         DA_DRWA + DA_32 + DA_DPL3
DATA32_DESC     :     Descriptor    0,    Data32SegLen - 1,    DA_DR + DA_32 + DA_DPL2
STACK32_DESC    :     Descriptor    0,     TopOfStack32,       DA_DRW + DA_32 + DA_DPL0
; GDT end

GdtLen    equ   $ - GDT_ENTRY

GdtPtr:
          dw   GdtLen - 1
          dd   0
          
          
; GDT Selector

Code32Selector     equ (0x0001 << 3) + SA_TIG + SA_RPL0
VideoSelector      equ (0x0002 << 3) + SA_TIG + SA_RPL3
Data32Selector     equ (0x0003 << 3) + SA_TIG + SA_RPL1
Stack32Selector    equ (0x0004 << 3) + SA_TIG + SA_RPL0

; end of [section .gdt]


; CPL = 2, RPL = 1, DPL = 3, 访问是否合法?   ===> yes
; CPL = 0, RPL = 3, DPL = 2, 访问是否合法?   ===> no
; CPL = 0, RPL = 1, DPL = 2, 访问是否合法?   ===> yes



TopOfStack16    equ 0x7c00

[section .s16]
[bits 16]
ENTRY_SEGMENT:
    mov ax, cs
    mov ds, ax
    mov es, ax
    mov ss, ax
    mov sp, TopOfStack16
    
    ; initialize GDT for 32 bits code segment
    mov esi, CODE32_SEGMENT
    mov edi, CODE32_DESC
    
    call InitDescItem
    
    mov esi, DATA32_SEGMENT
    mov edi, DATA32_DESC
    
    call InitDescItem
    
    mov esi, STACK32_SEGMENT
    mov edi, STACK32_DESC

    call InitDescItem
    
    ; initialize GDT pointer struct
    mov eax, 0
    mov ax, ds
    shl eax, 4
    add eax, GDT_ENTRY
    mov dword [GdtPtr + 2], eax

    ; 1. load GDT
    lgdt [GdtPtr]
    
    ; 2. close interrupt
    cli 
    
    ; 3. open A20
    in al, 0x92
    or al, 00000010b
    out 0x92, al
    
    ; 4. enter protect mode
    mov eax, cr0
    or eax, 0x01
    mov cr0, eax
    
    ; 5. jump to 32 bits code
    ; push Stack32Selector   ; 目标栈段选择子
    ; push TopOfStack32      ; 栈顶指针位置
    ; push Code32Selector    ; 目标代码段选择子
    ; push 0                 ; 目标代码段偏移
    ; retf
    jmp word Code32Selector : 0


; esi    --> code segment label
; edi    --> descriptor label
InitDescItem:
    push eax

    mov eax, 0
    mov ax, cs
    shl eax, 4
    add eax, esi
    mov word [edi + 2], ax
    shr eax, 16
    mov byte [edi + 4], al
    mov byte [edi + 7], ah
    
    pop eax
    
    ret

[section .dat]
[bits 32]
DATA32_SEGMENT:
    DTOS               db  "D.T.OS!", 0
    DTOS_OFFSET        equ DTOS - $$

Data32SegLen equ $ - DATA32_SEGMENT

   
[section .s32]
[bits 32]
CODE32_SEGMENT:
    mov ax, VideoSelector
    mov gs, ax
    
    mov ax, Data32Selector
    mov ds, ax
    
    mov ax, Stack32Selector
    mov ss, ax
    
    mov eax, TopOfStack32
    mov esp, eax
    
    mov ebp, DTOS_OFFSET
    mov bx, 0x0C
    mov dh, 12
    mov dl, 33
    
    call PrintString
    
    jmp $
    
; ds:ebp    --> string address
; bx        --> attribute
; dx        --> dh : row, dl : col
PrintString:
    push ebp
    push eax
    push edi
    push cx
    push dx
    
print:
    mov cl, [ds:ebp]
    cmp cl, 0
    je end
    mov eax, 80
    mul dh
    add al, dl
    shl eax, 1
    mov edi, eax
    mov ah, bl
    mov al, cl
    mov [gs:edi], ax
    inc ebp
    inc dl
    jmp print

end:
    pop dx
    pop cx
    pop edi
    pop eax
    pop ebp
    
    ret
    
Code32SegLen    equ    $ - CODE32_SEGMENT

[section .gs]
[bits 32]
STACK32_SEGMENT:
    times 1024 * 4 db 0
    
Stack32SegLen equ $ - STACK32_SEGMENT
TopOfStack32  equ Stack32SegLen - 1

我们做了34行-36行的实验。

实验结论

对于栈段,当为ss寄存器赋值时,使用规则(CPL == RPL) && (CPL == DPL) 保证特权级的匹配!

选择子被段寄存器加载时,会进行保护模式的检查

  • 检查选择子的下标是否合法(段描述符的合法性)
  • 检查特权级是否合法(CPL & RPL <= DPL)
  • 检查特权级时CPL和RPL之间不会进行比较(仅对普通数据段访问成立,不包括栈段的使用!)

代码段之间的跳转规则

非一致性代码段

  • 代码段之间只能平级转移(CPL == DPL && RPL <= DPL)

一致性代码段

  • 支持低特权级的代码向高特权级代码的转移(CPL >= DPL)
  • 虽然可以成功转移高特权级代码段,但是当前特权级不变

数据段只有一种,没有一致性和非一致性的区分;并且,数据段不允许被低特权级的代码段访问

%include "inc.asm"

org 0x9000

jmp ENTRY_SEGMENT

[section .gdt]
; GDT definition
;                                 段基址,       段界限,       段属性
GDT_ENTRY       :     Descriptor    0,            0,           0
CODE32_DESC     :     Descriptor    0,    Code32SegLen - 1,    DA_C + DA_32 + DA_DPL1
VIDEO_DESC      :     Descriptor 0xB8000,     0x07FFF,         DA_DRWA + DA_32 + DA_DPL2
DATA32_DESC     :     Descriptor    0,    Data32SegLen - 1,    DA_DR + DA_32 + DA_DPL2
STACK32_DESC    :     Descriptor    0,     TopOfStack32,       DA_DRW + DA_32 + DA_DPL1
FUNCTION_DESC   :     Descriptor    0,   FunctionSegLen - 1,   DA_C + DA_32 + DA_DPL1
NEW_DESC		:     Descriptor    0,       NewSegLen - 1,    DA_CCO + DA_32 + DA_DPL0

; GDT end

GdtLen    equ   $ - GDT_ENTRY

GdtPtr:
          dw   GdtLen - 1
          dd   0
          
          
; GDT Selector

Code32Selector     equ (0x0001 << 3) + SA_TIG + SA_RPL1
VideoSelector      equ (0x0002 << 3) + SA_TIG + SA_RPL2
Data32Selector     equ (0x0003 << 3) + SA_TIG + SA_RPL2
Stack32Selector    equ (0x0004 << 3) + SA_TIG + SA_RPL1
FunctionSelector   equ (0x0005 << 3) + SA_TIG + SA_RPL1
NewSelector		   equ (0x0006 << 3) + SA_TIG + SA_RPL0

; end of [section .gdt]



TopOfStack16    equ 0x7c00

[section .s16]
[bits 16]
ENTRY_SEGMENT:
    mov ax, cs
    mov ds, ax
    mov es, ax
    mov ss, ax
    mov sp, TopOfStack16
    
    ; initialize GDT for 32 bits code segment
    mov esi, CODE32_SEGMENT
    mov edi, CODE32_DESC
    
    call InitDescItem
    
    mov esi, DATA32_SEGMENT
    mov edi, DATA32_DESC
    
    call InitDescItem
    
    mov esi, STACK32_SEGMENT
    mov edi, STACK32_DESC
    
    call InitDescItem
    
    mov esi, FUNCTION_SEGMENT
    mov edi, FUNCTION_DESC
    
    call InitDescItem
    
    mov esi, NEW_SEGMENT
    mov edi, NEW_DESC
    
    call InitDescItem
    
    ; initialize GDT pointer struct
    mov eax, 0
    mov ax, ds
    shl eax, 4
    add eax, GDT_ENTRY
    mov dword [GdtPtr + 2], eax

    ; 1. load GDT
    lgdt [GdtPtr]
    
    ; 2. close interrupt
    cli 
    
    ; 3. open A20
    in al, 0x92
    or al, 00000010b
    out 0x92, al
    
    ; 4. enter protect mode
    mov eax, cr0
    or eax, 0x01
    mov cr0, eax
    
    ; 5. jump to 32 bits code
    push Stack32Selector   ; 目标栈段选择子
    push TopOfStack32      ; 栈顶指针位置
    push Code32Selector    ; 目标代码段选择子
    push 0                 ; 目标代码段偏移
    retf

; esi    --> code segment label
; edi    --> descriptor label
InitDescItem:
    push eax

    mov eax, 0
    mov ax, cs
    shl eax, 4
    add eax, esi
    mov word [edi + 2], ax
    shr eax, 16
    mov byte [edi + 4], al
    mov byte [edi + 7], ah
    
    pop eax
    
    ret

[section .dat]
[bits 32]
DATA32_SEGMENT:
    DTOS               db  "D.T.OS!", 0
    DTOS_OFFSET        equ DTOS - $$

Data32SegLen equ $ - DATA32_SEGMENT

   
[section .s32]
[bits 32]
CODE32_SEGMENT:
    mov ax, VideoSelector
    mov gs, ax
    
    mov ax, Data32Selector
    mov ds, ax
    
    mov ax, Stack32Selector
    mov ss, ax
    
    mov eax, TopOfStack32
    mov esp, eax
    
    ; mov ebp, DTOS_OFFSET
    ; mov bx, 0x0C
    ; mov dh, 12
    ; mov dl, 33
    
    ; call FunctionSelector : PrintString
    
    jmp NewSelector : 0
    
Code32SegLen    equ    $ - CODE32_SEGMENT

[section .new]
[bits 32]
NEW_SEGMENT:

	mov ebp, DTOS_OFFSET
    mov bx, 0x0C
    mov dh, 12
    mov dl, 33
    
    call FunctionSelector : PrintString
	jmp $
NewSegLen		equ		  $ - NEW_SEGMENT

[section .func]
[bits 32]
FUNCTION_SEGMENT:

; ds:ebp    --> string address
; bx        --> attribute
; dx        --> dh : row, dl : col
PrintStringFunc:
    push ebp
    push eax
    push edi
    push cx
    push dx
    
print:
    mov cl, [ds:ebp]
    cmp cl, 0
    je end
    mov eax, 80
    mul dh
    add al, dl
    shl eax, 1
    mov edi, eax
    mov ah, bl
    mov al, cl
    mov [gs:edi], ax
    inc ebp
    inc dl
    jmp print

end:
    pop dx
    pop cx
    pop edi
    pop eax
    pop ebp
    
    retf
    
PrintString    equ   PrintStringFunc - $$

FunctionSegLen    equ   $ - FUNCTION_SEGMENT

[section .gs]
[bits 32]
STACK32_SEGMENT:
    times 1024 * 4 db 0
    
Stack32SegLen equ $ - STACK32_SEGMENT
TopOfStack32  equ Stack32SegLen - 1

这个程序里我们定义了一个new段,并且设置它的属性为一致性代码段,DPL为0。第156行我们想从低特权级的非一致性代码段跳转到高特权级的一致性代码段,是合法的。但是,在一致性代码段中,我们的特权级还是之前非一致性代码段的特权级。一致性代码段和非一致性代码段的代码没有本质区别,这两种代码段仅仅是跳转时使用的合法判断规则不同。

 跳转到一致性代码段之前,cs寄存器后两位为01,代表特权级为1。

在进入一致性代码段之后,cs后两位为01,当前特权级依然为1,特权级没有发生变化 。

实验结论

特权级降低转移时,retf指令会触发栈段的特权级检查。

一致性代码段可直接跳转到其他同级非一致性代码段执行。

小技巧

大多数情况下,选择子中的RPL和对应段描述符中的DPL可设置为相同值。

小结

  • CPL,RPL,DPL是处理进行特权级保护的依据。
  • 对于数据段:CPL <= DPL && RPL <= DPL。
  • 对于非一致性代码段:CPL == DPL,RPL <= DPL。
  • 对于一致性代码段:CPL >= DPL,转移后CPL不变。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值