lkml.org 
[lkml]   [2021]   [Apr]   [1]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
Date
SubjectRe: [PATCH] drivers core: don't do anything in device_del() when device_add() fail
On Thu, Apr 1, 2021 at 2:56 PM Yufen Yu <yuyufen@huawei.com> wrote:
>
> Recently, our syzbot test reported NULL pointer dereference in
> device_del() by injecting memory allocation fail in device_add().
>
> For now, callers of device_add(), such as add_disk(), may ignore
> device_add()'s fail and go on working. In unregister path, it will
> call device_del() from del_gendisk(). That can cause various NULL
> pointer dereference, including dev->p is NULL in kill_device(),
> 'name' is NULL in sysfs_remove_link(), kobj->sd is 'NULL' when call
> dpm_sysfs_remove(), and so on.
>
> To avoid these kernel panic, we call device_del() only when device_add()
> is success.

The patch looks reasonable to me, but the above is not what it does.

It causes device_del() to be a no-op for devices that have not been
successfully added.

> Signed-off-by: Yufen Yu <yuyufen@huawei.com>
> ---
> drivers/base/core.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/drivers/base/core.c b/drivers/base/core.c
> index f29839382f81..a10ec5dbc577 100644
> --- a/drivers/base/core.c
> +++ b/drivers/base/core.c
> @@ -3380,6 +3380,9 @@ void device_del(struct device *dev)
> struct class_interface *class_intf;
> unsigned int noio_flag;
>
> + if (!dev->p)
> + return;
> +
> device_lock(dev);
> kill_device(dev);
> device_unlock(dev);
> --

\
 
 \ /
  Last update: 2021-04-01 20:40    [W:0.046 / U:0.148 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site