hfsplus: validate thread record before delete key rebuild
Subsystems (3)
PATCH v3 1/1 hfsplus: validate thread record before delete key rebuild
hfsplus_delete_cat() is called with str == NULL when the last open reference to an unlinked HFS+ hardlink backing inode is closed. In that case, the function finds the catalog thread by CNID and rebuilds the catalog key from thread.nodeName.
That reconstruction path reads thread.nodeName.length directly from the catalog B-tree into fd.search_key and then copies length * 2 bytes into fd.search_key->cat.name.unicode. It does not first check that the found record is a thread record, that the record size matches the thread name length, or that nodeName.length fits in the fixed HFS+ catalog key buffer.
A corrupted image can therefore provide an oversized thread name length and make hfs_bnode_read() write past the catalog search-key allocation.
Read the CNID record through hfsplus_brec_read_cat(), which performs the hfs_brec_find() lookup via hfs_brec_read() before validating the catalog record. Reject non-thread records before building the delete key from the validated thread name.
Move the HFS+ catalog thread name bound into hfsplus_brec_read_cat() so it is checked together with the record-size consistency validation, and factor the thread-record-type check into a shared HFS/HFS+ helper.
--- fs/hfsplus/bfind.c | 5 +++++ fs/hfsplus/catalog.c | 30 ++++++++++++------------------ include/linux/hfs_common.h | 5 +++++ 3 files changed, 22 insertions(+), 18 deletions(-)
fs/hfsplus/bfind.c +5 -0
@@ -322,6 +322,11 @@ int hfsplus_brec_read_cat(struct hfs_find_data *fd, hfsplus_cat_entry *entry)pr_err ("thread record too short (got %u)\n" , fd->entrylength);return -EIO ; } +if (be16_to_cpu (entry->thread.nodeName.length) > +HFSPLUS_MAX_STRLEN ) { +pr_err ("catalog name length corrupted\n" ); +return -EIO ; + } expected_size =hfsplus_cat_thread_size (&entry->thread);break ;default :
fs/hfsplus/catalog.c +12 -18
@@ -204,16 +204,11 @@ int hfsplus_find_cat(struct super_block *sb, u32 cnid,return err; type =be16_to_cpu (tmp.type); -if (type !=HFSPLUS_FOLDER_THREAD && type !=HFSPLUS_FILE_THREAD ) { +if (!is_hfs_thread_record_type (type)) {pr_err ("found bad thread record in catalog\n" );return -EIO ; } -if (be16_to_cpu (tmp.thread.nodeName.length) >255 ) { -pr_err ("catalog name length corrupted\n" ); -return -EIO ; - } -hfsplus_cat_build_key_uni (fd->search_key,be32_to_cpu (tmp.thread.parentID), &tmp.thread.nodeName);
@@ -350,23 +345,22 @@ int hfsplus_delete_cat(u32 cnid, struct inode *dir, const struct qstr *str) goto out;if (!str) { -int len; +hfsplus_cat_entry entry = {0 };hfsplus_cat_build_key_with_cnid (sb, fd.search_key, cnid); - err =hfs_brec_find (&fd, hfs_find_rec_by_key); + err =hfsplus_brec_read_cat (&fd, &entry);if (err) goto out; - off = fd.entryoffset + - offsetof(struct hfsplus_cat_thread , nodeName); - fd.search_key->cat.parent =cpu_to_be32 (dir->i_ino); -hfs_bnode_read (fd.bnode, - &fd.search_key->cat.name.length, off,2 ); - len =be16_to_cpu (fd.search_key->cat.name.length) *2 ; -hfs_bnode_read (fd.bnode, - &fd.search_key->cat.name.unicode, - off +2 , len); - fd.search_key->key_len =cpu_to_be16 (6 + len); + type =be16_to_cpu (entry.type); +if (!is_hfs_thread_record_type (type)) { +pr_err ("found bad thread record in catalog\n" ); + err = -EIO ; + goto out; + } + +hfsplus_cat_build_key_uni (fd.search_key, dir->i_ino, + &entry.thread.nodeName); }else { err =hfsplus_cat_build_key (sb, fd.search_key, dir->i_ino, str);if (unlikely (err))
include/linux/hfs_common.h +5 -0
@@ -92,6 +92,11 @@#define HFS_CDR_THD 0x03/* folder (directory) thread */ #define HFS_CDR_FTH 0x04/* file thread */ +static inline bool is_hfs_thread_record_type (u16 type) +{ +return type ==HFS_CDR_THD || type ==HFS_CDR_FTH ; +} +/* legal values for hfs_ext_key.FkType and hfs_file.fork */ #define HFS_FK_DATA 0x00#define HFS_FK_RSRC 0xFF
Signature · 1 line
2.54.0
Viacheslav Dubeyko
maintainerQuoted 49 lines · Kyle Zeng
hfsplus_delete_cat()is called with str == NULL when the last open reference to an unlinked HFS+ hardlink backing inode is closed. In that
case, the function finds the catalog thread by CNID and rebuilds the catalog key from thread.nodeName.That reconstruction path reads thread.nodeName.length directly from the
catalog B-tree into fd.search_key and then copies length * 2 bytes intofd.search_key->cat.name.unicode. It does not first check that the found
record is a thread record, that the record size matches the thread name
length, or that nodeName.length fits in the fixed HFS+ catalog key buffer.A corrupted image can therefore provide an oversized thread name length
and makehfs_bnode_read()write past the catalog search-key allocation.Read the CNID record through
hfsplus_brec_read_cat(), which performs thehfs_brec_find()lookup viahfs_brec_read()before validating the catalog
record. Reject non-thread records before building the delete key from the validated thread name.Move the HFS+ catalog thread name bound into
hfsplus_brec_read_cat()so
it is checked together with the record-size consistency validation, and
factor the thread-record-type check into a shared HFS/HFS+ helper.Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Assisted-by Codex:gpt-5.5Signed-off-by Kyle Zeng <[email protected]>---
fs/hfsplus/bfind.c | 5 +++++ fs/hfsplus/catalog.c | 30 ++++++++++++------------------include/linux/hfs_common.h| 5 +++++3 files changed, 22 insertions(+), 18 deletions(-)fs/hfsplus/bfind.c +0
-0
fs/hfsplus/bfind.c int hfsplus_brec_read_cat(struct hfs_find_data · line 331
Quoted 10 lines
fs/hfsplus/bfind.c +5
-0@@ -322,6 +322,11 @@ int hfsplus_brec_read_cat(struct hfs_find_data *fd, hfsplus_cat_entry *entry) pr_err("thread record too short (got %u)\n", fd->entrylength); return -EIO; } +if (be16_to_cpu (entry->thread.nodeName.length) > + HFSPLUS_MAX_STRLEN) { +pr_err ("catalog name length corrupted\n" ); +return -EIO ; + }
I was not against the function that v2 has. :) We simply need to have more smart checking logic. We are trying to check the thread record and hfsplus_brec_read_cat() operates by fd->entrylength. So, the entry-
Quoted 1 line
thread.nodeName.length should be consistent with
fd->entrylength. Does
it make sense to you?
Quoted 78 lines
expected_size= hfsplus_cat_thread_size(&entry-Quoted 1 line
thread);
break;
default:fs/hfsplus/catalog.c +0
-0@@ -204,16 +204,11 @@ int hfsplus_find_cat(struct super_block *sb,u32 cnid, return err;
type =be16_to_cpu(tmp.type);fs/hfsplus/catalog.c +0
-1- if (type != HFSPLUS_FOLDER_THREAD && type !=
HFSPLUS_FILE_THREAD) {fs/hfsplus/catalog.c +1
-0+if (!is_hfs_thread_record_type (type)) {
pr_err("found bad thread record in catalog\n"); return-EIO;
}fs/hfsplus/catalog.c +0
-5-if (be16_to_cpu (tmp.thread.nodeName.length) >255 ) { -pr_err ("catalog name length corrupted\n" ); -return -EIO ; - } -hfsplus_cat_build_key_uni(fd->search_key,
be32_to_cpu(tmp.thread.parentID),
&tmp.thread.nodeName);fs/hfsplus/catalog.c +0
-0@@ -350,23 +345,22 @@ int hfsplus_delete_cat(u32 cnid, struct inode *dir, const struct qstr *str) goto out;
if (!str) {fs/hfsplus/catalog.c +1
-1-int len; +hfsplus_cat_entry entry = {0 };
hfsplus_cat_build_key_with_cnid(sb, fd.search_key, cnid);fs/hfsplus/catalog.c +1
-1- err =hfs_brec_find (&fd, hfs_find_rec_by_key); + err =hfsplus_brec_read_cat (&fd, &entry);if (err)
goto out;fs/hfsplus/catalog.c +0
-2- off = fd.entryoffset + - offsetof(struct hfsplus_cat_thread,nodeName);
fs/hfsplus/catalog.c +0
-4- fd.search_key->cat.parent =cpu_to_be32 (dir->i_ino); -hfs_bnode_read (fd.bnode, - &fd.search_key->cat.name.length, off,2 ); - len = be16_to_cpu(fd.search_key->cat.name.length) *2;
fs/hfsplus/catalog.c +3
-4-hfs_bnode_read (fd.bnode, - &fd.search_key->cat.name.unicode, - off +2 , len); - fd.search_key->key_len =cpu_to_be16 (6 + len); + type =be16_to_cpu (entry.type); +if (!is_hfs_thread_record_type (type)) { + pr_err("found bad thread record incatalog\n");
fs/hfsplus/catalog.c +6
-0+ err = -EIO ; + goto out; + } + +hfsplus_cat_build_key_uni (fd.search_key, dir->i_ino, + &entry.thread.nodeName);} else {
err = hfsplus_cat_build_key(sb, fd.search_key, dir-Quoted 1 line
i_ino, str);if (unlikely(err))
include/linux/hfs_common.h +0
-0@@ -92,6 +92,11 @@ #define HFS_CDR_THD 0x03 /* folder (directory) thread*/ #define HFS_CDR_FTH 0x04 /* file thread */
include/linux/hfs_common.h line 98
Quoted 4 lines
include/linux/hfs_common.h +4
-0+static inline bool is_hfs_thread_record_type (u16 type) +{ +return type ==HFS_CDR_THD || type ==HFS_CDR_FTH ; +}
I don't want to place this function in linux/hfs_common.h. And I don't mean to use HFS_CDR_THD/HFS_CDR_FTH (HFS based constants) instead of HFS+ specific ones. It looks not correct to me. Currently, I suggest to use is_hfs_thread_record_type() name, to employ HFS+ specific constants and to place it into fs/hfsplus/hfsplus_fs.h.
Thanks,
Slava.
Quoted 4 lines
+
/* legal values for hfs_ext_key.FkType and hfs_file.fork */
#defineHFS_FK_DATA0x00
#defineHFS_FK_RSRC0xFF