Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1313620yba; Tue, 2 Apr 2019 06:42:19 -0700 (PDT) X-Google-Smtp-Source: APXvYqyU2fMjFwabJSJzFNfuIwpocAzH6qPUz2hBEOLxRobLL3YvGVO9Zhbobea1rtZegL7kcYfg X-Received: by 2002:a63:5d04:: with SMTP id r4mr4772755pgb.117.1554212539098; Tue, 02 Apr 2019 06:42:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554212539; cv=none; d=google.com; s=arc-20160816; b=B6tgyGIyqN7qg+UJnXY+BcjgcWtoDislKDxyzPLdjPULtEt91GPhgNM2M5TqlBe2FU BBKDovpRIvG4s2UodYCQwA4QYZZNe2wa8NYqKr5uJ6gfedv3M65Yp7iVfmwJCm4EvFR6 wD2Zc1TuvQiYfYWylJm6aVpjHtBBtTbyuLNNaAzSlT6SJFMvQB4pPrwd6ITUT/3IrsKv KS9/VaYSJaN/m2YgC3L9J5s0VSp5tN/2pn/+PETnbmUnlYqb10dlp3FwpiLdUwFttlrw +7DsfcV9gIgO8Bv7rOIZtPewMeIZjdteybUUq+mZB00Ibnt5x014CFf86clz3C2HBTU+ AK7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition; bh=voIz1BlZTzvwFcBifDYq+vMyyWLJNiYMQwpDtcK7uu4=; b=DGAh59fMISaMyDfUsAVC4ow0shnQPQChIa/IVGPRJIq9KZw9XZHTaHIR0Dc4PsJ5GN qIZRzN3V/pEaobcjXU4ZDlt9XrlqKnAwSZ/co5mRJTQPmopEsW3ulVGILhiy/jUHidNS pmQM+eoHSrq3qJkv/Ljct2uB9GBPgnhSqS9GWB02PMBTyFhDjXeUO9/LNb8ESiYFAwYG IKjrTD+AWMrLNXNBUmqEwBXF+8cVRgcc1DIoFG/4ZpfcFvPSmmLMCq6THffFi1/9aubB IE4FjGn7meWtPjbJyyY5B/BcC0rrGB817BDmGc0lMFP8ugTL+Yc2yfPmA9whur9dOVG3 5qXg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t3si4476923plq.181.2019.04.02.06.42.03; Tue, 02 Apr 2019 06:42:19 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731777AbfDBNkt (ORCPT + 99 others); Tue, 2 Apr 2019 09:40:49 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:43806 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731552AbfDBNkO (ORCPT ); Tue, 2 Apr 2019 09:40:14 -0400 Received: from [167.98.27.226] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1hBJe6-0002oa-Re; Tue, 02 Apr 2019 14:40:11 +0100 Received: from ben by deadeye with local (Exim 4.92) (envelope-from ) id 1hBJdw-0004v1-Sg; Tue, 02 Apr 2019 14:40:00 +0100 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, Denis Kirjanov , "Greg Kroah-Hartman" , "Jia-Ju Bai" Date: Tue, 02 Apr 2019 14:38:27 +0100 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) X-Patchwork-Hint: ignore Subject: [PATCH 3.16 57/99] usb: r8a66597: Fix a possible concurrency use-after-free bug in r8a66597_endpoint_disable() In-Reply-To: X-SA-Exim-Connect-IP: 167.98.27.226 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.65-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Jia-Ju Bai commit c85400f886e3d41e69966470879f635a2b50084c upstream. The function r8a66597_endpoint_disable() and r8a66597_urb_enqueue() may be concurrently executed. The two functions both access a possible shared variable "hep->hcpriv". This shared variable is freed by r8a66597_endpoint_disable() via the call path: r8a66597_endpoint_disable kfree(hep->hcpriv) (line 1995 in Linux-4.19) This variable is read by r8a66597_urb_enqueue() via the call path: r8a66597_urb_enqueue spin_lock_irqsave(&r8a66597->lock) init_pipe_info enable_r8a66597_pipe pipe = hep->hcpriv (line 802 in Linux-4.19) The read operation is protected by a spinlock, but the free operation is not protected by this spinlock, thus a concurrency use-after-free bug may occur. To fix this bug, the spin-lock and spin-unlock function calls in r8a66597_endpoint_disable() are moved to protect the free operation. Signed-off-by: Jia-Ju Bai Signed-off-by: Greg Kroah-Hartman Signed-off-by: Ben Hutchings --- drivers/usb/host/r8a66597-hcd.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) --- a/drivers/usb/host/r8a66597-hcd.c +++ b/drivers/usb/host/r8a66597-hcd.c @@ -1990,6 +1990,8 @@ static int r8a66597_urb_dequeue(struct u static void r8a66597_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep) +__acquires(r8a66597->lock) +__releases(r8a66597->lock) { struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd); struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv; @@ -2002,13 +2004,14 @@ static void r8a66597_endpoint_disable(st return; pipenum = pipe->info.pipenum; + spin_lock_irqsave(&r8a66597->lock, flags); if (pipenum == 0) { kfree(hep->hcpriv); hep->hcpriv = NULL; + spin_unlock_irqrestore(&r8a66597->lock, flags); return; } - spin_lock_irqsave(&r8a66597->lock, flags); pipe_stop(r8a66597, pipe); pipe_irq_disable(r8a66597, pipenum); disable_irq_empty(r8a66597, pipenum);