Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3919881pxj; Mon, 21 Jun 2021 09:22:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzSb7/YAK5BdaDawAeXyf+z4Z+WJW4Ca2XvdIVcNSUYuULzHM4yeGQAGzXXEtgaypmz2z+l X-Received: by 2002:a05:6402:1103:: with SMTP id u3mr22727682edv.342.1624292574326; Mon, 21 Jun 2021 09:22:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624292574; cv=none; d=google.com; s=arc-20160816; b=Hlqqg2/CAMWcaXprwEQgRXNBtg2QOMDexXgYiGlax8UptxB7ihKPuOME9a0IxBf2Bd FCCI83eP+/1rgAE2wAGqXPBVRHiUvKWLlhPgIVsFV1kRrZqwCHUJasPkxOsgWUXFidrT R5CDcxHGDYN/1PV9y1DSAPSn3kUkSjkQqJM4eyQRI8Ubb8Xpe2WBsoE6Z+J1Le/0Hdzm x58TPlXAqhLARGZaN6p8wVWobrUvyPjCtvyNptVtARet6Yog58XQOA5/R2ucZW5sd1zX JmJR8xFY89e4zhWfLyQ069l/y+ixkBrgRD3gyxEcQwNdbPUJVA+k9xuzbrJ4lTKXmrsp cvQA== 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=QNbGL6KnYDmUzMu9AvmpGN/B2G1XC8oW9vMfpHtNBF4=; b=zL201LlZRbC4wQseLCgTwOrKR3wy3wrwZPkSeOFTFlx47uuayG3JeG3hTE4T5k7HlZ pahneRgpFWYh774HxJPlXgQzCxsyYSKeLd3S1LDHjc25ea7fbIwLCzH1GqYGdm6qqgKL NaGpq/WokGHYfVJNe3NM68KwyS/bZ7oulm0hz8jjKD9M0107MbRpW4K+VblVsSb1XQaH dLt2eSQSHwO37eSwGbRXCRDeR2g+tqY84Gr0wjRtZuN0Z3jL3Upwy18Wxo7Mb3L13Dft AskCt0Owo/Fpxeji69yGvHE4hmc4KRb7uxVQ52Z+PlXcBkCLIMbZwYcutaYE9RYmpzrd KwKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=2Yiuy5r+; 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 v13si8744402edc.559.2021.06.21.09.22.31; Mon, 21 Jun 2021 09:22:54 -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=2Yiuy5r+; 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 S231857AbhFUQWP (ORCPT + 99 others); Mon, 21 Jun 2021 12:22:15 -0400 Received: from mail.kernel.org ([198.145.29.99]:42142 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231645AbhFUQVI (ORCPT ); Mon, 21 Jun 2021 12:21:08 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id B7401611C1; Mon, 21 Jun 2021 16:18:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1624292334; bh=DHAxnpUEXkMbtydLit97tKQ01mF9G1yJqfRoaatq2Ys=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2Yiuy5r+cuH+SiKmakLeoby9xNZQ8w7ofO2pW+o0k/hAE9OnDAERE8/jNHObWkqD6 QVICdOLMAF3mwYEMrr/gcWIqF7npMbSZu5pAKS6GMr11iwYHqAhDnHUvanRchAO4Qe ch295COqJ4rV5P2NIjNMQ9IbVawJo0KS3ob5pyYo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Pavel Skripkin , Marc Kleine-Budde , syzbot+57281c762a3922e14dfe@syzkaller.appspotmail.com Subject: [PATCH 5.4 53/90] can: mcba_usb: fix memory leak in mcba_usb Date: Mon, 21 Jun 2021 18:15:28 +0200 Message-Id: <20210621154905.942577639@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210621154904.159672728@linuxfoundation.org> References: <20210621154904.159672728@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: Pavel Skripkin commit 91c02557174be7f72e46ed7311e3bea1939840b0 upstream. Syzbot reported memory leak in SocketCAN driver for Microchip CAN BUS Analyzer Tool. The problem was in unfreed usb_coherent. In mcba_usb_start() 20 coherent buffers are allocated and there is nothing, that frees them: 1) In callback function the urb is resubmitted and that's all 2) In disconnect function urbs are simply killed, but URB_FREE_BUFFER is not set (see mcba_usb_start) and this flag cannot be used with coherent buffers. Fail log: | [ 1354.053291][ T8413] mcba_usb 1-1:0.0 can0: device disconnected | [ 1367.059384][ T8420] kmemleak: 20 new suspected memory leaks (see /sys/kernel/debug/kmem) So, all allocated buffers should be freed with usb_free_coherent() explicitly NOTE: The same pattern for allocating and freeing coherent buffers is used in drivers/net/can/usb/kvaser_usb/kvaser_usb_core.c Fixes: 51f3baad7de9 ("can: mcba_usb: Add support for Microchip CAN BUS Analyzer") Link: https://lore.kernel.org/r/20210609215833.30393-1-paskripkin@gmail.com Cc: linux-stable Reported-and-tested-by: syzbot+57281c762a3922e14dfe@syzkaller.appspotmail.com Signed-off-by: Pavel Skripkin Signed-off-by: Marc Kleine-Budde Signed-off-by: Greg Kroah-Hartman --- drivers/net/can/usb/mcba_usb.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) --- a/drivers/net/can/usb/mcba_usb.c +++ b/drivers/net/can/usb/mcba_usb.c @@ -82,6 +82,8 @@ struct mcba_priv { bool can_ka_first_pass; bool can_speed_check; atomic_t free_ctx_cnt; + void *rxbuf[MCBA_MAX_RX_URBS]; + dma_addr_t rxbuf_dma[MCBA_MAX_RX_URBS]; }; /* CAN frame */ @@ -633,6 +635,7 @@ static int mcba_usb_start(struct mcba_pr for (i = 0; i < MCBA_MAX_RX_URBS; i++) { struct urb *urb = NULL; u8 *buf; + dma_addr_t buf_dma; /* create a URB, and a buffer for it */ urb = usb_alloc_urb(0, GFP_KERNEL); @@ -642,7 +645,7 @@ static int mcba_usb_start(struct mcba_pr } buf = usb_alloc_coherent(priv->udev, MCBA_USB_RX_BUFF_SIZE, - GFP_KERNEL, &urb->transfer_dma); + GFP_KERNEL, &buf_dma); if (!buf) { netdev_err(netdev, "No memory left for USB buffer\n"); usb_free_urb(urb); @@ -661,11 +664,14 @@ static int mcba_usb_start(struct mcba_pr if (err) { usb_unanchor_urb(urb); usb_free_coherent(priv->udev, MCBA_USB_RX_BUFF_SIZE, - buf, urb->transfer_dma); + buf, buf_dma); usb_free_urb(urb); break; } + priv->rxbuf[i] = buf; + priv->rxbuf_dma[i] = buf_dma; + /* Drop reference, USB core will take care of freeing it */ usb_free_urb(urb); } @@ -708,7 +714,14 @@ static int mcba_usb_open(struct net_devi static void mcba_urb_unlink(struct mcba_priv *priv) { + int i; + usb_kill_anchored_urbs(&priv->rx_submitted); + + for (i = 0; i < MCBA_MAX_RX_URBS; ++i) + usb_free_coherent(priv->udev, MCBA_USB_RX_BUFF_SIZE, + priv->rxbuf[i], priv->rxbuf_dma[i]); + usb_kill_anchored_urbs(&priv->tx_submitted); }