diff mbox series

wl1251: replace deprecated strncpy with strscpy

Message ID 20231017-strncpy-drivers-net-wireless-ti-wl1251-acx-c-v1-1-a99584e82f65@google.com
State New
Headers show
Series wl1251: replace deprecated strncpy with strscpy | expand

Commit Message

Justin Stitt Oct. 17, 2023, 11:12 p.m. UTC
strncpy() is deprecated for use on NUL-terminated destination strings
[1] and as such we should prefer more robust and less ambiguous string
interfaces.

There was a lot of effort done here to ensure buf is NUL-terminated. We
now have access to better string apis -- `buf` should be NUL-terminated
and doesn't seem to require NUL-padding as its only use is with format
strings in wl1251/main.c
403 | wl1251_info("firmware booted (%s)", wl->fw_ver);

Therefore, a suitable replacement is `strscpy` [2] due to the fact that
it guarantees NUL-termination on the destination buffer without
unnecessarily NUL-padding.

Do note that there is only one caller of wl1251_acx_fw_version() in
drivers/net/wireless/ti/wl1251/boot.c:
264 | wl1251_acx_fw_version(wl, wl->fw_ver, sizeof(wl->fw_ver));
... which passes wl->fw_ver and sizeof(wl->fw_ver) wherein fw_ver is
defined as having size 21 in wl1251.h:
383 | char fw_ver[21];
... and since fw_version has a size of 20 in acx.h:
66 | char fw_version[20];
... there is no overflow or truncation because sizeof(dest) > sizeof(src).

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@vger.kernel.org
Signed-off-by: Justin Stitt <justinstitt@google.com>
---
Note: build-tested only.

Found with: $ rg "strncpy\("
---
 drivers/net/wireless/ti/wl1251/acx.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)


---
base-commit: 58720809f52779dc0f08e53e54b014209d13eebb
change-id: 20231017-strncpy-drivers-net-wireless-ti-wl1251-acx-c-47ebc6ad4435

Best regards,
--
Justin Stitt <justinstitt@google.com>
diff mbox series

Patch

diff --git a/drivers/net/wireless/ti/wl1251/acx.c b/drivers/net/wireless/ti/wl1251/acx.c
index f78fc3880423..ed5733e0dd5e 100644
--- a/drivers/net/wireless/ti/wl1251/acx.c
+++ b/drivers/net/wireless/ti/wl1251/acx.c
@@ -149,15 +149,7 @@  int wl1251_acx_fw_version(struct wl1251 *wl, char *buf, size_t len)
 		goto out;
 	}
 
-	/* be careful with the buffer sizes */
-	strncpy(buf, rev->fw_version, min(len, sizeof(rev->fw_version)));
-
-	/*
-	 * if the firmware version string is exactly
-	 * sizeof(rev->fw_version) long or fw_len is less than
-	 * sizeof(rev->fw_version) it won't be null terminated
-	 */
-	buf[min(len, sizeof(rev->fw_version)) - 1] = '\0';
+	strscpy(buf, rev->fw_version, len);
 
 out:
 	kfree(rev);