0011-net-crypto-set-sk-to-NULL-when-af_alg_release.patch 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Mao Wenan <[email protected]>
  3. Date: Mon, 18 Feb 2019 10:44:44 +0800
  4. Subject: [PATCH] net: crypto set sk to NULL when af_alg_release.
  5. KASAN has found use-after-free in sockfs_setattr.
  6. The existed commit 6d8c50dcb029 ("socket: close race condition between sock_close()
  7. and sockfs_setattr()") is to fix this simillar issue, but it seems to ignore
  8. that crypto module forgets to set the sk to NULL after af_alg_release.
  9. KASAN report details as below:
  10. BUG: KASAN: use-after-free in sockfs_setattr+0x120/0x150
  11. Write of size 4 at addr ffff88837b956128 by task syz-executor0/4186
  12. CPU: 2 PID: 4186 Comm: syz-executor0 Not tainted xxx + #1
  13. Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS
  14. 1.10.2-1ubuntu1 04/01/2014
  15. Call Trace:
  16. dump_stack+0xca/0x13e
  17. print_address_description+0x79/0x330
  18. ? vprintk_func+0x5e/0xf0
  19. kasan_report+0x18a/0x2e0
  20. ? sockfs_setattr+0x120/0x150
  21. sockfs_setattr+0x120/0x150
  22. ? sock_register+0x2d0/0x2d0
  23. notify_change+0x90c/0xd40
  24. ? chown_common+0x2ef/0x510
  25. chown_common+0x2ef/0x510
  26. ? chmod_common+0x3b0/0x3b0
  27. ? __lock_is_held+0xbc/0x160
  28. ? __sb_start_write+0x13d/0x2b0
  29. ? __mnt_want_write+0x19a/0x250
  30. do_fchownat+0x15c/0x190
  31. ? __ia32_sys_chmod+0x80/0x80
  32. ? trace_hardirqs_on_thunk+0x1a/0x1c
  33. __x64_sys_fchownat+0xbf/0x160
  34. ? lockdep_hardirqs_on+0x39a/0x5e0
  35. do_syscall_64+0xc8/0x580
  36. entry_SYSCALL_64_after_hwframe+0x49/0xbe
  37. RIP: 0033:0x462589
  38. Code: f7 d8 64 89 02 b8 ff ff ff ff c3 66 0f 1f 44 00 00 48 89 f8 48 89
  39. f7 48 89 d6 48 89
  40. ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3
  41. 48 c7 c1 bc ff ff
  42. ff f7 d8 64 89 01 48
  43. RSP: 002b:00007fb4b2c83c58 EFLAGS: 00000246 ORIG_RAX: 0000000000000104
  44. RAX: ffffffffffffffda RBX: 000000000072bfa0 RCX: 0000000000462589
  45. RDX: 0000000000000000 RSI: 00000000200000c0 RDI: 0000000000000007
  46. RBP: 0000000000000005 R08: 0000000000001000 R09: 0000000000000000
  47. R10: 0000000000000000 R11: 0000000000000246 R12: 00007fb4b2c846bc
  48. R13: 00000000004bc733 R14: 00000000006f5138 R15: 00000000ffffffff
  49. Allocated by task 4185:
  50. kasan_kmalloc+0xa0/0xd0
  51. __kmalloc+0x14a/0x350
  52. sk_prot_alloc+0xf6/0x290
  53. sk_alloc+0x3d/0xc00
  54. af_alg_accept+0x9e/0x670
  55. hash_accept+0x4a3/0x650
  56. __sys_accept4+0x306/0x5c0
  57. __x64_sys_accept4+0x98/0x100
  58. do_syscall_64+0xc8/0x580
  59. entry_SYSCALL_64_after_hwframe+0x49/0xbe
  60. Freed by task 4184:
  61. __kasan_slab_free+0x12e/0x180
  62. kfree+0xeb/0x2f0
  63. __sk_destruct+0x4e6/0x6a0
  64. sk_destruct+0x48/0x70
  65. __sk_free+0xa9/0x270
  66. sk_free+0x2a/0x30
  67. af_alg_release+0x5c/0x70
  68. __sock_release+0xd3/0x280
  69. sock_close+0x1a/0x20
  70. __fput+0x27f/0x7f0
  71. task_work_run+0x136/0x1b0
  72. exit_to_usermode_loop+0x1a7/0x1d0
  73. do_syscall_64+0x461/0x580
  74. entry_SYSCALL_64_after_hwframe+0x49/0xbe
  75. Syzkaller reproducer:
  76. r0 = perf_event_open(&(0x7f0000000000)={0x0, 0x70, 0x0, 0x0, 0x0, 0x0,
  77. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  78. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  79. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, @perf_config_ext}, 0x0, 0x0,
  80. 0xffffffffffffffff, 0x0)
  81. r1 = socket$alg(0x26, 0x5, 0x0)
  82. getrusage(0x0, 0x0)
  83. bind(r1, &(0x7f00000001c0)=@alg={0x26, 'hash\x00', 0x0, 0x0,
  84. 'sha256-ssse3\x00'}, 0x80)
  85. r2 = accept(r1, 0x0, 0x0)
  86. r3 = accept4$unix(r2, 0x0, 0x0, 0x0)
  87. r4 = dup3(r3, r0, 0x0)
  88. fchownat(r4, &(0x7f00000000c0)='\x00', 0x0, 0x0, 0x1000)
  89. Fixes: 6d8c50dcb029 ("socket: close race condition between sock_close() and sockfs_setattr()")
  90. Signed-off-by: Mao Wenan <[email protected]>
  91. Signed-off-by: Thomas Lamprecht <[email protected]>
  92. ---
  93. crypto/af_alg.c | 4 +++-
  94. 1 file changed, 3 insertions(+), 1 deletion(-)
  95. diff --git a/crypto/af_alg.c b/crypto/af_alg.c
  96. index 671e42ff21d4..493002afae73 100644
  97. --- a/crypto/af_alg.c
  98. +++ b/crypto/af_alg.c
  99. @@ -122,8 +122,10 @@ static void alg_do_release(const struct af_alg_type *type, void *private)
  100. int af_alg_release(struct socket *sock)
  101. {
  102. - if (sock->sk)
  103. + if (sock->sk) {
  104. sock_put(sock->sk);
  105. + sock->sk = NULL;
  106. + }
  107. return 0;
  108. }
  109. EXPORT_SYMBOL_GPL(af_alg_release);