]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
tcp: Replace strncpy() with strscpy()
authorKees Cook <kees@kernel.org>
Sun, 14 Jul 2024 04:11:15 +0000 (21:11 -0700)
committerJakub Kicinski <kuba@kernel.org>
Tue, 16 Jul 2024 14:52:15 +0000 (07:52 -0700)
Replace the deprecated[1] uses of strncpy() in tcp_ca_get_name_by_key()
and tcp_get_default_congestion_control(). The callers use the results as
standard C strings (via nla_put_string() and proc handlers respectively),
so trailing padding is not needed.

Since passing the destination buffer arguments decays it to a pointer,
the size can't be trivially determined by the compiler. ca->name is
the same length in both cases, 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
Signed-off-by: Kees Cook <kees@kernel.org>
Reviewed-by: Simon Horman <horms@kernel.org>
Reviewed-by: Eric Dumazet <edumazet@google.com>
Link: https://patch.msgid.link/20240714041111.it.918-kees@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
net/ipv4/tcp_cong.c

index 48617d99abb0d4be003913e40d7a69d95db0138d..0306d257fa6461e1b5e0c3edf0a1173c7d819b4b 100644 (file)
@@ -202,9 +202,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;
@@ -337,7 +338,7 @@ void tcp_get_default_congestion_control(struct net *net, char *name)
 
        rcu_read_lock();
        ca = rcu_dereference(net->ipv4.tcp_congestion_control);
-       strncpy(name, ca->name, TCP_CA_NAME_MAX);
+       strscpy(name, ca->name, TCP_CA_NAME_MAX);
        rcu_read_unlock();
 }