Kernel: Use the whole kernel PD range when randomizing the KASLR offset

Now that we reclaim the memory range that is created by KASLR before
the start of the kernel image, there's no need to be conservative with
the KASLR offset.
This commit is contained in:
Idan Horowitz 2022-03-22 19:10:17 +02:00 committed by Idan Horowitz
parent e0c7727934
commit f0166efe8c
6 changed files with 5 additions and 8 deletions

View file

@ -18,7 +18,6 @@ extern "C" PhysicalAddress start_of_prekernel_image;
extern "C" PhysicalAddress end_of_prekernel_image;
extern "C" size_t physical_to_virtual_offset;
extern "C" FlatPtr kernel_mapping_base;
extern "C" FlatPtr default_kernel_load_base;
extern "C" FlatPtr kernel_load_base;
#if ARCH(X86_64)
extern "C" u32 gdt64ptr;

View file

@ -36,7 +36,8 @@ UNMAP_AFTER_INIT NonnullRefPtr<PageDirectory> PageDirectory::must_create_kernel_
{
auto directory = adopt_ref_if_nonnull(new (nothrow) PageDirectory).release_nonnull();
MUST(directory->m_range_allocator.initialize_with_range(VirtualAddress(default_kernel_load_base), KERNEL_PD_END - default_kernel_load_base));
auto kernel_range_start = kernel_mapping_base + 2 * MiB; // The first 2 MiB are used for mapping the pre-kernel
MUST(directory->m_range_allocator.initialize_with_range(VirtualAddress(kernel_range_start), KERNEL_PD_END - kernel_range_start));
// Carve out the whole page directory covering the kernel image to make MemoryManager::initialize_physical_pages() happy
FlatPtr start_of_range = ((FlatPtr)start_of_kernel_image & ~(FlatPtr)0x1fffff);
FlatPtr end_of_range = ((FlatPtr)end_of_kernel_image & ~(FlatPtr)0x1fffff) + 0x200000;

View file

@ -13,6 +13,7 @@
#endif
#define MAX_KERNEL_SIZE 0x4000000
#define KERNEL_PD_SIZE 0x31000000
#ifdef __cplusplus
namespace Kernel {
@ -22,7 +23,6 @@ struct [[gnu::packed]] BootInfo {
u32 end_of_prekernel_image;
u64 physical_to_virtual_offset;
u64 kernel_mapping_base;
u64 default_kernel_load_base;
u64 kernel_load_base;
# if ARCH(X86_64)
u32 gdt64ptr;

View file

@ -97,7 +97,7 @@ extern "C" [[noreturn]] void init()
#endif
// KASLR
static constexpr auto maximum_offset = 256 * MiB;
FlatPtr maximum_offset = (FlatPtr)KERNEL_PD_SIZE - MAX_KERNEL_SIZE - 2 * MiB; // The first 2 MiB are used for mapping the pre-kernel
FlatPtr kernel_load_base = default_kernel_load_base + (generate_secure_seed() % maximum_offset);
kernel_load_base &= ~(2 * MiB - 1);
@ -184,7 +184,6 @@ extern "C" [[noreturn]] void init()
info.end_of_prekernel_image = (PhysicalPtr)end_of_prekernel_image;
info.physical_to_virtual_offset = kernel_load_base - kernel_physical_base;
info.kernel_mapping_base = kernel_mapping_base;
info.default_kernel_load_base = default_kernel_load_base;
info.kernel_load_base = kernel_load_base;
#if ARCH(X86_64)
info.gdt64ptr = (PhysicalPtr)gdt64ptr;

View file

@ -15,7 +15,7 @@
#define READONLY_AFTER_INIT __attribute__((section(".ro_after_init")))
#define UNMAP_AFTER_INIT NEVER_INLINE __attribute__((section(".unmap_after_init")))
#define KERNEL_PD_END (kernel_mapping_base + 0x31000000)
#define KERNEL_PD_END (kernel_mapping_base + KERNEL_PD_SIZE)
#define KERNEL_PT1024_BASE (kernel_mapping_base + 0x3FE00000)
#define KERNEL_QUICKMAP_PT (KERNEL_PT1024_BASE + 0x6000)
#define KERNEL_QUICKMAP_PD (KERNEL_PT1024_BASE + 0x7000)

View file

@ -117,7 +117,6 @@ READONLY_AFTER_INIT PhysicalAddress start_of_prekernel_image;
READONLY_AFTER_INIT PhysicalAddress end_of_prekernel_image;
READONLY_AFTER_INIT size_t physical_to_virtual_offset;
READONLY_AFTER_INIT FlatPtr kernel_mapping_base;
READONLY_AFTER_INIT FlatPtr default_kernel_load_base;
READONLY_AFTER_INIT FlatPtr kernel_load_base;
#if ARCH(X86_64)
READONLY_AFTER_INIT PhysicalAddress boot_pml4t;
@ -150,7 +149,6 @@ extern "C" [[noreturn]] UNMAP_AFTER_INIT void init(BootInfo const& boot_info)
end_of_prekernel_image = PhysicalAddress { boot_info.end_of_prekernel_image };
physical_to_virtual_offset = boot_info.physical_to_virtual_offset;
kernel_mapping_base = boot_info.kernel_mapping_base;
default_kernel_load_base = boot_info.default_kernel_load_base;
kernel_load_base = boot_info.kernel_load_base;
#if ARCH(X86_64)
gdt64ptr = boot_info.gdt64ptr;