Received: by 2002:a05:6500:1b45:b0:1f5:f2ab:c469 with SMTP id cz5csp703218lqb; Wed, 17 Apr 2024 08:27:40 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXg+xyQU4fXl7608Dy2Ef6Wbf1Ju6HEPm+UHJXK2BupPrZQdkQVbiSgHpEFCEmE2Aq0Grgu+1osLwZD9344rPO6xeOwPpKRUyQxDrvQhQ== X-Google-Smtp-Source: AGHT+IEBtr9hsmcAvR6/CsjTmvbVnwmw8SqtrpdIbfHYn8y9vO73IdYb5PUzzUZYCqgyIye8NF8z X-Received: by 2002:ac8:60ca:0:b0:437:3676:b3f with SMTP id i10-20020ac860ca000000b0043736760b3fmr8683953qtm.4.1713367660533; Wed, 17 Apr 2024 08:27:40 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713367660; cv=pass; d=google.com; s=arc-20160816; b=Dsth9KHGW1R6nnw2ut4l/gpebFSKuby5aKJ5VDI0T3D/kmSd/Glj0bxo83E5Jl79tp i8ik5J2YuLBriqKvT9Ho0jpbfpbTmEsuPjZYkqVIArPTiYriljgv5aJInBEvJcpMZMPb ix2tcv55JNOfXQGqI6EVUrre7nIPNON80yMU8B2ap1UIGGMYnPD+HM7/4PgLP4bGVcVD xAVd18wBe3MATL8ab+IgYFfpA7luz64bYMllOLALw2IGG6D6a5Lrg5PNfUImRUETyEAr cNDCtaQPhvsvsB2HD3U4dwCMGpN/cplnzq4xn0bUwJwJaGwd+D3CACwbbtjrkzDr04sD 4c/g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:feedback-id:references :in-reply-to:message-id:subject:cc:from:to:date:dkim-signature; bh=yE1RsTT/3QhtCokhgtHPH8FIR5On7XGmjEDodbAFAQI=; fh=rkUv5zhLdjS/nOxvYihz3ePDwkk2Ya5vn0lqeBCWm0o=; b=pnd9GZF65dvvdwUwzc3YfiPutL0aBtRTC/u7JYkp5Q6bsuzAS5iyasRgwB+z6oRRYu kNJUWwSokoE6lvrquOaNwACyVMZ7pDmu9WrCKYA/1TQAUVtALLFQcPUXvHgKe4EJYF45 W0gEaX8b47dCcyZuuJhptS1Hv1Kou0xdxPV00YJxl4Jf6vZxQZl2H7swa8MyUrbSan9K kdm/f+3LSfcdMKrkG/J2sqSrNkFxxikso1AtmoKYx9xUHdzd78QWqH6aM5E53uAu/3tR uigAnidVbL6cspQBWCtOZ3A83t70E89w0vFte/zPeu8v2tIjYFXpvNhHRKQImHElRZuG xxaw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=SEuodbfR; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-148798-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-148798-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id u15-20020a05622a198f00b0043461a30dc8si9620489qtc.320.2024.04.17.08.27.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Apr 2024 08:27:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-148798-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=SEuodbfR; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-148798-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-148798-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 89CAB1C21771 for ; Wed, 17 Apr 2024 15:27:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 03E991482FE; Wed, 17 Apr 2024 15:27:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="SEuodbfR" Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AF3C01474C7; Wed, 17 Apr 2024 15:27:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.16 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713367636; cv=none; b=UyyZ7elctML+giJgWeYxgelJ6DQhC7Yag1eDn2YqMZ7gw6bc57aU8UyUmDtaLy1Y1d7n7m1a8MxK9KbNC/JCOg0fzD64y1tW3ztZMRiF75wuY+ksd3qdaf0Przr0awuipsOgLdI/sMNk1kiAVtdj9hRvcjWAc6LlEBXoK0uSrUo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713367636; c=relaxed/simple; bh=9HPTSvA9Vl4MuqVnZmDO/gJ2gS6Uz84Qlx8Z9fvRJU4=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=czPVvzGb05XZPfJNS08fkVTf0CETc1SuPAJaoUjozWugQrtl7BifTueP0yoOBqw+3GgtAJm5SKhoaf0MZWRejIzVgDwNTxiVCxrmzBD4Up30zxrCsVKl1M5HBbcwqujs+V0h8E+NhlQut23r6pZsFQKj5ClqUBTAz01U6brmbr8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b=SEuodbfR; arc=none smtp.client-ip=185.70.43.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1713367632; x=1713626832; bh=yE1RsTT/3QhtCokhgtHPH8FIR5On7XGmjEDodbAFAQI=; 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=SEuodbfR8tDN1iztrdISMh608nxV7gNjcXC3IkbviYQDqAG/K7kbEYXVYFyVpsZED oTSH49hr/DqRrbM+whOMt8FqBzDbVrhicQLAQ7v3k5ZBc99gTS4EaNQRkemL8/nsRD eEM1WDBbocmVXAvCYGqjAZe4/gqJx0o9XcUfkwS7MqgRJsfb9iO1HHCZjR0/OBu422 vTFuAD3XuK/TnkqCwLlu0OUdK/QlEdYjy3hWUjgH27rf87Ma10qYMRAaW9TXb5QzZ5 CN3DYniWeLD6vNaOPDLllM7o6ZmTPkA2rfgRtGipuPZkhwu/sF/4qlg6Phoqn4Jv2y Aaw9y+q8TerZg== Date: Wed, 17 Apr 2024 15:27:02 +0000 To: Alice Ryhl , Gary Guo From: Benno Lossin Cc: Miguel Ojeda , Matthew Wilcox , Al Viro , Andrew Morton , Kees Cook , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , =?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: In-Reply-To: References: <20240415-alice-mm-v5-0-6f55e4d8ef51@google.com> <20240415-alice-mm-v5-1-6f55e4d8ef51@google.com> <20240417152802.6b7a7384@eugeo> Feedback-ID: 71780778:user:proton Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 17.04.24 16:40, Alice Ryhl wrote: > On Wed, Apr 17, 2024 at 4:28=E2=80=AFPM Gary Guo wrote= : >> >> On Mon, 15 Apr 2024 07:13:53 +0000 >> Alice Ryhl wrote: >> >>> From: Wedson Almeida Filho >>> >>> A pointer to an area in userspace memory, which can be either read-only >>> 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 addresses) >>> will return `EFAULT`. Concurrent access, *including data races to/from >>> userspace memory*, is permitted, because fundamentally another userspac= e >>> thread/process could always be modifying memory at the same time (in th= e >>> same way that userspace Rust's `std::io` permits data races with the >>> contents of files on disk). In the presence of a race, the exact byte >>> values read/written are unspecified but the operation is well-defined. >>> Kernelspace code should validate its copy of data after completing a >>> read, and not expect that multiple reads of the same address will retur= n >>> the same value. >>> >>> These APIs are designed to make it difficult to accidentally write >>> TOCTOU bugs. Every time you read from a memory location, the pointer is >>> advanced by the length so that you cannot use that reader to read the >>> same memory location twice. Preventing double-fetches avoids TOCTOU >>> bugs. This is accomplished by taking `self` by value to prevent >>> obtaining multiple readers on a given `UserSlicePtr`, and the readers >>> only permitting forward reads. If double-fetching a memory location is >>> necessary for some reason, then that is done by creating multiple >>> readers to the same memory location. >>> >>> Constructing a `UserSlicePtr` performs no checks on the provided >>> address and length, it can safely be constructed inside a kernel thread >>> with no current userspace process. Reads and writes wrap the kernel API= s >>> `copy_from_user` and `copy_to_user`, which check the memory map of the >>> current process and enforce that the address range is within the user >>> range (no additional calls to `access_ok` are needed). >>> >>> This code is based on something that was originally written by Wedson o= n >>> the old rust branch. It was modified by Alice by removing the >>> `IoBufferReader` and `IoBufferWriter` traits, and various other changes= . >>> >>> Signed-off-by: Wedson Almeida Filho >>> Co-developed-by: Alice Ryhl >>> Signed-off-by: Alice Ryhl >>> --- >>> rust/helpers.c | 14 +++ >>> rust/kernel/lib.rs | 1 + >>> rust/kernel/uaccess.rs | 304 ++++++++++++++++++++++++++++++++++++++++= +++++++++ >>> 3 files changed, 319 insertions(+) >>> >>> diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs >> >>> +/// [`std::io`]: https://doc.rust-lang.org/std/io/index.html >>> +/// [`clone_reader`]: UserSliceReader::clone_reader >>> +pub struct UserSlice { >>> + ptr: *mut c_void, >>> + length: usize, >>> +} >> >> How useful is the `c_void` in the struct and new signature? They tend >> to not be very useful in Rust. Given that provenance doesn't matter >> for userspace pointers, could this be `usize` simply? >> >> I think `*mut u8` or `*mut ()` makes more sense than `*mut c_void` for >> Rust code even if we don't want to use `usize`. >=20 > I don't have a strong opinion here. I suppose a usize could make > sense. But I also think c_void is fine, and I lean towards not > changing it. :) >=20 >> Some thinking aloud and brainstorming bits about the API. >> >> I wonder if it make sense to have `User<[u8]>` instead of `UserSlice`? >> The `User` type can be defined like this: >> >> ```rust >> struct User { >> ptr: *mut T, >> } >> ``` >> >> and this allows arbitrary T as long as it's POD. So we could have >> `User<[u8]>`, `User`, `User`. I imagine the >> `User<[u8]>` would be the general usage and the latter ones can be >> especially helpful if you are trying to implement ioctl and need to >> copy fixed size data structs from userspace. >=20 > Hmm, we have to be careful here. Generally, when you get a user slice > via an ioctl, you should make sure to use the length you get from > userspace. In binder, it looks like this: >=20 > let user_slice =3D UserSlice::new(arg, _IOC_SIZE(cmd)); >=20 > so whichever API we use, we must make sure to get the length as an > argument in bytes. What should we do if the length is not a multiple > of size_of(T)? We could print a warning and then just floor to the next multiple of `size_of::()`. I agree that is not perfect, but if one uses the current API, one also needs to do the length check eventually. > Another issue is that there's no stable way to get the length from a > `*mut [T]` without creating a reference, which is not okay for a user > slice. Seems like `<* const [T]>::len` (feature `slice_ptr_len`) [1] was just stabilized 5 days ago [1]. [1]: https://doc.rust-lang.org/std/primitive.pointer.html#method.len-1 [2]: https://github.com/rust-lang/rust/pull/123868 --=20 Cheers, Benno