Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp519103iob; Fri, 13 May 2022 06:52:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyhHz+WmZV1SSlcVM/s+sv0wbM9Wgmltq1rIUR6MXOCriPjW8XEq4iPAHTlSFINxZeXEgpx X-Received: by 2002:a17:907:6e07:b0:6f4:d185:9f57 with SMTP id sd7-20020a1709076e0700b006f4d1859f57mr4395052ejc.668.1652449929833; Fri, 13 May 2022 06:52:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652449929; cv=none; d=google.com; s=arc-20160816; b=P/TIcch50Sw3WcqdFVjr2rV6sJwVkq9LNUK5lSIhITNuQYPVYGgNXXA3nIz4HuxtZp WNAi5GiJ1cLqXmMur8aWjR/FMxoBrxvReoMyxjCSp0Kh+NPrheOidxUiivgWylouh5L/ bYh9CluQSeNFTvEVIAbU/WFBX7h7nTPIuQ0P+nOs2sAv1d0MQuHhslXgjlxOr3s5UeZZ CFUcFg8qmjxqEYhefi0azYBMM8q2R0/fyYC/a6oOP4jRrx/j9zSuOOrB2haflyTTzDgn fvwNGuR4KF8bQOoPvdxeyM/RwdyEZ1sOVAr1+qRIy1XZlZ9Dv428IWtbkdA2CVAGIGNy DtAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:mime-version:message-id:date :dkim-signature; bh=rc/b171eEbEHOcZ7/65TQW8GF+vL+OBn1WLQqFshNME=; b=HWAXJcKhGVjo3hZSZgSfrhSwaqD+HbJJ0Mf9dsPfTOrCy9MV5HNm5ApCAkw9p0773m fdCznIEgy0YQY5cpX4d6pKG+Iy13z8TC52USSUMlYw52DpHQAc4lbZmCZM3AojDzW+QF pd3CfsrSj8Joiw9n8U0LcDbMDXpVWhU8RY8VgP55Vgi/HdAkiEQdDaE1Q+Naicvc7toY mS6pddUC/TcKX/6hKk/ZXMHpwKHR7f3gj5NZYTvWNfF7eXtX/mG59o7u/BnBATdLTI/2 Jhvny6EOEs/GodEM1Vo1dbMfHCNFm2IPsAUkMzLU9BEAk3yZRSEto7PM6xMaDNHr0qi9 MHHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Dc8S7aaR; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h6-20020a17090619c600b006df7808f11fsi1870134ejd.913.2022.05.13.06.51.37; Fri, 13 May 2022 06:52: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=@google.com header.s=20210112 header.b=Dc8S7aaR; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347564AbiELEhz (ORCPT + 99 others); Thu, 12 May 2022 00:37:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347548AbiELEht (ORCPT ); Thu, 12 May 2022 00:37:49 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 50A23286C0 for ; Wed, 11 May 2022 21:37:48 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id x4-20020a1709028ec400b0015e84d42eaaso2146923plo.7 for ; Wed, 11 May 2022 21:37:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=rc/b171eEbEHOcZ7/65TQW8GF+vL+OBn1WLQqFshNME=; b=Dc8S7aaRKMlDer6zT72Yq6vWe2t3Mq53+blU/I4gv0bMRmn+5rx/JqZmKkcleUXrKz R38rBX7mdy/ho+yWIPY/2vqvH/geclVvPd3FleGn2jf79tXm5aPpOBiddmPlvM74/otT 7c/QXDAjLGEM7m8boPKze/sT+FgHHPZlmX3ercRPeG+3X/Vy1M/FgYuZoHil3VrbbhbR k8iJYy+xf6nOa6NtRlpKOtzWi+0m7C9g1owr7IG4tVbFJgX2hG5hA08QihzctvkocFqH jcBxpHBKPfSsqP5+1YKp/KrxXYtiQD9lvvRMoqhp1lJmUMnC+rpn9kEXrnOQHoxQOCBb U05Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=rc/b171eEbEHOcZ7/65TQW8GF+vL+OBn1WLQqFshNME=; b=6cIJxwbPpntXROxgO1cyE2SmYyIZspeKZSbgK9WQQ7CNyTxs0Jhv//xKpRX63aRo95 Z2UDuEmJsbhyVmMYugQLxzMwvah3apfgfJCTAnp30xEHtQu/YemsvXzcmR4m5L9Xnn60 /aMng9NGYyC5+WTVYFWoskYIvvJd8CdrY9NGdFUk5243EIkcWGQd6nOEIqjuaZ2PrDBf qoUOMD5ftYON05wgSdFasESLmm7nxz11zT3AJE13wHNTb4ZmDnk/Dvzi7bAsdcIG7k2z CKBvG2tGO41iAKzjUa6e5KaKrD36pySdHGzY5Z6eQ0avrskJHJZ9Qah/xVRWwRn7wAXh IY0A== X-Gm-Message-State: AOAM530Tjns90T5NgV0jvpYw8/CTWmNMg+DTZZJFoEm2kBeVnVy8Ksu4 kJcReiUMi2x3i859PnFg3WtBtrUVarrex2k3Y8o= X-Received: from albertccwang.ntc.corp.google.com ([2401:fa00:fc:202:8983:2d2c:f62b:626]) (user=albertccwang job=sendgmr) by 2002:a62:e819:0:b0:510:693e:b20c with SMTP id c25-20020a62e819000000b00510693eb20cmr28178767pfi.60.1652330266640; Wed, 11 May 2022 21:37:46 -0700 (PDT) Date: Thu, 12 May 2022 12:37:39 +0800 Message-Id: <20220512043739.1234857-1-albertccwang@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.36.0.550.gb090851708-goog Subject: [PATCH v2] usb: dwc3: gadget: Move null pinter check after window closed From: Albert Wang To: balbi@kernel.org, gregkh@linuxfoundation.org, quic_jackp@quicinc.com Cc: badhri@google.com, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Albert Wang Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,HK_RANDOM_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL 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 After inspecting further, we do see the locking is implicit, with the main gotcha being the unlock/re-lock. That creates a window for a race to happen. This change moves the NULL check to be adjacent to where to it's used and after the window is "closed". Fixes: 26288448120b ("usb: dwc3: gadget: Fix null pointer exception") Signed-off-by: Albert Wang --- drivers/usb/dwc3/gadget.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c index 19477f4bbf54..fda58951cf27 100644 --- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -3366,14 +3366,19 @@ static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep, struct dwc3 *dwc = dep->dwc; bool no_started_trb = true; - if (!dep->endpoint.desc) - return no_started_trb; - + /* + * This function eventually leads to dwc3_giveback() which unlocks + * the dwc->lock and relocks afterwards. This actually creates a + * a window for a race to happen. + */ dwc3_gadget_ep_cleanup_completed_requests(dep, event, status); if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) goto out; + if (!dep->endpoint.desc) + return no_started_trb; + if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && list_empty(&dep->started_list) && (list_empty(&dep->pending_list) || status == -EXDEV)) -- 2.36.0.550.gb090851708-goog