All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] rust: str: tie Formatter to its backing buffer
@ 2026-11-17 17:57 ` Peder Bergan
  0 siblings, 0 replies; 12+ messages in thread
From: Peder Bergan via B4 Relay @ 2026-11-17 17:57 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, rust-for-linux, linux-kernel, stable,
	Georgios Androutsopoulos

Split the lifetime fix and its documentation into separate patches as
requested by Gary.

I compiled the fix-only first patch with Clippy and ran the string tests
and kernel doctests under x86_64 QEMU. The final source tree is unchanged
from v1, including the previously tested example.

OpenAI Codex assisted with coding and testing.

---
Changes in v2:
- Split the documentation and example into a second patch, as requested by Gary.
- Link to v1: https://lore.kernel.org/rust-for-linux/20261117-formatter-lifetime-v1-1-9d7f38686552@gmail.com/

---
Peder Bergan (2):
      rust: str: tie Formatter to its backing buffer
      rust: str: add an example for Formatter::new

 rust/kernel/str.rs | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
---
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
change-id: 20261117-formatter-lifetime-48391ed7affd

Best regards,
--  
Peder Bergan <peder.bergan@gmail.com>



^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v2 0/2] rust: str: tie Formatter to its backing buffer
@ 2026-11-17 17:57 ` Peder Bergan
  0 siblings, 0 replies; 12+ messages in thread
From: Peder Bergan @ 2026-11-17 17:57 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, rust-for-linux, linux-kernel, stable,
	Georgios Androutsopoulos

Split the lifetime fix and its documentation into separate patches as
requested by Gary.

I compiled the fix-only first patch with Clippy and ran the string tests
and kernel doctests under x86_64 QEMU. The final source tree is unchanged
from v1, including the previously tested example.

OpenAI Codex assisted with coding and testing.

---
Changes in v2:
- Split the documentation and example into a second patch, as requested by Gary.
- Link to v1: https://lore.kernel.org/rust-for-linux/20261117-formatter-lifetime-v1-1-9d7f38686552@gmail.com/

---
Peder Bergan (2):
      rust: str: tie Formatter to its backing buffer
      rust: str: add an example for Formatter::new

 rust/kernel/str.rs | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
---
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
change-id: 20261117-formatter-lifetime-48391ed7affd

Best regards,
--  
Peder Bergan <peder.bergan@gmail.com>


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v2 1/2] rust: str: tie Formatter to its backing buffer
  2026-11-17 17:57 ` Peder Bergan
@ 2026-11-17 17:57   ` Peder Bergan
  -1 siblings, 0 replies; 12+ messages in thread
From: Peder Bergan via B4 Relay @ 2026-11-17 17:57 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, rust-for-linux, linux-kernel, stable,
	Georgios Androutsopoulos

From: Peder Bergan <peder.bergan@gmail.com>

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 <georgeandrout13@gmail.com>
Closes: https://github.com/Rust-for-Linux/linux/issues/1261
Signed-off-by: Peder Bergan <peder.bergan@gmail.com>
---
 rust/kernel/str.rs | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index 644b4279a..e5fe84eeb 100644
--- 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()) }

-- 
2.56.1



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v2 1/2] rust: str: tie Formatter to its backing buffer
@ 2026-11-17 17:57   ` Peder Bergan
  0 siblings, 0 replies; 12+ messages in thread
From: Peder Bergan @ 2026-11-17 17:57 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, rust-for-linux, linux-kernel, stable,
	Georgios Androutsopoulos

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 <georgeandrout13@gmail.com>
Closes: https://github.com/Rust-for-Linux/linux/issues/1261
Signed-off-by: Peder Bergan <peder.bergan@gmail.com>
---
 rust/kernel/str.rs | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index 644b4279a..e5fe84eeb 100644
--- 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()) }

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v2 2/2] rust: str: add an example for Formatter::new
  2026-11-17 17:57 ` Peder Bergan
