strncpy() is deprecated [1] and as such we should use different apis to
copy string data.

We can see that ct is NUL-initialized with fc_ct_hdr_fill:
|       ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rspn) + len,
...

In fc_ct_hdr_fill():
|       memset(ct, 0, ct_plen);

We also calculate the length of the source string:
|       len = strnlen(fc_host_symbolic_name(lport->host), 255);

then this argument is used in strncpy(), which is bad because the
pattern of (dest, src, strlen(src)) usually leaves the destination
buffer without NUL-termination. However, this seems to be correct in
this instance since fr_name is part of a seq_buf-like structure
monitoring the length:
|       struct fc_ns_rspn {
|               struct fc_ns_fid fr_fid;        /* port ID object */
|               __u8            fr_name_len;
|               char            fr_name[];
|       } __attribute__((__packed__));

So, this is really just a byte copy into a length-bounded string. Let's
use memcpy() and mark the buffers as __nonstring.

Link: 
https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings
 [1]
Link: https://github.com/KSPP/linux/issues/90
Cc: [email protected]
Signed-off-by: Justin Stitt <[email protected]>
---
Note: build-tested only.

Found with: $ rg "strncpy\("
---
 drivers/scsi/libfc/fc_encode.h | 8 ++++----
 include/uapi/scsi/fc/fc_ns.h   | 6 +++---
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/drivers/scsi/libfc/fc_encode.h b/drivers/scsi/libfc/fc_encode.h
index 7dcac3b6baa7..ba8cc4ee650c 100644
--- a/drivers/scsi/libfc/fc_encode.h
+++ b/drivers/scsi/libfc/fc_encode.h
@@ -140,8 +140,8 @@ static inline int fc_ct_ns_fill(struct fc_lport *lport,
                ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rspn) + len,
                                    FC_FST_DIR, FC_NS_SUBTYPE);
                hton24(ct->payload.spn.fr_fid.fp_fid, lport->port_id);
-               strncpy(ct->payload.spn.fr_name,
-                       fc_host_symbolic_name(lport->host), len);
+               memcpy(ct->payload.spn.fr_name,
+                      fc_host_symbolic_name(lport->host), len);
                ct->payload.spn.fr_name_len = len;
                break;
 
@@ -150,8 +150,8 @@ static inline int fc_ct_ns_fill(struct fc_lport *lport,
                ct = fc_ct_hdr_fill(fp, op, sizeof(struct fc_ns_rsnn) + len,
                                    FC_FST_DIR, FC_NS_SUBTYPE);
                put_unaligned_be64(lport->wwnn, &ct->payload.snn.fr_wwn);
-               strncpy(ct->payload.snn.fr_name,
-                       fc_host_symbolic_name(lport->host), len);
+               memcpy(ct->payload.snn.fr_name,
+                      fc_host_symbolic_name(lport->host), len);
                ct->payload.snn.fr_name_len = len;
                break;
 
diff --git a/include/uapi/scsi/fc/fc_ns.h b/include/uapi/scsi/fc/fc_ns.h
index 4cf0a40a099a..8a6f6c6b5213 100644
--- a/include/uapi/scsi/fc/fc_ns.h
+++ b/include/uapi/scsi/fc/fc_ns.h
@@ -145,7 +145,7 @@ struct fc_gid_pn_resp {
  */
 struct fc_gspn_resp {
        __u8    fp_name_len;
-       char    fp_name[];
+       char    fp_name[] __nonstring;
 };
 
 /*
@@ -171,7 +171,7 @@ struct fc_ns_rn_id {
 struct fc_ns_rsnn {
        __be64          fr_wwn;         /* node name */
        __u8            fr_name_len;
-       char            fr_name[];
+       char            fr_name[] __nonstring;
 } __attribute__((__packed__));
 
 /*
@@ -180,7 +180,7 @@ struct fc_ns_rsnn {
 struct fc_ns_rspn {
        struct fc_ns_fid fr_fid;        /* port ID object */
        __u8            fr_name_len;
-       char            fr_name[];
+       char            fr_name[] __nonstring;
 } __attribute__((__packed__));
 
 /*

---
base-commit: ffc253263a1375a65fa6c9f62a893e9767fbebfa
change-id: 20231030-strncpy-drivers-scsi-libfc-fc_encode-h-5ad629f6de9c

Best regards,
--
Justin Stitt <[email protected]>


Reply via email to