Skip to contentSkip to conversation

potential division by 0 in cfi_build_cmd() and cfi_merge_status()?

MTD Roel Kluin 3 participants 4 messages Jan 15, 2008 Memory technology devices (MTD)

lore
Share & tools
lore

Roel Kluin

author
#0 lore raw

Doing some grepping, I stumbled upon this possible error:

in include/linux/mtd/cfi.h, lines 302 and 366, resp. functions cfi_build_cmd() and cfi_merge_status() there is a division by cfi_interleave(cfi):

chip_mode = map_bankwidth(map) / cfi_interleave(cfi);

This could be problematic when No CONFIG_MTD_CFI_Ix is selected:
cfi_interleave will triggers BUG(), but when BUG is disabled, the function returns 0, causing a subsequent division by zero.

When a CONFIG_MTD_CFI_Ix is selected, cfi_interleave(cfi) is either defined 1 or defined (cfi)->interleave.

cfi is a struct cfi_private pointer, with interleave as an int.

I am not sure whether interleave can ever be 0 in this division when CONFIG_MTD_CFI_Ix is set.

shouldn't there be an error exit when cfi_interleave(cfi) evaluates to 0?

I am not subscribed to this list, so please CC.

Roel

#1 lore raw
Quoted 16 lines · Roel Kluin

Doing some grepping, I stumbled upon this possible error:

in include/linux/mtd/cfi.h, lines 302 and 366, resp. functions cfi_build_cmd() and cfi_merge_status() there is a division by cfi_interleave(cfi):

chip_mode = map_bankwidth(map) / cfi_interleave(cfi);

This could be problematic when No CONFIG_MTD_CFI_Ix is selected:
cfi_interleave will triggers BUG(), but when BUG is disabled, the function returns 0, causing a subsequent division by zero.

When a CONFIG_MTD_CFI_Ix is selected, cfi_interleave(cfi) is either defined 1 or defined (cfi)->interleave.

Quoted 7 lines

cfi is a struct cfi_private pointer, with interleave as an int.

I am not sure whether interleave can ever be 0 in this division when CONFIG_MTD_CFI_Ix is set.

shouldn't there be an error exit when cfi_interleave(cfi) evaluates to 0?

I don't think cfi_interleave(cfi) will ever be 0. But the functions definitely look a bit large for inlines. Anyone having both cfi_probe and jedec_probe will enjoy twice the kernel footprint from them. Patches to move that code out-of-line are welcome.

Jörn

Signature · 3 lines
Fools ignore complexity.  Pragmatists suffer it.
Some can avoid it.  Geniuses remove it.
-- Perlis's Programming Proverb #58, SIGPLAN Notices, Sept.  1982
#2 lore raw
Quoted 4 lines · Jörn Engel

I don't think cfi_interleave(cfi) will ever be 0. But the functions definitely look a bit large for inlines. Anyone having both cfi_probe and jedec_probe will enjoy twice the kernel footprint from them. Patches to move that code out-of-line are welcome.

Beware.

The typical use case is for your embedded system to be configured with your flash arrangement and not all possible combinations available under the sun.

In that case it is most likely that cfi_interleave(cfi) will simplify to a constant, and making this out of line _will_ enlarge your kernel, as well as possibly breaking interaction with a XIP kernel on ARM.

So please don't push this out of line.

Nicolas

#3 lore raw
Quoted 13 lines · Nicolas Pitre
Quoted 4 lines · Jörn Engel

I don't think cfi_interleave(cfi) will ever be 0. But the functions definitely look a bit large for inlines. Anyone having both cfi_probe and jedec_probe will enjoy twice the kernel footprint from them. Patches to move that code out-of-line are welcome.

Beware.

The typical use case is for your embedded system to be configured with your flash arrangement and not all possible combinations available under the sun.

Quoted 5 lines

In that case it is most likely that cfi_interleave(cfi) will simplify to a constant, and making this out of line _will_ enlarge your kernel, as well as possibly breaking interaction with a XIP kernel on ARM.

So please don't push this out of line.

Fair enough.

Jörn

Signature · 2 lines
Joern's library part 13:
http://www.chip-architect.com/