linux-stable/fs/cifs
Zeng Heng ee2536830b cifs: fix use-after-free caused by invalid pointer hostname
[ Upstream commit 153695d36e ]

`hostname` needs to be set as null-pointer after free in
`cifs_put_tcp_session` function, or when `cifsd` thread attempts
to resolve hostname and reconnect the host, the thread would deref
the invalid pointer.

Here is one of practical backtrace examples as reference:

Task 477
---------------------------
 do_mount
  path_mount
   do_new_mount
    vfs_get_tree
     smb3_get_tree
      smb3_get_tree_common
       cifs_smb3_do_mount
        cifs_mount
         mount_put_conns
          cifs_put_tcp_session
          --> kfree(server->hostname)

cifsd
---------------------------
 kthread
  cifs_demultiplex_thread
   cifs_reconnect
    reconn_set_ipaddr_from_hostname
    --> if (!server->hostname)
    --> if (server->hostname[0] == '\0')  // !! UAF fault here

CIFS: VFS: cifs_mount failed w/return code = -112
mount error(112): Host is down
BUG: KASAN: use-after-free in reconn_set_ipaddr_from_hostname+0x2ba/0x310
Read of size 1 at addr ffff888108f35380 by task cifsd/480
CPU: 2 PID: 480 Comm: cifsd Not tainted 6.1.0-rc2-00106-gf705792f89dd-dirty #25
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014
Call Trace:
 <TASK>
 dump_stack_lvl+0x68/0x85
 print_report+0x16c/0x4a3
 kasan_report+0x95/0x190
 reconn_set_ipaddr_from_hostname+0x2ba/0x310
 __cifs_reconnect.part.0+0x241/0x800
 cifs_reconnect+0x65f/0xb60
 cifs_demultiplex_thread+0x1570/0x2570
 kthread+0x2c5/0x380
 ret_from_fork+0x22/0x30
 </TASK>
Allocated by task 477:
 kasan_save_stack+0x1e/0x40
 kasan_set_track+0x21/0x30
 __kasan_kmalloc+0x7e/0x90
 __kmalloc_node_track_caller+0x52/0x1b0
 kstrdup+0x3b/0x70
 cifs_get_tcp_session+0xbc/0x19b0
 mount_get_conns+0xa9/0x10c0
 cifs_mount+0xdf/0x1970
 cifs_smb3_do_mount+0x295/0x1660
 smb3_get_tree+0x352/0x5e0
 vfs_get_tree+0x8e/0x2e0
 path_mount+0xf8c/0x1990
 do_mount+0xee/0x110
 __x64_sys_mount+0x14b/0x1f0
 do_syscall_64+0x3b/0x90
 entry_SYSCALL_64_after_hwframe+0x63/0xcd
Freed by task 477:
 kasan_save_stack+0x1e/0x40
 kasan_set_track+0x21/0x30
 kasan_save_free_info+0x2a/0x50
 __kasan_slab_free+0x10a/0x190
 __kmem_cache_free+0xca/0x3f0
 cifs_put_tcp_session+0x30c/0x450
 cifs_mount+0xf95/0x1970
 cifs_smb3_do_mount+0x295/0x1660
 smb3_get_tree+0x352/0x5e0
 vfs_get_tree+0x8e/0x2e0
 path_mount+0xf8c/0x1990
 do_mount+0xee/0x110
 __x64_sys_mount+0x14b/0x1f0
 do_syscall_64+0x3b/0x90
 entry_SYSCALL_64_after_hwframe+0x63/0xcd
The buggy address belongs to the object at ffff888108f35380
 which belongs to the cache kmalloc-16 of size 16
The buggy address is located 0 bytes inside of
 16-byte region [ffff888108f35380, ffff888108f35390)
The buggy address belongs to the physical page:
page:00000000333f8e58 refcount:1 mapcount:0 mapping:0000000000000000 index:0xffff888108f350e0 pfn:0x108f35
flags: 0x200000000000200(slab|node=0|zone=2)
raw: 0200000000000200 0000000000000000 dead000000000122 ffff8881000423c0
raw: ffff888108f350e0 000000008080007a 00000001ffffffff 0000000000000000
page dumped because: kasan: bad access detected
Memory state around the buggy address:
 ffff888108f35280: fa fb fc fc fa fb fc fc fa fb fc fc fa fb fc fc
 ffff888108f35300: fa fb fc fc fa fb fc fc fa fb fc fc fa fb fc fc
>ffff888108f35380: fa fb fc fc fa fb fc fc fa fb fc fc fa fb fc fc
                   ^
 ffff888108f35400: fa fb fc fc fc fc fc fc fc fc fc fc fc fc fc fc
 ffff888108f35480: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc

