Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp612604pxb; Tue, 5 Apr 2022 15:58:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzGWlRh/EpcTK/c9vwUGQNr0AVC66fP1VGlQ+r3RPy+XhnJnFCX9wNFic7h7NeLbdSlP8pN X-Received: by 2002:a17:90b:713:b0:1c9:8dbf:e6fd with SMTP id s19-20020a17090b071300b001c98dbfe6fdmr6669893pjz.80.1649199496886; Tue, 05 Apr 2022 15:58:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649199496; cv=none; d=google.com; s=arc-20160816; b=AV4EjlElpZClQwhwbsAAqwhAFEpLRsiJ7on4y+qou2V4/a4uSnyswTUM3zdvMeg+UH /aoRtEjOzAFHKfdLsBCLBAu45I4ZtER8LUADXK9b57DfZISRx7XM28fJsyCMok2IySDo 0ObO5250VyZXYjQIYceE7XmFMH8xnnLnTGC6ndGU3IU5RfUNtZJhUSjJLamGqaCW73cK 7yckdnO7eC8By5kN+zw7GXZR5nZQx8JvVcdZhWSD4il+5oTKPczF0kc4daX6tFO/ewBl Uy4TXGn8TnrmypnMPb7O6EwGr55wHBpCqxd0UkejRHA4SNz+ob2efAuKoa5Em/YTPLF4 Pvow== 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=VdHNan4IA2aOkma3RPXsCB1wcteSo8Mhl7VKSI8FBFQ=; b=G2ICH+Ipnxff0F3gaDnhz3jpLiUYBBU3UhCCQiDva8jImRT3TOQRZ+gnBGnRzuw/3+ BudoLJ5Clc6+IOdLwEmk5xSW4DbK1PpjS8NYRuwABXesXEUDzVA/SxnlahnxtCXy36rx dIwQmCOim2XHPlBoQlsOXWwx7pq4b7NIWaSsBCuosVHhQNGQ7mYXAkLlct+WFQ42EOH+ keHEXHMn33UDVamGuSUAnZ13fZoYgxn1Y0zjo8OUuq8jtjEJS5HKt8HPrxsfhOeZgSgl wPTfG3jjuM5i0hyCnEaoNbc4GPWGOLYCgKrWofKyjXQxtbCzJC57F6QctBKCNsPHcfd8 wX+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=m+7GjQh+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id t5-20020a654b85000000b003816043ee9dsi10049800pgq.146.2022.04.05.15.58.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Apr 2022 15:58:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=m+7GjQh+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 943F616CE78; Tue, 5 Apr 2022 15:39:38 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1387562AbiDEOc3 (ORCPT + 99 others); Tue, 5 Apr 2022 10:32:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236181AbiDEJev (ORCPT ); Tue, 5 Apr 2022 05:34:51 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7FD92887B2; Tue, 5 Apr 2022 02:24:11 -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 E3D96B81C69; Tue, 5 Apr 2022 09:24:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5233CC385A0; Tue, 5 Apr 2022 09:24:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649150648; bh=sKywzZUH5FV82mHccsP45ibhee3xzG3cgiuNbnWvg4U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=m+7GjQh+aPzoKpJGZ83JVPKNUXEYFOiikx1iZJVkPpoEW732oZhnJL5ItyeKj84OJ ltTY5vUBLtI3I4sm7SG+Q7RrrJLGe3gkSfCch2u7XuhaQ/3z8qXHCXTf/NkYyMjVoV t0oEgDw5rnf6M9aqGNs0XW3GapAAqkUDDp0spizA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Baokun Li , Richard Weinberger Subject: [PATCH 5.15 082/913] jffs2: fix memory leak in jffs2_scan_medium Date: Tue, 5 Apr 2022 09:19:04 +0200 Message-Id: <20220405070342.281702665@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070339.801210740@linuxfoundation.org> References: <20220405070339.801210740@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=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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; } } @@ -275,13 +275,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; }