scsi: megaraid: Fix uninitialized DMA handle and passthru length TOCTOU
Subsystems (2)
PATCH 1/1 scsi: megaraid: Fix uninitialized DMA handle and passthru length TOCTOU
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.
--- drivers/scsi/megaraid.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-)
drivers/scsi/megaraid.c +14 -11
@@ -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); }
Signature · 1 line
2.56.1.708.g77570a6a1d-goog