Skip to contentSkip to conversation

PCI/sysfs: Add lockdown checks to legacy I/O and memory handlers

PATCH Applied Krzysztof Wilczyński 3 participants 5 messages Jul 20 – Oct 14 PCI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI/sysfs: Add lockdown checks to legacy I/O and memory handlers

Currently, the legacy I/O and memory sysfs handlers do not check security_locked_down(LOCKDOWN_PCI_ACCESS), leaving the legacy_io and legacy_mem files unprotected when the kernel is locked down.

Commit eb627e17727e ("PCI: Lock down BAR access when the kernel is locked down") added the check to pci_write_config(), pci_mmap_resource(), and pci_write_resource_io() to prevent userspace from programming DMA-capable hardware that could be used to modify kernel code, but did not cover the legacy handlers.

As a result, root can still write arbitrary I/O ports and map the legacy I/O and memory spaces while the kernel is locked down, which is the same capability the lockdown is meant to remove.

Thus, add the same check to pci_write_legacy_io(), pci_mmap_legacy_mem(), and pci_mmap_legacy_io().

These generic handlers cover both architectures that define HAVE_PCI_LEGACY (such as Alpha and PowerPC).

Fixes eb627e17727e ("PCI: Lock down BAR access when the kernel is locked down")
---
 drivers/pci/pci-sysfs.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
drivers/pci/pci-sysfs.c +15 -0
index 5ec0b245a69b..c24d9270a7a5 100644
@@ -913,6 +913,11 @@ static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj,
 				   char *buf, loff_t off, size_t count)
 {
 	struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
+	int ret;
+
+	ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
+	if (ret)
+		return ret;
 
 	/* Only support 1, 2 or 4 byte accesses */
 	if (count != 1 && count != 2 && count != 4)
@@ -937,6 +942,11 @@ static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
 			       struct vm_area_struct *vma)
 {
 	struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
+	int ret;
+
+	ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
+	if (ret)
+		return ret;
 
 	return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
 }
@@ -957,6 +967,11 @@ static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
 			      struct vm_area_struct *vma)
 {
 	struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
+	int ret;
+
+	ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
+	if (ret)
+		return ret;
 
 	return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
 }
Signature · 1 line
2.55.0
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 17 lines

Currently, the legacy I/O and memory sysfs handlers do not check security_locked_down(LOCKDOWN_PCI_ACCESS), leaving the legacy_io and legacy_mem files unprotected when the kernel is locked down.

Commit eb627e17727e ("PCI: Lock down BAR access when the kernel is locked down") added the check to pci_write_config(), pci_mmap_resource(), and pci_write_resource_io() to prevent userspace from programming DMA-capable hardware that could be used to modify kernel code, but did not cover the legacy handlers.

As a result, root can still write arbitrary I/O ports and map the legacy I/O and memory spaces while the kernel is locked down, which is the same capability the lockdown is meant to remove.

Quoted 8 lines

Thus, add the same check to pci_write_legacy_io(), pci_mmap_legacy_mem(), and pci_mmap_legacy_io().

These generic handlers cover both architectures that define HAVE_PCI_LEGACY (such as Alpha and PowerPC).

Fixes eb627e17727e ("PCI: Lock down BAR access when the kernel is locked down")

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author Applied
#2 lore raw
In reply to Krzysztof Wilczyński

Hello,

Quoted 17 lines

Currently, the legacy I/O and memory sysfs handlers do not check security_locked_down(LOCKDOWN_PCI_ACCESS), leaving the legacy_io and legacy_mem files unprotected when the kernel is locked down.

Commit eb627e17727e ("PCI: Lock down BAR access when the kernel is locked down") added the check to pci_write_config(), pci_mmap_resource(), and pci_write_resource_io() to prevent userspace from programming DMA-capable hardware that could be used to modify kernel code, but did not cover the legacy handlers.

As a result, root can still write arbitrary I/O ports and map the legacy I/O and memory spaces while the kernel is locked down, which is the same capability the lockdown is meant to remove.

Quoted 5 lines

Thus, add the same check to pci_write_legacy_io(), pci_mmap_legacy_mem(), and pci_mmap_legacy_io().

