Skip to content

Commit

Permalink
boot: Add menu option to disable experimental kaslr
Browse files Browse the repository at this point in the history
  • Loading branch information
klange committed Dec 7, 2023
1 parent a66f5c5 commit ea99f32
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 7 additions & 0 deletions boot/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -171,6 +175,9 @@ int kmain() {
strcat(cmdline, "lfbwc ");
}

extern int disable_kaslr;
disable_kaslr = !_kaslr;

if (!boot_edit) break;
if (boot_editor()) break;

Expand Down
9 changes: 7 additions & 2 deletions boot/multiboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
asm volatile ( "rdtsc" : "=a"(rando), "=d"((uint32_t){0}) );
size_t xtra = (rando & 0xFF) << 12;
size_t xtra = 0;

if (!disable_kaslr) {
asm volatile ( "rdtsc" : "=a"(rando), "=d"((uint32_t){0}) );
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]));
Expand Down

0 comments on commit ea99f32

Please sign in to comment.