Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp664711pxv; Thu, 15 Jul 2021 12:53:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxomjkXpzv7JKXp+9UkzEcKv2ut2u2Uz5gY13nm+TRsydXAhVFW4m5Tj/0d9sIt5sA7eJq8 X-Received: by 2002:a05:6e02:138b:: with SMTP id d11mr3782173ilo.229.1626378780437; Thu, 15 Jul 2021 12:53:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626378780; cv=none; d=google.com; s=arc-20160816; b=eNLqexruI0Zy9gwZ/+2FGD59xR3aLgFtoBPR2FFZlBGRFEMwEAr5ZiD256jcfpcJEk LABhdWGgesTfth7kn6BEbZRmcxCapBTSroTfr6+JpX2VQlbMhmewCFFoJGFJ5E4FB8lN wSPzx4bpBKV9Y4wtNS+HOkWwe/ROLqA8D1ksV4UPp72d8VRtthhvv+JJej9Wf1xtnIVP 5svT7KBKSjCJ6FNsSiFwS3wbjm/BDjNYXY9ahD7DL1MWWUe7kbDCCSnnFJjjO+fHb0qN UjMDK2tQSTtPMvwrRN+Da+gH6J8NXdr3wcMc91Qp6HT5ilFmfZqmG6LOo3/nUce7s1O8 OvkA== 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=Z0WETmRRkddeaaX+RHcmzWQRrzIcIBBf1l3RIy81Wh8=; b=YtaoNh7XBoRo8nwey24oG8qBCTnNkrtqwGzxlGQsLoIL2NzJb3BgU5ZtVCXXZqonNX /PW692G1tsm9GOywbrTrNpCOw8d58rJRjJcdxkQBx/5qdKYGpBt9Kzd3FJuenki/oG0b BgfS2INjxNyRopHtMz7LVz/2R5gyy+G9mTZEHFbPJDIp2F5adCra+AulRnlDrO4sa3Ih yOAlP1qgW1gZDj9QgJw9fGhlOO0D8Et3yNnBeNiRbvS5zBf5UVe2TZ++oLK5zIwQMIyq UkmkP2CdW5qa2PZstdJ4dFKfzDls2kGtYK11IfFL8rS9xrvulBa173F3WNXV4nwRh9CA X0jw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=jKS3HSGM; 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 x17si6615500jat.44.2021.07.15.12.52.48; Thu, 15 Jul 2021 12:53:00 -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=jKS3HSGM; 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 S1343728AbhGOTxE (ORCPT + 99 others); Thu, 15 Jul 2021 15:53:04 -0400 Received: from mail.kernel.org ([198.145.29.99]:58178 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244839AbhGOTSf (ORCPT ); Thu, 15 Jul 2021 15:18:35 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id E9E8C613DC; Thu, 15 Jul 2021 19:13:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626376414; bh=OyQ4ERn12mp0E4LYqVJQ68GV9XctXcHpNAvUIaZqOoo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jKS3HSGMs3gcwlbgWNfvix/2ZKI+wtv/hsPgk3S9AQqCuYDs9vCoke9TJBhUY4o3m VSFJcINEIPhqEwfcv4EoQwezBjKoyPZxn7rA2/I8jhvrkjFWd+ee7rttTTLZ0yUtWd Tj3293iDyEmSmHKIOdbjMj8sTZ8H4ZEyqBxi0YiI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ye Bin , Theodore Tso , Jan Kara Subject: [PATCH 5.13 262/266] ext4: fix possible UAF when remounting r/o a mmp-protected file system Date: Thu, 15 Jul 2021 20:40:17 +0200 Message-Id: <20210715182653.279863842@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: Theodore Ts'o commit 61bb4a1c417e5b95d9edb4f887f131de32e419cb upstream. After commit 618f003199c6 ("ext4: fix memory leak in ext4_fill_super"), after the file system is remounted read-only, there is a race where the kmmpd thread can exit, causing sbi->s_mmp_tsk to point at freed memory, which the call to ext4_stop_mmpd() can trip over. Fix this by only allowing kmmpd() to exit when it is stopped via ext4_stop_mmpd(). Link: https://lore.kernel.org/r/20210707002433.3719773-1-tytso@mit.edu Reported-by: Ye Bin Bug-Report-Link: <20210629143603.2166962-1-yebin10@huawei.com> Signed-off-by: Theodore Ts'o Reviewed-by: Jan Kara Signed-off-by: Greg Kroah-Hartman --- fs/ext4/mmp.c | 31 +++++++++++++++---------------- fs/ext4/super.c | 6 +++++- 2 files changed, 20 insertions(+), 17 deletions(-) --- a/fs/ext4/mmp.c +++ b/fs/ext4/mmp.c @@ -156,7 +156,12 @@ static int kmmpd(void *data) memcpy(mmp->mmp_nodename, init_utsname()->nodename, sizeof(mmp->mmp_nodename)); - while (!kthread_should_stop()) { + while (!kthread_should_stop() && !sb_rdonly(sb)) { + if (!ext4_has_feature_mmp(sb)) { + ext4_warning(sb, "kmmpd being stopped since MMP feature" + " has been disabled."); + goto wait_to_exit; + } if (++seq > EXT4_MMP_SEQ_MAX) seq = 1; @@ -177,16 +182,6 @@ static int kmmpd(void *data) failed_writes++; } - if (!(le32_to_cpu(es->s_feature_incompat) & - EXT4_FEATURE_INCOMPAT_MMP)) { - ext4_warning(sb, "kmmpd being stopped since MMP feature" - " has been disabled."); - goto exit_thread; - } - - if (sb_rdonly(sb)) - break; - diff = jiffies - last_update_time; if (diff < mmp_update_interval * HZ) schedule_timeout_interruptible(mmp_update_interval * @@ -207,7 +202,7 @@ static int kmmpd(void *data) ext4_error_err(sb, -retval, "error reading MMP data: %d", retval); - goto exit_thread; + goto wait_to_exit; } mmp_check = (struct mmp_struct *)(bh_check->b_data); @@ -221,7 +216,7 @@ static int kmmpd(void *data) ext4_error_err(sb, EBUSY, "abort"); put_bh(bh_check); retval = -EBUSY; - goto exit_thread; + goto wait_to_exit; } put_bh(bh_check); } @@ -244,7 +239,13 @@ static int kmmpd(void *data) retval = write_mmp_block(sb, bh); -exit_thread: +wait_to_exit: + while (!kthread_should_stop()) { + set_current_state(TASK_INTERRUPTIBLE); + if (!kthread_should_stop()) + schedule(); + } + set_current_state(TASK_RUNNING); return retval; } @@ -391,5 +392,3 @@ failed: brelse(bh); return 1; } - - --- a/fs/ext4/super.c +++ b/fs/ext4/super.c @@ -5996,7 +5996,6 @@ static int ext4_remount(struct super_blo */ ext4_mark_recovery_complete(sb, es); } - ext4_stop_mmpd(sbi); } else { /* Make sure we can mount this feature set readwrite */ if (ext4_has_feature_readonly(sb) || @@ -6110,6 +6109,9 @@ static int ext4_remount(struct super_blo if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks) ext4_release_system_zone(sb); + if (!ext4_has_feature_mmp(sb) || sb_rdonly(sb)) + ext4_stop_mmpd(sbi); + /* * Some options can be enabled by ext4 and/or by VFS mount flag * either way we need to make sure it matches in both *flags and @@ -6143,6 +6145,8 @@ restore_opts: for (i = 0; i < EXT4_MAXQUOTAS; i++) kfree(to_free[i]); #endif + if (!ext4_has_feature_mmp(sb) || sb_rdonly(sb)) + ext4_stop_mmpd(sbi); kfree(orig_data); return err; }