Skip to content

Commit 7b2ef1a

Browse files
tobluxgregkh
authored andcommitted
scsi: hpsa: Fix potential memory leak in hpsa_big_passthru_ioctl()
commit b812965 upstream. 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: stable@vger.kernel.org Fixes: edd1636 ("[SCSI] hpsa: add driver for HP Smart Array controllers.") Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev> Acked-by: Don Brace <don.brace@microchip.com> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 305b1a3 commit 7b2ef1a

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
@@ -6528,18 +6528,21 @@ static int hpsa_big_passthru_ioctl(struct ctlr_info *h,
65286528
while (left) {
65296529
sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
65306530
buff_size[sg_used] = sz;
6531-
buff[sg_used] = kmalloc(sz, GFP_KERNEL);
6532-
if (buff[sg_used] == NULL) {
6533-
status = -ENOMEM;
6534-
goto cleanup1;
6535-
}
6531+
65366532
if (ioc->Request.Type.Direction & XFER_WRITE) {
6537-
if (copy_from_user(buff[sg_used], data_ptr, sz)) {
6538-
status = -EFAULT;
6533+
buff[sg_used] = memdup_user(data_ptr, sz);
6534+
if (IS_ERR(buff[sg_used])) {
6535+
status = PTR_ERR(buff[sg_used]);
65396536
goto cleanup1;
65406537
}
6541-
} else
6542-
memset(buff[sg_used], 0, sz);
6538+
} else {
6539+
buff[sg_used] = kzalloc(sz, GFP_KERNEL);
6540+
if (!buff[sg_used]) {
6541+
status = -ENOMEM;
6542+
goto cleanup1;
6543+
}
6544+
}
6545+
65436546
left -= sz;
65446547
data_ptr += sz;
65456548
sg_used++;

0 commit comments

Comments
 (0)