* [PATCH v5 1/8] ksmbd: verify the signature of session binding requests
2026-11-12 4:46 [PATCH v5 0/8] ksmbd: verify session setup requests before signing with their key Michail Andreas Michalopoulos
@ 2026-11-12 4:46 ` Michail Andreas Michalopoulos
2026-11-12 4:46 ` [PATCH v5 2/8] ksmbd: sign with the negotiated SMB 3.1.1 signing algorithm Michail Andreas Michalopoulos
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Michail Andreas Michalopoulos @ 2026-11-12 4:46 UTC (permalink / raw)
To: linkinjeon, stfrench
Cc: icb, senozhatsky, tom, chenxiaosong, ematsumiya, lsahlber,
hyc.lee, linux-cifs, linux-kernel, Michail Andreas Michalopoulos,
stable
When multichannel is enabled, smb2_sess_setup() accepts a binding
request on a new connection once it finds the session and sees
SMB2_FLAGS_SIGNED, but never checks the signature: SESSION_SETUP is
exempt from the generic check in __process_request(), because
work->sess is not set for it. The request then goes through
authentication, and the response is signed with the session's signing
key, since the request claims to be signed.
So anyone who knows a SessionId and ClientGUID can open a connection
and have the server sign STATUS_MORE_PROCESSING_REQUIRED responses with
another client's session key, each with a fresh NTLM challenge and at a
MessageId of their choice: a signing oracle.
MS-SMB2 3.3.5.2.4 requires the server to verify a binding request with
Session.SigningKey and to fail it with STATUS_ACCESS_DENIED if that
fails. It does so while receiving the message, before the checks of
3.3.5.5, so do it right after the check that the request is signed. The
failure response is not signed, because work->sess is only set once the
request has been accepted.
smb2_check_sign_req() and smb3_check_sign_req() zero the Signature
field in place to compute the MAC. Restore it in both before they
return, as generate_preauth_hash() later hashes the request as received
into the channel's SMB 3.1.1 preauth integrity hash.
A session that is still being set up has no signing key yet: the key is
all zeroes until authentication completes. Refuse a binding request for
it before verifying, in both binding branches, with the
STATUS_REQUEST_NOT_ACCEPTED that 3.3.5.5 gives for such a session
(3.3.5.2.4 would answer STATUS_NOT_SUPPORTED, as the key is NULL), so
that a request is never verified against a missing key. Set
SMB2_SESSION_VALID with smp_store_release() and read the state with
smp_load_acquire(), so that a session seen as valid has its key.
The other binding branch, which rejects a binding request that is not a
valid multichannel bind, verifies the signature as well. A missing
negation on the multichannel flag made that branch unreachable for an
SMB 3.x request when multichannel is disabled: such a request fell
through to the reauthentication path instead and was answered with an
unsigned STATUS_USER_SESSION_DELETED. Correct the condition so the
request is verified and rejected there.
That branch signs its refusal of a verified request with the session's
key. An SMB 3.x connection binding to an SMB 2.x session now gets there
too, and the connection's own handler would sign with the SMB 3.x
signing key, which such a session does not have. Sign that response
with smb2_set_sign_rsp(), as is already done the other way round.
An AI assistant was used to draft the patch and as an independent
reviewer.
Reported-by: Ian Bridges <icb@fastmail.org>
Closes: https://lore.kernel.org/all/20261103171742.18816-1-icb@fastmail.org/
Fixes: f5a544e3bab7 ("ksmbd: add support for SMB3 multichannel")
Cc: stable@vger.kernel.org
Signed-off-by: Michail Andreas Michalopoulos <mike.michalo@gmail.com>
---
fs/smb/server/server.c | 9 ++++++
fs/smb/server/smb2pdu.c | 69 ++++++++++++++++++++++++++++++++---------
2 files changed, 63 insertions(+), 15 deletions(-)
diff --git a/fs/smb/server/server.c b/fs/smb/server/server.c
index 064f99427ca6f..51aa981bcef4e 100644
--- a/fs/smb/server/server.c
+++ b/fs/smb/server/server.c
@@ -322,10 +322,19 @@ static void __handle_ksmbd_work(struct ksmbd_work *work,
if (work->sess &&
(work->sess->sign || smb3_11_final_sess_setup_resp(work) ||
conn->ops->is_sign_req(work, command))) {
+ /*
+ * A refused binding request is answered with the key
+ * of the session, which can be of another dialect
+ * family than this connection.
+ */
if (command == SMB2_SESSION_SETUP_HE &&
work->sess->dialect >= SMB30_PROT_ID &&
conn->dialect < SMB30_PROT_ID)
smb3_set_sign_rsp(work);
+ else if (command == SMB2_SESSION_SETUP_HE &&
+ work->sess->dialect < SMB30_PROT_ID &&
+ conn->dialect >= SMB30_PROT_ID)
+ smb2_set_sign_rsp(work);
else
conn->ops->set_sign_rsp(work);
}
diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index b084cca8813cc..3d9b966235dbb 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -2558,6 +2558,23 @@ static int krb5_authenticate(struct ksmbd_work *work,
}
#endif
+/*
+ * A binding request must be signed with the signing key of the session it
+ * binds to (MS-SMB2 3.3.5.2.4). Check it before the request is processed
+ * and before any response is signed with that key.
+ */
+static bool smb2_check_binding_sign_req(struct ksmbd_work *work,
+ struct ksmbd_session *sess)
+{
+ struct ksmbd_session *prev_sess = work->sess;
+ int ret;
+
+ work->sess = sess;
+ ret = smb3_check_sign_req(work);
+ work->sess = prev_sess;
+ return ret;
+}
+
int smb2_sess_setup(struct ksmbd_work *work)
{
struct ksmbd_conn *conn = work->conn;
@@ -2635,22 +2652,33 @@ int smb2_sess_setup(struct ksmbd_work *work)
goto out_err;
}
+ /*
+ * A session that is still being set up has no signing key
+ * yet, so there is nothing to verify a binding request with.
+ * Pairs with smp_store_release() in smb2_sess_setup().
+ */
+ if (smp_load_acquire(&sess->state) == SMB2_SESSION_IN_PROGRESS) {
+ rc = -EACCES;
+ goto out_err;
+ }
+
if (!(req->hdr.Flags & SMB2_FLAGS_SIGNED)) {
rc = -EINVAL;
goto out_err;
}
+ /* As part of receiving the request (MS-SMB2 3.3.5.2.4) */
+ if (!smb2_check_binding_sign_req(work, sess)) {
+ rc = -EKEYREJECTED;
+ goto out_err;
+ }
+
if (memcmp(conn->ClientGUID, sess->ClientGUID,
SMB2_CLIENT_GUID_SIZE)) {
rc = -ENOENT;
goto out_err;
}
- if (sess->state == SMB2_SESSION_IN_PROGRESS) {
- rc = -EACCES;
- goto out_err;
- }
-
if (sess->state == SMB2_SESSION_EXPIRED) {
rc = -EFAULT;
goto out_err;
@@ -2675,21 +2703,23 @@ int smb2_sess_setup(struct ksmbd_work *work)
conn->binding = true;
} else if ((conn->dialect < SMB30_PROT_ID ||
- server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) &&
+ !(server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL)) &&
(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) {
sess = ksmbd_session_lookup_slowpath(le64_to_cpu(req->hdr.SessionId));
if (sess) {
- int sign_ret;
+ int sign_ret = 0;
work->sess = sess;
installed = true;
- if (sess->dialect >= SMB30_PROT_ID)
- sign_ret = smb3_check_sign_req(work);
- else
- sign_ret = smb2_check_sign_req(work);
- if (sess->state != SMB2_SESSION_VALID ||
- !(req->hdr.Flags & SMB2_FLAGS_SIGNED) ||
- !sign_ret) {
+ /* Only a valid session has a signing key to check with */
+ if (smp_load_acquire(&sess->state) == SMB2_SESSION_VALID &&
+ (req->hdr.Flags & SMB2_FLAGS_SIGNED)) {
+ if (sess->dialect >= SMB30_PROT_ID)
+ sign_ret = smb3_check_sign_req(work);
+ else
+ sign_ret = smb2_check_sign_req(work);
+ }
+ if (!sign_ret) {
ksmbd_user_session_put(sess);
work->sess = NULL;
sess = NULL;
@@ -2928,7 +2958,12 @@ int smb2_sess_setup(struct ksmbd_work *work)
destroy_previous_session(conn, sess->user, previous_session_id);
if (!ksmbd_conn_need_reconnect(conn)) {
ksmbd_conn_set_good(conn);
- sess->state = SMB2_SESSION_VALID;
+ /*
+ * The session's keys are generated by now. Pairs with
+ * smp_load_acquire() where a binding request is
+ * verified with them.
+ */
+ smp_store_release(&sess->state, SMB2_SESSION_VALID);
}
}
@@ -12853,6 +12888,8 @@ int smb2_check_sign_req(struct ksmbd_work *work)
ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, 1,
signature);
+ /* Restore the request as received, for the SMB 3.1.1 preauth hash */
+ memcpy(hdr->Signature, signature_req, SMB2_SIGNATURE_SIZE);
if (crypto_memneq(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
pr_err("bad smb2 signature\n");
@@ -12972,6 +13009,8 @@ int smb3_check_sign_req(struct ksmbd_work *work)
iov[0].iov_len = len;
ksmbd_sign_smb3_pdu(conn, signing_key, iov, 1, signature);
+ /* Restore the request as received, for the SMB 3.1.1 preauth hash */
+ memcpy(hdr->Signature, signature_req, SMB2_SIGNATURE_SIZE);
if (crypto_memneq(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
pr_err("bad smb2 signature\n");
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH v5 2/8] ksmbd: sign with the negotiated SMB 3.1.1 signing algorithm
2026-11-12 4:46 [PATCH v5 0/8] ksmbd: verify session setup requests before signing with their key Michail Andreas Michalopoulos
2026-11-12 4:46 ` [PATCH v5 1/8] ksmbd: verify the signature of session binding requests Michail Andreas Michalopoulos
@ 2026-11-12 4:46 ` Michail Andreas Michalopoulos
2026-11-12 4:46 ` [PATCH v5 3/8] ksmbd: use the channel signing key for session setup on a bound channel Michail Andreas Michalopoulos
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Michail Andreas Michalopoulos @ 2026-11-12 4:46 UTC (permalink / raw)
To: linkinjeon, stfrench
Cc: icb, senozhatsky, tom, chenxiaosong, ematsumiya, lsahlber,
hyc.lee, linux-cifs, linux-kernel, Michail Andreas Michalopoulos,
stable
decode_sign_cap_ctxt() stores the algorithm picked from the client's
SMB2_SIGNING_CAPABILITIES context in conn->signing_algorithm, but it
runs before init_smb3_11_server(), which then overwrites it with
AES-CMAC. As a result the negotiate response always selects AES-CMAC,
even for a client that only offered HMAC-SHA256, and every SMB 3.1.1
connection is signed with AES-CMAC.
Keep the negotiated algorithm and sign with HMAC-SHA256 when that is
what was negotiated.
Connections of the same session can now have negotiated different
algorithms. MS-SMB2 takes the algorithm from the connection a message
travels on (3.1.4.1, 3.1.5.1), and a client that follows 3.2.4.2.2.2
offers only the already negotiated algorithm on further connections, so
for it nothing differs. A client that offers another list on a new
connection, as the Samba client can, signs the binding request with the
session signing key and the algorithm of the connection that set up the
session. smbd verifies it that way, and smbtorture's
smb2.session.bind_negative_smb3signCtoH and HtoC tests expect such a
bind to succeed. Do the same: store that connection's algorithm in the
session and use it together with the session key, while channel keys
keep using their own connection's algorithm.
An AI assistant was used to draft the patch and as an independent
reviewer. Testing is described in the cover letter.
Fixes: 378087cd17ee ("ksmbd: add support for negotiating signing algorithm")
Cc: stable@vger.kernel.org
Signed-off-by: Michail Andreas Michalopoulos <mike.michalo@gmail.com>
---
fs/smb/server/auth.c | 14 ++++++++++++--
fs/smb/server/auth.h | 4 ++--
fs/smb/server/mgmt/user_session.c | 1 +
fs/smb/server/mgmt/user_session.h | 2 ++
fs/smb/server/smb2ops.c | 4 +++-
fs/smb/server/smb2pdu.c | 20 ++++++++++++++++++--
6 files changed, 38 insertions(+), 7 deletions(-)
diff --git a/fs/smb/server/auth.c b/fs/smb/server/auth.c
index 0c51ca0cb14db..fc6ace49584b1 100644
--- a/fs/smb/server/auth.c
+++ b/fs/smb/server/auth.c
@@ -504,19 +504,29 @@ void ksmbd_sign_smb2_pdu(struct ksmbd_conn *conn, char *key, struct kvec *iov,
/**
* ksmbd_sign_smb3_pdu() - function to generate packet signing
* @conn: connection
+ * @signing_algorithm: signing algorithm the key was generated for
* @key: signing key
* @iov: buffer iov array
* @n_vec: number of iovecs
* @sig: signature value generated for client request packet
*
*/
-void ksmbd_sign_smb3_pdu(struct ksmbd_conn *conn, char *key, struct kvec *iov,
- int n_vec, char *sig)
+void ksmbd_sign_smb3_pdu(struct ksmbd_conn *conn, __le16 signing_algorithm,
+ char *key, struct kvec *iov, int n_vec, char *sig)
{
struct aes_cmac_key cmac_key __cleanup(aes_cmac_zeroize_key);
struct aes_cmac_ctx cmac_ctx;
int i;
+ if (signing_algorithm == SIGNING_ALG_HMAC_SHA256_LE) {
+ u8 mac[SMB2_HMACSHA256_SIZE];
+
+ ksmbd_sign_smb2_pdu(conn, key, iov, n_vec, mac);
+ memcpy(sig, mac, SMB2_SIGNATURE_SIZE);
+ memzero_explicit(mac, sizeof(mac));
+ return;
+ }
+
/* This cannot fail, since we always pass a valid key length. */
static_assert(SMB2_CMACAES_SIZE == AES_KEYSIZE_128);
aes_cmac_preparekey(&cmac_key, key, SMB2_CMACAES_SIZE);
diff --git a/fs/smb/server/auth.h b/fs/smb/server/auth.h
index 7ce9c42d58f1e..5a10482678ee4 100644
--- a/fs/smb/server/auth.h
+++ b/fs/smb/server/auth.h
@@ -59,8 +59,8 @@ int ksmbd_krb5_authenticate(struct ksmbd_session *sess, char *in_blob,
char *sess_key);
void ksmbd_sign_smb2_pdu(struct ksmbd_conn *conn, char *key, struct kvec *iov,
int n_vec, char *sig);
-void ksmbd_sign_smb3_pdu(struct ksmbd_conn *conn, char *key, struct kvec *iov,
- int n_vec, char *sig);
+void ksmbd_sign_smb3_pdu(struct ksmbd_conn *conn, __le16 signing_algorithm,
+ char *key, struct kvec *iov, int n_vec, char *sig);
int ksmbd_gen_smb30_signingkey(struct ksmbd_session *sess,
struct ksmbd_conn *conn);
int ksmbd_gen_smb311_signingkey(struct ksmbd_session *sess,
diff --git a/fs/smb/server/mgmt/user_session.c b/fs/smb/server/mgmt/user_session.c
index 15662fd745abb..87aba04a8cbdd 100644
--- a/fs/smb/server/mgmt/user_session.c
+++ b/fs/smb/server/mgmt/user_session.c
@@ -557,6 +557,7 @@ int ksmbd_session_register(struct ksmbd_conn *conn,
int ret;
sess->dialect = conn->dialect;
+ sess->signing_algorithm = conn->signing_algorithm;
memcpy(sess->ClientGUID, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE);
ret = xa_err(xa_store(&conn->sessions, sess->id, sess,
KSMBD_DEFAULT_GFP));
diff --git a/fs/smb/server/mgmt/user_session.h b/fs/smb/server/mgmt/user_session.h
index 5f0f6276e3168..931ca102b8392 100644
--- a/fs/smb/server/mgmt/user_session.h
+++ b/fs/smb/server/mgmt/user_session.h
@@ -48,6 +48,8 @@ struct ksmbd_session {
u64 id;
__u16 dialect;
+ /* Signing algorithm of the connection that created the session */
+ __le16 signing_algorithm;
char ClientGUID[SMB2_CLIENT_GUID_SIZE];
struct ksmbd_user *user;
diff --git a/fs/smb/server/smb2ops.c b/fs/smb/server/smb2ops.c
index 747276b171bd1..c5b3cf34494e7 100644
--- a/fs/smb/server/smb2ops.c
+++ b/fs/smb/server/smb2ops.c
@@ -320,7 +320,9 @@ int init_smb3_11_server(struct ksmbd_conn *conn)
conn->ops = &smb3_11_server_ops;
conn->cmds = smb2_0_server_cmds;
conn->max_cmds = ARRAY_SIZE(smb2_0_server_cmds);
- conn->signing_algorithm = SIGNING_ALG_AES_CMAC_LE;
+ /* Keep the algorithm chosen from the client's signing capabilities */
+ if (!conn->signing_negotiated)
+ conn->signing_algorithm = SIGNING_ALG_AES_CMAC_LE;
if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_LEASES)
conn->vals->req_capabilities |= SMB2_GLOBAL_CAP_LEASING |
diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index 3d9b966235dbb..d15024d34f3d6 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -1858,6 +1858,8 @@ static __le32 deassemble_neg_contexts(struct ksmbd_conn *conn,
bool posix_ctxt_seen = false;
ksmbd_debug(SMB, "decoding %d negotiate contexts\n", neg_ctxt_cnt);
+ /* A previous, failed NEGOTIATE must not leave its algorithm behind */
+ conn->signing_negotiated = false;
if (len_of_smb <= offset) {
ksmbd_debug(SMB, "Invalid response: negotiate context offset\n");
return status;
@@ -12965,6 +12967,7 @@ int smb3_check_sign_req(struct ksmbd_work *work)
{
struct ksmbd_conn *conn = work->conn;
char *signing_key;
+ __le16 signing_algorithm;
struct smb2_hdr *hdr;
struct channel *chann;
char signature_req[SMB2_SIGNATURE_SIZE];
@@ -12984,8 +12987,14 @@ int smb3_check_sign_req(struct ksmbd_work *work)
len = get_rfc1002_len(work->request_buf) -
work->next_smb2_rcv_hdr_off;
+ /*
+ * A key must be used with the algorithm it was generated for: the
+ * session key with the one of the connection that created the
+ * session, which can differ from this connection's when binding.
+ */
if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
signing_key = work->sess->smb3signingkey;
+ signing_algorithm = work->sess->signing_algorithm;
} else {
chann = lookup_chann_list(work->sess, conn);
if (!chann) {
@@ -12993,8 +13002,10 @@ int smb3_check_sign_req(struct ksmbd_work *work)
!(hdr->Flags & SMB2_FLAGS_SIGNED))
return 0;
signing_key = work->sess->smb3signingkey;
+ signing_algorithm = work->sess->signing_algorithm;
} else {
signing_key = chann->smb3signingkey;
+ signing_algorithm = conn->signing_algorithm;
}
}
@@ -13008,7 +13019,8 @@ int smb3_check_sign_req(struct ksmbd_work *work)
iov[0].iov_base = (char *)&hdr->ProtocolId;
iov[0].iov_len = len;
- ksmbd_sign_smb3_pdu(conn, signing_key, iov, 1, signature);
+ ksmbd_sign_smb3_pdu(conn, signing_algorithm, signing_key, iov, 1,
+ signature);
/* Restore the request as received, for the SMB 3.1.1 preauth hash */
memcpy(hdr->Signature, signature_req, SMB2_SIGNATURE_SIZE);
@@ -13035,6 +13047,7 @@ void smb3_set_sign_rsp(struct ksmbd_work *work)
u16 command;
int n_vec;
char *signing_key;
+ __le16 signing_algorithm;
hdr = ksmbd_resp_buf_curr(work);
command = le16_to_cpu(hdr->Command);
@@ -13042,12 +13055,14 @@ void smb3_set_sign_rsp(struct ksmbd_work *work)
if (command == SMB2_SESSION_SETUP_HE &&
(!work->session_setup_binding || hdr->Status != STATUS_SUCCESS)) {
signing_key = work->sess->smb3signingkey;
+ signing_algorithm = work->sess->signing_algorithm;
} else {
chann = lookup_chann_list(work->sess, work->conn);
if (!chann) {
return;
}
signing_key = chann->smb3signingkey;
+ signing_algorithm = conn->signing_algorithm;
}
if (!signing_key)
@@ -13058,7 +13073,8 @@ void smb3_set_sign_rsp(struct ksmbd_work *work)
iov = smb2_get_sign_rsp_iov(work, hdr, &n_vec);
- ksmbd_sign_smb3_pdu(conn, signing_key, iov, n_vec, signature);
+ ksmbd_sign_smb3_pdu(conn, signing_algorithm, signing_key, iov, n_vec,
+ signature);
memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
}
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH v5 3/8] ksmbd: use the channel signing key for session setup on a bound channel
2026-11-12 4:46 [PATCH v5 0/8] ksmbd: verify session setup requests before signing with their key Michail Andreas Michalopoulos
2026-11-12 4:46 ` [PATCH v5 1/8] ksmbd: verify the signature of session binding requests Michail Andreas Michalopoulos
2026-11-12 4:46 ` [PATCH v5 2/8] ksmbd: sign with the negotiated SMB 3.1.1 signing algorithm Michail Andreas Michalopoulos
@ 2026-11-12 4:46 ` Michail Andreas Michalopoulos
2026-11-12 4:46 ` [PATCH v5 4/8] ksmbd: refuse a binding request on a connection that is already a channel Michail Andreas Michalopoulos
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Michail Andreas Michalopoulos @ 2026-11-12 4:46 UTC (permalink / raw)
To: linkinjeon, stfrench
Cc: icb, senozhatsky, tom, chenxiaosong, ematsumiya, lsahlber,
hyc.lee, linux-cifs, linux-kernel, Michail Andreas Michalopoulos,
stable
smb3_check_sign_req() always uses the session signing key for
SESSION_SETUP, and smb3_set_sign_rsp() does for every SESSION_SETUP
response but the final one of a binding. That is right for a binding
request and its non-final responses (MS-SMB2 3.3.5.2.4, 3.3.5.5.3), but
a reauthentication sent on a channel that is already bound is then also
verified and signed with the session key instead of that channel's
key.
Use the session key for binding requests, for their non-final
responses, and for SESSION_SETUP on a connection that is not yet a
channel of the session. Use the channel key for everything else on a
channel, as the Samba client and smbd do. MS-SMB2 does not give one key
for a reauthentication on such a channel: 3.3.5.2.4 has the server
verify the request with Channel.SigningKey, while 3.2.4.1.1 has the
client sign it with Session.SigningKey. 3.3.4.1.1 has the server sign a
SESSION_SETUP response with Session.SigningKey unless its status is
STATUS_SUCCESS, and 3.3.5.5.3 the final response of a non-binding
session setup too. The two keys only differ on a channel that was bound
to the session, and that case has not been tested against Windows.
The signature of a reauthentication was not checked at all, because
SESSION_SETUP is exempt from the check in __process_request(). A
reauthentication of a live session with a forged signature therefore
got STATUS_MORE_PROCESSING_REQUIRED signed with the session key, with or
without multichannel: the same kind of signing oracle as an unverified
binding request. Verify it with the same helper as binding requests.
Also refuse an unsigned reauthentication of a session that requires
signing, as smbtorture's bind_negative tests expect from Windows,
unless the request arrived encrypted on a session that uses
encryption. A session that negotiated encryption has sess->sign cleared
even when signing was required when it was set up, so remember that in
sess->sign_required and take it and enforced encryption into account
too; merely having negotiated encryption is not enough, since clients
such as the Linux one always offer it and then send unsigned,
unencrypted requests. Do not expire the session when a check fails,
since the request did not come from its owner.
The checks apply to every session that has been authenticated before,
which is one that has a user, including while a reauthentication of an
expired session is in progress.
smb2_sess_setup() copied conn->binding into
work->session_setup_binding after its error path had cleared it, so the
response to a failed binding request was signed and added to the
preauth integrity hash as if the request had not been a binding one.
Copy it before that.
An AI assistant was used to draft the patch and as an independent
reviewer.
Fixes: e2f34481b24d ("cifsd: add server-side procedures for SMB3")
Cc: stable@vger.kernel.org
Signed-off-by: Michail Andreas Michalopoulos <mike.michalo@gmail.com>
---
fs/smb/server/mgmt/user_session.h | 2 +
fs/smb/server/smb2pdu.c | 118 +++++++++++++++++++++++-------
2 files changed, 92 insertions(+), 28 deletions(-)
diff --git a/fs/smb/server/mgmt/user_session.h b/fs/smb/server/mgmt/user_session.h
index 931ca102b8392..423f0a10315af 100644
--- a/fs/smb/server/mgmt/user_session.h
+++ b/fs/smb/server/mgmt/user_session.h
@@ -59,6 +59,8 @@ struct ksmbd_session {
unsigned int flags;
bool sign;
+ /* signing was required at setup; sign is cleared with encryption */
+ bool sign_required;
bool enc;
bool tearing_down;
diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index d15024d34f3d6..7b07108a48c15 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -2406,8 +2406,10 @@ static int ntlm_authenticate(struct ksmbd_work *work,
if ((rsp->SessionFlags != SMB2_SESSION_FLAG_IS_GUEST_LE &&
(conn->sign || server_conf.enforced_signing)) ||
- (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED))
+ (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED)) {
sess->sign = true;
+ sess->sign_required = true;
+ }
if (smb3_encryption_negotiated(conn) &&
!(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) {
@@ -2510,8 +2512,10 @@ static int krb5_authenticate(struct ksmbd_work *work,
if ((rsp->SessionFlags != SMB2_SESSION_FLAG_IS_GUEST_LE &&
(conn->sign || server_conf.enforced_signing)) ||
- (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED))
+ (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED)) {
sess->sign = true;
+ sess->sign_required = true;
+ }
if (smb3_encryption_negotiated(conn) &&
!(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) {
@@ -2561,22 +2565,47 @@ static int krb5_authenticate(struct ksmbd_work *work,
#endif
/*
- * A binding request must be signed with the signing key of the session it
- * binds to (MS-SMB2 3.3.5.2.4). Check it before the request is processed
- * and before any response is signed with that key.
+ * SESSION_SETUP is not covered by the signature check in __process_request(),
+ * because work->sess is not set for it. A binding request must be signed
+ * with the signing key of the session it binds to (MS-SMB2 3.3.5.2.4), and a
+ * signed reauthentication with the key of the channel it arrives on. Check
+ * them before the request is processed and before any response is signed
+ * with that key.
*/
-static bool smb2_check_binding_sign_req(struct ksmbd_work *work,
- struct ksmbd_session *sess)
+static bool smb2_check_sess_setup_sign_req(struct ksmbd_work *work,
+ struct ksmbd_session *sess)
{
struct ksmbd_session *prev_sess = work->sess;
int ret;
work->sess = sess;
- ret = smb3_check_sign_req(work);
+ /* The key, and so the way it signs, belongs to the session */
+ if (sess->dialect >= SMB30_PROT_ID)
+ ret = smb3_check_sign_req(work);
+ else
+ ret = smb2_check_sign_req(work);
work->sess = prev_sess;
return ret;
}
+/*
+ * Whether a reauthentication of @sess must be signed or encrypted. A session
+ * that negotiated encryption has sess->sign cleared even when signing was
+ * required when it was set up, which sess->sign_required remembers, while
+ * encryption itself is only enforced when configured. Merely having
+ * negotiated encryption does not count: clients such as the Linux one always
+ * offer it, and then legitimately send unsigned, unencrypted requests.
+ */
+static bool smb2_sess_needs_protection(struct ksmbd_session *sess)
+{
+ if (sess->sign)
+ return true;
+ if (!sess->enc)
+ return false;
+ return sess->sign_required ||
+ (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION);
+}
+
int smb2_sess_setup(struct ksmbd_work *work)
{
struct ksmbd_conn *conn = work->conn;
@@ -2670,7 +2699,7 @@ int smb2_sess_setup(struct ksmbd_work *work)
}
/* As part of receiving the request (MS-SMB2 3.3.5.2.4) */
- if (!smb2_check_binding_sign_req(work, sess)) {
+ if (!smb2_check_sess_setup_sign_req(work, sess)) {
rc = -EKEYREJECTED;
goto out_err;
}
@@ -2761,6 +2790,29 @@ int smb2_sess_setup(struct ksmbd_work *work)
goto out_err;
}
+ /*
+ * A reauthentication of a session that has keys must prove it
+ * comes from the session's owner: a signed request must verify,
+ * and a session that requires signing or encryption must not
+ * accept an unsigned request unless it arrived encrypted on a
+ * session that uses encryption, which has already authenticated
+ * it. The session has keys once it has been authenticated and
+ * so has a user, including while a reauthentication is in
+ * progress. Do not expire the session if the check fails: the
+ * request did not come from its owner.
+ */
+ if ((READ_ONCE(sess->state) != SMB2_SESSION_IN_PROGRESS ||
+ sess->user) &&
+ ((req->hdr.Flags & SMB2_FLAGS_SIGNED) ?
+ !smb2_check_sess_setup_sign_req(work, sess) :
+ (!(work->encrypted && sess->enc) &&
+ smb2_sess_needs_protection(sess)))) {
+ rc = -EKEYREJECTED;
+ ksmbd_user_session_put(sess);
+ sess = NULL;
+ goto out_err;
+ }
+
if (work->session_setup_reauth)
WRITE_ONCE(sess->state, SMB2_SESSION_IN_PROGRESS);
@@ -2843,6 +2895,8 @@ int smb2_sess_setup(struct ksmbd_work *work)
}
out_err:
+ /* The error path clears conn->binding; response processing needs it */
+ work->session_setup_binding = conn->binding;
if (rc == -EINVAL)
rsp->hdr.Status = STATUS_INVALID_PARAMETER;
else if (rc == -ENOENT)
@@ -2949,7 +3003,6 @@ int smb2_sess_setup(struct ksmbd_work *work)
rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
}
- work->session_setup_binding = conn->binding;
if (authenticated) {
/*
* Draining the previous session drops srv_mutex. Finish using the
@@ -12991,22 +13044,24 @@ int smb3_check_sign_req(struct ksmbd_work *work)
* A key must be used with the algorithm it was generated for: the
* session key with the one of the connection that created the
* session, which can differ from this connection's when binding.
+ *
+ * A binding request, or a SESSION_SETUP on a connection that is not
+ * yet a channel of the session, is verified with the session key. A
+ * request on an existing channel, including a reauthentication, is
+ * verified with that channel's key, so that the session key is not
+ * used at the MessageIds of another connection.
*/
- if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
+ chann = lookup_chann_list(work->sess, conn);
+ if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE &&
+ (!chann || (((struct smb2_sess_setup_req *)hdr)->Flags &
+ SMB2_SESSION_REQ_FLAG_BINDING))) {
signing_key = work->sess->smb3signingkey;
signing_algorithm = work->sess->signing_algorithm;
+ } else if (chann) {
+ signing_key = chann->smb3signingkey;
+ signing_algorithm = conn->signing_algorithm;
} else {
- chann = lookup_chann_list(work->sess, conn);
- if (!chann) {
- if (le16_to_cpu(hdr->Command) != SMB2_SESSION_SETUP_HE ||
- !(hdr->Flags & SMB2_FLAGS_SIGNED))
- return 0;
- signing_key = work->sess->smb3signingkey;
- signing_algorithm = work->sess->signing_algorithm;
- } else {
- signing_key = chann->smb3signingkey;
- signing_algorithm = conn->signing_algorithm;
- }
+ return 0;
}
if (!signing_key) {
@@ -13052,17 +13107,24 @@ void smb3_set_sign_rsp(struct ksmbd_work *work)
hdr = ksmbd_resp_buf_curr(work);
command = le16_to_cpu(hdr->Command);
+ /*
+ * Responses to binding requests, other than the final one, are signed
+ * with the session key (MS-SMB2 3.3.5.5.3), as is any SESSION_SETUP
+ * response on a connection that is not yet a channel. Everything else
+ * on a channel, including a reauthentication on a secondary channel,
+ * is signed with that channel's key.
+ */
+ chann = lookup_chann_list(work->sess, work->conn);
if (command == SMB2_SESSION_SETUP_HE &&
- (!work->session_setup_binding || hdr->Status != STATUS_SUCCESS)) {
+ (!chann || (work->session_setup_binding &&
+ hdr->Status != STATUS_SUCCESS))) {
signing_key = work->sess->smb3signingkey;
signing_algorithm = work->sess->signing_algorithm;
- } else {
- chann = lookup_chann_list(work->sess, work->conn);
- if (!chann) {
- return;
- }
+ } else if (chann) {
signing_key = chann->smb3signingkey;
signing_algorithm = conn->signing_algorithm;
+ } else {
+ return;
}
if (!signing_key)
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH v5 4/8] ksmbd: refuse a binding request on a connection that is already a channel
2026-11-12 4:46 [PATCH v5 0/8] ksmbd: verify session setup requests before signing with their key Michail Andreas Michalopoulos
` (2 preceding siblings ...)
2026-11-12 4:46 ` [PATCH v5 3/8] ksmbd: use the channel signing key for session setup on a bound channel Michail Andreas Michalopoulos
@ 2026-11-12 4:46 ` Michail Andreas Michalopoulos
2026-11-12 4:46 ` [PATCH v5 5/8] ksmbd: check a request before signing STATUS_NETWORK_SESSION_EXPIRED Michail Andreas Michalopoulos
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Michail Andreas Michalopoulos @ 2026-11-12 4:46 UTC (permalink / raw)
To: linkinjeon, stfrench
Cc: icb, senozhatsky, tom, chenxiaosong, ematsumiya, lsahlber,
hyc.lee, linux-cifs, linux-kernel, Michail Andreas Michalopoulos,
stable
smb2_sess_setup() refuses a binding request for a session that the
connection already uses, but is_ksmbd_session_in_connection() only
knows the sessions created on the connection, not the ones it was bound
to as a channel. A connection that is already a channel of a session
can therefore bind to it again. The new binding legs are verified and
signed with the session key at that connection's MessageIds, and on
completion generate_signingkey() replaces the channel's signing key,
while register_session_channel() keeps the old channel entry.
Refuse such a request with STATUS_REQUEST_NOT_ACCEPTED, as MS-SMB2
3.3.5.5 does for a session that is already in Connection.SessionTable,
which a binding adds the session to (3.3.5.5.3).
An AI assistant was used to draft the patch and as an independent
reviewer.
Fixes: f5a544e3bab7 ("ksmbd: add support for SMB3 multichannel")
Cc: stable@vger.kernel.org
Signed-off-by: Michail Andreas Michalopoulos <mike.michalo@gmail.com>
---
fs/smb/server/smb2pdu.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index 7b07108a48c15..0271ee4753236 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -2722,7 +2722,9 @@ int smb2_sess_setup(struct ksmbd_work *work)
goto out_err;
}
- if (is_ksmbd_session_in_connection(conn, sess_id)) {
+ /* This connection must not already be using the session */
+ if (is_ksmbd_session_in_connection(conn, sess_id) ||
+ lookup_chann_list(sess, conn)) {
rc = -EACCES;
goto out_err;
}
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH v5 5/8] ksmbd: check a request before signing STATUS_NETWORK_SESSION_EXPIRED
2026-11-12 4:46 [PATCH v5 0/8] ksmbd: verify session setup requests before signing with their key Michail Andreas Michalopoulos
` (3 preceding siblings ...)
2026-11-12 4:46 ` [PATCH v5 4/8] ksmbd: refuse a binding request on a connection that is already a channel Michail Andreas Michalopoulos
@ 2026-11-12 4:46 ` Michail Andreas Michalopoulos
2026-11-12 4:46 ` [PATCH v5 6/8] ksmbd: use the current command's header for signing in a compound Michail Andreas Michalopoulos
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Michail Andreas Michalopoulos @ 2026-11-12 4:46 UTC (permalink / raw)
To: linkinjeon, stfrench
Cc: icb, senozhatsky, tom, chenxiaosong, ematsumiya, lsahlber,
hyc.lee, linux-cifs, linux-kernel, Michail Andreas Michalopoulos,
stable
When a session's Kerberos ticket has expired, smb2_check_user_session()
returns -EKEYEXPIRED and __handle_ksmbd_work() signs the
STATUS_NETWORK_SESSION_EXPIRED response if the request has
SMB2_FLAGS_SIGNED set. This happens before __process_request(), which
is where the MessageId and the request signature are checked. So the
response is signed with the session's key for a request whose
signature was never verified, at a MessageId that was never checked.
Anyone who can inject requests into the connection can then get
responses signed with that key at MessageIds of their choice: a
signing oracle. A Kerberos reauthentication keeps the session's keys, so
the effect lasts for the rest of the session.
MS-SMB2 checks the MessageId (3.3.5.2.3) and the signature (3.3.5.2.4)
before the session (3.3.5.2.9). Do the same: sign the response only if
the request passes both checks, and otherwise answer with an unsigned
STATUS_INVALID_PARAMETER or STATUS_ACCESS_DENIED, as
__process_request() does.
For a later command of a compound, the response that is current at this
point is still the previous one, so the old code re-signed that response
instead. Leave the expired response of such a command unsigned.
An AI assistant was used to draft the patch and as an independent
reviewer.
Fixes: a47634cd729b ("ksmbd: expire SMB sessions when Kerberos tickets expire")
Reported-by: Ian Bridges <icb@fastmail.org>
Cc: stable@vger.kernel.org
Signed-off-by: Michail Andreas Michalopoulos <mike.michalo@gmail.com>
---
fs/smb/server/server.c | 28 +++++++++++++++++++++++++++-
1 file changed, 27 insertions(+), 1 deletion(-)
diff --git a/fs/smb/server/server.c b/fs/smb/server/server.c
index 51aa981bcef4e..ca0c78447f454 100644
--- a/fs/smb/server/server.c
+++ b/fs/smb/server/server.c
@@ -107,6 +107,32 @@ static inline int check_conn_state(struct ksmbd_work *work)
return 0;
}
+/*
+ * The keys of a session whose Kerberos ticket has expired are still valid,
+ * so STATUS_NETWORK_SESSION_EXPIRED is signed. Check the MessageId and the
+ * signature first (MS-SMB2 3.3.5.2.3 and 3.3.5.2.4), so that the key never
+ * signs a response to a request it did not sign.
+ */
+static void ksmbd_sign_expired_rsp(struct ksmbd_work *work)
+{
+ struct ksmbd_conn *conn = work->conn;
+
+ /*
+ * For a later command of a compound, the current response is still
+ * the previous one here, so signing would corrupt that response.
+ * Leave this one unsigned.
+ */
+ if (work->next_smb2_rcv_hdr_off)
+ return;
+
+ if (ksmbd_verify_smb_message(work))
+ conn->ops->set_rsp_status(work, STATUS_INVALID_PARAMETER);
+ else if (!conn->ops->check_sign_req(work))
+ conn->ops->set_rsp_status(work, STATUS_ACCESS_DENIED);
+ else
+ conn->ops->set_sign_rsp(work);
+}
+
#define SERVER_HANDLER_CONTINUE 0
#define SERVER_HANDLER_ABORT 1
@@ -265,7 +291,7 @@ static void __handle_ksmbd_work(struct ksmbd_work *work,
rsp_hdr = ksmbd_resp_buf_curr(work);
if (rc == -EKEYEXPIRED && work->sess &&
conn->ops->set_sign_rsp)
- conn->ops->set_sign_rsp(work);
+ ksmbd_sign_expired_rsp(work);
else
rsp_hdr->Flags |= SMB2_FLAGS_SIGNED;
}
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH v5 6/8] ksmbd: use the current command's header for signing in a compound
2026-11-12 4:46 [PATCH v5 0/8] ksmbd: verify session setup requests before signing with their key Michail Andreas Michalopoulos
` (4 preceding siblings ...)
2026-11-12 4:46 ` [PATCH v5 5/8] ksmbd: check a request before signing STATUS_NETWORK_SESSION_EXPIRED Michail Andreas Michalopoulos
@ 2026-11-12 4:46 ` Michail Andreas Michalopoulos
2026-11-12 4:46 ` [PATCH v5 7/8] ksmbd: consume the sequence number of a request on a failed session Michail Andreas Michalopoulos
2026-11-12 4:46 ` [PATCH v5 8/8] ksmbd: account for the credits " Michail Andreas Michalopoulos
7 siblings, 0 replies; 9+ messages in thread
From: Michail Andreas Michalopoulos @ 2026-11-12 4:46 UTC (permalink / raw)
To: linkinjeon, stfrench
Cc: icb, senozhatsky, tom, chenxiaosong, ematsumiya, lsahlber,
hyc.lee, linux-cifs, linux-kernel, Michail Andreas Michalopoulos,
stable
Three places in the signing path read the wrong command of a compound
request instead of the command being processed:
- smb2_is_sign_req() always inspects smb_get_msg(work->request_buf)->Flags,
so the per-command signing decision in __process_request() follows the
first command's SMB2_FLAGS_SIGNED bit rather than the current one.
smb2_check_sign_req() already uses ksmbd_req_buf_next() for a chained
command; do the same here.
- When check_user_session() fails on a chained command,
set_smb2_rsp_status() writes the error into ksmbd_resp_buf_next(), but
__handle_ksmbd_work() then set SMB2_FLAGS_SIGNED on
ksmbd_resp_buf_curr(), i.e. the previous command's response. Flag the
response that actually holds the error.
- After each command, __handle_ksmbd_work() decides whether to sign its
response only after is_chained_smb2_message() has moved on to the next
command, so smb2_is_sign_req() and smb3_11_final_sess_setup_resp()
would see the next command's request and response. Decide before
moving on, and still sign afterwards, once NextCommand and the padding
of the response are final.
When the session does not require signing, this mattered in three ways:
an unsigned command after a signed one was verified anyway and refused
with STATUS_ACCESS_DENIED; a signed command after an unsigned one was
processed without its signature being checked, so a wrong signature
was accepted, contrary to MS-SMB2 3.3.5.2.4; and the response to that
signed command was not signed. When the session requires signing, an
unsigned first command is still refused and every signed command was
and is verified against its own header, so mandatory signing is not
bypassed.
An AI assistant was used to draft the patch and as an independent
reviewer.
Fixes: e2f34481b24d ("cifsd: add server-side procedures for SMB3")
Cc: stable@vger.kernel.org
Signed-off-by: Michail Andreas Michalopoulos <mike.michalo@gmail.com>
---
fs/smb/server/server.c | 27 +++++++++++++++++++++++----
fs/smb/server/smb2pdu.c | 4 ++++
2 files changed, 27 insertions(+), 4 deletions(-)
diff --git a/fs/smb/server/server.c b/fs/smb/server/server.c
index ca0c78447f454..32bed8672dd2e 100644
--- a/fs/smb/server/server.c
+++ b/fs/smb/server/server.c
@@ -213,6 +213,7 @@ static void __handle_ksmbd_work(struct ksmbd_work *work,
u16 command = 0;
int rc;
bool is_chained = false;
+ bool sign_rsp;
if (conn->ops->is_transform_hdr &&
conn->ops->is_transform_hdr(work->request_buf)) {
@@ -288,7 +289,15 @@ static void __handle_ksmbd_work(struct ksmbd_work *work,
if (conn->ops->is_sign_req(work, conn->ops->get_cmd_val(work))) {
struct smb2_hdr *rsp_hdr;
- rsp_hdr = ksmbd_resp_buf_curr(work);
+ /*
+ * set_smb2_rsp_status() wrote the error
+ * into the next response for a chained
+ * command, so flag that one, not curr.
+ */
+ if (work->next_smb2_rcv_hdr_off)
+ rsp_hdr = ksmbd_resp_buf_next(work);
+ else
+ rsp_hdr = ksmbd_resp_buf_curr(work);
if (rc == -EKEYEXPIRED && work->sess &&
conn->ops->set_sign_rsp)
ksmbd_sign_expired_rsp(work);
@@ -343,11 +352,21 @@ static void __handle_ksmbd_work(struct ksmbd_work *work,
smb2_complete_request_open(work);
+ /*
+ * Decide now whether to sign this command's response:
+ * is_chained_smb2_message() moves on to the next command of a
+ * compound, after which the request and response helpers see
+ * that command. The response is still signed afterwards, once
+ * its NextCommand and padding are final.
+ */
+ sign_rsp = work->sess &&
+ (work->sess->sign ||
+ smb3_11_final_sess_setup_resp(work) ||
+ conn->ops->is_sign_req(work, command));
+
is_chained = is_chained_smb2_message(work);
- if (work->sess &&
- (work->sess->sign || smb3_11_final_sess_setup_resp(work) ||
- conn->ops->is_sign_req(work, command))) {
+ if (sign_rsp) {
/*
* A refused binding request is answered with the key
* of the session, which can be of another dialect
diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index 0271ee4753236..895afb41adacf 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -12904,6 +12904,10 @@ bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command)
{
struct smb2_hdr *rcv_hdr2 = smb_get_msg(work->request_buf);
+ /* In a compound, test the current command, not the first one. */
+ if (work->next_smb2_rcv_hdr_off)
+ rcv_hdr2 = ksmbd_req_buf_next(work);
+
if ((rcv_hdr2->Flags & SMB2_FLAGS_SIGNED) &&
command != SMB2_NEGOTIATE_HE)
return true;
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH v5 7/8] ksmbd: consume the sequence number of a request on a failed session
2026-11-12 4:46 [PATCH v5 0/8] ksmbd: verify session setup requests before signing with their key Michail Andreas Michalopoulos
` (5 preceding siblings ...)
2026-11-12 4:46 ` [PATCH v5 6/8] ksmbd: use the current command's header for signing in a compound Michail Andreas Michalopoulos
@ 2026-11-12 4:46 ` Michail Andreas Michalopoulos
2026-11-12 4:46 ` [PATCH v5 8/8] ksmbd: account for the credits " Michail Andreas Michalopoulos
7 siblings, 0 replies; 9+ messages in thread
From: Michail Andreas Michalopoulos @ 2026-11-12 4:46 UTC (permalink / raw)
To: linkinjeon, stfrench
Cc: icb, senozhatsky, tom, chenxiaosong, ematsumiya, lsahlber,
hyc.lee, linux-cifs, linux-kernel, Michail Andreas Michalopoulos,
stable
__handle_ksmbd_work() calls check_user_session() before
__process_request(). When the session is unknown, invalid or expired, it
sets the error status and jumps straight to send, skipping
__process_request() and therefore ksmbd_verify_smb_message(), which is
where smb2_check_sequence_number() consumes the request's MessageId from
the command sequence window and refuses a replay.
Only a signed request on an expired session had its MessageId checked,
by ksmbd_sign_expired_rsp(). Any other failed-session request (unsigned,
encrypted, or on an unknown or invalid session) left its MessageId
unconsumed: conn->seq_low then stays pinned at it while conn->seq_high
advances as credits are granted, until the window of
KSMBD_CMD_SEQ_WINDOW ids is full and the connection stalls. A replayed
MessageId on such a session is not refused either.
Verify the message once at the start of the failed-session path, as
__process_request() would have, and drop the now redundant check from
ksmbd_sign_expired_rsp(); verifying twice would consume the MessageId
twice and refuse a genuine request.
The commands of a compound that follow the failed one are still neither
answered nor consumed, as after any error that ends a compound; that is
a separate problem.
An AI assistant was used to draft the patch and as an independent
reviewer.
Fixes: fd8c97d7c1cc ("ksmbd: implement the command sequence window")
Cc: stable@vger.kernel.org
Signed-off-by: Michail Andreas Michalopoulos <mike.michalo@gmail.com>
---
fs/smb/server/server.c | 26 ++++++++++++++++++++------
1 file changed, 20 insertions(+), 6 deletions(-)
diff --git a/fs/smb/server/server.c b/fs/smb/server/server.c
index 32bed8672dd2e..52eee04e0d93a 100644
--- a/fs/smb/server/server.c
+++ b/fs/smb/server/server.c
@@ -109,9 +109,10 @@ static inline int check_conn_state(struct ksmbd_work *work)
/*
* The keys of a session whose Kerberos ticket has expired are still valid,
- * so STATUS_NETWORK_SESSION_EXPIRED is signed. Check the MessageId and the
- * signature first (MS-SMB2 3.3.5.2.3 and 3.3.5.2.4), so that the key never
- * signs a response to a request it did not sign.
+ * so STATUS_NETWORK_SESSION_EXPIRED is signed. The caller has already
+ * checked the MessageId (MS-SMB2 3.3.5.2.3); check the signature
+ * (3.3.5.2.4) too, so that the key never signs a response to a request it
+ * did not sign.
*/
static void ksmbd_sign_expired_rsp(struct ksmbd_work *work)
{
@@ -125,9 +126,7 @@ static void ksmbd_sign_expired_rsp(struct ksmbd_work *work)
if (work->next_smb2_rcv_hdr_off)
return;
- if (ksmbd_verify_smb_message(work))
- conn->ops->set_rsp_status(work, STATUS_INVALID_PARAMETER);
- else if (!conn->ops->check_sign_req(work))
+ if (!conn->ops->check_sign_req(work))
conn->ops->set_rsp_status(work, STATUS_ACCESS_DENIED);
else
conn->ops->set_sign_rsp(work);
@@ -277,6 +276,21 @@ static void __handle_ksmbd_work(struct ksmbd_work *work,
if (conn->ops->check_user_session) {
rc = conn->ops->check_user_session(work);
if (rc < 0) {
+ /*
+ * The error reply below skips
+ * __process_request(), so check the message
+ * and consume its sequence number here, as it
+ * would. Otherwise a request on an unknown,
+ * invalid or expired session leaves its
+ * MessageId unconsumed, which pins the command
+ * sequence window, and a replayed MessageId
+ * is not refused.
+ */
+ if (ksmbd_verify_smb_message(work)) {
+ conn->ops->set_rsp_status(work,
+ STATUS_INVALID_PARAMETER);
+ goto send;
+ }
if (rc == -EINVAL)
conn->ops->set_rsp_status(work,
STATUS_INVALID_PARAMETER);
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH v5 8/8] ksmbd: account for the credits of a request on a failed session
2026-11-12 4:46 [PATCH v5 0/8] ksmbd: verify session setup requests before signing with their key Michail Andreas Michalopoulos
` (6 preceding siblings ...)
2026-11-12 4:46 ` [PATCH v5 7/8] ksmbd: consume the sequence number of a request on a failed session Michail Andreas Michalopoulos
@ 2026-11-12 4:46 ` Michail Andreas Michalopoulos
7 siblings, 0 replies; 9+ messages in thread
From: Michail Andreas Michalopoulos @ 2026-11-12 4:46 UTC (permalink / raw)
To: linkinjeon, stfrench
Cc: icb, senozhatsky, tom, chenxiaosong, ematsumiya, lsahlber,
hyc.lee, linux-cifs, linux-kernel, Michail Andreas Michalopoulos,
stable
A request on an unknown, invalid or expired session is answered by
__handle_ksmbd_work() without reaching the code that calls
smb2_set_rsp_credits(). Its credit charge is never taken off
conn->total_credits, and the response grants no credits.
The client has spent those credits all the same. Each such request
leaves ksmbd's count of the client's credits too high by its charge,
which reduces what later responses can grant, and a client that keeps
getting these errors runs out of credits. Since the command sequence
window was added in commit fd8c97d7c1cc ("ksmbd: implement the command
sequence window"), this also ends the connection: after NEGOTIATE, the
second request that names an unknown session is already outside the
window. A client whose session has expired loses a credit with every
request, until it can no longer send the SESSION_SETUP that would
reauthenticate it.
MS-SMB2 has the server consume a credit for every request but CANCEL
(3.3.4.1.2) and put the credits it grants in an error response as in
any other (3.3.4.4). Charge the request's credits and grant the ones it
asks for, as for a request on a valid session, before the response is
signed, except for an SMB2 CANCEL, which consumes no credit. This
response ends the work, so it reports the credits granted for the whole
compound.
An AI assistant was used to draft the patch and as an independent
reviewer.
Fixes: 0626e6641f6b ("cifsd: add server handler for central processing and tranport layers")
Cc: stable@vger.kernel.org
Signed-off-by: Michail Andreas Michalopoulos <mike.michalo@gmail.com>
---
fs/smb/server/server.c | 41 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
diff --git a/fs/smb/server/server.c b/fs/smb/server/server.c
index 52eee04e0d93a..1522eb560e838 100644
--- a/fs/smb/server/server.c
+++ b/fs/smb/server/server.c
@@ -132,6 +132,36 @@ static void ksmbd_sign_expired_rsp(struct ksmbd_work *work)
conn->ops->set_sign_rsp(work);
}
+/*
+ * Charge the credits of a request that is answered without going through
+ * __process_request(), and grant the ones it asked for. This response ends
+ * the work, so it carries the credits granted for the whole compound.
+ */
+static int ksmbd_set_failed_rsp_credits(struct ksmbd_work *work)
+{
+ struct ksmbd_conn *conn = work->conn;
+ struct smb2_hdr *rsp_hdr;
+ int rc;
+
+ /* An SMB2 CANCEL consumes no credit (MS-SMB2 3.3.4.1.2) */
+ if (!conn->ops->set_rsp_credits ||
+ conn->ops->get_cmd_val(work) == SMB2_CANCEL_HE)
+ return 0;
+
+ spin_lock(&conn->credits_lock);
+ rc = conn->ops->set_rsp_credits(work);
+ spin_unlock(&conn->credits_lock);
+ if (rc < 0)
+ return rc;
+
+ if (work->next_smb2_rcv_hdr_off)
+ rsp_hdr = ksmbd_resp_buf_next(work);
+ else
+ rsp_hdr = ksmbd_resp_buf_curr(work);
+ rsp_hdr->CreditRequest = cpu_to_le16(work->credits_granted);
+ return 0;
+}
+
#define SERVER_HANDLER_CONTINUE 0
#define SERVER_HANDLER_ABORT 1
@@ -300,6 +330,17 @@ static void __handle_ksmbd_work(struct ksmbd_work *work,
else
conn->ops->set_rsp_status(work,
STATUS_USER_SESSION_DELETED);
+ /*
+ * The request has used its credits, so charge
+ * them and grant new ones in this response, as
+ * for a request on a valid session, and before
+ * the response is signed.
+ */
+ if (ksmbd_set_failed_rsp_credits(work)) {
+ conn->ops->set_rsp_status(work,
+ STATUS_INVALID_PARAMETER);
+ goto send;
+ }
if (conn->ops->is_sign_req(work, conn->ops->get_cmd_val(work))) {
struct smb2_hdr *rsp_hdr;
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread