Re: [PATCH] rnull: reject device size overflow
From: Andreas Hindborg
Date: Thu Dec 03 2026 - 08:57:12 EST
"Poil Zero" <poilandgginchi@xxxxxxxxx> writes:
> The configfs size attribute accepts any u64 value in MiB.
> NullBlkDevice::new() converts it to a count of 512-byte sectors with
> capacity_mib << (20 - SECTOR_SHIFT). Bits shifted out of the u64 are
> silently discarded, so 2^53 MiB becomes zero sectors and 2^53 + 1 MiB
> becomes 2048 sectors. Smaller values can also exceed
> BLK_DEV_MAX_SECTORS and are clamped by set_capacity().
>
> Reject sizes above BLK_DEV_MAX_SECTORS / SECTORS_PER_MIB in the
> configfs store before updating the saved size. Also use checked_mul()
> and check the resulting sector count in NullBlkDevice::new(),
> returning -EINVAL if either limit is exceeded. Validate capacity before
> allocating the tag set or queue data. This prevents a device from being
> created with a wrapped or clamped capacity.
>
> Apply the same overflow and capacity bounds proposed for C null_blk
> in the linked patch by Yingjie Wang.
>
> Fixes: d969d504bc13 ("rnull: enable configuration via `configfs`")
> Link: https://lore.kernel.org/all/20261201140214.346038-1-x-professor@xxxxxxxxxxx/
> Signed-off-by: Poil Zero <poilandgginchi@xxxxxxxxx>
Thanks for fixing! Looks good to me, but I think we should move some of
the constants.
Please define `MAX_SECTORS` in rust/kernel/block.rs based on the
C define.
I don't think `SECTORS_PER_MIB` exist in the C code, but please add that
to block.rs as well. It seems generally useful.
Best regards,
Andreas Hindborg