• Jason Wang's avatar
    tun: fix use after free for ptr_ring · ab75811f
    Jason Wang authored
    commit b196d88a upstream.
    
    We used to initialize ptr_ring during TUNSETIFF, this is because its
    size depends on the tx_queue_len of netdevice. And we try to clean it
    up when socket were detached from netdevice. A race were spotted when
    trying to do uninit during a read which will lead a use after free for
    pointer ring. Solving this by always initialize a zero size ptr_ring
    in open() and do resizing during TUNSETIFF, and then we can safely do
    cleanup during close(). With this, there's no need for the workaround
    that was introduced by commit 4df0bfc7 ("tun: fix a memory leak
    for tfile->tx_array").
    
    Backport Note :-
    Comparison with the upstream patch:
    [1] A "semantic revert" of the changes made in
        4df0bfc7("tun: fix a memory leak for tfile->tx_array").
            4df0bfc7 was applied upstream, and then skb array was changed
    	to use ptr_ring. The upstream patch then removes the changes introduced
    	by 4df0bfc7. This backport does the same; "revert" the changes
    	made by 4df0bfc7.
    [2] xdp_rxq_info_unreg() being called in relevant locations
            As xdp_rxq_info related patches are not present in 4.14, these
    	changes are not needed in the backport.
    [3] An instance of ptr_ring_init needs to be replaced by skb_array_init
    	Inside tun_attach()
    [4] ptr_ring_cleanup needs to be replaced by skb_array_cleanup
    	Inside tun_chr_close()
    
    Note that the backport for 7063efd3 ("tuntap: fix use after free during release")
    needs to be applied on top of this patch.
    
    Reported-by: syzbot+e8b902c3c3fadf0a9dba@syzkaller.appspotmail.com
    Cc: Eric Dumazet <eric.dumazet@gmail.com>
    Cc: Cong Wang <xiyou.wangcong@gmail.com>
    Cc: Michael S. Tsirkin <mst@redhat.com>
    Fixes: 1576d986 ("tun: switch to use skb array for tx")
    Signed-off-by: default avatarJason Wang <jasowang@redhat.com>
    Acked-by: default avatarMichael S. Tsirkin <mst@redhat.com>
    Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
    Signed-off-by: default avatarZubin Mithra <zsm@chromium.org>
    Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
    ab75811f
tun.c 67 KB