mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Kees Cook <kees@kernel.org>
To: Alejandro Colomar <alx+linux-hardening@kernel.org>
Cc: David Laight <david.laight.linux@gmail.com>,
	Mariia Nikitash <nikitash.mariiaw@gmail.com>,
	hverkuil@kernel.org, mchehab@kernel.org,
	linux-media@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-hardening@vger.kernel.org, justinstitt@google.com,
	morbo@google.com
Subject: Re: [PATCH] media: radio: Replace strlcat() with seq_buf in TEA drivers
Date: Thu, 12 Nov 2026 09:54:15 -0800	[thread overview]
Message-ID: <202611120947.AC428BE4B@keescook> (raw)
In-Reply-To: <avDmyLYpWOQ2WB-i@debian>

On Sun, Nov 08, 2026 at 10:25:35PM +0100, Alejandro Colomar wrote:
> Hi David,
> 
> > Date: 2026-11-08 21:02:32+0000
> > From: David Laight <david.laight.linux@gmail.com>
> >
> [...]
> > 
> > seq_buf is just OTT.
> > Using to replace a single strcat() is just silly.
> > There is no point doing these patches until there is a sensible alternative.
> 
> Agree.
> 
> [...]
> > >  	strscpy(v->driver, tea->v4l2_dev->name, sizeof(v->driver));
> > > -	strscpy(v->card, tea->card, sizeof(v->card));
> > > -	strlcat(v->card, " TEA5777", sizeof(v->card));
> > 
> > What is wrong with (and almost OTT):
> > #define TEA5777 " TEA5777"
> > 	BUILD_BUG_ON(sizeof(TEA5777) <= sizeof(v->card))
> > 
> > 	len = strscpy(v->card, tea->card, sizeof(v->card) - sizeof(TEA5777) + 1);
> 
> I can answer this one: bugs (particularly, off by one) are quite prone
> in such code.
> 
> From the Authors of strlcat(3), you can look at OpenBSD's manual page:
> 
> 	Since it is known how many characters were copied the first
> 	time, things can be sped up a bit by using a copy instead of an
> 	append:
> 
> 		char *dir, *file, pname[PATH_MAX];
> 		size_t n;
> 
> 		...
> 
> 		n = strlcpy(pname, dir, sizeof(pname));
> 		if (n >= sizeof(pname))
> 			goto toolong;
> 		if (strlcpy(pname + n, file, sizeof(pname) - n) >= sizeof(pname) - n)
> 			goto toolong;
> 
> 	However, one may question the validity of such optimizations, as
> 	they defeat the whole purpose of strlcpy() and strlcat().  As a
> 	matter of fact, the first version of this manual page got it
> 	wrong.
> 
> > 	if (len < 0)
> > 		goto fail;
> > 	memcpy(v->card + len, TEA5777, sizeof(TEA5777);
> > 
> > What might be useful is a helper that would let you write:
> > 	len = strscpy(v->card, tea->card);
> > 	len = strscpy_at(v->card, len, " TEA5777" [, implied_length]);
> > 	if (len < 0)
> > 		goto fail;
> 
> Indeed, as said in other threads, I have such an API:
> 
> 	p = v->card;
> 	e = ARRAY_END(v->card);
> 	p = stpecpy(p, e, tea->card);
> 	p = stpecpy(p, e, " TEA5777");
> 	if (p == NULL)
> 		goto fail;
> 
> Linus didn't like my function name.  We could indeed call it strscpy_at().

I really just don't want to have bare pointers or lengths being used for
any of these manipulations. strlcat needs to go away because it's part
of the whole C string API that is confused about array vs pointer and
has no sense of handling bounds. Yes, FORTIFY does its best, but between
lack of sane bounds and doing a strlen for every action, it has to go.

And I'd like _fewer_ APIs (I realize that didn't happen at all for
seq_buf, but that our current best direction). And I especially don't
want a "len" getting passed around. It needs bounds to be *in* the data
type. And that means either true arrays, or internal bounds tracking
(like seq_buf), and it shouldn't have "extra work" associated with it
(e.g. stpecpy above still has to find the current end of the C string).
C strings were a mistake, but here we are.

So, for "easy" constructions, sprintf-family, and for complex
constructions, seq_buf. And probably the large bit of thought at each
place where it is possible, is "how should truncation be communicated?"

-Kees

-- 
Kees Cook

  reply	other threads:[~2026-11-12 17:54 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-06  7:46 Mariia Nikitash
2026-11-06 10:39 ` Alejandro Colomar
2026-11-07  4:31   ` Mariia Nikitash
2026-11-07  7:39     ` Alejandro Colomar
2026-11-08 21:02 ` David Laight
2026-11-08 21:25   ` Alejandro Colomar
2026-11-12 17:54     ` Kees Cook [this message]
2026-11-10 10:08 ` Hans Verkuil
2026-11-10 13:54   ` David Laight
2026-11-10 18:41     ` Mariia Nikitash
2026-11-10 20:58       ` David Laight

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=202611120947.AC428BE4B@keescook \
    --to=kees@kernel.org \
    --cc=alx+linux-hardening@kernel.org \
    --cc=david.laight.linux@gmail.com \
    --cc=hverkuil@kernel.org \
    --cc=justinstitt@google.com \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=morbo@google.com \
    --cc=nikitash.mariiaw@gmail.com \
    /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®