accel/habanalabs: refactor deprecated strncpy to strscpy_pad
authorJustin Stitt <justinstitt@google.com>
Fri, 25 Aug 2023 22:09:51 +0000 (22:09 +0000)
committerOded Gabbay <ogabbay@kernel.org>
Mon, 9 Oct 2023 09:37:20 +0000 (12:37 +0300)
commita45d5cf09d49ed46c1cf1150ed5a891878a161f3
treec997910c4ffd485ed1ce3d6b4d15c1ce640c36dd
parent428f6882a6f9fc0521eb2dca5293436d5a041ff3
accel/habanalabs: refactor deprecated strncpy to strscpy_pad

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

We see that `prop->cpucp_info.card_name` is supposed to be
NUL-terminated based on its usage within `__hwmon_device_register()`
(wherein it's called "name"):
| if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
| dev_warn(dev,
|  "hwmon: '%s' is not a valid name attribute, please fix\n",
|  name);

A suitable replacement is `strscpy_pad` [2] due to the fact that it
guarantees both NUL-termination and NUL-padding on its destination
buffer.

NUL-padding on `prop->cpucp_info.card_name` is not strictly necessary as
`hdev->prop` is explicitly zero-initialized but should be used
regardless as it gets copied out to userspace directly -- as per Kees'
suggestion.

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>
Suggested-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Oded Gabbay <ogabbay@kernel.org>
drivers/accel/habanalabs/gaudi/gaudi.c
drivers/accel/habanalabs/gaudi2/gaudi2.c
drivers/accel/habanalabs/goya/goya.c