Received: by 2002:ab2:620c:0:b0:1ef:ffd0:ce49 with SMTP id o12csp1712595lqt; Wed, 20 Mar 2024 11:40:05 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUc/ReSdJSueN27FNUvqXWPOWd40UBIrZi6g4d3/vmjEEexM9Ya3Fs98D5lt/An7J0WAGidU8Rx6UzGdPZvL3r/94QMpQu2FiFhab8+BQ== X-Google-Smtp-Source: AGHT+IE0Vyt6FvLmDmoFQj3I5JmItUxnGOBtK3H7tp9yfm3PqRAb4kmgXg7lGjYFbCtugApD2owA X-Received: by 2002:a17:90a:ce8d:b0:29c:7592:febf with SMTP id g13-20020a17090ace8d00b0029c7592febfmr15462771pju.16.1710960005579; Wed, 20 Mar 2024 11:40:05 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710960005; cv=pass; d=google.com; s=arc-20160816; b=BR6vRv6v0h3fVOrKMY6aEEcwRcLknEafj+/w7m3LlvOkRegJLLOFCSaiIgs7JeuPnv ctGyOT4Rpsrd/914UK1ZUezPKvn2lG3QHkZkxghXsSQf2iXn194yowWXM7U1Ovh4WEDj 7tQ5354hGyrdK7+3GbFsuaMAtZxvVqDszHQHvkvBUqCb0yNdPrcFs9EfwV9h9fCr6hCP Kgb4LozZB6Kq2OQrIg6yCSrC1WKrnxnK0SNos5YtPJWUZRg3bzxspDiqJTiI3vEiPHu1 KwRYbWxvDnh5RnzgftV/MSFvwkBIHt4stB3KJjkcWt19z3Abyh7+y5oH2kQIoJAcdAvW pd3Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:feedback-id:dkim-signature; bh=xWRvU83WE/XxBo1v/F0X5t/KDOzdzaHYVuEHQEDHtYc=; fh=iCstqrMZgfKO4UA72Ov7/JfcRghryGAILPkAZ3ysdcg=; b=azLe4efZL7+OdVi8wAJfHkZVkRc41bn2kMgmNgGENdpzczVBawmXXLywg4AXqq1Mmm VkrnngdiY3UlOom3BtJAZ0Qr+Th7x/o9Vv487ZTxF5aWCXxUDdpwKykKJKE0zRqGT1N2 hDT8F8ux1zqUiSiMxMlAs72f/E8Kac8NaYc0oaGvZW1U9KzpDZb0EsjsuQp/iNE7akNd i99qBULQLDXKVvvFKiwDtnLBov5udm7LI7OzphAW+vfgLtcogXYVX8qZX0LHbTyAF/dt roZbSpzMIC4nfXvFp/eULt699jD5YQSFTSvEkfmiV5AgntRvcjBEUPFm108n/cmbfP+k nNrw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=H5TIoIGo; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-109301-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-109301-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id a22-20020a17090a8c1600b0029de042625csi1943097pjo.9.2024.03.20.11.40.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 11:40:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-109301-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=H5TIoIGo; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-109301-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-109301-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 3F83128527E for ; Wed, 20 Mar 2024 18:40:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 805A58529E; Wed, 20 Mar 2024 18:39:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="H5TIoIGo" Received: from mail-qt1-f182.google.com (mail-qt1-f182.google.com [209.85.160.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F01914F214; Wed, 20 Mar 2024 18:39:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710959994; cv=none; b=PyCxHCSWDy0BGa2VEIjTV+88YoMj+URjARyaWAWr6aK2LGiVE+Aeg9TIiLhcS37u/RtZDY8oKSWpoO7iiPoyAuUBtEcCz9qrIx/uIuZeYx+LtmEnCde031l0+RxxPnNRI1V/AnhqJdIlxENCXGMn/hou3QR+CcG+p9Scs7ZnOeQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710959994; c=relaxed/simple; bh=S/sytZTQ/V3fEb3RY0qv+twoobr9IaTxQMa2BQaJYzQ=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=kR87SUe8BFHuxu8IeZ1h1n9c1iAHqqsaYU32d3Xa7Bjb8Ghhb1gED4ylLXO8q9yhGibHNEpJEVIuMXcEbfK9dPVaau1EJItzri11rUv+GX3zlLMU9IBXi49wy49VWsHL3VaYfzljACjMReSHLbjKnf52u7nk0jQi3UaFD13zjaE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=H5TIoIGo; arc=none smtp.client-ip=209.85.160.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-qt1-f182.google.com with SMTP id d75a77b69052e-430a7497700so1302031cf.1; Wed, 20 Mar 2024 11:39:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710959992; x=1711564792; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=xWRvU83WE/XxBo1v/F0X5t/KDOzdzaHYVuEHQEDHtYc=; b=H5TIoIGoevzaoQUUJ+xBevuzfXysfgwcWqbsiNERd84oyEwpTiggF3ODIjjfSnku9x zeLO1EFpufEHDvUMe0rrcR/EzbhdJa0ELd2rdHdi93/2u8yzsFcEySyCh3kEqEElq+uh wbk7JPlkNnGfiOfIb3ugNeEIeSTaBVwPGf6O+fb3WSwQFyghxs1x8MFedogloiygajfW XjTSpKW0+IULbjPtWXk344XscuCrOLpa/qBM7xmWpuQflye1A+EMsFeQ5fA71noPeCBA tBBaBKzgP2HVhjfRJK4kD7nI6i7dMH2s8hrDfk9tMrgrifCRsREZISXQP8NKGlpYY99z 43mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710959992; x=1711564792; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xWRvU83WE/XxBo1v/F0X5t/KDOzdzaHYVuEHQEDHtYc=; b=HykhJh5t95OQ2GZ7ERhhXkYTiH29PXEXz2sNV0/Rk5a6o7huCzWsUCRumaqgpRC41M caQb0yWcXha+cop0aTAADFD94YY6VfD2WPQ65EHHO1AM7pDI1qPpgoeOv+hvIL03TQBt 43RKLlp6mZxFt8S1Htmetf8TscKxuEjUwYaCO3PzGXS+2cQfdD8m3Ie6EnvisuJIuJCT yD0I9NctKnFYQsw2R57MS7npSPoQyqNJAZ81g65gZccsmddnoIJcwburKdZwH6DfdnY4 qTV7e563+yh/0Bby8GxQFTp20S41S7KFZySSeDNoeBh1+m9omoxNj6YE4PhKWo+QyMRk Kztg== X-Forwarded-Encrypted: i=1; AJvYcCV6CikpnL5frIs1ZCzTeqnfiwMee0LmZcZx889YNPY5rX9stALf+mYvAJKKupykTyy60STjbp7ExzDlDFKNxUZZT43uwoNN8581VCSGJsnUxualKhJJ5R9q/XeEgvCKcxQYTgDWa8024SV4E+s= X-Gm-Message-State: AOJu0YzsamEDoGKCfVAZACsGcA/ol+c6BZhsLfdY9g2hTsLWp3niNX8b 4FrdH8xrnyKJiwKgonWo4fMw17D/QkMt7mvIEz14VhPTXyKiVfli X-Received: by 2002:a05:622a:1804:b0:430:ba7a:fe7b with SMTP id t4-20020a05622a180400b00430ba7afe7bmr14742759qtc.44.1710959991779; Wed, 20 Mar 2024 11:39:51 -0700 (PDT) Received: from fauth2-smtp.messagingengine.com (fauth2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id fb19-20020a05622a481300b004309cf16815sm4019519qtb.39.2024.03.20.11.39.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 11:39:51 -0700 (PDT) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailfauth.nyi.internal (Postfix) with ESMTP id A2C461200066; Wed, 20 Mar 2024 14:39:50 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Wed, 20 Mar 2024 14:39:50 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrleeggdduudegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvdenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpeejiefhtdeuvdegvddtudffgfegfeehgfdtiedvveevleevhfekhefftdek ieehvdenucffohhmrghinheprhhushhtqdhlrghnghdrohhrghenucevlhhushhtvghruf hiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsohhquhhnodhmvghsmhhtphgr uhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdeigedqudejjeekheehhedvqdgsoh hquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihigmhgvrdhnrghmvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 20 Mar 2024 14:39:49 -0400 (EDT) Date: Wed, 20 Mar 2024 11:39:33 -0700 From: Boqun Feng To: Alice Ryhl Cc: Miguel Ojeda , Matthew Wilcox , Al Viro , Andrew Morton , Kees Cook , Alex Gaynor , Wedson Almeida Filho , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Andreas Hindborg , Greg Kroah-Hartman , Arve =?iso-8859-1?B?SGr4bm5lduVn?= , 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 v3 1/4] rust: uaccess: add userspace pointers Message-ID: References: <20240311-alice-mm-v3-0-cdf7b3a2049c@google.com> <20240311-alice-mm-v3-1-cdf7b3a2049c@google.com> 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=us-ascii Content-Disposition: inline In-Reply-To: <20240311-alice-mm-v3-1-cdf7b3a2049c@google.com> On Mon, Mar 11, 2024 at 10:47:13AM +0000, Alice Ryhl wrote: > From: Wedson Almeida Filho > [...] > +/// # Examples > +/// > +/// Takes a region of userspace memory from the current process, and modify it > +/// by adding one to every byte in the region. > +/// > +/// ```no_run > +/// use alloc::vec::Vec; > +/// use core::ffi::c_void; > +/// use kernel::error::Result; > +/// use kernel::uaccess::UserSlice; > +/// > +/// pub fn bytes_add_one(uptr: *mut c_void, len: usize) -> Result<()> { I hit the following compile error when trying to run kunit test: ERROR:root:error: unreachable `pub` item --> rust/doctests_kernel_generated.rs:4167:1 | 4167 | pub fn bytes_add_one(uptr: *mut c_void, len: usize) -> Result<()> { | ---^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | | | help: consider restricting its visibility: `pub(crate)` | = help: or consider exporting it for use by other crates = note: requested on the command line with `-D unreachable-pub` error: unreachable `pub` item --> rust/doctests_kernel_generated.rs:4243:1 | 4243 | pub fn get_bytes_if_valid(uptr: *mut c_void, len: usize) -> Result> { | ---^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | | | help: consider restricting its visibility: `pub(crate)` | = help: or consider exporting it for use by other crates error: aborting due to 2 previous errors , which should be fixed if we make the function in the example not `pub`. > +/// let (read, mut write) = UserSlice::new(uptr, len).reader_writer(); > +/// > +/// let mut buf = Vec::new(); > +/// read.read_all(&mut buf)?; > +/// > +/// for b in &mut buf { > +/// *b = b.wrapping_add(1); > +/// } > +/// > +/// write.write_slice(&buf)?; > +/// Ok(()) > +/// } > +/// ``` > +/// > +/// Example illustrating a TOCTOU (time-of-check to time-of-use) bug. > +/// > +/// ```no_run > +/// use alloc::vec::Vec; > +/// use core::ffi::c_void; > +/// use kernel::error::{code::EINVAL, Result}; > +/// use kernel::uaccess::UserSlice; > +/// > +/// /// Returns whether the data in this region is valid. > +/// fn is_valid(uptr: *mut c_void, len: usize) -> Result { > +/// let read = UserSlice::new(uptr, len).reader(); > +/// > +/// let mut buf = Vec::new(); > +/// read.read_all(&mut buf)?; > +/// > +/// todo!() > +/// } > +/// > +/// /// Returns the bytes behind this user pointer if they are valid. > +/// pub fn get_bytes_if_valid(uptr: *mut c_void, len: usize) -> Result> { Ditto here. > +/// if !is_valid(uptr, len)? { > +/// return Err(EINVAL); > +/// } > +/// > +/// let read = UserSlice::new(uptr, len).reader(); > +/// > +/// let mut buf = Vec::new(); > +/// read.read_all(&mut buf)?; > +/// > +/// // THIS IS A BUG! The bytes could have changed since we checked them. > +/// // > +/// // To avoid this kind of bug, don't call `UserSlice::new` multiple > +/// // times with the same address. > +/// Ok(buf) > +/// } > +/// ``` > +/// > +/// [`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, > +} > + Regards, Boqun [...]