hfsplus: validate thread record before delete key rebuild
Subsystems (2)
PATCH 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 the name 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(), reject non-thread records, and explicitly bound nodeName.length before building the delete key from the validated thread name.
--- fs/hfsplus/catalog.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-)
fs/hfsplus/catalog.c +20 -12
@@ -351,23 +351,31 @@ int hfsplus_delete_cat(u32 cnid, struct inode *dir, const struct qstr *str) goto out;if (!str) { -int len; +hfsplus_cat_entry entry = {0 }; +u16 thread_type;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); + thread_type =be16_to_cpu (entry.type); +if (thread_type !=HFSPLUS_FOLDER_THREAD && + thread_type !=HFSPLUS_FILE_THREAD ) { +pr_err ("found bad thread record in catalog\n" ); + err = -EIO ; + goto out; + } + +if (be16_to_cpu (entry.thread.nodeName.length) > +HFSPLUS_MAX_STRLEN ) { +pr_err ("catalog name length corrupted\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))
Signature · 1 line
2.43.0
Viacheslav Dubeyko
maintainerQuoted 87 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 the name 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(), reject non- thread
records, and explicitly bound nodeName.length before building the delete
key from the validated thread name.Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Assisted-by Codex:gpt-5.5Signed-off-by Kyle Zeng <[email protected]>---
fs/hfsplus/catalog.c | 32 ++++++++++++++++++++------------1 file changed, 20 insertions(+), 12 deletions(-)fs/hfsplus/catalog.c +0
-0@@ -351,23 +351,31 @@ int hfsplus_delete_cat(u32 cnid, struct inode *dir, const struct qstr *str) goto out;
if (!str) {fs/hfsplus/catalog.c +2
-1-int len; +hfsplus_cat_entry entry = {0 }; +u16 thread_type;
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 +4
-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); + thread_type =be16_to_cpu (entry.type); +if (thread_type !=HFSPLUS_FOLDER_THREAD && + thread_type != HFSPLUS_FILE_THREAD) { + pr_err("found bad thread record incatalog\n");
fs/hfsplus/catalog.c +13
-0+ err = -EIO ; + goto out; + } + +if (be16_to_cpu (entry.thread.nodeName.length) > + HFSPLUS_MAX_STRLEN) { +pr_err ("catalog name length corrupted\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-
Quoted 2 lines
Quoted 1 line
i_ino, str);if (unlikely(err))
Have you run xfstests for this patch? Sorry, I am in personal trip right now and I cannot test your patch until the end of June. Please, ping me around that time.
Thanks,
Slava.
Hi Viacheslav,
I prepared a xfstests environment and tried to test the patches. But unfortunately, due to my lab environment constraints, I could not fully finish the tests: some tests errored out even without the patches due to environment constraints.
I think you will still be the best person to perform the test. I'll ping you around end of June.
Thanks,
Kyle
Quoted 98 lines · Viacheslav Dubeyko
Quoted 88 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 the name 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(), reject non- thread
records, and explicitly bound nodeName.length before building the delete
key from the validated thread name.Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Assisted-by Codex:gpt-5.5Signed-off-by Kyle Zeng <[email protected]>--- fs/hfsplus/catalog.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-)fs/hfsplus/catalog.c +7
-12@@ -351,23 +351,31 @@ int hfsplus_delete_cat(u32 cnid, struct inode *dir, const struct qstr *str) goto out;if (!str) { -int len; +hfsplus_cat_entry entry = {0 }; +u16 thread_type;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); + thread_type =be16_to_cpu (entry.type); +if (thread_type !=HFSPLUS_FOLDER_THREAD && + thread_type !=HFSPLUS_FILE_THREAD ) { + pr_err("found bad thread record incatalog\n");
fs/hfsplus/catalog.c +13
-0+ err = -EIO ; + goto out; + } + +if (be16_to_cpu (entry.thread.nodeName.length) > +HFSPLUS_MAX_STRLEN ) { +pr_err ("catalog name length corrupted\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-Quoted 1 line
i_ino, str);Quoted 1 line
if (unlikely(err))
Have you run xfstests for this patch? Sorry, I am in personal trip right now and I cannot test your patch until the end of June. Please, ping me around that time.
Thanks,
Slava.
Viacheslav Dubeyko
maintainerQuoted 9 lines · Kyle Zeng
Hi Viacheslav,
I prepared a xfstests environment and tried to test the patches. But unfortunately, due to my lab environment constraints, I could not fully finish the tests: some tests errored out even without the patches due to environment constraints.
I think you will still be the best person to perform the test. I'll ping you around end of June.
Currently, HFS+ has around 15 - 17 xfstests failures. If you can see around the same number of issues without your patches, then you are good if your patches will not increase the number of failed xfestests. Anyway, I'll be back in two weeks and I will be able to test your patches in more reliable environment. Sorry, I have pretty no time and capabilities to run xfstests during my trip. And not very much time for the review too. :)
Thanks,
Slava.
Quoted 112 lines
Quoted 109 lines · Viacheslav Dubeyko
Quoted 94 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 the name 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(), reject non- thread
records, and explicitly bound nodeName.length before building the delete
key from the validated thread name.Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Assisted-by Codex:gpt-5.5Signed-off-by Kyle Zeng <[email protected]>---
fs/hfsplus/catalog.c | 32 ++++++++++++++++++++------------1 file changed, 20 insertions(+), 12 deletions(-)fs/hfsplus/catalog.c +0
-0@@ -351,23 +351,31 @@ int hfsplus_delete_cat(u32 cnid, structinode
*dir, const struct qstr *str)
goto out;if (!str) {
- int len;
+hfsplus_cat_entryentry = {0};
+ u16thread_type;
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-Quoted 1 line
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); +thread_type=be16_to_cpu(entry.type);
+ if (thread_type!=HFSPLUS_FOLDER_THREAD&& +thread_type!=HFSPLUS_FILE_THREAD) { +pr_err("found bad thread record in catalog\n");
+ err =-EIO;
+ goto out;
+ }fs/hfsplus/catalog.c +9
-0+ + if (be16_to_cpu(entry.thread.nodeName.length) > + HFSPLUS_MAX_STRLEN) { + pr_err("catalog name length corrupted\n"); + err = -EIO; + goto out; + } + + hfsplus_cat_build_key_uni(fd.search_key, dir-Quoted 1 line
i_ino,Quoted 5 lines
+ &entry.thread.nodeName); } else {
err = hfsplus_cat_build_key(sb, fd.search_key, dir-Quoted 1 line
i_ino, str);if (unlikely(err))
Have you run xfstests for this patch? Sorry, I am in personal trip right now and I cannot test your patch until the end of June. Please,
ping me around that time.Thanks,
Slava.
Hi Viacheslav,
I hope you had a great trip.
It was difficult for us to test the patches without knowing the exact number of xfstests failures. So I wonder whether you could help test the two HFS patches we shared previously.
Thanks,
Kyle
Quoted 135 lines · Viacheslav Dubeyko
Quoted 9 lines · Kyle Zeng
Hi Viacheslav,
I prepared a xfstests environment and tried to test the patches. But unfortunately, due to my lab environment constraints, I could not fully finish the tests: some tests errored out even without the patches due to environment constraints.
I think you will still be the best person to perform the test. I'll ping you around end of June.Currently, HFS+ has around 15 - 17 xfstests failures. If you can see around the same number of issues without your patches, then you are good if your patches will not increase the number of failed xfestests. Anyway, I'll be back in two weeks and I will be able to test your patches in more reliable environment. Sorry, I have pretty no time and capabilities to run xfstests during my trip. And not very much time for the review too. :)
Thanks,
Slava.Quoted 112 lines
Quoted 109 lines · Viacheslav Dubeyko
Quoted 94 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 the name 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(), reject non- thread
records, and explicitly bound nodeName.length before building the delete
key from the validated thread name.Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Assisted-by Codex:gpt-5.5Signed-off-by Kyle Zeng <[email protected]>--- fs/hfsplus/catalog.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-)fs/hfsplus/catalog.c +0
-0@@ -351,23 +351,31 @@ int hfsplus_delete_cat(u32 cnid, structinode
*dir, const struct qstr *str)goto out; if (!str) {fs/hfsplus/catalog.c +2
-1-int len; +hfsplus_cat_entry entry = {0 }; +u16 thread_type; hfsplus_cat_build_key_with_cnid(sb, fd.search_key,cnid);
fs/hfsplus/catalog.c +1
-3- 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-Quoted 1 line
i_ino);fs/hfsplus/catalog.c +0
-3-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 +4
-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); + thread_type =be16_to_cpu (entry.type); +if (thread_type !=HFSPLUS_FOLDER_THREAD && + thread_type !=HFSPLUS_FILE_THREAD ) { + pr_err("found bad thread record incatalog\n");
fs/hfsplus/catalog.c +12
-0+ err = -EIO ; + goto out; + } + +if (be16_to_cpu (entry.thread.nodeName.length) > +HFSPLUS_MAX_STRLEN ) { +pr_err ("catalog name length corrupted\n" ); + err = -EIO ; + goto out; + } + + hfsplus_cat_build_key_uni(fd.search_key, dir-Quoted 1 line
i_ino,Quoted 5 lines
+ &entry.thread.nodeName);
} else {
err = hfsplus_cat_build_key(sb, fd.search_key, dir-Quoted 1 line
i_ino, str);if (unlikely(err))
Have you run xfstests for this patch? Sorry, I am in personal trip right now and I cannot test your patch until the end of June. Please,
ping me around that time.Thanks,
Slava.
Viacheslav Dubeyko
maintainerHi Kyle,
Quoted 7 lines · Kyle Zeng
Hi Viacheslav,
I hope you had a great trip.
It was difficult for us to test the patches without knowing the exact number of xfstests failures. So I wonder whether you could help test the two HFS patches we shared previously.
I need to manage multiple patches review during next 2 - 3 days. I am going to test your patches during next 2 - 3 days too. Let me take a look into the patches at first. I'll share the results ASAP.
Thanks,
Slava.
Viacheslav Dubeyko
maintainerQuoted 50 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 the name 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(), reject non- thread
records, and explicitly bound nodeName.length before building the delete
key from the validated thread name.Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Assisted-by Codex:gpt-5.5Signed-off-by Kyle Zeng <[email protected]>---
fs/hfsplus/catalog.c | 32 ++++++++++++++++++++------------1 file changed, 20 insertions(+), 12 deletions(-)fs/hfsplus/catalog.c +0
-0@@ -351,23 +351,31 @@ int hfsplus_delete_cat(u32 cnid, struct inode *dir, const struct qstr *str) goto out;
if (!str) {fs/hfsplus/catalog.c +2
-1-int len; +hfsplus_cat_entry entry = {0 }; +u16 thread_type;
hfsplus_cat_build_key_with_cnid(sb, fd.search_key, cnid);
fs/hfsplus/catalog.c int hfsplus_delete_cat(u32 cnid, struct inode *dir, const struct qstr *str) · line 358
Quoted 2 lines
fs/hfsplus/catalog.c +1
-1- err =hfs_brec_find (&fd, hfs_find_rec_by_key); + err =hfsplus_brec_read_cat (&fd, &entry);
We can read something only if we have found something. But the logic of lookup (hfs_brec_find) has been deleted. Are you sure that it is correct? As far as I can see, the hfsplus_brec_read_cat() will operate with invalid hfs_find_data instance.
Quoted 11 lines
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 int hfsplus_delete_cat(u32 cnid, struct inode *dir, const struct qstr *str) · line 362
Quoted 5 lines
fs/hfsplus/catalog.c +1
-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); + thread_type =be16_to_cpu (entry.type);
It's not thread_type but entry_type. The thread_type sounds like not a correct name here.
fs/hfsplus/catalog.c int hfsplus_delete_cat(u32 cnid, struct inode *dir, const struct qstr *str) · line 364
Quoted 2 lines
fs/hfsplus/catalog.c +2
-0+if (thread_type !=HFSPLUS_FOLDER_THREAD && + thread_type != HFSPLUS_FILE_THREAD) {
What if we introduce static inline function for checking entry.type? Let's call it is_hfsplus_thread_type().
fs/hfsplus/catalog.c int hfsplus_delete_cat(u32 cnid, struct inode *dir, const struct qstr *str) · line 371
Quoted 8 lines
fs/hfsplus/catalog.c +1
-0+ pr_err("found bad thread record incatalog\n");
fs/hfsplus/catalog.c +6
-0+ err = -EIO ; + goto out; + } + +if (be16_to_cpu (entry.thread.nodeName.length) > + HFSPLUS_MAX_STRLEN) {
I think that is_hfsplus_name_length_valid() could be good to introduce. Do we really have nothing yet? I suggest to have static inline function for checking correctness of name length.
Thanks,
Slava.
Quoted 11 lines
fs/hfsplus/catalog.c +7
-0+pr_err ("catalog name length corrupted\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-Quoted 1 line
i_ino, str);if (unlikely(err))