From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796299078; b=CCsjcf6ow4JQ7K+XqwDZ9uMiPyvcpPTpOUp1YN0Nn6KdOjuD6Wpqo9ChFM2Nf86Ya0LS htp8i3zwnruZo3amAImRF7haoZbORjNZSv0uoXPtmNl4hFWCG2ifmzQSyRmwfYwnpZc5j znwdvpoQks9LLYnq0Ccv7WQwnCkXoHCy8CivQBQDsqDXUNsd9AXwV7yCVTT+OfCO5m7Ww ndROrwoxNho7a+k4Oyd5wdQE6/osL6tfJZYbOToOOhK8c3s8T/EsUGFwyb+qbJ/e51o3P 8v4f/bkU0n/3lU1yFvCUuW/L04acQmEtwNCxam0WJy24fp5NCTscZIP6690BuktyGtw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796299078; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=izwiBWMWgoKNFIYq3WXZR60ZX29re34P+oxSHyqfTQI=; b=Ti0COpd9d6v2/uHCFdZ/QTrfM+zvNmGh34HOObWMJeca/fYUDf/rFrapLPw3oLbXZF3N /yZEMVrr7MgdZ9F5LBjK2sgpZI51ar/6Zmdy2uYRTqqqeqheEdt+SxFAjZAR+/Mvxq944 ToDVAi96LhAwQ8wgv3PYJo+NqF/S4mGxvSa7eJT7BJZJIAH4GxtO8ks+acdELCw79Vneo Hvs/lmobjuTnkZhv9F5TydCLNoCzcfAWnQt1RqY6fs1stWDGrLsgEOF1IpHUtAtERl8/0 CBOeBcau1bjcIlz9J43OccdhK2i9pjxULOAJJS2hmvTfYXhXZrK4Cs6AkxkJTQfk48A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=sqM3syOg; arc=none smtp.remote-ip=209.85.216.51 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8A3E6606A333 for ; Thu, 3 Dec 2026 11:57:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8A3E6606A333 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=sqM3syOg Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-3b59eb092f9so646937a91.1 for ; Thu, 03 Dec 2026 03:57:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796299078; x=1796903878; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=izwiBWMWgoKNFIYq3WXZR60ZX29re34P+oxSHyqfTQI=; b=sqM3syOgyEH4Rp5eBK7POrWQVcwNhLMbJOe0hqqlYd0r/uK8HFiiokLS5D03D+Dart duJRg+IF9JCOiXmGOOY6T7J/364NzkkVMN6UrH0OIYHWxXCz7kaKWlA0sIKQZW6CelS2 GHzdB971eZkyNxLl6lgdi9k0AyCJJHtxOsQFQrvsc0yRIRnWcZIAlI0o6WfZSD+25pWr r3fkMFRGpZVfZog8vrZ8QIHfQX+MSpDxVcGYuswpB8aZp9zuM8ypAG6eQNKPoONJUph7 kq4swCgWzK3KqT4als+aGYQVeHYJn9MxkRVACDA83rq+lieWB8kORvigzcl+0f5ENF2O 6nHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796299078; x=1796903878; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=izwiBWMWgoKNFIYq3WXZR60ZX29re34P+oxSHyqfTQI=; b=OKBEgJu+a+QCrVpj+WpJJl9AlMYWhTYrNfTnfzq9KJ3/IZzRycPHKDF3NCIBmZb1qe y6RklxtxV5A49O6x8wW3EDIwoQvugGU0SjiSObJNs9f8paVD1WStcvCs+89avuCfz8Dn hBIFXp+5pgbuwqsmaPvqkLVCfEB+ns9kV85aCsPJ4Qa/qiW9a+u7PUJbukk1hM7Gl9Zb lKeQ+E4ILV4NwLRM4oh3RlOGKH4AVhM49L3DsdEgezsS/cO8Dq48WskHjDVoyshcjb3w QEs3PXjq5fpN13WsJsGck78oaumDeAclsVjDEY6HgMjY0JcYeptZV4MCCioZNPvdOmBX jeng== X-Forwarded-Encrypted: i=1; AL+hErFJQb3PRhGDym+2CdjRHbJ0HFqqq3G4zbw925LNa+mBLLwioTMGSTFDci7owBYD3hI+5aFVe4HHn7+4eOc=@vger.kernel.org X-Gm-Message-State: APuU4Z+CWPnl/8/vn99IiwdkOsEvCaVPDPtxznFXS85957exc52ozNPm JV96kQKlPKERDtmX/zxGbdOUCX8p4GkWsWjjGQvs0PppSGkPDvBgeG2E X-Gm-Gg: AZFdTZcITJQiinqdW9KQ52SWyxK+Tc1Aokk7El/2/XdOBPq/+f7UaqnW9LF2tkN8CZ+ onyMnCIp84xErWUW6Bz8EBSMKEw/SIe67NX69fCzrw4saRA4s1jK20Picuv5lRe6lEM2GSZYSc7 uVs8d3dggxOLTPTaZwfhrdU1nERR6nzN0dM6mMWFenLQbi2O4rHxseeTuzK3+nWBdxNrSKwO/4A 2I3B5hnhCt+gFyBuMnpIkJcU7enJLLOxujKUUDXM01rfaVuJDOanwt0hpD1jQUnsZeoyz3cMixe Qx12n90+SK/xwQl24UDnT/wi5AuAckKWpBFyC/Vf4smMRiO6p7RM+i/9+8fuSpfarnqqFSem8K1 8OkBLRasBSrAD4v5iyqJpXLTGXoTzkKJde3t4w7vNmZxbxdFxuP1d7YIDeRYF+isULesk9KuiQ8 PTEbZKKw5B7ShKUfEBoBMaYYUH318sUW93HGF6SS8Km5KqlAeH9sH56PTlNKDRZNAtPNn3THupy TT4hzrB+djjxJLodqGN2+u9yHdIq8BN7SyewvunEtfbbXR6ZjiO5210TOKIKT28zmPFAtd6tyPP 7FxvgkHoN/Fg5ghkW841veQWm+krd5RULtR6Mmzu9EagtB6rVqNK3lS25yPLkyfxh5Xey1QOG2O BzwJmwbIbnnZPzzfsfqPv6p8H X-Received: by 2002:a17:90a:da8e:b0:3b3:bcc2:6f8a with SMTP id 98e67ed59e1d1-3b5cb75e82bmr1154778a91.35.1796299077711; Thu, 03 Dec 2026 03:57:57 -0800 (PST) Received: from poil-B760M-AORUS-ELITE-AX-DDR4.. ([223.104.134.25]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b44b7c6513sm33668607a91.4.2026.12.03.03.57.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 03:57:57 -0800 (PST) From: Poil Zero To: Andreas Hindborg , Jens Axboe Cc: Boqun Feng , Yingjie Wang , linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] rnull: reject device size overflow Date: Thu, 3 Dec 2026 19:57:42 +0800 Message-ID: <20261203115742.384873-1-poilandgginchi@gmail.com> X-Mailer: git-send-email 2.43.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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::().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> { + // 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