[PATCH] scsi: scsi_debug: Replace kzalloc() + copy_from_user() with memdup_user_nul()

Thorsten Blum posted 1 patch 4 days, 5 hours ago
drivers/scsi/scsi_debug.c | 11 +++--------
1 file changed, 3 insertions(+), 8 deletions(-)
[PATCH] scsi: scsi_debug: Replace kzalloc() + copy_from_user() with memdup_user_nul()
Posted by Thorsten Blum 4 days, 5 hours ago
Replace kzalloc() followed by copy_from_user() with memdup_user_nul() to
improve and simplify sdebug_error_write().

No functional changes intended.

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
 drivers/scsi/scsi_debug.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

diff --git a/drivers/scsi/scsi_debug.c b/drivers/scsi/scsi_debug.c
index 353cb60e1abe..83d49c58b11d 100644
--- a/drivers/scsi/scsi_debug.c
+++ b/drivers/scsi/scsi_debug.c
@@ -1155,14 +1155,9 @@ static ssize_t sdebug_error_write(struct file *file, const char __user *ubuf,
 	struct sdebug_err_inject *inject;
 	struct scsi_device *sdev = (struct scsi_device *)file->f_inode->i_private;
 
-	buf = kzalloc(count + 1, GFP_KERNEL);
-	if (!buf)
-		return -ENOMEM;
-
-	if (copy_from_user(buf, ubuf, count)) {
-		kfree(buf);
-		return -EFAULT;
-	}
+	buf = memdup_user_nul(ubuf, count);
+	if (IS_ERR(buf))
+		return PTR_ERR(buf);
 
 	if (buf[0] == '-')
 		return sdebug_err_remove(sdev, buf, count);
-- 
2.51.0
Re: [PATCH] scsi: scsi_debug: Replace kzalloc() + copy_from_user() with memdup_user_nul()
Posted by Bart Van Assche 3 days, 23 hours ago
On 9/5/25 3:31 AM, Thorsten Blum wrote:
> Replace kzalloc() followed by copy_from_user() with memdup_user_nul() to
> improve and simplify sdebug_error_write().
Reviewed-by: Bart Van Assche <bvanassche@acm.org>