`UserSliceReader::read_raw()` takes the destination as a mutable slice. Thus, it cannot be used to copy into memory that is subject to concurrent accesses, such as a page that is mapped to user space, since a mutable reference to such memory must not exist. Change `UserSliceReader::read_raw()` to take the destination as a raw pointer and a length, and make it unsafe. The copy is done by `copy_from_user()`, which uses byte-wise atomic store instructions for the kernel memory, so the destination only has to be valid for atomic writes. Update the callers. `UserSliceReader::read_slice()` remains as the safe method for copying into a slice. `Page::copy_from_user_slice_raw()` no longer creates a mutable slice of the data of the page, and thus no longer writes to the page with plain accesses. Remove the remark about this from its safety requirements. Signed-off-by: Andreas Hindborg --- rust/kernel/page.rs | 9 ++++----- rust/kernel/uaccess.rs | 47 ++++++++++++++++++++++++++++++++--------------- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs index ff191bfcd3f9..643af7cfbef4 100644 --- a/rust/kernel/page.rs +++ b/rust/kernel/page.rs @@ -600,8 +600,7 @@ pub unsafe fn fill_zero_raw(&self, offset: usize, len: usize) -> Result { /// # Safety /// /// * Callers must ensure that this call does not race with a read or write to the same page - /// that overlaps with this write. This includes byte-wise atomic accesses, as this method - /// writes to the page with plain accesses. + /// that overlaps with this write. /// * Callers must ensure that no reference to the data of the page that overlaps with this /// write exists during this call. Such references can be obtained from a /// [`Page`], see [`Page::as_slice`]. @@ -613,9 +612,9 @@ pub unsafe fn copy_from_user_slice_raw( ) -> Result { self.with_pointer_into_page(offset, len, move |dst| { // SAFETY: If `with_pointer_into_page` calls into this closure, then it has performed a - // bounds check and guarantees that `dst` is valid for `len` bytes. Furthermore, we have - // exclusive access to the slice since the caller guarantees that there are no races. - reader.read_raw(unsafe { core::slice::from_raw_parts_mut(dst.cast(), len) }) + // bounds check and guarantees that `dst` is valid for `len` bytes. The caller + // guarantees that there are no races, so `dst` is valid for atomic writes. + unsafe { reader.read_raw(dst, len) } }) } } diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs index f1aecda7a90c..b1df79500b98 100644 --- a/rust/kernel/uaccess.rs +++ b/rust/kernel/uaccess.rs @@ -252,25 +252,38 @@ pub fn is_empty(&self) -> bool { self.length == 0 } - /// Reads raw data from the user slice into a kernel buffer. + /// Reads raw data from the user slice into the kernel memory at `out`. /// - /// For a version that uses `&mut [u8]`, please see [`UserSliceReader::read_slice`]. + /// The writes to `out` are byte-wise atomic, so they are well-defined even if the memory at + /// `out` is concurrently accessed by hardware, by user space, or by other kernel code using + /// atomic or volatile accesses. For a safe version that uses `&mut [u8]`, please see + /// [`UserSliceReader::read_slice`]. /// /// Fails with [`EFAULT`] if the read happens on a bad address, or if the read goes out of - /// bounds of this [`UserSliceReader`]. This call may modify `out` even if it returns an error. + /// bounds of this [`UserSliceReader`]. This call may modify the memory at `out` even if it + /// returns an error. + /// + /// Implementation note: this is implemented by the kernel's `copy_from_user()`, which is + /// implemented in a way such that byte-wise atomic memory store instructions are used. /// /// # Guarantees /// - /// After a successful call to this method, all bytes in `out` are initialized. - pub fn read_raw(&mut self, out: &mut [MaybeUninit]) -> Result { - let len = out.len(); - let out_ptr = out.as_mut_ptr().cast::(); + /// This method only writes initialized bytes to `out`. After a successful call to this method, + /// all `len` bytes at `out` are initialized. + /// + /// # Safety + /// + /// Callers must ensure that `out` is valid for atomic writes for `len` bytes for the duration + /// of the call. + pub unsafe fn read_raw(&mut self, out: *mut u8, len: usize) -> Result { if len > self.length { return Err(EFAULT); } - // SAFETY: `out_ptr` points into a mutable slice of length `len`, so we may write - // that many bytes to it. - let res = unsafe { bindings::copy_from_user(out_ptr, self.ptr.as_const_ptr(), len) }; + // SAFETY: By the safety requirements of this function, `out` is valid for atomic writes + // for `len` bytes. `copy_from_user()` is a byte-wise atomic operation on the kernel + // memory. + let res = + unsafe { bindings::copy_from_user(out.cast::(), self.ptr.as_const_ptr(), len) }; if res != 0 { return Err(EFAULT); } @@ -284,10 +297,10 @@ pub fn read_raw(&mut self, out: &mut [MaybeUninit]) -> Result { /// Fails with [`EFAULT`] if the read happens on a bad address, or if the read goes out of /// bounds of this [`UserSliceReader`]. This call may modify `out` even if it returns an error. pub fn read_slice(&mut self, out: &mut [u8]) -> Result { - // SAFETY: The types are compatible and `read_raw` doesn't write uninitialized bytes to - // `out`. - let out = unsafe { &mut *(core::ptr::from_mut(out) as *mut [MaybeUninit]) }; - self.read_raw(out) + // SAFETY: `out` is a mutable slice, so it is valid for writing `out.len()` bytes, and + // nothing else accesses it during this call. `read_raw` doesn't write uninitialized bytes + // to `out`. + unsafe { self.read_raw(out.as_mut_ptr(), out.len()) } } /// Reads raw data from the user slice into a kernel buffer partially. @@ -372,7 +385,11 @@ pub fn read_all(mut self, buf: &mut Vec, flags: Flags) -> R buf.reserve(len, flags)?; // The call to `reserve` was successful, so the spare capacity is at least `len` bytes long. - self.read_raw(&mut buf.spare_capacity_mut()[..len])?; + let spare = &mut buf.spare_capacity_mut()[..len]; + + // SAFETY: `spare` is a mutable slice, so it is valid for writing `spare.len()` bytes, and + // nothing else accesses it during this call. + unsafe { self.read_raw(spare.as_mut_ptr().cast(), spare.len()) }?; // SAFETY: Since the call to `read_raw` was successful, so the next `len` bytes of the // vector have been initialized. -- 2.54.0