A secondary channel holds a srv_count reference of its primary server, which cifs_put_tcp_session() drops as soon as the channel's srv_count reaches 0. But the channel struct lives longer: its cifsd thread keeps running until it notices the signal and clean_demultiplex_info() is done, and in-flight requests on the channel now also hold struct references. These paths use server->primary_server, e.g. smb2_is_valid_lease_break() walking pserver->smb_ses_list, the signing key lookup in smb2transport.c or smb2_set_credits(). If the primary server's last references go away at the same time, the channel dereferences freed memory. Let the channel also hold a struct reference of the primary server, taken in cifs_get_tcp_session() and dropped when the channel struct is released. Fixes: 482c117c6dbe ("smb: client: hold server info until cifsd exits") Fixes: 0f2b305af944 ("cifs: connect individual channel servers to primary channel server") Cc: stable@vger.kernel.org Cc: Eduard Zateev Cc: Paulo Alcantara Cc: Namjae Jeon Cc: Ronnie Sahlberg Cc: Shyam Prasad N Cc: Tom Talpey Cc: Bharath SM Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Stefan Metzmacher --- fs/smb/client/connect.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/fs/smb/client/connect.c b/fs/smb/client/connect.c index 3766f486727a..97b06d24dfa5 100644 --- a/fs/smb/client/connect.c +++ b/fs/smb/client/connect.c @@ -1094,6 +1094,9 @@ static void cifs_tcp_server_release(struct kref *kref) put_net(cifs_net_ns(server)); kfree(server->leaf_fullpath); kfree(server->hostname); + /* taken in cifs_get_tcp_session() */ + if (SERVER_IS_CHAN(server)) + cifs_tcp_server_put(server->primary_server); kfree_sensitive(server); } @@ -1853,6 +1856,13 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx, spin_lock(&cifs_tcp_ses_lock); ++primary_server->srv_count; spin_unlock(&cifs_tcp_ses_lock); + /* + * The channel struct can outlive its srv_count reference + * of the primary server (cifsd and in-flight requests), + * so also keep the primary struct alive until our own + * struct is released. + */ + cifs_tcp_server_get(primary_server); tcp_ses->primary_server = primary_server; } init_waitqueue_head(&tcp_ses->response_q); @@ -1980,8 +1990,10 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx, out_err: if (tcp_ses) { - if (SERVER_IS_CHAN(tcp_ses)) + if (SERVER_IS_CHAN(tcp_ses)) { cifs_put_tcp_session(tcp_ses->primary_server, false); + cifs_tcp_server_put(tcp_ses->primary_server); + } kfree(tcp_ses->hostname); kfree(tcp_ses->leaf_fullpath); if (tcp_ses->ssocket) -- 2.43.0