From b65bbf13a3c53d4d86bbffc2086b83b3dfb4721d Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Fri, 10 Nov 2023 16:34:54 +0900 Subject: [PATCH] boot: rudimentary kaslr --- boot/multiboot.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/boot/multiboot.c b/boot/multiboot.c index ee3c5c02..f3db9c82 100644 --- a/boot/multiboot.c +++ b/boot/multiboot.c @@ -60,10 +60,18 @@ static uintptr_t ramdisk_len = 0; uintptr_t final_offset = 0; uintptr_t _xmain = 0; +static inline uint64_t read_tsc(void) { + uint32_t lo, hi; + asm volatile ( "rdtsc" : "=a"(lo), "=d"(hi) ); + return ((uint64_t)hi << 32) | (uint64_t)lo; +} + 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; - size_t xtra = 0x100000; + uint32_t rando = 0; + asm volatile ( "rdtsc" : "=a"(rando), "=d"((uint32_t){0}) ); + size_t 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]));