Supporting Linux kernel development in Rust
Supporting Linux kernel development in Rust
Posted Aug 31, 2020 21:19 UTC (Mon) by adobriyan (subscriber, #30858)Parent article: Supporting Linux kernel development in Rust
template<typename T>
int copy_from_user(T* k, user_ptr<T> u);
Posted Aug 31, 2020 21:32 UTC (Mon)
by adobriyan (subscriber, #30858)
[Link] (1 responses)
pub extern "C" fn init_module() -> i32 {
Posted Aug 31, 2020 22:52 UTC (Mon)
by geofft (subscriber, #59789)
[Link]
https://github.com/fishinabarrel/linux-kernel-module-rust... is safe bindings to copy_from_user / copy_to_user. See the rest of that repository for examples of its use.
Posted Aug 31, 2020 21:39 UTC (Mon)
by josh (subscriber, #17465)
[Link] (6 responses)
Posted Aug 31, 2020 21:41 UTC (Mon)
by adobriyan (subscriber, #30858)
[Link] (5 responses)
Posted Aug 31, 2020 22:19 UTC (Mon)
by nickodell (subscriber, #125165)
[Link] (4 responses)
Posted Aug 31, 2020 22:23 UTC (Mon)
by josh (subscriber, #17465)
[Link]
Posted Aug 31, 2020 22:25 UTC (Mon)
by adobriyan (subscriber, #30858)
[Link]
Posted Aug 31, 2020 22:47 UTC (Mon)
by notriddle (subscriber, #130608)
[Link]
But copy_from_user itself looks closer to ptr::copy. https://doc.rust-lang.org/stable/std/ptr/fn.copy.html
Both of these functions are unsafe, as any form of copy_from_user must be, since there's no way to be sure that the contents of userspace memory are valid for whatever data structure you're transmuting them into. You would need to ensure that the data structure in question can accept any arbitrary byte sequence, which is what "safe transmute" proposals are supposed to do.
Posted Aug 31, 2020 22:55 UTC (Mon)
by nybble41 (subscriber, #55106)
[Link]
It exists (you can cast raw pointers from one type to another and dereference them within unsafe blocks) but, like reinterpret_cast, you need to be very careful about how you use it. This is one area where it is probably easier to accidentally trigger undefined behavior in unsafe Rust code than in C, since Rust places more constraints on pointers/references than C does. At a minimum the target object would need to have a repr(C) type to ensure a consistent ABI, and the Copy trait as evidence that the content can be safely duplicated with a straightforward byte copy. The operation itself would also need to be marked as "unsafe" since there is no way that overwriting a Rust object (even one which is Copy and repr(C)) with arbitrary data from a buffer can be guaranteed to preserve whatever invariants might be expected by the object's implementation. With all that said, however, the std::ptr::read_unaligned function[1] is fairly close to a typed copy_from_user—without, obviously, the extra checking and error recovery that comes with accessing user memory from kernel mode.
[1] https://doc.rust-lang.org/std/ptr/fn.read_unaligned.html
Supporting Linux kernel development in Rust
let mut mutex_guard = MUTEX.acquire();
let parrot_ref = match mutex_guard.get_mut() {
Some(p) => p,
None => {
unsafe {
printk!("%s", to_ptr!(c_string!("Failed to get reference to global state")))
};
return -1;
}
};
match parrot_ref.init() {
Ok(_) => 0,
Err(e) => {
unsafe { printk!("%s", to_ptr!(e)) };
-1
}
}
}
Supporting Linux kernel development in Rust
Supporting Linux kernel development in Rust
Supporting Linux kernel development in Rust
Supporting Linux kernel development in Rust
Supporting Linux kernel development in Rust
Supporting Linux kernel development in Rust
Supporting Linux kernel development in Rust
Supporting Linux kernel development in Rust