Skip to contentSkip to conversation

hfs, hfsplus: Fix NULL pointer dereference in hfs_find_init()

PATCH In review Peilin Ye 5 participants 10 messages Aug 12, 2020

Subsystems (3)
lore
Share & tools
lore

Peilin Ye

author
#0 lore raw

PATCH 1/1 hfs, hfsplus: Fix NULL pointer dereference in hfs_find_init()

Prevent hfs_find_init() from dereferencing tree as NULL.

Reported-and-tested-by [email protected]
---
 fs/hfs/bfind.c     | 3 +++
 fs/hfsplus/bfind.c | 3 +++
 2 files changed, 6 insertions(+)
fs/hfs/bfind.c +3 -0
index 4af318fbda77..880b7ea2c0fc 100644
@@ -16,6 +16,9 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd)
 {
 	void *ptr;
 
+	if (!tree)
+		return -EINVAL;
+
 	fd->tree = tree;
 	fd->bnode = NULL;
 	ptr = kmalloc(tree->max_key_len * 2 + 4, GFP_KERNEL);
fs/hfsplus/bfind.c +3 -0
index ca2ba8c9f82e..85bef3e44d7a 100644
@@ -16,6 +16,9 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd)
 {
 	void *ptr;
 
+	if (!tree)
+		return -EINVAL;
+
 	fd->tree = tree;
 	fd->bnode = NULL;
 	ptr = kmalloc(tree->max_key_len * 2 + 4, GFP_KERNEL);
Signature · 1 line
2.25.1
#1 lore raw
Quoted 28 lines · Peilin Ye

Prevent hfs_find_init() from dereferencing tree as NULL.

Reported-and-tested-by [email protected]
---
 fs/hfs/bfind.c     | 3 +++
 fs/hfsplus/bfind.c | 3 +++
 2 files changed, 6 insertions(+)
fs/hfs/bfind.c +3 -0
index 4af318fbda77..880b7ea2c0fc 100644
@@ -16,6 +16,9 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd)
 {
 	void *ptr;
 
+	if (!tree)
+		return -EINVAL;
+
 	fd->tree = tree;
 	fd->bnode = NULL;
 	ptr = kmalloc(tree->max_key_len * 2 + 4, GFP_KERNEL);
fs/hfsplus/bfind.c +0 -0
index ca2ba8c9f82e..85bef3e44d7a 100644

fs/hfsplus/bfind.c int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd) · line 21

Quoted 6 lines
fs/hfsplus/bfind.c +3 -0
@@ -16,6 +16,9 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd)
 {
 	void *ptr;
 
+	if (!tree)
+		return -EINVAL;
+

How can tree ever be NULL in these calls? Shouldn't that be fixed as the root problem here?

thanks,

greg k-h

author
#2 lore raw
Quoted 32 lines · Greg Kroah-Hartman
Quoted 31 lines · Peilin Ye

Prevent hfs_find_init() from dereferencing tree as NULL.

Reported-and-tested-by [email protected]
---
 fs/hfs/bfind.c     | 3 +++
 fs/hfsplus/bfind.c | 3 +++
 2 files changed, 6 insertions(+)
fs/hfs/bfind.c +3 -0
index 4af318fbda77..880b7ea2c0fc 100644
@@ -16,6 +16,9 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd)
 {
 	void *ptr;
 
+	if (!tree)
+		return -EINVAL;
+
 	fd->tree = tree;
 	fd->bnode = NULL;
 	ptr = kmalloc(tree->max_key_len * 2 + 4, GFP_KERNEL);
fs/hfsplus/bfind.c +0 -0
index ca2ba8c9f82e..85bef3e44d7a 100644
@@ -16,6 +16,9 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd)
 {
 	void *ptr;
 
Quoted 6 lines

fs/hfsplus/bfind.c int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd) · line 21

Quoted 3 lines
fs/hfsplus/bfind.c +3 -0
+	if (!tree)
+		return -EINVAL;
+

How can tree ever be NULL in these calls? Shouldn't that be fixed as the root problem here?

I see, I will try to figure out what is going on with the reproducer.

Thank you,
Peilin Ye

#3 lore raw
Quoted 33 lines · Peilin Ye
Quoted 32 lines · Greg Kroah-Hartman
Quoted 31 lines · Peilin Ye

