From nobody Tue Apr 7 22:01:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 421F33D3001; Wed, 11 Mar 2026 10:51:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773226265; cv=none; b=hIQzU8sc+5f3DsBS1H0ZbcFfyJglUaGt/l3JH7RdSk3bF984fCudd/nAZu7w5pUedCW7H9flF8uu1H5ndtOeyXCZX0CgC9SXk6tDfSlSRnK3oueSLFOOs3KMJxRjr6N9YS1lhbweJ+E6DTS2faGaSSbfFJr8jZbhAfVPBTYrdEU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773226265; c=relaxed/simple; bh=MZON6RTe0+POhLMBN9IYZdEsh0baC1W9++LN5GUpjGA=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=HJn9U+G7c0fEnZqPFmLvOuUscyMybZ79X1mnes0IriaSYf+v21p+i7cevoz1KaSu6Vc9T5dIWVznnYzaoLAe93yTU1xhXJ3z+HyiUaZd1HSJUQQdOE/2QxDUpFJ37Wz9QZfLRNYwWVjc/RPvqDVVqoGobFlY3V3cW/WlfTujMOI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=j9n9FCHM; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="j9n9FCHM" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DB295C4CEF7; Wed, 11 Mar 2026 10:51:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773226264; bh=MZON6RTe0+POhLMBN9IYZdEsh0baC1W9++LN5GUpjGA=; h=From:To:Cc:Subject:Date:From; b=j9n9FCHMQCvtIGC2zj8VDkMnwbTYi+9rWZuqFXhYe+mgKLqk/f9AGJMQCqDezzzkD RZi7krplRUhzviV/8VI5A+pZGeJlyMAExGi3hzwT5pQ+xrBpGTFjdfc4NhVxF4rzEc VrNW6D2bCPVRkVa2Zbq0jvxuJbjCx1D0B8DKznONhyOB+Mk8B2D78epdQifupBrd1F dRonWG/0EnONefKB2+yIuNJ9YGpHdau9+iAeSjJ4F3Yr1cpoi55pJgwB09/xEfdt08 XflO0qxo3VWrzPy9rxZlrdlPNfeu5l3e1G/D3ZsEujzSQKKt22xXjCz3GPUdZ+0DIS n67P7jTug1+cw== From: Benno Lossin To: Benno Lossin , Gary Guo , Miguel Ojeda , Boqun Feng , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Fiona Behrens Cc: Tim Chirananthavat , stable@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] rust: pin-init: replace shadowed return token by `unsafe`-to-create token Date: Wed, 11 Mar 2026 11:50:49 +0100 Message-ID: <20260311105056.1425041-1-lossin@kernel.org> X-Mailer: git-send-email 2.53.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We use a unit struct `__InitOk` in the closure generated by the initializer macros as the return value. We shadow it by creating a struct with the same name again inside of the closure, preventing early returns of `Ok` in the initializer (before all fields have been initialized). In the face of Type Alias Impl Trait (TAIT) and the next trait solver, this solution no longer works [1]. The shadowed struct can be named through type inference. In addition, there is an RFC proposing to add the feature of path inference to Rust, which would similarly allow [2] Thus remove the shadowed token and replace it with an `unsafe` to create token. The reason we initially used the shadowing solution was because an alternative solution used a builder pattern. Gary writes [3]: In the early builder-pattern based InitOk, having a single InitOk type for token is unsound because one can launder an InitOk token used for one place to another initializer. I used a branded lifetime solution, and then you figured out that using a shadowed type would work better because nobody could construct it at all. The laundering issue does not apply to the approach we ended up with today. With this change, the example by Tim Chirananthavat in [1] no longer compiles and results in this error: error: cannot construct `pin_init::__internal::InitOk` with struct lite= ral syntax due to private fields --> src/main.rs:26:17 | 26 | InferredType {} | ^^^^^^^^^^^^ | =3D note: private field `0` that was not provided help: you might have meant to use the `new` associated function | 26 - InferredType {} 26 + InferredType::new() | Applying the suggestion of using the `::new()` function, results in another expected error: error[E0133]: call to unsafe function `pin_init::__internal::InitOk::ne= w` is unsafe and requires unsafe block --> src/main.rs:26:17 | 26 | InferredType::new() | ^^^^^^^^^^^^^^^^^^^ call to unsafe function | =3D note: consult the function's documentation for information on ho= w to avoid undefined behavior Reported-by: Tim Chirananthavat Link: https://github.com/rust-lang/rust/issues/153535 [1] Link: https://github.com/rust-lang/rfcs/pull/3444#issuecomment-4016145373 [= 2] Link: https://github.com/rust-lang/rust/issues/153535#issuecomment-40176208= 04 [3] Fixes: fc6c6baa1f40 ("rust: init: add initialization macros") Cc: stable@vger.kernel.org Signed-off-by: Benno Lossin Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo --- This is not yet a soundness issue, but could become one in the future when TAIT gets stabilized in a form that allows the problem described. --- rust/pin-init/internal/src/init.rs | 22 +++++++--------------- rust/pin-init/src/__internal.rs | 28 ++++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/rust/pin-init/internal/src/init.rs b/rust/pin-init/internal/sr= c/init.rs index 42936f915a07..cfae789d55ba 100644 --- a/rust/pin-init/internal/src/init.rs +++ b/rust/pin-init/internal/src/init.rs @@ -156,11 +156,6 @@ fn assert_zeroable(_: *mut = T) ); let field_check =3D make_field_check(&fields, init_kind, &path); Ok(quote! {{ - // We do not want to allow arbitrary returns, so we declare this t= ype as the `Ok` return - // type and shadow it later when we insert the arbitrary user code= . That way there will be - // no possibility of returning without `unsafe`. - struct __InitOk; - // Get the data about fields from the supplied type. // SAFETY: TODO let #data =3D unsafe { @@ -170,18 +165,15 @@ fn assert_zeroable(_: *mut= T) #path::#get_data() }; // Ensure that `#data` really is of type `#data` and help with typ= e inference: - let init =3D ::pin_init::__internal::#data_trait::make_closure::<_= , __InitOk, #error>( + let init =3D ::pin_init::__internal::#data_trait::make_closure::<_= , #error>( #data, move |slot| { - { - // Shadow the structure so it cannot be used to return= early. - struct __InitOk; - #zeroable_check - #this - #init_fields - #field_check - } - Ok(__InitOk) + #zeroable_check + #this + #init_fields + #field_check + // SAFETY: we are the `init!` macro that is allowed to cal= l this. + Ok(unsafe { ::pin_init::__internal::InitOk::new() }) } ); let init =3D move |slot| -> ::core::result::Result<(), #error> { diff --git a/rust/pin-init/src/__internal.rs b/rust/pin-init/src/__internal= .rs index 90f18e9a2912..90adbdc1893b 100644 --- a/rust/pin-init/src/__internal.rs +++ b/rust/pin-init/src/__internal.rs @@ -46,6 +46,24 @@ unsafe fn __pinned_init(self, slot: *mut T) -> Result<()= , E> { } } =20 +/// Token type to signify successful initialization. +/// +/// Can only be constructed via the unsafe [`Self::new`] function. The ini= tializer macros use this +/// token type to prevent returning `Ok` from an initializer without initi= alizing all fields. +pub struct InitOk(()); + +impl InitOk { + /// Creates a new token. + /// + /// # Safety + /// + /// This function may only be called from the `init!` macro in `../int= ernal/src/init.rs`. + #[inline(always)] + pub unsafe fn new() -> Self { + Self(()) + } +} + /// This trait is only implemented via the `#[pin_data]` proc-macro. It is= used to facilitate /// the pin projections within the initializers. /// @@ -68,9 +86,10 @@ pub unsafe trait PinData: Copy { type Datee: ?Sized + HasPinData; =20 /// Type inference helper function. - fn make_closure(self, f: F) -> F + #[inline(always)] + fn make_closure(self, f: F) -> F where - F: FnOnce(*mut Self::Datee) -> Result, + F: FnOnce(*mut Self::Datee) -> Result, { f } @@ -98,9 +117,10 @@ pub unsafe trait InitData: Copy { type Datee: ?Sized + HasInitData; =20 /// Type inference helper function. - fn make_closure(self, f: F) -> F + #[inline(always)] + fn make_closure(self, f: F) -> F where - F: FnOnce(*mut Self::Datee) -> Result, + F: FnOnce(*mut Self::Datee) -> Result, { f } base-commit: 1f318b96cc84d7c2ab792fcc0bfd42a7ca890681 --=20 2.53.0