From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796381150; b=awZjSaEXf+WpsY78hTV1Mbg51F+QbeX4Q/wlcOj7cXWRmeb1GgfF6d8dyct63UTXVI5A 8kUi00X57DNapJA/Nu62PNK7Jdl8eoJ8EZQialeVdOY0abPOsu2uZ+gfOg9lB0azhFTAi B3Bbocm/RlaO51ha1a4Fl51P3p5b7rghHH6FQL6d+TSF8vu3sfrxcyEBMTJyugYcFRG11 bqsXH5QedLBcbkfmeaWeTUbnecVBPuYZYfLwdygPEOrogqYqKc4m6Yi2zTJuipiynJfcJ 7rjlDKBbbCcmCW2FCpgbicHK05ieZk3bEQmg2Uv1y52jHAUAsGY1gtrAUlB+/jsUUbQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796381150; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=nuHgubQydYnIP9yTRWl+yog7S8XZ+eMTB4olWBAgSC0=; b=vVGgPzehXI4uB4VeD60IlIg8Ylh//pyvfvSh3zyLlR8QOxkmQ8/kKGeVjbJvHhqqoZcl iWduOpxO4Y1czB0ShfuYDdAf41rxCfZgBfkTW0BSrbNhDXOJSUqhEY7ZIoY/Ym0P94Yk9 YWz9aUVa1mzNxjQy5B/A/hLDC6Zs6d0btiikHeT56zZDCkD6uSGb9sjz3luCYHguxZooS DMTEfaEn8Z2Fz9y6qn2vcLNymyp7hDycYl+D6Baw3b6MSSnKHvRJkKHsVF54Fdjzph8QQ gWO3wOjHIpFjwbTR5eP1gJnUqn2wLpxem0H1F7zhOKb4oJuKg1+DeCONkG4ahDme65g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=eG7JnLBW; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2811D606A32C; Fri, 4 Dec 2026 10:45:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=eG7JnLBW Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5AF701F000C2; Fri, 4 Dec 2026 10:45:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796381149; bh=nuHgubQydYnIP9yTRWl+yog7S8XZ+eMTB4olWBAgSC0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=eG7JnLBWQIaXQwrAyYhQLb0Xik/MlSU7yabyvmjeHWnwu5XN5M5/lNRahMH/TYPeg 57glxX9xBfZBTlhYYER+563sN8mssV/0InWxf65gUa7CpfwJbSIEg7HpN1IaZlJKKN zqrNmopPmM+3vc55mw2mqGico06FGhWZ0+4GNH/4= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Georgios Androutsopoulos , Peder Bergan , Gary Guo , Andreas Hindborg , Alice Ryhl , Miguel Ojeda Subject: [PATCH 7.3 581/742] rust: str: tie Formatter to its backing buffer Date: Fri, 4 Dec 2026 19:07:25 +0900 Message-ID: <20261204100650.567014735@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Peder Bergan commit cf2212fd3053857f800cc22d903118d522131ca8 upstream. Formatter::new() returns a Formatter whose lifetime is independent of the borrow of its backing buffer. Safe code can therefore drop the buffer and then write through the formatter, causing a use-after-free. Tie the buffer borrow to the formatter's lifetime so that the borrow checker rejects such callers and prevents overlapping mutable access. Fixes: 87482d6d9104 ("rust: str: allow `str::Formatter` to format into `&mut [u8]`.") Cc: stable@vger.kernel.org Reported-by: Georgios Androutsopoulos Closes: https://github.com/Rust-for-Linux/linux/issues/1261 Signed-off-by: Peder Bergan Reviewed-by: Gary Guo Reviewed-by: Andreas Hindborg Reviewed-by: Alice Ryhl Link: https://patch.msgid.link/20261117-formatter-lifetime-v2-1-e216d86ffb57@gmail.com Signed-off-by: Miguel Ojeda Signed-off-by: Greg Kroah-Hartman --- rust/kernel/str.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -645,7 +645,7 @@ impl fmt::Write for RawFormatter { /// Fails if callers attempt to write more than will fit in the buffer. pub struct Formatter<'a>(RawFormatter, PhantomData<&'a mut ()>); -impl Formatter<'_> { +impl<'a> Formatter<'a> { /// Creates a new instance of [`Formatter`] with the given buffer. /// /// # Safety @@ -658,7 +658,7 @@ impl Formatter<'_> { } /// Create a new [`Self`] instance. - pub fn new(buffer: &mut [u8]) -> Self { + pub fn new(buffer: &'a mut [u8]) -> Self { // SAFETY: `buffer` is valid for writes for the entire length for // the lifetime of `Self`. unsafe { Formatter::from_buffer(buffer.as_mut_ptr(), buffer.len()) }