[BUG] smb: client: the key-dump ioctls read ses->auth_key while session setup replaces it
From: Piyus Kedia
Date: Sat Dec 05 2026 - 14:52:48 EST
Hi,
CIFS_DUMP_FULL_KEY copies the session key out of ses->auth_key with no
lock against session setup, and for the AES-128 ciphers always 16 bytes
of it, whatever auth_key holds:
fs/smb/client/ioctl.c:463 (cifs_dump_full_key)
switch (ses->server->cipher_type) {
case SMB2_ENCRYPTION_AES128_CCM:
case SMB2_ENCRYPTION_AES128_GCM:
out.session_key_length = CIFS_SESS_KEY_SIZE;
...
case SMB2_ENCRYPTION_AES256_CCM:
case SMB2_ENCRYPTION_AES256_GCM:
out.session_key_length = ses->auth_key.len;
...
if (copy_to_user(in, &out, sizeof(out))) { /* :490 */
...
end = in->data;
if (copy_to_user(end, ses->auth_key.response, /* :497 */
out.session_key_length)) {
and the older CIFS_DUMP_KEY does the same with memcpy() (ioctl.c:612).
Session setup, which every reconnect runs, replaces auth_key in steps
under ses->session_mutex:
fs/smb/client/connect.c:4229 (cifs_setup_session)
kfree_sensitive(ses->auth_key.response);
ses->auth_key.response = NULL;
fs/smb/client/sess.c:722 (decode_ntlmssp_challenge)
ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen,
GFP_KERNEL);
fs/smb/client/cifsencrypt.c:370 (set_auth_key_response)
ses->auth_key.len = len;
out:
ses->auth_key.response = rsp;
So auth_key is first NULL, then the target info of the server's NTLMSSP
CHALLENGE - tilen bytes, as many as the server sent - then the NTLMv2
response, whose first 16 bytes become the session key; and a setup that
fails stays at the step it reached. A dump during a reconnect, or after
one whose session setup failed, copies 16 bytes from NULL or from a
target info that can be shorter, and with AES-256 the length (line 471)
and the buffer (line 497) can belong to different setups, since the
copy_to_user() between them can sleep in a page fault. The fuzzer hit
the short target info on 7.4.0-rc5:
BUG: KASAN: slab-out-of-bounds in cifs_dump_full_key+0x65f/0x8a0 fs/smb/client/ioctl.c:497
Read of size 16 at addr ffff88804302dfa0 by task syz-executor/9603
copy_to_user include/linux/uaccess.h:241 [inline]
cifs_dump_full_key+0x65f/0x8a0 fs/smb/client/ioctl.c:497
cifs_ioctl+0xb47/0xf10 fs/smb/client/ioctl.c:643
vfs_ioctl+0x5d/0xb0 fs/ioctl.c:52
...
Allocated by task 1299:
kmemdup_noprof+0x3e/0xa0 mm/util.c:138
decode_ntlmssp_challenge+0x49a/0x690 fs/smb/client/sess.c:722
SMB2_sess_auth_rawntlmssp_negotiate+0x454/0x600 fs/smb/client/smb2pdu.c:1918
SMB2_sess_setup+0x200/0x3d0 fs/smb/client/smb2pdu.c:2087
cifs_setup_session+0x6a5/0xaa0 fs/smb/client/connect.c:4240
smb2_reconnect+0x851/0x1380 fs/smb/client/smb2pdu.c:411
smb2_reconnect_server+0x53b/0x9d0 fs/smb/client/smb2pdu.c:4450
The buggy address belongs to the object at ffff88804302dfa0
which belongs to the cache kmalloc-8 of size 8
The buggy address is located 0 bytes inside of
allocated 4-byte region [ffff88804302dfa0, ffff88804302dfa4)
and also the other two, under their own titles: "KASAN: null-ptr-deref
Read in cifs_dump_full_key", a read of size 16 at address 0 at the same
line, auth_key.response being NULL between cifs_setup_session() and the
CHALLENGE; and, with AES-256, a read of size 200 of a 96-byte auth_key
allocated by set_auth_key_response().
How it is reached. The reproducer is a minimal SMB 3.1.1 server on
loopback - NTLMv2 with a password it knows, AES-GCM, one share that
requires encryption (SHI1005_FLAGS_ENCRYPT_DATA, so that
smb3_encryption_required() holds) - and a mount of it; it makes the
client reconnect by closing the server's side of the connection, and
calls the ioctl on a file of the mount at the wrong moment:
1. AES-256-GCM. The mount's CHALLENGE carries 108 bytes of target
info, so auth_key.len is 200. CIFS_DUMP_FULL_KEY reads that
length, then stops in copy_to_user() of its header on a page
write-protected with userfaultfd; the client reconnects, the new
CHALLENGE carries 4 bytes of target info and auth_key becomes a
96-byte buffer; the write protection is lifted and the ioctl
copies 200 bytes from that 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
__x64_sys_ioctl+0x121/0x170 fs/ioctl.c:598
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 belongs to the object at ffff88802476bd80
which belongs to the cache kmalloc-96 of size 96
The buggy address is located 0 bytes inside of
allocated 96-byte region [ffff88802476bd80, ffff88802476bde0)
The ioctl returns 0 with session_key_length 200: bytes 16-95 of
the key are the NTLMv2 response the server received in the
reconnect, bytes 96-199 are what follows the buffer.
2. AES-128-GCM. The server refuses the reconnect's SESSION_SETUP
(STATUS_ACCESS_DENIED), which leaves auth_key.response NULL until
a session setup succeeds, and CIFS_DUMP_FULL_KEY copies 16 bytes
from it:
BUG: KASAN: null-ptr-deref in _copy_to_user+0xad/0xd0
Read of size 16 at addr 0000000000000000 by task poc/9449
_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
after which the copy faults and the ioctl fails with EINVAL.
CIFS_DUMP_KEY in the same state gives "null-ptr-deref in
cifs_ioctl+0x1594/0x2660", from __asan_memcpy() at ioctl.c:612.
3. AES-128-GCM, 4 bytes of target info in every CHALLENGE, the server
dropping the connection a few requests after every session setup,
one thread stat()ing so that the client reconnects and another
looping CIFS_DUMP_FULL_KEY: the 7.4-rc5 report above, read of size
16 of a 4-byte object allocated by decode_ntlmssp_challenge(),
alongside null-ptr-deref reads as in 2. This one is a race.
Impact - who can trigger it:
- Only root issues the ioctls. Both check capable(CAP_SYS_ADMIN)
(ioctl.c:600 and 649), in the initial user namespace, and mounting
cifs needs the same. They are debugging interfaces, for decrypting
captures of an encrypted share, and what they read goes to the
caller's buffer.
- The windows are opened by session setup, which every reconnect
runs, so they are there whatever the server: a dump that lands in
one reads from NULL, from the target info or from a buffer freed
under it, and after a re-setup that failed before the CHALLENGE -
refused, as here, or a sec=krb5 one, since SMB2_auth_kerberos()
frees auth_key on any error (smb2pdu.c:1830; we did not test krb5)
- auth_key.response stays NULL until a setup succeeds. A malicious
server controls how far the reads go: the length of its target
info (MS-NLMP has a server put at least its NetBIOS computer and
domain names there, so a short one is the server's choice; a 16-byte
read runs 16 - tilen bytes past it), whether key exchange is
negotiated (with it auth_key.len ends up 16, without it the length
of the NTLMv2 response, which grows with the target info, so an
AES-256 length of up to 255 - session_key_length is a u8 - can go
stale), and when the client has to set the session up again.
- What the reads do. A stale AES-256 length copies past the end of
the new auth_key into the caller's buffer: 104 bytes past a 96-byte
object here, which in our runs held direct-map addresses, the
address of module_ktype (this config has no KASLR) and pieces of
strings: a sysfs device path, a udev rules file name. An AES-128
dump during the target-info window copies 16 - tilen bytes past
it, 12 in the fuzzer's run. From NULL, CIFS_DUMP_FULL_KEY's
copy_to_user() faults and the ioctl fails with EINVAL;
CIFS_DUMP_KEY's memcpy() reads address 0, which without KASAN
should oops (we have not run such a kernel) - with KASAN the copy
is skipped after the report and the ioctl returned 0 with 16 bytes
of uninitialised stack as the session key.
- There is also a use-after-free. copy_to_user() can sleep in a
fault on the user buffer half way through, and nothing keeps the
buffer it copies from alive meanwhile. With the second part of the
user buffer on a page userfaultfd had not filled, the copy stopped
64 bytes into a 200-byte AES-256 key, the reconnect (with the same
target info, so the same length) freed auth_key.response, and the
copy went on in the freed buffer: 116 of the remaining 136 bytes
came back different from the key, every one of them zero, since
kfree_sensitive() clears what it frees. KASAN checks the source
range before the copy and reported nothing. Whether the freed
buffer can be reallocated before the copy goes on we did not try.
Seen on 7.4.0-rc5 and REPRODUCED ON ksmbd-for-next (d70570cf8003,
2026-12-04), whose fs/smb/client is identical to mainline
(c3500c5ef648, 2026-12-04), with KASAN: the stale AES-256 length gave
the report in 1. in 4 of 4 boots; the refused re-setup gave the
null-ptr-deref read in 3 of 3 boots with each ioctl; the held copy read
the freed buffer in 3 of 3 (from the bytes; no KASAN report); and the
looped dumps gave the 7.4-rc5 report, 5 and 19 times, in 2 of the 3
boots we ran them in - the third, with an earlier version of the
program, set the session up only twice - besides null-ptr-deref reads
in all 3. The code is the same in cifs-next (9b62aa64ed00,
2026-12-03).
CIFS_DUMP_KEY has copied 16 bytes from auth_key.response without a
lock since it was added in 7e7db86c7e10 ("smb3: allow decryption keys
to be dumped by admin for debugging"), v5.4, and CIFS_DUMP_FULL_KEY
since aa22ebc3826b ("smb3.1.1: allow dumping GCM256 keys to improve
debugging of encrypted shares"), v5.13; 1bb56810677f ("cifs: change
format of CIFS_FULL_KEY_DUMP ioctl"), also v5.13, gave it the
copy_to_user() above, and 5be7a0cef322 ("smb: client: Use
FullSessionKey for AES-256 encryption key derivation"), v7.1, made the
AES-256 length auth_key.len, which is where a stale length comes from.
A patch follows in reply to this mail. It takes the keys under
session_mutex, which every caller of cifs_setup_session() holds, into a
buffer on the stack; refuses the dump with EAGAIN when
auth_key.response is NULL - whatever auth_key.len says, since a
channel-binding setup whose kmemdup() of the target info fails leaves
the old length beside a NULL pointer (sess.c:722-726) - or shorter than
the length to be reported; and copies the buffer to user space after
dropping the mutex. A dump that comes during a session setup now waits
for it. On ksmbd-for-next, same config and image, the patch the only
change between the two builds:
without the patch the reports above, and the held copy reading the
freed buffer
with the patch in each of 3 boots, which ran every case once:
the held dumps return the keys as the ioctl first
found them (all 200 bytes the mount's), both
ioctls fail with EAGAIN after the refused
re-setup, and the looped dumps all return a key,
over 529, 582 and 563 session setups; no KASAN
report and no lockdep report
both with the connection left alone, or the re-setup
accepted, the dumps return the session's current
keys; the 16-byte session keys equal the one the
server derived
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