]> www.infradead.org Git - nvme.git/commitdiff
nvme-tcp: fix use-after-free of netns by kernel TCP socket.
authorKuniyuki Iwashima <kuniyu@amazon.com>
Tue, 8 Apr 2025 22:40:54 +0000 (15:40 -0700)
committerChristoph Hellwig <hch@lst.de>
Wed, 9 Apr 2025 08:43:34 +0000 (10:43 +0200)
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>
Signed-off-by: Christoph Hellwig <hch@lst.de>
drivers/nvme/host/tcp.c

index 26c459f0198d9ef688c24e364d9eee87785c4105..72d260201d8cfef23f55d29a562cc23e2cd3e3e0 100644 (file)
@@ -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 */