mfd: db8500-prcmu: Replace deprecated strncpy with strscpy
authorJustin Stitt <justinstitt@google.com>
Wed, 27 Sep 2023 05:10:54 +0000 (05:10 +0000)
committerLee Jones <lee@kernel.org>
Wed, 1 Nov 2023 10:02:14 +0000 (10:02 +0000)
`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 project_name to be NUL-terminated based on its use with
pr_info:
|  pr_info("PRCMU firmware: %s(%d), version %d.%d.%d\n",
|  fw_info.version.project_name,
|  fw_info.version.project,
|  fw_info.version.api_version,
|  fw_info.version.func_version,
|  fw_info.version.errata);

Moreover, NUL-padding does not seem to be needed.

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

Let's also change `PRCMU_FW_PROJECT_NAME_LEN` to just
sizeof(fw_info.version.project_name) as this is more idiomatic strscpy
usage.

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: Linus Walleij <linus.walleij@linaro.org>
Link: https://lore.kernel.org/r/20230927-strncpy-drivers-mfd-db8500-prcmu-c-v1-1-db9693f92a68@google.com
Signed-off-by: Lee Jones <lee@kernel.org>
drivers/mfd/db8500-prcmu.c

index 27a881da4d6e63523e1942b65ec4a21429fffcac..5b3e355e78f6b2040924f011c2a385a1016e36ad 100644 (file)
@@ -2639,9 +2639,9 @@ static void dbx500_fw_version_init(struct device_node *np)
        fw_info.version.api_version = (version >> 8) & 0xFF;
        fw_info.version.func_version = (version >> 16) & 0xFF;
        fw_info.version.errata = (version >> 24) & 0xFF;
-       strncpy(fw_info.version.project_name,
+       strscpy(fw_info.version.project_name,
                fw_project_name(fw_info.version.project),
-               PRCMU_FW_PROJECT_NAME_LEN);
+               sizeof(fw_info.version.project_name));
        fw_info.valid = true;
        pr_info("PRCMU firmware: %s(%d), version %d.%d.%d\n",
                fw_info.version.project_name,