Blob Blame History Raw
From 7b99d35064230f908551ba65c29264d90f49f246 Mon Sep 17 00:00:00 2001
From: Eric Desrochers <eric.desrochers@canonical.com>
Date: Wed, 11 Oct 2017 15:06:51 -0400
Subject: [PATCH] Fix lshw crashes with SEGV in privileged containers

lshw cmd segfault, when ran inside a privileged container.
All debugfs access is denied as it should be in privileged containers.

[strace of lshw]
open("/usr/share/hwdata/usb.ids", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/etc/usb.ids", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/usb.ids", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/local/share/usb.ids", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/lshw-common/usb.ids", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/usb.ids", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/sys/kernel/debug/usb/devices", O_RDONLY) = -1 EACCES (Permission denied)
open("/proc/bus/usb/devices", O_RDONLY) = -1 ENOENT (No such file or directory)

Signed-off-by: Lianbo Jiang <lijiang@redhat.com>
---
 src/core/usb.cc | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/core/usb.cc b/src/core/usb.cc
index ea27f2476195..da65e10df191 100644
--- a/src/core/usb.cc
+++ b/src/core/usb.cc
@@ -373,10 +373,15 @@ bool scan_usb(hwNode & n)
   }
   filenames.clear();
 
-  usbdevices = fopen(SYSKERNELDEBUGUSBDEVICES, "r");
-  if(!usbdevices)
+  if (exists(SYSKERNELDEBUGUSBDEVICES))
+    usbdevices = fopen(SYSKERNELDEBUGUSBDEVICES, "r");
+
+  if(!usbdevices && exists(PROCBUSUSBDEVICES))
     usbdevices = fopen(PROCBUSUSBDEVICES, "r");
 
+  if(!usbdevices)
+    return false;
+
   while(!feof(usbdevices))
   {
     char * buffer = NULL;
-- 
2.17.1