From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
stable@vger.kernel.org, Rhett Aultman <rhett.aultman@samsara.com>,
Marc Kleine-Budde <mkl@pengutronix.de>
Subject: [PATCH 4.19 05/31] can: gs_usb: gs_usb_open/close(): fix memory leak
Date: Mon, 11 Jul 2022 11:06:44 +0200 [thread overview]
Message-ID: <20220711090538.004891604@linuxfoundation.org> (raw)
In-Reply-To: <20220711090537.841305347@linuxfoundation.org>
From: Rhett Aultman <rhett.aultman@samsara.com>
commit 2bda24ef95c0311ab93bda00db40486acf30bd0a upstream.
The gs_usb driver appears to suffer from a malady common to many USB
CAN adapter drivers in that it performs usb_alloc_coherent() to
allocate a number of USB request blocks (URBs) for RX, and then later
relies on usb_kill_anchored_urbs() to free them, but this doesn't
actually free them. As a result, this may be leaking DMA memory that's
been used by the driver.
This commit is an adaptation of the techniques found in the esd_usb2
driver where a similar design pattern led to a memory leak. It
explicitly frees the RX URBs and their DMA memory via a call to
usb_free_coherent(). Since the RX URBs were allocated in the
gs_can_open(), we remove them in gs_can_close() rather than in the
disconnect function as was done in esd_usb2.
For more information, see the 928150fad41b ("can: esd_usb2: fix memory
leak").
Link: https://lore.kernel.org/all/alpine.DEB.2.22.394.2206031547001.1630869@thelappy
Fixes: d08e973a77d1 ("can: gs_usb: Added support for the GS_USB CAN devices")
Cc: stable@vger.kernel.org
Signed-off-by: Rhett Aultman <rhett.aultman@samsara.com>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
drivers/net/can/usb/gs_usb.c | 23 +++++++++++++++++++++--
1 file changed, 21 insertions(+), 2 deletions(-)
--- a/drivers/net/can/usb/gs_usb.c
+++ b/drivers/net/can/usb/gs_usb.c
@@ -192,6 +192,8 @@ struct gs_can {
struct usb_anchor tx_submitted;
atomic_t active_tx_urbs;
+ void *rxbuf[GS_MAX_RX_URBS];
+ dma_addr_t rxbuf_dma[GS_MAX_RX_URBS];
};
/* usb interface struct */
@@ -600,6 +602,7 @@ static int gs_can_open(struct net_device
for (i = 0; i < GS_MAX_RX_URBS; i++) {
struct urb *urb;
u8 *buf;
+ dma_addr_t buf_dma;
/* alloc rx urb */
urb = usb_alloc_urb(0, GFP_KERNEL);
@@ -610,7 +613,7 @@ static int gs_can_open(struct net_device
buf = usb_alloc_coherent(dev->udev,
sizeof(struct gs_host_frame),
GFP_KERNEL,
- &urb->transfer_dma);
+ &buf_dma);
if (!buf) {
netdev_err(netdev,
"No memory left for USB buffer\n");
@@ -618,6 +621,8 @@ static int gs_can_open(struct net_device
return -ENOMEM;
}
+ urb->transfer_dma = buf_dma;
+
/* fill, anchor, and submit rx urb */
usb_fill_bulk_urb(urb,
dev->udev,
@@ -641,10 +646,17 @@ static int gs_can_open(struct net_device
rc);
usb_unanchor_urb(urb);
+ usb_free_coherent(dev->udev,
+ sizeof(struct gs_host_frame),
+ buf,
+ buf_dma);
usb_free_urb(urb);
break;
}
+ dev->rxbuf[i] = buf;
+ dev->rxbuf_dma[i] = buf_dma;
+
/* Drop reference,
* USB core will take care of freeing it
*/
@@ -709,13 +721,20 @@ static int gs_can_close(struct net_devic
int rc;
struct gs_can *dev = netdev_priv(netdev);
struct gs_usb *parent = dev->parent;
+ unsigned int i;
netif_stop_queue(netdev);
/* Stop polling */
parent->active_channels--;
- if (!parent->active_channels)
+ if (!parent->active_channels) {
usb_kill_anchored_urbs(&parent->rx_submitted);
+ for (i = 0; i < GS_MAX_RX_URBS; i++)
+ usb_free_coherent(dev->udev,
+ sizeof(struct gs_host_frame),
+ dev->rxbuf[i],
+ dev->rxbuf_dma[i]);
+ }
/* Stop sending URBs */
usb_kill_anchored_urbs(&dev->tx_submitted);
next prev parent reply other threads:[~2022-07-11 9:11 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-11 9:06 [PATCH 4.19 00/31] 4.19.252-rc1 review Greg Kroah-Hartman
2022-07-11 9:06 ` [PATCH 4.19 01/31] esp: limit skb_page_frag_refill use to a single page Greg Kroah-Hartman
2022-07-11 9:06 ` [PATCH 4.19 02/31] mm/slub: add missing TID updates on slab deactivation Greg Kroah-Hartman
2022-07-11 9:06 ` [PATCH 4.19 03/31] can: bcm: use call_rcu() instead of costly synchronize_rcu() Greg Kroah-Hartman
2022-07-11 9:06 ` [PATCH 4.19 04/31] can: grcan: grcan_probe(): remove extra of_node_get() Greg Kroah-Hartman
2022-07-11 9:06 ` Greg Kroah-Hartman [this message]
2022-07-11 9:06 ` [PATCH 4.19 06/31] usbnet: fix memory leak in error case Greg Kroah-Hartman
2022-07-11 9:06 ` [PATCH 4.19 07/31] net: rose: fix UAF bug caused by rose_t0timer_expiry Greg Kroah-Hartman
2022-07-11 9:06 ` [PATCH 4.19 08/31] iommu/vt-d: Fix PCI bus rescan device hot add Greg Kroah-Hartman
2022-07-11 9:06 ` [PATCH 4.19 09/31] fbcon: Disallow setting font bigger than screen size Greg Kroah-Hartman
2022-07-11 9:06 ` [PATCH 4.19 10/31] video: of_display_timing.h: include errno.h Greg Kroah-Hartman
2022-07-11 9:06 ` [PATCH 4.19 11/31] powerpc/powernv: delay rng platform device creation until later in boot Greg Kroah-Hartman
2022-07-11 9:06 ` [PATCH 4.19 12/31] can: kvaser_usb: replace run-time checks with struct kvaser_usb_driver_info Greg Kroah-Hartman
2022-07-11 9:06 ` [PATCH 4.19 13/31] can: kvaser_usb: kvaser_usb_leaf: fix CAN clock frequency regression Greg Kroah-Hartman
2022-07-11 9:06 ` [PATCH 4.19 14/31] can: kvaser_usb: kvaser_usb_leaf: fix bittiming limits Greg Kroah-Hartman
2022-07-11 9:06 ` [PATCH 4.19 15/31] xfs: remove incorrect ASSERT in xfs_rename Greg Kroah-Hartman
2022-07-11 9:06 ` [PATCH 4.19 16/31] ARM: meson: Fix refcount leak in meson_smp_prepare_cpus Greg Kroah-Hartman
2022-07-11 9:06 ` [PATCH 4.19 17/31] pinctrl: sunxi: a83t: Fix NAND function name for some pins Greg Kroah-Hartman
2022-07-11 9:06 ` [PATCH 4.19 18/31] ARM: at91: pm: use proper compatible for sama5d2s rtc Greg Kroah-Hartman
2022-07-11 9:06 ` [PATCH 4.19 19/31] ibmvnic: Properly dispose of all skbs during a failover Greg Kroah-Hartman
2022-07-11 9:06 ` [PATCH 4.19 20/31] selftests: forwarding: fix flood_unicast_test when h2 supports IFF_UNICAST_FLT Greg Kroah-Hartman
2022-07-11 9:07 ` [PATCH 4.19 21/31] selftests: forwarding: fix learning_test when h1 " Greg Kroah-Hartman
2022-07-11 9:07 ` [PATCH 4.19 22/31] selftests: forwarding: fix error message in learning_test Greg Kroah-Hartman
2022-07-11 9:07 ` [PATCH 4.19 23/31] i2c: cadence: Unregister the clk notifier in error path Greg Kroah-Hartman
2022-07-11 9:07 ` [PATCH 4.19 24/31] misc: rtsx_usb: fix use of dma mapped buffer for usb bulk transfer Greg Kroah-Hartman
2022-07-11 9:07 ` [PATCH 4.19 25/31] misc: rtsx_usb: use separate command and response buffers Greg Kroah-Hartman
2022-07-11 9:07 ` [PATCH 4.19 26/31] misc: rtsx_usb: set return value in rsp_buf alloc err path Greg Kroah-Hartman
2022-07-11 9:07 ` [PATCH 4.19 27/31] ida: dont use BUG_ON() for debugging Greg Kroah-Hartman
2022-07-11 9:07 ` [PATCH 4.19 28/31] dmaengine: pl330: Fix lockdep warning about non-static key Greg Kroah-Hartman
2022-07-11 9:07 ` [PATCH 4.19 29/31] dmaengine: at_xdma: handle errors of at_xdmac_alloc_desc() correctly Greg Kroah-Hartman
2022-07-11 9:07 ` [PATCH 4.19 30/31] dmaengine: ti: Fix refcount leak in ti_dra7_xbar_route_allocate Greg Kroah-Hartman
2022-07-11 9:07 ` [PATCH 4.19 31/31] dmaengine: ti: Add missing put_device " Greg Kroah-Hartman
2022-07-11 11:07 ` [PATCH 4.19 00/31] 4.19.252-rc1 review Pavel Machek
2022-07-12 1:11 ` Guenter Roeck
2022-07-12 2:44 ` Shuah Khan
2022-07-12 6:09 ` Naresh Kamboju
2022-07-12 14:48 ` Sudip Mukherjee (Codethink)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220711090538.004891604@linuxfoundation.org \
--to=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mkl@pengutronix.de \
--cc=rhett.aultman@samsara.com \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox