diff mbox series

uio/uio_pci_generic: Disable bus-mastering on release

Message ID 20181109231637.231582-1-venkateshs@google.com (mailing list archive)
State New, archived
Headers show
Series uio/uio_pci_generic: Disable bus-mastering on release | expand

Commit Message

Venkatesh Srinivas Nov. 9, 2018, 11:16 p.m. UTC
Userspace code using uio_pci_generic may enable bus-mastering by
directly manipulating a PCI device's command register. If a userspace
program enables bus-mastering but exits/crashes uncleanly, bus-
mastering will still be enabled and stale DMA addresses may be
programmed and live in the device.

Disable bus-mastering unconditionally on last close of a UIO PCI fd
to avoid this. If the device did not have bus-mastering enabled,
pci_clear_master() is a no-op.

Signed-off-by: Venkatesh Srinivas <venkateshs@google.com>
Reviewed-by: Bjorn Helgaas <bhelgaas@google.com>
Reviewed-by: Catherine Sullivan <csully@google.com>
---
 drivers/uio/uio_pci_generic.c | 9 +++++++++
 1 file changed, 9 insertions(+)

Comments

Michael S. Tsirkin Nov. 10, 2018, 10:24 p.m. UTC | #1
On Fri, Nov 09, 2018 at 06:16:37PM -0500, Venkatesh Srinivas wrote:
> Userspace code using uio_pci_generic may enable bus-mastering by
> directly manipulating a PCI device's command register. If a userspace
> program enables bus-mastering but exits/crashes uncleanly, bus-
> mastering will still be enabled and stale DMA addresses may be
> programmed and live in the device.
> 
> Disable bus-mastering unconditionally on last close of a UIO PCI fd
> to avoid this. If the device did not have bus-mastering enabled,
> pci_clear_master() is a no-op.
> 
> Signed-off-by: Venkatesh Srinivas <venkateshs@google.com>
> Reviewed-by: Bjorn Helgaas <bhelgaas@google.com>
> Reviewed-by: Catherine Sullivan <csully@google.com>
> ---
>  drivers/uio/uio_pci_generic.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/drivers/uio/uio_pci_generic.c b/drivers/uio/uio_pci_generic.c
> index 8773e373ffe5..832af15678ef 100644
> --- a/drivers/uio/uio_pci_generic.c
> +++ b/drivers/uio/uio_pci_generic.c
> @@ -39,6 +39,14 @@ to_uio_pci_generic_dev(struct uio_info *info)
>  	return container_of(info, struct uio_pci_generic_dev, info);
>  }
>  
> +static int release(struct uio_info *info, struct inode *inode)
> +{
> +	struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info);

An empty line would add readability here.

> +	/* Stop the device from initiating further DMAs */

UIO is intended for non-DMA capable devices, since
it doesn't enforce IOMMU protection.
So this "further DMA" worries me as it might make people think it's ok.
How about:

/* This driver is insecure when used with devices doing DMA,
 * but some people (mis)use it with such devices.
 * Let's at least make sure DMA isn't left enabled
 * after the userspace driver closes the fd.
 * Note that there's a non-zero chance doing this will wedge the device
 * at least until reset.
 */


> +	pci_clear_master(gdev->pdev);
> +	return 0;
> +}
> +
>  /* Interrupt handler. Read/modify/write the command register to disable
>   * the interrupt. */
>  static irqreturn_t irqhandler(int irq, struct uio_info *info)
> @@ -78,6 +86,7 @@ static int probe(struct pci_dev *pdev,
>  
>  	gdev->info.name = "uio_pci_generic";
>  	gdev->info.version = DRIVER_VERSION;
> +	gdev->info.release = release;
>  	gdev->pdev = pdev;
>  	if (pdev->irq) {
>  		gdev->info.irq = pdev->irq;
> -- 
> 2.19.1.930.g4563a0d9d0-goog
diff mbox series

Patch

diff --git a/drivers/uio/uio_pci_generic.c b/drivers/uio/uio_pci_generic.c
index 8773e373ffe5..832af15678ef 100644
--- a/drivers/uio/uio_pci_generic.c
+++ b/drivers/uio/uio_pci_generic.c
@@ -39,6 +39,14 @@  to_uio_pci_generic_dev(struct uio_info *info)
 	return container_of(info, struct uio_pci_generic_dev, info);
 }
 
+static int release(struct uio_info *info, struct inode *inode)
+{
+	struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info);
+	/* Stop the device from initiating further DMAs */
+	pci_clear_master(gdev->pdev);
+	return 0;
+}
+
 /* Interrupt handler. Read/modify/write the command register to disable
  * the interrupt. */
 static irqreturn_t irqhandler(int irq, struct uio_info *info)
@@ -78,6 +86,7 @@  static int probe(struct pci_dev *pdev,
 
 	gdev->info.name = "uio_pci_generic";
 	gdev->info.version = DRIVER_VERSION;
+	gdev->info.release = release;
 	gdev->pdev = pdev;
 	if (pdev->irq) {
 		gdev->info.irq = pdev->irq;