Skip to contentSkip to conversation

mm/hugetlb: Fix potential double free in hugetlb_register_node() error path

PATCH Applied Miaohe Lin 4 participants 5 messages Jan 7, 2021 – Jan 9, 2021

Subsystems (2)
lore
Share & tools
lore

Miaohe Lin

author
#0 lore raw

PATCH 1/1 mm/hugetlb: Fix potential double free in hugetlb_register_node() error path

In hugetlb_sysfs_add_hstate(), we would do kobject_put() on hstate_kobjs when failed to create sysfs group but forget to set hstate_kobjs to NULL. Then in hugetlb_register_node() error path, we may free it again via hugetlb_unregister_node().

Fixes a3437870160c ("hugetlb: new sysfs interface")
---
 mm/hugetlb.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
mm/hugetlb.c +3 -1
index e249bffa0e75..91a2a2025a2c 100644
@@ -2947,8 +2947,10 @@ static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent,
 		return -ENOMEM;
 
 	retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group);
-	if (retval)
+	if (retval) {
 		kobject_put(hstate_kobjs[hi]);
+		hstate_kobjs[hi] = NULL;
+	}
 
 	return retval;
 }
Signature · 1 line
2.19.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Miaohe Lin

In hugetlb_sysfs_add_hstate(), we would do kobject_put() on hstate_kobjs when failed to create sysfs group but forget to set hstate_kobjs to NULL. Then in hugetlb_register_node() error path, we may free it again via hugetlb_unregister_node().

Fixes a3437870160c ("hugetlb: new sysfs interface")
---
 mm/hugetlb.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Thanks, this is a potential issue that should be fixed.

This has been around for a long time (more than 12 years). I suspect nobody actually experienced this issue. You just discovered via code inspection. Correct?
At one time cc stable would not be accepted for this type of issue, not sure about today.

Signature · 1 line
Mike Kravetz
maintainer
#2 lore raw
Quoted 15 lines · On Thu, 7 Jan 2021 11:59:38 -0800 Mike Kravetz <[email protected]> wrote:
Quoted 11 lines · Miaohe Lin

In hugetlb_sysfs_add_hstate(), we would do kobject_put() on hstate_kobjs when failed to create sysfs group but forget to set hstate_kobjs to NULL. Then in hugetlb_register_node() error path, we may free it again via hugetlb_unregister_node().

Fixes a3437870160c ("hugetlb: new sysfs interface")
---
 mm/hugetlb.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Thanks, this is a potential issue that should be fixed.

Quoted 7 lines

This has been around for a long time (more than 12 years). I suspect nobody actually experienced this issue. You just discovered via code inspection. Correct?
At one time cc stable would not be accepted for this type of issue, not sure about today.

sysfs_create_group() will only fail if something is terribly messed up
- probably it has never happened to anyone. I don't think the cc:stable is justified here.

author
#3 lore raw
Quoted 15 lines · Andrew Morton
Quoted 13 lines · On Thu, 7 Jan 2021 11:59:38 -0800 Mike Kravetz <[email protected]> wrote:
Quoted 11 lines · Miaohe Lin

In hugetlb_sysfs_add_hstate(), we would do kobject_put() on hstate_kobjs when failed to create sysfs group but forget to set hstate_kobjs to NULL. Then in hugetlb_register_node() error path, we may free it again via hugetlb_unregister_node().

Fixes a3437870160c ("hugetlb: new sysfs interface")
---
 mm/hugetlb.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 7 lines
Quoted 7 lines

Thanks, this is a potential issue that should be fixed.

This has been around for a long time (more than 12 years). I suspect nobody actually experienced this issue. You just discovered via code inspection. Correct?

Yes, I found this by code inspection.

Quoted 9 lines
Quoted 2 lines

At one time cc stable would not be accepted for this type of issue, not sure about today.

sysfs_create_group() will only fail if something is terribly messed up
- probably it has never happened to anyone. I don't think the cc:stable is justified here.

.

I would take care of more when cc stable. Many thanks for both of you!

Reviewed-by on v1 1/1
#4 lore raw
In reply to Miaohe Lin
Quoted 6 lines · Miaohe Lin

In hugetlb_sysfs_add_hstate(), we would do kobject_put() on hstate_kobjs when failed to create sysfs group but forget to set hstate_kobjs to NULL. Then in hugetlb_register_node() error path, we may free it again via hugetlb_unregister_node().

Quoted 6 lines
Fixes a3437870160c ("hugetlb: new sysfs interface")
---
 mm/hugetlb.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 20 lines
mm/hugetlb.c +3 -2
index e249bffa0e75..91a2a2025a2c 100644
@@ -2947,8 +2947,10 @@ static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent,
                return -ENOMEM;
 
        retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group);
-       if (retval)
+       if (retval) {
                kobject_put(hstate_kobjs[hi]);
+               hstate_kobjs[hi] = NULL;
+       }
 
        return retval;
 }
--

2.19.1