From patchwork Mon Oct 24 11:31:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 9235 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp461105wru; Mon, 24 Oct 2022 06:43:11 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5e6P9i5mYtjA+VtQxyYDDZhXY9WpWb+xzIJtFtL+OE97w++kDGu3SjrVvx05Ho4IzqhuR1 X-Received: by 2002:a17:907:7610:b0:79e:da2d:c126 with SMTP id jx16-20020a170907761000b0079eda2dc126mr12599000ejc.27.1666618990836; Mon, 24 Oct 2022 06:43:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666618990; cv=none; d=google.com; s=arc-20160816; b=nML25DwA1p2zLenfFIKYcBl7+NmrDJVOPYqga9ii7EjHcvJ4AR103aEYkxLJj8Gy/U pT0F8SH0WFPBtZgvpr6m9PyT0uiUDGXdfHZLIJW7WKwCSEPaWQPFsXZwxYllu4uVBba+ bNpGQ2Hy9W4FYs/09KOVr8Owq9N9uTgt2md/9a1Ieczyc1i0xtPMB8x8X/scvZNBWdSl qH3OMOViyjBylsK01iDSN7h8kBAF6yLgMuUOJ3tOi6feBHafew6fngo5f+btgkKzrHKJ 4dfc9Vj6DOomhdYM9xk6F4wtgaYwlL58mqNg95S3PubzQjodIdUBihD3pF29BV2OeEwn NsTg== 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=s3VH7kNH99YVf3YGlEA5l7GRK45nnQOJv/W2ci+Ef+c=; b=AYlNj7OSrmLXgEFCRwUqcG9knkhAReDKfGAql0CVrotxhHQC5C6sUUpKyqDa/i6Lhg PPZxH/VS5V/Oz8gWqfXoDjy4j4y72mKmu4rK3xnP9LXCat/VnYoCT/4wmvZwmKhVP/fh eUr0IVrOsuagUWwfGVPEtAAmHG9lVtcxkKePpg5Y7wmes+kGtb5q7PXc25nEWoLBFCJr 7ezCzJoqOY8LtLpM/bq+xy5K00jy8hee4Mp0SdY5wComPf6xGtCQm3WjZwZFpZ2XCSnX YDO7Sc6Rfd/n5Yq1JWYzRfYIF43cyb2D7GLm82bN7VE1WjzQfuSMUruCYuxQjNmqkV3a +giw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=YfpsV6P8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b8-20020a0564021f0800b00461b0566a29si3719033edb.137.2022.10.24.06.42.42; Mon, 24 Oct 2022 06:43:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=@linuxfoundation.org header.s=korg header.b=YfpsV6P8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S230107AbiJXNdP (ORCPT + 99 others); Mon, 24 Oct 2022 09:33:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50720 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236068AbiJXN3c (ORCPT ); Mon, 24 Oct 2022 09:29:32 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83816ABF30; Mon, 24 Oct 2022 05:32:40 -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 C609761314; Mon, 24 Oct 2022 12:29:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D7973C433D6; Mon, 24 Oct 2022 12:29:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666614552; bh=8kvMFNiK8S3+BVnSHaJK1OdWmE6iurGgh/4op3yCUVY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YfpsV6P8o9gMeO6BM/g1AVNQwR3AVDDNosf8HaduyyjCOBU8R27caUpwFZXgX7Vyj IuhvKC6ItuDZ4c2mhMB7KKt2aZHyrXDC+kB64yrk7pXDbUcKfQK4BzXYs4DftUsKfA 1KbsxA9DIYIPtDD8SU5ypAfaywJydAefrdUjqfpg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dai Ngo , Chuck Lever , Sasha Levin Subject: [PATCH 5.10 304/390] NFSD: fix use-after-free on source server when doing inter-server copy Date: Mon, 24 Oct 2022 13:31:41 +0200 Message-Id: <20221024113035.948291024@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024113022.510008560@linuxfoundation.org> References: <20221024113022.510008560@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.6 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-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747576674694790541?= X-GMAIL-MSGID: =?utf-8?q?1747576674694790541?= From: Dai Ngo [ Upstream commit 019805fea91599b22dfa62ffb29c022f35abeb06 ] 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. 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. 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. 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. Signed-off-by: Dai Ngo Signed-off-by: Chuck Lever Signed-off-by: Sasha Levin --- fs/nfsd/nfs4state.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c index f1b503bec222..665d0eaeb8db 100644 --- a/fs/nfsd/nfs4state.c +++ b/fs/nfsd/nfs4state.c @@ -843,6 +843,7 @@ static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp) static void nfs4_free_deleg(struct nfs4_stid *stid) { + WARN_ON(!list_empty(&stid->sc_cp_list)); kmem_cache_free(deleg_slab, stid); atomic_long_dec(&num_delegations); } @@ -1358,6 +1359,7 @@ static void nfs4_free_ol_stateid(struct nfs4_stid *stid) release_all_access(stp); if (stp->st_stateowner) nfs4_put_stateowner(stp->st_stateowner); + WARN_ON(!list_empty(&stid->sc_cp_list)); kmem_cache_free(stateid_slab, stid); } @@ -6207,6 +6209,7 @@ static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) struct nfs4_client *clp = s->st_stid.sc_client; bool unhashed; LIST_HEAD(reaplist); + struct nfs4_ol_stateid *stp; spin_lock(&clp->cl_lock); unhashed = unhash_open_stateid(s, &reaplist); @@ -6215,6 +6218,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);