Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

hwmon: (acpi_power_meter) replace open-coded kmemdup_nul

`strncpy` is deprecated for use on NUL-terminated destination strings [1].

Let's refactor this kcalloc() + strncpy() into a kmemdup_nul() which has
more obvious behavior and is less error prone.

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
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/20230926-strncpy-drivers-hwmon-acpi_power_meter-c-v5-1-3fc31a9daf99@google.com
Signed-off-by: Kees Cook <keescook@chromium.org>

authored by

Justin Stitt and committed by
Kees Cook
9cca73d7 fed2ef7a

+2 -3
+2 -3
drivers/hwmon/acpi_power_meter.c
··· 796 796 goto error; 797 797 } 798 798 799 - *str = kcalloc(element->string.length + 1, sizeof(u8), 800 - GFP_KERNEL); 799 + *str = kmemdup_nul(element->string.pointer, element->string.length, 800 + GFP_KERNEL); 801 801 if (!*str) { 802 802 res = -ENOMEM; 803 803 goto error; 804 804 } 805 805 806 - strncpy(*str, element->string.pointer, element->string.length); 807 806 str++; 808 807 } 809 808