ksmbd: verify transform SessionId matches the decrypted header
Subsystems (3)
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH v5 1/1 ksmbd: verify transform SessionId matches the decrypted header
Namjae,
You're right. I missed the decompression step in __handle_ksmbd_work(), and the "no decompression step" and "no ProtocolId check" claims in the v4 commit message were wrong. Sorry for the sloppy reading and the extra work it caused you.
v5 moves the check to __handle_ksmbd_work(), after the existing decompression step, so encrypted compression transforms are no longer rejected. The transform SessionId is read before decryption and verified against the final SMB2 message. The compound-walk bounds check now uses subtraction (the addition could wrap on 32-bit), and I added the Fixes tag.
From a320c66f1aeb773cf65d2489fae9f982eaf3873f Mon Sep 17 00:00:00 2001
From: Dairui Zhang <[email protected]>
Date: Tue, 29 Sep 2026 22:30:00 +0800
Subject: [PATCH v5] ksmbd: verify transform SessionId matches the decrypted
header
When an encrypted request comes in, the decryption key is picked by the SessionId in the transform header, but the request is then authorized under whatever session the decrypted inner header names. Nothing ever compares the two, so on a connection with more than one session a client can get a request decrypted with one session's key and run it as another session. Encrypted requests are exempt from signing too, so a valid AEAD tag is the only proof of session identity, and it is checked against the wrong session.
MS-SMB2 says the server must check that the transform SessionId matches the one in the decrypted SMB2 header and treat a mismatch as a protocol error. Add that check in __handle_ksmbd_work() after decryption, before dispatching: the message has to 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, and every following non-related operation in a compound chain must carry the same SessionId. SessionId ~0 is accepted in subsequent operations, same as the existing compound session check. NextCommand offsets must be 8-byte aligned and stay inside the message; the bounds check is done by subtraction so it cannot wrap.
When the decrypted payload is a compression transform, the existing decompression step runs first and the decompressed SMB2 message is checked instead, since a compression transform header carries no SessionId.
---
v4 -> v5:
- Move the check into __handle_ksmbd_work(), after the existing decompression step, instead of rejecting encrypted compression transforms in smb3_decrypt_req(), per Namjae's review. The transform SessionId is saved before decryption.
- Use subtraction for the compound-walk bounds check; the addition form could wrap on 32-bit.
- Add the Fixes tag and drop the incorrect claims about the decompression step and ProtocolId check from the commit message.
--- fs/smb/server/server.c | 16 ++++++++++++ fs/smb/server/smb2pdu.c | 54 ++++++++++++++++++++++++++++++++++++++++ fs/smb/server/smb2pdu.h | 1 + 3 files changed, 71 insertions(+)
fs/smb/server/server.c +16 -0
@@ -187,6 +187,16 @@if (conn->ops->is_transform_hdr && conn->ops->is_transform_hdr (work->request_buf)) { +u64 tr_sess_id; + +if (get_rfc1002_len (work->request_buf) < +sizeof (struct smb2_transform_hdr )) { +ksmbd_conn_abort (conn); +return ; + } + tr_sess_id =le64_to_cpu (((struct smb2_transform_hdr *) +smb_get_msg (work->request_buf))->SessionId); + rc = conn->ops->decrypt_req (work);if (rc <0 ) {ksmbd_conn_abort (conn);
@@ -215,6 +225,12 @@ksmbd_conn_abort (conn);return ; } + + rc =ksmbd_check_transform_session (work, tr_sess_id); +if (rc <0 ) { +ksmbd_conn_abort (conn); +return ; + } }if (conn->ops->allocate_rsp_buf (work))
fs/smb/server/smb2pdu.c +54 -0
60 lines not on this page · raw message
fs/smb/server/smb2pdu.h +1 -0
@@ -416,6 +416,7 @@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 ksmbd_work *work,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
PATCH v6 1/1 ksmbd: verify transform SessionId matches the decrypted header
Namjae, Tom,
Re-checked v5 against the updated MS-SMB2 text. The ~0 exemption was my mistake - the spec has no wildcard for non-related operations. Dropped it in v6. The other checks already match, decompression included. No other changes.
From a320c66f1aeb773cf65d2489fae9f982eaf3873f Mon Sep 17 00:00:00 2001
From: Dairui Zhang <[email protected]>
Date: Tue, 29 Sep 2026 22:30:00 +0800
Subject: [PATCH v6] ksmbd: verify transform SessionId matches the decrypted
header
When an encrypted request comes in, the decryption key is picked by the SessionId in the transform header, but the request is then authorized under whatever session the decrypted inner header names. Nothing ever compares the two, so on a connection with more than one session a client can get a request decrypted with one session's key and run it as another session. Encrypted requests are exempt from signing too, so a valid AEAD tag is the only proof of session identity, and it is checked against the wrong session.
MS-SMB2 says the server must check that the transform SessionId matches the one in the decrypted SMB2 header and treat a mismatch as a protocol error. Add that check in __handle_ksmbd_work() after decryption, before dispatching: the message has to 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, and every following non-related operation in a compound chain must carry the same SessionId. NextCommand offsets must be 8-byte aligned and stay inside the message; the bounds check is done by subtraction so it cannot wrap.
When the decrypted payload is a compression transform, the existing decompression step runs first and the decompressed SMB2 message is checked instead, since a compression transform header carries no SessionId.
---
v4 -> v5:
- Move the check into __handle_ksmbd_work(), after the existing decompression step, instead of rejecting encrypted compression transforms in smb3_decrypt_req(), per Namjae's review. The transform SessionId is saved before decryption.
- Use subtraction for the compound-walk bounds check; the addition form could wrap on 32-bit.
- Add the Fixes tag and drop the incorrect claims about the decompression step and ProtocolId check from the commit message.
v5 -> v6:
- Drop the SessionId ~0 exemption for subsequent non-related operations, per Tom Talpey's review.
--- fs/smb/server/server.c | 16 ++++++++++++ fs/smb/server/smb2pdu.c | 54 ++++++++++++++++++++++++++++++++++++++++ fs/smb/server/smb2pdu.h | 1 + 3 files changed, 71 insertions(+)
fs/smb/server/server.c +16 -0
@@ -187,6 +187,16 @@if (conn->ops->is_transform_hdr && conn->ops->is_transform_hdr (work->request_buf)) { +u64 tr_sess_id; + +if (get_rfc1002_len (work->request_buf) < +sizeof (struct smb2_transform_hdr )) { +ksmbd_conn_abort (conn); +return ; + } + tr_sess_id =le64_to_cpu (((struct smb2_transform_hdr *) +smb_get_msg (work->request_buf))->SessionId); + rc = conn->ops->decrypt_req (work);if (rc <0 ) {ksmbd_conn_abort (conn);
@@ -215,6 +225,12 @@ksmbd_conn_abort (conn);return ; } + + rc =ksmbd_check_transform_session (work, tr_sess_id); +if (rc <0 ) { +ksmbd_conn_abort (conn); +return ; + } }if (conn->ops->allocate_rsp_buf (work))
fs/smb/server/smb2pdu.c +54 -0
60 lines not on this page · raw message
fs/smb/server/smb2pdu.h +1 -0
@@ -416,6 +416,7 @@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 ksmbd_work *work,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
Quoted 7 lines · Dairui Zhang
Namjae, Tom,
Re-checked v5 against the updated MS-SMB2 text. The ~0 exemption was my mistake - the spec has no wildcard for non-related operations. Dropped it in v6. The other checks already match, decompression included. No other changes.
Applied this patch to #ksmbd-for-next.
Thanks!