mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] rnull: reject device size overflow
@ 2026-12-03 11:57 ` Poil Zero
  2026-12-03 13:56   ` Andreas Hindborg
  0 siblings, 1 reply; 2+ messages in thread
From: Poil Zero @ 2026-12-03 11:57 UTC (permalink / raw)
  To: Andreas Hindborg, Jens Axboe
  Cc: Boqun Feng, Yingjie Wang, linux-block, rust-for-linux, linux-kernel

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@outlook.com/
Signed-off-by: Poil Zero <poilandgginchi@gmail.com>
---
Applied with git am --whitespace=error on v7.4-rc5 (fe6ff8d5d675).
Build-tested drivers/block/rnull/rnull.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, including the block-layer
limit and u64 overflow boundaries.

 drivers/block/rnull/configfs.rs |  9 +++++++++
 drivers/block/rnull/rnull.rs    | 20 +++++++++++++++++++-
 2 files changed, 28 insertions(+), 1 deletion(-)

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..6e8b524f7296 100644
--- a/drivers/block/rnull/rnull.rs
+++ b/drivers/block/rnull/rnull.rs
@@ -51,6 +51,15 @@ impl kernel::InPlaceModule for NullBlkModule {
 
 struct NullBlkDevice;
 
+/// Number of 512-byte sectors in one MiB.
+const SECTORS_PER_MIB: u64 = 1 << (20 - block::SECTOR_SHIFT);
+
+/// Maximum sector count accepted by the block layer.
+const MAX_SECTORS: u64 = (i64::MAX as u64) >> block::SECTOR_SHIFT;
+
+/// Maximum capacity in MiB that does not exceed `MAX_SECTORS`.
+pub(crate) const MAX_CAPACITY_MIB: u64 = MAX_SECTORS / SECTORS_PER_MIB;
+
 impl NullBlkDevice {
     fn new(
         name: &CStr,
@@ -59,12 +68,21 @@ impl NullBlkDevice {
         capacity_mib: u64,
         irq_mode: IRQMode,
     ) -> Result<GenDisk<Self>> {
+        // Reject capacities that cannot be represented as a 512-byte sector
+        // count instead of silently truncating the shifted-out bits, and
+        // reject capacities that the block layer would have to clamp to
+        // `BLK_DEV_MAX_SECTORS`.
+        let capacity_sectors = capacity_mib
+            .checked_mul(SECTORS_PER_MIB)
+            .filter(|sectors| *sectors <= MAX_SECTORS)
+            .ok_or(EINVAL)?;
+
         let tagset = Arc::pin_init(TagSet::new(1, 256, 1), GFP_KERNEL)?;
 
         let queue_data = Box::new(QueueData { irq_mode }, GFP_KERNEL)?;
 
         gen_disk::GenDiskBuilder::new()
-            .capacity_sectors(capacity_mib << (20 - block::SECTOR_SHIFT))
+            .capacity_sectors(capacity_sectors)
             .logical_block_size(block_size)?
             .physical_block_size(block_size)?
             .rotational(rotational)
-- 
2.43.0

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] rnull: reject device size overflow
  2026-12-03 11:57 ` [PATCH] rnull: reject device size overflow Poil Zero
@ 2026-12-03 13:56   ` Andreas Hindborg
  0 siblings, 0 replies; 2+ messages in thread
From: Andreas Hindborg @ 2026-12-03 13:56 UTC (permalink / raw)
  To: Poil Zero, Jens Axboe
  Cc: Boqun Feng, Yingjie Wang, linux-block, rust-for-linux, linux-kernel

"Poil Zero" <poilandgginchi@gmail.com> 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@outlook.com/
> Signed-off-by: Poil Zero <poilandgginchi@gmail.com>

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



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-03 13:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <mefIjhqK67vjOlDMxBPBLxVsVn8lVZXLDwt5hMhTYPk_P1UNJ2l1ho-9PsROe0Tx3RAYp6Ye6p1yWz4CtDSQ8Q==@protonmail.internalid>
2026-12-03 11:57 ` [PATCH] rnull: reject device size overflow Poil Zero
2026-12-03 13:56   ` Andreas Hindborg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®