netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] net/ipv4: Replace tcp_ca_get_name_by_key()'s strncpy() with strscpy()
@ 2024-07-11 17:16 Kees Cook
  2024-07-11 17:38 ` Eric Dumazet
  0 siblings, 1 reply; 3+ messages in thread
From: Kees Cook @ 2024-07-11 17:16 UTC (permalink / raw)
  To: Eric Dumazet
  Cc: Kees Cook, David S. Miller, David Ahern, Jakub Kicinski,
	Paolo Abeni, netdev, linux-kernel, linux-hardening

Replace the deprecated[1] use of strncpy() in tcp_ca_get_name_by_key().
The only caller passes the results to nla_put_string(), so trailing
padding is not needed.

Since passing "buffer" decays it to a pointer, the size can't be
trivially determined by the compiler. ca->name is the same length,
so strscpy() won't fail (when ca->name is NUL-terminated). Include the
length explicitly instead of using the 2-argument strscpy().

Link: https://github.com/KSPP/linux/issues/90 [1]
Signed-off-by: Kees Cook <kees@kernel.org>
---
Cc: Eric Dumazet <edumazet@google.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: David Ahern <dsahern@kernel.org>
Cc: Jakub Kicinski <kuba@kernel.org>
Cc: Paolo Abeni <pabeni@redhat.com>
Cc: netdev@vger.kernel.org
---
 net/ipv4/tcp_cong.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/net/ipv4/tcp_cong.c b/net/ipv4/tcp_cong.c
index 28ffcfbeef14..2a303a7cba59 100644
--- a/net/ipv4/tcp_cong.c
+++ b/net/ipv4/tcp_cong.c
@@ -203,9 +203,10 @@ char *tcp_ca_get_name_by_key(u32 key, char *buffer)
 
 	rcu_read_lock();
 	ca = tcp_ca_find_key(key);
-	if (ca)
-		ret = strncpy(buffer, ca->name,
-			      TCP_CA_NAME_MAX);
+	if (ca) {
+		strscpy(buffer, ca->name, TCP_CA_NAME_MAX);
+		ret = buffer;
+	}
 	rcu_read_unlock();
 
 	return ret;
-- 
2.34.1


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

* Re: [PATCH] net/ipv4: Replace tcp_ca_get_name_by_key()'s strncpy() with strscpy()
  2024-07-11 17:16 [PATCH] net/ipv4: Replace tcp_ca_get_name_by_key()'s strncpy() with strscpy() Kees Cook
@ 2024-07-11 17:38 ` Eric Dumazet
  2024-07-14  4:09   ` Kees Cook
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Dumazet @ 2024-07-11 17:38 UTC (permalink / raw)
  To: Kees Cook
  Cc: David S. Miller, David Ahern, Jakub Kicinski, Paolo Abeni, netdev,
	linux-kernel, linux-hardening

On Thu, Jul 11, 2024 at 10:16 AM Kees Cook <kees@kernel.org> wrote:
>
> Replace the deprecated[1] use of strncpy() in tcp_ca_get_name_by_key().
> The only caller passes the results to nla_put_string(), so trailing
> padding is not needed.
>
> Since passing "buffer" decays it to a pointer, the size can't be
> trivially determined by the compiler. ca->name is the same length,
> so strscpy() won't fail (when ca->name is NUL-terminated). Include the
> length explicitly instead of using the 2-argument strscpy().
>
> Link: https://github.com/KSPP/linux/issues/90 [1]
> Signed-off-by: Kees Cook <kees@kernel.org>
> ---
> Cc: Eric Dumazet <edumazet@google.com>
> Cc: "David S. Miller" <davem@davemloft.net>
> Cc: David Ahern <dsahern@kernel.org>
> Cc: Jakub Kicinski <kuba@kernel.org>
> Cc: Paolo Abeni <pabeni@redhat.com>
> Cc: netdev@vger.kernel.org
> ---
>  net/ipv4/tcp_cong.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/net/ipv4/tcp_cong.c b/net/ipv4/tcp_cong.c
> index 28ffcfbeef14..2a303a7cba59 100644
> --- a/net/ipv4/tcp_cong.c
> +++ b/net/ipv4/tcp_cong.c
> @@ -203,9 +203,10 @@ char *tcp_ca_get_name_by_key(u32 key, char *buffer)
>
>         rcu_read_lock();
>         ca = tcp_ca_find_key(key);
> -       if (ca)
> -               ret = strncpy(buffer, ca->name,
> -                             TCP_CA_NAME_MAX);
> +       if (ca) {
> +               strscpy(buffer, ca->name, TCP_CA_NAME_MAX);
> +               ret = buffer;
> +       }
>         rcu_read_unlock();
>

Ok, but what about tcp_get_default_congestion_control() ?

Thanks.

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

* Re: [PATCH] net/ipv4: Replace tcp_ca_get_name_by_key()'s strncpy() with strscpy()
  2024-07-11 17:38 ` Eric Dumazet
