Skip to content

Commit b2bb147

Browse files
plinkrintel-lab-lkp
authored andcommitted
rust: Move FromBytes and AsBytes traits to a new transmute module
This patch refactors the `FromBytes` and `AsBytes` traits from `types.rs` into a new `transmute.rs` module. The traits and their implementations remain unchanged. - Adds `rust/kernel/transmute.rs` with the definitions of `FromBytes` and `AsBytes` - Removes the same trait definitions from `rust/kernel/types.rs` - Updates `rust/kernel/uaccess.rs` to import `AsBytes` and `FromBytes` from `transmute.rs` Suggested-by: Benno Lossin <[email protected]> Link: Rust-for-Linux#1117 Signed-off-by: Aliet Exposito Garcia <[email protected]>
1 parent a2f1154 commit b2bb147

File tree

4 files changed

+69
-65
lines changed

4 files changed

+69
-65
lines changed

rust/kernel/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub mod str;
5252
pub mod sync;
5353
pub mod task;
5454
pub mod time;
55+
pub mod transmute;
5556
pub mod types;
5657
pub mod uaccess;
5758
pub mod workqueue;

rust/kernel/transmute.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Traits for transmuting types.
4+
5+
/// Types for which any bit pattern is valid.
6+
///
7+
/// Not all types are valid for all values. For example, a `bool` must be either zero or one, so
8+
/// reading arbitrary bytes into something that contains a `bool` is not okay.
9+
///
10+
/// It's okay for the type to have padding, as initializing those bytes has no effect.
11+
///
12+
/// # Safety
13+
///
14+
/// All bit-patterns must be valid for this type. This type must not have interior mutability.
15+
pub unsafe trait FromBytes {}
16+
17+
// SAFETY: All bit patterns are acceptable values of the types below.
18+
unsafe impl FromBytes for u8 {}
19+
unsafe impl FromBytes for u16 {}
20+
unsafe impl FromBytes for u32 {}
21+
unsafe impl FromBytes for u64 {}
22+
unsafe impl FromBytes for usize {}
23+
unsafe impl FromBytes for i8 {}
24+
unsafe impl FromBytes for i16 {}
25+
unsafe impl FromBytes for i32 {}
26+
unsafe impl FromBytes for i64 {}
27+
unsafe impl FromBytes for isize {}
28+
// SAFETY: If all bit patterns are acceptable for individual values in an array, then all bit
29+
// patterns are also acceptable for arrays of that type.
30+
unsafe impl<T: FromBytes> FromBytes for [T] {}
31+
unsafe impl<T: FromBytes, const N: usize> FromBytes for [T; N] {}
32+
33+
/// Types that can be viewed as an immutable slice of initialized bytes.
34+
///
35+
/// If a struct implements this trait, then it is okay to copy it byte-for-byte to userspace. This
36+
/// means that it should not have any padding, as padding bytes are uninitialized. Reading
37+
/// uninitialized memory is not just undefined behavior, it may even lead to leaking sensitive
38+
/// information on the stack to userspace.
39+
///
40+
/// The struct should also not hold kernel pointers, as kernel pointer addresses are also considered
41+
/// sensitive. However, leaking kernel pointers is not considered undefined behavior by Rust, so
42+
/// this is a correctness requirement, but not a safety requirement.
43+
///
44+
/// # Safety
45+
///
46+
/// Values of this type may not contain any uninitialized bytes. This type must not have interior
47+
/// mutability.
48+
pub unsafe trait AsBytes {}
49+
50+
// SAFETY: Instances of the following types have no uninitialized portions.
51+
unsafe impl AsBytes for u8 {}
52+
unsafe impl AsBytes for u16 {}
53+
unsafe impl AsBytes for u32 {}
54+
unsafe impl AsBytes for u64 {}
55+
unsafe impl AsBytes for usize {}
56+
unsafe impl AsBytes for i8 {}
57+
unsafe impl AsBytes for i16 {}
58+
unsafe impl AsBytes for i32 {}
59+
unsafe impl AsBytes for i64 {}
60+
unsafe impl AsBytes for isize {}
61+
unsafe impl AsBytes for bool {}
62+
unsafe impl AsBytes for char {}
63+
unsafe impl AsBytes for str {}
64+
// SAFETY: If individual values in an array have no uninitialized portions, then the array itself
65+
// does not have any uninitialized portions either.
66+
unsafe impl<T: AsBytes> AsBytes for [T] {}
67+
unsafe impl<T: AsBytes, const N: usize> AsBytes for [T; N] {}

