Home

Awesome

PTEditor

GitHub Actions

A small library to modify all page-table levels of all processes from user space for x86_64 (Linux and Windows 10) and ARMv8 (Linux). It also allows to read and program memory types (i.e., PATs on x86 and MAIRs on ARM).

Installation

The library relies on the pteditor kernel module (Linux) or kernel driver (Windows). The kernel part is provided as source code for compilation (Linux and Windows), PPA (Linux), and as pre-built binary (Windows). The library can be used by linking it to the application (see example.c) or as a single header (ptedit_header.h) which can be directly included (see the demos).

Install from PPA (Linux, recommended)

First, add the public key of the PPA and the PPA URL to the package manager, and update the package manager

curl -s "https://misc0110.github.io/ppa/KEY.gpg" | sudo tee /etc/apt/trusted.gpg.d/pteditor.asc
sudo curl -s -o /etc/apt/sources.list.d/misc0110.list "https://misc0110.github.io/ppa/file.list"
sudo apt update

Then, simply install the kernel module

sudo apt install pteditor-dkms

Pre-Built Driver (Windows, recommended)

The repository also contains a pre-built driver for Windows 10 in the driver folder. To load the driver, you have to first disable secure boot and driver signature enforcement.

Temporarily Disable Driver Signature Enforcement

Hold the shift key while clicking on "Restart" in the start menu. This brings up a restart menu, where you can disable driver signature enforcement in "Troubleshoot > Advanced Options > Startup Settings". Press "Restart", and the in the startup settings press "7" or "F7" to disable driver signature enforcement. After the PC is started, the driver can be loaded. Keep in mind that the driver signature enforcement is enabled when the PC is rebooted.

Permanently Disable Driver Signature Enforcement

To permanently disable driver signature enforcement, enable Windows test mode by entering

bcdedit /set testsigning on

in an administrator command prompt. To disable test mode, run

bcdedit /set testsigning off

Loading the Driver

To load and active the driver, the repository contains a loader in driver/PTEditorLoader. Simply run

PTEditorLoader.exe

as an administrator. To unload the driver, run

PTEditorLoader.exe --unload

Alternatively, you can also use any other driver-loading tool, e.g., OSRLoader or NoVirusThanks Kernel-Mode Driver Loader.

Install Kernel Part From Source

Linux

Building the kernel module requires the kernel headers of the kernel. On Ubuntu, they can be installed by running

sudo apt install linux-headers-$(uname -r)

Both the library and the the kernel module can be build by running

make

The resulting kernel module can be loaded using

sudo insmod module/pteditor.ko

Windows

The kernel driver for Windows requires Visual Studio with Visual C++, the Windows SDK, and the Windows Driver Kit (WDK) to build. Using the Visual Studio project, the driver can then simply be built from Visual Studio.

Requirements

The library requires a recent Linux kernel (continuously tested on the current kernel for 20.04 (kernel 5.8), and 22.04 (kernel 5.15 and 6.2)) or Windows 10. It supports both x86_64 and ARMv8.

The library does not rely on any other library. It uses only standard C functionality. On Linux, the library does not require root privileges, whereas on Windows it requires administrator privileges.

Test

To test whether the kernel part and the library works, the repository contains unit tests. The tests are found in the folder test and can be compiled with make (Linux) or Visual Studio (Windows).

Example

The basic functionality (ptedit_init and ptedit_cleanup) is always required. After the initialization, all functions provided by the library can be used.

For examples see example.c or the examples in the demo folder. The demo folder contains multiple examples:

API

