Skip to contentSkip to conversation

PCI/sysfs: do not allow enabling/disabling a device with a driver

PATCH New Christoph Hellwig 4 participants 4 messages May 18, 2018 – May 25, 2018

Subsystems (2)
lore
Share & tools
lore

Christoph Hellwig

author maintainer
#0 lore raw

PATCH 1/1 PCI/sysfs: do not allow enabling/disabling a device with a driver

Manpulating the enable_cnt behind the back of the driver will wreck complete havok with the kernel state, so disallow it.

Signed-off-by Christoph Hellwig <hch at lst.de>
---
 drivers/pci/pci-sysfs.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/pci/pci-sysfs.c +9 -6
index 366d93af051d..788a200fb2dc 100644
@@ -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;
 }
Signature · 1 line
2.17.0
#1 lore raw

PATCH 1/1 PCI/sysfs: do not allow enabling/disabling a device with a driver

Quoted 4 lines · Christoph Hellwig

Manpulating the enable_cnt behind the back of the driver will wreck complete havok with the kernel state, so disallow it.

Nice, looks like you beat me to it, and used device locking that I missed.

Bjorn,
Please disregard https://marc.info/?l=linux-pci&m=152666267830742&w=2

#2 lore raw

PATCH 1/1 PCI/sysfs: do not allow enabling/disabling a device with a driver

In reply to Christoph Hellwig

Looks good,
Reviewed-by: Johannes Thumshirn <jthumshirn at suse.de>

Signature · 6 lines
Johannes Thumshirn                                          Storage
jthumshirn at suse.de                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 N?rnberg
GF: Felix Imend?rffer, Jane Smithard, Graham Norton
HRB 21284 (AG N?rnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
#3 lore raw

PATCH 1/1 PCI/sysfs: do not allow enabling/disabling a device with a driver

In reply to Christoph Hellwig
Quoted 4 lines · Christoph Hellwig

Manpulating the enable_cnt behind the back of the driver will wreck complete havok with the kernel state, so disallow it.

Signed-off-by Christoph Hellwig <hch at lst.de>

Applied to pci/enumeration for v4.18, thanks!

Quoted 34 lines
---
 drivers/pci/pci-sysfs.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/pci/pci-sysfs.c +9 -6
index 366d93af051d..788a200fb2dc 100644
@@ -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;
 }
Signature · 1 line
2.17.0