如何一次取消保护跨越多个页面的内存区域?

How to unprotect memory region spanning several pages on one go?

我正在尝试解除对跨越多个页面的一大块内存区域的保护。 我正在使用这段代码取消保护内存的一页但是我需要一次取消保护几个页面因为我在访问其他页面时遇到段错误我手头有一个起始地址和一个结束地址但是下面的函数使用我的起始地址 rdi 并为我提供对当前页面的写访问权限,我如何使用结束地址 r15 以便我可以拥有跨越 rdi -> r15 内存页的写访问权限:

例如:一次性写入从rdi = 0x4012a0 到r15 = 0x402340 地址的页面

call getpagesize
; rax has 0x1000
mov rcx, rax
; save rax for later use when passing to mprotect
sub rcx, 0x1
not rcx
mov rdi, %1
and rdi, rcx
; AND them and the result will be stored in rcx
; rdi must hold the page_start address
mov rsi, rax
; rsi must have the page length
mov rdx, 0x7
; read+write+exec = 0x7
call mprotect

这不太正确:

; rsi must have the page length

此处,rsimprotect() 调用的第二个参数,它应该是您要更改的区域的长度。如果您希望区域的长度大于一页,那么您需要 rsi 大于您从 call getpagesize.

获得的值

具体来说;也许你想要更像:

    call getpagesize
    ; rax has 0x1000
    mov rcx, rax
    ; save rax for later use when passing to mprotect
    sub rcx, 0x1
    not rcx
    mov rdi, %1
    and rdi, rcx
    ; AND them and the result will be stored in rcx
    ; rdi must hold the page_start address

    mov rsi, r15      ;rsi = end
    sub rsi,rdi       ;rsi = end - aligned_start = length

    mov rdx, 0x7
    ; read+write+exec = 0x7
    call mprotect