Skip to contentSkip to conversation

nvmet-tcp: fix possible NULL deref

PATCH New Sagi Grimberg 2 participants 5 messages Aug 8, 2019 – Aug 13, 2019

Subsystems (2)
lore
Share & tools
lore

Sagi Grimberg

author maintainer
#0 lore raw

PATCH 1/2 nvmet-tcp: fix possible NULL deref

We must only call sgl_free for sgl that we actually allocated.

Signed-off-by Sagi Grimberg <sagi at grimberg.me>
---
 drivers/nvme/target/tcp.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/nvme/target/tcp.c +8 -4
index 69b83fa0c76c..0d63f3da0117 100644
@@ -348,7 +348,8 @@ static int nvmet_tcp_map_data(struct nvmet_tcp_cmd *cmd)
 
 	return 0;
 err:
-	sgl_free(cmd->req.sg);
+	if (cmd->req.sg_cnt)
+		sgl_free(cmd->req.sg);
 	return NVME_SC_INTERNAL;
 }
 
@@ -553,7 +554,8 @@ static int nvmet_try_send_data(struct nvmet_tcp_cmd *cmd)
 
 	if (queue->nvme_sq.sqhd_disabled) {
 		kfree(cmd->iov);
-		sgl_free(cmd->req.sg);
+		if (cmd->req.sg_cnt)
+			sgl_free(cmd->req.sg);
 	}
 
 	return 1;
@@ -584,7 +586,8 @@ static int nvmet_try_send_response(struct nvmet_tcp_cmd *cmd,
 		return -EAGAIN;
 
 	kfree(cmd->iov);
-	sgl_free(cmd->req.sg);
+	if (cmd->req.sg_cnt)
+		sgl_free(cmd->req.sg);
 	cmd->queue->snd_cmd = NULL;
 	nvmet_tcp_put_cmd(cmd);
 	return 1;
@@ -1306,7 +1309,8 @@ static void nvmet_tcp_finish_cmd(struct nvmet_tcp_cmd *cmd)
 {
 	nvmet_req_uninit(&cmd->req);
 	nvmet_tcp_unmap_pdu_iovec(cmd);
-	sgl_free(cmd->req.sg);
+	if (cmd->req.sg_cnt)
+		sgl_free(cmd->req.sg);
 }
 
 static void nvmet_tcp_uninit_data_in_cmds(struct nvmet_tcp_queue *queue)
Signature · 1 line
2.17.1
author maintainer
#1 lore raw

PATCH 2/2 nvmet-tcp: fix possible memory leak

when we uninit a command in error flow we also need to free an iovec if it was allocated.

Signed-off-by Sagi Grimberg <sagi at grimberg.me>
---
 drivers/nvme/target/tcp.c | 1 +
 1 file changed, 1 insertion(+)
drivers/nvme/target/tcp.c +1 -0
index 0d63f3da0117..76e43750b9e5 100644
@@ -1309,6 +1309,7 @@ static void nvmet_tcp_finish_cmd(struct nvmet_tcp_cmd *cmd)
 {
 	nvmet_req_uninit(&cmd->req);
 	nvmet_tcp_unmap_pdu_iovec(cmd);
+	kfree(cmd->iov);
 	if (cmd->req.sg_cnt)
 		sgl_free(cmd->req.sg);
 }
Signature · 1 line
2.17.1
#2 lore raw

PATCH 2/2 nvmet-tcp: fix possible memory leak

Quoted 13 lines · Sagi Grimberg

when we uninit a command in error flow we also need to free an iovec if it was allocated.

Signed-off-by Sagi Grimberg <sagi at grimberg.me>
---
  drivers/nvme/target/tcp.c | 1 +
  1 file changed, 1 insertion(+)
drivers/nvme/target/tcp.c +0 -0
index 0d63f3da0117..76e43750b9e5 100644

drivers/nvme/target/tcp.c static void nvmet_tcp_finish_cmd(struct nvmet_tcp_cmd *cmd) · line 1315

Quoted 7 lines
drivers/nvme/target/tcp.c +1 -0
@@ -1309,6 +1309,7 @@ static void nvmet_tcp_finish_cmd(struct nvmet_tcp_cmd *cmd)
  {
  	nvmet_req_uninit(&cmd->req);
  	nvmet_tcp_unmap_pdu_iovec(cmd);
+	kfree(cmd->iov);
  	if (cmd->req.sg_cnt)
  		sgl_free(cmd->req.sg);
  }

Looks good,

Reviewed-by Max Gurtovoy <maxg at mellanox.com>
#3 lore raw

PATCH 1/2 nvmet-tcp: fix possible NULL deref

In reply to Sagi Grimberg
Quoted 13 lines · Sagi Grimberg

We must only call sgl_free for sgl that we actually allocated.

Signed-off-by Sagi Grimberg <sagi at grimberg.me>
---
  drivers/nvme/target/tcp.c | 12 ++++++++----
  1 file changed, 8 insertions(+), 4 deletions(-)
drivers/nvme/target/tcp.c +0 -0
index 69b83fa0c76c..0d63f3da0117 100644

drivers/nvme/target/tcp.c static int nvmet_tcp_map_data(struct nvmet_tcp_cmd *cmd) · line 352

Quoted 6 lines
drivers/nvme/target/tcp.c +2 -1
@@ -348,7 +348,8 @@ static int nvmet_tcp_map_data(struct nvmet_tcp_cmd *cmd)
 
  	return 0;
  err:
-	sgl_free(cmd->req.sg);
+	if (cmd->req.sg_cnt)
+		sgl_free(cmd->req.sg);

what about zeroing the sg_cnt during nvmet_tcp_get_cmd ?

and NULLify the cmd->req.sg as well in nvmet_tcp_get_cmd ?

Quoted 33 lines
drivers/nvme/target/tcp.c +6 -3
  	return NVME_SC_INTERNAL;
  }
 
@@ -553,7 +554,8 @@ static int nvmet_try_send_data(struct nvmet_tcp_cmd *cmd)
 
  	if (queue->nvme_sq.sqhd_disabled) {
  		kfree(cmd->iov);
-		sgl_free(cmd->req.sg);
+		if (cmd->req.sg_cnt)
+			sgl_free(cmd->req.sg);
  	}
 
  	return 1;
@@ -584,7 +586,8 @@ static int nvmet_try_send_response(struct nvmet_tcp_cmd *cmd,
  		return -EAGAIN;
 
  	kfree(cmd->iov);
-	sgl_free(cmd->req.sg);
+	if (cmd->req.sg_cnt)
+		sgl_free(cmd->req.sg);
  	cmd->queue->snd_cmd = NULL;
  	nvmet_tcp_put_cmd(cmd);
  	return 1;
@@ -1306,7 +1309,8 @@ static void nvmet_tcp_finish_cmd(struct nvmet_tcp_cmd *cmd)
  {
  	nvmet_req_uninit(&cmd->req);
  	nvmet_tcp_unmap_pdu_iovec(cmd);
-	sgl_free(cmd->req.sg);
+	if (cmd->req.sg_cnt)
+		sgl_free(cmd->req.sg);
  }
 
  static void nvmet_tcp_uninit_data_in_cmds(struct nvmet_tcp_queue *queue)
author maintainer
#4 lore raw

PATCH 1/2 nvmet-tcp: fix possible NULL deref

Quoted 4 lines

what about zeroing the sg_cnt during nvmet_tcp_get_cmd ?

and NULLify the cmd->req.sg as well in nvmet_tcp_get_cmd ?

Its set in nvmet_req_init()