PCI: sysfs: Reject unaligned resource I/O port accesses
PATCH 1/1 PCI: sysfs: Reject unaligned resource I/O port accesses
pci_resource_io() validates that off+count stays within the BAR's range but never checks that the resulting port 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 accesses before they reach the low-level accessor.
--- drivers/pci/pci-sysfs.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/pci/pci-sysfs.c +3 -0
@@ -1175,6 +1175,9 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,if (port + count -1 >pci_resource_end (pdev, bar))return -EINVAL ; +if (!IS_ALIGNED (port, count)) +return -EINVAL ; +switch (count) {case 1 :if (write)
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The `IS_ALIGNED()` macro is invoked with a user-controlled argument (`count`) that is not…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The IS_ALIGNED() macro is invoked with a user-controlled argument (count) that is not guaranteed to be a power of two. --
--- Patch [1]: [PATCH] PCI: sysfs: Reject unaligned resource I/O port accesses --- commit 5ca53d551b5755e8c03169b8a70d53f035905df7
Author: Deepanshu Kartikey <[email protected]>
PCI: sysfs: Reject unaligned resource I/O port accesses
This commit adds a check in pci_resource_io() to validate that the requested port is aligned to the access size. This prevents alignment faults and kernel oopses on architectures like arm64 when accessing Device-memory mappings.
drivers/pci/pci-sysfs.c static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj, · line 1180
Quoted 11 lines
drivers/pci/pci-sysfs.c +3
-0@@ -1175,6 +1175,9 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,if (port + count -1 >pci_resource_end (pdev, bar))return -EINVAL ; +if (!IS_ALIGNED (port, count)) +return -EINVAL ; +
[Severity: Low]
Does this code call IS_ALIGNED() with an unvalidated user input for the alignment argument?
The IS_ALIGNED() macro strictly requires its alignment argument to be a power of two, but count comes from user space (via pread or pwrite) and could be a non-power-of-two value like 3. Passing a non-power-of-two value results in meaningless bitwise operations.
While the subsequent switch (count) statement catches and rejects invalid sizes by returning -EINVAL, would it be more appropriate to perform this alignment check after validating that count is actually 1, 2, or 4?
Quoted 3 lines
drivers/pci/pci-sysfs.c +0
-0switch (count) {case 1 :if (write)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
drivers/pci/pci-sysfs.c static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj, · line 1179
Quoted 2 lines · Deepanshu Kartikey
drivers/pci/pci-sysfs.c +2
-0+if (!IS_ALIGNED (port, count)) +return -EINVAL ;
You beat me to it :) I had the same change under test here, so rather than send a duplicate patch, here are the results.
I had already run the same diff through a syzbot test. Quoting the result for reference, since it went to linux-kernel and not to linux-pci:
Quoted 7 lines
syzbot has tested the proposed patch and the reproducer did not trigger any issue:
Reported-by [email protected]Tested-by [email protected]Tested on:
Quoted 7 lines
commit: ba5cc80f Merge branch 'for-next/core' into for-kernelci git tree: git://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git console output: https://syzkaller.appspot.com/x/log.txt?x=15898149580000 kernel config: https://syzkaller.appspot.com/x/.config?x=ccf4bea59f67007 dashboard link: https://syzkaller.appspot.com/bug?extid=7134530b25073b4ef373 userspace arch: arm64 patch: https://syzkaller.appspot.com/x/patch.diff?x=1431e132580000
I also tested it locally, on v7.2-rc4 arm64 defconfig under qemu 9.2, with and without this hunk, over every offset and width on an I/O BAR. The unaligned 2- and 4-byte accesses go from oopsing to -EINVAL in both directions, and every aligned access is unchanged. Widths of 3, 5 and 8 return -EINVAL before and after, so the IS_ALIGNED() point the bot raised makes no difference in practice: with a non-power-of-two count the test is meaningless, and the switch rejects the width anyway.
I wonder if it is worth mentioning in the commit log that the two directions are not the same here. You already say pread64() hits it too, but pci_write_resource() calls security_locked_down() with LOCKDOWN_PCI_ACCESS while pci_read_resource() has no such check, so with lockdown at integrity or above the write is refused and the read still faults. Both need root either way, since the attribute is 0600, so maybe it is noise; feel free to ignore.
I went through the same thing in the syzbot thread earlier today, if any of it is useful:
https://lore.kernel.org/all/[email protected]/
Thanks,
Rihyeon Kim
Quoted 33 lines · Deepanshu Kartikey
pci_resource_io()validates that off+count stays within the BAR's range but never checks that the resulting port 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 accesses before they reach the low-level accessor.
Reported-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>--- drivers/pci/pci-sysfs.c | 3 +++ 1 file changed, 3 insertions(+)drivers/pci/pci-sysfs.c +3
-1@@ -1175,6 +1175,9 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,if (port + count -1 >pci_resource_end (pdev, bar))return -EINVAL ; +if (!IS_ALIGNED (port, count)) +return -EINVAL ; +switch (count) {case 1 :if (write) --
Quoted 2 lines
2.43.0
Gentle Reminder. Please let me know the status of this patch
Thanks
Deepanshu
Quoted 28 lines · Deepanshu Kartikey
Quoted 26 lines · Deepanshu Kartikey
pci_resource_io()validates that off+count stays within the BAR's range but never checks that the resulting port 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 accesses before they reach the low-level accessor.
Reported-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>--- drivers/pci/pci-sysfs.c | 3 +++ 1 file changed, 3 insertions(+)drivers/pci/pci-sysfs.c +0
-0@@ -1175,6 +1175,9 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,if (port + count -1 >pci_resource_end (pdev, bar))return -EINVAL ;
Quoted 11 lines
Quoted 9 lines
drivers/pci/pci-sysfs.c +3
-1+if (!IS_ALIGNED (port, count)) +return -EINVAL ; +switch (count) {case 1 :if (write) --2.43.0
Gentle Reminder. Please let me know the status of this patch
We're in the middle of the merge window, so won't be adding new material until after v7.3-rc1 (probably Aug 31).
Quoted 8 lines · Deepanshu Kartikey
pci_resource_io()validates that off+count stays within the BAR's range but never checks that the resulting port 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 5 lines
Reject misaligned accesses before they reach the low-level accessor.
Reported-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>
- Mani
Signature · 1 line
மணிவண்ணன் சதாசிவம்
Quoted 33 lines · Deepanshu Kartikey
pci_resource_io()validates that off+count stays within the BAR's range but never checks that the resulting port 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 accesses before they reach the low-level accessor.
Reported-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>--- drivers/pci/pci-sysfs.c | 3 +++ 1 file changed, 3 insertions(+)drivers/pci/pci-sysfs.c +3
-1@@ -1175,6 +1175,9 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,if (port + count -1 >pci_resource_end (pdev, bar))return -EINVAL ; +if (!IS_ALIGNED (port, count)) +return -EINVAL ; +switch (count) {case 1 :if (write) --
Quoted 2 lines
2.43.0
Please let me know the status of this patch.
Thanks
Deepanshu
Quoted 28 lines · Deepanshu Kartikey
Quoted 26 lines · Deepanshu Kartikey
pci_resource_io()validates that off+count stays within the BAR's range but never checks that the resulting port 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 accesses before they reach the low-level accessor.
Reported-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>--- drivers/pci/pci-sysfs.c | 3 +++ 1 file changed, 3 insertions(+)drivers/pci/pci-sysfs.c +0
-0@@ -1175,6 +1175,9 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,if (port + count -1 >pci_resource_end (pdev, bar))return -EINVAL ;
Quoted 10 lines
Quoted 9 lines
drivers/pci/pci-sysfs.c +3
-1+if (!IS_ALIGNED (port, count)) +return -EINVAL ; +switch (count) {case 1 :if (write) --2.43.0
Please let me know the status of this patch.
What do you think of the sashiko comments:
https://lore.kernel.org/all/[email protected]?
Quoted 21 lines · On Wed, 23 Sep 2026 15:55:18 -0500 Bjorn Helgaas <[email protected]> wrote:
Quoted 20 lines · Deepanshu Kartikey
Quoted 18 lines · Deepanshu Kartikey
pci_resource_io()validates that off+count stays within the BAR's range but never checks that the resulting port 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 accesses before they reach the low-level accessor.
Reported-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>--- drivers/pci/pci-sysfs.c | 3 +++ 1 file changed, 3 insertions(+)
Quoted 6 lines
Quoted 6 lines
drivers/pci/pci-sysfs.c static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj, · line 1175
Quoted 6 lines
drivers/pci/pci-sysfs.c +0
-0@@ -1175,6 +1175,9 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,if (port + count -1 >pci_resource_end (pdev, bar))
I think that test can ignore count.
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
drivers/pci/pci-sysfs.c +3
-1return -EINVAL ; +if (!IS_ALIGNED (port, count)) +return -EINVAL ; +switch (count) {case 1 :if (write) --2.43.0
Quoted 5 lines
Quoted 1 line
Please let me know the status of this patch.
What do you think of the sashiko comments:
https://lore.kernel.org/all/[email protected]?
You could just add 'if (port & 1) return -EINVAL;' to the 'case 2:' branch, and a similar '& 3' to the 'case 4:' one.
Did it miss that loff_t is (probably) 64bit on 32bit? Or do large offsets get detected earlier?
David
Quoted 8 lines · David Laight
Quoted 4 lines · On Wed, 23 Sep 2026 15:55:18 -0500 Bjorn Helgaas <[email protected]> wrote:
What do you think of the sashiko comments:
https://lore.kernel.org/all/[email protected]?
Quoted 7 lines
You could just add 'if (port & 1) return
-EINVAL;' to the 'case 2:' branch, and a similar '& 3' to the 'case 4:' one.Did it miss that
loff_tis (probably) 64bit on 32bit? Or do large offsets get detected earlier?David
Thanks David for the suggestion. I have send patch v2
Thanks
Deepanshu
Quoted 11 lines · Deepanshu Kartikey
Quoted 9 lines
Quoted 3 lines
What do you think of the sashiko comments:
https://lore.kernel.org/all/[email protected]?You could just add 'if (port & 1) return
-EINVAL;' to the 'case 2:' branch, and a similar '& 3' to the 'case 4:' one.Did it miss that
loff_tis (probably) 64bit on 32bit? Or do large offsets get detected earlier?Thanks David for the suggestion. I have send patch v2
FWIW it seems like overengineering to me and obfuscating the intent. The
call to IS_ALIGNED will correctly reject unaligned addresses for correct values of count' and incorrect values of count' will then be rejected by falling through the `switch' statement. Also I expect extra machine code produced with the individual checks.
Maciej
Bjorn Helgaas
AppliedQuoted 8 lines · Deepanshu Kartikey
pci_resource_io()validates that off+count stays within the BAR's range but never checks that the resulting port 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 5 lines
Reject misaligned accesses before they reach the low-level accessor.
Reported-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>
Applied to pci/sysfs for v7.4, thanks!
Hopefully the last patch for v7.4.
Quoted 21 lines
--- drivers/pci/pci-sysfs.c | 3 +++ 1 file changed, 3 insertions(+)drivers/pci/pci-sysfs.c +3
-0@@ -1175,6 +1175,9 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,if (port + count -1 >pci_resource_end (pdev, bar))return -EINVAL ; +if (!IS_ALIGNED (port, count)) +return -EINVAL ; +switch (count) {case 1 :if (write)Signature · 1 line
2.43.0