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]
Date: Mon, 15 Apr 2024 09:36:56 +0000
From: Benno Lossin <benno.lossin@...ton.me>
To: Alice Ryhl <aliceryhl@...gle.com>, Miguel Ojeda <ojeda@...nel.org>, Matthew Wilcox <willy@...radead.org>, Al Viro <viro@...iv.linux.org.uk>, Andrew Morton <akpm@...ux-foundation.org>, Kees Cook <keescook@...omium.org>
Cc: Alex Gaynor <alex.gaynor@...il.com>, Wedson Almeida Filho <wedsonaf@...il.com>, Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>, Björn Roy Baron <bjorn3_gh@...tonmail.com>, Andreas Hindborg <a.hindborg@...sung.com>, Greg Kroah-Hartman <gregkh@...uxfoundation.org>, Arve Hjønnevåg <arve@...roid.com>, Todd Kjos <tkjos@...roid.com>, Martijn Coenen <maco@...roid.com>, Joel Fernandes <joel@...lfernandes.org>, Carlos Llamas <cmllamas@...gle.com>, Suren Baghdasaryan <surenb@...gle.com>, Arnd Bergmann <arnd@...db.de>, linux-mm@...ck.org, linux-kernel@...r.kernel.org, rust-for-linux@...r.kernel.org, Christian Brauner <brauner@...nel.org>
Subject: Re: [PATCH v5 1/4] rust: uaccess: add userspace pointers

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(())
> +    }


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