Skip to content

Commit

Permalink
qla2x00t-32gbit: Avoid possible run-time warning with long model_num
Browse files Browse the repository at this point in the history
The prior strlcpy() replacement of strncpy() here (which was
later replaced with strscpy()) expected pinfo->model_num (and
pinfo->model_description) to be NUL-terminated, but it is possible
it was not, as the code pattern here shows vha->hw->model_number (and
vha->hw->model_desc) being exactly 1 character larger, and the replaced
strncpy() was copying only up to the size of the source character
array. Replace this with memtostr(), which is the unambiguous way to
convert a maybe not-NUL-terminated character array into a NUL-terminated
string.

Fixes: 527e9b704c3d ("scsi: qla2xxx: Use memcpy() and strlcpy() instead of strcpy() and strncpy()")
Reviewed-by: Martin K. Petersen <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Kees Cook <[email protected]>
[ commit c3408c4ae041 upstream ]
  • Loading branch information
lnocturno committed Jul 15, 2024
1 parent d776e70 commit ceba304
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
6 changes: 2 additions & 4 deletions qla2x00t-32gbit/qla_mr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1919,10 +1919,8 @@ qlafx00_fx_disc(scsi_qla_host_t *vha, fc_port_t *fcport, uint16_t fx_type)
if (fx_type == FXDISC_GET_CONFIG_INFO) {
struct config_info_data *pinfo =
(struct config_info_data *) fdisc->u.fxiocb.rsp_addr;
strscpy(vha->hw->model_number, pinfo->model_num,
ARRAY_SIZE(vha->hw->model_number));
strscpy(vha->hw->model_desc, pinfo->model_description,
ARRAY_SIZE(vha->hw->model_desc));
memtostr(vha->hw->model_number, pinfo->model_num);
memtostr(vha->hw->model_desc, pinfo->model_description);
memcpy(&vha->hw->mr.symbolic_name, pinfo->symbolic_name,
sizeof(vha->hw->mr.symbolic_name));
memcpy(&vha->hw->mr.serial_num, pinfo->serial_num,
Expand Down
19 changes: 19 additions & 0 deletions scst/include/backport.h
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,25 @@ static inline ssize_t strscpy(char *dest, const char *src, size_t count)
}
#endif

#ifndef memtostr
/*
* See also commit 0efc5990bca5 ("string.h: Introduce memtostr() and memtostr_pad()") # v6.10.
*/
#define memtostr(dest, src) do { \
const size_t _dest_len = __builtin_object_size(dest, 1); \
const size_t _src_len = __builtin_object_size(src, 1); \
const size_t _src_chars = strnlen(src, _src_len); \
const size_t _copy_len = min(_dest_len - 1, _src_chars); \
\
BUILD_BUG_ON(!__builtin_constant_p(_dest_len) || \
!__builtin_constant_p(_src_len) || \
_dest_len == 0 || _dest_len == (size_t)-1 || \
_src_len == 0 || _src_len == (size_t)-1); \
memcpy(dest, src, _copy_len); \
dest[_copy_len] = '\0'; \
} while (0)
#endif

/* <linux/sysfs.h> */

#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0) && \
Expand Down

0 comments on commit ceba304

Please sign in to comment.