Received: by 10.192.165.156 with SMTP id m28csp74704imm; Tue, 10 Apr 2018 16:45:14 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+jm+Jja2Cz3v4pSfqyXo6CYJZazB88938QOvs7tp51IEiFans5Hf1mc0uohvqRL7T/+NO0 X-Received: by 2002:a17:902:4003:: with SMTP id b3-v6mr2560890pld.15.1523403914283; Tue, 10 Apr 2018 16:45:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523403914; cv=none; d=google.com; s=arc-20160816; b=oioUUgaAZb8VyFtvGhLSeXeHFrm7JHPXpISKHep6xBo1OvxKmYw5w7BU4qaNYxbr2g wd+pjRLz0uub51UZbguDgvYSelqUlj7+tCedx30GGcoTwiw0Oi03KdvP6/2R6nBhCQ5U NMLJVTYe9me7KcmF72kWH2DZhCKVEUmAlUWvEUeqOrwxdTq4N6cqu2t/Drwve5Sm1qWK ko68KlkzVIEjb8W1TGDyShr7Ytzzsr+uO4gdPn8diWyHRzO3mvm4yC3inNC/DjnPw0mL /euxAkN61PilHl0n3UeHcfEcmLg9tTs9LZDBncdtuRIvTCLT7uzhZXa8stJIqNEkr4Ki /HLw== 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=/mzRTBRI79PokizBSGJ+KkaCnnPiRDm24/Egy44fN/Y=; b=qMWTUq9GyWLnLiav/0JayN3YZVr+mTNlpMlmzOS7FXrqV/0Pc7GVUQGiuLRJRRf8i0 vngVQiwzxqpstTSejzIXD5wzon/8+tEYqLEIr0B01Tgmqcy+LkPcowxFhyLIUhVstz6V QF36j1L6F7TR2qPPKEHOb6uQOFnqh9XMYm9reWs3YTkKEMQ+CoBe+afQ1HYUN4OB8fUV MbyzL9flYuOync5tIRnp0jkwd2NR35kyoNZhbdxsRAmDEDRAcDeiLfqFKPVK7gZwqcFV iTedG725WFxq82us+6sPlDCElmYBZCB/XPoJSca0TYdSkQ1WJx3jcnoVZlV6Hwxo4gX9 SUfg== 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 r16si2444779pgr.560.2018.04.10.16.44.37; Tue, 10 Apr 2018 16:45:14 -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 S1754121AbeDJXkT (ORCPT + 99 others); Tue, 10 Apr 2018 19:40:19 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:37252 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753180AbeDJW0g (ORCPT ); Tue, 10 Apr 2018 18:26:36 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 9105BDC3; Tue, 10 Apr 2018 22:26:35 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, NeilBrown , Al Viro , Sasha Levin Subject: [PATCH 4.15 021/168] VFS: close race between getcwd() and d_move() Date: Wed, 11 Apr 2018 00:22:43 +0200 Message-Id: <20180410212801.104739278@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180410212800.144079021@linuxfoundation.org> References: <20180410212800.144079021@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.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: NeilBrown [ Upstream commit 61647823aa920e395afcce4b57c32afb51456cab ] d_move() will call __d_drop() and then __d_rehash() on the dentry being moved. This creates a small window when the dentry appears to be unhashed. Many tests of d_unhashed() are made under ->d_lock and so are safe from racing with this window, but some aren't. In particular, getcwd() calls d_unlinked() (which calls d_unhashed()) without d_lock protection, so it can race. This races has been seen in practice with lustre, which uses d_move() as part of name lookup. See: https://jira.hpdd.intel.com/browse/LU-9735 It could race with a regular rename(), and result in ENOENT instead of either the 'before' or 'after' name. The race can be demonstrated with a simple program which has two threads, one renaming a directory back and forth while another calls getcwd() within that directory: it should never fail, but does. See: https://patchwork.kernel.org/patch/9455345/ We could fix this race by taking d_lock and rechecking when d_unhashed() reports true. Alternately when can remove the window, which is the approach this patch takes. ___d_drop() is introduce which does *not* clear d_hash.pprev so the dentry still appears to be hashed. __d_drop() calls ___d_drop(), then clears d_hash.pprev. __d_move() now uses ___d_drop() and only clears d_hash.pprev when not rehashing. Signed-off-by: NeilBrown Signed-off-by: Al Viro Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- fs/dcache.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) --- a/fs/dcache.c +++ b/fs/dcache.c @@ -468,9 +468,11 @@ static void dentry_lru_add(struct dentry * d_drop() is used mainly for stuff that wants to invalidate a dentry for some * reason (NFS timeouts or autofs deletes). * - * __d_drop requires dentry->d_lock. + * __d_drop requires dentry->d_lock + * ___d_drop doesn't mark dentry as "unhashed" + * (dentry->d_hash.pprev will be LIST_POISON2, not NULL). */ -void __d_drop(struct dentry *dentry) +static void ___d_drop(struct dentry *dentry) { if (!d_unhashed(dentry)) { struct hlist_bl_head *b; @@ -486,12 +488,17 @@ void __d_drop(struct dentry *dentry) hlist_bl_lock(b); __hlist_bl_del(&dentry->d_hash); - dentry->d_hash.pprev = NULL; hlist_bl_unlock(b); /* After this call, in-progress rcu-walk path lookup will fail. */ write_seqcount_invalidate(&dentry->d_seq); } } + +void __d_drop(struct dentry *dentry) +{ + ___d_drop(dentry); + dentry->d_hash.pprev = NULL; +} EXPORT_SYMBOL(__d_drop); void d_drop(struct dentry *dentry) @@ -2386,7 +2393,7 @@ EXPORT_SYMBOL(d_delete); static void __d_rehash(struct dentry *entry) { struct hlist_bl_head *b = d_hash(entry->d_name.hash); - BUG_ON(!d_unhashed(entry)); + hlist_bl_lock(b); hlist_bl_add_head_rcu(&entry->d_hash, b); hlist_bl_unlock(b); @@ -2821,9 +2828,9 @@ static void __d_move(struct dentry *dent write_seqcount_begin_nested(&target->d_seq, DENTRY_D_LOCK_NESTED); /* unhash both */ - /* __d_drop does write_seqcount_barrier, but they're OK to nest. */ - __d_drop(dentry); - __d_drop(target); + /* ___d_drop does write_seqcount_barrier, but they're OK to nest. */ + ___d_drop(dentry); + ___d_drop(target); /* Switch the names.. */ if (exchange) @@ -2835,6 +2842,8 @@ static void __d_move(struct dentry *dent __d_rehash(dentry); if (exchange) __d_rehash(target); + else + target->d_hash.pprev = NULL; /* ... and switch them in the tree */ if (IS_ROOT(dentry)) {