Thanks to Stephen, your research here was useful to me. I had several problems, although I tried to do this on the 2.6.32 kernel and get WARNING: at arch/x86/mm/pageattr.c:877 change_page_attr_set_clr+0x343/0x530() (Not tainted) followed by the OOPS of the kernel, about the impossibility of writing to the memory address.
The comment above the line reads:
// People should not be passing in unaligned addresses
The following modified code works:
int set_page_rw(long unsigned int _addr) { return set_memory_rw(PAGE_ALIGN(_addr) - PAGE_SIZE, 1); } int set_page_ro(long unsigned int _addr) { return set_memory_ro(PAGE_ALIGN(_addr) - PAGE_SIZE, 1); }
Note that this still does not set the page as read / write in some situations. The static_protections() function, which is called inside set_memory_rw() , removes the _PAGE_RW flag if:
- In the field of BIOS
- The address is inside .rodata li>
- CONFIG_DEBUG_RODATA is set, and the kernel is configured to read only
I found this after debugging, why I still "could not handle the kernel swap request" when I tried to change the address of the kernel functions. In the end, I was able to solve this problem by finding the record in the table for the address myself and manually setting it to record. Fortunately, the lookup_address() function is exported in version 2.6.26+. Here is the code I wrote for this:
void set_addr_rw(unsigned long addr) { unsigned int level; pte_t *pte = lookup_address(addr, &level); if (pte->pte &~ _PAGE_RW) pte->pte |= _PAGE_RW; } void set_addr_ro(unsigned long addr) { unsigned int level; pte_t *pte = lookup_address(addr, &level); pte->pte = pte->pte &~_PAGE_RW; }
Finally, while Mark's answer is technically correct, a problem will occur when starting inside Xen. If you want to disable write protection, use the cr0 read / write functions. I am making a macro this way:
#define GPF_DISABLE write_cr0(read_cr0() & (~ 0x10000)) #define GPF_ENABLE write_cr0(read_cr0() | 0x10000)
Hope this helps someone else who stumbles upon this question.
Corey Henderson Jul 19 2018-11-11T00: 00Z
source share