Skip to contentSkip to conversation

scsi: hpsa: Replace kmalloc + copy_from_user with memdup_user

PATCH RESEND Applied Thorsten Blum 3 participants 4 messages Sep 23, 2025 – Sep 25, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 scsi: hpsa: Replace kmalloc + copy_from_user with memdup_user

Replace kmalloc() followed by copy_from_user() with memdup_user() to improve and simplify hpsa_passthru_ioctl().

Since memdup_user() already allocates memory, use kzalloc() in the else branch instead of manually zeroing 'buff' using memset(0).

Return early if an error occurs and remove the 'out_kfree' label.

No functional changes intended.

---
 drivers/scsi/hpsa.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
drivers/scsi/hpsa.c +6 -11
index c73a71ac3c29..55448238d8ed 100644
@@ -6407,18 +6407,14 @@ static int hpsa_passthru_ioctl(struct ctlr_info *h,
 		return -EINVAL;
 	}
 	if (iocommand->buf_size > 0) {
-		buff = kmalloc(iocommand->buf_size, GFP_KERNEL);
-		if (buff == NULL)
-			return -ENOMEM;
 		if (iocommand->Request.Type.Direction & XFER_WRITE) {
-			/* Copy the data into the buffer we created */
-			if (copy_from_user(buff, iocommand->buf,
-				iocommand->buf_size)) {
-				rc = -EFAULT;
-				goto out_kfree;
-			}
+			buff = memdup_user(iocommand->buf, iocommand->buf_size);
+			if (IS_ERR(buff))
+				return PTR_ERR(buff);
 		} else {
-			memset(buff, 0, iocommand->buf_size);
+			buff = kzalloc(iocommand->buf_size, GFP_KERNEL);
+			if (!buff)
+				return -ENOMEM;
 		}
 	}
 	c = cmd_alloc(h);
@@ -6478,7 +6474,6 @@ static int hpsa_passthru_ioctl(struct ctlr_info *h,
 	}
 out:
 	cmd_free(h, c);
-out_kfree:
 	kfree(buff);
 	return rc;
 }
Signature · 1 line
2.51.0
maintainer Acked-by on v1 1/1
#1 lore raw

________________________________________
From: Thorsten Blum <[email protected]>
Sent: Tuesday, September 23, 2025 12:15 PM
To: Don Brace - C33706 <[email protected]>; James E.J. Bottomley <[email protected]>; Martin K. Petersen <[email protected]>
Cc: Thorsten Blum <[email protected]>; storagedev <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]> Subject: [PATCH RESEND] scsi: hpsa: Replace kmalloc + copy_from_user with memdup_user

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 improve and simplify hpsa_passthru_ioctl().

Since memdup_user() already allocates memory, use kzalloc() in the else branch instead of manually zeroing 'buff' using memset(0).

Return early if an error occurs and remove the 'out_kfree' label.

No functional changes intended.

---

Thanks for your patch.

We do not normally make changes to this driver as it is in maintenance mode.

#2 lore raw

Hi Don,

Quoted 4 lines · On 23. Sep 2025, at 21:40, [email protected] wrote:

...
Thanks for your patch.

We do not normally make changes to this driver as it is in maintenance mode.

Is there a general way to check if a driver is in maintenance mode?

Did you get a chance to look at my other patch [1] yet, which fixes a potential memory leak in this driver? That's probably more important than the refactorings.

Thanks,
Thorsten

[1] https://lore.kernel.org/lkml/[email protected]/

#3 lore raw
In reply to Thorsten Blum

Thorsten,

Quoted 7 lines

Replace kmalloc() followed by copy_from_user() with memdup_user() to improve and simplify hpsa_passthru_ioctl().

Since memdup_user() already allocates memory, use kzalloc() in the else branch instead of manually zeroing 'buff' using memset(0).

Return early if an error occurs and remove the 'out_kfree' label.

Applied to 6.18/scsi-staging, thanks!

Signature · 1 line
Martin K. Petersen