Skip to contentSkip to conversation

drivers: nvme: target: core: deref after null

PATCH In review Alexander Sapozhnikov 4 participants 4 messages Jun 7, 2024 – Jun 10, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drivers: nvme: target: core: deref after null

From: Alexandr Sapozhnikov <[email protected]>

After having been compared to a NULL value at core.c:813, pointer '(**sq->ctrl).sqs' is dereferenced at core.c:838.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

---
 drivers/nvme/target/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvme/target/core.c +1 -1
index 06f0c587f343..5a67d2bc0c55 100644
@@ -827,7 +827,7 @@ void nvmet_sq_destroy(struct nvmet_sq *sq)
 	 */
 	ctrl = sq->ctrl;
 
-	if (ctrl) {
+	if (ctrl && ctrl->sqs) {
 		/*
 		 * The teardown flow may take some time, and the host may not
 		 * send us keep-alive during this period, hence reset the
Signature · 1 line
2.39.2
#1 lore raw
Quoted 4 lines · Alexander Sapozhnikov

From: Alexandr Sapozhnikov <[email protected]>

After having been compared to a NULL value at core.c:813, pointer '(**sq->ctrl).sqs' is dereferenced at core.c:838.

really didn't understand the NULL check mentioned above :-

 812          */
 813         if (ctrl && ctrl->sqs && ctrl->sqs[0] == sq) 814                 nvmet_async_events_failall(ctrl);

from what I know it checks the non NULL ctrl->sqs and then validates the sq is admin queue before calling failing the async events ...

Quoted 12 lines

Found by Linux Verification Center (linuxtesting.org) with SVACE.

---
  drivers/nvme/target/core.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvme/target/core.c +0 -0
index 06f0c587f343..5a67d2bc0c55 100644

drivers/nvme/target/core.c void nvmet_sq_destroy(struct nvmet_sq *sq) · line 833

Quoted 8 lines
drivers/nvme/target/core.c +1 -1
@@ -827,7 +827,7 @@ void nvmet_sq_destroy(struct nvmet_sq *sq)
  	 */
  	ctrl = sq->ctrl;
 
-	if (ctrl) {
+	if (ctrl && ctrl->sqs) {
  		/*
  		 * The teardown flow may take some time, and the host may not
  		 * send us keep-alive during this period, hence reset the

do we need more fine grained check where ctrl->sqs is actually accessed :-

drivers/nvme/target/core.c +0 -0
index 06f0c587f343..a70cad5a6262 100644
@@ -835,7 +835,8 @@ void nvmet_sq_destroy(struct nvmet_sq *sq)
                  * controller teardown as a result of a keep-alive 

expiration.
                 */
                ctrl->reset_tbkas = true;
-               sq->ctrl->sqs[sq->qid] = NULL;
+               if (sq->ctrl->sqs)
+                       sq->ctrl->sqs[sq->qid] = NULL;
                nvmet_ctrl_put(ctrl);
                sq->ctrl = NULL; /* allows reusing the queue later */ }

-ck

#2 lore raw
In reply to Alexander Sapozhnikov
Quoted 2 lines

After having been compared to a NULL value at core.c:813, pointer '(**sq->ctrl).sqs' is dereferenced at core.c:838.

* Please choose another imperative wording for an improved change description.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.10-rc2#n94

* Would you like to add the tag “Fixes” accordingly?

* How do you think about to use the summary phrase “Avoid null pointer dereference in nvmet_sq_destroy()”?

* Is the subsystem specification “nvmet” more appropriate?

Regards,
Markus

maintainer
#3 lore raw
In reply to Alexander Sapozhnikov
Quoted 17 lines · Alexander Sapozhnikov

From: Alexandr Sapozhnikov <[email protected]>

After having been compared to a NULL value at core.c:813, pointer '(**sq->ctrl).sqs' is dereferenced at core.c:838.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

---
  drivers/nvme/target/core.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvme/target/core.c +0 -0
index 06f0c587f343..5a67d2bc0c55 100644

drivers/nvme/target/core.c void nvmet_sq_destroy(struct nvmet_sq *sq) · line 833

Quoted 8 lines
drivers/nvme/target/core.c +1 -1
@@ -827,7 +827,7 @@ void nvmet_sq_destroy(struct nvmet_sq *sq)
  	 */
  	ctrl = sq->ctrl;
 
-	if (ctrl) {
+	if (ctrl && ctrl->sqs) {
  		/*
  		 * The teardown flow may take some time, and the host may not
  		 * send us keep-alive during this period, hence reset the

This is a redundant check. sq->ctrl and sq->ctrl->sqs have the same lifetime. If sq->ctrl was assigned, it means that it was allocated and needs a final put to fully cleanup. ctrl->sqs is allocated in nvmet_alloc_ctrl and freed in nvmet_free_ctrl which is the final reference.