Skip to content

Commit a3bfc09

Browse files
keeskuba-moo
authored andcommitted
tcp: Replace strncpy() with strscpy()
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: KSPP#90 [1] 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>
1 parent b49991d commit a3bfc09

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

net/ipv4/tcp_cong.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,10 @@ char *tcp_ca_get_name_by_key(u32 key, char *buffer)
202202

203203
rcu_read_lock();
204204
ca = tcp_ca_find_key(key);
205-
if (ca)
206-
ret = strncpy(buffer, ca->name,
207-
TCP_CA_NAME_MAX);
205+
if (ca) {
206+
strscpy(buffer, ca->name, TCP_CA_NAME_MAX);
207+
ret = buffer;
208+
}
208209
rcu_read_unlock();
209210

210211
return ret;
@@ -337,7 +338,7 @@ void tcp_get_default_congestion_control(struct net *net, char *name)
337338

338339
rcu_read_lock();
339340
ca = rcu_dereference(net->ipv4.tcp_congestion_control);
340-
strncpy(name, ca->name, TCP_CA_NAME_MAX);
341+
strscpy(name, ca->name, TCP_CA_NAME_MAX);
341342
rcu_read_unlock();
342343
}
343344

0 commit comments

Comments
 (0)