* [PATCH] ASoC: arizona: Disable jack detection when the component is removed
@ 2026-11-24 5:02 Suyuan Wang via B4 Relay
2026-11-24 5:17 ` sashiko-bot
0 siblings, 1 reply; 3+ messages in thread
From: Suyuan Wang via B4 Relay @ 2026-11-24 5:02 UTC (permalink / raw)
To: broonie, lgirdwood
Cc: linux-sound, hansg, patches, ckeepax, tiwai, linux-kernel, perex
From: Suyuan Wang <stevewong@hust.edu.cn>
The wm5102, wm5110, wm8997 and wm8998 codecs start jack detection when
the machine driver calls snd_soc_component_set_jack().
arizona_jack_enable_jack_detect() then requests the jack, MICDET and
HPDET interrupts of the arizona MFD with the codec's struct arizona_priv
as the cookie, and keeps the snd_soc_jack pointer in info->jack. Only
set_jack(NULL) frees these interrupts and cancels the detection works.
Nothing calls set_jack(NULL) on teardown. The component_remove
callbacks don't, and neither does bytcr_wm5102, the only machine driver
that sets a jack on these codecs. The interrupts belong to the MFD, so
they stay registered after the card or the codec goes away:
- When the card is unbound, the snd_soc_jack in the machine driver's
private data is freed, but jack detection stays active and can still
report events to it.
- When the codec is unbound, its private data, which contains struct
arizona_priv, is freed while the interrupts still use it as cookie.
A later headphone plug or unplug can then run the handlers, or the
works they queue, on freed memory.
Before commit 37dbabf14ff6 ("ASoC: arizona: Make the wm5102, wm5110,
wm8997 and wm8998 drivers use the new jack library"), jack detection
was done by the extcon-arizona driver, which freed these interrupts and
cancelled the works in its own remove callback.
Disable jack detection in the component_remove callbacks. The component
is removed both when the card and when the codec is unbound, and
disabling does nothing if no jack is set. When the card is bound again,
the machine driver sets the jack again.
Found with a static analysis tool and checked by hand.
Fixes: 37dbabf14ff6 ("ASoC: arizona: Make the wm5102, wm5110, wm8997 and wm8998 drivers use the new jack library")
Cc: stable@vger.kernel.org
Co-developed-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
Signed-off-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
Signed-off-by: Suyuan Wang <stevewong@hust.edu.cn>
---
Compile tested on x86_64 (W=1, no new warnings). I don't have hardware
with these codecs.
---
sound/soc/codecs/wm5102.c | 2 ++
sound/soc/codecs/wm5110.c | 2 ++
sound/soc/codecs/wm8997.c | 2 ++
sound/soc/codecs/wm8998.c | 2 ++
4 files changed, 8 insertions(+)
diff --git a/sound/soc/codecs/wm5102.c b/sound/soc/codecs/wm5102.c
index 762e81979cb8..43df7cb46b59 100644
--- a/sound/soc/codecs/wm5102.c
+++ b/sound/soc/codecs/wm5102.c
@@ -1982,6 +1982,8 @@ static void wm5102_component_remove(struct snd_soc_component *component)
{
struct wm5102_priv *priv = snd_soc_component_get_drvdata(component);
+ arizona_jack_set_jack(component, NULL, NULL);
+
wm_adsp2_component_remove(&priv->core.adsp[0], component);
priv->core.arizona->dapm = NULL;
diff --git a/sound/soc/codecs/wm5110.c b/sound/soc/codecs/wm5110.c
index faa85d88a489..c772f4380ae7 100644
--- a/sound/soc/codecs/wm5110.c
+++ b/sound/soc/codecs/wm5110.c
@@ -2340,6 +2340,8 @@ static void wm5110_component_remove(struct snd_soc_component *component)
struct wm5110_priv *priv = snd_soc_component_get_drvdata(component);
int i;
+ arizona_jack_set_jack(component, NULL, NULL);
+
for (i = 0; i < WM5110_NUM_ADSP; ++i)
wm_adsp2_component_remove(&priv->core.adsp[i], component);
diff --git a/sound/soc/codecs/wm8997.c b/sound/soc/codecs/wm8997.c
index f041dfc905a4..aa9f23ddd997 100644
--- a/sound/soc/codecs/wm8997.c
+++ b/sound/soc/codecs/wm8997.c
@@ -1077,6 +1077,8 @@ static void wm8997_component_remove(struct snd_soc_component *component)
{
struct wm8997_priv *priv = snd_soc_component_get_drvdata(component);
+ arizona_jack_set_jack(component, NULL, NULL);
+
priv->core.arizona->dapm = NULL;
}
diff --git a/sound/soc/codecs/wm8998.c b/sound/soc/codecs/wm8998.c
index 1158e9ca40a1..47adbcd80e07 100644
--- a/sound/soc/codecs/wm8998.c
+++ b/sound/soc/codecs/wm8998.c
@@ -1301,6 +1301,8 @@ static void wm8998_component_remove(struct snd_soc_component *component)
{
struct wm8998_priv *priv = snd_soc_component_get_drvdata(component);
+ arizona_jack_set_jack(component, NULL, NULL);
+
priv->core.arizona->dapm = NULL;
}
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] ASoC: arizona: Disable jack detection when the component is removed
2026-11-24 5:02 [PATCH] ASoC: arizona: Disable jack detection when the component is removed Suyuan Wang via B4 Relay
@ 2026-11-24 5:17 ` sashiko-bot
2026-11-24 6:09 ` Wong Steve
0 siblings, 1 reply; 3+ messages in thread
From: sashiko-bot @ 2026-11-24 5:17 UTC (permalink / raw)
To: Suyuan Wang; +Cc: linux-sound
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] ASoC: arizona: GPIO UAF in arizona_jack_codec_dev_remove() on
codec unbind
- [Medium] ASoC: arizona: stale jack and HPDET state in
arizona_jack_disable_jack_detect()
--
commit 64c10974d73769b43875985100c8e416d589c894
Author: Suyuan Wang <stevewong@hust.edu.cn>
ASoC: arizona: Disable jack detection when the component is removed
This commit calls arizona_jack_set_jack(component, NULL, NULL) in the
component_remove callbacks of the wm5102, wm5110, wm8997, and wm8998
codec drivers to disable jack detection on teardown.
> diff --git a/sound/soc/codecs/wm5102.c b/sound/soc/codecs/wm5102.c
> index 762e81979cb8d..43df7cb46b599 100644
> --- a/sound/soc/codecs/wm5102.c
> +++ b/sound/soc/codecs/wm5102.c
> @@ -1982,6 +1982,8 @@ static void wm5102_component_remove(struct snd_soc_component *component)
> {
> struct wm5102_priv *priv = snd_soc_component_get_drvdata(component);
>
> + arizona_jack_set_jack(component, NULL, NULL);
> +
> wm_adsp2_component_remove(&priv->core.adsp[0], component);
>
> priv->core.arizona->dapm = NULL;
> diff --git a/sound/soc/codecs/wm5110.c b/sound/soc/codecs/wm5110.c
> index faa85d88a489b..c772f4380ae7d 100644
> --- a/sound/soc/codecs/wm5110.c
> +++ b/sound/soc/codecs/wm5110.c
> @@ -2340,6 +2340,8 @@ static void wm5110_component_remove(struct snd_soc_component *component)
> struct wm5110_priv *priv = snd_soc_component_get_drvdata(component);
> int i;
>
> + arizona_jack_set_jack(component, NULL, NULL);
> +
> for (i = 0; i < WM5110_NUM_ADSP; ++i)
> wm_adsp2_component_remove(&priv->core.adsp[i], component);
>
> diff --git a/sound/soc/codecs/wm8997.c b/sound/soc/codecs/wm8997.c
> index f041dfc905a42..aa9f23ddd9975 100644
> --- a/sound/soc/codecs/wm8997.c
> +++ b/sound/soc/codecs/wm8997.c
> @@ -1077,6 +1077,8 @@ static void wm8997_component_remove(struct snd_soc_component *component)
> {
> struct wm8997_priv *priv = snd_soc_component_get_drvdata(component);
>
> + arizona_jack_set_jack(component, NULL, NULL);
> +
> priv->core.arizona->dapm = NULL;
> }
>
> diff --git a/sound/soc/codecs/wm8998.c b/sound/soc/codecs/wm8998.c
> index 1158e9ca40a13..47adbcd80e077 100644
> --- a/sound/soc/codecs/wm8998.c
> +++ b/sound/soc/codecs/wm8998.c
> @@ -1301,6 +1301,8 @@ static void wm8998_component_remove(struct snd_soc_component *component)
> {
> struct wm8998_priv *priv = snd_soc_component_get_drvdata(component);
>
> + arizona_jack_set_jack(component, NULL, NULL);
> +
> priv->core.arizona->dapm = NULL;
> }
[Severity: Medium]
When a codec platform device is unbound while a card is bound, can a jack
IRQ or delayed work use info->micd_pol_gpio or info->hpdet_id_gpio after
they have been freed?
The codec drivers register their ASoC components with
devm_snd_soc_register_component(), whereas their platform_driver.remove
callbacks call arizona_jack_codec_dev_remove() directly. Because
snd_soc_card_bind() creates a stateless device link, unbinding the codec
runs platform_driver.remove before devres_release_all() unregisters the
component and invokes component_remove:
CPU0 CPU1
wm5102_remove()
arizona_jack_codec_dev_remove()
gpiod_put(info->micd_pol_gpio)
gpiod_put(info->hpdet_id_gpio)
arizona_hpdet_irq()
gpiod_set_value_cansleep(
info->hpdet_id_gpio, 0)
<-- use-after-free
devres_release_all()
...
wm5102_component_remove()
arizona_jack_set_jack(..., NULL, NULL)
Calling arizona_jack_disable_jack_detect() inside
arizona_jack_codec_dev_remove() before dropping the GPIO descriptors
(while keeping the call in component_remove for card unbind) would close
this window.
[Severity: Medium]
If the card is unbound and bound again while a jack is plugged in, does
stale state in struct arizona_priv prevent the newly bound card from
detecting the jack?
info->last_jackdet is only initialized in arizona_jack_codec_dev_probe(),
and neither arizona_jack_disable_jack_detect() nor
arizona_jack_enable_jack_detect() resets info->last_jackdet or
info->hpdet_done. When the card is rebound and sets a new snd_soc_jack,
arizona_jackdet() sees val == info->last_jackdet and suppresses the event
instead of reporting the inserted jack, and stale info->hpdet_done causes
arizona_identify_headphone() to skip headphone detection.
In addition, if arizona_jack_disable_jack_detect() runs while an HPDET
measurement is in flight (info->hpdet_active is true), freeing
ARIZONA_IRQ_HPDET prevents arizona_hpdet_irq() from clearing
arizona->hpdet_clamp and dropping the pm_runtime_get_sync() reference
taken when starting HPDET.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124-arizona-jack-remove-v1-1-11d8ec426a41@gmail.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] ASoC: arizona: Disable jack detection when the component is removed
2026-11-24 5:17 ` sashiko-bot
@ 2026-11-24 6:09 ` Wong Steve
0 siblings, 0 replies; 3+ messages in thread
From: Wong Steve @ 2026-11-24 6:09 UTC (permalink / raw)
To: sashiko-reviews; +Cc: linux-sound
> - [Medium] ASoC: arizona: GPIO UAF in arizona_jack_codec_dev_remove() on
> codec unbind
> - [Medium] ASoC: arizona: stale jack and HPDET state in
> arizona_jack_disable_jack_detect()
Thanks, both are right. On codec unbind the platform remove callback
runs before the component is removed, and disabling jack detection
during a headphone measurement or before a rebind leaves state behind.
I'll send a v2 that also disables jack detection in
arizona_jack_codec_dev_remove() before the GPIOs are put, and makes
arizona_jack_disable_jack_detect() finish a running measurement and
reset the detection state. Mark, please don't pick up v1.
Thanks,
Suyuan
<sashiko-bot@kernel.org> 于2026年11月24日周二 13:17写道:
>
> Thank you for your contribution! Sashiko AI review found 2 potential
> issues to consider:
> - [Medium] ASoC: arizona: GPIO UAF in arizona_jack_codec_dev_remove() on
> codec unbind
> - [Medium] ASoC: arizona: stale jack and HPDET state in
> arizona_jack_disable_jack_detect()
> --
>
> commit 64c10974d73769b43875985100c8e416d589c894
> Author: Suyuan Wang <stevewong@hust.edu.cn>
>
> ASoC: arizona: Disable jack detection when the component is removed
>
> This commit calls arizona_jack_set_jack(component, NULL, NULL) in the
> component_remove callbacks of the wm5102, wm5110, wm8997, and wm8998
> codec drivers to disable jack detection on teardown.
>
> > diff --git a/sound/soc/codecs/wm5102.c b/sound/soc/codecs/wm5102.c
> > index 762e81979cb8d..43df7cb46b599 100644
> > --- a/sound/soc/codecs/wm5102.c
> > +++ b/sound/soc/codecs/wm5102.c
> > @@ -1982,6 +1982,8 @@ static void wm5102_component_remove(struct snd_soc_component *component)
> > {
> > struct wm5102_priv *priv = snd_soc_component_get_drvdata(component);
> >
> > + arizona_jack_set_jack(component, NULL, NULL);
> > +
> > wm_adsp2_component_remove(&priv->core.adsp[0], component);
> >
> > priv->core.arizona->dapm = NULL;
> > diff --git a/sound/soc/codecs/wm5110.c b/sound/soc/codecs/wm5110.c
> > index faa85d88a489b..c772f4380ae7d 100644
> > --- a/sound/soc/codecs/wm5110.c
> > +++ b/sound/soc/codecs/wm5110.c
> > @@ -2340,6 +2340,8 @@ static void wm5110_component_remove(struct snd_soc_component *component)
> > struct wm5110_priv *priv = snd_soc_component_get_drvdata(component);
> > int i;
> >
> > + arizona_jack_set_jack(component, NULL, NULL);
> > +
> > for (i = 0; i < WM5110_NUM_ADSP; ++i)
> > wm_adsp2_component_remove(&priv->core.adsp[i], component);
> >
> > diff --git a/sound/soc/codecs/wm8997.c b/sound/soc/codecs/wm8997.c
> > index f041dfc905a42..aa9f23ddd9975 100644
> > --- a/sound/soc/codecs/wm8997.c
> > +++ b/sound/soc/codecs/wm8997.c
> > @@ -1077,6 +1077,8 @@ static void wm8997_component_remove(struct snd_soc_component *component)
> > {
> > struct wm8997_priv *priv = snd_soc_component_get_drvdata(component);
> >
> > + arizona_jack_set_jack(component, NULL, NULL);
> > +
> > priv->core.arizona->dapm = NULL;
> > }
> >
> > diff --git a/sound/soc/codecs/wm8998.c b/sound/soc/codecs/wm8998.c
> > index 1158e9ca40a13..47adbcd80e077 100644
> > --- a/sound/soc/codecs/wm8998.c
> > +++ b/sound/soc/codecs/wm8998.c
> > @@ -1301,6 +1301,8 @@ static void wm8998_component_remove(struct snd_soc_component *component)
> > {
> > struct wm8998_priv *priv = snd_soc_component_get_drvdata(component);
> >
> > + arizona_jack_set_jack(component, NULL, NULL);
> > +
> > priv->core.arizona->dapm = NULL;
> > }
>
> [Severity: Medium]
> When a codec platform device is unbound while a card is bound, can a jack
> IRQ or delayed work use info->micd_pol_gpio or info->hpdet_id_gpio after
> they have been freed?
>
> The codec drivers register their ASoC components with
> devm_snd_soc_register_component(), whereas their platform_driver.remove
> callbacks call arizona_jack_codec_dev_remove() directly. Because
> snd_soc_card_bind() creates a stateless device link, unbinding the codec
> runs platform_driver.remove before devres_release_all() unregisters the
> component and invokes component_remove:
>
> CPU0 CPU1
> wm5102_remove()
> arizona_jack_codec_dev_remove()
> gpiod_put(info->micd_pol_gpio)
> gpiod_put(info->hpdet_id_gpio)
> arizona_hpdet_irq()
> gpiod_set_value_cansleep(
> info->hpdet_id_gpio, 0)
> <-- use-after-free
> devres_release_all()
> ...
> wm5102_component_remove()
> arizona_jack_set_jack(..., NULL, NULL)
>
> Calling arizona_jack_disable_jack_detect() inside
> arizona_jack_codec_dev_remove() before dropping the GPIO descriptors
> (while keeping the call in component_remove for card unbind) would close
> this window.
>
> [Severity: Medium]
> If the card is unbound and bound again while a jack is plugged in, does
> stale state in struct arizona_priv prevent the newly bound card from
> detecting the jack?
>
> info->last_jackdet is only initialized in arizona_jack_codec_dev_probe(),
> and neither arizona_jack_disable_jack_detect() nor
> arizona_jack_enable_jack_detect() resets info->last_jackdet or
> info->hpdet_done. When the card is rebound and sets a new snd_soc_jack,
> arizona_jackdet() sees val == info->last_jackdet and suppresses the event
> instead of reporting the inserted jack, and stale info->hpdet_done causes
> arizona_identify_headphone() to skip headphone detection.
>
> In addition, if arizona_jack_disable_jack_detect() runs while an HPDET
> measurement is in flight (info->hpdet_active is true), freeing
> ARIZONA_IRQ_HPDET prevents arizona_hpdet_irq() from clearing
> arizona->hpdet_clamp and dropping the pm_runtime_get_sync() reference
> taken when starting HPDET.
>
> --
> Sashiko AI review · https://sashiko.dev/#/patchset/20261124-arizona-jack-remove-v1-1-11d8ec426a41@gmail.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-24 6:09 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 5:02 [PATCH] ASoC: arizona: Disable jack detection when the component is removed Suyuan Wang via B4 Relay
2026-11-24 5:17 ` sashiko-bot
2026-11-24 6:09 ` Wong Steve
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox