mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] scsi: megaraid: Fix uninitialized DMA handle and passthru length TOCTOU
@ 2026-11-19 17:05 Ian Rogers
  0 siblings, 0 replies; only message in thread
From: Ian Rogers @ 2026-11-19 17:05 UTC (permalink / raw)
  To: Kashyap Desai, Sumit Saxena, Shivasharan S, Chandrakanth patil,
	James E.J. Bottomley, Martin K. Petersen
  Cc: megaraidlinux.pdl, linux-scsi, linux-kernel, llvm, Elkin Cruz,
	Ian Rogers

In megadev_ioctl(), data_dma_hndl is declared uninitialized. On the DCMD
path, when uioc.xferlen == 0, dma_alloc_coherent() is skipped, yet
mc.xferaddr = (u32)data_dma_hndl unconditionally writes 32 bits of
uninitialized kernel stack into the mailbox command sent to the adapter.

On the MEGA_MBOXCMD_PASSTHRU path, pthru resides in coherent DMA memory
shared with the controller, and pthru->dataxferlen is re-read after
mega_internal_command() for both copy_to_user() and dma_free_coherent().
If the device modifies pthru->dataxferlen in coherent memory, the cleanup
path can over-read data or pass a mismatched size or uninitialized
data_dma_hndl to dma_free_coherent().

Initialize data_dma_hndl to 0 and snapshot pthru->dataxferlen into a local
variable once copied from userspace.

Found via an x86_64 LLVM=1 (Clang 21.1.8) allyesconfig audit with
-Wconditional-uninitialized enabled; fix and changelog drafted with an AI
coding assistant. Build-tested under LLVM=1 W=1 and
-Wconditional-uninitialized; not tested on legacy MegaRAID hardware.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Ian Rogers <irogers@google.com>
---
 drivers/scsi/megaraid.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/drivers/scsi/megaraid.c b/drivers/scsi/megaraid.c
index a4079866f747..970e9b2ec781 100644
--- a/drivers/scsi/megaraid.c
+++ b/drivers/scsi/megaraid.c
@@ -2966,7 +2966,7 @@ megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
 	mega_passthru	*pthru;		/* copy user passthru here */
 	dma_addr_t	pthru_dma_hndl;
 	void		*data = NULL;	/* data to be transferred */
-	dma_addr_t	data_dma_hndl;	/* dma handle for data xfer area */
+	dma_addr_t	data_dma_hndl = 0; /* dma handle for data xfer area */
 	megacmd_t	mc;
 #if MEGA_HAVE_STATS
 	megastat_t	__user *ustats = NULL;
@@ -3137,6 +3137,7 @@ megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
 		/* Is it a passthru command or a DCMD */
 		if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
 			/* Passthru commands */
+			u32 dataxferlen;
 
 			pthru = dma_alloc_coherent(&pdev->dev,
 						   sizeof(mega_passthru),
@@ -3170,9 +3171,10 @@ megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
 			/*
 			 * Is there a data transfer
 			 */
-			if( pthru->dataxferlen ) {
+			dataxferlen = pthru->dataxferlen;
+			if (dataxferlen) {
 				data = dma_alloc_coherent(&pdev->dev,
-							  pthru->dataxferlen,
+							  dataxferlen,
 							  &data_dma_hndl,
 							  GFP_KERNEL);
 
@@ -3199,12 +3201,13 @@ megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
 			/*
 			 * Is data coming down-stream
 			 */
-			if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
+			if (dataxferlen && (uioc.flags & UIOC_WR)) {
 				/*
 				 * Get the user data
 				 */
-				if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
-							pthru->dataxferlen) ) {
+				if (copy_from_user(data,
+						   (char __user *)(unsigned long)uxferaddr,
+						   dataxferlen)) {
 					rval = (-EFAULT);
 					goto freemem_and_return;
 				}
@@ -3228,9 +3231,9 @@ megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
 			/*
 			 * Is data going up-stream
 			 */
-			if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
-				if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
-							pthru->dataxferlen) ) {
+			if (dataxferlen && (uioc.flags & UIOC_RD)) {
+				if (copy_to_user((char __user *)(unsigned long)uxferaddr,
+						 data, dataxferlen)) {
 					rval = (-EFAULT);
 				}
 			}
@@ -3244,9 +3247,9 @@ megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
 				rval = -EFAULT;
 
 freemem_and_return:
-			if( pthru->dataxferlen ) {
+			if (dataxferlen) {
 				dma_free_coherent(&pdev->dev,
-						  pthru->dataxferlen, data,
+						  dataxferlen, data,
 						  data_dma_hndl);
 			}
 
-- 
2.56.1.708.g77570a6a1d-goog


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-19 17:05 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 17:05 [PATCH] scsi: megaraid: Fix uninitialized DMA handle and passthru length TOCTOU Ian Rogers

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®