Skip to contentSkip to conversation

sparc: Fail userspace mmap for disabled PCI I/O and MEM BARs.

PATCH In review Liam R. Howlett 2 participants 3 messages Dec 21, 2016 SPARC + UltraSPARC (sparc/sparc64)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sparc: Fail userspace mmap for disabled PCI I/O and MEM BARs.

Check if the PCI memory starting address is disabled and return -EINVAL. This will have minimal impact to performance as it is only during the set up of the mmap and will avoid consequences of a failed load/store.

---
 arch/sparc/kernel/pci.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
arch/sparc/kernel/pci.c +42 -0
index 9c1878f..208d7c0 100644
@@ -20,6 +20,7 @@
 #include <linux/init.h>
 #include <linux/of.h>
 #include <linux/of_device.h>
+#include <linux/types.h>
 
 #include <asm/uaccess.h>
 #include <asm/pgtable.h>
@@ -854,6 +855,44 @@ static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vm
 	/* Our io_remap_pfn_range takes care of this, do nothing.  */
 }
 
+/* Ensure the mmap address range is within an enabled resource range. */
+int __pci_mmap_page_invalid(struct pci_dev *dev, unsigned long pfn)
+{
+	int i;
+	unsigned short cmd;
+	phys_addr_t page_start = pfn << PAGE_SHIFT;
+
+	pci_read_config_word(dev, PCI_COMMAND, &cmd);
+	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
+		struct resource *res = &dev->resource[i];
+
+		/* Check if this is the resource in the range.
+		 * Note: Size is not checked, so running off the end of an
+		 * enabled block into a disabled block will still result in
+		 * issues.
+		 */
+		if (res->start <= page_start && res->end >= page_start) {
+			/* Not initialized */
+			if (!res->flags)
+				return -EINVAL;
+
+			/* IO Resource disabled */
+			if ((res->flags & IORESOURCE_IO) &&
+			    !(cmd & PCI_COMMAND_IO))
+				return -EINVAL;
+
+			/* Memory Resource disabled */
+			if ((res->flags & IORESOURCE_MEM) &&
+			    !(cmd & PCI_COMMAND_MEMORY))
+				return -EINVAL;
+
+			/* Looks good */
+			return 0;
+		}
+	}
+	return -EINVAL;
+}
+
 /* Perform the actual remap of the pages for a PCI device mapping, as appropriate
  * for this architecture.  The region in the process to map is described by vm_start
  * and vm_end members of VMA, the base physical address is found in vm_pgoff.
@@ -872,6 +911,9 @@ int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
 	if (ret < 0)
 		return ret;
 
+	if (__pci_mmap_page_invalid(dev, vma->vm_pgoff))
+		return -EINVAL;
+
 	__pci_mmap_set_pgprot(dev, vma, mmap_state);
 
 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Signature · 1 line
2.1.4
maintainer
#1 lore raw

From: "Liam R. Howlett" <[email protected]>
Date: Wed, 21 Dec 2016 13:43:38 -0500

Quoted 5 lines

Check if the PCI memory starting address is disabled and return -EINVAL. This will have minimal impact to performance as it is only during the set up of the mmap and will avoid consequences of a failed load/store.

Some X server or similar userland application may be depending upon the current behavior.

I'd rather not check, and let the user take the SIGBUS if it tries to access it.

Furthermore, the user can legitimately map a disabled BAR then enable it with PCI config accesses.

So I'm not applying this, sorry.

#2 lore raw
In reply to Liam R. Howlett
Quoted 12 lines · David Miller

From: "Liam R. Howlett" <[email protected]>
Date: Wed, 21 Dec 2016 13:43:38 -0500

Quoted 5 lines

Check if the PCI memory starting address is disabled and return -EINVAL. This will have minimal impact to performance as it is only during the set up of the mmap and will avoid consequences of a failed load/store.

Some X server or similar userland application may be depending upon the current behavior.

Quoted 7 lines

I'd rather not check, and let the user take the SIGBUS if it tries to access it.

Furthermore, the user can legitimately map a disabled BAR then enable it with PCI config accesses.

So I'm not applying this, sorry.

I appreciate you taking the time to tell me how this patch is not the right way to fix this issue. I will rework my solution to this issue to behave in a more appropriate manner.

I should have mentioned that this patch isn't to remap a SIGBUS to an -EINVAL, it's to stop the system from a panic due to a non-recoverable error occurring when trying to read an mmap'ed resource that is not enabled.

Thank you,
Liam