Skip to contentSkip to conversation

ocfs2: possible memory leak in mlog_sys_init()

PATCH In review Yang Yingliang 3 participants 10 messages Oct 18, 2022 – Oct 20, 2022 OCFS2

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ocfs2: possible memory leak in mlog_sys_init()

Inject fault while probing module, kset_register() may fail, if it fails, but the refcount of kobject is not decreased to 0, the name allocated in kobject_set_name() is leaked. Fix this by calling kset_put(), so that name can be freed in callback function kobject_cleanup().

unreferenced object 0xffff888100da9348 (size 8):
comm "modprobe", pid 257, jiffies 4294701096 (age 33.334s) hex dump (first 8 bytes):
6c 6f 67 6d 61 73 6b 00 logmask.
backtrace:

    [<00000000306e441c>] __kmalloc_node_track_caller+0x44/0x1b0
    [<000000007c491a9e>] kstrdup+0x3a/0x70
    [<0000000015719a3b>] kstrdup_const+0x63/0x80
    [<0000000084e458ea>] kvasprintf_const+0x149/0x180
    [<0000000091302b42>] kobject_set_name_vargs+0x56/0x150
    [<000000005f48eeac>] kobject_set_name+0xab/0xe0
Fixes 34980ca8faeb ("Drivers: clean up direct setting of the name of a kset")
---
 fs/ocfs2/cluster/masklog.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/ocfs2/cluster/masklog.c +6 -1
index 563881ddbf00..7f9ba816d955 100644
@@ -156,6 +156,7 @@ static struct kset mlog_kset = {
 int mlog_sys_init(struct kset *o2cb_kset)
 {
 	int i = 0;
+	int ret;
 
 	while (mlog_attrs[i].attr.mode) {
 		mlog_default_attrs[i] = &mlog_attrs[i].attr;
@@ -165,7 +166,11 @@ int mlog_sys_init(struct kset *o2cb_kset)
 
 	kobject_set_name(&mlog_kset.kobj, "logmask");
 	mlog_kset.kobj.kset = o2cb_kset;
-	return kset_register(&mlog_kset);
+	ret = kset_register(&mlog_kset);
+	if (ret)
+		kset_put(&mlog_kset);
+
+	return ret;
 }
 
 void mlog_sys_shutdown(void)
Signature · 1 line
2.25.1
maintainer
#1 lore raw

Hi,

Quoted 37 lines · Yang Yingliang

Inject fault while probing module, kset_register() may fail, if it fails, but the refcount of kobject is not decreased to 0, the name allocated in kobject_set_name() is leaked. Fix this by calling kset_put(), so that name can be freed in callback function kobject_cleanup().

unreferenced object 0xffff888100da9348 (size 8):
comm "modprobe", pid 257, jiffies 4294701096 (age 33.334s) hex dump (first 8 bytes):
6c 6f 67 6d 61 73 6b 00 logmask.
backtrace:

    [<00000000306e441c>] __kmalloc_node_track_caller+0x44/0x1b0
    [<000000007c491a9e>] kstrdup+0x3a/0x70
    [<0000000015719a3b>] kstrdup_const+0x63/0x80
    [<0000000084e458ea>] kvasprintf_const+0x149/0x180
    [<0000000091302b42>] kobject_set_name_vargs+0x56/0x150
    [<000000005f48eeac>] kobject_set_name+0xab/0xe0
Fixes 34980ca8faeb ("Drivers: clean up direct setting of the name of a kset")
---
 fs/ocfs2/cluster/masklog.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/ocfs2/cluster/masklog.c +1 -0
index 563881ddbf00..7f9ba816d955 100644
@@ -156,6 +156,7 @@ static struct kset mlog_kset = {
 int mlog_sys_init(struct kset *o2cb_kset)
 {
 	int i = 0;
+	int ret;
 
 	while (mlog_attrs[i].attr.mode) {
 		mlog_default_attrs[i] = &mlog_attrs[i].attr;

fs/ocfs2/cluster/masklog.c int mlog_sys_init(struct kset *o2cb_kset) · line 169

Quoted 5 lines
fs/ocfs2/cluster/masklog.c +1 -1
@@ -165,7 +166,11 @@ int mlog_sys_init(struct kset *o2cb_kset)
 
 	kobject_set_name(&mlog_kset.kobj, "logmask");
 	mlog_kset.kobj.kset = o2cb_kset;
-	return kset_register(&mlog_kset);
+	ret = kset_register(&mlog_kset);

If register fails, it will call unregister in o2cb_sys_init(), which will put kobject.

Thanks,
Joseph

fs/ocfs2/cluster/masklog.c int mlog_sys_init(struct kset *o2cb_kset) · line 176

Quoted 7 lines
fs/ocfs2/cluster/masklog.c +4 -0
+	if (ret)
+		kset_put(&mlog_kset);
+
+	return ret;
 }
 
 void mlog_sys_shutdown(void)

_______________________________________________
Ocfs2-devel mailing list
[email protected]
https://oss.oracle.com/mailman/listinfo/ocfs2-devel

#2 lore raw

Hi,

Quoted 41 lines · Joseph Qi

Hi,

Quoted 38 lines · Yang Yingliang

Inject fault while probing module, kset_register() may fail, if it fails, but the refcount of kobject is not decreased to 0, the name allocated in kobject_set_name() is leaked. Fix this by calling kset_put(), so that name can be freed in callback function kobject_cleanup().

unreferenced object 0xffff888100da9348 (size 8):

   comm "modprobe", pid 257, jiffies 4294701096 (age 33.334s)
   hex dump (first 8 bytes):
     6c 6f 67 6d 61 73 6b 00                          logmask.
   backtrace:
     [<00000000306e441c>] __kmalloc_node_track_caller+0x44/0x1b0
     [<000000007c491a9e>] kstrdup+0x3a/0x70
     [<0000000015719a3b>] kstrdup_const+0x63/0x80
     [<0000000084e458ea>] kvasprintf_const+0x149/0x180
     [<0000000091302b42>] kobject_set_name_vargs+0x56/0x150
     [<000000005f48eeac>] kobject_set_name+0xab/0xe0
Fixes 34980ca8faeb ("Drivers: clean up direct setting of the name of a kset")
---
  fs/ocfs2/cluster/masklog.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
fs/ocfs2/cluster/masklog.c +1 -0
index 563881ddbf00..7f9ba816d955 100644
@@ -156,6 +156,7 @@ static struct kset mlog_kset = {
  int mlog_sys_init(struct kset *o2cb_kset)
  {
  	int i = 0;
+	int ret;
 
  	while (mlog_attrs[i].attr.mode) {
  		mlog_default_attrs[i] = &mlog_attrs[i].attr;
@@ -165,7 +166,11 @@ int mlog_sys_init(struct kset *o2cb_kset)
 
Quoted 6 lines

fs/ocfs2/cluster/masklog.c int mlog_sys_init(struct kset *o2cb_kset) · line 169

Quoted 4 lines
fs/ocfs2/cluster/masklog.c +1 -1
  	kobject_set_name(&mlog_kset.kobj, "logmask");
  	mlog_kset.kobj.kset = o2cb_kset;
-	return kset_register(&mlog_kset);
+	ret = kset_register(&mlog_kset);

If register fails, it will call unregister in o2cb_sys_init(), which will put kobject.

They are different ksets, the kset unregistered in o2cb_sys_init() is 'o2cb_kset', the kset used to registered in mlog_sys_init() is 'mlog_kset', and they hold difference refcounts.

Thanks,
Yang

Quoted 12 lines

Thanks,
Joseph

fs/ocfs2/cluster/masklog.c int mlog_sys_init(struct kset *o2cb_kset) · line 176

Quoted 7 lines
fs/ocfs2/cluster/masklog.c +4 -0
+	if (ret)
+		kset_put(&mlog_kset);
+
+	return ret;
  }
 
  void mlog_sys_shutdown(void)

.

maintainer
#3 lore raw
Quoted 48 lines · Yang Yingliang

Hi,

Quoted 37 lines · Joseph Qi

Hi,

Quoted 34 lines · Yang Yingliang

Inject fault while probing module, kset_register() may fail, if it fails, but the refcount of kobject is not decreased to 0, the name allocated in kobject_set_name() is leaked. Fix this by calling kset_put(), so that name can be freed in callback function kobject_cleanup().

unreferenced object 0xffff888100da9348 (size 8):
   comm "modprobe", pid 257, jiffies 4294701096 (age 33.334s) hex dump (first 8 bytes):
     6c 6f 67 6d 61 73 6b 00                          logmask. backtrace:

     [<00000000306e441c>] __kmalloc_node_track_caller+0x44/0x1b0
     [<000000007c491a9e>] kstrdup+0x3a/0x70
     [<0000000015719a3b>] kstrdup_const+0x63/0x80
     [<0000000084e458ea>] kvasprintf_const+0x149/0x180
     [<0000000091302b42>] kobject_set_name_vargs+0x56/0x150
     [<000000005f48eeac>] kobject_set_name+0xab/0xe0
Fixes 34980ca8faeb ("Drivers: clean up direct setting of the name of a kset")

---
  fs/ocfs2/cluster/masklog.c | 7 ++++++-

  1 file changed, 6 insertions(+), 1 deletion(-)
fs/ocfs2/cluster/masklog.c +1 -0
index 563881ddbf00..7f9ba816d955 100644
@@ -156,6 +156,7 @@ static struct kset mlog_kset = {
   int mlog_sys_init(struct kset *o2cb_kset)
   {
       int i = 0;
+    int ret;
         while (mlog_attrs[i].attr.mode) {
Quoted 8 lines

fs/ocfs2/cluster/masklog.c int mlog_sys_init(struct kset *o2cb_kset) · line 168

Quoted 6 lines
fs/ocfs2/cluster/masklog.c +1 -1
           mlog_default_attrs[i] = &mlog_attrs[i].attr;
@@ -165,7 +166,11 @@ int mlog_sys_init(struct kset *o2cb_kset)
         kobject_set_name(&mlog_kset.kobj, "logmask");
       mlog_kset.kobj.kset = o2cb_kset;
-    return kset_register(&mlog_kset);
+    ret = kset_register(&mlog_kset);

If register fails, it will call unregister in o2cb_sys_init(), which will put kobject.

Quoted 4 lines

They are different ksets, the kset unregistered in o2cb_sys_init() is 'o2cb_kset', the kset used to registered in mlog_sys_init() is 'mlog_kset', and they hold difference refcounts.
Yes, you are right. I've mixed the two ksets up.

In theory, kset_register() may return error because of a NULL kset, so here we may not call kset_put() directly, I'm not sure if a static checker will happy.
Though this can't happen since it's already statically allocated...

Thanks,
Joseph

#4 lore raw
Quoted 50 lines · Joseph Qi
Quoted 48 lines · Yang Yingliang

Hi,

Quoted 45 lines · Joseph Qi

Hi,

Quoted 36 lines · Yang Yingliang

Inject fault while probing module, kset_register() may fail, if it fails, but the refcount of kobject is not decreased to 0, the name allocated in kobject_set_name() is leaked. Fix this by calling kset_put(), so that name can be freed in callback function kobject_cleanup().

unreferenced object 0xffff888100da9348 (size 8):
   comm "modprobe", pid 257, jiffies 4294701096 (age 33.334s) hex dump (first 8 bytes):
     6c 6f 67 6d 61 73 6b 00                          logmask. backtrace:

      [<00000000306e441c>] __kmalloc_node_track_caller+0x44/0x1b0
      [<000000007c491a9e>] kstrdup+0x3a/0x70
      [<0000000015719a3b>] kstrdup_const+0x63/0x80
      [<0000000084e458ea>] kvasprintf_const+0x149/0x180
      [<0000000091302b42>] kobject_set_name_vargs+0x56/0x150
      [<000000005f48eeac>] kobject_set_name+0xab/0xe0
Fixes 34980ca8faeb ("Drivers: clean up direct setting of the name of a kset")
---
   fs/ocfs2/cluster/masklog.c | 7 ++++++-
   1 file changed, 6 insertions(+), 1 deletion(-)
fs/ocfs2/cluster/masklog.c +1 -0
index 563881ddbf00..7f9ba816d955 100644
@@ -156,6 +156,7 @@ static struct kset mlog_kset = {
   int mlog_sys_init(struct kset *o2cb_kset)
   {
       int i = 0;
+    int ret;
         while (mlog_attrs[i].attr.mode) {
           mlog_default_attrs[i] = &mlog_attrs[i].attr;

fs/ocfs2/cluster/masklog.c int mlog_sys_init(struct kset *o2cb_kset) · line 168

Quoted 4 lines
fs/ocfs2/cluster/masklog.c +1 -1
@@ -165,7 +166,11 @@ int mlog_sys_init(struct kset *o2cb_kset)
         kobject_set_name(&mlog_kset.kobj, "logmask");
       mlog_kset.kobj.kset = o2cb_kset;
-    return kset_register(&mlog_kset);
+    ret = kset_register(&mlog_kset);

If register fails, it will call unregister in o2cb_sys_init(), which will put kobject.

Quoted 8 lines
Quoted 4 lines

They are different ksets, the kset unregistered in o2cb_sys_init() is 'o2cb_kset', the kset used to registered in mlog_sys_init() is 'mlog_kset', and they hold difference refcounts.
Yes, you are right. I've mixed the two ksets up.

In theory, kset_register() may return error because of a NULL kset, so here we may not call kset_put() directly, I'm not sure if a static checker will happy.
Though this can't happen since it's already statically allocated...

kset_register() may fail if kobject_add_internal() return error (can't allocate memory), the name "logmask" is dynamically alloctated while ocfs2 is compile as module and insert it (if ocfs2 is built in kernel, the name is constant, it won't cause a leak), so the name can be leaked.

Thanks,
Yang

Quoted 5 lines

Thanks,
Joseph

.

maintainer
#5 lore raw
Quoted 56 lines · Yang Yingliang
Quoted 54 lines · Joseph Qi
Quoted 52 lines · Yang Yingliang

Hi,

Quoted 37 lines · Joseph Qi

Hi,

Quoted 34 lines · Yang Yingliang

Inject fault while probing module, kset_register() may fail, if it fails, but the refcount of kobject is not decreased to 0, the name allocated in kobject_set_name() is leaked. Fix this by calling kset_put(), so that name can be freed in callback function kobject_cleanup().

unreferenced object 0xffff888100da9348 (size 8):
    comm "modprobe", pid 257, jiffies 4294701096 (age 33.334s) hex dump (first 8 bytes):
      6c 6f 67 6d 61 73 6b 00                          logmask. backtrace:

      [<00000000306e441c>] __kmalloc_node_track_caller+0x44/0x1b0
      [<000000007c491a9e>] kstrdup+0x3a/0x70
      [<0000000015719a3b>] kstrdup_const+0x63/0x80
      [<0000000084e458ea>] kvasprintf_const+0x149/0x180
      [<0000000091302b42>] kobject_set_name_vargs+0x56/0x150
      [<000000005f48eeac>] kobject_set_name+0xab/0xe0
Fixes 34980ca8faeb ("Drivers: clean up direct setting of the name of a kset")

---
   fs/ocfs2/cluster/masklog.c | 7 ++++++-

   1 file changed, 6 insertions(+), 1 deletion(-)
fs/ocfs2/cluster/masklog.c +1 -0
index 563881ddbf00..7f9ba816d955 100644
@@ -156,6 +156,7 @@ static struct kset mlog_kset = {
    int mlog_sys_init(struct kset *o2cb_kset)
    {
        int i = 0;
+    int ret;
          while (mlog_attrs[i].attr.mode) {
Quoted 8 lines

fs/ocfs2/cluster/masklog.c int mlog_sys_init(struct kset *o2cb_kset) · line 168

Quoted 6 lines
fs/ocfs2/cluster/masklog.c +1 -1
            mlog_default_attrs[i] = &mlog_attrs[i].attr;
@@ -165,7 +166,11 @@ int mlog_sys_init(struct kset *o2cb_kset)
          kobject_set_name(&mlog_kset.kobj, "logmask");
        mlog_kset.kobj.kset = o2cb_kset;
-    return kset_register(&mlog_kset);
+    ret = kset_register(&mlog_kset);

If register fails, it will call unregister in o2cb_sys_init(), which will put kobject.

They are different ksets, the kset unregistered in o2cb_sys_init() is 'o2cb_kset', the kset used to registered in mlog_sys_init() is 'mlog_kset', and they hold difference refcounts.
Yes, you are right. I've mixed the two ksets up.

Quoted 7 lines
Quoted 4 lines

In theory, kset_register() may return error because of a NULL kset, so here we may not call kset_put() directly, I'm not sure if a static checker will happy.
Though this can't happen since it's already statically allocated...

kset_register() may fail if kobject_add_internal() return error (can't allocate memory), the name "logmask" is dynamically alloctated while ocfs2 is compile as module and insert it (if ocfs2 is built in kernel, the name is constant, it won't cause a leak), so the name can be leaked.

What I mean is kset_register() may fail with many reasons, or even without kset_init().
I wonder if we have to handle this internal kset_register(), but not leave it to caller. This may benefit other callers as well.

Something like:
err = kobject_add_internal(&k->kobj);

if (err) {
	kset_put(k);
	return err;
}

Thanks,
Joseph

#6 lore raw
Quoted 68 lines · Joseph Qi
Quoted 54 lines · Yang Yingliang
Quoted 53 lines · Joseph Qi
Quoted 52 lines · Yang Yingliang

Hi,

Quoted 37 lines · Joseph Qi

Hi,

Quoted 34 lines · Yang Yingliang

Inject fault while probing module, kset_register() may fail, if it fails, but the refcount of kobject is not decreased to 0, the name allocated in kobject_set_name() is leaked. Fix this by calling kset_put(), so that name can be freed in callback function kobject_cleanup().

unreferenced object 0xffff888100da9348 (size 8):
    comm "modprobe", pid 257, jiffies 4294701096 (age 33.334s) hex dump (first 8 bytes):
      6c 6f 67 6d 61 73 6b 00                          logmask. backtrace:

       [<00000000306e441c>] __kmalloc_node_track_caller+0x44/0x1b0
       [<000000007c491a9e>] kstrdup+0x3a/0x70
       [<0000000015719a3b>] kstrdup_const+0x63/0x80
       [<0000000084e458ea>] kvasprintf_const+0x149/0x180
       [<0000000091302b42>] kobject_set_name_vargs+0x56/0x150
       [<000000005f48eeac>] kobject_set_name+0xab/0xe0
Fixes 34980ca8faeb ("Drivers: clean up direct setting of the name of a kset")
---
    fs/ocfs2/cluster/masklog.c | 7 ++++++-
    1 file changed, 6 insertions(+), 1 deletion(-)
fs/ocfs2/cluster/masklog.c +1 -0
index 563881ddbf00..7f9ba816d955 100644
@@ -156,6 +156,7 @@ static struct kset mlog_kset = {
    int mlog_sys_init(struct kset *o2cb_kset)
    {
        int i = 0;
+    int ret;
          while (mlog_attrs[i].attr.mode) {
Quoted 8 lines

fs/ocfs2/cluster/masklog.c int mlog_sys_init(struct kset *o2cb_kset) · line 168

Quoted 6 lines
fs/ocfs2/cluster/masklog.c +1 -1
            mlog_default_attrs[i] = &mlog_attrs[i].attr;
@@ -165,7 +166,11 @@ int mlog_sys_init(struct kset *o2cb_kset)
          kobject_set_name(&mlog_kset.kobj, "logmask");
        mlog_kset.kobj.kset = o2cb_kset;
-    return kset_register(&mlog_kset);
+    ret = kset_register(&mlog_kset);

If register fails, it will call unregister in o2cb_sys_init(), which will put kobject.

They are different ksets, the kset unregistered in o2cb_sys_init() is 'o2cb_kset', the kset used to registered in mlog_sys_init() is 'mlog_kset', and they hold difference refcounts.
Yes, you are right. I've mixed the two ksets up.

Quoted 7 lines
Quoted 4 lines

In theory, kset_register() may return error because of a NULL kset, so here we may not call kset_put() directly, I'm not sure if a static checker will happy.
Though this can't happen since it's already statically allocated...

kset_register() may fail if kobject_add_internal() return error (can't allocate memory), the name "logmask" is dynamically alloctated while ocfs2 is compile as module and insert it (if ocfs2 is built in kernel, the name is constant, it won't cause a leak), so the name can be leaked.

What I mean is kset_register() may fail with many reasons, or even without kset_init().
I wonder if we have to handle this internal kset_register(), but not leave it to caller. This may benefit other callers as well.

Quoted 6 lines

Something like:
err = kobject_add_internal(&k->kobj);

if (err) {
	kset_put(k);
	return err;
}

I had think about this method to fix this, but some kset is allocated dynamically in driver and it's freed in callback function which is called after kset_put() and in error path in driver will free it again, it leads double free in some drivers.

I think kset_register() is similar with device_register(), if it fails need another put function to give up reference in driver.

Thanks,
Yang

Quoted 5 lines

Thanks,
Joseph

.

maintainer
#7 lore raw
Quoted 76 lines · Yang Yingliang
Quoted 68 lines · Joseph Qi
Quoted 54 lines · Yang Yingliang
Quoted 53 lines · Joseph Qi
Quoted 52 lines · Yang Yingliang

Hi,

Quoted 37 lines · Joseph Qi

Hi,

Quoted 34 lines · Yang Yingliang

Inject fault while probing module, kset_register() may fail, if it fails, but the refcount of kobject is not decreased to 0, the name allocated in kobject_set_name() is leaked. Fix this by calling kset_put(), so that name can be freed in callback function kobject_cleanup().

unreferenced object 0xffff888100da9348 (size 8):
     comm "modprobe", pid 257, jiffies 4294701096 (age 33.334s) hex dump (first 8 bytes):
       6c 6f 67 6d 61 73 6b 00                          logmask. backtrace:

       [<00000000306e441c>] __kmalloc_node_track_caller+0x44/0x1b0
       [<000000007c491a9e>] kstrdup+0x3a/0x70
       [<0000000015719a3b>] kstrdup_const+0x63/0x80
       [<0000000084e458ea>] kvasprintf_const+0x149/0x180
       [<0000000091302b42>] kobject_set_name_vargs+0x56/0x150
       [<000000005f48eeac>] kobject_set_name+0xab/0xe0
Fixes 34980ca8faeb ("Drivers: clean up direct setting of the name of a kset")

---
    fs/ocfs2/cluster/masklog.c | 7 ++++++-

    1 file changed, 6 insertions(+), 1 deletion(-)
fs/ocfs2/cluster/masklog.c +1 -0
index 563881ddbf00..7f9ba816d955 100644
@@ -156,6 +156,7 @@ static struct kset mlog_kset = {
     int mlog_sys_init(struct kset *o2cb_kset)
     {
         int i = 0;
+    int ret;
           while (mlog_attrs[i].attr.mode) {
Quoted 8 lines

fs/ocfs2/cluster/masklog.c int mlog_sys_init(struct kset *o2cb_kset) · line 168

Quoted 6 lines
fs/ocfs2/cluster/masklog.c +1 -1
             mlog_default_attrs[i] = &mlog_attrs[i].attr;
@@ -165,7 +166,11 @@ int mlog_sys_init(struct kset *o2cb_kset)
           kobject_set_name(&mlog_kset.kobj, "logmask");
         mlog_kset.kobj.kset = o2cb_kset;
-    return kset_register(&mlog_kset);
+    ret = kset_register(&mlog_kset);

If register fails, it will call unregister in o2cb_sys_init(), which will put kobject.

They are different ksets, the kset unregistered in o2cb_sys_init() is 'o2cb_kset', the kset used to registered in mlog_sys_init() is 'mlog_kset', and they hold difference refcounts.
Yes, you are right. I've mixed the two ksets up.

Quoted 7 lines
Quoted 4 lines

In theory, kset_register() may return error because of a NULL kset, so here we may not call kset_put() directly, I'm not sure if a static checker will happy.
Though this can't happen since it's already statically allocated...

kset_register() may fail if kobject_add_internal() return error (can't allocate memory), the name "logmask" is dynamically alloctated while ocfs2 is compile as module and insert it (if ocfs2 is built in kernel, the name is constant, it won't cause a leak), so the name can be leaked.

What I mean is kset_register() may fail with many reasons, or even without kset_init().
I wonder if we have to handle this internal kset_register(), but not leave it to caller. This may benefit other callers as well.

Quoted 6 lines

Something like:
err = kobject_add_internal(&k->kobj);
if (err) {
    kset_put(k);
    return err;
}

Quoted 4 lines

I had think about this method to fix this, but some kset is allocated dynamically in driver and it's freed in callback function which is called after kset_put() and in error path in driver will free it again, it leads double free in some drivers.

I don't think it's good idea that caller has to take care part of the internal logic of kset_register() in case of error.
Hi Greg, what do you think?

Thanks,
Joseph

Quoted 3 lines

I think kset_register() is similar with device_register(), if it fails need another put function to give up reference in driver.

#8 lore raw
Quoted 79 lines · Joseph Qi
Quoted 76 lines · Yang Yingliang
Quoted 74 lines · Joseph Qi
Quoted 54 lines · Yang Yingliang
Quoted 53 lines · Joseph Qi
Quoted 52 lines · Yang Yingliang

Hi,

Quoted 37 lines · Joseph Qi

Hi,

Quoted 34 lines · Yang Yingliang

Inject fault while probing module, kset_register() may fail, if it fails, but the refcount of kobject is not decreased to 0, the name allocated in kobject_set_name() is leaked. Fix this by calling kset_put(), so that name can be freed in callback function kobject_cleanup().

unreferenced object 0xffff888100da9348 (size 8):
     comm "modprobe", pid 257, jiffies 4294701096 (age 33.334s) hex dump (first 8 bytes):
       6c 6f 67 6d 61 73 6b 00                          logmask. backtrace:

       [<00000000306e441c>] __kmalloc_node_track_caller+0x44/0x1b0
       [<000000007c491a9e>] kstrdup+0x3a/0x70
       [<0000000015719a3b>] kstrdup_const+0x63/0x80
       [<0000000084e458ea>] kvasprintf_const+0x149/0x180
       [<0000000091302b42>] kobject_set_name_vargs+0x56/0x150
       [<000000005f48eeac>] kobject_set_name+0xab/0xe0
Fixes 34980ca8faeb ("Drivers: clean up direct setting of the name of a kset")

---
    fs/ocfs2/cluster/masklog.c | 7 ++++++-

    1 file changed, 6 insertions(+), 1 deletion(-)
fs/ocfs2/cluster/masklog.c +1 -0
index 563881ddbf00..7f9ba816d955 100644
@@ -156,6 +156,7 @@ static struct kset mlog_kset = {
     int mlog_sys_init(struct kset *o2cb_kset)
     {
         int i = 0;
+    int ret;
           while (mlog_attrs[i].attr.mode) {
Quoted 8 lines

fs/ocfs2/cluster/masklog.c int mlog_sys_init(struct kset *o2cb_kset) · line 168

Quoted 6 lines
fs/ocfs2/cluster/masklog.c +1 -1
             mlog_default_attrs[i] = &mlog_attrs[i].attr;
@@ -165,7 +166,11 @@ int mlog_sys_init(struct kset *o2cb_kset)
           kobject_set_name(&mlog_kset.kobj, "logmask");
         mlog_kset.kobj.kset = o2cb_kset;
-    return kset_register(&mlog_kset);
+    ret = kset_register(&mlog_kset);

If register fails, it will call unregister in o2cb_sys_init(), which will put kobject.

They are different ksets, the kset unregistered in o2cb_sys_init() is 'o2cb_kset', the kset used to registered in mlog_sys_init() is 'mlog_kset', and they hold difference refcounts.
Yes, you are right. I've mixed the two ksets up.

Quoted 7 lines
Quoted 4 lines

In theory, kset_register() may return error because of a NULL kset, so here we may not call kset_put() directly, I'm not sure if a static checker will happy.
Though this can't happen since it's already statically allocated...

kset_register() may fail if kobject_add_internal() return error (can't allocate memory), the name "logmask" is dynamically alloctated while ocfs2 is compile as module and insert it (if ocfs2 is built in kernel, the name is constant, it won't cause a leak), so the name can be leaked.

What I mean is kset_register() may fail with many reasons, or even without kset_init().
I wonder if we have to handle this internal kset_register(), but not leave it to caller. This may benefit other callers as well.

Something like:
err = kobject_add_internal(&k->kobj);
if (err) {
    kset_put(k);
    return err;
}

Quoted 7 lines
Quoted 4 lines

I had think about this method to fix this, but some kset is allocated dynamically in driver and it's freed in callback function which is called after kset_put() and in error path in driver will free it again, it leads double free in some drivers.

I don't think it's good idea that caller has to take care part of the internal logic of kset_register() in case of error.
Hi Greg, what do you think?

I think if you are messing around with raw ksets, you have to handle them properly :)

For some driver and kobject core functions, once you call register, you have to call put() to handle an error because the driver core can not know what you are doing with that memory at times.

But, maybe for ksets this is not needed and the kobject core can properly clean up from an error here. Yang, can you please look into this? That might make this much simpler.

Either way, the documentation for kset_register() needs to be fixed up first, so that people have a chance to know what to do here and THEN we can fix up the callers like this.

Yang, can you do this all as one long series that I can take through the driver core tree. No need to scatter it all across a bunch of different subsystems.

thanks,

greg k-h

_______________________________________________
Ocfs2-devel mailing list
[email protected]
https://oss.oracle.com/mailman/listinfo/ocfs2-devel

#9 lore raw
Quoted 87 lines · Greg Kroah-Hartman
Quoted 76 lines · Joseph Qi
Quoted 74 lines · Yang Yingliang
Quoted 73 lines · Joseph Qi
Quoted 54 lines · Yang Yingliang
Quoted 53 lines · Joseph Qi
Quoted 52 lines · Yang Yingliang

Hi,

Quoted 37 lines · Joseph Qi

Hi,

Quoted 34 lines · Yang Yingliang

Inject fault while probing module, kset_register() may fail, if it fails, but the refcount of kobject is not decreased to 0, the name allocated in kobject_set_name() is leaked. Fix this by calling kset_put(), so that name can be freed in callback function kobject_cleanup().

unreferenced object 0xffff888100da9348 (size 8):
     comm "modprobe", pid 257, jiffies 4294701096 (age 33.334s) hex dump (first 8 bytes):
       6c 6f 67 6d 61 73 6b 00                          logmask. backtrace:

        [<00000000306e441c>] __kmalloc_node_track_caller+0x44/0x1b0
        [<000000007c491a9e>] kstrdup+0x3a/0x70
        [<0000000015719a3b>] kstrdup_const+0x63/0x80
        [<0000000084e458ea>] kvasprintf_const+0x149/0x180
        [<0000000091302b42>] kobject_set_name_vargs+0x56/0x150
        [<000000005f48eeac>] kobject_set_name+0xab/0xe0
Fixes 34980ca8faeb ("Drivers: clean up direct setting of the name of a kset")
---
     fs/ocfs2/cluster/masklog.c | 7 ++++++-
     1 file changed, 6 insertions(+), 1 deletion(-)
fs/ocfs2/cluster/masklog.c +1 -0
index 563881ddbf00..7f9ba816d955 100644
@@ -156,6 +156,7 @@ static struct kset mlog_kset = {
     int mlog_sys_init(struct kset *o2cb_kset)
     {
         int i = 0;
+    int ret;
           while (mlog_attrs[i].attr.mode) {
Quoted 8 lines

fs/ocfs2/cluster/masklog.c int mlog_sys_init(struct kset *o2cb_kset) · line 168

Quoted 6 lines
fs/ocfs2/cluster/masklog.c +1 -1
             mlog_default_attrs[i] = &mlog_attrs[i].attr;
@@ -165,7 +166,11 @@ int mlog_sys_init(struct kset *o2cb_kset)
           kobject_set_name(&mlog_kset.kobj, "logmask");
         mlog_kset.kobj.kset = o2cb_kset;
-    return kset_register(&mlog_kset);
+    ret = kset_register(&mlog_kset);

If register fails, it will call unregister in o2cb_sys_init(), which will put kobject.

They are different ksets, the kset unregistered in o2cb_sys_init() is 'o2cb_kset', the kset used to registered in mlog_sys_init() is 'mlog_kset', and they hold difference refcounts.
Yes, you are right. I've mixed the two ksets up.

Quoted 7 lines
Quoted 4 lines

In theory, kset_register() may return error because of a NULL kset, so here we may not call kset_put() directly, I'm not sure if a static checker will happy.
Though this can't happen since it's already statically allocated...

kset_register() may fail if kobject_add_internal() return error (can't allocate memory), the name "logmask" is dynamically alloctated while ocfs2 is compile as module and insert it (if ocfs2 is built in kernel, the name is constant, it won't cause a leak), so the name can be leaked.

What I mean is kset_register() may fail with many reasons, or even without kset_init().
I wonder if we have to handle this internal kset_register(), but not leave it to caller. This may benefit other callers as well.

Something like:
err = kobject_add_internal(&k->kobj);
if (err) {
    kset_put(k);
    return err;
}

Quoted 7 lines
Quoted 4 lines

I had think about this method to fix this, but some kset is allocated dynamically in driver and it's freed in callback function which is called after kset_put() and in error path in driver will free it again, it leads double free in some drivers.

I don't think it's good idea that caller has to take care part of the internal logic of kset_register() in case of error.
Hi Greg, what do you think?

I think if you are messing around with raw ksets, you have to handle them properly :)

Quoted 7 lines

For some driver and kobject core functions, once you call register, you have to call put() to handle an error because the driver core can not know what you are doing with that memory at times.

But, maybe for ksets this is not needed and the kobject core can properly clean up from an error here. Yang, can you please look into this? That might make this much simpler.

OK, I will look into this to see how to make it simpler for drivers.

Quoted 8 lines

Either way, the documentation for kset_register() needs to be fixed up first, so that people have a chance to know what to do here and THEN we can fix up the callers like this.

Yang, can you do this all as one long series that I can take through the driver core tree. No need to scatter it all across a bunch of different subsystems.

OK, I will fix all these in one series.

Thanks,
Yang

Quoted 5 lines

thanks,

greg k-h
.