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: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250918144356.28585-2-manerakai@protonmail.com>
Date: Thu, 18 Sep 2025 14:45:28 +0000
From: ManeraKai <manerakai@...tonmail.com>
To: "aliceryhl@...gle.com" <aliceryhl@...gle.com>, "gregkh@...uxfoundation.org" <gregkh@...uxfoundation.org>, "arnd@...db.de" <arnd@...db.de>
Cc: "rust-for-linux@...r.kernel.org" <rust-for-linux@...r.kernel.org>, "linux-fsdevel@...r.kernel.org" <linux-fsdevel@...r.kernel.org>, "linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>, "manerakai@...tonmail.com" <manerakai@...tonmail.com>
Subject: [PATCH 1/3] rust: miscdevice: Moved `MiscDevice` to a more general abstraction

This new general abstraction is called `FileOperations`.

`struct file_operations` is not only meant for misc. Its methods are
accessible from any other driver type. This change, however, doesn't
generalize the safe wrapping for all driver types, but rather just the
method declarations. The actual safe wrappings are left for every driver
type to implement. This may make each implementation simpler. For
example, misc can choose not to implement the safe wrapping for `lock`,
`sendfile`, or `sendpage`, since they have no use in misc drivers.

Signed-off-by: ManeraKai <manerakai@...tonmail.com>
---
 rust/kernel/fs.rs                 |  1 +
 rust/kernel/fs/file_operations.rs | 91 +++++++++++++++++++++++++++++++
 rust/kernel/miscdevice.rs         | 86 ++---------------------------
 samples/rust/rust_misc_device.rs  |  6 +-
 4 files changed, 101 insertions(+), 83 deletions(-)
 create mode 100644 rust/kernel/fs/file_operations.rs

diff --git a/rust/kernel/fs.rs b/rust/kernel/fs.rs
index 0121b38c59e6..94519b41086b 100644
--- a/rust/kernel/fs.rs
+++ b/rust/kernel/fs.rs
@@ -5,4 +5,5 @@
 //! C headers: [`include/linux/fs.h`](srctree/include/linux/fs.h)
 
 pub mod file;
+pub mod file_operations;
 pub use self::file::{File, LocalFile};
