Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp359625ybb; Wed, 8 Apr 2020 00:59:15 -0700 (PDT) X-Google-Smtp-Source: APiQypJqAON3RV0tVml7B7U0FbDWihNSR/IB+jsvAhggzJCDmgp2YUd993EDG95FTZY6RnYscsv9 X-Received: by 2002:a05:6830:231b:: with SMTP id u27mr4930413ote.111.1586332754783; Wed, 08 Apr 2020 00:59:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586332754; cv=none; d=google.com; s=arc-20160816; b=GQVpIkFSYZmMtTIv+68k0efmkainQ+IynzGJaA+RCR2RUB6ud4+PiYHsdPVPthHjbV hJ7wq1O91EdxkgLsOU2cav95anZPT7oUvOiR0ohp5qozt+cKWW/vTxu/JeaFuxFavnvZ j1Ll2v0pBiSH48h5RyI7BMSsRy7A368GPwJRrDypzr5K/lilw6adOMtww8jzHgaul8ro pe/ZuGY14wcu+T8yRpsLED7M3zQqf7gnsm3X5rI/JXjyTdzRom2/k8PiyGadpwO+62GK FvclKWeiz0mDgH9wv14SoSCEYkQm6gPnYzzscwKzArqFNHy9fHhQ72CfATXDh/4PDP3x Pguw== 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=E4WIiXeGbKDxs9UbyyfZOjVgyCJTWA/AduHG+mw3Ozg=; b=xu9lck0vdbw+F2xsuptjfMjnnvSDTDA07UOvfosa+6ERRSt0T5KhYLjUHAJ+t/1qUN dNW3qqUVt6VVZb9+pjwIB/7aIjtj1Qs5VSpCTEcxSp4vEUHgei1cLwyDGNWh5bSfHlGT bV/w+d7NUVR2G6V7/n2pIp8WGpONk+pfV9lyIFwrstkA1G47vUiuKxGFBW9vU+h41Qux NlcK7HmSbHWU2xSfy0o/Rm2TgXfqx5pu31STFmWhYWlAwJ+hwwC1T0csNoW2o4wKCyjD Zr8mLZ2ahX2BQjF1aKgiSoCNje8qr4cES/DpGtCumiTMEdtF6muPMIoAwy/yGJzPa9gb kG9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=TOZYTN0Z; 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 e18si2417408oth.79.2020.04.08.00.59.00; Wed, 08 Apr 2020 00:59:14 -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=TOZYTN0Z; 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 S1727573AbgDHH53 (ORCPT + 99 others); Wed, 8 Apr 2020 03:57:29 -0400 Received: from mail-pj1-f66.google.com ([209.85.216.66]:33055 "EHLO mail-pj1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727515AbgDHH52 (ORCPT ); Wed, 8 Apr 2020 03:57:28 -0400 Received: by mail-pj1-f66.google.com with SMTP id cp9so1952645pjb.0 for ; Wed, 08 Apr 2020 00:57:27 -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=E4WIiXeGbKDxs9UbyyfZOjVgyCJTWA/AduHG+mw3Ozg=; b=TOZYTN0ZmTo7tkdrjh0d1AhqQRXNkDEiNfrJu4HSuIaIiOkXzgv343+X9uduChlxuH 5GPD/mmxsXBoBB14GTJKfsYkmdbX+iUL+dCVfbt+u75yXYegqNi1SrMLOcTYAi4T88lv qtT4EPWIizz8lTpXFwCmAj5VEUJfA30aJ5B+himyfvXRvcepspXbnZEigEMTMyxBRsI1 rCfbOUbOO2fuhdV2fehBFtONQJoNk61FccNMvakWX+2TCphrgANZ1IFSPA42eCeWa9OG M+RbsCIz3G7evFBUt61IGLqkg9rI5gPYiJuMTj5URYLiZTZW/IrnKhHTcL/h5ex2W4c1 2vRQ== 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=E4WIiXeGbKDxs9UbyyfZOjVgyCJTWA/AduHG+mw3Ozg=; b=drAT3cDZ9RWWUtVeBk06ZXpajPno+IkJeRs85YRSoonkqom2VHaglcNio3DqAjD/Nc QGR5AYfGNMlx6L6Or5aLnwanxrVFpc5GjL4a5cg3tjz0YFvRHPoJZoY3S64LzM2RfXmf zRWxTil59GSn4IrtvESFDkncLmU8pN9Omrtl1QYihAmrL5rPlnGG/AOy1FoKJR24YEYK AYwQ2Gwct3i1MvNidvAP01Ftj2L5EkklIsDUys6vbTSpJ8itg7zkeACyg/0iK7khcAKA eSBWosfAn6vh/4yFNnP5pyL2q0H7aLQ/vxdYMWFZepgPYhInymCSKKNRrcSIh3S674r2 KNSQ== X-Gm-Message-State: AGi0PuZLTzexvEw7CAQR1fGc/iRu9yxWHEEQ7skLYunAEEwZUE4AJHg9 fL7fCgRmGKHy0X4pdQqHSpP2xw== X-Received: by 2002:a17:90a:714b:: with SMTP id g11mr3741642pjs.17.1586332646601; Wed, 08 Apr 2020 00:57:26 -0700 (PDT) Received: from hsinchu02.internal.sifive.com (114-34-229-221.HINET-IP.hinet.net. [114.34.229.221]) by smtp.gmail.com with ESMTPSA id d85sm485599pfd.157.2020.04.08.00.57.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Apr 2020 00:57:26 -0700 (PDT) From: Zong Li To: palmer@dabbelt.com, paul.walmsley@sifive.com, aou@eecs.berkeley.edu, mhiramat@kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Zong Li Subject: [PATCH v5 9/9] riscv: add STRICT_KERNEL_RWX support Date: Wed, 8 Apr 2020 15:57:04 +0800 Message-Id: <100e739c5fd722a96fcc640c8ee0c82fe34fcb6a.1586332296.git.zong.li@sifive.com> X-Mailer: git-send-email 2.26.0 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 The commit contains that make text section as non-writable, rodata section as read-only, and data section as non-executable. The init section should be changed to non-executable. Signed-off-by: Zong Li --- arch/riscv/Kconfig | 1 + arch/riscv/include/asm/set_memory.h | 8 ++++++ arch/riscv/mm/init.c | 44 +++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 1e1efc998baf..58b556167d59 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -61,6 +61,7 @@ config RISCV select ARCH_HAS_GIGANTIC_PAGE select ARCH_HAS_SET_DIRECT_MAP select ARCH_HAS_SET_MEMORY + select ARCH_HAS_STRICT_KERNEL_RWX select ARCH_WANT_HUGE_PMD_SHARE if 64BIT select SPARSEMEM_STATIC if 32BIT select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU diff --git a/arch/riscv/include/asm/set_memory.h b/arch/riscv/include/asm/set_memory.h index 4c5bae7ca01c..c38df4771c09 100644 --- a/arch/riscv/include/asm/set_memory.h +++ b/arch/riscv/include/asm/set_memory.h @@ -22,6 +22,14 @@ static inline int set_memory_x(unsigned long addr, int numpages) { return 0; } static inline int set_memory_nx(unsigned long addr, int numpages) { return 0; } #endif +#ifdef CONFIG_STRICT_KERNEL_RWX +void set_kernel_text_ro(void); +void set_kernel_text_rw(void); +#else +static inline void set_kernel_text_ro(void) { } +static inline void set_kernel_text_rw(void) { } +#endif + int set_direct_map_invalid_noflush(struct page *page); int set_direct_map_default_noflush(struct page *page); diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index fab855963c73..b55be44ff9bd 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -477,6 +478,17 @@ static void __init setup_vm_final(void) csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | SATP_MODE); local_flush_tlb_all(); } + +void free_initmem(void) +{ + unsigned long init_begin = (unsigned long)__init_begin; + unsigned long init_end = (unsigned long)__init_end; + + /* Make the region as non-execuatble. */ + set_memory_nx(init_begin, (init_end - init_begin) >> PAGE_SHIFT); + free_initmem_default(POISON_FREE_INITMEM); +} + #else asmlinkage void __init setup_vm(uintptr_t dtb_pa) { @@ -488,6 +500,38 @@ static inline void setup_vm_final(void) } #endif /* CONFIG_MMU */ +#ifdef CONFIG_STRICT_KERNEL_RWX +void set_kernel_text_rw(void) +{ + unsigned long text_start = (unsigned long)_text; + unsigned long text_end = (unsigned long)_etext; + + set_memory_rw(text_start, (text_end - text_start) >> PAGE_SHIFT); +} + +void set_kernel_text_ro(void) +{ + unsigned long text_start = (unsigned long)_text; + unsigned long text_end = (unsigned long)_etext; + + set_memory_ro(text_start, (text_end - text_start) >> PAGE_SHIFT); +} + +void mark_rodata_ro(void) +{ + unsigned long text_start = (unsigned long)_text; + unsigned long text_end = (unsigned long)_etext; + unsigned long rodata_start = (unsigned long)__start_rodata; + unsigned long data_start = (unsigned long)_data; + unsigned long max_low = (unsigned long)(__va(PFN_PHYS(max_low_pfn))); + + set_memory_ro(text_start, (text_end - text_start) >> PAGE_SHIFT); + set_memory_ro(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT); + set_memory_nx(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT); + set_memory_nx(data_start, (max_low - data_start) >> PAGE_SHIFT); +} +#endif + void __init paging_init(void) { setup_vm_final(); -- 2.26.0