History

usb: core: add option of only authorizing internal devices

On Chrome OS we want to use USBguard to potentially limit access to USB devices based on policy. We however to do not want to wait for userspace to come up before initializing fixed USB devices to not regress our boot times. This patch adds option to instruct the kernel to only authorize devices connected to the internal ports. Previously we could either authorize all or none (or, by default, we'd only authorize wired devices). The behavior is controlled via usbcore.authorized_default command line option.

Dmitry Torokhov committed 7 years ago

Commit 7bae0431 parent ca942a05 files · +69 −32Browse files

Unified · Split

5 files
  1. Documentation/admin-guide/kernel-parameters.txt +2 −1
  2. Documentation/usb/authorization.txt +3 −1
  3. drivers/usb/core/hcd.c +31 −20
  4. drivers/usb/core/usb.c +26 −7
  5. include/linux/usb/hcd.h +7 −3
Documentation/admin-guide/kernel-parameters.txt+2−1
@@ -4697,7 +4697,8 @@ · 4696 unmodified lines
46974697 usbcore.authorized_default=
46984698 [USB] Default USB device authorization:
46994699 (default -1 = authorized except for wireless USB,
4700− 0 = not authorized, 1 = authorized)
4700+ 0 = not authorized, 1 = authorized, 2 = authorized
4701+ if device connected to internal port)
47014702
47024703 usbcore.autosuspend=
47034704 [USB] The autosuspend time delay (in seconds) used
Documentation/usb/authorization.txt+3−1
@@ -34,7 +34,9 @@ $ echo 1 > /sys/bus/usb/devices/usbX/authorized_default · 33 unmodified lines
3434 By default, Wired USB devices are authorized by default to
3535 connect. Wireless USB hosts deauthorize by default all new connected
3636 devices (this is so because we need to do an authentication phase
37−before authorizing).
37+before authorizing). Writing "2" to the authorized_default attribute
38+causes kernel to only authorize by default devices connected to internal
39+USB ports.
3840
3941
4042 Example system lockdown (lame)
drivers/usb/core/hcd.c+31−20
@@ -373,13 +373,19 @@ static const u8 ss_rh_config_descriptor[] = { · 372 unmodified lines
373373 * -1 is authorized for all devices except wireless (old behaviour)
374374 * 0 is unauthorized for all devices
375375 * 1 is authorized for all devices
376+ * 2 is authorized for internal devices
376377 */
377−static int authorized_default = -1;
378+#define USB_AUTHORIZE_WIRED -1
379+#define USB_AUTHORIZE_NONE 0
380+#define USB_AUTHORIZE_ALL 1
381+#define USB_AUTHORIZE_INTERNAL 2
382+
383+static int authorized_default = USB_AUTHORIZE_WIRED;
378384 module_param(authorized_default, int, S_IRUGO|S_IWUSR);
379385 MODULE_PARM_DESC(authorized_default,
380386 "Default USB device authorization: 0 is not authorized, 1 is "
381− "authorized, -1 is authorized except for wireless USB (default, "
382− "old behaviour");
387+ "authorized, 2 is authorized for internal devices, -1 is "
388+ "authorized except for wireless USB (default, old behaviour");
383389 /*-------------------------------------------------------------------------*/
384390
385391 /**
@@ -884,7 +890,7 @@ static ssize_t authorized_default_show(struct device *dev, · 498 unmodified lines
884890 struct usb_hcd *hcd;
885891
886892 hcd = bus_to_hcd(usb_bus);
887− return snprintf(buf, PAGE_SIZE, "%u\n", !!HCD_DEV_AUTHORIZED(hcd));
893+ return snprintf(buf, PAGE_SIZE, "%u\n", hcd->dev_policy);
888894 }
889895
890896 static ssize_t authorized_default_store(struct device *dev,
@@ -900,11 +906,8 @@ static ssize_t authorized_default_store(struct device *dev, · 9 unmodified lines
900906 hcd = bus_to_hcd(usb_bus);
901907 result = sscanf(buf, "%u\n", &val);
902908 if (result == 1) {
903− if (val)
904− set_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags);
905− else
906− clear_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags);
907−
909+ hcd->dev_policy = val <= USB_DEVICE_AUTHORIZE_INTERNAL ?
910+ val : USB_DEVICE_AUTHORIZE_ALL;
908911 result = size;
909912 } else {
910913 result = -EINVAL;
@@ -2748,18 +2751,26 @@ int usb_add_hcd(struct usb_hcd *hcd, · 1837 unmodified lines
27482751
27492752 dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
27502753
2751− /* Keep old behaviour if authorized_default is not in [0, 1]. */
2752− if (authorized_default < 0 || authorized_default > 1) {
2753− if (hcd->wireless)
2754− clear_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags);
2755− else
2756− set_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags);
2757− } else {
2758− if (authorized_default)
2759− set_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags);
2760− else
2761− clear_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags);
2754+ switch (authorized_default) {
2755+ case USB_AUTHORIZE_NONE:
2756+ hcd->dev_policy = USB_DEVICE_AUTHORIZE_NONE;
2757+ break;
2758+
2759+ case USB_AUTHORIZE_ALL:
2760+ hcd->dev_policy = USB_DEVICE_AUTHORIZE_ALL;
2761+ break;
2762+
2763+ case USB_AUTHORIZE_INTERNAL:
2764+ hcd->dev_policy = USB_DEVICE_AUTHORIZE_INTERNAL;
2765+ break;
2766+
2767+ case USB_AUTHORIZE_WIRED:
2768+ default:
2769+ hcd->dev_policy = hcd->wireless ?
2770+ USB_DEVICE_AUTHORIZE_NONE : USB_DEVICE_AUTHORIZE_ALL;
2771+ break;
27622772 }
2773+
27632774 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
27642775
27652776 /* per default all interfaces are authorized */
drivers/usb/core/usb.c+26−7
@@ -46,8 +46,7 @@ · 45 unmodified lines
4646 #include <linux/mm.h>
4747 #include <linux/dma-mapping.h>
4848
49−#include "usb.h"
50−
49+#include "hub.h"
5150
5251 const char *usbcore_name = "usbcore";
5352
@@ -536,6 +535,27 @@ static unsigned usb_bus_is_wusb(struct usb_bus *bus) · 482 unmodified lines
536535 return hcd->wireless;
537536 }
538537
538+static bool usb_dev_authorized(struct usb_device *dev, struct usb_hcd *hcd)
539+{
540+ struct usb_hub *hub;
541+
542+ if (!dev->parent)
543+ return true; /* Root hub always ok [and always wired] */
544+
545+ switch (hcd->dev_policy) {
546+ case USB_DEVICE_AUTHORIZE_NONE:
547+ default:
548+ return false;
549+
550+ case USB_DEVICE_AUTHORIZE_ALL:
551+ return true;
552+
553+ case USB_DEVICE_AUTHORIZE_INTERNAL:
554+ hub = usb_hub_to_struct_hub(dev->parent);
555+ return hub->ports[dev->portnum - 1]->connect_type ==
556+ USB_PORT_CONNECT_TYPE_HARD_WIRED;
557+ }
558+}
539559
540560 /**
541561 * usb_alloc_dev - usb device constructor (usbcore-internal)
@@ -663,12 +683,11 @@ struct usb_device *usb_alloc_dev(struct usb_device *parent, · 121 unmodified lines
663683 dev->connect_time = jiffies;
664684 dev->active_duration = -jiffies;
665685 #endif
666− if (root_hub) /* Root hub always ok [and always wired] */
667− dev->authorized = 1;
668− else {
669− dev->authorized = !!HCD_DEV_AUTHORIZED(usb_hcd);
686+
687+ dev->authorized = usb_dev_authorized(dev, usb_hcd);
688+ if (!root_hub)
670689 dev->wusb = usb_bus_is_wusb(bus) ? 1 : 0;
671− }
690+
672691 return dev;
673692 }
674693 EXPORT_SYMBOL_GPL(usb_alloc_dev);
include/linux/usb/hcd.h+7−3
@@ -72,6 +72,12 @@ struct giveback_urb_bh { · 71 unmodified lines
7272 struct usb_host_endpoint *completing_ep;
7373 };
7474
75+enum usb_dev_authorize_policy {
76+ USB_DEVICE_AUTHORIZE_NONE = 0,
77+ USB_DEVICE_AUTHORIZE_ALL = 1,
78+ USB_DEVICE_AUTHORIZE_INTERNAL = 2,
79+};
80+
7581 struct usb_hcd {
7682
7783 /*
@@ -117,7 +123,6 @@ struct usb_hcd { · 39 unmodified lines
117123 #define HCD_FLAG_RH_RUNNING 5 /* root hub is running? */
118124 #define HCD_FLAG_DEAD 6 /* controller has died? */
119125 #define HCD_FLAG_INTF_AUTHORIZED 7 /* authorize interfaces? */
120−#define HCD_FLAG_DEV_AUTHORIZED 8 /* authorize devices? */
121126
122127 /* The flags can be tested using these macros; they are likely to
123128 * be slightly faster than test_bit().
@@ -142,8 +147,7 @@ struct usb_hcd { · 18 unmodified lines
142147 * or they require explicit user space authorization; this bit is
143148 * settable through /sys/class/usb_host/X/authorized_default
144149 */
145−#define HCD_DEV_AUTHORIZED(hcd) \
146− ((hcd)->flags & (1U << HCD_FLAG_DEV_AUTHORIZED))
150+ enum usb_dev_authorize_policy dev_policy;
147151
148152 /* Flags that get set only during HCD registration or removal. */
149153 unsigned rh_registered:1;/* is root hub registered? */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t