Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp5794791rwb; Tue, 22 Nov 2022 05:08:50 -0800 (PST) X-Google-Smtp-Source: AA0mqf7toy2h6S6CsiP1FPOqTUwpHR6GeJ07IGx3s8aVZJQz41zCWb5/fQBahMzagAzDYUxJNC8N X-Received: by 2002:a63:4046:0:b0:460:fa0c:ab73 with SMTP id n67-20020a634046000000b00460fa0cab73mr3450552pga.315.1669122529925; Tue, 22 Nov 2022 05:08:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669122529; cv=none; d=google.com; s=arc-20160816; b=V+4kcdMHEnuXQk6jumoJIXkRrExJG7TxoI3t7fy2DCC/BawKaOZhAh44QzSihNuGDf dxNa1G7ZeBkY++79LeteaXN6dBBDcsKXkETBGQLx7jBO0ZELAHWFhll1YT+u8v+WV2aV gUtQ3Wa6TZJVDtxcrEhO0jhGW1C3Nz3JwxSBALJHc9fmAz0e1WpVwY01yO6VOPjIhXxf pWLwe90WWXUSPHhlWNWLvUn1O4BB2Dbrkd+h+GDbjgCDrzizM92yAzz5spYtO5l7nOin MNCXune4IlZtDBL9wqR0WD5VRD48wHYKzXQBG10eIUfwcerY8tt2+lNBcJy8yjmXsd4l wevA== 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-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=aTtIsYmSgjNJMwYngbptj8dWJgxtfDsvSXVDb4j71Ik=; b=ocEKau/OA3+IXoivjwCtlhFkNGfxo4Z0gtGmlSBgv0IusRFIaySq3fJb5JFBLNoh1O lKMQDfBMAsJxtUb+cHuEw7EYu8gJaBxevENC44V/QAmacCGR934b7OunataAZ52YfuUu +KRHbQCHaur+dv417RaIqYGQY1zae7eOCzoyq8nE6t5O/JO64Sjs21noqwHOluTktdyv DWJeWu/Kyw2dZhVIylRucGz1rvIAEoONnor52Z2sHQ8uQaG1WCFs/kOeg2Wn9YYgALdl RrKLqB3hB2NTx96Qcta+WVvkybxhYWpdweeNGtrQCSSdweWR5G39/fQlt/0XEEOTi2nE rixw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass (test mode) header.i=@metanate.com header.s=stronger header.b=yrMxle2p; 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=NONE dis=NONE) header.from=metanate.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nl7-20020a17090b384700b00214201e8996si14731259pjb.18.2022.11.22.05.08.36; Tue, 22 Nov 2022 05:08:49 -0800 (PST) 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 (test mode) header.i=@metanate.com header.s=stronger header.b=yrMxle2p; 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=NONE dis=NONE) header.from=metanate.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233490AbiKVLzu (ORCPT + 90 others); Tue, 22 Nov 2022 06:55:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233516AbiKVLzo (ORCPT ); Tue, 22 Nov 2022 06:55:44 -0500 Received: from metanate.com (unknown [IPv6:2001:8b0:1628:5005::111]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9690327929; Tue, 22 Nov 2022 03:55:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=metanate.com; s=stronger; h=In-Reply-To:Content-Type:References:Message-ID: Subject:Cc:To:From:Date:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description; bh=aTtIsYmSgjNJMwYngbptj8dWJgxtfDsvSXVDb4j71Ik=; b=yrMxl e2p3K14JvAJf0dt208JO3KloDsF2pWnjIKfWaOQ2b93xP9BUJUZ6bSsY+SxRNl/5HMW4JZotetuVg vXpyRWkQiIKI9MsDaIjrNxzx8UYwiYQlwz61Kl5yKLe3rfzPZ6tR/2QU+v+P75v6u/tpJvanpi99B W/rCV8NalOfF8PdAheVNOAb5zpAvQbR4EJPk43ap8+t9qlgxK70aKeVIkGu4ZuOwtRTd7q8XP/po4 dOYTUyf1MGsgUAhgF6nRgGt7fRDeiS6x/7EANKBeMXlkx/z1BRrh7lDdZmIKTtozCLQgRtqfFtB4Y LoW/f+Sdjd4M3UdkdFNExNkvVqIQA==; Received: from [81.174.171.191] (helo=donbot) by email.metanate.com with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1oxRsK-0002rD-97; Tue, 22 Nov 2022 11:55:40 +0000 Date: Tue, 22 Nov 2022 11:55:39 +0000 From: John Keeping To: Lee Jones Cc: Alan Stern , Greg KH , balbi@kernel.org, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org Subject: Re: [PATCH 1/1] usb: gadget: f_hid: Conduct proper refcounting on shared f_hidg pointer Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Authenticated: YES X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RDNS_NONE,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, Nov 22, 2022 at 08:31:08AM +0000, Lee Jones wrote: > On Mon, 21 Nov 2022, John Keeping wrote: > > Subject: [PATCH] usb: gadget: f_hid: fix f_hidg lifetime vs cdev > > > > The embedded struct cdev does not have its lifetime correctly tied to > > the enclosing struct f_hidg, so there is a use-after-free if /dev/hidgN > > is held open while the gadget is deleted. > > > > This can readily be replicated with libusbgx's example programs (for > > conciseness - operating directly via configfs is equivalent): > > > > gadget-hid > > exec 3<> /dev/hidg0 > > gadget-vid-pid-remove > > exec 3<&- > > > > Pull the existing device up in to struct f_hidg and make use of the > > cdev_device_{add,del}() helpers. This changes the lifetime of the > > device object to match struct f_hidg, but note that it is still added > > and deleted at the same time. > > This is much better, thanks for re-spinning. > > > [Also fix refcount leak on an error path.] > > > > Signed-off-by: John Keeping > > --- > > drivers/usb/gadget/function/f_hid.c | 50 ++++++++++++++++------------- > > 1 file changed, 28 insertions(+), 22 deletions(-) > > > > diff --git a/drivers/usb/gadget/function/f_hid.c b/drivers/usb/gadget/function/f_hid.c > > index ca0a7d9eaa34..0b94668a3812 100644 > > --- a/drivers/usb/gadget/function/f_hid.c > > +++ b/drivers/usb/gadget/function/f_hid.c > > @@ -71,7 +71,7 @@ struct f_hidg { > > wait_queue_head_t write_queue; > > struct usb_request *req; > > > > - int minor; > > + struct device dev; > > struct cdev cdev; > > struct usb_function func; > > > > @@ -84,6 +84,14 @@ static inline struct f_hidg *func_to_hidg(struct usb_function *f) > > return container_of(f, struct f_hidg, func); > > } > > > > +static void hidg_release(struct device *dev) > > +{ > > + struct f_hidg *hidg = container_of(dev, struct f_hidg, dev); > > Could we store/fetch this with dev_{set,get}_drvdata(), and make > hidg->dev a pointer reducing the size of the struct f_hidg. That will reduce the size of struct f_hidg, but we'll have an extra allocation for the device object, so I don't think that's a real benefit. It seems simpler to keep a single allocation and embed the device. > > + kfree(hidg->set_report_buf); > > + kfree(hidg); > > +} > > + > > /*-------------------------------------------------------------------------*/ > > /* Static descriptors */ > > > > @@ -904,9 +912,7 @@ static int hidg_bind(struct usb_configuration *c, struct usb_function *f) > > struct usb_ep *ep; > > struct f_hidg *hidg = func_to_hidg(f); > > struct usb_string *us; > > - struct device *device; > > int status; > > - dev_t dev; > > > > /* maybe allocate device-global string IDs, and patch descriptors */ > > us = usb_gstrings_attach(c->cdev, ct_func_strings, > > @@ -999,21 +1005,12 @@ static int hidg_bind(struct usb_configuration *c, struct usb_function *f) > > > > /* create char device */ > > cdev_init(&hidg->cdev, &f_hidg_fops); > > - dev = MKDEV(major, hidg->minor); > > - status = cdev_add(&hidg->cdev, dev, 1); > > + cdev_set_parent(&hidg->cdev, &hidg->dev.kobj); > > cdev_device_add() should take care of this, so long as: > > if (dev->devt) > dev_set_parent(cdev, &dev->kobj); Thanks, I'll change this. > > + status = cdev_device_add(&hidg->cdev, &hidg->dev); > > if (status) > > goto fail_free_descs; > > > > - device = device_create(hidg_class, NULL, dev, NULL, > > - "%s%d", "hidg", hidg->minor); > > - if (IS_ERR(device)) { > > - status = PTR_ERR(device); > > - goto del; > > - } > > - > > return 0; > > -del: > > - cdev_del(&hidg->cdev); > > fail_free_descs: > > usb_free_all_descriptors(f); > > fail: > > @@ -1244,9 +1241,7 @@ static void hidg_free(struct usb_function *f) > > > > hidg = func_to_hidg(f); > > opts = container_of(f->fi, struct f_hid_opts, func_inst); > > - kfree(hidg->report_desc); > > - kfree(hidg->set_report_buf); > > - kfree(hidg); > > + put_device(&hidg->dev); > > mutex_lock(&opts->lock); > > --opts->refcnt; > > mutex_unlock(&opts->lock); > > @@ -1256,8 +1251,7 @@ static void hidg_unbind(struct usb_configuration *c, struct usb_function *f) > > { > > struct f_hidg *hidg = func_to_hidg(f); > > > > - device_destroy(hidg_class, MKDEV(major, hidg->minor)); > > - cdev_del(&hidg->cdev); > > + cdev_device_del(&hidg->cdev); > > > > usb_free_all_descriptors(f); > > } > > @@ -1266,6 +1260,7 @@ static struct usb_function *hidg_alloc(struct usb_function_instance *fi) > > { > > struct f_hidg *hidg; > > struct f_hid_opts *opts; > > + int ret; > > > > /* allocate and initialize one new instance */ > > hidg = kzalloc(sizeof(*hidg), GFP_KERNEL); > > @@ -1277,17 +1272,28 @@ static struct usb_function *hidg_alloc(struct usb_function_instance *fi) > > mutex_lock(&opts->lock); > > ++opts->refcnt; > > > > - hidg->minor = opts->minor; > > + device_initialize(&hidg->dev); > > + hidg->dev.release = hidg_release; > > + hidg->dev.class = hidg_class; > > + hidg->dev.devt = MKDEV(major, opts->minor); > > + ret = dev_set_name(&hidg->dev, "hidg%d", opts->minor); > > + if (ret) { > > + --opts->refcnt; > > Since we're holding the opts lock at this point, is there anything > preventing us from incrementing the refcnt at the end, just before > giving up the lock, thus saving 2 decrements in the error paths? This makes sense. I'll respin this as a series and include a patch tidying up the error handling as a final step. > > + mutex_unlock(&opts->lock); > > + return ERR_PTR(ret); > > + } > > + > > hidg->bInterfaceSubClass = opts->subclass; > > hidg->bInterfaceProtocol = opts->protocol; > > hidg->report_length = opts->report_length; > > hidg->report_desc_length = opts->report_desc_length; > > if (opts->report_desc) { > > - hidg->report_desc = kmemdup(opts->report_desc, > > + hidg->report_desc = devm_kmemdup(&hidg->dev, opts->report_desc, > > Nice. > > > opts->report_desc_length, > > GFP_KERNEL); > > if (!hidg->report_desc) { > > - kfree(hidg); > > + put_device(&hidg->dev); > > + --opts->refcnt; > > mutex_unlock(&opts->lock); > > return ERR_PTR(-ENOMEM); > > } > > Thanks for doing this John, your work is appreciated.