Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp648301pxv; Thu, 15 Jul 2021 12:25:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxo9UV9RJjQJZYS1DsnyUn6Xn084zwyW6yKLrDYIvC9SuMTIJAyngi5zmUB2zbIOpMiSk7u X-Received: by 2002:a17:906:fa8e:: with SMTP id lt14mr7135865ejb.21.1626377117133; Thu, 15 Jul 2021 12:25:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626377117; cv=none; d=google.com; s=arc-20160816; b=rn/LMDnKudXuGqychOAf+m54y1Wj2wV1r5zFcs9xX69cw67LQAikZoAM/R3yfuDWUF wmNuU1WrmMUzX/TMtudSALH532P8jtPhwVE0l7pRjFUUcoXHVCasxzGQueRT993ryBPQ uJoSn6fyzUpoGWVrKt4MHigxr8FW+6Q0JCfXYOHgl18Gw6kfvR1weUA/+2HmbxuskvRl zE0x4pTaQVQGcxZagrtJb+y0QyXBY8Yavs3kj2sH+swqWAuueE9nZWIq1FqD87nhq4wn +Fi2CvnAja3tVWRdKgfS6aV8SwrLgxIOKw7HSZF0cZvUBkkPGhW1JhNhZSdu/VAPpras 2I7g== 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=5eVmJCXKuCj6OYM5sd7QEbJo2OqDU+lCj+sYm5N53BA=; b=WrKzCMj73O7itaUFl0e05c9gVrWQUlI3w8U7mzcKJ0WOrCJJ3T3EgtW6BNVUVufXP0 +B08KWE2wFEnlkHep+1EHz1wZKFoVZD4nBoCtq5Xj2jwLmIHvLVm6pnfYkTOh7fM/ZGG rsOJV3mbnih5nKBUHp5+whtBko0mvsXhUFLcgUfHi8wqw4ur81QjcLtH975UIdSK9H13 nHx26nLpw1+h6XhZMQDZzuVNUdIyU87m998BojObG0HSM0XqR3K5gAIOrEwPSIYKEKDn fABImQMsHSVp7ZSOmIGUGl7sqfaRr1/vbnAokXkE/Fpk1buhYnTwUbH15z7zgRytEg3s oEDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ljMM8L6G; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z15si8225571edc.64.2021.07.15.12.24.54; Thu, 15 Jul 2021 12:25:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ljMM8L6G; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.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: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245182AbhGOTYD (ORCPT + 99 others); Thu, 15 Jul 2021 15:24:03 -0400 Received: from mail.kernel.org ([198.145.29.99]:45628 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242369AbhGOTHZ (ORCPT ); Thu, 15 Jul 2021 15:07:25 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 49802613D7; Thu, 15 Jul 2021 19:03:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626375801; bh=XuDzwAQwpwMH6Rp/TWickEcCg9y6r77JvxH44Ini5Gc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ljMM8L6Gce29rcD4eZmRpWsAardL4QRAd/KjI/QpEpSVGWA3KoA+07Hgxv9nQ1qhN YZZYZy0FfVSgSJ9OwlwhHKa0c0O6B+sSk7jl4w0/DcfUSnl/MD4SFrdRtBEySgeVHX 5WRsWRW9xrz+/LnuqH1F0eiJPE/1p+Pr+J5Xq0L4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+d9e482e303930fa4f6ff@syzkaller.appspotmail.com, Pavel Skripkin , Theodore Tso Subject: [PATCH 5.12 241/242] ext4: fix memory leak in ext4_fill_super Date: Thu, 15 Jul 2021 20:40:03 +0200 Message-Id: <20210715182635.305393881@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210715182551.731989182@linuxfoundation.org> References: <20210715182551.731989182@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Pavel Skripkin commit 618f003199c6188e01472b03cdbba227f1dc5f24 upstream. static int kthread(void *_create) will return -ENOMEM or -EINTR in case of internal failure or kthread_stop() call happens before threadfn call. To prevent fancy error checking and make code more straightforward we moved all cleanup code out of kmmpd threadfn. Also, dropped struct mmpd_data at all. Now struct super_block is a threadfn data and struct buffer_head embedded into struct ext4_sb_info. Reported-by: syzbot+d9e482e303930fa4f6ff@syzkaller.appspotmail.com Signed-off-by: Pavel Skripkin Link: https://lore.kernel.org/r/20210430185046.15742-1-paskripkin@gmail.com Signed-off-by: Theodore Ts'o Signed-off-by: Greg Kroah-Hartman --- fs/ext4/ext4.h | 4 ++++ fs/ext4/mmp.c | 28 +++++++++++++--------------- fs/ext4/super.c | 10 ++++------ 3 files changed, 21 insertions(+), 21 deletions(-) --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -1490,6 +1490,7 @@ struct ext4_sb_info { struct kobject s_kobj; struct completion s_kobj_unregister; struct super_block *s_sb; + struct buffer_head *s_mmp_bh; /* Journaling */ struct journal_s *s_journal; @@ -3663,6 +3664,9 @@ extern struct ext4_io_end_vec *ext4_last /* mmp.c */ extern int ext4_multi_mount_protect(struct super_block *, ext4_fsblk_t); +/* mmp.c */ +extern void ext4_stop_mmpd(struct ext4_sb_info *sbi); + /* verity.c */ extern const struct fsverity_operations ext4_verityops; --- a/fs/ext4/mmp.c +++ b/fs/ext4/mmp.c @@ -127,9 +127,9 @@ void __dump_mmp_msg(struct super_block * */ static int kmmpd(void *data) { - struct super_block *sb = ((struct mmpd_data *) data)->sb; - struct buffer_head *bh = ((struct mmpd_data *) data)->bh; + struct super_block *sb = (struct super_block *) data; struct ext4_super_block *es = EXT4_SB(sb)->s_es; + struct buffer_head *bh = EXT4_SB(sb)->s_mmp_bh; struct mmp_struct *mmp; ext4_fsblk_t mmp_block; u32 seq = 0; @@ -245,12 +245,18 @@ static int kmmpd(void *data) retval = write_mmp_block(sb, bh); exit_thread: - EXT4_SB(sb)->s_mmp_tsk = NULL; - kfree(data); - brelse(bh); return retval; } +void ext4_stop_mmpd(struct ext4_sb_info *sbi) +{ + if (sbi->s_mmp_tsk) { + kthread_stop(sbi->s_mmp_tsk); + brelse(sbi->s_mmp_bh); + sbi->s_mmp_tsk = NULL; + } +} + /* * Get a random new sequence number but make sure it is not greater than * EXT4_MMP_SEQ_MAX. @@ -275,7 +281,6 @@ int ext4_multi_mount_protect(struct supe struct ext4_super_block *es = EXT4_SB(sb)->s_es; struct buffer_head *bh = NULL; struct mmp_struct *mmp = NULL; - struct mmpd_data *mmpd_data; u32 seq; unsigned int mmp_check_interval = le16_to_cpu(es->s_mmp_update_interval); unsigned int wait_time = 0; @@ -364,24 +369,17 @@ skip: goto failed; } - mmpd_data = kmalloc(sizeof(*mmpd_data), GFP_KERNEL); - if (!mmpd_data) { - ext4_warning(sb, "not enough memory for mmpd_data"); - goto failed; - } - mmpd_data->sb = sb; - mmpd_data->bh = bh; + EXT4_SB(sb)->s_mmp_bh = bh; /* * Start a kernel thread to update the MMP block periodically. */ - EXT4_SB(sb)->s_mmp_tsk = kthread_run(kmmpd, mmpd_data, "kmmpd-%.*s", + EXT4_SB(sb)->s_mmp_tsk = kthread_run(kmmpd, sb, "kmmpd-%.*s", (int)sizeof(mmp->mmp_bdevname), bdevname(bh->b_bdev, mmp->mmp_bdevname)); if (IS_ERR(EXT4_SB(sb)->s_mmp_tsk)) { EXT4_SB(sb)->s_mmp_tsk = NULL; - kfree(mmpd_data); ext4_warning(sb, "Unable to create kmmpd thread for %s.", sb->s_id); goto failed; --- a/fs/ext4/super.c +++ b/fs/ext4/super.c @@ -1245,8 +1245,8 @@ static void ext4_put_super(struct super_ ext4_xattr_destroy_cache(sbi->s_ea_block_cache); sbi->s_ea_block_cache = NULL; - if (sbi->s_mmp_tsk) - kthread_stop(sbi->s_mmp_tsk); + ext4_stop_mmpd(sbi); + brelse(sbi->s_sbh); sb->s_fs_info = NULL; /* @@ -5168,8 +5168,7 @@ failed_mount3a: failed_mount3: flush_work(&sbi->s_error_work); del_timer_sync(&sbi->s_err_report); - if (sbi->s_mmp_tsk) - kthread_stop(sbi->s_mmp_tsk); + ext4_stop_mmpd(sbi); failed_mount2: rcu_read_lock(); group_desc = rcu_dereference(sbi->s_group_desc); @@ -5967,8 +5966,7 @@ static int ext4_remount(struct super_blo */ ext4_mark_recovery_complete(sb, es); } - if (sbi->s_mmp_tsk) - kthread_stop(sbi->s_mmp_tsk); + ext4_stop_mmpd(sbi); } else { /* Make sure we can mount this feature set readwrite */ if (ext4_has_feature_readonly(sb) ||