Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp2579006imm; Thu, 16 Aug 2018 11:50:26 -0700 (PDT) X-Google-Smtp-Source: AA+uWPzeCn6IBWwSsl9skLQKLqqeBv9v+jY0WENtoUMsX012qFaCQVnQ81pw7VRljWx920H4jm6R X-Received: by 2002:a62:201b:: with SMTP id g27-v6mr33525555pfg.253.1534445426813; Thu, 16 Aug 2018 11:50:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534445426; cv=none; d=google.com; s=arc-20160816; b=KfhnRWoVjGPnz2DcxoczdJz4ReHGgixPbu0eVg1IL/JyWBXjgBiHUM4LRq33DdggZx NV6hcLTakDu3E/ozF4E6bh/Hr1GXTevyhkI1FHTmGyXAkUiD7KdQhlcFBbE3PeXSwvMV qJpNIEyLozHBtcm/muH6NdrwzmCcX4S0kzq6bI0joit5U8Ri/rRNG6RGNAXbHQcJ3BG/ 5aQ41CDnEauLpfSye/5G8rqMJEIdG8Mfr6PD/vxBiCwyDv3D3P909DjLz2wFy/GeGtVB 5Rzkzgn2pAQZFvysMWvnRqviIDH2s2zQ/d7lAUpGf71peFy2ptOqUz8UBaaLvF6Ei4UW b7ww== 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 :arc-authentication-results; bh=VgmZZe3XAogo6at+l2EreaQTkkPKTDYktcGdLl9YVc8=; b=pD/AnXj0cYGZ8uZwwZIo8Jbxngw0j4nFLJNCQsysUCCmTOTtEkQ9bULbB4YfmqTFsP eSzJu51Uo5dPWxyFE/LbnqKWAwnpM92HSFA7P6zJ5iYshByq9lTXo4yjPO3rC4PPVIqq f91HvwpfwHEauVSnezdtNnfyeezK91MOT7eT9Zv1fKccKkCyQ0kibqyvG3R5ULJATB5e BSlEEakMu+6IkQmzoQ5Z4F6PutOb6mqD3jUKWyJVzpCXlQxdZmt/fLvOZGF8OBDHi2lT QLnkMh5MWc5kShxa4IwPS22tRjNONS1BA/hPr+AXYXPql77XcT5MnM3Dg/wb5YFVSkCU 0Ipg== 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 k23-v6si20322pgl.633.2018.08.16.11.50.11; Thu, 16 Aug 2018 11:50:26 -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 S1728551AbeHPVmm (ORCPT + 99 others); Thu, 16 Aug 2018 17:42:42 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:56108 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728520AbeHPVml (ORCPT ); Thu, 16 Aug 2018 17:42:41 -0400 Received: from localhost (unknown [194.244.16.108]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 51610CE8; Thu, 16 Aug 2018 18:42:33 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Oleg Nesterov , Al Viro Subject: [PATCH 3.18 04/15] fix __legitimize_mnt()/mntput() race Date: Thu, 16 Aug 2018 20:41:41 +0200 Message-Id: <20180816171633.712842503@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180816171633.546734046@linuxfoundation.org> References: <20180816171633.546734046@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 3.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Al Viro commit 119e1ef80ecfe0d1deb6378d4ab41f5b71519de1 upstream. __legitimize_mnt() has two problems - one is that in case of success the check of mount_lock is not ordered wrt preceding increment of refcount, making it possible to have successful __legitimize_mnt() on one CPU just before the otherwise final mntpu() on another, with __legitimize_mnt() not seeing mntput() taking the lock and mntput() not seeing the increment done by __legitimize_mnt(). Solved by a pair of barriers. Another is that failure of __legitimize_mnt() on the second read_seqretry() leaves us with reference that'll need to be dropped by caller; however, if that races with final mntput() we can end up with caller dropping rcu_read_lock() and doing mntput() to release that reference - with the first mntput() having freed the damn thing just as rcu_read_lock() had been dropped. Solution: in "do mntput() yourself" failure case grab mount_lock, check if MNT_DOOMED has been set by racing final mntput() that has missed our increment and if it has - undo the increment and treat that as "failure, caller doesn't need to drop anything" case. It's not easy to hit - the final mntput() has to come right after the first read_seqretry() in __legitimize_mnt() *and* manage to miss the increment done by __legitimize_mnt() before the second read_seqretry() in there. The things that are almost impossible to hit on bare hardware are not impossible on SMP KVM, though... Reported-by: Oleg Nesterov Fixes: 48a066e72d97 ("RCU'd vsfmounts") Cc: stable@vger.kernel.org Signed-off-by: Al Viro Signed-off-by: Greg Kroah-Hartman --- fs/namespace.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) --- a/fs/namespace.c +++ b/fs/namespace.c @@ -590,12 +590,21 @@ bool legitimize_mnt(struct vfsmount *bas return true; mnt = real_mount(bastard); mnt_add_count(mnt, 1); + smp_mb(); // see mntput_no_expire() if (likely(!read_seqretry(&mount_lock, seq))) return true; if (bastard->mnt_flags & MNT_SYNC_UMOUNT) { mnt_add_count(mnt, -1); return false; } + lock_mount_hash(); + if (unlikely(bastard->mnt_flags & MNT_DOOMED)) { + mnt_add_count(mnt, -1); + unlock_mount_hash(); + return true; + } + unlock_mount_hash(); + rcu_read_unlock(); mntput(bastard); rcu_read_lock(); @@ -1064,6 +1073,11 @@ static void mntput_no_expire(struct moun return; } lock_mount_hash(); + /* + * make sure that if __legitimize_mnt() has not seen us grab + * mount_lock, we'll see their refcount increment here. + */ + smp_mb(); mnt_add_count(mnt, -1); if (mnt_get_count(mnt)) { rcu_read_unlock();