Skip to content

Commit 1ace6fa

Browse files
q2venjfvogel
authored andcommitted
Revert "smb: client: fix TCP timers deadlock after rmmod"
commit 95d2b9f693ff2a1180a23d7d59acc0c4e72f4c41 upstream. This reverts commit e9f2517. Commit e9f2517 ("smb: client: fix TCP timers deadlock after rmmod") is intended to fix a null-ptr-deref in LOCKDEP, which is mentioned as CVE-2024-54680, but is actually did not fix anything; The issue can be reproduced on top of it. [0] Also, it reverted the change by commit ef7134c ("smb: client: Fix use-after-free of network namespace.") and introduced a real issue by reviving the kernel TCP socket. When a reconnect happens for a CIFS connection, the socket state transitions to FIN_WAIT_1. Then, inet_csk_clear_xmit_timers_sync() in tcp_close() stops all timers for the socket. If an incoming FIN packet is lost, the socket will stay at FIN_WAIT_1 forever, and such sockets could be leaked up to net.ipv4.tcp_max_orphans. Usually, FIN can be retransmitted by the peer, but if the peer aborts the connection, the issue comes into reality. I warned about this privately by pointing out the exact report [1], but the bogus fix was finally merged. So, we should not stop the timers to finally kill the connection on our side in that case, meaning we must not use a kernel socket for TCP whose sk->sk_net_refcnt is 0. The kernel socket does not have a reference to its netns to make it possible to tear down netns without cleaning up every resource in it. For example, tunnel devices use a UDP socket internally, but we can destroy netns without removing such devices and let it complete during exit. Otherwise, netns would be leaked when the last application died. However, this is problematic for TCP sockets because TCP has timers to close the connection gracefully even after the socket is close()d. The lifetime of the socket and its netns is different from the lifetime of the underlying connection. If the socket user does not maintain the netns lifetime, the timer could be fired after the socket is close()d and its netns is freed up, resulting in use-after-free. Actually, we have seen so many similar issues and converted such sockets to have a reference to netns. That's why I converted the CIFS client socket to have a reference to netns (sk->sk_net_refcnt == 1), which is somehow mentioned as out-of-scope of CIFS and technically wrong in e9f2517, but **is in-scope and right fix**. Regarding the LOCKDEP issue, we can prevent the module unload by bumping the module refcount when switching the LOCKDDEP key in sock_lock_init_class_and_name(). [2] For a while, let's revert the bogus fix. Note that now we can use sk_net_refcnt_upgrade() for the socket conversion, but I'll do so later separately to make backport easy. Link: https://lore.kernel.org/all/[email protected]/ #[0] Link: https://lore.kernel.org/netdev/[email protected]/ #[1] Link: https://lore.kernel.org/lkml/[email protected]/ #[2] Fixes: e9f2517 ("smb: client: fix TCP timers deadlock after rmmod") Signed-off-by: Kuniyuki Iwashima <[email protected]> Cc: [email protected] Signed-off-by: Steve French <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> (cherry picked from commit f761eeefd531e6550cd3a5c047835b4892acb00d) Signed-off-by: Jack Vogel <[email protected]>
1 parent a77e959 commit 1ace6fa

File tree

1 file changed

+10
-26
lines changed

1 file changed

+10
-26
lines changed

fs/smb/client/connect.c

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -987,13 +987,9 @@ clean_demultiplex_info(struct TCP_Server_Info *server)
987987
msleep(125);
988988
if (cifs_rdma_enabled(server))
989989
smbd_destroy(server);
990-
991990
if (server->ssocket) {
992991
sock_release(server->ssocket);
993992
server->ssocket = NULL;
994-
995-
/* Release netns reference for the socket. */
996-
put_net(cifs_net_ns(server));
997993
}
998994

999995
if (!list_empty(&server->pending_mid_q)) {
@@ -1041,7 +1037,6 @@ clean_demultiplex_info(struct TCP_Server_Info *server)
10411037
*/
10421038
}
10431039

