Skip to content

Commit ac83631

Browse files
lwfingergregkh
authored andcommitted
staging: r8712: Fix memory leak in _r8712_init_xmit_priv()
In the above mentioned routine, memory is allocated in several places. If the first succeeds and a later one fails, the routine will leak memory. This patch fixes commit 2865d42 ("staging: r8712u: Add the new driver to the mainline kernel"). A potential memory leak in r8712_xmit_resource_alloc() is also addressed. Fixes: 2865d42 ("staging: r8712u: Add the new driver to the mainline kernel") Reported-by: syzbot+cf71097ffb6755df8251@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/x/log.txt?x=11ac3fa0a80000 Cc: stable@vger.kernel.org Cc: Nam Cao <namcaov@gmail.com> Signed-off-by: Larry Finger <Larry.Finger@lwfinger.net> Reviewed-by: Nam Cao <namcaov@gmail.com> Link: https://lore.kernel.org/r/20230714175417.18578-1-Larry.Finger@lwfinger.net Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 6eaae19 commit ac83631

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

drivers/staging/rtl8712/rtl871x_xmit.c

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "osdep_intf.h"
2222
#include "usb_ops.h"
2323

24+
#include <linux/usb.h>
2425
#include <linux/ieee80211.h>
2526

2627
static const u8 P802_1H_OUI[P80211_OUI_LEN] = {0x00, 0x00, 0xf8};
@@ -55,6 +56,7 @@ int _r8712_init_xmit_priv(struct xmit_priv *pxmitpriv,
5556
sint i;
5657
struct xmit_buf *pxmitbuf;
5758
struct xmit_frame *pxframe;
59+
int j;
5860

5961
memset((unsigned char *)pxmitpriv, 0, sizeof(struct xmit_priv));
6062
spin_lock_init(&pxmitpriv->lock);
@@ -117,25 +119,26 @@ int _r8712_init_xmit_priv(struct xmit_priv *pxmitpriv,
117119
_init_queue(&pxmitpriv->pending_xmitbuf_queue);
118120
pxmitpriv->pallocated_xmitbuf =
119121
kmalloc(NR_XMITBUFF * sizeof(struct xmit_buf) + 4, GFP_ATOMIC);
120-
if (!pxmitpriv->pallocated_xmitbuf) {
121-
kfree(pxmitpriv->pallocated_frame_buf);
122-
pxmitpriv->pallocated_frame_buf = NULL;
123-
return -ENOMEM;
124-
}
122+
if (!pxmitpriv->pallocated_xmitbuf)
123+
goto clean_up_frame_buf;
125124
pxmitpriv->pxmitbuf = pxmitpriv->pallocated_xmitbuf + 4 -
126125
((addr_t)(pxmitpriv->pallocated_xmitbuf) & 3);
127126
pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmitbuf;
128127
for (i = 0; i < NR_XMITBUFF; i++) {
129128
INIT_LIST_HEAD(&pxmitbuf->list);
130129
pxmitbuf->pallocated_buf =
131130
kmalloc(MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ, GFP_ATOMIC);
132-
if (!pxmitbuf->pallocated_buf)
133-
return -ENOMEM;
131+
if (!pxmitbuf->pallocated_buf) {
132+
j = 0;
133+
goto clean_up_alloc_buf;
134+
}
134135
pxmitbuf->pbuf = pxmitbuf->pallocated_buf + XMITBUF_ALIGN_SZ -
135136
((addr_t) (pxmitbuf->pallocated_buf) &
136137
(XMITBUF_ALIGN_SZ - 1));
137-
if (r8712_xmit_resource_alloc(padapter, pxmitbuf))
138-
return -ENOMEM;
138+
if (r8712_xmit_resource_alloc(padapter, pxmitbuf)) {
139+
j = 1;
140+
goto clean_up_alloc_buf;
141+
}
139142
list_add_tail(&pxmitbuf->list,
140143
&(pxmitpriv->free_xmitbuf_queue.queue));
141144
pxmitbuf++;
@@ -146,6 +149,28 @@ int _r8712_init_xmit_priv(struct xmit_priv *pxmitpriv,
146149
init_hwxmits(pxmitpriv->hwxmits, pxmitpriv->hwxmit_entry);
147150
tasklet_setup(&pxmitpriv->xmit_tasklet, r8712_xmit_bh);
148151
return 0;
152+
153+
clean_up_alloc_buf:
154+
if (j) {
155+
/* failure happened in r8712_xmit_resource_alloc()
156+
* delete extra pxmitbuf->pallocated_buf
157+
*/
158+
kfree(pxmitbuf->pallocated_buf);
159+
}
160+
for (j = 0; j < i; j++) {
161+
int k;
162+
163+
pxmitbuf--; /* reset pointer */
164+
kfree(pxmitbuf->pallocated_buf);
165+
for (k = 0; k < 8; k++) /* delete xmit urb's */
166+
usb_free_urb(pxmitbuf->pxmit_urb[k]);
167+
}
168+
kfree(pxmitpriv->pallocated_xmitbuf);
169+
pxmitpriv->pallocated_xmitbuf = NULL;
170+
clean_up_frame_buf:
171+
kfree(pxmitpriv->pallocated_frame_buf);
172+
pxmitpriv->pallocated_frame_buf = NULL;
173+
return -ENOMEM;
149174
}
150175

151176
void _free_xmit_priv(struct xmit_priv *pxmitpriv)

drivers/staging/rtl8712/xmit_linux.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ int r8712_xmit_resource_alloc(struct _adapter *padapter,
112112
for (i = 0; i < 8; i++) {
113113
pxmitbuf->pxmit_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
114114
if (!pxmitbuf->pxmit_urb[i]) {
115+
int k;
116+
117+
for (k = i - 1; k >= 0; k--) {
118+
/* handle allocation errors part way through loop */
119+
usb_free_urb(pxmitbuf->pxmit_urb[k]);
120+
}
115121
netdev_err(padapter->pnetdev, "pxmitbuf->pxmit_urb[i] == NULL\n");
116122
return -ENOMEM;
117123
}

0 commit comments

Comments
 (0)