@ 2024-07-14  4:09   ` Kees Cook
  0 siblings, 0 replies; 3+ messages in thread
From: Kees Cook @ 2024-07-14  4:09 UTC (permalink / raw)
  To: Eric Dumazet
  Cc: David S. Miller, David Ahern, Jakub Kicinski, Paolo Abeni, netdev,
	linux-kernel, linux-hardening

On Thu, Jul 11, 2024 at 10:38:01AM -0700, Eric Dumazet wrote:
> On Thu, Jul 11, 2024 at 10:16 AM Kees Cook <kees@kernel.org> wrote:
> >
> > Replace the deprecated[1] use of strncpy() in tcp_ca_get_name_by_key().
> > The only caller passes the results to nla_put_string(), so trailing
> > padding is not needed.
> >
> > Since passing "buffer" decays it to a pointer, the size can't be
> > trivially determined by the compiler. ca->name is the same length,
> > so strscpy() won't fail (when ca->name is NUL-terminated). Include the
> > length explicitly instead of using the 2-argument strscpy().
> >
> > Link: https://github.com/KSPP/linux/issues/90 [1]
> > Signed-off-by: Kees Cook <kees@kernel.org>
> > ---
> > Cc: Eric Dumazet <edumazet@google.com>
> > Cc: "David S. Miller" <davem@davemloft.net>
> > Cc: David Ahern <dsahern@kernel.org>
> > Cc: Jakub Kicinski <kuba@kernel.org>
> > Cc: Paolo Abeni <pabeni@redhat.com>
> > Cc: netdev@vger.kernel.org
> > ---
> >  net/ipv4/tcp_cong.c | 7 ++++---
> >  1 file changed, 4 insertions(+), 3 deletions(-)
> >
> > diff --git a/net/ipv4/tcp_cong.c b/net/ipv4/tcp_cong.c
> > index 28ffcfbeef14..2a303a7cba59 100644
> > --- a/net/ipv4/tcp_cong.c
> > +++ b/net/ipv4/tcp_cong.c
> > @@ -203,9 +203,10 @@ char *tcp_ca_get_name_by_key(u32 key, char *buffer)
> >
> >         rcu_read_lock();
> >         ca = tcp_ca_find_key(key);
> > -       if (ca)
> > -               ret = strncpy(buffer, ca->name,
> > -                             TCP_CA_NAME_MAX);
> > +       if (ca) {
> > +               strscpy(buffer, ca->name, TCP_CA_NAME_MAX);
> > +               ret = buffer;
> > +       }
> >         rcu_read_unlock();
> >
> 
> Ok, but what about tcp_get_default_congestion_control() ?

Whoops. Yes. I'll do that at the same time. v2 coming...

-- 
Kees Cook

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

end of thread, other threads:[~2024-07-14  4:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-11 17:16 [PATCH] net/ipv4: Replace tcp_ca_get_name_by_key()'s strncpy() with strscpy() Kees Cook
2024-07-11 17:38 ` Eric Dumazet
2024-07-14  4:09   ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).