Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion internal/vgpu/pciutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,14 @@ func (d *PCIDevice) GetVendorSpecificCapability() ([]byte, error) {
break
}
if id == PciCapabilityVendorSpecificID {
capability := d.Config[pos+PciCapabilityListID : pos+PciCapabilityListID+length]
start := int(pos) + PciCapabilityListID
if length == 0 || start+int(length) > len(d.Config) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's move start+int(length) to a variable

// Malformed capability record (e.g. length read from PCI
// config space exceeds the buffer). Skip it instead of
// panicking so a single bad device can't crash GFD.
return nil, nil

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Instead of adding a multi-line comment, I'd just add a warning log statement concisely explaining why we are dropping this device

}
capability := d.Config[start : start+int(length)]
return capability, nil
}

Expand Down
20 changes: 20 additions & 0 deletions internal/vgpu/pciutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,23 @@ func TestGetVendorSpecificCapability(t *testing.T) {
}
}
}

func TestGetVendorSpecificCapabilityMalformedLength(t *testing.T) {
// A truncated config (or a device exposing a malformed vendor-specific
// capability record whose length field makes start+length exceed the
// buffer) must not panic. It should be skipped so a single bad device
// can't crash gpu-feature-discovery.
config := make([]byte, 256)
config[PciStatusByte] |= PciStatusCapabilityList
// Point the capability list at offset 224 and mark it vendor-specific.
config[PciCapabilityList] = 224
config[224+PciCapabilityListID] = PciCapabilityVendorSpecificID
config[224+PciCapabilityListNext] = 0
config[224+PciCapabilityLength] = 40
// 224 + 40 == 264 which is past the 256-byte buffer.

device := &PCIDevice{Address: "malformed", Config: config}
capability, err := device.GetVendorSpecificCapability()
require.NoError(t, err, "malformed capability length must not panic")
require.Nil(t, capability, "malformed capability record should be skipped")
}