diff mbox series

scsi: lpfc: replace deprecated strncpy with strscpy

Message ID 20240222-strncpy-drivers-scsi-lpfc-lpfc_ct-c-v1-1-20c685bd1b43@google.com
State New
Headers show
Series scsi: lpfc: replace deprecated strncpy with strscpy | expand

Commit Message

Justin Stitt Feb. 22, 2024, 12:41 a.m. UTC
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(-)


---
base-commit: 39133352cbed6626956d38ed72012f49b0421e7b
change-id: 20240222-strncpy-drivers-scsi-lpfc-lpfc_ct-c-f54b67eeeb68

Best regards,
--
Justin Stitt <justinstitt@google.com>

Comments

Justin Stitt Feb. 23, 2024, 8:02 p.m. UTC | #1
Hi,

On Wed, Feb 21, 2024 at 6:38 PM Kees Cook <kees@kernel.org> wrote:
>
>
>
> 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...

Am I putting too much faith in this comment?

static inline int lpfc_fdmi_set_attr_string(void *attr, uint16_t
attrtype, char *attrstring)
...
/*
* We are trusting the caller that if a fdmi string field
* is capped at 64 bytes, the caller passes in a string of
* 64 bytes or less.
*/
...

I see lpfc_fdmi_hba_attr_os_ver() calls lpfc_fdmi_set_attr_string()
with an attrstring sized at 256 bytes:
char buf[256] = { 0 };

Can we really return -E2BIG from strscpy() if the dest buffer is the
same size as the source buffer?

I'm happy to just make the standard strncpy -> strscpy replacement and
drop the len assignment. Let me know what you think, Kees.

>
> -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

Thanks
Justin
Kees Cook Feb. 23, 2024, 11:44 p.m. UTC | #2
On Fri, Feb 23, 2024 at 12:02:22PM -0800, Justin Stitt wrote:
> Hi,
> 
> On Wed, Feb 21, 2024 at 6:38 PM Kees Cook <kees@kernel.org> wrote:
> >
> >
> >
> > 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...
> 
> Am I putting too much faith in this comment?
> 
> static inline int lpfc_fdmi_set_attr_string(void *attr, uint16_t
> attrtype, char *attrstring)
> ...
> /*
> * We are trusting the caller that if a fdmi string field
> * is capped at 64 bytes, the caller passes in a string of
> * 64 bytes or less.
> */

This comment is clearly wrong, given lpfc_fdmi_hba_attr_os_ver(). :)
But I feel like I'm misunderstanding it since it was added by the same
commit that added the 256-byte callers, commit 045c58c87560 ("scsi:
lpfc: Rework FDMI attribute registration for unintential padding")

> 
> I see lpfc_fdmi_hba_attr_os_ver() calls lpfc_fdmi_set_attr_string()
> with an attrstring sized at 256 bytes:
> char buf[256] = { 0 };
> 
> Can we really return -E2BIG from strscpy() if the dest buffer is the
> same size as the source buffer?

I see my confusion: I didn't check the size of ae->value_string, which I
assumed was 64 bytes. But it's 256, so in theory we can't overflow.

> I'm happy to just make the standard strncpy -> strscpy replacement and
> drop the len assignment. Let me know what you think, Kees.

For robustness, let's leave the strlen() in place...
diff mbox series

Patch

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));
+	/* 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;