hfs: validate catalog CNIDs before writeback
Subsystems (2)
Hi Slava,
Thank you again for the previous review.
I reworked the HFS patch following your feedback. This version uses a focused CNID validator for catalog records.
The patch now:
- adds hfs_is_valid_cnid();
- validates file and directory CNIDs in hfs_read_inode();
- marks invalid catalog records as bad inodes;
- rejects a bad root inode in hfs_fill_super();
- validates the found catalog record in hfs_cat_find_brec();
- keeps BUG() in hfs_write_inode() for internal impossible state.
Local validation:
- git diff --check: pass
- reverse apply check: pass
- checkpatch strict: 0 errors
- make fs/hfs/: pass
- make bzImage: pass
- QEMU HFS repro: started and completed
- no kernel BUG, Oops, panic, KASAN, UBSAN, or GPF observed
The goal is to treat corrupted on-disk catalog metadata as filesystem corruption, without crashing the kernel.
Please let me know whether this matches the expected direction.
Thanks,
David
---
fs/hfs/catalog.c +30 -1
@@ -184,6 +184,31 @@ int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2)/* Try to get a catalog entry for given catalog id */ // move to read_super??? +static int hfs_cat_validate_found_cnid (struct hfs_find_data *fd) +{ +hfs_cat_rec rec; +int err; + + err =hfs_brec_read (fd, &rec,sizeof (rec)); +if (err) +return err; + +switch (rec.type) { +case HFS_CDR_FIL : +if (!hfs_is_valid_cnid (be32_to_cpu (rec.file.FlNum),HFS_CDR_FIL )) +return -EFSCORRUPTED ; +break ; +case HFS_CDR_DIR : +if (!hfs_is_valid_cnid (be32_to_cpu (rec.dir.DirID),HFS_CDR_DIR )) +return -EFSCORRUPTED ; +break ; +default : +return -EFSCORRUPTED ; + } + +return 0 ; +} +int hfs_cat_find_brec (struct super_block *sb,u32 cnid,struct hfs_find_data *fd) {
@@ -208,7 +233,11 @@ int hfs_cat_find_brec(struct super_block *sb, u32 cnid,return -EIO ; }memcpy (fd->search_key->cat.CName.name, rec.thread.CName.name, len); -return hfs_brec_find (fd); + err =hfs_brec_find (fd); +if (err) +return err; + +return hfs_cat_validate_found_cnid (fd); }
fs/hfs/hfs_fs.h +24 -0
@@ -186,6 +186,30 @@ extern void hfs_cat_build_key(struct super_block *, btree_key *, u32, const stru/* dir.c */ extern const struct file_operations hfs_dir_operations; + +/* +* Validate the CNID of a catalog record. +*/ +static inline bool hfs_is_valid_cnid (u32 cnid,u8 type) +{ +if (likely (cnid >=HFS_FIRSTUSER_CNID )) +return true; + +switch (cnid) { +case HFS_ROOT_CNID : +return type ==HFS_CDR_DIR ; +case HFS_EXT_CNID : +case HFS_CAT_CNID : +return type ==HFS_CDR_FIL ; +case HFS_POR_CNID : +case HFS_BAD_CNID : +case HFS_EXCH_CNID : +return false; +default : +return false; + } +} +extern const struct inode_operations hfs_dir_inode_operations;/* extent.c */
fs/hfs/inode.c +31 -15
72 lines not on this page · raw message
fs/hfs/super.c +1 -1
@@ -432,7 +432,7 @@ static int hfs_fill_super(struct super_block *sb, void *data, int silent) res = -EINVAL ; root_inode =hfs_iget (sb, &fd.search_key->cat, &rec);hfs_find_exit (&fd); -if (!root_inode) +if (!root_inode ||is_bad_inode (root_inode)) goto bail_no_root; sb->s_d_op = &hfs_dentry_operations;
Viacheslav Dubeyko
maintainerQuoted 41 lines · David Maximiliano Hermitte
Hi Slava,
Thank you again for the previous review.
I reworked the HFS patch following your feedback. This version uses a focused CNID validator for catalog records.
The patch now:
- adds
hfs_is_valid_cnid();
- validates file and directory CNIDs inhfs_read_inode();
- marks invalid catalog records as bad inodes;
- rejects a bad root inode inhfs_fill_super();
- validates the found catalog record inhfs_cat_find_brec();
- keepsBUG()inhfs_write_inode()for internal impossible state.Local validation:
- git diff --check: pass
- reverse apply check: pass
- checkpatch strict: 0 errors
- makefs/hfs/: pass
- make bzImage: pass
- QEMU HFS repro: started and completed
- no kernel BUG, Oops, panic, KASAN, UBSAN, or GPF observedThe goal is to treat corrupted on-disk catalog metadata as filesystem corruption, without crashing the kernel.
Please let me know whether this matches the expected direction.
Thanks,
David---
fs/hfs/catalog.c +0
-0@@ -184,6 +184,31 @@ int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2)
Quoted 3 lines
/* Try to get a catalog entry for given catalog id */ // move toread_super???
It is comment for the hfs_cat_find_brec().
fs/hfs/catalog.c int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2) · line 192
Quoted 6 lines
fs/hfs/catalog.c +6
-0+static int hfs_cat_validate_found_cnid (struct hfs_find_data *fd) +{ +hfs_cat_rec rec; +int err; + + err =hfs_brec_read (fd, &rec,sizeof (rec));
I think that hfs_brec_read() is not correct method here. We need to use hfs_bnode_read().
Quoted 7 lines
fs/hfs/catalog.c +6
-0+if (err) +return err; + + switch (rec.type) { + case HFS_CDR_FIL: + if (!hfs_is_valid_cnid(be32_to_cpu(rec.file.FlNum),
HFS_CDR_FIL))
We need to check not only validity but if we have the CNID that we tried to find.
Quoted 82 lines
fs/hfs/catalog.c +4
-0+return -EFSCORRUPTED ; +break ; +case HFS_CDR_DIR : + if (!hfs_is_valid_cnid(be32_to_cpu(rec.dir.DirID),
HFS_CDR_DIR))fs/hfs/catalog.c +9
-0+return -EFSCORRUPTED ; +break ; +default : +return -EFSCORRUPTED ; + } + +return 0 ; +} +int
hfs_cat_find_brec(struct super_block *sb, u32 cnid, struct hfs_find_data *fd){fs/hfs/catalog.c +5
-1@@ -208,7 +233,11 @@ int hfs_cat_find_brec(struct super_block *sb,u32 cnid, return -EIO; } memcpy(fd->search_key->cat.CName.name, rec.thread.CName.name, len); -return hfs_brec_find (fd); + err =hfs_brec_find (fd); +if (err) +return err; + +return hfs_cat_validate_found_cnid (fd); }
fs/hfs/hfs_fs.h +0
-0@@ -186,6 +186,30 @@ extern void hfs_cat_build_key(struct super_block *, btree_key *, u32, const stru
/*dir.c*/
extern const structfile_operationshfs_dir_operations;fs/hfs/hfs_fs.h +24
-0+ +/* +* Validate the CNID of a catalog record. +*/ +static inline bool hfs_is_valid_cnid (u32 cnid,u8 type) +{ +if (likely (cnid >=HFS_FIRSTUSER_CNID )) +return true; + +switch (cnid) { +case HFS_ROOT_CNID : +return type ==HFS_CDR_DIR ; +case HFS_EXT_CNID : +case HFS_CAT_CNID : +return type ==HFS_CDR_FIL ; +case HFS_POR_CNID : +case HFS_BAD_CNID : +case HFS_EXCH_CNID : +return false; +default : +return false; + } +} +extern const struct
inode_operationshfs_dir_inode_operations;
/*extent.c*/fs/hfs/inode.c +0
-0@@ -344,6 +344,11 @@ static int hfs_read_inode(struct inode *inode, void *data) rec = idata->rec; switch (rec->type) {case
HFS_CDR_FIL:fs/hfs/inode.c +1
-0+ if (!hfs_is_valid_cnid(be32_to_cpu(rec->file.FlNum),
HFS_CDR_FIL)) {fs/hfs/inode.c +1
-0+ pr_warn_ratelimited("hfs: invalid file CNID%u; run fsck\n",
fs/hfs/inode.c +1
-0+ be32_to_cpu(rec-
Quoted 1 line
Quoted 1 line
file.FlNum));
What about "running fsck.hfsplus is recommended"?
Quoted 31 lines
fs/hfs/inode.c +2
-0+ goto bad_inode; + }if (!HFS_IS_RSRC(inode)) { hfs_inode_read_fork(inode,
rec->file.ExtRec,rec->file.LgLen,
rec->file.PyLen,be16_to_cpu(rec->file.ClpSize));fs/hfs/inode.c +0
-0@@ -365,6 +370,11 @@ static int hfs_read_inode(struct inode *inode, void *data) inode->i_mapping->a_ops = &hfs_aops; break;case
HFS_CDR_DIR:fs/hfs/inode.c +1
-0+ if (!hfs_is_valid_cnid(be32_to_cpu(rec->dir.DirID),
HFS_CDR_DIR)) {fs/hfs/inode.c +1
-0+ pr_warn_ratelimited("hfs: invalid directoryCNID %u; run fsck\n",
fs/hfs/inode.c +1
-0+ be32_to_cpu(rec-Quoted 1 line
dir.DirID));
fs/hfs/inode.c +2
-0+ goto bad_inode; + }
inode->i_ino=be32_to_cpu(rec->dir.DirID);inode->i_size=be16_to_cpu(rec->dir.Val)+ 2; HFS_I(inode)->fs_blocks = 0;fs/hfs/inode.c +0
-0@@ -375,9 +385,15 @@ static int hfs_read_inode(struct inode *inode, void *data) inode->i_fop = &hfs_dir_operations; break;default:
fs/hfs/inode.c +1
-1-make_bad_inode (inode); + pr_warn_ratelimited("hfs: invalid catalog recordtype %u; run fsck\n",
fs/hfs/inode.c static int hfs_read_inode(struct inode *inode, void *data) · line 389
Quoted 1 line
fs/hfs/inode.c +1
-0+ rec->type);
I am not sure that if we make bad inode, then we always have corruption. I don't think that we need this message here.
fs/hfs/inode.c static int hfs_read_inode(struct inode *inode, void *data) · line 396
Quoted 7 lines
fs/hfs/inode.c +1
-0+ goto bad_inode;}
return 0;fs/hfs/inode.c +4
-0+ +bad_inode: +make_bad_inode (inode); +return 0 ;
What is this?
Thanks,
Slava.
Quoted 56 lines
}
/*fs/hfs/inode.c +0
-0@@ -435,20 +451,20 @@ int hfs_write_inode(struct inode *inode, struct writeback_control *wbc) if (res) return res;fs/hfs/inode.c +0
-6-if (inode->i_ino <HFS_FIRSTUSER_CNID ) { -switch (inode->i_ino) { -case HFS_ROOT_CNID : -break ; -case HFS_EXT_CNID : - hfs_btree_write(HFS_SB(inode->i_sb)-Quoted 1 line
ext_tree);fs/hfs/inode.c +0
-3-return 0 ; -case HFS_CAT_CNID : - hfs_btree_write(HFS_SB(inode->i_sb)-Quoted 1 line
cat_tree);fs/hfs/inode.c +2
-5-return 0 ; -default : -BUG (); -return -EIO ; - } + if (!hfs_is_valid_cnid(inode->i_ino, + S_ISDIR(inode->i_mode) ? HFS_CDR_DIR:
fs/hfs/inode.c +12
-0+ HFS_CDR_FIL)) +BUG (); + +switch (inode->i_ino) { +case HFS_EXT_CNID : +hfs_btree_write (HFS_SB (inode->i_sb)->ext_tree); +return 0 ; +case HFS_CAT_CNID : +hfs_btree_write (HFS_SB (inode->i_sb)->cat_tree); +return 0 ; +default : +break ;}
if (HFS_IS_RSRC(inode))fs/hfs/super.c +1
-1@@ -432,7 +432,7 @@ static int hfs_fill_super(struct super_block *sb, void *data, int silent) res = -EINVAL; root_inode = hfs_iget(sb, &fd.search_key->cat, &rec); hfs_find_exit(&fd); -if (!root_inode) +if (!root_inode ||is_bad_inode (root_inode)) goto bail_no_root;
sb->s_d_op=&hfs_dentry_operations;