linux/rust/kernel
Wedson Almeida Filho 1b580e7b9b rust: uaccess: add userspace pointers
A pointer to an area in userspace memory, which can be either read-only
or read-write.

All methods on this struct are safe: attempting to read or write on bad
addresses (either out of the bound of the slice or unmapped addresses)
will return `EFAULT`. Concurrent access, *including data races to/from
userspace memory*, is permitted, because fundamentally another userspace
thread/process could always be modifying memory at the same time (in the
same way that userspace Rust's `std::io` permits data races with the
contents of files on disk). In the presence of a race, the exact byte
values read/written are unspecified but the operation is well-defined.
Kernelspace code should validate its copy of data after completing a
read, and not expect that multiple reads of the same address will return
the same value.

These APIs are designed to make it difficult to accidentally write
TOCTOU bugs. Every time you read from a memory location, the pointer is
advanced by the length so that you cannot use that reader to read the
same memory location twice. Preventing double-fetches avoids TOCTOU
bugs. This is accomplished by taking `self` by value to prevent
obtaining multiple readers on a given `UserSlice`, and the readers only
permitting forward reads. If double-fetching a memory location is
necessary for some reason, then that is done by creating multiple
readers to the same memory location.

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).

This code is based on something that was originally written by Wedson on
the old rust branch. It was modified by Alice by removing the
`IoBufferReader` and `IoBufferWriter` traits, and various other changes.

Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com>
Reviewed-by: Benno Lossin <benno.lossin@proton.me>
Reviewed-by: Trevor Gross <tmgross@umich.edu>
Reviewed-by: Boqun Feng <boqun.feng@gmail.com>
Co-developed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Link: https://lore.kernel.org/r/20240528-alice-mm-v7-1-78222c31b8f4@google.com
[ Wrapped docs to 100 and added a few intra-doc links. - Miguel ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2024-07-08 23:44:01 +02:00
..
alloc rust: avoid unused import warning in rusttest 2024-06-11 23:33:28 +02:00
init rust: init: change the generated name of guard variables 2024-05-05 18:05:00 +02:00
net Rust changes for v6.10 2024-05-13 15:13:54 -07:00
sync rust: kernel: remove redundant imports 2024-05-05 19:22:25 +02:00
alloc.rs rust: alloc: add __GFP_HIGHMEM flag 2024-07-08 22:39:39 +02:00
build_assert.rs rust: upgrade to Rust 1.68.2 2023-05-31 17:35:03 +02:00
error.rs rust: kernel: remove redundant imports 2024-05-05 19:22:25 +02:00
init.rs Rust changes for v6.10 2024-05-13 15:13:54 -07:00
ioctl.rs rust: kernel: mark code fragments in docs with backticks 2024-02-18 21:22:27 +01:00
kunit.rs rust: support srctree-relative links 2023-12-21 20:54:17 +01:00
lib.rs rust: uaccess: add userspace pointers 2024-07-08 23:44:01 +02:00
net.rs rust: core abstractions for network PHY drivers 2023-12-15 09:35:50 +00:00
prelude.rs rust: alloc: introduce the BoxExt trait 2024-04-16 22:47:20 +02:00
print.rs rust: kernel: remove redundant imports 2024-05-05 19:22:25 +02:00
static_assert.rs rust: static_assert: add static_assert! macro 2022-12-04 01:59:16 +01:00
std_vendor.rs rust: update dbg!() to format column number 2024-05-05 18:09:04 +02:00
str.rs rust: kernel: remove redundant imports 2024-05-05 19:22:25 +02:00
sync.rs rust: sync: implement Default for LockClassKey 2024-05-05 19:22:25 +02:00
task.rs rust: kernel: remove redundant imports 2024-05-05 19:22:25 +02:00
time.rs rust: time: doc: Add missing C header links 2024-05-01 00:04:47 +02:00
types.rs rust: types: Make Opaque::get const 2024-05-05 13:19:37 +02:00
uaccess.rs rust: uaccess: add userspace pointers 2024-07-08 23:44:01 +02:00
workqueue.rs rust: kernel: make impl_has_work compatible with more generics 2024-07-08 22:39:39 +02:00