mtd: blktrans: fix multiplication overflow
PATCH v2 1/1 mtd: blktrans: fix multiplication overflow
In drivers/mtd/mtd_blkdevs.c:
406 set_capacity(gd, (new->size * tr->blksize) >> 9);
The type of new->size is unsigned long and the type of tr->blksize is int, the result of 'new->size * tr->blksize' may exceed ULONG_MAX on 32bit machines.
I use nand chip MT29F32G08CBADBWP which is 4GB and the parameters passed to kernel is 'mtdparts=gpmi-nand:-(user)', the whole nand chip will be treated as a 4GB mtd partition. new->size is 0x800000 and tr->blksize is 0x200, 'new->size * tr->blksize' however is 0. This is what we do not want to see.
Using type cast u64 to fix the multiplication overflow issue.
---
Changes V2:
use type cast to avoid build break for platforms which does not support 64 bit multilplication.
drivers/mtd/mtd_blkdevs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mtd/mtd_blkdevs.c +1 -1
@@ -399,7 +399,7 @@ int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)snprintf (gd->disk_name,sizeof (gd->disk_name),"%s%d" , tr->name, new->devnum); -set_capacity (gd, (new->size * tr->blksize) >>9 ); +set_capacity (gd, ((u64 )new->size * tr->blksize) >>9 );/* Create the request queue */ spin_lock_init (&new->queue_lock);
Signature · 1 line
1.8.4
Quoted 14 lines · Peng Fan
In
drivers/mtd/mtd_blkdevs.c:
406set_capacity(gd, (new->size * tr->blksize) >> 9);
The type ofnew->sizeis unsigned long and the type oftr->blksizeis int, the result of 'new->size*tr->blksize' may exceedULONG_MAXon 32bit machines.I use nand chip MT29F32G08CBADBWP which is 4GB and the parameters passed to kernel is 'mtdparts=gpmi-nand:-(user)', the whole nand chip will be treated as a 4GB mtd partition.
new->sizeis0x800000andtr->blksizeis0x200, 'new->size*tr->blksize' however is 0. This is what we do not want to see.Using type cast u64 to fix the multiplication overflow issue.
Quoted 7 lines
Signed-off-by Peng Fan <[email protected]>Cc David Woodhouse <[email protected]>Cc Brian Norris <[email protected]>---
Changes V2:
use type cast to avoid build break for platforms which does not support 64 bit multilplication.
Thanks, pushed to l2-mtd.git