Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2658864pxj; Mon, 14 Jun 2021 04:14:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwI5VwZSJw5X4FDAGhYBrG4clb8fEr/Q0Gb2PxRW+e9uyCh50jfvDmFdFzIjrHCHMl5GFyq X-Received: by 2002:a17:907:270c:: with SMTP id w12mr14825365ejk.175.1623669284241; Mon, 14 Jun 2021 04:14:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623669284; cv=none; d=google.com; s=arc-20160816; b=myjQBFwHgh9PHhqpRP7fE0GRgEcGCIZatgtlGmRNiIX8aAq+nJ0q2AjybzeaTeFN9I OXyVhkFsPm2ZjlFUgK6PU4IHhavnFjgpKIqm42OjHNp7ad4YGzs0ynM1BmdQJxinJjO/ JFqM3dFKCrOx3Des8ERslQYL+CQrMBEWSFsFgUVwQhWuxf87TMSRudHn4tSzdNSRiZnC zViRbOFScNmroaQJPI5iuprn8FAT2rA+Uos5wHwH+d80LCUXP0S/TAOfJUxOkdCuXzh5 2Oy8Xego8tf0CzRpnlAms2tJZZSNk3se2YSiQmJ/7f+/6IetTe4cwPXcwDTebmwoCtnj ftEw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ObyDO2USpcKfX1fqfj7mmd1P0hEUy+ufz0503Rvkifc=; b=w2VjUhtk68LRlpJcwtpXhIx+y66LCAhCemB2x1jsDsrZzjpdd3lyk6RDqnQq9rqhyf 7DD3TG2gNMG1jTb8wQ1xF07N4JPh/rLC/WERpu5BdwTj9FSfIgRKCl+LkMjuCjFnHkUa Bdm2/++HeAGYKlRfYuIiXLII/IizoctXEQKfJ+FOxAmHGFn6W+eETsta/VIBZWWvwybU B+HbXUCa9Eg0JJYik/mup9Xj02tQlvXOPOFjzlbHC0jQYMcvpApi/OI3hEeY3AdH5kyO ZWAgq2ToYsLFk4Zg8mjyNRqbUaLxCR0u4nnzTIRjnqmM/qI0RSW/eMzhKOHUKDkGc5eJ r04Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="mZd+W/Zj"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m14si10943872edp.172.2021.06.14.04.14.21; Mon, 14 Jun 2021 04:14:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="mZd+W/Zj"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234071AbhFNLPE (ORCPT + 99 others); Mon, 14 Jun 2021 07:15:04 -0400 Received: from mail.kernel.org ([198.145.29.99]:39116 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234681AbhFNLCu (ORCPT ); Mon, 14 Jun 2021 07:02:50 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 26CC36143A; Mon, 14 Jun 2021 10:44:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1623667443; bh=fJqNwFQZKtb2D/b++FzjAcS0aTJDZRcHfR1c37cWSFo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mZd+W/Zj0utcWqjQpMlRYcTznUEmBhBoaVnXbwtgriZSVtZuRlvCT0t72O1GruNqV 3YyCqyl/ixpxZDEcsSft/yzO0/a0WVRhRwrJO0imyzToWA/CKFn19Ol0SL7bAlds92 dmuyZ4gbHYx98BE0vasttpbrFBKE9zmXkaE6q2Iw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Peter Chen , Jack Pham Subject: [PATCH 5.10 072/131] usb: dwc3: gadget: Bail from dwc3_gadget_exit() if dwc->gadget is NULL Date: Mon, 14 Jun 2021 12:27:13 +0200 Message-Id: <20210614102655.482158144@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210614102652.964395392@linuxfoundation.org> References: <20210614102652.964395392@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jack Pham commit 03715ea2e3dbbc56947137ce3b4ac18a726b2f87 upstream. There exists a possible scenario in which dwc3_gadget_init() can fail: during during host -> peripheral mode switch in dwc3_set_mode(), and a pending gadget driver fails to bind. Then, if the DRD undergoes another mode switch from peripheral->host the resulting dwc3_gadget_exit() will attempt to reference an invalid and dangling dwc->gadget pointer as well as call dma_free_coherent() on unmapped DMA pointers. The exact scenario can be reproduced as follows: - Start DWC3 in peripheral mode - Configure ConfigFS gadget with FunctionFS instance (or use g_ffs) - Run FunctionFS userspace application (open EPs, write descriptors, etc) - Bind gadget driver to DWC3's UDC - Switch DWC3 to host mode => dwc3_gadget_exit() is called. usb_del_gadget() will put the ConfigFS driver instance on the gadget_driver_pending_list - Stop FunctionFS application (closes the ep files) - Switch DWC3 to peripheral mode => dwc3_gadget_init() fails as usb_add_gadget() calls check_pending_gadget_drivers() and attempts to rebind the UDC to the ConfigFS gadget but fails with -19 (-ENODEV) because the FFS instance is not in FFS_ACTIVE state (userspace has not re-opened and written the descriptors yet, i.e. desc_ready!=0). - Switch DWC3 back to host mode => dwc3_gadget_exit() is called again, but this time dwc->gadget is invalid. Although it can be argued that userspace should take responsibility for ensuring that the FunctionFS application be ready prior to allowing the composite driver bind to the UDC, failure to do so should not result in a panic from the kernel driver. Fix this by setting dwc->gadget to NULL in the failure path of dwc3_gadget_init() and add a check to dwc3_gadget_exit() to bail out unless the gadget pointer is valid. Fixes: e81a7018d93a ("usb: dwc3: allocate gadget structure dynamically") Cc: Reviewed-by: Peter Chen Signed-off-by: Jack Pham Link: https://lore.kernel.org/r/20210528160405.17550-1-jackp@codeaurora.org Signed-off-by: Greg Kroah-Hartman --- drivers/usb/dwc3/gadget.c | 4 ++++ 1 file changed, 4 insertions(+) --- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -3936,6 +3936,7 @@ err5: dwc3_gadget_free_endpoints(dwc); err4: usb_put_gadget(dwc->gadget); + dwc->gadget = NULL; err3: dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, dwc->bounce_addr); @@ -3955,6 +3956,9 @@ err0: void dwc3_gadget_exit(struct dwc3 *dwc) { + if (!dwc->gadget) + return; + usb_del_gadget(dwc->gadget); dwc3_gadget_free_endpoints(dwc); usb_put_gadget(dwc->gadget);