History

Revert "smb: client: fix TCP timers deadlock after rmmod"

This reverts commit e9f2517a3e18a54a3943c098d2226b245d488801. Commit e9f2517a3e18 ("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 ef7134c7fc48 ("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 e9f2517a3e18, 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.

Kuniyuki Iwashima committed 1 year ago

Commit 95d2b9f1 parent c7071931 file · +10 −26Browse files

Unified · Split

fs/smb/client/connect.c+10−26
@@ -1073,13 +1073,9 @@ clean_demultiplex_info(struct TCP_Server_Info *server) · 1072 unmodified lines
1073 msleep(125);1073 msleep(125);
1074 if (cifs_rdma_enabled(server))1074 if (cifs_rdma_enabled(server))
1075 smbd_destroy(server);1075 smbd_destroy(server);
1076
1077 if (server->ssocket) {1076 if (server->ssocket) {
1078 sock_release(server->ssocket);1077 sock_release(server->ssocket);
1079 server->ssocket = NULL;1078 server->ssocket = NULL;
1080
1081 /* Release netns reference for the socket. */
1082 put_net(cifs_net_ns(server));
1083 }1079 }
1084 1080
1085 if (!list_empty(&server->pending_mid_q)) {1081 if (!list_empty(&server->pending_mid_q)) {
@@ -1127,7 +1123,6 @@ clean_demultiplex_info(struct TCP_Server_Info *server) · 41 unmodified lines
1127 */1123 */
1128 }1124 }
1129 1125
1130 /* Release netns reference for this server. */
1131 put_net(cifs_net_ns(server));1126 put_net(cifs_net_ns(server));
1132 kfree(server->leaf_fullpath);1127 kfree(server->leaf_fullpath);
1133 kfree(server->hostname);1128 kfree(server->hostname);
@@ -1773,8 +1768,6 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx, · 639 unmodified lines
1773 1768
1774 tcp_ses->ops = ctx->ops;1769 tcp_ses->ops = ctx->ops;
1775 tcp_ses->vals = ctx->vals;1770 tcp_ses->vals = ctx->vals;
1776
1777 /* Grab netns reference for this server. */
1778 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));1771 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
1779 1772
1780 tcp_ses->sign = ctx->sign;1773 tcp_ses->sign = ctx->sign;
@@ -1902,7 +1895,6 @@ smbd_connected: · 121 unmodified lines
1902out_err_crypto_release:1895out_err_crypto_release:
1903 cifs_crypto_secmech_release(tcp_ses);1896 cifs_crypto_secmech_release(tcp_ses);
1904 1897
1905 /* Release netns reference for this server. */
1906 put_net(cifs_net_ns(tcp_ses));1898 put_net(cifs_net_ns(tcp_ses));
1907 1899
1908out_err:1900out_err:
@@ -1911,10 +1903,8 @@ out_err: · 2 unmodified lines
1911 cifs_put_tcp_session(tcp_ses->primary_server, false);1903 cifs_put_tcp_session(tcp_ses->primary_server, false);
1912 kfree(tcp_ses->hostname);1904 kfree(tcp_ses->hostname);
1913 kfree(tcp_ses->leaf_fullpath);1905 kfree(tcp_ses->leaf_fullpath);
1914 if (tcp_ses->ssocket) {1906 if (tcp_ses->ssocket)
1915 sock_release(tcp_ses->ssocket);1907 sock_release(tcp_ses->ssocket);
1916 put_net(cifs_net_ns(tcp_ses));
1917 }
1918 kfree(tcp_ses);1908 kfree(tcp_ses);
1919 }1909 }
1920 return ERR_PTR(rc);1910 return ERR_PTR(rc);
@@ -3358,20 +3348,20 @@ generic_ip_connect(struct TCP_Server_Info *server) · 1437 unmodified lines
3358 socket = server->ssocket;3348 socket = server->ssocket;
3359 } else {3349 } else {
3360 struct net *net = cifs_net_ns(server);3350 struct net *net = cifs_net_ns(server);
3351 struct sock *sk;
3361 3352
3362 rc = sock_create_kern(net, sfamily, SOCK_STREAM, IPPROTO_TCP, &server->ssocket);3353 rc = __sock_create(net, sfamily, SOCK_STREAM,
3354 IPPROTO_TCP, &server->ssocket, 1);
3363 if (rc < 0) {3355 if (rc < 0) {
3364 cifs_server_dbg(VFS, "Error %d creating socket\n", rc);3356 cifs_server_dbg(VFS, "Error %d creating socket\n", rc);
3365 return rc;3357 return rc;
3366 }3358 }
3367 3359
3368 /*3360 sk = server->ssocket->sk;
3369 * Grab netns reference for the socket.3361 __netns_tracker_free(net, &sk->ns_tracker, false);
3370 *3362 sk->sk_net_refcnt = 1;
3371 * It'll be released here, on error, or in clean_demultiplex_info() upon server3363 get_net_track(net, &sk->ns_tracker, GFP_KERNEL);
3372 * teardown.3364 sock_inuse_add(net, 1);
3373 */
3374 get_net(net);
3375 3365
3376 /* BB other socket options to set KEEPALIVE, NODELAY? */3366 /* BB other socket options to set KEEPALIVE, NODELAY? */
3377 cifs_dbg(FYI, "Socket created\n");3367 cifs_dbg(FYI, "Socket created\n");
@@ -3385,10 +3375,8 @@ generic_ip_connect(struct TCP_Server_Info *server) · 7 unmodified lines
3385 }3375 }
3386 3376
3387 rc = bind_socket(server);3377 rc = bind_socket(server);
3388 if (rc < 0) {3378 if (rc < 0)
3389 put_net(cifs_net_ns(server));
3390 return rc;3379 return rc;
3391 }
3392 3380
3393 /*3381 /*
3394 * Eventually check for other socket options to change from3382 * Eventually check for other socket options to change from
@@ -3425,7 +3413,6 @@ generic_ip_connect(struct TCP_Server_Info *server) · 30 unmodified lines
3425 if (rc < 0) {3413 if (rc < 0) {
3426 cifs_dbg(FYI, "Error %d connecting to server\n", rc);3414 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
3427 trace_smb3_connect_err(server->hostname, server->conn_id, &server->dstaddr, rc);3415 trace_smb3_connect_err(server->hostname, server->conn_id, &server->dstaddr, rc);
3428 put_net(cifs_net_ns(server));
3429 sock_release(socket);3416 sock_release(socket);
3430 server->ssocket = NULL;3417 server->ssocket = NULL;
3431 return rc;3418 return rc;
@@ -3443,9 +3430,6 @@ generic_ip_connect(struct TCP_Server_Info *server) · 11 unmodified lines
3443 (server->rfc1001_sessinit == -1 && sport == htons(RFC1001_PORT)))3430 (server->rfc1001_sessinit == -1 && sport == htons(RFC1001_PORT)))
3444 rc = ip_rfc1001_connect(server);3431 rc = ip_rfc1001_connect(server);
3445 3432
3446 if (rc < 0)
3447 put_net(cifs_net_ns(server));
3448
3449 return rc;3433 return rc;
3450}3434}
3451 3435

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t