Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3939843pxj; Mon, 21 Jun 2021 09:48:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxOJ7TKzX8TUlIGy7508azytjI99hU/IZBURKbMNEwLV83m2eva7EWoKbYkb+f/Uf5cN2Uq X-Received: by 2002:a05:6e02:12c3:: with SMTP id i3mr19397455ilm.234.1624294129295; Mon, 21 Jun 2021 09:48:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624294129; cv=none; d=google.com; s=arc-20160816; b=CG6egH+ddwbJ1jRW8bEyXm4SGOBFZkLEj1VC+8S3rdvJ0aHHUAEg/BdbMn1zrqOn5m F8vhFD4u3KWPUgdh+PC9Ni/Pkk/QILGCd0sEx5NxOPljLmEHapmCbAzvjPwVgAFB3pQT BzGuVYdKvYERsIyLm4sakBUupFFBBbQMWCzfpVhrTphYUqMsEV35c0LymN/10o+WU3bk FNZG6yLVOze4VZdrPoA12sH4zpKSbLXLasHekKb2ais8QlYvD95BiTCYrZG+FIk6WdQ9 Uq9LujH2CBGTMVjcvexl+IWvv3lDx0daBC5VTiaqDaohZRPyhdFz8ax04DAPpiA28BVp CXHw== 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=KYgAuBRe02fCChVonGZq3X2Z/ola1NyLZ40outOWU3CgywQ7z7sbdh/qPaRziK7nEu IN+rP2Nykf6TAdmSHZY+rW9ousBoo+FP4PI3b8aJrhQIMN4vKFQIYQ3GLTMqSO/rw7IB iu2WvMchHls1PSF5wfmaepfI/Bnf62odnnTKD1MpRDrJbGA5bK0xnlwyiyOU0ALzLqC0 ZNe+IxL9IU3JhdG6mEigrIRMBG1/K/dPrvrvoHmblOEiljKC+v74QfKeS2BEdJbcDcRr sX3ybBZVN5ihJk2ACd97+HWSIDRJqaQUataAqIeohe3gXfyNH0VrWrd3WISEs+TneAUq D5rw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="Oj/Ffgyp"; 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 v1si4540703iot.18.2021.06.21.09.48.37; Mon, 21 Jun 2021 09:48:49 -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="Oj/Ffgyp"; 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 S233514AbhFUQtr (ORCPT + 99 others); Mon, 21 Jun 2021 12:49:47 -0400 Received: from mail.kernel.org ([198.145.29.99]:38068 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233882AbhFUQpn (ORCPT ); Mon, 21 Jun 2021 12:45:43 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id CE938613C9; Mon, 21 Jun 2021 16:32:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1624293160; bh=DHAxnpUEXkMbtydLit97tKQ01mF9G1yJqfRoaatq2Ys=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Oj/Ffgyp9iVO6DxyZBOwHTLFpjqOqMne0m1HRaQuxx0IOlqakH1O2rSsU9EJpt5Oy IrSNWRZ3qA7PemMn6uhS0kSEY34sc2q2VwCN+uNxBu67c//CaqYlbY3MNB0K18UatV GDceZ9ZEwIEb/02KnpkePhrdA15fP3YsJZDBY5qQ= 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.12 120/178] can: mcba_usb: fix memory leak in mcba_usb Date: Mon, 21 Jun 2021 18:15:34 +0200 Message-Id: <20210621154926.864191222@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210621154921.212599475@linuxfoundation.org> References: <20210621154921.212599475@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); }