Received: by 2002:a05:6520:2586:b029:fa:41f3:c225 with SMTP id u6csp31184lky; Wed, 9 Jun 2021 15:00:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwdbercJSFgL3QH5+laen+2lX/8+8VewKcnL06F3lsPuT6qM7f/gkXdQ78A+Nr4IlOjH/WF X-Received: by 2002:a17:906:1912:: with SMTP id a18mr1695895eje.117.1623276037165; Wed, 09 Jun 2021 15:00:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623276037; cv=none; d=google.com; s=arc-20160816; b=MCPfXpbDL2Fu4otI5l4FN5fZwVQZDnliV+CtmlIhouez3H052YreOTgkpBw0XycLPZ WtwzbZN/4FjAzUncbj+Jx+fqj5uqwkDhpCWjbJ0Iv3k68b8+reyhn/YAr0NqB25+eLqh wz4r+15CrFzLggSxkmNkeBwuENd0AZkMYnWw8s63U0xQ/soPrK6uJtHiRCr0QdiovZdE I6CdBpsTUfh+JKQAT0Vj3ev8ZINqo+ePTgp2A+inhtczs7iUYCPxbHuLtErLGkhU+E02 hjTKb8E8FLnhvcnOVfLbQT+C4NftlNrrnh+Bd7LfY7O8AP8l8+PyN/SYu7H7hiiY49hV nvZQ== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=szIH5XmBD8gBJtERHlpOkSnME50sJ1/XS/4Dv/VOoFQ=; b=pzfbKZ6Syf+IRl8Y2B6+a1tfuXCvz3nJGQG1mW3LZalbJlWsx1IpIcERjwQuSd7Pyx /GmAsgH4g42OqawT3NutejWh/35Kr82juBBBXAenlHqSHgCQOjfxqBH4Edfygx2uhzho 0fJgzjgRNViB/gQxKKtQgEltE8gmd0AUXXL7c/y2pDNwbClSNnj1vc2vj4UtyPEgCTI1 G9ml/lCKG7nQAglj9SNJrcov73L8Z2zLdxseinBtOmGSoFPJtqZp9xBsmYxX8PFMYzXH fMzCcGDkykpX9JvPkEoGiwbmiDV2tKKmaaAU+bVTh1pcD4FBXhhbvfCkk3pNVzqKD798 MYug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=lDY6B4Ms; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a8si666645ejc.334.2021.06.09.15.00.12; Wed, 09 Jun 2021 15:00:37 -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=@gmail.com header.s=20161025 header.b=lDY6B4Ms; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229740AbhFIWAp (ORCPT + 99 others); Wed, 9 Jun 2021 18:00:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229535AbhFIWAp (ORCPT ); Wed, 9 Jun 2021 18:00:45 -0400 Received: from mail-lj1-x22f.google.com (mail-lj1-x22f.google.com [IPv6:2a00:1450:4864:20::22f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F08BBC061574; Wed, 9 Jun 2021 14:58:37 -0700 (PDT) Received: by mail-lj1-x22f.google.com with SMTP id c11so1802475ljd.6; Wed, 09 Jun 2021 14:58:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=szIH5XmBD8gBJtERHlpOkSnME50sJ1/XS/4Dv/VOoFQ=; b=lDY6B4Msxh/CS2Z2CiZCDh/XeW2xwQAO7cDPG9t33BfHmYHTzXJZsagZgXBe5646Ma sVsWURHsL2PoBdYqaDbLznGgVVEzQuJotk9/GFjKPW/W2SqJ/UuERzdSLEtJd2a1XX6Q zrPjDa0pbZC8kvNf5p7Xajjaj3wcGnxK0ItYUIB0I0QLGJ3RgAgIVlz8s96MQaTAUKmL uXz8i/kn85KER//jkjtVteWNuuj7sowX0efVSa58S9NkHJupoXYEoZ9xFXt8y6VXgb7K GDEezj0wjwTMQHDqjX0uj+xeAuJWx7vEc5825XedITxDQKKzGKt9/9tkP1JXFS3WqIaD KoHQ== 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:mime-version :content-transfer-encoding; bh=szIH5XmBD8gBJtERHlpOkSnME50sJ1/XS/4Dv/VOoFQ=; b=CsX4WtSVEREw0n2tzxZWBGtzfPAsgeX+0x5Yqctc6usT43f1JS7yVX5w88GjqtHuy4 ZpJm2ybKkJbBYyyxXBQA/d1T4QHWmdXwR6RPGv24GQuiLanSeUSyFB/Hh+IIrZuegfCz piwzQuJM9Ck3SoX99rmlGXhUZoc44Gix/LUGSDaHKWQwyGeHemnydns/qsb3uURM0gvO PKDbuOlGHuwGrlsVg84zsBkVWssVcmcqW+u+cPhp8bu8KzfzkJd4BprT96DtqrMbPioF jmoTxQdh4F6CSKtxPOK536S42kAfbmNaY+J/cKFVUhxkjiYH/E4pqy/3+DWsptfxKkE8 zcEg== X-Gm-Message-State: AOAM533t/aCwRt0SV6dsaDkWpQU/tWSZDYj2ZiJZkTgdUUJsSZt/PG2M ddQrkmpNSivsEOpM8vMkn8o8HSN3QC8= X-Received: by 2002:a2e:3c0c:: with SMTP id j12mr1391782lja.131.1623275916242; Wed, 09 Jun 2021 14:58:36 -0700 (PDT) Received: from localhost.localdomain ([94.103.224.40]) by smtp.gmail.com with ESMTPSA id v26sm117156lfp.0.2021.06.09.14.58.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Jun 2021 14:58:35 -0700 (PDT) From: Pavel Skripkin To: wg@grandegger.com, mkl@pengutronix.de, davem@davemloft.net Cc: linux-can@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Pavel Skripkin , syzbot+57281c762a3922e14dfe@syzkaller.appspotmail.com Subject: [PATCH] can: mcba_usb: fix memory leak in mcba_usb Date: Thu, 10 Jun 2021 00:58:33 +0300 Message-Id: <20210609215833.30393-1-paskripkin@gmail.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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") Reported-and-tested-by: syzbot+57281c762a3922e14dfe@syzkaller.appspotmail.com Signed-off-by: Pavel Skripkin --- drivers/net/can/usb/mcba_usb.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/drivers/net/can/usb/mcba_usb.c b/drivers/net/can/usb/mcba_usb.c index 029e77dfa773..a45865bd7254 100644 --- 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_priv *priv) 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_priv *priv) } 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_priv *priv) 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_device *netdev) 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); } -- 2.31.1