]> git.hungrycats.org Git - linux/commitdiff
ksmbd: fix session reference leak on compounded SESSION_SETUP
authorIan Bridges <icb@fastmail.org>
Tue, 3 Nov 2026 15:29:53 +0000 (10:29 -0500)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 6 Nov 2026 17:06:13 +0000 (12:06 -0500)
[ Upstream commit 03bd8f5279e490e15e0c0f0fa388ffbd2fbacd65 ]

A compound request can place SESSION_SETUP after another command. That
earlier command leaves a reference to its session in work->sess.
smb2_sess_setup() then overwrites work->sess with the session it looks up
or creates, and the dispatcher later puts work->sess only once. The
inherited reference is never dropped. That leaks the session, together
with its setup connection and socket, and every repeat of the compound
leaks another.

  unreferenced object (size 512):
    __kmalloc_cache_noprof+0x32c/0x3a0
    ksmbd_smb2_session_create+0xf6/0x9c0 [ksmbd]
    smb2_sess_setup+0xda4/0x6280 [ksmbd]
    handle_ksmbd_work+0x88c/0x1cb0 [ksmbd]

Save the inherited session on entry. After the request has been
processed, release it once, but only when smb2_sess_setup() has replaced
work->sess with its own session or cleared it. Do this after
ksmbd_conn_unlock() so the put does not run under srv_mutex. An error path
may instead leave the inherited session in work->sess. Keep it there, so
the dispatcher still puts it and the error response is still signed or
encrypted with it as before.

Fixes: 7aa8804c0b67 ("ksmbd: fix user-after-free from session log off")
Cc: stable@vger.kernel.org
Signed-off-by: Ian Bridges <icb@fastmail.org>
Signed-off-by: Namjae Jeon <linkinjeon@kernel.org>
[ sashal: Omitted the installed = true assignment for the absent rejected-binding signature-check block. ]
Signed-off-by: Sasha Levin <sashal@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/smb/server/smb2pdu.c

index 5ba6869038f3dd077b2da9b28fc266947f70b081..39c98fc304f3f2f9dc8607364d535667ee9b4f49 100644 (file)
@@ -1698,6 +1698,8 @@ int smb2_sess_setup(struct ksmbd_work *work)
        struct smb2_sess_setup_req *req;
        struct smb2_sess_setup_rsp *rsp;
        struct ksmbd_session *sess;
+       struct ksmbd_session *prev_sess = work->sess;
+       bool installed = false;
        struct negotiate_message *negblob;
        unsigned int negblob_len, negblob_off;
        int rc = 0;
@@ -1835,6 +1837,7 @@ int smb2_sess_setup(struct ksmbd_work *work)
                conn->binding = false;
        }
        work->sess = sess;
+       installed = true;
 
        negblob_off = le16_to_cpu(req->SecurityBufferOffset);
        negblob_len = le16_to_cpu(req->SecurityBufferLength);
@@ -1988,6 +1991,14 @@ out_err:
        }
 
        ksmbd_conn_unlock(conn);
+
+       /*
+        * Later in a compound, work->sess holds the reference that an
+        * earlier command took. Drop it once this request has installed
+        * its own session there or cleared it.
+        */
+       if (prev_sess && (installed || !work->sess))
+               ksmbd_user_session_put(prev_sess);
        return rc;
 }