Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp3138518pxb; Mon, 18 Apr 2022 17:01:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz0+GpV5DwazUZLlu6KKGQ7OMUjnTHBEY7EgNtk492DgzmAw6RYAWqrBnxUzImXkm5r26gm X-Received: by 2002:a17:907:6e0d:b0:6e0:59ae:21f1 with SMTP id sd13-20020a1709076e0d00b006e059ae21f1mr11374319ejc.362.1650326518360; Mon, 18 Apr 2022 17:01:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650326518; cv=none; d=google.com; s=arc-20160816; b=yHLAC9OjulH0rW6/unVWOLlqo9ZgIG0aNSQ2wdF/FyMAfU7GbGJHOdMzhTZ/J5LrOq SwqEjY8ATTMXDI9tcY8nKu6uCnDPfZYHQdqWDx2EzL+bud35xTnx+uUc1WyGIibA09NZ miUzkwaF27+BDFAoqO/KMf25BKRURKmmu2omGP57iNkiYQc66i9Dd3aoiENklhOoIkaB PtpMsLX08bvl7n1TpbKATw2jRtkI7P9w974vzMAWghzig5PNRA/GVASpybW6B8V0wivu xLWigd34kyCuA2H91RaeRTehcZdjetFCR2+tj1uTMbmu+g7dV8CfnbpckE5pxzYeajQM G4FA== 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=fXnqIFthkq2hVXHm7dPXAY8quCgBmo2K/8V93uafwck=; b=YXdnbbbXv4Xeg0AQ+Fg9GdJgp/of3WP+nfrVSf84LkXnuPfPW6aNAwsYVdVQC1s3n4 HJzIjplQpObRGXrXdTxUPZMkJPtESQwaS68VABcI3d9Qnet2xsmLe576uBQaSFHDMBgy IwaIXqIfoB21/3jfD93QiIwmep/4nKaTshs26SUFGilWxqB3wTiKKOrlBIwtzPN4frX4 det6vXzsNLlu+1JunsYY2yqyUNbMZcrcM51/2fItnxstRG0Y3qyPZDFJi7rXF0EFbHm8 D/srGCbA/1cXJIlhLvcGvAMWhuwH6FvazvZSt2yzUkpoSOde6Zzx1oUXQKyMJPVyLdNh A8BA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="Vub1YS/u"; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n18-20020a05640204d200b0041cd1200204si6783793edw.556.2022.04.18.17.01.35; Mon, 18 Apr 2022 17:01:58 -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="Vub1YS/u"; 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 S243474AbiDRNly (ORCPT + 99 others); Mon, 18 Apr 2022 09:41:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242126AbiDRNIp (ORCPT ); Mon, 18 Apr 2022 09:08:45 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0657134B80; Mon, 18 Apr 2022 05:48:20 -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 25856B80E4E; Mon, 18 Apr 2022 12:48:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 62838C385A1; Mon, 18 Apr 2022 12:48:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650286097; bh=NcRu25js3toUPx0KLfzmrjPtNYkGWmP5Qed61QgN4e4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Vub1YS/uqv9TanzgBHcXyTKurynUtV6Q/AZgFcxkCQ3DYXfgZmOj90fn4biG1LEfb iHoEza1UamnD+Twti5evArEiyKflbxYfcapxfej6oQSvJlgc1Gph7r3kRxuTxIOK/e JWwX6hhAlRvh7vjLWclHY22lGsyNEyIiijFhQ4XI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Baokun Li , Richard Weinberger Subject: [PATCH 4.14 028/284] jffs2: fix memory leak in jffs2_scan_medium Date: Mon, 18 Apr 2022 14:10:09 +0200 Message-Id: <20220418121211.496495273@linuxfoundation.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220418121210.689577360@linuxfoundation.org> References: <20220418121210.689577360@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 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 From: Baokun Li commit 9cdd3128874f5fe759e2c4e1360ab7fb96a8d1df upstream. If an error is returned in jffs2_scan_eraseblock() and some memory has been added to the jffs2_summary *s, we can observe the following kmemleak report: -------------------------------------------- unreferenced object 0xffff88812b889c40 (size 64): comm "mount", pid 692, jiffies 4294838325 (age 34.288s) hex dump (first 32 bytes): 40 48 b5 14 81 88 ff ff 01 e0 31 00 00 00 50 00 @H........1...P. 00 00 01 00 00 00 01 00 00 00 02 00 00 00 09 08 ................ backtrace: [] __kmalloc+0x613/0x910 [] jffs2_sum_add_dirent_mem+0x5c/0xa0 [] jffs2_scan_medium.cold+0x36e5/0x4794 [] jffs2_do_mount_fs.cold+0xa7/0x2267 [] jffs2_do_fill_super+0x383/0xc30 [] jffs2_fill_super+0x2ea/0x4c0 [] mtd_get_sb+0x254/0x400 [] mtd_get_sb_by_nr+0x4f/0xd0 [] get_tree_mtd+0x498/0x840 [] jffs2_get_tree+0x25/0x30 [] vfs_get_tree+0x8d/0x2e0 [] path_mount+0x50f/0x1e50 [] do_mount+0x107/0x130 [] __se_sys_mount+0x1c5/0x2f0 [] __x64_sys_mount+0xc7/0x160 [] do_syscall_64+0x45/0x70 unreferenced object 0xffff888114b54840 (size 32): comm "mount", pid 692, jiffies 4294838325 (age 34.288s) hex dump (first 32 bytes): c0 75 b5 14 81 88 ff ff 02 e0 02 00 00 00 02 00 .u.............. 00 00 84 00 00 00 44 00 00 00 6b 6b 6b 6b 6b a5 ......D...kkkkk. backtrace: [] kmem_cache_alloc_trace+0x584/0x880 [] jffs2_sum_add_inode_mem+0x54/0x90 [] jffs2_scan_medium.cold+0x4481/0x4794 [...] unreferenced object 0xffff888114b57280 (size 32): comm "mount", pid 692, jiffies 4294838393 (age 34.357s) hex dump (first 32 bytes): 10 d5 6c 11 81 88 ff ff 08 e0 05 00 00 00 01 00 ..l............. 00 00 38 02 00 00 28 00 00 00 6b 6b 6b 6b 6b a5 ..8...(...kkkkk. backtrace: [] kmem_cache_alloc_trace+0x584/0x880 [] jffs2_sum_add_xattr_mem+0x54/0x90 [] jffs2_scan_medium.cold+0x298c/0x4794 [...] unreferenced object 0xffff8881116cd510 (size 16): comm "mount", pid 692, jiffies 4294838395 (age 34.355s) hex dump (first 16 bytes): 00 00 00 00 00 00 00 00 09 e0 60 02 00 00 6b a5 ..........`...k. backtrace: [] kmem_cache_alloc_trace+0x584/0x880 [] jffs2_sum_add_xref_mem+0x54/0x90 [] jffs2_scan_medium.cold+0x3a20/0x4794 [...] -------------------------------------------- Therefore, we should call jffs2_sum_reset_collected(s) on exit to release the memory added in s. In addition, a new tag "out_buf" is added to prevent the NULL pointer reference caused by s being NULL. (thanks to Zhang Yi for this analysis) Fixes: e631ddba5887 ("[JFFS2] Add erase block summary support (mount time improvement)") Cc: stable@vger.kernel.org Co-developed-with: Zhihao Cheng Signed-off-by: Baokun Li Signed-off-by: Richard Weinberger Signed-off-by: Greg Kroah-Hartman --- fs/jffs2/scan.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) --- a/fs/jffs2/scan.c +++ b/fs/jffs2/scan.c @@ -136,7 +136,7 @@ int jffs2_scan_medium(struct jffs2_sb_in if (!s) { JFFS2_WARNING("Can't allocate memory for summary\n"); ret = -ENOMEM; - goto out; + goto out_buf; } } @@ -274,13 +274,15 @@ int jffs2_scan_medium(struct jffs2_sb_in } ret = 0; out: + jffs2_sum_reset_collected(s); + kfree(s); + out_buf: if (buf_size) kfree(flashbuf); #ifndef __ECOS else mtd_unpoint(c->mtd, 0, c->mtd->size); #endif - kfree(s); return ret; }