Skip to contentSkip to conversation

dmaengine: rcar-dmac: avoid array overflow

PATCH In review Arnd Bergmann 3 participants 3 messages Jul 28, 2017 – Jul 29, 2017

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

PATCH 1/1 dmaengine: rcar-dmac: avoid array overflow

Building with CONFIG_UBSAN_SANITIZE_ALL shows this warning:

drivers/dma/sh/rcar-dmac.c: In function 'rcar_dmac_chan_prep_sg':
drivers/dma/sh/rcar-dmac.c:839:29: error: array subscript is above array bounds [-Werror=array-bounds]
desc->chcr = chcr | chcr_ts[desc->xfer_shift];

As the compiler doesn't know what the xfer_size is, it is impossible to rule out the array overflow here. As we know that xfer_size can only be within enum dma_slave_buswidth, this will not overflow for correct users, and adding a range check will handle the obscure case and shut up the warning.

Fixes 87244fe5abdf ("dmaengine: rcar-dmac: Add Renesas R-Car Gen2 DMA Controller (DMAC) driver")
---
 drivers/dma/sh/rcar-dmac.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/dma/sh/rcar-dmac.c +2 -1
index ffcadca53243..f5b28eb4009e 100644
@@ -836,7 +836,8 @@ static void rcar_dmac_chan_configure_desc(struct rcar_dmac_chan *chan,
 	}
 
 	desc->xfer_shift = ilog2(xfer_size);
-	desc->chcr = chcr | chcr_ts[desc->xfer_shift];
+	if (desc->xfer_shift < ARRAY_SIZE(chcr_ts))
+		desc->chcr = chcr | chcr_ts[desc->xfer_shift];
 }
 
 /*
Signature · 1 line
2.9.0
Reviewed-by on v1 1/1
#1 lore raw

Hi Arnd,

Quoted 6 lines · Arnd Bergmann

Building with CONFIG_UBSAN_SANITIZE_ALL shows this warning:

drivers/dma/sh/rcar-dmac.c: In function 'rcar_dmac_chan_prep_sg':
drivers/dma/sh/rcar-dmac.c:839:29: error: array subscript is above array bounds [-Werror=array-bounds]
desc->chcr = chcr | chcr_ts[desc->xfer_shift];

Quoted 8 lines

As the compiler doesn't know what the xfer_size is, it is impossible to rule out the array overflow here. As we know that xfer_size can only be within enum dma_slave_buswidth, this will not overflow for correct users, and adding a range check will handle the obscure case and shut up the warning.

Fixes 87244fe5abdf ("dmaengine: rcar-dmac: Add Renesas R-Car Gen2 DMA Controller (DMAC) driver")
Quoted 21 lines
---
 drivers/dma/sh/rcar-dmac.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/dma/sh/rcar-dmac.c +2 -1
index ffcadca53243..f5b28eb4009e 100644
@@ -836,7 +836,8 @@ static void rcar_dmac_chan_configure_desc(struct rcar_dmac_chan *chan,
 	}
 
 	desc->xfer_shift = ilog2(xfer_size);
-	desc->chcr = chcr | chcr_ts[desc->xfer_shift];
+	if (desc->xfer_shift < ARRAY_SIZE(chcr_ts))
+		desc->chcr = chcr | chcr_ts[desc->xfer_shift];
 }
 
 /*
Signature · 1 line
2.9.0
Signature · 2 lines
Regards,
Niklas Söderlund
#2 lore raw
In reply to Arnd Bergmann

Hi Arnd,

Thank you for the patch.

Quoted 24 lines · On Friday 28 Jul 2017 15:15:49 Arnd Bergmann wrote:

Building with CONFIG_UBSAN_SANITIZE_ALL shows this warning:

drivers/dma/sh/rcar-dmac.c: In function 'rcar_dmac_chan_prep_sg':
drivers/dma/sh/rcar-dmac.c:839:29: error: array subscript is above array bounds [-Werror=array-bounds] desc->chcr = chcr | chcr_ts[desc->xfer_shift];

As the compiler doesn't know what the xfer_size is, it is impossible to rule out the array overflow here. As we know that xfer_size can only be within enum dma_slave_buswidth, this will not overflow for correct users, and adding a range check will handle the obscure case and shut up the warning.

Fixes 87244fe5abdf ("dmaengine: rcar-dmac: Add Renesas R-Car Gen2 DMA

Controller (DMAC) driver") Signed-off-by: Arnd Bergmann <[email protected]>

---
 drivers/dma/sh/rcar-dmac.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/dma/sh/rcar-dmac.c +0 -0
index ffcadca53243..f5b28eb4009e 100644

drivers/dma/sh/rcar-dmac.c static void rcar_dmac_chan_configure_desc(struct · line 840

Quoted 6 lines
drivers/dma/sh/rcar-dmac.c +2 -1
@@ -836,7 +836,8 @@ static void rcar_dmac_chan_configure_desc(struct
 rcar_dmac_chan *chan, }
 
 	desc->xfer_shift = ilog2(xfer_size);
-	desc->chcr = chcr | chcr_ts[desc->xfer_shift];
+	if (desc->xfer_shift < ARRAY_SIZE(chcr_ts))
+		desc->chcr = chcr | chcr_ts[desc->xfer_shift];

I think this counts as an invalid warning. As you stated in the commit message, we know that xfer_shift is within a valid range of values. True, if the DMA engine API changed to support larger transfer sizes without updating the driver, we would have a problem. But your patch will silently leave desc-

Quoted 1 line

chcr unset in that case, which is not good either. We should instead track

back xfer_size to where it gets set (in rcar_dmac_device_config() if I'm not mistaken, but I haven't checked in details whether other locations need to be handled too), and return an error there, possibly with a debug or warning message.

Assuming we want to guard against that problem (which could be argued), that's in my opinion the right fix. And it won't get rid of the compiler warning I'm afraid.

Quoted 3 lines

}

/*

Signature · 3 lines
Regards,

Laurent Pinchart