Skip to contentSkip to conversation

scsi: bfa: Replace kzalloc + copy_from_user with memdup_user

PATCH In review Thorsten Blum 2 participants 4 messages Sep 18, 2025 – Sep 29, 2025

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

PATCH 1/1 scsi: bfa: Replace kzalloc + copy_from_user with memdup_user

Replace kzalloc() followed by copy_from_user() with memdup_user() to improve and simplify bfad_im_bsg_els_ct_request(). memdup_user() returns either -ENOMEM or -EFAULT (instead of -EIO) if an error occurs.

Use u64_to_user_ptr() instead of manually casting 'bsg_data->payload'.

No functional changes intended other than returning the more idiomatic error code -EFAULT.

---
 drivers/scsi/bfa/bfad_bsg.c | 19 ++++---------------
 1 file changed, 4 insertions(+), 15 deletions(-)
drivers/scsi/bfa/bfad_bsg.c +4 -15
index 54bd11e6d593..d90dfee95160 100644
@@ -3392,21 +3392,10 @@ bfad_im_bsg_els_ct_request(struct bsg_job *job)
 	if (bsg_data == NULL)
 		goto out;
 
-	/*
-	 * Allocate buffer for bsg_fcpt and do a copy_from_user op for payload
-	 * buffer of size bsg_data->payload_len
-	 */
-	bsg_fcpt = kzalloc(bsg_data->payload_len, GFP_KERNEL);
-	if (!bsg_fcpt) {
-		rc = -ENOMEM;
-		goto out;
-	}
-
-	if (copy_from_user((uint8_t *)bsg_fcpt,
-				(void *)(unsigned long)bsg_data->payload,
-				bsg_data->payload_len)) {
-		kfree(bsg_fcpt);
-		rc = -EIO;
+	bsg_fcpt = memdup_user(u64_to_user_ptr(bsg_data->payload),
+			       bsg_data->payload_len);
+	if (IS_ERR(bsg_fcpt)) {
+		rc = PTR_ERR(bsg_fcpt);
 		goto out;
 	}
 
Signature · 1 line
2.51.0
#1 lore raw

Thorsten,

Quoted 2 lines

No functional changes intended other than returning the more idiomatic error code -EFAULT.

How can we be sure that this doesn't break existing applications?

Signature · 1 line
Martin K. Petersen
#2 lore raw

Hi Martin,

Quoted 5 lines · Martin K. Petersen
Quoted 3 lines

No functional changes intended other than returning the more idiomatic error code -EFAULT.

How can we be sure that this doesn't break existing applications?

I guess we can't because the error code is returned to userspace as the result of a bsg_job? That wasn't immediately obvious to me.

Thanks,
Thorsten

#3 lore raw

Hi Thorsten!

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

No functional changes intended other than returning the more idiomatic error code -EFAULT.

How can we be sure that this doesn't break existing applications?

I guess we can't because the error code is returned to userspace as the result of a bsg_job? That wasn't immediately obvious to me.

Correct. And with little chance of having the associated management utilities updated, it's best to leave things as they are.

Signature · 1 line
Martin K. Petersen