mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] nvmet: prevent sprintf() overflow in nvmet_subsys_nsid_exists()
@ 2024-05-08  7:43 Dan Carpenter
  2024-05-08  7:48 ` Sagi Grimberg
  2024-05-08 13:13 ` Keith Busch
  0 siblings, 2 replies; 3+ messages in thread
From: Dan Carpenter @ 2024-05-08  7:43 UTC (permalink / raw)
  To: Sagi Grimberg
  Cc: Christoph Hellwig, Chaitanya Kulkarni, Keith Busch, linux-nvme,
	linux-kernel, kernel-janitors

The nsid value is a u32 that comes from nvmet_req_find_ns().  It's
endian data and we're on an error path and both of those raise red
flags.  So let's make this safer.

1) Make the buffer large enough for any u32.
2) Remove the unnecessary initialization.
3) Use snprintf() instead of sprintf() for even more safety.
4) The sprintf() function returns the number of bytes printed, not
   counting the NUL terminator. It is impossible for the return value to
   be <= 0 so delete that.

Fixes: 505363957fad ("nvmet: fix nvme status code when namespace is disabled")
Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
---
 drivers/nvme/target/configfs.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
index 89a001101a7f..37cd75ff150e 100644
--- a/drivers/nvme/target/configfs.c
+++ b/drivers/nvme/target/configfs.c
@@ -757,10 +757,9 @@ static struct configfs_attribute *nvmet_ns_attrs[] = {
 bool nvmet_subsys_nsid_exists(struct nvmet_subsys *subsys, u32 nsid)
 {
 	struct config_item *ns_item;
-	char name[4] = {};
+	char name[12];
 
-	if (sprintf(name, "%u", nsid) <= 0)
-		return false;
+	snprintf(name, sizeof(name), "%u", nsid);
 	mutex_lock(&subsys->namespaces_group.cg_subsys->su_mutex);
 	ns_item = config_group_find_item(&subsys->namespaces_group, name);
 	mutex_unlock(&subsys->namespaces_group.cg_subsys->su_mutex);
-- 
2.43.0


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

* Re: [PATCH] nvmet: prevent sprintf() overflow in nvmet_subsys_nsid_exists()
  2024-05-08  7:43 [PATCH] nvmet: prevent sprintf() overflow in nvmet_subsys_nsid_exists() Dan Carpenter
@ 2024-05-08  7:48 ` Sagi Grimberg
  2024-05-08 13:13 ` Keith Busch
  1 sibling, 0 replies; 3+ messages in thread
From: Sagi Grimberg @ 2024-05-08  7:48 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Christoph Hellwig, Chaitanya Kulkarni, Keith Busch, linux-nvme,
	linux-kernel, kernel-janitors



On 08/05/2024 10:43, Dan Carpenter wrote:
> The nsid value is a u32 that comes from nvmet_req_find_ns().  It's
> endian data and we're on an error path and both of those raise red
> flags.  So let's make this safer.
>
> 1) Make the buffer large enough for any u32.
> 2) Remove the unnecessary initialization.
> 3) Use snprintf() instead of sprintf() for even more safety.
> 4) The sprintf() function returns the number of bytes printed, not
>     counting the NUL terminator. It is impossible for the return value to
>     be <= 0 so delete that.
>
> Fixes: 505363957fad ("nvmet: fix nvme status code when namespace is disabled")
> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>

Reviewed-by: Sagi Grimberg <sagi@grimberg.me>

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

* Re: [PATCH] nvmet: prevent sprintf() overflow in nvmet_subsys_nsid_exists()
  2024-05-08  7:43 [PATCH] nvmet: prevent sprintf() overflow in nvmet_subsys_nsid_exists() Dan Carpenter
  2024-05-08  7:48 ` Sagi Grimberg
@ 2024-05-08 13:13 ` Keith Busch
  1 sibling, 0 replies; 3+ messages in thread
From: Keith Busch @ 2024-05-08 13:13 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Sagi Grimberg, Christoph Hellwig, Chaitanya Kulkarni, linux-nvme,
	linux-kernel, kernel-janitors

On Wed, May 08, 2024 at 10:43:04AM +0300, Dan Carpenter wrote:
> The nsid value is a u32 that comes from nvmet_req_find_ns().  It's
> endian data and we're on an error path and both of those raise red
> flags.  So let's make this safer.
> 
> 1) Make the buffer large enough for any u32.
> 2) Remove the unnecessary initialization.
> 3) Use snprintf() instead of sprintf() for even more safety.
> 4) The sprintf() function returns the number of bytes printed, not
>    counting the NUL terminator. It is impossible for the return value to
>    be <= 0 so delete that.
> 
> Fixes: 505363957fad ("nvmet: fix nvme status code when namespace is disabled")
> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>

Thanks, applied to nvme-6.9.

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

end of thread, other threads:[~2024-05-08 13:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-08  7:43 [PATCH] nvmet: prevent sprintf() overflow in nvmet_subsys_nsid_exists() Dan Carpenter
2024-05-08  7:48 ` Sagi Grimberg
2024-05-08 13:13 ` Keith Busch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®