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