Linux Hardening
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx+linux-hardening@kernel.org>
To: Steven Rostedt <rostedt@goodmis.org>
Cc: Jiri Kosina <jkosina@suse.cz>, Petr Mladek <pmladek@suse.cz>,
	 Kees Cook <kees@kernel.org>,
	linux-hardening@vger.kernel.org
Subject: seq_buf_str() silent truncation (data corruption)
Date: Wed, 28 Oct 2026 22:30:38 +0100	[thread overview]
Message-ID: <auJmkx1p3nIrYeJ_@devuan> (raw)
In-Reply-To: <auIrUV7A8eF8DCGG@devuan>

[-- Attachment #1: Type: text/plain, Size: 1686 bytes --]

Hi Steve,


I believe I've found a bug in seq_buf_str().

Consider the following code:

	char            buf[3];
	struct seq_buf  s;

	seq_buf_init(&s, buf, 3);
	seq_buf_putmem(&s, "foo", 3);  // This will return 0, I believe.

	// At this point, ->len==3 and ->size==3, I believe.

	seq_buf_str(&s);  // BUG

Let's see the implementation of seq_buf_str():

	$ git show v7.3:include/linux/seq_buf.h | grepc seq_buf_str
	(standard input):static inline const char *seq_buf_str(struct seq_buf *s)
	{
		if (WARN_ON(s->size == 0))
			return "";

		if (seq_buf_buffer_left(s))
			s->buffer[s->len] = 0;
		else
			s->buffer[s->size - 1] = 0;

		return s->buffer;
	}

The first check is fine (we don't trigger the WARN_ON()).
seq_buf_buffer_left(s) will return 0 (it returns 's->size - s->len').
This means we'll execute the second branch.  That branch will result in
overwriting the third byte with a '\0', without reporting the overflow.

I believe the following should fix the bug:

	@@ -102,10 +104,12 @@ static inline const char *seq_buf_str(struct seq_buf *s)
		if (WARN_ON(s->size == 0))
			return "";
	 
	-       if (seq_buf_buffer_left(s))
	+       if (seq_buf_buffer_left(s)) {
			s->buffer[s->len] = 0;
	-       else
	+       } else {
	+               seq_buf_set_overflow(s);
			s->buffer[s->size - 1] = 0;
	+       }
	 
		return s->buffer;
	 }

I have not yet researched enough to sign a patch, but please let me know
what you think.  I can prepare a formal patch next week.

NOT-assisted-by: LLM
Reported-by: Alejandro Colomar <alx@kernel.org>


Have a lovely night!
Alex

-- 
<https://www.alejandro-colomar.es>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  parent reply	other threads:[~2026-10-28 21:30 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-28 17:13 seq_buf_putc() and strings Alejandro Colomar
2026-10-28 17:30 ` Alejandro Colomar
2026-10-28 17:47 ` Steven Rostedt
2026-10-28 18:00   ` Alejandro Colomar
2026-10-28 20:59 ` seq_buf_used() return value on overflow Alejandro Colomar
2026-10-28 21:03   ` Alejandro Colomar
2026-10-28 21:30 ` Alejandro Colomar [this message]
2026-10-29  1:04   ` seq_buf_str() silent truncation (data corruption) Steven Rostedt
2026-10-29 11:48     ` Alejandro Colomar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=auJmkx1p3nIrYeJ_@devuan \
    --to=alx+linux-hardening@kernel.org \
    --cc=jkosina@suse.cz \
    --cc=kees@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=pmladek@suse.cz \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox