Re: [PATCH v2] rnull: reject device size overflow

From: Andreas Hindborg

Date: Fri Dec 04 2026 - 08:39:31 EST


"Shengqiang Zeng" <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: Shengqiang Zeng <poilandgginchi@xxxxxxxxx>

Overall looks good to me.

Please make sure to address the relevant maintainers:

git format-patch -1 --stdout HEAD | scripts/get_maintainer.pl --norolestats

Jens Axboe <axboe@xxxxxxxxx>
Andreas Hindborg <a.hindborg@xxxxxxxxxx>
Boqun Feng <boqun@xxxxxxxxxx>
Miguel Ojeda <ojeda@xxxxxxxxxx>
Gary Guo <gary@xxxxxxxxxxx>
"Björn Roy Baron" <bjorn3_gh@xxxxxxxxxxxxxx>
Benno Lossin <lossin@xxxxxxxxxx>
Alice Ryhl <aliceryhl@xxxxxxxxxx>
Trevor Gross <tmgross@xxxxxxxxx>
Danilo Krummrich <dakr@xxxxxxxxxx>
Daniel Almeida <daniel.almeida@xxxxxxxxxxxxx>
Tamir Duberstein <tamird@xxxxxxxxxx>
Alexandre Courbot <acourbot@xxxxxxxxxx>
"Onur Özkan" <work@xxxxxxxxxxxxx>
Asahi Lina <lina+kernel@xxxxxxxxxxxxx>
Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
Thomas Zimmermann <tzimmermann@xxxxxxx>
Zhi Wang <zhiw@xxxxxxxxxx>
Joel Fernandes <joelagnelf@xxxxxxxxxx>
Carlos Llamas <cmllamas@xxxxxxxxxx>
linux-block@xxxxxxxxxxxxxxx
linux-kernel@xxxxxxxxxxxxxxx
rust-for-linux@xxxxxxxxxxxxxxx

Or just use b4 for sending patches, that is much simpler [1].

> ---
> Changes in v2:
> - Move MAX_SECTORS and SECTORS_PER_MIB to rust/kernel/block.rs (Andreas).
> - Define MAX_SECTORS from the C BLK_DEV_MAX_SECTORS macro. Move the
> macro to include/linux/blkdev.h and add a bindgen constant helper.
> - Use the shared block-layer constants in rnull.
>
> v1: https://lore.kernel.org/all/20261203115742.384873-1-poilandgginchi@xxxxxxxxx/
>
> Applied with git am --whitespace=error on v7.4-rc5 (fe6ff8d5d675).
> Build-tested rust/kernel.o, drivers/block/rnull/rnull.o and block/genhd.o
> on x86_64 with LLVM=1, CONFIG_RUST=y and CONFIG_BLK_DEV_RUST_NULL=y.
> The standalone capacity conversion model passes all nine tests.
>
> block/blk.h | 1 -
> drivers/block/rnull/configfs.rs | 9 +++++++++
> drivers/block/rnull/rnull.rs | 14 +++++++++++++-
> include/linux/blkdev.h | 2 ++
> rust/bindings/bindings_helper.h | 1 +
> rust/kernel/block.rs | 6 ++++++
> 6 files changed, 31 insertions(+), 2 deletions(-)
>
> diff --git a/block/blk.h b/block/blk.h
> index 105fff157bfb..13fa24aa05ea 100644
> --- a/block/blk.h
> +++ b/block/blk.h
> @@ -23,7 +23,6 @@ struct elv_change_ctx;
> */
> #define BLK_DEF_MAX_SECTORS_CAP (SZ_4M >> SECTOR_SHIFT)
>
> -#define BLK_DEV_MAX_SECTORS (LLONG_MAX >> 9)
> #define BLK_MIN_SEGMENT_SIZE 4096
>
> /* Max future timer expiry for timeouts */
> diff --git a/drivers/block/rnull/configfs.rs b/drivers/block/rnull/configfs.rs
> index 34ef96fa125f..65bf87f96b1a 100644
> --- a/drivers/block/rnull/configfs.rs
> +++ b/drivers/block/rnull/configfs.rs
> @@ -247,6 +247,15 @@ impl configfs::AttributeOperations<3> for DeviceConfig {
> let text = core::str::from_utf8(page)?.trim();
> let value = text.parse::<u64>().map_err(|_| EINVAL)?;
>
> + // `NullBlkDevice::new()` converts MiB to 512-byte sectors by
> + // multiplying by `SECTORS_PER_MIB`. Reject values that would overflow
> + // that conversion, or that exceed the maximum block device capacity,
> + // instead of letting the device be created with a silently truncated
> + // or clamped capacity.
> + if value > super::MAX_CAPACITY_MIB {
> + return Err(EINVAL);
> + }
> +
> guard.capacity_mib = value;
> Ok(())
> }
> diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs
> index 13048cea8bb0..6d28192ddd3c 100644
> --- a/drivers/block/rnull/rnull.rs
> +++ b/drivers/block/rnull/rnull.rs
> @@ -51,6 +51,9 @@ impl kernel::InPlaceModule for NullBlkModule {
>
> struct NullBlkDevice;
>
> +/// Maximum capacity in MiB that does not exceed [`block::MAX_SECTORS`].
> +pub(crate) const MAX_CAPACITY_MIB: u64 = block::MAX_SECTORS / block::SECTORS_PER_MIB;

Can you move this constant to configfs.rs where it is used and then do
not export it?

Best regards,
Andreas Hindborg

[1] https://b4.docs.kernel.org/en/latest/