Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp36576831rwd; Tue, 11 Jul 2023 02:55:49 -0700 (PDT) X-Google-Smtp-Source: APBJJlEFxL4EWxmBSPGFgK9zDSuWqrO9XYFJB55Ee2MTKgBhkaj+mAgrQ0dhLIOhOlvxcRP0Z8kF X-Received: by 2002:a05:6a20:7343:b0:122:5441:b415 with SMTP id v3-20020a056a20734300b001225441b415mr14036629pzc.58.1689069349263; Tue, 11 Jul 2023 02:55:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689069349; cv=none; d=google.com; s=arc-20160816; b=prnuRuZhIM7h1k/3aS+8phdV0gkW4mEIHuk8Dq6OGmab9ULzA/q7enDQNC0774WkJX nkYTr0ug3P4XzFqOPx/CljfPdKoZNwP0Ptkj20H3RM0z0rLG2xYDA7X01SKjpAc5ZEGe 2eSKqfj1R8AEPoHLK1an1oVtOsEK6bhbCHIDLJxIjflNr1W8/1yPjR3qNapdK/jAAW0g Dq1nMe7xWrYrsM0a5MtcY3zE2KCYhWMVPIA32paCQjsYaNphh9zKTjFmiFjZR3BM3dJj mXbaaRwH+KJg84h7MCwLym1Ti+995hCBibeVWT05aqWqlTE1rcoQUVHAdzRTNBsPzE+h dK+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :organization:references:in-reply-to:message-id:subject:cc:to:from :date:dkim-signature; bh=1mQpjvbXTqSZMh2oqXJLwIPvBh65UpQSNz+TWmuX7pg=; fh=ENe8jpmHtFQ239dgsN9KyrIOb0pvydNpdA8nKfEn8rg=; b=iJqyc4nlo0ClrSHYN18u6nFZDFFQFo6RlnrA7LB7c1rdG7NFY56icFjysVh1TrXvc1 c+JrIRDyfcKkraXfHr3ul0cDSKtg7RL02g2TCXmOnL4sgWMgcfcm7Ba/eSnHEzRVo0Um BkoiF/sR5QkKkfHziIwrUdtOn4QQdDVlp8b/9Sth/o+XLCcZmA83Zdzb4vJprg6pF20k HDI9YV+/6+vhqYSHOiuj/eZIHf61BNEph9etm8vAnemDtnJnyxXLqPHR7s6Fe0tITMqa qWChSa9BW/1pxzZqFX5jyHCHX3UMgEqwvUUukaKWHd7/WGRgXN0YSYJ0I8oCqoRAg/nT 6k9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=J0IPQRNM; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q4-20020a056a00084400b006669351f328si1251561pfk.388.2023.07.11.02.55.37; Tue, 11 Jul 2023 02:55:49 -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; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=J0IPQRNM; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230233AbjGKJmD (ORCPT + 99 others); Tue, 11 Jul 2023 05:42:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229990AbjGKJmA (ORCPT ); Tue, 11 Jul 2023 05:42:00 -0400 Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::227]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A3E691; Tue, 11 Jul 2023 02:41:59 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id 6C38D20008; Tue, 11 Jul 2023 09:41:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1689068517; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1mQpjvbXTqSZMh2oqXJLwIPvBh65UpQSNz+TWmuX7pg=; b=J0IPQRNMF4rW2vhHO+MYH6DbHjXzguu5d4CblnTphTAbz9ZHmZ8RKYVpLXShsypPVO7j6N LYIc1MsuzN/0rKV95AwKrjMPziCl43osWurZYVBCqlwo2TdBB8aEX5PQTCS5AGn50ZwmVL NTk/4ViLGUGU5qLaKuzX8Ib9PeofopHsvXevdHx5GDuHYF0bvSswTu9giUyFQtqS/YpbaK FvIou48O/Tbv0p4Kuv01/RRyikSfEnqANaZHaheeak+lCFg1RslpA9s4A6rqxdFdALqKXN FgIQbgkJJ+fVrlUVqyoL8iUiKDvvHr+9Xgt6QwIxvaipNaeSZgstvccv138LAg== Date: Tue, 11 Jul 2023 11:41:55 +0200 From: Herve Codina To: "Chunfeng Yun (=?UTF-8?B?5LqR5pil5bOw?=)" Cc: "yhao016@ucr.edu" , "linux-arm-kernel@lists.infradead.org" , "linux-kernel@vger.kernel.org" , "linux-mediatek@lists.infradead.org" , "gregkh@linuxfoundation.org" , "linux-usb@vger.kernel.org" Subject: Re: [PATCH] usb: mtu3: Fix possible use-before-initialization bug Message-ID: <20230711114155.4a000704@bootlin.com> In-Reply-To: <29a71cbb61148d2085cc08da681526c4e20d31b9.camel@mediatek.com> References: <20230705080625.02b2bac5@bootlin.com> <20230710082558.2f82d607@bootlin.com> <29a71cbb61148d2085cc08da681526c4e20d31b9.camel@mediatek.com> Organization: Bootlin X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; x86_64-redhat-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-GND-Sasl: herve.codina@bootlin.com X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham 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 Hi Chunfeng, On Tue, 11 Jul 2023 08:48:35 +0000 Chunfeng Yun (云春峰) wrote: > On Mon, 2023-07-10 at 08:25 +0200, Herve Codina wrote: > > > > External email : Please do not click links or open attachments until > > you have verified the sender or the content. > > Hi Yu, > > > > On Sun, 9 Jul 2023 17:48:15 -0700 > > Yu Hao wrote: > > > > > Hi Hervé, > > > > > > Thanks for the comments. How about this patch? > > > --- > > > drivers/usb/mtu3/mtu3_gadget_ep0.c | 11 ++++++++--- > > > 1 file changed, 8 insertions(+), 3 deletions(-) > > > > > > diff --git a/drivers/usb/mtu3/mtu3_gadget_ep0.c > > > b/drivers/usb/mtu3/mtu3_gadget_ep0.c > > > index e4fd1bb14a55..af2884943c2a 100644 > > > --- a/drivers/usb/mtu3/mtu3_gadget_ep0.c > > > +++ b/drivers/usb/mtu3/mtu3_gadget_ep0.c > > > @@ -600,7 +600,7 @@ static void ep0_tx_state(struct mtu3 *mtu) > > > mtu3_readl(mtu->mac_base, U3D_EP0CSR)); > > > } > > > > > > -static void ep0_read_setup(struct mtu3 *mtu, struct > > usb_ctrlrequest *setup) > > > +static int ep0_read_setup(struct mtu3 *mtu, struct usb_ctrlrequest > > *setup) > > > { > > > struct mtu3_request *mreq; > > > u32 count; > > > @@ -608,6 +608,8 @@ static void ep0_read_setup(struct mtu3 *mtu, > > > struct usb_ctrlrequest *setup) > > > > > > csr = mtu3_readl(mtu->mac_base, U3D_EP0CSR) & EP0_W1C_BITS; > > > count = mtu3_readl(mtu->mac_base, U3D_RXCOUNT0); > > > + if (count == 0) > > > + return -EINVAL; > > > > 'count' should be tested against sizeof(*setup). Indeed, we need to > > have a > > setup data packet in the fifo. > > > > What do you think about: > > if (count < sizef(*setup)) > > return -EINVAL; > before call this function, already check the data length in fifo, it > should be 8 bytes. > see mtu3_ep0_isr(), about line 761. Indeed, I missed that point. Thanks for pointing it. Regards, Hervé > > I think no need this patch > > Thanks a lot > > > > > > > > > ep0_read_fifo(mtu->ep0, (u8 *)setup, count); > > > > > > @@ -642,7 +644,8 @@ __acquires(mtu->lock) > > > struct mtu3_request *mreq; > > > int handled = 0; > > > > > > - ep0_read_setup(mtu, &setup); > > > + if (ep0_read_setup(mtu, &setup)) > > > + return -EINVAL; > > > > Forward the error code to the caller ? > > > > ret = ep0_read_setup(mtu, &setup) > > if (ret < 0) > > return ret; > > > > > > > trace_mtu3_handle_setup(&setup); > > > > > > if ((setup.bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) > > > @@ -764,7 +767,9 @@ irqreturn_t mtu3_ep0_isr(struct mtu3 *mtu) > > > break; > > > } > > > > > > - ep0_handle_setup(mtu); > > > + if (ep0_handle_setup(mtu)) > > > + break; > > > + > > > > Ok > > > > > ret = IRQ_HANDLED; > > > break; > > > default: > > > > Be careful, your patch is wrongly indented. > > tabs replaced by 4 spaces. You need to keep tabs. > > > > Regards, > > Hervé Codina > >