Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp2223656ybh; Mon, 9 Mar 2020 01:23:38 -0700 (PDT) X-Google-Smtp-Source: ADFU+vuEDMecxA0HfY6Sxje9MPbhZtksyoswIGd6hPqvQ3bYWRiGSNnN6ZGB4kmHPZpfdkgkNthr X-Received: by 2002:aca:5044:: with SMTP id e65mr10728361oib.28.1583742218260; Mon, 09 Mar 2020 01:23:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583742218; cv=none; d=google.com; s=arc-20160816; b=QLKEWUBA8ZWT1z1wku6qjXVUiS63S/8yBaMB7XIn7QNqMTbWyHz2q7LORJP0uc9cCh RJVK8FaRzREMzlE5nTlvInoTGl2c7R+wGE/fKlNAc39zEcu+EQ8t7PtQqKwubprugAKM LTTyTEeIjFBXHNaXcaoYyzOQH8501651UUHlmiIEBHU9oPhVTkpDxN4uct4bMHGjZHVB mYmkls+KryhWYKtFp2o0WFU/99HovChkxSVdCuHBLEN7EV8imDP9Ag5+/GdaEWoxi2w9 Ixyd8ou7KUSPHr6d/V+L77sOGex+/dqI0t7S2GbjVpNUyfls9BJj3A/H4lbC1tjBg+9a D8fQ== 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=Jk+6Ai0zQ5+yGQN7AMCqCE/JDR6IbzEcXQt+dOJB8WI=; b=MWCrDV7+xHfHghGquL59K7QeFXCM/vrKfuEYSP91EhhsXyVANRZqTlWQVaKZOHFO5a P85KFRcqmTElGVl+i7qBcx3/JhKJIFpMVacq/Q9pYbA3OGiqmJkm+KlMTq4kR58weSiG eauPwEPHOnH9z8P0R2KYJQqmtYHmCD1PCwp4hBA6w0Esl0kOq/Tsf9H2CGlD7Pp1HINX r5rK3PCR6RE4Old56hGRcgr4VyZDwB2nkbopBnMSwsUSVIuMJdi9Zubf8I/fLR3VT06o LncJwMl2KTJ7yv+xhv3uAvYGmKy6BXxLGfCi/9q6dq4xizaa5gfPxJ9dgdo4AECVoapF A6Ow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=fVAQJLcB; 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 g11si474629otq.309.2020.03.09.01.23.27; Mon, 09 Mar 2020 01:23:38 -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=fVAQJLcB; 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 S1726621AbgCIIWy (ORCPT + 99 others); Mon, 9 Mar 2020 04:22:54 -0400 Received: from mail-pl1-f193.google.com ([209.85.214.193]:33800 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726604AbgCIIWu (ORCPT ); Mon, 9 Mar 2020 04:22:50 -0400 Received: by mail-pl1-f193.google.com with SMTP id a23so984074plm.1 for ; Mon, 09 Mar 2020 01:22:49 -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=Jk+6Ai0zQ5+yGQN7AMCqCE/JDR6IbzEcXQt+dOJB8WI=; b=fVAQJLcBuAarKhkTLGiY/MTte975yy67x3a7nUDmF4mkzDjt6o3dy8EIh77gfD4hzG sE/62YaH+XwsdN+emghA8rxvQZRehXhZnBgIleErSQO3B34aw4F/KpqeYw0y6042HvYT FxykW2M/abiQ3pX3bS61STAoJIMsB81Ip0sSmUkVDArjAnYXVSQqGQP73Xi8xss0g8jD pVl4O8zzupKD/ra3KWi9vJcQhDg3xAIeLq2mrdxCJo1Mvobz4LjTV94STWCOU/QYvTJh SMDGJHinzP6+ES8n9FLQerccPRtfVQnj73kEWfCS+NjpIiepMMUKkvXcy97ImZJ7Uscc b84A== 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=Jk+6Ai0zQ5+yGQN7AMCqCE/JDR6IbzEcXQt+dOJB8WI=; b=ZxKFmqjgZWYqB03HOeEu11/VdH8aQ8uIW7U4YH8If3D0x05KZkE9rYIWJrk8O5vZjB DM5vFBLXy6tCqwwVBJ/nHFjbBsJsvujt5jjwiHDUl14EMjk+nhFuC5mGcJqjF3QnTzas FObRQnnsZrr0MaHA59E9hpvtX1xJNgcAvpTm/LejGdHONt0Gc8UUaSlNnfs6t43+P5JK pREP741gfGvPFcme0+97us8BbxygTv7dyOtxbpVV9katoTfMsmySJ2M1XHQHLjPLwQfs 1N94Jc8LiKCdqhQiksSrpq4g71skP7V1/Kci5F1buCIhnHBqnvba06TVGOigChDhUr7B GImA== X-Gm-Message-State: ANhLgQ0Zns+efL8CnZxwIBl2JopOczmZCH2TfMD4RmxmXXL1TkBGjLyj wO/PMaKUFECJIuoFe6ylUwjKAg== X-Received: by 2002:a17:902:c214:: with SMTP id 20mr15571049pll.0.1583742169463; Mon, 09 Mar 2020 01:22:49 -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 v5sm18364779pfn.64.2020.03.09.01.22.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Mar 2020 01:22:48 -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 v2 6/9] riscv: add STRICT_KERNEL_RWX support Date: Mon, 9 Mar 2020 16:22:26 +0800 Message-Id: <074b7da48f0b651f218346ef59cfdc89ac098503.1583741997.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 | 45 +++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index f524d7e60648..308a4dbc0b39 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 a91f192063c2..d3076087cb34 100644 --- a/arch/riscv/include/asm/set_memory.h +++ b/arch/riscv/include/asm/set_memory.h @@ -15,6 +15,14 @@ int set_memory_rw(unsigned long addr, int numpages); int set_memory_x(unsigned long addr, int numpages); int set_memory_nx(unsigned long addr, int numpages); +#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 965a8cf4829c..0d489cb51d5a 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -12,11 +12,13 @@ #include #include #include +#include #include #include #include #include +#include #include #include "../kernel/head.h" @@ -477,6 +479,49 @@ static void __init setup_vm_final(void) csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | SATP_MODE); local_flush_tlb_all(); } + +#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 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) { -- 2.25.1