Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp2254651ybt; Tue, 16 Jun 2020 00:49:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw0ZtZmSSUHNyx7tgUmniZFZM9HNcvOtb9Hjf68Bczo/w1Dhp25lcq+XoNursjAvbiQI8KS X-Received: by 2002:a17:907:207b:: with SMTP id qp27mr1707640ejb.532.1592293779148; Tue, 16 Jun 2020 00:49:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592293779; cv=none; d=google.com; s=arc-20160816; b=rrPaMCmfE81Dg8YfqXI5pVhZ+Kai7QkQfuTQ1FmBnZ/iHFIhVVSgXKcgZP/TQn+E1+ 9eHBov8msZaJLtJz0VGTje5kOjFZSiD8oH/dj7rVSF6Q541NnZ9uGWlHl2y2b2S15gK2 PN9PBa0mjNrea4/FihKaCiAGBlka0EDZ5pamHjZclE6VdQ+Hxlcg85ldNZC4NFHkV4z1 XOqS5sKfQjkGv1ZnxONMB2GWlhYvdgrdOgtiSGbC8TwHhrgrWaI9GUXigy0UPfW3K6bw lFCb5q3ffKBay8rrwzO+58u7qC/T+z4fWLzxLjJ9ZOApMKk0u3+lGX8dqt5gCvAuQRdL lDPQ== 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=vV7PDthSiPR3LChm1dR2y1ass0+TcfMiVV19frp0t7U=; b=y+i62sSygBtw5IQPaHYVK5uTt2wSHiIbq2A2MDn0D3NFsR/TDYpcklnv1mF6IhVDyL OKDHVAlMoOpJsNGfwhYoaH2dmZAq4xJe5rMr2evkhsa2yPpg5OEcvJRDetRM/LlH7Wx7 iU5SmxiD1oujbeEpOgMqbtIiTcVfbm/55vGLYn++OGEXGSSkzxOEjY0By2wbC7XUFrpU UiPQ0wFRiWYau5pRSckwn6WOUi89R4b2j9U19v4JavS+nw/3kSWkx5j8C8SNKLTotZZz lh+LrXGQ03p/NxbbWkV//+uzSMfUo2a2kJIbyw4L/fmegJOAhs1NwfIbanZFqh/yvDH5 shoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=VJHC11Uq; 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 yd12si10406682ejb.237.2020.06.16.00.49.17; Tue, 16 Jun 2020 00:49:39 -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=VJHC11Uq; 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 S1727097AbgFPHp5 (ORCPT + 99 others); Tue, 16 Jun 2020 03:45:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726259AbgFPHp4 (ORCPT ); Tue, 16 Jun 2020 03:45:56 -0400 Received: from mail-pl1-x643.google.com (mail-pl1-x643.google.com [IPv6:2607:f8b0:4864:20::643]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C378FC03E96A for ; Tue, 16 Jun 2020 00:45:55 -0700 (PDT) Received: by mail-pl1-x643.google.com with SMTP id k6so3058746pll.9 for ; Tue, 16 Jun 2020 00:45:55 -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=vV7PDthSiPR3LChm1dR2y1ass0+TcfMiVV19frp0t7U=; b=VJHC11UqQBI1FGJ+MeKOiWKzMZGBTEHwNKV3OXnRsqMaz6M/rTt7pvyn+uVO+MYabl iaSOBJwnznJ+7y6uHyDg4fqR8po25vKF0mvJjZDEgyajtVYm1mLTp4Vl8hwuRcNA3r1M SiolWjgx/7vQjDLvauayB86aTzSmQoPqZdAQCLjaM0Zplg64t7mLe0iDxiP9Un6Y1QIY vB2XGsQm/zuZPXi3yIGPUaPKviKVm0IBx/uEKmBbmcwwSxp7La/sKBU3bXoXIXLNIWIv jEnxWscZ62T1Ym1ZosQcPG6lVZcy/+sW611lSpjlRhFs19m0bxs/Yh+Iun86+OOP3p50 iuGA== 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=vV7PDthSiPR3LChm1dR2y1ass0+TcfMiVV19frp0t7U=; b=VdueKA8e06QuS9nCyxuPnT+AC9o/lYPlUlV7+k2xCgOAjYF8p9S7lHMYhiDI5zyC2b Z9jD2QthyM8+v8l4+tvwY5/kNkQUn+FRnKw50IJ7rjmJJXrEd+9h/PF9AVqK1fOQ3G/Q TNpUIHAy4sZHT9g4WashlG1Z/goR5Xjqvvgrde1q6hIcucVkEuj60WpODHiHlvt4H9Gu SmgxNtA10SRkVksW8XVK3zDt6XxW8fWNSgOj5i2C4CPcwv3DL4knWhTrZlFzkmd3TRRl oX7S28smLmeqHNp2G/lDsKihx5IBM9dSXiMvUQR8RndcxisHAIs3hjXAF5X7PzGqv/fz FFPg== X-Gm-Message-State: AOAM530lOGaJFdJZ47NgNEZpSvl3FCNcmTYr8fFH1jZjwyb2GW++LBRE KAVoilxNQWe/SUkFVllEhHo+Xw== X-Received: by 2002:a17:90a:9f81:: with SMTP id o1mr1578412pjp.139.1592293555400; Tue, 16 Jun 2020 00:45: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 i22sm15948250pfo.92.2020.06.16.00.45.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Jun 2020 00:45:55 -0700 (PDT) From: Zong Li To: paul.walmsley@sifive.com, palmer@dabbelt.com, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Zong Li Subject: [PATCH 2/2] riscv: Support CONFIG_STRICT_DEVMEM Date: Tue, 16 Jun 2020 15:45:47 +0800 Message-Id: <7faa60aa4a606b5c5c1ae374d82a7eee6c764b38.1592292685.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 Signed-off-by: Zong Li --- arch/riscv/Kconfig | 1 + arch/riscv/include/asm/io.h | 2 ++ arch/riscv/mm/init.c | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 128192e14ff2..ffd7841ede4c 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -16,6 +16,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/init.c b/arch/riscv/mm/init.c index bbe816e03b2f..5e7e61519acc 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -517,6 +517,25 @@ void mark_rodata_ro(void) } #endif +#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 + void __init resource_init(void) { struct memblock_region *region; -- 2.27.0