mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Kees Cook <kees@kernel.org>
To: David Laight <david.laight.linux@gmail.com>
Cc: Bill Wendling <morbo@google.com>,
	"Matthew Wilcox (Oracle)" <willy@infradead.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	David Gow <david@davidgow.net>, Jiri Kosina <jikos@kernel.org>,
	Petr Mladek <pmladek@suse.com>,
	Shuvam Pandey <shuvampandey1@gmail.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: Re: [PATCH v5 05/12] seq_buf: Clear what a writer did not claim when a seq_buf overflows
Date: Tue, 6 Oct 2026 14:26:56 -0700	[thread overview]
Message-ID: <20261006212552.51e0c3-kees@kernel.org> (raw)
In-Reply-To: <20261005195926.09e240d9@pumpkin>

On Mon, Oct 05, 2026 at 07:59:26PM +0100, David Laight wrote:
> >  static inline void
> >  seq_buf_set_overflow(struct seq_buf *s)
> >  {
> > +	if (s->len < s->size)
> > +		memset(s->buffer + s->len, 0, s->size - s->len);
>
> What is the performance impact of zeroing the buffer?

Overflow is never a fast path: it happens at most once per seq_buf
(after that len > size, so the memset is skipped), and it is already a
failure the caller has to handle.

It is also usually zero bytes. printf, bprintf, puts, putmem, and putc
all fill to the end before overflowing, so len == size and there is
nothing to clear. Only a writer handed the tail by seq_buf_get_buf()
that then gives up leaves anything behind (seq_buf_path() with d_path(),
or landlock's string_escape_mem()), and then the clear covers only the
space that writer was given, which it may have partly filled.

> I don't think it would be a good idea to be zeroing the buffer on entry
> either (I've not looked to see it that happens - but there will be PAGE_SIZE
> buffers (maybe 64k) that get a a small number of characters written to them).

Agreed, and it doesn't: seq_buf_init() writes a single NUL.

> Writing a single '\0' really ought to be enough.

It would be for seq_buf_str(), but once a seq_buf has overflowed,
seq_buf_used() reports the whole buffer, and seq_buf_print_seq() and
seq_buf_to_user() copy that many bytes. Whatever followed the NUL (the
path fragment d_path() left at the end, say) would still reach the
seq_file or userspace.

-Kees

-- 
Kees Cook

  reply	other threads:[~2026-10-06 21:26 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-05 15:56 [PATCH v5 00/12] seq_buf: Add seq_buf_strlen() Kees Cook
2026-10-05 15:56 ` [PATCH v5 01/12] seq_buf: Do not print an empty line from an overflowed seq_buf_do_printk() Kees Cook
2026-10-05 15:56 ` [PATCH v5 02/12] seq_buf: Do not pop from an overflowed seq_buf Kees Cook
2026-10-05 15:56 ` [PATCH v5 03/12] scsi: lpfc: Print rx monitor records straight into the seq_buf Kees Cook
2026-10-05 18:54   ` David Laight
2026-10-06 13:26     ` Kees Cook
2026-10-05 15:56 ` [PATCH v5 04/12] seq_buf: Copy what fits when seq_buf_puts() and seq_buf_putmem() overflow Kees Cook
2026-10-05 15:56 ` [PATCH v5 05/12] seq_buf: Clear what a writer did not claim when a seq_buf overflows Kees Cook
2026-10-05 18:59   ` David Laight
2026-10-06 21:26     ` Kees Cook [this message]
2026-10-05 15:56 ` [PATCH v5 06/12] seq_buf: Add seq_buf_strlen() Kees Cook
2026-10-05 15:56 ` [PATCH v5 07/12] seq_buf: Add seq_buf_terminate() Kees Cook
2026-10-05 15:56 ` [PATCH v5 08/12] bpf: Remove dead newline stripping from format_disasm_line() Kees Cook
2026-10-05 15:56 ` [PATCH v5 09/12] seq_buf: Add seq_buf_init_append() Kees Cook
2026-10-05 15:57 ` [PATCH v5 10/12] powerpc/papr_scm: Return the string length from the sysfs show functions Kees Cook
2026-10-05 15:57 ` [PATCH v5 11/12] nvdimm: ndtest: Return the string length from flags_show() Kees Cook
2026-10-05 15:57 ` [PATCH v5 12/12] docs: core-api: Document the seq_buf API Kees Cook

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=20261006212552.51e0c3-kees@kernel.org \
    --to=kees@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=david.laight.linux@gmail.com \
    --cc=david@davidgow.net \
    --cc=jikos@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=morbo@google.com \
    --cc=pmladek@suse.com \
    --cc=rostedt@goodmis.org \
    --cc=shuvampandey1@gmail.com \
    --cc=willy@infradead.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

all inboxes | Powered by JetHome®