All of lore.kernel.org
 help / color / mirror / Atom feed
From: Zong Li <zong.li@sifive.com>
To: palmer@dabbelt.com, paul.walmsley@sifive.com, alex@ghiti.fr,
	linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org
Cc: Zong Li <zong.li@sifive.com>
Subject: [PATCH RFC 6/8] riscv/kaslr: clear the original kernel image
Date: Tue, 24 Mar 2020 15:30:51 +0800	[thread overview]
Message-ID: <8373a9d18958b99b72ed9499786dffe45adf9617.1584352425.git.zong.li@sifive.com> (raw)
In-Reply-To: <cover.1584352425.git.zong.li@sifive.com>

After completing final page table, we can clear original kernel image
and remove executable permission.

Signed-off-by: Zong Li <zong.li@sifive.com>
---
 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@sifive.com>
+ */
+
+#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


  parent reply	other threads:[~2020-03-24  7:31 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-24  7:30 [PATCH RFC 0/8] Support KASLR for RISC-V Zong Li
2020-03-24  7:30 ` [PATCH RFC 1/8] riscv/kaslr: add interface to get kaslr offset Zong Li
2020-04-07  5:08   ` Alex Ghiti
2020-03-24  7:30 ` [PATCH RFC 2/8] riscv/kaslr: introduce functions to clear page table Zong Li
2020-04-07  5:09   ` Alex Ghiti
2020-04-07  9:17     ` Zong Li
2020-04-07  9:17       ` Zong Li
2020-03-24  7:30 ` [PATCH RFC 3/8] riscv/kaslr: support KASLR infrastructure Zong Li
2020-04-07  5:10   ` Alex Ghiti
2020-04-07 10:34     ` Zong Li
2020-04-07 10:34       ` Zong Li
2020-04-09  5:53       ` Alex Ghiti
2020-04-09  5:53         ` Alex Ghiti
2020-04-09 11:08         ` Zong Li
2020-04-09 11:08           ` Zong Li
2020-03-24  7:30 ` [PATCH RFC 4/8] riscv/kaslr: randomize the kernel image offset Zong Li
2020-04-07  5:11   ` Alex Ghiti
2020-04-07 10:53     ` Zong Li
2020-04-07 10:53       ` Zong Li
2020-04-09  5:51       ` Alex Ghiti
2020-04-09  5:51         ` Alex Ghiti
2020-04-09 10:31         ` Zong Li
2020-04-09 10:31           ` Zong Li
2020-04-10 15:58           ` Alex Ghiti
2020-04-10 15:58             ` Alex Ghiti
2020-04-11  8:20             ` Zong Li
2020-04-11  8:20               ` Zong Li
2020-04-12  6:53               ` Alex Ghiti
2020-04-12  6:53                 ` Alex Ghiti
2020-04-14  2:46                 ` Zong Li
2020-04-14  2:46                   ` Zong Li
2020-04-14  5:43                   ` Alex Ghiti
2020-04-14  5:43                     ` Alex Ghiti
2020-04-14  7:00                     ` Zong Li
2020-04-14  7:00                       ` Zong Li
2020-03-24  7:30 ` [PATCH RFC 5/8] riscv/kaslr: support sparse memory model Zong Li
2020-03-24  7:30 ` Zong Li [this message]
2020-04-07  5:11   ` [PATCH RFC 6/8] riscv/kaslr: clear the original kernel image Alex Ghiti
2020-04-07 11:18     ` Zong Li
2020-04-07 11:18       ` Zong Li
2020-04-09  5:53       ` Alex Ghiti
2020-04-09  5:53         ` Alex Ghiti
2020-04-09  8:14         ` Alex Ghiti
2020-04-09  8:14           ` Alex Ghiti
2020-03-24  7:30 ` [PATCH RFC 7/8] riscv/kaslr: add cmdline support to disable KASLR Zong Li
2020-03-24  7:30 ` [PATCH RFC 8/8] riscv/kaslr: dump out kernel offset information on panic Zong Li

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8373a9d18958b99b72ed9499786dffe45adf9617.1584352425.git.zong.li@sifive.com \
    --to=zong.li@sifive.com \
    --cc=alex@ghiti.fr \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=palmer@dabbelt.com \
    --cc=paul.walmsley@sifive.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.