linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 2/2] i2c: Fix a potential use after free
@ 2019-12-27  9:34 Xu Wang
  2019-12-28 12:50 ` Markus Elfring
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Xu Wang @ 2019-12-27  9:34 UTC (permalink / raw)
  To: wsa; +Cc: linux-i2c, linux-kernel

Free the adap structure only after we are done using it.
This patch just moves the put_device() down a bit to avoid the
use after free.

Signed-off-by: Xu Wang <vulab@iscas.ac.cn>
---
 drivers/i2c/i2c-core-base.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
index 9f8dcd3..160d43e 100644
--- a/drivers/i2c/i2c-core-base.c
+++ b/drivers/i2c/i2c-core-base.c
@@ -2301,8 +2301,8 @@ void i2c_put_adapter(struct i2c_adapter *adap)
 	if (!adap)
 		return;
 
-	put_device(&adap->dev);
 	module_put(adap->owner);
+	put_device(&adap->dev);
 }
 EXPORT_SYMBOL(i2c_put_adapter);
 
-- 
2.7.4


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

* Re: [PATCH 2/2] i2c: Fix a potential use after free
  2019-12-27  9:34 [PATCH 2/2] i2c: Fix a potential use after free Xu Wang
@ 2019-12-28 12:50 ` Markus Elfring
  2020-03-22 15:56 ` Wolfram Sang
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Markus Elfring @ 2019-12-28 12:50 UTC (permalink / raw)
  To: Xu Wang, linux-i2c; +Cc: linux-kernel, Wolfram Sang

> Free the adap structure only after we are done using it.

This information can be reasonable.


> This patch just moves the put_device() down a bit to avoid the
> use after free.

I suggest to reconsider such a change because a device reference count
should eventually be decremented before decrementing the reference count
for the module which is managed by this programming interface.
Would you like to clarify the dependencies for such an use case any more?

Regards,
Markus

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

* Re: [PATCH 2/2] i2c: Fix a potential use after free
  2019-12-27  9:34 [PATCH 2/2] i2c: Fix a potential use after free Xu Wang
  2019-12-28 12:50 ` Markus Elfring
@ 2020-03-22 15:56 ` Wolfram Sang
  2022-06-16 21:00 ` Wolfram Sang
  2022-07-26 21:13 ` Wolfram Sang
  3 siblings, 0 replies; 5+ messages in thread
From: Wolfram Sang @ 2020-03-22 15:56 UTC (permalink / raw)
  To: Xu Wang; +Cc: linux-i2c, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 978 bytes --]

On Fri, Dec 27, 2019 at 09:34:32AM +0000, Xu Wang wrote:
> Free the adap structure only after we are done using it.
> This patch just moves the put_device() down a bit to avoid the
> use after free.
> 
> Signed-off-by: Xu Wang <vulab@iscas.ac.cn>

Do you have a testcase to reproduce it?

I wonder because we are freeing the device structure which is embedded
inside the adap structure, not the adap structure itself. Or?

> ---
>  drivers/i2c/i2c-core-base.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
> index 9f8dcd3..160d43e 100644
> --- a/drivers/i2c/i2c-core-base.c
> +++ b/drivers/i2c/i2c-core-base.c
> @@ -2301,8 +2301,8 @@ void i2c_put_adapter(struct i2c_adapter *adap)
>  	if (!adap)
>  		return;
>  
> -	put_device(&adap->dev);
>  	module_put(adap->owner);
> +	put_device(&adap->dev);
>  }
>  EXPORT_SYMBOL(i2c_put_adapter);
>  
> -- 
> 2.7.4
> 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH 2/2] i2c: Fix a potential use after free
  2019-12-27  9:34 [PATCH 2/2] i2c: Fix a potential use after free Xu Wang
  2019-12-28 12:50 ` Markus Elfring
  2020-03-22 15:56 ` Wolfram Sang
@ 2022-06-16 21:00 ` Wolfram Sang
  2022-07-26 21:13 ` Wolfram Sang
  3 siblings, 0 replies; 5+ messages in thread
From: Wolfram Sang @ 2022-06-16 21:00 UTC (permalink / raw)
  To: Xu Wang; +Cc: linux-i2c, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 487 bytes --]

On Fri, Dec 27, 2019 at 09:34:32AM +0000, Xu Wang wrote:
> Free the adap structure only after we are done using it.
> This patch just moves the put_device() down a bit to avoid the
> use after free.
> 
> Signed-off-by: Xu Wang <vulab@iscas.ac.cn>

Added a comment why we reverse the order for putting our stuff and
applied to for-next, thanks! This way, we get more testing until it hits
upstream. Still, stable tag added because we want it to be backported if
all is well.


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH 2/2] i2c: Fix a potential use after free
  2019-12-27  9:34 [PATCH 2/2] i2c: Fix a potential use after free Xu Wang
                   ` (2 preceding siblings ...)
  2022-06-16 21:00 ` Wolfram Sang
@ 2022-07-26 21:13 ` Wolfram Sang
  3 siblings, 0 replies; 5+ messages in thread
From: Wolfram Sang @ 2022-07-26 21:13 UTC (permalink / raw)
  To: Xu Wang; +Cc: linux-i2c, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 287 bytes --]

On Fri, Dec 27, 2019 at 09:34:32AM +0000, Xu Wang wrote:
> Free the adap structure only after we are done using it.
> This patch just moves the put_device() down a bit to avoid the
> use after free.
> 
> Signed-off-by: Xu Wang <vulab@iscas.ac.cn>

Applied to for-next, thanks!


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

end of thread, other threads:[~2022-07-26 21:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-27  9:34 [PATCH 2/2] i2c: Fix a potential use after free Xu Wang
2019-12-28 12:50 ` Markus Elfring
2020-03-22 15:56 ` Wolfram Sang
2022-06-16 21:00 ` Wolfram Sang
2022-07-26 21:13 ` Wolfram Sang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).