public inbox for linux-scsi@vger.kernel.org
 help / color / mirror / Atom feed
From: James Bottomley <James.Bottomley@HansenPartnership.com>
To: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
Cc: Anil Gurumurthy <anil.gurumurthy@qlogic.com>,
	Sudarsana Kalluru <sudarsana.kalluru@qlogic.com>,
	linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] scsi: bfa: bfad_attr.c:  Cleaning up missing null-terminate after strncpy call
Date: Wed, 04 Jun 2014 14:59:41 -0700	[thread overview]
Message-ID: <1401919181.17510.36.camel@dabdike> (raw)
In-Reply-To: <1401917575-2954-1-git-send-email-rickard_strandqvist@spectrumdigital.se>

On Wed, 2014-06-04 at 23:32 +0200, Rickard Strandqvist wrote:
> Added a guaranteed null-terminate after call to strncpy.
> 
> This was partly found using a static code analysis program called cppcheck.
> 
> Signed-off-by: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
> ---
>  drivers/scsi/bfa/bfad_attr.c |    3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/scsi/bfa/bfad_attr.c b/drivers/scsi/bfa/bfad_attr.c
> index 40be670..06aa3dd 100644
> --- a/drivers/scsi/bfa/bfad_attr.c
> +++ b/drivers/scsi/bfa/bfad_attr.c
> @@ -843,7 +843,8 @@ bfad_im_symbolic_name_show(struct device *dev, struct device_attribute *attr,
>  
>  	bfa_fcs_lport_get_attr(&bfad->bfa_fcs.fabric.bport, &port_attr);
>  	strncpy(symname, port_attr.port_cfg.sym_name.symname,
> -			BFA_SYMNAME_MAXLEN);
> +			sizeof(symname));
> +	symname[sizeof(symname) - 1] = '\0';

So actually, this isn't the correct pattern for this type of potential
problem, where the problem exists, the pattern is to replace strncpy()
with strlcpy() which does a correct null termination on truncation.

In this case I presume your static checker isn't sufficiently clever to
see that there isn't a bug because port_attr.port_cfg.sym_name.symname
is carefully copied to be NULL terminated and always less than
BFA_SYMNAME_MAXLEN, so when copying out of it we can rely on the NULL
termination fitting into BFA_SYMNAME_MAXLEN.

James

  reply	other threads:[~2014-06-04 21:59 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-04 21:32 [PATCH] scsi: bfa: bfad_attr.c: Cleaning up missing null-terminate after strncpy call Rickard Strandqvist
2014-06-04 21:59 ` James Bottomley [this message]
2014-06-06 12:24   ` Rickard Strandqvist

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1401919181.17510.36.camel@dabdike \
    --to=james.bottomley@hansenpartnership.com \
    --cc=anil.gurumurthy@qlogic.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=rickard_strandqvist@spectrumdigital.se \
    --cc=sudarsana.kalluru@qlogic.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox