pci_mmap_resource() does not check if device is enabled before mapping
a PCI BAR resource into userspace. This allows new mmaps to succeed
even after a device has been marked disabled or soft-unplugged by the
driver to prevent further access.
Add the check to return -ENODEV when the resource is disabled, blocking
new userspace mmaps of BAR resources after device removal.
Tested by marking the PCI BAR resource as disabled and verifying that
a subsequent mmap attempt fails with -ENODEV.
$ sudo python3 -c "
import os, mmap, errno
try:
fd = os.open('/sys/bus/pci/devices/0001:01:00.0/resource0',os.O_RDONLY)
mmap.mmap(fd, 4096, access=mmap.ACCESS_READ)
print('mmap succeeded')
except OSError as e:
print(f'mmap failed - {e}')
"
mmap failed - [Errno 19] No such device
$
Signed-off-by: Ravi Kumar Bandi <ravib@amazon.com>
---
drivers/pci/pci-sysfs.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index d37860841260..83d580256c4b 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -1089,6 +1089,9 @@ static int pci_mmap_resource(struct kobject *kobj, const struct bin_attribute *a
if (ret)
return ret;
+ if (pci_dev_is_disconnected(pdev))
+ return -ENODEV;
+
if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
return -EINVAL;
--
2.47.3