clk: starfive: Avoid casting iomem pointers
Subsystems (5)
Stephen Boyd
maintainerPATCH 1/1 clk: starfive: Avoid casting iomem pointers
Let's use a wrapper struct for the auxiliary_device made in jh7110_reset_controller_register() so that we can stop casting iomem pointers. The casts trip up tools like sparse, and make for some awkward casts that are largely unnecessary. While we're here, change the allocation from devm and actually free the auxiliary_device memory in the release function. This avoids any use after free problems where the parent device driver is unbound from the device but the auxiliuary_device is still in use accessing devm freed memory.
---
I can take this via clk tree.
drivers/clk/starfive/clk-starfive-jh7110-sys.c | 15 ++++++++++++--- drivers/reset/starfive/reset-starfive-jh7110.c | 9 ++++++--- include/soc/starfive/reset-starfive-jh71x0.h | 17 +++++++++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 include/soc/starfive/reset-starfive-jh71x0.h
drivers/clk/starfive/clk-starfive-jh7110-sys.c +12 -3
@@ -11,6 +11,9 @@#include <linux/init.h> #include <linux/io.h> #include <linux/platform_device.h> +#include <linux/slab.h> + +#include <soc/starfive/reset-starfive-jh71x0.h> #include <dt-bindings/clock/starfive,jh7110-crg.h>
@@ -335,26 +338,32 @@ static void jh7110_reset_unregister_adev(void *_adev)struct auxiliary_device *adev = _adev;auxiliary_device_delete (adev); +auxiliary_device_uninit (adev); }static void jh7110_reset_adev_release (struct device *dev) {struct auxiliary_device *adev =to_auxiliary_dev (dev); +struct jh71x0_reset_adev *rdev =to_jh71x0_reset_adev (adev); -auxiliary_device_uninit (adev); +kfree (rdev); }int jh7110_reset_controller_register (struct jh71x0_clk_priv *priv,const char *adev_name,u32 adev_id) { +struct jh71x0_reset_adev *rdev;struct auxiliary_device *adev;int ret; - adev =devm_kzalloc (priv->dev,sizeof (*adev),GFP_KERNEL ); -if (!adev) + rdev =kzalloc (sizeof (*rdev),GFP_KERNEL ); +if (!rdev)return -ENOMEM ; + rdev->base = priv->base; + + adev = &rdev->adev; adev->name = adev_name; adev->dev.parent = priv->dev; adev->dev.release = jh7110_reset_adev_release;
drivers/reset/starfive/reset-starfive-jh7110.c +6 -3
@@ -7,6 +7,8 @@#include <linux/auxiliary_bus.h> +#include <soc/starfive/reset-starfive-jh71x0.h> +#include "reset-starfive-jh71x0.h" #include <dt-bindings/reset/starfive,jh7110-crg.h>
@@ -33,14 +35,15 @@ static int jh7110_reset_probe(struct auxiliary_device *adev, const struct auxiliary_device_id *id) {struct jh7110_reset_info *info = (struct jh7110_reset_info *)(id->driver_data); -void __iomem **base = (void __iomem **)dev_get_drvdata (adev->dev.parent); +struct jh71x0_reset_adev *rdev =to_jh71x0_reset_adev (adev); +void __iomem *base = rdev->base;if (!info || !base)return -ENODEV ;return reset_starfive_jh71x0_register (&adev->dev, adev->dev.parent->of_node, - *base + info->assert_offset, - *base + info->status_offset, + base + info->assert_offset, + base + info->status_offset,NULL , info->nr_resets,NULL );
include/soc/starfive/reset-starfive-jh71x0.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __SOC_STARFIVE_RESET_JH71X0_H +#define __SOC_STARFIVE_RESET_JH71X0_H + +#include <linux/auxiliary_bus.h> +#include <linux/compiler_types.h> +#include <linux/container_of.h> + +struct jh71x0_reset_adev { +void __iomem *base; +struct auxiliary_device adev; +}; + +#define to_jh71x0_reset_adev (_adev) \ + container_of((_adev), struct jh71x0_reset_adev, adev) + +#endif
base-commit: 601e5d464d535d655917c2cfb29c394d367fb676
Signature · 2 lines
https://git.kernel.org/pub/scm/linux/kernel/git/clk/linux.git/ https://git.kernel.org/pub/scm/linux/kernel/git/sboyd/spmi.git
Quoted 4 lines · Stephen Boyd
Let's use a wrapper struct for the
auxiliary_devicemade injh7110_reset_controller_register()so that we can stop casting iomem pointers. The casts trip up tools like sparse, and make for some awkward casts that are largely unnecessary.
Cool, thanks for doing it!
Quoted 38 lines
While we're here, change the
allocation from devm and actually free theauxiliary_devicememory in the release function. This avoids any use after free problems where the parent device driver is unbound from the device but theauxiliuary_deviceis still in use accessing devm freed memory.Cc Tommaso Merciai <[email protected]>Cc Emil Renner Berthing <[email protected]>Cc Hal Feng <[email protected]>Cc Conor Dooley <[email protected]>Cc Xingyu Wu <[email protected]>Fixes edab7204afe5 ("clk: starfive: Add StarFive JH7110 system clock driver")Signed-off-by Stephen Boyd <[email protected]>---
I can take this via clk tree.
drivers/clk/starfive/clk-starfive-jh7110-sys.c | 15 ++++++++++++--- drivers/reset/starfive/reset-starfive-jh7110.c | 9 ++++++--- include/soc/starfive/reset-starfive-jh71x0.h | 17 +++++++++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 include/soc/starfive/reset-starfive-jh71x0.hdrivers/clk/starfive/clk-starfive-jh7110-sys.c +3
-0@@ -11,6 +11,9 @@#include <linux/init.h> #include <linux/io.h> #include <linux/platform_device.h> +#include <linux/slab.h> + +#include <soc/starfive/reset-starfive-jh71x0.h> #include <dt-bindings/clock/starfive,jh7110-crg.h>
drivers/clk/starfive/clk-starfive-jh7110-sys.c static void jh7110_reset_unregister_adev(void *_adev) · line 341
Quoted 4 lines
drivers/clk/starfive/clk-starfive-jh7110-sys.c +1
-0@@ -335,26 +338,32 @@ static void jh7110_reset_unregister_adev(void *_adev)struct auxiliary_device *adev = _adev;auxiliary_device_delete (adev); +auxiliary_device_uninit (adev);
Huh, I think you didn't explicitly mention this one, but it's actually part of the UAF fix AFAICT?
When I did the aux device stuff for the clk-mpfs driver, I copied from peci as there were almost no examples of aux dev stuff in-tree. It looks like subsequently to me starting development, this fix landed:1c11289b34ab ("peci: cpu: Fix use-after-free in adev_release()")
It similarly moves the uninit() to the release callback...
I think I need the below (whitespace damaged):
drivers/clk/microchip/clk-mpfs.c +1 -2
@@ -374,14 +374,13 @@ static void mpfs_reset_unregister_adev(void *_adev)struct auxiliary_device *adev = _adev;auxiliary_device_delete (adev); +auxiliary_device_uninit (adev); }static void mpfs_reset_adev_release (struct device *dev) {struct auxiliary_device *adev =to_auxiliary_dev (dev); -auxiliary_device_uninit (adev); -kfree (adev); }
Anyways, for this patch:
Thanks,
Conor.
Quoted 92 lines
drivers/clk/starfive/clk-starfive-jh7110-sys.c +8
-3}static void jh7110_reset_adev_release (struct device *dev) {struct auxiliary_device *adev =to_auxiliary_dev (dev); +struct jh71x0_reset_adev *rdev =to_jh71x0_reset_adev (adev); -auxiliary_device_uninit (adev); +kfree (rdev); }int jh7110_reset_controller_register (struct jh71x0_clk_priv *priv,const char *adev_name,u32 adev_id) { +struct jh71x0_reset_adev *rdev;struct auxiliary_device *adev;int ret; - adev =devm_kzalloc (priv->dev,sizeof (*adev),GFP_KERNEL ); -if (!adev) + rdev =kzalloc (sizeof (*rdev),GFP_KERNEL ); +if (!rdev)return -ENOMEM ; + rdev->base = priv->base; + + adev = &rdev->adev; adev->name = adev_name; adev->dev.parent = priv->dev; adev->dev.release = jh7110_reset_adev_release;drivers/reset/starfive/reset-starfive-jh7110.c +6
-3@@ -7,6 +7,8 @@#include <linux/auxiliary_bus.h> +#include <soc/starfive/reset-starfive-jh71x0.h> +#include "reset-starfive-jh71x0.h" #include <dt-bindings/reset/starfive,jh7110-crg.h> @@ -33,14 +35,15 @@ static int jh7110_reset_probe(struct auxiliary_device *adev, const struct auxiliary_device_id *id) {struct jh7110_reset_info *info = (struct jh7110_reset_info *)(id->driver_data); -void __iomem **base = (void __iomem **)dev_get_drvdata (adev->dev.parent); +struct jh71x0_reset_adev *rdev =to_jh71x0_reset_adev (adev); +void __iomem *base = rdev->base;if (!info || !base)return -ENODEV ;return reset_starfive_jh71x0_register (&adev->dev, adev->dev.parent->of_node, - *base + info->assert_offset, - *base + info->status_offset, + base + info->assert_offset, + base + info->status_offset,NULL , info->nr_resets,NULL );include/soc/starfive/reset-starfive-jh71x0.h new file +17
-0@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __SOC_STARFIVE_RESET_JH71X0_H +#define __SOC_STARFIVE_RESET_JH71X0_H + +#include <linux/auxiliary_bus.h> +#include <linux/compiler_types.h> +#include <linux/container_of.h> + +struct jh71x0_reset_adev { +void __iomem *base; +struct auxiliary_device adev; +}; + +#define to_jh71x0_reset_adev (_adev) \ + container_of((_adev), struct jh71x0_reset_adev, adev) + +#endif base-commit:
601e5d464d535d655917c2cfb29c394d367fb676Signature · 2 lines
https://git.kernel.org/pub/scm/linux/kernel/git/clk/linux.git/ https://git.kernel.org/pub/scm/linux/kernel/git/sboyd/spmi.git
Stephen Boyd
maintainerQuoting Conor Dooley (2023-04-13 14:26:56)
Quoted 9 lines
drivers/clk/microchip/clk-mpfs.c static void jh7110_reset_unregister_adev(void *_adev) · line 341
Quoted 5 lines
drivers/clk/microchip/clk-mpfs.c +1
-0@@ -335,26 +338,32 @@ static void jh7110_reset_unregister_adev(void *_adev)struct auxiliary_device *adev = _adev;auxiliary_device_delete (adev); +auxiliary_device_uninit (adev);Huh, I think you didn't explicitly mention this one, but it's actually part of the UAF fix AFAICT?
Quoted 8 lines
When I did the aux device stuff for the clk-mpfs driver, I copied from peci as there were almost no examples of aux dev stuff in-tree. It looks like subsequently to me starting development, this fix landed:
1c11289b34ab("peci: cpu: Fix use-after-free inadev_release()")It similarly moves the
uninit()to the release callback...I think I need the below (whitespace damaged):
Yeah that looks better. Care to send a proper patch for it?
Quoted 21 lines
drivers/clk/microchip/clk-mpfs.c +1
-2@@ -374,14 +374,13 @@ static void mpfs_reset_unregister_adev(void *_adev)struct auxiliary_device *adev = _adev;auxiliary_device_delete (adev); +auxiliary_device_uninit (adev); }static void mpfs_reset_adev_release (struct device *dev) {struct auxiliary_device *adev =to_auxiliary_dev (dev); -auxiliary_device_uninit (adev); -kfree (adev); }Anyways, for this patch:
Quoted 2 lines
Reviewed-by Conor Dooley <[email protected]>
Thanks.
Quoted 1 line · Stephen Boyd
Yeah that looks better. Care to send a proper patch for it?
Yup, no problem: 20230413-critter-synopsis-dac070a86cb4@spud
Cheers,
Conor.
Quoting Stephen Boyd (2023-04-13 13:55:28)
Quoted 9 lines
Let's use a wrapper struct for the
auxiliary_devicemade injh7110_reset_controller_register()so that we can stop casting iomem pointers. The casts trip up tools like sparse, and make for some awkward casts that are largely unnecessary. While we're here, change the allocation from devm and actually free theauxiliary_devicememory in the release function. This avoids any use after free problems where the parent device driver is unbound from the device but theauxiliuary_deviceis still in use accessing devm freed memory.
Quoted 8 lines
Cc Tommaso Merciai <[email protected]>Cc Emil Renner Berthing <[email protected]>Cc Hal Feng <[email protected]>Cc Conor Dooley <[email protected]>Cc Xingyu Wu <[email protected]>Fixes edab7204afe5 ("clk: starfive: Add StarFive JH7110 system clock driver")Signed-off-by Stephen Boyd <[email protected]>---
Applied to clk-next
Quoted 90 lines · Stephen Boyd
Let's use a wrapper struct for the
auxiliary_devicemade injh7110_reset_controller_register()so that we can stop casting iomem pointers. The casts trip up tools like sparse, and make for some awkward casts that are largely unnecessary. While we're here, change the allocation from devm and actually free theauxiliary_devicememory in the release function. This avoids any use after free problems where the parent device driver is unbound from the device but theauxiliuary_deviceis still in use accessing devm freed memory.Cc Tommaso Merciai <[email protected]>Cc Emil Renner Berthing <[email protected]>Cc Hal Feng <[email protected]>Cc Conor Dooley <[email protected]>Cc Xingyu Wu <[email protected]>Fixes edab7204afe5 ("clk: starfive: Add StarFive JH7110 system clock driver")Signed-off-by Stephen Boyd <[email protected]>---
I can take this via clk tree.
drivers/clk/starfive/clk-starfive-jh7110-sys.c | 15 ++++++++++++--- drivers/reset/starfive/reset-starfive-jh7110.c | 9 ++++++--- include/soc/starfive/reset-starfive-jh71x0.h | 17 +++++++++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 include/soc/starfive/reset-starfive-jh71x0.hdrivers/clk/starfive/clk-starfive-jh7110-sys.c +12
-3@@ -11,6 +11,9 @@#include <linux/init.h> #include <linux/io.h> #include <linux/platform_device.h> +#include <linux/slab.h> + +#include <soc/starfive/reset-starfive-jh71x0.h> #include <dt-bindings/clock/starfive,jh7110-crg.h> @@ -335,26 +338,32 @@ static void jh7110_reset_unregister_adev(void *_adev)struct auxiliary_device *adev = _adev;auxiliary_device_delete (adev); +auxiliary_device_uninit (adev); }static void jh7110_reset_adev_release (struct device *dev) {struct auxiliary_device *adev =to_auxiliary_dev (dev); +struct jh71x0_reset_adev *rdev =to_jh71x0_reset_adev (adev); -auxiliary_device_uninit (adev); +kfree (rdev); }int jh7110_reset_controller_register (struct jh71x0_clk_priv *priv,const char *adev_name,u32 adev_id) { +struct jh71x0_reset_adev *rdev;struct auxiliary_device *adev;int ret; - adev =devm_kzalloc (priv->dev,sizeof (*adev),GFP_KERNEL ); -if (!adev) + rdev =kzalloc (sizeof (*rdev),GFP_KERNEL ); +if (!rdev)return -ENOMEM ; + rdev->base = priv->base; + + adev = &rdev->adev; adev->name = adev_name; adev->dev.parent = priv->dev; adev->dev.release = jh7110_reset_adev_release;drivers/reset/starfive/reset-starfive-jh7110.c +2
-0@@ -7,6 +7,8 @@#include <linux/auxiliary_bus.h> +#include <soc/starfive/reset-starfive-jh71x0.h> +#include "reset-starfive-jh71x0.h" #include <dt-bindings/reset/starfive,jh7110-crg.h>
drivers/reset/starfive/reset-starfive-jh7110.c static int jh7110_reset_probe(struct auxiliary_device *adev, · line 36
Quoted 4 lines
drivers/reset/starfive/reset-starfive-jh7110.c +0
-1@@ -33,14 +35,15 @@ static int jh7110_reset_probe(struct auxiliary_device *adev, const struct auxiliary_device_id *id) {struct jh7110_reset_info *info = (struct jh7110_reset_info *)(id->driver_data); -void __iomem **base = (void __iomem **)dev_get_drvdata (adev->dev.parent);
Thank you for doing that. BTW, if drop the dev_get_drvdata(), the dev_set_drvdata() should also be dropped.
drivers/clk/starfive/clk-starfive-jh7110-aon.c +0 -2
@@ -83,8 +83,6 @@ static int jh7110_aoncrg_probe(struct platform_device *pdev)if (IS_ERR (priv->base))return PTR_ERR (priv->base); -dev_set_drvdata (priv->dev, (void *)(&priv->base)); -for (idx =0 ; idx <JH7110_AONCLK_END ; idx++) {u32 max = jh7110_aonclk_data[idx].max;struct clk_parent_data parents[4 ] = {};
drivers/clk/starfive/clk-starfive-jh7110-sys.c +0 -2
@@ -393,8 +393,6 @@ static int __init jh7110_syscrg_probe(struct platform_device *pdev)if (IS_ERR (priv->base))return PTR_ERR (priv->base); -dev_set_drvdata (priv->dev, (void *)(&priv->base)); -/* * These PLL clocks are not actually fixed factor clocks and can be * controlled by the syscon registers of JH7110. They will be dropped
Quoted 38 lines
drivers/reset/starfive/reset-starfive-jh7110.c +4
-2+struct jh71x0_reset_adev *rdev =to_jh71x0_reset_adev (adev); +void __iomem *base = rdev->base;if (!info || !base)return -ENODEV ;return reset_starfive_jh71x0_register (&adev->dev, adev->dev.parent->of_node, - *base + info->assert_offset, - *base + info->status_offset, + base + info->assert_offset, + base + info->status_offset,NULL , info->nr_resets,NULL );include/soc/starfive/reset-starfive-jh71x0.h new file +17
-0@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __SOC_STARFIVE_RESET_JH71X0_H +#define __SOC_STARFIVE_RESET_JH71X0_H + +#include <linux/auxiliary_bus.h> +#include <linux/compiler_types.h> +#include <linux/container_of.h> + +struct jh71x0_reset_adev { +void __iomem *base; +struct auxiliary_device adev; +}; + +#define to_jh71x0_reset_adev (_adev) \ + container_of((_adev), struct jh71x0_reset_adev, adev) + +#endif
Quoted 1 line
base-commit:
601e5d464d535d655917c2cfb29c394d367fb676
Best regards,
Xingyu Wu
Hal Feng
maintainerQuoted 117 lines · Xingyu Wu
Quoted 90 lines · Stephen Boyd
Let's use a wrapper struct for the
auxiliary_devicemade injh7110_reset_controller_register()so that we can stop casting iomem pointers. The casts trip up tools like sparse, and make for some awkward casts that are largely unnecessary. While we're here, change the allocation from devm and actually free theauxiliary_devicememory in the release function. This avoids any use after free problems where the parent device driver is unbound from the device but theauxiliuary_deviceis still in use accessing devm freed memory.Cc Tommaso Merciai <[email protected]>Cc Emil Renner Berthing <[email protected]>Cc Hal Feng <[email protected]>Cc Conor Dooley <[email protected]>Cc Xingyu Wu <[email protected]>Fixes edab7204afe5 ("clk: starfive: Add StarFive JH7110 system clock driver")Signed-off-by Stephen Boyd <[email protected]>---
I can take this via clk tree.
drivers/clk/starfive/clk-starfive-jh7110-sys.c | 15 ++++++++++++--- drivers/reset/starfive/reset-starfive-jh7110.c | 9 ++++++--- include/soc/starfive/reset-starfive-jh71x0.h | 17 +++++++++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 include/soc/starfive/reset-starfive-jh71x0.hdrivers/clk/starfive/clk-starfive-jh7110-sys.c +12
-3@@ -11,6 +11,9 @@#include <linux/init.h> #include <linux/io.h> #include <linux/platform_device.h> +#include <linux/slab.h> + +#include <soc/starfive/reset-starfive-jh71x0.h> #include <dt-bindings/clock/starfive,jh7110-crg.h> @@ -335,26 +338,32 @@ static void jh7110_reset_unregister_adev(void *_adev)struct auxiliary_device *adev = _adev;auxiliary_device_delete (adev); +auxiliary_device_uninit (adev); }static void jh7110_reset_adev_release (struct device *dev) {struct auxiliary_device *adev =to_auxiliary_dev (dev); +struct jh71x0_reset_adev *rdev =to_jh71x0_reset_adev (adev); -auxiliary_device_uninit (adev); +kfree (rdev); }int jh7110_reset_controller_register (struct jh71x0_clk_priv *priv,const char *adev_name,u32 adev_id) { +struct jh71x0_reset_adev *rdev;struct auxiliary_device *adev;int ret; - adev =devm_kzalloc (priv->dev,sizeof (*adev),GFP_KERNEL ); -if (!adev) + rdev =kzalloc (sizeof (*rdev),GFP_KERNEL ); +if (!rdev)return -ENOMEM ; + rdev->base = priv->base; + + adev = &rdev->adev; adev->name = adev_name; adev->dev.parent = priv->dev; adev->dev.release = jh7110_reset_adev_release;drivers/reset/starfive/reset-starfive-jh7110.c +2
-0@@ -7,6 +7,8 @@#include <linux/auxiliary_bus.h> +#include <soc/starfive/reset-starfive-jh71x0.h> +#include "reset-starfive-jh71x0.h" #include <dt-bindings/reset/starfive,jh7110-crg.h> drivers/reset/starfive/reset-starfive-jh7110.c static int jh7110_reset_probe(struct auxiliary_device *adev, · line 36
Quoted 4 lines
drivers/reset/starfive/reset-starfive-jh7110.c +0
-1@@ -33,14 +35,15 @@ static int jh7110_reset_probe(struct auxiliary_device *adev, const struct auxiliary_device_id *id) {struct jh7110_reset_info *info = (struct jh7110_reset_info *)(id->driver_data); -void __iomem **base = (void __iomem **)dev_get_drvdata (adev->dev.parent);Thank you for doing that. BTW, if drop the
dev_get_drvdata(), thedev_set_drvdata()should also be dropped.drivers/clk/starfive/clk-starfive-jh7110-aon.c +0
-2@@ -83,8 +83,6 @@ static int jh7110_aoncrg_probe(struct platform_device *pdev)if (IS_ERR (priv->base))return PTR_ERR (priv->base); -dev_set_drvdata (priv->dev, (void *)(&priv->base)); -for (idx =0 ; idx <JH7110_AONCLK_END ; idx++) {u32 max = jh7110_aonclk_data[idx].max;struct clk_parent_data parents[4 ] = {};drivers/clk/starfive/clk-starfive-jh7110-sys.c +0
-0
drivers/clk/starfive/clk-starfive-jh7110-sys.c static int __init jh7110_syscrg_probe(struct platform_device *pdev) · line 398
Quoted 8 lines
drivers/clk/starfive/clk-starfive-jh7110-sys.c +0
-2@@ -393,8 +393,6 @@ static int __init jh7110_syscrg_probe(struct platform_device *pdev)if (IS_ERR (priv->base))return PTR_ERR (priv->base); -dev_set_drvdata (priv->dev, (void *)(&priv->base)); -/* * These PLL clocks are not actually fixed factor clocks and can be * controlled by the syscon registers of JH7110. They will be dropped
Hi, Stephen,
Thanks for your fix to my previous patches, and I have tested this patch on VisionFive 2 board. As Xingyu said above, I think dev_set_drvdata() should also be dropped in clk-starfive-jh7110-sys.c and clk-starfive-jh7110-aon.c.
Best regards,
Hal
Stephen Boyd
maintainerQuoting Hal Feng (2023-04-14 16:31:45)
Quoted 6 lines
Thanks for your fix to my previous patches, and I have tested this patch on VisionFive 2 board. As Xingyu said above, I think
dev_set_drvdata()should also be dropped inclk-starfive-jh7110-sys.candclk-starfive-jh7110-aon.c.
Sure. I see you sent the patch. Thanks.