rust/kernel/types.rs

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -468,67 +468,3 @@ pub enum Either<L, R> {
468468
/// Constructs an instance of [`Either`] containing a value of type `R`.
469469
Right(R),
470470
}
471-
472-
/// Types for which any bit pattern is valid.
473-
///
474-
/// Not all types are valid for all values. For example, a `bool` must be either zero or one, so
475-
/// reading arbitrary bytes into something that contains a `bool` is not okay.
476-
///
477-
/// It's okay for the type to have padding, as initializing those bytes has no effect.
478-
///
479-
/// # Safety
480-
///
481-
/// All bit-patterns must be valid for this type. This type must not have interior mutability.
482-
pub unsafe trait FromBytes {}
483-
484-
// SAFETY: All bit patterns are acceptable values of the types below.
485-
unsafe impl FromBytes for u8 {}
486-
unsafe impl FromBytes for u16 {}
487-
unsafe impl FromBytes for u32 {}
488-
unsafe impl FromBytes for u64 {}
489-
unsafe impl FromBytes for usize {}
490-
unsafe impl FromBytes for i8 {}
491-
unsafe impl FromBytes for i16 {}
492-
unsafe impl FromBytes for i32 {}
493-
unsafe impl FromBytes for i64 {}
494-
unsafe impl FromBytes for isize {}
495-
// SAFETY: If all bit patterns are acceptable for individual values in an array, then all bit
496-
// patterns are also acceptable for arrays of that type.
497-
unsafe impl<T: FromBytes> FromBytes for [T] {}
498-
unsafe impl<T: FromBytes, const N: usize> FromBytes for [T; N] {}
499-
500-
/// Types that can be viewed as an immutable slice of initialized bytes.
501-
///
502-
/// If a struct implements this trait, then it is okay to copy it byte-for-byte to userspace. This
503-
/// means that it should not have any padding, as padding bytes are uninitialized. Reading
504-
/// uninitialized memory is not just undefined behavior, it may even lead to leaking sensitive
505-
/// information on the stack to userspace.
506-
///
507-
/// The struct should also not hold kernel pointers, as kernel pointer addresses are also considered
508-
/// sensitive. However, leaking kernel pointers is not considered undefined behavior by Rust, so
509-
/// this is a correctness requirement, but not a safety requirement.
510-
///
511-
/// # Safety
512-
///
513-
/// Values of this type may not contain any uninitialized bytes. This type must not have interior
514-
/// mutability.
515-
pub unsafe trait AsBytes {}
516-
517-
// SAFETY: Instances of the following types have no uninitialized portions.
518-
unsafe impl AsBytes for u8 {}
519-
unsafe impl AsBytes for u16 {}
520-
unsafe impl AsBytes for u32 {}
521-
unsafe impl AsBytes for u64 {}
522-
unsafe impl AsBytes for usize {}
523-
unsafe impl AsBytes for i8 {}
524-
unsafe impl AsBytes for i16 {}
525-
unsafe impl AsBytes for i32 {}
526-
unsafe impl AsBytes for i64 {}
527-
unsafe impl AsBytes for isize {}
528-
unsafe impl AsBytes for bool {}
529-
unsafe impl AsBytes for char {}
530-
unsafe impl AsBytes for str {}
531-
// SAFETY: If individual values in an array have no uninitialized portions, then the array itself
532-
// does not have any uninitialized portions either.
533-
unsafe impl<T: AsBytes> AsBytes for [T] {}
534-
unsafe impl<T: AsBytes, const N: usize> AsBytes for [T; N] {}

rust/kernel/uaccess.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
bindings,
1010
error::Result,
1111
prelude::*,
12-
types::{AsBytes, FromBytes},
12+
transmute::{AsBytes, FromBytes},
1313
};
1414
use alloc::vec::Vec;
1515
use core::ffi::{c_ulong, c_void};

0 commit comments

Comments
 (0)