ACPI: OSI: refactor deprecated strncpy()
authorJustin Stitt <justinstitt@google.com>
Mon, 11 Sep 2023 20:36:44 +0000 (20:36 +0000)
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>
Thu, 21 Sep 2023 18:55:43 +0000 (20:55 +0200)
`strncpy()` is deprecated for use on NUL-terminated destination strings [1].

We know `osi->string` is a NUL-terminated string due to its eventual use
in `acpi_install_interface()` and `acpi_remove_interface()` which expect
a `acpi_string` which has been specifically typedef'd as:
|  typedef char *acpi_string; /* Null terminated ASCII string */

... and which also has other string functions used on it like `strlen`.
Furthermore, padding is not needed in this instance either.

Due to the reasoning above a suitable replacement is `strscpy` [2] since
it guarantees NUL-termination on the destination buffer and doesn't
unnecessarily NUL-pad.

While there is unlikely to be a buffer overread (or other related bug)
in this case, we should still favor a more robust and less ambiguous
interface.

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
Signed-off-by: Justin Stitt <justinstitt@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
drivers/acpi/osi.c

index d4405e1ca9b9719df5e6bb8ebdde3032357722aa..df9328c850bd33bdd1b2a3112a70a6414863fed5 100644 (file)
@@ -110,7 +110,7 @@ void __init acpi_osi_setup(char *str)
                        break;
                } else if (osi->string[0] == '\0') {
                        osi->enable = enable;
-                       strncpy(osi->string, str, OSI_STRING_LENGTH_MAX);
+                       strscpy(osi->string, str, OSI_STRING_LENGTH_MAX);
                        break;
                }
        }