From: Alice Ryhl <aliceryhl@google.com>
To: Boqun Feng <boqun.feng@gmail.com>
Cc: "Al Viro" <viro@zeniv.linux.org.uk>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Arnd Bergmann" <arnd@arndb.de>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] uaccess: rust: use newtype for user pointers
Date: Wed, 28 May 2025 10:48:01 +0000 [thread overview]
Message-ID: <aDbp4cM3Dmv84bm8@google.com> (raw)
In-Reply-To: <68364701.050a0220.48858.0017@mx.google.com>
On Tue, May 27, 2025 at 04:13:03PM -0700, Boqun Feng wrote:
> On Tue, May 27, 2025 at 11:12:11PM +0100, Al Viro wrote:
> > On Tue, May 27, 2025 at 01:53:12PM +0000, Alice Ryhl wrote:
> > > In C code we use sparse with the __user annotation to detect cases where
> > > a user pointer is mixed up with other things. To replicate that, we
> > > introduce a new struct UserPtr that serves the same purpose using the
> > > newtype pattern.
> > >
> > > The UserPtr type is not marked with #[derive(Debug)], which means that
> > > it's not possible to print values of this type. This avoids ASLR
> > > leakage.
> > >
> > > The type is added to the prelude as it is a fairly fundamental type
> > > similar to c_int. The wrapping_add() method is renamed to
> > > wrapping_byte_add() for consistency with the method name found on raw
> > > pointers.
> >
> > That's considerably weaker than __user, though - with
> > struct foo {struct bar x; struct baz y[2]; };
>
> Translate to Rust this is:
>
> struct Foo {
> x: Bar,
> y: Baz[2],
> }
>
> > struct foo __user *p;
>
> UserPtr should probably be generic over pointee, so:
>
> pub struct UserPtr<T>(*mut c_void, PhantomData<*mut T>);
>
> and
>
> let p: UserPtr<Foo> = ...;
>
> > void f(struct bar __user *);
>
> and this is:
>
> pub fn f(bar: UserPtr<Bar>)
>
> and the checking should work, a (maybe unrelated) tricky part though..
>
> > sparse does figure out that f(&p->y[1]) is a type error - &p->y[1] is
>
> In Rust, you will need to play a little unsafe game to get &p->y[1]:
>
> let foo_ptr: *mut Foo = p.as_mut_ptr();
> let y_ptr: *mut Baz = unsafe { addr_of_mut!((*foo_ptr).y[1]) };
> let y: UserPtr<Baz> = unsafe { UserPtr::from_ptr(y_ptr) };
>
> passing y to f() will get a type mismatch, so the detection/checking
> works. To avoid the unsafe game we need field projection [1].
That looks pretty unergonomic. We can do better than that.
Alice
> > struct baz __user * and f() expects struct bar __user *.
> >
> > It's not just mixing userland pointers with other things - it's not mixing
> > userland pointers to different types, etc.
> >
>
> In short, with UserPtr generic over pointee, we can have the similar
> detection as sparse.
>
> [1]: https://github.com/rust-lang/rfcs/pull/3735
>
> Regards,
> Boqun
>
> > In practice I've seen quite a few brainos caught by that...
next prev parent reply other threads:[~2025-05-28 10:48 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-27 13:53 [PATCH v2] uaccess: rust: use newtype for user pointers Alice Ryhl
2025-05-27 14:42 ` Greg Kroah-Hartman
2025-05-27 15:20 ` Boqun Feng
2025-05-27 15:22 ` Alice Ryhl
2025-05-27 19:03 ` Christian Schrefl
2025-05-27 19:09 ` Alice Ryhl
2025-05-27 22:12 ` Al Viro
2025-05-27 23:13 ` Boqun Feng
2025-05-28 10:48 ` Alice Ryhl [this message]
2025-05-28 15:38 ` Benno Lossin
2025-05-28 16:02 ` Boqun Feng
2025-05-28 10:47 ` Alice Ryhl
2025-05-28 17:45 ` Al Viro
2025-05-28 20:01 ` Al Viro
2025-05-28 20:35 ` Benno Lossin
2025-05-28 10:52 ` Danilo Krummrich
2025-05-28 16:55 ` Benno Lossin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aDbp4cM3Dmv84bm8@google.com \
--to=aliceryhl@google.com \
--cc=a.hindborg@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=arnd@arndb.de \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.