crypto: use-after-free in rng_recvmsg

76 views
Skip to first unread message

Dmitry Vyukov

unread,
Dec 28, 2015, 8:49:08 AM12/28/15
to Herbert Xu, David S. Miller, linux-...@vger.kernel.org, LKML, syzkaller, Kostya Serebryany, Alexander Potapenko, Sasha Levin, Eric Dumazet
Hello,

On commit a88164345b81292b55a8d4829fdd35c8d611cd7d (Dec 23)
+ crypto: algif_skcipher - Use new skcipher interface
+ crypto: algif_skcipher - Require setkey before accept(2)

The following program triggers use-after-free in rng_recvmsg:

// autogenerated by syzkaller (http://github.com/google/syzkaller)
#include <unistd.h>
#include <sys/syscall.h>
#include <string.h>
#include <stdint.h>
#include <pthread.h>

#ifndef SYS_memfd_create
#define SYS_memfd_create 319
#endif

long r[13];

void *thr(void *arg)
{
switch ((long)arg) {
case 0:
r[0] = syscall(SYS_mmap, 0x20000000ul, 0x11000ul,
0x3ul, 0x32ul, 0xfffffffffffffffful, 0x0ul);
break;
case 1:
r[1] = syscall(SYS_socket, 0x26ul, 0x5ul, 0x0ul, 0, 0, 0);
break;
case 2:
*(uint16_t*)0x20003000 = (uint16_t)0x26;
memcpy((void*)0x20003002,
"\x72\x6e\x67\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 14);
*(uint32_t*)0x20003010 = (uint32_t)0x8;
*(uint32_t*)0x20003014 = (uint32_t)0x800;
memcpy((void*)0x20003018,
"\x73\x74\x64\x72\x6e\x67\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
64);
r[7] = syscall(SYS_bind, r[1], 0x20003000ul, 0x58ul, 0, 0, 0);
break;
case 3:
memcpy((void*)0x20003ff0,
"\x26\x73\x79\x73\x74\x65\x6d\x73\x65\x63\x75\x72\x69\x74\x79\x00",
16);
r[9] = syscall(SYS_memfd_create, 0x20003ff0ul, 0x2ul,
0, 0, 0, 0);
break;
case 4:
r[10] = syscall(SYS_ioctl, r[9], 0x540bul, 0x4ul, 0, 0, 0);
break;
case 5:
r[11] = syscall(SYS_accept, r[1], 0x0ul, 0x2000affcul, 0, 0, 0);
break;
case 6:
r[12] = syscall(SYS_read, r[11], 0x200102a3ul, 0x54ul, 0, 0, 0);
break;
}
return 0;
}

int main()
{
long i;
pthread_t th[7];

memset(r, -1, sizeof(r));
for (i = 0; i < 7; i++) {
pthread_create(&th[i], 0, thr, (void*)i);
usleep(10000);
}
for (i = 0; i < 7; i++) {
pthread_create(&th[i], 0, thr, (void*)i);
if (i%2==0)
usleep(10000);
}
usleep(100000);
return 0;
}


==================================================================
BUG: KASAN: use-after-free in rng_recvmsg+0x20f/0x230 at addr ffff880061c4d758
Read of size 8 by task a.out/8875
=============================================================================
BUG kmalloc-512 (Not tainted): kasan: bad access detected
-----------------------------------------------------------------------------

INFO: Allocated in crypto_create_tfm+0x7e/0x240 age=97 cpu=2 pid=8833
[< inline >] kmalloc include/linux/slab.h:463
[< inline >] kzalloc include/linux/slab.h:602
[< none >] crypto_create_tfm+0x7e/0x240 crypto/api.c:470
[< none >] crypto_alloc_tfm+0x13c/0x370 crypto/api.c:555
[< none >] crypto_alloc_rng+0x2c/0x40 crypto/rng.c:120
[< none >] rng_bind+0x25/0x30 crypto/algif_rng.c:119
[< none >] alg_bind+0x18e/0x3a0 crypto/af_alg.c:155
[< none >] SYSC_bind+0x1ea/0x250 net/socket.c:1376
[< none >] SyS_bind+0x24/0x30 net/socket.c:1362
[< none >] entry_SYSCALL_64_fastpath+0x16/0x7a
arch/x86/entry/entry_64.S:185

INFO: Freed in kzfree+0x28/0x30 age=46 cpu=1 pid=8859
[< none >] kfree+0x26a/0x290 mm/slub.c:3662
[< none >] kzfree+0x28/0x30 mm/slab_common.c:1270
[< none >] crypto_destroy_tfm+0xcb/0x190 crypto/api.c:596
[< inline >] crypto_free_rng include/crypto/rng.h:122
[< none >] rng_release+0x18/0x20 crypto/algif_rng.c:124
[< inline >] alg_do_release crypto/af_alg.c:116
[< none >] alg_bind+0x246/0x3a0 crypto/af_alg.c:170
[< none >] SYSC_bind+0x1ea/0x250 net/socket.c:1376
[< none >] SyS_bind+0x24/0x30 net/socket.c:1362
[< none >] entry_SYSCALL_64_fastpath+0x16/0x7a
arch/x86/entry/entry_64.S:185

INFO: Slab 0xffffea0001871300 objects=16 used=14 fp=0xffff880061c4e670
flags=0x5fffc0000004080
INFO: Object 0xffff880061c4d710 @offset=5904 fp=0x (null)
CPU: 1 PID: 8875 Comm: a.out Tainted: G B 4.4.0-rc6+ #178
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
00000000ffffffff ffff880036edf9a0 ffffffff8289db7d ffff88003e804f00
ffff880061c4d710 ffff880061c4c000 ffff880036edf9d0 ffffffff816c9064
ffff88003e804f00 ffffea0001871300 ffff880061c4d710 ffff880036edfaf0

Call Trace:
[<ffffffff816d25de>] __asan_report_load8_noabort+0x3e/0x40
mm/kasan/report.c:295
[< inline >] crypto_rng_get_bytes include/crypto/rng.h:112
[<ffffffff827e634f>] rng_recvmsg+0x20f/0x230 crypto/algif_rng.c:85
[< inline >] sock_recvmsg_nosec net/socket.c:712
[<ffffffff84b5bcfd>] sock_recvmsg+0x9d/0xb0 net/socket.c:720
[<ffffffff84b5bf5d>] sock_read_iter+0x24d/0x360 net/socket.c:797
[< inline >] new_sync_read fs/read_write.c:422
[<ffffffff81712b0d>] __vfs_read+0x2fd/0x460 fs/read_write.c:434
[<ffffffff81714c76>] vfs_read+0x106/0x310 fs/read_write.c:454
[< inline >] SYSC_read fs/read_write.c:569
[<ffffffff81717a21>] SyS_read+0x111/0x220 fs/read_write.c:562
[<ffffffff85c8ecf6>] entry_SYSCALL_64_fastpath+0x16/0x7a
arch/x86/entry/entry_64.S:185
==================================================================


Another report on a different program, but with a similar cause:

==================================================================
BUG: KASAN: use-after-free in hash_sock_destruct+0x1a7/0x1d0 at addr
ffff88003abce248
Read of size 8 by task syz-executor/21732
=============================================================================
BUG kmalloc-192 (Tainted: G B D ): kasan: bad access detected
-----------------------------------------------------------------------------

INFO: Allocated in crypto_create_tfm+0x7e/0x240 age=28 cpu=1 pid=21732
[< inline >] kmalloc include/linux/slab.h:463
[< inline >] kzalloc include/linux/slab.h:602
[< none >] crypto_create_tfm+0x7e/0x240 crypto/api.c:470
[< none >] crypto_alloc_tfm+0x13c/0x370 crypto/api.c:555
[< none >] crypto_alloc_ahash+0x2c/0x40 crypto/ahash.c:538
[< none >] hash_bind+0x25/0x30 crypto/algif_hash.c:240
[< none >] alg_bind+0x18e/0x3a0 crypto/af_alg.c:155
[< none >] SYSC_bind+0x1ea/0x250 net/socket.c:1376
[< none >] SyS_bind+0x24/0x30 net/socket.c:1362
[< none >] entry_SYSCALL_64_fastpath+0x16/0x7a
arch/x86/entry/entry_64.S:185

INFO: Freed in kzfree+0x28/0x30 age=32 cpu=0 pid=21733
[< none >] kfree+0x26a/0x290 mm/slub.c:3662
[< none >] kzfree+0x28/0x30 mm/slab_common.c:1270
[< none >] crypto_destroy_tfm+0xcb/0x190 crypto/api.c:596
[< inline >] crypto_free_ahash include/crypto/hash.h:258
[< none >] hash_release+0x19/0x20 crypto/algif_hash.c:245
[< inline >] alg_do_release crypto/af_alg.c:116
[< none >] alg_bind+0x246/0x3a0 crypto/af_alg.c:170
[< none >] SYSC_bind+0x1ea/0x250 net/socket.c:1376
[< none >] SyS_bind+0x24/0x30 net/socket.c:1362
[< none >] entry_SYSCALL_64_fastpath+0x16/0x7a
arch/x86/entry/entry_64.S:185

INFO: Slab 0xffffea0000eaf300 objects=24 used=15 fp=0xffff88003abce1b8
flags=0x1fffc0000004080
INFO: Object 0xffff88003abce1b8 @offset=8632 fp=0xffff88003abce6e8
CPU: 1 PID: 21732 Comm: syz-executor Tainted: G B D 4.4.0-rc6+ #178
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
00000000ffffffff ffff88006d4977d8 ffffffff8289db7d ffff88003e804c00
ffff88003abce1b8 ffff88003abcc000 ffff88006d497808 ffffffff816c9064
ffff88003e804c00 ffffea0000eaf300 ffff88003abce1b8 0000000000000000

Call Trace:
[<ffffffff816d25de>] __asan_report_load8_noabort+0x3e/0x40
mm/kasan/report.c:295
[< inline >] crypto_ahash_digestsize include/crypto/hash.h:290
[<ffffffff827dff87>] hash_sock_destruct+0x1a7/0x1d0 crypto/algif_hash.c:259
[<ffffffff84b6f46a>] sk_destruct+0x4a/0x490 net/core/sock.c:1447
[<ffffffff84b6f907>] __sk_free+0x57/0x200 net/core/sock.c:1475
[<ffffffff84b6fae0>] sk_free+0x30/0x40 net/core/sock.c:1486
[< inline >] sock_put include/net/sock.h:1627
[<ffffffff827dcf5b>] af_alg_release+0x5b/0x70 crypto/af_alg.c:123
[<ffffffff84b5944d>] sock_release+0x8d/0x1d0 net/socket.c:571
[<ffffffff84b595a6>] sock_close+0x16/0x20 net/socket.c:1022
[<ffffffff81719a73>] __fput+0x233/0x780 fs/file_table.c:208
[<ffffffff8171a045>] ____fput+0x15/0x20 fs/file_table.c:244
[<ffffffff8134679b>] task_work_run+0x16b/0x200 kernel/task_work.c:115
[< inline >] exit_task_work include/linux/task_work.h:21
[<ffffffff812f4d3b>] do_exit+0x8bb/0x2b20 kernel/exit.c:750
[<ffffffff812f7118>] do_group_exit+0x108/0x320 kernel/exit.c:880
[<ffffffff813196c4>] get_signal+0x5e4/0x1500 kernel/signal.c:2307
[<ffffffff811507a3>] do_signal+0x83/0x1c90 arch/x86/kernel/signal.c:712
[<ffffffff81003901>] exit_to_usermode_loop+0xf1/0x1a0
arch/x86/entry/common.c:247
[< inline >] prepare_exit_to_usermode arch/x86/entry/common.c:282
[<ffffffff8100631f>] syscall_return_slowpath+0x19f/0x210
arch/x86/entry/common.c:344
[<ffffffff85c8ee62>] int_ret_from_sys_call+0x25/0x9f
arch/x86/entry/entry_64.S:281
==================================================================

Stephan Mueller

unread,
Dec 30, 2015, 3:54:11 PM12/30/15
to Dmitry Vyukov, Herbert Xu, David S. Miller, linux-...@vger.kernel.org, LKML, syzkaller, Kostya Serebryany, Alexander Potapenko, Sasha Levin, Eric Dumazet
Am Montag, 28. Dezember 2015, 14:48:48 schrieb Dmitry Vyukov:

Hi Dmitry,

> Hello,
>
> On commit a88164345b81292b55a8d4829fdd35c8d611cd7d (Dec 23)
> + crypto: algif_skcipher - Use new skcipher interface
> + crypto: algif_skcipher - Require setkey before accept(2)
>
> The following program triggers use-after-free in rng_recvmsg:

Thank you for the bug report.

After applying the patch from Herbert [1], I was not able to reproduce the
issue even after 1000 rounds of the test code. Considering the issue and the
patch from Herbert, I would consider the issue being fixed.

[1] http://www.mail-archive.com/linux-...@vger.kernel.org/msg17450.html

Thank you.
--
Ciao
Stephan
Reply all
Reply to author
Forward
Message has been deleted
0 new messages