hfsplus: fix null-ptr-deref in hfsplus_cat_write_inode
Subsystems (2)
PATCH v3 1/1 hfsplus: fix null-ptr-deref in hfsplus_cat_write_inode
When a corrupt HFS+ image is mounted where the hidden directory is absent, hfsplus_fill_super() handles -ENOENT from hfsplus_get_hidden_dir_entry() silently and continues with sbi->hidden_dir left as NULL.
hfsplus_link() and hfsplus_unlink() call hfsplus_cat_write_inode(sbi->hidden_dir) unconditionally without checking whether hidden_dir is NULL, triggering a general protection fault caught by KASAN as a null-ptr-deref at offset 0x28.
Other call sites in dir.c already guard against this by checking hidden_dir for NULL before use. Apply the same guard to the two unprotected call sites in hfsplus_link() and hfsplus_unlink().
Changes in v3:
- Correct fix location: guard sbi->hidden_dir before calling hfsplus_cat_write_inode() in hfsplus_link() and hfsplus_unlink() in dir.c, as suggested by Vyacheslav Dubeyko.
Changes in v2:
- Fixed commit message: hfsplus_delete_cat() is called from multiple callers, not just hfsplus_unlink() as incorrectly stated in v1.
--- fs/hfsplus/dir.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
fs/hfsplus/dir.c +8 -7
@@ -362,9 +362,11 @@ static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir,if (res) goto out; - res =hfsplus_cat_write_inode (sbi->hidden_dir); -if (res) - goto out; +if (sbi->hidden_dir) { + res =hfsplus_cat_write_inode (sbi->hidden_dir); +if (res) + goto out; + } res =hfsplus_cat_write_inode (inode);
@@ -431,11 +433,10 @@ static int hfsplus_unlink(struct inode *dir, struct dentry *dentry) out:if (!res) { res =hfsplus_cat_write_inode (dir); -if (!res) { +if (!res && sbi->hidden_dir) res =hfsplus_cat_write_inode (sbi->hidden_dir); -if (!res) - res =hfsplus_cat_write_inode (inode); - } +if (!res) + res =hfsplus_cat_write_inode (inode); }mutex_unlock (&sbi->vh_mutex);
Signature · 1 line
2.43.0
Quoted 56 lines · Deepanshu Kartikey
When a corrupt HFS+ image is mounted where the hidden directory is absent,
hfsplus_fill_super()handles-ENOENTfromhfsplus_get_hidden_dir_entry()silently and continues withsbi->hidden_dirleft as NULL.
hfsplus_link()andhfsplus_unlink()callhfsplus_cat_write_inode(sbi->hidden_dir)unconditionally without checking whetherhidden_diris NULL, triggering a general protection fault caught by KASAN as a null-ptr-deref at offset0x28.Other call sites in
dir.calready guard against this by checkinghidden_dirfor NULL before use. Apply the same guard to the two unprotected call sites inhfsplus_link()andhfsplus_unlink().Changes in v3:
- Correct fix location: guardsbi->hidden_dirbefore callinghfsplus_cat_write_inode()inhfsplus_link()andhfsplus_unlink()indir.c, as suggested by Vyacheslav Dubeyko.Changes in v2:
- Fixed commit message:hfsplus_delete_cat()is called from multiple callers, not justhfsplus_unlink()as incorrectly stated in v1.Reported-by [email protected]Tested-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>--- fs/hfsplus/dir.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)fs/hfsplus/dir.c +5
-4@@ -362,9 +362,11 @@ static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir,if (res) goto out; - res =hfsplus_cat_write_inode (sbi->hidden_dir); -if (res) - goto out; +if (sbi->hidden_dir) { + res =hfsplus_cat_write_inode (sbi->hidden_dir); +if (res) + goto out; + } res =hfsplus_cat_write_inode (inode);@@ -431,11 +433,10 @@ static int hfsplus_unlink(struct inode *dir, struct dentry *dentry) out:if (!res) { res =hfsplus_cat_write_inode (dir); -if (!res) {
fs/hfsplus/dir.c static int hfsplus_unlink(struct inode *dir, struct dentry *dentry) · line 442
Quoted 10 lines
fs/hfsplus/dir.c +3
-3+if (!res && sbi->hidden_dir) res =hfsplus_cat_write_inode (sbi->hidden_dir); -if (!res) - res =hfsplus_cat_write_inode (inode); - } +if (!res) + res =hfsplus_cat_write_inode (inode); }mutex_unlock (&sbi->vh_mutex);
As far as I can see, we have such logic in hfsplus_fill_super() [1]:
if (!sb_rdonly(sb)) {
<skipped>
if (!sbi->hidden_dir) {
<create hidden dir>
}<skipped>
}
So, I have two questions:
(1) If we mounted volume in READ-ONLY mode initially and we re-mount it into READ-WRITE mode, then we skip hidden directory creation. Should we process this in hfsplus_reconfigure()?
(2) Should we create hidden directory if corrupted volume hasn't hidden directory?
If we don't create the hidden dir, then we could have troubles in multiple
places: hfsplus_rename_cat() [2,3], hfsplus_delete_cat() [4,5]. It looks like that we need to create the hidden dir. Otherwise, we could trigger the kernel crash in multiple places.
Thanks,
Slava.
[1] https://elixir.bootlin.com/linux/v7.0/source/fs/hfsplus/super.c#L589
[2] https://elixir.bootlin.com/linux/v7.0/source/fs/hfsplus/dir.c#L326
[3] https://elixir.bootlin.com/linux/v7.0/source/fs/hfsplus/dir.c#L397
[4] https://elixir.bootlin.com/linux/v7.0/source/fs/hfsplus/dir.c#L418
[5] https://elixir.bootlin.com/linux/v7.0/source/fs/hfsplus/inode.c#L257
Quoted 23 lines · Viacheslav Dubeyko
As far as I can see, we have such logic in
hfsplus_fill_super()[1]:if (!sb_rdonly(sb)) {
<skipped>if (!sbi->hidden_dir) { <create hidden dir> }<skipped>
}So, I have two questions:
(1) If we mounted volume in READ-ONLY mode initially and we re-mount it into READ-WRITE mode, then we skip hidden directory creation. Should we process this inhfsplus_reconfigure()?
(2) Should we create hidden directory if corrupted volume hasn't hidden directory?If we don't create the hidden dir, then we could have troubles in multiple
places:hfsplus_rename_cat()[2,3],hfsplus_delete_cat()[4,5]. It looks like that we need to create the hidden dir. Otherwise, we could trigger the kernel crash in multiple places.
Quoted 9 lines
Thanks,
Slava.[1] https://elixir.bootlin.com/linux/v7.0/source/fs/hfsplus/super.c#L589
[2] https://elixir.bootlin.com/linux/v7.0/source/fs/hfsplus/dir.c#L326
[3] https://elixir.bootlin.com/linux/v7.0/source/fs/hfsplus/dir.c#L397
[4] https://elixir.bootlin.com/linux/v7.0/source/fs/hfsplus/dir.c#L418
[5] https://elixir.bootlin.com/linux/v7.0/source/fs/hfsplus/inode.c#L257
Hi Vyacheslav,
Thank you for the detailed feedback and for pointing out the deeper design issue.
You are absolutely right on both counts.
(1) hfsplus_reconfigure() does not create the hidden directory when remounting from read-only to read-write. This means sbi->hidden_dir can be NULL on a writable mount even if the volume is otherwise healthy. This needs to be fixed in hfsplus_reconfigure().
(2) The correct approach is not to add NULL checks at individual call sites — that is a band-aid. The real fix is to ensure sbi->hidden_dir is always valid on any read-write mount, enforcing this invariant at mount and remount time.
My plan for v4:
- Extract the hidden directory creation logic from hfsplus_fill_super() into a helper function hfsplus_create_hidden_dir().
- Call it from hfsplus_fill_super() as before.
- Also call it from hfsplus_reconfigure() when switching from read-only to read-write and hidden_dir is NULL.
This way hidden_dir is guaranteed valid for all write operations and the individual call sites in dir.c and inode.c are safe without needing NULL guards.
I will send v4 shortly.
Regards,
Deeanshu Kartikey
Quoted 58 lines · Deepanshu Kartikey
Quoted 23 lines · Viacheslav Dubeyko
As far as I can see, we have such logic in
hfsplus_fill_super()[1]:if (!sb_rdonly(sb)) {
<skipped>if (!sbi->hidden_dir) { <create hidden dir> }<skipped>
}So, I have two questions:
(1) If we mounted volume in READ-ONLY mode initially and we re-mount it into READ-WRITE mode, then we skip hidden directory creation. Should we process this inhfsplus_reconfigure()?
(2) Should we create hidden directory if corrupted volume hasn't hidden directory?If we don't create the hidden dir, then we could have troubles in multiple
places:hfsplus_rename_cat()[2,3],hfsplus_delete_cat()[4,5]. It looks like that we need to create the hidden dir. Otherwise, we could trigger the kernel crash in multiple places.Quoted 9 lines
Thanks,
Slava.[1] https://elixir.bootlin.com/linux/v7.0/source/fs/hfsplus/super.c#L589
[2] https://elixir.bootlin.com/linux/v7.0/source/fs/hfsplus/dir.c#L326
[3] https://elixir.bootlin.com/linux/v7.0/source/fs/hfsplus/dir.c#L397
[4] https://elixir.bootlin.com/linux/v7.0/source/fs/hfsplus/dir.c#L418
[5] https://elixir.bootlin.com/linux/v7.0/source/fs/hfsplus/inode.c#L257Hi Vyacheslav,
Thank you for the detailed feedback and for pointing out the deeper design issue.
You are absolutely right on both counts.
(1)
hfsplus_reconfigure()does not create the hidden directory when remounting from read-only to read-write. This meanssbi->hidden_dircan be NULL on a writable mount even if the volume is otherwise healthy. This needs to be fixed inhfsplus_reconfigure().(2) The correct approach is not to add NULL checks at individual call sites — that is a band-aid. The real fix is to ensure
sbi->hidden_diris always valid on any read-write mount, enforcing this invariant at mount and remount time.My plan for v4:
- Extract the hidden directory creation logic fromhfsplus_fill_super()into a helper functionhfsplus_create_hidden_dir().
- Call it fromhfsplus_fill_super()as before.
- Also call it fromhfsplus_reconfigure()when switching from read-only to read-write andhidden_diris NULL.
Quoted 4 lines
This way
hidden_diris guaranteed valid for all write operations and the individual call sites indir.candinode.care safe without needing NULL guards.
Makes sense.
Quoted 3 lines
I will send v4 shortly.
Sounds good.
Thanks,
Slava.