PCI: sysfs: Reject unaligned resource I/O port accesses
PATCH v2 1/1 PCI: sysfs: Reject unaligned resource I/O port accesses
pci_resource_io() validates that the requested port lies within the BAR but never checks that it is aligned to the access size. A pwrite64()/pread64() on a resourceN file with an odd offset and count=2 or count=4 reaches outw()/outl() with a misaligned address. On arm64 this becomes a store/load to a Device-memory mapping (PCI_IOBASE + port), which architecturally requires natural alignment, causing an alignment fault and kernel oops.
Reject misaligned 2- and 4-byte accesses before they reach the low-level accessor. With natural alignment enforced, an access that starts inside the BAR cannot extend past its end, so drop the port + count - 1 range check.
---
v2: Check alignment per access size in the case 2/case 4 branches
instead of using IS_ALIGNED() with a user-controlled count, and drop the now-redundant port + count - 1 range check. (Bjorn, David, sashiko)
--- drivers/pci/pci-sysfs.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/pci/pci-sysfs.c +4 -3
@@ -1209,9 +1209,6 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,if (port >pci_resource_end (pdev, bar))return 0 ; -if (port + count -1 >pci_resource_end (pdev, bar)) -return -EINVAL ; -switch (count) {case 1 :if (write)
@@ -1220,12 +1217,16 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj, *(u8 *)buf =inb (port);return 1 ;case 2 : +if (port &1 ) +return -EINVAL ;if (write)outw (*(u16 *)buf, port);else *(u16 *)buf =inw (port);return 2 ;case 4 : +if (port &3 ) +return -EINVAL ;if (write)outl (*(u32 *)buf, port);else
Signature · 1 line
2.43.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines
pci_resource_io()validates that the requested port lies within the BAR but never checks that it is aligned to the access size. A pwrite64()/pread64() on a resourceN file with an odd offset and count=2 or count=4 reaches outw()/outl() with a misaligned address. On arm64 this becomes a store/load to a Device-memory mapping (PCI_IOBASE+ port), which architecturally requires natural alignment, causing an alignment fault and kernel oops.
Quoted 8 lines
Reject misaligned 2- and 4-byte accesses before they reach the low-level accessor. With natural alignment enforced, an access that starts inside the BAR cannot extend past its end, so drop the port + count - 1 range check.
Reported-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>
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
Quoted 42 lines · Deepanshu Kartikey
pci_resource_io()validates that the requested port lies within the BAR but never checks that it is aligned to the access size. A pwrite64()/pread64() on a resourceN file with an odd offset and count=2 or count=4 reaches outw()/outl() with a misaligned address. On arm64 this becomes a store/load to a Device-memory mapping (PCI_IOBASE+ port), which architecturally requires natural alignment, causing an alignment fault and kernel oops.Reject misaligned 2- and 4-byte accesses before they reach the low-level accessor. With natural alignment enforced, an access that starts inside the BAR cannot extend past its end, so drop the port + count - 1 range check.
Reported-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>---
v2: Check alignment per access size in the case 2/case 4 branches
instead of usingIS_ALIGNED()with a user-controlled count, and drop the now-redundant port + count - 1 range check. (Bjorn, David, sashiko)--- drivers/pci/pci-sysfs.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)drivers/pci/pci-sysfs.c +0
-3@@ -1209,9 +1209,6 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,if (port >pci_resource_end (pdev, bar))return 0 ; -if (port + count -1 >pci_resource_end (pdev, bar)) -return -EINVAL ; -switch (count) {case 1 :if (write)@@ -1220,12 +1217,16 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj, *(u8 *)buf =inb (port);return 1 ;
drivers/pci/pci-sysfs.c static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj, · line 1228
Quoted 10 lines
drivers/pci/pci-sysfs.c +3
-0case 2 : +if (port &1 ) +return -EINVAL ;if (write)outw (*(u16 *)buf, port);else *(u16 *)buf =inw (port);return 2 ;case 4 : +if (port &3 )
Why fon't these use !IS_ALIGNED() anymore? You can use e.g. sizeof() to connect it to the natural alignment of the type so you don't have to use literals at all.
Quoted 5 lines
drivers/pci/pci-sysfs.c +1
-0+return -EINVAL ;if (write)outl (*(u32 *)buf, port);else
Signature · 1 line
i.
Quoted 50 lines · On Mon, 28 Sep 2026 15:57:23 +0300 (EEST) Ilpo Järvinen…
Quoted 48 lines · Deepanshu Kartikey
pci_resource_io()validates that the requested port lies within the BAR but never checks that it is aligned to the access size. A pwrite64()/pread64() on a resourceN file with an odd offset and count=2 or count=4 reaches outw()/outl() with a misaligned address. On arm64 this becomes a store/load to a Device-memory mapping (PCI_IOBASE+ port), which architecturally requires natural alignment, causing an alignment fault and kernel oops.Reject misaligned 2- and 4-byte accesses before they reach the low-level accessor. With natural alignment enforced, an access that starts inside the BAR cannot extend past its end, so drop the port + count - 1 range check.
Reported-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>---
v2: Check alignment per access size in the case 2/case 4 branches
instead of usingIS_ALIGNED()with a user-controlled count, and drop the now-redundant port + count - 1 range check. (Bjorn, David, sashiko)--- drivers/pci/pci-sysfs.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)drivers/pci/pci-sysfs.c +2
-3@@ -1209,9 +1209,6 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,if (port >pci_resource_end (pdev, bar))return 0 ; -if (port + count -1 >pci_resource_end (pdev, bar)) -return -EINVAL ; -switch (count) {case 1 :if (write)@@ -1220,12 +1217,16 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj, *(u8 *)buf =inb (port);return 1 ;case 2 : +if (port &1 ) +return -EINVAL ;if (write)outw (*(u16 *)buf, port);else
Quoted 8 lines
drivers/pci/pci-sysfs.c static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj, · line 1228
Quoted 4 lines
drivers/pci/pci-sysfs.c +1
-0*(u16 *)buf =inw (port);return 2 ;case 4 : +if (port &3 )Why fon't these use !IS_ALIGNED() anymore? You can use e.g.
sizeof()to connect it to the natural alignment of the type so you don't have to use literals at all.
That just obfuscates what is going on....
David
Quoted 7 lines
Quoted 6 lines
drivers/pci/pci-sysfs.c +1
-0+return -EINVAL ;if (write)outl (*(u32 *)buf, port);else
Quoted 58 lines · Deepanshu Kartikey
pci_resource_io()validates that the requested port lies within the BAR but never checks that it is aligned to the access size. A pwrite64()/pread64() on a resourceN file with an odd offset and count=2 or count=4 reaches outw()/outl() with a misaligned address. On arm64 this becomes a store/load to a Device-memory mapping (PCI_IOBASE+ port), which architecturally requires natural alignment, causing an alignment fault and kernel oops.Reject misaligned 2- and 4-byte accesses before they reach the low-level accessor. With natural alignment enforced, an access that starts inside the BAR cannot extend past its end, so drop the port + count - 1 range check.
Reported-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>---
v2: Check alignment per access size in the case 2/case 4 branches
instead of usingIS_ALIGNED()with a user-controlled count, and drop the now-redundant port + count - 1 range check. (Bjorn, David, sashiko)--- drivers/pci/pci-sysfs.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)drivers/pci/pci-sysfs.c +4
-4@@ -1209,9 +1209,6 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,if (port >pci_resource_end (pdev, bar))return 0 ; -if (port + count -1 >pci_resource_end (pdev, bar)) -return -EINVAL ; -switch (count) {case 1 :if (write)@@ -1220,12 +1217,16 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj, *(u8 *)buf =inb (port);return 1 ;case 2 : +if (port &1 ) +return -EINVAL ;if (write)outw (*(u16 *)buf, port);else *(u16 *)buf =inw (port);return 2 ;case 4 : +if (port &3 ) +return -EINVAL ;if (write)outl (*(u32 *)buf, port);else --
Quoted 2 lines
2.43.0
Hi David,
Gentle Reminder. Please let me know the status of this patch.
Thanks
Deepanshu
Quoted 65 lines · On Sun, 18 Oct 2026 16:30:19 +0530 Deepanshu Kartikey <[email protected]>…
Quoted 58 lines · Deepanshu Kartikey
pci_resource_io()validates that the requested port lies within the BAR but never checks that it is aligned to the access size. A pwrite64()/pread64() on a resourceN file with an odd offset and count=2 or count=4 reaches outw()/outl() with a misaligned address. On arm64 this becomes a store/load to a Device-memory mapping (PCI_IOBASE+ port), which architecturally requires natural alignment, causing an alignment fault and kernel oops.Reject misaligned 2- and 4-byte accesses before they reach the low-level accessor. With natural alignment enforced, an access that starts inside the BAR cannot extend past its end, so drop the port + count - 1 range check.
Reported-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>---
v2: Check alignment per access size in the case 2/case 4 branches
instead of usingIS_ALIGNED()with a user-controlled count, and drop the now-redundant port + count - 1 range check. (Bjorn, David, sashiko)--- drivers/pci/pci-sysfs.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)drivers/pci/pci-sysfs.c +4
-4@@ -1209,9 +1209,6 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,if (port >pci_resource_end (pdev, bar))return 0 ; -if (port + count -1 >pci_resource_end (pdev, bar)) -return -EINVAL ; -switch (count) {case 1 :if (write)@@ -1220,12 +1217,16 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj, *(u8 *)buf =inb (port);return 1 ;case 2 : +if (port &1 ) +return -EINVAL ;if (write)outw (*(u16 *)buf, port);else *(u16 *)buf =inw (port);return 2 ;case 4 : +if (port &3 ) +return -EINVAL ;if (write)outl (*(u32 *)buf, port);else --Quoted 2 lines
2.43.0
Hi David,
Quoted 1 line
Gentle Reminder. Please let me know the status of this patch.
You need to prod the relevant maintainer, not me.
David
Quoted 4 lines
Thanks
Deepanshu