Skip to content

Commit b812965

Browse files
tobluxmartinkpetersen
authored andcommitted
scsi: hpsa: Fix potential memory leak in hpsa_big_passthru_ioctl()
Replace kmalloc() followed by copy_from_user() with memdup_user() to fix a memory leak that occurs when copy_from_user(buff[sg_used],,) fails and the 'cleanup1:' path does not free the memory for 'buff[sg_used]'. Using memdup_user() avoids this by freeing the memory internally. Since memdup_user() already allocates memory, use kzalloc() in the else branch instead of manually zeroing 'buff[sg_used]' using memset(0). Cc: [email protected] Fixes: edd1636 ("[SCSI] hpsa: add driver for HP Smart Array controllers.") Signed-off-by: Thorsten Blum <[email protected]> Acked-by: Don Brace <[email protected]> Signed-off-by: Martin K. Petersen <[email protected]>
1 parent 88e8acf commit b812965

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

drivers/scsi/hpsa.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6517,18 +6517,21 @@ static int hpsa_big_passthru_ioctl(struct ctlr_info *h,
65176517
while (left) {
65186518
sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
65196519
buff_size[sg_used] = sz;
6520-
buff[sg_used] = kmalloc(sz, GFP_KERNEL);
6521-
if (buff[sg_used] == NULL) {
6522-
status = -ENOMEM;
6523-
goto cleanup1;
6524-
}
6520+
65256521
if (ioc->Request.Type.Direction & XFER_WRITE) {
6526-
if (copy_from_user(buff[sg_used], data_ptr, sz)) {
6527-
status = -EFAULT;
6522+
buff[sg_used] = memdup_user(data_ptr, sz);
6523+
if (IS_ERR(buff[sg_used])) {
6524+
status = PTR_ERR(buff[sg_used]);
65286525
goto cleanup1;
65296526
}
6530-
} else
6531-
memset(buff[sg_used], 0, sz);
6527+
} else {
6528+
buff[sg_used] = kzalloc(sz, GFP_KERNEL);
6529+
if (!buff[sg_used]) {
6530+
status = -ENOMEM;
6531+
goto cleanup1;
6532+
}
6533+
}
6534+
65326535
left -= sz;
65336536
data_ptr += sz;
65346537
sg_used++;

0 commit comments

Comments
 (0)