]> git.baikalelectronics.ru Git - kernel.git/commitdiff
i2c: Fix a potential use after free
authorXu Wang <vulab@iscas.ac.cn>
Fri, 27 Dec 2019 09:34:32 +0000 (09:34 +0000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 25 Aug 2022 09:17:38 +0000 (11:17 +0200)
[ Upstream commit a855e89f04e126abf7860672e2f44703ac69fabd ]

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: 1b4ed6d818e4 ("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>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/i2c/i2c-core-base.c

index 1b93fae58ec753870208f02659c061e45546c775..964e8a29b27b4cfcf879936aeafc44bc61d3ae0a 100644 (file)
@@ -2358,8 +2358,9 @@ void i2c_put_adapter(struct i2c_adapter *adap)
        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);