• Kuniyuki Iwashima's avatar
    tcp: Fix potential use-after-free due to double kfree() · c89dffc7
    Kuniyuki Iwashima authored
    Receiving ACK with a valid SYN cookie, cookie_v4_check() allocates struct
    request_sock and then can allocate inet_rsk(req)->ireq_opt. After that,
    tcp_v4_syn_recv_sock() allocates struct sock and copies ireq_opt to
    inet_sk(sk)->inet_opt. Normally, tcp_v4_syn_recv_sock() inserts the full
    socket into ehash and sets NULL to ireq_opt. Otherwise,
    tcp_v4_syn_recv_sock() has to reset inet_opt by NULL and free the full
    socket.
    
    The commit 01770a16 ("tcp: fix race condition when creating child
    sockets from syncookies") added a new path, in which more than one cores
    create full sockets for the same SYN cookie. Currently, the core which
    loses the race frees the full socket without resetting inet_opt, resulting
    in that both sock_put() and reqsk_put() call kfree() for the same memory:
    
      sock_put
        sk_free
          __sk_free
            sk_destruct
              __sk_destruct
                sk->sk_destruct/inet_sock_destruct
                  kfree(rcu_dereference_protected(...
    c89dffc7
tcp_ipv4.c 79.1 KB