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

staging: greybus: change strncpy() to strscpy_pad()

gcc-10 warns about a strncpy() that does not enforce zero-termination:

In file included from include/linux/string.h:369,
from drivers/staging/greybus/fw-management.c:9:
In function 'strncpy',
inlined from 'fw_mgmt_backend_fw_update_operation' at drivers/staging/greybus/fw-management.c:306:2:
include/linux/fortify-string.h:108:30: error: '__builtin_strncpy' specified bound 10 equals destination size [-Werror=stringop-truncation]
108 | #define __underlying_strncpy __builtin_strncpy
| ^
include/linux/fortify-string.h:187:9: note: in expansion of macro '__underlying_strncpy'
187 | return __underlying_strncpy(p, q, size);
| ^~~~~~~~~~~~~~~~~~~~

For some reason, I cannot reproduce this with gcc-9 or gcc-11, and I only
get a warning for one of the four related strncpy()s, so I'm not
sure what's going on.

Change all four to strscpy_pad(), which is the safest replacement here,
as it avoids ending up with uninitialized stack data in the tag name.

Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Reviewed-by: Dan Carpenter <dan.carpenter@linaro.org>
Reviewed-by: Justin Stitt <justinstitt@google.com>
Link: https://github.com/KSPP/linux/issues/90 [1]
Link: https://lore.kernel.org/r/20240408194821.3183462-3-arnd@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Arnd Bergmann and committed by
Greg Kroah-Hartman
18f44de6 c3a8f7df

+5 -7
+5 -7
drivers/staging/greybus/fw-management.c
··· 123 123 fw_info->major = le16_to_cpu(response.major); 124 124 fw_info->minor = le16_to_cpu(response.minor); 125 125 126 - strncpy(fw_info->firmware_tag, response.firmware_tag, 127 - GB_FIRMWARE_TAG_MAX_SIZE); 126 + strscpy_pad(fw_info->firmware_tag, response.firmware_tag); 128 127 129 128 /* 130 129 * The firmware-tag should be NULL terminated, otherwise throw error but ··· 152 153 } 153 154 154 155 request.load_method = load_method; 155 - strncpy(request.firmware_tag, tag, GB_FIRMWARE_TAG_MAX_SIZE); 156 + strscpy_pad(request.firmware_tag, tag); 156 157 157 158 /* 158 159 * The firmware-tag should be NULL terminated, otherwise throw error and ··· 248 249 struct gb_fw_mgmt_backend_fw_version_response response; 249 250 int ret; 250 251 251 - strncpy(request.firmware_tag, fw_info->firmware_tag, 252 - GB_FIRMWARE_TAG_MAX_SIZE); 252 + strscpy_pad(request.firmware_tag, fw_info->firmware_tag); 253 253 254 254 /* 255 255 * The firmware-tag should be NULL terminated, otherwise throw error and ··· 301 303 struct gb_fw_mgmt_backend_fw_update_request request; 302 304 int ret; 303 305 304 - strncpy(request.firmware_tag, tag, GB_FIRMWARE_TAG_MAX_SIZE); 306 + ret = strscpy_pad(request.firmware_tag, tag); 305 307 306 308 /* 307 309 * The firmware-tag should be NULL terminated, otherwise throw error and 308 310 * fail. 309 311 */ 310 - if (request.firmware_tag[GB_FIRMWARE_TAG_MAX_SIZE - 1] != '\0') { 312 + if (ret == -E2BIG) { 311 313 dev_err(fw_mgmt->parent, "backend-update: firmware-tag is not NULL terminated\n"); 312 314 return -EINVAL; 313 315 }