Basic FunctionalityDescriptions
int ptedit_init()Initializes (and acquires) PTEditor kernel module
void ptedit_cleanup()Releases PTEditor kernel module
void ptedit_use_implementation(int implementation)Select the PTEditor implementation to use
Page tablesDescriptions
ptedit_entry_t ptedit_resolve(void * address,pid_t pid)Resolves the page-table entries of all levels for a virtual address of a given process.
void ptedit_update(void * address,pid_t pid,ptedit_entry_t * vm)Updates one or more page-table entries for a virtual address of a given process. The TLB for the given address is flushed after updating the entries.
void ptedit_pte_set_bit(void * address,pid_t pid,int bit)Sets a bit directly in the PTE of an address.
void ptedit_pte_clear_bit(void * address,pid_t pid,int bit)Clears a bit directly in the PTE of an address.
unsigned char ptedit_pte_get_bit(void * address,pid_t pid,int bit)Returns the value of a bit directly from the PTE of an address.
size_t ptedit_pte_get_pfn(void * address,pid_t pid)Reads the PFN directly from the PTE of an address.
void ptedit_pte_set_pfn(void * address,pid_t pid,size_t pfn)Sets the PFN directly in the PTE of an address.
TYPE ptedit_cast(size_t entry, TYPE)Casts a paging structure entry (e.g., page table) to a structure with easy access to its fields
System InfoDescriptions
int ptedit_get_pagesize()Returns the default page size of the system
Page frame numbers (PFN)Descriptions
size_t ptedit_set_pfn(size_t entry,size_t pfn)Returns a new page-table entry where the page-frame number (PFN) is replaced by the specified one.
size_t ptedit_get_pfn(size_t entry)Returns the page-frame number (PFN) of a page-table entry.
Physical pagesDescriptions
void ptedit_read_physical_page(size_t pfn,char * buffer)Retrieves the content of a physical page.
void ptedit_write_physical_page(size_t pfn,char * content)Replaces the content of a physical page.
void * ptedit_pmap(size_t physical,size_t length)Map a physical address range to the virtual address space.
PagingDescriptions
size_t ptedit_get_paging_root(pid_t pid)Returns the root of the paging structure (i.e., CR3 on x86 and TTBR0 on ARM).
void ptedit_set_paging_root(pid_t pid,size_t root)Sets the root of the paging structure (i.e., CR3 on x86 and TTBR0 on ARM).
TLB/BarriersDescriptions
void ptedit_invalidate_tlb(void * address)Invalidates the TLB entry of current process for a given address on all CPUs.
void ptedit_invalidate_tlb_pid(pid_t pid, void * address)Invalidates the TLB for a given PID and address on all CPUs.
void ptedit_full_serializing_barrier()A full serializing barrier which stops everything.
Memory types (PATs/MAIRs)Descriptions
size_t ptedit_get_mts()Reads the value of all memory types (x86 PATs / ARM MAIRs). This is equivalent to reading the MSR 0x277 (x86) / MAIR_EL1 (ARM).
void ptedit_set_mts(size_t mts)Programs the value of all memory types (x86 PATs / ARM MAIRs). This is equivalent to writing to the MSR 0x277 (x86) / MAIR_EL1 (ARM) on all CPUs.
char ptedit_get_mt(unsigned char mt)Reads the value of a specific memory type attribute (PAT/MAIR).
void ptedit_set_mt(unsigned char mt,unsigned char value)Programs the value of a specific memory type attribute (PAT/MAIR).
unsigned char ptedit_find_mt(unsigned char type)Generates a bitmask of all memory type attributes (PAT/MAIR) which are programmed to the given value.
int ptedit_find_first_mt(unsigned char type)Returns the first memory type attribute (PAT/MAIR) which is programmed to the given memory type.
size_t ptedit_apply_mt(size_t entry,unsigned char mt)Returns a new page-table entry which uses the given memory type (PAT/MAIR).
unsigned char ptedit_extract_mt(size_t entry)Returns the memory type (i.e., PAT/MAIR ID) which is used by a page-table entry.
size_t ptedit_apply_mt_huge(size_t entry,unsigned char mt)Returns a new entry for a huge page which uses the given memory type (PAT/MAIR).
unsigned char ptedit_extract_mt_huge(size_t entry)Returns the memory type (i.e., PAT/MAIR ID) which is used by a huge-page entry.
const char * ptedit_mt_to_string(unsigned char mt)Returns a human-readable representation of a memory type (PAT/MAIR value).
Pretty printDescriptions
void ptedit_print_entry(size_t entry)Pretty prints a page-table entry.
void ptedit_print_entry_line(size_t entry,int line)Prints a single line of the pretty-print representation of a page-table entry.

Basic Functionality

int ptedit_init()

Initializes (and acquires) PTEditor kernel module

Returns 0 Initialization was successful

Returns -1 Initialization failed

void ptedit_cleanup()

Releases PTEditor kernel module

void ptedit_use_implementation(int implementation)

Select the PTEditor implementation to use

Parameters

Page tables

ptedit_entry_t ptedit_resolve(void * address,pid_t pid)

Resolves the page-table entries of all levels for a virtual address of a given process.

Parameters

Returns A structure containing the page-table entries of all levels.

void ptedit_update(void * address,pid_t pid,ptedit_entry_t * vm)

Updates one or more page-table entries for a virtual address of a given process. The TLB for the given address is flushed after updating the entries.

Parameters

void ptedit_pte_set_bit(void * address,pid_t pid,int bit)

Sets a bit directly in the PTE of an address.

Parameters

void ptedit_pte_clear_bit(void * address,pid_t pid,int bit)

Clears a bit directly in the PTE of an address.

Parameters

unsigned char ptedit_pte_get_bit(void * address,pid_t pid,int bit)

Returns the value of a bit directly from the PTE of an address.

Parameters

Returns The value of the bit (0 or 1)

size_t ptedit_pte_get_pfn(void * address,pid_t pid)

Reads the PFN directly from the PTE of an address.

