Skip to contentSkip to conversation

rust: num: seal Integer

PATCH Applied Younes Akhouayri 3 participants 3 messages Sep 5 – Sep 6

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 rust: num: seal Integer

Bounded relies on Integer implementations to describe primitive integer semantics correctly. In particular, it uses Integer::BITS and Signedness to justify unchecked operations.

Integer is currently safe and externally implementable, so an implementation can violate those assumptions and make safe Bounded operations reach undefined behavior.

For example, an Integer implementation for a u8 wrapper can report BITS = 16. Safe code can then cast a Bounded<u16, 9> containing 256 to that wrapper. Its TryFrom<u16> implementation returns Err, and Bounded::cast() calls unwrap_unchecked() on it, causing undefined behavior.

Seal Integer so only the primitive implementations provided by the kernel crate can satisfy it.

---
Changes in v2:
- Explain how an incorrect Integer implementation can cause undefined behavior.
- Add the missing Reported-by trailer.
- Link to v1: https://patch.msgid.link/20260905-feature-rust-num-seal-integer-v1-1-83096115ad64@younes.io

---
 rust/kernel/num.rs | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
rust/kernel/num.rs +8 -1
index dbe848e30efe..de589792a77a 100644
@@ -15,9 +15,14 @@ pub enum Unsigned {}
 /// Designates signed primitive types.
 pub enum Signed {}
 
+mod private {
+    pub trait Sealed {}
+}
+
 /// Describes core properties of integer types.
 pub trait Integer:
-    Sized
+    private::Sealed
+    + Sized
     + Copy
     + Clone
     + PartialEq
@@ -56,6 +61,8 @@ pub trait Integer:
 macro_rules! impl_integer {
     ($($type:ty: $signedness:ty), *) => {
         $(
+        impl private::Sealed for $type {}
+
         impl Integer for $type {
             type Signedness = $signedness;
 

---
base-commit: e510334fbaeaa016ac76d80b4c5f47611c5f7860
change-id: 20260903-feature-rust-num-seal-integer-a4262df429c6

Best regards,

Signature · 1 line
Younes Akhouayri <[email protected]>
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 19 lines · On Sun Sep 6, 2026 at 12:16 AM JST, Younes Akhouayri via B4 Relay wrote:

From: Younes Akhouayri <[email protected]>

Bounded relies on Integer implementations to describe primitive integer semantics correctly. In particular, it uses Integer::BITS and Signedness to justify unchecked operations.

Integer is currently safe and externally implementable, so an implementation can violate those assumptions and make safe Bounded operations reach undefined behavior.

For example, an Integer implementation for a u8 wrapper can report BITS = 16. Safe code can then cast a Bounded<u16, 9> containing 256 to that wrapper. Its TryFrom<u16> implementation returns Err, and Bounded::cast() calls unwrap_unchecked() on it, causing undefined behavior.

Seal Integer so only the primitive implementations provided by the kernel crate can satisfy it.

Quoted 6 lines

Thanks for taking care of this!

Applied
#2 lore raw
In reply to Younes Akhouayri
Quoted 20 lines · On Sat, Sep 5, 2026 at 5:17 PM Younes Akhouayri via B4 Relay…

From: Younes Akhouayri <[email protected]>

Bounded relies on Integer implementations to describe primitive integer semantics correctly. In particular, it uses Integer::BITS and Signedness to justify unchecked operations.

Integer is currently safe and externally implementable, so an implementation can violate those assumptions and make safe Bounded operations reach undefined behavior.

For example, an Integer implementation for a u8 wrapper can report BITS = 16. Safe code can then cast a Bounded<u16, 9> containing 256 to that wrapper. Its TryFrom<u16> implementation returns Err, and Bounded::cast() calls unwrap_unchecked() on it, causing undefined behavior.

Seal Integer so only the primitive implementations provided by the kernel crate can satisfy it.

Quoted 6 lines

Applied to rust-fixes -- thanks everyone!

It would be nice to explain somewhere in the code why the trait is sealed, especially if we rely on it for safety, but that can be improved later.

Cheers,
Miguel