Skip to contentSkip to conversation

ksmbd: verify session setup requests before signing with their key

PATCH Superseded Michail Andreas Michalopoulos 1 participant 9 messages Nov 12

Subsystems (3)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v5 cover ksmbd: verify session setup requests before signing with their key

ksmbd signs some SESSION_SETUP and error responses with a session's key without having verified the request, which lets a peer that does not hold the key obtain signatures made with it. This series fixes those cases, makes ksmbd sign with the SMB 3.1.1 signing algorithm it negotiated, and fixes related problems found in review.

1/8 verifies multichannel binding requests with the session signing key

    (MS-SMB2 3.3.5.2.4), as reported by Ian Bridges.

2/8 keeps the negotiated SMB 3.1.1 signing algorithm instead of always
using AES-CMAC, and signs each key with its own algorithm.
3/8 verifies signed reauthentication requests and uses the channel key
for session setup on a bound channel. A reauthentication with a forged signature used to get a response signed with the session key.
4/8 refuses a binding request on a connection that is already a channel
of the session.
5/8 checks the MessageId and signature of a request before signing
STATUS_NETWORK_SESSION_EXPIRED, also reported by Ian Bridges.
6/8 makes the signing decision in a compound follow each command's own
header instead of the first command's.
7/8 consumes the MessageId of a request on an unknown, invalid or
expired session, which the error path skipped.
8/8 charges and grants credits for such a request, which the error path
also skipped.

Changes in v5, following Sashiko's and Ian Bridges' reviews of v4 and another review of the series:
- 1/8: refuse a binding request for a session that is still being set up before verifying its signature, in both binding branches. Such a session has no signing key yet, and v4 verified the request against the all-zero key. It gets the STATUS_REQUEST_NOT_ACCEPTED of 3.3.5.5 (3.3.5.2.4 would answer STATUS_NOT_SUPPORTED, as the key is NULL). SMB2_SESSION_VALID is now set with smp_store_release(), so a session seen as valid has its key.
- 1/8: with multichannel disabled, the refusal of a correctly signed binding request from an SMB 3.x connection to an SMB 2.x session was signed with the session's SMB 3.x signing key, which is all zeroes for such a session. It is now signed with smb2_set_sign_rsp().
- 1/8: drop the unused hdr argument of the session-setup helper, and shorten the commit message.
- 2/8: no code change. The commit message presented using the session key with the algorithm of the connection that set up the session as something MS-SMB2 requires. As Ian Bridges pointed out, the specification takes the algorithm from the connection; the patch follows what the Samba client and smbd do, and the message now says so.
- 3/8: remember in sess->sign_required that signing was required when the session was set up, since negotiating encryption clears sess->sign. v4 derived it from the connection, which missed a client that asks for signing only in SESSION_SETUP and a channel whose connection does not require signing.
- 3/8: apply the reauthentication checks to every session that has a user rather than only where the connection is a channel. SMB 2.x sessions have no channels, so the second leg of the reauthentication of an expired SMB 2.x session was not checked, and a forged one destroyed the session.
- 3/8: copy conn->binding into work->session_setup_binding before the error path clears it.
- 3/8: treat an encrypted request as authenticated only on a session that uses encryption.
- 4/8 is new: refuse a binding request on a connection that is already a channel of the session. Sashiko noted it on 3/6; it was already so before the series, so it is a patch of its own.
- 3/8 and 5/8: commit message corrections. 3/8 now lists what MS-SMB2 says about the key of a reauthentication on a bound channel; see below.
- 8/8 is new. Sashiko noted on 4/6 that a request answered on the failed-session path is never charged its credits and gets none. That was already so before the series, so it is fixed in a patch of its own. It leaves out SMB2 CANCEL, which consumes no credit. The problem goes back to the first version of ksmbd's request loop, so its Fixes tag points there.
Not changed:
- 1/8: Sashiko suggested checking the ClientGUID, the session state and the user before the signature, in the order 3.3.5.5 lists them. The signature stays first: 3.3.5.2.4 verifies it while the request is received, before the command is processed, as 5/8 also assumes, and a request without the key then learns nothing about the session. The refusals of a binding request before it has been verified are unsigned, as they were before the series.
- 3/8: a reauthentication on a channel that was bound to the session is verified and answered with that channel's key, as the Samba client and smbd do. MS-SMB2 does not give one key for it: 3.3.5.2.4 has the server verify the request with Channel.SigningKey and 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 it is STATUS_SUCCESS, and 3.3.5.5.3 a final non-binding response too. On the session's first connection the two keys are the same. I have not tested this against Windows.
- 6/8: Sashiko noted that a signed SESSION_SETUP for another session, chained after a command, is now checked against the earlier command's session and refused. That is right for related operations (3.3.5.2.7.2), and ksmbd already refuses unrelated commands on different sessions in one compound, although 3.3.5.2.7.1 would handle them separately. The same check keeps the refusal of a forged chained SESSION_SETUP from being signed with the earlier session's key:
leaving SESSION_SETUP out of it made the refusal signed. A test now covers this.

