From f37e1c2229509e2133986b9a05133967b5c60361 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 12 Aug 2024 14:28:52 +0300 Subject: [PATCH] Kernel: Map .rodata as read only instead of read-write --- kernel/arch/i686/PageTable.cpp | 13 ++++++++++++- kernel/arch/i686/linker.ld | 2 ++ kernel/arch/x86_64/PageTable.cpp | 13 ++++++++++++- kernel/arch/x86_64/linker.ld | 2 ++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/kernel/arch/i686/PageTable.cpp b/kernel/arch/i686/PageTable.cpp index 60d568a09f..15978e671e 100644 --- a/kernel/arch/i686/PageTable.cpp +++ b/kernel/arch/i686/PageTable.cpp @@ -9,6 +9,9 @@ extern uint8_t g_kernel_end[]; extern uint8_t g_kernel_execute_start[]; extern uint8_t g_kernel_execute_end[]; +extern uint8_t g_kernel_writable_start[]; +extern uint8_t g_kernel_writable_end[]; + extern uint8_t g_userspace_start[]; extern uint8_t g_userspace_end[]; @@ -148,7 +151,7 @@ namespace Kernel V2P(g_kernel_start), (vaddr_t)g_kernel_start, g_kernel_end - g_kernel_start, - Flags::ReadWrite | Flags::Present + Flags::Present ); // Map executable kernel memory as executable @@ -159,6 +162,14 @@ namespace Kernel Flags::Execute | Flags::Present ); + // Map writable kernel memory as writable + map_range_at( + V2P(g_kernel_writable_start), + (vaddr_t)g_kernel_writable_start, + g_kernel_writable_end - g_kernel_writable_start, + Flags::ReadWrite | Flags::Present + ); + // Map userspace memory map_range_at( V2P(g_userspace_start), diff --git a/kernel/arch/i686/linker.ld b/kernel/arch/i686/linker.ld index a8e042d997..767bfb687b 100644 --- a/kernel/arch/i686/linker.ld +++ b/kernel/arch/i686/linker.ld @@ -34,12 +34,14 @@ SECTIONS } .data ALIGN(4K) : AT(ADDR(.data) - KERNEL_OFFSET) { + g_kernel_writable_start = .; *(.data) } .bss ALIGN(4K) : AT(ADDR(.bss) - KERNEL_OFFSET) { *(COMMON) *(.bss) + g_kernel_writable_end = .; } g_kernel_end = .; } diff --git a/kernel/arch/x86_64/PageTable.cpp b/kernel/arch/x86_64/PageTable.cpp index 65cd0bfebb..b00da9d564 100644 --- a/kernel/arch/x86_64/PageTable.cpp +++ b/kernel/arch/x86_64/PageTable.cpp @@ -11,6 +11,9 @@ extern uint8_t g_kernel_end[]; extern uint8_t g_kernel_execute_start[]; extern uint8_t g_kernel_execute_end[]; +extern uint8_t g_kernel_writable_start[]; +extern uint8_t g_kernel_writable_end[]; + extern uint8_t g_userspace_start[]; extern uint8_t g_userspace_end[]; @@ -170,7 +173,7 @@ namespace Kernel V2P(g_kernel_start), (vaddr_t)g_kernel_start, g_kernel_end - g_kernel_start, - Flags::ReadWrite | Flags::Present + Flags::Present ); // Map executable kernel memory as executable @@ -181,6 +184,14 @@ namespace Kernel Flags::Execute | Flags::Present ); + // Map writable kernel memory as writable + map_range_at( + V2P(g_kernel_writable_start), + (vaddr_t)g_kernel_writable_start, + g_kernel_writable_end - g_kernel_writable_start, + Flags::ReadWrite | Flags::Present + ); + // Map userspace memory map_range_at( V2P(g_userspace_start), diff --git a/kernel/arch/x86_64/linker.ld b/kernel/arch/x86_64/linker.ld index d8bb8012aa..cb871f463f 100644 --- a/kernel/arch/x86_64/linker.ld +++ b/kernel/arch/x86_64/linker.ld @@ -34,12 +34,14 @@ SECTIONS } .data ALIGN(4K) : AT(ADDR(.data) - KERNEL_OFFSET) { + g_kernel_writable_start = .; *(.data) } .bss ALIGN(4K) : AT(ADDR(.bss) - KERNEL_OFFSET) { *(COMMON) *(.bss) + g_kernel_writable_end = .; } g_kernel_end = .; }