]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
restrict /dev/mem to idle io memory ranges
authorDan Williams <dan.j.williams@intel.com>
Mon, 23 Nov 2015 23:49:03 +0000 (15:49 -0800)
committerDan Duval <dan.duval@oracle.com>
Wed, 7 Dec 2016 17:25:36 +0000 (12:25 -0500)
Orabug: 22913653

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>
(cherry picked from commit 90a545e981267e917b9d698ce07affd69787db87)
Signed-off-by: Dan Duval <dan.duval@oracle.com>
kernel/resource.c
lib/Kconfig.debug

index 249b1eb1e6e1381c6963296db8c408f99a01b3b5..3669d1bfc4254213e0e42dacab374624d74cdd5c 100644 (file)
@@ -1499,8 +1499,15 @@ int iomem_is_exclusive(u64 addr)
                        break;
                if (p->end < addr)
                        continue;
-               if (p->flags & IORESOURCE_BUSY &&
-                    p->flags & IORESOURCE_EXCLUSIVE) {
+               /*
+                * A resource is exclusive if IORESOURCE_EXCLUSIVE is set
+                * or CONFIG_IO_STRICT_DEVMEM is enabled and the
+                * resource is busy.
+                */
+               if ((p->flags & IORESOURCE_BUSY) == 0)
+                       continue;
+               if (IS_ENABLED(CONFIG_IO_STRICT_DEVMEM)
+                               || p->flags & IORESOURCE_EXCLUSIVE) {
                        err = 1;
                        break;
                }
index af7bec643ae41bda50b7f84773abada21e5b8ea2..e911f7c9b014cdef2bdb4e6eb8c5900b9c290dca 100644 (file)
@@ -1793,9 +1793,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.