v4: https://lore.kernel.org/linux-cifs/[email protected]/

Changes in v4, following Namjae's review of v3 (numbered as in v4; v4's 4/6 to 6/6 are now 5/8 to 7/8):
- 1/6: smb2_check_sign_req() and smb3_check_sign_req() now restore the Signature field themselves, using their signature_req buffer, instead of the session-setup helper saving and restoring it.
- 2/6: use SMB2_HMACSHA256_SIZE for the HMAC-SHA256 buffer.
- 3/6: no longer changes __process_request(), as the check helpers now restore the field it used to restore there.

v3: https://lore.kernel.org/linux-cifs/[email protected]/

Changes in v3, following Sashiko's review of v2:
- 1/6: correct a missing negation on the multichannel flag, so an SMB 3.x binding request received while multichannel is disabled is verified and rejected in the non-multichannel branch instead of falling through to the reauthentication path.
- 3/6: also refuse an unsigned, unencrypted reauthentication when the session requires signing or encryption. Negotiating encryption clears sess->sign even when the connection required signing, so the v2 check let such a reauthentication through. Merely having negotiated encryption is not treated as a requirement, since clients routinely negotiate it and then send unsigned, unencrypted requests.
- 5/6 is new. With signing not required, a signed later command was processed without its signature being checked, a correctly signed one got an unsigned response, and an unsigned one after a signed first command was refused. It also decides whether to sign each response before moving on to the next command of the compound.
- 6/6 is new. Only a signed request on an expired session had its MessageId checked (by 4/6); any other failed-session request left it unconsumed, pinning the command sequence window and letting a replayed MessageId be answered again. 4/6's own check moves to the common path, so the MessageId is consumed exactly once.

Testing of v5, on a KASAN/lockdep UML kernel based on next-20261105. New tests, each failing on v4 and passing on v5:
- a binding request for a session still in setup gets STATUS_REQUEST_NOT_ACCEPTED whether its signature is random or made with an all-zero key (v4 answered the two differently);
- a binding request with a bad signature gets STATUS_ACCESS_DENIED whatever its ClientGUID and for an expired session, while a correctly signed one gets STATUS_USER_SESSION_DELETED and STATUS_NETWORK_SESSION_EXPIRED there (this one passes on v4 too);
- the refusal of an SMB 3.1.1 bind to an SMB 2.1 session verifies with the session key (v4: with an all-zero key);
- during the reauthentication of an expired SMB 2.1 session, an unsigned or badly signed second leg is refused and the real one then succeeds (v4: the forged leg destroyed the session);
- a second binding request on a bound channel is refused (v4: answered with STATUS_MORE_PROCESSING_REQUIRED);
- with signing asked for only in SESSION_SETUP and encryption negotiated, an unsigned, unencrypted reauthentication is refused (v4:
accepted), and an encrypted one is still accepted;
- 8/8: 40 requests on an unknown session right after NEGOTIATE, and 300 on an expired session, are all answered and their responses grant credits (without 8/8: one and 127 answered, then the connection is dropped); a signed one still gets a response that verifies; and CANCELs naming an unknown session are granted no credits and leave their MessageId usable;
- a signed reauthentication of one session chained after a command on another session, or a forged one, is refused, and the refusal is signed with neither session's key.
The binding, expired-session, compound (including SESSION_SETUP inside a compound), encrypted-reauthentication and sequence-number tests pass as with v3 and v4, the Linux cifs client works signed and encrypted, and each patch builds on its own with W=1.

Testing of v3, on a KASAN/lockdep UML kernel with ksmbd-tools:
- forged and replayed binding, reauthentication and expired-session requests, also inside compounds, get unsigned refusals with AES-CMAC and HMAC-SHA256;
- new targeted tests for 3/6, 5/6 and 6/6, each failing on the kernel without the fix and passing with it (5/6: each response of a compound mixing signed and unsigned commands is signed exactly when its request is). The 3/6 test uses an SMB 3.1.1 client with AES-128-GCM
encryption: an unsigned reauthentication is allowed when the session requires neither signing nor encryption, refused when the client required signing, and an encrypted one is allowed;
- randomized requests against these paths, and 8- and 16-thread bind/reauth/logoff stress (about 130,000 operations), found no signed response to an unverified request and no KASAN or lockdep report;
- the Linux cifs client works with SMB 2.1 to 3.1.1, signed and encrypted, and recovers after its session expires;
- smbtorture's session, signing, I/O, compound, lock, oplock, lease, notify and ioctl suites give the same results as without the series. With multichannel disabled, the session tests also pass and fail as without the series; six binding tests that fail either way (the d and s variants of bind_negative_smb2to3, smb3to3 and smb3encGtoC) now get STATUS_ACCESS_DENIED or STATUS_REQUEST_NOT_ACCEPTED instead of STATUS_USER_SESSION_DELETED (they expect STATUS_INVALID_PARAMETER), since 1/6 now rejects such a binding request in the binding branch.
Ian Bridges also tested an earlier revision with his own probes and smbtorture. Not tested: Windows clients, and Kerberos expiry with a real KDC (it was emulated with a test-only change).

An AI assistant was used to draft the patches and as an independent reviewer.

Michail Andreas Michalopoulos (8):
ksmbd: verify the signature of session binding requests
ksmbd: sign with the negotiated SMB 3.1.1 signing algorithm
ksmbd: use the channel signing key for session setup on a bound
channel
ksmbd: refuse a binding request on a connection that is already a
channel
ksmbd: check a request before signing STATUS_NETWORK_SESSION_EXPIRED
ksmbd: use the current command's header for signing in a compound
ksmbd: consume the sequence number of a request on a failed session
ksmbd: account for the credits of a request on a failed session

 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 |   4 +
 fs/smb/server/server.c            | 119 +++++++++++++++++-
 fs/smb/server/smb2ops.c           |   4 +-
 fs/smb/server/smb2pdu.c           | 197 ++++++++++++++++++++++++------
 7 files changed, 296 insertions(+), 47 deletions(-)

base-commit: ddfe51b0a0f5dc9e415cc2c8ad8db17532dd426b

Signature · 1 line
2.43.0
#1 lore raw

PATCH v5 1/8 ksmbd: verify the signature of session binding requests

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.

---
 fs/smb/server/server.c  |  9 ++++++
 fs/smb/server/smb2pdu.c | 69 ++++++++++++++++++++++++++++++++---------
 2 files changed, 63 insertions(+), 15 deletions(-)
fs/smb/server/server.c +9 -0
index 064f99427ca6f..51aa981bcef4e 100644
@@ -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);
 		}