@ 2026-11-17 17:57   ` Peder Bergan
  -1 siblings, 0 replies; 12+ messages in thread
From: Peder Bergan via B4 Relay @ 2026-11-17 17:57 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, rust-for-linux, linux-kernel, stable,
	Georgios Androutsopoulos

From: Peder Bergan <peder.bergan@gmail.com>

Document the exclusive buffer borrow held by Formatter::new() and show
formatting into a live buffer. Check the number of bytes written and the
resulting buffer contents.

Signed-off-by: Peder Bergan <peder.bergan@gmail.com>
---
 rust/kernel/str.rs | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index e5fe84eeb..6222faacf 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -658,6 +658,22 @@ impl<'a> Formatter<'a> {
     }
 
     /// Create a new [`Self`] instance.
+    ///
+    /// The formatter exclusively borrows `buffer` for its lifetime.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use kernel::fmt::Write as _;
+    /// use kernel::str::Formatter;
+    ///
+    /// let mut buffer = [0; 4];
+    /// let mut formatter = Formatter::new(&mut buffer);
+    /// formatter.write_str("test")?;
+    /// assert_eq!(formatter.bytes_written(), 4);
+    /// assert_eq!(&buffer, b"test");
+    /// # Ok::<(), kernel::error::Error>(())
+    /// ```
     pub fn new(buffer: &'a mut [u8]) -> Self {
         // SAFETY: `buffer` is valid for writes for the entire length for
         // the lifetime of `Self`.

-- 
2.56.1



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v2 2/2] rust: str: add an example for Formatter::new
@ 2026-11-17 17:57   ` Peder Bergan
  0 siblings, 0 replies; 12+ messages in thread
From: Peder Bergan @ 2026-11-17 17:57 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, rust-for-linux, linux-kernel, stable,
	Georgios Androutsopoulos

Document the exclusive buffer borrow held by Formatter::new() and show
formatting into a live buffer. Check the number of bytes written and the
resulting buffer contents.

Signed-off-by: Peder Bergan <peder.bergan@gmail.com>
---
 rust/kernel/str.rs | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index e5fe84eeb..6222faacf 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -658,6 +658,22 @@ impl<'a> Formatter<'a> {
     }
 
     /// Create a new [`Self`] instance.
+    ///
+    /// The formatter exclusively borrows `buffer` for its lifetime.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use kernel::fmt::Write as _;
+    /// use kernel::str::Formatter;
+    ///
+    /// let mut buffer = [0; 4];
+    /// let mut formatter = Formatter::new(&mut buffer);
+    /// formatter.write_str("test")?;
+    /// assert_eq!(formatter.bytes_written(), 4);
+    /// assert_eq!(&buffer, b"test");
+    /// # Ok::<(), kernel::error::Error>(())
+    /// ```
     pub fn new(buffer: &'a mut [u8]) -> Self {
         // SAFETY: `buffer` is valid for writes for the entire length for
         // the lifetime of `Self`.

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 1/2] rust: str: tie Formatter to its backing buffer
  2026-11-17 17:57   ` Peder Bergan
  (?)
@ 2026-11-18 14:57   ` Gary Guo
  -1 siblings, 0 replies; 12+ messages in thread
From: Gary Guo @ 2026-11-18 14:57 UTC (permalink / raw)
  To: peder.bergan, Miguel Ojeda
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, rust-for-linux, linux-kernel, stable,
	Georgios Androutsopoulos

On Tue Nov 17, 2026 at 5:57 PM GMT, Peder Bergan via B4 Relay wrote:
> From: Peder Bergan <peder.bergan@gmail.com>
> 
> 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 <georgeandrout13@gmail.com>
> Closes: https://github.com/Rust-for-Linux/linux/issues/1261
> Signed-off-by: Peder Bergan <peder.bergan@gmail.com>

Reviewed-by: Gary Guo <gary@garyguo.net>

> ---
>  rust/kernel/str.rs | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 1/2] rust: str: tie Formatter to its backing buffer
  2026-11-17 17:57   ` Peder Bergan
  (?)
  (?)
@ 2026-11-19  8:03   ` Andreas Hindborg
  -1 siblings, 0 replies; 12+ messages in thread
From: Andreas Hindborg @ 2026-11-19  8:03 UTC (permalink / raw)
  To: peder.bergan, Miguel Ojeda
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Alice Ryhl, Trevor Gross, Danilo Krummrich, Daniel Almeida,
	Tamir Duberstein, Alexandre Courbot, Onur Özkan,
	rust-for-linux, linux-kernel, stable, Georgios Androutsopoulos

"Peder Bergan via B4 Relay" <devnull+peder.bergan.gmail.com@kernel.org>
writes:

> From: Peder Bergan <peder.bergan@gmail.com>
>
> 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 <georgeandrout13@gmail.com>
> Closes: https://github.com/Rust-for-Linux/linux/issues/1261
> Signed-off-by: Peder Bergan <peder.bergan@gmail.com>

Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>

Best regards,
Andreas Hindborg




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 2/2] rust: str: add an example for Formatter::new
  2026-11-17 17:57   ` Peder Bergan
  (?)
@ 2026-11-19  8:03   ` Andreas Hindborg
  -1 siblings, 0 replies; 12+ messages in thread
