[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <CAH5fLgiRgg2zD3tJ9Xrvk+bvH3srDGw9ud_gjRa97cd7a+jROA@mail.gmail.com>
Date: Mon, 12 Feb 2024 10:30:24 +0100
From: Alice Ryhl <aliceryhl@...gle.com>
To: David Laight <David.Laight@...lab.com>
Cc: Trevor Gross <tmgross@...ch.edu>, Miguel Ojeda <ojeda@...nel.org>,
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>,
Benno Lossin <benno.lossin@...ton.me>, Andreas Hindborg <a.hindborg@...sung.com>,
Kees Cook <keescook@...omium.org>, Al Viro <viro@...iv.linux.org.uk>,
Andrew Morton <akpm@...ux-foundation.org>, 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-mm@...ck.org>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>,
"rust-for-linux@...r.kernel.org" <rust-for-linux@...r.kernel.org>, Christian Brauner <brauner@...nel.org>
Subject: Re: [PATCH 1/3] rust: add userspace pointers
On Sat, Feb 10, 2024 at 3:15 PM David Laight <David.Laight@...lab.com> wrote:
>
> ...
> > > Maybe something like
> > >
> > > Every time a memory location is read, the reader's position is advanced by
> > > the read length and the next read will start from there. This helps prevent
> > > accidentally reading the same location twice and causing a TOCTOU bug.
>
> WTF TOCTOU? I'm guessing it is reading things twice and getting
> different answers.
Yes. In v2 of this patchset [1], I expanded TOCTOU to "time-of-check
to time-of-use" at the first use to reduce this confusion.
> That really doesn't match how copying from userspace is used is many places.
> Sometimes you really do want to be using offsets and lengths.
> For instance the user buffer might contain offsets of items further
> down the buffer.
For this use-case, you can call UserSlice::new multiple times, or use
clone_reader. This use-case does appear sometimes in Rust Binder and
is supported, but I didn't find it to be the most common use-case.
> There is also the code (eg ioctl) that does a read-modify-write
> on a buffer.
The read-modify-write use-case is quite common in Rust Binder and is
supported by the API provided by this patchset. When you call
reader_writer, you get a separate reader and writer. Then, you first
use the reader to read the data. Then you modify it. Then you use the
writer to write it back.
> > > + /// Reads the entirety of the user slice.
> > > + ///
> > > + /// Returns `EFAULT` if the address does not currently point to
> > > + /// mapped, readable memory.
> > > + pub fn read_all(self) -> Result<Vec<u8>> {
> > > + self.reader().read_all()
> > > + }
> >
> > If I understand it correctly, the function will return `EFAULT` if _any_
> > address in the interval `[self.0, self.0 + self.1)` does not point to
> > mapped, readable memory. Maybe the docs could be more explicit.
>
> That isn't (and can't be) how it works.
> access_ok() checks that the buffer isn't in kernel space.
> The copy is then done until it actually faults on an invalid address.
> In that case the destination buffer has been updated to the point
> of failure.
>
> You can't do a check before the copy because another thread can
> change the mapping (it would also be horribly expensive).
This was reworded in v2 [1]:
/// Fails with `EFAULT` if the read encounters a page fault.
But ultimately, the real condition here is just that it returns EFAULT
if copy_from_user fails. I'm happy to reword further.
Alice
[1]: https://lore.kernel.org/all/20240208-alice-mm-v2-1-d821250204a6@googlecom/
Powered by blists - more mailing lists