Skip to content

Commit 5de4a65

Browse files
Adam Cozzettegregkh
Adam Cozzette
authored andcommitted
ums_realtek: do not use stack memory for DMA
commit 065e609 upstream. This patch changes rts51x_read_mem, rts51x_write_mem, and rts51x_read_status to allocate temporary buffers with kmalloc. This way stack addresses are not used for DMA when these functions call rts51x_bulk_transport. Signed-off-by: Adam Cozzette <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 3401c77 commit 5de4a65

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

drivers/usb/storage/realtek_cr.c

+30-5
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,11 @@ static int rts51x_read_mem(struct us_data *us, u16 addr, u8 *data, u16 len)
320320
{
321321
int retval;
322322
u8 cmnd[12] = { 0 };
323+
u8 *buf;
324+
325+
buf = kmalloc(len, GFP_NOIO);
326+
if (buf == NULL)
327+
return USB_STOR_TRANSPORT_ERROR;
323328

324329
US_DEBUGP("%s, addr = 0x%x, len = %d\n", __func__, addr, len);
325330

@@ -331,17 +336,27 @@ static int rts51x_read_mem(struct us_data *us, u16 addr, u8 *data, u16 len)
331336
cmnd[5] = (u8) len;
332337

333338
retval = rts51x_bulk_transport(us, 0, cmnd, 12,
334-
data, len, DMA_FROM_DEVICE, NULL);
335-
if (retval != USB_STOR_TRANSPORT_GOOD)
339+
buf, len, DMA_FROM_DEVICE, NULL);
340+
if (retval != USB_STOR_TRANSPORT_GOOD) {
341+
kfree(buf);
336342
return -EIO;
343+
}
337344

345+
memcpy(data, buf, len);
346+
kfree(buf);
338347
return 0;
339348
}
340349

341350
static int rts51x_write_mem(struct us_data *us, u16 addr, u8 *data, u16 len)
342351
{
343352
int retval;
344353
u8 cmnd[12] = { 0 };
354+
u8 *buf;
355+
356+
buf = kmalloc(len, GFP_NOIO);
357+
if (buf == NULL)
358+
return USB_STOR_TRANSPORT_ERROR;
359+
memcpy(buf, data, len);
345360

346361
US_DEBUGP("%s, addr = 0x%x, len = %d\n", __func__, addr, len);
347362

@@ -353,7 +368,8 @@ static int rts51x_write_mem(struct us_data *us, u16 addr, u8 *data, u16 len)
353368
cmnd[5] = (u8) len;
354369

355370
retval = rts51x_bulk_transport(us, 0, cmnd, 12,
356-
data, len, DMA_TO_DEVICE, NULL);
371+
buf, len, DMA_TO_DEVICE, NULL);
372+
kfree(buf);
357373
if (retval != USB_STOR_TRANSPORT_GOOD)
358374
return -EIO;
359375

@@ -365,17 +381,26 @@ static int rts51x_read_status(struct us_data *us,
365381
{
366382
int retval;
367383
u8 cmnd[12] = { 0 };
384+
u8 *buf;
385+
386+
buf = kmalloc(len, GFP_NOIO);
387+
if (buf == NULL)
388+
return USB_STOR_TRANSPORT_ERROR;
368389

369390
US_DEBUGP("%s, lun = %d\n", __func__, lun);
370391

371392
cmnd[0] = 0xF0;
372393
cmnd[1] = 0x09;
373394

374395
retval = rts51x_bulk_transport(us, lun, cmnd, 12,
375-
status, len, DMA_FROM_DEVICE, actlen);
376-
if (retval != USB_STOR_TRANSPORT_GOOD)
396+
buf, len, DMA_FROM_DEVICE, actlen);
397+
if (retval != USB_STOR_TRANSPORT_GOOD) {
398+
kfree(buf);
377399
return -EIO;
400+
}
378401

402+
memcpy(status, buf, len);
403+
kfree(buf);
379404
return 0;
380405
}
381406

0 commit comments

Comments
 (0)