mtd: phram: Prevent divide by zero bug in phram_setup()
authorDan Carpenter <dan.carpenter@oracle.com>
Fri, 21 Jan 2022 11:55:05 +0000 (14:55 +0300)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 23 Feb 2022 11:03:16 +0000 (12:03 +0100)
commit9fd00a568663e20348de3a77c7a26851d3668e4f
tree34d8a23d405c9f33e0cfe64ffded82fc6424cb71
parent3eb5185896a68373714dc7d0009111744adc3345
mtd: phram: Prevent divide by zero bug in phram_setup()

commit 3e3765875b1b8864898603768fd5c93eeb552211 upstream.

The problem is that "erasesize" is a uint64_t type so it might be
non-zero but the lower 32 bits are zero so when it's truncated,
"(uint32_t)erasesize", then that value is zero. This leads to a
divide by zero bug.

Avoid the bug by delaying the divide until after we have validated
that "erasesize" is non-zero and within the uint32_t range.

Fixes: dc2b3e5cbc80 ("mtd: phram: use div_u64_rem to stop overwrite len in phram_setup")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/linux-mtd/20220121115505.GI1978@kadam
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/mtd/devices/phram.c