ASoC: Make sure clk_init_data is fully initialized
Subsystems (6)
cover ASoC: 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 ASoC 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):
ASoC: codecs: lpass: Make sure clk_init_data is fully initialized
ASoC: codecs: wcd934x: Make sure clk_init_data is fully initialized
ASoC: tlv320aic32x4: Make sure clk_init_data is fully initialized
sound/soc/codecs/lpass-rx-macro.c | 2 +- sound/soc/codecs/lpass-tx-macro.c | 2 +- sound/soc/codecs/lpass-va-macro.c | 2 +- sound/soc/codecs/lpass-wsa-macro.c | 2 +- sound/soc/codecs/tlv320aic32x4-clk.c | 2 +- sound/soc/codecs/wcd934x.c | 2 +- 6 files changed, 6 insertions(+), 6 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/3 ASoC: codecs: lpass: 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.
--- sound/soc/codecs/lpass-rx-macro.c | 2 +- sound/soc/codecs/lpass-tx-macro.c | 2 +- sound/soc/codecs/lpass-va-macro.c | 2 +- sound/soc/codecs/lpass-wsa-macro.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)
sound/soc/codecs/lpass-rx-macro.c +1 -1
@@ -3732,8 +3732,8 @@ static int rx_macro_register_mclk_output(struct rx_macro *rx)struct device *dev = rx->dev;const char *parent_clk_name =NULL ;const char *clk_name ="lpass-rx-mclk" ; +struct clk_init_data init = {};struct clk_hw *hw; -struct clk_init_data init;int ret;if (rx->npl)
sound/soc/codecs/lpass-tx-macro.c +1 -1
@@ -2210,8 +2210,8 @@ static int tx_macro_register_mclk_output(struct tx_macro *tx)struct device *dev = tx->dev;const char *parent_clk_name =NULL ;const char *clk_name ="lpass-tx-mclk" ; +struct clk_init_data init = {};struct clk_hw *hw; -struct clk_init_data init;int ret;if (tx->npl)
sound/soc/codecs/lpass-va-macro.c +1 -1
@@ -1414,7 +1414,7 @@ static int va_macro_register_fsgen_output(struct va_macro *va)struct device_node *np = dev->of_node;const char *parent_clk_name;const char *clk_name ="fsgen" ; -struct clk_init_data init; +struct clk_init_data init = {};int ret;if (va->has_npl_clk)
sound/soc/codecs/lpass-wsa-macro.c +1 -1
@@ -2638,10 +2638,10 @@ static const struct clk_ops swclk_gate_ops = {static int wsa_macro_register_mclk_output (struct wsa_macro *wsa) { +struct clk_init_data init = {};struct device *dev = wsa->dev;const char *parent_clk_name;struct clk_hw *hw; -struct clk_init_data init;int ret;if (wsa->npl)
Signature · 1 line
2.43.0
Re: [PATCH 1/3] ASoC: codecs: lpass: Make sure clk_init_data is fully initialized
Quoted 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/3 ASoC: codecs: wcd934x: 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.
--- sound/soc/codecs/wcd934x.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
sound/soc/codecs/wcd934x.c +1 -1
@@ -2130,9 +2130,9 @@ static struct clk *wcd934x_register_mclk_output(struct wcd934x_codec *wcd)struct device *dev = wcd->dev;struct device_node *np = dev->parent->of_node;const char *parent_clk_name =NULL ; +struct clk_init_data init = {};const char *clk_name ="mclk" ;struct clk_hw *hw; -struct clk_init_data init;int ret;if (of_property_read_u32 (np,"clock-frequency" , &wcd->rate))
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH 2/3] ASoC: codecs: wcd934x: 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] Use-After-Free of wcd structure due to mismatched devres lifecycles. --
--- Patch [2]: [PATCH 2/3] ASoC: codecs: wcd934x: Make sure clk_init_data is fully initialized --- commit ff8f9601f6c3274a4ff6020b3e29ffe892c8e05d
Author: Geert Uytterhoeven <[email protected]>
ASoC: codecs: wcd934x: 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. This commit makes sure all members are fully initialized.
Quoted 5 lines
sound/soc/codecs/wcd934x.c +0
-0
sound/soc/codecs/wcd934x.c static struct clk *wcd934x_register_mclk_output(struct wcd934x_codec *wcd) · line 2138
Quoted 10 lines
sound/soc/codecs/wcd934x.c +1
-1@@ -2130,9 +2130,9 @@ static struct clk *wcd934x_register_mclk_output(struct wcd934x_codec *wcd)struct device *dev = wcd->dev;struct device_node *np = dev->parent->of_node;const char *parent_clk_name =NULL ; +struct clk_init_data init = {};const char *clk_name ="mclk" ;struct clk_hw *hw; -struct clk_init_data init;int ret;if (of_property_read_u32 (np,"clock-frequency" , &wcd->rate))
[Severity: High]
This is a pre-existing issue, but is there a use-after-free risk due to mismatched devres lifecycles when the driver is unbound?
The wcd structure is allocated using devm_kzalloc on the codec device's lifecycle in wcd934x_codec_probe:
sound/soc/codecs/wcd934x.c:wcd934x_codec_probe() {
...
wcd = devm_kzalloc(dev, sizeof(*wcd), GFP_KERNEL);
...
}However, further down in wcd934x_register_mclk_output, the embedded clock hardware is registered against the parent device's lifecycle:
sound/soc/codecs/wcd934x.c:wcd934x_register_mclk_output() {
...
ret = devm_clk_hw_register(wcd->dev->parent, hw);
...
}When the parent MFD driver is unbound (e.g. via rmmod), the driver core first unbinds all child devices. The codec device unbinds, freeing wcd. Then, the parent MFD device unbinds, and its devres cleanup invokes clk_hw_unregister on the now-freed hw pointer.
Does devm_clk_hw_register need to be called on the child dev instead of the parent?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH 2/3] ASoC: codecs: wcd934x: 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 3/3 ASoC: tlv320aic32x4: 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.
--- sound/soc/codecs/tlv320aic32x4-clk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
sound/soc/codecs/tlv320aic32x4-clk.c +1 -1
@@ -451,8 +451,8 @@ static struct aic32x4_clkdesc aic32x4_clkdesc_array[] = {static struct clk *aic32x4_register_clk (struct device *dev,struct aic32x4_clkdesc *desc) { -struct clk_init_data init;struct clk_aic32x4 *priv; +struct clk_init_data init = {};const char *devname =dev_name (dev); init.ops = desc->ops;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH 3/3] ASoC: tlv320aic32x4: Make sure clk_init_data is fully initialized
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
Pre-existing issues:
- [High] Memory leak and Use-After-Free via unmanaged clk_lookup entries.
- [High] Data race and stack Use-After-Free due to assigning a block-scoped compound literal to a global array.
- [High] Unchecked return values of resource allocations and clock registrations mask initialization failures.
- [High] Hardcoded clock names cause registration failures for multiple codec instances.
- [High] Unvalidated requested clock rate causes a division by zero panic.
- [High] Unvalidated parent clock rate causes a division by zero panic.
--
--- Patch [3]: [PATCH 3/3] ASoC: tlv320aic32x4: Make sure clk_init_data is fully initialized --- commit bdb202ba7d1e5e85e1733f12689e1c8729d8c7d3
Author: Geert Uytterhoeven <[email protected]>
ASoC: tlv320aic32x4: 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 5 lines
sound/soc/codecs/tlv320aic32x4-clk.c +0
-0
sound/soc/codecs/tlv320aic32x4-clk.c static struct aic32x4_clkdesc aic32x4_clkdesc_array[] = { · line 458
Quoted 9 lines
sound/soc/codecs/tlv320aic32x4-clk.c +1
-1@@ -451,8 +451,8 @@ static struct aic32x4_clkdesc aic32x4_clkdesc_array[] = {static struct clk *aic32x4_register_clk (struct device *dev,struct aic32x4_clkdesc *desc) { -struct clk_init_data init;struct clk_aic32x4 *priv; +struct clk_init_data init = {};const char *devname =dev_name (dev); init.ops = desc->ops;
[Severity: High]
This isn't a bug introduced by this patch, but does hardcoding the clock name directly from the static descriptor (init.name = desc->name) prevent probing multiple instances of this codec?
If devm_clk_register() is called on a system with multiple codecs, will it fail with -EEXIST because names like 'pll' and 'codec_clkin' are reused?
While reviewing this file, I noticed a few other pre-existing regressions:
[Severity: High]
This is a pre-existing issue, but is there a clk_lookup entry leaked when the device is unbound?
sound/soc/codecs/tlv320aic32x4-clk.c:aic32x4_register_clk() {
...
clk_hw_register_clkdev(&priv->hw, desc->name, devname);
return devm_clk_register(dev, &priv->hw);
}Since clk_hw_register_clkdev() dynamically allocates a clk_lookup on the global list without using a devres variant or dropping it in teardown, will the lookup retain a dangling pointer to the freed priv->hw if probe fails or the driver is unbound?
[Severity: High]
This isn't a bug introduced by this patch, but is stack memory being written into the static global aic32x4_clkdesc_array?
sound/soc/codecs/tlv320aic32x4-clk.c:aic32x4_register_clocks() {
...
aic32x4_clkdesc_array[0].parent_names =
(const char* []) { mclk_name, "bclk", "gpio", "din" };
aic32x4_clkdesc_array[1].parent_names =
(const char *[]) { mclk_name, "bclk", "gpio", "pll" };
...
}Because these compound literals are block-scoped (stack allocated), if this function returns, won't the global array point to dead stack memory, leading to a use-after-free when the clock framework reads the parent names?
[Severity: High]
This is a pre-existing issue, but are we discarding the return value of aic32x4_register_clk() and masking probe failures?
sound/soc/codecs/tlv320aic32x4-clk.c:aic32x4_register_clocks() {
...
for (i = 0; i < ARRAY_SIZE(aic32x4_clkdesc_array); ++i)
aic32x4_register_clk(dev, &aic32x4_clkdesc_array[i]);
return 0;
}If aic32x4_register_clk() fails due to low memory or clock name collisions, will the driver falsely report a successful probe?
[Severity: High]
This isn't a bug introduced by this patch, but could DIV_ROUND_UP() trigger a division by zero panic?
sound/soc/codecs/tlv320aic32x4-clk.c:clk_aic32x4_div_determine_rate() {
...
divisor = DIV_ROUND_UP(req->best_parent_rate, req->rate);
...
}If a 0 rate is supplied via the Common Clock Framework API, will this cause a kernel panic?
This same pattern also appears in clk_aic32x4_div_set_rate() where DIV_ROUND_UP(parent_rate, rate) is called.
[Severity: High]
This is a pre-existing issue, but might do_div() trigger a division by zero panic?
sound/soc/codecs/tlv320aic32x4-clk.c:clk_aic32x4_pll_calc_muldiv() {
...
multiplier = (u64)rate * settings->p * 10000;
do_div(multiplier, parent_rate);
...
}If the PLL clock is queried or set when its parent clock is uninitialized or reporting a 0 Hz rate, will do_div() divide by zero?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH 3/3] ASoC: tlv320aic32x4: 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]>
Mark Brown
maintainerQuoted 4 lines · Geert Uytterhoeven
ASoC: Make sure
clk_init_datais fully initializedHi all,
Quoted 8 lines
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.[...]
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-7.4
Thanks!
[1/3] ASoC: codecs: lpass: Make sure clk_init_data is fully initialized
https://git.kernel.org/broonie/sound/c/2cf55c64e64e [2/3] ASoC: codecs: wcd934x: Make sure clk_init_data is fully initialized https://git.kernel.org/broonie/sound/c/738a163b7ad5 [3/3] ASoC: tlv320aic32x4: Make sure clk_init_data is fully initialized https://git.kernel.org/broonie/sound/c/f1b2a0434b5b
All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying to this mail.
Thanks,
Mark