Skip to contentSkip to conversation

scsi: smartpqi: Replace kmalloc + copy_from_user with memdup_user

PATCH Applied Thorsten Blum 3 participants 4 messages Sep 22, 2025 – Sep 30, 2025

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

PATCH 1/1 scsi: smartpqi: Replace kmalloc + copy_from_user with memdup_user

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

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

Return early if an error occurs. No functional changes intended.

---
 drivers/scsi/smartpqi/smartpqi_init.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
drivers/scsi/smartpqi/smartpqi_init.c +8 -9
index 125944941601..03c97e60d36f 100644
@@ -20,6 +20,7 @@
 #include <linux/reboot.h>
 #include <linux/cciss_ioctl.h>
 #include <linux/crash_dump.h>
+#include <linux/string.h>
 #include <scsi/scsi_host.h>
 #include <scsi/scsi_cmnd.h>
 #include <scsi/scsi_device.h>
@@ -6774,17 +6775,15 @@ static int pqi_passthru_ioctl(struct pqi_ctrl_info *ctrl_info, void __user *arg)
 	}
 
 	if (iocommand.buf_size > 0) {
-		kernel_buffer = kmalloc(iocommand.buf_size, GFP_KERNEL);
-		if (!kernel_buffer)
-			return -ENOMEM;
 		if (iocommand.Request.Type.Direction & XFER_WRITE) {
-			if (copy_from_user(kernel_buffer, iocommand.buf,
-				iocommand.buf_size)) {
-				rc = -EFAULT;
-				goto out;
-			}
+			kernel_buffer = memdup_user(iocommand.buf,
+						    iocommand.buf_size);
+			if (IS_ERR(kernel_buffer))
+				return PTR_ERR(kernel_buffer);
 		} else {
-			memset(kernel_buffer, 0, iocommand.buf_size);
+			kernel_buffer = kzalloc(iocommand.buf_size, GFP_KERNEL);
+			if (!kernel_buffer)
+				return -ENOMEM;
 		}
 	}
 
Signature · 1 line
2.51.0
maintainer Acked-by on v1 1/1
#1 lore raw

________________________________________
From: Thorsten Blum <[email protected]>
Sent: Monday, September 22, 2025 3:18 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] scsi: smartpqi: 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 simplify and improve pqi_passthru_ioctl().

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

Return early if an error occurs.  No functional changes intended.

---

Sorry about duplicate e-mail, forgot to switch to text-only.

#2 lore raw
In reply to Thorsten Blum

Thorsten,

Quoted 8 lines

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

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

Return early if an error occurs. No functional changes intended.

Applied to 6.18/scsi-staging, thanks!

Signature · 1 line
Martin K. Petersen
#3 lore raw
In reply to Thorsten Blum
Quoted 9 lines · Thorsten Blum

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

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

Return early if an error occurs. No functional changes intended.

[...]

Applied to 6.18/scsi-queue, thanks!

[1/1] scsi: smartpqi: Replace kmalloc + copy_from_user with memdup_user

      https://git.kernel.org/mkp/scsi/c/0ac3c901fbeb
Signature · 1 line
Martin K. Petersen