These generic handlers cover both architectures that define HAVE_PCI_LEGACY (such as Alpha and PowerPC).

Applied to the sysfs branch.

Thank you!

Krzysztof

#3 lore raw
In reply to Krzysztof Wilczyński

Hi,

Quoted 35 lines · Krzysztof Wilczyński

Currently, the legacy I/O and memory sysfs handlers do not check security_locked_down(LOCKDOWN_PCI_ACCESS), leaving the legacy_io and legacy_mem files unprotected when the kernel is locked down.

Commit eb627e17727e ("PCI: Lock down BAR access when the kernel is locked down") added the check to pci_write_config(), pci_mmap_resource(), and pci_write_resource_io() to prevent userspace from programming DMA-capable hardware that could be used to modify kernel code, but did not cover the legacy handlers.

As a result, root can still write arbitrary I/O ports and map the legacy I/O and memory spaces while the kernel is locked down, which is the same capability the lockdown is meant to remove.

Thus, add the same check to pci_write_legacy_io(), pci_mmap_legacy_mem(), and pci_mmap_legacy_io().

These generic handlers cover both architectures that define HAVE_PCI_LEGACY (such as Alpha and PowerPC).

Fixes eb627e17727e ("PCI: Lock down BAR access when the kernel is locked down")
---
  drivers/pci/pci-sysfs.c | 15 +++++++++++++++
  1 file changed, 15 insertions(+)
drivers/pci/pci-sysfs.c +0 -0
index 5ec0b245a69b..c24d9270a7a5 100644
@@ -913,6 +913,11 @@ static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj,
  				   char *buf, loff_t off, size_t count)

drivers/pci/pci-sysfs.c static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj, · line 923

Quoted 10 lines
drivers/pci/pci-sysfs.c +5 -0
  {
  	struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
+	int ret;
+
+	ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
+	if (ret)
+		return ret;
 
  	/* Only support 1, 2 or 4 byte accesses */
  	if (count != 1 && count != 2 && count != 4)

This hunk was misapplied AFAICT. The commit 747b9bbbbdfd ("PCI/sysfs: Add lockdown checks to legacy I/O and memory handlers") applies the hunk to __pci_legacy_is_visible() instead.

Two effects:
1) pci_write_legacy_io() is still not covered,
2) with lockdown, __pci_legacy_is_visible() now returns EPERM which should trigger a WARN() in create_files() and it would create the file with 0664 (!), if I am looking correctly (and Claude confirms).

Quoted 24 lines
drivers/pci/pci-sysfs.c +10 -0
@@ -937,6 +942,11 @@ static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj,
  			       struct vm_area_struct *vma)
  {
  	struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
+	int ret;
+
+	ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
+	if (ret)
+		return ret;
 
  	return pci_mmap_legacy_page_range(bus, vma, pci_mmap_mem);
  }
@@ -957,6 +967,11 @@ static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
  			      struct vm_area_struct *vma)
  {
  	struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
+	int ret;
+
+	ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
+	if (ret)
+		return ret;
 
  	return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
  }
Signature · 2 lines
js
suse labs
#4 lore raw

Hello,

Quoted 11 lines
Quoted 11 lines
drivers/pci/pci-sysfs.c +3 -0
index 5ec0b245a69b..c24d9270a7a5 100644
@@ -913,6 +913,11 @@ static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj,
  				   char *buf, loff_t off, size_t count)
  {
  	struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj));
+	int ret;
+
+	ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
Quoted 8 lines

drivers/pci/pci-sysfs.c static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj, · line 923

Quoted 4 lines
drivers/pci/pci-sysfs.c +2 -0
+	if (ret)
+		return ret;
  	/* Only support 1, 2 or 4 byte accesses */
  	if (count != 1 && count != 2 && count != 4)

This hunk was misapplied AFAICT. The commit 747b9bbbbdfd ("PCI/sysfs: Add lockdown checks to legacy I/O and memory handlers") applies the hunk to __pci_legacy_is_visible() instead.

Yes, sadly it was misapplied. We found out recently, too. I will sent a patch to correct this a later today.

Thank you for letting us know, Jiri. Appreciated.

All the best,

Krzysztof