public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nvme-tcp: Fix use-after-free of netns by kernel TCP socket.
@ 2025-04-08 22:40 Kuniyuki Iwashima
  2025-04-09  8:44 ` Christoph Hellwig
  2025-04-13 22:07 ` Sagi Grimberg
  0 siblings, 2 replies; 4+ messages in thread
From: Kuniyuki Iwashima @ 2025-04-08 22:40 UTC (permalink / raw)
  To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg
  Cc: Peijie Shao, Kuniyuki Iwashima, Kuniyuki Iwashima, linux-nvme,
	linux-kernel

Commit 1be52169c348 ("nvme-tcp: fix selinux denied when calling
sock_sendmsg") converted sock_create() in nvme_tcp_alloc_queue()
to sock_create_kern().

sock_create_kern() creates a kernel socket, which does not hold
a reference to netns.  If the code does not manage the netns
lifetime properly, use-after-free could happen.

Also, TCP kernel socket with sk_net_refcnt 0 has a socket leak
problem: it remains FIN_WAIT_1 if it misses FIN after close()
because tcp_close() stops all timers.

To fix such problems, let's hold netns ref by sk_net_refcnt_upgrade().

We had the same issue in CIFS, SMC, etc, and applied the same
solution, see commit ef7134c7fc48 ("smb: client: Fix use-after-free
of network namespace.") and commit 9744d2bf1976 ("smc: Fix
use-after-free in tcp_write_timer_handler().").

Fixes: 1be52169c348 ("nvme-tcp: fix selinux denied when calling sock_sendmsg")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 drivers/nvme/host/tcp.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
index 26c459f0198d..72d260201d8c 100644
--- a/drivers/nvme/host/tcp.c
+++ b/drivers/nvme/host/tcp.c
@@ -1803,6 +1803,8 @@ static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid,
 		ret = PTR_ERR(sock_file);
 		goto err_destroy_mutex;
 	}
+
+	sk_net_refcnt_upgrade(queue->sock->sk);
 	nvme_tcp_reclassify_socket(queue->sock);
 
 	/* Single syn retry */
-- 
2.49.0


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

* Re: [PATCH] nvme-tcp: Fix use-after-free of netns by kernel TCP socket.
  2025-04-08 22:40 [PATCH] nvme-tcp: Fix use-after-free of netns by kernel TCP socket Kuniyuki Iwashima
@ 2025-04-09  8:44 ` Christoph Hellwig
  2025-04-09 18:59   ` Kuniyuki Iwashima
  2025-04-13 22:07 ` Sagi Grimberg
  1 sibling, 1 reply; 4+ messages in thread
From: Christoph Hellwig @ 2025-04-09  8:44 UTC (permalink / raw)
  To: Kuniyuki Iwashima
  Cc: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
	Peijie Shao, Kuniyuki Iwashima, linux-nvme, linux-kernel

Thanks, applied to nvme-6.15.

> To fix such problems, let's hold netns ref by sk_net_refcnt_upgrade().
> 
> We had the same issue in CIFS, SMC, etc, and applied the same
> solution, see commit ef7134c7fc48 ("smb: client: Fix use-after-free
> of network namespace.") and commit 9744d2bf1976 ("smc: Fix
> use-after-free in tcp_write_timer_handler().").

I wish the netns APIs would be a little more robus to prevent these
bugs from creeping in everywhere..


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

* Re: [PATCH] nvme-tcp: Fix use-after-free of netns by kernel TCP socket.
  2025-04-09  8:44 ` Christoph Hellwig
@ 2025-04-09 18:59   ` Kuniyuki Iwashima
  0 siblings, 0 replies; 4+ messages in thread
From: Kuniyuki Iwashima @ 2025-04-09 18:59 UTC (permalink / raw)
  To: hch
  Cc: axboe, kbusch, kuni1840, kuniyu, linux-kernel, linux-nvme, sagi,
	shaopeijie

From: Christoph Hellwig <hch@lst.de>
Date: Wed, 9 Apr 2025 10:44:46 +0200
> Thanks, applied to nvme-6.15.

Thanks!

> 
> > To fix such problems, let's hold netns ref by sk_net_refcnt_upgrade().
> > 
> > We had the same issue in CIFS, SMC, etc, and applied the same
> > solution, see commit ef7134c7fc48 ("smb: client: Fix use-after-free
> > of network namespace.") and commit 9744d2bf1976 ("smc: Fix
> > use-after-free in tcp_write_timer_handler().").
> 
> I wish the netns APIs would be a little more robus to prevent these
> bugs from creeping in everywhere..

Can't agree more!

Actually, last year I tried to clean up such APIs to prevent this type
of issue.
https://lore.kernel.org/netdev/20241213092152.14057-1-kuniyu@amazon.com/

I'll revise this in this cycle once the fix reaches net tree.

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

* Re: [PATCH] nvme-tcp: Fix use-after-free of netns by kernel TCP socket.
  2025-04-08 22:40 [PATCH] nvme-tcp: Fix use-after-free of netns by kernel TCP socket Kuniyuki Iwashima
  2025-04-09  8:44 ` Christoph Hellwig
@ 2025-04-13 22:07 ` Sagi Grimberg
  1 sibling, 0 replies; 4+ messages in thread
From: Sagi Grimberg @ 2025-04-13 22:07 UTC (permalink / raw)
  To: Kuniyuki Iwashima, Keith Busch, Jens Axboe, Christoph Hellwig
  Cc: Peijie Shao, Kuniyuki Iwashima, linux-nvme, linux-kernel

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

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

end of thread, other threads:[~2025-04-13 22:07 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-08 22:40 [PATCH] nvme-tcp: Fix use-after-free of netns by kernel TCP socket Kuniyuki Iwashima
2025-04-09  8:44 ` Christoph Hellwig
2025-04-09 18:59   ` Kuniyuki Iwashima
2025-04-13 22:07 ` Sagi Grimberg

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