Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp2514608ybt; Sun, 21 Jun 2020 23:36:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxF0VhJZg3PZ78yqNEls46fw97r1t2sFQKWFtR1BdX6i8ypjgPCDfvjl1TsUsm0n8r93vg0 X-Received: by 2002:a17:906:fcb7:: with SMTP id qw23mr13509944ejb.229.1592807811018; Sun, 21 Jun 2020 23:36:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592807811; cv=none; d=google.com; s=arc-20160816; b=kn2JuaM6BqgDSHKqjHHpwfQIRltuagAkLckDWm5XaqWo91hmS39qAeLiM3LkjQr4VP qfxR/ELnjqaGBNBlC/L54tJ0DmC8198T8G8rTQljeXA4UOpkxTlHt44FRpgoYpxGfS5y qTs2Vz4ezA7fdcwt27E9/PLNdMVM560RuT584YVFs9+UlbjTCEsHa/WezPjirNIfcuP5 J8GIJnRGzEQwD2DiBMJ9KVIUFfpEnKPRNH5+cSZCJbVoVOvzAPwM1Hi1TtAfnO2NXwv3 ejc+g2NjAqqNfYnoWRQpvBki2+hLp54La1Ez+3NJHenh5EFbW2ROE5bmwoyPdS16SH75 73xw== 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=ezv4JnfVR6LWgBcc7PypHNj9Qesz1Vb4pgdtDvjBS6U=; b=k7PiIn7dBcqvZPoGypajOrmNn72g1hwWVWc/0QvTPGt7DC5Vxj81c05asZaJFQur/E uvPw6++UHZEFIGWsbT+ql/za2LuLGVzfjM4nvtC5ULbYrrs4I3ffZO9o/WTR+wm6pICc yYyBtn3G8nCAbl/2VKqeW7XIE4Ouj1aTER8TF+jdToJd5YP9d8pnsmL5hf2C2JPFzIsM SFSRLY3+iPaAppthjyWHn92E1lwvbS72Aj9ywKUeAJWSfoQAo+T9VdU2FxBOSC8P9/l8 j68EX+HD1Kly36bZuy7ReaYVgK8ap/1ynwgmkp8cKgmmYLV/HooYPzS88ejQMO30OJ7n gNrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=De6eVo+E; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n23si8968342eje.711.2020.06.21.23.36.28; Sun, 21 Jun 2020 23:36:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=De6eVo+E; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731258AbgFVGd6 (ORCPT + 99 others); Mon, 22 Jun 2020 02:33:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726686AbgFVGd4 (ORCPT ); Mon, 22 Jun 2020 02:33:56 -0400 Received: from mail-pj1-x1044.google.com (mail-pj1-x1044.google.com [IPv6:2607:f8b0:4864:20::1044]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 631C6C061794 for ; Sun, 21 Jun 2020 23:33:56 -0700 (PDT) Received: by mail-pj1-x1044.google.com with SMTP id b7so6301136pju.0 for ; Sun, 21 Jun 2020 23:33:56 -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=ezv4JnfVR6LWgBcc7PypHNj9Qesz1Vb4pgdtDvjBS6U=; b=De6eVo+EUy6FB1RenXpF+tB/HeWze+V+ZVIbaffFxDVW8f2PVwswMIQC2ZsQPNPBkn fmwOdBvg3XxjmYfOKrDO1e9Z2fjaorlzvuP9n4KWdmYTPxfAYGBIE/36wEW6MKNYMJzK 6FPckM7xvYCVCBBrhKgNX/tu6ebOD1oAhXUCMWHteJKpDxUXMHn/zhgJhz4gP2rKqICw djiuC0vKWG5JeXeSrBIajmnE6b6iskFFEzi4s8LSVy7xtUXEmTpA0113rdjUZVV7kJMa 88UemmoEAr3AaTekjmUk2I/dZsSWlR5D4B9k4X79uwE5zp2Dd090+uWNSqJe+BBB2FvO C0hA== 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=ezv4JnfVR6LWgBcc7PypHNj9Qesz1Vb4pgdtDvjBS6U=; b=hOAzSTcYjMQ+vBQd5bvqBd9MQ/2x8llzFZamJIJSy6SNpeyQWFFENOM2s8rpKzb2Q4 3YycBBzII7Ul6zvbBpRp4VrozYscQXJtoXN1pgelp20/ekb4b6mOqK/rh+iKfaO5gxSD /jzVHNl0sdHQcqwuF45MnCZEw4U9SmIsVe4ZZgpRuysDHq3IPuMcCXj0rLi3Z56G25gU eu/cEwIAdPhY/xAYSVxm98NpDp4t0/q/YLuLd+/q+2P/2jxNAqek2iEToQYyaFjlZHcd mdziY9IGMxusLr8eO8F8WNMnMeWTdrn14347Z5cbqgaebHrBdq8PC1tWndMQoW7wlTFo EKZQ== X-Gm-Message-State: AOAM532Iz67tng3yQivfLGVobQeL9SI2pG2ofJQ2emocE2UhlEA1YgdQ LUp7zxecI6T1ApgBlxlBuOAf6w== X-Received: by 2002:a17:90b:796:: with SMTP id l22mr15316991pjz.45.1592807635928; Sun, 21 Jun 2020 23:33:55 -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 w11sm12530999pfi.93.2020.06.21.23.33.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jun 2020 23:33:55 -0700 (PDT) From: Zong Li To: paul.walmsley@sifive.com, palmer@dabbelt.com, mick@ics.forth.gr, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Zong Li Subject: [PATCH v2 1/2] riscv: Support CONFIG_STRICT_DEVMEM Date: Mon, 22 Jun 2020 14:33:49 +0800 Message-Id: <07db05fd125ce591cc773d03d994184ddbc7dee2.1592807136.git.zong.li@sifive.com> X-Mailer: git-send-email 2.27.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 Implement the 'devmem_is_allowed()' interface for RISC-V, like some of other architectures have done. It will be called from range_is_allowed() when userpsace attempts to access /dev/mem. Access to exclusive IOMEM and kernel RAM is denied unless CONFIG_STRICT_DEVMEM is set to 'n'. Test it by devmem, the result as follows: - CONFIG_STRICT_DEVMEM=y $ devmem 0x10010000 0x00000000 $ devmem 0x80200000 0x0000106F - CONFIG_STRICT_DEVMEM is not set $ devmem 0x10010000 devmem: mmap: Operation not permitted $ devmem 0x80200000 devmem: mmap: Operation not permitted Changes in v2: - Move implementations out from mm/init.c. Suggested by Nick Kossifidis. Signed-off-by: Zong Li --- arch/riscv/Kconfig | 1 + arch/riscv/include/asm/io.h | 2 ++ arch/riscv/mm/Makefile | 1 + arch/riscv/mm/mmap.c | 24 ++++++++++++++++++++++++ 4 files changed, 28 insertions(+) create mode 100644 arch/riscv/mm/mmap.c diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 089293e4ad46..c6569939f7ba 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -17,6 +17,7 @@ config RISCV select ARCH_HAS_BINFMT_FLAT select ARCH_HAS_DEBUG_VIRTUAL if MMU select ARCH_HAS_DEBUG_WX + select ARCH_HAS_DEVMEM_IS_ALLOWED select ARCH_HAS_GCOV_PROFILE_ALL select ARCH_HAS_GIGANTIC_PAGE select ARCH_HAS_MMIOWB diff --git a/arch/riscv/include/asm/io.h b/arch/riscv/include/asm/io.h index 3835c3295dc5..04ac65ab93ce 100644 --- a/arch/riscv/include/asm/io.h +++ b/arch/riscv/include/asm/io.h @@ -147,4 +147,6 @@ __io_writes_outs(outs, u64, q, __io_pbr(), __io_paw()) #include +extern int devmem_is_allowed(unsigned long pfn); + #endif /* _ASM_RISCV_IO_H */ diff --git a/arch/riscv/mm/Makefile b/arch/riscv/mm/Makefile index 363ef01c30b1..4530ad52f1d6 100644 --- a/arch/riscv/mm/Makefile +++ b/arch/riscv/mm/Makefile @@ -10,6 +10,7 @@ obj-y += extable.o obj-$(CONFIG_MMU) += fault.o pageattr.o obj-y += cacheflush.o obj-y += context.o +obj-y += mmap.o ifeq ($(CONFIG_MMU),y) obj-$(CONFIG_SMP) += tlbflush.o diff --git a/arch/riscv/mm/mmap.c b/arch/riscv/mm/mmap.c new file mode 100644 index 000000000000..095fc82897b1 --- /dev/null +++ b/arch/riscv/mm/mmap.c @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2020 SiFive + */ + +#ifdef CONFIG_STRICT_DEVMEM +#include + +/* + * devmem_is_allowed() checks to see if /dev/mem access to a certain address + * is valid. The argument is a physical page number. + * + * Disallow access to system RAM as well as device-exclusive MMIO regions. + * This effectively disable read()/write() on /dev/mem. + */ +int devmem_is_allowed(unsigned long pfn) +{ + if (iomem_is_exclusive(pfn << PAGE_SHIFT)) + return 0; + if (!page_is_ram(pfn)) + return 1; + return 0; +} +#endif -- 2.27.0