Blob Blame History Raw
From: Xu Wang <vulab@iscas.ac.cn>
Date: Fri, 27 Dec 2019 09:34:32 +0000
Subject: i2c: Fix a potential use after free
Git-commit: e4c72c06c367758a14f227c847f9d623f1994ecf
Patch-mainline: v6.0
References: bsc#1220409 CVE-2019-25162

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.

Fixes: 611e12ea0f12 ("i2c: core: manage i2c bus device refcount in i2c_[get|put]_adapter")
Signed-off-by: Xu Wang <vulab@iscas.ac.cn>
[wsa: added comment to the code, added Fixes tag]
Signed-off-by: Wolfram Sang <wsa@kernel.org>
Acked-by: Jean Delvare <jdelvare@suse.de>
---
 drivers/i2c/i2c-core.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -3157,8 +3157,9 @@ void i2c_put_adapter(struct i2c_adapter
 	if (!adap)
 		return;
 
-	put_device(&adap->dev);
 	module_put(adap->owner);
+	/* Should be last, otherwise we risk use-after-free with 'adap' */
+	put_device(&adap->dev);
 }
 EXPORT_SYMBOL(i2c_put_adapter);