Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp4619500rwb; Tue, 8 Aug 2023 11:03:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG77xVq4iiPIzFdRvg7Fck3kUoi/H6+md5ZSzN+X880Z1InSTu1CAZFvt7tLKAbnKZfd9z6 X-Received: by 2002:a05:6a20:1445:b0:12b:2170:7b13 with SMTP id a5-20020a056a20144500b0012b21707b13mr269434pzi.16.1691517793473; Tue, 08 Aug 2023 11:03:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691517793; cv=none; d=google.com; s=arc-20160816; b=MSrqSNiwB1Iemq7qnVIw0KQRlcGyQmCf3LqMTEPwkNQiGTIBU8imrEwxIh4/jlK+VT xwnT27/5UW7eFTIhQq1fYW+mZHOWF15ItZagjRxWNukquNGk+Hdt8VvrWcAi4QzcIRGw J9pPRdXpI0QCZs5JLrxldjUXYf7/XpAvee2MbU1DMLY15xB1lKj3yii6fdusmcupIgi9 6EkcLkxP5KtySXQNgR0ri14DFH4hDnj0YR/+vtrcqu/WutcLH7jJ5XNL70JjSOxl1mIh 4ZZWJJcLrTlImGTaqvrBxseouZP3ivOb91XJF1ZK7tV7QvuIlHNv/AFSzlifQbw4d4Br LpzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=vtvFVZh4JO7BYhA8vnzhvS1kejlyo7XMrITFp8P7MKs=; fh=eOBxEwfb3I7GEM+7A4thmHnVchfumi47Alv7aAR7rqs=; b=rwMib0Zsy+0qdfW1RlAUbLHYDTZWLMSf8MWPRSp6N7Ca2wY/B5Nvj5SGxh3gvInhB8 2vBxPHKFOoNz5TR/GKyJvBkmOhFQD2iVW2PduIqC6FHPksi8ioI1mPGiPji9wGJLxpA8 8EVmh99yrhpFsS82IUU3Aw6wVdC4C/GOa3b3u8jNrUfVwQ3fHw6FBKM3cqdafWYLGb0B JqCAvM0AkhzYTPtwcpQ+bAYYxxvX5T5TI+GhSYwUtdR4Fs8+9gQX7crenfaGkV4aV5RA MVkbFr0FvVzkCbKGB2LpnRHkPIflCWXRJkPOvqLJsogXEIzOS80XH3ETryTs0Lw4Chbm RH5g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=harvard.edu Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a26-20020a630b5a000000b0055c79b73046si7517440pgl.494.2023.08.08.11.02.59; Tue, 08 Aug 2023 11:03:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=harvard.edu Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234204AbjHHROi (ORCPT + 99 others); Tue, 8 Aug 2023 13:14:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36432 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230318AbjHHROC (ORCPT ); Tue, 8 Aug 2023 13:14:02 -0400 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by lindbergh.monkeyblade.net (Postfix) with SMTP id 115311C11A for ; Tue, 8 Aug 2023 09:05:50 -0700 (PDT) Received: (qmail 161982 invoked by uid 1000); 8 Aug 2023 11:05:14 -0400 Date: Tue, 8 Aug 2023 11:05:14 -0400 From: Alan Stern To: Greg KH Cc: Zhang Shurong , jgross@suse.com, xen-devel@lists.xenproject.org, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] xen: fix potential shift out-of-bounds in xenhcd_hub_control() Message-ID: References: <2023062628-shame-ebook-56f2@gregkh> <4825193.GXAFRqVoOG@localhost.localdomain> <2023080659-turban-exemption-1196@gregkh> <3481a644-1648-4fa9-86eb-2a0b86b8f47a@rowland.harvard.edu> <2023080845-talisman-ravage-0b58@gregkh> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <2023080845-talisman-ravage-0b58@gregkh> X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_PASS, SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Aug 08, 2023 at 10:26:38AM +0200, Greg KH wrote: > On Sun, Aug 06, 2023 at 11:15:51AM -0400, Alan Stern wrote: > > On Sun, Aug 06, 2023 at 04:27:27PM +0200, Greg KH wrote: > > > On Sun, Aug 06, 2023 at 10:11:43PM +0800, Zhang Shurong wrote: > > > > 在 2023年7月1日星期六 CST 下午11:51:43,Zhang Shurong 写道: > > > > > 在 2023年6月26日星期一 CST 下午1:52:02,您写道: > > > > > > > > > > > On Mon, Jun 26, 2023 at 07:48:05AM +0200, Jan Beulich wrote: > > > > > > > On 25.06.2023 18:42, Zhang Shurong wrote: > > > > > > > > --- a/drivers/usb/host/xen-hcd.c > > > > > > > > +++ b/drivers/usb/host/xen-hcd.c > > > > > > > > @@ -456,6 +456,8 @@ static int xenhcd_hub_control(struct usb_hcd *hcd, > > > > > > > > __u16 typeReq, __u16 wValue,> > > > > > > > > > > > > > > > > > info->ports[wIndex - 1].c_connection = > > > > > > > > > > false; > > > > > > > > > > > > > fallthrough; > > > > > > > > > > > > > > > > default: > > > > > > > > + if (wValue >= 32) > > > > > > > > + goto error; > > > > > > > > > > > > > > > > info->ports[wIndex - 1].status &= ~(1 > > > > > > > > > > << wValue); > > > > > > > > > > > > Even 31 is out of bounds (as in: UB) as long as it's 1 here rather > > > > > > > than 1u. > > > > > > > > > > > > Why isn't the caller fixed so this type of value could never be passed > > > > > > to the hub_control callback? > > > > > > > > > > > > thanks, > > > > > > > > > > > > greg k-h > > > > > > > > > > Although I'm not knowledgeable about the USB subsystem, I've observed that > > > > > not all driver code that implements hub_control callback performs a shift > > > > > operation on wValue, and not all shift operations among them cause > > > > > problems. Therefore, I've decided to fix this issue within each driver > > > > > itself. > > > > > > > > > > For example, in r8a66597_hub_control, it will first check whether wValue is > > > > > valid (always < 31) before the shift operation. In case of an invalid > > > > > number, the code would execute the error branch instead of the shift > > > > > operation. > > > > > > > > > > switch (wValue) { > > > > > case USB_PORT_FEAT_ENABLE: > > > > > rh->port &= ~USB_PORT_STAT_POWER; > > > > > break; > > > > > case USB_PORT_FEAT_SUSPEND: > > > > > break; > > > > > case USB_PORT_FEAT_POWER: > > > > > r8a66597_port_power(r8a66597, port, 0); > > > > > break; > > > > > case USB_PORT_FEAT_C_ENABLE: > > > > > case USB_PORT_FEAT_C_SUSPEND: > > > > > case USB_PORT_FEAT_C_CONNECTION: > > > > > case USB_PORT_FEAT_C_OVER_CURRENT: > > > > > case USB_PORT_FEAT_C_RESET: > > > > > break; > > > > > default: > > > > > goto error; > > > > > } > > > > > rh->port &= ~(1 << wValue); > > > > > > > > Hi there. I apologize for reaching out once more. I'm feeling a bit puzzled > > > > about what my next step should be. I'm unsure whether I should rewrite this > > > > patch or attempt to address the issue at the caller level. > > > > > > Try addressing it at the caller level first please. If that somehow > > > does not work, then we will take a patch series that fixes all of the > > > host controller drivers at once. > > > > It's not feasible to fix all the callers, because the calls can come > > from userspace via usbfs. > > It can? Hm, that happens through the call in rh_call_control(), right? > But there, we do a bunch of validation before calling hub_control() so > why can't we do the same thing in that one place as well? Making > invalid requests from userspace should be disallowed (or we can catch > this in the usbfs interface.) Yes, we could filter these things out at either spot. But that's not the best approach. The reason xen-hcd.c needs this change in the first place is because the code is buggy, and the change does not fix the real bug. Section 11.24.2.2 (CLEAR PORT FEATURE) of the USB-2 spec says: It is a Request Error if wValue is not a feature selector listed in Table 11-17, if wIndex specifies a port that does not exist, or if wLength is not as specified above. xenhcd_hub_control() validates wIndex but not wValue. (In theory we should validate wLength also, but in practice it doesn't matter.) Here's an example from the code: case ClearPortFeature: if (!wIndex || wIndex > ports) goto error; switch (wValue) { case USB_PORT_FEAT_SUSPEND: xenhcd_rhport_resume(info, wIndex); break; case USB_PORT_FEAT_POWER: xenhcd_rhport_power_off(info, wIndex); break; case USB_PORT_FEAT_ENABLE: xenhcd_rhport_disable(info, wIndex); break; case USB_PORT_FEAT_C_CONNECTION: info->ports[wIndex - 1].c_connection = false; default: info->ports[wIndex - 1].status &= ~(1 << wValue); This line is wrong, and not just because wValue might be too large. The only status bits that should be manipulated are the ones controlling features the driver actually implements. Not random bits passed in by the caller! break; } break; So here's what the code _should_ look like (just the end part): case USB_PORT_FEAT_C_CONNECTION: info->ports[wIndex - 1].c_connection = false; fallthrough; case USB_PORT_FEAT_C_RESET: case USB_PORT_FEAT_C_ENABLE: case USB_PORT_FEAT_C_SUSPEND: info->ports[wIndex - 1].status &= ~(1 << wValue); break; default: goto error; } break; (Perhaps also include USB_PORT_FEAT_C_OVER_CURRENT and USB_PORT_FEAT_INDICATOR, depending on whether the driver supports them.) This way the driver does the right thing in all cases and it never runs the risk of a shift amount being too big. The other HCD drivers are written this way. Similar reasoning applies to the SetPortFeature section. Alan Stern