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 4A93AC04FDE for ; Mon, 12 Dec 2022 09:55:45 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5AB8F8540C; Mon, 12 Dec 2022 10:55:43 +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="Rr+wRdXb"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 5F92D85413; Mon, 12 Dec 2022 10:55:42 +0100 (CET) Received: from mail-lf1-x130.google.com (mail-lf1-x130.google.com [IPv6:2a00:1450:4864:20::130]) (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 DC52A85404 for ; Mon, 12 Dec 2022 10:55:39 +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-lf1-x130.google.com with SMTP id b13so17663716lfo.3 for ; Mon, 12 Dec 2022 01:55:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=HaK6N+opF7rVPX1h5JGxuui1vgkzkXbh3Gd4/STamZI=; b=Rr+wRdXbgDAfsQd3tzyQBFxKIUp+gdujXV3SEeISvtSegLEJg5xvgYTsZfECwHV8Db gnvp+i9rkvzYpDyXa78D4KKukLXDOZc93V79IiB5kLoekPzJLvatDjfr6G5KMCx/Yvw7 3WCAYGugu7l7u0dKtVduQMIjzCreQHaxJQNr9o4P5wbrdel6KYo9QyHWZjSS/KbJt+Ni s7QeZEFiJB/z65c8WPL1zdqIucFK9uqOVpFu3IG+gK+hbpQvUZnYRczYC6LcxsBErPMH 3Hcl8cUQf3LxgLOCGa3SSXpJeF5YRoyfd1KXAgGpDmB9WyxgU9G3+D0Enjor1t9fVQ3F hP9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=HaK6N+opF7rVPX1h5JGxuui1vgkzkXbh3Gd4/STamZI=; b=qHNg0m0H176qanVqa6EOJZrZbs3uy3k+gZE0Fl9kNu9wmMbUeEUME2JW+1kv+Yjj6f VulSzwVqeGQKn0/JBdPbkXQMBEKIjJ0xwcZAuJHNVJovKREGAQuXRMHM0iSKsE5m7Hip j0jKhyl89vR3OlcM5QXwIBeyWt/x33mceE1ajNaB5YzOKEaY87bmeG+6S3/KTnJb4ODs uScw13w74cRYfVaEBoIvPG5LFdt2G/fbs23SwPPClWwqeUBSPT+jhA1sVUpYz9vOaElZ dMzgcYHBXXDjZk5Wzu4F/rHwjxlfdE+kiM/nJWm+e0jbDTxSyiSuzL1b+ONpij9coXbK BZTA== X-Gm-Message-State: ANoB5pkz/v9CCeRTWDvzoev9J65RjyVTMeZeIKn4+E5W73dGGtufHunp LCrNrfdILchHdJ8XmAojTrQ= X-Google-Smtp-Source: AA0mqf4TMNmqvjdFRwGgY9teENp5prA9VlapRYBtRHF+zr2k3+0GpJa74SINCO4yY+bWNNw2LWC+4Q== X-Received: by 2002:ac2:4e10:0:b0:4b1:eb97:f611 with SMTP id e16-20020ac24e10000000b004b1eb97f611mr6948228lfr.35.1670838939113; Mon, 12 Dec 2022 01:55:39 -0800 (PST) Received: from [192.168.50.20] (077222238151.warszawa.vectranet.pl. [77.222.238.151]) by smtp.gmail.com with ESMTPSA id c16-20020a056512075000b004a47e7b91c4sm1571627lfs.195.2022.12.12.01.55.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 12 Dec 2022 01:55:38 -0800 (PST) Message-ID: <2ad27a1b-a84a-84c4-3e52-313c7d01f455@gmail.com> Date: Mon, 12 Dec 2022 10:55:36 +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.1 Subject: Re: [PATCH] Prevent buffer overflow on USB control endpoint 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> <5e6b2d2d-376a-b1b7-4826-f0c7bdfe1f65@gmail.com> <6062efed-da78-6b04-fd02-46c0590db39c@denx.de> Content-Language: en-US From: Szymon Heidrich In-Reply-To: <6062efed-da78-6b04-fd02-46c0590db39c@denx.de> 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 28/11/2022 10:27, Marek Vasut wrote: > On 11/28/22 10:21, Szymon Heidrich wrote: >> 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? > > Sorry for the delay, I am still processing outstanding email. Could you please review this patch?