PCI: Prevent sysfs disable of device while driver is attached
authorChristoph Hellwig <hch@lst.de>
Fri, 18 May 2018 16:56:24 +0000 (18:56 +0200)
committerBjorn Helgaas <bhelgaas@google.com>
Fri, 25 May 2018 22:28:02 +0000 (17:28 -0500)
Manipulating the enable_cnt behind the back of the driver will wreak
complete havoc with the kernel state, so disallow it.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Reviewed-by: Johannes Thumshirn <jthumshirn@suse.de>
Acked-by: Keith Busch <keith.busch@intel.com>
drivers/pci/pci-sysfs.c

index 366d93a..788a200 100644 (file)
@@ -288,13 +288,16 @@ static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
        if (!capable(CAP_SYS_ADMIN))
                return -EPERM;
 
-       if (!val) {
-               if (pci_is_enabled(pdev))
-                       pci_disable_device(pdev);
-               else
-                       result = -EIO;
-       } else
+       device_lock(dev);
+       if (dev->driver)
+               result = -EBUSY;
+       else if (val)
                result = pci_enable_device(pdev);
+       else if (pci_is_enabled(pdev))
+               pci_disable_device(pdev);
+       else
+               result = -EIO;
+       device_unlock(dev);
 
        return result < 0 ? result : count;
 }