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

staging: greybus: Check for string truncation instead of NUL-termination

Commit 18f44de63f88 ("staging: greybus: change strncpy() to
strscpy_pad()") didn't remove the now unnecessary NUL-termination
checks. Unlike strncpy(), strscpy_pad() guarantees that the destination
buffer is NUL-terminated, making these checks obsolete. Remove them and
check for string truncation instead.

Compile-tested only.

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
Link: https://lore.kernel.org/r/20250402115755.1929043-1-thorsten.blum@linux.dev
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Thorsten Blum and committed by
Greg Kroah-Hartman
c511db3f d486f2e0

+17 -31
+17 -31
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 - strscpy_pad(fw_info->firmware_tag, response.firmware_tag); 127 - 128 - /* 129 - * The firmware-tag should be NULL terminated, otherwise throw error but 130 - * don't fail. 131 - */ 132 - if (fw_info->firmware_tag[GB_FIRMWARE_TAG_MAX_SIZE - 1] != '\0') { 126 + ret = strscpy_pad(fw_info->firmware_tag, response.firmware_tag); 127 + if (ret == -E2BIG) 133 128 dev_err(fw_mgmt->parent, 134 - "fw-version: firmware-tag is not NULL terminated\n"); 135 - fw_info->firmware_tag[GB_FIRMWARE_TAG_MAX_SIZE - 1] = '\0'; 136 - } 129 + "fw-version: truncated firmware tag: %s\n", 130 + fw_info->firmware_tag); 137 131 138 132 return 0; 139 133 } ··· 146 152 } 147 153 148 154 request.load_method = load_method; 149 - strscpy_pad(request.firmware_tag, tag); 150 155 151 - /* 152 - * The firmware-tag should be NULL terminated, otherwise throw error and 153 - * fail. 154 - */ 155 - if (request.firmware_tag[GB_FIRMWARE_TAG_MAX_SIZE - 1] != '\0') { 156 - dev_err(fw_mgmt->parent, "load-and-validate: firmware-tag is not NULL terminated\n"); 156 + ret = strscpy_pad(request.firmware_tag, tag); 157 + if (ret == -E2BIG) { 158 + dev_err(fw_mgmt->parent, 159 + "load-and-validate: truncated firmware tag: %s\n", 160 + request.firmware_tag); 157 161 return -EINVAL; 158 162 } 159 163 ··· 240 248 struct gb_fw_mgmt_backend_fw_version_response response; 241 249 int ret; 242 250 243 - strscpy_pad(request.firmware_tag, fw_info->firmware_tag); 244 - 245 - /* 246 - * The firmware-tag should be NULL terminated, otherwise throw error and 247 - * fail. 248 - */ 249 - if (request.firmware_tag[GB_FIRMWARE_TAG_MAX_SIZE - 1] != '\0') { 250 - dev_err(fw_mgmt->parent, "backend-version: firmware-tag is not NULL terminated\n"); 251 + ret = strscpy_pad(request.firmware_tag, fw_info->firmware_tag); 252 + if (ret == -E2BIG) { 253 + dev_err(fw_mgmt->parent, 254 + "backend-fw-version: truncated firmware tag: %s\n", 255 + request.firmware_tag); 251 256 return -EINVAL; 252 257 } 253 258 ··· 291 302 int ret; 292 303 293 304 ret = strscpy_pad(request.firmware_tag, tag); 294 - 295 - /* 296 - * The firmware-tag should be NULL terminated, otherwise throw error and 297 - * fail. 298 - */ 299 305 if (ret == -E2BIG) { 300 - dev_err(fw_mgmt->parent, "backend-update: firmware-tag is not NULL terminated\n"); 306 + dev_err(fw_mgmt->parent, 307 + "backend-fw-update: truncated firmware tag: %s\n", 308 + request.firmware_tag); 301 309 return -EINVAL; 302 310 } 303 311