]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
hfs: Sanity check the root record
authorLeo Stone <leocstone@gmail.com>
Sun, 1 Dec 2024 05:14:19 +0000 (21:14 -0800)
committerChristian Brauner <brauner@kernel.org>
Mon, 2 Dec 2024 14:32:19 +0000 (15:32 +0100)
In the syzbot reproducer, the hfs_cat_rec for the root dir has type
HFS_CDR_FIL after being read with hfs_bnode_read() in hfs_super_fill().
This indicates it should be used as an hfs_cat_file, which is 102 bytes.
Only the first 70 bytes of that struct are initialized, however,
because the entrylength passed into hfs_bnode_read() is still the length of
a directory record. This causes uninitialized values to be used later on,
when the hfs_cat_rec union is treated as the larger hfs_cat_file struct.

Add a check to make sure the retrieved record has the correct type
for the root directory (HFS_CDR_DIR), and make sure we load the correct
number of bytes for a directory record.

Reported-by: syzbot+2db3c7526ba68f4ea776@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=2db3c7526ba68f4ea776
Tested-by: syzbot+2db3c7526ba68f4ea776@syzkaller.appspotmail.com
Tested-by: Leo Stone <leocstone@gmail.com>
Signed-off-by: Leo Stone <leocstone@gmail.com>
Link: https://lore.kernel.org/r/20241201051420.77858-1-leocstone@gmail.com
Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/hfs/super.c

index 3bee9b5dba5e4771dd05711d3ea30c4b5ee721b7..fe09c2093a93d981c8d1341e14079b29c4d18137 100644 (file)
@@ -349,11 +349,13 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc)
                goto bail_no_root;
        res = hfs_cat_find_brec(sb, HFS_ROOT_CNID, &fd);
        if (!res) {
-               if (fd.entrylength > sizeof(rec) || fd.entrylength < 0) {
+               if (fd.entrylength != sizeof(rec.dir)) {
                        res =  -EIO;
                        goto bail_hfs_find;
                }
                hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, fd.entrylength);
+               if (rec.type != HFS_CDR_DIR)
+                       res = -EIO;
        }
        if (res)
                goto bail_hfs_find;