summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEliot Courtney <ecourtney@nvidia.com>2026-09-03 13:01:33 +0200
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2026-09-07 17:37:23 +0200
commitceab9302388b22baa11c08386f3a04315baf1593 (patch)
tree225a2c1741db38bad82cd95eba2fc9441bbabc6e
parent641dacada33127948850bd31e50bbe9d5e84a4d2 (diff)
downloadlinux-stable-ceab9302388b22baa11c08386f3a04315baf1593.tar.gz
linux-stable-ceab9302388b22baa11c08386f3a04315baf1593.zip
rust: num: reject Bounded::shr overshifts at build time
[ Upstream commit 223aa25aee82e188ddf043a8703b16e5fdfc37d8 ] Make `shr` reject shifts of at least the type's bit width at build time, instead of panicking or masking the shift amount at runtime. [ This implies we can break the type invariant, which in turn means we can trigger UB via `Deref`, e.g.: rust_kernel: panicked at rust/kernel/num/bounded.rs:528:22: unsafe precondition(s) violated: hint::unreachable_unchecked must never be reached - Miguel ] Signed-off-by: Eliot Courtney <ecourtney@nvidia.com> Acked-by: Alexandre Courbot <acourbot@nvidia.com> Reviewed-by: Gary Guo <gary@garyguo.net> Reviewed-by: Danilo Krummrich <dakr@kernel.org> Cc: stable@vger.kernel.org Fixes: c59a2d14cd24 ("rust: num: add `shr` and `shl` methods to `Bounded`") Link: https://patch.msgid.link/20260810-pramin-split-v2-2-65a00b3c7309@nvidia.com Signed-off-by: Miguel Ojeda <ojeda@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
-rw-r--r--rust/kernel/num/bounded.rs1
1 files changed, 1 insertions, 0 deletions
diff --git a/rust/kernel/num/bounded.rs b/rust/kernel/num/bounded.rs
index 304ef04d86cd..6d766c22e61b 100644
--- a/rust/kernel/num/bounded.rs
+++ b/rust/kernel/num/bounded.rs
@@ -495,6 +495,7 @@ where
/// assert_eq!(v_shifted.get(), 0xff);
/// ```
pub fn shr<const SHIFT: u32, const RES: u32>(self) -> Bounded<T, RES> {
+ const_assert!(SHIFT < T::BITS);
const { assert!(RES + SHIFT >= N) }
// SAFETY: We shift the value right by `SHIFT`, reducing the number of bits needed to