mirror of
https://github.com/torvalds/linux.git
synced 2024-11-22 12:11:40 +00:00
409db27e3a
syzbot reported a use-after-free in do_accept(), precisely nr_accept() as sk_prot_alloc() allocated the memory and sock_put() frees it. [0] The issue could happen if the heartbeat timer is fired and nr_heartbeat_expiry() calls nr_destroy_socket(), where a socket has SOCK_DESTROY or a listening socket has SOCK_DEAD. In this case, the first condition cannot be true. SOCK_DESTROY is flagged in nr_release() only when the file descriptor is close()d, but accept() is being called for the listening socket, so the second condition must be true. Usually, the AF_NETROM listener neither starts timers nor sets SOCK_DEAD. However, the condition is met if connect() fails before listen(). connect() starts the t1 timer and heartbeat timer, and t1timer calls nr_disconnect() when timeout happens. Then, SOCK_DEAD is set, and if we call listen(), the heartbeat timer calls nr_destroy_socket(). nr_connect nr_establish_data_link(sk) nr_start_t1timer(sk) nr_start_heartbeat(sk) nr_t1timer_expiry nr_disconnect(sk, ETIMEDOUT) nr_sk(sk)->state = NR_STATE_0 sk->sk_state = TCP_CLOSE sock_set_flag(sk, SOCK_DEAD) nr_listen if (sk->sk_state != TCP_LISTEN) sk->sk_state = TCP_LISTEN nr_heartbeat_expiry switch (nr->state) case NR_STATE_0 if (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_DEAD)) nr_destroy_socket(sk) This path seems expected, and nr_destroy_socket() is called to clean up resources. Initially, there was sock_hold() before nr_destroy_socket() so that the socket would not be freed, but the commit517a16b1a8
("netrom: Decrease sock refcount when sock timers expire") accidentally removed it. To fix use-after-free, let's add sock_hold(). [0]: BUG: KASAN: use-after-free in do_accept+0x483/0x510 net/socket.c:1848 Read of size 8 at addr ffff88807978d398 by task syz-executor.3/5315 CPU: 0 PID: 5315 Comm: syz-executor.3 Not tainted 6.2.0-rc3-syzkaller-00165-gd9fc1511728c #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 10/26/2022 Call Trace: <TASK> __dump_stack lib/dump_stack.c:88 [inline] dump_stack_lvl+0xd1/0x138 lib/dump_stack.c:106 print_address_description mm/kasan/report.c:306 [inline] print_report+0x15e/0x461 mm/kasan/report.c:417 kasan_report+0xbf/0x1f0 mm/kasan/report.c:517 do_accept+0x483/0x510 net/socket.c:1848 __sys_accept4_file net/socket.c:1897 [inline] __sys_accept4+0x9a/0x120 net/socket.c:1927 __do_sys_accept net/socket.c:1944 [inline] __se_sys_accept net/socket.c:1941 [inline] __x64_sys_accept+0x75/0xb0 net/socket.c:1941 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x39/0xb0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x63/0xcd RIP: 0033:0x7fa436a8c0c9 Code: 28 00 00 00 75 05 48 83 c4 28 c3 e8 f1 19 00 00 90 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 b8 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007fa437784168 EFLAGS: 00000246 ORIG_RAX: 000000000000002b RAX: ffffffffffffffda RBX: 00007fa436bac050 RCX: 00007fa436a8c0c9 RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000005 RBP: 00007fa436ae7ae9 R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000 R13: 00007ffebc6700df R14: 00007fa437784300 R15: 0000000000022000 </TASK> Allocated by task 5294: kasan_save_stack+0x22/0x40 mm/kasan/common.c:45 kasan_set_track+0x25/0x30 mm/kasan/common.c:52 ____kasan_kmalloc mm/kasan/common.c:371 [inline] ____kasan_kmalloc mm/kasan/common.c:330 [inline] __kasan_kmalloc+0xa3/0xb0 mm/kasan/common.c:380 kasan_kmalloc include/linux/kasan.h:211 [inline] __do_kmalloc_node mm/slab_common.c:968 [inline] __kmalloc+0x5a/0xd0 mm/slab_common.c:981 kmalloc include/linux/slab.h:584 [inline] sk_prot_alloc+0x140/0x290 net/core/sock.c:2038 sk_alloc+0x3a/0x7a0 net/core/sock.c:2091 nr_create+0xb6/0x5f0 net/netrom/af_netrom.c:433 __sock_create+0x359/0x790 net/socket.c:1515 sock_create net/socket.c:1566 [inline] __sys_socket_create net/socket.c:1603 [inline] __sys_socket_create net/socket.c:1588 [inline] __sys_socket+0x133/0x250 net/socket.c:1636 __do_sys_socket net/socket.c:1649 [inline] __se_sys_socket net/socket.c:1647 [inline] __x64_sys_socket+0x73/0xb0 net/socket.c:1647 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x39/0xb0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x63/0xcd Freed by task 14: kasan_save_stack+0x22/0x40 mm/kasan/common.c:45 kasan_set_track+0x25/0x30 mm/kasan/common.c:52 kasan_save_free_info+0x2b/0x40 mm/kasan/generic.c:518 ____kasan_slab_free mm/kasan/common.c:236 [inline] ____kasan_slab_free+0x13b/0x1a0 mm/kasan/common.c:200 kasan_slab_free include/linux/kasan.h:177 [inline] __cache_free mm/slab.c:3394 [inline] __do_kmem_cache_free mm/slab.c:3580 [inline] __kmem_cache_free+0xcd/0x3b0 mm/slab.c:3587 sk_prot_free net/core/sock.c:2074 [inline] __sk_destruct+0x5df/0x750 net/core/sock.c:2166 sk_destruct net/core/sock.c:2181 [inline] __sk_free+0x175/0x460 net/core/sock.c:2192 sk_free+0x7c/0xa0 net/core/sock.c:2203 sock_put include/net/sock.h:1991 [inline] nr_heartbeat_expiry+0x1d7/0x460 net/netrom/nr_timer.c:148 call_timer_fn+0x1da/0x7c0 kernel/time/timer.c:1700 expire_timers+0x2c6/0x5c0 kernel/time/timer.c:1751 __run_timers kernel/time/timer.c:2022 [inline] __run_timers kernel/time/timer.c:1995 [inline] run_timer_softirq+0x326/0x910 kernel/time/timer.c:2035 __do_softirq+0x1fb/0xadc kernel/softirq.c:571 Fixes:517a16b1a8
("netrom: Decrease sock refcount when sock timers expire") Reported-by: syzbot+5fafd5cfe1fc91f6b352@syzkaller.appspotmail.com Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com> Link: https://lore.kernel.org/r/20230120231927.51711-1-kuniyu@amazon.com Signed-off-by: Paolo Abeni <pabeni@redhat.com>
249 lines
5.2 KiB
C
249 lines
5.2 KiB
C
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
/*
|
|
*
|
|
* Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
|
|
* Copyright (C) 2002 Ralf Baechle DO1GRB (ralf@gnu.org)
|
|
*/
|
|
#include <linux/errno.h>
|
|
#include <linux/types.h>
|
|
#include <linux/socket.h>
|
|
#include <linux/in.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/jiffies.h>
|
|
#include <linux/timer.h>
|
|
#include <linux/string.h>
|
|
#include <linux/sockios.h>
|
|
#include <linux/net.h>
|
|
#include <net/ax25.h>
|
|
#include <linux/inet.h>
|
|
#include <linux/netdevice.h>
|
|
#include <linux/skbuff.h>
|
|
#include <net/sock.h>
|
|
#include <net/tcp_states.h>
|
|
#include <linux/uaccess.h>
|
|
#include <linux/fcntl.h>
|
|
#include <linux/mm.h>
|
|
#include <linux/interrupt.h>
|
|
#include <net/netrom.h>
|
|
|
|
static void nr_heartbeat_expiry(struct timer_list *);
|
|
static void nr_t1timer_expiry(struct timer_list *);
|
|
static void nr_t2timer_expiry(struct timer_list *);
|
|
static void nr_t4timer_expiry(struct timer_list *);
|
|
static void nr_idletimer_expiry(struct timer_list *);
|
|
|
|
void nr_init_timers(struct sock *sk)
|
|
{
|
|
struct nr_sock *nr = nr_sk(sk);
|
|
|
|
timer_setup(&nr->t1timer, nr_t1timer_expiry, 0);
|
|
timer_setup(&nr->t2timer, nr_t2timer_expiry, 0);
|
|
timer_setup(&nr->t4timer, nr_t4timer_expiry, 0);
|
|
timer_setup(&nr->idletimer, nr_idletimer_expiry, 0);
|
|
|
|
/* initialized by sock_init_data */
|
|
sk->sk_timer.function = nr_heartbeat_expiry;
|
|
}
|
|
|
|
void nr_start_t1timer(struct sock *sk)
|
|
{
|
|
struct nr_sock *nr = nr_sk(sk);
|
|
|
|
sk_reset_timer(sk, &nr->t1timer, jiffies + nr->t1);
|
|
}
|
|
|
|
void nr_start_t2timer(struct sock *sk)
|
|
{
|
|
struct nr_sock *nr = nr_sk(sk);
|
|
|
|
sk_reset_timer(sk, &nr->t2timer, jiffies + nr->t2);
|
|
}
|
|
|
|
void nr_start_t4timer(struct sock *sk)
|
|
{
|
|
struct nr_sock *nr = nr_sk(sk);
|
|
|
|
sk_reset_timer(sk, &nr->t4timer, jiffies + nr->t4);
|
|
}
|
|
|
|
void nr_start_idletimer(struct sock *sk)
|
|
{
|
|
struct nr_sock *nr = nr_sk(sk);
|
|
|
|
if (nr->idle > 0)
|
|
sk_reset_timer(sk, &nr->idletimer, jiffies + nr->idle);
|
|
}
|
|
|
|
void nr_start_heartbeat(struct sock *sk)
|
|
{
|
|
sk_reset_timer(sk, &sk->sk_timer, jiffies + 5 * HZ);
|
|
}
|
|
|
|
void nr_stop_t1timer(struct sock *sk)
|
|
{
|
|
sk_stop_timer(sk, &nr_sk(sk)->t1timer);
|
|
}
|
|
|
|
void nr_stop_t2timer(struct sock *sk)
|
|
{
|
|
sk_stop_timer(sk, &nr_sk(sk)->t2timer);
|
|
}
|
|
|
|
void nr_stop_t4timer(struct sock *sk)
|
|
{
|
|
sk_stop_timer(sk, &nr_sk(sk)->t4timer);
|
|
}
|
|
|
|
void nr_stop_idletimer(struct sock *sk)
|
|
{
|
|
sk_stop_timer(sk, &nr_sk(sk)->idletimer);
|
|
}
|
|
|
|
void nr_stop_heartbeat(struct sock *sk)
|
|
{
|
|
sk_stop_timer(sk, &sk->sk_timer);
|
|
}
|
|
|
|
int nr_t1timer_running(struct sock *sk)
|
|
{
|
|
return timer_pending(&nr_sk(sk)->t1timer);
|
|
}
|
|
|
|
static void nr_heartbeat_expiry(struct timer_list *t)
|
|
{
|
|
struct sock *sk = from_timer(sk, t, sk_timer);
|
|
struct nr_sock *nr = nr_sk(sk);
|
|
|
|
bh_lock_sock(sk);
|
|
switch (nr->state) {
|
|
case NR_STATE_0:
|
|
/* Magic here: If we listen() and a new link dies before it
|
|
is accepted() it isn't 'dead' so doesn't get removed. */
|
|
if (sock_flag(sk, SOCK_DESTROY) ||
|
|
(sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_DEAD))) {
|
|
sock_hold(sk);
|
|
bh_unlock_sock(sk);
|
|
nr_destroy_socket(sk);
|
|
goto out;
|
|
}
|
|
break;
|
|
|
|
case NR_STATE_3:
|
|
/*
|
|
* Check for the state of the receive buffer.
|
|
*/
|
|
if (atomic_read(&sk->sk_rmem_alloc) < (sk->sk_rcvbuf / 2) &&
|
|
(nr->condition & NR_COND_OWN_RX_BUSY)) {
|
|
nr->condition &= ~NR_COND_OWN_RX_BUSY;
|
|
nr->condition &= ~NR_COND_ACK_PENDING;
|
|
nr->vl = nr->vr;
|
|
nr_write_internal(sk, NR_INFOACK);
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
|
|
nr_start_heartbeat(sk);
|
|
bh_unlock_sock(sk);
|
|
out:
|
|
sock_put(sk);
|
|
}
|
|
|
|
static void nr_t2timer_expiry(struct timer_list *t)
|
|
{
|
|
struct nr_sock *nr = from_timer(nr, t, t2timer);
|
|
struct sock *sk = &nr->sock;
|
|
|
|
bh_lock_sock(sk);
|
|
if (nr->condition & NR_COND_ACK_PENDING) {
|
|
nr->condition &= ~NR_COND_ACK_PENDING;
|
|
nr_enquiry_response(sk);
|
|
}
|
|
bh_unlock_sock(sk);
|
|
sock_put(sk);
|
|
}
|
|
|
|
static void nr_t4timer_expiry(struct timer_list *t)
|
|
{
|
|
struct nr_sock *nr = from_timer(nr, t, t4timer);
|
|
struct sock *sk = &nr->sock;
|
|
|
|
bh_lock_sock(sk);
|
|
nr_sk(sk)->condition &= ~NR_COND_PEER_RX_BUSY;
|
|
bh_unlock_sock(sk);
|
|
sock_put(sk);
|
|
}
|
|
|
|
static void nr_idletimer_expiry(struct timer_list *t)
|
|
{
|
|
struct nr_sock *nr = from_timer(nr, t, idletimer);
|
|
struct sock *sk = &nr->sock;
|
|
|
|
bh_lock_sock(sk);
|
|
|
|
nr_clear_queues(sk);
|
|
|
|
nr->n2count = 0;
|
|
nr_write_internal(sk, NR_DISCREQ);
|
|
nr->state = NR_STATE_2;
|
|
|
|
nr_start_t1timer(sk);
|
|
nr_stop_t2timer(sk);
|
|
nr_stop_t4timer(sk);
|
|
|
|
sk->sk_state = TCP_CLOSE;
|
|
sk->sk_err = 0;
|
|
sk->sk_shutdown |= SEND_SHUTDOWN;
|
|
|
|
if (!sock_flag(sk, SOCK_DEAD)) {
|
|
sk->sk_state_change(sk);
|
|
sock_set_flag(sk, SOCK_DEAD);
|
|
}
|
|
bh_unlock_sock(sk);
|
|
sock_put(sk);
|
|
}
|
|
|
|
static void nr_t1timer_expiry(struct timer_list *t)
|
|
{
|
|
struct nr_sock *nr = from_timer(nr, t, t1timer);
|
|
struct sock *sk = &nr->sock;
|
|
|
|
bh_lock_sock(sk);
|
|
switch (nr->state) {
|
|
case NR_STATE_1:
|
|
if (nr->n2count == nr->n2) {
|
|
nr_disconnect(sk, ETIMEDOUT);
|
|
goto out;
|
|
} else {
|
|
nr->n2count++;
|
|
nr_write_internal(sk, NR_CONNREQ);
|
|
}
|
|
break;
|
|
|
|
case NR_STATE_2:
|
|
if (nr->n2count == nr->n2) {
|
|
nr_disconnect(sk, ETIMEDOUT);
|
|
goto out;
|
|
} else {
|
|
nr->n2count++;
|
|
nr_write_internal(sk, NR_DISCREQ);
|
|
}
|
|
break;
|
|
|
|
case NR_STATE_3:
|
|
if (nr->n2count == nr->n2) {
|
|
nr_disconnect(sk, ETIMEDOUT);
|
|
goto out;
|
|
} else {
|
|
nr->n2count++;
|
|
nr_requeue_frames(sk);
|
|
}
|
|
break;
|
|
}
|
|
|
|
nr_start_t1timer(sk);
|
|
out:
|
|
bh_unlock_sock(sk);
|
|
sock_put(sk);
|
|
}
|