Skip to content

ksmbd: enforce mandatory signing and encryption on requests

RootSys GmbH Research linux-cifs 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest4 patches 2 repliesIn review
  2. v14 patches 3 repliesSuperseded

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4ksmbd: keep negotiated capabilities per connectionfs/smb/server/connection.h fs/smb/server/smb2ops.c+17 -40 replies
2/4ksmbd: require signing on sessions that negotiated encryptionfs/smb/server/server.c+6 -20 replies
3/4ksmbd: reject plaintext requests on sessions that must be encryptedfs/smb/server/mgmt/user_session.h fs/smb/server/server.c fs/smb/server/smb2pdu.c+46 -31 reply
4/4ksmbd: refuse session setup without encryption when it is mandatoryfs/smb/server/smb2pdu.c+11 -01 reply

Reviews trailers per patch

None of the 4 patches has a Reviewed-by or Acked-by.

PatchMichail Andreas Michalopoulos
1/4T-b
2/4T-b
3/4
4/4

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [PATCH v2 0/4] ksmbd: enforce mandatory signing and encryption on requests

From: 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