Skip to contentSkip to conversation

libxl: Check return value of libxl__xs_directory in name2bdf

PATCH In review Anthony PERARD 4 participants 4 messages Jul 11, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libxl: Check return value of libxl__xs_directory in name2bdf

libxl__xs_directory() can potentially return NULL without setting n. As n isn't initialised, we need to check libxl__xs_directory() return value before checking n. Otherwise, n might be non-zero with bdfs NULL which would lead to a segv.

Fixes 57bff091f4 ("libxl: add 'name' field to 'libxl_device_pci' in the IDL...")

---

Hi G.R., you've reported a segv in name2bdf(), and that the only potential segv I've found. I hope it's the same one as you've experienced!

---
 tools/libs/light/libxl_pci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libs/light/libxl_pci.c +1 -1
index 96f88795b6..f4c4f17545 100644
@@ -859,7 +859,7 @@ static int name2bdf(libxl__gc *gc, libxl_device_pci *pci)
     int rc = ERROR_NOTFOUND;
 
     bdfs = libxl__xs_directory(gc, XBT_NULL, PCI_INFO_PATH, &n);
-    if (!n)
+    if (!bdfs || !n)
         goto out;
 
     for (i = 0; i < n; i++) {
Signature · 1 line
Anthony PERARD
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Anthony PERARD

libxl__xs_directory() can potentially return NULL without setting n. As n isn't initialised, we need to check libxl__xs_directory() return value before checking n. Otherwise, n might be non-zero with bdfs NULL which would lead to a segv.

Fixes 57bff091f4 ("libxl: add 'name' field to 'libxl_device_pci' in the IDL...")

Juergen

#2 lore raw
Quoted 7 lines · Juergen Gross
Quoted 5 lines · Anthony PERARD

libxl__xs_directory() can potentially return NULL without setting n. As n isn't initialised, we need to check libxl__xs_directory() return value before checking n. Otherwise, n might be non-zero with bdfs NULL which would lead to a segv.

Quoted 5 lines
Quoted 3 lines
Fixes 57bff091f4 ("libxl: add 'name' field to 'libxl_device_pci' in the IDL...")

Hi Anthony,

I can confirm that the change fixed the segment fault issue I observed on 4.16.1 release.

However, the behavior is still not entirely clean after the fix. But that's probably a separate issue.
I have two devices assigned to pckback through kernel command-line. When I attempted to use pci-assignable-remove on the SSD that caused me a lot of trouble, it fails on the first attempt but works fine later...

root@gaia:~# xl pci-assignable-list
0000:00:17.0
0000:05:00.0
root@gaia:~# xl pci-assignable-remove 05:00.0
libxl: error: libxl_pci.c:906:libxl__device_pci_assignable_remove:
failed to de-quarantine 0000:05:00.0
root@gaia:~# xl pci-assignable-list
0000:00:17.0
0000:05:00.0
root@gaia:~# xl pci-assignable-add 05:00.0
libxl: warning: libxl_pci.c:802:libxl__device_pci_assignable_add:
0000:05:00.0 already assigned to pciback
root@gaia:~# xl pci-assignable-list
0000:00:17.0
0000:05:00.0
root@gaia:~# xl pci-assignable-remove 05:00.0
root@gaia:~# xl pci-assignable-list
0000:00:17.0
root@gaia:~# xl pci-assignable-add 05:00.0
libxl: warning: libxl_pci.c:822:libxl__device_pci_assignable_add:
0000:05:00.0 not bound to a driver, will not be rebound.
root@gaia:~# xl pci-assignable-list
0000:00:17.0
0000:05:00.0

I'm no longer in a debug environment, so cannot collect more debug info right now.

Thanks,
G.R.

#3 lore raw
Quoted 8 lines · G.R.
Quoted 7 lines · Juergen Gross
Quoted 5 lines · Anthony PERARD

libxl__xs_directory() can potentially return NULL without setting n. As n isn't initialised, we need to check libxl__xs_directory() return value before checking n. Otherwise, n might be non-zero with bdfs NULL which would lead to a segv.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Fixes 57bff091f4 ("libxl: add 'name' field to 'libxl_device_pci' in the IDL...")

I can confirm that the change fixed the segment fault issue I observed on 4.16.1 release.

I'll take the liberty and transform this into a Tested-by:.

Jan