Skip to contentSkip to conversation

media: Make sure clk_init_data is fully initialized

PATCH In review Geert Uytterhoeven 4 participants 7 messages Aug 21 – Aug 27

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

cover media: Make sure clk_init_data is fully initialized

Hi all,

The clk_init_data structure contains several mutually-exclusive members for different methods to specify the possible parents of a clock, prompting drivers to initialize only the members they need. However, not initializing all members may cause subtle issues, which are only exposed when CONFIG_INIT_STACK_ALL_PATTERN or CONFIG_INIT_STACK_NONE is enabled.

Hence this series aims to make sure all members are fully initialized, to avoid such bugs, and to prevent future breakage when converting drivers to a different method for specifying the parents.

Part One[1] fixed all cases that I identified to be real bugs, in response to a crash I saw on BeagleBone Black.

This series is the media subpart of Part Two, which fixes remaining cases that are currently harmless. These are still fragile, and may cause future breakage when converting drivers to a different method for specifying the parents.

Thanks for your comments!

[1] "[PATCH treewide 0/5] clk: Make sure clk_init_data is fully initialized (part 1)"
https://lore.kernel.org/[email protected]

Geert Uytterhoeven (3):
media: cec: meson: ao-cec-g12a: Make sure clk_init_data is fully
initialized
media: microchip: Make sure clk_init_data is fully initialized
media: samsung: exynos4-is: Make sure clk_init_data is fully
initialized

 drivers/media/cec/platform/meson/ao-cec-g12a.c        | 2 +-
 drivers/media/platform/microchip/microchip-isc-clk.c  | 2 +-
 drivers/media/platform/samsung/exynos4-is/media-dev.c | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
Signature · 12 lines
2.43.0

Gr{oetje,eeting}s,

						Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
							    -- Linus Torvalds
#1 lore raw

PATCH 1/3 media: cec: meson: ao-cec-g12a: Make sure clk_init_data is fully initialized

The clk_init_data structure contains several mutually-exclusive members for different methods to specify the possible parents of a clock, prompting drivers to initialize only the members they need. However, not initializing all members may cause subtle issues, which are only exposed when CONFIG_INIT_STACK_ALL_PATTERN or CONFIG_INIT_STACK_NONE is enabled.

Make sure all members are fully initialized, to avoid such bugs, and to prevent future breakage when converting drivers to a different method for specifying the parents.

---
Compile-tested only.

---
 drivers/media/cec/platform/meson/ao-cec-g12a.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/cec/platform/meson/ao-cec-g12a.c +1 -1
