Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp209992rwb; Mon, 26 Sep 2022 11:13:17 -0700 (PDT) X-Google-Smtp-Source: AMsMyM70uIn5R9Bbb+7GwIhTRtx0jFOkBx+R3dOFga+Dxn7vOt+ik+knV55/cBULgtFZCKEEzULR X-Received: by 2002:a17:903:41cb:b0:178:36c2:a98 with SMTP id u11-20020a17090341cb00b0017836c20a98mr22793920ple.47.1664215996996; Mon, 26 Sep 2022 11:13:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664215996; cv=none; d=google.com; s=arc-20160816; b=ls99MnWLSb7/FvkFAEhvWJ9uhAh+PAaN+BaigdgiBCYGvQx7534oxq3jo6tIKHrtsb i37XnBhG8QMOxORXzB4gKN9YexU6etE8IUsZRsJ3KagvrFsrMyJx4BxvneaKYoff2Nsl v9L8CFCJ/tQ9myrYzXUQ+/nzRbAhuodKc7HpsjiR+RL6GrVOs0pOuJ5I1AFDkrwoes80 zpb073ixrUKKLs5TaGuP2k4Q4qQ1chyTG9PFUvTRg1EUg0By5mAVUQDHiDzJLs6totfk 7/ktKkNdQO7tFTUw8/ovqE1z4EJH1uuNpcml+jcWtoQu4frv3jljvJs3sKA4A3tzz+Du ND3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=OJ6FXKDeY8XFFOgyN71A5MdgbMslzsXe5eDp7hMoh6M=; b=0c1rsKTpHx0vBXGmHHpA5lQqZ+T1A2rJxAV0TweNM64cUaqIjaICMd4qe3q6oHrDK4 clVt/EQNer5AdwQtAj9bU6CXugqpuNBYDvwPqZWfE8wVmx9y58NtDoGIDLm9vJbt25zw g4/s+a+zwcx2/h9V+TmPFwI2NyufDz8OiwYGn6CQcrNL+BcfVGKUYESNJAIn/2ik6oKn TLDs6puzS11kkmvZ7crQf8fbn3shExC52sZk2tWBBKZItUt5v7UvZ6miTm/bfYB+CZtT IJVOOo6pe8Yg1FDJZkaxknrfgqcBWIX01Fcmi4RKfpREfednLKPpN18sIZQQiQGhPEVp runA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2022-7-12 header.b=ZDsNB4Vh; 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=oracle.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ip7-20020a17090b314700b00202bb6e0923si14011339pjb.155.2022.09.26.11.12.57; Mon, 26 Sep 2022 11:13:16 -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=@oracle.com header.s=corp-2022-7-12 header.b=ZDsNB4Vh; 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=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229494AbiIZSMU (ORCPT + 99 others); Mon, 26 Sep 2022 14:12:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229734AbiIZSMG (ORCPT ); Mon, 26 Sep 2022 14:12:06 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 131AC5720D for ; Mon, 26 Sep 2022 10:59:26 -0700 (PDT) Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 28QHY0qh010319; Mon, 26 Sep 2022 17:59:23 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2022-7-12; bh=OJ6FXKDeY8XFFOgyN71A5MdgbMslzsXe5eDp7hMoh6M=; b=ZDsNB4VhgnEoFJcSzhlVHtqrsOeiWngpKIuwkfA67+YLYB7Z9Ynj0tJ1PDcPPapxoEkp Q3tmc/TVypCvECjzjDXSoIQYJgpm/4DAN2Suwl3qWmP+HIoxb+/BAH+XudnOP65hV5N0 ihA9DOqQLJW7ftybTvFFul9GYcAgpoJ9bZoF3J8hVyXHi/feJg10E+pysvfCqZoh6E1l gNylrDtKHzSgdm1pn74vNl12ukaSZWfWf9cwMTQB7yzeMijVROZxDsjYZ+jImcXu9HxL 4IMq8vz6r8ZyjOTHdwOVFbPqQDaLqaEMWtRCMBYXd8qaDZq9kt/JDEsm9FqRW8pc0neO dw== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3jst13cexy-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 26 Sep 2022 17:59:23 +0000 Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 28QHBbud002697; Mon, 26 Sep 2022 17:59:22 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3jtprtfq7a-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 26 Sep 2022 17:59:22 +0000 Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 28QHxLkW037565; Mon, 26 Sep 2022 17:59:21 GMT Received: from ca-common-hq.us.oracle.com (ca-common-hq.us.oracle.com [10.211.9.209]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 3jtprtfq6x-2; Mon, 26 Sep 2022 17:59:21 +0000 From: Dai Ngo To: chuck.lever@oracle.com, jlayton@kernel.org Cc: linux-nfs@vger.kernel.org Subject: [PATCH v3 1/1] NFSD: fix use-after-free on source server when doing inter-server copy Date: Mon, 26 Sep 2022 10:59:16 -0700 Message-Id: <1664215156-9970-2-git-send-email-dai.ngo@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1664215156-9970-1-git-send-email-dai.ngo@oracle.com> References: <1664215156-9970-1-git-send-email-dai.ngo@oracle.com> X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.528,FMLib:17.11.122.1 definitions=2022-09-26_09,2022-09-22_02,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 adultscore=0 suspectscore=0 phishscore=0 mlxscore=0 mlxlogscore=999 spamscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2209130000 definitions=main-2209260112 X-Proofpoint-ORIG-GUID: Solw0x2VGD1ghS8so3AeeXkGeFTV0wpS X-Proofpoint-GUID: Solw0x2VGD1ghS8so3AeeXkGeFTV0wpS X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE 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 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 --- fs/nfsd/nfs4state.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c index c5d199d7e6b4..0bc36472f8b7 100644 --- a/fs/nfsd/nfs4state.c +++ b/fs/nfsd/nfs4state.c @@ -1049,6 +1049,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); } @@ -1462,6 +1463,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); } @@ -6684,6 +6686,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); @@ -6692,6 +6695,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); -- 2.9.5