Skip to contentSkip to conversation

nvme-tcp: don't access released socket during error recovery

PATCH Applied Akinobu Mita 4 participants 4 messages Feb 26, 2023 – Feb 27, 2023 NVMe

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvme-tcp: don't access released socket during error recovery

While the error recovery work is temporarily failing reconnect attempts, running the 'nvme list' command causes a kernel NULL pointer dereference by calling getsockname() with a released socket.

During error recovery work, the nvme tcp socket is released and a new one created, so it is not safe to access the socket without proper check.

Fixes 02c57a82c008 ("nvme-tcp: print actual source IP address through sysfs "address" attr")
Cc Martin Belanger <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc Chaitanya Kulkarni <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Keith Busch <[email protected]>
Cc Jens Axboe <[email protected]>
Cc Hannes Reinecke <[email protected]>
---
 drivers/nvme/host/tcp.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/nvme/host/tcp.c +6 -0
index 1955c0ec209e..7723a4989524 100644
@@ -2492,6 +2492,10 @@ static int nvme_tcp_get_address(struct nvme_ctrl *ctrl, char *buf, int size)
 
 	len = nvmf_get_address(ctrl, buf, size);
 
+	mutex_lock(&queue->queue_lock);
+
+	if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags))
+		goto done;
 	ret = kernel_getsockname(queue->sock, (struct sockaddr *)&src_addr);
 	if (ret > 0) {
 		if (len > 0)
@@ -2499,6 +2503,8 @@ static int nvme_tcp_get_address(struct nvme_ctrl *ctrl, char *buf, int size)
 		len += scnprintf(buf + len, size - len, "%ssrc_addr=%pISc\n",
 				(len) ? "," : "", &src_addr);
 	}
+done:
+	mutex_unlock(&queue->queue_lock);
 
 	return len;
 }
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 36 lines · Akinobu Mita

While the error recovery work is temporarily failing reconnect attempts, running the 'nvme list' command causes a kernel NULL pointer dereference by calling getsockname() with a released socket.

During error recovery work, the nvme tcp socket is released and a new one created, so it is not safe to access the socket without proper check.

Fixes 02c57a82c008 ("nvme-tcp: print actual source IP address through sysfs "address" attr")
Cc Martin Belanger <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc Chaitanya Kulkarni <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Keith Busch <[email protected]>
Cc Jens Axboe <[email protected]>
Cc Hannes Reinecke <[email protected]>
---
  drivers/nvme/host/tcp.c | 6 ++++++
  1 file changed, 6 insertions(+)
drivers/nvme/host/tcp.c +4 -0
index 1955c0ec209e..7723a4989524 100644
@@ -2492,6 +2492,10 @@ static int nvme_tcp_get_address(struct nvme_ctrl *ctrl, char *buf, int size)
 
  	len = nvmf_get_address(ctrl, buf, size);
 
+	mutex_lock(&queue->queue_lock);
+
+	if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags))
+		goto done;
  	ret = kernel_getsockname(queue->sock, (struct sockaddr *)&src_addr);
  	if (ret > 0) {
  		if (len > 0)

drivers/nvme/host/tcp.c static int nvme_tcp_get_address(struct nvme_ctrl *ctrl, char *buf, int size) · line 2510

Quoted 8 lines
drivers/nvme/host/tcp.c +2 -0
@@ -2499,6 +2503,8 @@ static int nvme_tcp_get_address(struct nvme_ctrl *ctrl, char *buf, int size)
  		len += scnprintf(buf + len, size - len, "%ssrc_addr=%pISc\n",
  				(len) ? "," : "", &src_addr);
  	}
+done:
+	mutex_unlock(&queue->queue_lock);
 
  	return len;
  }

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Ivo Totev, Andrew
Myers, Andrew McDonald, Martje Boudien Moerman
Reviewed-by
#2 lore raw
Quoted 47 lines · Akinobu Mita
Quoted 26 lines

While the error recovery work is temporarily failing reconnect attempts, running the 'nvme list' command causes a kernel NULL pointer dereference by calling getsockname() with a released socket.

During error recovery work, the nvme tcp socket is released and a new one created, so it is not safe to access the socket without proper check.

Fixes 02c57a82c008 ("nvme-tcp: print actual source IP address through

sysfs "address" attr")
Cc: Martin Belanger <[email protected]>
Cc: Sagi Grimberg <[email protected]>
Cc: Chaitanya Kulkarni <[email protected]>
Cc: Christoph Hellwig <[email protected]>
Cc: Keith Busch <[email protected]>
Cc: Jens Axboe <[email protected]>
Cc: Hannes Reinecke <[email protected]>

---
  drivers/nvme/host/tcp.c | 6 ++++++
  1 file changed, 6 insertions(+)
drivers/nvme/host/tcp.c index renamed from drivers/nvme/host/tcp.c +0 -0

1955c0ec209e..7723a4989524 100644

drivers/nvme/host/tcp.c static int nvme_tcp_get_address(struct · line 2500

Quoted 9 lines
drivers/nvme/host/tcp.c +4 -0
@@ -2492,6 +2492,10 @@ static int nvme_tcp_get_address(struct
 nvme_ctrl *ctrl, char *buf, int size)
 
  	len = nvmf_get_address(ctrl, buf, size);
 
+	mutex_lock(&queue->queue_lock);
+
+	if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags))
+		goto done;
  	ret = kernel_getsockname(queue->sock, (struct sockaddr

*)&src_addr);

drivers/nvme/host/tcp.c static int nvme_tcp_get_address(struct

Quoted 3 lines
drivers/nvme/host/tcp.c +0 -0
  	if (ret > 0) {

if (len > 0)

drivers/nvme/host/tcp.c +0 -0
@@ -2499,6 +2503,8 @@ static int nvme_tcp_get_address(struct

nvme_ctrl *ctrl, char *buf, int size)

Quoted 6 lines
drivers/nvme/host/tcp.c +2 -0
  		len += scnprintf(buf + len, size - len, "%ssrc_addr=%pISc\n",
  				(len) ? "," : "", &src_addr);
  	}
+done:
+	mutex_unlock(&queue->queue_lock);
 
Quoted 3 lines

drivers/nvme/host/tcp.c static int nvme_tcp_get_address(struct nvme_ctrl *ctrl, char *buf, int size) · line 2510

Quoted 2 lines
drivers/nvme/host/tcp.c +0 -0
  	return len;
  }