On 15.04.24 11:44, Alice Ryhl wrote: > On Mon, Apr 15, 2024 at 11:37 AM Benno Lossin <benno.lossin@xxxxxxxxx> wrote: >> >> 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) > > This will fail with an EFAULT error. There is a check on the C side > that verifies that the address is in userspace. (The access_ok call.) I see, that makes a lot of sense. Regardless of whether you fix the nit about the guarantees section: Reviewed-by: Benno Lossin <benno.lossin@xxxxxxxxx> -- Cheers, Benno