Skip to contentSkip to conversation

mtd: blktrans: fix integer overflow

PATCH Superseded Peng Fan 2 participants 4 messages Aug 21, 2015 – Sep 9, 2015 Memory technology devices (MTD)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Peng Fan

author
#0 lore raw

PATCH 1/1 mtd: blktrans: fix integer 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.

Change the type of entry size of mtd_blktrans_dev to unsigned long long to fix the overflow issue.

Cc David Woodhouse <[email protected]>
Cc Brian Norris <[email protected]>
---
 include/linux/mtd/blktrans.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/mtd/blktrans.h +1 -1
index e93837f..3853d74 100644
@@ -38,7 +38,7 @@ struct mtd_blktrans_dev {
 	struct mutex lock;
 	int devnum;
 	bool bg_stop;
-	unsigned long size;
+	unsigned long long size;
 	int readonly;
 	int open;
 	struct kref ref;
Signature · 1 line
1.8.4
#1 lore raw
Quoted 27 lines · Peng Fan

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.

Change the type of entry size of mtd_blktrans_dev to unsigned long long to fix the overflow issue.

Cc David Woodhouse <[email protected]>
Cc Brian Norris <[email protected]>
---
 include/linux/mtd/blktrans.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/mtd/blktrans.h +0 -0
index e93837f..3853d74 100644

include/linux/mtd/blktrans.h struct mtd_blktrans_dev { · line 41

Quoted 5 lines
include/linux/mtd/blktrans.h +1 -1
@@ -38,7 +38,7 @@ struct mtd_blktrans_dev {
 	struct mutex lock;
 	int devnum;
 	bool bg_stop;
-	unsigned long size;
+	unsigned long long size;

Sorry, you can't just do that. Some builds won't have right arithmetic instructions (or compiler helpers) to do certain 64-bit operations, so you either need to go and fix all the blktrans users to be more careful (e.g., do bit shifts instead of division/modulo), or else find another solution. See, on an ARM32 build:

LD init/built-in.o
drivers/built-in.o: In function `nftl_add_mtd':
:(.text+0x1369dc): undefined reference to __aeabi_uldivmod' :(.text+0x1369f4): undefined reference to __aeabi_uldivmod' :(.text+0x136a70): undefined reference to __aeabi_uldivmod' drivers/built-in.o: In function inftl_add_mtd':
:(.text+0x137eb0): undefined reference to __aeabi_uldivmod' :(.text+0x137ec8): undefined reference to __aeabi_uldivmod' drivers/built-in.o::(.text+0x137f4c): more undefined references to `__aeabi_uldivmod' follow

include/linux/mtd/blktrans.h struct mtd_blktrans_dev { · line 44

Quoted 3 lines
include/linux/mtd/blktrans.h +0 -0
 	int readonly;
 	int open;
 	struct kref ref;

One possibility, since you only point to a single computation that overflows, is to just fix the overflow locally. It's not like the 'size' (which represents number of sectors) is actually ever overflowing a 32-bit integer. It's just the multiplication that overflows. So you could cast to 64-bit arithmetic just for the multiplication. e.g.:

	set_capcity(gd, ((u64)new->size * tr->blksize) >> 9);

Or some other creative solution.

Then, we don't have to address this problem till we start seeing 2TB MTDs!

Brian

author
#2 lore raw
Quoted 40 lines · Brian Norris
Quoted 27 lines · Peng Fan

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.

Change the type of entry size of mtd_blktrans_dev to unsigned long long to fix the overflow issue.

Cc David Woodhouse <[email protected]>
Cc Brian Norris <[email protected]>
---
 include/linux/mtd/blktrans.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/mtd/blktrans.h +0 -0
index e93837f..3853d74 100644

include/linux/mtd/blktrans.h struct mtd_blktrans_dev { · line 41

Quoted 5 lines
include/linux/mtd/blktrans.h +1 -1
@@ -38,7 +38,7 @@ struct mtd_blktrans_dev {
 	struct mutex lock;
 	int devnum;
 	bool bg_stop;
-	unsigned long size;
+	unsigned long long size;

Sorry, you can't just do that. Some builds won't have right arithmetic instructions (or compiler helpers) to do certain 64-bit operations, so you either need to go and fix all the blktrans users to be more careful (e.g., do bit shifts instead of division/modulo), or else find another solution. See, on an ARM32 build:

Quoted 10 lines

LD init/built-in.o
drivers/built-in.o: In function `nftl_add_mtd':
:(.text+0x1369dc): undefined reference to __aeabi_uldivmod' :(.text+0x1369f4): undefined reference to __aeabi_uldivmod' :(.text+0x136a70): undefined reference to __aeabi_uldivmod' drivers/built-in.o: In function inftl_add_mtd':
:(.text+0x137eb0): undefined reference to __aeabi_uldivmod' :(.text+0x137ec8): undefined reference to __aeabi_uldivmod' drivers/built-in.o::(.text+0x137f4c): more undefined references to `__aeabi_uldivmod' follow

My bad. I only tested this patch on i.mx6 platform, but not take other platforms into consideration.

Quoted 11 lines

include/linux/mtd/blktrans.h struct mtd_blktrans_dev { · line 44

Quoted 3 lines
include/linux/mtd/blktrans.h +0 -0
 	int readonly;
 	int open;
 	struct kref ref;

One possibility, since you only point to a single computation that overflows, is to just fix the overflow locally. It's not like the 'size' (which represents number of sectors) is actually ever overflowing a 32-bit integer. It's just the multiplication that overflows. So you could cast to 64-bit arithmetic just for the multiplication. e.g.:

	set_capcity(gd, ((u64)new->size * tr->blksize) >> 9);

Yeah. It is the multiplication overflowing a 32-bit interger. My frist implementation is this way, using u64 to do type cast. But I guess one day the number of sectors can be larger than UINT_MAX, so I change it to 'unsigned long long' from 'unsigned long', which is not the right way, thanks for correcting me.

Quoted 2 lines

Or some other creative solution.

I think this should be a bug fix. If you need a quick fix, I can send out the simple patch that just do (u64) cast. If not, I can take some time to "fix all the blktrans users".

Regards,
Peng.

Quoted 5 lines

Then, we don't have to address this problem till we start seeing 2TB MTDs!

Brian

#3 lore raw
Quoted 15 lines · Peng Fan
Quoted 7 lines · Brian Norris

One possibility, since you only point to a single computation that overflows, is to just fix the overflow locally. It's not like the 'size' (which represents number of sectors) is actually ever overflowing a 32-bit integer. It's just the multiplication that overflows. So you could cast to 64-bit arithmetic just for the multiplication. e.g.:

	set_capcity(gd, ((u64)new->size * tr->blksize) >> 9);

Yeah. It is the multiplication overflowing a 32-bit interger. My frist implementation is this way, using u64 to do type cast. But I guess one day the number of sectors can be larger than UINT_MAX, so I change it to 'unsigned long long' from 'unsigned long', which is
not the right way, thanks for correcting me.

Quoted 1 line
Quoted 5 lines
Quoted 1 line

Or some other creative solution.

I think this should be a bug fix. If you need a quick fix, I can send out the simple patch that just do (u64) cast. If not, I can take some time to "fix all the blktrans users".

I think since you've identified (and tested) a fix, we might as well include it. So I suppose the cast approach. The larger change would be more invasive and may take a while.

Quoted 3 lines
Quoted 3 lines

Then, we don't have to address this problem till we start seeing 2TB MTDs!

Brian