scsi: hpsa: Fix potential memory leak in hpsa_big_passthru_ioctl()
Subsystems (3)
PATCH RESEND 1/1 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).
--- drivers/scsi/hpsa.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-)
drivers/scsi/hpsa.c +12 -9
@@ -6522,18 +6522,21 @@ static int hpsa_big_passthru_ioctl(struct ctlr_info *h,while (left) { sz = (left > ioc->malloc_size) ? ioc->malloc_size : left; buff_size[sg_used] = sz; - buff[sg_used] =kmalloc (sz,GFP_KERNEL ); -if (buff[sg_used] ==NULL ) { - status = -ENOMEM ; - goto cleanup1; - } +if (ioc->Request.Type.Direction &XFER_WRITE ) { -if (copy_from_user (buff[sg_used], data_ptr, sz)) { - status = -EFAULT ; + buff[sg_used] =memdup_user (data_ptr, sz); +if (IS_ERR (buff[sg_used])) { + status =PTR_ERR (buff[sg_used]); goto cleanup1; } - }else -memset (buff[sg_used],0 , sz); + }else { + buff[sg_used] =kzalloc (sz,GFP_KERNEL ); +if (!buff[sg_used]) { + status = -ENOMEM ; + goto cleanup1; + } + } + left -= sz; data_ptr += sz; sg_used++;
Signature · 1 line
2.51.0
________________________________________
From: Thorsten Blum <[email protected]>
Sent: Friday, September 19, 2025 4:26 AM
To: Don Brace - C33706 <[email protected]>; James E.J. Bottomley <[email protected]>; Martin K. Petersen <[email protected]>; Mike Miller <[email protected]>; James Bottomley <[email protected]>; Andrew Morton <[email protected]>; Alex Chiang <[email protected]>; Stephen M. Cameron <[email protected]> Cc: Thorsten Blum <[email protected]>; [email protected] <[email protected]>; storagedev <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>
Subject: [PATCH RESEND] scsi: hpsa: Fix potential memory leak in hpsa_big_passthru_ioctl()
EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
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).
Thanks for your patch.
Martin K. Petersen
AppliedThorsten,
Quoted 9 lines
Replace
kmalloc()followed bycopy_from_user()withmemdup_user()to fix a memory leak that occurs whencopy_from_user(buff[sg_used],,)fails and the 'cleanup1:' path does not free the memory for 'buff[sg_used]'. Usingmemdup_user()avoids this by freeing the memory internally.Since
memdup_user()already allocates memory, usekzalloc()in the else branch instead of manually zeroing 'buff[sg_used]' using memset(0).
Applied to 6.18/scsi-staging, thanks!
Signature · 1 line
Martin K. Petersen