Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp635529pxv; Thu, 15 Jul 2021 12:05:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwmaUEkc+5iTRUHYzFetYMCGv5NW9I3bzmV+9kw8/TJDBNUxLMdmAD3fOmmyMeOp5rQFWTQ X-Received: by 2002:a92:3610:: with SMTP id d16mr3571546ila.16.1626375939805; Thu, 15 Jul 2021 12:05:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626375939; cv=none; d=google.com; s=arc-20160816; b=adBeztv+2jHcAwPApCliAA9UCTzSTbiTLOl5r2w69CC6P9IU7IkqdpSu9KZ8f047+r mCNwJq9eCzWXmbgGxNnFSUfCKB25ycI/GPP+i+RE1Lr5bd8e4j/9bPn6hs2W/gBgmKzR GkJjLesYphr0DIwgJAW2CKHX0In8HPv+dH6HrcZtxIAFbMMMYu+Dc9Twmp7b7Z3H4xXc hSNV3uWIsEvGLs4/e5mg99azoHJqyfj/zxjLSlISQ7Ff1koOFfUFd/OUMr1wAIiS40ji VO4nuJwV/BE/FZdbCMB3yu0zD0ryYx311SN8EduEk3eI/2F6b/oQFRxU5tfTwkpO1msQ acmQ== 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=aOw68lVlRR+KQp++ak1qRa6VWo1mSYUCDFWTdN8zk1U=; b=NzJaXhvnEU81TxGymEznElnEQPL/vtlpe+GqLgaVlgt47IAgs3BTTOVaJgXwTj2lEc iWFJN74ipdqvKk6qNMrU/mx/wlx9QK2r1E7vg1IMGdlUoEPOEfqKafHEgjZH7VJ0J/fJ a7Rr5Nsw19CqVdcW9DDcGqOksckgHIcPMA6P6ZJ8/jcgzvbekFGjWailTU8oOQnFzQ1G o4PqyhC1FrMOqk7SLs5UoH0oBi3jm5MqYRSS5mR9xRQ8KduBzwE87ZglUcIyw/XeAvJI Xz2jY6x1QjUdLAhwNxU5ZyTa5FXuYMu0cPVcGG8Xau8YJziaRPlZMJEk2wt9VeWqrpx9 nHLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=jffPUDN0; 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 t11si7733953ils.106.2021.07.15.12.05.26; Thu, 15 Jul 2021 12:05:39 -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=jffPUDN0; 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 S241023AbhGOTGw (ORCPT + 99 others); Thu, 15 Jul 2021 15:06:52 -0400 Received: from mail.kernel.org ([198.145.29.99]:59882 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241028AbhGOS4m (ORCPT ); Thu, 15 Jul 2021 14:56:42 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id D4140613C4; Thu, 15 Jul 2021 18:53:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626375228; bh=M/YarBLgGLZs5J4oVWxU+ihkCU8UuKy2p89D/jtlqE4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jffPUDN0GiVitqBqZznaWELhbHK8erYtE8WT+AT8Aw/Gty4GiTmwHzEG9DHkuGKvE euJwRa/hpFM+Fo31DWYbAxlbmtiZQRk3PhIADnq6CvnPnyG5PzXQAazqR+AcwvIyro vIOnTZ3xUDXOZIhkigpwVhp+1bnnT08Idx75UpDE= 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.10 214/215] ext4: fix memory leak in ext4_fill_super Date: Thu, 15 Jul 2021 20:39:46 +0200 Message-Id: <20210715182636.931701201@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210715182558.381078833@linuxfoundation.org> References: <20210715182558.381078833@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 @@ -1480,6 +1480,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; @@ -3624,6 +3625,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 @@ -1260,8 +1260,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; /* @@ -5173,8 +5173,7 @@ failed_mount3a: ext4_es_unregister_shrinker(sbi); failed_mount3: 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); @@ -5927,8 +5926,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) ||