From nobody Tue Sep 16 02:28:48 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8629CC61DB3 for ; Sat, 7 Jan 2023 02:57:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236266AbjAGC5B (ORCPT ); Fri, 6 Jan 2023 21:57:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32828 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232399AbjAGC46 (ORCPT ); Fri, 6 Jan 2023 21:56:58 -0500 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A51028CD36; Fri, 6 Jan 2023 18:56:57 -0800 (PST) Received: from dggpeml500021.china.huawei.com (unknown [172.30.72.53]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4NplFT2ZqSzJrCs; Sat, 7 Jan 2023 10:55:41 +0800 (CST) Received: from huawei.com (10.175.127.227) by dggpeml500021.china.huawei.com (7.185.36.21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Sat, 7 Jan 2023 10:56:55 +0800 From: Baokun Li To: CC: , , , , , , , , =?UTF-8?q?Lu=C3=ADs=20Henriques?= Subject: [PATCH v2 1/2] ext4: fail ext4_iget if special inode unallocated Date: Sat, 7 Jan 2023 11:21:25 +0800 Message-ID: <20230107032126.4165860-2-libaokun1@huawei.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230107032126.4165860-1-libaokun1@huawei.com> References: <20230107032126.4165860-1-libaokun1@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.175.127.227] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpeml500021.china.huawei.com (7.185.36.21) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In ext4_fill_super(), EXT4_ORPHAN_FS flag is cleared after ext4_orphan_cleanup() is executed. Therefore, when __ext4_iget() is called to get an inode whose i_nlink is 0 when the flag exists, no error is returned. If the inode is a special inode, a null pointer dereference may occur. If the value of i_nlink is 0 for any inodes (except boot loader inodes) got by using the EXT4_IGET_SPECIAL flag, the current file system is corrupted. Therefore, make the ext4_iget() function return an error if it gets such an abnormal special inode. Link: https://bugzilla.kernel.org/show_bug.cgi?id=3D199179 Link: https://bugzilla.kernel.org/show_bug.cgi?id=3D216541 Link: https://bugzilla.kernel.org/show_bug.cgi?id=3D216539 Reported-by: Lu=C3=ADs Henriques Suggested-by: Theodore Ts'o Signed-off-by: Baokun Li Reviewed-by: Jan Kara --- V1->V2: -EFSCORRUPTED is returned instead of -ESTALE for special inode errors, and the extra parentheses are removed. fs/ext4/inode.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 9d9f414f99fe..ed7598127e7c 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -4872,13 +4872,6 @@ struct inode *__ext4_iget(struct super_block *sb, un= signed long ino, goto bad_inode; raw_inode =3D ext4_raw_inode(&iloc); =20 - if ((ino =3D=3D EXT4_ROOT_INO) && (raw_inode->i_links_count =3D=3D 0)) { - ext4_error_inode(inode, function, line, 0, - "iget: root inode unallocated"); - ret =3D -EFSCORRUPTED; - goto bad_inode; - } - if ((flags & EXT4_IGET_HANDLE) && (raw_inode->i_links_count =3D=3D 0) && (raw_inode->i_mode =3D=3D 0)) { ret =3D -ESTALE; @@ -4951,11 +4944,16 @@ struct inode *__ext4_iget(struct super_block *sb, u= nsigned long ino, * NeilBrown 1999oct15 */ if (inode->i_nlink =3D=3D 0) { - if ((inode->i_mode =3D=3D 0 || + if ((inode->i_mode =3D=3D 0 || flags & EXT4_IGET_SPECIAL || !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) && ino !=3D EXT4_BOOT_LOADER_INO) { - /* this inode is deleted */ - ret =3D -ESTALE; + /* this inode is deleted or unallocated */ + if (flags & EXT4_IGET_SPECIAL) { + ext4_error_inode(inode, function, line, 0, + "iget: special inode unallocated"); + ret =3D -EFSCORRUPTED; + } else + ret =3D -ESTALE; goto bad_inode; } /* The only unlinked inodes we let through here have --=20 2.31.1 From nobody Tue Sep 16 02:28:48 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 21ACCC5479D for ; Sat, 7 Jan 2023 02:57:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236679AbjAGC5I (ORCPT ); Fri, 6 Jan 2023 21:57:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32830 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229950AbjAGC46 (ORCPT ); Fri, 6 Jan 2023 21:56:58 -0500 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B02CC8CD39; Fri, 6 Jan 2023 18:56:57 -0800 (PST) Received: from dggpeml500021.china.huawei.com (unknown [172.30.72.57]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4NplF435HNzRqpq; Sat, 7 Jan 2023 10:55:20 +0800 (CST) Received: from huawei.com (10.175.127.227) by dggpeml500021.china.huawei.com (7.185.36.21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Sat, 7 Jan 2023 10:56:55 +0800 From: Baokun Li To: CC: , , , , , , , , =?UTF-8?q?Lu=C3=ADs=20Henriques?= Subject: [PATCH v2 2/2] ext4: update s_journal_inum if it changes after journal replay Date: Sat, 7 Jan 2023 11:21:26 +0800 Message-ID: <20230107032126.4165860-3-libaokun1@huawei.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230107032126.4165860-1-libaokun1@huawei.com> References: <20230107032126.4165860-1-libaokun1@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.175.127.227] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpeml500021.china.huawei.com (7.185.36.21) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When mounting a crafted ext4 image, s_journal_inum may change after journal replay, which is obviously unreasonable because we have successfully loaded and replayed the journal through the old s_journal_inum. And the new s_journal_inum bypasses some of the checks in ext4_get_journal(), which may trigger a null pointer dereference problem. So if s_journal_inum changes after the journal replay, we ignore the change, and rewrite the current journal_inum to the superblock. Link: https://bugzilla.kernel.org/show_bug.cgi?id=3D216541 Reported-by: Lu=C3=ADs Henriques Signed-off-by: Baokun Li Reviewed-by: Jan Kara --- fs/ext4/super.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/fs/ext4/super.c b/fs/ext4/super.c index 260c1b3e3ef2..3fe9dc19ff9c 100644 --- a/fs/ext4/super.c +++ b/fs/ext4/super.c @@ -5953,8 +5953,11 @@ static int ext4_load_journal(struct super_block *sb, if (!really_read_only && journal_devnum && journal_devnum !=3D le32_to_cpu(es->s_journal_dev)) { es->s_journal_dev =3D cpu_to_le32(journal_devnum); - - /* Make sure we flush the recovery flag to disk. */ + ext4_commit_super(sb); + } + if (!really_read_only && journal_inum && + journal_inum !=3D le32_to_cpu(es->s_journal_inum)) { + es->s_journal_inum =3D cpu_to_le32(journal_inum); ext4_commit_super(sb); } =20 --=20 2.31.1