Re: [PATCH v5] driver core: Fix use-after-free and double free on glue directory

From: Mukesh Ojha
Date: Wed Jul 24 2019 - 10:23:49 EST



On 7/24/2019 7:11 PM, Mukesh Ojha wrote:

On 7/18/2019 4:49 PM, Muchun Song wrote:
There is a race condition between removing glue directory and adding a new
device under the glue directory. It can be reproduced in following test:

path 1: Add the child device under glue dir
device_add()
ÂÂÂÂ get_device_parent()
ÂÂÂÂÂÂÂÂ mutex_lock(&gdp_mutex);
ÂÂÂÂÂÂÂÂ ....
ÂÂÂÂÂÂÂÂ /*find parent from glue_dirs.list*/
ÂÂÂÂÂÂÂÂ list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
ÂÂÂÂÂÂÂÂÂÂÂÂ if (k->parent == parent_kobj) {
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ kobj = kobject_get(k);
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ break;
ÂÂÂÂÂÂÂÂÂÂÂÂ }
ÂÂÂÂÂÂÂÂ ....
ÂÂÂÂÂÂÂÂ mutex_unlock(&gdp_mutex);
ÂÂÂÂÂÂÂÂ ....
ÂÂÂÂ ....
ÂÂÂÂ kobject_add()
ÂÂÂÂÂÂÂÂ kobject_add_internal()
ÂÂÂÂÂÂÂÂÂÂÂÂ create_dir()
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ sysfs_create_dir_ns()
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ if (kobj->parent)
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ parent = kobj->parent->sd;
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ ....
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ kernfs_create_dir_ns(parent)
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ kernfs_new_node()
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ kernfs_get(parent)
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ ....
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ /* link in */
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ rc = kernfs_add_one(kn);
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ if (!rc)
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ return kn;

ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ kernfs_put(kn)
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ ....
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ repeat:
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ kmem_cache_free(kn)
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ kn = parent;

ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ if (kn) {
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ if (atomic_dec_and_test(&kn->count))
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ goto repeat;
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ }
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ ....

path2: Remove last child device under glue dir
device_del()
ÂÂÂÂ cleanup_glue_dir()
ÂÂÂÂÂÂÂÂ mutex_lock(&gdp_mutex);
ÂÂÂÂÂÂÂÂ if (!kobject_has_children(glue_dir))
ÂÂÂÂÂÂÂÂÂÂÂÂ kobject_del(glue_dir);
ÂÂÂÂÂÂÂÂ kobject_put(glue_dir);
ÂÂÂÂÂÂÂÂ mutex_unlock(&gdp_mutex);

Before path2 remove last child device under glue dir, If path1 add a new
device under glue dir, the glue_dir kobject reference count will be
increase to 2 via kobject_get(k) in get_device_parent(). And path1 has
been called kernfs_new_node(), but not call kernfs_get(parent).
Meanwhile, path2 call kobject_del(glue_dir) beacause 0 is returned by
kobject_has_children(). This result in glue_dir->sd is freed and it's
reference count will be 0. Then path1 call kernfs_get(parent) will trigger
a warning in kernfs_get()(WARN_ON(!atomic_read(&kn->count))) and increase
it's reference count to 1. Because glue_dir->sd is freed by path2, the next
call kernfs_add_one() by path1 will fail(This is also use-after-free)
and call atomic_dec_and_test() to decrease reference count. Because the
reference count is decremented to 0, it will also call kmem_cache_free()
to free glue_dir->sd again. This will result in double free.

In order to avoid this happening, we also should make sure that kernfs_node
for glue_dir is released in path2 only when refcount for glue_dir kobj is
1 to fix this race.

The following calltrace is captured in kernel 4.14 with the following patch
applied:

commit 726e41097920 ("drivers: core: Remove glue dirs from sysfs earlier")

--------------------------------------------------------------------------
[ÂÂÂ 3.633703] WARNING: CPU: 4 PID: 513 at .../fs/kernfs/dir.c:494
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ Here is WARN_ON(!atomic_read(&kn->count) in kernfs_get().
....
[ÂÂÂ 3.633986] Call trace:
[ÂÂÂ 3.633991]Â kernfs_create_dir_ns+0xa8/0xb0
[ÂÂÂ 3.633994]Â sysfs_create_dir_ns+0x54/0xe8
[ÂÂÂ 3.634001]Â kobject_add_internal+0x22c/0x3f0
[ÂÂÂ 3.634005]Â kobject_add+0xe4/0x118
[ÂÂÂ 3.634011]Â device_add+0x200/0x870
[ÂÂÂ 3.634017]Â _request_firmware+0x958/0xc38
[ÂÂÂ 3.634020]Â request_firmware_into_buf+0x4c/0x70
....
[ÂÂÂ 3.634064] kernel BUG at .../mm/slub.c:294!
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ Here is BUG_ON(object == fp) in set_freepointer().
....
[ÂÂÂ 3.634346] Call trace:
[ÂÂÂ 3.634351]Â kmem_cache_free+0x504/0x6b8
[ÂÂÂ 3.634355]Â kernfs_put+0x14c/0x1d8
[ÂÂÂ 3.634359]Â kernfs_create_dir_ns+0x88/0xb0
[ÂÂÂ 3.634362]Â sysfs_create_dir_ns+0x54/0xe8
[ÂÂÂ 3.634366]Â kobject_add_internal+0x22c/0x3f0
[ÂÂÂ 3.634370]Â kobject_add+0xe4/0x118
[ÂÂÂ 3.634374]Â device_add+0x200/0x870
[ÂÂÂ 3.634378]Â _request_firmware+0x958/0xc38
[ÂÂÂ 3.634381]Â request_firmware_into_buf+0x4c/0x70
--------------------------------------------------------------------------

Fixes: 726e41097920 ("drivers: core: Remove glue dirs from sysfs earlier")

Signed-off-by: Muchun Song <smuchun@xxxxxxxxx>
---

Change in v5:
ÂÂÂÂÂÂÂ 1. Revert to the v1 fix.
ÂÂÂÂÂÂÂ 2. Add some comment to explain why we need do this in
ÂÂÂÂÂÂÂÂÂÂ cleanup_glue_dir().
Change in v4:
ÂÂÂÂÂÂÂ 1. Add some kerneldoc comment.
ÂÂÂÂÂÂÂ 2. Remove unlock_if_glue_dir().
ÂÂÂÂÂÂÂ 3. Rename get_device_parent_locked_if_glue_dir() to
ÂÂÂÂÂÂÂÂÂÂ get_device_parent_locked.
ÂÂÂÂÂÂÂ 4. Update commit message.
Change in v3:
ÂÂÂÂÂÂÂ Add change log.
Change in v2:
ÂÂÂÂÂÂÂ Fix device_move() also.

 drivers/base/core.c | 54 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 53 insertions(+), 1 deletion(-)

diff --git a/drivers/base/core.c b/drivers/base/core.c
index 4aeaa0c92bda..1a67ee325584 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -1825,7 +1825,59 @@ static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
ÂÂÂÂÂÂÂÂÂ return;
 Â mutex_lock(&gdp_mutex);
-ÂÂÂ if (!kobject_has_children(glue_dir))
+ÂÂÂ /**
+ÂÂÂÂ * There is a race condition between removing glue directory
+ÂÂÂÂ * and adding a new device under the glue directory.
+ÂÂÂÂ *
+ÂÂÂÂ * path 1: Add the child device under glue dir
+ÂÂÂÂ * device_add()
+ÂÂÂÂ *ÂÂÂ get_device_parent()
+ÂÂÂÂ *ÂÂÂÂÂÂÂ mutex_lock(&gdp_mutex);
+ÂÂÂÂ *ÂÂÂÂÂÂÂ ....
+ÂÂÂÂ *ÂÂÂÂÂÂÂ list_for_each_entry(k, &dev->class->p->glue_dirs.list,
+ÂÂÂÂ *ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ entry)
+ÂÂÂÂ *ÂÂÂÂÂÂÂ if (k->parent == parent_kobj) {
+ÂÂÂÂ *ÂÂÂÂÂÂÂÂÂÂÂ kobj = kobject_get(k);
+ÂÂÂÂ *ÂÂÂÂÂÂÂÂÂÂÂ break;
+ÂÂÂÂ *ÂÂÂÂÂÂÂ }
+ÂÂÂÂ *ÂÂÂÂÂÂÂ ....
+ÂÂÂÂ *ÂÂÂÂÂÂÂ mutex_unlock(&gdp_mutex);
+ÂÂÂÂ *ÂÂÂÂÂÂÂ ....
+ÂÂÂÂ *ÂÂÂ ....
+ÂÂÂÂ *ÂÂÂ kobject_add()
+ÂÂÂÂ *ÂÂÂÂÂÂÂ kobject_add_internal()
+ÂÂÂÂ *ÂÂÂÂÂÂÂ create_dir()
+ÂÂÂÂ *ÂÂÂÂÂÂÂÂÂÂÂ ....
+ÂÂÂÂ *ÂÂÂÂÂÂÂÂÂÂÂ if (kobj->parent)
+ÂÂÂÂ *ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ parent = kobj->parent->sd;
+ÂÂÂÂ *ÂÂÂÂÂÂÂÂÂÂÂ ....
+ÂÂÂÂ *ÂÂÂÂÂÂÂÂÂÂÂ kernfs_create_dir_ns(parent)
+ÂÂÂÂ *ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ ....
+ÂÂÂÂ *
+ÂÂÂÂ * path2: Remove last child device under glue dir
+ÂÂÂÂ * device_del()
+ÂÂÂÂ *ÂÂÂ cleanup_glue_dir()
+ÂÂÂÂ *ÂÂÂÂÂÂÂ ....
+ÂÂÂÂ *ÂÂÂÂÂÂÂ mutex_lock(&gdp_mutex);
+ÂÂÂÂ *ÂÂÂÂÂÂÂ if (!kobject_has_children(glue_dir))
+ÂÂÂÂ *ÂÂÂÂÂÂÂÂÂÂÂ kobject_del(glue_dir);
+ÂÂÂÂ *ÂÂÂÂÂÂÂ ....
+ÂÂÂÂ *ÂÂÂÂÂÂÂ mutex_unlock(&gdp_mutex);
+ÂÂÂÂ *
+ÂÂÂÂ * Before path2 remove last child device under glue dir, if path1 add
+ÂÂÂÂ * a new device under glue dir, the glue_dir kobject reference count
+ÂÂÂÂ * will be increase to 2 via kobject_get(k) in get_device_parent().
+ÂÂÂÂ * And path1 has been called kernfs_create_dir_ns(). Meanwhile,
+ÂÂÂÂ * path2 call kobject_del(glue_dir) beacause 0 is returned by
+ÂÂÂÂ * kobject_has_children(). This result in glue_dir->sd is freed.
+ÂÂÂÂ * Then the path1 will see a stale "empty" but still potentially used
+ÂÂÂÂ * glue dir around.
+ÂÂÂÂ *
+ÂÂÂÂ * In order to avoid this happening, we also should make sure that
+ÂÂÂÂ * kernfs_node for glue_dir is released in path2 only when refcount
+ÂÂÂÂ * for glue_dir kobj is 1.
+ÂÂÂÂ */
+ÂÂÂ if (!kobject_has_children(glue_dir) && kref_read(&glue_dir->kref) == 1)
Instead of hardcoding "1 " , can't we check against zero Like Prateek sood did in his patch.https://lkml.org/lkml/2019/5/1/3

+ref = kref_read(&glue_dir->kref);
+if (!kobject_has_children(glue_dir) && --ref)

I meant if (!kobject_has_children(glue_dir) && ! --ref)


We have seen many pattern of this issue which is coming from different driver e.g uinput as well in 4.14 kernel.(https://lkml.org/lkml/2019/4/10/146)
The reason why it started coming after

726e41097920 ("drivers: core: Remove glue dirs from sysfs earlier")

Looks good to me

Reviewed-by: Mukesh Ojha <mojha@xxxxxxxxxxxxxx>


-Mukesh


ÂÂÂÂÂÂÂÂÂ kobject_del(glue_dir);
ÂÂÂÂÂ kobject_put(glue_dir);
ÂÂÂÂÂ mutex_unlock(&gdp_mutex);