Linux CIFS filesystem development
 help / color / mirror / Atom feed
* [BUG] smb: client: NULL tfm in crypt_message() when the server requires encryption without a cipher
@ 2026-12-05  4:31 Piyus Kedia
  2026-12-05  4:31 ` [PATCH] smb: client: allocate the encryption AEAD before encrypting a request Piyus Kedia
  0 siblings, 1 reply; 2+ messages in thread
From: Piyus Kedia @ 2026-12-05  4:31 UTC (permalink / raw)
  To: Paulo Alcantara, Namjae Jeon, linux-cifs
  Cc: Ronnie Sahlberg, Shyam Prasad N, Tom Talpey, Bharath SM,
	samba-technical, linux-kernel, Kapil Vaswani

Hi,

The client encrypts a request with server->secmech.enc whenever
smb3_encryption_required() says the session or share needs it, but
that AEAD is only allocated at negotiate time, and only when the server
named a cipher:

  fs/smb/client/smb2pdu.c:1391 (SMB2_negotiate)
      if (server->cipher_type && !rc)
          rc = smb3_crypto_aead_allocate(server);

smb3_encryption_required() (smb2pdu.c:77) decides from values the
server sends: SMB2_SESSION_FLAG_ENCRYPT_DATA in the session flags,
SHI1005_FLAGS_ENCRYPT_DATA in the share flags, and, for a "seal" mount,
SMB2_GLOBAL_CAP_ENCRYPTION in the server capabilities.  For SMB 3.1.1
a cipher is negotiated only through an encryption context, which a
server can leave out of its NEGOTIATE response.  The next request is
then encrypted with a NULL tfm:

  fs/smb/client/smb2ops.c:5003 (smb3_init_transform_rq)
      rc = crypt_message(server, num_rqst, new_rq, 1, server->secmech.enc);

  BUG: KASAN: null-ptr-deref in crypto_aead_setkey+0x2c/0x170
  Read of size 8 at addr 0000000000000020 by task poc/9466
   crypto_aead_setkey (include/linux/crypto.h:458 crypto/aead.c:47)
   crypt_message (fs/smb/client/smb2ops.c:4876)
   smb3_init_transform_rq (fs/smb/client/smb2ops.c:5003)
   smb_send_rqst (fs/smb/client/transport.c:419)
   compound_send_recv (fs/smb/client/transport.c:1014)
   cifs_send_recv (fs/smb/client/transport.c:1197)
   SMB2_tcon (fs/smb/client/smb2pdu.c:2252)
   cifs_setup_ipc (fs/smb/client/connect.c:2099)
   cifs_get_smb_ses (fs/smb/client/connect.c:2565)
   cifs_mount_get_session (fs/smb/client/connect.c:3660)
   dfs_mount_share (fs/smb/client/dfs.c:352)
   cifs_mount (fs/smb/client/connect.c:3942)
   cifs_smb3_do_mount (fs/smb/client/cifsfs.c:1132)
   smb3_get_tree (fs/smb/client/fs_context.c:917)
   vfs_get_tree (fs/super.c:1881)
   ...
   __x64_sys_mount (fs/namespace.c:4540)
  BUG: kernel NULL pointer dereference, address: 0000000000000020

How it is reached.  The reproducer is a minimal SMB 3.1.1 server on
loopback and a mount of it in the same program.  Its NEGOTIATE response
carries a preauth integrity context and no encryption context, so no
cipher is negotiated, and any one of these then crashes the mount:

  1. SHI1005_FLAGS_ENCRYPT_DATA in the TREE_CONNECT response's
     ShareFlags (SMB2_tcon() stores them unchecked, smb2pdu.c:2281);
  2. SMB2_SESSION_FLAG_ENCRYPT_DATA in the final SESSION_SETUP
     response; the crash is then in the IPC tree connect,
     cifs_setup_ipc() -> SMB2_tcon(), as in the trace above;
  3. SMB2_GLOBAL_CAP_ENCRYPTION in the NEGOTIATE Capabilities and the
     "seal" mount option.  The check that refuses a seal mount on a
     server without encryption reads that capability bit, not whether a
     cipher was negotiated; without the bit the same mount fails
     cleanly with -EOPNOTSUPP.

All three crashed the client on 7.4.0-rc5, where we found them, and
each crashed it again on upstream (below).

The receive side does not have the problem: decrypt_raw_data()
allocates the AEAD itself when it is missing.  Until b0abcd65ec54
("smb: client: fix UAF in async decryption"), in v6.12, crypt_message()
called smb3_crypto_aead_allocate() for both directions; that commit
moved the call into decrypt_raw_data() and left the send side relying
on the allocation in SMB2_negotiate().

Impact - who can trigger it:

  - The SMB server the client connects to.  All three triggers are
    values in the server's own NEGOTIATE, SESSION_SETUP and
    TREE_CONNECT responses, so a malicious or compromised server can
    crash any client that mounts it, including one a DFS referral
    sends the client to.  Our server accepted the client's NTLMSSP
    authentication without checking it, and the mount options were
    ordinary ones.

  - A local unprivileged user cannot trigger it directly: mount(2) of
    cifs needs CAP_SYS_ADMIN in the initial user namespace (the
    filesystem is not FS_USERNS_MOUNT).  Where an administrator allows
    user mounts through mount.cifs, the server is the one the
    administrator configured; we have not tested that.

  - What happens: a read through a NULL pointer (address 0x20) in the
    task that sends the request - the mounting task in our runs.  The
    kernel oopses and kills that task; on a system with panic_on_oops
    set it panics.  It is a denial of service; nothing is written and
    nothing is read back to the server.

Seen on 7.4.0-rc5 and REPRODUCED ON UPSTREAM: on ksmbd-for-next
(d70570cf8003, 2026-12-04), whose fs/smb/client is identical to
mainline (c3500c5ef648, 2026-12-04), with KASAN, each of the three
triggers gave the report above in its run (the trace is trigger 2's).
The code is the same in cifs-next (9b62aa64ed00, 2026-12-04), which
changes fill_transform_hdr() beside the call but not the tfm passed to
crypt_message().

A patch follows in reply to this mail.  It allocates the AEAD in
smb3_init_transform_rq() when it is missing, as decrypt_raw_data()
does on the receive side.  With it, same config and image and no
other change to fs/smb/client, none of the three crashes: the client
sends the request encrypted (with AES-128-CCM, since no cipher was
negotiated, as it did before v6.12), our minimal server, which cannot
decrypt it, closes the connection, and the mount fails with -EHOSTDOWN
(trigger 1) or -EAGAIN (2 and 3).  No KASAN report.

Whether the client should refuse such a server outright - for example
not accept SMB2_GLOBAL_CAP_ENCRYPTION from a 3.1.1 NEGOTIATE response
that has no encryption context - is a separate question that we leave
to you; the patch only restores the behaviour from before v6.12.

This was found by AI-driven fuzzing built on top of syzkaller; its
analysis was then checked by hand against the code and every claim
above was reproduced.  The reproducer is fuzzer-generated, runs a
minimal SMB server and the mount in one program over loopback, and we
are happy to send it on request.

Kapil Vaswani
Piyus Kedia

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH] smb: client: allocate the encryption AEAD before encrypting a request
  2026-12-05  4:31 [BUG] smb: client: NULL tfm in crypt_message() when the server requires encryption without a cipher Piyus Kedia
@ 2026-12-05  4:31 ` Piyus Kedia
  0 siblings, 0 replies; 2+ messages in thread
From: Piyus Kedia @ 2026-12-05  4:31 UTC (permalink / raw)
  To: Paulo Alcantara, Namjae Jeon, linux-cifs
  Cc: Ronnie Sahlberg, Shyam Prasad N, Tom Talpey, Bharath SM,
	samba-technical, linux-kernel, Kapil Vaswani

crypt_message() is handed server->secmech.enc for every request that is
encrypted, and that AEAD is only allocated at negotiate time, by
SMB2_negotiate(), when the server named a cipher:

	if (server->cipher_type && !rc)
		rc = smb3_crypto_aead_allocate(server);

Whether a request is encrypted is decided separately, by
smb3_encryption_required(), from the session's SessionFlags, the share's
ShareFlags and the server's SMB2_GLOBAL_CAP_ENCRYPTION, all of which the
server sets.  A server that negotiates SMB 3.1.1 without an encryption
context, so that no cipher is negotiated, and then sets
SMB2_SESSION_FLAG_ENCRYPT_DATA in the final SESSION_SETUP response or
SHI1005_FLAGS_ENCRYPT_DATA in a TREE_CONNECT response, or sets
SMB2_GLOBAL_CAP_ENCRYPTION in the NEGOTIATE response for a "seal" mount,
makes the client encrypt the next request with a NULL tfm:

  BUG: KASAN: null-ptr-deref in crypto_aead_setkey+0x2c/0x170
  Read of size 8 at addr 0000000000000020 by task poc/9466
   crypto_aead_setkey+0x2c/0x170 crypto/aead.c:47
   crypt_message+0x27b/0x1300 fs/smb/client/smb2ops.c:4876
   smb3_init_transform_rq+0x4bf/0x610 fs/smb/client/smb2ops.c:5003
   smb_send_rqst+0x187/0x300 fs/smb/client/transport.c:419
   compound_send_recv+0x89a/0x1fc0 fs/smb/client/transport.c:1014
   cifs_send_recv+0x43/0x60 fs/smb/client/transport.c:1197
   SMB2_tcon+0x42f/0x1080 fs/smb/client/smb2pdu.c:2252
   cifs_setup_ipc+0x276/0x600 fs/smb/client/connect.c:2099
   cifs_get_smb_ses+0x1070/0x14e0 fs/smb/client/connect.c:2565

Until commit b0abcd65ec54 ("smb: client: fix UAF in async decryption")
crypt_message() called smb3_crypto_aead_allocate() itself, for both
directions.  That commit moved the call into decrypt_raw_data(), for the
receive side, and left the send side to rely on the allocation at
negotiate time.  Allocate in smb3_init_transform_rq() too, as the
receive side does.

We found this with a syzkaller-based fuzzing harness driven by an AI tool.

Fixes: b0abcd65ec54 ("smb: client: fix UAF in async decryption")
Closes: https://lore.kernel.org/linux-cifs/cifs-encrypt-without-cipher.20261205@iiitd.ac.in/
Cc: stable@vger.kernel.org
Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
---
Tested on ksmbd-for-next (d70570cf8003), whose fs/smb/client is the
same as mainline's, with KASAN; this patch was the only change to
fs/smb/client between the two builds.  The reproducer is a minimal SMB
3.1.1 server on loopback, with no encryption context in its NEGOTIATE
response, and a mount of it:

  before   KASAN null-ptr-deref in crypto_aead_setkey() from
           crypt_message(), for each of: SHI1005_FLAGS_ENCRYPT_DATA on
           the share, SMB2_SESSION_FLAG_ENCRYPT_DATA on the session,
           and SMB2_GLOBAL_CAP_ENCRYPTION with a "seal" mount
  after    the request goes out encrypted, the server (which cannot
           decrypt it) closes the connection, and the mount fails with
           -EHOSTDOWN or -EAGAIN; no report

The patch is against cifs-next (9b62aa64ed00) and applies to mainline.

 fs/smb/client/smb2ops.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/fs/smb/client/smb2ops.c b/fs/smb/client/smb2ops.c
index 8783738a31ad..2ea3a49fc4d5 100644
--- a/fs/smb/client/smb2ops.c
+++ b/fs/smb/client/smb2ops.c
@@ -5032,7 +5032,18 @@ smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
 {
 	struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
 	unsigned int orig_len = 0;
-	int rc = -ENOMEM;
+	int rc;
+
+	/*
+	 * secmech.enc is only allocated at negotiate time when the server named
+	 * a cipher, but a server can require encryption without one: with the
+	 * session or share ENCRYPT_DATA flag, or SMB2_GLOBAL_CAP_ENCRYPTION and
+	 * no encryption context.  Allocate it here as decrypt_raw_data() does
+	 * for the receive side, rather than pass crypt_message() a NULL tfm.
+	 */
+	rc = smb3_crypto_aead_allocate(server);
+	if (unlikely(rc))
+		return rc;
 
 	for (int i = 1; i < num_rqst; i++) {
 		struct smb_rqst *old = &old_rq[i - 1];
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-05  4:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05  4:31 [BUG] smb: client: NULL tfm in crypt_message() when the server requires encryption without a cipher Piyus Kedia
2026-12-05  4:31 ` [PATCH] smb: client: allocate the encryption AEAD before encrypting a request Piyus Kedia

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox