Re: [PATCH v5 1/4] rust: uaccess: add userspace pointers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 15.04.24 09:13, Alice Ryhl wrote:
> +impl UserSlice {
> +    /// Constructs a user slice from a raw pointer and a length in bytes.
> +    ///
> +    /// Constructing a [`UserSlice`] performs no checks on the provided address and length, it can
> +    /// safely be constructed inside a kernel thread with no current userspace process. Reads and
> +    /// writes wrap the kernel APIs `copy_from_user` and `copy_to_user`, which check the memory map
> +    /// of the current process and enforce that the address range is within the user range (no
> +    /// additional calls to `access_ok` are needed).
> +    ///
> +    /// Callers must be careful to avoid time-of-check-time-of-use (TOCTOU) issues. The simplest way
> +    /// is to create a single instance of [`UserSlice`] per user memory block as it reads each byte
> +    /// at most once.
> +    pub fn new(ptr: *mut c_void, length: usize) -> Self {

What would happen if I call this with a kernel pointer and then
read/write to it? For example

     let mut arr = [MaybeUninit::uninit(); 64];
     let ptr: *mut [MaybeUninit<u8>] = &mut arr;
     let ptr = ptr.cast::<c_void>();

     let slice = UserSlice::new(ptr, 64);
     let (mut r, mut w) = slice.reader_writer();

     r.read_raw(&mut arr)?;
     // SAFETY: `arr` was initialized above.
     w.write_slice(unsafe { MaybeUninit::slice_assume_init_ref(&arr) })?;

I think this would violate the exclusivity of `&mut` without any
`unsafe` code. (the `unsafe` block at the end cannot possibly be wrong)

> +        UserSlice { ptr, length }
> +    }

[...]

> +    /// Returns `true` if no data is available in the io buffer.
> +    pub fn is_empty(&self) -> bool {
> +        self.length == 0
> +    }
> +
> +    /// Reads raw data from the user slice into a kernel buffer.
> +    ///
> +    /// After a successful call to this method, all bytes in `out` are initialized.

I think we should put things like this into a `# Guarantees` section.

-- 
Cheers,
Benno

> +    ///
> +    /// Fails with `EFAULT` if the read happens on a bad address.
> +    pub fn read_raw(&mut self, out: &mut [MaybeUninit<u8>]) -> Result {
> +        let len = out.len();
> +        let out_ptr = out.as_mut_ptr().cast::<c_void>();
> +        if len > self.length {
> +            return Err(EFAULT);
> +        }
> +        let Ok(len_ulong) = c_ulong::try_from(len) else {
> +            return Err(EFAULT);
> +        };
> +        // SAFETY: `out_ptr` points into a mutable slice of length `len_ulong`, so we may write
> +        // that many bytes to it.
> +        let res = unsafe { bindings::copy_from_user(out_ptr, self.ptr, len_ulong) };
> +        if res != 0 {
> +            return Err(EFAULT);
> +        }
> +        // Userspace pointers are not directly dereferencable by the kernel, so we cannot use `add`,
> +        // which has C-style rules for defined behavior.
> +        self.ptr = self.ptr.wrapping_byte_add(len);
> +        self.length -= len;
> +        Ok(())
> +    }






[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Bugtraq]     [Linux OMAP]     [Linux MIPS]     [eCos]     [Asterisk Internet PBX]     [Linux API]

  Powered by Linux