Prevent hfs_find_init() from dereferencing tree as NULL.

Reported-and-tested-by [email protected]
---
 fs/hfs/bfind.c     | 3 +++
 fs/hfsplus/bfind.c | 3 +++
 2 files changed, 6 insertions(+)
fs/hfs/bfind.c +3 -0
index 4af318fbda77..880b7ea2c0fc 100644
@@ -16,6 +16,9 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd)
 {
 	void *ptr;
 
+	if (!tree)
+		return -EINVAL;
+
 	fd->tree = tree;
 	fd->bnode = NULL;
 	ptr = kmalloc(tree->max_key_len * 2 + 4, GFP_KERNEL);
fs/hfsplus/bfind.c +0 -0
index ca2ba8c9f82e..85bef3e44d7a 100644
@@ -16,6 +16,9 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd)
 {
 	void *ptr;
 
Quoted 8 lines
Quoted 6 lines

fs/hfsplus/bfind.c int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd) · line 21

Quoted 3 lines
fs/hfsplus/bfind.c +3 -0
+	if (!tree)
+		return -EINVAL;
+

How can tree ever be NULL in these calls? Shouldn't that be fixed as the root problem here?

I see, I will try to figure out what is going on with the reproducer.

That's good to figure out. Note, your patch might be the correct thing to do, as that might be an allowed way to call the function. But in looking at all the callers, they seem to think they have a valid pointer at the moment, so perhaps if this check is added, some other root problem is papered over to be only found later on?

thanks,

greg k-h

author
#4 lore raw
Quoted 41 lines · Greg Kroah-Hartman
Quoted 40 lines · Peilin Ye
Quoted 38 lines · Greg Kroah-Hartman
Quoted 28 lines · Peilin Ye

Prevent hfs_find_init() from dereferencing tree as NULL.

Reported-and-tested-by [email protected]
---
 fs/hfs/bfind.c     | 3 +++
 fs/hfsplus/bfind.c | 3 +++
 2 files changed, 6 insertions(+)
fs/hfs/bfind.c +3 -0
index 4af318fbda77..880b7ea2c0fc 100644
@@ -16,6 +16,9 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd)
 {
 	void *ptr;
 
+	if (!tree)
+		return -EINVAL;
+
 	fd->tree = tree;
 	fd->bnode = NULL;
 	ptr = kmalloc(tree->max_key_len * 2 + 4, GFP_KERNEL);
fs/hfsplus/bfind.c +0 -0
index ca2ba8c9f82e..85bef3e44d7a 100644

fs/hfsplus/bfind.c int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd) · line 21

