lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite for Android: free password hash cracker in your pocket
[<prev] [next>] [day] [month] [year] [list]
Message-ID: <20250819003824.23019-2-contact@antoniohickey.com>
Date: Tue, 19 Aug 2025 00:38:46 +0000
From: Antonio Hickey <contact@...oniohickey.com>
To: Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>, Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>, Björn Roy Baron <bjorn3_gh@...tonmail.com>, Benno Lossin <lossin@...nel.org>, Andreas Hindborg <a.hindborg@...nel.org>, Alice Ryhl <aliceryhl@...gle.com>, Trevor Gross <tmgross@...ch.edu>, Danilo Krummrich <dakr@...nel.org>
Cc: Antonio Hickey <contact@...oniohickey.com>, Daniel Cote <danielstonecote@...il.com>, linux-kernel@...r.kernel.org, rust-for-linux@...r.kernel.org
Subject: [PATCH v5 1/2] rust: kernel: create `overflow_assert!` macro

This commit creates a macro for overflow assertions, the use of this
macro will avoid local `#ifdef`s by encapsulating the conditional
behavior (like `#[cfg(CONFIG_RUST_OVERFLOW_CHECKS)]`) to the macro.

In addition this macro allows us to document the intent of the assertion
more clearly.

Co-developed-by: Daniel Cote <danielstonecote@...il.com>
Signed-off-by: Daniel Cote <danielstonecote@...il.com>
Signed-off-by: Antonio Hickey <contact@...oniohickey.com>
Link: https://github.com/Rust-for-Linux/linux/issues/1159
Suggested-by: Miguel Ojeda <ojeda@...nel.org>
---
 rust/kernel/lib.rs             |  1 +
 rust/kernel/overflow_assert.rs | 44 ++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)
 create mode 100644 rust/kernel/overflow_assert.rs

diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index ed53169e795c..901e54a509a3 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -103,6 +103,7 @@
 pub mod of;
 #[cfg(CONFIG_PM_OPP)]
 pub mod opp;
+pub mod overflow_assert;
 pub mod page;
 #[cfg(CONFIG_PCI)]
 pub mod pci;
diff --git a/rust/kernel/overflow_assert.rs b/rust/kernel/overflow_assert.rs
new file mode 100644
index 000000000000..f3de3b1b2cf3
--- /dev/null
+++ b/rust/kernel/overflow_assert.rs
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Overflow assertion.
+
+/// Asserts that a boolean expression is `true` at runtime.
+///
+/// This will invoke the [`panic!`] macro if the provided
+/// expression cannot be evaluated to `true` at runtime.
+///
+/// This macro only has effect when `CONFIG_RUST_OVERFLOW_CHECKS`
+/// is enabled, otherwise it expands to a no-op.
+///
+/// This assertion is intended only for extra validation within
+/// builds and environments where panics are acceptable. **Do not
+/// rely on `overflow_assert!` for checks that must *always* execute**
+/// (e.g. to prevent undefined behavior, perform access checks, etc).
+///
+/// # Examples
+///
+/// Basic boolean condition:
+///
+/// ```
+/// let a: u32 = 10;
+/// let b: u32 = 5;
+/// overflow_assert!(a >= b);
+/// ```
+///
+/// A guard before doing a size computation that could overflow:
+/// ```
+/// fn reserve_for_concat(curr: usize, to_add: usize, cap: usize) {
+///     // If enabled, catch obvious overflow logic errors early:
+///     overflow_assert!(curr <= cap, "curr={} > cap={}", curr, cap);
+///     overflow_assert!(to_add <= cap - curr, "would exceed cap: {}+{} > {}", curr, to_add, cap);
+///     // ... then proceed to grow/append
+/// }
+/// ```
+#[macro_export]
+macro_rules! overflow_assert {
+    ($($arg:tt)*) => {
+        if cfg!(CONFIG_RUST_OVERFLOW_CHECKS) {
+            ::core::assert!($($arg)*);
+        }
+    };
+}
-- 
2.50.1



Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