KASAN slab-out-of-bounds in tun_chr_open/sock_init_data (Was: Re: [PATCH v14 00/23] LSM: Module stacking for AppArmor)

Stephen Smalley sds at tycho.nsa.gov
Mon Jan 27 16:56:58 UTC 2020


On 1/27/20 11:14 AM, Stephen Smalley wrote:
> On 1/24/20 4:49 PM, Casey Schaufler wrote:
>> On 1/24/2020 1:04 PM, Stephen Smalley wrote:
>>> On 1/23/20 7:22 PM, Casey Schaufler wrote:
>>>> This patchset provides the changes required for
>>>> the AppArmor security module to stack safely with any other.
>>>>
>>>> v14: Rebase to 5.5-rc5
>>>>        Incorporate feedback from v13
>>>>        - Use an array of audit rules (patch 0002)
>>>>        - Significant change, removed Acks (patch 0002)
>>>>        - Remove unneeded include (patch 0013)
>>>>        - Use context.len correctly (patch 0015)
>>>>        - Reorder code to be more sensible (patch 0016)
>>>>        - Drop SO_PEERCONTEXT as it's not needed yet (patch 0023)
>>>
>>> I don't know for sure if this is your bug, but it happens every time 
>>> I boot with your patches applied and not at all on stock v5.5-rc5 so 
>>> here it is.  Will try to bisect as time permits but not until next 
>>> week. Trigger seems to be loading the tun driver.
>>
>> Thanks. I will have a look as well.
> 
> Bisection led to the first patch in the series, "LSM: Infrastructure 
> management of the sock security". Still not sure if the bug is in the 
> patch itself or just being surfaced by it.

Looks like the bug is pre-existing to me and just exposed by your patch. 
tun_chr_open() is creating a struct tun_file via sk_alloc() with its own 
tun_proto with a custom .obj_size.  It then passes the tun_file->socket 
and ->sk fields to sock_init_data().  sock_init_data() assumes it can 
safely use SOCK_INODE(sock) if sock is non-NULL, which means that it 
presumes all such sockets were wrapped in a struct socket_alloc.  But 
this one wasn't.  I don't know if that's a bug in the tun driver for not 
wrapping its socket in a socket_alloc or in sock_init_data() for 
assuming that all sockets it is passed have been so wrapped.  KASAN is 
tripping on this assignment in sock_init_data():

net/core/sock.c:
    2871                 sk->sk_uid      =       SOCK_INODE(sock)->i_uid;

