Skip to content

Commit

Permalink
hfs/hfsplus: avoid WARN_ON() for sanity check, use proper error handling
Browse files Browse the repository at this point in the history
commit cb7a95af78d29442b8294683eca4897544b8ef46 upstream.

Commit 55d1cbbbb29e ("hfs/hfsplus: use WARN_ON for sanity check") fixed
a build warning by turning a comment into a WARN_ON(), but it turns out
that syzbot then complains because it can trigger said warning with a
corrupted hfs image.

The warning actually does warn about a bad situation, but we are much
better off just handling it as the error it is.  So rather than warn
about us doing bad things, stop doing the bad things and return -EIO.

While at it, also fix a memory leak that was introduced by an earlier
fix for a similar syzbot warning situation, and add a check for one case
that historically wasn't handled at all (ie neither comment nor
subsequent WARN_ON).

Reported-by: [email protected]
Fixes: 55d1cbbbb29e ("hfs/hfsplus: use WARN_ON for sanity check")
Fixes: 8d824e69d9f3 ("hfs: fix OOB Read in __hfs_brec_find")
Link: https://lore.kernel.org/lkml/[email protected]/
Tested-by: Michael Schmitz <[email protected]>
Cc: Arnd Bergmann <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Viacheslav Dubeyko <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
torvalds authored and gregkh committed Jan 18, 2023
1 parent 781fa14 commit da23752
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions fs/hfs/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,15 +453,16 @@ int hfs_write_inode(struct inode *inode, struct writeback_control *wbc)
/* panic? */
return -EIO;

res = -EIO;
if (HFS_I(main_inode)->cat_key.CName.len > HFS_NAMELEN)
return -EIO;
goto out;
fd.search_key->cat = HFS_I(main_inode)->cat_key;
if (hfs_brec_find(&fd))
/* panic? */
goto out;

if (S_ISDIR(main_inode->i_mode)) {
WARN_ON(fd.entrylength < sizeof(struct hfs_cat_dir));
if (fd.entrylength < sizeof(struct hfs_cat_dir))
goto out;
hfs_bnode_read(fd.bnode, &rec, fd.entryoffset,
sizeof(struct hfs_cat_dir));
if (rec.type != HFS_CDR_DIR ||
Expand All @@ -474,14 +475,17 @@ int hfs_write_inode(struct inode *inode, struct writeback_control *wbc)
hfs_bnode_write(fd.bnode, &rec, fd.entryoffset,
sizeof(struct hfs_cat_dir));
} else if (HFS_IS_RSRC(inode)) {
if (fd.entrylength < sizeof(struct hfs_cat_file))
goto out;
hfs_bnode_read(fd.bnode, &rec, fd.entryoffset,
sizeof(struct hfs_cat_file));
hfs_inode_write_fork(inode, rec.file.RExtRec,
&rec.file.RLgLen, &rec.file.RPyLen);
hfs_bnode_write(fd.bnode, &rec, fd.entryoffset,
sizeof(struct hfs_cat_file));
} else {
WARN_ON(fd.entrylength < sizeof(struct hfs_cat_file));
if (fd.entrylength < sizeof(struct hfs_cat_file))
goto out;
hfs_bnode_read(fd.bnode, &rec, fd.entryoffset,
sizeof(struct hfs_cat_file));
if (rec.type != HFS_CDR_FIL ||
Expand All @@ -498,9 +502,10 @@ int hfs_write_inode(struct inode *inode, struct writeback_control *wbc)
hfs_bnode_write(fd.bnode, &rec, fd.entryoffset,
sizeof(struct hfs_cat_file));
}
res = 0;
out:
hfs_find_exit(&fd);
return 0;
return res;
}

static struct dentry *hfs_file_lookup(struct inode *dir, struct dentry *dentry,
Expand Down

0 comments on commit da23752

Please sign in to comment.