fs/smb/server/smb2pdu.c +54 -15

122 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

PATCH v5 2/8 ksmbd: sign with the negotiated SMB 3.1.1 signing algorithm

In reply to Michail Andreas Michalopoulos

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")
---
 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(-)
fs/smb/server/auth.c +12 -2
index 0c51ca0cb14db..fc6ace49584b1 100644
@@ -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);
fs/smb/server/auth.h +2 -2
index 7ce9c42d58f1e..5a10482678ee4 100644
@@ -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,
fs/smb/server/mgmt/user_session.c +1 -0
index 15662fd745abb..87aba04a8cbdd 100644
@@ -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));
fs/smb/server/mgmt/user_session.h +2 -0
index 5f0f6276e3168..931ca102b8392 100644
@@ -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;
fs/smb/server/smb2ops.c +3 -1
index 747276b171bd1..c5b3cf34494e7 100644
@@ -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 |
fs/smb/server/smb2pdu.c +18 -2
index 3d9b966235dbb..d15024d34f3d6 100644
@@ -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);
 }
 
Signature · 1 line
2.43.0
#3 lore raw

PATCH v5 3/8 ksmbd: use the channel signing key for session setup on a bound channel

In reply to Michail Andreas Michalopoulos

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")
---
 fs/smb/server/mgmt/user_session.h |   2 +
 fs/smb/server/smb2pdu.c           | 118 +++++++++++++++++++++++-------
 2 files changed, 92 insertions(+), 28 deletions(-)
fs/smb/server/mgmt/user_session.h +2 -0
index 931ca102b8392..423f0a10315af 100644
@@ -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;
 
fs/smb/server/smb2pdu.c +90 -28

193 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw

PATCH v5 4/8 ksmbd: refuse a binding request on a connection that is already a channel

In reply to Michail Andreas Michalopoulos

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")
---
 fs/smb/server/smb2pdu.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/smb/server/smb2pdu.c +3 -1
index 7b07108a48c15..0271ee4753236 100644
@@ -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;
 		}
Signature · 1 line
2.43.0
#5 lore raw

PATCH v5 5/8 ksmbd: check a request before signing STATUS_NETWORK_SESSION_EXPIRED

In reply to Michail Andreas Michalopoulos

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")
---
 fs/smb/server/server.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
fs/smb/server/server.c +27 -1
index 51aa981bcef4e..ca0c78447f454 100644
@@ -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;
 				}
Signature · 1 line
2.43.0
#6 lore raw

PATCH v5 6/8 ksmbd: use the current command's header for signing in a compound

In reply to Michail Andreas Michalopoulos

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")
---
 fs/smb/server/server.c  | 27 +++++++++++++++++++++++----
 fs/smb/server/smb2pdu.c |  4 ++++
 2 files changed, 27 insertions(+), 4 deletions(-)
fs/smb/server/server.c +23 -4
index ca0c78447f454..32bed8672dd2e 100644
@@ -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
fs/smb/server/smb2pdu.c +4 -0
index 0271ee4753236..895afb41adacf 100644
@@ -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;
Signature · 1 line
2.43.0
#7 lore raw

PATCH v5 7/8 ksmbd: consume the sequence number of a request on a failed session

In reply to Michail Andreas Michalopoulos

__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")
---
 fs/smb/server/server.c | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)
fs/smb/server/server.c +20 -6
index 32bed8672dd2e..52eee04e0d93a 100644
@@ -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);
Signature · 1 line
2.43.0
#8 lore raw

PATCH v5 8/8 ksmbd: account for the credits of a request on a failed session

In reply to Michail Andreas Michalopoulos

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")
---
 fs/smb/server/server.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
fs/smb/server/server.c +41 -0
index 52eee04e0d93a..1522eb560e838 100644
@@ -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;
 
Signature · 1 line
2.43.0