Skip to contentSkip to conversation

nvmet-tcp: Fix NULL dereference when a connect data comes in h2cdata pdu

PATCH In review Ziye Yang 2 participants 3 messages Aug 21, 2020 – Aug 24, 2020 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Ziye Yang

author
#1 lore raw

PATCH 1/1 nvmet-tcp: Fix NULL dereference when a connect data comes in h2cdata pdu

When handling commands without in-capsule data, we assign the ttag assuming we already have the queue commands array allocated (based on the queue size information in the connect data payload). However if the connect itself did not send the connect data in-capsule we have yet to allocate the queue commands,and we will assign a bogus ttag and suffer a NULL dereference when we receive the corresponding h2cdata pdu.

Fix this by checking if we already allocated commands before dereferencing it when handling h2cdata, if we didn't, its for sure a connect and we should use the preallocated connect command.

---
 drivers/nvme/target/tcp.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/nvme/target/tcp.c +9 -1
index 9eda91162fe4..8e0d766d2722 100644
@@ -160,6 +160,11 @@ static void nvmet_tcp_finish_cmd(struct nvmet_tcp_cmd *cmd);
 static inline u16 nvmet_tcp_cmd_tag(struct nvmet_tcp_queue *queue,
 		struct nvmet_tcp_cmd *cmd)
 {
+	if (unlikely(!queue->nr_cmds)) {
+		/* We didn't allocate cmds yet, send 0xffff */
+		return USHRT_MAX;
+	}
+
 	return cmd - queue->cmds;
 }
 
@@ -866,7 +871,10 @@ static int nvmet_tcp_handle_h2c_data_pdu(struct nvmet_tcp_queue *queue)
 	struct nvme_tcp_data_pdu *data = &queue->pdu.data;
 	struct nvmet_tcp_cmd *cmd;
 
-	cmd = &queue->cmds[data->ttag];
+	if (likely(queue->nr_cmds))
+		cmd = &queue->cmds[data->ttag];
+	else
+		cmd = &queue->connect;
 
 	if (le32_to_cpu(data->data_offset) != cmd->rbytes_done) {
 		pr_err("ttag %u unexpected data offset %u (expected %u)\n",
Signature · 7 lines
2.21.3


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
maintainer Reviewed-by on v1 1/1
#2 lore raw
Quoted 37 lines

When handling commands without in-capsule data, we assign the ttag assuming we already have the queue commands array allocated (based on the queue size information in the connect data payload). However if the connect itself did not send the connect data in-capsule we have yet to allocate the queue commands,and we will assign a bogus ttag and suffer a NULL dereference when we receive the corresponding h2cdata pdu.

Fix this by checking if we already allocated commands before dereferencing it when handling h2cdata, if we didn't, its for sure a connect and we should use the preallocated connect command.

---
  drivers/nvme/target/tcp.c | 10 +++++++++-
  1 file changed, 9 insertions(+), 1 deletion(-)
drivers/nvme/target/tcp.c +5 -0
index 9eda91162fe4..8e0d766d2722 100644
@@ -160,6 +160,11 @@ static void nvmet_tcp_finish_cmd(struct nvmet_tcp_cmd *cmd);
  static inline u16 nvmet_tcp_cmd_tag(struct nvmet_tcp_queue *queue,
  		struct nvmet_tcp_cmd *cmd)
  {
+	if (unlikely(!queue->nr_cmds)) {
+		/* We didn't allocate cmds yet, send 0xffff */
+		return USHRT_MAX;
+	}
+
  	return cmd - queue->cmds;
  }
 
@@ -866,7 +871,10 @@ static int nvmet_tcp_handle_h2c_data_pdu(struct nvmet_tcp_queue *queue)
  	struct nvme_tcp_data_pdu *data = &queue->pdu.data;

drivers/nvme/target/tcp.c static int nvmet_tcp_handle_h2c_data_pdu(struct nvmet_tcp_queue *queue) · line 880

Quoted 10 lines
drivers/nvme/target/tcp.c +4 -1
  	struct nvmet_tcp_cmd *cmd;
 
-	cmd = &queue->cmds[data->ttag];
+	if (likely(queue->nr_cmds))
+		cmd = &queue->cmds[data->ttag];
+	else
+		cmd = &queue->connect;
 
  	if (le32_to_cpu(data->data_offset) != cmd->rbytes_done) {
  		pr_err("ttag %u unexpected data offset %u (expected %u)\n",

Ziye, usually we send versioning of the patches with prefix PATCH v1/2/3,

and also add what was changed since prior versions.

This patch looks good,

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

author Reviewed-by Sagi Grimberg
#3 lore raw

Hi Sagi,

Got it.

Best Regards
Ziye Yang

-----Original Message-----
From: Sagi Grimberg <[email protected]>
Sent: Friday, August 21, 2020 11:56 PM
To: Yang, Ziye <[email protected]>; [email protected]
Subject: Re: [PATCH] nvmet-tcp: Fix NULL dereference when a connect data comes in h2cdata pdu

Quoted 36 lines

When handling commands without in-capsule data, we assign the ttag assuming we already have the queue commands array allocated (based on the queue size information in the connect data payload). However if the connect itself did not send the connect data in-capsule we have yet to allocate the queue commands,and we will assign a bogus ttag and suffer a NULL dereference when we receive the corresponding h2cdata pdu.

Fix this by checking if we already allocated commands before dereferencing it when handling h2cdata, if we didn't, its for sure a connect and we should use the preallocated connect command.

---
  drivers/nvme/target/tcp.c | 10 +++++++++-
  1 file changed, 9 insertions(+), 1 deletion(-)
drivers/nvme/target/tcp.c +5 -0
index 9eda91162fe4..8e0d766d2722 100644
@@ -160,6 +160,11 @@ static void nvmet_tcp_finish_cmd(struct nvmet_tcp_cmd *cmd);
  static inline u16 nvmet_tcp_cmd_tag(struct nvmet_tcp_queue *queue,
  		struct nvmet_tcp_cmd *cmd)
  {
+	if (unlikely(!queue->nr_cmds)) {
+		/* We didn't allocate cmds yet, send 0xffff */
+		return USHRT_MAX;
+	}
+
  	return cmd - queue->cmds;
  }
 
@@ -866,7 +871,10 @@ static int nvmet_tcp_handle_h2c_data_pdu(struct nvmet_tcp_queue *queue)
  	struct nvme_tcp_data_pdu *data = &queue->pdu.data;

drivers/nvme/target/tcp.c static int nvmet_tcp_handle_h2c_data_pdu(struct nvmet_tcp_queue *queue) · line 880

Quoted 10 lines
drivers/nvme/target/tcp.c +4 -1
  	struct nvmet_tcp_cmd *cmd;
 
-	cmd = &queue->cmds[data->ttag];
+	if (likely(queue->nr_cmds))
+		cmd = &queue->cmds[data->ttag];
+	else
+		cmd = &queue->connect;
 
  	if (le32_to_cpu(data->data_offset) != cmd->rbytes_done) {
  		pr_err("ttag %u unexpected data offset %u (expected %u)\n",

Ziye, usually we send versioning of the patches with prefix PATCH v1/2/3,

and also add what was changed since prior versions.

This patch looks good,

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme