From patchwork Fri Nov 18 09:02:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhihao Cheng X-Patchwork-Id: 22195 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp69946wrr; Fri, 18 Nov 2022 00:46:06 -0800 (PST) X-Google-Smtp-Source: AA0mqf6hI3KtYHgeJDQ01Xc0XPi0x+xg4l7lOIU6BCoAEbUaaNWBituSAs66cuHQQBZaIBE1PGv7 X-Received: by 2002:a17:902:9a03:b0:186:a22a:177e with SMTP id v3-20020a1709029a0300b00186a22a177emr6502916plp.163.1668761166522; Fri, 18 Nov 2022 00:46:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668761166; cv=none; d=google.com; s=arc-20160816; b=cLCY0jd7jzjF9B1/dknKQuaiwK24Ma42zNAAEy3Qi1E8NDpmlLzUlXQXGi1NZ1zSDN SWk+kz+IBkVOfr2FviD+kowER6rcgLq7EOmnLZsSclcwv85TungH4IeXi6DKQ1xqqh/0 4DlmsG1TAFGxf7IcCg26ST1FzNXU/MH5uaJcuAOo27TWpbTCQzwgiLjTN0lLJj51U+W2 LzHC4x/ShMuAjBYnqhW7pk/lFDf4ev1ZgHkPp1p26CXBldAv+bdPKFwi3bmiX5J7bB31 jOpZQWcHlNkBzszGWcUAQeLiIzS7FFh8e8yF4kCDjWxG0YsbCgNRN+fGhx0qhi0P0WxP Of1A== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=H6hVyw7gFK4UaFpjXp3bQgcxCj2u3Shocz367dGXs2c=; b=k9BQGftlxLIR0PjF2cDam/aXQ82bhO4J2GAyX1t0RktBPPnKmaToIu1CgMu52Cte2Y 484MFPiLAK831q8wvImiIR5/WckTjwq40ANYrwEpEvmW+0ATpJMmHCXFQUp8WGv5RcoX pGsfYd2s3rrmELWjuXCc29g+h/GPsokELgWaezpvkxCB2pKUqWS3wjrxCTLXHqRrkU3u bFqWCBHSRy84MqNviVVsKN4rDXdAmurkj717mlwYMU5/yjr1mL3TuKmzf1ryoOqHVGCf 9k6r/zHbA/nwVpuho+FJV84TlEKtplBX34z4IpGoAUKQVaBAJSR6G7ogD+JD29spCWga 926A== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n3-20020a17090aab8300b0021328e7e073si2809393pjq.165.2022.11.18.00.45.54; Fri, 18 Nov 2022 00:46:06 -0800 (PST) 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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241398AbiKRIld (ORCPT + 99 others); Fri, 18 Nov 2022 03:41:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241208AbiKRIlW (ORCPT ); Fri, 18 Nov 2022 03:41:22 -0500 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 55F5824975 for ; Fri, 18 Nov 2022 00:41:21 -0800 (PST) Received: from dggemv703-chm.china.huawei.com (unknown [172.30.72.53]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4ND9Ch5v8dzJnpc; Fri, 18 Nov 2022 16:38:08 +0800 (CST) Received: from kwepemm600013.china.huawei.com (7.193.23.68) by dggemv703-chm.china.huawei.com (10.3.19.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 18 Nov 2022 16:41:19 +0800 Received: from huawei.com (10.175.127.227) by kwepemm600013.china.huawei.com (7.193.23.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 18 Nov 2022 16:41:18 +0800 From: Zhihao Cheng To: , , CC: , , , Subject: [PATCH 2/2] ubifs: dirty_cow_znode: Fix memleak in error handling path Date: Fri, 18 Nov 2022 17:02:36 +0800 Message-ID: <20221118090236.664244-3-chengzhihao1@huawei.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20221118090236.664244-1-chengzhihao1@huawei.com> References: <20221118090236.664244-1-chengzhihao1@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.127.227] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To kwepemm600013.china.huawei.com (7.193.23.68) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, 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?1749822908783560015?= X-GMAIL-MSGID: =?utf-8?q?1749822908783560015?= Following process will cause a memleak for copied up znode: dirty_cow_znode zn = copy_znode(c, znode); err = insert_old_idx(c, zbr->lnum, zbr->offs); if (unlikely(err)) return ERR_PTR(err); // No one refers to zn. Fix it by adding copied znode back to tnc, then it will be freed by ubifs_destroy_tnc_subtree() while closing tnc. Fetch a reproducer in [Link]. Link: https://bugzilla.kernel.org/show_bug.cgi?id=216705 Fixes: 1e51764a3c2a ("UBIFS: add new flash file system") Signed-off-by: Zhihao Cheng --- fs/ubifs/tnc.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/fs/ubifs/tnc.c b/fs/ubifs/tnc.c index 2df56bbc6865..2469f72eeaab 100644 --- a/fs/ubifs/tnc.c +++ b/fs/ubifs/tnc.c @@ -267,11 +267,18 @@ static struct ubifs_znode *dirty_cow_znode(struct ubifs_info *c, if (zbr->len) { err = insert_old_idx(c, zbr->lnum, zbr->offs); if (unlikely(err)) - return ERR_PTR(err); + /* + * Obsolete znodes will be freed by tnc_destroy_cnext() + * or free_obsolete_znodes(), copied up znodes should + * be added back to tnc and freed by + * ubifs_destroy_tnc_subtree(). + */ + goto out; err = add_idx_dirt(c, zbr->lnum, zbr->len); } else err = 0; +out: zbr->znode = zn; zbr->lnum = 0; zbr->offs = 0;