Home > Software engineering >  How does linux implement PROT_NONE mode of mprotect on x64 platform?
How does linux implement PROT_NONE mode of mprotect on x64 platform?

Time:05-26

The method mprotect have a PROT_NONE option to disable memory access. It means "The memory cannot be accessed at all"

I wonder how it is implemented on x86/x64 platform.

According to the attributes R/W and XD of page table entry, a page can be set to read only and execution-disable. But how to implement PROT_NONE mode so that the memory cannot be accessed at all?

In another words, how to produce a exception when memory is read.

  1. Directly to invalid the virtual page to trigger page fault?
  2. Using CPL and DPL to trigger privilege violation?
  3. Delete the map between virtual memory and physical memory to trigger page fault ?

Is limited by my ability, I can't find the implementation mechanism through source code. It seems like only modify the protection flags of page table entry.

CodePudding user response:

Note that mprotect is called from user space on virtual address regions. When protection is set to PROT_NONE, the _PAGE_PRESENT bit is cleared and the _PAGE_PROTNONE bit is set. Attempting to access the address will cause a page fault. Source

  • Related