tty: n_gsm: replace deprecated strncpy with strscpy
authorJustin Stitt <justinstitt@google.com>
Mon, 18 Mar 2024 23:02:12 +0000 (23:02 +0000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 9 Apr 2024 13:49:04 +0000 (15:49 +0200)
strncpy() is deprecated for use on NUL-terminated destination strings
[1] and as such we should prefer more robust and less ambiguous string
interfaces.

We expect nc->if_name to be NUL-terminated based on existing manual
NUL-byte assignments and checks:
| nc.if_name[IFNAMSIZ-1] = '\0';
...
|  if (nc->if_name[0] != '\0')

Let's use the new 2-argument strscpy() since it guarantees
NUL-termination on the destination buffer while correctly using the
destination buffers size to bound the operation.

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings
Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html
Link: https://github.com/KSPP/linux/issues/90
Cc: linux-hardening@vger.kernel.org
Signed-off-by: Justin Stitt <justinstitt@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20240318-strncpy-drivers-tty-n_gsm-c-v1-1-da37a07c642e@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/tty/n_gsm.c

index 4036566febcba50fd8070249ae515f4456df8d60..f5b0d91d32a7ed6d9a99fe89e559bcaf539cb8ce 100644 (file)
@@ -4010,7 +4010,7 @@ static int gsm_create_network(struct gsm_dlci *dlci, struct gsm_netconfig *nc)
        mux_net = netdev_priv(net);
        mux_net->dlci = dlci;
        kref_init(&mux_net->ref);
-       strncpy(nc->if_name, net->name, IFNAMSIZ); /* return net name */
+       strscpy(nc->if_name, net->name); /* return net name */
 
        /* reconfigure dlci for network */
        dlci->prev_adaption = dlci->adaption;