Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp1910177imn; Mon, 1 Aug 2022 04:50:09 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vNr+yGV2s0Cbd/PFie2CCiCzgNeEZ9KP0a8+kXHruWNUEtHECYTdA45z8lWR+RQGDk0xYz X-Received: by 2002:a17:907:1690:b0:72b:4f11:66c4 with SMTP id hc16-20020a170907169000b0072b4f1166c4mr11891436ejc.562.1659354609672; Mon, 01 Aug 2022 04:50:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659354609; cv=none; d=google.com; s=arc-20160816; b=f3aOIBaAid2EwSX/iWMlPDuGV/rUDceY+wn58VWDRi8XHchYIyRnC52EawxHTsQ4Hk Wp51pwuAy+faIXVYqJZI/+56N6ENEZCrClMh6Ib0Uzyr2Jb3Qe1pr9Sb1voZtXHzXV/H vM2ujYsa6A/7hk4rViZkJOhahez/SlzIgRpzazmmkngxNNrUIYH9kiel+rJ8fN8kpg2S 4T6NmRhUDL2E3nJ7AbqGO0kRQFuRbxdTHpNLQffYmK5G0X3k4zl1+NGIJg/iUcALyNZP Ou5bw81Qxoa+CaeymnR4IyRpbeL2ggC3F7PK+sbdB95p313B5NM+F7MM1Cqg1z0U2TbO Or8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent :content-transfer-encoding:references:in-reply-to:date:cc:to:from :subject:message-id:dkim-signature; bh=eoKxtSLv5VdvjcJ0F1hTmj1apeg9sAh37OGbNeIwTso=; b=x9WA60TBlw0Iz3VTZ2vXV+CGKYdYz5oyKSLDboaQaWsYOVVzxurO/vQNjd7/bQWCch 2EnOV6CDe/TVUhKQUceOj+o2K5iFfiIx4UBP5C0zte9W83JPP215H8VdmUX28n+qYnXu EIDHJqyqi6A4jsXTuGXoBrogJkkHBtoN4wT75O1vWFftU1iicMCaQrOJ9jlusmWi5LAj 9Kzx9PSLXMuSY0tn1/lEAWQVdI/4JrvxkL8c9YiPRBK/p6MUDGJMp1rCDpbCatg5FUo0 +2O/9l+65O3EIjlWKkg/UoCOO2vKVVIpvVgx7e2Y1K5o5gEvKXRxy7Gsxc1ypu1s7QH1 CZNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=fMU2cIcl; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g10-20020a056402090a00b0043d0fd7247bsi10586885edz.48.2022.08.01.04.49.17; Mon, 01 Aug 2022 04:50:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=fMU2cIcl; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230238AbiHALr0 (ORCPT + 99 others); Mon, 1 Aug 2022 07:47:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229892AbiHALrZ (ORCPT ); Mon, 1 Aug 2022 07:47:25 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5B64357E3 for ; Mon, 1 Aug 2022 04:47:24 -0700 (PDT) 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 dfw.source.kernel.org (Postfix) with ESMTPS id 3DA84612C6 for ; Mon, 1 Aug 2022 11:47:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2A1F6C433C1; Mon, 1 Aug 2022 11:47:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1659354443; bh=LIGL9weSYUePxvV8yw1LKvfBLFSQ2jw7uu6eXTUtDpo=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=fMU2cIclrW+mGEJ+Ty8Vss7RHWzfHQy4jDxttypR9ucvMVdK/dj+jEfe2pVzW4rZF 3gyjpJNQurPIAYy/KTS6jqpjCk+PL6PsYKP5uaiLfT4I22OEMD2tqALhDNsw3Z6qYe LOSJjVCs7l4bAoxiGBU4QIjU9YOAjEEnTSU0XdVcq5zSIvQ3ORDOdN15am7KwjOufb m/iC0x49jbsSLT9l8rYCm6R/uI3QTzbF4uoIEuLt1asZoIp1Pv8caUcX1y6f881Z3R 9kJQCywcivexfxQn9v17QR7kMcJonWdYWZMHGNb4DMte3gqiLP5FnYk5uUidwjZPRv Ky5ICzrAScVNA== Message-ID: Subject: Re: [PATCH] NFSD: fix use-after-free on source server when doing inter-server copy From: Jeff Layton To: Dai Ngo , chuck.lever@oracle.com, olga.kornievskaia@gmail.com Cc: linux-nfs@vger.kernel.org Date: Mon, 01 Aug 2022 07:47:21 -0400 In-Reply-To: <1659298792-5735-1-git-send-email-dai.ngo@oracle.com> References: <1659298792-5735-1-git-send-email-dai.ngo@oracle.com> Content-Type: text/plain; charset="ISO-8859-15" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.44.3 (3.44.3-1.fc36) MIME-Version: 1.0 X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org On Sun, 2022-07-31 at 13:19 -0700, Dai Ngo wrote: > Use-after-free occurred when the laundromat tried to free expired > cpntf_state entry on the s2s_cp_stateids list after inter-server > copy completed. The sc_cp_list that the expired copy state was > inserted on was already freed. >=20 > When COPY completes, the Linux client normally sends LOCKU(lock_state x), > FREE_STATEID(lock_state x) and CLOSE(open_state y) to the source server. > The nfs4_put_stid call from nfsd4_free_stateid cleans up the copy state > from the s2s_cp_stateids list before freeing the lock state's stid. >=20 > However, sometimes the CLOSE was sent before the FREE_STATEID request. > When this happens, the nfsd4_close_open_stateid call from nfsd4_close > frees all lock states on its st_locks list without cleaning up the copy > state on the sc_cp_list list. When the time the FREE_STATEID arrives the > server returns BAD_STATEID since the lock state was freed. This causes > the use-after-free error to occur when the laundromat tries to free > the expired cpntf_state. >=20 > This patch adds a call to nfs4_free_cpntf_statelist in > nfsd4_close_open_stateid to clean up the copy state before calling > free_ol_stateid_reaplist to free the lock state's stid on the reaplist. >=20 > Signed-off-by: Dai Ngo > --- > fs/nfsd/nfs4state.c | 3 +++ > 1 file changed, 3 insertions(+) >=20 > diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c > index 9409a0dc1b76..749f51dff5c7 100644 > --- a/fs/nfsd/nfs4state.c > +++ b/fs/nfsd/nfs4state.c > @@ -6608,6 +6608,7 @@ static void nfsd4_close_open_stateid(struct nfs4_ol= _stateid *s) > struct nfs4_client *clp =3D s->st_stid.sc_client; > bool unhashed; > LIST_HEAD(reaplist); > + struct nfs4_ol_stateid *stp; > =20 > spin_lock(&clp->cl_lock); > unhashed =3D unhash_open_stateid(s, &reaplist); > @@ -6616,6 +6617,8 @@ static void nfsd4_close_open_stateid(struct nfs4_ol= _stateid *s) > if (unhashed) > put_ol_stateid_locked(s, &reaplist); > spin_unlock(&clp->cl_lock); > + list_for_each_entry(stp, &reaplist, st_locks) > + nfs4_free_cpntf_statelist(clp->net, &stp->st_stid); > free_ol_stateid_reaplist(&reaplist); > } else { > spin_unlock(&clp->cl_lock); Nice catch. There are a number of places that call free_ol_stateid_reaplist. Is it really only in nfsd4_close_open_stateid that we need to do this? I wonder if it would be better to do this inside free_ol_stateid_reaplist instead so that all of those callers clean up the copy states as well? --=20 Jeff Layton