Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp4552690ybb; Tue, 24 Mar 2020 00:32:51 -0700 (PDT) X-Google-Smtp-Source: ADFU+vvJgd79UNpkIq5aB1Z4OqkYXsZqO7xPMJnOOG+YsRwRxv9iJF0Eronpy8E/S0ov5keVov7K X-Received: by 2002:a05:6830:1d4f:: with SMTP id p15mr19814174oth.38.1585035171510; Tue, 24 Mar 2020 00:32:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585035171; cv=none; d=google.com; s=arc-20160816; b=Zlb0RPyEkCSBZMhQvZYEBp+iFVMnONe22Li9v1xrDnP+if4Sfs+MdOWJulfhFHvcNG g0lnUgmulgPvcylqYihjKkeS0QOnU6GGUszB+p5IoP6gfrZIEdeIczOiDmorv2NPIhNh uaPFE+h+L3noH9EiwOj86NbzoipRFnD+eAzdN0K6q8jioVvupuK4pn/2iXusM/415xqx M6uUa3zzhT2b13MXvHK/88QYPIyFsG8gzMUuB2nLd2Eh8zLxTrGrnb5GynT2GG8KpaX2 lBcKRnuZL4KwvVkTS/x1LTg+LzsmrL0vgQeYlLiy3i03DjEcGIztk+9uULxM46UvpeTH gvDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=XEXvf7YM3IfG8+Nza4lz7YWo/aCxS7DRWP5Lj0YYBjc=; b=J4+YTJj2tmpEilYXOxOXbK7Sq2j66BnNITBBEODWILBDlEIl549CPAno3C0ulM8weu CMb4N4EIU45uxboaYypesrE+eJ5GBWQp2I/0dFFL02juAakbuufazs4Ic6h+i6iheVti 7zSiXTlDwIP3eKXLFoeRIIpdUtqDLBG36Wv72m1n2iwXpa31Owfl1XHDZSjBxfr/3dwI Snra/RBAlaFgGTc/1mmRcJyXBjo+PLENPQqQGSd2BCqL1LeOnwYiUNCgHXgJq7OHMCmL wHdrq1QfqNkNnvXDzAT670HfWZ0N33ZS7kvd32KImSM6MA3GUf1dJ0JPqjjRbQDnPMKY vyNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=FFPXtP0J; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w81si9128055oia.48.2020.03.24.00.32.39; Tue, 24 Mar 2020 00:32:51 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=FFPXtP0J; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727556AbgCXHbS (ORCPT + 99 others); Tue, 24 Mar 2020 03:31:18 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:35689 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727509AbgCXHbI (ORCPT ); Tue, 24 Mar 2020 03:31:08 -0400 Received: by mail-pg1-f196.google.com with SMTP id 7so8616157pgr.2 for ; Tue, 24 Mar 2020 00:31:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=XEXvf7YM3IfG8+Nza4lz7YWo/aCxS7DRWP5Lj0YYBjc=; b=FFPXtP0JGFZuxhK64DdBrFFIn56NYFvcpNK4yw2jPsrNBzClysK7yzg71194N9zxlm DahcEGASnS2ix5XgdwV4Lo9/xTQnS7dbdexFOYf9D9V/N1n6i5a6r5ro0rFbNJFZcj11 zLkBMr4Vt++GfCQOzTAFEa0m+Wvc+ZyFp5Z8gZl6H6Yrdc0JCKu7QON2n2vuswysspfK FjefZR8RpVRjH6rQq94sITHb2ZfhG8tVOzJJsl4bUe2bFTLcio7f5m4GDprt6yrHDQlf fJJiesOscjpShsShndfOjML8ZFBlEYONjEBWGDRYn6lcWE46TdJ+ecom/MTHMqdtm32f wAIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XEXvf7YM3IfG8+Nza4lz7YWo/aCxS7DRWP5Lj0YYBjc=; b=BiOjW87uIBct9YMAy7/Xk+vabTQg6QGirhjDjFJohX6OkwOhJvDO12F+IJKPVXjxaw jnaj0ayESQZ6+Z4fzSOnxt1jyPpoDU3cX1Sg4r73V5H+wMYTmIhvkQizdEZRK2HN8BRE L8RAbLltVrJALjvUOmYNph4uC9EZVrfI+wnHMetNXXsKYAmTaRszVELt7NB7M4zcumfo AqrokJ4G4/+TJpIjc2jPtUrJNAOv26h9kqsbpTANzut5MbwbpJpkdQ15RHtK3ChlSj2n HaX/9uZl9AtpEpwapqKHPHJBBVJqkjeLY8Wleky2zyMBRtfBOMhJeFyRpjkYPaq7XXz3 4Wsw== X-Gm-Message-State: ANhLgQ1vPS9gsqJDV9rYroi3srVPGOS/dUv0nNIFCYDnNjK/6UESYA9F 1Xku5+KB46OmVIK02OsWWZb18A== X-Received: by 2002:a63:7c02:: with SMTP id x2mr26880475pgc.236.1585035067681; Tue, 24 Mar 2020 00:31:07 -0700 (PDT) Received: from hsinchu02.internal.sifive.com (220-132-236-182.HINET-IP.hinet.net. [220.132.236.182]) by smtp.gmail.com with ESMTPSA id i187sm15124648pfg.33.2020.03.24.00.31.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Mar 2020 00:31:07 -0700 (PDT) From: Zong Li To: palmer@dabbelt.com, paul.walmsley@sifive.com, alex@ghiti.fr, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Zong Li Subject: [PATCH RFC 6/8] riscv/kaslr: clear the original kernel image Date: Tue, 24 Mar 2020 15:30:51 +0800 Message-Id: <8373a9d18958b99b72ed9499786dffe45adf9617.1584352425.git.zong.li@sifive.com> X-Mailer: git-send-email 2.25.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org After completing final page table, we can clear original kernel image and remove executable permission. Signed-off-by: Zong Li --- 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 + */ + +#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 #ifdef CONFIG_RELOCATABLE #include +#include #endif #include @@ -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