ksmbd: enforce mandatory signing and encryption on requests
Subsystems (2)
cover ksmbd: enforce mandatory signing and encryption on requests
ksmbd does not enforce "server signing = mandatory" or "smb3 encryption = mandatory" on requests once a client has offered a cipher in NEGOTIATE, which the Linux and current Windows clients always do. sess->sign is cleared for such sessions, and the gate added by 2bebf2470af1 ("ksmbd:
enforce signing required by the session") only looks at sess->sign, so an unsigned plaintext request is executed on a session that requires signing, and a plaintext request on a session that was told SMB2_SESSION_FLAG_ENCRYPT_DATA. A client that offers no cipher at all gets a plaintext session even with mandatory encryption. On SMB 3.0 and 3.0.2, one client's SMB2_GLOBAL_CAP_ENCRYPTION is ORed into the static per-dialect table and changes how every later session of that dialect is set up, until the module is reloaded.
The consequence is that a man-in-the-middle between a client and ksmbd can inject requests into an authenticated session and have them run with the victim's credentials, and that an NTLM relay to ksmbd is not stopped by either setting, which is what the two settings exist for.
The session handling goes back to e2f34481b24d ("cifsd: add server-side procedures for SMB3"); the signing part is what is left after 2bebf2470af1 (v7.2-rc5). We reproduced it on mainline 4405c18c95d2 (v7.4-rc3-230), on v7.3 and on ksmbd-for-next 267029b35ad1. A reproducer is available to the maintainers on request.
The series is based on ksmbd-for-next 267029b35ad1, because patch 1 uses sess->sign_required from the session setup signing series queued there. On mainline it applies on top of that series' first three commits, 16ab0adace8f ("ksmbd: verify the signature of session binding requests"), 42a1d80e64a3 ("ksmbd: sign with the negotiated SMB 3.1.1 signing algorithm") and ea472ddf7a9a ("ksmbd: use the channel signing key for session setup on a bound channel").
Patch 1 makes the request gate honour sess->sign_required. Patch 2 refuses plaintext requests on sessions that were told to encrypt. Patch 3 applies the existing per-share "needs a negotiated cipher" check to tree connects when encryption is mandatory for the whole server, so an SMB 2.1 client can no longer connect in that configuration, as ksmbd.conf(5) documents. Patch 4 gives each connection its own copy of its dialect's capability table.
Testing: built with W=1 for CONFIG_SMB_SERVER=y and =m without warnings, each patch on its own, and checkpatch --strict clean. On KASAN kernels built from ksmbd-for-next 267029b35ad1 and from mainline 4405c18c95d2 (with the three commits above), each with this series, every unsigned or plaintext TREE_CONNECT that gets through on the unpatched trees is refused with STATUS_ACCESS_DENIED, and the next NEGOTIATE no longer advertises a capability another client offered. The in-kernel cifs client still mounts, writes and reads with sign and with seal under mandatory signing (vers=2.1, 3.0, 3.0.2 and 3.1.1; seal with 3.0 and 3.1.1), and with seal under mandatory encryption. Not tested:
Kerberos sessions (krb5_authenticate() gets the same change), SMB Direct and multichannel binding.
We found this with AI-assisted code review and testing; the reproducer and the fix were tested by us on current mainline.
RootSys GmbH Research (4):
ksmbd: require signing on sessions that negotiated encryption
ksmbd: reject plaintext requests on sessions that must be encrypted
ksmbd: refuse tree connect without a cipher when encryption is
mandatory
ksmbd: keep negotiated capabilities per connection
fs/smb/server/connection.h | 2 ++ fs/smb/server/mgmt/user_session.h | 2 ++ fs/smb/server/server.c | 14 ++++++++++++-- fs/smb/server/smb2ops.c | 19 +++++++++++++++---- fs/smb/server/smb2pdu.c | 16 ++++++++++++---- 5 files changed, 43 insertions(+), 10 deletions(-)
base-commit: 267029b35ad1245a36d069cafe93b4ea654e0f81
Signature · 1 line
2.43.0
PATCH 1/4 ksmbd: require signing on sessions that negotiated encryption
ntlm_authenticate() and krb5_authenticate() clear sess->sign whenever smb3_encryption_negotiated() is true, that is whenever the client offered a cipher in NEGOTIATE. Offering a cipher does not mean the client will encrypt: unless the server sets SMB2_SESSION_FLAG_ENCRYPT_DATA, the client keeps sending plaintext requests and signs them because signing is required.
__process_request() only refuses an unsigned plaintext request when sess->sign is set, so on a server with "server signing = mandatory", or for a client that sent SMB2_NEGOTIATE_SIGNING_REQUIRED, unsigned plaintext requests are executed as soon as the client offered a cipher. The Linux client always offers one, and so do current Windows clients. An attacker on the path between client and server can then inject requests that run with the victim's session and tree connections, which mandatory signing is meant to prevent; commit 2bebf2470af1 ("ksmbd: enforce signing required by the session") closed this only for sessions that did not negotiate encryption.
sess->sign_required already records that signing was required when the session was set up. Use it in the request gate too.
Found with AI-assisted code review and testing.
--- fs/smb/server/server.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
fs/smb/server/server.c +6 -2
@@ -203,8 +203,12 @@ static int __process_request(struct ksmbd_work *work, struct ksmbd_conn *conn, } signed_req = conn->ops->is_sign_req && conn->ops->is_sign_req (work, command); -if (work->sess && work->sess->sign && !work->encrypted && - !signed_req) { +/* +* sess->sign is cleared once encryption is negotiated, even when the +* client does not encrypt, so check sess->sign_required as well. +*/ +if (work->sess && (work->sess->sign || work->sess->sign_required) && + !work->encrypted && !signed_req) { conn->ops->set_rsp_status (work,STATUS_ACCESS_DENIED );return SERVER_HANDLER_ABORT ; }
Signature · 1 line
2.43.0
PATCH 2/4 ksmbd: reject plaintext requests on sessions that must be encrypted
In reply to RootSys GmbH ResearchWith "smb3 encryption = mandatory" ksmbd sets SMB2_SESSION_FLAG_ENCRYPT_DATA in the SESSION_SETUP response of every session that negotiated a cipher, so the client encrypts all further requests. Nothing on the request path checks this, though: a plaintext request on such a session is executed like an encrypted one. Since sess->sign is cleared for these sessions, it does not even have to be signed. The only encryption check on the request path is the one for shares carrying KSMBD_SHARE_FLAG_ENCRYPT_DATA.
An attacker on the path can therefore inject plaintext, unsigned requests into a session that both sides believe to be encrypted. MS-SMB2 3.3.5.2.9 requires the server to fail such a request with STATUS_ACCESS_DENIED.
Record in the session that it was told to encrypt and refuse unencrypted requests on it.
Found with AI-assisted code review and testing.
--- fs/smb/server/mgmt/user_session.h | 2 ++ fs/smb/server/server.c | 6 ++++++ fs/smb/server/smb2pdu.c | 8 ++++++-- 3 files changed, 14 insertions(+), 2 deletions(-)
fs/smb/server/mgmt/user_session.h +2 -0
@@ -62,6 +62,8 @@ struct ksmbd_session {/* signing was required at setup; sign is cleared with encryption */ bool sign_required;bool enc; +/* the session was told to encrypt (SMB2_SESSION_FLAG_ENCRYPT_DATA) */ +bool enc_required;bool tearing_down;int state;
fs/smb/server/server.c +6 -0
@@ -202,6 +202,12 @@ static int __process_request(struct ksmbd_work *work, struct ksmbd_conn *conn,return SERVER_HANDLER_ABORT ; } +/* MS-SMB2 3.3.5.2.9: a session that must encrypt takes no plaintext */ +if (work->sess && work->sess->enc_required && !work->encrypted) { + conn->ops->set_rsp_status (work,STATUS_ACCESS_DENIED ); +return SERVER_HANDLER_ABORT ; + } + signed_req = conn->ops->is_sign_req && conn->ops->is_sign_req (work, command);/* * sess->sign is cleared once encryption is negotiated, even when the
fs/smb/server/smb2pdu.c +6 -2
@@ -2407,8 +2407,10 @@ static int ntlm_authenticate(struct ksmbd_work *work, !(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) { conn->ops->generate_encryptionkey (conn, sess); sess->enc = true; -if (server_conf.flags &KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION ) +if (server_conf.flags &KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION ) { rsp->SessionFlags =SMB2_SESSION_FLAG_ENCRYPT_DATA_LE ; + sess->enc_required = true; + }/* * signing is disable if encryption is enable * on this session
@@ -2513,8 +2515,10 @@ static int krb5_authenticate(struct ksmbd_work *work, !(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) { conn->ops->generate_encryptionkey (conn, sess); sess->enc = true; -if (server_conf.flags &KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION ) +if (server_conf.flags &KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION ) { rsp->SessionFlags =SMB2_SESSION_FLAG_ENCRYPT_DATA_LE ; + sess->enc_required = true; + } sess->sign = false; }
Signature · 1 line
2.43.0
PATCH 3/4 ksmbd: refuse tree connect without a cipher when encryption is mandatory
In reply to RootSys GmbH Researchksmbd.conf(5) documents "smb3 encryption = mandatory" as "clients that do not support encryption will be denied access to all shares". The kernel does not do that: KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION only makes the server advertise the capability and set SMB2_SESSION_FLAG_ENCRYPT_DATA on sessions that negotiated a cipher. A client that offers no cipher (SMB 2.1, or SMB 3.1.1 without an encryption context) gets an ordinary plaintext session and can use every share. That includes an attacker relaying a victim's NTLM authentication, who chooses the NEGOTIATE contexts itself.
smb2_tree_connect() already refuses a connection without a negotiated cipher for shares that carry KSMBD_SHARE_FLAG_ENCRYPT_DATA. Apply the same check when encryption is mandatory for the whole server.
Found with AI-assisted code review and testing.
--- fs/smb/server/smb2pdu.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
fs/smb/server/smb2pdu.c +6 -2
@@ -3120,8 +3120,12 @@ int smb2_tree_connect(struct ksmbd_work *work) rsp->hdr.Id.SyncId.TreeId =cpu_to_le32 (status.tree_conn->id); share = status.tree_conn->share_conf; -/* A share that requires encryption needs a negotiated SMB3 cipher. */ -if (test_share_config_flag (share,KSMBD_SHARE_FLAG_ENCRYPT_DATA ) && +/* +* A share that requires encryption, or a server configured with +* mandatory encryption, needs a negotiated SMB3 cipher. +*/ +if ((test_share_config_flag (share,KSMBD_SHARE_FLAG_ENCRYPT_DATA ) || + server_conf.flags &KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION ) && !smb3_encryption_negotiated (conn)) {ksmbd_tree_conn_disconnect (sess, status.tree_conn); status.tree_conn =NULL ;
Signature · 1 line
2.43.0
PATCH 4/4 ksmbd: keep negotiated capabilities per connection
In reply to RootSys GmbH Researchinit_smb2_1_server(), init_smb3_0_server(), init_smb3_02_server() and init_smb3_11_server() point conn->vals at the static smb21/smb30/smb302/smb311_server_values tables and then OR capability bits into conn->vals->req_capabilities. Those bits therefore land in the table shared by every connection of that dialect and are never cleared, not even by restarting the server or changing its configuration; later NEGOTIATE responses advertise them.
For SMB 3.0 and 3.0.2 this changes security decisions:smb3_encryption_negotiated() tests req_capabilities, so once a single client has offered SMB2_GLOBAL_CAP_ENCRYPTION, every later session of that dialect is treated as having negotiated encryption, including sessions of clients that never offered it. An attacker can trigger this with one NEGOTIATE.
Give each connection its own copy of its dialect's table and set the capability bits there.
Found with AI-assisted code review and testing.
--- fs/smb/server/connection.h | 2 ++ fs/smb/server/smb2ops.c | 19 +++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-)
fs/smb/server/connection.h +2 -0
@@ -55,6 +55,8 @@ struct ksmbd_transport;struct ksmbd_conn {struct smb_version_values *vals; +/* this connection's copy of its dialect's values, see smb2ops.c */ +struct smb_version_values vals_copy;struct smb_version_ops *ops;struct smb_version_cmds *cmds;unsigned int max_cmds;
fs/smb/server/smb2ops.c +15 -4
@@ -231,6 +231,17 @@ static struct smb_version_cmds smb2_0_server_cmds[NUMBER_OF_SMB2_COMMANDS] = { [SMB2_CHANGE_NOTIFY_HE] = { .proc = smb2_notify}, }; +/* +* The capability bits depend on the client and the configuration, so set +* them on a copy owned by the connection instead of on the shared table. +*/ +static void ksmbd_conn_init_vals (struct ksmbd_conn *conn, +const struct smb_version_values *vals) +{ + conn->vals_copy = *vals; + conn->vals = &conn->vals_copy; +} +/** * init_smb2_1_server() - initialize a smb server connection with smb2.1 * command dispatcher
@@ -238,7 +249,7 @@ static struct smb_version_cmds smb2_0_server_cmds[NUMBER_OF_SMB2_COMMANDS] = {*/ void init_smb2_1_server (struct ksmbd_conn *conn) { - conn->vals = &smb21_server_values; +ksmbd_conn_init_vals (conn, &smb21_server_values); conn->ops = &smb2_0_server_ops; conn->cmds = smb2_0_server_cmds; conn->max_cmds =ARRAY_SIZE (smb2_0_server_cmds);
@@ -255,7 +266,7 @@ void init_smb2_1_server(struct ksmbd_conn *conn)*/ void init_smb3_0_server (struct ksmbd_conn *conn) { - conn->vals = &smb30_server_values; +ksmbd_conn_init_vals (conn, &smb30_server_values); conn->ops = &smb3_0_server_ops; conn->cmds = smb2_0_server_cmds; conn->max_cmds =ARRAY_SIZE (smb2_0_server_cmds);
@@ -285,7 +296,7 @@ void init_smb3_0_server(struct ksmbd_conn *conn)*/ void init_smb3_02_server (struct ksmbd_conn *conn) { - conn->vals = &smb302_server_values; +ksmbd_conn_init_vals (conn, &smb302_server_values); conn->ops = &smb3_0_server_ops; conn->cmds = smb2_0_server_cmds; conn->max_cmds =ARRAY_SIZE (smb2_0_server_cmds);
@@ -316,7 +327,7 @@ void init_smb3_02_server(struct ksmbd_conn *conn)*/ int init_smb3_11_server (struct ksmbd_conn *conn) { - conn->vals = &smb311_server_values; +ksmbd_conn_init_vals (conn, &smb311_server_values); conn->ops = &smb3_11_server_ops; conn->cmds = smb2_0_server_cmds; conn->max_cmds =ARRAY_SIZE (smb2_0_server_cmds);
Signature · 1 line
2.43.0
Hi,
I tested the series on top of ksmbd-for-next with the session setup signing series: with "server signing = mandatory" an unsigned plaintext request on a session that offered a cipher is now refused, with "smb3 encryption = mandatory" a plaintext request on a session told to encrypt is refused, and a client that offers no cipher can no longer tree connect. My binding, expired-session, compound, encryption, guest and anonymous tests give the same results as without the series, the Linux cifs client works, and there were no KASAN reports.
A few comments:
- Signed-off-by: submitting-patches.rst asks for "a known identity (sorry, no anonymous contributions)", and I'm not sure a team name counts, so it may be worth checking. Separately, From: is [email protected] but Signed-off-by is [email protected], which checkpatch warns about.
- Patch 1 depends on sess->sign_required, which no stable tree has yet, so the Cc: stable tags need a dependency note; patch 4 does not depend on anything and could go first.
- Patch 3: MS-SMB2 3.3.5.5 fails the SESSION_SETUP itself with STATUS_ACCESS_DENIED when encryption is required and the dialect is 2.x, or 3.x without SMB2_GLOBAL_CAP_ENCRYPTION, so a relayed login would be stopped before authentication completes. On SMB 3.0 and 3.0.2 the tree connect check never triggers under mandatory encryption, since init_smb3_0_server() and init_smb3_02_server() set the capability unconditionally; patch 2 covers those instead.
- Patch 4: in the default "auto" mode the leaked capability gives a later 3.0 client that offered no encryption sess->enc and clears sess->sign, which is the hole patch 1 closes; that may be worth saying in the commit message.
- A signed but plaintext SESSION_SETUP reauthentication is still accepted on a session that was told to encrypt. Should it be refused? MS-SMB2 3.3.5.2.9 fails an unencrypted request on such a session, but I could not find text saying whether that applies to SESSION_SETUP. Only the session's owner can sign it, so it is minor either way.
Thanks,
Michail
Hi Michail,
thanks a lot for testing this and for the detailed review.
Quoted 5 lines
- Signed-off-by:
submitting-patches.rstasks for "a known identity (sorry, no anonymous contributions)", and I'm not sure a team name counts, so it may be worth checking. Separately, From: is [email protected] but Signed-off-by is [email protected], which checkpatch warns about.
RootSys GmbH Research is the security research team of RootSys GmbH in Innsbruck, Austria, and [email protected] is the team's address. If that does not work for the tree, please let us know.
The From: mismatch is our mail setup: [email protected] is our alias for research, pointing to the [email protected] mailbox on Google Workspace, and Gmail rewrote the header when the series went out through that mailbox. Both addresses reach the same people. v2 will carry an in-body From: so the author matches the Signed-off-by.
Quoted 3 lines
- Patch 1 depends on
sess->sign_required, which no stable tree has yet, so the Cc: stable tags need a dependency note; patch 4 does not depend on anything and could go first.
Agreed, v2 moves the capability patch to the front and adds the dependency note to the stable tag of the signing patch.
Quoted 7 lines
- Patch 3: MS-SMB2 3.3.5.5 fails the
SESSION_SETUPitself withSTATUS_ACCESS_DENIEDwhen encryption is required and the dialect is 2.x, or 3.x withoutSMB2_GLOBAL_CAP_ENCRYPTION, so a relayed login would be stopped before authentication completes. On SMB 3.0 and 3.0.2 the tree connect check never triggers under mandatory encryption, sinceinit_smb3_0_server()andinit_smb3_02_server()set the capability unconditionally; patch 2 covers those instead.
Good point, failing the SESSION_SETUP is better than catching it at tree connect. v2 does the check there as 3.3.5.5 describes and says in the changelog that 3.0/3.0.2 are covered by the plaintext request check.
Quoted 4 lines
- Patch 4: in the default "auto" mode the leaked capability gives a later 3.0 client that offered no encryption
sess->encand clearssess->sign, which is the hole patch 1 closes; that may be worth saying in the commit message.
Will add that.
Quoted 5 lines
- A signed but plaintext
SESSION_SETUPreauthentication is still accepted on a session that was told to encrypt. Should it be refused? MS-SMB2 3.3.5.2.9 fails an unencrypted request on such a session, but I could not find text saying whether that applies toSESSION_SETUP. Only the session's owner can sign it, so it is minor either way.
We didn't find anything explicit either. Since it has to be signed by the session owner, we'd leave it as it is in this series, unless Namjae prefers to have it refused as well.
v2 follows in a day or two, once it has been through the same tests again.
Thanks,
RootSys GmbH Research
Hi,
I tested the series on ksmbd-for-next 4616bbc5389a with KASAN. I used my own probes, smbclient, smbtorture, the Linux cifs client and a Windows 11 client. I compared the series with the unpatched head under four server configurations. The first kept the default settings. The second made signing mandatory. The third made encryption mandatory. The fourth made both mandatory.
My probes negotiated SMB 2.1, 3.0, 3.0.2 and 3.1.1, with and without encryption support. They sent TREE_CONNECT in plaintext, signed and encrypted. NTLM sessions ran in all four configurations. Kerberos sessions on 3.0 and 3.1.1 ran in the two configurations with mandatory encryption. With the series, ksmbd refuses unsigned plaintext requests on sessions that require signing and negotiated a cipher. It refuses plaintext requests on sessions told to encrypt, and tree connects without a cipher when encryption is mandatory. A 3.0 or 3.0.2 client no longer sees an encryption capability that only an earlier client offered. Encrypted requests get the same results as without the series, and so do signed requests on sessions that are not told to encrypt.
Linux client file operations matched those without the series in all four configurations. The mounts used SMB 2.1, 3.0, 3.0.2 and 3.1.1, each without sign or seal, with the sign option and with the seal option. The only difference was the expected SMB 2.1 refusal when encryption is mandatory. With encryption mandatory, I also tested SMB 3.1.1 multichannel and Kerberos mounts on SMB 3.0 and 3.1.1. One Kerberos mount was still in use after its ticket expired. Windows 11 mapped a share and copied, listed and deleted files in all four configurations. The smbtorture subset I ran gave the same results with and without the series in each configuration, including one test that times out in both cases. smbclient also gave the same results, apart from the same SMB 2.1 refusal. No KASAN reports were recorded.
This review leaves out the issues that Sashiko and Michail have already raised. Apart from those, I found one response-handling issue in the new rejection path in patch 2. Only a client that sends plaintext on a session that must encrypt reaches it.
Review
Quoted 5 lines
(quoted diff) +5
-0+ /* MS-SMB2 3.3.5.2.9: a session that must encrypt takes no plaintext */ + if (work->sess && work->sess->enc_required && !work->encrypted) { + conn->ops->set_rsp_status(work, STATUS_ACCESS_DENIED); + return SERVER_HANDLER_ABORT; + }
An unsigned plaintext SMB2 CANCEL for a request that is not pending now gets a STATUS_ACCESS_DENIED response. Without the series it gets no response, as MS-SMB2 3.3.5.16 requires. That section verifies the session only for a signed CANCEL.
Thanks,
Ian
v2 cover ksmbd: enforce mandatory signing and encryption on requests
In reply to RootSys GmbH ResearchFrom: RootSys GmbH Research <[email protected]>
ksmbd does not enforce "server signing = mandatory" or "smb3 encryption = mandatory" on requests once a client has offered a cipher in NEGOTIATE, which the Linux and current Windows clients always do. sess->sign is cleared for such sessions, and the gate added by 2bebf2470af1 ("ksmbd:
enforce signing required by the session") only looks at sess->sign, so an unsigned plaintext request is executed on a session that requires signing, and a plaintext request on a session that was told SMB2_SESSION_FLAG_ENCRYPT_DATA. A client that offers no cipher at all gets a plaintext session even with mandatory encryption. On SMB 3.0 and 3.0.2, one client's SMB2_GLOBAL_CAP_ENCRYPTION is ORed into the static per-dialect table and changes how every later session of that dialect is set up, until the module is reloaded.
The consequence is that a man-in-the-middle between a client and ksmbd can inject requests into an authenticated session and have them run with the victim's credentials, and that an NTLM relay to ksmbd is not stopped by either setting, which is what the two settings exist for.
Patch 1 gives each connection its own copy of its dialect's capability table. Patch 2 makes the request gate honour sess->sign_required. Patch 3 refuses plaintext requests on sessions that were told to encrypt. Patch 4 fails SESSION_SETUP with STATUS_ACCESS_DENIED, as MS-SMB2 3.3.5.5 requires, when encryption is mandatory and the connection cannot encrypt (SMB 2.x, or SMB 3.1.1 without a common cipher).
The series is based on ksmbd-for-next 3c31349456c5, because patch 2 uses sess->sign_required from the session setup signing series queued there. On mainline it applies on top of that series' first three commits, 16ab0adace8f ("ksmbd: verify the signature of session binding requests"), 42a1d80e64a3 ("ksmbd: sign with the negotiated SMB 3.1.1 signing algorithm") and ea472ddf7a9a ("ksmbd: use the channel signing key for session setup on a bound channel"). Patch 1 has no dependencies.
Changes in v2, following the review of v1:
- Order: the per-connection capability patch (v1 4/4) is now patch 1, since it depends on nothing. Its changelog now says that in the default "auto" mode the leaked capability gives a later SMB 3.0 client that offered no encryption sess->enc and clears sess->sign, which is the gap patch 2 closes.
- Stable: patch 2 needs sess->sign_required, so its stable tags now name the three commits above as prerequisites, in the format of stable-kernel-rules.rst. The other patches keep a plain stable tag.
- v1 3/4 ("refuse tree connect without a cipher when encryption is mandatory") is replaced by patch 4, which follows MS-SMB2 3.3.5.5 and fails the SESSION_SETUP itself, so a relayed login is stopped before authentication completes. On SMB 3.0 and 3.0.2, init_smb3_0_server() and init_smb3_02_server() set SMB2_GLOBAL_CAP_ENCRYPTION unconditionally under mandatory encryption, so the check does not trigger there; such sessions are told to encrypt, and patch 3 refuses their plaintext requests.
- A signed plaintext SESSION_SETUP reauthentication on a session that was told to encrypt is still accepted. In MS-SMB2, 3.3.5.2.9 applies where a command section calls for it: 3.3.5.6 (LOGOFF) to 3.3.5.21 (SET_INFO) locate or verify the session "as specified in section 3.3.5.2.9". 3.3.5.5 looks the session up itself and does not refer to 3.3.5.2.9; its only encryption requirement is the dialect and capability check at its start, and neither 3.3.5.5.2 (reauthentication) nor 3.3.5.5.3 asks whether the request was encrypted. Signature verification (3.3.5.2.4) does apply, and ksmbd does it.
- CANCEL (thanks, Ian): patch 3 drops a plaintext SMB2 CANCEL on a session that must encrypt without a response, where v1 answered it with STATUS_ACCESS_DENIED:
MS-SMB2 3.3.5.16 never answers a CANCEL and verifies the session as in 3.3.5.2.9 only for a signed one. The signing check in patch 2 still answers an unsigned CANCEL on a session that requires signing with STATUS_ACCESS_DENIED, as ksmbd already does when sess->sign is set:
3.3.5.16 exempts a CANCEL only from 3.3.5.2.3, and 3.3.5.2.4 fails an unsigned request on a session that requires signing.
- Sashiko's review of v1 2/4 (now patch 3) raised four points. We checked each one on a KASAN kernel:
- Reauthentication: SESSION_SETUP is outside the new check on purpose; smb2_sess_setup() checks the session it reauthenticates. It refuses an unsigned plaintext reauthentication of a user session that must encrypt with STATUS_ACCESS_DENIED at the first leg, and the session stays usable. smb2_sess_needs_protection() is true for every such session, since sess->enc_required implies sess->enc and the global flag. A guest session is not checked there, so an injected reauthentication can expire it, with or without this series; its keys are derived from an all-zero session key, so an attacker on the path could send the same request encrypted. No change.
- Compounds: reproduced. After a plaintext SESSION_SETUP that completes a session, a chained ECHO with SessionId 0 and a chained SESSION_SETUP for a new session got STATUS_ACCESS_DENIED. The check now leaves SESSION_SETUP to smb2_sess_setup() and skips an ECHO with SessionId 0 (3.3.5.17); a related ECHO, which uses the new session, is still refused.
- Order: reproduced for the tree connect. A plaintext request got STATUS_NETWORK_NAME_DELETED for an invalid TreeId and STATUS_ACCESS_DENIED for a valid one. The check now runs where the session is verified, before the tree connect is looked up, so both get STATUS_ACCESS_DENIED. A session whose Kerberos ticket has expired still answers STATUS_NETWORK_SESSION_EXPIRED first, since 3.3.5.2.9 checks Session.State before EncryptData.
- Reauthentication response: reproduced, the response to an encrypted NTLM reauthentication had SessionFlags 0. Patch 3 now sets SMB2_SESSION_FLAG_ENCRYPT_DATA there, as 3.3.5.5.3 requires. The Linux client is not affected either way: it sends a non-binding SESSION_SETUP only with SessionId 0, so after a reconnect it sets up a new session, and a vers=3.1.1 mount without "seal" under mandatory encryption kept working across a server restart, with and without the series.
- Tested-by added to patches 1 and 2, whose code is unchanged from v1. Patch 3 has changed (CANCEL and the points above) and patch 4 is new code; they carry none.
- The patches carry an in-body From: [email protected], so the author matches Signed-off-by:. The mail itself goes out through the [email protected] mailbox, which [email protected] is an alias of.
v1: https://lore.kernel.org/linux-cifs/[email protected]/
Testing: W=1 builds of fs/smb/server after each patch, with CONFIG_SMB_SERVER=y (and CONFIG_SMB_SERVER_KERBEROS5=y) and =m, give no warnings. checkpatch --strict reports no errors or warnings, only CamelCase checks for the MS-SMB2 field names SessionId and SessionFlags. On KASAN kernels built from
ksmbd-for-next 3c31349456c5 and from mainline 8dac6df92a2e (with the three commits above), each with this series:
- every unsigned or plaintext request that gets through on the unpatched trees is refused with STATUS_ACCESS_DENIED: TREE_CONNECT after an NTLM login over SMB 3.1.1 with a cipher offered (mandatory signing, and mandatory encryption), and over SMB 3.0 after another client offered SMB2_GLOBAL_CAP_ENCRYPTION;
- under mandatory encryption, SMB 2.1 and SMB 3.1.1 clients that offer no cipher fail the first SESSION_SETUP with STATUS_ACCESS_DENIED, and an SMB 3.0 client without SMB2_GLOBAL_CAP_ENCRYPTION is told to encrypt and its plaintext TREE_CONNECT is refused;
- the next NEGOTIATE no longer advertises a capability another client offered;
- an unsigned plaintext CANCEL for a request that is not pending gets no response on a session that must encrypt, as without the series; on a session that requires signing it gets STATUS_ACCESS_DENIED, as it did without the series when the client offered no cipher;
- on a session that must encrypt, a plaintext request is refused with STATUS_ACCESS_DENIED whether its TreeId is valid or not, an unsigned plaintext reauthentication is refused and the session stays usable, and the response to an encrypted reauthentication carries SMB2_SESSION_FLAG_ENCRYPT_DATA;
- in a plaintext compound, after the SESSION_SETUP that completes a session, an ECHO with SessionId 0 and a SESSION_SETUP for a new session are processed as without the series, and a related ECHO is refused;
- the in-kernel cifs client mounts, writes and reads under mandatory signing with vers=2.1, 3.0, 3.0.2 and 3.1.1 using sign, and with vers=3.0, 3.0.2 and 3.1.1 using seal; under mandatory encryption with vers=3.0, 3.0.2 and 3.1.1, sign or seal. vers=2.1 under mandatory encryption fails at SESSION_SETUP with STATUS_ACCESS_DENIED, as intended. A vers=3.1.1 mount without "seal" under mandatory encryption keeps working across a server restart.
No KASAN, UBSAN or WARN reports. Not tested: Kerberos sessions (krb5_authenticate() gets the same change as ntlm_authenticate() in patch 3), SMB Direct, multichannel binding, and a signed plaintext CANCEL on a session that must encrypt.
We found this with AI-assisted code review and testing; the reproducer and the fix were tested by us on current mainline.
RootSys GmbH Research (4):
ksmbd: keep negotiated capabilities per connection
ksmbd: require signing on sessions that negotiated encryption
ksmbd: reject plaintext requests on sessions that must be encrypted
ksmbd: refuse session setup without encryption when it is mandatory
fs/smb/server/connection.h | 2 ++ fs/smb/server/mgmt/user_session.h | 2 ++ fs/smb/server/server.c | 41 ++++++++++++++++++++++++++++--- fs/smb/server/smb2ops.c | 19 +++++++++++--- fs/smb/server/smb2pdu.c | 25 +++++++++++++++++-- 5 files changed, 80 insertions(+), 9 deletions(-)
base-commit: 3c31349456c5b71edc837270cf9075b28dc59ee3
Signature · 1 line
2.43.0
PATCH v2 1/4 ksmbd: keep negotiated capabilities per connection
From: RootSys GmbH Research <[email protected]>
init_smb2_1_server(), init_smb3_0_server(), init_smb3_02_server() and init_smb3_11_server() point conn->vals at the static smb21/smb30/smb302/smb311_server_values tables and then OR capability bits into conn->vals->req_capabilities. Those bits therefore land in the table shared by every connection of that dialect and are never cleared, not even by restarting the server or changing its configuration; later NEGOTIATE responses advertise them.
For SMB 3.0 and 3.0.2 this changes security decisions:smb3_encryption_negotiated() tests req_capabilities, so once a single client has offered SMB2_GLOBAL_CAP_ENCRYPTION, every later session of that dialect is treated as having negotiated encryption, including sessions of clients that never offered it. An attacker can trigger this with one NEGOTIATE.
In the default "smb3 encryption = auto" mode, such a later SMB 3.0 client that offered no encryption gets sess->enc set and sess->sign cleared in SESSION_SETUP, but is not told to encrypt. With mandatory signing, its unsigned plaintext requests then pass __process_request(), which only checks sess->sign. That is the gap "ksmbd: require signing on sessions that negotiated encryption" closes for clients that do offer a cipher; here it opens for a client that never did.
Give each connection its own copy of its dialect's table and set the capability bits there.
Found with AI-assisted code review and testing.
--- fs/smb/server/connection.h | 2 ++ fs/smb/server/smb2ops.c | 19 +++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-)
fs/smb/server/connection.h +2 -0
@@ -55,6 +55,8 @@ struct ksmbd_transport;struct ksmbd_conn {struct smb_version_values *vals; +/* this connection's copy of its dialect's values, see smb2ops.c */ +struct smb_version_values vals_copy;struct smb_version_ops *ops;struct smb_version_cmds *cmds;unsigned int max_cmds;
fs/smb/server/smb2ops.c +15 -4
@@ -231,6 +231,17 @@ static struct smb_version_cmds smb2_0_server_cmds[NUMBER_OF_SMB2_COMMANDS] = { [SMB2_CHANGE_NOTIFY_HE] = { .proc = smb2_notify}, }; +/* +* The capability bits depend on the client and the configuration, so set +* them on a copy owned by the connection instead of on the shared table. +*/ +static void ksmbd_conn_init_vals (struct ksmbd_conn *conn, +const struct smb_version_values *vals) +{ + conn->vals_copy = *vals; + conn->vals = &conn->vals_copy; +} +/** * init_smb2_1_server() - initialize a smb server connection with smb2.1 * command dispatcher
@@ -238,7 +249,7 @@ static struct smb_version_cmds smb2_0_server_cmds[NUMBER_OF_SMB2_COMMANDS] = {*/ void init_smb2_1_server (struct ksmbd_conn *conn) { - conn->vals = &smb21_server_values; +ksmbd_conn_init_vals (conn, &smb21_server_values); conn->ops = &smb2_0_server_ops; conn->cmds = smb2_0_server_cmds; conn->max_cmds =ARRAY_SIZE (smb2_0_server_cmds);
@@ -255,7 +266,7 @@ void init_smb2_1_server(struct ksmbd_conn *conn)*/ void init_smb3_0_server (struct ksmbd_conn *conn) { - conn->vals = &smb30_server_values; +ksmbd_conn_init_vals (conn, &smb30_server_values); conn->ops = &smb3_0_server_ops; conn->cmds = smb2_0_server_cmds; conn->max_cmds =ARRAY_SIZE (smb2_0_server_cmds);
@@ -285,7 +296,7 @@ void init_smb3_0_server(struct ksmbd_conn *conn)*/ void init_smb3_02_server (struct ksmbd_conn *conn) { - conn->vals = &smb302_server_values; +ksmbd_conn_init_vals (conn, &smb302_server_values); conn->ops = &smb3_0_server_ops; conn->cmds = smb2_0_server_cmds; conn->max_cmds =ARRAY_SIZE (smb2_0_server_cmds);
@@ -316,7 +327,7 @@ void init_smb3_02_server(struct ksmbd_conn *conn)*/ int init_smb3_11_server (struct ksmbd_conn *conn) { - conn->vals = &smb311_server_values; +ksmbd_conn_init_vals (conn, &smb311_server_values); conn->ops = &smb3_11_server_ops; conn->cmds = smb2_0_server_cmds; conn->max_cmds =ARRAY_SIZE (smb2_0_server_cmds);
Signature · 1 line
2.43.0
PATCH v2 2/4 ksmbd: require signing on sessions that negotiated encryption
In reply to RootSys GmbH ResearchFrom: RootSys GmbH Research <[email protected]>
ntlm_authenticate() and krb5_authenticate() clear sess->sign whenever smb3_encryption_negotiated() is true, that is whenever the client offered a cipher in NEGOTIATE. Offering a cipher does not mean the client will encrypt: unless the server sets SMB2_SESSION_FLAG_ENCRYPT_DATA, the client keeps sending plaintext requests and signs them because signing is required.
__process_request() only refuses an unsigned plaintext request when sess->sign is set, so on a server with "server signing = mandatory", or for a client that sent SMB2_NEGOTIATE_SIGNING_REQUIRED, unsigned plaintext requests are executed as soon as the client offered a cipher. The Linux client always offers one, and so do current Windows clients. An attacker on the path between client and server can then inject requests that run with the victim's session and tree connections, which mandatory signing is meant to prevent; commit 2bebf2470af1 ("ksmbd: enforce signing required by the session") closed this only for sessions that did not negotiate encryption.
sess->sign_required already records that signing was required when the session was set up. Use it in the request gate too.
Found with AI-assisted code review and testing.
--- fs/smb/server/server.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
fs/smb/server/server.c +6 -2
@@ -203,8 +203,12 @@ static int __process_request(struct ksmbd_work *work, struct ksmbd_conn *conn, } signed_req = conn->ops->is_sign_req && conn->ops->is_sign_req (work, command); -if (work->sess && work->sess->sign && !work->encrypted && - !signed_req) { +/* +* sess->sign is cleared once encryption is negotiated, even when the +* client does not encrypt, so check sess->sign_required as well. +*/ +if (work->sess && (work->sess->sign || work->sess->sign_required) && + !work->encrypted && !signed_req) { conn->ops->set_rsp_status (work,STATUS_ACCESS_DENIED );return SERVER_HANDLER_ABORT ; }
Signature · 1 line
2.43.0
PATCH v2 3/4 ksmbd: reject plaintext requests on sessions that must be encrypted
In reply to RootSys GmbH ResearchFrom: RootSys GmbH Research <[email protected]>
With "smb3 encryption = mandatory" ksmbd sets SMB2_SESSION_FLAG_ENCRYPT_DATA in the SESSION_SETUP response of every session that negotiated a cipher, so the client encrypts all further requests. Nothing on the request path checks this, though: a plaintext request on such a session is executed like an encrypted one. Since sess->sign is cleared for these sessions, it does not even have to be signed. The only encryption check on the request path is the one for shares carrying KSMBD_SHARE_FLAG_ENCRYPT_DATA.
An attacker on the path can therefore inject plaintext, unsigned requests into a session that both sides believe to be encrypted. MS-SMB2 3.3.5.2.9 requires the server to fail such a request with STATUS_ACCESS_DENIED.
Record in the session that it was told to encrypt and refuse unencrypted requests on it with STATUS_ACCESS_DENIED where the session is verified, before the tree connect is looked up, in the order of MS-SMB2 3.3.5.2.9. SESSION_SETUP is left to smb2_sess_setup(), which checks the session it reauthenticates, and an ECHO without a SessionId is not verified (3.3.5.17). A plaintext SMB2 CANCEL on such a session is dropped without a response: MS-SMB2 3.3.5.16 never answers a CANCEL and verifies the session as in 3.3.5.2.9 only for a signed one.
The response to a reauthentication did not carry SMB2_SESSION_FLAG_ENCRYPT_DATA; set it there too, as 3.3.5.5.3 requires, so that a client that takes its session flags from that response keeps encrypting.
Found with AI-assisted code review and testing.
--- fs/smb/server/mgmt/user_session.h | 2 ++ fs/smb/server/server.c | 33 ++++++++++++++++++++++++++++++- fs/smb/server/smb2pdu.c | 14 +++++++++++-- 3 files changed, 46 insertions(+), 3 deletions(-)
fs/smb/server/mgmt/user_session.h +2 -0
@@ -62,6 +62,8 @@ struct ksmbd_session {/* signing was required at setup; sign is cleared with encryption */ bool sign_required;bool enc; +/* the session was told to encrypt (SMB2_SESSION_FLAG_ENCRYPT_DATA) */ +bool enc_required;bool tearing_down;int state;
fs/smb/server/server.c +32 -1
@@ -245,6 +245,22 @@ static int __process_request(struct ksmbd_work *work, struct ksmbd_conn *conn,return SERVER_HANDLER_CONTINUE ; } +/* +* MS-SMB2 3.3.5.2.9: a session that must encrypt takes no plaintext request. +* SESSION_SETUP checks its own session in smb2_sess_setup(), and an ECHO +* without a SessionId is not verified (3.3.5.17). +*/ +static bool ksmbd_plaintext_refused (struct ksmbd_work *work,u16 cmd) +{ +struct smb2_hdr *hdr =ksmbd_req_buf_next (work); + +if (!work->sess || !work->sess->enc_required || work->encrypted) +return false; +if (cmd ==SMB2_SESSION_SETUP_HE ) +return false; +return cmd !=SMB2_ECHO_HE ||le64_to_cpu (hdr->SessionId); +} +static void __handle_ksmbd_work (struct ksmbd_work *work,struct ksmbd_conn *conn) {
@@ -371,7 +387,22 @@ static void __handle_ksmbd_work(struct ksmbd_work *work, rsp_hdr->Flags |=SMB2_FLAGS_SIGNED ; } goto send; - }else if (rc >0 ) { + } +/* +* Before the tree connect is looked up, so that a +* plaintext request learns nothing about its TreeId. +* A CANCEL is never answered (MS-SMB2 3.3.5.16). +*/ + command = conn->ops->get_cmd_val (work); +if (ksmbd_plaintext_refused (work, command)) { +if (command ==SMB2_CANCEL_HE ) + work->send_no_response =1 ; +else + conn->ops->set_rsp_status (work, +STATUS_ACCESS_DENIED ); + goto send; + } +if (rc >0 ) { rc = conn->ops->get_ksmbd_tcon (work);if (rc <0 ) {if (rc == -EINVAL )
fs/smb/server/smb2pdu.c +12 -2
@@ -2419,8 +2419,10 @@ static int ntlm_authenticate(struct ksmbd_work *work, !(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) { conn->ops->generate_encryptionkey (conn, sess); sess->enc = true; -if (server_conf.flags &KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION ) +if (server_conf.flags &KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION ) { rsp->SessionFlags =SMB2_SESSION_FLAG_ENCRYPT_DATA_LE ; + sess->enc_required = true; + }/* * signing is disable if encryption is enable * on this session
@@ -2525,8 +2527,10 @@ static int krb5_authenticate(struct ksmbd_work *work, !(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) { conn->ops->generate_encryptionkey (conn, sess); sess->enc = true; -if (server_conf.flags &KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION ) +if (server_conf.flags &KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION ) { rsp->SessionFlags =SMB2_SESSION_FLAG_ENCRYPT_DATA_LE ; + sess->enc_required = true; + } sess->sign = false; }
@@ -3052,6 +3056,12 @@ int smb2_sess_setup(struct ksmbd_work *work) }if (authenticated) { +/* +* MS-SMB2 3.3.5.5.3: the response to a reauthentication tells +* the client to keep encrypting, too. +*/ +if (sess->enc_required && !work->session_setup_binding) + rsp->SessionFlags =SMB2_SESSION_FLAG_ENCRYPT_DATA_LE ;/* * Draining the previous session drops srv_mutex. Finish using the * connection's authentication state and free mechToken first, and
Signature · 1 line
2.43.0
Namjae Jeon
maintainerRe: [PATCH v2 3/4] ksmbd: reject plaintext requests on sessions that must be encrypted
Quoted 8 lines
fs/smb/server/server.c +3
-1@@ -371,7 +387,22 @@ static void __handle_ksmbd_work(struct ksmbd_work *work, rsp_hdr->Flags |=SMB2_FLAGS_SIGNED ; } goto send; - }else if (rc >0 ) { + } +/* +* Before the tree connect is looked up, so that a
fs/smb/server/server.c static void __handle_ksmbd_work(struct ksmbd_work *work, · line 402
Quoted 10 lines
fs/smb/server/server.c +10
-0+* plaintext request learns nothing about its TreeId. +* A CANCEL is never answered (MS-SMB2 3.3.5.16). +*/ + command = conn->ops->get_cmd_val (work); +if (ksmbd_plaintext_refused (work, command)) { +if (command ==SMB2_CANCEL_HE ) + work->send_no_response =1 ; +else + conn->ops->set_rsp_status (work, +STATUS_ACCESS_DENIED );
For the response path, this jumps to send before __process_request() validates the message, so the MessageId is left unconsumed. It also skips credit settlement. If this ends a compound, credits from earlier commands may not be returned. Could we validate the request and call ksmbd_set_failed_rsp_credits() before sending the error response?
Quoted 11 lines
fs/smb/server/server.c +3
-0+ goto send; + } +if (rc >0 ) { rc = conn->ops->get_ksmbd_tcon (work);if (rc <0 ) {if (rc == -EINVAL )fs/smb/server/smb2pdu.c +0
-0
fs/smb/server/smb2pdu.c static int ntlm_authenticate(struct ksmbd_work *work, · line 2424
Quoted 7 lines
fs/smb/server/smb2pdu.c +2
-1@@ -2419,8 +2419,10 @@ static int ntlm_authenticate(struct ksmbd_work *work, !(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) { conn->ops->generate_encryptionkey (conn, sess); sess->enc = true; -if (server_conf.flags &KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION ) +if (server_conf.flags &KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION ) { rsp->SessionFlags =SMB2_SESSION_FLAG_ENCRYPT_DATA_LE ; + sess->enc_required = true;
The guest path above sets SMB2_SESSION_FLAG_IS_GUEST_LE and skips NTLM session-key setup, but still reaches this block. In mandatory mode, the existing assignment overwrites it with SMB2_SESSION_FLAG_ENCRYPT_DATA_LE. The new enc_required flag also marks a guest session.
fs/smb/server/smb2pdu.c static int krb5_authenticate(struct ksmbd_work *work, · line 2532
Quoted 12 lines
fs/smb/server/smb2pdu.c +3
-1+ }/* * signing is disable if encryption is enable * on this session @@ -2525,8 +2527,10 @@ static int krb5_authenticate(struct ksmbd_work *work, !(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) { conn->ops->generate_encryptionkey (conn, sess); sess->enc = true; -if (server_conf.flags &KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION ) +if (server_conf.flags &KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION ) { rsp->SessionFlags =SMB2_SESSION_FLAG_ENCRYPT_DATA_LE ; + sess->enc_required = true;
smb2_sess_setup() treats SessionId == 0 as a new session and clears conn->binding, but this condition still uses the request’s BINDING bit directly. With SessionId == 0 and BINDING set, the new session can skip encryption setup and leave enc_required clear. Could we validate this flag/SessionId combination before authentication and use conn->binding in both auth paths?
Quoted 20 lines
fs/smb/server/smb2pdu.c +7
-1+ } sess->sign = false; }@@ -3052,6 +3056,12 @@ int smb2_sess_setup(struct ksmbd_work *work) }if (authenticated) { +/* +* MS-SMB2 3.3.5.5.3: the response to a reauthentication tells +* the client to keep encrypting, too. +*/ +if (sess->enc_required && !work->session_setup_binding) + rsp->SessionFlags =SMB2_SESSION_FLAG_ENCRYPT_DATA_LE ;/* * Draining the previous session drops srv_mutex. Finish using the * connection's authentication state and free mechToken first, and -- 2.43.0
PATCH v2 4/4 ksmbd: refuse session setup without encryption when it is mandatory
In reply to RootSys GmbH ResearchFrom: RootSys GmbH Research <[email protected]>
ksmbd.conf(5) documents "smb3 encryption = mandatory" as "clients that do not support encryption will be denied access to all shares". The kernel does not do that: KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION only makes the server advertise the capability and set SMB2_SESSION_FLAG_ENCRYPT_DATA on sessions that negotiated encryption. A client that cannot encrypt (SMB 2.1, or SMB 3.1.1 without an encryption context) gets an ordinary plaintext session and can use every share. That includes an attacker relaying a victim's NTLM authentication, who chooses the dialect and NEGOTIATE contexts itself.
MS-SMB2 3.3.5.5 stops such a client at SESSION_SETUP, before it authenticates:
If the server implements the SMB 3.x dialect family, Connection.Dialect does not belong to the SMB 3.x dialect family, EncryptData is TRUE, and RejectUnencryptedAccess is TRUE, the server MUST fail the request with STATUS_ACCESS_DENIED.
If Connection.Dialect belongs to the SMB 3.x dialect family, EncryptData is TRUE, RejectUnencryptedAccess is TRUE, and Connection.ClientCapabilities does not include the SMB2_GLOBAL_CAP_ENCRYPTION bit, the server MUST fail the request with STATUS_ACCESS_DENIED.
Do the same: when encryption is mandatory, fail every SESSION_SETUP with STATUS_ACCESS_DENIED on a connection that has not negotiated encryption, that is SMB 2.x, or SMB 3.1.1 without a common cipher.
On SMB 3.0 and 3.0.2 this check does not trigger:init_smb3_0_server() and init_smb3_02_server() set SMB2_GLOBAL_CAP_ENCRYPTION unconditionally when encryption is mandatory, so every such session is told to encrypt, whatever the client offered, and "ksmbd: reject plaintext requests on sessions that must be encrypted" refuses its plaintext requests.
Found with AI-assisted code review and testing.
--- fs/smb/server/smb2pdu.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
fs/smb/server/smb2pdu.c +11 -0
@@ -2658,6 +2658,17 @@ int smb2_sess_setup(struct ksmbd_work *work) rsp->SecurityBufferLength =0 ;ksmbd_conn_lock (conn); +/* +* MS-SMB2 3.3.5.5: when encryption is mandatory, a connection that +* cannot encrypt gets no session. SMB 3.0 and 3.0.2 connections always +* negotiate encryption then, see init_smb3_0_server(). +*/ +if (server_conf.flags &KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION && + !smb3_encryption_negotiated (conn)) { + rc = -EKEYREJECTED ; + goto out_err; + } +if (!req->hdr.SessionId) {/* Bound abandoned SessionId-zero authentication exchanges. */ if (ksmbd_too_many_session_setups (conn)) {
Signature · 1 line
2.43.0
Namjae Jeon
maintainerRe: [PATCH v2 4/4] ksmbd: refuse session setup without encryption when it is mandatory
fs/smb/server/smb2pdu.c int smb2_sess_setup(struct ksmbd_work *work) · line 2667
Quoted 11 lines
fs/smb/server/smb2pdu.c +7
-0@@ -2658,6 +2658,17 @@ int smb2_sess_setup(struct ksmbd_work *work) rsp->SecurityBufferLength =0 ;ksmbd_conn_lock (conn); +/* +* MS-SMB2 3.3.5.5: when encryption is mandatory, a connection that +* cannot encrypt gets no session. SMB 3.0 and 3.0.2 connections always +* negotiate encryption then, see init_smb3_0_server(). +*/ +if (server_conf.flags &KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION && + !smb3_encryption_negotiated (conn)) {
I think this still lets clients through when they don’t support encryption. The above comment says SMB 3.0/3.0.2 always negotiate it, but that’s just the server advertising the capability. it doesn’t mean the client offered it. In mandatory mode, smb3_encryption_negotiated() still returns true when conn->cli_cap lacks the bit. Could we check conn->cli_cap for these dialects? For SMB 3.1.1, the selected cipher should be enough.
Quoted 10 lines
fs/smb/server/smb2pdu.c +4
-1+ rc = -EKEYREJECTED ; + goto out_err; + } +if (!req->hdr.SessionId) {/* Bound abandoned SessionId-zero authentication exchanges. */ if (ksmbd_too_many_session_setups (conn)) { --2.43.0