Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp2764581imw; Wed, 6 Jul 2022 11:19:13 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uJQfdvTYdHRovqpj24waFRQ/Uu/wRmFGKixPw/SR8bsprtzuOCL06e2qwawWFPmmowSEUX X-Received: by 2002:a17:902:e849:b0:16b:f38c:d90b with SMTP id t9-20020a170902e84900b0016bf38cd90bmr9601041plg.154.1657131553614; Wed, 06 Jul 2022 11:19:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657131553; cv=none; d=google.com; s=arc-20160816; b=j7u9sTLhsJY0rb/IvCTjfoOdA9tXORnon2dQaK42BamDbsGwsFHY1znGeJGt09D6Mj 1SuGFwvdln5G1NygQS3TNy6OF4TeDeZNOn2F5vFxbr6fuwRE0gJEXf6l9Ruj8WlcwDB/ WnMe6NP4QF90iH4CjSnXw6egr2lLlvuTXvGsAmIVhEDEtXgcuVLJqEdo01MKz/lLhb7z rXBno13ORq2Md/aAZOGHuyL5nut78xoJcD//dUxjmB+lpNbYM6LTeGC/hhPvPcQWK//1 0gfSuoVlfuJHKVQiNW7XPtIv1ycOlRDsyvhabDG0/JO6+VcHTteMk3dNnmTBH/1EV8d6 m2KA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=i0l+YZo6jS5olHzX1+ToZjmRJMVUsrxlw2UmljZ6+5s=; b=egB61SGiAa+6vaYCu7oz1FHSdNjVMYxZQNDOQpMUiumjHgCjjToiejrqWjYwh096S5 VNC/2CZebiJKJlU7kPE0fVnYzEk5jmB4eo3RzB9TEQsn+Wvj0xo1b7o13kAKf0ZQh+Ct i+L2ebdEwgekMCpsXus8Wakgg1VRpCs7zsrtM/G+WbOgM/lqSBkCSs9y3d+ypovW9JZl wAD+15OIUlHYf3LQNfVjEw7IguoDr+YN0hpAcpEGKBAl0/uJQ+pBS0VL9P/IOwy0gCEW ynGmSeB8NceE6d1hT0fjz6ETQ2+kd7UyzG4O7Fit2b+1pqGySfh+UoDG3YJXTgPZPojo tPVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="BcrW4/oE"; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ip3-20020a17090b314300b001ed38150f5csi28798516pjb.170.2022.07.06.11.18.47; Wed, 06 Jul 2022 11:19:13 -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=@kernel.org header.s=k20201202 header.b="BcrW4/oE"; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233860AbiGFRYs (ORCPT + 99 others); Wed, 6 Jul 2022 13:24:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46412 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233851AbiGFRYr (ORCPT ); Wed, 6 Jul 2022 13:24:47 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69012C12; Wed, 6 Jul 2022 10:24:46 -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 ams.source.kernel.org (Postfix) with ESMTPS id 0E794B81E67; Wed, 6 Jul 2022 17:24:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B7E03C3411C; Wed, 6 Jul 2022 17:24:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1657128283; bh=6Sk0xJ/PruRoP1qaorVhBxfYBDuwBIhPQI+yDcyDC3c=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=BcrW4/oEZY42Rwe+0NFnXd0OrVChxxYsIXIbUIaobGN6cN6pHdNMbQMXe1m1tcmaS WR9PkAXn0msjm6Upoz06gxjxfuXBUEYM+Qejv895yDQD9d9U1McsgPzVjMJXqHXW9X Mk5eMho5H0+bUK2/5BzT9MTa7EtCV9IrgxJPokcevVj+iAatLJ3wZI/+yyH0h9UjlT GM4zOy9M1fBrLNhsPkt3XBT+53dNrpcIfccQb53p9yfmeRuC85yZb86axm9kP+rqLx BJWa9TJ6CFhWcUKuTpiQbWMYHpOyrHg7y3iIO1JgXCbL4ILqmpGt1Cw9p7aBzixgKy CKtLCzkeX2fcQ== Date: Wed, 6 Jul 2022 10:24:43 -0700 From: "Darrick J. Wong" To: Jianglei Nie Cc: dchinner@redhat.com, chandan.babu@oracle.com, linux-xfs@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] xfs: fix potential memory leak in xfs_bmap_add_attrfork() Message-ID: References: <20220706082237.2255887-1-niejianglei2021@163.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220706082237.2255887-1-niejianglei2021@163.com> X-Spam-Status: No, score=-7.8 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,T_SCC_BODY_TEXT_LINE 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 On Wed, Jul 06, 2022 at 04:22:37PM +0800, Jianglei Nie wrote: > xfs_bmap_add_attrfork() allocates a memory chunk for ip->i_afp with > xfs_ifork_alloc(). When some error occurs, the function goto trans_cancel; > without releasing the ip->i_afp, which will lead to a memory leak. > > We should release the ip->i_afp with kmem_cache_free() and set "ip->i_afp > = NULL" if ip->i_afp is not NULL pointer. > > Signed-off-by: Jianglei Nie > --- > fs/xfs/libxfs/xfs_bmap.c | 4 ++++ > 1 file changed, 4 insertions(+) > > diff --git a/fs/xfs/libxfs/xfs_bmap.c b/fs/xfs/libxfs/xfs_bmap.c > index 6833110d1bd4..0c99726c0968 100644 > --- a/fs/xfs/libxfs/xfs_bmap.c > +++ b/fs/xfs/libxfs/xfs_bmap.c > @@ -1088,6 +1088,10 @@ xfs_bmap_add_attrfork( > trans_cancel: > xfs_trans_cancel(tp); > xfs_iunlock(ip, XFS_ILOCK_EXCL); > + if (ip->i_afp) { > + kmem_cache_free(xfs_ifork_cache, ip->i_afp); > + ip->a_afp = NULL; > + } I don't think this is correct. If xfs_bmap_add_attrfork_* fail without dirtying the transaction, this function cancels the transaction without shutting down the filesystem, and return the error code to the caller. However, i_forkoff is not reset to zero, which means that the inode still has an attr fork, so i_afp must not be freed. Freeing the memory and nulling out the pointer without resetting i_forkoff results in inconsistent incore state, which will probably lead to a crash somewhere. In the end, inode reclaim will free i_afp. I think this is mooted by[1], right? --D [1] https://lore.kernel.org/linux-xfs/165705898555.2826746.14913566803667615290.stgit@magnolia/T/#u > return error; > } > > -- > 2.25.1 >