* [PATCH v3 0/5] ksmbd: enforce mandatory signing and encryption on requests
@ 2026-12-04 6:53 RootSys GmbH Research
2026-12-04 6:53 ` [PATCH v3 1/5] ksmbd: keep negotiated capabilities per connection RootSys GmbH Research
` (5 more replies)
0 siblings, 6 replies; 8+ messages in thread
From: RootSys GmbH Research @ 2026-12-04 6:53 UTC (permalink / raw)
To: Namjae Jeon
Cc: Michail Andreas Michalopoulos, Ian Bridges, Sergey Senozhatsky,
Tom Talpey, ChenXiaoSong, linux-cifs, RootSys GmbH Research
From: RootSys GmbH Research <research@rootsys.at>
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 cannot encrypt gets a
plaintext session even with mandatory encryption, and so does a
SESSION_SETUP that sets SMB2_SESSION_REQ_FLAG_BINDING without a
SessionId. 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 a binding SESSION_SETUP without a SessionId. Patch 4 refuses
plaintext requests on sessions that were told to encrypt. Patch 5 fails
SESSION_SETUP with STATUS_ACCESS_DENIED, as MS-SMB2 3.3.5.5 requires,
when encryption is mandatory and the client cannot encrypt, and fails a
guest login then.
The series is based on ksmbd-for-next 43dc36660aff. Patch 2 uses
sess->sign_required, and patch 4 ksmbd_set_failed_rsp_credits() and the
MessageId check on the failed-session path, from commits queued there.
On mainline 007722b8211f it applies on top of the eight ksmbd-for-next
commits from e1b357794105 ("ksmbd: verify the signature of session
binding requests") to 332ac50e7533 ("ksmbd: account for the credits of
a request on a failed session"). Patch 1 has no dependencies.
Changes in v3, following Namjae's review of v2:
- Patch 5 (v2 4/4) checks what the client offered, as 3.3.5.5 says:
SMB2_GLOBAL_CAP_ENCRYPTION in the client's NEGOTIATE capabilities on
SMB 3.0 and 3.0.2, a common cipher on SMB 3.1.1. v2 used
smb3_encryption_negotiated(), which holds on 3.0 and 3.0.2 whenever
encryption is mandatory, because init_smb3_0_server() and
init_smb3_02_server() then set the capability on the server side. An
SMB 3.0 client that offered no encryption got a session and was only
stopped at its first plaintext request; it now fails the first
SESSION_SETUP.
- Patch 4 (v2 3/4) handles a refused plaintext request like a request
on a failed session: the request is verified first, which consumes
its MessageId and refuses a replay, and its credits are charged and
new ones granted before the response is sent. In v2 a refused
request did neither: 600 plaintext ECHOs on a session that must
encrypt were answered with no credits and left their MessageIds
unconsumed, and ksmbd dropped the connection at the client's next,
encrypted, request. With v3 the same 600 requests get their credits
and the next encrypted request succeeds. A plaintext CANCEL
is still dropped without a response and consumes no credit.
- Guest sessions: with mandatory encryption a guest login reached the
encryption setup, the response carried SMB2_SESSION_FLAG_ENCRYPT_DATA
instead of SMB2_SESSION_FLAG_IS_GUEST, the keys were derived from an
all-zero session key, and v2 also set sess->enc_required on it. A
guest login got such a session with and without v2, and requests
encrypted with keys derived from a zero key were accepted on it.
3.3.5.5.3 derives encryption keys only for a session that is neither
guest nor anonymous, so a guest session cannot meet the setting;
patch 5 now fails a guest login with STATUS_ACCESS_DENIED when
encryption is mandatory, as Samba does.
- New patch 3: smb2_sess_setup() treats SessionId 0 as a new session
and clears conn->binding, but ntlm_authenticate() and
krb5_authenticate() looked at the request's
SMB2_SESSION_REQ_FLAG_BINDING bit to skip the encryption setup. With
NTLM, the challenge stays on the connection, so after the pending
session of the first leg has failed, an AUTHENTICATE sent with
SessionId 0 and the binding bit set up a new session without
encryption keys and without SMB2_SESSION_FLAG_ENCRYPT_DATA, and an
unsigned plaintext TREE_CONNECT on it succeeded, with and without
v2. Patch 3 fails such a request with STATUS_INVALID_PARAMETER, as
Samba does, and both authentication paths use conn->binding. On
every other path that reaches them, conn->binding and the bit agree.
- Stable: patch 2's prerequisite tags name the current ksmbd-for-next
ids of the three commits; patch 4 now also names ac68edfa99c7 and
332ac50e7533 as prerequisites.
- Tested-by kept on patches 1 and 2, whose code is unchanged since v1.
Patch 3 is new and patches 4 and 5 have changed; they carry none.
The v2 changes (order, stable tags, v1 3/4 replaced by the
SESSION_SETUP check, CANCEL, Sashiko's four points) are described in
the v2 cover letter.
v2: https://lore.kernel.org/linux-cifs/cover.1795701726.git.research@rootsys.at/
v1: https://lore.kernel.org/linux-cifs/20261119103805.29514-1-research@rootsys.at/
Testing: W=1 builds of fs/smb/server after each patch, with
CONFIG_SMB_SERVER=y and CONFIG_SMB_SERVER_KERBEROS5=y, 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 43dc36660aff and from mainline
007722b8211f with the eight commits above, each with this series:
- every unsigned or plaintext request that gets through on the
unpatched tree 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 clients, SMB 3.0 clients without
SMB2_GLOBAL_CAP_ENCRYPTION, SMB 3.1.1 clients that offer no cipher
and guest logins fail SESSION_SETUP with STATUS_ACCESS_DENIED, and an
AUTHENTICATE with SessionId 0 and the binding bit fails with
STATUS_INVALID_PARAMETER;
- 600 refused plaintext requests are granted credits, and an encrypted
request after them succeeds;
- 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; on a session that
requires signing it gets STATUS_ACCESS_DENIED;
- 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 changes as ntlm_authenticate() in
patches 3 and 4), 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 (5):
ksmbd: keep negotiated capabilities per connection
ksmbd: require signing on sessions that negotiated encryption
ksmbd: refuse a binding request without a SessionId
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 | 51 ++++++++++++++++++++++++--
fs/smb/server/smb2ops.c | 19 +++++++---
fs/smb/server/smb2pdu.c | 60 +++++++++++++++++++++++++++----
5 files changed, 121 insertions(+), 13 deletions(-)
base-commit: 43dc36660aff09d415b2949d3498c94763798b21
--
2.43.0
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v3 1/5] ksmbd: keep negotiated capabilities per connection
2026-12-04 6:53 [PATCH v3 0/5] ksmbd: enforce mandatory signing and encryption on requests RootSys GmbH Research
@ 2026-12-04 6:53 ` RootSys GmbH Research
2026-12-04 6:53 ` [PATCH v3 2/5] ksmbd: require signing on sessions that negotiated encryption RootSys GmbH Research
` (4 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: RootSys GmbH Research @ 2026-12-04 6:53 UTC (permalink / raw)
To: Namjae Jeon
Cc: Michail Andreas Michalopoulos, Ian Bridges, Sergey Senozhatsky,
Tom Talpey, ChenXiaoSong, linux-cifs, RootSys GmbH Research
From: RootSys GmbH Research <research@rootsys.at>
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.
Fixes: e2f34481b24d ("cifsd: add server-side procedures for SMB3")
Cc: stable@vger.kernel.org
Tested-by: Michail Andreas Michalopoulos <mike.michalo@gmail.com>
Signed-off-by: RootSys GmbH Research <research@rootsys.at>
---
fs/smb/server/connection.h | 2 ++
fs/smb/server/smb2ops.c | 19 +++++++++++++++----
2 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/fs/smb/server/connection.h b/fs/smb/server/connection.h
index 891612382d0f..adddcda735c6 100644
--- a/fs/smb/server/connection.h
+++ b/fs/smb/server/connection.h
@@ -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;
diff --git a/fs/smb/server/smb2ops.c b/fs/smb/server/smb2ops.c
index c5b3cf34494e..4a69c8832820 100644
--- a/fs/smb/server/smb2ops.c
+++ b/fs/smb/server/smb2ops.c
@@ -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);
--
2.43.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v3 2/5] ksmbd: require signing on sessions that negotiated encryption
2026-12-04 6:53 [PATCH v3 0/5] ksmbd: enforce mandatory signing and encryption on requests RootSys GmbH Research
2026-12-04 6:53 ` [PATCH v3 1/5] ksmbd: keep negotiated capabilities per connection RootSys GmbH Research
@ 2026-12-04 6:53 ` RootSys GmbH Research
2026-12-04 6:53 ` [PATCH v3 3/5] ksmbd: refuse a binding request without a SessionId RootSys GmbH Research
` (3 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: RootSys GmbH Research @ 2026-12-04 6:53 UTC (permalink / raw)
To: Namjae Jeon
Cc: Michail Andreas Michalopoulos, Ian Bridges, Sergey Senozhatsky,
Tom Talpey, ChenXiaoSong, linux-cifs, RootSys GmbH Research
From: RootSys GmbH Research <research@rootsys.at>
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.
Fixes: 2bebf2470af1 ("ksmbd: enforce signing required by the session")
Cc: stable@vger.kernel.org # 7.2.x: e1b357794105: ksmbd: verify the signature of session binding requests
Cc: stable@vger.kernel.org # 7.2.x: 9834c8c44feb: ksmbd: sign with the negotiated SMB 3.1.1 signing algorithm
Cc: stable@vger.kernel.org # 7.2.x: 4e23a40e3e69: ksmbd: use the channel signing key for session setup on a bound channel
Cc: stable@vger.kernel.org # 7.2.x
Tested-by: Michail Andreas Michalopoulos <mike.michalo@gmail.com>
Signed-off-by: RootSys GmbH Research <research@rootsys.at>
---
fs/smb/server/server.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/fs/smb/server/server.c b/fs/smb/server/server.c
index a376e2bed59b..74851291b9b2 100644
--- a/fs/smb/server/server.c
+++ b/fs/smb/server/server.c
@@ -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;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v3 3/5] ksmbd: refuse a binding request without a SessionId
2026-12-04 6:53 [PATCH v3 0/5] ksmbd: enforce mandatory signing and encryption on requests RootSys GmbH Research
2026-12-04 6:53 ` [PATCH v3 1/5] ksmbd: keep negotiated capabilities per connection RootSys GmbH Research
2026-12-04 6:53 ` [PATCH v3 2/5] ksmbd: require signing on sessions that negotiated encryption RootSys GmbH Research
@ 2026-12-04 6:53 ` RootSys GmbH Research
2026-12-04 6:53 ` [PATCH v3 4/5] ksmbd: reject plaintext requests on sessions that must be encrypted RootSys GmbH Research
` (2 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: RootSys GmbH Research @ 2026-12-04 6:53 UTC (permalink / raw)
To: Namjae Jeon
Cc: Michail Andreas Michalopoulos, Ian Bridges, Sergey Senozhatsky,
Tom Talpey, ChenXiaoSong, linux-cifs, RootSys GmbH Research
From: RootSys GmbH Research <research@rootsys.at>
smb2_sess_setup() handles a SESSION_SETUP with SessionId 0 as the start
of a new session and clears conn->binding, whatever its Flags say. The
authentication paths, though, decide whether to derive encryption keys
from the request's SMB2_SESSION_REQ_FLAG_BINDING bit. A request with
SessionId 0 and that bit set therefore sets up a new session without
encryption keys: sess->enc stays false, and with "smb3 encryption =
mandatory" the response does not carry SMB2_SESSION_FLAG_ENCRYPT_DATA.
A client, or an attacker relaying a client's NTLM authentication, gets
a plaintext session from a server that requires encryption.
A binding request always names the session it binds to (MS-SMB2
3.2.4.2.3, 3.3.5.5). Fail one without a SessionId with
STATUS_INVALID_PARAMETER, as Samba does, and use conn->binding in
ntlm_authenticate() and krb5_authenticate(), which is what the rest of
both functions already looks at.
Found with AI-assisted code review and testing.
Fixes: f5a544e3bab7 ("ksmbd: add support for SMB3 multichannel")
Cc: stable@vger.kernel.org
Signed-off-by: RootSys GmbH Research <research@rootsys.at>
---
fs/smb/server/smb2pdu.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index 0e0946bd5684..589155469134 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -2415,8 +2415,7 @@ static int ntlm_authenticate(struct ksmbd_work *work,
sess->sign_required = true;
}
- if (smb3_encryption_negotiated(conn) &&
- !(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) {
+ if (smb3_encryption_negotiated(conn) && !conn->binding) {
conn->ops->generate_encryptionkey(conn, sess);
sess->enc = true;
if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION)
@@ -2521,8 +2520,7 @@ static int krb5_authenticate(struct ksmbd_work *work,
sess->sign_required = true;
}
- if (smb3_encryption_negotiated(conn) &&
- !(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) {
+ if (smb3_encryption_negotiated(conn) && !conn->binding) {
conn->ops->generate_encryptionkey(conn, sess);
sess->enc = true;
if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION)
@@ -2654,6 +2652,15 @@ int smb2_sess_setup(struct ksmbd_work *work)
rsp->SecurityBufferLength = 0;
ksmbd_conn_lock(conn);
+ /*
+ * A binding request names the session it binds to. Without one, the
+ * request would set up a new session as if it were binding, skipping
+ * the encryption setup.
+ */
+ if (!req->hdr.SessionId && req->Flags & SMB2_SESSION_REQ_FLAG_BINDING) {
+ rc = -EINVAL;
+ goto out_err;
+ }
if (!req->hdr.SessionId) {
/* Bound abandoned SessionId-zero authentication exchanges. */
if (ksmbd_too_many_session_setups(conn)) {
--
2.43.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v3 4/5] ksmbd: reject plaintext requests on sessions that must be encrypted
2026-12-04 6:53 [PATCH v3 0/5] ksmbd: enforce mandatory signing and encryption on requests RootSys GmbH Research
` (2 preceding siblings ...)
2026-12-04 6:53 ` [PATCH v3 3/5] ksmbd: refuse a binding request without a SessionId RootSys GmbH Research
@ 2026-12-04 6:53 ` RootSys GmbH Research
2026-12-04 6:53 ` [PATCH v3 5/5] ksmbd: refuse session setup without encryption when it is mandatory RootSys GmbH Research
2026-12-04 10:04 ` [PATCH v3 0/5] ksmbd: enforce mandatory signing and encryption on requests mixalhs
5 siblings, 0 replies; 8+ messages in thread
From: RootSys GmbH Research @ 2026-12-04 6:53 UTC (permalink / raw)
To: Namjae Jeon
Cc: Michail Andreas Michalopoulos, Ian Bridges, Sergey Senozhatsky,
Tom Talpey, ChenXiaoSong, linux-cifs, RootSys GmbH Research
From: RootSys GmbH Research <research@rootsys.at>
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.
A refused request is otherwise handled like one on a failed session in
__handle_ksmbd_work(): it is verified first, which consumes its
MessageId, and its credits are charged and new ones granted, so that
refused requests neither pin the command sequence window nor use up the
client's credits.
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.
Fixes: e2f34481b24d ("cifsd: add server-side procedures for SMB3")
Cc: stable@vger.kernel.org # 7.3.x: ac68edfa99c7: ksmbd: consume the sequence number of a request on a failed session
Cc: stable@vger.kernel.org # 7.3.x: 332ac50e7533: ksmbd: account for the credits of a request on a failed session
Cc: stable@vger.kernel.org # 7.3.x
Signed-off-by: RootSys GmbH Research <research@rootsys.at>
---
fs/smb/server/mgmt/user_session.h | 2 ++
fs/smb/server/server.c | 43 ++++++++++++++++++++++++++++++-
fs/smb/server/smb2pdu.c | 14 ++++++++--
3 files changed, 56 insertions(+), 3 deletions(-)
diff --git a/fs/smb/server/mgmt/user_session.h b/fs/smb/server/mgmt/user_session.h
index e494da25da2c..f8e4ee501cb9 100644
--- a/fs/smb/server/mgmt/user_session.h
+++ b/fs/smb/server/mgmt/user_session.h
@@ -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;
diff --git a/fs/smb/server/server.c b/fs/smb/server/server.c
index 74851291b9b2..3f952baea1d4 100644
--- a/fs/smb/server/server.c
+++ b/fs/smb/server/server.c
@@ -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,32 @@ 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.
+ * As for a failed session above, consume the MessageId
+ * and settle the credits. 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 (ksmbd_verify_smb_message(work)) {
+ conn->ops->set_rsp_status(work,
+ STATUS_INVALID_PARAMETER);
+ goto send;
+ }
+ if (command == SMB2_CANCEL_HE)
+ work->send_no_response = 1;
+ else
+ conn->ops->set_rsp_status(work,
+ STATUS_ACCESS_DENIED);
+ if (ksmbd_set_failed_rsp_credits(work))
+ conn->ops->set_rsp_status(work,
+ STATUS_INVALID_PARAMETER);
+ goto send;
+ }
+ if (rc > 0) {
rc = conn->ops->get_ksmbd_tcon(work);
if (rc < 0) {
if (rc == -EINVAL)
diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index 589155469134..02474030f211 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -2418,8 +2418,10 @@ static int ntlm_authenticate(struct ksmbd_work *work,
if (smb3_encryption_negotiated(conn) && !conn->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
@@ -2523,8 +2525,10 @@ static int krb5_authenticate(struct ksmbd_work *work,
if (smb3_encryption_negotiated(conn) && !conn->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;
}
@@ -3059,6 +3063,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
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v3 5/5] ksmbd: refuse session setup without encryption when it is mandatory
2026-12-04 6:53 [PATCH v3 0/5] ksmbd: enforce mandatory signing and encryption on requests RootSys GmbH Research
` (3 preceding siblings ...)
2026-12-04 6:53 ` [PATCH v3 4/5] ksmbd: reject plaintext requests on sessions that must be encrypted RootSys GmbH Research
@ 2026-12-04 6:53 ` RootSys GmbH Research
2026-12-04 10:04 ` [PATCH v3 0/5] ksmbd: enforce mandatory signing and encryption on requests mixalhs
5 siblings, 0 replies; 8+ messages in thread
From: RootSys GmbH Research @ 2026-12-04 6:53 UTC (permalink / raw)
To: Namjae Jeon
Cc: Michail Andreas Michalopoulos, Ian Bridges, Sergey Senozhatsky,
Tom Talpey, ChenXiaoSong, linux-cifs, RootSys GmbH Research
From: RootSys GmbH Research <research@rootsys.at>
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 SMB 2.x, on SMB 3.0 and 3.0.2 when the
client's NEGOTIATE did not offer SMB2_GLOBAL_CAP_ENCRYPTION, and on SMB
3.1.1 when no common cipher was negotiated. smb3_encryption_negotiated()
cannot be used for this: init_smb3_0_server() and init_smb3_02_server()
set SMB2_GLOBAL_CAP_ENCRYPTION in the server's capabilities
unconditionally when encryption is mandatory, so it holds on SMB 3.0
and 3.0.2 whatever the client offered.
A guest session cannot be encrypted either: no session key comes out
of a guest login, and 3.3.5.5.3 derives encryption keys only for a
session that is neither anonymous nor guest. ksmbd still derived them,
from an all-zero session key, and replaced SMB2_SESSION_FLAG_IS_GUEST
with SMB2_SESSION_FLAG_ENCRYPT_DATA in the response. Fail a guest login
with STATUS_ACCESS_DENIED when encryption is mandatory, as Samba does.
Found with AI-assisted code review and testing.
Fixes: e2f34481b24d ("cifsd: add server-side procedures for SMB3")
Cc: stable@vger.kernel.org
Signed-off-by: RootSys GmbH Research <research@rootsys.at>
---
fs/smb/server/smb2pdu.c | 31 +++++++++++++++++++++++++++++++
1 file changed, 31 insertions(+)
diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index 02474030f211..51d7b6357ebc 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -1689,6 +1689,21 @@ bool smb3_encryption_negotiated(struct ksmbd_conn *conn)
conn->cipher_type;
}
+/*
+ * smb3_encryption_negotiated() also holds on SMB 3.0 and 3.0.2 when only the
+ * server set SMB2_GLOBAL_CAP_ENCRYPTION, as it does when encryption is
+ * mandatory. Whether the client can encrypt is in its own capabilities, and
+ * on SMB 3.1.1 in the cipher both sides agreed on.
+ */
+static bool smb3_client_can_encrypt(struct ksmbd_conn *conn)
+{
+ if (conn->dialect == SMB311_PROT_ID)
+ return conn->cipher_type;
+ if (conn->dialect >= SMB30_PROT_ID)
+ return conn->cli_cap & SMB2_GLOBAL_CAP_ENCRYPTION;
+ return false;
+}
+
static __le32 decode_compress_ctxt(struct ksmbd_conn *conn,
struct smb2_compression_capabilities_context *pneg_ctxt,
int ctxt_len)
@@ -2376,6 +2391,12 @@ static int ntlm_authenticate(struct ksmbd_work *work,
}
if (conn->binding == false && user_guest(sess->user)) {
+ /*
+ * A guest session has no session key to derive encryption
+ * keys from (MS-SMB2 3.3.5.5.3), so it cannot be encrypted.
+ */
+ if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION)
+ return -EKEYREJECTED;
rsp->SessionFlags = SMB2_SESSION_FLAG_IS_GUEST_LE;
} else {
struct authenticate_message *authblob;
@@ -2656,6 +2677,16 @@ 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 client that cannot
+ * encrypt gets no session.
+ */
+ if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION &&
+ !smb3_client_can_encrypt(conn)) {
+ rc = -EKEYREJECTED;
+ goto out_err;
+ }
+
/*
* A binding request names the session it binds to. Without one, the
* request would set up a new session as if it were binding, skipping
--
2.43.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v3 0/5] ksmbd: enforce mandatory signing and encryption on requests
2026-12-04 6:53 [PATCH v3 0/5] ksmbd: enforce mandatory signing and encryption on requests RootSys GmbH Research
` (4 preceding siblings ...)
2026-12-04 6:53 ` [PATCH v3 5/5] ksmbd: refuse session setup without encryption when it is mandatory RootSys GmbH Research
@ 2026-12-04 10:04 ` mixalhs
2026-12-04 10:54 ` RootSys GmbH Research
5 siblings, 1 reply; 8+ messages in thread
From: mixalhs @ 2026-12-04 10:04 UTC (permalink / raw)
To: RootSys GmbH Research
Cc: Namjae Jeon, Ian Bridges, Sergey Senozhatsky, Tom Talpey,
ChenXiaoSong, linux-cifs, RootSys GmbH Research
Hi,
> - New patch 3: smb2_sess_setup() treats SessionId 0 as a new session
> and clears conn->binding, but ntlm_authenticate() and
> krb5_authenticate() looked at the request's
> SMB2_SESSION_REQ_FLAG_BINDING bit to skip the encryption setup.
[...]
> On every other path that reaches them, conn->binding and the bit
> agree.
I don't think they always agree. The branch in smb2_sess_setup() that
refuses a binding request is:
} else if ((conn->dialect < SMB30_PROT_ID ||
server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) &&
(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) {
With multichannel enabled, the branch above it already takes an SMB 3.x
binding request, so this one only catches SMB 2.x. On SMB 3.x with
"server multi channel support = no", the default in ksmbd.conf(5), a
request with the binding bit and a non-zero SessionId falls through to
the last branch with conn->binding false. That includes the
AUTHENTICATE leg of an ordinary NTLM login, whose SessionId is the
pending session from the first leg, so nothing has to fail first.
I checked this with "smb3 encryption = mandatory", NTLM over SMB 3.1.1:
- mainline 007722b8211f, multichannel off, binding bit set on the
AUTHENTICATE leg: the response has SessionFlags 0; without the bit
it carries SMB2_SESSION_FLAG_ENCRYPT_DATA
- multichannel on: the same request fails with
STATUS_INVALID_PARAMETER
- 007722b8211f with patch 3 applied by hand (its auth-path hunks
need fuzz without the ksmbd-for-next commits), multichannel off:
the response carries SMB2_SESSION_FLAG_ENCRYPT_DATA
So patch 3 already closes this through the switch to conn->binding.
Without that switch, patch 4 would not set sess->enc_required on such
a session and would still take plaintext requests on it. Could the
commit message and the cover letter describe this case as well, and
could your tests cover it? It needs only the default multichannel
setting, so it is the more likely of the two.
The condition itself looks like it was meant to test
!(server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL). It may be
worth fixing separately, since the error path in smb2_sess_setup() also
goes by the bit: a failed request that carries it without binding is
neither unpublished nor expired, so its pending session stays.
Minor: between patches 4 and 5, a guest login under mandatory
encryption gets sess->enc_required set, with keys derived from a zero
session key, until patch 5 refuses it. It is not a regression, but
moving the guest check before patch 4 would avoid that state.
Thanks,
Michail
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v3 0/5] ksmbd: enforce mandatory signing and encryption on requests
2026-12-04 10:04 ` [PATCH v3 0/5] ksmbd: enforce mandatory signing and encryption on requests mixalhs
@ 2026-12-04 10:54 ` RootSys GmbH Research
0 siblings, 0 replies; 8+ messages in thread
From: RootSys GmbH Research @ 2026-12-04 10:54 UTC (permalink / raw)
To: Michail Andreas Michalopoulos
Cc: RootSys GmbH Research, Namjae Jeon, Ian Bridges,
Sergey Senozhatsky, Tom Talpey, ChenXiaoSong, linux-cifs
From: RootSys GmbH Research <research@rootsys.at>
Hi Michail,
thanks, and you are right about mainline.
> I don't think they always agree. The branch in smb2_sess_setup() that
> refuses a binding request is:
> } else if ((conn->dialect < SMB30_PROT_ID ||
> server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) &&
> (req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) {
> [...] On SMB 3.x with "server multi channel support = no" [...] a
> request with the binding bit and a non-zero SessionId falls through to
> the last branch with conn->binding false.
On 007722b8211f alone, yes. With multichannel off that branch catches
only SMB 2.x, so the SMB 3.x AUTHENTICATE with the bit set reaches
ntlm_authenticate, and patch 3's switch to conn->binding is what makes
it encrypt. We reproduced your two mainline results.
That condition is negated by e1b357794105 ("ksmbd: verify the signature
of session binding requests"):
} else if ((conn->dialect < SMB30_PROT_ID ||
!(server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL)) &&
(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) {
which is the "it may be worth fixing separately" you suspected, already
done. It is in ksmbd-for-next, the base of this series, and is the first
of the eight commits the cover letter lists for applying on mainline.
With it, a binding request on a non-zero SessionId with multichannel off
is refused at that branch with STATUS_REQUEST_NOT_ACCEPTED before the
authentication paths, so the only binding request that reaches them is a
real channel bind, where conn->binding is true and the bit is set. That
is what the cover-letter sentence means; we will add "on this base" so it
is not read as a claim about bare mainline.
Your case on the series base (ksmbd-for-next 43dc36660aff), multichannel
off, mandatory encryption, with and without the series:
leg 2 (AUTHENTICATE, pending non-zero SessionId), binding bit set:
STATUS_REQUEST_NOT_ACCEPTED (0xc00000d0), no session
control, no binding bit:
session with SMB2_SESSION_FLAG_ENCRYPT_DATA; a plaintext
TREE_CONNECT on it is accepted on the base and refused with
STATUS_ACCESS_DENIED by patch 4.
So on this base patch 3 closes the SessionId-0 variant (the -EINVAL in
smb2_sess_setup(), STATUS_INVALID_PARAMETER) and e1b357794105 closes the
non-zero one; the conn->binding switch in the two authenticate functions
is then behaviour-neutral and kept for consistency. We will add this case
to the test matrix next to the SessionId-0 one.
On the error path going by the bit: on this base the last branch is
reached only with the bit clear, so the unregister/expire checks in
smb2_sess_setup() that test the bit run only for a real binding request,
where they are correct. On bare mainline, as you note, a failed
AUTHENTICATE carrying the bit without binding would leave its
IN_PROGRESS session unexpired; e1b357794105 refuses it before that too.
> Minor: between patches 4 and 5, a guest login under mandatory
> encryption gets sess->enc_required set [...] moving the guest check
> before patch 4 would avoid that state.
Agreed. v4 moves the guest refusal ahead of the enc_required patch, so
the intermediate state does not exist at any point in the series.
Thanks,
RootSys GmbH Research
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-12-04 10:54 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 6:53 [PATCH v3 0/5] ksmbd: enforce mandatory signing and encryption on requests RootSys GmbH Research
2026-12-04 6:53 ` [PATCH v3 1/5] ksmbd: keep negotiated capabilities per connection RootSys GmbH Research
2026-12-04 6:53 ` [PATCH v3 2/5] ksmbd: require signing on sessions that negotiated encryption RootSys GmbH Research
2026-12-04 6:53 ` [PATCH v3 3/5] ksmbd: refuse a binding request without a SessionId RootSys GmbH Research
2026-12-04 6:53 ` [PATCH v3 4/5] ksmbd: reject plaintext requests on sessions that must be encrypted RootSys GmbH Research
2026-12-04 6:53 ` [PATCH v3 5/5] ksmbd: refuse session setup without encryption when it is mandatory RootSys GmbH Research
2026-12-04 10:04 ` [PATCH v3 0/5] ksmbd: enforce mandatory signing and encryption on requests mixalhs
2026-12-04 10:54 ` RootSys GmbH Research
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox