PCI/sysfs: Check CAP_SYS_ADMIN before parsing user input
authorKrzysztof Wilczyński <kw@linux.com>
Wed, 15 Sep 2021 23:01:25 +0000 (23:01 +0000)
committerBjorn Helgaas <bhelgaas@google.com>
Tue, 28 Sep 2021 22:46:28 +0000 (17:46 -0500)
Check if the "CAP_SYS_ADMIN" capability flag is set before parsing user
input as it makes more sense to first check whether the current user
actually has the right permissions before accepting any input from such
user.

This will also make order in which enable_store() and msi_bus_store()
perform the "CAP_SYS_ADMIN" capability check consistent with other
PCI-related sysfs objects that first verify whether user has this
capability set.

Link: https://lore.kernel.org/r/20210915230127.2495723-1-kw@linux.com
Signed-off-by: Krzysztof Wilczyński <kw@linux.com>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
drivers/pci/pci-sysfs.c

index 7fb5cd17cc981eed803c08e47e425ef9ab4e0e12..6832e161be1c068f47c4fbd4f9515b10d0cbfc03 100644 (file)
@@ -273,15 +273,16 @@ static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
 {
        struct pci_dev *pdev = to_pci_dev(dev);
        unsigned long val;
-       ssize_t result = kstrtoul(buf, 0, &val);
-
-       if (result < 0)
-               return result;
+       ssize_t result;
 
        /* this can crash the machine when done on the "wrong" device */
        if (!capable(CAP_SYS_ADMIN))
                return -EPERM;
 
+       result = kstrtoul(buf, 0, &val);
+       if (result < 0)
+               return result;
+
        device_lock(dev);
        if (dev->driver)
                result = -EBUSY;
@@ -378,12 +379,12 @@ static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr,
        struct pci_bus *subordinate = pdev->subordinate;
        unsigned long val;
 
-       if (kstrtoul(buf, 0, &val) < 0)
-               return -EINVAL;
-
        if (!capable(CAP_SYS_ADMIN))
                return -EPERM;
 
+       if (kstrtoul(buf, 0, &val) < 0)
+               return -EINVAL;
+
        /*
         * "no_msi" and "bus_flags" only affect what happens when a driver
         * requests MSI or MSI-X.  They don't affect any drivers that have