Received: by 2002:a05:6358:16cd:b0:dc:6189:e246 with SMTP id r13csp784480rwl; Fri, 4 Nov 2022 06:25:30 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7V64xeTWto/4oMKKYSRPmWozNRwjAXXHXEkL3MHyBUpbr6y0dNn4+U1MBh8UJ0s23+m2+T X-Received: by 2002:a17:903:2346:b0:186:ba56:f520 with SMTP id c6-20020a170903234600b00186ba56f520mr34436088plh.138.1667568330512; Fri, 04 Nov 2022 06:25:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667568330; cv=none; d=google.com; s=arc-20160816; b=drfLUCkA1POz0L8zyiiyfEEiqywlFw8UrfOv5sZbr7WHroV3nOro4cde4ReUt85dpI PsELxxy8k/+F8vWk3MxYweUumLn+xgUbPHtkfJgUXPmjWSBIfas00XeCTUQPRZISyM6t RD9rBFu8MRcGDqiKu14v/lBQJV7/LmxNsoCrZjqn1WOZvyljQW0zHJOlAWKzorL3InVw KuE5XNmMAeIvMtkW3S8O4xPg3ODtJqPQetOYXbsnibRLjLDy8NPXiB96aHtWG1Mgi+On ejqm1IeaIELOf0aze+rdrw6W+CnIp+1TSYazqHeo9TgIChSjJO0+zEEYocRIQjFs8CUe F6iQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=l3/ovoYw2gF6L/q4NB8AiNjkUY+Opg1IlQWtK3FxSIk=; b=RoTD54DQV6pin6T2q8Y6nLAUCegrjVBJO13MBrue29IYFWgqopQOBtTX7+uQa839YZ /TrLcsL4aziBkGfNCOPtZEMXVGAHCUssZUCJlWuMoaPhqPMyZL4NSNQMxgQ3aHgeelMM 8pJBD0cDO6sy5myBRYyu1XrM6AZaLooTEBSp6Izinajk+1saAU/76pErHDwjgXxr7xe6 Vywdmcn7zSqfpbhv8Lq2e3Gcsa5mjUF1svLNL+h2LHKZiKFf4v4dphSOx7gSUwURMHI3 7gPclxLPuEHRP9ykKW200OzsOd9rY5QnW11Qq84j3Rh4Xiam5SDXGXztUMXQPjbzkjMv frUw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id oa17-20020a17090b1bd100b0020b15fcac76si4032058pjb.4.2022.11.04.06.25.18; Fri, 04 Nov 2022 06:25:30 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231653AbiKDNKk (ORCPT + 96 others); Fri, 4 Nov 2022 09:10:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231596AbiKDNKh (ORCPT ); Fri, 4 Nov 2022 09:10:37 -0400 Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [IPv6:2001:67c:670:201:290:27ff:fe1d:cc33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9EFC828E06 for ; Fri, 4 Nov 2022 06:10:36 -0700 (PDT) Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oqwSw-0000Yk-F9; Fri, 04 Nov 2022 14:10:34 +0100 Received: from [2a0a:edc0:0:1101:1d::28] (helo=dude02.red.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.94.2) (envelope-from ) id 1oqwSw-002HHf-My; Fri, 04 Nov 2022 14:10:33 +0100 Received: from sha by dude02.red.stw.pengutronix.de with local (Exim 4.94.2) (envelope-from ) id 1oqwSv-004046-4s; Fri, 04 Nov 2022 14:10:33 +0100 From: Sascha Hauer To: linux-usb@vger.kernel.org Cc: Greg Kroah-Hartman , linux-kernel@vger.kernel.org, kernel@pengutronix.de, Sascha Hauer Subject: [PATCH 2/2] usb: gadget: f_ecm: Always set current gadget in ecm_bind() Date: Fri, 4 Nov 2022 14:10:31 +0100 Message-Id: <20221104131031.850850-3-s.hauer@pengutronix.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221104131031.850850-1-s.hauer@pengutronix.de> References: <20221104131031.850850-1-s.hauer@pengutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: sha@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 The gadget may change over bind/unbind cycles, so set it each time during bind, not only the first time. Without it we get a use-after-free with the following example: cd /sys/kernel/config/usb_gadget/; mkdir -p mygadget; cd mygadget mkdir -p configs/c.1/strings/0x409 echo "C1:Composite Device" > configs/c.1/strings/0x409/configuration mkdir -p functions/ecm.usb0 ln -s functions/ecm.usb0 configs/c.1/ rmmod dummy_hcd modprobe dummy_hcd KASAN will complain shortly after the 'modprobe': usb 2-1: New USB device found, idVendor=0000, idProduct=0000, bcdDevice= 6.01 usb 2-1: New USB device strings: Mfr=0, Product=0, SerialNumber=0 ================================================================== BUG: KASAN: use-after-free in gether_connect+0xb8/0x30c Read of size 4 at addr cbef170c by task swapper/3/0 CPU: 3 PID: 0 Comm: swapper/3 Not tainted 6.1.0-rc3-00014-g41ff012f50cb-dirty #322 Hardware name: Freescale i.MX6 Quad/DualLite (Device Tree) unwind_backtrace from show_stack+0x10/0x14 show_stack from dump_stack_lvl+0x58/0x70 dump_stack_lvl from print_report+0x134/0x4d4 print_report from kasan_report+0x78/0x10c kasan_report from gether_connect+0xb8/0x30c gether_connect from ecm_set_alt+0x124/0x254 ecm_set_alt from composite_setup+0xb98/0x2b18 composite_setup from configfs_composite_setup+0x80/0x98 configfs_composite_setup from dummy_timer+0x8f0/0x14a0 [dummy_hcd] ... Signed-off-by: Sascha Hauer --- drivers/usb/gadget/function/f_ecm.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/drivers/usb/gadget/function/f_ecm.c b/drivers/usb/gadget/function/f_ecm.c index ffe2486fce71c..a7ab30e603e20 100644 --- a/drivers/usb/gadget/function/f_ecm.c +++ b/drivers/usb/gadget/function/f_ecm.c @@ -685,7 +685,7 @@ ecm_bind(struct usb_configuration *c, struct usb_function *f) struct usb_composite_dev *cdev = c->cdev; struct f_ecm *ecm = func_to_ecm(f); struct usb_string *us; - int status; + int status = 0; struct usb_ep *ep; struct f_ecm_opts *ecm_opts; @@ -695,23 +695,19 @@ ecm_bind(struct usb_configuration *c, struct usb_function *f) ecm_opts = container_of(f->fi, struct f_ecm_opts, func_inst); - /* - * in drivers/usb/gadget/configfs.c:configfs_composite_bind() - * configurations are bound in sequence with list_for_each_entry, - * in each configuration its functions are bound in sequence - * with list_for_each_entry, so we assume no race condition - * with regard to ecm_opts->bound access - */ + mutex_lock(&ecm_opts->lock); + + gether_set_gadget(ecm_opts->net, cdev->gadget); + if (!ecm_opts->bound) { - mutex_lock(&ecm_opts->lock); - gether_set_gadget(ecm_opts->net, cdev->gadget); status = gether_register_netdev(ecm_opts->net); - mutex_unlock(&ecm_opts->lock); - if (status) - return status; ecm_opts->bound = true; } + mutex_unlock(&ecm_opts->lock); + if (status) + return status; + ecm_string_defs[1].s = ecm->ethaddr; us = usb_gstrings_attach(cdev, ecm_strings, -- 2.30.2