All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] driver core: access dev->driver under dev->mutex in dev_uevent
@ 2015-01-14 18:10 ashishsangwan2
  2015-01-14 20:46 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 3+ messages in thread
From: ashishsangwan2 @ 2015-01-14 18:10 UTC (permalink / raw)
  To: Greg Kroah-Hartman; +Cc: linux-kernel, Ashish Sangwan, Namjae Jeon

From: Ashish Sangwan <a.sangwan@samsung.com>

We have hit a race condition while parallely accessing device's uevent
and rmmoding device's driver. In dev_uevent() first dev->driver is checked if
present which becomes true but before calling add_uevent_var 
dev->driver is set to NULL and driver is freed in __device_release_driver().
This results in either NULL pointer derefrence or invalid virtual address
access. Fix this race by accessing dev->driver under dev->mutex.

Signed-off-by: Ashish Sangwan <a.sangwan@samsung.com>
Signed-off-by: Namjae Jeon <namjae.jeon@samsung.com>
---
 drivers/base/core.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/base/core.c b/drivers/base/core.c
index 97e2baf..f8e854a 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -326,8 +326,15 @@ static int dev_uevent(struct kset *kset, struct kobject *kobj,
 	if (dev->type && dev->type->name)
 		add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
 
+	/*
+	 * dev->driver is set NULL under dev->mutex so it should be checked
+	 * after acquiring dev->mutex. This prevents a race between 
+	 * __device_release_driver and dev_uevent.
+	 */
+	device_lock(dev);
 	if (dev->driver)
 		add_uevent_var(env, "DRIVER=%s", dev->driver->name);
+	device_unlock(dev);
 
 	/* Add common DT information about the device */
 	of_device_uevent(dev, env);
-- 
1.7.11.4


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

* Re: [PATCH] driver core: access dev->driver under dev->mutex in dev_uevent
  2015-01-14 18:10 [PATCH] driver core: access dev->driver under dev->mutex in dev_uevent ashishsangwan2
@ 2015-01-14 20:46 ` Greg Kroah-Hartman
  2015-01-19 18:20   ` Ashish Sangwan
  0 siblings, 1 reply; 3+ messages in thread
From: Greg Kroah-Hartman @ 2015-01-14 20:46 UTC (permalink / raw)
  To: ashishsangwan2; +Cc: linux-kernel, Ashish Sangwan, Namjae Jeon

On Wed, Jan 14, 2015 at 11:40:54PM +0530, ashishsangwan2@gmail.com wrote:
> From: Ashish Sangwan <a.sangwan@samsung.com>
> 
> We have hit a race condition while parallely accessing device's uevent
> and rmmoding device's driver.

How are you doing that?  By reading the uevent file and removing the
device?

Removing a kernel module is not a "normal" thing at all, so this type of
"error" is very low on anyone's list.

> In dev_uevent() first dev->driver is checked if
> present which becomes true but before calling add_uevent_var 
> dev->driver is set to NULL and driver is freed in __device_release_driver().
> This results in either NULL pointer derefrence or invalid virtual address
> access. Fix this race by accessing dev->driver under dev->mutex.
> 
> Signed-off-by: Ashish Sangwan <a.sangwan@samsung.com>
> Signed-off-by: Namjae Jeon <namjae.jeon@samsung.com>
> ---
>  drivers/base/core.c | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/drivers/base/core.c b/drivers/base/core.c
> index 97e2baf..f8e854a 100644
> --- a/drivers/base/core.c
> +++ b/drivers/base/core.c
> @@ -326,8 +326,15 @@ static int dev_uevent(struct kset *kset, struct kobject *kobj,
>  	if (dev->type && dev->type->name)
>  		add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
>  
> +	/*
> +	 * dev->driver is set NULL under dev->mutex so it should be checked
> +	 * after acquiring dev->mutex. This prevents a race between 
> +	 * __device_release_driver and dev_uevent.
> +	 */
> +	device_lock(dev);
>  	if (dev->driver)
>  		add_uevent_var(env, "DRIVER=%s", dev->driver->name);
> +	device_unlock(dev);

Always run your patches through checkpatch before sending them out so
you don't get grumpy email responses about how you need to run patches
through checkpatch before sending them out...

thanks,

greg k-h

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

* Re: [PATCH] driver core: access dev->driver under dev->mutex in dev_uevent
  2015-01-14 20:46 ` Greg Kroah-Hartman
@ 2015-01-19 18:20   ` Ashish Sangwan
  0 siblings, 0 replies; 3+ messages in thread
From: Ashish Sangwan @ 2015-01-19 18:20 UTC (permalink / raw)
  To: Greg Kroah-Hartman; +Cc: LKML, Ashish Sangwan, Namjae Jeon

On Thu, Jan 15, 2015 at 2:16 AM, Greg Kroah-Hartman
<gregkh@linuxfoundation.org> wrote:
> On Wed, Jan 14, 2015 at 11:40:54PM +0530, ashishsangwan2@gmail.com wrote:
>> From: Ashish Sangwan <a.sangwan@samsung.com>
>>
>> We have hit a race condition while parallely accessing device's uevent
>> and rmmoding device's driver.
>
> How are you doing that?  By reading the uevent file and removing the
> device?
uevent file /sys/<device>/uevent is read by udevd while at the same
time driver is removed by rmmod.
>
> Removing a kernel module is not a "normal" thing at all, so this type of
> "error" is very low on anyone's list.
Ours is an embedded device with not much memory and driver occupies a
considerable amount of it. So when the device is not in use, its driver is
removed. Device is a platform device and attached at the boot time.
I understand if the patch is low priority for mainline kernel,
so just want to get views regarding any side effects that can
be caused by this patch as we plan to use this patch in our product.
We have hit this race quite a few times.
>
>> In dev_uevent() first dev->driver is checked if
>> present which becomes true but before calling add_uevent_var
>> dev->driver is set to NULL and driver is freed in __device_release_driver().
>> This results in either NULL pointer derefrence or invalid virtual address
>> access. Fix this race by accessing dev->driver under dev->mutex.
>>
>> Signed-off-by: Ashish Sangwan <a.sangwan@samsung.com>
>> Signed-off-by: Namjae Jeon <namjae.jeon@samsung.com>
>> ---
>>  drivers/base/core.c | 7 +++++++
>>  1 file changed, 7 insertions(+)
>>
>> diff --git a/drivers/base/core.c b/drivers/base/core.c
>> index 97e2baf..f8e854a 100644
>> --- a/drivers/base/core.c
>> +++ b/drivers/base/core.c
>> @@ -326,8 +326,15 @@ static int dev_uevent(struct kset *kset, struct kobject *kobj,
>>       if (dev->type && dev->type->name)
>>               add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
>>
>> +     /*
>> +      * dev->driver is set NULL under dev->mutex so it should be checked
>> +      * after acquiring dev->mutex. This prevents a race between
>> +      * __device_release_driver and dev_uevent.
>> +      */
>> +     device_lock(dev);
>>       if (dev->driver)
>>               add_uevent_var(env, "DRIVER=%s", dev->driver->name);
>> +     device_unlock(dev);
>
> Always run your patches through checkpatch before sending them out so
> you don't get grumpy email responses about how you need to run patches
> through checkpatch before sending them out...
Sure, will do that from next time.

Thanks,
Ashish
>
> thanks,
>
> greg k-h

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

end of thread, other threads:[~2015-01-19 18:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-14 18:10 [PATCH] driver core: access dev->driver under dev->mutex in dev_uevent ashishsangwan2
2015-01-14 20:46 ` Greg Kroah-Hartman
2015-01-19 18:20   ` Ashish Sangwan

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.