# Authorizing (or not) your USB devices to connect to the system

_Peios Kernel / Hardware and drivers / USB_

> Copyright (C) 2007 Inaky Perez-Gonzalez <inaky@linux.intel.com> Intel Corporation

> **Peios:** References to Unix root and file modes below describe the inherited interface conventions. They are not sufficient Peios authorization: native descriptor/handle checks, the separate sysfs write gate, and mapped native privileges apply as appropriate. Debugfs additionally remains subject to integrity lockdown. See [Peios kernel behaviour](</kernel/introduction/peios-behaviour.md>).


<a id="rst-authorizing-or-not-your-usb-devices-to-connect-to-the-system"></a>

Copyright (C) 2007 Inaky Perez-Gonzalez &lt;[inaky@linux.intel.com](<mailto:inaky@linux.intel.com>)&gt; Intel Corporation

This feature allows you to control if a USB device can be used (or not) in a system. This feature will allow you to implement a lock-down of USB devices, fully controlled by user space.

As of now, when a USB device is connected it is configured and its interfaces are immediately made available to the users.  With this mechanism, the device must be authorized before it can be used. In Peios, changing the authorization attribute requires access through the native sysfs write gate; Unix UID 0 alone is not the authorization rule.

<a id="rst-usage"></a>

## Usage

Authorize a device to connect:



```text
$ echo 1 > /sys/bus/usb/devices/DEVICE/authorized
```

De-authorize a device:



```text
$ echo 0 > /sys/bus/usb/devices/DEVICE/authorized
```

Set new devices connected to hostX to be deauthorized by default (ie: lock down):



```text
$ echo 0 > /sys/bus/usb/devices/usbX/authorized_default
```

Remove the lock down:



```text
$ echo 1 > /sys/bus/usb/devices/usbX/authorized_default
```

By default, all USB devices are authorized.  Writing "2" to the authorized_default attribute causes the kernel to authorize by default only devices connected to internal USB ports.

<a id="rst-example-system-lockdown-lame"></a>

### Example system lockdown (lame)

Imagine you want to implement a lockdown so only devices of type XYZ can be connected (for example, it is a kiosk machine with a visible USB port):



```text
boot up
rc.local ->

 for host in /sys/bus/usb/devices/usb*
 do
    echo 0 > $host/authorized_default
 done
```

Hookup an script to udev, for new USB devices:



```text
if device_is_my_type $DEV
then
  echo 1 > $device_path/authorized
done
```

Now, device_is_my_type() is where the juice for a lockdown is. Just checking if the class, type and protocol match something is the worse security verification you can make (or the best, for someone willing to break it). If you need something secure, use crypto and Certificate Authentication or stuff like that. Something simple for an storage key could be:



```text
function device_is_my_type()
{
  echo 1 > authorized          # temporarily authorize it
                               # FIXME: make sure none can mount it
  mount DEVICENODE /mntpoint
  sum=$(md5sum /mntpoint/.signature)
  if [ $sum = $(cat /etc/lockdown/keysum) ]
  then
       echo "We are good, connected"
       umount /mntpoint
       # Other stuff so others can use it
  else
       echo 0 > authorized
  fi
}
```

Of course, this is lame, you'd want to do a real certificate verification stuff with PKI, so you don't depend on a shared secret, etc, but you get the idea. Anybody with access to a device gadget kit can fake descriptors and device info. Don't trust that. You are welcome.

<a id="rst-interface-authorization"></a>

### Interface authorization

There is a similar approach to allow or deny specific USB interfaces. That allows to block only a subset of an USB device.

Authorize an interface:



```text
$ echo 1 > /sys/bus/usb/devices/INTERFACE/authorized
```

Deauthorize an interface:



```text
$ echo 0 > /sys/bus/usb/devices/INTERFACE/authorized
```

The default value for new interfaces on a particular USB bus can be changed, too.

Allow interfaces per default:



```text
$ echo 1 > /sys/bus/usb/devices/usbX/interface_authorized_default
```

Deny interfaces per default:



```text
$ echo 0 > /sys/bus/usb/devices/usbX/interface_authorized_default
```

Per default the interface_authorized_default bit is 1. So all interfaces would authorized per default.

- **Note:**

  If a deauthorized interface will be authorized so the driver probing must be triggered manually by writing INTERFACE to /sys/bus/usb/drivers_probe

For drivers that need multiple interfaces all needed interfaces should be authorized first. After that the drivers should be probed. This avoids side effects.
