rust: miscdevice: Export vtable testing
Subsystems (2)
PATCH 1/1 rust: miscdevice: Export vtable testing
A common pattern in the kernel is to test whether a file belongs to a particular driver by checking its f_op struct against an expected value. This provides a safe way to perform that test for MiscDevice implementations without needing to directly expose the vtable.
--- rust/kernel/miscdevice.rs | 8 ++++++++ 1 file changed, 8 insertions(+)
rust/kernel/miscdevice.rs +8 -0
@@ -177,6 +177,14 @@ fn show_fdinfo( } } +/// Determines whether a given `File` is backed by the `T` `MiscDevice` based on vtable matching. +pub fn is_miscdevice_file <T :MiscDevice >(file: &File ) ->bool { +let vtable = core::ptr::from_ref (&MiscdeviceVTable ::<T >::VTABLE ); +// SAFETY: `f_op` is not mutated after file creation +let file_vtable =unsafe { (*file.as_ptr ()).f_op }; + vtable == file_vtable +} +/// A vtable for the file operations of a Rust miscdevice. struct MiscdeviceVTable <T :MiscDevice >(PhantomData <T >);
---
base-commit: 86731a2a651e58953fc949573895f2fa6d456841
change-id: 20250627-linux-miscident-7b67db234a5c
Best regards,
Signature · 1 line
Matthew Maurer <[email protected]>
Quoted 7 lines · Matthew Maurer
A common pattern in the kernel is to test whether a file belongs to a particular driver by checking its
f_opstruct against an expected value. This provides a safe way to perform that test forMiscDeviceimplementations without needing to directly expose the vtable.Signed-off-by Matthew Maurer <[email protected]>
Additionally, we have a sample user[1] of this in the Android ashmem wrapper. They're currently working around it by grabbing the vtable out of the registration and testing manually.
[1]: https://android-review.git.corp.google.com/c/kernel/common/+/3477511
Quoted 32 lines
--- rust/kernel/miscdevice.rs | 8 ++++++++ 1 file changed, 8 insertions(+)rust/kernel/miscdevice.rs +8
-1@@ -177,6 +177,14 @@ fn show_fdinfo( } } +/// Determines whether a given `File` is backed by the `T` `MiscDevice` based on vtable matching. +pub fn is_miscdevice_file <T :MiscDevice >(file: &File ) ->bool { +let vtable = core::ptr::from_ref (&MiscdeviceVTable ::<T >::VTABLE ); +// SAFETY: `f_op` is not mutated after file creation +let file_vtable =unsafe { (*file.as_ptr ()).f_op }; + vtable == file_vtable +} +/// A vtable for the file operations of a Rust miscdevice. struct MiscdeviceVTable <T :MiscDevice >(PhantomData <T >); ---base-commit:
86731a2a651e58953fc949573895f2fa6d456841
change-id: 20250627-linux-miscident-7b67db234a5cBest regards,
--
Matthew Maurer <[email protected]>
Greg KH
maintainerQuoted 7 lines · Matthew Maurer
Quoted 6 lines · Matthew Maurer
A common pattern in the kernel is to test whether a file belongs to a particular driver by checking its
f_opstruct against an expected value. This provides a safe way to perform that test forMiscDeviceimplementations without needing to directly expose the vtable.
Quoted 7 lines
Quoted 1 line
Signed-off-by Matthew Maurer <[email protected]>Additionally, we have a sample user[1] of this in the Android ashmem wrapper. They're currently working around it by grabbing the vtable out of the registration and testing manually.
[1]: https://android-review.git.corp.google.com/c/kernel/common/+/3477511
As ashmem isn't going to be upstream, it's hard for us to treat this as a real user :)
Benno Lossin
reviewerQuoted 15 lines · Matthew Maurer
A common pattern in the kernel is to test whether a file belongs to a particular driver by checking its
f_opstruct against an expected value. This provides a safe way to perform that test forMiscDeviceimplementations without needing to directly expose the vtable.Signed-off-by Matthew Maurer <[email protected]>--- rust/kernel/miscdevice.rs | 8 ++++++++ 1 file changed, 8 insertions(+)rust/kernel/miscdevice.rs +0
-0
rust/kernel/miscdevice.rs fn show_fdinfo( · line 182
Quoted 6 lines
rust/kernel/miscdevice.rs +3
-0@@ -177,6 +177,14 @@ fn show_fdinfo( } } +/// Determines whether a given `File` is backed by the `T` `MiscDevice` based on vtable matching. +pub fn is_miscdevice_file <T :MiscDevice >(file: &File ) ->bool { +let vtable = core::ptr::from_ref (&MiscdeviceVTable ::<T >::VTABLE );
I don't think this always returns the same pointer. So this function might return false in a case where T actually backs file...
---
Cheers,
Benno
Quoted 14 lines
rust/kernel/miscdevice.rs +5
-1+// SAFETY: `f_op` is not mutated after file creation +let file_vtable =unsafe { (*file.as_ptr ()).f_op }; + vtable == file_vtable +} +/// A vtable for the file operations of a Rust miscdevice. struct MiscdeviceVTable <T :MiscDevice >(PhantomData <T >); ---base-commit:
86731a2a651e58953fc949573895f2fa6d456841
change-id: 20250627-linux-miscident-7b67db234a5cBest regards,
Greg KH
maintainerQuoted 4 lines · Matthew Maurer
A common pattern in the kernel is to test whether a file belongs to a particular driver by checking its
f_opstruct against an expected value. This provides a safe way to perform that test forMiscDeviceimplementations without needing to directly expose the vtable.
Ick, who does that? And why? Who cares within the kernel what driver owns a fd as why would any driver ever be passed a fd that is not owned by it?
I would like to see a real user first please, or point out some places in the kernel today that does this so we can go and fix them up :)
thanks,
greg k-h