Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp4553135imm; Mon, 17 Sep 2018 16:24:41 -0700 (PDT) X-Google-Smtp-Source: ANB0Vdb2qmCdua5jG12a+56lnRG0glJKtTOqeKbyG0oDbs0iHZc0v1tn5T18tW0cs2w46Rm7HuIP X-Received: by 2002:a63:444d:: with SMTP id t13-v6mr25097473pgk.102.1537226681358; Mon, 17 Sep 2018 16:24:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537226681; cv=none; d=google.com; s=arc-20160816; b=LFmKqqNgfH63w707c01XdSRjCuCUBocvd3dstrriIvB0dDK6pBNgYTfVF26kdIfJvx Gw1IEk6AjTTl8Lws6n1DpTENo1By8z0ngWgvbmnnhox6IdQCyEh2HGFXtsYmlAEZs0BY PdLk6NpfPwClPRd6MFzTP9q8RHN55Bi9QsUceGBPt8WUc7WBd80qT21nv0rGwtR3Mcse eQLkGfC9tTDMPe3+gTpnuUiVQFtj06ZTZv5CdoylbBp+s2pZQz8BNkV2Jn0v51Ze46uj OhhuzOUPyYuCWOhFF/WA0vUCCoB3VSUoNMYn5ezXMGbxsPAGx3Cmxf40ZOXMRlUZ7hr3 pw1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from; bh=Kgykf5ff6p+kdvWfFB8/TCEqaZ9H35YYY4Ue2zWV2jg=; b=feh0NqtjL0D0tBuDUH8cqDrLHyxb28f61VGxC9f+3q9+lZyoQjqJ80IFmaOaH3PJMt hySc4dG8QiI0ksBOg2KK7Zz69N3FJPAsUph8ZVmpVfcdXCWzGbiee2NgC6wkxoAH2yud Xdt3+BGq4Q7ZyrH4EebWvrsgtbJO9XxO/xJQfFVmf9T/kdrEOqN7yDKpeMoTZrGBNRrW 6pXrCU6SAfuoAJarhRAH6dsrQNQhPUsyaIOI9Dseqtkm7qJK0dB9Towktn1S48H31KTA Hl+RhixIB8ORnzSOL+A85/iWP4qAlWmUCaT378FmhSBQdXUXWGmEJcMVgqRnsKDi1jje dTFw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l12-v6si16342494plt.440.2018.09.17.16.24.26; Mon, 17 Sep 2018 16:24:41 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731345AbeIREgO (ORCPT + 99 others); Tue, 18 Sep 2018 00:36:14 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:49552 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728677AbeIREgN (ORCPT ); Tue, 18 Sep 2018 00:36:13 -0400 Received: from localhost (li1825-44.members.linode.com [172.104.248.44]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 594EBC49; Mon, 17 Sep 2018 23:06:43 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+e8b902c3c3fadf0a9dba@syzkaller.appspotmail.com, Eric Dumazet , Cong Wang , "Michael S. Tsirkin" , Jason Wang , "David S. Miller" , Zubin Mithra Subject: [PATCH 4.14 123/126] tun: fix use after free for ptr_ring Date: Tue, 18 Sep 2018 00:42:51 +0200 Message-Id: <20180917211712.041602122@linuxfoundation.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20180917211703.481236999@linuxfoundation.org> References: <20180917211703.481236999@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jason Wang commit b196d88aba8ac72b775137854121097f4c4c6862 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 4df0bfc79904 ("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 4df0bfc799("tun: fix a memory leak for tfile->tx_array"). 4df0bfc799 was applied upstream, and then skb array was changed to use ptr_ring. The upstream patch then removes the changes introduced by 4df0bfc799. This backport does the same; "revert" the changes made by 4df0bfc799. [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 7063efd33b ("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 Cc: Cong Wang Cc: Michael S. Tsirkin Fixes: 1576d9860599 ("tun: switch to use skb array for tx") Signed-off-by: Jason Wang Acked-by: Michael S. Tsirkin Signed-off-by: David S. Miller Signed-off-by: Zubin Mithra Signed-off-by: Greg Kroah-Hartman --- drivers/net/tun.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -534,14 +534,6 @@ static void tun_queue_purge(struct tun_f skb_queue_purge(&tfile->sk.sk_error_queue); } -static void tun_cleanup_tx_array(struct tun_file *tfile) -{ - if (tfile->tx_array.ring.queue) { - skb_array_cleanup(&tfile->tx_array); - memset(&tfile->tx_array, 0, sizeof(tfile->tx_array)); - } -} - static void __tun_detach(struct tun_file *tfile, bool clean) { struct tun_file *ntfile; @@ -583,7 +575,6 @@ static void __tun_detach(struct tun_file tun->dev->reg_state == NETREG_REGISTERED) unregister_netdevice(tun->dev); } - tun_cleanup_tx_array(tfile); sock_put(&tfile->sk); } } @@ -623,13 +614,11 @@ static void tun_detach_all(struct net_de /* Drop read queue */ tun_queue_purge(tfile); sock_put(&tfile->sk); - tun_cleanup_tx_array(tfile); } list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) { tun_enable_queue(tfile); tun_queue_purge(tfile); sock_put(&tfile->sk); - tun_cleanup_tx_array(tfile); } BUG_ON(tun->numdisabled != 0); @@ -675,7 +664,7 @@ static int tun_attach(struct tun_struct } if (!tfile->detached && - skb_array_init(&tfile->tx_array, dev->tx_queue_len, GFP_KERNEL)) { + skb_array_resize(&tfile->tx_array, dev->tx_queue_len, GFP_KERNEL)) { err = -ENOMEM; goto out; } @@ -2624,6 +2613,11 @@ static int tun_chr_open(struct inode *in &tun_proto, 0); if (!tfile) return -ENOMEM; + if (skb_array_init(&tfile->tx_array, 0, GFP_KERNEL)) { + sk_free(&tfile->sk); + return -ENOMEM; + } + RCU_INIT_POINTER(tfile->tun, NULL); tfile->flags = 0; tfile->ifindex = 0; @@ -2644,8 +2638,6 @@ static int tun_chr_open(struct inode *in sock_set_flag(&tfile->sk, SOCK_ZEROCOPY); - memset(&tfile->tx_array, 0, sizeof(tfile->tx_array)); - return 0; } @@ -2654,6 +2646,7 @@ static int tun_chr_close(struct inode *i struct tun_file *tfile = file->private_data; tun_detach(tfile, true); + skb_array_cleanup(&tfile->tx_array); return 0; }