drm/tyr: allow mismatched buffers for dev_query
Subsystems (4)
cover drm/tyr: allow mismatched buffers for dev_query
Panthor UAPI for DRM_IOCTL_TYR_DEV_QUERY allows userspace to provide a smaller or larger buffer than the kernel needs. Revise Tyr's implementation to match the expected UAPI behaviour.
---
Deborah Brouwer (2):
rust: uaccess: add UserSliceWriter::clear() drm/tyr: allow userspace buffers to be mismatched
drivers/gpu/drm/tyr/file.rs | 45 +++++++++++++++++++++++++++++++++++++-------- rust/helpers/uaccess.c | 6 ++++++ rust/kernel/uaccess.rs | 23 +++++++++++++++++++++++ 3 files changed, 66 insertions(+), 8 deletions(-)
---
base-commit: 5a1b7121a6c4db6c0239d3ac3fd1c2bb33a40025
change-id: 20261014-b4-dev_query_fixes-4e0ab024294a
Best regards,
Signature · 1 line
Deborah Brouwer <[email protected]>
PATCH 1/2 rust: uaccess: add UserSliceWriter::clear()
Add a method to zero the remaining bytes in a UserSliceWriter.
If userspace provides a buffer that is larger than necessary, provide a method to zero out the remaining UserSliceWriter buffer. This avoids userspace consuming potentially uninitialized memory and/or mistaking it for data intentionally provided by the kernel.
Since clear_user() is architecturally defined and may be a macro or inline function, add a rust helper to make clear_user() callable from rust.
--- rust/helpers/uaccess.c | 6 ++++++ rust/kernel/uaccess.rs | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+)
rust/helpers/uaccess.c +6 -0
@@ -27,3 +27,9 @@ unsigned long rust_helper__copy_to_user(void __user *to, const void *from, unsig return _inline_copy_to_user(to, from, n); }#endif + +__rust_helper unsigned long +rust_helper_clear_user (void __user *to,unsigned long n) +{ +return clear_user (to, n); +}
rust/kernel/uaccess.rs +23 -0
@@ -638,6 +638,29 @@ pub fn write<T: AsBytes>(&mut self, value: &T) -> Result {pub fn write_truncated <T :AsBytes >(&mut self, value: &T ) ->Result <usize > { self.write_slice_partial (value.as_bytes (),0 ) } + +/// Zeros remaining bytes in the userspace buffer. +/// +/// Fails with [`EFAULT`] if the userspace memory cannot be cleared. +pub fn clear (&mut self) ->Result { +let len = self.length; + +if len ==0 { +return Ok (()); + } +// SAFETY: `clear_user` checks the userspace address and range and handles +// faults when accessing userspace memory +let res =unsafe { bindings::clear_user (self.ptr.as_mut_ptr (), len) }; + +if res !=0 { +return Err (EFAULT ); + } + + self.ptr = self.ptr.wrapping_byte_add (len); + self.length =0 ; + +Ok (()) + } }/// Reads a nul-terminated string into `dst` and returns the length.
Signature · 1 line
2.56.0
PATCH 2/2 drm/tyr: allow userspace buffers to be mismatched
In reply to Deborah BrouwerAllow userspace to provide a buffer that is smaller or larger than the object that the kernel expects to write into it.
Set a minimum buffer size to match those in PANTHOR_UOBJ_MIN_SIZE. If userspace provides at least the minimum buffer size, then simply write until the buffer is full.
If userspace provides a larger buffer than necessary, then zero out the remaining buffer.
This ensures UAPI compatibility with Panthor.
--- drivers/gpu/drm/tyr/file.rs | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-)
drivers/gpu/drm/tyr/file.rs +37 -8
@@ -1,11 +1,17 @@// SPDX-License-Identifier: GPL-2.0 or MIT +use core::mem::{ + offset_of, + size_of_val,// +}; +use kernel::{ drm::{ self,Registered ,// }, prelude::*, + transmute::AsBytes , uaccess::UserSlice , uapi,// };
@@ -16,6 +22,31 @@ TyrDrmRegistrationData, // }; +trait UobjMinSize { +fn min_size (&self) ->usize ; +} + +impl UobjMinSize for uapi::drm_panthor_gpu_info { +fn min_size (&self) ->usize { +offset_of! (Self , tiler_present) +size_of_val (&self.tiler_present) + } +} + +// If the user buffer is at least the minimum required size, then +// copy as much of the kernel object as will fit into the user buffer. +// Don't fail if the user buffer is smaller than the kernel object. +// If the user buffer is bigger than needed, zero out the remainder. +fn set_uobj <T :AsBytes >(usr_dst:u64 , size:u32 , min_size:usize , src_obj: &T ) ->Result { +if (sizeas usize ) < min_size { +return Err (EINVAL ); + } + +let mut writer =UserSlice ::new (UserPtr ::from_addr (usr_dstas usize ), sizeas usize ).writer (); + writer.write_truncated (src_obj)?; + writer.clear ()?; +Ok (()) +} +#[pin_data] pub (crate )struct TyrDrmFileData {}
@@ -48,14 +79,12 @@ pub(crate) fn dev_query( }else {match devquery.type_ { uapi::drm_panthor_dev_query_type_DRM_PANTHOR_DEV_QUERY_GPU_INFO => { -let mut writer =UserSlice ::new ( -UserPtr ::from_addr (devquery.pointeras usize ), - devquery.sizeas usize , - ) - .writer (); - - writer.write (®_data.gpu_info)?; - +set_uobj ( + devquery.pointer, + devquery.size, + reg_data.gpu_info.min_size (), + ®_data.gpu_info, + )?;Ok (0 ) } _ =>Err (EINVAL ),
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH 1/2] rust: uaccess: add UserSliceWriter::clear()
In reply to Deborah BrouwerThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] rust: uaccess: UserSliceWriter::clear() missing #[inline] attribute --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] rust: uaccess: UserSliceWriter::clear() missing #[inline] attribute --
commit 3db28db591b863b3412fb9ace61521aee539bc5a
Author: Deborah Brouwer <[email protected]>
rust: uaccess: add UserSliceWriter::clear()
This commit adds a clear() method to UserSliceWriter to zero out the remaining buffer in userspace, preventing uninitialized memory consumption. It also introduces a rust_helper_clear_user() wrapper for the architecturally defined clear_user() C function so it can be called from Rust.
Quoted 4 lines
rust/kernel/uaccess.rs +0
-0
[ ... ]
rust/kernel/uaccess.rs pub fn write<T: AsBytes>(&mut self, value: &T) -> Result { · line 645
Quoted 9 lines
rust/kernel/uaccess.rs +5
-0@@ -638,6 +638,29 @@ pub fn write<T: AsBytes>(&mut self, value: &T) -> Result {pub fn write_truncated <T :AsBytes >(&mut self, value: &T ) ->Result <usize > { self.write_slice_partial (value.as_bytes (),0 ) } + +/// Zeros remaining bytes in the userspace buffer. +/// +/// Fails with [`EFAULT`] if the userspace memory cannot be cleared. +pub fn clear (&mut self) ->Result {
[Severity: Low]
This isn't a bug, but should this method be marked with the #[inline] attribute? Small wrapper functions in abstractions that primarily forward to C bindings (like bindings::clear_user in this case) typically include the #[inline] annotation as recommended by the Rust subsystem coding guidelines.
Quoted 8 lines
rust/kernel/uaccess.rs +8
-0+let len = self.length; + +if len ==0 { +return Ok (()); + } +// SAFETY: `clear_user` checks the userspace address and range and handles +// faults when accessing userspace memory +let res =unsafe { bindings::clear_user (self.ptr.as_mut_ptr (), len) };
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1