[PATCH] smb: client: fix use-after-free of session strings
From: Jaegyun Lee
Date: Fri Dec 04 2026 - 23:01:17 EST
decode_unicode_ssetup() stores the server's OS, LAN manager and
domain strings in the session and decode_ascii_ssetup() the OS and LAN
manager strings, freeing the previous ones first:
kfree(ses->serverOS);
ses->serverOS = cifs_strndup_from_utf16(data, bleft, true,
ses->local_nls);
cifs_debug_data_proc_show() prints these strings with %s under
ses_lock, which the decoders do not take. The decoders run again on
every session re-setup, so a reader of /proc/fs/cifs/DebugData that
runs while a reconnect is between the kfree() and the new assignment
prints freed memory:
Trigger: an SMB1 mount whose server closes the connection, so the client
re-runs the session setup, while another process reads DebugData.
[ 45.808596][ T9532] BUG: KASAN: slab-use-after-free in string+0x4a9/0x4f0
[ 45.808614][ T9532] Read of size 1 at addr ffff888014101c00 by task smb1cli/9532
[ 45.808622][ T9532]
[ 45.808628][ T9532] CPU: 0 UID: 0 PID: 9532 Comm: smb1cli Not tainted 7.4.0-rc5-1492227-gc3500c5ef648 #1 PREEMPT(full)
[ 45.808642][ T9532] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014
[ 45.808649][ T9532] Call Trace:
[ 45.808652][ T9532] <TASK>
[ 45.808656][ T9532] dump_stack_lvl+0x116/0x1b0
[ 45.808673][ T9532] print_report+0xf1/0x5c0
[ 45.808685][ T9532] ? __virt_addr_valid+0x238/0x430
[ 45.808700][ T9532] ? string+0x4a9/0x4f0
[ 45.808711][ T9532] kasan_report+0xca/0x100
[ 45.808722][ T9532] ? string+0x4a9/0x4f0
[ 45.808735][ T9532] string+0x4a9/0x4f0
[ 45.808746][ T9532] ? __pfx_string+0x10/0x10
[ 45.808757][ T9532] ? __pfx_convert_num_spec+0x10/0x10
[ 45.808768][ T9532] ? __pfx_format_decode+0x10/0x10
[ 45.808779][ T9532] ? __pfx_convert_num_spec+0x10/0x10
[ 45.808791][ T9532] vsnprintf+0x654/0x1250
[ 45.808805][ T9532] ? __pfx_vsnprintf+0x10/0x10
[ 45.808818][ T9532] ? __lock_acquire+0x509/0x2110
[ 45.808829][ T9532] seq_printf+0x19b/0x250
[ 45.808841][ T9532] ? __pfx_seq_printf+0x10/0x10
[ 45.808852][ T9532] ? lock_acquire+0x1bf/0x370
[ 45.808862][ T9532] ? do_raw_spin_lock+0x12b/0x270
[ 45.808873][ T9532] ? __pfx_do_raw_spin_lock+0x10/0x10
[ 45.808885][ T9532] cifs_debug_data_proc_show+0xa67/0x2690
[ 45.808900][ T9532] ? __kvmalloc_node_noprof+0x3a7/0x980
[ 45.808917][ T9532] seq_read_iter+0x516/0x12e0
[ 45.808931][ T9532] proc_reg_read_iter+0x20e/0x2e0
[ 45.808942][ T9532] vfs_read+0x865/0xc70
[ 45.808954][ T9532] ? __pfx_vfs_read+0x10/0x10
[ 45.808968][ T9532] ksys_read+0x121/0x240
[ 45.808979][ T9532] ? __pfx_ksys_read+0x10/0x10
[ 45.808991][ T9532] do_syscall_64+0x132/0x7c0
[ 45.809005][ T9532] entry_SYSCALL_64_after_hwframe+0x77/0x7f
[ 45.809016][ T9532] RIP: 0033:0x44decc
[ 45.809025][ T9532] Code: ec 28 48 89 54 24 18 48 89 74 24 10 89 7c 24 08 e8 b9 b1 02 00 48 8b 54 24 18 48 8b 74 24 10 41 89 c0 8b 7c 24 08 31 c0 0f 05 <48> 3d 00 f0 ff ff 77 34 44 89 c7 48 89 44 24 08 e8 ff b1 02 00 48
[ 45.809035][ T9532] RSP: 002b:00007f0575637190 EFLAGS: 00000246 ORIG_RAX: 0000000000000000
[ 45.809046][ T9532] RAX: ffffffffffffffda RBX: 0000000000000003 RCX: 000000000044decc
[ 45.809053][ T9532] RDX: 0000000000020000 RSI: 0000000000526320 RDI: 0000000000000003
[ 45.809060][ T9532] RBP: 0000000000526320 R08: 0000000000000000 R09: 00007fff9d4ed34f
[ 45.809067][ T9532] R10: 0000000000000000 R11: 0000000000000246 R12: 00000000004b302e
[ 45.809073][ T9532] R13: 0000000000000000 R14: 0000000000416d70 R15: 00007f0574e37000
[ 45.809083][ T9532] </TASK>
[ 45.809087][ T9532]
[ 45.809089][ T9532] Allocated by task 9529:
[ 45.809093][ T9532] kasan_save_stack+0x24/0x50
[ 45.809103][ T9532] kasan_save_track+0x14/0x40
[ 45.809111][ T9532] __kasan_kmalloc+0xaa/0xb0
[ 45.809119][ T9532] __kmalloc_noprof+0x357/0x810
[ 45.809132][ T9532] cifs_strndup_from_utf16+0xe2/0x170
[ 45.809141][ T9532] decode_unicode_ssetup.isra.0+0xe3/0x760
[ 45.809157][ T9532] sess_auth_rawntlmssp_authenticate+0x93b/0xd90
[ 45.809171][ T9532] CIFS_SessSetup+0x26d/0x340
[ 45.809184][ T9532] cifs_setup_session+0x64c/0x11f0
[ 45.809193][ T9532] cifs_get_smb_ses+0x1652/0x2130
[ 45.809201][ T9532] cifs_mount_get_session+0xf8/0x490
[ 45.809209][ T9532] dfs_mount_share+0x3a5/0x2ac0
[ 45.809221][ T9532] cifs_mount+0xc1/0xe10
[ 45.809234][ T9532] cifs_smb3_do_mount+0x2a7/0x1bb0
[ 45.809243][ T9532] smb3_get_tree+0x23e/0x490
[ 45.809256][ T9532] vfs_get_tree+0x93/0x340
[ 45.809270][ T9532] fc_mount+0x1a/0x230
[ 45.809279][ T9532] path_mount+0x76e/0x2320
[ 45.809291][ T9532] __x64_sys_mount+0x293/0x310
[ 45.809304][ T9532] do_syscall_64+0x132/0x7c0
[ 45.809316][ T9532] entry_SYSCALL_64_after_hwframe+0x77/0x7f
[ 45.809325][ T9532]
[ 45.809327][ T9532] Freed by task 9529:
[ 45.809332][ T9532] kasan_save_stack+0x24/0x50
[ 45.809340][ T9532] kasan_save_track+0x14/0x40
[ 45.809349][ T9532] kasan_save_free_info+0x3b/0x60
[ 45.809361][ T9532] __kasan_slab_free+0x75/0x90
[ 45.809370][ T9532] kfree+0x2cc/0x6e0
[ 45.809380][ T9532] decode_unicode_ssetup.isra.0+0xa3/0x760
[ 45.809394][ T9532] sess_auth_rawntlmssp_authenticate+0x93b/0xd90
[ 45.809408][ T9532] CIFS_SessSetup+0x26d/0x340
[ 45.809421][ T9532] cifs_setup_session+0x64c/0x11f0
[ 45.809429][ T9532] cifs_reconnect_tcon.part.0+0x7cc/0xd30
[ 45.809442][ T9532] smb_init+0x33/0x130
[ 45.809454][ T9532] CIFS_open+0x7c1/0x1160
[ 45.809467][ T9532] cifs_open_file+0x69a/0x830
[ 45.809485][ T9532] cifs_open+0x2077/0x3a60
[ 45.809497][ T9532] do_dentry_open+0x68e/0x14f0
[ 45.809507][ T9532] vfs_open_consume+0xcc/0x460
[ 45.809520][ T9532] path_openat+0x1c16/0x2240
[ 45.809531][ T9532] do_file_open+0x216/0x470
[ 45.809542][ T9532] do_sys_openat2+0x139/0x1d0
[ 45.809554][ T9532] __x64_sys_openat+0x13f/0x1f0
[ 45.809567][ T9532] do_syscall_64+0x132/0x7c0
[ 45.809579][ T9532] entry_SYSCALL_64_after_hwframe+0x77/0x7f
[ 45.809588][ T9532]
[ 45.809590][ T9532] The buggy address belongs to the object at ffff888014101c00
[ 45.809590][ T9532] which belongs to the cache kmalloc-512 of size 512
[ 45.809599][ T9532] The buggy address is located 0 bytes inside of
[ 45.809599][ T9532] freed 512-byte region [ffff888014101c00, ffff888014101e00)
[ 45.809609][ T9532]
[ 45.809611][ T9532] The buggy address belongs to the physical page:
[ 45.809616][ T9532] page: refcount:0 mapcount:0 mapping:0000000000000000 index:0xffff888014101400 pfn:0x14100
[ 45.809625][ T9532] head: order:2 mapcount:0 entire_mapcount:0 nr_pages_mapped:0 pincount:0
[ 45.809634][ T9532] flags: 0xfff00000000240(workingset|head|node=0|zone=1|lastcpupid=0x7ff)
[ 45.809644][ T9532] page_type: f5(slab)
[ 45.809653][ T9532] raw: 00fff00000000240 ffff88801d041c80 ffffea00009cfe10 ffffea0000a0e310
[ 45.809663][ T9532] raw: ffff888014101400 0000000000100008 00000000f5000000 0000000000000000
[ 45.809673][ T9532] head: 00fff00000000240 ffff88801d041c80 ffffea00009cfe10 ffffea0000a0e310
[ 45.809682][ T9532] head: ffff888014101400 0000000000100008 00000000f5000000 0000000000000000
[ 45.809691][ T9532] head: 00fff00000000002 ffffffffffffff01 00000000ffffffff 00000000ffffffff
[ 45.809701][ T9532] head: ffffffffffffffff 0000000000000000 00000000ffffffff 0000000000000004
[ 45.809706][ T9532] page dumped because: kasan: bad access detected
[ 45.809711][ T9532] page_owner tracks the page as allocated
[ 45.809715][ T9532] page last allocated via order 2, migratetype Unmovable, gfp_mask 0xd2000(__GFP_NOWARN|__GFP_NORETRY|__GFP_COMP|__GFP_NOMEMALLOC), pid 9356, tgid 9356 (v4l_id), ts 37463542652
[ 45.809732][ T9532] post_alloc_hook+0xff/0x130
[ 45.809742][ T9532] get_page_from_freelist+0xdf0/0x2a10
[ 45.809754][ T9532] __alloc_frozen_pages_noprof+0x28b/0x2dd0
[ 45.809765][ T9532] new_slab+0xb9/0x6b0
[ 45.809776][ T9532] refill_objects+0x10e/0x3d0
[ 45.809787][ T9532] __pcs_replace_empty_main+0x35a/0x680
[ 45.809798][ T9532] __kmalloc_flags_noprof+0x6d7/0x8a0
[ 45.809810][ T9532] __kfree_rcu_sheaf+0x4ad/0x8f0
[ 45.809823][ T9532] kvfree_call_rcu+0x1bd/0x4b0
[ 45.809835][ T9532] ma_free_rcu+0x41/0x60
[ 45.809846][ T9532] mas_topiary_replace+0xdfe/0x1d40
[ 45.809855][ T9532] mas_wmb_replace+0xfe/0x4c0
[ 45.809863][ T9532] mas_wr_split+0x129a/0x2d20
[ 45.809871][ T9532] mas_wr_store_entry+0xb1c/0x1d20
[ 45.809880][ T9532] mas_store_prealloc+0x192/0xa80
[ 45.809889][ T9532] vma_complete+0x98d/0x1130
[ 45.809899][ T9532] page last free pid 16 tgid 16 ts 31118004648 stack trace:
[ 45.809906][ T9532] __free_frozen_pages+0x75e/0x1190
[ 45.809915][ T9532] tlb_remove_table_rcu+0x31d/0x560
[ 45.809929][ T9532] rcu_core+0x5f0/0x1260
[ 45.809940][ T9532] handle_softirqs+0x20e/0x9e0
[ 45.809953][ T9532] run_ksoftirqd+0x3a/0x60
[ 45.809965][ T9532] smpboot_thread_fn+0x2bf/0xb40
[ 45.809976][ T9532] kthread+0x390/0x4a0
[ 45.809988][ T9532] ret_from_fork+0xb0c/0xdb0
[ 45.810001][ T9532] ret_from_fork_asm+0x1a/0x30
[ 45.810013][ T9532]
[ 45.810015][ T9532] Memory state around the buggy address:
[ 45.810019][ T9532] ffff888014101b00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
[ 45.810027][ T9532] ffff888014101b80: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
[ 45.810034][ T9532] >ffff888014101c00: fa fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
[ 45.810039][ T9532] ^
[ 45.810044][ T9532] ffff888014101c80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
[ 45.810051][ T9532] ffff888014101d00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
[ 45.810056][ T9532] ==================================================================
Replace each pointer under ses_lock and free the old string afterwards.
The posix open fallback in cifs_open() prints serverNOS the same way;
take ses_lock around it too.
Fixes: 3979877e5606 ("[CIFS] Support for setting up SMB sessions to legacy lanman servers")
Cc: stable@xxxxxxxxxxxxxxx
Assisted-by: ZCode:GLM-5.4
Signed-off-by: Jaegyun Lee <dreams200260@xxxxxxxxx>
---
fs/smb/client/file.c | 12 ++++++---
fs/smb/client/smb1session.c | 53 ++++++++++++++++++++++---------------
2 files changed, 39 insertions(+), 26 deletions(-)
diff --git a/fs/smb/client/file.c b/fs/smb/client/file.c
index d272320..007c129 100644
--- a/fs/smb/client/file.c
+++ b/fs/smb/client/file.c
@@ -8,6 +8,7 @@
* Jeremy Allison (jra@xxxxxxxxx)
*
*/
+#include <linux/cleanup.h>
#include <linux/fs.h>
#include <linux/fs_struct.h>
#include <linux/filelock.h>
@@ -1603,10 +1604,13 @@ int cifs_open(struct inode *inode, struct file *file)
cifs_dbg(FYI, "posix open succeeded\n");
posix_open_ok = true;
} else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
- if (tcon->ses->serverNOS)
- cifs_dbg(VFS, "server %s of type %s returned unexpected error on SMB posix open, disabling posix open support. Check if server update available.\n",
- tcon->ses->ip_addr,
- tcon->ses->serverNOS);
+ scoped_guard(spinlock, &tcon->ses->ses_lock) {
+ if (tcon->ses->serverNOS)
+ cifs_dbg(VFS, "server %s of type %s returned unexpected error on SMB posix open, "
+ "disabling posix open support. Check if server update available.\n",
+ tcon->ses->ip_addr,
+ tcon->ses->serverNOS);
+ }
tcon->broken_posix_open = true;
} else if ((rc != -EIO) && (rc != -EREMOTE) &&
(rc != -EOPNOTSUPP)) /* path not found or net err */
diff --git a/fs/smb/client/smb1session.c b/fs/smb/client/smb1session.c
index 5f469b4..49219c6 100644
--- a/fs/smb/client/smb1session.c
+++ b/fs/smb/client/smb1session.c
@@ -8,6 +8,8 @@
*
*/
+#include <linux/cleanup.h>
+
#include "cifsproto.h"
#include "smb1proto.h"
#include "ntlmssp.h"
@@ -214,6 +216,14 @@ static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses)
*pbcc_area = bcc_ptr;
}
+/* cifs_debug_data_proc_show prints these under ses_lock */
+static void ses_replace_str(struct cifs_ses *ses, char **field, char *val)
+{
+ scoped_guard(spinlock, &ses->ses_lock)
+ swap(*field, val);
+ kfree(val);
+}
+
static void decode_unicode_ssetup(char **pbcc_area, int bleft,
struct cifs_ses *ses)
{
@@ -222,9 +232,9 @@ static void decode_unicode_ssetup(char **pbcc_area, int bleft,
cifs_dbg(FYI, "bleft %d\n", bleft);
- kfree(ses->serverOS);
- ses->serverOS = cifs_strndup_from_utf16(data, bleft, true,
- ses->local_nls);
+ ses_replace_str(ses, &ses->serverOS,
+ cifs_strndup_from_utf16(data, bleft, true,
+ ses->local_nls));
cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS);
len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
data += len;
@@ -232,9 +242,9 @@ static void decode_unicode_ssetup(char **pbcc_area, int bleft,
if (bleft <= 0)
return;
- kfree(ses->serverNOS);
- ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true,
- ses->local_nls);
+ ses_replace_str(ses, &ses->serverNOS,
+ cifs_strndup_from_utf16(data, bleft, true,
+ ses->local_nls));
cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS);
len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2;
data += len;
@@ -242,9 +252,9 @@ static void decode_unicode_ssetup(char **pbcc_area, int bleft,
if (bleft <= 0)
return;
- kfree(ses->serverDomain);
- ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true,
- ses->local_nls);
+ ses_replace_str(ses, &ses->serverDomain,
+ cifs_strndup_from_utf16(data, bleft, true,
+ ses->local_nls));
cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain);
return;
@@ -253,6 +263,7 @@ static void decode_unicode_ssetup(char **pbcc_area, int bleft,
static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
struct cifs_ses *ses)
{
+ char *val;
int len;
char *bcc_ptr = *pbcc_area;
@@ -262,15 +273,14 @@ static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
if (len >= bleft)
return;
- kfree(ses->serverOS);
-
- ses->serverOS = kmalloc(len + 1, GFP_KERNEL);
- if (ses->serverOS) {
- memcpy(ses->serverOS, bcc_ptr, len);
- ses->serverOS[len] = 0;
- if (strncmp(ses->serverOS, "OS/2", 4) == 0)
+ val = kmalloc(len + 1, GFP_KERNEL);
+ if (val) {
+ memcpy(val, bcc_ptr, len);
+ val[len] = 0;
+ if (strncmp(val, "OS/2", 4) == 0)
cifs_dbg(FYI, "OS/2 server\n");
}
+ ses_replace_str(ses, &ses->serverOS, val);
bcc_ptr += len + 1;
bleft -= len + 1;
@@ -279,13 +289,12 @@ static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft,
if (len >= bleft)
return;
- kfree(ses->serverNOS);
-
- ses->serverNOS = kmalloc(len + 1, GFP_KERNEL);
- if (ses->serverNOS) {
- memcpy(ses->serverNOS, bcc_ptr, len);
- ses->serverNOS[len] = 0;
+ val = kmalloc(len + 1, GFP_KERNEL);
+ if (val) {
+ memcpy(val, bcc_ptr, len);
+ val[len] = 0;
}
+ ses_replace_str(ses, &ses->serverNOS, val);
bcc_ptr += len + 1;
bleft -= len + 1;
--
2.34.1