Skip to content

Commit 90a545e

Browse files
committed
restrict /dev/mem to idle io memory ranges
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. In general if a device driver is busily using a memory region it already informs other parts of the kernel to not touch it via request_mem_region(). /dev/mem should honor the same safety restriction by default. Debugging a device driver from userspace becomes more difficult with this enabled. Any application using /dev/mem or mmap of sysfs pci resources will now need to perform the extra step of either: 1/ Disabling the driver, for example: echo <device id> > /dev/bus/<parent bus>/drivers/<driver name>/unbind 2/ Rebooting with "iomem=relaxed" on the command line 3/ Recompiling with CONFIG_IO_STRICT_DEVMEM=n Traditional users of /dev/mem like dosemu are unaffected because the first 1MB of memory is not subject to the IO_STRICT_DEVMEM restriction. Legacy X configurations use /dev/mem to talk to graphics hardware, but that functionality has since moved to kernel graphics drivers. Cc: Arnd Bergmann <arnd@arndb.de> Cc: Russell King <linux@arm.linux.org.uk> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Acked-by: Kees Cook <keescook@chromium.org> Acked-by: Ingo Molnar <mingo@redhat.com> Signed-off-by: Dan Williams <dan.j.williams@intel.com>
1 parent 21266be commit 90a545e

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

‎kernel/resource.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,8 +1498,15 @@ int iomem_is_exclusive(u64 addr)
14981498
break;
14991499
if (p->end < addr)
15001500
continue;
1501-
if (p->flags & IORESOURCE_BUSY &&
1502-
p->flags & IORESOURCE_EXCLUSIVE) {
1501+
/*
1502+
* A resource is exclusive if IORESOURCE_EXCLUSIVE is set
1503+
* or CONFIG_IO_STRICT_DEVMEM is enabled and the
1504+
* resource is busy.
1505+
*/
1506+
if ((p->flags & IORESOURCE_BUSY) == 0)
1507+
continue;
1508+
if (IS_ENABLED(CONFIG_IO_STRICT_DEVMEM)
1509+
|| p->flags & IORESOURCE_EXCLUSIVE) {
15031510
err = 1;
15041511
break;
15051512
}

‎lib/Kconfig.debug‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,9 +1869,26 @@ config STRICT_DEVMEM
18691869
enabled, even in this case there are restrictions on /dev/mem
18701870
use due to the cache aliasing requirements.
18711871

1872+
If this option is switched on, and IO_STRICT_DEVMEM=n, the /dev/mem
1873+
file only allows userspace access to PCI space and the BIOS code and
1874+
data regions. This is sufficient for dosemu and X and all common
1875+
users of /dev/mem.
1876+
1877+
If in doubt, say Y.
1878+
1879+
config IO_STRICT_DEVMEM
1880+
bool "Filter I/O access to /dev/mem"
1881+
depends on STRICT_DEVMEM
1882+
default STRICT_DEVMEM
1883+
---help---
1884+
If this option is disabled, you allow userspace (root) access to all
1885+
io-memory regardless of whether a driver is actively using that
1886+
range. Accidental access to this is obviously disastrous, but
1887+
specific access can be used by people debugging kernel drivers.
1888+
18721889
If this option is switched on, the /dev/mem file only allows
1873-
userspace access to PCI space and the BIOS code and data regions.
1874-
This is sufficient for dosemu and X and all common users of
1875-
/dev/mem.
1890+
userspace access to *idle* io-memory ranges (see /proc/iomem) This
1891+
may break traditional users of /dev/mem (dosemu, legacy X, etc...)
1892+
if the driver using a given range cannot be disabled.
18761893

18771894
If in doubt, say Y.

0 commit comments

Comments
 (0)