Skip to contentSkip to conversation

ksmbd: verify transform SessionId matches the decrypted header

PATCH New Dairui Zhang 1 participant 1 message Sep 28

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ksmbd: verify transform SessionId matches the decrypted header

The decryption key for an encrypted request is selected by the SessionId in the encryption transform header, but the request is then authorized under the session named in the decrypted inner SMB2 header. Nothing compares the two, so on a connection carrying more than one session a client can have a request decrypted with one session's key and executed under another session's identity. Since encrypted requests are also exempt from the signing requirement, the AEAD tag is the only proof of session identity, and it is checked against the wrong session.

Per MS-SMB2 the server must verify that the SessionId in the transform header matches the one in the decrypted SMB2 header and treat a mismatch as a protocol error. Validate the whole decrypted message before dispatching it: the message must be at least one fixed SMB2 header; the first operation must not set SMB2_FLAGS_RELATED_OPERATIONS and its SessionId must match the transform SessionId; each subsequent operation in a compound chain must either set SMB2_FLAGS_RELATED_OPERATIONS or carry the same SessionId; and NextCommand offsets must be 8-byte aligned and within the message. When the encrypted payload is a compression transform, the transform SessionId is saved during decryption and the same validation runs on the decompressed message. (The 8-byte alignment of compound responses is already handled by the existing chained-response padding.)

---
v2 -> v3:
- Per review, extend the check to the whole decrypted message before
dispatch: first-op size/RELATED_OPERATIONS/SessionId rules and per-op compound-chain SessionId and NextCommand validation, shared between the plain and compression paths via a common helper.
v1 -> v2:
- Cover the compression-transform case.
- Reject a decrypted SMB2 message smaller than the fixed header before reading its SessionId.

---
 fs/smb/server/compress.c   | 17 +++++++++
 fs/smb/server/ksmbd_work.h |  4 ++
 fs/smb/server/smb2pdu.c    | 78 ++++++++++++++++++++++++++++++++++++++
 fs/smb/server/smb2pdu.h    |  2 +
 4 files changed, 101 insertions(+)
fs/smb/server/compress.c +17 -0
index 5162fb8..b07b140 100644
@@ -10,6 +10,7 @@
 
 #include "compress.h"
 #include "smb_common.h"
+#include "smb2pdu.h"
 #include "../common/compress/lz77.h"
 
 #define SMB_COMPRESS_MIN_LEN	PAGE_SIZE
@@ -125,6 +126,22 @@ int ksmbd_decompress_work_request(struct ksmbd_work *work)
 	if (rc)
 		return rc;
 
+	/*
+	 * The encryption transform SessionId was saved before the
+	 * compression transform was parsed; the decompressed message must
+	 * pass the same session binding validation as a plain decrypted
+	 * message.
+	 */
+	if (work->tr_sess_id) {
+		rc = ksmbd_check_transform_session(
+				(struct smb2_hdr *)smb_get_msg(out_buf),
+				get_rfc1002_len(out_buf), work->tr_sess_id);
+		if (rc) {
+			kvfree(out_buf);
+			return rc;
+		}
+	}
+
 	kvfree(work->request_buf);
 	work->request_buf = out_buf;
 	return 0;
fs/smb/server/ksmbd_work.h +4 -0
index 5f1d3eb..3e8bf2a 100644
@@ -82,6 +82,10 @@ struct ksmbd_work {
 	/* Contiguous SMB2 compression transform owned by this work item. */
 	void				*compress_buf;
 
+	/* SessionId from the encryption transform header, for the
+	 * post-decompression SessionId check. Zero when unset. */
+	__u64				tr_sess_id;
+
 	unsigned char			state;
 	/* No response for cancelled request */
 	bool                            send_no_response:1;
fs/smb/server/smb2pdu.c +78 -0

96 lines not on this page · raw message

fs/smb/server/smb2pdu.h +2 -0
index 1836259..2cdeb56 100644
@@ -399,6 +399,8 @@ struct channel *lookup_chann_list(struct ksmbd_session *sess,
 void smb3_preauth_hash_rsp(struct ksmbd_work *work);
 bool smb3_is_transform_hdr(void *buf);
 int smb3_decrypt_req(struct ksmbd_work *work);
+int ksmbd_check_transform_session(struct smb2_hdr *hdr,
+				  unsigned int msg_len, __u64 tr_sess_id);
 int smb3_encrypt_resp(struct ksmbd_work *work);
 bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work);
 int smb2_set_rsp_credits(struct ksmbd_work *work);
Signature · 1 line
2.53.0