Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1836037pxb; Mon, 18 Jan 2021 00:51:03 -0800 (PST) X-Google-Smtp-Source: ABdhPJx5SwVIQ69g12cj8rahYMW81D32BGiumUAdkc3rQ3jt2UPq3fjnEBHobjj3VnyIoOpQPmqZ X-Received: by 2002:a17:906:32d6:: with SMTP id k22mr16709784ejk.457.1610959863404; Mon, 18 Jan 2021 00:51:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610959863; cv=none; d=google.com; s=arc-20160816; b=NVXks9eU0Xk7WTJACWux01Y3Drpn3lgXY5vd+nol77lr4dFgACWyTWnPCAtReeeubD K2D09MSjVGsdAGCdU9jcX/U98F8ODNWyraIP3vSkhEABQb90+5EMN9vQbqfdsgdTj3w9 1KGD9n/P/rJ5i7ibyuBYYGrht4f6vR6SAG4nWAh6Ks/W3YWyDuAaxB+tLGav6vD46QmM rp3ZFNwmpFyUQ6B0fec+bNNhKcTBbDFc+PuVB1eJxal5N33KnkUbdwN1Z7DgHVEhlSzS WglG6O/rjCvYdE9XpRzIFE34ogGylfiVLRN3RgO4+skUjES1gWEKXst2qBkbnMYnieDl j4Zg== 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 :dkim-signature; bh=OyQZB4rl5Zar0wRlUg3JZS81nsXMFf2BksOtPVpSskk=; b=bqXjNNG1cwKazF0G4qeSGbOG77R5ciMKp0yYdKGNb59jZyBUr58hB0t7NAUZypcQJr 6DDEu+93mHs2s1bLGIkyInhgp8CANhAj5PK0jjGSl65LOsgRdW7OnTqKuhaj6k3NbFwF njBPyI1mlXEplYlVuBO/VUyMb9veuOq9wvXMysdRdZBLzE4PiSJQLtyUQVoKJwiizjlJ htnKNgznlqez6pYYn9gVsV3KeWxROo5dFouR00STxdtiVrg3CvyDDc21vhp3WRvH/JJ3 v8AMPvuB+DxKmJ1TncC2ViCYQQAn19HQG7aQg5aKky17hgU0MvEZCUINOE4JyQXHfKNb EeKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=1eDIvqm2; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ks11si889760ejb.186.2021.01.18.00.50.39; Mon, 18 Jan 2021 00:51:03 -0800 (PST) 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=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=1eDIvqm2; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387940AbhARIsP (ORCPT + 99 others); Mon, 18 Jan 2021 03:48:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387903AbhARIrp (ORCPT ); Mon, 18 Jan 2021 03:47:45 -0500 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5060CC0613C1 for ; Mon, 18 Jan 2021 00:47:04 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id 6so8297493wri.3 for ; Mon, 18 Jan 2021 00:47:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=OyQZB4rl5Zar0wRlUg3JZS81nsXMFf2BksOtPVpSskk=; b=1eDIvqm2DYBQvrlEqrZOdoWFbDIRO661ivbAjhUrNDhelTRzl7byhn20hOJ85oJkbL b7zrCpvOOaWz6/ecVYNyQ9+YN9D55OrwQgAjzIWATqfremCD8/lk8X6iMYKtdaGnvSpb m+eLJP37fOXg5Z0WUwwbqFmRw748VjyEYfp7ApQHwSF41krg9wAExURz+5ScqvdOk4mg WzRgr/PPaFcn8eUOi8Mq95V0H/jF4fvilivYDzZ1uZurTW9RNtjk+3d23p/fsUcPaqGC ZzchM2EtTLBQOpYSu5VimxKpLNG042e1NYdZb7kDM3Zlw5rxjQmIwQVl4Z05XVJB7vZB xujQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=OyQZB4rl5Zar0wRlUg3JZS81nsXMFf2BksOtPVpSskk=; b=U+DoOLrIfi9BLR3h2G2l0RtbXfXwTqBRns50jNRDpS3sKRwuF64qv/BdV+Xsy7N3el M9w2jODh2FPRv+o77KwCzeLZXPo/B3FTtnfEW49Iw16rCV1x7+GXLSLrz6NABvF/QFGs FADJ36Y25go3fjtlRlIkdsUC/psFca8o15wp2TK2hd6CZX5XmIF/1RmEeMLj63bF6t5V VVbW+SZYwdFXMDPJVKvzcI+Jiuz8PsG4TS/liT+rjNMeK1rqSsTY6mH76CylC60YPx+i upAojU/mHQkKfuEdOwUbzVsvM+VUV53bn9GJazLWQ0kuEZ/eM+pw4Qa7evWCmkF7ftzV nxmQ== X-Gm-Message-State: AOAM530IcRFroZLTwxKyzYG8wlrdClhBDOMSL+z0BH0oXHhgOVY7Dvfe ULS2Ofz/Tcwtsv7CjVWv0AKO1g== X-Received: by 2002:a5d:6cd4:: with SMTP id c20mr24712137wrc.57.1610959622969; Mon, 18 Jan 2021 00:47:02 -0800 (PST) Received: from starbuck.lan (82-65-169-74.subs.proxad.net. [82.65.169.74]) by smtp.googlemail.com with ESMTPSA id y63sm26224518wmd.21.2021.01.18.00.47.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Jan 2021 00:47:02 -0800 (PST) From: Jerome Brunet To: Felipe Balbi , Greg Kroah-Hartman Cc: Jack Pham , Ruslan Bilovol , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Ferry Toth , Peter Chen , Jerome Brunet Subject: [PATCH v3 1/4] usb: gadget: u_audio: Free requests only after callback Date: Mon, 18 Jan 2021 09:46:39 +0100 Message-Id: <20210118084642.322510-2-jbrunet@baylibre.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210118084642.322510-1-jbrunet@baylibre.com> References: <20210118084642.322510-1-jbrunet@baylibre.com> MIME-Version: 1.0 X-Patchwork-Bot: notify Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jack Pham As per the kernel doc for usb_ep_dequeue(), it states that "this routine is asynchronous, that is, it may return before the completion routine runs". And indeed since v5.0 the dwc3 gadget driver updated its behavior to place dequeued requests on to a cancelled list to be given back later after the endpoint is stopped. The free_ep() was incorrectly assuming that a request was ready to be freed after calling dequeue which results in a use-after-free in dwc3 when it traverses its cancelled list. Fix this by moving the usb_ep_free_request() call to the callback itself in case the ep is disabled. Fixes: eb9fecb9e69b0 ("usb: gadget: f_uac2: split out audio core") Reported-and-tested-by: Ferry Toth Reviewed-and-tested-by: Peter Chen Signed-off-by: Jack Pham Signed-off-by: Jerome Brunet Acked-by: Felipe Balbi --- drivers/usb/gadget/function/u_audio.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/drivers/usb/gadget/function/u_audio.c b/drivers/usb/gadget/function/u_audio.c index e6d32c536781..908e49dafd62 100644 --- a/drivers/usb/gadget/function/u_audio.c +++ b/drivers/usb/gadget/function/u_audio.c @@ -89,7 +89,12 @@ static void u_audio_iso_complete(struct usb_ep *ep, struct usb_request *req) struct snd_uac_chip *uac = prm->uac; /* i/f shutting down */ - if (!prm->ep_enabled || req->status == -ESHUTDOWN) + if (!prm->ep_enabled) { + usb_ep_free_request(ep, req); + return; + } + + if (req->status == -ESHUTDOWN) return; /* @@ -336,8 +341,14 @@ static inline void free_ep(struct uac_rtd_params *prm, struct usb_ep *ep) for (i = 0; i < params->req_number; i++) { if (prm->ureq[i].req) { - usb_ep_dequeue(ep, prm->ureq[i].req); - usb_ep_free_request(ep, prm->ureq[i].req); + if (usb_ep_dequeue(ep, prm->ureq[i].req)) + usb_ep_free_request(ep, prm->ureq[i].req); + /* + * If usb_ep_dequeue() cannot successfully dequeue the + * request, the request will be freed by the completion + * callback. + */ + prm->ureq[i].req = NULL; } } -- 2.29.2