[PATCH] tty: n_gsm: replace deprecated strncpy with strscpy

From: Justin Stitt
Date: Mon Mar 18 2024 - 19:02:24 EST


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 [1]
Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [2]
Link: https://github.com/KSPP/linux/issues/90
Cc: linux-hardening@xxxxxxxxxxxxxxx
Signed-off-by: Justin Stitt <justinstitt@xxxxxxxxxx>
---
Note: build-tested only.

Found with: $ rg "strncpy\("
---
drivers/tty/n_gsm.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c
index 4036566febcb..f5b0d91d32a7 100644
--- a/drivers/tty/n_gsm.c
+++ b/drivers/tty/n_gsm.c
@@ -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;

---
base-commit: bf3a69c6861ff4dc7892d895c87074af7bc1c400
change-id: 20240318-strncpy-drivers-tty-n_gsm-c-ab1336e0e196

Best regards,
--
Justin Stitt <justinstitt@xxxxxxxxxx>