18c2ecf20Sopenharmony_ci==============================================================
28c2ecf20Sopenharmony_ciAuthorizing (or not) your USB devices to connect to the system
38c2ecf20Sopenharmony_ci==============================================================
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ciCopyright (C) 2007 Inaky Perez-Gonzalez <inaky@linux.intel.com> Intel Corporation
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ciThis feature allows you to control if a USB device can be used (or
88c2ecf20Sopenharmony_cinot) in a system. This feature will allow you to implement a lock-down
98c2ecf20Sopenharmony_ciof USB devices, fully controlled by user space.
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ciAs of now, when a USB device is connected it is configured and
128c2ecf20Sopenharmony_ciits interfaces are immediately made available to the users.  With this
138c2ecf20Sopenharmony_cimodification, only if root authorizes the device to be configured will
148c2ecf20Sopenharmony_cithen it be possible to use it.
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ciUsage
178c2ecf20Sopenharmony_ci=====
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ciAuthorize a device to connect::
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci	$ echo 1 > /sys/bus/usb/devices/DEVICE/authorized
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ciDe-authorize a device::
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	$ echo 0 > /sys/bus/usb/devices/DEVICE/authorized
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ciSet new devices connected to hostX to be deauthorized by default (ie:
288c2ecf20Sopenharmony_cilock down)::
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	$ echo 0 > /sys/bus/usb/devices/usbX/authorized_default
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ciRemove the lock down::
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	$ echo 1 > /sys/bus/usb/devices/usbX/authorized_default
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ciBy default, Wired USB devices are authorized by default to
378c2ecf20Sopenharmony_ciconnect. Wireless USB hosts deauthorize by default all new connected
388c2ecf20Sopenharmony_cidevices (this is so because we need to do an authentication phase
398c2ecf20Sopenharmony_cibefore authorizing). Writing "2" to the authorized_default attribute
408c2ecf20Sopenharmony_cicauses kernel to only authorize by default devices connected to internal
418c2ecf20Sopenharmony_ciUSB ports.
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ciExample system lockdown (lame)
458c2ecf20Sopenharmony_ci------------------------------
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ciImagine you want to implement a lockdown so only devices of type XYZ
488c2ecf20Sopenharmony_cican be connected (for example, it is a kiosk machine with a visible
498c2ecf20Sopenharmony_ciUSB port)::
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci  boot up
528c2ecf20Sopenharmony_ci  rc.local ->
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci   for host in /sys/bus/usb/devices/usb*
558c2ecf20Sopenharmony_ci   do
568c2ecf20Sopenharmony_ci      echo 0 > $host/authorized_default
578c2ecf20Sopenharmony_ci   done
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ciHookup an script to udev, for new USB devices::
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci if device_is_my_type $DEV
628c2ecf20Sopenharmony_ci then
638c2ecf20Sopenharmony_ci   echo 1 > $device_path/authorized
648c2ecf20Sopenharmony_ci done
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ciNow, device_is_my_type() is where the juice for a lockdown is. Just
688c2ecf20Sopenharmony_cichecking if the class, type and protocol match something is the worse
698c2ecf20Sopenharmony_cisecurity verification you can make (or the best, for someone willing
708c2ecf20Sopenharmony_cito break it). If you need something secure, use crypto and Certificate
718c2ecf20Sopenharmony_ciAuthentication or stuff like that. Something simple for an storage key
728c2ecf20Sopenharmony_cicould be::
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci function device_is_my_type()
758c2ecf20Sopenharmony_ci {
768c2ecf20Sopenharmony_ci   echo 1 > authorized		# temporarily authorize it
778c2ecf20Sopenharmony_ci                                # FIXME: make sure none can mount it
788c2ecf20Sopenharmony_ci   mount DEVICENODE /mntpoint
798c2ecf20Sopenharmony_ci   sum=$(md5sum /mntpoint/.signature)
808c2ecf20Sopenharmony_ci   if [ $sum = $(cat /etc/lockdown/keysum) ]
818c2ecf20Sopenharmony_ci   then
828c2ecf20Sopenharmony_ci        echo "We are good, connected"
838c2ecf20Sopenharmony_ci        umount /mntpoint
848c2ecf20Sopenharmony_ci        # Other stuff so others can use it
858c2ecf20Sopenharmony_ci   else
868c2ecf20Sopenharmony_ci        echo 0 > authorized
878c2ecf20Sopenharmony_ci   fi
888c2ecf20Sopenharmony_ci }
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ciOf course, this is lame, you'd want to do a real certificate
928c2ecf20Sopenharmony_civerification stuff with PKI, so you don't depend on a shared secret,
938c2ecf20Sopenharmony_cietc, but you get the idea. Anybody with access to a device gadget kit
948c2ecf20Sopenharmony_cican fake descriptors and device info. Don't trust that. You are
958c2ecf20Sopenharmony_ciwelcome.
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ciInterface authorization
998c2ecf20Sopenharmony_ci-----------------------
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ciThere is a similar approach to allow or deny specific USB interfaces.
1028c2ecf20Sopenharmony_ciThat allows to block only a subset of an USB device.
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ciAuthorize an interface::
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	$ echo 1 > /sys/bus/usb/devices/INTERFACE/authorized
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ciDeauthorize an interface::
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	$ echo 0 > /sys/bus/usb/devices/INTERFACE/authorized
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ciThe default value for new interfaces
1138c2ecf20Sopenharmony_cion a particular USB bus can be changed, too.
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ciAllow interfaces per default::
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	$ echo 1 > /sys/bus/usb/devices/usbX/interface_authorized_default
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ciDeny interfaces per default::
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	$ echo 0 > /sys/bus/usb/devices/usbX/interface_authorized_default
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ciPer default the interface_authorized_default bit is 1.
1248c2ecf20Sopenharmony_ciSo all interfaces would authorized per default.
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ciNote:
1278c2ecf20Sopenharmony_ci  If a deauthorized interface will be authorized so the driver probing must
1288c2ecf20Sopenharmony_ci  be triggered manually by writing INTERFACE to /sys/bus/usb/drivers_probe
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ciFor drivers that need multiple interfaces all needed interfaces should be
1318c2ecf20Sopenharmony_ciauthorized first. After that the drivers should be probed.
1328c2ecf20Sopenharmony_ciThis avoids side effects.
133