Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp13926658pxu; Mon, 4 Jan 2021 08:09:21 -0800 (PST) X-Google-Smtp-Source: ABdhPJy4IZZQwaDFsuzmIeTWPlbUa8fbJHSo4NUFcBb2ICS7g2NLocsZdwey6kpIAzEbXVHIOj7k X-Received: by 2002:a17:907:20dc:: with SMTP id qq28mr66990928ejb.403.1609776561492; Mon, 04 Jan 2021 08:09:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609776561; cv=none; d=google.com; s=arc-20160816; b=eL/OYKGjdP6/l2JhoPQJQ833U4cKwHa8KaQwEgFBMsqxQ5cUQBwnJjeGYXFyJ5/Bb/ On7k8WIJdJXj8T1xeXcXZSQF8fhDTJwYPVXy6zYmAPPpoTob4mnotyKqKbf4cr+Pavtd NyDdpTP3s3Tmki7F4HHz25ykDo7kP8kDhhKvUde7JL/sQIBJZc2Ebu3TEnR6IOYLYFft YXvQ+E9biBm8tYYKZphk6V+CNfq5zBVCBcrgojLjL6g7HfmHNNJXdKQOa+1P3qRjbaRT Kb2qmJN2s8iyAV5uWAEFNa/4cSv+Pp9ND/0OZfG9vU9AFZMnLuf+JZ+i3Sf4Qczt9nR0 Arbw== 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=+b6uoL+hN9ZlY1rMN18xnZnEP7R2C3uux+1GpOnb+6o=; b=d6D64sFmn6/KO87IdXQsclw5zFzpdS9vnQIEZM9ef0ttli813EwO408BSEJr5c94Sf a1PFVrJ39lWdx6zJjENbFJrSqneGjwWRlf/U1QpPthN5/AJxf4GQ66KbfeltCbMiHaCi Hq16I/asyEM7RuYYEyQCN0R7ktANtwUL0mKMBfiugm+NpryF2Od68TBN5JI3Kyaqf9g3 0Zoi6SqHXueumsKd05F36jnbDMmE5dvjUq/AziQHEkPzA166zG/28tc6+8QW9B8WQbPO dOfUANeyCHeYdpsZFrzChRLoglyQmisUbCRtpcHltn4rGUz21MwII2YPCzN4GiGI/kiJ dbsw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=NfMCvvvt; 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 38si31982602edq.246.2021.01.04.08.08.58; Mon, 04 Jan 2021 08:09:21 -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=NfMCvvvt; 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 S1728761AbhADQF7 (ORCPT + 99 others); Mon, 4 Jan 2021 11:05:59 -0500 Received: from mail.kernel.org ([198.145.29.99]:40524 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729047AbhADQDM (ORCPT ); Mon, 4 Jan 2021 11:03:12 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 234BB22517; Mon, 4 Jan 2021 16:02:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1609776151; bh=CzRYXEk9ZbdYqqv0mBK0IJg95yy1SokTxylsOXRqXIw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NfMCvvvt1yIKXkVHh2XsTSbVJTvPAH6vbjIvG63LfjVNfGMPuSzd4wrp8o8awjxbP NY4fxI9O9JBIpu3ydlAR0ncYgELLqGcE8ii0UXDWbSriQo5EtO6mD4MuF6y8JQG7P4 UI/i2jzzYJREh2L1C9t6txBRgWikqRYve5PWUX38= 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.10 45/63] NFSv4: Fix a pNFS layout related use-after-free race when freeing the inode Date: Mon, 4 Jan 2021 16:57:38 +0100 Message-Id: <20210104155711.007242412@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210104155708.800470590@linuxfoundation.org> References: <20210104155708.800470590@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 93f5c1678ec29..984cc42ee54d8 100644 --- a/fs/nfs/nfs4super.c +++ b/fs/nfs/nfs4super.c @@ -67,7 +67,7 @@ static void nfs4_evict_inode(struct inode *inode) nfs_inode_evict_delegation(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); nfs4_xattr_cache_zap(inode); diff --git a/fs/nfs/pnfs.c b/fs/nfs/pnfs.c index 0e50b9d45c320..07f59dc8cb2e7 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); } } @@ -734,8 +739,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); @@ -753,9 +757,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 2661c44c62db4..78c3893918486 100644 --- a/fs/nfs/pnfs.h +++ b/fs/nfs/pnfs.h @@ -266,6 +266,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, @@ -710,6 +711,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