Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp4453079ybb; Tue, 7 Apr 2020 07:48:20 -0700 (PDT) X-Google-Smtp-Source: APiQypIAkuHqN2wH6Q1hXxni4pdBW63rtHLXQV+0ZFZab1cJE1af0vvNQ/qJcS52z1rZ4Q5dCQ+C X-Received: by 2002:a9d:6c45:: with SMTP id g5mr1870416otq.347.1586270900134; Tue, 07 Apr 2020 07:48:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586270900; cv=none; d=google.com; s=arc-20160816; b=yBIFTeoXC4GpTNnnVLN5Ot+KkoH9bLZ0SH6Q3MbT4XbzG27qhCwLNuxFgyLX6ak10E FX3gatZzlV6zf7lYSp6dH9YxIgEgJ641PFmBkKmv6N05Y3tVP0yr90LxbuZP/a7J7fqx rh4rQ9Vp2xz+Nl0/E2TyEbyozf8i64sY6Q1oq8XADTbSJ3/JKm9g+SwGpx1mK2V+4axK wNf81dj3dFolIv6q/rUY8k6Jw709QA3PoxQRP/bEe3J+Znt1p5F837/bJPFvggN6SszP iSnp0rK5ciMQtY/gD7oUl7U9/n7w5qZUk32kdz/BnxWYaTrrQ2DR2a4sMFjYVuALoIAM exiw== 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=fODgGkkN2asWfej+DQJrSZ5Gc6KZjeMUjqXWswxbmEppjnBatZOFE9jjTOtdOJWb0x qON1G5m09hv9R6qdicf5ljw5G+Q1MH90EjvVVG9qGRJDKFzSCstsqnyvhhr2r+ZpYetl e/SM/2Px71BuPAZneOg1fPE3aoeOlQLO877FUyq8/LHKDh6ScwXqEwODz2pnBWXN5Rsw rsVzHSE27s5GAvgQCu8l5vz/rmPSYAb+IqPUle1i3qDiCco/cqln6vHJSj0rwkP+OJ6N 0EAPlwalMsVdH6i665IQ2pqxChmazi72na3a4Vv+C0VPltohAakZQQOAl17WEGjAjyAv ISTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=TtvzXxSJ; 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 d12si1350393oom.31.2020.04.07.07.48.07; Tue, 07 Apr 2020 07:48:20 -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=TtvzXxSJ; 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 S1729376AbgDGOrU (ORCPT + 99 others); Tue, 7 Apr 2020 10:47:20 -0400 Received: from mail-pj1-f65.google.com ([209.85.216.65]:38371 "EHLO mail-pj1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729348AbgDGOrS (ORCPT ); Tue, 7 Apr 2020 10:47:18 -0400 Received: by mail-pj1-f65.google.com with SMTP id m15so856379pje.3 for ; Tue, 07 Apr 2020 07:47:17 -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=TtvzXxSJ0vpaB0ZG++G2fkPig6AtFEsttCjqY2Xvv9MaF0/H7Z/pUc+9b4ouCtetSj 4120O3V+N+4RGONMM6wYETKEPPwBodh7xtC8Aeat9tSSrhoRmifL+6stENNVtQwsGgn4 4qdDIhygGHzmwFk2y8DpRVDHh5WpmIDnqHRLSWxY83gDpKpJjWbZh7S7P9kq1Ks1kXxI b3Qe2gvh2ejACDVV9xe+XJV40SBNBlygiUBuFR/PU+9KbIRTWi5fg15zt3eVXvMTByU+ xzW7/AEiuPvSp7AYkSo4O/lwLVRR/E/a40w/7WqVXe6vnpHdE8iaICEgw4NRtXQ3zqjW W6Gg== 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=CUIvox9mXUXQChntcXYB3laa11VLec78jN03DypySeHanexTj6PFoPBfGDOoWr6cFt 6EC2z/24wDFwuNkrKjmss++Zp4tCRztal7z7k11eGgAY55/JRA9mfubKTQuC2XYJjXiO cttpuCv+huRaUDg/0ApjHm+KjGeaU6EqScK0b2i89A6g04r6W/aaBUhYpdTP5E3fhVjM cah7+EMqX/z5/sfrzjKy930qdydrFBiuEmpcdU0i/54bQQWTDDW4gL1iS9edOvGWoaqP 8ZlcaAIZk1jFPo+qwv1koGmxdIBGFiSfd2/FTXM0BA7tAHfAlQ8jcOSVpsdnCPEd7kjV 3iIw== X-Gm-Message-State: AGi0PuYrkG8utcpggfZMJnj4vtu1+uCVE1aOvRU2PzVo94qd6pXKTMEO wDZpYBgQYoJqMv4zvr/K0EI/HA== X-Received: by 2002:a17:90a:c207:: with SMTP id e7mr3190940pjt.117.1586270837136; Tue, 07 Apr 2020 07:47:17 -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 y15sm14190093pfc.206.2020.04.07.07.47.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Apr 2020 07:47:16 -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 v4 9/9] riscv: add STRICT_KERNEL_RWX support Date: Tue, 7 Apr 2020 22:46:54 +0800 Message-Id: <933a02ac01c1ac2a5c174474d46da12e8db96d63.1586265122.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