Received: by 2002:ab2:69cc:0:b0:1f4:be93:e15a with SMTP id n12csp1337103lqp; Mon, 15 Apr 2024 03:32:14 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX3AlSgSFgmGSh6h8F7P49Ev69PJcVYXiYkmRPW8QsJAVwl7fWw2Rt1wTKEjzryf+SFTXy80E0AkEEcrisTsEyYuolfGJ7H17CPPeqqKA== X-Google-Smtp-Source: AGHT+IE4yj5QcPgpZPBN8BnwnnFpv+vh+DQ2t94z8iR34034McVHcWSrgkb7Hgo0s8/U2W230TCO X-Received: by 2002:a05:6a20:12c1:b0:1a7:66ad:b93d with SMTP id v1-20020a056a2012c100b001a766adb93dmr9682582pzg.37.1713177134507; Mon, 15 Apr 2024 03:32:14 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713177134; cv=pass; d=google.com; s=arc-20160816; b=dme/pSWn3z6ZELg2ORc464BgSLdxLD9jRB6o69mJaXVjY19pKwoyVJLgY6OnvEOyz9 J0kqbP7k8Fq837M+fajG6KF1X/Wvl6dtjAfOmr+9EXbIcCJKAAk+t5wAb/pQZ+ryVy6c llWJyifAMQlAoiiZ2p+WxZjgbw86WIdPu1EIjhLRiyCfhiFNJ9MnNcdmKMH4yVGE9dO8 gQ6q+NPC4nYJBXGMw0mreDuGvMzs3O7ZR3HIBDlGnc8HFkIDP9iUx2Wu5V6Z1wtsxAJq mzKlcNXAL8BszOxVoqkg7OdPo7Sv57MH8fWsC4qV+R913/xYLXBBvErWIC0IwNkoUQlR PMmA== 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=c6ckz4hhAbktVZkF1RuxUbUmw/jaVAkQ7dsloKnHX8M=; fh=Z+quULPzVxY/Y3+ePaY+rTSCnQGPWrYTfB9G+SR+Y7I=; b=QcMq0UUCCzePvbHq6rBC5oar5Uwbss2m8IfsTdzPYjRb8cFZYt2EbfbsJSJgMRY4Vy G5JrcrNgG7uDzm/9G+JtUzPtwLz/JtxcSj1jnAUTxD9/Kd4uEtpYByS9zkzcmNQm7KMa 4dN8Zyz3aCsfXnOJGKuO8dfsAUIkIMHI/+YAVe9kp4ooZUa98yrrjFGGtt2jDMoHF/0Q qT1NHVLBNgfnFyLik2twj2sMfAbq9CpgXjCF8OfZ8bYjh7RoCIjZr6e9sv0uCIiqcFYO 6E00iiRyDj5x/l7sGneuHvMAwQaICauDEk+jDX9bM5HFTCXiqJ+dOI4s6h464M9e22kz g2tA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b="H4m/cOlC"; 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-144868-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-144868-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id p27-20020a63951b000000b005f751914a66si3518228pgd.604.2024.04.15.03.32.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Apr 2024 03:32:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-144868-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=@proton.me header.s=protonmail header.b="H4m/cOlC"; 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-144868-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-144868-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 54F2E288AD6 for ; Mon, 15 Apr 2024 09:52:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CBE3E4597D; Mon, 15 Apr 2024 09:52:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="H4m/cOlC" Received: from mail-0301.mail-europe.com (mail-0301.mail-europe.com [188.165.51.139]) (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 692B14E1A0 for ; Mon, 15 Apr 2024 09:52:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=188.165.51.139 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713174753; cv=none; b=lQm7lDfBp31cC93I1PhQVok0ZFdAKIUruxmZohqFlswSrKopkrCJ1TD5uRShEKhmd88d6X40vhGAxo2KsHZv2caiSPMw4ovm9DPHHk/msISrg2fPWOzwcAZJJtxx2Rbmsqz3KpgS7yL6W3o+n8OeUnnzDqVprnQqJzNKcST6RvM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713174753; c=relaxed/simple; bh=wRVTJ0UbYbZDQRp6UEkosuQ5Xv/tRLFqmEIuhSEGVu4=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=j/GCWmwUfw++Xybri/j0LHXsYgK6K78xiRLfVT2ehvyBl3quqPoqqSHG1SZnuTMtRJUo20ebtjcGRtlIxfjJC4QRDiOdRB1IiuyPlQ6ifHxrgcKXZ2Dl1bbajuMS+JOQ3k1I31zB1w0dvk/lbx9wI4zqaTP9p1K+HXtKSDQp8/Y= 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=H4m/cOlC; arc=none smtp.client-ip=188.165.51.139 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=1713174736; x=1713433936; bh=c6ckz4hhAbktVZkF1RuxUbUmw/jaVAkQ7dsloKnHX8M=; 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=H4m/cOlCahXLO+0GLUL9cXxPqoObTqJqbMgZaX0TOJbG4YwQf5PO4XUDPkJ7/d85Y 1z6MlUat8q+X7wyjRBLEpLI86mo77Iir0TCVWoiiyaVSK7GTVSITXAhnpFYJrx5G9L 5VwJ7e2Vecmjew40OzAOwZ3W/RLVqQAbhRISYnsBhDpt+i91x37qze+C/S8arERy2v NARZhDhUhivIqHZ+LHnxFbfWJsXvzgWoNrEDFfHZF66n8uxgU1Um0Kg/G3c0ULZCQr dJds5xaHflmkLw+dKiWj2w/RIBKaJegRspfuavfuEWEshLTJNpskEsxbQAECMBfV3Y QNOmEt1kDxQRg== Date: Mon, 15 Apr 2024 09:51:47 +0000 To: Alice Ryhl From: Benno Lossin Cc: Miguel Ojeda , Matthew Wilcox , Al Viro , Andrew Morton , Kees Cook , 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: <36c22716-ba41-46e0-8e18-991f1a14b208@proton.me> In-Reply-To: References: <20240415-alice-mm-v5-0-6f55e4d8ef51@google.com> <20240415-alice-mm-v5-1-6f55e4d8ef51@google.com> <2cae6fd4-906c-44ad-88be-0dfed090d07c@proton.me> 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 15.04.24 11:44, Alice Ryhl wrote: > On Mon, Apr 15, 2024 at 11:37=E2=80=AFAM Benno Lossin wrote: >> >> On 15.04.24 09:13, Alice Ryhl wrote: >>> +impl UserSlice { >>> + /// Constructs a user slice from a raw pointer and a length in byt= es. >>> + /// >>> + /// Constructing a [`UserSlice`] performs no checks on the provide= d address and length, it can >>> + /// safely be constructed inside a kernel thread with no current u= serspace 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 w= ithin the user range (no >>> + /// additional calls to `access_ok` are needed). >>> + /// >>> + /// Callers must be careful to avoid time-of-check-time-of-use (TO= CTOU) issues. The simplest way >>> + /// is to create a single instance of [`UserSlice`] per user memor= y 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) >=20 > This will fail with an EFAULT error. There is a check on the C side > that verifies that the address is in userspace. (The access_ok call.) I see, that makes a lot of sense. Regardless of whether you fix the nit about the guarantees section: Reviewed-by: Benno Lossin --=20 Cheers, Benno