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 EA0EAC00A94 for ; Mon, 15 Apr 2024 09:37:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 85C866B0098; Mon, 15 Apr 2024 05:37:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 80CC96B00A0; Mon, 15 Apr 2024 05:37:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6FB256B00A1; Mon, 15 Apr 2024 05:37:10 -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 504F26B0098 for ; Mon, 15 Apr 2024 05:37:10 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C9FA61C03B6 for ; Mon, 15 Apr 2024 09:37:09 +0000 (UTC) X-FDA: 82011262578.21.630AB01 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by imf18.hostedemail.com (Postfix) with ESMTP id 7ED1A1C000D for ; Mon, 15 Apr 2024 09:37:06 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=proton.me header.s=protonmail header.b=OtCZE2Gc; spf=pass (imf18.hostedemail.com: domain of benno.lossin@proton.me designates 185.70.43.16 as permitted sender) smtp.mailfrom=benno.lossin@proton.me; dmarc=pass (policy=quarantine) header.from=proton.me ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1713173827; 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=p1EtZ30uvut0peHMj2MGXUDT4+1A886YAPfyu8wzS+o=; b=0X+v59M+3xpAaK+od+CRvPk7qJMf43wE9NnS1VbUFiY8BFjIJa8v0/svyZsAsyiUTstpzf RWhd3I6VMY0K0h4vkClgEztwYl8UuUyTRa7svVAMlksQxx0dgKs6OR+hmGV4kvS38DPvwn TBDUirGeKQKaYUnhQoIDla5SLSfmVPE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713173827; a=rsa-sha256; cv=none; b=A+KHbd3KgBebUbPdGhCoJVsIg4htsCpU5L/c0x3t/UrFoEm4PsMxFtOfaTUFGKFppKi6RK huD+sbCTMXSYgz4rdJs7s8kfd1vtPJgRgpyDMCV3ei6SMHgblfIuEV4BYre/VDrOxkeegF W37qe6JTl8ftw3pnfJKowDHv/HzcaFU= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=proton.me header.s=protonmail header.b=OtCZE2Gc; spf=pass (imf18.hostedemail.com: domain of benno.lossin@proton.me designates 185.70.43.16 as permitted sender) smtp.mailfrom=benno.lossin@proton.me; dmarc=pass (policy=quarantine) header.from=proton.me DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1713173823; x=1713433023; bh=p1EtZ30uvut0peHMj2MGXUDT4+1A886YAPfyu8wzS+o=; 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=OtCZE2Gcf48z1GRL3TfFWnx+/2Ld3oUzuWrpovC4uLBIfddJEmt53KdLyL9i1arQM R9Oo+wLpeefAMZmRozQ044xIh6fEww62Kf8hx5uFVn/a0OBTZkj/Nx6FgUXeH70cjH Tv24NKvrIu//f+qbYMNOEyU1+FtKIx123W3pQTxTz6FqHDy62jxKb5nLa7jDRYIbF2 oSUaVXzRKPmXXkAlXacw7vO5lzJ5vYT9SYpKeXfoka6QVyz2BcU4YtHlyVzeTZ2b1S 5+jHrosdq9s3dH9TPAtpjmRSCepNtVdY5T50lvhrkjLGXM5D4TZ13Air1r9QnSGT6/ 7KsIUcSFjO/3A== Date: Mon, 15 Apr 2024 09:36:56 +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 v5 1/4] rust: uaccess: add userspace pointers Message-ID: <2cae6fd4-906c-44ad-88be-0dfed090d07c@proton.me> In-Reply-To: <20240415-alice-mm-v5-1-6f55e4d8ef51@google.com> References: <20240415-alice-mm-v5-0-6f55e4d8ef51@google.com> <20240415-alice-mm-v5-1-6f55e4d8ef51@google.com> Feedback-ID: 71780778:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Stat-Signature: tu1j6jfo4xsx1i11qur393mtys3dmkya X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 7ED1A1C000D X-Rspam-User: X-HE-Tag: 1713173826-657707 X-HE-Meta: U2FsdGVkX18fjGkMgcfyhJzkvtUGW6aNrWwbapyqoL6etBTvebVn50VNVRlUVPUsrOdna230xdfzZL5YlopeHzYX5xy7S7b16tYy+XwogpI60OAn+U5d8tca4gB9CLkfz/oTwfvfp0UKdlb6A7F8gEMrAK2zVDNQf7cmlmzA7aNKgIV3J7noe10xX3IL/DOOEyW2kmOuCfWxivTnvL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ter, 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 15.04.24 09:13, Alice Ryhl wrote: > +impl UserSlice { > + /// Constructs a user slice from a raw pointer and a length in bytes= . > + /// > + /// Constructing a [`UserSlice`] performs no checks on the provided = address and length, it can > + /// safely be constructed inside a kernel thread with no current use= rspace process. Reads and > + /// writes wrap the kernel APIs `copy_from_user` and `copy_to_user`,= which check the memory map > + /// of the current process and enforce that the address range is wit= hin the user range (no > + /// additional calls to `access_ok` are needed). > + /// > + /// Callers must be careful to avoid time-of-check-time-of-use (TOCT= OU) issues. The simplest way > + /// is to create a single instance of [`UserSlice`] per user memory = block as it reads each byte > + /// at most once. > + pub fn new(ptr: *mut c_void, length: usize) -> Self { What would happen if I call this with a kernel pointer and then read/write to it? For example let mut arr =3D [MaybeUninit::uninit(); 64]; let ptr: *mut [MaybeUninit] =3D &mut arr; let ptr =3D ptr.cast::(); let slice =3D UserSlice::new(ptr, 64); let (mut r, mut w) =3D slice.reader_writer(); r.read_raw(&mut arr)?; // SAFETY: `arr` was initialized above. w.write_slice(unsafe { MaybeUninit::slice_assume_init_ref(&arr) })?; I think this would violate the exclusivity of `&mut` without any `unsafe` code. (the `unsafe` block at the end cannot possibly be wrong) > + UserSlice { ptr, length } > + } [...] > + /// Returns `true` if no data is available in the io buffer. > + pub fn is_empty(&self) -> bool { > + self.length =3D=3D 0 > + } > + > + /// Reads raw data from the user slice into a kernel buffer. > + /// > + /// After a successful call to this method, all bytes in `out` are i= nitialized. I think we should put things like this into a `# Guarantees` section. --=20 Cheers, Benno > + /// > + /// 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: `out_ptr` points into a mutable slice of length `len_= ulong`, so we may write > + // that many bytes to it. > + 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 behavior. > + self.ptr =3D self.ptr.wrapping_byte_add(len); > + self.length -=3D len; > + Ok(()) > + }