From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6FDA0CD1292 for ; Thu, 4 Apr 2024 20:40:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 03B746B009C; Thu, 4 Apr 2024 16:40:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F2DA26B009D; Thu, 4 Apr 2024 16:40:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E1CBB6B009E; Thu, 4 Apr 2024 16:40:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id BFCDD6B009C for ; Thu, 4 Apr 2024 16:40:34 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 7EDA581057 for ; Thu, 4 Apr 2024 20:40:34 +0000 (UTC) X-FDA: 81973017588.10.B787867 Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) by imf30.hostedemail.com (Postfix) with ESMTP id 3ECC580006 for ; Thu, 4 Apr 2024 20:40:31 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=proton.me header.s=protonmail header.b="f2cNO35/"; dmarc=pass (policy=quarantine) header.from=proton.me; spf=pass (imf30.hostedemail.com: domain of benno.lossin@proton.me designates 185.70.43.22 as permitted sender) smtp.mailfrom=benno.lossin@proton.me ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712263233; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/r2dqqHabl6s0yl9bFd2ZPmBVmtjU4+UW6w3beXmG14=; b=MvcuCu/n6ltX6t6sqZETW1FoEItUCsHFQyLIv8i/VbroWj0hGT4MLK7ZVwsctMbgLw78Tf AJ40sREIQMDcyIzTLVkO8VtSAKiO40K6prjQvRcj0foW2j3UFNfZdIFmPXVOAPo6qVCKFL GgR82FfG7VhxvUC+gwjjU1T7BVF9AUc= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=proton.me header.s=protonmail header.b="f2cNO35/"; dmarc=pass (policy=quarantine) header.from=proton.me; spf=pass (imf30.hostedemail.com: domain of benno.lossin@proton.me designates 185.70.43.22 as permitted sender) smtp.mailfrom=benno.lossin@proton.me ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712263233; a=rsa-sha256; cv=none; b=7OGLWgL6EmMBiNpRX7PTK8wNoGd2YKQlhTK1a/vAqeAsVSArSBtxxWcrurJuRgg1f3l/Y4 c6IiXB7HbEz/Css9+XycD/VIgj2Xz33F3nDWBif4vrzm+qnaT/+OMcbXxNNQYwFCHnv4/O S++9DT+h5x1/PtPJ/KuANmkjEAq+Mio= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1712263229; x=1712522429; bh=/r2dqqHabl6s0yl9bFd2ZPmBVmtjU4+UW6w3beXmG14=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=f2cNO35/R2nxiVhhEcnbzWuJBmvLAaPKSihWgXgw9Dx4EeXN1Tkn2Tq4Z9usRC5wr ewf4R20al1GzRJoZwnVK7wzD5w+uHnpLlJdmFe+OgmvEgCYdST1V/DeScMqB9F1N8m 8NhAu8VHcp7rEpYi7wlYDFNbQfSk/ZwzO8D4q3aCJTO1HywnZPt4icfwQ/4EROBX/f fMyHzaealHFwMNPb8Oi7iN5sCHkgVor5N2rTJ6wit6GAPgzr2lsFn17Qhe4YjVtqYy SZgPBqdlP/0dB7c9IdjVdcwCq1szysQ9mnrf7LFews4x33uDN2WTND0k4WqxJFZd5H yg/CclUuyGBxw== Date: Thu, 04 Apr 2024 20:40:20 +0000 To: Alice Ryhl , Miguel Ojeda , Matthew Wilcox , Al Viro , Andrew Morton , Kees Cook From: Benno Lossin Cc: Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Greg Kroah-Hartman , =?utf-8?Q?Arve_Hj=C3=B8nnev=C3=A5g?= , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan , Arnd Bergmann , linux-mm@kvack.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Christian Brauner Subject: Re: [PATCH v4 1/4] rust: uaccess: add userspace pointers Message-ID: In-Reply-To: <20240404-alice-mm-v4-1-49a84242cf02@google.com> References: <20240404-alice-mm-v4-0-49a84242cf02@google.com> <20240404-alice-mm-v4-1-49a84242cf02@google.com> Feedback-ID: 71780778:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 3ECC580006 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: bee75x8f3gegudjmffou7ukmx3o65y6n X-HE-Tag: 1712263231-167652 X-HE-Meta: U2FsdGVkX1+x+k4goSonBdpnO3liOEBys75hY3WUptXrprmGCtp/IiK0mzPRjAT9fomAnueRr4zWwVuumnMczgMtV5q+e7BmDEuS0p/gmKvIU+WPotiYOKfeps3oVTgeR/iY4UROG6VIrZwymYtXa35olZQR2dzI51fc+4PPRPSXWKwl9fE4p+nkmDJA75guEoMHsxLIA0/x5NYR5x/lWk/hVpf/JpjSa2rbfIXqxx6kNL9qRp5VgQve5UiE0R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ogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 04.04.24 14:31, Alice Ryhl wrote: > diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs > new file mode 100644 > index 000000000000..3f8ad4dc13c4 > --- /dev/null > +++ b/rust/kernel/uaccess.rs > @@ -0,0 +1,311 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +//! Slices to user space memory regions. > +//! > +//! C header: [`include/linux/uaccess.h`](srctree/include/linux/uaccess.= h) > + > +use crate::{bindings, error::code::*, error::Result}; > +use alloc::vec::Vec; > +use core::ffi::{c_ulong, c_void}; > +use core::mem::MaybeUninit; > + > +/// A pointer to an area in userspace memory, which can be either read-o= nly 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 addresse= s) will > +/// return `EFAULT`. Concurrent access, *including data races to/from us= erspace > +/// 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 t= he > +/// contents of files on disk). In the presence of a race, the exact byt= e values > +/// read/written are unspecified but the operation is well-defined. Kern= elspace > +/// code should validate its copy of data after completing a read, and n= ot > +/// expect that multiple reads of the same address will return the same = value. > +/// > +/// These APIs are designed to make it difficult to accidentally write T= OCTOU > +/// (time-of-check to time-of-use) bugs. Every time a memory location is= read, > +/// the reader's position is advanced by the read length and the next re= ad will > +/// start from there. This helps prevent accidentally reading the same l= ocation > +/// twice and causing a TOCTOU bug. > +/// > +/// Creating a [`UserSliceReader`] and/or [`UserSliceWriter`] consumes t= he > +/// `UserSlice`, helping ensure that there aren't multiple readers or wr= iters to > +/// the same location. > +/// > +/// If double-fetching a memory location is necessary for some reason, t= hen that > +/// is done by creating multiple readers to the same memory location, e.= g. using > +/// [`clone_reader`]. I think we should have consistent 100 column formatting. And not something less. > +/// > +/// # Examples [...] > + /// Reads raw data from the user slice into a kernel buffer. > + /// > + /// Fails with `EFAULT` if the read happens on a bad address. > + pub fn read_raw(&mut self, out: &mut [MaybeUninit]) -> Result { > + let len =3D out.len(); > + let out_ptr =3D out.as_mut_ptr().cast::(); > + if len > self.length { > + return Err(EFAULT); > + } > + let Ok(len_ulong) =3D c_ulong::try_from(len) else { > + return Err(EFAULT); > + }; > + // SAFETY: The caller promises that `out` is valid for writing `= len` bytes. This comment needs updating. > + let res =3D unsafe { bindings::copy_from_user(out_ptr, self.ptr,= len_ulong) }; > + if res !=3D 0 { > + return Err(EFAULT); > + } > + // Userspace pointers are not directly dereferencable by the ker= nel, so > + // we cannot use `add`, which has C-style rules for defined beha= vior. > + self.ptr =3D self.ptr.wrapping_byte_add(len); > + self.length -=3D len; > + Ok(()) > + } > + > + /// Reads raw data from the user slice into a kernel buffer. > + /// > + /// Fails with `EFAULT` if the read happens on a bad address. > + pub fn read_slice(&mut self, out: &mut [u8]) -> Result { > + // SAFETY: The types are compatible and `read_raw` doesn't write > + // uninitialized bytes to `out`. Can you add this as a guarantee to `read_raw`? > + let out =3D unsafe { &mut *(out as *mut [u8] as *mut [MaybeUnini= t]) }; > + self.read_raw(out) > + } > + > + /// Reads the entirety of the user slice, appending it to the end of= the > + /// provided buffer. > + /// > + /// Fails with `EFAULT` if the read happens on a bad address. > + pub fn read_all(mut self, buf: &mut Vec) -> Result { > + let len =3D self.length; > + buf.try_reserve(len)?; > + > + // The call to `try_reserve` was successful, so the spare capaci= ty is at > + // least `len` bytes long. > + self.read_raw(&mut buf.spare_capacity_mut()[..len])?; > + > + // SAFETY: Since the call to `read_raw` was successful, so the n= ext > + // `len` bytes of the vector have been initialized. > + unsafe { buf.set_len(buf.len() + len) }; > + Ok(()) > + } > +} > + > +/// A writer for [`UserSlice`]. > +/// > +/// Used to incrementally write into the user slice. > +pub struct UserSliceWriter { > + ptr: *mut c_void, > + length: usize, > +} > + > +impl UserSliceWriter { > + /// Returns the amount of space remaining in this buffer. > + /// > + /// Note that even writing less than this number of bytes may fail. > + pub fn len(&self) -> usize { > + self.length > + } > + > + /// Returns `true` if no more data can be written to this buffer. > + pub fn is_empty(&self) -> bool { > + self.length =3D=3D 0 > + } > + > + /// Writes raw data to this user pointer from a kernel buffer. > + /// > + /// Fails with `EFAULT` if the write happens on a bad address. > + pub fn write_slice(&mut self, data: &[u8]) -> Result { > + let len =3D data.len(); > + let data_ptr =3D data.as_ptr().cast::(); > + if len > self.length { > + return Err(EFAULT); > + } > + let Ok(len_ulong) =3D c_ulong::try_from(len) else { > + return Err(EFAULT); > + }; > + let res =3D unsafe { bindings::copy_to_user(self.ptr, data_ptr, = len_ulong) }; Missing SAFETY comment. --=20 Cheers, Benno > + if res !=3D 0 { > + return Err(EFAULT); > + } > + // Userspace pointers are not directly dereferencable by the ker= nel, so > + // we cannot use `add`, which has C-style rules for defined beha= vior. > + self.ptr =3D self.ptr.wrapping_byte_add(len); > + self.length -=3D len; > + Ok(()) > + } > +} >=20 > -- > 2.44.0.478.gd926399ef9-goog >=20