drm: Make sure clk_init_data is fully initialized
Subsystems (8)
cover drm: 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 DRM 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 (2):
drm/sun4i: Make sure clk_init_data is fully initialized drm/tegra: Make sure clk_init_data is fully initialized
drivers/gpu/drm/sun4i/sun4i_hdmi_ddc_clk.c | 2 +- drivers/gpu/drm/sun4i/sun4i_hdmi_tmds_clk.c | 2 +- drivers/gpu/drm/sun4i/sun4i_tcon_dclk.c | 2 +- drivers/gpu/drm/sun4i/sun8i_hdmi_phy_clk.c | 2 +- drivers/gpu/drm/tegra/sor.c | 2 +- 5 files changed, 5 insertions(+), 5 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 TorvaldsPATCH 1/2 drm/sun4i: 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/gpu/drm/sun4i/sun4i_hdmi_ddc_clk.c | 2 +- drivers/gpu/drm/sun4i/sun4i_hdmi_tmds_clk.c | 2 +- drivers/gpu/drm/sun4i/sun4i_tcon_dclk.c | 2 +- drivers/gpu/drm/sun4i/sun8i_hdmi_phy_clk.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/sun4i/sun4i_hdmi_ddc_clk.c +1 -1
@@ -109,7 +109,7 @@ static const struct clk_ops sun4i_ddc_ops = {int sun4i_ddc_create (struct sun4i_hdmi *hdmi,struct clk *parent) { -struct clk_init_data init; +struct clk_init_data init = {};struct sun4i_ddc *ddc;const char *parent_name;
drivers/gpu/drm/sun4i/sun4i_hdmi_tmds_clk.c +1 -1
@@ -202,7 +202,7 @@ static const struct clk_ops sun4i_tmds_ops = {int sun4i_tmds_create (struct sun4i_hdmi *hdmi) { -struct clk_init_data init; +struct clk_init_data init = {};struct sun4i_tmds *tmds;const char *parents[2 ];
drivers/gpu/drm/sun4i/sun4i_tcon_dclk.c +1 -1
@@ -167,7 +167,7 @@ static const struct clk_ops sun4i_dclk_ops = {int sun4i_dclk_create (struct device *dev,struct sun4i_tcon *tcon) {const char *clk_name, *parent_name; -struct clk_init_data init; +struct clk_init_data init = {};struct sun4i_dclk *dclk;int ret;
drivers/gpu/drm/sun4i/sun8i_hdmi_phy_clk.c +1 -1
@@ -143,7 +143,7 @@ static const struct clk_ops sun8i_phy_clk_ops = {int sun8i_phy_clk_create (struct sun8i_hdmi_phy *phy,struct device *dev,bool second_parent) { -struct clk_init_data init; +struct clk_init_data init = {};struct sun8i_phy_clk *priv;const char *parents[2 ];
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH 1/2] drm/sun4i: 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] Resource leak and duplicate registration failure due to misuse of devm_ APIs against an external device in a component .bind callback. --
--- Patch [1]: [PATCH 1/2] drm/sun4i: Make sure clk_init_data is fully initialized --- commit c10328b2f0b88fab02ca03cdd9ba8cea5c03c556
Author: Geert Uytterhoeven <[email protected]>
drm/sun4i: Make sure clk_init_data is fully initialized
This commit zero-initializes the clk_init_data structure to ensure all members are fully initialized. This prevents subtle issues when only some mutually-exclusive members are set, especially under certain stack initialization configs.
[ ... ]
drivers/gpu/drm/sun4i/sun8i_hdmi_phy_clk.c static const struct clk_ops sun8i_phy_clk_ops = { · line 149
Quoted 13 lines
drivers/gpu/drm/sun4i/sun8i_hdmi_phy_clk.c +1
-1@@ -143,7 +143,7 @@ static const struct clk_ops sun8i_phy_clk_ops = {int sun8i_phy_clk_create (struct sun8i_hdmi_phy *phy,struct device *dev,bool second_parent) { -struct clk_init_data init; +struct clk_init_data init = {};struct sun8i_phy_clk *priv;const char *parents[2 ];
[Severity: High]
This isn't a bug introduced by this patch, but I noticed a potential problem with resource lifetimes in this function.
During the component bind phase, this code uses devm_kzalloc() and devm_clk_register() against the external PHY platform device:
sun8i_phy_clk_create() {
...
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
...
phy->clk_phy = devm_clk_register(dev, &priv->hw);
...
}Because the devres group is attached to the external PHY device (dev) rather than the component device, it escapes the component unbind cycle.
When the HDMI component is unbound and re-bound, sun8i_phy_clk_create() runs again. Will this permanently leak the memory allocated for priv?
Furthermore, will devm_clk_register() fail with -EEXIST on the second bind since the previous clock was never unregistered?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH 1/2] drm/sun4i: Make sure clk_init_data is fully initialized
In reply to Geert UytterhoevenQuoted 8 lines · Geert Uytterhoeven
The
clk_init_datastructure 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 whenCONFIG_INIT_STACK_ALL_PATTERNorCONFIG_INIT_STACK_NONEis enabled.
Quoted 6 lines
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.
Signed-off-by Geert Uytterhoeven <[email protected]>---
Re: [PATCH 1/2] drm/sun4i: Make sure clk_init_data is fully initialized
In reply to Geert UytterhoevenQuoted 7 lines · Geert Uytterhoeven
The
clk_init_datastructure 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 whenCONFIG_INIT_STACK_ALL_PATTERNorCONFIG_INIT_STACK_NONEis enabled.
Quoted 5 lines
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.
Signed-off-by Geert Uytterhoeven <[email protected]>
PATCH 2/2 drm/tegra: Make sure clk_init_data is fully initialized
In reply to Geert UytterhoevenThe 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/gpu/drm/tegra/sor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/tegra/sor.c +1 -1
@@ -600,8 +600,8 @@ static const struct clk_ops tegra_clk_sor_pad_ops = {static struct clk *tegra_clk_sor_pad_register (struct tegra_sor *sor,const char *name) { +struct clk_init_data init = {};struct tegra_clk_sor_pad *pad; -struct clk_init_data init;struct clk *clk; pad =devm_kzalloc (sor->dev,sizeof (*pad),GFP_KERNEL );
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH 2/2] drm/tegra: 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] Missing bounds check on the nvidia,interface device tree property causes out-of-bounds array indexing and arbitrary MMIO access. --
--- Patch [2]: [PATCH 2/2] drm/tegra: Make sure clk_init_data is fully initialized --- commit d6e18a3c09cc145658765914a32b191ad5da6aa8
Author: Geert Uytterhoeven <[email protected]>
drm/tegra: 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.
Quoted 14 lines
drivers/gpu/drm/tegra/sor.c +1
-1@@ -596,8 +596,8 @@ static const struct clk_ops tegra_clk_sor_pad_ops = {static struct clk *tegra_clk_sor_pad_register (struct tegra_sor *sor,const char *name) { +struct clk_init_data init = {};struct tegra_clk_sor_pad *pad; -struct clk_init_data init;struct clk *clk; pad =devm_kzalloc (sor->dev,sizeof (*pad),GFP_KERNEL );
[ ... ]
drivers/gpu/drm/tegra/sor.c
Quoted 5 lines
drivers/gpu/drm/tegra/sor.c +0
-0pad->sor = sor; init.name = name; init.flags =0 ; init.parent_names = tegra_clk_sor_pad_parents[sor->index];
[Severity: High]
This is a pre-existing issue, but can this out-of-bounds array indexing occur if the Device Tree provides an invalid "nvidia,interface" value?
In tegra_sor_parse_dt(), the "nvidia,interface" property is read and assigned directly to sor->index without any bounds checking:
drivers/gpu/drm/tegra/sor.c:tegra_sor_parse_dt() {
...
err = of_property_read_u32(np, "nvidia,interface", &value);
if (err < 0)
return err;
sor->index = value;
...
}If a malformed Device Tree is provided, does using this unbounded sor->index to access the tegra_clk_sor_pad_parents array lead to an out-of-bounds memory read?
It appears it could also cause arbitrary MMIO reads and writes when computing register offsets using DC_DISP_CORE_SOR_SET_CONTROL(sor->index).
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH 2/2] drm/tegra: Make sure clk_init_data is fully initialized
In reply to Geert UytterhoevenQuoted 7 lines · Geert Uytterhoeven
The
clk_init_datastructure 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 whenCONFIG_INIT_STACK_ALL_PATTERNorCONFIG_INIT_STACK_NONEis enabled.
Quoted 5 lines
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.
Signed-off-by Geert Uytterhoeven <[email protected]>