mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] scsi: lpfc: fix heap overflow in lpfc_bsg_diag_loopback_run()
@ 2026-04-22  9:18 Junrui Luo
  2026-04-22 13:28 ` James Bottomley
  0 siblings, 1 reply; 3+ messages in thread
From: Junrui Luo @ 2026-04-22  9:18 UTC (permalink / raw)
  To: Justin Tee, Paul Ely, James E.J. Bottomley, Martin K. Petersen,
	James Smart, James Bottomley
  Cc: linux-scsi, linux-kernel, Yuhao Jiang, stable, Junrui Luo

lpfc_bsg_diag_loopback_run() allocates dataout as a staging buffer
before copying loopback test data into DMA segments. When the
user-supplied payload size exceeds 64KB, the allocation is capped at
64KB while sg_copy_to_buffer() and the subsequent memcpy loop operate
on the full payload (up to 80 * 4096 bytes).

This leads to a heap buffer overflow with user-controlled data.

Fix by allocating full_size in the large-size path, consistent with the
small-size path and with diag_cmd_data_alloc().

Fixes: 3b5dd52aaffd ("[SCSI] lpfc 8.3.8: (BSG4) Add new vendor specific BSG Commands")
Reported-by: Yuhao Jiang <danisjiang@gmail.com>
Cc: stable@vger.kernel.org
Signed-off-by: Junrui Luo <moonafterrain@outlook.com>
---
 drivers/scsi/lpfc/lpfc_bsg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/scsi/lpfc/lpfc_bsg.c b/drivers/scsi/lpfc/lpfc_bsg.c
index 7406dfa60016..83fef256f324 100644
--- a/drivers/scsi/lpfc/lpfc_bsg.c
+++ b/drivers/scsi/lpfc/lpfc_bsg.c
@@ -3111,7 +3111,7 @@ lpfc_bsg_diag_loopback_run(struct bsg_job *job)
 		if (size <= (64 * 1024))
 			total_mem = full_size;
 		else
-			total_mem = 64 * 1024;
+			total_mem = full_size;
 	} else
 		/* Allocate memory for ioctl data */
 		total_mem = BUF_SZ_4K;

---
base-commit: 7aaa8047eafd0bd628065b15757d9b48c5f9c07d
change-id: 20260422-fixes-4b4edb0b7e88

Best regards,
-- 
Junrui Luo <moonafterrain@outlook.com>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] scsi: lpfc: fix heap overflow in lpfc_bsg_diag_loopback_run()
  2026-04-22  9:18 [PATCH] scsi: lpfc: fix heap overflow in lpfc_bsg_diag_loopback_run() Junrui Luo
@ 2026-04-22 13:28 ` James Bottomley
  2026-04-23  7:45   ` Junrui Luo
  0 siblings, 1 reply; 3+ messages in thread
From: James Bottomley @ 2026-04-22 13:28 UTC (permalink / raw)
  To: Junrui Luo, Justin Tee, Paul Ely, Martin K. Petersen,
	James Smart, James Bottomley
  Cc: linux-scsi, linux-kernel, Yuhao Jiang, stable

On Wed, 2026-04-22 at 17:18 +0800, Junrui Luo wrote:
> lpfc_bsg_diag_loopback_run() allocates dataout as a staging buffer
> before copying loopback test data into DMA segments. When the
> user-supplied payload size exceeds 64KB, the allocation is capped at
> 64KB while sg_copy_to_buffer() and the subsequent memcpy loop operate
> on the full payload (up to 80 * 4096 bytes).

Are you sure?  The comment above the code you're changing says:

/*
 * Allocate memory for ioctl data. If buffer is bigger than 64k,
 * then we allocate 64k and re-use that buffer over and over to
 * xfer the whole block. This is because Linux kernel has a
 * problem allocating more than 120k of kernel space memory. Saw
 * problem with GET_FCPTARGETMAPPING...
 */

Which implies the intention is to allocate only 64kb if the payload
exceeds that and break the work into 64kb or less sized chunks.  So if
there is a problem here, it sounds like the loop over 64k chunks would
need fixing rather than the allocation.

> 
> This leads to a heap buffer overflow with user-controlled data.
> 
> Fix by allocating full_size in the large-size path, consistent with
> the small-size path and with diag_cmd_data_alloc().
> 
> Fixes: 3b5dd52aaffd ("[SCSI] lpfc 8.3.8: (BSG4) Add new vendor
> specific BSG Commands")
> Reported-by: Yuhao Jiang <danisjiang@gmail.com>
> Cc: stable@vger.kernel.org
> Signed-off-by: Junrui Luo <moonafterrain@outlook.com>
> ---
>  drivers/scsi/lpfc/lpfc_bsg.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/scsi/lpfc/lpfc_bsg.c
> b/drivers/scsi/lpfc/lpfc_bsg.c
> index 7406dfa60016..83fef256f324 100644
> --- a/drivers/scsi/lpfc/lpfc_bsg.c
> +++ b/drivers/scsi/lpfc/lpfc_bsg.c
> @@ -3111,7 +3111,7 @@ lpfc_bsg_diag_loopback_run(struct bsg_job *job)
>  		if (size <= (64 * 1024))
>  			total_mem = full_size;
>  		else
> -			total_mem = 64 * 1024;
> +			total_mem = full_size;

This would render the if clause useless because the branches have the
same statement.

Regards,

James


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] scsi: lpfc: fix heap overflow in lpfc_bsg_diag_loopback_run()
  2026-04-22 13:28 ` James Bottomley
@ 2026-04-23  7:45   ` Junrui Luo
  0 siblings, 0 replies; 3+ messages in thread
From: Junrui Luo @ 2026-04-23  7:45 UTC (permalink / raw)
  To: James Bottomley
  Cc: Justin Tee, Paul Ely, Martin K. Petersen, James Smart,
	James Bottomley, linux-scsi, linux-kernel, Yuhao Jiang, stable

On Wed, Apr 22, 2026 at 09:28:03AM -0400, James Bottomley wrote:
> Are you sure?  The comment above the code you're changing says:
> 
> /*
>  * Allocate memory for ioctl data. If buffer is bigger than 64k,
>  * then we allocate 64k and re-use that buffer over and over to
>  * xfer the whole block. This is because Linux kernel has a
>  * problem allocating more than 120k of kernel space memory. Saw
>  * problem with GET_FCPTARGETMAPPING...
>  */
> 
> Which implies the intention is to allocate only 64kb if the payload
> exceeds that and break the work into 64kb or less sized chunks.  So if
> there is a problem here, it sounds like the loop over 64k chunks would
> need fixing rather than the allocation.
> 

The chunked transfer described in the comment was never implemented.
sg_copy_to_buffer() at line 3127 copies the full size bytes into
dataout in one shot, and the list_for_each_entry loop reads from it
linearly without any chunked reuse.

The comment's concern about "Linux kernel has a problem allocating more
than 120k" dates from 2010. Modern kmalloc supports up to
4MB (KMALLOC_MAX_SIZE in include/linux/slab.h).

> This would render the if clause useless because the branches have the
> same statement.

I'll send a v2 removing the branch and the outdated comment.

Thanks,
Junrui Luo

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-04-23  7:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-04-22  9:18 [PATCH] scsi: lpfc: fix heap overflow in lpfc_bsg_diag_loopback_run() Junrui Luo
2026-04-22 13:28 ` James Bottomley
2026-04-23  7:45   ` Junrui Luo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®