1*4882a593Smuzhiyun============================================================== 2*4882a593SmuzhiyunAuthorizing (or not) your USB devices to connect to the system 3*4882a593Smuzhiyun============================================================== 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunCopyright (C) 2007 Inaky Perez-Gonzalez <inaky@linux.intel.com> Intel Corporation 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunThis feature allows you to control if a USB device can be used (or 8*4882a593Smuzhiyunnot) in a system. This feature will allow you to implement a lock-down 9*4882a593Smuzhiyunof USB devices, fully controlled by user space. 10*4882a593Smuzhiyun 11*4882a593SmuzhiyunAs of now, when a USB device is connected it is configured and 12*4882a593Smuzhiyunits interfaces are immediately made available to the users. With this 13*4882a593Smuzhiyunmodification, only if root authorizes the device to be configured will 14*4882a593Smuzhiyunthen it be possible to use it. 15*4882a593Smuzhiyun 16*4882a593SmuzhiyunUsage 17*4882a593Smuzhiyun===== 18*4882a593Smuzhiyun 19*4882a593SmuzhiyunAuthorize a device to connect:: 20*4882a593Smuzhiyun 21*4882a593Smuzhiyun $ echo 1 > /sys/bus/usb/devices/DEVICE/authorized 22*4882a593Smuzhiyun 23*4882a593SmuzhiyunDe-authorize a device:: 24*4882a593Smuzhiyun 25*4882a593Smuzhiyun $ echo 0 > /sys/bus/usb/devices/DEVICE/authorized 26*4882a593Smuzhiyun 27*4882a593SmuzhiyunSet new devices connected to hostX to be deauthorized by default (ie: 28*4882a593Smuzhiyunlock down):: 29*4882a593Smuzhiyun 30*4882a593Smuzhiyun $ echo 0 > /sys/bus/usb/devices/usbX/authorized_default 31*4882a593Smuzhiyun 32*4882a593SmuzhiyunRemove the lock down:: 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun $ echo 1 > /sys/bus/usb/devices/usbX/authorized_default 35*4882a593Smuzhiyun 36*4882a593SmuzhiyunBy default, Wired USB devices are authorized by default to 37*4882a593Smuzhiyunconnect. Wireless USB hosts deauthorize by default all new connected 38*4882a593Smuzhiyundevices (this is so because we need to do an authentication phase 39*4882a593Smuzhiyunbefore authorizing). Writing "2" to the authorized_default attribute 40*4882a593Smuzhiyuncauses kernel to only authorize by default devices connected to internal 41*4882a593SmuzhiyunUSB ports. 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun 44*4882a593SmuzhiyunExample system lockdown (lame) 45*4882a593Smuzhiyun------------------------------ 46*4882a593Smuzhiyun 47*4882a593SmuzhiyunImagine you want to implement a lockdown so only devices of type XYZ 48*4882a593Smuzhiyuncan be connected (for example, it is a kiosk machine with a visible 49*4882a593SmuzhiyunUSB port):: 50*4882a593Smuzhiyun 51*4882a593Smuzhiyun boot up 52*4882a593Smuzhiyun rc.local -> 53*4882a593Smuzhiyun 54*4882a593Smuzhiyun for host in /sys/bus/usb/devices/usb* 55*4882a593Smuzhiyun do 56*4882a593Smuzhiyun echo 0 > $host/authorized_default 57*4882a593Smuzhiyun done 58*4882a593Smuzhiyun 59*4882a593SmuzhiyunHookup an script to udev, for new USB devices:: 60*4882a593Smuzhiyun 61*4882a593Smuzhiyun if device_is_my_type $DEV 62*4882a593Smuzhiyun then 63*4882a593Smuzhiyun echo 1 > $device_path/authorized 64*4882a593Smuzhiyun done 65*4882a593Smuzhiyun 66*4882a593Smuzhiyun 67*4882a593SmuzhiyunNow, device_is_my_type() is where the juice for a lockdown is. Just 68*4882a593Smuzhiyunchecking if the class, type and protocol match something is the worse 69*4882a593Smuzhiyunsecurity verification you can make (or the best, for someone willing 70*4882a593Smuzhiyunto break it). If you need something secure, use crypto and Certificate 71*4882a593SmuzhiyunAuthentication or stuff like that. Something simple for an storage key 72*4882a593Smuzhiyuncould be:: 73*4882a593Smuzhiyun 74*4882a593Smuzhiyun function device_is_my_type() 75*4882a593Smuzhiyun { 76*4882a593Smuzhiyun echo 1 > authorized # temporarily authorize it 77*4882a593Smuzhiyun # FIXME: make sure none can mount it 78*4882a593Smuzhiyun mount DEVICENODE /mntpoint 79*4882a593Smuzhiyun sum=$(md5sum /mntpoint/.signature) 80*4882a593Smuzhiyun if [ $sum = $(cat /etc/lockdown/keysum) ] 81*4882a593Smuzhiyun then 82*4882a593Smuzhiyun echo "We are good, connected" 83*4882a593Smuzhiyun umount /mntpoint 84*4882a593Smuzhiyun # Other stuff so others can use it 85*4882a593Smuzhiyun else 86*4882a593Smuzhiyun echo 0 > authorized 87*4882a593Smuzhiyun fi 88*4882a593Smuzhiyun } 89*4882a593Smuzhiyun 90*4882a593Smuzhiyun 91*4882a593SmuzhiyunOf course, this is lame, you'd want to do a real certificate 92*4882a593Smuzhiyunverification stuff with PKI, so you don't depend on a shared secret, 93*4882a593Smuzhiyunetc, but you get the idea. Anybody with access to a device gadget kit 94*4882a593Smuzhiyuncan fake descriptors and device info. Don't trust that. You are 95*4882a593Smuzhiyunwelcome. 96*4882a593Smuzhiyun 97*4882a593Smuzhiyun 98*4882a593SmuzhiyunInterface authorization 99*4882a593Smuzhiyun----------------------- 100*4882a593Smuzhiyun 101*4882a593SmuzhiyunThere is a similar approach to allow or deny specific USB interfaces. 102*4882a593SmuzhiyunThat allows to block only a subset of an USB device. 103*4882a593Smuzhiyun 104*4882a593SmuzhiyunAuthorize an interface:: 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun $ echo 1 > /sys/bus/usb/devices/INTERFACE/authorized 107*4882a593Smuzhiyun 108*4882a593SmuzhiyunDeauthorize an interface:: 109*4882a593Smuzhiyun 110*4882a593Smuzhiyun $ echo 0 > /sys/bus/usb/devices/INTERFACE/authorized 111*4882a593Smuzhiyun 112*4882a593SmuzhiyunThe default value for new interfaces 113*4882a593Smuzhiyunon a particular USB bus can be changed, too. 114*4882a593Smuzhiyun 115*4882a593SmuzhiyunAllow interfaces per default:: 116*4882a593Smuzhiyun 117*4882a593Smuzhiyun $ echo 1 > /sys/bus/usb/devices/usbX/interface_authorized_default 118*4882a593Smuzhiyun 119*4882a593SmuzhiyunDeny interfaces per default:: 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun $ echo 0 > /sys/bus/usb/devices/usbX/interface_authorized_default 122*4882a593Smuzhiyun 123*4882a593SmuzhiyunPer default the interface_authorized_default bit is 1. 124*4882a593SmuzhiyunSo all interfaces would authorized per default. 125*4882a593Smuzhiyun 126*4882a593SmuzhiyunNote: 127*4882a593Smuzhiyun If a deauthorized interface will be authorized so the driver probing must 128*4882a593Smuzhiyun be triggered manually by writing INTERFACE to /sys/bus/usb/drivers_probe 129*4882a593Smuzhiyun 130*4882a593SmuzhiyunFor drivers that need multiple interfaces all needed interfaces should be 131*4882a593Smuzhiyunauthorized first. After that the drivers should be probed. 132*4882a593SmuzhiyunThis avoids side effects. 133