Parameters

Returns The page-frame number (PFN)

void ptedit_pte_set_pfn(void * address,pid_t pid,size_t pfn)

Sets the PFN directly in the PTE of an address.

Parameters

TYPE ptedit_cast(size_t entry, TYPE)

Casts a paging structure entry (e.g., page table) to a structure with easy access to its fields.

Parameters

Returns A struct of type type which has bit-fields for the parts of the corresponding paging structure.

System info

int ptedit_get_pagesize()

Returns the default page size of the system

Returns Page size of the system in bytes

Page frame numbers (PFN)

size_t ptedit_set_pfn(size_t entry,size_t pfn)

Returns a new page-table entry where the page-frame number (PFN) is replaced by the specified one.

Parameters

Returns A new page-table entry with the given page-frame number

size_t ptedit_get_pfn(size_t entry)

Returns the page-frame number (PFN) of a page-table entry.

Parameters

Returns The page-frame number

Physical pages

void ptedit_read_physical_page(size_t pfn,char * buffer)

Retrieves the content of a physical page.

Parameters

void ptedit_write_physical_page(size_t pfn,char * content)

Replaces the content of a physical page.

Parameters

void * ptedit_pmap(size_t physical,size_t length)

Map a physical address range to the virtual address space.

Parameters

Returns A virtual address that can be used to access the physical address.

Note This function is not supported on Windows.

Paging

size_t ptedit_get_paging_root(pid_t pid)

Returns the root of the paging structure (i.e., CR3 on x86 and TTBR0 on ARM).

Parameters

Returns The phyiscal address (not PFN!) of the first page table (i.e., the PGD)

void ptedit_set_paging_root(pid_t pid,size_t root)

Sets the root of the paging structure (i.e., CR3 on x86 and TTBR0 on ARM).

Parameters

TLB/Barriers

void ptedit_invalidate_tlb(void * address)

Invalidates the TLB for a given address on all CPUs.

Parameters

void ptedit_full_serializing_barrier()

A full serializing barrier which stops everything.

Memory types (PATs/MAIRs)

size_t ptedit_get_mts()

Reads the value of all memory types (x86 PATs / ARM MAIRs). This is equivalent to reading the MSR 0x277 (x86) / MAIR_EL1 (ARM).

Returns The memory types in the same format as in the IA32_PAT MSR / MAIR_EL1

void ptedit_set_mts(size_t mts)

Programs the value of all memory types (x86 PATs / ARM MAIRs). This is equivalent to writing to the MSR 0x277 (x86) / MAIR_EL1 (ARM) on all CPUs.

Parameters

char ptedit_get_mt(unsigned char mt)

Reads the value of a specific memory type attribute (PAT/MAIR).

Parameters

Returns The PAT/MAIR value (can be one of PTEDIT_MT_*)

void ptedit_set_mt(unsigned char mt,unsigned char value)

Programs the value of a specific memory type attribute (PAT/MAIR).

Parameters

unsigned char ptedit_find_mt(unsigned char type)

Generates a bitmask of all memory type attributes (PAT/MAIR) which are programmed to the given value.

Parameters

Returns A bitmask where a set bit indicates that the corresponding PAT/MAIR has the given type

int ptedit_find_first_mt(unsigned char type)

Returns the first memory type attribute (PAT/MAIR) which is programmed to the given memory type.

Parameters

Returns A PAT/MAIR ID, or -1 if no PAT/MAIR of this type was found

size_t ptedit_apply_mt(size_t entry,unsigned char mt)

Returns a new page-table entry which uses the given memory type (PAT/MAIR).

Parameters

Returns A new page-table entry with the given memory type (PAT/MAIR)

unsigned char ptedit_extract_mt(size_t entry)

Returns the memory type (i.e., PAT/MAIR ID) which is used by a page-table entry.

Parameters

Returns A PAT/MAIR ID (between 0 and 7)

size_t ptedit_apply_mt_huge(size_t entry,unsigned char mt)

Returns a new entry for a huge page which uses the given memory type (PAT/MAIR).

Parameters

Returns A new page-table entry with the given memory type (PAT/MAIR)

unsigned char ptedit_extract_mt(size_t entry)

Returns the memory type (i.e., PAT/MAIR ID) which is used by a huge-page entry.

Parameters

Returns A PAT/MAIR ID (between 0 and 7)

const char * ptedit_mt_to_string(unsigned char mt)

Returns a human-readable representation of a memory type (PAT/MAIR value).

Parameters

Returns A human-readable representation of the memory type

Pretty print

void ptedit_print_entry(size_t entry)

Pretty prints a page-table entry.

Parameters

void ptedit_print_entry_line(size_t entry,int line)

Prints a single line of the pretty-print representation of a page-table entry.

Parameters

Use in Academic Papers