hfs: validate catalog CNIDs before instantiating inodes
Subsystems (2)
PATCH 1/1 hfs: validate catalog CNIDs before instantiating inodes
hfs_cat_find_brec() first resolves a catalog thread record by CNID and then looks up the corresponding catalog record by parent/name. On a corrupted filesystem image, the second lookup may find a record whose CNID does not match the CNID that was requested.
Validate the catalog record found by the second lookup before returning it to callers. Inspect the already-found record with hfs_bnode_read(), not hfs_brec_read(), and reject records whose CNID is invalid for their record type or does not match the requested CNID.
Also validate CNIDs in hfs_read_inode() before the inode is populated, and propagate hfs_read_inode() errors from the resource-fork lookup path. For the root inode path, require the root catalog record to be a directory with DirID == HFS_ROOT_CNID, and drop the root inode reference if it was instantiated as a bad inode.
This keeps hfs_write_inode() unchanged and prevents corrupted catalog records from reaching the existing reserved-CNID BUG() path during writeback.
--- fs/hfs/catalog.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- fs/hfs/hfs_fs.h | 19 +++++++++++++++++++ fs/hfs/inode.c | 9 ++++++++- fs/hfs/super.c | 8 +++++++- 4 files changed, 81 insertions(+), 3 deletions(-)
fs/hfs/catalog.c +47 -1
@@ -182,6 +182,47 @@ int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2) key2->cat.CName.name, key2->cat.CName.len); } +static int hfs_cat_validate_found_cnid (struct hfs_find_data *fd,u32 cnid) +{ +hfs_cat_rec rec; +u32 found_cnid; +unsigned int rec_len; +size_t cnid_off; + +if (fd->entrylength <=0 ) +return -EIO ; + +if ((unsigned int )fd->entrylength >sizeof (rec)) + rec_len =sizeof (rec); +else + rec_len = fd->entrylength; + +memset (&rec,0 ,sizeof (rec)); +hfs_bnode_read (fd->bnode, &rec, fd->entryoffset, rec_len); + +switch (rec.type) { +case HFS_CDR_FIL : + cnid_off = offsetof(struct hfs_cat_file , FlNum); +if ((size_t )rec_len < cnid_off +sizeof (rec.file.FlNum)) +return -EIO ; + found_cnid =be32_to_cpu (rec.file.FlNum); +break ; +case HFS_CDR_DIR : + cnid_off = offsetof(struct hfs_cat_dir , DirID); +if ((size_t )rec_len < cnid_off +sizeof (rec.dir.DirID)) +return -EIO ; + found_cnid =be32_to_cpu (rec.dir.DirID); +break ; +default : +return -EIO ; + } + +if (!hfs_is_valid_cnid (found_cnid, rec.type) || found_cnid != cnid) +return -EIO ; + +return 0 ; +} +/* Try to get a catalog entry for given catalog id */ // move to read_super??? int hfs_cat_find_brec(struct super_block *sb, u32 cnid,
@@ -208,7 +249,12 @@ 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); + + res =hfs_brec_find (fd); +if (res) +return res; + +return hfs_cat_validate_found_cnid (fd, cnid); } static inline
fs/hfs/hfs_fs.h +19 -0
@@ -155,6 +155,25 @@ extern int hfs_cat_move(u32 cnid, struct inode *src_dir,extern void hfs_cat_build_key (struct super_block *sb,btree_key *key,u32 parent,const struct qstr *name); +/* +* 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 ; +default : +return false; + } +} +/* dir.c */ extern const struct file_operations hfs_dir_operations;extern const struct inode_operations hfs_dir_inode_operations;
fs/hfs/inode.c +8 -1
@@ -367,6 +367,9 @@ static int hfs_read_inode(struct inode *inode, void *data) rec = idata->rec;switch (rec->type) {case HFS_CDR_FIL : +if (!hfs_is_valid_cnid (be32_to_cpu (rec->file.FlNum), rec->type)) +return -EIO ; +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));
@@ -390,6 +393,9 @@ static int hfs_read_inode(struct inode *inode, void *data) inode->i_mapping->a_ops = &hfs_aops;break ;case HFS_CDR_DIR : +if (!hfs_is_valid_cnid (be32_to_cpu (rec->dir.DirID), rec->type)) +return -EIO ; + 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 ;
@@ -571,7 +577,8 @@ static struct dentry *hfs_file_lookup(struct inode *dir, struct dentry *dentry, res =hfs_brec_read (&fd, &rec,sizeof (rec));if (!res) {struct hfs_iget_data idata = {NULL , &rec }; -hfs_read_inode (inode, &idata); + + res =hfs_read_inode (inode, &idata); }hfs_find_exit (&fd);if (res) {
fs/hfs/super.c +7 -1
@@ -361,7 +361,8 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc) goto bail_hfs_find; }hfs_bnode_read (fd.bnode, &rec, fd.entryoffset, fd.entrylength); -if (rec.type !=HFS_CDR_DIR ) +if (rec.type !=HFS_CDR_DIR || +be32_to_cpu (rec.dir.DirID) !=HFS_ROOT_CNID ) res = -EIO ; }if (res)
@@ -372,6 +373,11 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc)if (!root_inode) goto bail_no_root; +if (is_bad_inode (root_inode)) { +iput (root_inode); + goto bail_no_root; + } +set_default_d_op (sb, &hfs_dentry_operations); res = -ENOMEM ; sb->s_root =d_make_root (root_inode);
Signature · 1 line
2.43.0
Sashiko has some comments on this patch:
https://sashiko.dev/#/patchset/20260702183800.1119202-1-davemadmaxxx%40gmail.com
Quoted 31 lines · David Maximiliano Hermitte
hfs_cat_find_brec()first resolves a catalog thread record by CNID and then looks up the corresponding catalog record by parent/name. On a corrupted filesystem image, the second lookup may find a record whose CNID does not match the CNID that was requested.Validate the catalog record found by the second lookup before returning it to callers. Inspect the already-found record with
hfs_bnode_read(), nothfs_brec_read(), and reject records whose CNID is invalid for their record type or does not match the requested CNID.Also validate CNIDs in
hfs_read_inode()before the inode is populated, and propagatehfs_read_inode()errors from the resource-fork lookup path. For the root inode path, require the root catalog record to be a directory with DirID ==HFS_ROOT_CNID, and drop the root inode reference if it was instantiated as a bad inode.This keeps
hfs_write_inode()unchanged and prevents corrupted catalog records from reaching the existing reserved-CNIDBUG()path during writeback.Reported-by [email protected]Cc George Anthony Vernon <[email protected]>Cc Tetsuo Handa <[email protected]>Signed-off-by David Maximiliano Hermitte <[email protected]>--- fs/hfs/catalog.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- fs/hfs/hfs_fs.h | 19 +++++++++++++++++++ fs/hfs/inode.c | 9 ++++++++- fs/hfs/super.c | 8 +++++++- 4 files changed, 81 insertions(+), 3 deletions(-)
Viacheslav Dubeyko
maintainerQuoted 53 lines · David Maximiliano Hermitte
hfs_cat_find_brec()first resolves a catalog thread record by CNID and
then looks up the corresponding catalog record by parent/name. On a corrupted filesystem image, the second lookup may find a record whose CNID does not match the CNID that was requested.Validate the catalog record found by the second lookup before returning
it to callers. Inspect the already-found record withhfs_bnode_read(),
nothfs_brec_read(), and reject records whose CNID is invalid for their
record type or does not match the requested CNID.Also validate CNIDs in
hfs_read_inode()before the inode is populated,
and propagatehfs_read_inode()errors from the resource-fork lookup path.
For the root inode path, require the root catalog record to be a directory
with DirID ==HFS_ROOT_CNID, and drop the root inode reference if it was
instantiated as a bad inode.This keeps
hfs_write_inode()unchanged and prevents corrupted catalog records from reaching the existing reserved-CNIDBUG()path during writeback.Reported-by [email protected]Cc George Anthony Vernon <[email protected]>Cc Tetsuo Handa <[email protected]>Signed-off-by David Maximiliano Hermitte <[email protected]>---
fs/hfs/catalog.c | 48
+++++++++++++++++++++++++++++++++++++++++++++++-
fs/hfs/hfs_fs.h | 19 +++++++++++++++++++
fs/hfs/inode.c | 9 ++++++++-
fs/hfs/super.c | 8 +++++++-4 files changed, 81 insertions(+), 3 deletions(-)fs/hfs/catalog.c +0
-0@@ -182,6 +182,47 @@ int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2)
key2->cat.CName.name, key2-Quoted 1 line
cat.CName.len);
}
+static inthfs_cat_validate_found_cnid(struct hfs_find_data *fd, u32 cnid)
fs/hfs/catalog.c int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2) · line 189
Quoted 4 lines
fs/hfs/catalog.c +4
-0+{ +hfs_cat_rec rec; +u32 found_cnid; +unsigned int rec_len;
I prefer to see the size_t here.
fs/hfs/catalog.c int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2) · line 195
Quoted 6 lines
fs/hfs/catalog.c +6
-0+size_t cnid_off; + +if (fd->entrylength <=0 ) +return -EIO ; + +if ((unsigned int )fd->entrylength >sizeof (rec))
The hfs_cat_rec union is big enough to keep any type of record in Catalog File. If fd->entrylength is bigger one, then it sounds like a incorrect behavior or corruption. We cannot silently process such situation.
fs/hfs/catalog.c
Quoted 13 lines
fs/hfs/catalog.c +11
-0+ rec_len =sizeof (rec); + else + rec_len = fd->entrylength; + +memset (&rec,0 ,sizeof (rec)); +hfs_bnode_read (fd->bnode, &rec, fd->entryoffset, rec_len); + + switch (rec.type) { + case HFS_CDR_FIL: + cnid_off = offsetof(struct hfs_cat_file, FlNum); + if ((size_t)rec_len < cnid_off +sizeof(rec.file.FlNum))
fs/hfs/catalog.c +1
-0+return -EIO ;
If you would like to check that size of the record, then you don't need to do this weird logic.
typedef union hfs_cat_rec {
s8 type; /* The type of entry */
struct hfs_cat_file file;
struct hfs_cat_dir dir;
struct hfs_cat_thread thread;
} hfs_cat_rec;You can simply compare with particular structure size.
fs/hfs/catalog.c
Quoted 7 lines
fs/hfs/catalog.c +5
-0+ found_cnid =be32_to_cpu (rec.file.FlNum); +break ; +case HFS_CDR_DIR : + cnid_off = offsetof(struct hfs_cat_dir , DirID); + if ((size_t)rec_len < cnid_off +sizeof(rec.dir.DirID))
fs/hfs/catalog.c +1
-0+return -EIO ;
Ditto.
fs/hfs/catalog.c int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2) · line 217
Quoted 4 lines
fs/hfs/catalog.c +4
-0+ found_cnid =be32_to_cpu (rec.dir.DirID); +break ; +default : +return -EIO ;
Are we sure that thread record cannot be checked by this logic?
Quoted 4 lines
fs/hfs/catalog.c +3
-0+ } + + if (!hfs_is_valid_cnid(found_cnid, rec.type) || found_cnid!= cnid)
We don't need to call hfs_is_valid_cnid() because found_cnid != cnid is completely enough.
Quoted 87 lines
fs/hfs/catalog.c +5
-0+return -EIO ; + +return 0 ; +} +/* Try to get a catalog entry for given catalog id */ // move to
read_super???
int hfs_cat_find_brec(structsuper_block*sb, u32 cnid,fs/hfs/catalog.c +6
-1@@ -208,7 +249,12 @@ 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); + + res =hfs_brec_find (fd); +if (res) +return res; + +return hfs_cat_validate_found_cnid (fd, cnid); }
static inlinefs/hfs/hfs_fs.h +0
-0@@ -155,6 +155,25 @@ extern int hfs_cat_move(u32 cnid, struct inode *src_dir,extern void hfs_cat_build_key(struct
super_block*sb,btree_key*key,
u32 parent, const struct qstr *name);fs/hfs/hfs_fs.h +19
-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 ; +default : +return false; + } +} +/*
dir.c*/
extern const structfile_operationshfs_dir_operations; extern const structinode_operationshfs_dir_inode_operations;fs/hfs/inode.c +0
-0@@ -367,6 +367,9 @@ 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),
rec->type))fs/hfs/inode.c +2
-0+return -EIO ; +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@@ -390,6 +393,9 @@ 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),
rec->type))fs/hfs/inode.c +2
-0+return -EIO ; +
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 static struct dentry *hfs_file_lookup(struct · line 581
Quoted 6 lines
fs/hfs/inode.c +1
-1@@ -571,7 +577,8 @@ static struct dentry *hfs_file_lookup(struct inode *dir, struct dentry *dentry, res = hfs_brec_read(&fd, &rec, sizeof(rec)); if (!res) { struct hfs_iget_data idata = { NULL, &rec }; -hfs_read_inode (inode, &idata); +
Do we really need empty line here?
fs/hfs/inode.c static struct dentry *hfs_file_lookup(struct inode *dir, struct dentry *dentry, · line 583
Quoted 3 lines
fs/hfs/inode.c +1
-0+ res =hfs_read_inode (inode, &idata); } hfs_find_exit(&fd);
I believe we need something like this here:
if (res || is_bad_inode(inode)) {
iput(inode);
return ERR_PTR(-EIO);
}
Quoted 6 lines
if (res) {
fs/hfs/super.c +0
-0
fs/hfs/super.c static int hfs_fill_super(struct super_block *sb, · line 367
Quoted 8 lines
fs/hfs/super.c +2
-1@@ -361,7 +361,8 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc) goto bail_hfs_find; } hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, fd.entrylength); -if (rec.type !=HFS_CDR_DIR ) +if (rec.type !=HFS_CDR_DIR || + be32_to_cpu(rec.dir.DirID) != HFS_ROOT_CNID)
This check is completely not necessary here because hfs_cat_validate_found_cnid() will execute the CNID check.
Thanks,
Slava.
Quoted 16 lines
fs/hfs/super.c +0
-0res = -EIO;}
if (res)fs/hfs/super.c +0
-0@@ -372,6 +373,11 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc) if (!root_inode) goto bail_no_root;fs/hfs/super.c +5
-0+if (is_bad_inode (root_inode)) { +iput (root_inode); + goto bail_no_root; + } +
set_default_d_op(sb, &hfs_dentry_operations); res =-ENOMEM;
sb->s_root=d_make_root(root_inode);
PATCH RESEND v3 1/1 hfs: validate catalog CNIDs before instantiating inodes
In reply to David Maximiliano Hermittehfs_cat_find_brec() first resolves a catalog thread record by CNID and then looks up the corresponding catalog record by parent/name. On a corrupted filesystem image, the second lookup may find a record whose CNID does not match the CNID that was requested.
Validate the catalog record found by the second lookup before returning it to callers. Inspect the already-found record with hfs_bnode_read(), not hfs_brec_read(), and reject records whose CNID is invalid for their record type or does not match the requested CNID.
Also validate CNIDs in hfs_read_inode() before the inode is populated, and propagate hfs_read_inode() errors from the resource-fork lookup path. For the root inode path, require the root catalog record to be a directory with DirID == HFS_ROOT_CNID, and drop the root inode reference if it was instantiated as a bad inode.
This keeps hfs_write_inode() unchanged and prevents corrupted catalog records from reaching the existing reserved-CNID BUG() path during writeback.
---
Changes in v3:
- Drop the stray blank line before assigning the hfs_read_inode() result in hfs_file_lookup().
Changes in v2:
- Use size_t for the catalog record length.
- Return -EIO when fd->entrylength is larger than hfs_cat_rec instead of truncating the record length.
- Compare catalog record lengths against sizeof(struct hfs_cat_file) and sizeof(struct hfs_cat_dir).
- Drop the hfs_is_valid_cnid() call from the found-record CNID comparison; found_cnid != cnid is enough there.
- Treat non-file/non-directory records found by the second lookup as invalid for this path.
- Check is_bad_inode() in the resource-fork lookup error path.
- Drop the redundant root DirID check from hfs_fill_super().
fs/hfs/catalog.c | 44 +++++++++++++++++++++++++++++++++++++++++++- fs/hfs/hfs_fs.h | 19 +++++++++++++++++++ fs/hfs/inode.c | 12 +++++++++--- fs/hfs/super.c | 5 +++++ 4 files changed, 76 insertions(+), 4 deletions(-)
fs/hfs/catalog.c +43 -1
@@ -182,6 +182,43 @@ int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2) key2->cat.CName.name, key2->cat.CName.len); } +static int hfs_cat_validate_found_cnid (struct hfs_find_data *fd,u32 cnid) +{ +hfs_cat_rec rec; +u32 found_cnid; +size_t rec_len; + +if (fd->entrylength <=0 ) +return -EIO ; + + rec_len = fd->entrylength; +if (rec_len >sizeof (rec)) +return -EIO ; + +memset (&rec,0 ,sizeof (rec)); +hfs_bnode_read (fd->bnode, &rec, fd->entryoffset, rec_len); + +switch (rec.type) { +case HFS_CDR_FIL : +if (rec_len <sizeof (struct hfs_cat_file )) +return -EIO ; + found_cnid =be32_to_cpu (rec.file.FlNum); +break ; +case HFS_CDR_DIR : +if (rec_len <sizeof (struct hfs_cat_dir )) +return -EIO ; + found_cnid =be32_to_cpu (rec.dir.DirID); +break ; +default : +return -EIO ; + } + +if (found_cnid != cnid) +return -EIO ; + +return 0 ; +} +/* Try to get a catalog entry for given catalog id */ // move to read_super??? int hfs_cat_find_brec(struct super_block *sb, u32 cnid,
@@ -208,7 +245,12 @@ 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); + + res =hfs_brec_find (fd); +if (res) +return res; + +return hfs_cat_validate_found_cnid (fd, cnid); } static inline
fs/hfs/hfs_fs.h +19 -0
@@ -155,6 +155,25 @@ extern int hfs_cat_move(u32 cnid, struct inode *src_dir,extern void hfs_cat_build_key (struct super_block *sb,btree_key *key,u32 parent,const struct qstr *name); +/* +* 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 ; +default : +return false; + } +} +/* dir.c */ extern const struct file_operations hfs_dir_operations;extern const struct inode_operations hfs_dir_inode_operations;
fs/hfs/inode.c +9 -3
@@ -367,6 +367,9 @@ static int hfs_read_inode(struct inode *inode, void *data) rec = idata->rec;switch (rec->type) {case HFS_CDR_FIL : +if (!hfs_is_valid_cnid (be32_to_cpu (rec->file.FlNum), rec->type)) +return -EIO ; +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));
@@ -390,6 +393,9 @@ static int hfs_read_inode(struct inode *inode, void *data) inode->i_mapping->a_ops = &hfs_aops;break ;case HFS_CDR_DIR : +if (!hfs_is_valid_cnid (be32_to_cpu (rec->dir.DirID), rec->type)) +return -EIO ; + 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 ;
@@ -571,12 +577,12 @@ static struct dentry *hfs_file_lookup(struct inode *dir, struct dentry *dentry, res =hfs_brec_read (&fd, &rec,sizeof (rec));if (!res) {struct hfs_iget_data idata = {NULL , &rec }; -hfs_read_inode (inode, &idata); + res =hfs_read_inode (inode, &idata); }hfs_find_exit (&fd); -if (res) { +if (res ||is_bad_inode (inode)) {iput (inode); -return ERR_PTR (res); +return ERR_PTR (-EIO ); }HFS_I (inode)->rsrc_inode = dir;HFS_I (dir)->rsrc_inode = inode;
fs/hfs/super.c +5 -0
@@ -372,6 +372,11 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc)if (!root_inode) goto bail_no_root; +if (is_bad_inode (root_inode)) { +iput (root_inode); + goto bail_no_root; + } +set_default_d_op (sb, &hfs_dentry_operations); res = -ENOMEM ; sb->s_root =d_make_root (root_inode);
Signature · 1 line
2.43.0
Viacheslav Dubeyko
maintainerQuoted 73 lines · David Maximiliano Hermitte
hfs_cat_find_brec()first resolves a catalog thread record by CNID and
then looks up the corresponding catalog record by parent/name. On a corrupted filesystem image, the second lookup may find a record whose CNID does not match the CNID that was requested.Validate the catalog record found by the second lookup before returning
it to callers. Inspect the already-found record withhfs_bnode_read(),
nothfs_brec_read(), and reject records whose CNID is invalid for their
record type or does not match the requested CNID.Also validate CNIDs in
hfs_read_inode()before the inode is populated,
and propagatehfs_read_inode()errors from the resource-fork lookup path.
For the root inode path, require the root catalog record to be a directory
with DirID ==HFS_ROOT_CNID, and drop the root inode reference if it was
instantiated as a bad inode.This keeps
hfs_write_inode()unchanged and prevents corrupted catalog records from reaching the existing reserved-CNIDBUG()path during writeback.Reported-by [email protected]Cc George Anthony Vernon <[email protected]>Cc Tetsuo Handa <[email protected]>Signed-off-by David Maximiliano Hermitte <[email protected]>---
Changes in v3:
- Drop the stray blank line before assigning thehfs_read_inode()result
inhfs_file_lookup().Changes in v2:
- Usesize_tfor the catalog record length.
- Return-EIOwhenfd->entrylengthis larger thanhfs_cat_recinstead of
truncating the record length.
- Compare catalog record lengths againstsizeof(struct hfs_cat_file)and
sizeof(struct hfs_cat_dir).
- Drop thehfs_is_valid_cnid()call from the found-record CNID comparison;found_cnid!= cnid is enough there.
- Treat non-file/non-directory records found by the second lookup as invalid for this path.
- Checkis_bad_inode()in the resource-fork lookup error path.
- Drop the redundant root DirID check fromhfs_fill_super().fs/hfs/catalog.c | 44 +++++++++++++++++++++++++++++++++++++++++++- fs/hfs/hfs_fs.h | 19 +++++++++++++++++++ fs/hfs/inode.c | 12 +++++++++---
fs/hfs/super.c | 5 +++++4 files changed, 76 insertions(+), 4 deletions(-)fs/hfs/catalog.c +0
-0@@ -182,6 +182,43 @@ int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2)
key2->cat.CName.name, key2-Quoted 1 line
cat.CName.len);
}
+static inthfs_cat_validate_found_cnid(struct hfs_find_data *fd, u32 cnid)fs/hfs/catalog.c +1
-0+{
fs/hfs/catalog.c int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2) · line 196
Quoted 10 lines
fs/hfs/catalog.c +10
-0+hfs_cat_rec rec; +u32 found_cnid; +size_t rec_len; + +if (fd->entrylength <=0 ) +return -EIO ; + + rec_len = fd->entrylength; +if (rec_len >sizeof (rec)) +return -EIO ;
Why do not have one check?
rec_len = fd->entrylength;
if (rec_len <= 0 || rec_len > sizeof(rec))
return -EIO;
fs/hfs/catalog.c int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2) · line 203
Quoted 7 lines
fs/hfs/catalog.c +7
-0+ +memset (&rec,0 ,sizeof (rec)); +hfs_bnode_read (fd->bnode, &rec, fd->entryoffset, rec_len); + +switch (rec.type) { +case HFS_CDR_FIL : +if (rec_len <sizeof (struct hfs_cat_file ))
rec_len != sizeof(struct hfs_cat_file)?
fs/hfs/catalog.c int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2) · line 208
Quoted 5 lines
fs/hfs/catalog.c +5
-0+return -EIO ; + found_cnid =be32_to_cpu (rec.file.FlNum); +break ; +case HFS_CDR_DIR : +if (rec_len <sizeof (struct hfs_cat_dir ))
rec_len != sizeof(struct hfs_cat_dir)?
Quoted 69 lines
fs/hfs/catalog.c +13
-0+return -EIO ; + found_cnid =be32_to_cpu (rec.dir.DirID); +break ; +default : +return -EIO ; + } + +if (found_cnid != cnid) +return -EIO ; + +return 0 ; +} +/* Try to get a catalog entry for given catalog id */ // move to
read_super???
int hfs_cat_find_brec(structsuper_block*sb, u32 cnid,fs/hfs/catalog.c +6
-1@@ -208,7 +245,12 @@ 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); + + res =hfs_brec_find (fd); +if (res) +return res; + +return hfs_cat_validate_found_cnid (fd, cnid); }
static inlinefs/hfs/hfs_fs.h +0
-0@@ -155,6 +155,25 @@ extern int hfs_cat_move(u32 cnid, struct inode *src_dir,extern void hfs_cat_build_key(struct
super_block*sb,btree_key*key,
u32 parent, const struct qstr *name);fs/hfs/hfs_fs.h +19
-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 ; +default : +return false; + } +} +/*
dir.c*/
extern const structfile_operationshfs_dir_operations; extern const structinode_operationshfs_dir_inode_operations;fs/hfs/inode.c +0
-0
fs/hfs/inode.c static int hfs_read_inode(struct inode *inode, · line 367
Quoted 1 line
fs/hfs/inode.c +0
-0@@ -367,6 +367,9 @@ static int hfs_read_inode(struct inode *inode, void *data)
Have you decided not to have the hfs_is_valid_cnid() for the case of hfs_write_inode()?
Thanks,
Slava.
Quoted 58 lines
fs/hfs/inode.c +0
-0rec = 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),
rec->type))fs/hfs/inode.c +2
-0+return -EIO ; +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@@ -390,6 +393,9 @@ 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),
rec->type))fs/hfs/inode.c +2
-0+return -EIO ; +
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 +3
-3@@ -571,12 +577,12 @@ static struct dentry *hfs_file_lookup(struct inode *dir, struct dentry *dentry, res = hfs_brec_read(&fd, &rec, sizeof(rec)); if (!res) { struct hfs_iget_data idata = { NULL, &rec }; -hfs_read_inode (inode, &idata); + res =hfs_read_inode (inode, &idata); } hfs_find_exit(&fd); -if (res) { +if (res ||is_bad_inode (inode)) { iput(inode); -return ERR_PTR (res); +return ERR_PTR (-EIO ); } HFS_I(inode)->rsrc_inode = dir;HFS_I(dir)->rsrc_inode = inode;
fs/hfs/super.c +0
-0@@ -372,6 +372,11 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc) if (!root_inode) goto bail_no_root;fs/hfs/super.c +5
-0+if (is_bad_inode (root_inode)) { +iput (root_inode); + goto bail_no_root; + } +
set_default_d_op(sb, &hfs_dentry_operations); res =-ENOMEM;
sb->s_root=d_make_root(root_inode);
A message that is not in the archive (sent off-list or not yet received): CADy8qZgo3rKwUiPr60tzPhaisMy7R71vjTtPi7+jEXgcarihRw@mail.gmail.com
Viacheslav Dubeyko
maintainerQuoted 14 lines · dave mad maxXx
Hi Slava,
Thank you for the review.
Yes, I agree that the initial length validation can be simplified to:rec_len=fd->entrylength;
if (rec_len<= 0 ||rec_len> sizeof(rec))
return-EIO;I also agree that, for the fixed-size file and directory catalog records, the checks should require the exact record size:
rec_len!=sizeof(struct hfs_cat_file)and:
rec_len!=sizeof(struct hfs_cat_dir)
Quoted 9 lines
Regarding
hfs_write_inode(), I intentionally left it unchanged because the patch was trying to reject invalid CNIDs at the catalog lookup and inode-instantiation boundaries, before a corrupted inode could reach writeback.
However, I agree that an additional validation there may be useful as defense in depth. Would you preferhfs_is_valid_cnid()to be checked inhfs_write_inode()as well, and should that check precede the existing BUG path while otherwise leaving its behavior unchanged? I will prepare the next revision after your guidance on that point.
Let's leave hfs_write_inode() untouched. I don't see a valid point in this modification.
Thanks,
Slava.
PATCH v4 1/1 hfs: validate catalog CNIDs before instantiating inodes
In reply to David Maximiliano Hermittehfs_cat_find_brec() first resolves a catalog thread record by CNID and then looks up the corresponding catalog record by parent/name. On a corrupted filesystem image, the second lookup may find a record whose CNID does not match the CNID that was requested.
Validate the record found by the second lookup before returning it. Read the already-found record with hfs_bnode_read(), require the exact fixed size for file and directory records, reject other record types, and verify that the stored CNID matches the requested CNID.
Also validate reserved CNIDs in hfs_read_inode() before populating the inode, propagate hfs_read_inode() failures from the resource-fork lookup path, and reject bad resource-fork and root inodes.
Keep hfs_write_inode() unchanged, so corrupted catalog records are rejected before reaching its existing reserved-CNID BUG() path.
---
Changes in v4:
- Combine the catalog record length validation into one check.
- Require exact sizes for file and directory catalog records.
- Keep hfs_write_inode() unchanged following maintainer feedback.
- Revalidate the exact final patch with the HFS build and QEMU repro.
Changes in v3:
- Drop the stray blank line in hfs_file_lookup().
Changes in v2:
- Use size_t for the catalog record length.
- Reject catalog records larger than hfs_cat_rec.
- Treat non-file/non-directory records as invalid.
- Propagate hfs_read_inode() failures from resource-fork lookup.
- Reject bad resource-fork and root inodes.
fs/hfs/catalog.c | 41 ++++++++++++++++++++++++++++++++++++++++- fs/hfs/hfs_fs.h | 19 +++++++++++++++++++ fs/hfs/inode.c | 12 +++++++++--- fs/hfs/super.c | 5 +++++ 4 files changed, 73 insertions(+), 4 deletions(-)
fs/hfs/catalog.c +40 -1
@@ -182,6 +182,40 @@ int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2) key2->cat.CName.name, key2->cat.CName.len); } +static int hfs_cat_validate_found_cnid (struct hfs_find_data *fd,u32 cnid) +{ +hfs_cat_rec rec; +u32 found_cnid; +size_t rec_len; + + rec_len = fd->entrylength; +if (rec_len <=0 || rec_len >sizeof (rec)) +return -EIO ; + +memset (&rec,0 ,sizeof (rec)); +hfs_bnode_read (fd->bnode, &rec, fd->entryoffset, rec_len); + +switch (rec.type) { +case HFS_CDR_FIL : +if (rec_len !=sizeof (struct hfs_cat_file )) +return -EIO ; + found_cnid =be32_to_cpu (rec.file.FlNum); +break ; +case HFS_CDR_DIR : +if (rec_len !=sizeof (struct hfs_cat_dir )) +return -EIO ; + found_cnid =be32_to_cpu (rec.dir.DirID); +break ; +default : +return -EIO ; + } + +if (found_cnid != cnid) +return -EIO ; + +return 0 ; +} +/* Try to get a catalog entry for given catalog id */ // move to read_super??? int hfs_cat_find_brec(struct super_block *sb, u32 cnid,
@@ -208,7 +242,12 @@ 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); + + res =hfs_brec_find (fd); +if (res) +return res; + +return hfs_cat_validate_found_cnid (fd, cnid); } static inline
fs/hfs/hfs_fs.h +19 -0
@@ -155,6 +155,25 @@ extern int hfs_cat_move(u32 cnid, struct inode *src_dir,extern void hfs_cat_build_key (struct super_block *sb,btree_key *key,u32 parent,const struct qstr *name); +/* +* 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 ; +default : +return false; + } +} +/* dir.c */ extern const struct file_operations hfs_dir_operations;extern const struct inode_operations hfs_dir_inode_operations;
fs/hfs/inode.c +9 -3
@@ -367,6 +367,9 @@ static int hfs_read_inode(struct inode *inode, void *data) rec = idata->rec;switch (rec->type) {case HFS_CDR_FIL : +if (!hfs_is_valid_cnid (be32_to_cpu (rec->file.FlNum), rec->type)) +return -EIO ; +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));
@@ -390,6 +393,9 @@ static int hfs_read_inode(struct inode *inode, void *data) inode->i_mapping->a_ops = &hfs_aops;break ;case HFS_CDR_DIR : +if (!hfs_is_valid_cnid (be32_to_cpu (rec->dir.DirID), rec->type)) +return -EIO ; + 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 ;
@@ -571,12 +577,12 @@ static struct dentry *hfs_file_lookup(struct inode *dir, struct dentry *dentry, res =hfs_brec_read (&fd, &rec,sizeof (rec));if (!res) {struct hfs_iget_data idata = {NULL , &rec }; -hfs_read_inode (inode, &idata); + res =hfs_read_inode (inode, &idata); }hfs_find_exit (&fd); -if (res) { +if (res ||is_bad_inode (inode)) {iput (inode); -return ERR_PTR (res); +return ERR_PTR (-EIO ); }HFS_I (inode)->rsrc_inode = dir;HFS_I (dir)->rsrc_inode = inode;
fs/hfs/super.c +5 -0
@@ -372,6 +372,11 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc)if (!root_inode) goto bail_no_root; +if (is_bad_inode (root_inode)) { +iput (root_inode); + goto bail_no_root; + } +set_default_d_op (sb, &hfs_dentry_operations); res = -ENOMEM ; sb->s_root =d_make_root (root_inode);
Signature · 1 line
2.43.0
Viacheslav Dubeyko
maintainerQuoted 61 lines · David Maximiliano Hermitte
hfs_cat_find_brec()first resolves a catalog thread record by CNID and
then looks up the corresponding catalog record by parent/name. On a corrupted filesystem image, the second lookup may find a record whose CNID does not match the CNID that was requested.Validate the record found by the second lookup before returning it. Read the already-found record with
hfs_bnode_read(), require the exact
fixed size for file and directory records, reject other record types, and verify that the stored CNID matches the requested CNID.Also validate reserved CNIDs in
hfs_read_inode()before populating the
inode, propagatehfs_read_inode()failures from the resource-fork lookup
path, and reject bad resource-fork and root inodes.Keep
hfs_write_inode()unchanged, so corrupted catalog records are rejected before reaching its existing reserved-CNIDBUG()path.Reported-by [email protected]Cc George Anthony Vernon <[email protected]>Cc Tetsuo Handa <[email protected]>Signed-off-by David Maximiliano Hermitte <[email protected]>---
Changes in v4:
- Combine the catalog record length validation into one check.
- Require exact sizes for file and directory catalog records.
- Keephfs_write_inode()unchanged following maintainer feedback.
- Revalidate the exact final patch with the HFS build and QEMU repro.Changes in v3:
- Drop the stray blank line inhfs_file_lookup().Changes in v2:
- Usesize_tfor the catalog record length.
- Reject catalog records larger thanhfs_cat_rec.
- Treat non-file/non-directory records as invalid.
- Propagatehfs_read_inode()failures from resource-fork lookup.
- Reject bad resource-fork and root inodes.fs/hfs/catalog.c | 41 ++++++++++++++++++++++++++++++++++++++++- fs/hfs/hfs_fs.h | 19 +++++++++++++++++++ fs/hfs/inode.c | 12 +++++++++---
fs/hfs/super.c | 5 +++++4 files changed, 73 insertions(+), 4 deletions(-)fs/hfs/catalog.c +0
-0@@ -182,6 +182,40 @@ int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2)
key2->cat.CName.name, key2-Quoted 1 line
cat.CName.len);
}
+static inthfs_cat_validate_found_cnid(struct hfs_find_data *fd, u32 cnid)
fs/hfs/catalog.c int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2) · line 192
Quoted 7 lines
fs/hfs/catalog.c +7
-0+{ +hfs_cat_rec rec; +u32 found_cnid; +size_t rec_len; + + rec_len = fd->entrylength; +if (rec_len <=0 || rec_len >sizeof (rec))
If we still have size_t rec_len, then we cannot keep negative values because fd->entrylength has int data type. So, this rec_len <= 0 is not correct now. We need to rework it somehow. Maybe, ssize_t here?
Quoted 111 lines
fs/hfs/catalog.c +26
-0+return -EIO ; + +memset (&rec,0 ,sizeof (rec)); +hfs_bnode_read (fd->bnode, &rec, fd->entryoffset, rec_len); + +switch (rec.type) { +case HFS_CDR_FIL : +if (rec_len !=sizeof (struct hfs_cat_file )) +return -EIO ; + found_cnid =be32_to_cpu (rec.file.FlNum); +break ; +case HFS_CDR_DIR : +if (rec_len !=sizeof (struct hfs_cat_dir )) +return -EIO ; + found_cnid =be32_to_cpu (rec.dir.DirID); +break ; +default : +return -EIO ; + } + +if (found_cnid != cnid) +return -EIO ; + +return 0 ; +} +/* Try to get a catalog entry for given catalog id */ // move to
read_super???
int hfs_cat_find_brec(structsuper_block*sb, u32 cnid,fs/hfs/catalog.c +6
-1@@ -208,7 +242,12 @@ 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); + + res =hfs_brec_find (fd); +if (res) +return res; + +return hfs_cat_validate_found_cnid (fd, cnid); }
static inlinefs/hfs/hfs_fs.h +0
-0@@ -155,6 +155,25 @@ extern int hfs_cat_move(u32 cnid, struct inode *src_dir,extern void hfs_cat_build_key(struct
super_block*sb,btree_key*key,
u32 parent, const struct qstr *name);fs/hfs/hfs_fs.h +19
-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 ; +default : +return false; + } +} +/*
dir.c*/
extern const structfile_operationshfs_dir_operations; extern const structinode_operationshfs_dir_inode_operations;fs/hfs/inode.c +0
-0@@ -367,6 +367,9 @@ 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),
rec->type))fs/hfs/inode.c +2
-0+return -EIO ; +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@@ -390,6 +393,9 @@ 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),
rec->type))fs/hfs/inode.c +2
-0+return -EIO ; +
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@@ -571,12 +577,12 @@ static struct dentry *hfs_file_lookup(struct inode *dir, struct dentry *dentry, res = hfs_brec_read(&fd, &rec, sizeof(rec)); if (!res) {
fs/hfs/inode.c static struct dentry *hfs_file_lookup(struct · line 586
Quoted 10 lines
fs/hfs/inode.c +3
-3struct hfs_iget_data idata = { NULL, &rec }; -hfs_read_inode (inode, &idata); + res =hfs_read_inode (inode, &idata); } hfs_find_exit(&fd); -if (res) { +if (res ||is_bad_inode (inode)) { iput(inode); -return ERR_PTR (res); +return ERR_PTR (-EIO );
I don't think that it's correct modification. We need to return res if we have the error code there. And we could return -EIO if we have bad inode. Do you agree that we need to rework it?
Thanks,
Slava.
Quoted 20 lines
fs/hfs/inode.c +0
-0} HFS_I(inode)->rsrc_inode = dir;HFS_I(dir)->rsrc_inode = inode;
fs/hfs/super.c +0
-0@@ -372,6 +372,11 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc) if (!root_inode) goto bail_no_root;fs/hfs/super.c +5
-0+if (is_bad_inode (root_inode)) { +iput (root_inode); + goto bail_no_root; + } +
set_default_d_op(sb, &hfs_dentry_operations); res =-ENOMEM;
sb->s_root=d_make_root(root_inode);
PATCH v5 1/1 hfs: validate catalog CNIDs before instantiating inodes
In reply to David Maximiliano Hermittehfs_cat_find_brec() first resolves a catalog thread record by CNID and then looks up the corresponding catalog record by parent/name. On a corrupted filesystem image, the second lookup may find a record whose CNID does not match the CNID that was requested.
Validate the record found by the second lookup before returning it. Read the already-found record with hfs_bnode_read(), require the exact fixed size for file and directory records, reject other record types, and verify that the stored CNID matches the requested CNID.
Also validate reserved CNIDs in hfs_read_inode() before populating the inode, propagate hfs_read_inode() failures from the resource-fork lookup path, and reject bad resource-fork and root inodes.
Keep hfs_write_inode() unchanged, so corrupted catalog records are rejected before reaching its existing reserved-CNID BUG() path.
---
Changes in v5:
- Keep rec_len signed by using int to match fd->entrylength, making the non-positive length check meaningful before passing the validated value to hfs_bnode_read().
- Use (size_t) casts when comparing rec_len against record sizes to avoid signed/unsigned comparisons.
- Preserve the original error code in hfs_file_lookup(), returning -EIO separately only when the inode is marked bad.
Changes in v4:
- Combine the catalog record length validation into one check.
- Require exact sizes for file and directory catalog records.
- Keep hfs_write_inode() unchanged following maintainer feedback.
- Revalidate the exact final patch with the HFS build and QEMU repro.
Changes in v3:
- Drop the stray blank line in hfs_file_lookup().
Changes in v2:
- Use size_t for the catalog record length.
- Reject catalog records larger than hfs_cat_rec.
- Treat non-file/non-directory records as invalid.
- Propagate hfs_read_inode() failures from resource-fork lookup.
- Reject bad resource-fork and root inodes.
fs/hfs/catalog.c | 41 ++++++++++++++++++++++++++++++++++++++++- fs/hfs/hfs_fs.h | 19 +++++++++++++++++++ fs/hfs/inode.c | 13 ++++++++++++- fs/hfs/super.c | 5 +++++ 4 files changed, 76 insertions(+), 2 deletions(-)
fs/hfs/catalog.c +40 -1
@@ -182,6 +182,40 @@ int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2) key2->cat.CName.name, key2->cat.CName.len); } +static int hfs_cat_validate_found_cnid (struct hfs_find_data *fd,u32 cnid) +{ +hfs_cat_rec rec; +u32 found_cnid; +int rec_len; + + rec_len = fd->entrylength; +if (rec_len <=0 || (size_t )rec_len >sizeof (rec)) +return -EIO ; + +memset (&rec,0 ,sizeof (rec)); +hfs_bnode_read (fd->bnode, &rec, fd->entryoffset, rec_len); + +switch (rec.type) { +case HFS_CDR_FIL : +if ((size_t )rec_len !=sizeof (struct hfs_cat_file )) +return -EIO ; + found_cnid =be32_to_cpu (rec.file.FlNum); +break ; +case HFS_CDR_DIR : +if ((size_t )rec_len !=sizeof (struct hfs_cat_dir )) +return -EIO ; + found_cnid =be32_to_cpu (rec.dir.DirID); +break ; +default : +return -EIO ; + } + +if (found_cnid != cnid) +return -EIO ; + +return 0 ; +} +/* Try to get a catalog entry for given catalog id */ // move to read_super??? int hfs_cat_find_brec(struct super_block *sb, u32 cnid,
@@ -208,7 +242,12 @@ 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); + + res =hfs_brec_find (fd); +if (res) +return res; + +return hfs_cat_validate_found_cnid (fd, cnid); } static inline
fs/hfs/hfs_fs.h +19 -0
@@ -155,6 +155,25 @@ extern int hfs_cat_move(u32 cnid, struct inode *src_dir,extern void hfs_cat_build_key (struct super_block *sb,btree_key *key,u32 parent,const struct qstr *name); +/* +* 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 ; +default : +return false; + } +} +/* dir.c */ extern const struct file_operations hfs_dir_operations;extern const struct inode_operations hfs_dir_inode_operations;
fs/hfs/inode.c +12 -1
@@ -367,6 +367,9 @@ static int hfs_read_inode(struct inode *inode, void *data) rec = idata->rec;switch (rec->type) {case HFS_CDR_FIL : +if (!hfs_is_valid_cnid (be32_to_cpu (rec->file.FlNum), rec->type)) +return -EIO ; +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));
@@ -390,6 +393,9 @@ static int hfs_read_inode(struct inode *inode, void *data) inode->i_mapping->a_ops = &hfs_aops;break ;case HFS_CDR_DIR : +if (!hfs_is_valid_cnid (be32_to_cpu (rec->dir.DirID), rec->type)) +return -EIO ; + 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 ;
@@ -571,13 +577,18 @@ static struct dentry *hfs_file_lookup(struct inode *dir, struct dentry *dentry, res =hfs_brec_read (&fd, &rec,sizeof (rec));if (!res) {struct hfs_iget_data idata = {NULL , &rec }; -hfs_read_inode (inode, &idata); + res =hfs_read_inode (inode, &idata); }hfs_find_exit (&fd);if (res) {iput (inode);return ERR_PTR (res); } + +if (is_bad_inode (inode)) { +iput (inode); +return ERR_PTR (-EIO ); + }HFS_I (inode)->rsrc_inode = dir;HFS_I (dir)->rsrc_inode = inode;igrab (dir);
fs/hfs/super.c +5 -0
@@ -372,6 +372,11 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc)if (!root_inode) goto bail_no_root; +if (is_bad_inode (root_inode)) { +iput (root_inode); + goto bail_no_root; + } +set_default_d_op (sb, &hfs_dentry_operations); res = -ENOMEM ; sb->s_root =d_make_root (root_inode);
Signature · 1 line
2.43.0
Quoted 220 lines · David Maximiliano Hermitte
hfs_cat_find_brec()first resolves a catalog thread record by CNID and
then looks up the corresponding catalog record by parent/name. On a corrupted filesystem image, the second lookup may find a record whose CNID does not match the CNID that was requested.Validate the record found by the second lookup before returning it. Read the already-found record with
hfs_bnode_read(), require the exact
fixed size for file and directory records, reject other record types, and verify that the stored CNID matches the requested CNID.Also validate reserved CNIDs in
hfs_read_inode()before populating the
inode, propagatehfs_read_inode()failures from the resource-fork lookup
path, and reject bad resource-fork and root inodes.Keep
hfs_write_inode()unchanged, so corrupted catalog records are rejected before reaching its existing reserved-CNIDBUG()path.Reported-by [email protected]Cc George Anthony Vernon <[email protected]>Cc Tetsuo Handa <[email protected]>Signed-off-by David Maximiliano Hermitte <[email protected]>---
Changes in v5:
- Keeprec_lensigned by using int to matchfd->entrylength, making the non-positive length check meaningful before passing the validated
value tohfs_bnode_read().
- Use (size_t) casts when comparingrec_lenagainst record sizes to avoid signed/unsigned comparisons.
- Preserve the original error code inhfs_file_lookup(), returning-EIOseparately only when the inode is marked bad.Changes in v4:
- Combine the catalog record length validation into one check.
- Require exact sizes for file and directory catalog records.
- Keephfs_write_inode()unchanged following maintainer feedback.
- Revalidate the exact final patch with the HFS build and QEMU repro.Changes in v3:
- Drop the stray blank line inhfs_file_lookup().Changes in v2:
- Usesize_tfor the catalog record length.
- Reject catalog records larger thanhfs_cat_rec.
- Treat non-file/non-directory records as invalid.
- Propagatehfs_read_inode()failures from resource-fork lookup.
- Reject bad resource-fork and root inodes.fs/hfs/catalog.c | 41 ++++++++++++++++++++++++++++++++++++++++- fs/hfs/hfs_fs.h | 19 +++++++++++++++++++ fs/hfs/inode.c | 13 ++++++++++++-
fs/hfs/super.c | 5 +++++4 files changed, 76 insertions(+), 2 deletions(-)fs/hfs/catalog.c +0
-0@@ -182,6 +182,40 @@ int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2)
key2->cat.CName.name, key2-Quoted 1 line
cat.CName.len);
}
+static inthfs_cat_validate_found_cnid(struct hfs_find_data *fd, u32 cnid)fs/hfs/catalog.c +33
-0+{ +hfs_cat_rec rec; +u32 found_cnid; +int rec_len; + + rec_len = fd->entrylength; +if (rec_len <=0 || (size_t )rec_len >sizeof (rec)) +return -EIO ; + +memset (&rec,0 ,sizeof (rec)); +hfs_bnode_read (fd->bnode, &rec, fd->entryoffset, rec_len); + +switch (rec.type) { +case HFS_CDR_FIL : +if ((size_t )rec_len !=sizeof (struct hfs_cat_file )) +return -EIO ; + found_cnid =be32_to_cpu (rec.file.FlNum); +break ; +case HFS_CDR_DIR : +if ((size_t )rec_len !=sizeof (struct hfs_cat_dir )) +return -EIO ; + found_cnid =be32_to_cpu (rec.dir.DirID); +break ; +default : +return -EIO ; + } + +if (found_cnid != cnid) +return -EIO ; + +return 0 ; +} +/* Try to get a catalog entry for given catalog id */ // move to
read_super???
int hfs_cat_find_brec(structsuper_block*sb, u32 cnid,fs/hfs/catalog.c +6
-1@@ -208,7 +242,12 @@ 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); + + res =hfs_brec_find (fd); +if (res) +return res; + +return hfs_cat_validate_found_cnid (fd, cnid); }
static inlinefs/hfs/hfs_fs.h +0
-0@@ -155,6 +155,25 @@ extern int hfs_cat_move(u32 cnid, struct inode *src_dir,extern void hfs_cat_build_key(struct
super_block*sb,btree_key*key,
u32 parent, const struct qstr *name);fs/hfs/hfs_fs.h +19
-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 ; +default : +return false; + } +} +/*
dir.c*/
extern const structfile_operationshfs_dir_operations; extern const structinode_operationshfs_dir_inode_operations;fs/hfs/inode.c +0
-0@@ -367,6 +367,9 @@ 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),
rec->type))fs/hfs/inode.c +2
-0+return -EIO ; +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@@ -390,6 +393,9 @@ 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),
rec->type))fs/hfs/inode.c +2
-0+return -EIO ; +
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 +6
-1@@ -571,13 +577,18 @@ static struct dentry *hfs_file_lookup(struct inode *dir, struct dentry *dentry, res = hfs_brec_read(&fd, &rec, sizeof(rec)); if (!res) { struct hfs_iget_data idata = { NULL, &rec }; -hfs_read_inode (inode, &idata); + res =hfs_read_inode (inode, &idata); } hfs_find_exit(&fd); if (res) { iput(inode); return ERR_PTR(res); } + +if (is_bad_inode (inode)) { +iput (inode); +return ERR_PTR (-EIO ); + } HFS_I(inode)->rsrc_inode = dir; HFS_I(dir)->rsrc_inode = inode;igrab(dir);
fs/hfs/super.c +0
-0@@ -372,6 +372,11 @@ static int hfs_fill_super(struct super_block *sb, struct fs_context *fc) if (!root_inode) goto bail_no_root;fs/hfs/super.c +5
-0+if (is_bad_inode (root_inode)) { +iput (root_inode); + goto bail_no_root; + } +
Quoted 3 lines
set_default_d_op(sb, &hfs_dentry_operations); res =-ENOMEM;
sb->s_root=d_make_root(root_inode);
Looks good. Thanks a lot for the fix.
I am happy to test the patch. But xfstests still has too many failures for the case of HFS file system.
Thanks,
Slava.
Quoted 1 line · Viacheslav Dubeyko
Looks good. Thanks a lot for the fix.
Not a new bug introduced by this patch, but sashiko thinks that there are things to check.
https://sashiko.dev/#/patchset/20260708215636.73815-1-davemadmaxxx%40gmail.com