Quoted 6 lines
fs/hfsplus/bfind.c +3 -0
@@ -16,6 +16,9 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd)
 {
 	void *ptr;
 
+	if (!tree)
+		return -EINVAL;
+

How can tree ever be NULL in these calls? Shouldn't that be fixed as the root problem here?

Quoted 7 lines
Quoted 1 line

I see, I will try to figure out what is going on with the reproducer.

That's good to figure out. Note, your patch might be the correct thing to do, as that might be an allowed way to call the function. But in looking at all the callers, they seem to think they have a valid pointer at the moment, so perhaps if this check is added, some other root problem is papered over to be only found later on?

That's right - Yesterday I noticed that this function has a number of callers who don't check tree at all, so I thought maybe we just add the check here...Turned out to be quite the opposite.

Thank you,
Peilin Ye
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#5 lore raw
In reply to Peilin Ye

Yeah, the patch doesn't work at all. I looked at one call tree and it is:

hfs_mdb_get() tries to allocate HFS_SB(sb)->ext_tree.

	HFS_SB(sb)->ext_tree = hfs_btree_open(sb, HFS_EXT_CNID, hfs_ext_keycmp);
                    ^^^^^^^^

hfs_btree_open() calls page = read_mapping_page(mapping, 0, NULL); read_mapping_page() calls mapping->a_ops->readpage() which leads to hfs_readpage() which leads to hfs_ext_read_extent() which calls

res = hfs_find_init(HFS_SB(inode->i_sb)->ext_tree, &fd);
                                         ^^^^^^^^

So we need ->ext_tree to be non-NULL before we can set ->ext_tree to be non-NULL... :/

I wonder how long this has been broken and if we should just delete the AFS file system.

regards,
dan carpenter

#6 lore raw

Hello guys hope you are doing good! we are Bigbudsupply we grow and sell the best medical marijuana product, we are looking for long time customers, you can Email us /[email protected]
Text/+14432672189
Looking forward to working with you guys

Quoted 52 lines · On Wed, 12 Aug 2020 at 09:59 Dan Carpenter <[email protected]> wrote:

Yeah, the patch doesn't work at all. I looked at one call tree and it

is:

hfs_mdb_get() tries to allocate HFS_SB(sb)->ext_tree.

        HFS_SB(sb)->ext_tree = hfs_btree_open(sb, HFS_EXT_CNID,

hfs_ext_keycmp);

                    ^^^^^^^^

hfs_btree_open() calls page = read_mapping_page(mapping, 0, NULL);

read_mapping_page() calls mapping->a_ops->readpage() which leads to

hfs_readpage() which leads to hfs_ext_read_extent() which calls

res = hfs_find_init(HFS_SB(inode->i_sb)->ext_tree, &fd);

                                         ^^^^^^^^

So we need ->ext_tree to be non-NULL before we can set ->ext_tree to be

non-NULL... :/

I wonder how long this has been broken and if we should just delete the

AFS file system.

regards,

dan carpenter

--

You received this message because you are subscribed to the Google Groups "syzkaller-bugs" group.

Quoted 8 lines

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To view this discussion on the web visit
https://groups.google.com/d/msgid/syzkaller-bugs/20200812085904.GA16441%40kadam
.

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

author
#7 lore raw
In reply to Dan Carpenter
Quoted 5 lines · Dan Carpenter

Yeah, the patch doesn't work at all. I looked at one call tree and it is:

hfs_mdb_get() tries to allocate HFS_SB(sb)->ext_tree.

Quoted 8 lines
	HFS_SB(sb)->ext_tree = hfs_btree_open(sb, HFS_EXT_CNID, hfs_ext_keycmp);
                    ^^^^^^^^

hfs_btree_open() calls page = read_mapping_page(mapping, 0, NULL); read_mapping_page() calls mapping->a_ops->readpage() which leads to hfs_readpage() which leads to hfs_ext_read_extent() which calls

res = hfs_find_init(HFS_SB(inode->i_sb)->ext_tree, &fd);
                                         ^^^^^^^^

Thank you for pointing this out! I will try to come up with a better way to fix it.

Peilin Ye

Quoted 9 lines

So we need ->ext_tree to be non-NULL before we can set ->ext_tree to be non-NULL... :/

I wonder how long this has been broken and if we should just delete the AFS file system.

regards,
dan carpenter

#8 lore raw
In reply to Dan Carpenter

Hi,

Quoted 8 lines · Dan Carpenter

Yeah, the patch doesn't work at all. I looked at one call tree and it is:

hfs_mdb_get() tries to allocate HFS_SB(sb)->ext_tree.

	HFS_SB(sb)->ext_tree = hfs_btree_open(sb, HFS_EXT_CNID, hfs_ext_keycmp);
                    ^^^^^^^^
Quoted 8 lines

hfs_btree_open() calls page = read_mapping_page(mapping, 0, NULL); read_mapping_page() calls mapping->a_ops->readpage() which leads to hfs_readpage() which leads to hfs_ext_read_extent() which calls

res = hfs_find_init(HFS_SB(inode->i_sb)->ext_tree, &fd);
                                         ^^^^^^^^

So we need ->ext_tree to be non-NULL before we can set ->ext_tree to be non-NULL... :/

For HFS+, the first 8 extents for a file are kept inside its own fork data structure, not in the extent tree. So, in normal operation, you don't need to search the extent tree to find the first page of the extent tree itself. The HFS layout is different, but it should work the same way.

Of course this sort of thing can still be triggered by crafted filesystems. If that's what the reproducer is about, I think just returning an error is reasonable. But these modules will never be safe against attacks such as this.

Quoted 5 lines

I wonder how long this has been broken and if we should just delete the AFS file system.

regards,
dan carpenter

#9 lore raw
Quoted 2 lines · Ernesto A. Fernández

If that's what the reproducer is about, I think just returning an error is reasonable.

I guess it would be better to put a check inside hfsplus_inode_read_fork(), to verify that the first extent is always in the right place and wide enough.