index b175be3f2bf4de87..55bd0d416460f820 100644
@@ -334,7 +334,7 @@ static int meson_ao_cec_g12a_setup_clk(struct meson_ao_cec_g12a_device *ao_cec)
 {
 	struct meson_ao_cec_g12a_dualdiv_clk *dualdiv_clk;
 	struct device *dev = &ao_cec->pdev->dev;
-	struct clk_init_data init;
+	struct clk_init_data init = {};
 	const char *parent_name;
 	struct clk *clk;
 	char *name;
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/3 media: microchip: Make sure clk_init_data is fully initialized

In reply to Geert Uytterhoeven

The clk_init_data structure contains several mutually-exclusive members for different methods to specify the possible parents of a clock, prompting drivers to initialize only the members they need. However, not initializing all members may cause subtle issues, which are only exposed when CONFIG_INIT_STACK_ALL_PATTERN or CONFIG_INIT_STACK_NONE is enabled.

Make sure all members are fully initialized, to avoid such bugs, and to prevent future breakage when converting drivers to a different method for specifying the parents.

---
Compile-tested only.

---
 drivers/media/platform/microchip/microchip-isc-clk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/microchip/microchip-isc-clk.c +1 -1
index 24358d804e75d75c..65d5fb8019b341b8 100644
@@ -231,7 +231,7 @@ static int isc_clk_register(struct isc_device *isc, unsigned int id)
 	struct regmap *regmap = isc->regmap;
 	struct device_node *np = isc->dev->of_node;
 	struct isc_clk *isc_clk;
-	struct clk_init_data init;
+	struct clk_init_data init = {};
 	const char *clk_name = np->name;
 	const char *parent_names[3];
 	int num_parents;
Signature · 1 line
2.43.0
maintainer Reviewed-by on v1 2/3
#3 lore raw

Re: [PATCH 2/3] media: microchip: Make sure clk_init_data is fully initialized

Quoted 11 lines · Geert Uytterhoeven

The clk_init_data structure contains several mutually-exclusive members for different methods to specify the possible parents of a clock, prompting drivers to initialize only the members they need. However, not initializing all members may cause subtle issues, which are only exposed when CONFIG_INIT_STACK_ALL_PATTERN or CONFIG_INIT_STACK_NONE is enabled.

Make sure all members are fully initialized, to avoid such bugs, and to prevent future breakage when converting drivers to a different method for specifying the parents.

Quoted 7 lines

---
Compile-tested only.

---
 drivers/media/platform/microchip/microchip-isc-clk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

The only nitpick I have is that "media: microchip" holds multiple drivers, while you are fixing this for the microchip ISC.

Regardless,

Quoted 13 lines
drivers/media/platform/microchip/microchip-isc-clk.c +1 -1
index 24358d804e75d75c..65d5fb8019b341b8 100644
@@ -231,7 +231,7 @@ static int isc_clk_register(struct isc_device *isc, unsigned int id)
 	struct regmap *regmap = isc->regmap;
 	struct device_node *np = isc->dev->of_node;
 	struct isc_clk *isc_clk;
-	struct clk_init_data init;
+	struct clk_init_data init = {};
 	const char *clk_name = np->name;
 	const char *parent_names[3];
 	int num_parents;
#4 lore raw

PATCH 3/3 media: samsung: exynos4-is: Make sure clk_init_data is fully initialized

In reply to Geert Uytterhoeven

The clk_init_data structure contains several mutually-exclusive members for different methods to specify the possible parents of a clock, prompting drivers to initialize only the members they need. However, not initializing all members may cause subtle issues, which are only exposed when CONFIG_INIT_STACK_ALL_PATTERN or CONFIG_INIT_STACK_NONE is enabled.

Make sure all members are fully initialized, to avoid such bugs, and to prevent future breakage when converting drivers to a different method for specifying the parents.

---
Compile-tested only.

---
 drivers/media/platform/samsung/exynos4-is/media-dev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/samsung/exynos4-is/media-dev.c +1 -1
index 2e08c4435859e8d2..638babf52822113c 100644
@@ -1313,7 +1313,7 @@ static int fimc_md_register_clk_provider(struct fimc_md *fmd)
 
 	for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
 		struct cam_clk *camclk = &cp->camclk[i];
-		struct clk_init_data init;
+		struct clk_init_data init = {};
 		const char *p_name;
 
 		ret = of_property_read_string_index(dev->of_node,
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH 3/3] media: samsung: exynos4-is: Make sure clk_init_data is fully initialized

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Double free and use-after-free of device node references in OF iterator loop. --

--- Patch [3]: [PATCH 3/3] media: samsung: exynos4-is: Make sure clk_init_data is fully initialized --- commit f3a10ad891243d9b897aed17537077a8bb8725bc
Author: Geert Uytterhoeven <[email protected]>

media: samsung: exynos4-is: Make sure clk_init_data is fully initialized

The clk_init_data structure contains mutually-exclusive members for specifying clock parents. This commit ensures all members are fully initialized to zero, preventing subtle bugs with uninitialized data.

drivers/media/platform/samsung/exynos4-is/media-dev.c static int fimc_md_register_clk_provider(struct fimc_md *fmd) · line 1319

Quoted 13 lines
drivers/media/platform/samsung/exynos4-is/media-dev.c +1 -1
index 2e08c4435859e..638babf528221 100644
@@ -1313,7 +1313,7 @@ static int fimc_md_register_clk_provider(struct fimc_md *fmd)
 
 	for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
 		struct cam_clk *camclk = &cp->camclk[i];
-		struct clk_init_data init;
+		struct clk_init_data init = {};
 		const char *p_name;
 
 		ret = of_property_read_string_index(dev->of_node,

[Severity: High]
This is a pre-existing issue, but I noticed a potential double-free of device node references elsewhere in this file.

Looking at fimc_md_parse_port_node(), it iterates over child nodes using the scoped iterator macro:

drivers/media/platform/samsung/exynos4-is/media-dev.c:fimc_md_parse_port_node() {
    ...
	for_each_child_of_node_scoped(port, ep) {
		ret = fimc_md_parse_one_endpoint(fmd, ep);
		if (ret < 0)
			return ret;
	}
    ...
}

This macro automatically manages the reference counting for ep, dropping it implicitly when the loop advances or exits.

However, inside fimc_md_parse_one_endpoint(), the code explicitly calls of_node_put(ep) on all paths:

drivers/media/platform/samsung/exynos4-is/media-dev.c:fimc_md_parse_one_endpoint() {
    ...
	asd = v4l2_async_nf_add_fwnode_remote(&fmd->subdev_notifier,
					      of_fwnode_handle(ep),
					      struct v4l2_async_connection);

	of_node_put(ep);
    ...
}

Because the reference is already dropped in the helper function, wouldn't the subsequent put by the iterator's loop advance or scope exit result in a double-free and a use-after-free of the device node?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Reviewed-by on the v1 series
#6 lore raw
In reply to Geert Uytterhoeven
Quoted 16 lines · Geert Uytterhoeven

Hi all,

The clk_init_data structure contains several mutually-exclusive members for different methods to specify the possible parents of a clock, prompting drivers to initialize only the members they need. However, not initializing all members may cause subtle issues, which are only exposed when CONFIG_INIT_STACK_ALL_PATTERN or CONFIG_INIT_STACK_NONE is enabled.

Hence this series aims to make sure all members are fully initialized, to avoid such bugs, and to prevent future breakage when converting drivers to a different method for specifying the parents.

Part One[1] fixed all cases that I identified to be real bugs, in response to a crash I saw on BeagleBone Black.

Quoted 6 lines

This series is the media subpart of Part Two, which fixes remaining cases that are currently harmless. These are still fragile, and may cause future breakage when converting drivers to a different method for specifying the parents.

Thanks for your comments!

For the series: