Skip to content

[Deepin-Kernel-SIG] [linux 6.18.y] [FROMLIST] [Security] rust: add untrusted data API with Validate trait and defconfig updates#1754

Open
Avenger-285714 wants to merge 4 commits into
deepin-community:linux-6.18.yfrom
Avenger-285714:rust-untrusted-6.18
Open

[Deepin-Kernel-SIG] [linux 6.18.y] [FROMLIST] [Security] rust: add untrusted data API with Validate trait and defconfig updates#1754
Avenger-285714 wants to merge 4 commits into
deepin-community:linux-6.18.yfrom
Avenger-285714:rust-untrusted-6.18

Conversation

@Avenger-285714
Copy link
Copy Markdown
Member

@Avenger-285714 Avenger-285714 commented May 20, 2026

Please note that the deepin kernel still defaults to building with GCC. Make sure all CI environments are configured so that running make rustavailable prints Rust is available!.

Summary by Sourcery

Introduce a Rust untrusted data abstraction and related utilities for safely handling and validating data from untrusted sources.

New Features:

  • Add an Untrusted wrapper type and Validate/ValidateInput traits to model and validate untrusted data in Rust kernel code.
  • Expose the new validate module from the Rust kernel crate.
  • Provide safe transmute helpers to cast between slices of layout-compatible types, used by the untrusted data abstractions.

Avenger-285714 and others added 4 commits May 20, 2026 17:43
Add CONFIG_RUST=y to the deepin desktop defconfig files for arm64,
loongarch, s390x, and x86 architectures to enable kernel Rust support.

Please note that the deepin kernel still defaults to building with GCC.
Make sure all CI environments are configured so that running "make
rustavailable" prints "Rust is available!".

Signed-off-by: WangYuli <wangyl5933@chinaunicom.cn>
Add functions to make casting slices only one `unsafe` block.

Signed-off-by: Benno Lossin <benno.lossin@proton.me>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Link: https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core.git/commit/?h=untrusted&id=24faf69bdc54e056621786f4c48bc6cd15c3cbf6
Signed-off-by: WangYuli <wangyl5933@chinaunicom.cn>
When the kernel receives external data (e.g. from userspace), it usually
is a very bad idea to directly use the data for logic decision in the
kernel. For this reason, such data should be explicitly marked and
validated before making decision based on its value.

The `Untrusted<T>` wrapper type marks a value of type `T` as untrusted.
The particular meaning of "untrusted" highly depends on the type `T`.
For example `T = u8` ensures that the value of the byte cannot be
retrieved. However, `T = [u8]` still allows to access the length of the
slice. Similarly, `T = KVec<U>` allows modifications.

Signed-off-by: Benno Lossin <benno.lossin@proton.me>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[WangYuli: Fix conflicts]
Link: https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core.git/commit/?h=untrusted&id=3937bad8a8bf2e5d7fc3e11b4ed1aae21df71b02
Signed-off-by: WangYuli <wangyl5933@chinaunicom.cn>
Introduce the `Validate<Input>` trait and functions to validate
`Untrusted<T>` using said trait. This allows one to access the inner
value of `Untrusted<T>` via `validate{,_ref,_mut}` functions which
subsequently delegate the validation to user-implemented `Validate`
trait.

The `Validate` trait is the only entry point for validation code, making
it easy to spot where data is being validated.

The reason for restricting the types that can be inputs to
`Validate::validate` is to be able to have the `validate...` functions
on `Untrusted`. This is also the reason for the suggestions in the
`Usage in API Design` section in the commit that introduced
`Untrusted<T>`.

Signed-off-by: Benno Lossin <benno.lossin@proton.me>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Link: https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core.git/commit/?h=untrusted&id=4de2ce9189dcc45103f39fcc3e55eb1d792cf51b
Signed-off-by: WangYuli <wangyl5933@chinaunicom.cn>
@Avenger-285714 Avenger-285714 requested review from Copilot and opsiff May 20, 2026 10:51
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 20, 2026

Reviewer's Guide

Introduces a Rust "untrusted data" API centered around a new Untrusted wrapper and Validate trait, adds safe slice casting helpers in transmute, wires the new module into the Rust kernel crate, and updates deepin defconfigs for Rust support.

File-Level Changes

Change Details Files
Add generic unsafe helpers to reinterpret slices between types with identical layouts.
  • Introduce unsafe cast_slice<T, U>(&[T]) -> &[U] using pointer casts and slice::from_raw_parts.
  • Introduce unsafe cast_slice_mut<T, U>(&mut [T]) -> &mut [U] using pointer casts and slice::from_raw_parts_mut.
  • Document safety requirements (identical layout for T and U) and add examples using repr(transparent) wrappers.
rust/kernel/transmute.rs
Expose a new validate module from the Rust kernel crate.
  • Add pub mod validate; so the untrusted data API is available to Rust kernel code.
rust/kernel/lib.rs
Introduce an untrusted data abstraction and validation trait for Rust kernel code.
  • Add Untrusted wrapper type with constructors and validate/validate_ref/validate_mut methods that delegate to Validate implementations.
  • Provide Deref/DerefMut for Untrusted<[T]> to [Untrusted] using the new cast_slice helpers, enabling element-wise untrusted access.
  • Provide Deref/DerefMut for Untrusted<Vec<T, A>> to Vec<Untrusted, A> via transparent-pointer casting, treating vector elements as untrusted.
  • Define a sealed ValidateInput trait with implementations for Untrusted and references, exposing an Inner associated type for raw access.
  • Define a Validate<Input: ValidateInput> trait with associated Err and validate(raw: Input::Inner) -> Result<Self, Err>, giving controlled access to untrusted data for validation logic.
  • Seal ValidateInput via a private::Sealed trait to constrain who can implement it and prevent misuse.
rust/kernel/validate.rs
Adjust deepin defconfig files for Rust-related configuration (e.g., ensuring Rust availability in GCC-default builds).
  • Update per-arch deepin *_desktop_defconfig and s390x_z13_defconfig to reflect new Rust/untrusted-data support requirements (exact option changes not shown in diff).
arch/arm64/configs/deepin_arm64_desktop_defconfig
arch/loongarch/configs/deepin_loongarch_desktop_defconfig
arch/s390/configs/deepin_s390x_z13_defconfig
arch/x86/configs/deepin_x86_desktop_defconfig

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please ask for approval from avenger-285714. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="rust/kernel/validate.rs" line_range="68-69" />
<code_context>
+/// pub fn read_from_userspace(buf: &mut Untrusted<[u8]>) { todo!() }
+/// ```
+///
+/// The reason for this is that `&mut Untrusted<[u8]>` can beconverted into `&mut [Untrusted<u8>]`
+/// very easily, but the converse is not possible.
+///
</code_context>
<issue_to_address>
**nitpick (typo):** Fix a small documentation typo in the `Untrusted` usage section.

There’s a missing space in "can beconverted"; please change it to "can be converted".

```suggestion
//// The reason for this is that `&mut Untrusted<[u8]>` can be converted into `&mut [Untrusted<u8>]`
/// very easily, but the converse is not possible.
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread rust/kernel/validate.rs
Comment on lines +68 to +69
/// The reason for this is that `&mut Untrusted<[u8]>` can beconverted into `&mut [Untrusted<u8>]`
/// very easily, but the converse is not possible.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick (typo): Fix a small documentation typo in the Untrusted usage section.

There’s a missing space in "can beconverted"; please change it to "can be converted".

Suggested change
/// The reason for this is that `&mut Untrusted<[u8]>` can beconverted into `&mut [Untrusted<u8>]`
/// very easily, but the converse is not possible.
//// The reason for this is that `&mut Untrusted<[u8]>` can be converted into `&mut [Untrusted<u8>]`
/// very easily, but the converse is not possible.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a Rust “untrusted data” abstraction to help prevent accidental use of attacker-controlled bytes for kernel logic, and enables Rust support in Deepin defconfigs so the new Rust-side APIs can be built.

Changes:

  • Introduce rust/kernel/validate.rs with Untrusted<T> and a Validate trait to gate access to untrusted inputs.
  • Add cast_slice / cast_slice_mut helpers to rust/kernel/transmute.rs to support transparent-wrapper slice casting.
  • Enable CONFIG_RUST=y in multiple Deepin arch defconfigs and export the new module via rust/kernel/lib.rs.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
rust/kernel/validate.rs New untrusted-data wrapper and validation trait intended to prevent logic on unvalidated input.
rust/kernel/transmute.rs Adds slice-casting helpers used by Untrusted deref implementations.
rust/kernel/lib.rs Exposes the new validate module.
arch/x86/configs/deepin_x86_desktop_defconfig Enables Rust in the x86 Deepin desktop defconfig.
arch/s390/configs/deepin_s390x_z13_defconfig Enables Rust in the s390x z13 Deepin defconfig.
arch/loongarch/configs/deepin_loongarch_desktop_defconfig Enables Rust in the LoongArch Deepin desktop defconfig.
arch/arm64/configs/deepin_arm64_desktop_defconfig Enables Rust in the arm64 Deepin desktop defconfig.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread rust/kernel/validate.rs
/// pub fn read_from_userspace(buf: &mut Untrusted<[u8]>) { todo!() }
/// ```
///
/// The reason for this is that `&mut Untrusted<[u8]>` can beconverted into `&mut [Untrusted<u8>]`
Comment thread rust/kernel/validate.rs
type Inner: ?Sized;
}

impl<T: ?Sized> ValidateInput for Untrusted<T> {
Comment thread rust/kernel/transmute.rs
Comment on lines +185 to +211
/// Casts the type of a slice to another.
///
/// Also see [`cast_slice_mut`].
///
/// # Examples
///
/// ```rust
/// # use kernel::transmute::cast_slice;
/// #[repr(transparent)]
/// #[derive(Debug)]
/// struct Container<T>(T);
///
/// let array = [0u32; 42];
/// let slice = &array;
/// // SAFETY: `Container<u32>` transparently wraps a `u32`.
/// let container_slice = unsafe { cast_slice::<u32, Container<u32>>(slice) };
/// pr_info!("{container_slice:?}");
/// ```
///
/// # Safety
///
/// - `T` and `U` must have the same layout.
pub unsafe fn cast_slice<T, U>(slice: &[T]) -> &[U] {
// CAST: by the safety requirements, `T` and `U` have the same layout.
let ptr = slice.as_ptr().cast::<U>();
// SAFETY: `ptr` and `len` come from the same slice reference.
unsafe { slice::from_raw_parts(ptr, slice.len()) }
Comment thread rust/kernel/transmute.rs
Comment on lines +214 to +241
/// Casts the type of a slice to another.
///
/// Also see [`cast_slice`].
///
/// # Examples
///
/// ```rust
/// # use kernel::transmute::cast_slice_mut;
/// #[repr(transparent)]
/// #[derive(Debug)]
/// struct Container<T>(T);
///
/// let mut array = [0u32; 42];
/// let slice = &mut array;
/// // SAFETY: `Container<u32>` transparently wraps a `u32`.
/// let container_slice = unsafe { cast_slice_mut::<u32, Container<u32>>(slice) };
/// pr_info!("{container_slice:?}");
/// ```
///
/// # Safety
///
/// - `T` and `U` must have the same layout.
pub unsafe fn cast_slice_mut<T, U>(slice: &mut [T]) -> &mut [U] {
// CAST: by the safety requirements, `T` and `U` have the same layout.
let ptr = slice.as_mut_ptr().cast::<U>();
// SAFETY: `ptr` and `len` come from the same slice reference.
unsafe { slice::from_raw_parts_mut(ptr, slice.len()) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants