Skip to content
This repository was archived by the owner on Mar 7, 2021. It is now read-only.

Commit 459e63f

Browse files
authored
Make UserSlicePtr::new unsafe (#95)
* Make UserSlicePtr::new unsafe * womp, make this pub(crate) * fmt * Remove unneeded unsafe
1 parent b7b3ebf commit 459e63f

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/user_ptr.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,19 @@ impl UserSlicePtr {
4444
/// the actual pages are mapped in the current process with
4545
/// appropriate permissions. Those checks are handled in the read
4646
/// and write methods.
47-
pub fn new(ptr: *mut c_types::c_void, length: usize) -> error::KernelResult<UserSlicePtr> {
47+
///
48+
/// This is `unsafe` because if it is called within `set_fs(KERNEL_DS)` context then
49+
/// `access_ok` will not do anything. As a result the only place you can safely use this is
50+
/// with an `__user` pointer that was provided by the kernel.
51+
pub(crate) unsafe fn new(
52+
ptr: *mut c_types::c_void,
53+
length: usize,
54+
) -> error::KernelResult<UserSlicePtr> {
4855
// No current access_ok implementation actually distinguishes
4956
// between VERIFY_READ and VERIFY_WRITE, so passing VERIFY_WRITE
5057
// is fine in practice and fails safe if a future implementation
5158
// bothers.
52-
if unsafe { access_ok_helper(bindings::VERIFY_WRITE, ptr, length as c_types::c_ulong) } == 0
53-
{
59+
if access_ok_helper(bindings::VERIFY_WRITE, ptr, length as c_types::c_ulong) == 0 {
5460
return Err(error::Error::EFAULT);
5561
}
5662
return Ok(UserSlicePtr(ptr, length));

0 commit comments

Comments
 (0)