[PATCH v2] mtd: block2mtd: Fix divide error when erase_size is zero

Pei Xiao posted 1 patch 1 month, 2 weeks ago
There is a newer version of this series
drivers/mtd/devices/block2mtd.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH v2] mtd: block2mtd: Fix divide error when erase_size is zero
Posted by Pei Xiao 1 month, 2 weeks ago
The erase size is parsed from the "block2mtd" module parameter and can
be set to zero. add_device() then evaluates

        if (size % erase_size)

with a zero divisor, which triggers a divide error:

        divide error: 0000 [#1] PREEMPT SMP PTI
        RIP: 0010:add_device drivers/mtd/devices/block2mtd.c:296 [inline]
        RIP: 0010:block2mtd_setup2+0x592/0xda0 drivers/mtd/devices/block2mtd.c:459
        Call Trace:
         block2mtd_setup+0x27/0xe0 drivers/mtd/devices/block2mtd.c:476
         param_attr_store+0x214/0x310 kernel/params.c:589
         module_attr_store+0x65/0x90 kernel/params.c:904
         kernfs_fop_write_iter+0x3a4/0x540 fs/kernfs/file.c:345
         ...

Reject a zero erase size before performing the modulo operation so the
existing "erasesize must be a divisor of device size" error path
reports the invalid argument and frees the device.

While at it, drop the unnecessary (long) cast from the size operand of
the modulo.

Fixes: ea6d833a3fdd ("mtd: block2mtd: check device size")
Reported-by: syzbot+b320a4d5f65a61dbbf89@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/lkml/6a7b58ba.ac361c09.22ff0a.0045.GAE@google.com/
Suggested-by: Jörn Engel <joern@barelysecure.org>
Cc: stable@vger.kernel.org
Signed-off-by: Pei Xiao <xiaopei01@kylinos.cn>
---
changlogs in v2:
1.Add Suggested-by tag
2.remove unnecessary (long) cast from the size
---
 drivers/mtd/devices/block2mtd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/mtd/devices/block2mtd.c b/drivers/mtd/devices/block2mtd.c
index 03e80b2c4f5a..3e2367dfff88 100644
--- a/drivers/mtd/devices/block2mtd.c
+++ b/drivers/mtd/devices/block2mtd.c
@@ -293,7 +293,7 @@ static struct block2mtd_dev *add_device(char *devname, int erase_size,
 	}
 
 	size = bdev_nr_bytes(bdev);
-	if ((long)size % erase_size) {
+	if (!erase_size || size % erase_size) {
 		pr_err("erasesize must be a divisor of device size\n");
 		goto err_free_block2mtd;
 	}
-- 
2.25.1

Re: [PATCH v2] mtd: block2mtd: Fix divide error when erase_size is zero
Posted by Miquel Raynal 1 month, 2 weeks ago
On 12/08/2026 at 09:52:42 +08, Pei Xiao <xiaopei01@kylinos.cn> wrote:

> The erase size is parsed from the "block2mtd" module parameter and can
> be set to zero. add_device() then evaluates
>
>         if (size % erase_size)
>
> with a zero divisor, which triggers a divide error:
>
>         divide error: 0000 [#1] PREEMPT SMP PTI
>         RIP: 0010:add_device drivers/mtd/devices/block2mtd.c:296 [inline]
>         RIP: 0010:block2mtd_setup2+0x592/0xda0 drivers/mtd/devices/block2mtd.c:459
>         Call Trace:
>          block2mtd_setup+0x27/0xe0 drivers/mtd/devices/block2mtd.c:476
>          param_attr_store+0x214/0x310 kernel/params.c:589
>          module_attr_store+0x65/0x90 kernel/params.c:904
>          kernfs_fop_write_iter+0x3a4/0x540 fs/kernfs/file.c:345
>          ...
>
> Reject a zero erase size before performing the modulo operation so the
> existing "erasesize must be a divisor of device size" error path
> reports the invalid argument and frees the device.
>
> While at it, drop the unnecessary (long) cast from the size operand of
> the modulo.
>
> Fixes: ea6d833a3fdd ("mtd: block2mtd: check device size")
> Reported-by: syzbot+b320a4d5f65a61dbbf89@syzkaller.appspotmail.com
> Closes: https://lore.kernel.org/lkml/6a7b58ba.ac361c09.22ff0a.0045.GAE@google.com/
> Suggested-by: Jörn Engel <joern@barelysecure.org>
> Cc: stable@vger.kernel.org
> Signed-off-by: Pei Xiao <xiaopei01@kylinos.cn>

Apparently, removing the cast leads to an unsupported operation with a
64-bit operand on 32-bit architectures (See Sashiko report).

I guess it is best to keep that cast in the end.

Sorry for the changes back-and-forth.

Miquèl
Re: [PATCH v2] mtd: block2mtd: Fix divide error when erase_size is zero
Posted by Pei Xiao 1 month, 2 weeks ago

在 2026/8/12 20:34, Miquel Raynal 写道:
> On 12/08/2026 at 09:52:42 +08, Pei Xiao <xiaopei01@kylinos.cn> wrote:
> 
>> The erase size is parsed from the "block2mtd" module parameter and can
>> be set to zero. add_device() then evaluates
>>
>>         if (size % erase_size)
>>
>> with a zero divisor, which triggers a divide error:
>>
>>         divide error: 0000 [#1] PREEMPT SMP PTI
>>         RIP: 0010:add_device drivers/mtd/devices/block2mtd.c:296 [inline]
>>         RIP: 0010:block2mtd_setup2+0x592/0xda0 drivers/mtd/devices/block2mtd.c:459
>>         Call Trace:
>>          block2mtd_setup+0x27/0xe0 drivers/mtd/devices/block2mtd.c:476
>>          param_attr_store+0x214/0x310 kernel/params.c:589
>>          module_attr_store+0x65/0x90 kernel/params.c:904
>>          kernfs_fop_write_iter+0x3a4/0x540 fs/kernfs/file.c:345
>>          ...
>>
>> Reject a zero erase size before performing the modulo operation so the
>> existing "erasesize must be a divisor of device size" error path
>> reports the invalid argument and frees the device.
>>
>> While at it, drop the unnecessary (long) cast from the size operand of
>> the modulo.
>>
>> Fixes: ea6d833a3fdd ("mtd: block2mtd: check device size")
>> Reported-by: syzbot+b320a4d5f65a61dbbf89@syzkaller.appspotmail.com
>> Closes: https://lore.kernel.org/lkml/6a7b58ba.ac361c09.22ff0a.0045.GAE@google.com/
>> Suggested-by: Jörn Engel <joern@barelysecure.org>
>> Cc: stable@vger.kernel.org
>> Signed-off-by: Pei Xiao <xiaopei01@kylinos.cn>
> 
> Apparently, removing the cast leads to an unsupported operation with a
> 64-bit operand on 32-bit architectures (See Sashiko report).
Using i386 for testing does reveal compilation issues; I will retain
this (long) cast and send the third version of the patch.

make ARCH=i386 -j$(nproc)

ld: drivers/mtd/devices/block2mtd.o: in function `block2mtd_setup2':
block2mtd.c:(.text+0x315): undefined reference to `__moddi3'
make[2]: *** [scripts/Makefile.vmlinux:72:vmlinux.unstripped] 错误 1

> 
> I guess it is best to keep that cast in the end.
> 
> Sorry for the changes back-and-forth.
It is my pleasure.

Pei.
> 
> Miquèl