From: Andreas Hindborg @ 2026-11-19  8:03 UTC (permalink / raw)
  To: peder.bergan, Miguel Ojeda
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Alice Ryhl, Trevor Gross, Danilo Krummrich, Daniel Almeida,
	Tamir Duberstein, Alexandre Courbot, Onur Özkan,
	rust-for-linux, linux-kernel, stable, Georgios Androutsopoulos

"Peder Bergan via B4 Relay" <devnull+peder.bergan.gmail.com@kernel.org>
writes:

> From: Peder Bergan <peder.bergan@gmail.com>
>
> Document the exclusive buffer borrow held by Formatter::new() and show
> formatting into a live buffer. Check the number of bytes written and the
> resulting buffer contents.
>
> Signed-off-by: Peder Bergan <peder.bergan@gmail.com>

Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>

Best regards,
Andreas Hindborg




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 2/2] rust: str: add an example for Formatter::new
  2026-11-17 17:57   ` Peder Bergan
  (?)
  (?)
@ 2026-11-19  8:52   ` Alice Ryhl
  -1 siblings, 0 replies; 12+ messages in thread
From: Alice Ryhl @ 2026-11-19  8:52 UTC (permalink / raw)
  To: peder.bergan
  Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Trevor Gross, Danilo Krummrich,
	Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, rust-for-linux, linux-kernel, stable,
	Georgios Androutsopoulos

On Tue, Nov 17, 2026 at 6:57 PM Peder Bergan via B4 Relay
<devnull+peder.bergan.gmail.com@kernel.org> wrote:
>
> From: Peder Bergan <peder.bergan@gmail.com>
>
> Document the exclusive buffer borrow held by Formatter::new() and show
> formatting into a live buffer. Check the number of bytes written and the
> resulting buffer contents.
>
> Signed-off-by: Peder Bergan <peder.bergan@gmail.com>

Reviewed-by: Alice Ryhl <aliceryhl@google.com>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 1/2] rust: str: tie Formatter to its backing buffer
  2026-11-17 17:57   ` Peder Bergan
                     ` (2 preceding siblings ...)
  (?)
@ 2026-11-19  8:53   ` Alice Ryhl
  -1 siblings, 0 replies; 12+ messages in thread
From: Alice Ryhl @ 2026-11-19  8:53 UTC (permalink / raw)
  To: peder.bergan
  Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Trevor Gross, Danilo Krummrich,
	Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, rust-for-linux, linux-kernel, stable,
	Georgios Androutsopoulos

On Tue, Nov 17, 2026 at 6:57 PM Peder Bergan via B4 Relay
<devnull+peder.bergan.gmail.com@kernel.org> wrote:
>
> From: Peder Bergan <peder.bergan@gmail.com>
>
> 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 <georgeandrout13@gmail.com>
> Closes: https://github.com/Rust-for-Linux/linux/issues/1261
> Signed-off-by: Peder Bergan <peder.bergan@gmail.com>

Reviewed-by: Alice Ryhl <aliceryhl@google.com>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 0/2] rust: str: tie Formatter to its backing buffer
  2026-11-17 17:57 ` Peder Bergan
                   ` (2 preceding siblings ...)
  (?)
@ 2026-11-19  9:16 ` Miguel Ojeda
  -1 siblings, 0 replies; 12+ messages in thread
From: Miguel Ojeda @ 2026-11-19  9:16 UTC (permalink / raw)
  To: peder.bergan
  Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan, rust-for-linux, linux-kernel,
	stable, Georgios Androutsopoulos

On Tue, Nov 17, 2026 at 6:57 PM Peder Bergan via B4 Relay
<devnull+peder.bergan.gmail.com@kernel.org> wrote:
>
>       rust: str: tie Formatter to its backing buffer

Applied (just the first one) to `rust-fixes` -- thanks everyone!

(The other I will take it through `rust-next`).

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2026-11-19  9:16 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 17:57 [PATCH v2 0/2] rust: str: tie Formatter to its backing buffer Peder Bergan via B4 Relay
2026-11-17 17:57 ` Peder Bergan
2026-11-17 17:57 ` [PATCH v2 1/2] " Peder Bergan via B4 Relay
2026-11-17 17:57   ` Peder Bergan
2026-11-18 14:57   ` Gary Guo
2026-11-19  8:03   ` Andreas Hindborg
2026-11-19  8:53   ` Alice Ryhl
2026-11-17 17:57 ` [PATCH v2 2/2] rust: str: add an example for Formatter::new Peder Bergan via B4 Relay
2026-11-17 17:57   ` Peder Bergan
2026-11-19  8:03   ` Andreas Hindborg
2026-11-19  8:52   ` Alice Ryhl
2026-11-19  9:16 ` [PATCH v2 0/2] rust: str: tie Formatter to its backing buffer Miguel Ojeda

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.