How to randomly remap the kernel?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
zaptor
Posts: 10
Joined: Wed Aug 01, 2018 9:09 pm

How to randomly remap the kernel?

Post by zaptor »

I understand how to identity map the kernel - just map the physical addresses of the kernel code to the same virtual addresses in your page tables and then set cr3 to these page tables and everything works.

My question is, how do you remap the kernel code to a different (potentially random) place in virtual memory? If you change the virtual address, the next instruction after the page table is updated will immediately seg fault as the instruction pointer is invalid. It seems impossible to change the virtual address of the kernel code.

In this case, must you copy the kernel byte code to some different physical address and then map the original address of the kernel as a virtual address to this new physical address? Basically you can only randomize the physical location of the kernel in ram. What are the advantages/disadvantages of doing this?
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How to randomly remap the kernel?

Post by iansjack »

Your boot loader can create the page map, load the kernel to the appropriate location and then, as it's last instruction, far jump to the kernel code.
User avatar
Velko
Member
Member
Posts: 153
Joined: Fri Oct 03, 2008 4:13 am
Location: Ogre, Latvia, EU

Re: How to randomly remap the kernel?

Post by Velko »

First you map it to the new location, but keep the original mapping intact. Then jmp to the new location. And finally - remove the mappings of the old addresses.

Note, that if you want to place the code at random location, it has to be compiled/linked as PIC code.
If something looks overcomplicated, most likely it is.
Post Reply