Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp281655rdg; Thu, 12 Oct 2023 05:45:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGkP+m3kmvO68aOp3GP3rDHHXYwXcYg4DPeNeYEy53lJ6PWgRvxxnAtHIyMWe7caeLu94wk X-Received: by 2002:a05:6870:5ba0:b0:1be:f8d9:7bdd with SMTP id em32-20020a0568705ba000b001bef8d97bddmr29651564oab.6.1697114728620; Thu, 12 Oct 2023 05:45:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697114728; cv=none; d=google.com; s=arc-20160816; b=H0McVEWrSsX74bmAlmtP1bIkIv2XfqIOwrxhQTitN0ABgwlfTAXEqyHLTZvjz/1req ievRlvx3CE49B8XZPlNXA3KvgxMAF8W4GKpaecU3lHfUrleBM1QQhVojtopo2ZyhuuUb en+vqxoUJl5gjs/MpsfuitnUUK0qw/wdf3ym8s4YMgOKD4rVJ4pNEujEP7rC5UBTMCyf +dD1uILm1Pk5V+G9W1EHRbe8vGle77BUFUHrRhHM9U/PlMwUqcxes80EUFb4lsjW5rjD HxdZHbkXNng8vcRnnDk7ovMQKO7kQ8NeXcVEPh6SQg6eLfIWIMAnjYU+mgFr73r/afB7 fPog== 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=3CxX1rQ3v33pEn/t6LCy0tQMUXc2LG3chFTfZdkjAWw=; fh=BVgP9LVHPm7aOgHXXeIQHtv2bkWyQGsFDtzj0KCUEgE=; b=yQQTtXD5TWWrC9Z9hoFINU0necVGEFDr/uWf05zj7YfSLEcbh6uLgOJs6oeJyQuE5K wmGlbtBVPn9dz87o12RHbTXJMRqw0rpV3QNKNmVSeSOaG/WWDUyKFSEBEIHr5DFkTzE9 Tcw1/zhdEZFA+F1kUo72ccKZd3SQ6Qf+xPv1mCYCpXr2t9sVhgr+tKf3C1WAe/cM8Sw9 JkyDgsVZZISvhpeLdk03TMmx64K0u3ciU6F53NT+h1SaVo+80Rv+l8FDM0UoNcqSQUyA slBuz+Bw/Tyi3a7URj1srjv4nq14Nba2PTnLSYNnEGmyUAp6NZ1aMYwgdLzVQlQ50nsp FAXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="j7v0/bwj"; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id z14-20020a630a4e000000b005859c1e41a0si2243768pgk.201.2023.10.12.05.45.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Oct 2023 05:45:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="j7v0/bwj"; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 7D3AD80A3673; Thu, 12 Oct 2023 05:44:59 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235709AbjJLMo6 (ORCPT + 99 others); Thu, 12 Oct 2023 08:44:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37466 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233496AbjJLMo6 (ORCPT ); Thu, 12 Oct 2023 08:44:58 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96BD891 for ; Thu, 12 Oct 2023 05:44:55 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 650A1C433C7; Thu, 12 Oct 2023 12:44:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1697114695; bh=hYikDbKIFlA5SYALnivWpkSNVZhOw55S9ve0U/TrItM=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=j7v0/bwjXtfwpgUKm9dX4SrAjwUIjey2Owi7+vr8bOQmPpmr4ZDK02mxcn/P+d1KA 9CUoKEs5pmdLNgg3AlY7dY+fcTuZ3O5bUXaM+D9+OPwhYskObZpu/LyUjEEGNU8c3B ln1qHBwkYMI9WPWU3Vfa/6MlCVDqtr+9INTsx9dQNN7WR0jVfT8eHhBNUj8p0+ax/m sTfSghSfFkv1FKSHRhK5XBtjqlGbxb+uRH9Z25Nftikwo73fUur5QnD7Z9yrGpTig9 2zlTV8YtbpstiB2tDC0jS3NOJDgOPw8vbPtJBSdKRyI5XPyHTJWLf6qmI3QZKi7+mf uwqWnCHJDSbkw== Message-ID: <00b0e6b1963cd1a620e38c493e1d14871d83e151.camel@kernel.org> Subject: Re: [PATCH v1] NFSD: clean up alloc_init_deleg() From: Jeff Layton To: =?UTF-8?Q?=E9=BB=84=E6=80=9D=E8=81=AA?= Cc: chuck.lever@oracle.com, neilb@suse.de, kolga@netapp.com, Dai.Ngo@oracle.com, tom@talpey.com, linux-nfs@vger.kernel.org Date: Thu, 12 Oct 2023 08:44:53 -0400 In-Reply-To: <280c4ab8.22ed.18b230651e6.Coremail.huangsicong@iie.ac.cn> References: <49ad6b84.57cc.18b1de7572b.Coremail.huangsicong@iie.ac.cn> <168b769e12553d9a5974943f523de2f8b903d61b.camel@kernel.org> <280c4ab8.22ed.18b230651e6.Coremail.huangsicong@iie.ac.cn> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.48.4 (3.48.4-1.module_f38+17164+63eeee4a) MIME-Version: 1.0 X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Thu, 12 Oct 2023 05:44:59 -0700 (PDT) On Thu, 2023-10-12 at 16:34 +0800, =E9=BB=84=E6=80=9D=E8=81=AA wrote: > > On Wed, 2023-10-11 at 16:43 +0800, =E9=BB=84=E6=80=9D=E8=81=AA wrote= : > > > Pointer dereference error may occur in "alloc_init_deleg" funct= ion. > > >=20 > > > The "alloc_init_deleg" function located in "fs/nfsd/nfs4state.c= " may occur a pointer dereference error when it calls the function "nfs4_al= loc_stid" located in the same kernel file. The "nfs4_alloc_stid" function w= ill call the "kmem_cache_zalloc" function to allocate enough memory for sto= ring the "stid" variable. If there are significant memory fragmentation iss= ues, insufficient free memory blocks, or internal errors in the allocation = function, the "kmem_cache_zalloc" function will return NULL. Then the "nfs4= _alloc_stid" function will return NULL to the "alloc_init_deleg" function. = Finally, the "alloc_init_deleg" function will execute the following instruc= tions. > > > dp =3D delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_= deleg));   > > > if (dp =3D=3D NULL)   > > >         goto out_dec; > > > dp->dl_stid.sc_stateid.si_generation =3D 1; > > >=20 > > > The "delegstateid" function is defined as below: > > > static inline struct nfs4_delegation *delegstateid(struct nfs4_= stid *s)   > > > {   > > >         return container_of(s, struct nfs4_= delegation, dl_stid);   > > > } > > >=20 > > > When the parameter "struct nfs4_stid *s" is NULL, the function = will return a strange value which is a negative number. The value will be i= nterpreted as a very large number. Then the variable "dp" in the "alloc_ini= t_deleg" function will get the value, and it will pass the following "if" c= onditional statements. In the last, the variable "dp" will be dereferenced,= and it will cause an error. > > >=20 > > > My experimental kernel version is "LINUX 6.1", and this problem= exists in all the version from "LINUX v3.2-rc1" to "LINUX v6.6-rc5". > >=20 > >=20 > > (I don't think there are security implications here, so I'm cc'ing t= he > > mailing list and making this public.) > >=20 > > Well spotted! Ordinarily you'd be correct, but dl_stid is the first > > field in the struct, so the container_of will just return the same > > value that you pass in. > >=20 > > Still, this is not something we ought to rely on going forward. Woul= d > > you care to make a patch to clean this up and make that a bit less > > subtle? > >=20 > > Thanks! > > --=20 > > Jeff Layton >=20 >=20 > Thank you for your feedback! Indeed, you are correct! Next time I will ch= eck it twice before reporting a problem. >=20 > My patch is below: >=20 > Modify the conditional statement for null pointer check in the function > 'alloc_init_deleg' to make this function more robust and clear. Otherwise= , > this function may have potential pointer dereference problem in the futur= e, > when modifying or expanding the nfs4_delegation structure. >=20 > Signed-off-by: Sicong Huang > --- > fs/nfsd/nfs4state.c | 6 ++++-- > 1 file changed, 4 insertions(+), 2 deletions(-) >=20 > diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c > index b1118050ff52..516b8bd6cb53 100644 > --- a/fs/nfsd/nfs4state.c > +++ b/fs/nfsd/nfs4state.c > @@ -1160,6 +1160,7 @@ alloc_init_deleg(struct nfs4_client *clp, struct nf= s4_file *fp, > struct nfs4_clnt_odstate *odstate, u32 dl_type) > { > struct nfs4_delegation *dp; > + struct nfs4_stid *stid; > long n; > =20 > dprintk("NFSD alloc_init_deleg\n"); > @@ -1168,9 +1169,10 @@ alloc_init_deleg(struct nfs4_client *clp, struct n= fs4_file *fp, > goto out_dec; > if (delegation_blocked(&fp->fi_fhandle)) > goto out_dec; > - dp =3D delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg)); > - if (dp =3D=3D NULL) > + stid =3D nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg); > + if (stid =3D=3D NULL) > goto out_dec; > + dp =3D delegstateid(stid); > =20 > /* > * delegation seqid's are never incremented. The 4.1 special Reviewed-by: Jeff Layton