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=1796382574; b=VnZAc39/Ls0dHZJ3PrZixtOGD4aI7z3nSxlk6AmviQAOlc0DcG8I32F5BWnceOwns3BM z2vuXy0TPfLAV0dApxd9kzTnHzir7rKst2H4DCA7pYndTKwhtpArduic3KUWX+Io0gR7b xRccxNDw6TJyhiNyH5NiT/cvRIGHElUgDWuS7WzdVcJjAUL8PTphxWk6nOBKsxJXU8dnN 1Gdu/vpnDAZTITEpMUj1CB3CXowVFd8LdOUVU67i6DOCmdx0YCCqQzCOb00xXJ5u8zgk8 32D1F60HOhjISvEAVRHejg7yrlOkUDkd24ISFkq30gqd0HX4HBFofML9HOpWCpWPeKA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382574; 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=d8o59E+/7lEUlKDTA4O7NRMNY66km2oaw7GZoXSihME=; b=lKxz8oBWj38OFhfi8dtOoE8UV4GZVpTxzbCuoMoCGG5yfxGi1rYSitCTDlHerYLCZ63q OaxfQ+iiPFmm1pryegBdA/pRzjC5ZUbLIMKCMnK6RiFKbzuWSIDYP9Dr5vABkMAlPM4X8 ixUxMVlhhCtnDge9SHVNGTyVjmeriM74Y7x1VyJC0XkDsWvL/+v7MCPyaAIq0KnXDao4I gbb4ozm5b8QfO483P1mYlokCTgab5a9AMz9tEL7AiBtav+L2N241j7EuTHsmRM1z4U1g7 cGAXFoRMMMUvOx5bWS8dteaIP/DLud1O6K4DIlsHepNvXl963auiM3aTEc5Gzy8WQdA== 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=rZfWvwO+; 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 8CE2A606A322; Fri, 4 Dec 2026 11:09:34 +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=rZfWvwO+ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1230F1F000CE; Fri, 4 Dec 2026 11:09:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796382574; bh=d8o59E+/7lEUlKDTA4O7NRMNY66km2oaw7GZoXSihME=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=rZfWvwO+Evlc8QOXfxG/+HC0vt3SYKuFddRhjDoBa/+uSK8PRSp7G5cs9CEfm2OSD C6yfvnPbAcWFjiojLT1HkL1IkLXivRWNiH1ZULs4HDsczbXXvyWaAeZqumJl2qS4mA X0dvEiXxVM2HarvdRufw258qk1+n7vF/gaPAwGeQ= 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 6.18 513/703] rust: str: tie Formatter to its backing buffer Date: Fri, 4 Dec 2026 19:06:52 +0900 Message-ID: <20261204100641.443989386@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-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 @@ -845,7 +845,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 @@ -858,7 +858,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()) }