All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx+linux-hardening@kernel.org>
To: David Laight <david.laight.linux@gmail.com>
Cc: 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, kees@kernel.org,
	justinstitt@google.com, morbo@google.com
Subject: Re: [PATCH] media: radio: Replace strlcat() with seq_buf in TEA drivers
Date: Sun, 8 Nov 2026 22:25:35 +0100	[thread overview]
Message-ID: <avDmyLYpWOQ2WB-i@debian> (raw)
In-Reply-To: <20261108210232.13800dd3@pumpkin>

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

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().


Have a lovely night!
Alex

> 
> David
> 
> > +
> > +	seq_buf_init(&s, (char *)v->card, sizeof(v->card));
> > +	seq_buf_puts(&s, (const char *)tea->card);
> > +	seq_buf_puts(&s, " TEA5777");
> > +	seq_buf_terminate(&s);
> > +
> >  	strscpy(v->bus_info, tea->bus_info, sizeof(v->bus_info));
> >  	return 0;
> >  }
> > diff --git a/drivers/media/radio/tea575x.c b/drivers/media/radio/tea575x.c
> > index c37315226c42..e49228eb7c46 100644
> > --- a/drivers/media/radio/tea575x.c
> > +++ b/drivers/media/radio/tea575x.c
> > @@ -9,6 +9,7 @@
> >  #include <linux/module.h>
> >  #include <linux/init.h>
> >  #include <linux/slab.h>
> > +#include <linux/seq_buf.h>
> >  #include <linux/sched.h>
> >  #include <asm/io.h>
> >  #include <media/v4l2-device.h>
> > @@ -221,10 +222,15 @@ static int vidioc_querycap(struct file *file, void  *priv,
> >  					struct v4l2_capability *v)
> >  {
> >  	struct snd_tea575x *tea = video_drvdata(file);
> > +	struct seq_buf s;
> >  
> >  	strscpy(v->driver, tea->v4l2_dev->name, sizeof(v->driver));
> > -	strscpy(v->card, tea->card, sizeof(v->card));
> > -	strlcat(v->card, tea->tea5759 ? " TEA5759" : " TEA5757", sizeof(v->card));
> > +
> > +	seq_buf_init(&s, (char *)v->card, sizeof(v->card));
> > +	seq_buf_puts(&s, (const char *)tea->card);
> > +	seq_buf_puts(&s, tea->tea5759 ? " TEA5759" : " TEA5757");
> > +	seq_buf_terminate(&s);
> > +
> >  	strscpy(v->bus_info, tea->bus_info, sizeof(v->bus_info));
> >  	return 0;
> >  }
> 
> 

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

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

  reply	other threads:[~2026-11-08 21:25 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-06  7:46 [PATCH] media: radio: Replace strlcat() with seq_buf in TEA drivers 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 [this message]
2026-11-12 17:54     ` Kees Cook
2026-11-12 19:07       ` David Laight
2026-11-12 20:31         ` Kees Cook
2026-11-12 22:07       ` Alejandro Colomar
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=avDmyLYpWOQ2WB-i@debian \
    --to=alx+linux-hardening@kernel.org \
    --cc=david.laight.linux@gmail.com \
    --cc=hverkuil@kernel.org \
    --cc=justinstitt@google.com \
    --cc=kees@kernel.org \
    --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 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.