Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp2641768ybh; Mon, 9 Mar 2020 09:57:25 -0700 (PDT) X-Google-Smtp-Source: ADFU+vsWRAIUA7Tuu/IsOK5ZvJaPcFICfU6y+SSjLd5gs7PLqnR0LnTDL6g6FFwziTRLsKq0zbii X-Received: by 2002:aca:aa8b:: with SMTP id t133mr150116oie.95.1583773045077; Mon, 09 Mar 2020 09:57:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583773045; cv=none; d=google.com; s=arc-20160816; b=N/Whjucc9aNIcB4EK4ypJglOSm6bPJhDhg+PK4I+egC3D3ki813n50cgBFr+F8pCgS MN6pc7ahpjfC9AipHbubN9u8dzUqc6MWmhRcG0WpZiXALg7t/KEqI+uvteDNA4Pv6wvJ TUaKupzjaCmpvcmMQowwzE2+RLfJQ7NzFEUu7OAuteSS7KG/a0ZWG0J5tiDAp6+G3Wz2 3FGtjPv1Usum87WBLBlacqTaaYS6DavmU/LD/hb8DTVom9Bwe5fOSswdiKrQq0iaR1Mf jBVmv10SNEKYaaE9R66EId/los3D6HRL1mOf7qf9leyVyq0lBNCWMRoOpZXgmwwcGh/Y sAxQ== 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=xY84tDONTtbk2uTeIT3VIK8abrA7sFeiX9nBiqOTOno=; b=AedvjiHG3lnCUIK/T6aqAKgOww43+MvLVB+VA/9UT/+GSartgF/WoMRW+crhKog0J9 OY+Rvr1TXDP6OQ9xytEuV/TuPIX54s/P+Zrq9Q5A1drZg/aV9pcZfga1/62kj1vaL5ap PwPPT0HBQAukuta2BtXsNHZVPS37pLha52Asi23yxQrm5BCOWUdbSqNhhUNeuUowUcSx T1ociKKpN4WKoyznP2y6+zGNPFI6O0pjcb0wzr+IT/B3bnFTXG/Dera49INJxN23EeuL xzIQKcUuVomXoTsOjwjAakobVMz6M5G3DNBoqZzATAm09PzcdzJV01/ZZG8RPJiCgQ5f VcMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=gqj0dx0+; 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 l21si6422175otk.142.2020.03.09.09.57.13; Mon, 09 Mar 2020 09:57:25 -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=gqj0dx0+; 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 S1727401AbgCIQ4V (ORCPT + 99 others); Mon, 9 Mar 2020 12:56:21 -0400 Received: from mail-pj1-f67.google.com ([209.85.216.67]:37626 "EHLO mail-pj1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727328AbgCIQ4D (ORCPT ); Mon, 9 Mar 2020 12:56:03 -0400 Received: by mail-pj1-f67.google.com with SMTP id ca13so115125pjb.2 for ; Mon, 09 Mar 2020 09:56:03 -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=xY84tDONTtbk2uTeIT3VIK8abrA7sFeiX9nBiqOTOno=; b=gqj0dx0+9O/PE/Ao8bweu2u+birAb0nGJCfIkBAwDQSn/SGLUmrx94rqv39Lde9Fc3 ntUMyFTVnMqHoFeFW5qwD50st2uFwzUJXqQAbZbY/jse5cNOYHMJN0LCpkZoAVByBh9v SZC4iABowHXDDyqOJ5l3JvAIH1edUm/DlDyp0WwQVN368zus1Uw4JLje87iTo7XRKaCV FFohQ2MkOYbqUq6wRtCvVT2tOLIIHkUzuT6i9JdHf/idhJgvq1gZuSxKfgy3s9xov6hR BRk5kz8cfTWGPL0KdhkR8QK3myFA5Di4/mfF+eZmFJUENKcLqYo05qJ1n0Zt9LsRRDoQ GCWw== 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=xY84tDONTtbk2uTeIT3VIK8abrA7sFeiX9nBiqOTOno=; b=l/0O+IcEgeKvK+zSufYU2e02pHk6aYf9uuc+uQ/m6FC3WKX9Bri3oyRXdMnP6LzPjD s5rvmE4NaNoUXnAYtVujXEm2AkaMShD2ffJv/gTAdc95y4gssT7r4T8AH68fHiGHqHZ4 tIX63fJYuZzm+IYwKrOiQjt/sn/dFamytujzEP0CyDf2gp3sfEJGv4urqODJ/IiI56fe PDU5m+BSKeXYzBvHMzOFnC0/iJkvqF7gcVvnt6T+eP78SJ1KryA6nAufSGqx9Z/WZDs/ LRCNGBBlEo+b/NeyjRn4i72g8D3ZwKygARfX1Bxp/w/PzGh1rQqTZ11YV1ZHjJUnXwme yleA== X-Gm-Message-State: ANhLgQ14KrrmvaXEFt14rUVY0mXcXGRH6PplCLlFggJe1rtEhAAtV4oE aL53T1Eba0oN+weucys2tWuy4A== X-Received: by 2002:a17:90a:9af:: with SMTP id 44mr260267pjo.160.1583772962532; Mon, 09 Mar 2020 09:56:02 -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 cm2sm104013pjb.23.2020.03.09.09.56.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Mar 2020 09:56:02 -0700 (PDT) From: Zong Li To: palmer@dabbelt.com, paul.walmsley@sifive.com, aou@eecs.berkeley.edu, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Zong Li Subject: [PATCH v3 6/9] riscv: add STRICT_KERNEL_RWX support Date: Tue, 10 Mar 2020 00:55:41 +0800 Message-Id: <8359c2289e12d54a34b69e95842f8fc219343460.1583772574.git.zong.li@sifive.com> X-Mailer: git-send-email 2.25.1 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 54437d7662a5..5fbae6380b32 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -62,6 +62,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.25.1