linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] nvme-fc: replace deprecated strncpy with strscpy
@ 2023-10-19 21:34 Justin Stitt
  2023-10-19 23:20 ` Kees Cook
  2023-11-30 22:01 ` Kees Cook
  0 siblings, 2 replies; 3+ messages in thread
From: Justin Stitt @ 2023-10-19 21:34 UTC (permalink / raw)
  To: James Smart, Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg
  Cc: linux-nvme, linux-kernel, linux-hardening, Justin Stitt

strncpy() is deprecated for use on NUL-terminated destination strings
[1] and as such we should prefer more robust and less ambiguous string
interfaces.

Let's instead use strscpy() [2] as it guarantees NUL-termination on the
destination buffer.

Moreover, there is no need to use:

|       min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));

I imagine this was originally done to make sure the destination buffer
is NUL-terminated by ensuring we copy a number of bytes less than the
size of our destination, thus leaving some NUL-bytes at the end.

However, with strscpy(), we no longer need to do this and we can instead
opt for the more idiomatic strscpy() usage of:

| strscpy(dest, src, sizeof(dest))

Also, no NUL-padding is required as lsop is zero-allocated:

|       lsop = kzalloc((sizeof(*lsop) +
|                        sizeof(*assoc_rqst) + sizeof(*assoc_acc) +
|                        ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);

... and assoc_rqst points to a field in lsop:

|       assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)&lsop[1];

Therefore, any additional NUL-byte assignments (like the ones that
strncpy() makes) are redundant.

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>
Similar-to: https://lore.kernel.org/all/20231018-strncpy-drivers-nvme-host-fabrics-c-v1-1-b6677df40a35@google.com/
---
Note: build-tested only.
---
 drivers/nvme/host/fc.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/nvme/host/fc.c b/drivers/nvme/host/fc.c
index a15b37750d6e..680585253f7c 100644
--- a/drivers/nvme/host/fc.c
+++ b/drivers/nvme/host/fc.c
@@ -1218,10 +1218,10 @@ nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
 	/* Linux supports only Dynamic controllers */
 	assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
 	uuid_copy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id);
-	strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
-		min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));
-	strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
-		min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE));
+	strscpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
+		sizeof(assoc_rqst->assoc_cmd.hostnqn));
+	strscpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
+		sizeof(assoc_rqst->assoc_cmd.subnqn));
 
 	lsop->queue = queue;
 	lsreq->rqstaddr = assoc_rqst;

---
base-commit: dab3e01664eaddae965699f1fec776609db0ea9d
change-id: 20231019-strncpy-drivers-nvme-host-fc-c-a542b34b8710

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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] nvme-fc: replace deprecated strncpy with strscpy
  2023-10-19 21:34 [PATCH] nvme-fc: replace deprecated strncpy with strscpy Justin Stitt
@ 2023-10-19 23:20 ` Kees Cook
  2023-11-30 22:01 ` Kees Cook
  1 sibling, 0 replies; 3+ messages in thread
From: Kees Cook @ 2023-10-19 23:20 UTC (permalink / raw)
  To: Justin Stitt
  Cc: James Smart, Keith Busch, Jens Axboe, Christoph Hellwig,
	Sagi Grimberg, linux-nvme, linux-kernel, linux-hardening

On Thu, Oct 19, 2023 at 09:34:35PM +0000, Justin Stitt 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.
> 
> Let's instead use strscpy() [2] as it guarantees NUL-termination on the
> destination buffer.
> 
> Moreover, there is no need to use:
> 
> |       min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));
> 
> I imagine this was originally done to make sure the destination buffer
> is NUL-terminated by ensuring we copy a number of bytes less than the
> size of our destination, thus leaving some NUL-bytes at the end.

Yeah, this is odd, but I agree that the resulting strscpy does the
intended copy, since we've seen that other nqn strings are expected to
be %NUL terminated.

> 
> However, with strscpy(), we no longer need to do this and we can instead
> opt for the more idiomatic strscpy() usage of:
> 
> | strscpy(dest, src, sizeof(dest))
> 
> Also, no NUL-padding is required as lsop is zero-allocated:
> 
> |       lsop = kzalloc((sizeof(*lsop) +
> |                        sizeof(*assoc_rqst) + sizeof(*assoc_acc) +
> |                        ctrl->lport->ops->lsrqst_priv_sz), GFP_KERNEL);
> 
> ... and assoc_rqst points to a field in lsop:
> 
> |       assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)&lsop[1];
> 
> Therefore, any additional NUL-byte assignments (like the ones that
> strncpy() makes) are redundant.
> 
> 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>
> Similar-to: https://lore.kernel.org/all/20231018-strncpy-drivers-nvme-host-fabrics-c-v1-1-b6677df40a35@google.com/

Reviewed-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] nvme-fc: replace deprecated strncpy with strscpy
  2023-10-19 21:34 [PATCH] nvme-fc: replace deprecated strncpy with strscpy Justin Stitt
  2023-10-19 23:20 ` Kees Cook
@ 2023-11-30 22:01 ` Kees Cook
  1 sibling, 0 replies; 3+ messages in thread
From: Kees Cook @ 2023-11-30 22:01 UTC (permalink / raw)
  To: James Smart, Keith Busch, Jens Axboe, Christoph Hellwig,
	Sagi Grimberg, Justin Stitt
  Cc: Kees Cook, linux-nvme, linux-kernel, linux-hardening

On Thu, 19 Oct 2023 21:34:35 +0000, Justin Stitt 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.
> 
> Let's instead use strscpy() [2] as it guarantees NUL-termination on the
> destination buffer.
> 
> [...]

Applied to for-next/hardening, thanks!

[1/1] nvme-fc: replace deprecated strncpy with strscpy
      https://git.kernel.org/kees/c/f62cacf390b7

Take care,

-- 
Kees Cook


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-11-30 22:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-19 21:34 [PATCH] nvme-fc: replace deprecated strncpy with strscpy Justin Stitt
2023-10-19 23:20 ` Kees Cook
2023-11-30 22:01 ` Kees Cook

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