diff --git a/staging/src/k8s.io/dynamic-resource-allocation/deviceattribute/pci_linux.go b/staging/src/k8s.io/dynamic-resource-allocation/deviceattribute/pci_linux.go index 61e381e81f3..2f8c5490208 100644 --- a/staging/src/k8s.io/dynamic-resource-allocation/deviceattribute/pci_linux.go +++ b/staging/src/k8s.io/dynamic-resource-allocation/deviceattribute/pci_linux.go @@ -88,8 +88,11 @@ func resolvePCIeRoot(pciBusID string) (string, error) { // targetAbs must be /sys/devices/pci0000:01/....../0000:00:1f.0 devicePathPrefix := sysfs.devices("pci") - if !strings.HasPrefix(target, devicePathPrefix) || filepath.Base(target) != pciBusID { - return "", fmt.Errorf("invalid symlink target for PCI Bus ID %s: %s", sysBusPath, target) + if !strings.HasPrefix(target, devicePathPrefix) { + return "", fmt.Errorf("symlink target for PCI Bus ID %s is invalid: it must start with %s: %s", pciBusID, devicePathPrefix, target) + } + if filepath.Base(target) != pciBusID { + return "", fmt.Errorf("symlink target for PCI Bus ID %s is invalid: it must end with %s: %s", pciBusID, pciBusID, target) } // We need to extract the PCIe Root part, which is the first part of the path after /sys/devices/. diff --git a/staging/src/k8s.io/dynamic-resource-allocation/deviceattribute/pci_linux_test.go b/staging/src/k8s.io/dynamic-resource-allocation/deviceattribute/pci_linux_test.go index 41bc9dfa658..04cd28bd38f 100644 --- a/staging/src/k8s.io/dynamic-resource-allocation/deviceattribute/pci_linux_test.go +++ b/staging/src/k8s.io/dynamic-resource-allocation/deviceattribute/pci_linux_test.go @@ -17,6 +17,7 @@ limitations under the License. package deviceattribute import ( + "fmt" "os" "path/filepath" "reflect" @@ -72,7 +73,7 @@ func TestGetPCIeRootBAttributeyPCIBusID(t *testing.T) { expectsError: true, expectedErrMsg: "no such file or directory", }, - "invalid symlink": { + "invalid symlink (invalid prefix)": { mockSysfsSetup: func(t *testing.T, mockSysfs sysfsPath) { devicePath := mockSysfs.devices(filepath.Join("invalid-pci-root", "0000:00:13.1", pciBusID)) touchFile(t, devicePath) @@ -82,7 +83,19 @@ func TestGetPCIeRootBAttributeyPCIBusID(t *testing.T) { address: pciBusID, expectedAttribute: nil, expectsError: true, - expectedErrMsg: "invalid symlink target for PCI Bus ID", + expectedErrMsg: fmt.Sprintf("symlink target for PCI Bus ID %s is invalid: it must start with", pciBusID), + }, + "invalid symlink (invalid suffix)": { + mockSysfsSetup: func(t *testing.T, mockSysfs sysfsPath) { + devicePath := mockSysfs.devices(filepath.Join(pcieRoot, "0000:00:13.1", "0000:01:02.4")) // different PCI Bus ID + touchFile(t, devicePath) + busPath := mockSysfs.bus(filepath.Join("pci", "devices", pciBusID)) + createSymlink(t, devicePath, busPath) + }, + address: pciBusID, + expectedAttribute: nil, + expectsError: true, + expectedErrMsg: fmt.Sprintf("symlink target for PCI Bus ID %s is invalid: it must end with", pciBusID), }, } for name, test := range tests {