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)
`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

index e0f6541..29fb71c 100644 (file)
@@ -664,7 +664,7 @@ static int gaudi_set_fixed_properties(struct hl_device *hdev)
        prop->pcie_dbi_base_address = mmPCIE_DBI_BASE;
        prop->pcie_aux_dbi_reg_addr = CFG_BASE + mmPCIE_AUX_DBI;
 
-       strncpy(prop->cpucp_info.card_name, GAUDI_DEFAULT_CARD_NAME,
+       strscpy_pad(prop->cpucp_info.card_name, GAUDI_DEFAULT_CARD_NAME,
                                        CARD_NAME_MAX_LEN);
 
        prop->max_pending_cs = GAUDI_MAX_PENDING_CS;
@@ -8004,7 +8004,7 @@ static int gaudi_cpucp_info_get(struct hl_device *hdev)
                return rc;
 
        if (!strlen(prop->cpucp_info.card_name))
-               strncpy(prop->cpucp_info.card_name, GAUDI_DEFAULT_CARD_NAME,
+               strscpy_pad(prop->cpucp_info.card_name, GAUDI_DEFAULT_CARD_NAME,
                                CARD_NAME_MAX_LEN);
 
        hdev->card_type = le32_to_cpu(hdev->asic_prop.cpucp_info.card_type);
index d94acec..e53de95 100644 (file)
@@ -2528,7 +2528,7 @@ static int gaudi2_set_fixed_properties(struct hl_device *hdev)
        prop->pcie_dbi_base_address = CFG_BASE + mmPCIE_DBI_BASE;
        prop->pcie_aux_dbi_reg_addr = CFG_BASE + mmPCIE_AUX_DBI;
 
-       strncpy(prop->cpucp_info.card_name, GAUDI2_DEFAULT_CARD_NAME, CARD_NAME_MAX_LEN);
+       strscpy_pad(prop->cpucp_info.card_name, GAUDI2_DEFAULT_CARD_NAME, CARD_NAME_MAX_LEN);
 
        prop->mme_master_slave_mode = 1;
 
@@ -2981,7 +2981,8 @@ static int gaudi2_cpucp_info_get(struct hl_device *hdev)
        }
 
        if (!strlen(prop->cpucp_info.card_name))
-               strncpy(prop->cpucp_info.card_name, GAUDI2_DEFAULT_CARD_NAME, CARD_NAME_MAX_LEN);
+               strscpy_pad(prop->cpucp_info.card_name, GAUDI2_DEFAULT_CARD_NAME,
+                               CARD_NAME_MAX_LEN);
 
        /* Overwrite binning masks with the actual binning values from F/W */
        hdev->dram_binning = prop->cpucp_info.dram_binning_mask;
index 7c685e6..024ccf2 100644 (file)
@@ -466,7 +466,7 @@ int goya_set_fixed_properties(struct hl_device *hdev)
        prop->pcie_dbi_base_address = mmPCIE_DBI_BASE;
        prop->pcie_aux_dbi_reg_addr = CFG_BASE + mmPCIE_AUX_DBI;
 
-       strncpy(prop->cpucp_info.card_name, GOYA_DEFAULT_CARD_NAME,
+       strscpy_pad(prop->cpucp_info.card_name, GOYA_DEFAULT_CARD_NAME,
                CARD_NAME_MAX_LEN);
 
        prop->max_pending_cs = GOYA_MAX_PENDING_CS;
@@ -5122,7 +5122,7 @@ int goya_cpucp_info_get(struct hl_device *hdev)
        }
 
        if (!strlen(prop->cpucp_info.card_name))
-               strncpy(prop->cpucp_info.card_name, GOYA_DEFAULT_CARD_NAME,
+               strscpy_pad(prop->cpucp_info.card_name, GOYA_DEFAULT_CARD_NAME,
                                CARD_NAME_MAX_LEN);
 
        return 0;