public inbox for linux-fsdevel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] hfs: Sanity check the root record
@ 2024-12-01  5:14 Leo Stone
  2024-12-02 12:24 ` Jan Kara
  2024-12-02 14:32 ` Christian Brauner
  0 siblings, 2 replies; 3+ messages in thread
From: Leo Stone @ 2024-12-01  5:14 UTC (permalink / raw)
  To: brauner, sandeen, jack, viro, quic_jjohnson
  Cc: Leo Stone, linux-fsdevel, linux-kernel,
	syzbot+2db3c7526ba68f4ea776

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>
---
v2: Made the check on fd.entrylength more strict. Tested with real HFS
    images.
v1: https://lore.kernel.org/all/20241123194949.9243-1-leocstone@gmail.com
---
 fs/hfs/super.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/fs/hfs/super.c b/fs/hfs/super.c
index 3bee9b5dba5e..fe09c2093a93 100644
--- a/fs/hfs/super.c
+++ b/fs/hfs/super.c
@@ -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;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] hfs: Sanity check the root record
  2024-12-01  5:14 [PATCH v2] hfs: Sanity check the root record Leo Stone
@ 2024-12-02 12:24 ` Jan Kara
  2024-12-02 14:32 ` Christian Brauner
  1 sibling, 0 replies; 3+ messages in thread
From: Jan Kara @ 2024-12-02 12:24 UTC (permalink / raw)
  To: Leo Stone
  Cc: brauner, sandeen, jack, viro, quic_jjohnson, linux-fsdevel,
	linux-kernel, syzbot+2db3c7526ba68f4ea776

On Sat 30-11-24 21:14:19, Leo Stone wrote:
> 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>

Looks good. Feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza
> ---
> v2: Made the check on fd.entrylength more strict. Tested with real HFS
>     images.
> v1: https://lore.kernel.org/all/20241123194949.9243-1-leocstone@gmail.com
> ---
>  fs/hfs/super.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/hfs/super.c b/fs/hfs/super.c
> index 3bee9b5dba5e..fe09c2093a93 100644
> --- a/fs/hfs/super.c
> +++ b/fs/hfs/super.c
> @@ -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;
> -- 
> 2.43.0
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] hfs: Sanity check the root record
  2024-12-01  5:14 [PATCH v2] hfs: Sanity check the root record Leo Stone
  2024-12-02 12:24 ` Jan Kara
@ 2024-12-02 14:32 ` Christian Brauner
  1 sibling, 0 replies; 3+ messages in thread
From: Christian Brauner @ 2024-12-02 14:32 UTC (permalink / raw)
  To: Leo Stone
  Cc: Christian Brauner, linux-fsdevel, linux-kernel,
	syzbot+2db3c7526ba68f4ea776, sandeen, jack, viro, quic_jjohnson

On Sat, 30 Nov 2024 21:14:19 -0800, Leo Stone wrote:
> 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.
> 
> [...]

Applied to the vfs.fixes branch of the vfs/vfs.git tree.
Patches in the vfs.fixes branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a
new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the
patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase,
trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs.fixes

[1/1] hfs: Sanity check the root record
      https://git.kernel.org/vfs/vfs/c/b905bafdea21

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-12-02 14:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-01  5:14 [PATCH v2] hfs: Sanity check the root record Leo Stone
2024-12-02 12:24 ` Jan Kara
2024-12-02 14:32 ` Christian Brauner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox