Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp13923847pxu; Mon, 4 Jan 2021 08:05:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJwbA8ymN/B0FaGpq82trEhGlE5mJJfMNn2/6fxUALvebBJda6mHALYIiujIKKSArUa5v9a1 X-Received: by 2002:a17:906:780c:: with SMTP id u12mr41222593ejm.125.1609776335159; Mon, 04 Jan 2021 08:05:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609776335; cv=none; d=google.com; s=arc-20160816; b=NOxCfxQvSgDB3naeaKs6sULv+uRwcbi+i7Y9EpjjDIv+LPX5sGmz8S3IkpzEBSmKVn +nwrw6AEOoN/OOAUReVOPxe2Eq26t7kb69dp23aiBemEfK/Xm9vZ10LS+fCEwZslcddm OtVyEyJZlc2jLeVoNRTG7wegw7j0/j8Kcs19LR17mc1RzjJfi1BOyT5aHQpGILDw/to6 DI1yZZsWx0zXP15U9pft08VHTOQNYbXnE0vuqE+ynqo7VjQuWcw7f9f2jPePxRY1ebH0 G4pjDnvFklYQzdPFnP4blLhhAod+CcKIQANxGYXXg4yMShhRO0scrLAVPaGX2RqrWgSF q5vA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=B61bm+vLF+OnxfF8rqqxtRrjAcUiKfDYfhYwNbIJrwE=; b=RcOINkA9/Na1kSAwICyF6O4HpWziSx8MCo/OuUImIq1a5eKfwOWfF9qXH8tJMOCmtf ycUZ+f8xd+EmNVugW3nHLY9SGF9LqwFb0jSL9IW70rAR2Bh+BxoJRwZdyJclJl7DTTUd rV+5ey00RsawYzPKTURda0hCr4HV+f1zSEK0HfSRGg+Ub8PCxs5SoUYhbcUgwgBZ2qhC m3sSP8Ps8THJy0Bu3fxeaNindmRfY9EBwdSVi8wmv4S/AifntZSzMceIUqja6msczp9b rIKlgBVJ4xGBkMpN/TUAJlvG30ASiCGf5OhOk69HLFEGd+CObbf90QfQvjj2I/rNK37O q6HQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=BoJp6xoz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ot14si28582121ejb.493.2021.01.04.08.05.11; Mon, 04 Jan 2021 08:05:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=BoJp6xoz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728548AbhADQBS (ORCPT + 99 others); Mon, 4 Jan 2021 11:01:18 -0500 Received: from mail.kernel.org ([198.145.29.99]:38446 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728531AbhADQBR (ORCPT ); Mon, 4 Jan 2021 11:01:17 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 99E26224DF; Mon, 4 Jan 2021 16:00:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1609776036; bh=sMyrdGaSkUI7HSmRU9MW0autRFm3hR4EuYecnD49Y8U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BoJp6xozDJbbOcvAMBVfO7aeQydLyigcbUM5I721t70mUAwB47fUWezCp+i2vH4Hc eSVHErjflDNlfv2cFMuiv1X4ItsYPr9MuFEZppYK0N5mdqr6BYwC1K3+6YkZrQFK61 pGon30ew9Xr1ARla2uJbA9+Jv+O4sYxZaXm3bsos= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Trond Myklebust , Sasha Levin Subject: [PATCH 5.4 40/47] NFSv4: Fix a pNFS layout related use-after-free race when freeing the inode Date: Mon, 4 Jan 2021 16:57:39 +0100 Message-Id: <20210104155707.667059998@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210104155705.740576914@linuxfoundation.org> References: <20210104155705.740576914@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Trond Myklebust [ Upstream commit b6d49ecd1081740b6e632366428b960461f8158b ] When returning the layout in nfs4_evict_inode(), we need to ensure that the layout is actually done being freed before we can proceed to free the inode itself. Signed-off-by: Trond Myklebust Signed-off-by: Sasha Levin --- fs/nfs/nfs4super.c | 2 +- fs/nfs/pnfs.c | 33 +++++++++++++++++++++++++++++++-- fs/nfs/pnfs.h | 5 +++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/fs/nfs/nfs4super.c b/fs/nfs/nfs4super.c index 04c57066a11af..b90642b022eb9 100644 --- a/fs/nfs/nfs4super.c +++ b/fs/nfs/nfs4super.c @@ -96,7 +96,7 @@ static void nfs4_evict_inode(struct inode *inode) nfs_inode_return_delegation_noreclaim(inode); /* Note that above delegreturn would trigger pnfs return-on-close */ pnfs_return_layout(inode); - pnfs_destroy_layout(NFS_I(inode)); + pnfs_destroy_layout_final(NFS_I(inode)); /* First call standard NFS clear_inode() code */ nfs_clear_inode(inode); } diff --git a/fs/nfs/pnfs.c b/fs/nfs/pnfs.c index 9c2b07ce57b27..9fd115c4d0a2f 100644 --- a/fs/nfs/pnfs.c +++ b/fs/nfs/pnfs.c @@ -294,6 +294,7 @@ void pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo) { struct inode *inode; + unsigned long i_state; if (!lo) return; @@ -304,8 +305,12 @@ pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo) if (!list_empty(&lo->plh_segs)) WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n"); pnfs_detach_layout_hdr(lo); + i_state = inode->i_state; spin_unlock(&inode->i_lock); pnfs_free_layout_hdr(lo); + /* Notify pnfs_destroy_layout_final() that we're done */ + if (i_state & (I_FREEING | I_CLEAR)) + wake_up_var(lo); } } @@ -723,8 +728,7 @@ pnfs_free_lseg_list(struct list_head *free_me) } } -void -pnfs_destroy_layout(struct nfs_inode *nfsi) +static struct pnfs_layout_hdr *__pnfs_destroy_layout(struct nfs_inode *nfsi) { struct pnfs_layout_hdr *lo; LIST_HEAD(tmp_list); @@ -742,9 +746,34 @@ pnfs_destroy_layout(struct nfs_inode *nfsi) pnfs_put_layout_hdr(lo); } else spin_unlock(&nfsi->vfs_inode.i_lock); + return lo; +} + +void pnfs_destroy_layout(struct nfs_inode *nfsi) +{ + __pnfs_destroy_layout(nfsi); } EXPORT_SYMBOL_GPL(pnfs_destroy_layout); +static bool pnfs_layout_removed(struct nfs_inode *nfsi, + struct pnfs_layout_hdr *lo) +{ + bool ret; + + spin_lock(&nfsi->vfs_inode.i_lock); + ret = nfsi->layout != lo; + spin_unlock(&nfsi->vfs_inode.i_lock); + return ret; +} + +void pnfs_destroy_layout_final(struct nfs_inode *nfsi) +{ + struct pnfs_layout_hdr *lo = __pnfs_destroy_layout(nfsi); + + if (lo) + wait_var_event(lo, pnfs_layout_removed(nfsi, lo)); +} + static bool pnfs_layout_add_bulk_destroy_list(struct inode *inode, struct list_head *layout_list) diff --git a/fs/nfs/pnfs.h b/fs/nfs/pnfs.h index f8a38065c7e47..63da33a92d831 100644 --- a/fs/nfs/pnfs.h +++ b/fs/nfs/pnfs.h @@ -255,6 +255,7 @@ struct pnfs_layout_segment *pnfs_layout_process(struct nfs4_layoutget *lgp); void pnfs_layoutget_free(struct nfs4_layoutget *lgp); void pnfs_free_lseg_list(struct list_head *tmp_list); void pnfs_destroy_layout(struct nfs_inode *); +void pnfs_destroy_layout_final(struct nfs_inode *); void pnfs_destroy_all_layouts(struct nfs_client *); int pnfs_destroy_layouts_byfsid(struct nfs_client *clp, struct nfs_fsid *fsid, @@ -651,6 +652,10 @@ static inline void pnfs_destroy_layout(struct nfs_inode *nfsi) { } +static inline void pnfs_destroy_layout_final(struct nfs_inode *nfsi) +{ +} + static inline struct pnfs_layout_segment * pnfs_get_lseg(struct pnfs_layout_segment *lseg) { -- 2.27.0