From patchwork Fri Jan 6 15:33:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 13091468 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6DC4FC4708D for ; Fri, 6 Jan 2023 15:33:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229701AbjAFPd5 (ORCPT ); Fri, 6 Jan 2023 10:33:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235292AbjAFPd4 (ORCPT ); Fri, 6 Jan 2023 10:33:56 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C684E37246 for ; Fri, 6 Jan 2023 07:33:53 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 545BFB81D9D for ; Fri, 6 Jan 2023 15:33:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9B478C433D2; Fri, 6 Jan 2023 15:33:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1673019231; bh=TZ7dYTm1ZkNokXZQm4eQ91frJCTyhCCWUu82dIXD4Os=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CEltLyLQBGRdaqb2jHfGXzbgTMDnh2nJrSZaQAHCwcUZl8YRh4ckHc7I3VqQ8RQBT HgVRzNe5K45asu3hOeN4SpnjDn0FH/WLXqDT7D2pyDhd6a0ecoBDcxkj/IL+05wh+O V3A7Fk689R1A33Gkfe+L6+4zli4KDjStaKxLeKVtzAt45cYJnWcKOODv2gIdeT9OFM R1p/CglSUuK+wCr40AxMPvax8aEgTHRJ8fLlTxoBk9toRHE+dP0vEWd4tysP94JKEv RGodDQOOu/O0RPb29nxUqFfyzC6g4kiamcciMbOTyunMSKWxSo+TE+hNEnrX1RJq6J r1yf13IgVZKcw== From: Jeff Layton To: chuck.lever@oracle.com Cc: linux-nfs@vger.kernel.org, NeilBrown Subject: [PATCH v2 2/2] nfsd: fix potential race in nfs4_find_file Date: Fri, 6 Jan 2023 10:33:48 -0500 Message-Id: <20230106153348.104214-2-jlayton@kernel.org> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230106153348.104214-1-jlayton@kernel.org> References: <20230106153348.104214-1-jlayton@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org The WARN_ON_ONCE check is not terribly useful. It also seems possible for nfs4_find_file to race with the destruction of an fi_deleg_file while trying to take a reference to it. Now that it's safe to pass nfs_get_file a NULL pointer, remove the WARN and NULL pointer check. Take the fi_lock when fetching fi_deleg_file. Cc: NeilBrown Signed-off-by: Jeff Layton --- fs/nfsd/nfs4state.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c index 655fcfec0ace..f923bab09a31 100644 --- a/fs/nfsd/nfs4state.c +++ b/fs/nfsd/nfs4state.c @@ -6415,23 +6415,26 @@ nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, static struct nfsd_file * nfs4_find_file(struct nfs4_stid *s, int flags) { + struct nfsd_file *ret = NULL; + if (!s) return NULL; switch (s->sc_type) { case NFS4_DELEG_STID: - if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file)) - return NULL; - return nfsd_file_get(s->sc_file->fi_deleg_file); + spin_lock(&s->sc_file->fi_lock); + ret = nfsd_file_get(s->sc_file->fi_deleg_file); + spin_unlock(&s->sc_file->fi_lock); + break; case NFS4_OPEN_STID: case NFS4_LOCK_STID: if (flags & RD_STATE) - return find_readable_file(s->sc_file); + ret = find_readable_file(s->sc_file); else - return find_writeable_file(s->sc_file); + ret = find_writeable_file(s->sc_file); } - return NULL; + return ret; } static __be32