* [PATCH v4 1/6] ksmbd: verify the signature of session binding requests
2026-11-10 16:58 [PATCH v4 0/6] ksmbd: verify session setup requests before signing with their key Michail Andreas Michalopoulos
@ 2026-11-10 16:58 ` Michail Andreas Michalopoulos
2026-11-10 16:58 ` [PATCH v4 2/6] ksmbd: sign with the negotiated SMB 3.1.1 signing algorithm Michail Andreas Michalopoulos
` (5 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Michail Andreas Michalopoulos @ 2026-11-10 16:58 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. With AES-CMAC this is a signing oracle. With
AES-GMAC the nonce is the MessageId, so it also lets two different
messages be authenticated under the same key and nonce, which reveals
the GHASH key and allows forging signatures for that session.
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. Do so before the request is processed further. 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.
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.
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/smb2pdu.c | 29 ++++++++++++++++++++++++++++-
1 file changed, 28 insertions(+), 1 deletion(-)
diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index b084cca8813cc..053f3eb398f5a 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -2558,6 +2558,24 @@ 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 smb2_hdr *hdr,
+ 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;
@@ -2640,6 +2658,11 @@ int smb2_sess_setup(struct ksmbd_work *work)
goto out_err;
}
+ if (!smb2_check_binding_sign_req(work, &req->hdr, sess)) {
+ rc = -EKEYREJECTED;
+ goto out_err;
+ }
+
if (memcmp(conn->ClientGUID, sess->ClientGUID,
SMB2_CLIENT_GUID_SIZE)) {
rc = -ENOENT;
@@ -2675,7 +2698,7 @@ 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) {
@@ -12853,6 +12876,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 +12997,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] 8+ messages in thread* [PATCH v4 2/6] ksmbd: sign with the negotiated SMB 3.1.1 signing algorithm
2026-11-10 16:58 [PATCH v4 0/6] ksmbd: verify session setup requests before signing with their key Michail Andreas Michalopoulos
2026-11-10 16:58 ` [PATCH v4 1/6] ksmbd: verify the signature of session binding requests Michail Andreas Michalopoulos
@ 2026-11-10 16:58 ` Michail Andreas Michalopoulos
2026-11-10 16:58 ` [PATCH v4 3/6] ksmbd: use the channel signing key for session setup on a bound channel Michail Andreas Michalopoulos
` (4 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Michail Andreas Michalopoulos @ 2026-11-10 16:58 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.
Since connections of the same session can now use different
algorithms, each signing key also has to be used with the algorithm it
was created for. The session signing key belongs to the connection
that set up the session, and MS-SMB2 3.3.5.5 requires a binding
request on a new channel to be verified with it. 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 053f3eb398f5a..ddaa98055154a 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;
@@ -12953,6 +12955,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];
@@ -12972,8 +12975,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) {
@@ -12981,8 +12990,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;
}
}
@@ -12996,7 +13007,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);
@@ -13023,6 +13035,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);
@@ -13030,12 +13043,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)
@@ -13046,7 +13061,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] 8+ messages in thread* [PATCH v4 3/6] ksmbd: use the channel signing key for session setup on a bound channel
2026-11-10 16:58 [PATCH v4 0/6] ksmbd: verify session setup requests before signing with their key Michail Andreas Michalopoulos
2026-11-10 16:58 ` [PATCH v4 1/6] ksmbd: verify the signature of session binding requests Michail Andreas Michalopoulos
2026-11-10 16:58 ` [PATCH v4 2/6] ksmbd: sign with the negotiated SMB 3.1.1 signing algorithm Michail Andreas Michalopoulos
@ 2026-11-10 16:58 ` Michail Andreas Michalopoulos
2026-11-10 16:58 ` [PATCH v4 4/6] ksmbd: check a request before signing STATUS_NETWORK_SESSION_EXPIRED Michail Andreas Michalopoulos
` (3 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Michail Andreas Michalopoulos @ 2026-11-10 16:58 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() and smb3_set_sign_rsp() always use the session
signing key for SESSION_SETUP. 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. Samba does the same. MS-SMB2 is ambiguous here: 3.2.4.1.1
has the client sign any session setup with Session.SigningKey, while
3.3.5.2.4 has the server verify non-binding requests with
Channel.SigningKey.
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 is encrypted. A session that negotiated encryption
has sess->sign cleared even when the connection required signing, so
take that 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.
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/smb2pdu.c | 112 ++++++++++++++++++++++++++++++----------
1 file changed, 86 insertions(+), 26 deletions(-)
diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index ddaa98055154a..293e660265c95 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -2561,23 +2561,51 @@ 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 smb2_hdr *hdr,
- struct ksmbd_session *sess)
+static bool smb2_check_sess_setup_sign_req(struct ksmbd_work *work,
+ struct smb2_hdr *hdr,
+ 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 on the connection, while encryption itself is only enforced when
+ * configured, so check what the session actually requires. 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_conn *conn,
+ struct ksmbd_session *sess)
+{
+ if (sess->sign)
+ return true;
+ if (!sess->enc)
+ return false;
+ if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION)
+ return true;
+ return sess->user && !user_guest(sess->user) &&
+ (conn->sign || server_conf.enforced_signing);
+}
+
int smb2_sess_setup(struct ksmbd_work *work)
{
struct ksmbd_conn *conn = work->conn;
@@ -2660,7 +2688,7 @@ int smb2_sess_setup(struct ksmbd_work *work)
goto out_err;
}
- if (!smb2_check_binding_sign_req(work, &req->hdr, sess)) {
+ if (!smb2_check_sess_setup_sign_req(work, &req->hdr, sess)) {
rc = -EKEYREJECTED;
goto out_err;
}
@@ -2754,6 +2782,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 is encrypted, since
+ * encryption has already authenticated it. The session has
+ * keys once this connection is one of its channels, 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 ||
+ lookup_chann_list(sess, conn)) &&
+ ((req->hdr.Flags & SMB2_FLAGS_SIGNED) ?
+ !smb2_check_sess_setup_sign_req(work, &req->hdr, sess) :
+ (!work->encrypted &&
+ smb2_sess_needs_protection(conn, 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);
@@ -12979,22 +13030,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) {
@@ -13040,17 +13093,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] 8+ messages in thread* [PATCH v4 4/6] ksmbd: check a request before signing STATUS_NETWORK_SESSION_EXPIRED
2026-11-10 16:58 [PATCH v4 0/6] ksmbd: verify session setup requests before signing with their key Michail Andreas Michalopoulos
` (2 preceding siblings ...)
2026-11-10 16:58 ` [PATCH v4 3/6] ksmbd: use the channel signing key for session setup on a bound channel Michail Andreas Michalopoulos
@ 2026-11-10 16:58 ` Michail Andreas Michalopoulos
2026-11-10 16:58 ` [PATCH v4 5/6] ksmbd: use the current command's header for signing in a compound Michail Andreas Michalopoulos
` (2 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Michail Andreas Michalopoulos @ 2026-11-10 16:58 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. 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 064f99427ca6f..276dbbd986503 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] 8+ messages in thread* [PATCH v4 5/6] ksmbd: use the current command's header for signing in a compound
2026-11-10 16:58 [PATCH v4 0/6] ksmbd: verify session setup requests before signing with their key Michail Andreas Michalopoulos
` (3 preceding siblings ...)
2026-11-10 16:58 ` [PATCH v4 4/6] ksmbd: check a request before signing STATUS_NETWORK_SESSION_EXPIRED Michail Andreas Michalopoulos
@ 2026-11-10 16:58 ` Michail Andreas Michalopoulos
2026-11-10 16:58 ` [PATCH v4 6/6] ksmbd: consume the sequence number of a request on a failed session Michail Andreas Michalopoulos
2026-11-10 18:17 ` [PATCH v4 0/6] ksmbd: verify session setup requests before signing with their key mixalhs
6 siblings, 0 replies; 8+ messages in thread
From: Michail Andreas Michalopoulos @ 2026-11-10 16:58 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 276dbbd986503..13341ff38aeeb 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) {
if (command == SMB2_SESSION_SETUP_HE &&
work->sess->dialect >= SMB30_PROT_ID &&
conn->dialect < SMB30_PROT_ID)
diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index 293e660265c95..f918744870a8f 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -12888,6 +12888,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] 8+ messages in thread* [PATCH v4 6/6] ksmbd: consume the sequence number of a request on a failed session
2026-11-10 16:58 [PATCH v4 0/6] ksmbd: verify session setup requests before signing with their key Michail Andreas Michalopoulos
` (4 preceding siblings ...)
2026-11-10 16:58 ` [PATCH v4 5/6] ksmbd: use the current command's header for signing in a compound Michail Andreas Michalopoulos
@ 2026-11-10 16:58 ` Michail Andreas Michalopoulos
2026-11-10 18:17 ` [PATCH v4 0/6] ksmbd: verify session setup requests before signing with their key mixalhs
6 siblings, 0 replies; 8+ messages in thread
From: Michail Andreas Michalopoulos @ 2026-11-10 16:58 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 13341ff38aeeb..e3fe856a8258a 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] 8+ messages in thread* Re: [PATCH v4 0/6] ksmbd: verify session setup requests before signing with their key
2026-11-10 16:58 [PATCH v4 0/6] ksmbd: verify session setup requests before signing with their key Michail Andreas Michalopoulos
` (5 preceding siblings ...)
2026-11-10 16:58 ` [PATCH v4 6/6] ksmbd: consume the sequence number of a request on a failed session Michail Andreas Michalopoulos
@ 2026-11-10 18:17 ` mixalhs
6 siblings, 0 replies; 8+ messages in thread
From: mixalhs @ 2026-11-10 18:17 UTC (permalink / raw)
To: linkinjeon, stfrench
Cc: icb, senozhatsky, tom, chenxiaosong, ematsumiya, lsahlber,
hyc.lee, linux-cifs, linux-kernel
Sashiko’s review has already pointed out some real bugs that will require a V5
I will fix them and send it, will spend extra time verifying everything but if there
Is any feedback for this version I would appreciate it.
Thanks,
Michails
^ permalink raw reply [flat|nested] 8+ messages in thread