usb: core: add option of only authorizing internal devices
Subsystems (2)
PATCH 1/1 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.
--- .../admin-guide/kernel-parameters.txt | 3 +- Documentation/usb/authorization.txt | 4 +- drivers/usb/core/hcd.c | 51 +++++++++++-------- drivers/usb/core/usb.c | 33 +++++++++--- include/linux/usb/hcd.h | 10 ++-- 5 files changed, 69 insertions(+), 32 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +2 -1
@@ -4675,7 +4675,8 @@ usbcore.authorized_default= [USB] Default USB device authorization: (default -1 = authorized except for wireless USB, - 0 = not authorized, 1 = authorized) + 0 = not authorized, 1 = authorized, 2 = authorized + if device connected to internal port) usbcore.autosuspend= [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 By default, Wired USB devices are authorized by default to connect. Wireless USB hosts deauthorize by default all new connected devices (this is so because we need to do an authentication phase -before authorizing). +before authorizing). Writing "2" to the authorized_default attribute +causes kernel to only authorize by default devices connected to internal +USB ports. Example system lockdown (lame)
drivers/usb/core/hcd.c +31 -20
80 lines not on this page · raw message
drivers/usb/core/usb.c +26 -7
@@ -46,8 +46,7 @@#include <linux/mm.h> #include <linux/dma-mapping.h> -#include "usb.h" - +#include "hub.h" const char *usbcore_name ="usbcore" ;
@@ -536,6 +535,27 @@ static unsigned usb_bus_is_wusb(struct usb_bus *bus)return hcd->wireless; } +static bool usb_dev_authorized (struct usb_device *dev,struct usb_hcd *hcd) +{ +struct usb_hub *hub; + +if (!dev->parent) +return true;/* Root hub always ok [and always wired] */ + +switch (hcd->dev_policy) { +case USB_DEVICE_AUTHORIZE_NONE : +default : +return false; + +case USB_DEVICE_AUTHORIZE_ALL : +return true; + +case USB_DEVICE_AUTHORIZE_INTERNAL : + hub =usb_hub_to_struct_hub (dev->parent); +return hub->ports[dev->portnum -1 ]->connect_type == +USB_PORT_CONNECT_TYPE_HARD_WIRED ; + } +}/** * usb_alloc_dev - usb device constructor (usbcore-internal)
@@ -663,12 +683,11 @@ struct usb_device *usb_alloc_dev(struct usb_device *parent, dev->connect_time = jiffies; dev->active_duration = -jiffies;#endif -if (root_hub)/* Root hub always ok [and always wired] */ - dev->authorized =1 ; -else { - dev->authorized = !!HCD_DEV_AUTHORIZED (usb_hcd); + + dev->authorized =usb_dev_authorized (dev, usb_hcd); +if (!root_hub) dev->wusb =usb_bus_is_wusb (bus) ?1 :0 ; - } +return dev; }EXPORT_SYMBOL_GPL (usb_alloc_dev);
include/linux/usb/hcd.h +7 -3
@@ -72,6 +72,12 @@ struct giveback_urb_bh {struct usb_host_endpoint *completing_ep; }; +enum usb_dev_authorize_policy { +USB_DEVICE_AUTHORIZE_NONE =0 , +USB_DEVICE_AUTHORIZE_ALL =1 , +USB_DEVICE_AUTHORIZE_INTERNAL =2 , +}; +struct usb_hcd {/*
@@ -117,7 +123,6 @@ struct usb_hcd {#define HCD_FLAG_RH_RUNNING 5/* root hub is running? */ #define HCD_FLAG_DEAD 6/* controller has died? */ #define HCD_FLAG_INTF_AUTHORIZED 7/* authorize interfaces? */ -#define HCD_FLAG_DEV_AUTHORIZED 8/* authorize devices? */ /* The flags can be tested using these macros; they are likely to * be slightly faster than test_bit().
@@ -142,8 +147,7 @@ struct usb_hcd {* or they require explicit user space authorization; this bit is * settable through /sys/class/usb_host/X/authorized_default */ -#define HCD_DEV_AUTHORIZED (hcd) \ - ((hcd)->flags & (1U << HCD_FLAG_DEV_AUTHORIZED)) +enum usb_dev_authorize_policy dev_policy;/* Flags that get set only during HCD registration or removal. */ unsigned rh_registered:1;/* is root hub registered? */
Greg Kroah-Hartman
maintainerQuoted 26 lines · Dmitry Torokhov
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.
Signed-off-by Dmitry Torokhov <[email protected]>--- .../admin-guide/kernel-parameters.txt | 3 +- Documentation/usb/authorization.txt | 4 +- drivers/usb/core/hcd.c | 51 +++++++++++-------- drivers/usb/core/usb.c | 33 +++++++++--- include/linux/usb/hcd.h | 10 ++-- 5 files changed, 69 insertions(+), 32 deletions(-)Documentation/admin-guide/kernel-parameters.txt +0
-0
Documentation/admin-guide/kernel-parameters.txt line 4679
Quoted 6 lines
Documentation/admin-guide/kernel-parameters.txt +2
-1@@ -4675,7 +4675,8 @@ usbcore.authorized_default= [USB] Default USB device authorization: (default -1 = authorized except for wireless USB, - 0 = not authorized, 1 = authorized) + 0 = not authorized, 1 = authorized, 2 = authorized + if device connected to internal port)
Oh nice, another "simple" flag modified over time to be more complex :(
Anyway, that's fine, it's how APIs grow, just grumpy...
This all looks good to me, I'll go queue it up now.
greg k-h