Skip to content

xen/pci: shift the segment as unsigned in PCI_SBDF() - #114

Merged
kdaula merged 2 commits into
edera/4.22from
kavi/prt-1138-pci-sbdf-unsigned-shift
Nov 16, 2026
Merged

kdaula merged 2 commits into
edera/4.22from
kavi/prt-1138-pci-sbdf-unsigned-shift

Conversation

@kdaula

@kdaula kdaula commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

PCI_SBDF() masked the segment with 0xffff, which is an int, so a segment of 0x8000 or above was shifted into the sign bit of an int. That is undefined behaviour. The PV-IOMMU IOMMU_reattach_device hypercall builds an SBDF from the segment a guest passes, and a UBSAN build reports the shift there. GCC still computes the expected value, so release builds behave the same.

PCI_SBDF4_() and PCI_SBDF2_() now mask with 0xffffU, so the shift is unsigned. Upstream staging has the same code.

The new tools/tests/pci-sbdf test takes the macros from pci.h, builds an SBDF in each of the macro's forms for segments up to 0xffff, and traps on a shift that does not fit its type. Without the fix it traps at segment 0x8000.

PRT-1138

PCI_SBDF4_() and PCI_SBDF2_() masked the segment with 0xffff, an int,
so a segment of 0x8000 or above was shifted into the sign bit of an
int, which is undefined behaviour. The PV-IOMMU IOMMU_reattach_device
hypercall builds an SBDF from the segment a guest passes, and a UBSAN
build reports the shift there. Mask with 0xffffU so the shift is
unsigned. Upstream staging has the same code.

The copy of PCI_SBDF4_() in tools/tests/spcr gets the same change.

Signed-off-by: Kavi <kavi@edera.dev>
Takes the PCI_* macros and pci_sbdf_t from xen/include/xen/pci.h and
builds an SBDF in each of the macro's three forms for segments from 0
to 0xffff, from values read at run time. The test is built with
-fsanitize=shift and traps on a shift whose result does not fit its
type, so a segment shifted into the sign bit of an int fails it.

Signed-off-by: Kavi <kavi@edera.dev>

@github-actions github-actions Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Review

Reviewed at a8d13c1.

The 0xffffU mask makes the segment shift in PCI_SBDF() unsigned without changing any SBDF it produces. The new tools/tests/pci-sbdf builds the macros straight from pci.h with shift checks that trap, so going back to 0xffff would fail it at segment 0x8000. Nothing concerning; I could not run a compiler in this environment, so the claim that the old mask traps comes from how GCC's shift sanitizer treats C99 code, not from a run.

Test Coverage

Reviewed at a8d13c1.

The new unit test in tools/tests/pci-sbdf runs on every pull request and builds all three PCI_SBDF() forms straight from pci.h. It traps if either macro shifts a segment of 0x8000 or above as an int again, and fails if the SBDF or its decoded fields come out wrong.

@kdaula

kdaula commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Confirmed by a run. Built against the old 0xffff mask, the test traps at the segment 0x8000 case on arm64 and on x86_64 under qemu-user, and with 0xffffU all five cases pass on both. CI's tools-tests jobs pass on x86_64 and arm64.

@kdaula
kdaula merged commit 4d96dae into edera/4.22 Nov 16, 2026
23 checks passed
@kdaula
kdaula deleted the kavi/prt-1138-pci-sbdf-unsigned-shift branch November 16, 2026 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants