[PATCH] smb: client: dump the session keys under session_mutex
From: Piyus Kedia
Date: Sat Dec 05 2026 - 14:53:00 EST
CIFS_DUMP_FULL_KEY and CIFS_DUMP_KEY copy the session key out of
ses->auth_key.response, and the keys derived from it out of the session,
with no lock against session setup. Session setup, which every
reconnect runs under ses->session_mutex, replaces auth_key in steps: it
frees it and leaves it NULL (cifs_setup_session()), points it at a copy
of the target info in the server's NTLMSSP CHALLENGE, whose length the
server chooses (decode_ntlmssp_challenge()), and then at the NTLMv2
response (setup_ntlmv2_rsp()); a setup that fails stays at the step it
reached. For the AES-128 ciphers both ioctls copy 16 bytes whatever
auth_key holds, and for AES-256 cifs_dump_full_key() reads auth_key.len
and, after a copy_to_user() that can sleep, auth_key.response. So a
dump during a reconnect, or after one whose session setup failed, reads
from NULL, past the end of a target info shorter than 16 bytes, or an
old length from a new, shorter buffer:
BUG: KASAN: slab-out-of-bounds in _copy_to_user+0xad/0xd0
Read of size 200 at addr ffff88802476bd80 by task poc/9454
_copy_to_user+0xad/0xd0 lib/usercopy.c:24
cifs_dump_full_key fs/smb/client/ioctl.c:497 [inline]
cifs_ioctl+0x1ec1/0x2660 fs/smb/client/ioctl.c:643
Allocated by task 3131:
__kmalloc_noprof+0x357/0x810 mm/slub.c:5556
set_auth_key_response fs/smb/client/cifsencrypt.c:354 [inline]
setup_ntlmv2_rsp+0x546/0xb60 fs/smb/client/cifsencrypt.c:429
build_ntlmssp_auth_blob+0x2a/0x780 fs/smb/client/sess.c:929
SMB2_sess_auth_rawntlmssp_authenticate+0x293/0x460 fs/smb/client/smb2pdu.c:1969
SMB2_sess_setup+0x1b4/0x470 fs/smb/client/smb2pdu.c:2087
cifs_setup_session+0x49d/0xaf0 fs/smb/client/connect.c:4240
smb2_reconnect.part.0+0x70e/0x18b0 fs/smb/client/smb2pdu.c:411
smb2_reconnect_server+0x71a/0x1100 fs/smb/client/smb2pdu.c:4450
The buggy address is located 0 bytes inside of
allocated 96-byte region [ffff88802476bd80, ffff88802476bde0)
and, after a reconnect whose session setup the server refused:
BUG: KASAN: null-ptr-deref in _copy_to_user+0xad/0xd0
Read of size 16 at addr 0000000000000000 by task poc/9449
Take the keys under session_mutex into a buffer on the stack, refuse the
dump with -EAGAIN when auth_key holds no session key of the length to be
reported, and copy that buffer to user space after dropping the mutex:
copy_to_user() can sleep in a fault on the user buffer, which must not
hold up session setup, nor leave the copy reading an auth_key that
session setup has freed meanwhile. A dump that comes during a session
setup now waits for it.
We found this with a syzkaller-based fuzzing harness driven by an AI tool.
Fixes: 7e7db86c7e10 ("smb3: allow decryption keys to be dumped by admin for debugging")
Fixes: aa22ebc3826b ("smb3.1.1: allow dumping GCM256 keys to improve debugging of encrypted shares")
Closes: https://lore.kernel.org/r/cifs-dump-key-auth-key.20261206@xxxxxxxxxxx/
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Kapil Vaswani <kapil@xxxxxxxxxxxxxxx>
Signed-off-by: Piyus Kedia <piyus@xxxxxxxxxxx>
---
Tested on ksmbd-for-next (d70570cf8003), whose fs/smb/client is the
same as mainline's, with KASAN and lockdep; this patch was the only
change between the two builds. The reproducer is a minimal SMB 3.1.1
server on loopback whose share requires encryption, a mount of it, and
reconnects forced by closing the connection:
before AES-256, a dump held (userfaultfd) after reading auth_key.len
while the reconnect shrinks auth_key: the report above, 4 of
4 boots; a dump whose key copy is held half way while the
reconnect frees auth_key: the rest of the key comes back as
zeros from the freed buffer, 3 of 3 (no KASAN report); a
refused re-setup, then either ioctl: null-ptr-deref, 3 of 3
each; dumps looped across reconnects with 4 bytes of target
info: slab-out-of-bounds reads of the target info in 2 of 3
boots, null-ptr-deref reads in 3 of 3
after the held dumps return the keys as the ioctl first found
them, both ioctls fail with -EAGAIN after the refused
re-setup, the looped dumps all return a key; no KASAN and no
lockdep report in 3 boots, each of which ran every case
both with the connection left alone, or the re-setup accepted,
the dumps return the session's current keys
The patch applies to cifs-next (9b62aa64ed00) and mainline (c3500c5ef648).
fs/smb/client/ioctl.c | 74 +++++++++++++++++++++++++++----------------
1 file changed, 46 insertions(+), 28 deletions(-)
diff --git a/fs/smb/client/ioctl.c b/fs/smb/client/ioctl.c
index bb8808289a46..985696c72df9 100644
--- a/fs/smb/client/ioctl.c
+++ b/fs/smb/client/ioctl.c
@@ -408,10 +408,12 @@ static int cifs_shutdown(struct super_block *sb, unsigned long arg)
static int cifs_dump_full_key(struct cifs_tcon *tcon, struct smb3_full_key_debug_info __user *in)
{
struct smb3_full_key_debug_info out;
+ /* session_key_length is a u8; the server keys are 32 bytes at most */
+ u8 keys[U8_MAX + 2 * SMB3_GCM256_CRYPTKEY_SIZE];
+ unsigned int keys_len;
struct cifs_ses *ses;
int rc = 0;
bool found = false;
- u8 __user *end;
if (!smb3_encryption_required(tcon)) {
rc = -EOPNOTSUPP;
@@ -460,6 +462,15 @@ static int cifs_dump_full_key(struct cifs_tcon *tcon, struct smb3_full_key_debug
}
}
+ /*
+ * Session setup frees and replaces ses->auth_key, and rewrites the keys
+ * derived from it, under session_mutex. One that is under way or has
+ * failed can leave auth_key NULL or holding the target info of the
+ * server's NTLMSSP challenge, which can be shorter than a session key.
+ * Take the keys under the mutex, only if the session key is as long as
+ * we report it, and copy them out after dropping it.
+ */
+ mutex_lock(&ses->session_mutex);
switch (ses->server->cipher_type) {
case SMB2_ENCRYPTION_AES128_CCM:
case SMB2_ENCRYPTION_AES128_GCM:
@@ -473,45 +484,42 @@ static int cifs_dump_full_key(struct cifs_tcon *tcon, struct smb3_full_key_debug
break;
default:
rc = -EOPNOTSUPP;
- goto out;
+ goto out_unlock;
+ }
+
+ if (!ses->auth_key.response ||
+ ses->auth_key.len < out.session_key_length) {
+ rc = -EAGAIN;
+ goto out_unlock;
}
/* check if user buffer is big enough to store all the keys */
- if (out.in_size < sizeof(out) + out.session_key_length + out.server_in_key_length
- + out.server_out_key_length) {
+ keys_len = out.session_key_length + out.server_in_key_length +
+ out.server_out_key_length;
+ if (out.in_size < sizeof(out) + keys_len) {
rc = -ENOBUFS;
- goto out;
+ goto out_unlock;
}
+ memcpy(keys, ses->auth_key.response, out.session_key_length);
+ memcpy(keys + out.session_key_length, ses->smb3encryptionkey,
+ out.server_in_key_length);
+ memcpy(keys + out.session_key_length + out.server_in_key_length,
+ ses->smb3decryptionkey, out.server_out_key_length);
out.session_id = ses->Suid;
out.cipher_type = le16_to_cpu(ses->server->cipher_type);
+ mutex_unlock(&ses->session_mutex);
- /* overwrite user input with our output */
- if (copy_to_user(in, &out, sizeof(out))) {
+ /* overwrite user input with our output, and append the keys to it */
+ if (copy_to_user(in, &out, sizeof(out)) ||
+ copy_to_user(in->data, keys, keys_len))
rc = -EINVAL;
- goto out;
- }
-
- /* append all the keys at the end of the user buffer */
- end = in->data;
- if (copy_to_user(end, ses->auth_key.response, out.session_key_length)) {
- rc = -EINVAL;
- goto out;
- }
- end += out.session_key_length;
-
- if (copy_to_user(end, ses->smb3encryptionkey, out.server_in_key_length)) {
- rc = -EINVAL;
- goto out;
- }
- end += out.server_in_key_length;
-
- if (copy_to_user(end, ses->smb3decryptionkey, out.server_out_key_length)) {
- rc = -EINVAL;
- goto out;
- }
+ goto out;
+out_unlock:
+ mutex_unlock(&ses->session_mutex);
out:
+ memzero_explicit(keys, sizeof(keys));
if (found)
cifs_put_smb_ses(ses);
return rc;
@@ -606,6 +614,15 @@ long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg)
cifs_put_tlink(tlink);
break;
}
+ /* see cifs_dump_full_key() */
+ mutex_lock(&tcon->ses->session_mutex);
+ if (!tcon->ses->auth_key.response ||
+ tcon->ses->auth_key.len < SMB2_NTLMV2_SESSKEY_SIZE) {
+ mutex_unlock(&tcon->ses->session_mutex);
+ rc = -EAGAIN;
+ cifs_put_tlink(tlink);
+ break;
+ }
pkey_inf.cipher_type =
le16_to_cpu(tcon->ses->server->cipher_type);
pkey_inf.Suid = tcon->ses->Suid;
@@ -615,6 +632,7 @@ long cifs_ioctl(struct file *filep, unsigned int command, unsigned long arg)
tcon->ses->smb3decryptionkey, SMB3_SIGN_KEY_SIZE);
memcpy(pkey_inf.smb3encryptionkey,
tcon->ses->smb3encryptionkey, SMB3_SIGN_KEY_SIZE);
+ mutex_unlock(&tcon->ses->session_mutex);
if (copy_to_user((void __user *)arg, &pkey_inf,
sizeof(struct smb3_key_debug_info)))
rc = -EFAULT;
--
2.34.1