Rust in the Linux kernel

Posted by Wedson Almeida Filho, Android Team In our previous post, we announced that Android now supports the Rust programming language for developing the OS itself. Related to this, we are also participating in the effort to evaluate the use of Rust as a supported language for developing the Linux kernel. In this post, we discuss some technical aspects of this work using a few simple examples. C has been the language of choice for writing kernels for almost half a century because it offers the level of control and predictable performance required by such a critical component. Density of memory safety bugs in the Linux kernel is generally quite low due to high code quality, high standards of code review, and carefully implemented safeguards. However, memory safety bugs do still regularly occur. On Android, vulnerabilities in the kernel are generally considered high-severity because they can result in a security model bypass due to the privileged mode that the kernel runs in. We feel that Rust is now ready to join C as a practical language for implementing the kernel. It can help us reduce the number of potential bugs and security vulnerabilities in privileged code while playing nicely with the core kernel and preserving its performance characteristics. Supporting Rust We developed an initial prototype of the Binder driver to allow us to make meaningful comparisons between the safety and performance characteristics of the existing C version and its Rust counterpart. The Linux kernel has over 30 million lines of code, so naturally our goal is not to convert it all to Rust but rather to allow new code to be written in Rust. We believe this incremental approach allows us to benefit from the kernel’s existing high-performance implementation while providing kernel developers with new tools to improve memory safety and maintain performance going forward. We joined the Rust for Linux organization, where the community had already done and continues to do great work toward adding Rust support to the Linux kernel build system. We also need designs that allow code in the two languages to interact with each other: we’re particularly interested in safe, zero-cost abstractions that allow Rust code to use kernel functionality written in C, and how to implement functionality in idiomatic Rust that can be called seamlessly from the C portions of the kernel. Since Rust is a new language for the kernel, we also have the opportunity to enforce best practices in terms of documentation and uniformity. For example, we have specific machine-checked requirements around the usage of unsafe code: for every unsafe function, the developer must document the requirements that need to be satisfied by callers to ensure that its usage is safe; additionally, for every call to unsafe functions (or usage of unsafe constructs like dereferencing a raw pointer), the developer must document the justification for why it is safe to do so. Just as important as safety, Rust support needs to be convenient and helpful for developers to use. Let’s get into a few examples of how Rust can assist kernel developers in writing drivers that are safe and correct. Example driver We’ll use an implementation of a semaphore character device. Each device has a current value; writes of n bytes result in the device value being incremented by n; reads decrement the value by 1 unless the value is 0, in which case they will block until they can decrement the count without going below 0. Suppose semaphore is a file representing our device. We can interact with it from the shell as follows: > cat semaphore When semaphore is a newly initialized device, the command above will block because the device’s current value is 0. It will be unblocked if we run the following command from another shell because it increments the value by 1, which allows the original read to complete: > echo -n a > semaphore We could also increment the count by more than 1 if we write more data, for example: > echo -n abc > semaphore increments the count by 3, so the next 3 reads won’t block. To allow us to show a few more aspects of Rust, we’ll add the following features to our driver: remember what the maximum value was throughout the lifetime of a device, and remember how many reads each file issued on the device. We’ll now show how such a driver would be implemented in Rust, contrasting it with a C implementation. We note, however, we are still early on so this is all subject to change in the future. How Rust can assist the developer is the aspect that we’d like to emphasize. For example, at compile time it allows us to eliminate or greatly reduce the chances of introducing classes of bugs, while at the same time remaining flexible and having minimal overhead. Character devices A developer needs to do the following to implement a driver for a new character device in Rust: Implement the FileOperations trait: all associated functions are optional, so the developer only needs to implement the relevant ones for their scenario. They relate to the fields in C’s struct file_operations. Implement the FileOpener trait: it is a type-safe equivalent to C’s open field of struct file_operations. Register the new device type with the kernel: this lets the kernel know what functions need to be called in response to files of this new type being operated on.The following outlines how the first two steps of our example compare in Rust and C: impl FileOpener<Arc<Semaphore>> for FileState {fn open( shared: &Arc<Semaphore> ) -> KernelResult<Box<Self>> { […] }}impl FileOperations for FileState {type Wrapper = Box<Self>;fn read( &self, _: &File, data: &mut UserSlicePtrWriter, offset: u64 ) -> KernelResult<usize> { […] }fn write( &self, data: &mut UserSlicePtrReader, _offset: u64 ) -> KernelResult<usize> { […] }fn ioctl( &self, file: &File, cmd: &mut IoctlCommand ) -> KernelResult<i32> { […] }fn release(_obj: Box<Self>, _file: &File) { […] } declare_file_operations!(read, write, ioctl);} static int semaphore_open(struct inode *nodp,struct file *filp){struct semaphore_state *shared = container_of(filp->private_data,struct semaphore_state, miscdev); […]}staticssize_t semaphore_write(struct file *filp,const char __user *buffer,size_t count, loff_t *ppos){struct file_state *state = filp->private_data; […]}staticssize_t semaphore_read(struct file *filp,char __user *buffer,size_t count, loff_t *ppos){struct file_state *state = filp->private_data; […]}staticlong semaphore_ioctl(struct file *filp,unsigned int cmd,unsigned long arg){struct file_state *state = filp->private_data; […]}staticint semaphore_release(struct inode *nodp,struct file *filp){struct file_state *state = filp->private_data; […]}static const struct file_operations semaphore_fops = { .owner = THIS_MODULE, .open = semaphore_open, .read = semaphore_read, .write = semaphore_write, .compat_ioctl = semaphore_ioctl, .release = semaphore_release,}; Character devices in Rust benefit from a number of safety features: Per-file state lifetime management: FileOpener::open returns an object whose lifetime is owned by the caller from then on. Any object that implements the PointerWrapper trait can be returned, and we provide implementations for Box<T> and Arc<T>, so developers that use Rust’s idiomatic heap-allocated or reference-counted pointers have no additional requirements. All associated functions in FileOperations receive non-mutable references to self (more about this below), except the release function, which is the last function to be called and receives the plain object back (and its ownership with it). The release implementation can then defer the object destruction by transferring its ownership elsewhere, or destroy it then; in the case of a reference-counted object, ‘destruction’ means decrementing the reference count (and actual object destruction if the count goes to zero). That is, we use Rust’s ownership discipline when interacting with C code by handing the C portion ownership of a Rust object, allowing it to call functions implemented in Rust, then eventually giving ownership back. So as long as the C code is correct, the lifetime of Rust file objects work seamlessly as well, with the compiler enforcing correct lifetime management on the Rust side, for example: open cannot return stack-allocated pointers or heap-allocated objects containing pointers to the stack, ioctl/read/write cannot free (or modify without synchronization) the contents of the object stored in filp->private_data, etc. Non-mutable references: the associated functions called between open and release all receive non-mutable references to self because they can be called concurrently by multiple threads and Rust aliasing rules prohibit more than one mutable reference to an object at any given time. If a developer needs to modify some state (and they generally do), they can do so via interior mutability: mutable state can be wrapped in a Mutex<T> or SpinLock<T> (or atomics) and safely modified through them. This prevents, at compile-time, bugs where a developer fails to acquire the appropriate lock when accessing a field (the field is inaccessible), or when a developer fails to wrap a field with a lock (the field is read-only). Per-device state: when file instances need to share per-device state, which is a very common occurrence in drivers, they can do so safely in Rust. When a device is registered, a typed object can be provided and a non-mutable reference to it is provided when FileOperation::open is called. In our example, the shared object is wrapped in Arc<T>, so files can safely clone and hold on to a reference to them. The reason FileOperation is its own trait (as opposed to, for example, open being part of the FileOperations trait) is to allow a single file implementation to be registered in different ways. This eliminates opportunities for developers to get the wrong data when trying to retrieve shared state. For example, in C when a miscdevice is registered, a pointer to it is available in filp->private_data; when a cdev is registered, a pointer to it is available in inode->i_cdev. These structs are usually embedded in an outer struct that contains the shared state, so developers usually use the container_of macro to recover the shared state. Rust encapsulates all of this and the potentially troublesome pointer casts in a safe abstraction. Static typing: we take advantage of Rust’s support for generics to implement all of the above functions and types with static types. So there are no opportunities for a developer to convert an untyped variable or field to the wrong type. The C code in the table above has casts from an essentially untyped (void *) pointer to the desired type at the start of each function: this is likely to work fine when first written, but may lead to bugs as the code evolves and assumptions change. Rust would catch any such mistakes at compile time. File operations: as we mentioned before, a developer needs to implement the FileOperations trait to customize the behavior of their device. They do this with a block starting with impl FileOperations for Device, where Device is the type implementing the file behavior (FileState in our example). Once inside this block, tools know that only a limited number of functions can be defined, so they can automatically insert the prototypes. (Personally, I use neovim and the rust-analyzer LSP server.) While we use this trait in Rust, the C portion of the kernel still requires an instance of struct file_operations. The kernel crate automatically generates one from the trait implementation (and optionally the declare_file_operations macro): although it has code to generate the correct struct, it is all const, so evaluated at compile-time with zero runtime cost. Ioctl handling For a driver to provide a custom ioctl handler, it needs to implement the ioctl function that is part of the FileOperations trait, as exemplified in the table below. fn ioctl( &self, file: &File, cmd: &mut IoctlCommand) -> KernelResult<i32> { cmd.dispatch(self, file)}impl IoctlHandler for FileState {fn read( &self, _file: &File, cmd: u32, writer: &mut UserSlicePtrWriter ) -> KernelResult<i32> {match cmd { IOCTL_GET_READ_COUNT => { writer.write( &self .read_count .load(Ordering::Relaxed))?;Ok(0) } _ => Err(Error::EINVAL), } }fn write( &self, _file: &File, cmd: u32, reader: &mut UserSlicePtrReader ) -> KernelResult<i32> {match cmd { IOCTL_SET_READ_COUNT => {self .read_count .store(reader.read()?, Ordering::Relaxed);Ok(0) } _ => Err(Error::EINVAL), } }} #define IOCTL_GET_READ_COUNT _IOR(‘c’, 1, u64)#define IOCTL_SET_READ_COUNT _IOW(‘c’, 1, u64)staticlong semaphore_ioctl(struct file *filp,unsigned int cmd,unsigned long arg){struct file_state *state = filp->private_data;void __user *buffer = (void __user *)arg; u64 value;switch (cmd) {case IOCTL_GET_READ_COUNT: value = atomic64_read(&state->read_count);if (copy_to_user(buffer, &value, sizeof(value)))return -EFAULT;return 0;case IOCTL_SET_READ_COUNT:if (copy_from_user(&value, buffer, sizeof(value)))return -EFAULT; atomic64_set(&state->read_count, value);return 0;default:return -EINVAL; }} Ioctl commands are standardized such that, given a command, we know whether a user buffer is provided, its intended use (read, write, both, none), and its size. In Rust, we provide a dispatcher (accessible by calling cmd.dispatch) that uses this information to automatically create user memory access helpers and pass them to the caller. A driver is not required to use this though. If, for example, it doesn’t use the standard ioctl encoding, Rust offers the flexibility of simply calling cmd.raw to extract the raw arguments and using them to handle the ioctl (potentially with unsafe code, which will need to be justified). However, if a driver implementation does use the standard dispatcher, it will benefit from not having to implement any unsafe code, and: The pointer to user memory is never a native pointer, so the developer cannot accidentally dereference it. The types that allow the driver to read from user space only allow data to be read once, so we eliminate the risk of time-of-check to time-of-use (TOCTOU) bugs because when a driver needs to access data twice, it needs to copy it to kernel memory, where an attacker is not allowed to modify it. Excluding unsafe blocks, there is no way to introduce this class of bugs in Rust. No accidental overflow of the user buffer: we’ll never read or write past the end of the user buffer because this is enforced automatically based on the size encoded in the ioctl command. In our example above, the implementation of IOCTL_GET_READ_COUNT only has access to an instance of UserSlicePtrWriter, which limits the number of writable bytes to sizeof(u64) as encoded in the ioctl command. No mixing of reads and writes: we’ll never write buffers for ioctls that are only meant to read and never read buffers for ioctls that are only meant to write. This is enforced by read and write handlers only getting instances of UserSlicePtrWriter and UserSlicePtrReader respectively. All of the above could potentially also be done in C, but it’s very easy for developers to (likely unintentionally) break contracts that lead to unsafety; Rust requires unsafe blocks for this, which should only be used in rare cases and brings additional scrutiny. Additionally, Rust offers the following: The types used to read and write user memory do not implement the Send and Sync traits, which means that they (and pointers to them) are not safe to be used in another thread context. In Rust, if a driver developer attempted to write code that passed one of these objects to another thread (where it wouldn’t be safe to use them because it isn’t necessarily in the right memory manager context), they would get a compilation error. When calling IoctlCommand::dispatch, one might understandably think that we need dynamic dispatching to reach the actual handler implementation (which would incur additional cost in comparison to C), but we don’t. Our usage of generics will lead the compiler to monomorphize the function, which will result in static function calls that can even be inlined if the optimizer so chooses. Locking and condition variables We allow developers to use mutexes and spinlocks to provide interior mutability. In our example, we use a mutex to protect mutable data; in the tables below we show the data structures we use in C and Rust, and how we implement a wait until the count is nonzero so that we can satisfy a read: struct SemaphoreInner { count: usize, max_seen: usize,}struct Semaphore { changed: CondVar, inner: Mutex<SemaphoreInner>,}struct FileState { read_count: AtomicU64, shared: Arc<Semaphore>,}struct semaphore_state {struct kref ref;struct miscdevice miscdev;wait_queue_head_t changed;struct mutex mutex;size_t count;size_t max_seen;};struct file_state {atomic64_t read_count;struct semaphore_state *shared;}; fn consume(&self) -> KernelResult {let mut inner = self.shared.inner.lock();while inner.count == 0 {if self.shared.changed.wait(&mut inner) {return Err(Error::EINTR); } } inner.count -= 1;Ok(())}static int semaphore_consume(struct semaphore_state *state){ DEFINE_WAIT(wait); mutex_lock(&state->mutex);while (state->count == 0) { prepare_to_wait(&state->changed, &wait, TASK_INTERRUPTIBLE); mutex_unlock(&state->mutex); schedule(); finish_wait(&state->changed, &wait);if (signal_pending(current))return -EINTR; mutex_lock(&state->mutex); } state->count–; mutex_unlock(&state->mutex);return 0;} We note that such waits are not uncommon in the existing C code, for example, a pipe waiting for a “partner” to write, a unix-domain socket waiting for data, an inode search waiting for completion of a delete, or a user-mode helper waiting for state change. The following are benefits from the Rust implementation: The Semaphore::inner field is only accessible when the lock is held, through the guard returned by the lock function. So developers cannot accidentally read or write protected data without locking it first. In the C example above, count and max_seen in semaphore_state are protected by mutex, but there is no enforcement that the lock is held while they’re accessed. Resource Acquisition Is Initialization (RAII): the lock is unlocked automatically when the guard (inner in this case) goes out of scope. This ensures that locks are always unlocked: if the developer needs to keep a lock locked, they can keep the guard alive, for example, by returning the guard itself; conversely, if they need to unlock before the end of the scope, they can explicitly do it by calling the drop function. Developers can use any lock that implements the Lock trait, which includes Mutex and SpinLock, at no additional runtime cost when compared to a C implementation. Other synchronization constructs, including condition variables, also work transparently and with zero additional run-time cost. Rust implements condition variables using kernel wait queues. This allows developers to benefit from atomic release of the lock and putting the thread to sleep without having to reason about low-level kernel scheduler functions. In the C example above, semaphore_consume is a mix of semaphore logic and subtle Linux scheduling: for example, the code is incorrect if mutex_unlock is called before prepare_to_wait because it may result in a wake up being missed. No unsynchronized access: as we mentioned before, variables shared by multiple threads/CPUs must be read-only, with interior mutability being the solution for cases when mutability is needed. In addition to the example with locks above, the ioctl example in the previous section also has an example of using an atomic variable; Rust also requires developers to specify how memory is to be synchronized by atomic accesses. In the C part of the example, we happen to use atomic64_t, but the compiler won’t alert a developer to this need. Error handling and control flow In the tables below, we show how open, read, and write are implemented in our example driver: fn read( &self, _: &File, data: &mut UserSlicePtrWriter, offset: u64) -> KernelResult<usize> {if data.is_empty() || offset > 0 {return Ok(0); }self.consume()?; data.write_slice(&[0u8; 1])?;self.read_count.fetch_add(1, Ordering::Relaxed);Ok(1)} staticssize_t semaphore_read(struct file *filp,char __user *buffer,size_t count, loff_t *ppos){struct file_state *state = filp->private_data;char c = 0;int ret;if (count == 0 || *ppos > 0)return 0; ret = semaphore_consume(state->shared);if (ret)return ret;if (copy_to_user(buffer, &c, sizeof(c)))return -EFAULT; atomic64_add(1, &state->read_count); *ppos += 1;return 1;} fn write( &self, data: &mut UserSlicePtrReader, _offset: u64) -> KernelResult<usize> { {let mut inner = self.shared.inner.lock(); inner.count = inner.count.saturating_add(data.len());if inner.count > inner.max_seen { inner.max_seen = inner.count; } }self.shared.changed.notify_all();Ok(data.len())}staticssize_t semaphore_write(struct file *filp,const char __user *buffer,size_t count, loff_t *ppos){struct file_state *state = filp->private_data;struct semaphore_state *shared = state->shared; mutex_lock(&shared->mutex); shared->count += count;if (shared->count < count) shared->count = SIZE_MAX;if (shared->count > shared->max_seen) shared->max_seen = shared->count; mutex_unlock(&shared->mutex); wake_up_all(&shared->changed);return count;} fn open( shared: &Arc<Semaphore>) -> KernelResult<Box<Self>> {Ok(Box::try_new(Self { read_count: AtomicU64::new(0), shared: shared.clone(), })?)}static int semaphore_open(struct inode *nodp,struct file *filp){struct semaphore_state *shared = container_of(filp->private_data,struct semaphore_state, miscdev);struct file_state *state; state = kzalloc(sizeof(*state), GFP_KERNEL);if (!state)return -ENOMEM; kref_get(&shared->ref); state->shared = shared; atomic64_set(&state->read_count, 0); filp->private_data = state;return 0;} They illustrate other benefits brought by Rust: The ? operator: it is used by the Rust open and read implementations to do error handling implicitly; the developer can focus on the semaphore logic, the resulting code being quite small and readable. The C versions have error-handling noise that can make them less readable. Required initialization: Rust requires all fields of a struct to be initialized on construction, so the developer can never accidentally fail to initialize a field; C offers no such facility. In our open example above, the developer of the C version could easily fail to call kref_get (even though all fields would have been initialized); in Rust, the user is required to call clone (which increments the ref count), otherwise they get a compilation error. RAII scoping: the Rust write implementation uses a statement block to control when inner goes out of scope and therefore the lock is released. Integer overflow behavior: Rust encourages developers to always consider how overflows should be handled. In our write example, we want a saturating one so that we don’t end up with a zero value when adding to our semaphore. In C, we need to manually check for overflows, there is no additional support from the compiler. What’s next The examples above are only a small part of the whole project. We hope it gives readers a glimpse of the kinds of benefits that Rust brings. At the moment we have nearly all generic kernel functionality needed by Binder neatly wrapped in safe Rust abstractions, so we are in the process of gathering feedback from the broader Linux kernel community with the intent of upstreaming the existing Rust support. We also continue to make progress on our Binder prototype, implement additional abstractions, and smooth out some rough edges. This is an exciting time and a rare opportunity to potentially influence how the Linux kernel is developed, as well as inform the evolution of the Rust language. We invite those interested to join us in Rust for Linux and attend our planned talk at Linux Plumbers Conference 2021! Thanks Nick Desaulniers, Kees Cook, and Adrian Taylor for contributions to this post. Special thanks to Jeff Vander Stoep for contributions and editing, and to Greg Kroah-Hartman for reviewing and contributing to the code examples.

—————
Free Unlimited, Encrypted, Anti Snoop & Ad Free Email
Boost Aviation Internet Speeds – Cuts tracking and Junk at source
Register UK names for just £2.99 a year
Check our Premium Domains and Freebies