[PATCH RFC 6/8] riscv/kaslr: clear the original kernel image
From: Zong Li
Date: Tue Mar 24 2020 - 03:31:20 EST
After completing final page table, we can clear original kernel image
and remove executable permission.
Signed-off-by: Zong Li <zong.li@xxxxxxxxxx>
---
arch/riscv/include/asm/kaslr.h | 12 ++++++++++++
arch/riscv/kernel/kaslr.c | 12 ++++++++++++
arch/riscv/mm/init.c | 6 ++++++
3 files changed, 30 insertions(+)
create mode 100644 arch/riscv/include/asm/kaslr.h
diff --git a/arch/riscv/include/asm/kaslr.h b/arch/riscv/include/asm/kaslr.h
new file mode 100644
index 000000000000..b165fe71dd4a
--- /dev/null
+++ b/arch/riscv/include/asm/kaslr.h
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2020 SiFive
+ * Copyright (C) 2020 Zong Li <zong.li@xxxxxxxxxx>
+ */
+
+#ifndef _ASM_RISCV_KASLR_H
+#define _ASM_RISCV_KASLR_H
+
+void __init kaslr_late_init(void);
+
+#endif /* _ASM_RISCV_KASLR_H */
diff --git a/arch/riscv/kernel/kaslr.c b/arch/riscv/kernel/kaslr.c
index 59001d6fdfc3..0bd30831c455 100644
--- a/arch/riscv/kernel/kaslr.c
+++ b/arch/riscv/kernel/kaslr.c
@@ -356,6 +356,18 @@ static __init uintptr_t get_random_offset(u64 seed, uintptr_t kernel_size)
return get_legal_offset(random, kernel_size_align);
}
+void __init kaslr_late_init(void)
+{
+ uintptr_t kernel_size;
+
+ /* Clear original kernel image. */
+ if (kaslr_offset) {
+ kernel_size = (uintptr_t) _end - (uintptr_t) _start;
+ memset((void *)PAGE_OFFSET, 0, kernel_size);
+ set_memory_nx(PAGE_OFFSET, kaslr_offset >> PAGE_SHIFT);
+ }
+}
+
uintptr_t __init kaslr_early_init(void)
{
u64 seed;
diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
index 34c6ecf2c599..08e2ce170533 100644
--- a/arch/riscv/mm/init.c
+++ b/arch/riscv/mm/init.c
@@ -15,6 +15,7 @@
#include <linux/set_memory.h>
#ifdef CONFIG_RELOCATABLE
#include <linux/elf.h>
+#include <asm/kaslr.h>
#endif
#include <asm/fixmap.h>
@@ -649,6 +650,11 @@ static void __init setup_vm_final(void)
/* Move to swapper page table */
csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | SATP_MODE);
local_flush_tlb_all();
+
+#ifdef CONFIG_RANDOMIZE_BASE
+ /* Clear orignial kernel image and set the right permission. */
+ kaslr_late_init();
+#endif
}
void free_initmem(void)
--
2.25.1