1044-
/* Release netns reference for this server. */
10451040
put_net(cifs_net_ns(server));
10461041
kfree(server->leaf_fullpath);
10471042
kfree(server->hostname);
@@ -1717,8 +1712,6 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx,
17171712

17181713
tcp_ses->ops = ctx->ops;
17191714
tcp_ses->vals = ctx->vals;
1720-
1721-
/* Grab netns reference for this server. */
17221715
cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
17231716

17241717
tcp_ses->sign = ctx->sign;
@@ -1851,7 +1844,6 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx,
18511844
out_err_crypto_release:
18521845
cifs_crypto_secmech_release(tcp_ses);
18531846

1854-
/* Release netns reference for this server. */
18551847
put_net(cifs_net_ns(tcp_ses));
18561848

18571849
out_err:
@@ -1860,10 +1852,8 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx,
18601852
cifs_put_tcp_session(tcp_ses->primary_server, false);
18611853
kfree(tcp_ses->hostname);
18621854
kfree(tcp_ses->leaf_fullpath);
1863-
if (tcp_ses->ssocket) {
1855+
if (tcp_ses->ssocket)
18641856
sock_release(tcp_ses->ssocket);
1865-
put_net(cifs_net_ns(tcp_ses));
1866-
}
18671857
kfree(tcp_ses);
18681858
}
18691859
return ERR_PTR(rc);
@@ -3131,20 +3121,20 @@ generic_ip_connect(struct TCP_Server_Info *server)
31313121
socket = server->ssocket;
31323122
} else {
31333123
struct net *net = cifs_net_ns(server);
3124+
struct sock *sk;
31343125

3135-
rc = sock_create_kern(net, sfamily, SOCK_STREAM, IPPROTO_TCP, &server->ssocket);
3126+
rc = __sock_create(net, sfamily, SOCK_STREAM,
3127+
IPPROTO_TCP, &server->ssocket, 1);
31363128
if (rc < 0) {
31373129
cifs_server_dbg(VFS, "Error %d creating socket\n", rc);
31383130
return rc;
31393131
}
31403132

3141-
/*
3142-
* Grab netns reference for the socket.
3143-
*
3144-
* It'll be released here, on error, or in clean_demultiplex_info() upon server
3145-
* teardown.
3146-
*/
3147-
get_net(net);
3133+
sk = server->ssocket->sk;
3134+
__netns_tracker_free(net, &sk->ns_tracker, false);
3135+
sk->sk_net_refcnt = 1;
3136+
get_net_track(net, &sk->ns_tracker, GFP_KERNEL);
3137+
sock_inuse_add(net, 1);
31483138

31493139
/* BB other socket options to set KEEPALIVE, NODELAY? */
31503140
cifs_dbg(FYI, "Socket created\n");
@@ -3158,10 +3148,8 @@ generic_ip_connect(struct TCP_Server_Info *server)
31583148
}
31593149

31603150
rc = bind_socket(server);
3161-
if (rc < 0) {
3162-
put_net(cifs_net_ns(server));
3151+
if (rc < 0)
31633152
return rc;
3164-
}
31653153

31663154
/*
31673155
* Eventually check for other socket options to change from
@@ -3198,7 +3186,6 @@ generic_ip_connect(struct TCP_Server_Info *server)
31983186
if (rc < 0) {
31993187
cifs_dbg(FYI, "Error %d connecting to server\n", rc);
32003188
trace_smb3_connect_err(server->hostname, server->conn_id, &server->dstaddr, rc);
3201-
put_net(cifs_net_ns(server));
32023189
sock_release(socket);
32033190
server->ssocket = NULL;
32043191
return rc;
@@ -3207,9 +3194,6 @@ generic_ip_connect(struct TCP_Server_Info *server)
32073194
if (sport == htons(RFC1001_PORT))
32083195
rc = ip_rfc1001_connect(server);
32093196

3210-
if (rc < 0)
3211-
put_net(cifs_net_ns(server));
3212-
32133197
return rc;
32143198
}
32153199

0 commit comments

Comments
 (0)