boot: Add menu option to disable experimental kaslr

This commit is contained in:
K. Lange 2023-12-07 10:36:18 +09:00
parent a66f5c5f39
commit ea99f32e9d
2 changed files with 14 additions and 2 deletions

View File

@ -105,6 +105,10 @@ int kmain() {
"Enables write-combining PAT configuration for",
"framebuffers. Toggle if graphics are slow.");
BOOT_OPTION(_kaslr, 1, "KASLR (experimental)",
"Enables rudimentary randomization of the kernel",
"load address within a small range.");
while (1) {
/* Loop over rendering the menu */
show_menu();
@ -171,6 +175,9 @@ int kmain() {
strcat(cmdline, "lfbwc ");
}
extern int disable_kaslr;
disable_kaslr = !_kaslr;
if (!boot_edit) break;
if (boot_editor()) break;

View File

@ -59,6 +59,7 @@ static uintptr_t ramdisk_off = 0;
static uintptr_t ramdisk_len = 0;
uintptr_t final_offset = 0;
uintptr_t _xmain = 0;
int disable_kaslr = 0;
static inline uint64_t read_tsc(void) {
uint32_t lo, hi;
@ -70,8 +71,12 @@ static int load_aout(uint32_t * hdr) {
uintptr_t base_offset = (uintptr_t)hdr - (uintptr_t)kernel_load_start;
uintptr_t hdr_offset = hdr[3] - base_offset;
uint32_t rando = 0;
size_t xtra = 0;
if (!disable_kaslr) {
asm volatile ( "rdtsc" : "=a"(rando), "=d"((uint32_t){0}) );
size_t xtra = (rando & 0xFF) << 12;
xtra = (rando & 0xFF) << 12;
}
memcpy((void*)(uintptr_t)hdr[4] + xtra, kernel_load_start + (hdr[4] - hdr_offset), (hdr[5] - hdr[4]));
memset((void*)(uintptr_t)hdr[5] + xtra, 0, (hdr[6] - hdr[5]));