Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp445654iog; Mon, 13 Jun 2022 06:08:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyaSxpPqxOROYfnR2toN90KoIWhwsk9dfTl1xc0GMPMm9SyZMmWNq5FFOsGr2JsSxga7JeX X-Received: by 2002:a17:902:e80c:b0:167:5e77:e696 with SMTP id u12-20020a170902e80c00b001675e77e696mr47783517plg.119.1655125702903; Mon, 13 Jun 2022 06:08:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655125702; cv=none; d=google.com; s=arc-20160816; b=BCXwGfVPvsDBjasWTUZCzLu7gL1SUPTdvGbeNxI5qLsD0nLS7jOxBPq218qdAbgWQJ Be0kjSBTqQuO8nqmrpp93CIypQv/Po6POl/MTf9v6B4MCWeEwOHmpyABDOeHmGYe1QRk GhxH5eXQv6jbACotZvLh5NY2Esmqd6Obkp2VmEkB2XJXIgK0GmjBi+hnyhWGpl05zkao 6ct84yDgCLhCzL/H0U1N4Sa3gI/LuONEUhpbt5dVormru+52NjE30EQcEJGkFeJLlw6j VycPVWaBsGSIryFPUENQ4C0K7QLVjUcFtfp9Fby6yXSVHTFk4akGdlB9ZOyQAODx0/W9 Fk5A== 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=UQjLFGyq0K2HZ9nB+GVKgvgLbj+sv57OnkdW7JLG1j0=; b=dE9fbAdWlHcvZJ4EbGexxonuomswsz4UGYMhFwaA7Oc6C6u8zzpqiWT6oQV4DjQOYG 0z2ruYWV3b1WLa1VArJDSxWi0B1lINNeqU4HT6+8a6nlPpTk1mbOJIOgdMQW0IeNyMLx XdiJ3HedJTyxf4Ji4LLtUPJqGu7+oGn5mNp17NlL7fjcSNeMPJtR3WVQEuOdgoazF+xP rJyrW4fR5iwCoM+H9+CVrQRusjQpRlm61EKOIFW4sLGoI8CGess6f3aVajFqmEjawaUg 7gHoxoOz3m5GiMfMnZbMwIOZhyaT23Ujk1HhkUoPafnq+MJFTSuPLaQiU3PEdhyb8F0q ZAxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=K8fIXoQW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x126-20020a626384000000b0051b9771d617si8825747pfb.179.2022.06.13.06.08.08; Mon, 13 Jun 2022 06:08:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=K8fIXoQW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S242016AbiFMKVZ (ORCPT + 99 others); Mon, 13 Jun 2022 06:21:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243102AbiFMKUs (ORCPT ); Mon, 13 Jun 2022 06:20:48 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 145182181C; Mon, 13 Jun 2022 03:17:34 -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 sin.source.kernel.org (Postfix) with ESMTPS id 7B25ECE1166; Mon, 13 Jun 2022 10:17:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 91F2FC3411C; Mon, 13 Jun 2022 10:17:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655115450; bh=1NU80AnL8y3iAzFP+OyKKgI3pBIP8WckFqBUVyErnU4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=K8fIXoQWRUUe6RgroIKMufzXMSQSQFXNYUAahBhmd7G1l7vITh/zvOGm4FSItyBDU k2sEv3XjTPsjhvo4BkFPIuy7ysB5CIOcz+H2V9v0Y+qi/vq8h9WDVYF5uHnvuVO4kL QQykieci0F//Erd6o2DKkdTHnAGoeVNtMkrt8QZw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ye Bin , Jan Kara , Theodore Tso , stable@kernel.org Subject: [PATCH 4.9 078/167] ext4: fix use-after-free in ext4_rename_dir_prepare Date: Mon, 13 Jun 2022 12:09:12 +0200 Message-Id: <20220613094859.183470668@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220613094840.720778945@linuxfoundation.org> References: <20220613094840.720778945@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=-8.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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: Ye Bin commit 0be698ecbe4471fcad80e81ec6a05001421041b3 upstream. We got issue as follows: EXT4-fs (loop0): mounted filesystem without journal. Opts: ,errors=continue ext4_get_first_dir_block: bh->b_data=0xffff88810bee6000 len=34478 ext4_get_first_dir_block: *parent_de=0xffff88810beee6ae bh->b_data=0xffff88810bee6000 ext4_rename_dir_prepare: [1] parent_de=0xffff88810beee6ae ================================================================== BUG: KASAN: use-after-free in ext4_rename_dir_prepare+0x152/0x220 Read of size 4 at addr ffff88810beee6ae by task rep/1895 CPU: 13 PID: 1895 Comm: rep Not tainted 5.10.0+ #241 Call Trace: dump_stack+0xbe/0xf9 print_address_description.constprop.0+0x1e/0x220 kasan_report.cold+0x37/0x7f ext4_rename_dir_prepare+0x152/0x220 ext4_rename+0xf44/0x1ad0 ext4_rename2+0x11c/0x170 vfs_rename+0xa84/0x1440 do_renameat2+0x683/0x8f0 __x64_sys_renameat+0x53/0x60 do_syscall_64+0x33/0x40 entry_SYSCALL_64_after_hwframe+0x44/0xa9 RIP: 0033:0x7f45a6fc41c9 RSP: 002b:00007ffc5a470218 EFLAGS: 00000246 ORIG_RAX: 0000000000000108 RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 00007f45a6fc41c9 RDX: 0000000000000005 RSI: 0000000020000180 RDI: 0000000000000005 RBP: 00007ffc5a470240 R08: 00007ffc5a470160 R09: 0000000020000080 R10: 00000000200001c0 R11: 0000000000000246 R12: 0000000000400bb0 R13: 00007ffc5a470320 R14: 0000000000000000 R15: 0000000000000000 The buggy address belongs to the page: page:00000000440015ce refcount:0 mapcount:0 mapping:0000000000000000 index:0x1 pfn:0x10beee flags: 0x200000000000000() raw: 0200000000000000 ffffea00043ff4c8 ffffea0004325608 0000000000000000 raw: 0000000000000001 0000000000000000 00000000ffffffff 0000000000000000 page dumped because: kasan: bad access detected Memory state around the buggy address: ffff88810beee580: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ffff88810beee600: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff >ffff88810beee680: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ^ ffff88810beee700: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ffff88810beee780: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ================================================================== Disabling lock debugging due to kernel taint ext4_rename_dir_prepare: [2] parent_de->inode=3537895424 ext4_rename_dir_prepare: [3] dir=0xffff888124170140 ext4_rename_dir_prepare: [4] ino=2 ext4_rename_dir_prepare: ent->dir->i_ino=2 parent=-757071872 Reason is first directory entry which 'rec_len' is 34478, then will get illegal parent entry. Now, we do not check directory entry after read directory block in 'ext4_get_first_dir_block'. To solve this issue, check directory entry in 'ext4_get_first_dir_block'. [ Trigger an ext4_error() instead of just warning if the directory is missing a '.' or '..' entry. Also make sure we return an error code if the file system is corrupted. -TYT ] Signed-off-by: Ye Bin Reviewed-by: Jan Kara Link: https://lore.kernel.org/r/20220414025223.4113128-1-yebin10@huawei.com Signed-off-by: Theodore Ts'o Cc: stable@kernel.org Signed-off-by: Greg Kroah-Hartman --- fs/ext4/namei.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -3318,6 +3318,9 @@ static struct buffer_head *ext4_get_firs struct buffer_head *bh; if (!ext4_has_inline_data(inode)) { + struct ext4_dir_entry_2 *de; + unsigned int offset; + /* The first directory block must not be a hole, so * treat it as DIRENT_HTREE */ @@ -3326,9 +3329,30 @@ static struct buffer_head *ext4_get_firs *retval = PTR_ERR(bh); return NULL; } - *parent_de = ext4_next_entry( - (struct ext4_dir_entry_2 *)bh->b_data, - inode->i_sb->s_blocksize); + + de = (struct ext4_dir_entry_2 *) bh->b_data; + if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data, + bh->b_size, 0) || + le32_to_cpu(de->inode) != inode->i_ino || + strcmp(".", de->name)) { + EXT4_ERROR_INODE(inode, "directory missing '.'"); + brelse(bh); + *retval = -EFSCORRUPTED; + return NULL; + } + offset = ext4_rec_len_from_disk(de->rec_len, + inode->i_sb->s_blocksize); + de = ext4_next_entry(de, inode->i_sb->s_blocksize); + if (ext4_check_dir_entry(inode, NULL, de, bh, bh->b_data, + bh->b_size, offset) || + le32_to_cpu(de->inode) == 0 || strcmp("..", de->name)) { + EXT4_ERROR_INODE(inode, "directory missing '..'"); + brelse(bh); + *retval = -EFSCORRUPTED; + return NULL; + } + *parent_de = de; + return bh; }