On Sat, Feb 10, 2024 at 3:15 PM David Laight <David.Laight@xxxxxxxxxx> 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@xxxxxxxxxx/