All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drivers core: don't do anything in device_del() when device_add() fail
@ 2021-04-01 13:01 Yufen Yu
  2021-04-01 13:31 ` Rafael J. Wysocki
  2021-04-02 15:06 ` Greg KH
  0 siblings, 2 replies; 3+ messages in thread
From: Yufen Yu @ 2021-04-01 13:01 UTC (permalink / raw)
  To: gregkh, linux-kernel; +Cc: rafael, axboe, hch, linux-block

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.

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);
-- 
2.25.4


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] drivers core: don't do anything in device_del() when device_add() fail
  2021-04-01 13:01 [PATCH] drivers core: don't do anything in device_del() when device_add() fail Yufen Yu
@ 2021-04-01 13:31 ` Rafael J. Wysocki
  2021-04-02 15:06 ` Greg KH
  1 sibling, 0 replies; 3+ messages in thread
From: Rafael J. Wysocki @ 2021-04-01 13:31 UTC (permalink / raw)
  To: Yufen Yu
  Cc: Greg Kroah-Hartman, Linux Kernel Mailing List, Rafael J. Wysocki,
	Jens Axboe, Christoph Hellwig, linux-block

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);
> --

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] drivers core: don't do anything in device_del() when device_add() fail
  2021-04-01 13:01 [PATCH] drivers core: don't do anything in device_del() when device_add() fail Yufen Yu
  2021-04-01 13:31 ` Rafael J. Wysocki
@ 2021-04-02 15:06 ` Greg KH
  1 sibling, 0 replies; 3+ messages in thread
From: Greg KH @ 2021-04-02 15:06 UTC (permalink / raw)
  To: Yufen Yu; +Cc: linux-kernel, rafael, axboe, hch, linux-block

On Thu, Apr 01, 2021 at 09:01:38AM -0400, Yufen Yu wrote:
> Recently, our syzbot test reported NULL pointer dereference in
> device_del() by injecting memory allocation fail in device_add().

Don't do that :)

> For now, callers of device_add(), such as add_disk(), may ignore
> device_add()'s fail and go on working.

Please fix up those users, if device_add() fails there is no need to
call device_del().

> 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.

As Rafael said, that's not what you are doing here, so even if I wanted
to take this patch, I can't.

> 
> 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;

Also, this isn't a good test as it's not obvious what you are trying to
check for here.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-04-02 15:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-01 13:01 [PATCH] drivers core: don't do anything in device_del() when device_add() fail Yufen Yu
2021-04-01 13:31 ` Rafael J. Wysocki
2021-04-02 15:06 ` Greg KH

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.