Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3927907pxj; Mon, 21 Jun 2021 09:33:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzQlOaj+FqbjRHMQ2cMtl0fGjHMDpcrsh4Me1HR30x4TXTaGbpsr5+7UUpsupec8WOuyWkC X-Received: by 2002:a6b:7110:: with SMTP id q16mr20402221iog.53.1624293204239; Mon, 21 Jun 2021 09:33:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624293204; cv=none; d=google.com; s=arc-20160816; b=Pn8rJbKCc6NKFFkVuYn2Xi4vslkMgGWHcZq9rmI6m2klghtJ44GfhEeSYup18VhdP1 amdjmmYfdrBTJcTzt4zNqw+S4+nMM6d4+KA0tznDr+75QCyg3CVfFZZFEAjVXA+7ETOk cL8wae22CIIteKQZbPpU/I9P188SMDQPmjYtNoZ86Uy1llbIvVQPFeMh04twwVzXFms6 SBqvhCwFwxU63bvQEf75wjXzk0eVCK3eBBTnVmSAT3l2I999VfzgAff0SSUCfH0Batrw GHgE4JqYkUSlRePJ7oiiozd1gyXorJvKbca+fxbIm039EdfPunefCoGDOxhFdYsoFHug z4mA== 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=DKdtuYzjMA+8nzR9Ac3bXBlYq2LKlRiQfQYfeSrS4P+7ztMAwUVuvDO9CMM3CkvJ5S 8h1Z9XSZ9LirwBm9X5Gcd8Q2PTgTggL1i+BR9UJKhhxqMOfM8zJba8BigrieSZ5p1Uhv RbyuxBSZgLEKuI7Xj/LHY/ffPb8r4B58O1RWS+CjyH+f3uGkhRvMZnoZHptKKnytBBOy rHnbg48i2QHL0yUzmVyO+PiUl24sm/mNy2xi/v3kWVJ/p22N6qRanNU+XNniefmMFvVk fz8USNy1cdwZEl0QPbdJiAv4YQFVqI9ety3tqYO6rGj0q82lCUvw19f7LhaF5aaVD7pO YE6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=sg+SpjWZ; 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 l1si12755926ilk.81.2021.06.21.09.33.10; Mon, 21 Jun 2021 09:33:24 -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=sg+SpjWZ; 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 S232022AbhFUQd7 (ORCPT + 99 others); Mon, 21 Jun 2021 12:33:59 -0400 Received: from mail.kernel.org ([198.145.29.99]:48950 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232256AbhFUQab (ORCPT ); Mon, 21 Jun 2021 12:30:31 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 46D406135D; Mon, 21 Jun 2021 16:25:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1624292711; bh=DHAxnpUEXkMbtydLit97tKQ01mF9G1yJqfRoaatq2Ys=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sg+SpjWZVEsyIjD3D6b05ks4PnkwgoIcqS9+j1CXEiRZ0DYP7nnEBzfA2t7aMaZuQ f/40k+z635zzk5CDTi7aSjbaGnnlVSFfyG6+zibLXcDauvp+0CA9uKfnF1TogrOwAi DbcH1ImeEJiPA/quXqVqdYUQ+IC8qzJCVFjoN2Ss= 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.10 101/146] can: mcba_usb: fix memory leak in mcba_usb Date: Mon, 21 Jun 2021 18:15:31 +0200 Message-Id: <20210621154917.646889748@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210621154911.244649123@linuxfoundation.org> References: <20210621154911.244649123@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); }