Repository navigation
xen/pci: shift the segment as unsigned in PCI_SBDF() - #114
Conversation
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>
There was a problem hiding this comment.
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.
|
Confirmed by a run. Built against the old |
PCI_SBDF()masked the segment with0xffff, which is anint, so a segment of0x8000or above was shifted into the sign bit of anint. That is undefined behaviour. The PV-IOMMUIOMMU_reattach_devicehypercall 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_()andPCI_SBDF2_()now mask with0xffffU, so the shift is unsigned. Upstream staging has the same code.The new
tools/tests/pci-sbdftest takes the macros frompci.h, builds an SBDF in each of the macro's forms for segments up to0xffff, and traps on a shift that does not fit its type. Without the fix it traps at segment0x8000.PRT-1138