Linux 内核中的 USB 安全

Dropbox 不仅致力于确保我们的产品值得信赖,还会审视整条组件栈以发现风险和改进空间。和许多公司一样,我们使用 Linux 服务器来支撑 Dropbox 产品。我们定期进行补丁更新、进行安全配置以及性能和安全日志记录,但有时需要更深入的调查。

USB 刚出现时

Linux 内核核心代码大约有二十年的历史,其中很多是 Linus 本人编写的,早于现代计算机安全行业的出现。当时 USB 还是新技术,恶意设备并未被纳入威胁模型,因此代码在兼容性和可靠性上极度宽松,牺牲了安全性。由于这段代码被 Android 手机、Chromebook、Linux 桌面、笔记本和服务器共享,在当今时代其安全性至关重要。

缺乏防御性编程

该时代编写的代码早于现代的防御性编码实践。为了确保代码中的错误不可被利用,特别是针对来自设备的非信任输入,需要采用长度校验、规范化以及消除过于宽松的情况等防御性编程手段。由于这些策略在核心 USB 代码的最初版本之后才流行起来,必须进行以安全为导向的修订。

潜在可利用代码示例

来自设备的未验证长度

示意图:在 18 字节的 usb_device_descriptor 上的错误 bLength 导致溢出 237 字节到 struct usb_device,覆盖 bos、config、actconfig 和 ep_in 指针。

补丁提交于 5/14/2019https://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