diff --git a/rust/kernel/fs/file_operations.rs b/rust/kernel/fs/file_operations.rs
new file mode 100644
index 000000000000..aa60cd46a012
--- /dev/null
+++ b/rust/kernel/fs/file_operations.rs
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Wrapper for struct file_operations.
+//!
+//! C headers: [`include/linux/fs.h`](srctree/include/linux/fs.h).
+
+use macros::vtable;
+
+#[cfg(CONFIG_COMPAT)]
+use crate::fs::File;
+use crate::{
+    build_error,
+    error::{Result, VTABLE_DEFAULT_ERROR},
+    miscdevice::MiscDeviceRegistration,
+    mm::virt::VmaNew,
+    seq_file::SeqFile,
+    types::ForeignOwnable,
+};
+
+/// Trait implemented by the private data of an open misc device.
+#[vtable]
+pub trait FileOperations: Sized {
+    /// What kind of pointer should `Self` be wrapped in.
+    type Ptr: ForeignOwnable + Send + Sync;
+
+    /// Called when the misc device is opened.
+    ///
+    /// The returned pointer will be stored as the private data for the file.
+    fn open(_file: &File, _misc: &MiscDeviceRegistration<Self>) -> Result<Self::Ptr>;
+
+    /// Called when the misc device is released.
+    fn release(device: Self::Ptr, _file: &File) {
+        drop(device);
+    }
+
+    /// Handle for mmap.
+    ///
+    /// This function is invoked when a user space process invokes the `mmap` system call on
+    /// `file`. The function is a callback that is part of the VMA initializer. The kernel will do
+    /// initial setup of the VMA before calling this function. The function can then interact with
+    /// the VMA initialization by calling methods of `vma`. If the function does not return an
+    /// error, the kernel will complete initialization of the VMA according to the properties of
+    /// `vma`.
+    fn mmap(
+        _device: <Self::Ptr as ForeignOwnable>::Borrowed<'_>,
+        _file: &File,
+        _vma: &VmaNew,
+    ) -> Result {
+        build_error!(VTABLE_DEFAULT_ERROR)
+    }
+
+    /// Handler for ioctls.
+    ///
+    /// The `cmd` argument is usually manipulated using the utilities in [`kernel::ioctl`].
+    ///
+    /// [`kernel::ioctl`]: mod@...te::ioctl
+    fn ioctl(
+        _device: <Self::Ptr as ForeignOwnable>::Borrowed<'_>,
+        _file: &File,
+        _cmd: u32,
+        _arg: usize,
+    ) -> Result<isize> {
+        build_error!(VTABLE_DEFAULT_ERROR)
+    }
+
+    /// Handler for ioctls.
+    ///
+    /// Used for 32-bit userspace on 64-bit platforms.
+    ///
+    /// This method is optional and only needs to be provided if the ioctl relies on structures
+    /// that have different layout on 32-bit and 64-bit userspace. If no implementation is
+    /// provided, then `compat_ptr_ioctl` will be used instead.
+    #[cfg(CONFIG_COMPAT)]
+    fn compat_ioctl(
+        _device: <Self::Ptr as ForeignOwnable>::Borrowed<'_>,
+        _file: &File,
+        _cmd: u32,
+        _arg: usize,
+    ) -> Result<isize> {
+        build_error!(VTABLE_DEFAULT_ERROR)
+    }
+
+    /// Show info for this fd.
+    fn show_fdinfo(
+        _device: <Self::Ptr as ForeignOwnable>::Borrowed<'_>,
+        _m: &SeqFile,
+        _file: &File,
+    ) {
+        build_error!(VTABLE_DEFAULT_ERROR)
+    }
+}
diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs
index 6373fe183b27..578f33383ce6 100644
--- a/rust/kernel/miscdevice.rs
+++ b/rust/kernel/miscdevice.rs
@@ -11,9 +11,9 @@
 use crate::{
     bindings,
     device::Device,
-    error::{to_result, Error, Result, VTABLE_DEFAULT_ERROR},
+    error::{to_result, Error},
     ffi::{c_int, c_long, c_uint, c_ulong},
-    fs::File,
+    fs::{file_operations::FileOperations, File},
     mm::virt::VmaNew,
     prelude::*,
     seq_file::SeqFile,
@@ -30,7 +30,7 @@ pub struct MiscDeviceOptions {
 
 impl MiscDeviceOptions {
     /// Create a raw `struct miscdev` ready for registration.
-    pub const fn into_raw<T: MiscDevice>(self) -> bindings::miscdevice {
+    pub const fn into_raw<T: FileOperations>(self) -> bindings::miscdevice {
         // SAFETY: All zeros is valid for this C type.
         let mut result: bindings::miscdevice = unsafe { MaybeUninit::zeroed().assume_init() };
         result.minor = bindings::MISC_DYNAMIC_MINOR as ffi::c_int;
@@ -66,7 +66,7 @@ unsafe impl<T> Send for MiscDeviceRegistration<T> {}
 // parallel.
 unsafe impl<T> Sync for MiscDeviceRegistration<T> {}
 
-impl<T: MiscDevice> MiscDeviceRegistration<T> {
+impl<T: FileOperations> MiscDeviceRegistration<T> {
     /// Register a misc device.
     pub fn register(opts: MiscDeviceOptions) -> impl PinInit<Self, Error> {
         try_pin_init!(Self {
@@ -108,84 +108,10 @@ fn drop(self: Pin<&mut Self>) {
         unsafe { bindings::misc_deregister(self.inner.get()) };
     }
 }
-
-/// Trait implemented by the private data of an open misc device.
-#[vtable]
-pub trait MiscDevice: Sized {
-    /// What kind of pointer should `Self` be wrapped in.
-    type Ptr: ForeignOwnable + Send + Sync;
-
-    /// Called when the misc device is opened.
-    ///
-    /// The returned pointer will be stored as the private data for the file.
-    fn open(_file: &File, _misc: &MiscDeviceRegistration<Self>) -> Result<Self::Ptr>;
-
-    /// Called when the misc device is released.
-    fn release(device: Self::Ptr, _file: &File) {
-        drop(device);
-    }
-
-    /// Handle for mmap.
-    ///
-    /// This function is invoked when a user space process invokes the `mmap` system call on
-    /// `file`. The function is a callback that is part of the VMA initializer. The kernel will do
-    /// initial setup of the VMA before calling this function. The function can then interact with
-    /// the VMA initialization by calling methods of `vma`. If the function does not return an
-    /// error, the kernel will complete initialization of the VMA according to the properties of
-    /// `vma`.
-    fn mmap(
-        _device: <Self::Ptr as ForeignOwnable>::Borrowed<'_>,
-        _file: &File,
-        _vma: &VmaNew,
-    ) -> Result {
-        build_error!(VTABLE_DEFAULT_ERROR)
-    }
-
-    /// Handler for ioctls.
-    ///
-    /// The `cmd` argument is usually manipulated using the utilities in [`kernel::ioctl`].
-    ///
-    /// [`kernel::ioctl`]: mod@...te::ioctl
-    fn ioctl(
-        _device: <Self::Ptr as ForeignOwnable>::Borrowed<'_>,
-        _file: &File,
-        _cmd: u32,
-        _arg: usize,
-    ) -> Result<isize> {
-        build_error!(VTABLE_DEFAULT_ERROR)
-    }
-
-    /// Handler for ioctls.
-    ///
-    /// Used for 32-bit userspace on 64-bit platforms.
-    ///
-    /// This method is optional and only needs to be provided if the ioctl relies on structures
-    /// that have different layout on 32-bit and 64-bit userspace. If no implementation is
-    /// provided, then `compat_ptr_ioctl` will be used instead.
-    #[cfg(CONFIG_COMPAT)]
-    fn compat_ioctl(
-        _device: <Self::Ptr as ForeignOwnable>::Borrowed<'_>,
-        _file: &File,
-        _cmd: u32,
-        _arg: usize,
-    ) -> Result<isize> {
-        build_error!(VTABLE_DEFAULT_ERROR)
-    }
-
-    /// Show info for this fd.
-    fn show_fdinfo(
-        _device: <Self::Ptr as ForeignOwnable>::Borrowed<'_>,
-        _m: &SeqFile,
-        _file: &File,
-    ) {
-        build_error!(VTABLE_DEFAULT_ERROR)
-    }
-}
-
 /// A vtable for the file operations of a Rust miscdevice.
-struct MiscdeviceVTable<T: MiscDevice>(PhantomData<T>);
+struct MiscdeviceVTable<T: FileOperations>(PhantomData<T>);
 
-impl<T: MiscDevice> MiscdeviceVTable<T> {
+impl<T: FileOperations> MiscdeviceVTable<T> {
     /// # Safety
     ///
     /// `file` and `inode` must be the file and inode for a file that is undergoing initialization.
diff --git a/samples/rust/rust_misc_device.rs b/samples/rust/rust_misc_device.rs
index e7ab77448f75..d052294cebb8 100644
--- a/samples/rust/rust_misc_device.rs
+++ b/samples/rust/rust_misc_device.rs
@@ -100,9 +100,9 @@
 use kernel::{
     c_str,
     device::Device,
-    fs::File,
+    fs::{file_operations::FileOperations, File},
     ioctl::{_IO, _IOC_SIZE, _IOR, _IOW},
-    miscdevice::{MiscDevice, MiscDeviceOptions, MiscDeviceRegistration},
+    miscdevice::{MiscDeviceOptions, MiscDeviceRegistration},
     new_mutex,
     prelude::*,
     sync::Mutex,
@@ -154,7 +154,7 @@ struct RustMiscDevice {
 }
 
 #[vtable]
-impl MiscDevice for RustMiscDevice {
+impl FileOperations for RustMiscDevice {
     type Ptr = Pin<KBox<Self>>;
 
     fn open(_file: &File, misc: &MiscDeviceRegistration<Self>) -> Result<Pin<KBox<Self>>> {
-- 
2.43.0



Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