mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Shengqiang Zeng <poilandgginchi@gmail.com>
To: Andreas Hindborg <a.hindborg@kernel.org>, Jens Axboe <axboe@kernel.dk>
Cc: Boqun Feng <boqun@kernel.org>,
	Yingjie Wang <x-professor@outlook.com>,
	linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2] rnull: reject device size overflow
Date: Fri,  4 Dec 2026 17:42:13 +0800	[thread overview]
Message-ID: <20261204094213.2149164-1-poilandgginchi@gmail.com> (raw)

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: Shengqiang Zeng <poilandgginchi@gmail.com>
---
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@gmail.com/

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;
+
 impl NullBlkDevice {
     fn new(
         name: &CStr,
@@ -59,12 +62,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(block::SECTORS_PER_MIB)
+            .filter(|sectors| *sectors <= block::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)
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 215753171cef..3acdcb895394 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -58,6 +58,8 @@ extern const struct class block_class;
 #define DISK_MAX_PARTS			256
 #define DISK_NAME_LEN			32
 
+#define	BLK_DEV_MAX_SECTORS	(LLONG_MAX >> 9)
+
 #define PARTITION_META_INFO_VOLNAMELTH	64
 /*
  * Enough for the string representation of any kind of UUID plus NULL.
diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
index 3791af0aadc7..d9ff2be53e17 100644
--- a/rust/bindings/bindings_helper.h
+++ b/rust/bindings/bindings_helper.h
@@ -123,6 +123,7 @@ const gfp_t RUST_CONST_HELPER_GFP_NOWAIT = GFP_NOWAIT;
 const gfp_t RUST_CONST_HELPER___GFP_ZERO = __GFP_ZERO;
 const gfp_t RUST_CONST_HELPER___GFP_HIGHMEM = ___GFP_HIGHMEM;
 const gfp_t RUST_CONST_HELPER___GFP_NOWARN = ___GFP_NOWARN;
+const u64 RUST_CONST_HELPER_BLK_DEV_MAX_SECTORS = BLK_DEV_MAX_SECTORS;
 const blk_features_t RUST_CONST_HELPER_BLK_FEAT_ROTATIONAL = BLK_FEAT_ROTATIONAL;
 const fop_flags_t RUST_CONST_HELPER_FOP_UNSIGNED_OFFSET = FOP_UNSIGNED_OFFSET;
 
diff --git a/rust/kernel/block.rs b/rust/kernel/block.rs
index 32c8d865afb6..99548379c73d 100644
--- a/rust/kernel/block.rs
+++ b/rust/kernel/block.rs
@@ -4,6 +4,9 @@
 
 pub mod mq;
 
+/// Maximum sector count accepted by the block layer.
+pub const MAX_SECTORS: u64 = bindings::BLK_DEV_MAX_SECTORS;
+
 /// Bit mask for masking out [`SECTOR_SIZE`].
 pub const SECTOR_MASK: u32 = bindings::SECTOR_MASK;
 
@@ -13,6 +16,9 @@ pub const SECTOR_SHIFT: u32 = bindings::SECTOR_SHIFT;
 /// Size of a sector.
 pub const SECTOR_SIZE: u32 = bindings::SECTOR_SIZE;
 
+/// Number of 512-byte sectors in one MiB.
+pub const SECTORS_PER_MIB: u64 = 1 << (20 - SECTOR_SHIFT);
+
 /// The difference between the size of a page and the size of a sector,
 /// expressed as a power of two.
 pub const PAGE_SECTORS_SHIFT: u32 = bindings::PAGE_SECTORS_SHIFT;
-- 
2.43.0

             reply	other threads:[~2026-12-04  9:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <L7GePTQrBqxYMnMfFQVg-AF2X-IgMeP-354uGDGNcy1dmMQKeALW6dqAac8P34TXe2xkjV8kuEqzpeNz8-slJg==@protonmail.internalid>
2026-12-04  9:42 ` Shengqiang Zeng [this message]
2026-12-04 13:39   ` Andreas Hindborg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204094213.2149164-1-poilandgginchi@gmail.com \
    --to=poilandgginchi@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=axboe@kernel.dk \
    --cc=boqun@kernel.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=x-professor@outlook.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®