Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp629976pxb; Tue, 5 Apr 2022 16:33:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzFfkkxxkCj6klGEWKCmvO6fVcRbaQUHk3iWAIlasfNOMUR88HbmCxXurVKO6KZpzZLkPz0 X-Received: by 2002:a17:90a:2a84:b0:1ca:aefa:91b3 with SMTP id j4-20020a17090a2a8400b001caaefa91b3mr6937694pjd.104.1649201612980; Tue, 05 Apr 2022 16:33:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649201612; cv=none; d=google.com; s=arc-20160816; b=F1HT5wGFEyfQxpHZxCHrH0vDL4fbdaA7MrDdN74stlbJleEaTmvYMcp3FRKlcF4EqW 5UimQJmqFjr2JocD8ilETitxGWkP7prw+JaVyQ+9ldrLSbR1KC4ZouSCOd3hJ01Qo2pY U9//zka/rJ6tBHzqsI2gfB08BKL17M9ssWPFE7AY8xfqDU6eetU9gOj3kEBdJl46Ge/P G+JuqaQlRX3h50xYqIb0GMtD/BqEIzKOffqeZn4M8MqkaFQ8nIF9vYfHvBHSrquSmPrw DMg9mbVK2QzDAF0C0lhh8RD9JfM0soWsV0SegMFVnO1pmHya5HxfURLJyKPuvCSkTdn9 DWZw== 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=i42GYCexYKNmPHelUMGf6pAhJ1FMP2GuPgSMHBandTw6ai06vxLa+JHi2mZuVkDazl IyTmRwgaw5j2luYSvON2oRhp7AtDnliHEBgQqTk71+m3gNxDRyL8ey/GRykOK0dflKI7 JDFvHH/MoL6UlhKqlstOGNyrlInORZga8Im2RIVu3AAIln8EFzLqL51mWTN+4JE86VHk lU3SKEWDxctV0WFK91EheAZ0TCVGMJSasAoQkpLZe6uK59IZGhzKlZbwgHhxIFKE8uIf lehoZ+lb4aM/sb9v2s1CjJKiYb36Ia/1OKCDADqFOA1uOkdbXh3hC3cBo+NVnGiMcW7F wRHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=FjsspM8f; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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. [23.128.96.19]) by mx.google.com with ESMTPS id h16-20020a056a001a5000b004fa3a8dffa1si14713687pfv.88.2022.04.05.16.33.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Apr 2022 16:33:32 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=FjsspM8f; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 6DE2370869; Tue, 5 Apr 2022 16:17:46 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351093AbiDELEX (ORCPT + 99 others); Tue, 5 Apr 2022 07:04:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55428 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237394AbiDEIl4 (ORCPT ); Tue, 5 Apr 2022 04:41:56 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9AEB8AE64; Tue, 5 Apr 2022 01:34:45 -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 4DF29B81BBF; Tue, 5 Apr 2022 08:34:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B5D03C385A0; Tue, 5 Apr 2022 08:34:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649147683; bh=sKywzZUH5FV82mHccsP45ibhee3xzG3cgiuNbnWvg4U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FjsspM8fPDgAAtVe/LOx4Z4QXntrqLlat+55PaUectCVxugiVvl9N4ptru4zBty2C pFc3Ph4TzasQD8DYzgCL9TZ6a02uDxRTOIrViuN3mdTfghwPu0jkypo5vQQ9jemBIh lRTfpesD1/dlwsqwQw4abZBLt9iM9bvqbnq80gqc= 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.16 0085/1017] jffs2: fix memory leak in jffs2_scan_medium Date: Tue, 5 Apr 2022 09:16:38 +0200 Message-Id: <20220405070356.715247553@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070354.155796697@linuxfoundation.org> References: <20220405070354.155796697@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; }