USB Security in the Linux Kernel

Dropboxは、製品が信頼に値するようにするだけでなく、リスクや改善点を見つけるためにスタック全体を上から下まで検証しています。多くの企業と同様に、Dropbox製品はLinuxサーバで動作しています。定期的なパッチ適用、セキュアな設定、パフォーマンスとセキュリティのロギングを行っていますが、時にはさらに深く掘り下げる必要があります。

When USB Was New

Linuxカーネルのコアコードの多くは約20年前のもので、Linus本人が書いたものや、現代のコンピュータセキュリティ産業ができる前のものです。当時、USBは新しく、悪意あるデバイスは脅威モデルに含まれていませんでした。そのため、コードはセキュリティよりも互換性と信頼性を重視して非常に緩く書かれています。このコードはAndroidスマートフォン、Chromebook、Linuxデスクトップ、ラップトップ、サーバに共有されているため、現在ではそのセキュリティが極めて重要です。

Lack of Defensive Programming

この時代に書かれたコードは、現代の防御的コーディングというセキュリティ慣行が存在する前のものです。デバイスからの信頼できない入力に対してコードのミスが悪用されないようにするため、長さの検証、正規化、過度に許容的なケースの除去といった防御的プログラミング手法を採用する必要があります。これらの戦略の多くは、コアUSBコードの初期バージョンがリリースされた後に広まったため、安全性を考慮したリビジョンが必要です。

Examples of Potentially Exploitable Code

Unvalidated Length from Device

図: 18バイトのusb_device_descriptorの不正なbLengthが237バイトオーバーフローし、struct usb_deviceのbos、config、actconfig、ep_inポインタを上書きする

2019年5月14日提出のパッチhttps://marc.info/?l=linux-usb&m=155780009303416&w=2--- drivers/usb/core/config.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/drivers/usb/core/config.c b/drivers/usb/core/config.cindex 7b5cb28ff..8cb9a136e 100644--- a/drivers/usb/core/config.c+++ b/drivers/usb/core/config.c@@ -33,6 +33,13 @@ static int find_next_descriptor(unsigned char*buffer, int size, /* Find the next descriptor of type dt1 or dt2 */ while (size > 0) {+ if (size < sizeof(struct usb_descriptor_header)) {+ printk( KERN_ERR "usb config: find_next_descriptor "+ "with size %d not sizeof("+ "struct usb_descriptor_header)", size );+ break;+ }+ h = (struct usb_descriptor_header *) buffer; if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2) break;@@ -58,6 +65,13 @@ static voidusb_parse_ssp_isoc_endpoint_companion(struct device *ddev, * The SuperSpeedPlus Isoc endpoint companion descriptor immediately * follows the SuperSpeed Endpoint Companion descriptor */+ if (size < sizeof(struct usb_ssp_isoc_ep_comp_descriptor)) {+ dev_warn(ddev, "Invalid size %d for SuperSpeedPlus isocendpoint companion"+ "for config %d interface %d altsetting %d ep %d.\n",+ size, cfgno, inum, asnum, ep->desc.bEndpointAddress);+ return;+ }+ desc = (struct usb_ssp_isoc_ep_comp_descriptor *) buffer; if (desc->bDescriptorType != USB_DT_SSP_ISOC_ENDPOINT_COMP || size < USB_DT_SSP_ISOC_EP_COMP_SIZE) {@@ -76,6 +90,14 @@ static void usb_parse_ss_endpoint_companion(structdevice *ddev, int cfgno, struct usb_ss_ep_comp_descriptor *desc; int max_tx; + if (size < sizeof(struct usb_ss_ep_comp_descriptor)) {+ dev_warn(ddev, "Invalid size %d of SuperSpeed endpoint"+ " companion for config %d "+ " interface %d altsetting %d: "+ "using minimum values\n",+ size, cfgno, inum, asnum);+ return;+ } /* The SuperSpeed endpoint companion descriptor is supposed to * be the first thing immediately following the endpoint descriptor. */@@ -103,6 +125,16 @@ static voidusb_parse_ss_endpoint_companion(struct device *ddev, int cfgno, ep->desc.wMaxPacketSize; return; }++ if ((size - desc->bLength) < 0 ||+ size < USB_DT_SS_EP_COMP_SIZE) {+ dev_warn(ddev, "Control endpoint with bMaxBurst = %d in "+ "config %d interface %d altsetting %d ep %d: "+ "has invalid bLength %d vs size %d\n", desc->bMaxBurst,+ cfgno, inum, asnum, ep->desc.bEndpointAddress,desc->bLength, size);+ return;+ }+ buffer += desc->bLength; size -= desc->bLength; memcpy(&ep->ss_ep_comp, desc, USB_DT_SS_EP_COMP_SIZE);@@ -214,7 +246,24 @@ static int usb_parse_endpoint(struct device*ddev, int cfgno, int inum, unsigned int maxp; const unsigned short *maxpacket_maxes; + if (size < sizeof(struct usb_endpoint_descriptor)) {+ dev_warn(ddev, "config %d interface %d altsetting %d has an "+ "size %d smaller then endpoint descriptor, skipping\n",+ cfgno, inum, asnum, size);++ return -EINVAL;+ }+ d = (struct usb_endpoint_descriptor *) buffer;++ if ((size - d->bLength) < 0) {+ dev_warn(ddev, "config %d interface %d altsetting %d has an "+ "invalid endpoint descriptor of length %d, skipping\n",+ cfgno, inum, asnum, d->bLength);++ return -EINVAL;+ }+ buffer += d->bLength; size -= d->bLength; @@ -446,7 +495,18 @@ static int usb_parse_interface(struct device*ddev, int cfgno, int len, retval; int num_ep, num_ep_orig; + if (size < sizeof(struct usb_interface_descriptor)) {+ dev_err(ddev, "config %d interface %d has an "+ "invalid endpoint descriptor of length %d, skipping\n",+ cfgno, inum, size);+ } d = (struct usb_interface_descriptor *) buffer;++ if ((size - d->bLength) < 0) {+ dev_err(ddev, "config %d interface %d has an "+ "invalid endpoint descriptor of length %d, skipping\n",+ cfgno, inum, d->bLength);+ } buffer += d->bLength; size -= d->bLength; @@ -514,6 +574,13 @@ static int usb_parse_interface(struct device*ddev, int cfgno, /* Parse all the endpoint descriptors */ n = 0; while (size > 0) {+ if (size < sizeof(struct usb_descriptor_header)) {+ dev_err(ddev, "config %d interface %d has an "+ "invalid endpoint descriptor of length %d,skipping\n",+ cfgno, inum, size);+ return -EINVAL;+ }+ if (((struct usb_descriptor_header *) buffer)->bDescriptorType == USB_DT_INTERFACE) break;-- 2.11.0