Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp2970534rwp; Fri, 14 Jul 2023 14:46:09 -0700 (PDT) X-Google-Smtp-Source: APBJJlFQZTB03sqIB7Lh0qLqcByqvUBr3Dsw0rhSBU8anJBbzFxG/h+h8PyPEMhnEbvVh2QSR05P X-Received: by 2002:a5d:6782:0:b0:315:a91c:1a8c with SMTP id v2-20020a5d6782000000b00315a91c1a8cmr5094812wru.22.1689371169713; Fri, 14 Jul 2023 14:46:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689371169; cv=none; d=google.com; s=arc-20160816; b=SfA/yadtlFVOefWv9Y14jSO7HQjWRLUsOF1qBJXX1JCmKnodfvMpRKOeFxxwPExp6j 9LnBM3jKwY0bOmgNrdZ1j3PNmb0eDkCCFvM4ISEwTJ8llYjT8PWNLxqkK5q9XLzQ7E4+ MkzUSHOIaXUW5KVGvAkT2m1zuDroVMPk4ah/gggJt+MsUu5aIhQxONU52bGMVFAYfidv daFb2D+WDCp0T2iHbLwqo72AMKQ1Gcd2Q9j9ytQdllFTB9u6UQgR+5AyOYFOhUnvbsZ0 Sidf8smC9x9ldlUR/oZStj/Qabh9EdCV7oyQIfjwYkfZDD4XZuheYDgh0ualAOCb/gQk UZtA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=p2iRVOWuFG5CSavPgNPM0rD9zBfEzQVEcWQXV3fkSWk=; fh=mog5F/Y78Y68eQdiyzoECJIB77Q1PVzRCObAgu+GMA8=; b=gV5CY0QTV+RMdHIkiUyEfm/2X0Imu3ZmSgCi0l/MxF05OGl+VRFwivbFiLZYWPV6pK 2LqlNf4HgvD1dPCtz2DKTNfIloTeod4NVFNTcOb+ompAIDXCR5m/LOMPqM0EQ8zS50iv +iQc+qjZSvKBxI58PuDB4Yajk/kRJuKstHVdPkuynz4ZGyFVVELoNkGRfJbdErsuRiI2 4RyIJOUMNT+invWd83Jy1Mc0AmWGHek5mK6/b9J1aBhYfyONRKNxsSOT97AHiG6csZDA X64K4YbT/frsUt5/fDutYH9rprKMwC02aLLNZcUBMN4pPRYoj3MXGj0m6YNWuGFc5/II MsVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=gQvhll80; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c6-20020a170906340600b009827e183e9fsi9493667ejb.636.2023.07.14.14.45.45; Fri, 14 Jul 2023 14:46:09 -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=@gmail.com header.s=20221208 header.b=gQvhll80; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229709AbjGNVnf (ORCPT + 99 others); Fri, 14 Jul 2023 17:43:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47954 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229483AbjGNVne (ORCPT ); Fri, 14 Jul 2023 17:43:34 -0400 Received: from mail-il1-x134.google.com (mail-il1-x134.google.com [IPv6:2607:f8b0:4864:20::134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34CD11BDA; Fri, 14 Jul 2023 14:43:33 -0700 (PDT) Received: by mail-il1-x134.google.com with SMTP id e9e14a558f8ab-3460e9f2376so10863045ab.1; Fri, 14 Jul 2023 14:43:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689371012; x=1691963012; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=p2iRVOWuFG5CSavPgNPM0rD9zBfEzQVEcWQXV3fkSWk=; b=gQvhll804QGcXkSeF0zAwJB26XCaFd9zRFwx121UFE6AmVmBr2X/xY4VUk9jvfzXyZ /lMMbnP9X8BqPvtTKKlgFMNKzwgEpBn6nNEyS9qnb2d/CTf24SjtqUQbilF9S5BS4IJy lTiiQqh+TBvVkxYrm6uldiZWoTE2eJ27xi3G2OEmgjAmt4y5cOwvaF3LKlAgWLFioz77 DOULRvqnMxLT2QbOjtP3Jk4w/39vwE/tE8HZWAGEWSv9LGS+k7aYRlr51CXi58cAsRnp LO3frNloCJhOD7IgHEcsBcGotrXdi3chKbDCzhXMQVXk151zbsAKKuoTTDZRwq6VmXaj pFnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689371012; x=1691963012; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=p2iRVOWuFG5CSavPgNPM0rD9zBfEzQVEcWQXV3fkSWk=; b=BN4EX1xu9O7yaO5GuScyNuCLw5up2VmoXug00KMXHzLuGEBAqGg+MGPJNJIAx/1reI 4O3SLE10YtFO7z334HAnuooryKPV1qSvJ2MFaB63QaN6gkAC7k3INC45pP/SwIg/NK4n bs1/eigfkMWznvNR0B97p6xHQmfDwer36rhIr242hy79NGL5tq4z6g4Ms/EJcWGzkxv7 qGiO+rueb6pceyrh9J9ztONEF/WTNZww/xkoGkxdXPR99vK8sUKUS64gdlCs9tnWo+nj h6O4aigQC5JdAwPaJPNGXdoLA0AleYjcAYJyXeaZSVv9V5m6Ba2qFdof/WUA7rplqs0D AKtA== X-Gm-Message-State: ABy/qLYgWYs0tDc1jBWZG6t7QUnbLb9IUwnVI/duCjh7KifAGJJi3kfy xBOJS28FFRWttWH2O3owF2nDsQgAMcDlmR8+pPM= X-Received: by 2002:a92:c944:0:b0:345:d58d:9ae5 with SMTP id i4-20020a92c944000000b00345d58d9ae5mr5841824ilq.7.1689371012540; Fri, 14 Jul 2023 14:43:32 -0700 (PDT) MIME-Version: 1.0 References: <20230714074011.20989-1-qiang.zhang1211@gmail.com> In-Reply-To: <20230714074011.20989-1-qiang.zhang1211@gmail.com> From: Andrey Konovalov Date: Fri, 14 Jul 2023 23:43:21 +0200 Message-ID: Subject: Re: [PATCH] USB: gadget: Fix the memory leak in raw_gadget driver To: Zqiang , gregkh@linuxfoundation.org Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,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 On Fri, Jul 14, 2023 at 9:40=E2=80=AFAM Zqiang = wrote: > > Currently, increasing raw_dev->count happens before invoke the > raw_queue_event(), if the raw_queue_event() return error, invoke > raw_release() will not trigger the dev_free() to be called. > > [ 268.905865][ T5067] raw-gadget.0 gadget.0: failed to queue event > [ 268.912053][ T5067] udc dummy_udc.0: failed to start USB Raw Gadget: -= 12 > [ 268.918885][ T5067] raw-gadget.0: probe of gadget.0 failed with error = -12 > [ 268.925956][ T5067] UDC core: USB Raw Gadget: couldn't find an availab= le UDC or it's busy > [ 268.934657][ T5067] misc raw-gadget: fail, usb_gadget_register_driver = returned -16 > > BUG: memory leak > > [] kmalloc_trace+0x24/0x90 mm/slab_common.c:1076 > [] kmalloc include/linux/slab.h:582 [inline] > [] kzalloc include/linux/slab.h:703 [inline] > [] dev_new drivers/usb/gadget/legacy/raw_gadget.c:191 [= inline] > [] raw_open+0x45/0x110 drivers/usb/gadget/legacy/raw_ga= dget.c:385 > [] misc_open+0x1a9/0x1f0 drivers/char/misc.c:165 > > [] kmalloc_trace+0x24/0x90 mm/slab_common.c:1076 > [] kmalloc include/linux/slab.h:582 [inline] > [] raw_ioctl_init+0xdf/0x410 drivers/usb/gadget/legacy/= raw_gadget.c:460 > [] raw_ioctl+0x5f9/0x1120 drivers/usb/gadget/legacy/raw= _gadget.c:1250 > [] vfs_ioctl fs/ioctl.c:51 [inline] > > [] kmalloc_trace+0x24/0x90 mm/slab_common.c:1076 > [] kmalloc include/linux/slab.h:582 [inline] > [] kzalloc include/linux/slab.h:703 [inline] > [] dummy_alloc_request+0x5a/0xe0 drivers/usb/gadget/udc= /dummy_hcd.c:665 > [] usb_ep_alloc_request+0x22/0xd0 drivers/usb/gadget/ud= c/core.c:196 > [] gadget_bind+0x6d/0x370 drivers/usb/gadget/legacy/raw= _gadget.c:292 > > This commit therefore invoke kref_get() under the condition that > raw_queue_event() return success. > > Reported-by: syzbot+feb045d335c1fdde5bf7@syzkaller.appspotmail.com > Closes: https://syzkaller.appspot.com/bug?extid=3Dfeb045d335c1fdde5bf7 > Signed-off-by: Zqiang > --- > drivers/usb/gadget/legacy/raw_gadget.c | 10 ++++++---- > 1 file changed, 6 insertions(+), 4 deletions(-) > > diff --git a/drivers/usb/gadget/legacy/raw_gadget.c b/drivers/usb/gadget/= legacy/raw_gadget.c > index 2acece16b890..e549022642e5 100644 > --- a/drivers/usb/gadget/legacy/raw_gadget.c > +++ b/drivers/usb/gadget/legacy/raw_gadget.c > @@ -310,13 +310,15 @@ static int gadget_bind(struct usb_gadget *gadget, > dev->eps_num =3D i; > spin_unlock_irqrestore(&dev->lock, flags); > > - /* Matches kref_put() in gadget_unbind(). */ > - kref_get(&dev->count); > - > ret =3D raw_queue_event(dev, USB_RAW_EVENT_CONNECT, 0, NULL); > - if (ret < 0) > + if (ret < 0) { > dev_err(&gadget->dev, "failed to queue event\n"); > + set_gadget_data(gadget, NULL); > + return ret; > + } > > + /* Matches kref_put() in gadget_unbind(). */ > + kref_get(&dev->count); > return ret; > } > > -- > 2.17.1 Indeed, if gadget_bind fails due to a raw_queue_event failure, the core gadget code will never call gadget_unbind. Reviewed-by: Andrey Konovalov Tested-by: Andrey Konovalov Thanks!