linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kees Cook <kees@kernel.org>
To: Justin Stitt <justinstitt@google.com>,
	James Smart <james.smart@broadcom.com>,
	Dick Kennedy <dick.kennedy@broadcom.com>,
	"James E.J. Bottomley" <jejb@linux.ibm.com>,
	"Martin K. Petersen" <martin.petersen@oracle.com>
Cc: linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-hardening@vger.kernel.org
Subject: Re: [PATCH] scsi: lpfc: replace deprecated strncpy with strscpy
Date: Wed, 21 Feb 2024 18:37:48 -0800	[thread overview]
Message-ID: <BE9C61AA-633F-4CD1-BE5E-B01DC947626E@kernel.org> (raw)
In-Reply-To: <20240222-strncpy-drivers-scsi-lpfc-lpfc_ct-c-v1-1-20c685bd1b43@google.com>



On February 21, 2024 4:41:52 PM PST, Justin Stitt <justinstitt@google.com> wrote:
>strncpy() is deprecated for use on NUL-terminated destination strings
>[1] and as such we should prefer more robust and less ambiguous string
>interfaces.
>
>We expect ae->value_string to be NUL-terminated because there's a
>comment that says as much; these attr strings are also used with other
>string APIs, further cementing the fact.
>
>Now, the question of whether or not to NUL-pad the destination buffer:
>lpfc_fdmi_rprt_defer() initializes vports (all zero-initialized), then
>we call lpfc_fdmi_cmd() with each vport and a mask. Then, inside of
>lpfc_fdmi_cmd() we check each bit in the mask to invoke the proper
>callback. Importantly, the zero-initialized vport is passed in as the
>"attr" parameter. Seeing this:
>|	struct lpfc_fdmi_attr_string *ae = attr;
>... we can tell that ae->value_string is entirely zero-initialized. Due
>to this, NUL-padding is _not_ required as it would be redundant.
>
>Conveniently, strscpy also returns the number of bytes copied into the
>destination buffer, eliminating the need for strnlen!
>
>Considering the above, a suitable replacement is `strscpy` [2].
>
>Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
>Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [2]
>Link: https://github.com/KSPP/linux/issues/90
>Cc: linux-hardening@vger.kernel.org
>Signed-off-by: Justin Stitt <justinstitt@google.com>
>---
> drivers/scsi/lpfc/lpfc_ct.c | 5 ++---
> 1 file changed, 2 insertions(+), 3 deletions(-)
>
>diff --git a/drivers/scsi/lpfc/lpfc_ct.c b/drivers/scsi/lpfc/lpfc_ct.c
>index baae1f8279e0..42594ec87290 100644
>--- a/drivers/scsi/lpfc/lpfc_ct.c
>+++ b/drivers/scsi/lpfc/lpfc_ct.c
>@@ -2569,9 +2569,8 @@ lpfc_fdmi_set_attr_string(void *attr, uint16_t attrtype, char *attrstring)
> 	 * 64 bytes or less.
> 	 */
> 
>-	strncpy(ae->value_string, attrstring, sizeof(ae->value_string));
>-	len = strnlen(ae->value_string, sizeof(ae->value_string));
>-	/* round string length to a 32bit boundary. Ensure there's a NULL */
>+	len = strscpy(ae->value_string, attrstring, sizeof(ae->value_string));

This could be < 0 on error, and at least lpfc_fdmi_hba_attr_os_ver() may present more than 64 bytes...

-Kees


>+	/* round string length to a 32bit boundary */
> 	len += (len & 3) ? (4 - (len & 3)) : 4;
> 	/* size is Type/Len (4 bytes) plus string length */
> 	size = FOURBYTES + len;
>
>---
>base-commit: 39133352cbed6626956d38ed72012f49b0421e7b
>change-id: 20240222-strncpy-drivers-scsi-lpfc-lpfc_ct-c-f54b67eeeb68
>
>Best regards,
>--
>Justin Stitt <justinstitt@google.com>
>
>

-- 
Kees Cook

  reply	other threads:[~2024-02-22  2:37 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-22  0:41 [PATCH] scsi: lpfc: replace deprecated strncpy with strscpy Justin Stitt
2024-02-22  2:37 ` Kees Cook [this message]
2024-02-23 20:02   ` Justin Stitt
2024-02-23 23:44     ` Kees Cook

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=BE9C61AA-633F-4CD1-BE5E-B01DC947626E@kernel.org \
    --to=kees@kernel.org \
    --cc=dick.kennedy@broadcom.com \
    --cc=james.smart@broadcom.com \
    --cc=jejb@linux.ibm.com \
    --cc=justinstitt@google.com \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.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;
as well as URLs for NNTP newsgroup(s).