Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6259583imu; Mon, 21 Jan 2019 06:01:15 -0800 (PST) X-Google-Smtp-Source: ALg8bN4ytx1A/7xd+iz68g9sZnuNCOvjjiHNUD2A7e15/23KG6Aek2pZLRPZLYcJl0o9szlp7w/0 X-Received: by 2002:aa7:810c:: with SMTP id b12mr29631362pfi.44.1548079275138; Mon, 21 Jan 2019 06:01:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548079275; cv=none; d=google.com; s=arc-20160816; b=Y/Xt43AJHrbojfyD2Njo4fIwzBSTqBzOAQy1yvIYIZ4o7Q8hESNVyNvAh+p5DBqceK kh7o5DhiLQ+bRYoPW67wCIXIdexdQlnjQa/H3hlXr/Q4MGTm+SWT9pZXsJOVMaZG8DJE nFobBeWgbhgtVKv1iWSOICZxSqXHmwj6oqFjFtD75b2mcOEGcQHR2G0s7v2bwrUnZtI8 AXqYwiyXssqIapHlUuLl6bCAPFoaOTd1vusbQ+qcElTh0hgw8qnUgtK8PLvDaUD9JQcl d9yOigqGf809uSVe0nszygEo0FbD2PhgDJdpsNlHNtxXVaBAv8ZToESdg9+43HeqJENe 1n5w== 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:dkim-signature; bh=xunfBRNFz9R4dAMQkxauee7smMU8v0gYSxsmHapCxm8=; b=u6nn5C2hf7vR+Kyuklou4rcuNCaRrlY/0Y8prw95XyJ/mSDQUl0aJkNCuuope17/U7 ++NEQz2Km1X5XUvSH+NZ6LlzaP0+2PMAV4h/btKS4dY6ympnPsMFV3qXZ109gQCCyr9g qimCqkDeD6q/Nt/Lrd46CVFLVTbzc5zobXtJUOPZbdyOIvL+Si/+nNKngY1anHzPxDvf zFMeskzyS4Lh5vYom4j9gChGLgVDfFWCb6Rynn07yced7q5Ed1hLw1fINxsFGmxloOZt ygqQIEqk7ZcMlkJHD6c0xI4H6+j98yc7DHtG24mxAb3UCBrTeXSedfxOui8K+m6QcKy5 yABw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=1+jLuPkH; 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 e13si11918564pfi.271.2019.01.21.06.00.59; Mon, 21 Jan 2019 06:01:15 -0800 (PST) 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=1+jLuPkH; 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 S1732135AbfAUN7J (ORCPT + 99 others); Mon, 21 Jan 2019 08:59:09 -0500 Received: from mail.kernel.org ([198.145.29.99]:45034 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731803AbfAUN7H (ORCPT ); Mon, 21 Jan 2019 08:59:07 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (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 2775E20879; Mon, 21 Jan 2019 13:59:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1548079146; bh=DbV1FdZPsgUgbzLQGXOpzm6F2sawJjVIYsHSPk0Bdr8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1+jLuPkHktKeAhvecmesZF7U4LG14zhb7iDcaUKtN7VGJOfbzfZVe3fs3h/tJQ8CJ ru0HX0odDOXoy3QdfT12FjcZn+Sdy+u421Gk09DAgL9RI9+drcToo4+nwuB3Rt+sUa NhzSdwrrMGZhlj53PjDgB5PUzsJz8uxpyBfw0p4U= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot , Stanislav Fomichev , "David S. Miller" Subject: [PATCH 4.19 24/99] tun: publish tfile after its fully initialized Date: Mon, 21 Jan 2019 14:48:16 +0100 Message-Id: <20190121134914.823404472@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190121134913.924726465@linuxfoundation.org> References: <20190121134913.924726465@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore 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.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Stanislav Fomichev [ Upstream commit 0b7959b6257322f7693b08a459c505d4938646f2 ] BUG: unable to handle kernel NULL pointer dereference at 00000000000000d1 Call Trace: ? napi_gro_frags+0xa7/0x2c0 tun_get_user+0xb50/0xf20 tun_chr_write_iter+0x53/0x70 new_sync_write+0xff/0x160 vfs_write+0x191/0x1e0 __x64_sys_write+0x5e/0xd0 do_syscall_64+0x47/0xf0 entry_SYSCALL_64_after_hwframe+0x44/0xa9 I think there is a subtle race between sending a packet via tap and attaching it: CPU0: CPU1: tun_chr_ioctl(TUNSETIFF) tun_set_iff tun_attach rcu_assign_pointer(tfile->tun, tun); tun_fops->write_iter() tun_chr_write_iter tun_napi_alloc_frags napi_get_frags napi->skb = napi_alloc_skb tun_napi_init netif_napi_add napi->skb = NULL napi->skb is NULL here napi_gro_frags napi_frags_skb skb = napi->skb skb_reset_mac_header(skb) panic() Move rcu_assign_pointer(tfile->tun) and rcu_assign_pointer(tun->tfiles) to be the last thing we do in tun_attach(); this should guarantee that when we call tun_get() we always get an initialized object. v2 changes: * remove extra napi_mutex locks/unlocks for napi operations Reported-by: syzbot Fixes: 90e33d459407 ("tun: enable napi_gro_frags() for TUN/TAP driver") Signed-off-by: Stanislav Fomichev Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- drivers/net/tun.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -859,10 +859,6 @@ static int tun_attach(struct tun_struct err = 0; } - rcu_assign_pointer(tfile->tun, tun); - rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); - tun->numqueues++; - if (tfile->detached) { tun_enable_queue(tfile); } else { @@ -876,6 +872,13 @@ static int tun_attach(struct tun_struct * refcnt. */ + /* Publish tfile->tun and tun->tfiles only after we've fully + * initialized tfile; otherwise we risk using half-initialized + * object. + */ + rcu_assign_pointer(tfile->tun, tun); + rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); + tun->numqueues++; out: return err; }