From patchwork Sun Nov 22 03:57:07 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 7675321 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id CA0F59F2E2 for ; Sun, 22 Nov 2015 04:00:22 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id C541520615 for ; Sun, 22 Nov 2015 04:00:21 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id ED96C205F9 for ; Sun, 22 Nov 2015 04:00:20 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1a0Ln2-0006KD-Pz; Sun, 22 Nov 2015 03:58:12 +0000 Received: from mga14.intel.com ([192.55.52.115]) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1a0Lml-0005Wv-QK for linux-arm-kernel@lists.infradead.org; Sun, 22 Nov 2015 03:57:58 +0000 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga103.fm.intel.com with ESMTP; 21 Nov 2015 19:57:36 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.20,331,1444719600"; d="scan'208";a="844473132" Received: from dwillia2-desk3.jf.intel.com ([10.54.39.39]) by fmsmga001.fm.intel.com with ESMTP; 21 Nov 2015 19:57:36 -0800 Subject: [PATCH 2/2] restrict /dev/mem to idle io memory ranges From: Dan Williams To: linux-kernel@vger.kernel.org Date: Sat, 21 Nov 2015 19:57:07 -0800 Message-ID: <20151122035707.9313.59919.stgit@dwillia2-desk3.jf.intel.com> In-Reply-To: <20151122035702.9313.52457.stgit@dwillia2-desk3.jf.intel.com> References: <20151122035702.9313.52457.stgit@dwillia2-desk3.jf.intel.com> User-Agent: StGit/0.17.1-9-g687f MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20151121_195755_975606_4A754CBF X-CRM114-Status: GOOD ( 14.13 ) X-Spam-Score: -7.5 (-------) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arch@vger.kernel.org, Russell King , Kees Cook , Arnd Bergmann , linux-nvdimm@lists.01.org, Greg Kroah-Hartman , Andrew Morton , linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.8 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This effectively promotes IORESOURCE_BUSY to IORESOURCE_EXCLUSIVE semantics by default. If userspace really believes it is safe to access the memory region it can also perform the extra step of disabling an active driver. This protects device address ranges with read side effects and otherwise directs userspace to use the driver. Persistent memory presents a large "mistake surface" to /dev/mem as now accidental writes can corrupt a filesystem. Cc: Arnd Bergmann Cc: Kees Cook Cc: Russell King Cc: Andrew Morton Cc: Greg Kroah-Hartman Signed-off-by: Dan Williams Acked-by: Kees Cook --- kernel/resource.c | 3 +++ lib/Kconfig.debug | 23 ++++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/kernel/resource.c b/kernel/resource.c index f150dbbe6f62..03a8b09f68a8 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -1498,6 +1498,9 @@ int iomem_is_exclusive(u64 addr) break; if (p->end < addr) continue; + if (IS_ENABLED(CONFIG_IO_STRICT_DEVMEM) + && p->flags & IORESOURCE_BUSY) + break; if (p->flags & IORESOURCE_BUSY && p->flags & IORESOURCE_EXCLUSIVE) { err = 1; diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index ad85145d0047..be47f99fb191 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -1866,9 +1866,26 @@ config STRICT_DEVMEM enabled, even in this case there are restrictions on /dev/mem use due to the cache aliasing requirements. + If this option is switched on, and IO_STRICT_DEVMEM=n, the /dev/mem + file only allows userspace access to PCI space and the BIOS code and + data regions. This is sufficient for dosemu and X and all common + users of /dev/mem. + + If in doubt, say Y. + +config IO_STRICT_DEVMEM + bool "Filter I/O access to /dev/mem" + depends on STRICT_DEVMEM + default STRICT_DEVMEM + ---help--- + If this option is disabled, you allow userspace (root) access to all + io-memory regardless of whether a driver is actively using that + range. Accidental access to this is obviously disastrous, but + specific access can be used by people debugging kernel drivers. + If this option is switched on, the /dev/mem file only allows - userspace access to PCI space and the BIOS code and data regions. - This is sufficient for dosemu and X and all common users of - /dev/mem. + userspace access to *idle* io-memory ranges (see /proc/iomem) This + may break traditional users of /dev/mem (dosemu, legacy X, etc...) + if the driver using a given range cannot be disabled. If in doubt, say Y.