Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp714574pxv; Thu, 15 Jul 2021 14:14:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwxF0Xq7NPi9WTXOh/pGInH2IVaRRCHKtP1ODs0B2ToPbzT4WORHSdnE7ljzfSW+RoOSYyy X-Received: by 2002:a05:6402:1acf:: with SMTP id ba15mr9623962edb.167.1626383654945; Thu, 15 Jul 2021 14:14:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626383654; cv=none; d=google.com; s=arc-20160816; b=H4QujYxR2VxZXa1SQemYOPPz9vDklWH/98mWXcFM1l26S2ly8b2ncSvBJ/kbifuCvI 05auYrrswkF9PhhQSOaQHMaknDvz1LZgi+2tmitbG2D6HFrpw9Cv0Zq3FR92Z9Tfayj3 3qMRVQZlmPII9oMUqGmlJLdjeJLCh3VQmzkeYm3WZs6C8EYiBu90Mjv7ln6v3LXVvoP2 AxYsbJ5XsBPtRncQJ9UOh9iGx7qjKa3WTSdrhMSi6a3J/zcSnvELfrsEU3cN203QtFrR sxKQmF+I3tbaO8GIw6WLYugEbULrY0oD/07Kfy52x9wBUHV4HSJP81Nrvh73s9GSwVDU DsyQ== 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=CpHnUIMeT4XMVopip7EKSmg+GuvP1i5W7GncdYZfFXY=; b=mqhVdJwY02yS6rtWVmNVVEMHisufTUR/dtElwsAkLc1EWU/SjeD6GU8sXxipo9pxUn YeKLHFC9boKgfbLiM03Lp/CxDiysZpGct02QRrgCuvLTFPvCEe+ofuYjuSrUcK+lIXNW bOkFIpCz+cO5TKg2eK98eAIn/KSkzc49m8B0qnEqP8LakETIzRl2zOtbJN0oI1z3A1AV WJ1HqC7xoMg7eNq8zEXyoqGKwMDvhiB2tqOyavCPUVaB/4c9oxhaw7A+ypk474MEuslS G8I3qvAztND/RwPP2r7tBlW7nPl+6LF58JOoITpYFzZPH5Yjh+ZlRrQ/nPqCQEfq6bXC 3fMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Mpj2TXbP; 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 w25si8581658edq.408.2021.07.15.14.13.51; Thu, 15 Jul 2021 14:14:14 -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=Mpj2TXbP; 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 S245350AbhGOTc7 (ORCPT + 99 others); Thu, 15 Jul 2021 15:32:59 -0400 Received: from mail.kernel.org ([198.145.29.99]:46122 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244124AbhGOTKk (ORCPT ); Thu, 15 Jul 2021 15:10:40 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id A2E5761370; Thu, 15 Jul 2021 19:07:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626376066; bh=Jqpmm8dYoDE00AYtJ1/Vfd85rotV2KoWrHtWr9EACEs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Mpj2TXbPF0DP1B7DRp9SElNyGcr7ehCOyuLreaRHS1Ta/oluJA2fMIvT5LNbDxGQy 40BwZuIiH++GeXz+5lSpq1yMtAVXz2bBN+PA1dR1HTeUw7RNf3J0fz5rMxg8EAcjZV PrQeKVE8SQYE2TsnEdFqIKRB4Xfy/JOId9PTAU9A= 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 , Sasha Levin Subject: [PATCH 5.13 110/266] ext4: fix memory leak in ext4_fill_super Date: Thu, 15 Jul 2021 20:37:45 +0200 Message-Id: <20210715182633.320154234@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210715182613.933608881@linuxfoundation.org> References: <20210715182613.933608881@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 [ Upstream commit 618f003199c6188e01472b03cdbba227f1dc5f24 ] 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: Sasha Levin --- fs/ext4/ext4.h | 4 ++++ fs/ext4/mmp.c | 28 +++++++++++++--------------- fs/ext4/super.c | 10 ++++------ 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index 37002663d521..a179c0bbc12e 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -1488,6 +1488,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; @@ -3720,6 +3721,9 @@ extern struct ext4_io_end_vec *ext4_last_io_end_vec(ext4_io_end_t *io_end); /* 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; diff --git a/fs/ext4/mmp.c b/fs/ext4/mmp.c index 68fbeedd627b..6cb598b549ca 100644 --- a/fs/ext4/mmp.c +++ b/fs/ext4/mmp.c @@ -127,9 +127,9 @@ void __dump_mmp_msg(struct super_block *sb, struct mmp_struct *mmp, */ 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 super_block *sb, 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; diff --git a/fs/ext4/super.c b/fs/ext4/super.c index 736724ce86d7..3b6203543607 100644 --- a/fs/ext4/super.c +++ b/fs/ext4/super.c @@ -1245,8 +1245,8 @@ static void ext4_put_super(struct super_block *sb) 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; /* @@ -5194,8 +5194,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); @@ -5997,8 +5996,7 @@ static int ext4_remount(struct super_block *sb, int *flags, char *data) */ 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) || -- 2.30.2