From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D626CC4332F for ; Mon, 28 Nov 2022 09:21:18 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 3176B84F1C; Mon, 28 Nov 2022 10:21:16 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="DOhHL1Fv"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id F0F7D8509B; Mon, 28 Nov 2022 10:21:13 +0100 (CET) Received: from mail-lj1-x22c.google.com (mail-lj1-x22c.google.com [IPv6:2a00:1450:4864:20::22c]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 1F2E184BD2 for ; Mon, 28 Nov 2022 10:21:11 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=szymon.heidrich@gmail.com Received: by mail-lj1-x22c.google.com with SMTP id a7so12379244ljq.12 for ; Mon, 28 Nov 2022 01:21:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id:from:to :cc:subject:date:message-id:reply-to; bh=Gop0MEz12nbruJSmyGwsDy6fQn1NNM/7mIznwsbm9AA=; b=DOhHL1FvSe/djyzxsXuj5yoqRax5LMN4NDUi6Nvc/M3j8bPiHkPbLYOUFkXnxciSbj guXA06UG6Ed8tJWn0NCIVd+0ldu/zVqe3+Di4fNuhr3p0jLBB8TT3jIbm+ygaXeMP5ly JwTjrF3gyfgqAoRKy2IQ9y+aWzvblJHrS3bEcemrZBpoAjXN4QXs2THiUXW+fJgHcmPX 5evUl09MMiXCnfRT0wwxYnt3t+WJ/6BHk8mRuWV4PUwoGXcrIcoPtngOReaU6i+iv0T9 kyKzxR8Q0VOxU6skFJwM5yvbOw4FXu/vHugeWq8j39Kst/C0dQK9GDt3KaLpJ/yrsp8G Xbcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Gop0MEz12nbruJSmyGwsDy6fQn1NNM/7mIznwsbm9AA=; b=JXDbmPVDBqqCUFdwm7x5fFI8BFkyxXWW5dZOJ4A9JEIkIvs6Gu9WqcLsbjJAwHVRJ/ Mb5X5Z8Ucyp/i6csI3IwLrEqHmth1+GOXWc0Cqin36UDJoIljZAX+CZncesXCfp+p6dy tOnk2oVodNsfM6gF61s8A2tdjTglywYPQJbawdrNTp0mX3aYj3wzS7UkeaDvBhicwkuO lTRxNYeK7Mhw9JZ2zR5FOj1gpeUrkWA+p+nTvLLXFYq+eJ0rhgy55XeqS+/J7gA/7YHq IdPx3npUecPplo0cBloYXctNiuDTmljf4fX07obv1ZQnlTUttkdZipPovjGEkz17HjVu WESA== X-Gm-Message-State: ANoB5plVWKInjI+0vKPgL3V0iPX06iUhuIHFk4A33YZadqF7cx57x4sb /XHRvHw7ImYlh9eekbY+D7Y= X-Google-Smtp-Source: AA0mqf50P2AWuSqsymYnlvzzK/xuv8y3C6d4RwTn4YFWM5pYB0A/9FxtH4wQbxta9fVQgaa58EO8+g== X-Received: by 2002:a05:651c:244:b0:277:d86:a36d with SMTP id x4-20020a05651c024400b002770d86a36dmr15714013ljn.288.1669627270209; Mon, 28 Nov 2022 01:21:10 -0800 (PST) Received: from [192.168.50.20] (077222238151.warszawa.vectranet.pl. [77.222.238.151]) by smtp.gmail.com with ESMTPSA id v19-20020a056512349300b0049876c1bb24sm1640031lfr.225.2022.11.28.01.21.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 28 Nov 2022 01:21:09 -0800 (PST) Message-ID: <5e6b2d2d-376a-b1b7-4826-f0c7bdfe1f65@gmail.com> Date: Mon, 28 Nov 2022 10:21:08 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.5.0 Subject: Re: [PATCH] Prevent buffer overflow on USB control endpoint From: Szymon Heidrich To: Marek Vasut , Fabio Estevam , Lukasz Majewski Cc: u-boot@lists.denx.de References: <20221117094847.60409-1-szymon.heidrich@gmail.com> <030344eb-e9d8-2bf9-a2c3-f124a32f323b@denx.de> <18de8ae7-fda2-ce0c-b83b-98c3af85aa9d@gmail.com> Content-Language: en-US In-Reply-To: <18de8ae7-fda2-ce0c-b83b-98c3af85aa9d@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean On 20/11/2022 16:29, Szymon Heidrich wrote: > On 20/11/2022 15:43, Marek Vasut wrote: >> On 11/17/22 12:50, Fabio Estevam wrote: >>> [Adding Lukasz and Marek] >>> >>> On Thu, Nov 17, 2022 at 6:50 AM Szymon Heidrich >>> wrote: >>>> >>>> Assure that the control endpoint buffer of size USB_BUFSIZ (4096) >>>> can not be overflown during handling of USB control transfer >>>> requests with wLength greater than USB_BUFSIZ. >>>> >>>> Signed-off-by: Szymon Heidrich >>>> --- >>>>   drivers/usb/gadget/composite.c | 11 +++++++++++ >>>>   1 file changed, 11 insertions(+) >>>> >>>> diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c >>>> index 2a309e624e..cb89f6dca9 100644 >>>> --- a/drivers/usb/gadget/composite.c >>>> +++ b/drivers/usb/gadget/composite.c >>>> @@ -1019,6 +1019,17 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) >>>>          u8                              endp; >>>>          struct usb_configuration        *c; >>>> >>>> +       if (w_length > USB_BUFSIZ) { >>>> +               if (ctrl->bRequestType & USB_DIR_IN) { >>>> +                       /* Cast away the const, we are going to overwrite on purpose. */ >>>> +                       __le16 *temp = (__le16 *)&ctrl->wLength; >>>> +                       *temp = cpu_to_le16(USB_BUFSIZ); >>>> +                       w_length = USB_BUFSIZ; >> >> Won't this end up sending corrupted packets in case they are longer than USB_BUFSIZ ? >> >> Where do such long packets come from ? >> >> What is the test-case ? > > The USB host will not attempt to retrieve more than wLenght bytes during transfer phase. > If the device would erroneously attempt to provide more data it would result in an unexpected state. > In case of most implementations the buffer for endpoint 0 along with max control transfer is limited to 4096 bytes (USB_BUFSIZ for U-Boot and Linux kernel). > Still according to the USB specification wLength is two bytes an the device may receive requests with wLength larger than 4096 bytes e.g. in case of a custom/malicious USB host. > For example one may build libusb with MAX_CTRL_BUFFER_LENGTH altered to 0xffff and this will allow the host to send requests with wLength up to 0xffff. > In this case the original implementation may result in buffer overflows as in multiple locations a value directly derived from wLength is set as the transfer phase length. > With the change applied IN requests with wLength larger than USB_BUFSIZ will be trimmed to USB_BUFSIZ, otherwise the host would read wLength-USB_BUFSIZ past cdev->req->buf. > I am not aware of any cases where more than USB_BUFSIZ would be provided from a buffer other than cdev->req->buf. In case I missed such case please let me know. Is there anything additional required from my side?