Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp1963230imm; Thu, 24 May 2018 03:40:43 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpxLiXhiG6/OxBtYEwOteTV/MzomgPsbOVWNZvK15lAAk2UAl6OaQKw5Jf1xWIvwttwrZ8M X-Received: by 2002:a65:60d1:: with SMTP id r17-v6mr5501904pgv.410.1527158443875; Thu, 24 May 2018 03:40:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527158443; cv=none; d=google.com; s=arc-20160816; b=l6lACF4JOh4rH4p2DoHQa46S4By2o+mhcCVrkATr68rgscdMFxXCeEOAOM63bmSlir XYcU5pZd7lVBRTlCk2Dv6u6Ybjz1H5PshU6D/GsP3+O6j2OtSemslFz4WqUZ4qOkEP1c vLAHID17MCzuALxXqmmHlXUsQl/tX6DnS0Uz6+Ed0JY9zpC+AZtoVTGvzH2T9J5N+RCB VakpSTHOojbryaorHMxB1a3Ws3r5d0QXZQ730Yz4eo7Kn0OTQzzytppSuHjuyPPLFbHe I0phLq3F56PeauJVQGVtHytWWDTWQVYf8ydLyxrN86UsaQcdHMf/3sCzpKTqQuisMh89 LZlw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=0SUc0skS64inpjZWHpJcUYfOXB8Zz3+LAl+RNIPsEE8=; b=RxpuU7rjbeG1uDOjmnT+Q52HIL8qsagiCnZPqSGFJ397sTfIkA/0HlQ0uLJyDt10v7 1vt770I/dB+bfih+/5vT+rGMqPII9KY3AKrpwTXC69tPTxXmwjBlLtjIdIRw95kgYcTF 4ZupoE9dQQXFmH1HSkOdh8Ac5w4Xey6+TU3/xxKjAvdDD0RR1fdrk8ByX5y2yejM0dnN hKV7SvWcHfxRm/Vz4E6LQMbeRztHjzf1QtnbpabgjMYS/u0FFdUhlgEEKaYmS313CgQ9 JYWCZv+cGFhuKHgcpVEg9+QKiu+oyvgF3twYo6yHN5wAjm6qVgyqYfPwo3/UrhJBxTr9 KtfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=uIk4nZxy; 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 d10-v6si17052382pgu.626.2018.05.24.03.40.29; Thu, 24 May 2018 03:40:43 -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; dkim=pass header.i=@kernel.org header.s=default header.b=uIk4nZxy; 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 S1030901AbeEXKjr (ORCPT + 99 others); Thu, 24 May 2018 06:39:47 -0400 Received: from mail.kernel.org ([198.145.29.99]:39982 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1030728AbeEXJ7N (ORCPT ); Thu, 24 May 2018 05:59:13 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 9344020895; Thu, 24 May 2018 09:59:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527155952; bh=dQTQRmdVNmcpLzI9j7bw15cDOMsnJHRlayJGmyUH71I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uIk4nZxy4nbvxnUeTW0KyhHfDCCv5PthxUvQZWpNesDZCxro1ct+SBZh8YxUCJV6n Qvai9ah9zvbQ4z8zMjeiQ/0EXbK4gmRmHOdmV1huOTbRNYTFDyZlinSXhCqUK+pxDs 735sWSCyjWYCtOAp2n4e6aKgOp/Svbd0yhVKt5Ro= 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" Subject: [PATCH 4.16 011/161] tun: fix use after free for ptr_ring Date: Thu, 24 May 2018 11:37:16 +0200 Message-Id: <20180524093019.778543667@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180524093018.331893860@linuxfoundation.org> References: <20180524093018.331893860@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.16-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jason Wang [ Upstream commit b196d88aba8ac72b775137854121097f4c4c6862 ] 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"). 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: Greg Kroah-Hartman --- drivers/net/tun.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -680,15 +680,6 @@ static void tun_queue_purge(struct tun_f skb_queue_purge(&tfile->sk.sk_error_queue); } -static void tun_cleanup_tx_ring(struct tun_file *tfile) -{ - if (tfile->tx_ring.queue) { - ptr_ring_cleanup(&tfile->tx_ring, tun_ptr_free); - xdp_rxq_info_unreg(&tfile->xdp_rxq); - memset(&tfile->tx_ring, 0, sizeof(tfile->tx_ring)); - } -} - static void __tun_detach(struct tun_file *tfile, bool clean) { struct tun_file *ntfile; @@ -735,7 +726,8 @@ static void __tun_detach(struct tun_file tun->dev->reg_state == NETREG_REGISTERED) unregister_netdevice(tun->dev); } - tun_cleanup_tx_ring(tfile); + if (tun) + xdp_rxq_info_unreg(&tfile->xdp_rxq); sock_put(&tfile->sk); } } @@ -775,14 +767,14 @@ static void tun_detach_all(struct net_de tun_napi_del(tun, tfile); /* Drop read queue */ tun_queue_purge(tfile); + xdp_rxq_info_unreg(&tfile->xdp_rxq); sock_put(&tfile->sk); - tun_cleanup_tx_ring(tfile); } list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) { tun_enable_queue(tfile); tun_queue_purge(tfile); + xdp_rxq_info_unreg(&tfile->xdp_rxq); sock_put(&tfile->sk); - tun_cleanup_tx_ring(tfile); } BUG_ON(tun->numdisabled != 0); @@ -826,7 +818,8 @@ static int tun_attach(struct tun_struct } if (!tfile->detached && - ptr_ring_init(&tfile->tx_ring, dev->tx_queue_len, GFP_KERNEL)) { + ptr_ring_resize(&tfile->tx_ring, dev->tx_queue_len, + GFP_KERNEL, tun_ptr_free)) { err = -ENOMEM; goto out; } @@ -3131,6 +3124,11 @@ static int tun_chr_open(struct inode *in &tun_proto, 0); if (!tfile) return -ENOMEM; + if (ptr_ring_init(&tfile->tx_ring, 0, GFP_KERNEL)) { + sk_free(&tfile->sk); + return -ENOMEM; + } + RCU_INIT_POINTER(tfile->tun, NULL); tfile->flags = 0; tfile->ifindex = 0; @@ -3151,8 +3149,6 @@ static int tun_chr_open(struct inode *in sock_set_flag(&tfile->sk, SOCK_ZEROCOPY); - memset(&tfile->tx_ring, 0, sizeof(tfile->tx_ring)); - return 0; } @@ -3161,6 +3157,7 @@ static int tun_chr_close(struct inode *i struct tun_file *tfile = file->private_data; tun_detach(tfile, true); + ptr_ring_cleanup(&tfile->tx_ring, tun_ptr_free); return 0; }