This appears to have been broken since commit 
86741ec25462e4c8cdce6df2f41ead05568c7d5e ("net: core: Add a UID field to 
struct sock.").

Previously reported here by someone else with RFC patches:
https://lore.kernel.org/netdev/20190929110502.2284-1-amade@asmblr.net/

> 
>>>
>>> [   67.726834] tun: Universal TUN/TAP device driver, 1.6
>>> [   67.736657] 
>>> ==================================================================
>>> [   67.741335] BUG: KASAN: slab-out-of-bounds in 
>>> sock_init_data+0x14a/0x5a0
>>> [   67.745037] Read of size 4 at addr ffff88870afe8928 by task 
>>> libvirtd/1238
>>>
>>> [   67.751861] CPU: 4 PID: 1238 Comm: libvirtd Tainted: G T 
>>> 5.5.0-rc5+ #54
>>> [   67.756250] Call Trace:
>>> [   67.759510]  dump_stack+0xb8/0x110
>>> [   67.761604]  print_address_description.constprop.0+0x1f/0x280
>>> [   67.763768]  __kasan_report.cold+0x75/0x8f
>>> [   67.765895]  ? sock_init_data+0x14a/0x5a0
>>> [   67.768282]  kasan_report+0xe/0x20
>>> [   67.770397]  sock_init_data+0x14a/0x5a0
>>> [   67.772511]  tun_chr_open+0x1de/0x280 [tun]
>>> [   67.774644]  misc_open+0x1cb/0x210
>>> [   67.776820]  chrdev_open+0x15b/0x350
>>> [   67.778917]  ? cdev_put.part.0+0x30/0x30
>>> [   67.781030]  do_dentry_open+0x2cb/0x800
>>> [   67.783135]  ? cdev_put.part.0+0x30/0x30
>>> [   67.785225]  ? devcgroup_check_permission+0x11a/0x260
>>> [   67.787321]  ? __x64_sys_fchdir+0xf0/0xf0
>>> [   67.789418]  ? security_inode_permission+0x5b/0x70
>>> [   67.791513]  path_openat+0x858/0x14a0
>>> [   67.793589]  ? path_mountpoint+0x5e0/0x5e0
>>> [   67.795719]  ? mark_lock+0xb8/0xb00
>>> [   67.797786]  do_filp_open+0x11e/0x1b0
>>> [   67.799840]  ? may_open_dev+0x60/0x60
>>> [   67.801871]  ? match_held_lock+0x1b/0x240
>>> [   67.803968]  ? lock_downgrade+0x360/0x360
>>> [   67.805997]  ? do_raw_spin_lock+0x119/0x1d0
>>> [   67.808041]  ? rwlock_bug.part.0+0x60/0x60
>>> [   67.810099]  ? do_raw_spin_unlock+0xa3/0x130
>>> [   67.812244]  ? _raw_spin_unlock+0x1f/0x30
>>> [   67.814287]  ? __alloc_fd+0x143/0x2f0
>>> [   67.816324]  do_sys_open+0x1f0/0x2d0
>>> [   67.818358]  ? filp_open+0x50/0x50
>>> [   67.820404]  ? trace_hardirqs_on_thunk+0x1a/0x1c
>>> [   67.822447]  ? lockdep_hardirqs_off+0xbe/0x100
>>> [   67.824473]  ? mark_held_locks+0x24/0x90
>>> [   67.826484]  do_syscall_64+0x74/0xd0
>>> [   67.828480]  entry_SYSCALL_64_after_hwframe+0x49/0xbe
>>> [   67.830478] RIP: 0033:0x7f1a2cce6074
>>> [   67.832495] Code: 24 20 eb 8f 66 90 44 89 54 24 0c e8 86 f4 ff ff 
>>> 44 8b 54 24 0c 44 89 e2 48 89 ee 41 89 c0 bf 9c ff ff ff b8 01 01 00 
>>> 00 0f 05 <48> 3d 00 f0 ff ff 77 32 44 89 c7 89 44 24 0c e8 b8 f4 ff 
>>> ff 8b 44
>>> [   67.834760] RSP: 002b:00007f19e4af46d0 EFLAGS: 00000293 ORIG_RAX: 
>>> 0000000000000101
>>> [   67.837032] RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 
>>> 00007f1a2cce6074
>>> [   67.839318] RDX: 0000000000000002 RSI: 00007f1a2d0bfb67 RDI: 
>>> 00000000ffffff9c
>>> [   67.841598] RBP: 00007f1a2d0bfb67 R08: 0000000000000000 R09: 
>>> 00007f19e4af4914
>>> [   67.843941] R10: 0000000000000000 R11: 0000000000000293 R12: 
>>> 0000000000000002
>>> [   67.846283] R13: 000000000000000d R14: 00007f19e4af4920 R15: 
>>> 00007f1a2d0bfb67
>>>
>>> [   67.850936] Allocated by task 1238:
>>> [   67.853241]  save_stack+0x1b/0x80
>>> [   67.855533]  __kasan_kmalloc.constprop.0+0xc2/0xd0
>>> [   67.857935]  sk_prot_alloc+0x115/0x170
>>> [   67.860235]  sk_alloc+0x2f/0xa10
>>> [   67.862541]  tun_chr_open+0x4d/0x280 [tun]
>>> [   67.864894]  misc_open+0x1cb/0x210
>>> [   67.867164]  chrdev_open+0x15b/0x350
>>> [   67.869448]  do_dentry_open+0x2cb/0x800
>>> [   67.871768]  path_openat+0x858/0x14a0
>>> [   67.874041]  do_filp_open+0x11e/0x1b0
>>> [   67.876328]  do_sys_open+0x1f0/0x2d0
>>> [   67.878592]  do_syscall_64+0x74/0xd0
>>> [   67.880899]  entry_SYSCALL_64_after_hwframe+0x49/0xbe
>>>
>>> [   67.885431] Freed by task 726:
>>> [   67.887689]  save_stack+0x1b/0x80
>>> [   67.889967]  __kasan_slab_free+0x12c/0x170
>>> [   67.892197]  kfree+0xff/0x430
>>> [   67.894444]  uevent_show+0x176/0x1b0
>>> [   67.896709]  dev_attr_show+0x37/0x70
>>> [   67.898940]  sysfs_kf_seq_show+0x119/0x210
>>> [   67.901159]  seq_read+0x29d/0x720
>>> [   67.903367]  vfs_read+0xf9/0x1f0
>>> [   67.905538]  ksys_read+0xc9/0x160
>>> [   67.907736]  do_syscall_64+0x74/0xd0
>>> [   67.909889]  entry_SYSCALL_64_after_hwframe+0x49/0xbe
>>>
>>> [   67.914100] The buggy address belongs to the object at 
>>> ffff88870afe8000
>>>                  which belongs to the cache kmalloc-4k of size 4096
>>> [   67.918357] The buggy address is located 2344 bytes inside of
>>>                  4096-byte region [ffff88870afe8000, ffff88870afe9000)
>>> [   67.922562] The buggy address belongs to the page:
>>> [   67.924725] page:ffffea001c2bfa00 refcount:1 mapcount:0 
>>> mapping:ffff88881f00de00 index:0x0 compound_mapcount: 0
>>> [   67.926926] raw: 0017ffe000010200 ffffea001c167a00 
>>> 0000000200000002 ffff88881f00de00
>>> [   67.929144] raw: 0000000000000000 0000000080040004 
>>> 00000001ffffffff 0000000000000000
>>> [   67.931362] page dumped because: kasan: bad access detected
>>>
>>> [   67.936192] Memory state around the buggy address:
>>> [   67.938438]  ffff88870afe8800: 00 00 00 00 00 00 00 00 00 00 00 00 
>>> 00 00 00 00
>>> [   67.941078]  ffff88870afe8880: fc fc fc fc fc fc fc fc fc fc fc fc 
>>> fc fc fc fc
>>> [   67.943393] >ffff88870afe8900: fc fc fc fc fc fc fc fc fc fc fc fc 
>>> fc fc fc fc
>>> [   67.945709]                                   ^
>>> [   67.948000]  ffff88870afe8980: fc fc fc fc fc fc fc fc fc fc fc fc 
>>> fc fc fc fc
>>> [   67.950311]  ffff88870afe8a00: fc fc fc fc fc fc fc fc fc fc fc fc 
>>> fc fc fc fc
>>> [   67.952629] 
>>> ==================================================================
> 



More information about the Linux-security-module-archive mailing list