Fixes: 7be3248f31 ("cifs: To match file servers, make sure the server hostname matches")
Signed-off-by: Zeng Heng <zengheng4@huawei.com>
Reviewed-by: Paulo Alcantara (SUSE) <pc@cjr.nz>
Signed-off-by: Steve French <stfrench@microsoft.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2022-12-14 11:37:18 +01:00
..
asn1.c
cache.c cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
cifs_debug.c cifs: protect session channel fields with chan_lock 2022-03-08 19:12:41 +01:00
cifs_debug.h
cifs_dfs_ref.c cifs: support nested dfs links over reconnect 2022-12-02 17:40:59 +01:00
cifs_fs_sb.h cifs: support nested dfs links over reconnect 2022-12-02 17:40:59 +01:00
cifs_ioctl.h cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
cifs_spnego.c cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
cifs_spnego.h cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
cifs_spnego_negtokeninit.asn1
cifs_swn.c cifs: remove support for NTLM and weaker authentication algorithms 2021-08-25 15:47:06 -05:00
cifs_swn.h
cifs_unicode.c cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
cifs_unicode.h
cifs_uniupr.h
cifsacl.c cifs: modefromsids must add an ACE for authenticated users 2022-03-08 19:12:30 +01:00
cifsacl.h cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
cifsencrypt.c cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
cifsfs.c cifs: Fix xid leak in cifs_copy_file_range() 2022-10-29 10:12:56 +02:00
cifsfs.h cifs: fix reconnect on smb3 mount types 2022-06-14 18:36:25 +02:00
cifsglob.h cifs: support nested dfs links over reconnect 2022-12-02 17:40:59 +01:00
cifspdu.h cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
cifsproto.h cifs: support nested dfs links over reconnect 2022-12-02 17:40:59 +01:00
cifsroot.c
cifssmb.c cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
connect.c cifs: fix use-after-free caused by invalid pointer hostname 2022-12-14 11:37:18 +01:00
dfs_cache.c cifs: support nested dfs links over reconnect 2022-12-02 17:40:59 +01:00
dfs_cache.h cifs: support share failover when remounting 2021-07-22 11:43:23 -05:00
dir.c cifs: Fix xid leak in cifs_create() 2022-10-29 10:12:56 +02:00
dns_resolve.c cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
dns_resolve.h cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
export.c cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
file.c cifs: Fix xid leak in cifs_flock() 2022-10-29 10:12:56 +02:00
fs_context.c smb3: fix snapshot mount option 2022-02-23 12:03:14 +01:00
fs_context.h cifs: To match file servers, make sure the server hostname matches 2021-11-18 19:16:06 +01:00
fscache.c cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
fscache.h cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
inode.c cifs: Deferred close performance improvements 2021-09-17 16:39:53 -05:00
ioctl.c cifs: Fix wrong return value checking when GETFLAGS 2022-11-26 09:24:41 +01:00
Kconfig cifs: create a MD4 module and switch cifs.ko to use it 2021-08-25 15:48:00 -05:00
link.c cifs: potential buffer overflow in handling symlinks 2022-04-20 09:34:13 +02:00
Makefile
misc.c cifs: support nested dfs links over reconnect 2022-12-02 17:40:59 +01:00
netlink.c
netlink.h
netmisc.c cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
nterr.c
nterr.h
ntlmssp.h cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
readdir.c cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
rfc1002pdu.h cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
sess.c cifs: Fix xid leak in cifs_ses_add_channel() 2022-10-29 10:12:56 +02:00
smb1ops.c
smb2file.c cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
smb2glob.h cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
smb2inode.c SMB3: EBADF/EIO errors in rename/open caused by race condition in smb2_compound_op 2022-06-09 10:23:30 +02:00
smb2maperror.c cifs: cifs_md4 convert to SPDX identifier 2021-08-25 15:51:52 -05:00
smb2misc.c cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
smb2ops.c cifs: support nested dfs links over reconnect 2022-12-02 17:40:59 +01:00
smb2pdu.c cifs: support nested dfs links over reconnect 2022-12-02 17:40:59 +01:00
smb2pdu.h cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
smb2proto.h cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
smb2status.h cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
smb2transport.c cifs: return correct error in ->calc_signature() 2022-10-26 12:35:26 +02:00
smbdirect.c
smbdirect.h
smbencrypt.c cifs: rename cifs_common to smbfs_common 2021-09-08 23:59:26 -05:00
smberr.h cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
trace.c
trace.h
transport.c cifs: always initialize struct msghdr smb_msg completely 2022-09-23 14:15:48 +02:00
unc.c
winucase.c cifs: remove pathname for file from SPDX header 2021-09-13 14:51:10 -05:00
xattr.c cifs: fix set of group SID via NTSD xattrs 2022-02-23 12:03:14 +01:00