Re: [PATCH] scsi: qedf: replace memset/memcpy with safer strscpy

From: Rasmus Villemoes
Date: Fri Apr 12 2019 - 05:36:33 EST


On 12/04/2019 11.05, Colin King wrote:
> From: Colin Ian King <colin.king@xxxxxxxxxxxxx>
>
> Currently the qedf_dbg_* family of functions can overrun the end
> of the source string if it is less than the destination buffer
> length because of the use of a fixed sized memcpy. Replace the
> memset/memcpy calls with the safer strscpy as this won't overrun
> the end of the source string and it ensures the destination
> string is always terminated with NUL.
>
> Addresses-Coverity: ("Out-of-bounds access")
> Fixes: 61d8658b4a43 ("scsi: qedf: Add QLogic FastLinQ offload FCoE driver framework.")
> Signed-off-by: Colin Ian King <colin.king@xxxxxxxxxxxxx>
> ---
> drivers/scsi/qedf/qedf_dbg.c | 12 ++++--------
> 1 file changed, 4 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/scsi/qedf/qedf_dbg.c b/drivers/scsi/qedf/qedf_dbg.c
> index f2397ee9ba69..b491bebeaca9 100644
> --- a/drivers/scsi/qedf/qedf_dbg.c
> +++ b/drivers/scsi/qedf/qedf_dbg.c
> @@ -17,8 +17,7 @@ qedf_dbg_err(struct qedf_dbg_ctx *qedf, const char *func, u32 line,
> struct va_format vaf;
> char nfunc[32];
>
> - memset(nfunc, 0, sizeof(nfunc));
> - memcpy(nfunc, func, sizeof(nfunc) - 1);
> + strscpy(nfunc, func, sizeof(nfunc));

Why not drop the pointless nfunc and stack copying and just pass func as
the %s parameter; these are only called via macros that pass __func__ as
the func argument, and that is always a proper nul-terminated string.

Rasmus