From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-sn1nam02on0102.outbound.protection.outlook.com ([104.47.36.102]:5120 "EHLO NAM02-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1032324AbeCAPem (ORCPT ); Thu, 1 Mar 2018 10:34:42 -0500 From: Sasha Levin To: "stable@vger.kernel.org" , "stable-commits@vger.kernel.org" CC: Cong Wang , Linus Torvalds , Sasha Levin Subject: [added to the 4.1 stable tree] nsfs: mark dentry with DCACHE_RCUACCESS Date: Thu, 1 Mar 2018 15:26:08 +0000 Message-ID: <20180301152116.1486-335-alexander.levin@microsoft.com> References: <20180301152116.1486-1-alexander.levin@microsoft.com> In-Reply-To: <20180301152116.1486-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Cong Wang This patch has been added to the 4.1 stable tree. If you have any objections, please let us know. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [ Upstream commit 073c516ff73557a8f7315066856c04b50383ac34 ] Andrey reported a use-after-free in __ns_get_path(): spin_lock include/linux/spinlock.h:299 [inline] lockref_get_not_dead+0x19/0x80 lib/lockref.c:179 __ns_get_path+0x197/0x860 fs/nsfs.c:66 open_related_ns+0xda/0x200 fs/nsfs.c:143 sock_ioctl+0x39d/0x440 net/socket.c:1001 vfs_ioctl fs/ioctl.c:45 [inline] do_vfs_ioctl+0x1bf/0x1780 fs/ioctl.c:685 SYSC_ioctl fs/ioctl.c:700 [inline] SyS_ioctl+0x8f/0xc0 fs/ioctl.c:691 We are under rcu read lock protection at that point: rcu_read_lock(); d =3D atomic_long_read(&ns->stashed); if (!d) goto slow; dentry =3D (struct dentry *)d; if (!lockref_get_not_dead(&dentry->d_lockref)) goto slow; rcu_read_unlock(); but don't use a proper RCU API on the free path, therefore a parallel __d_free() could free it at the same time. We need to mark the stashed dentry with DCACHE_RCUACCESS so that __d_free() will be called after all readers leave RCU. Fixes: e149ed2b805f ("take the targets of /proc/*/ns/* symlinks to separate= fs") Cc: Alexander Viro Cc: Andrew Morton Reported-by: Andrey Konovalov Signed-off-by: Cong Wang Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- fs/nsfs.c | 1 + 1 file changed, 1 insertion(+) diff --git a/fs/nsfs.c b/fs/nsfs.c index 99521e7c492b..845f29e15ac9 100644 --- a/fs/nsfs.c +++ b/fs/nsfs.c @@ -94,6 +94,7 @@ void *ns_get_path(struct path *path, struct task_struct *= task, return ERR_PTR(-ENOMEM); } d_instantiate(dentry, inode); + dentry->d_flags |=3D DCACHE_RCUACCESS; dentry->d_fsdata =3D (void *)ns_ops; d =3D atomic_long_cmpxchg(&ns->stashed, 0, (unsigned long)dentry); if (d) { --=20 2.14.1