mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ASoC: SDCA: fix NULL pointer dereference in sdca_dev_unregister_functions
@ 2026-06-11  2:37 Kean Ren
  2026-06-11  9:07 ` Charles Keepax
  2026-06-11 14:55 ` Mark Brown
  0 siblings, 2 replies; 3+ messages in thread
From: Kean Ren @ 2026-06-11  2:37 UTC (permalink / raw)
  To: Charles Keepax
  Cc: Maciej Strozek, Bard Liao, Pierre-Louis Bossart, Liam Girdwood,
	Mark Brown, Jaroslav Kysela, Takashi Iwai, Vinod Koul,
	Peter Ujfalusi, linux-sound, patches, linux-kernel, Kean Ren

sdca_dev_unregister_functions() iterates over all SDCA function
descriptors and calls sdca_dev_unregister() on each func_dev without
checking for NULL. When a function registration has failed partway
through, or the device cleanup races with probe deferral, func_dev
entries may be NULL, leading to a kernel oops:

  BUG: kernel NULL pointer dereference, address: 0000000000000040
  RIP: 0010:device_del+0x1e/0x3e0
  Call Trace:
   sdca_dev_unregister_functions+0x37/0x60 [snd_soc_sdca]
   release_nodes+0x35/0xb0
   devres_release_all+0x90/0x100
   device_unbind_cleanup+0xe/0x80
   device_release_driver_internal+0x1c1/0x200
   bus_remove_device+0xc6/0x130
   device_del+0x161/0x3e0
   device_unregister+0x17/0x60
   sdw_delete_slave+0xb6/0xd0 [soundwire_bus]
   sdw_bus_master_delete+0x1e/0x50 [soundwire_bus]
   ...
   sof_probe_work+0x19/0x30 [snd_sof]

This was observed on a Lenovo ThinkPad X1 Carbon G14 (Panther Lake)
with the SOF audio driver probe failing due to missing Panther Lake
firmware, causing the subsequent cleanup of SoundWire devices to
trigger the crash.

Fix this with three changes:

1) Add a NULL guard in sdca_dev_unregister() so that callers do not
   need to pre-validate the pointer (defense in depth).

2) In sdca_dev_unregister_functions(), skip NULL func_dev entries
   and clear func_dev to NULL after unregistration, making the
   function idempotent and safe against double-invocation.

3) In sdca_dev_register_functions(), roll back all previously
   registered functions when a later one fails, so the function
   array is never left in a partially-populated state.

Fixes: 4496d1c65bad ("ASoC: SDCA: add function devices")
Signed-off-by: Kean Ren <rh_king@163.com>
---
 sound/soc/sdca/sdca_function_device.c | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/sound/soc/sdca/sdca_function_device.c b/sound/soc/sdca/sdca_function_device.c
index feacfbc6a518..b5ca98283a88 100644
--- a/sound/soc/sdca/sdca_function_device.c
+++ b/sound/soc/sdca/sdca_function_device.c
@@ -82,6 +82,9 @@ static struct sdca_dev *sdca_dev_register(struct device *parent,
 
 static void sdca_dev_unregister(struct sdca_dev *sdev)
 {
+	if (!sdev)
+		return;
+
 	auxiliary_device_delete(&sdev->auxdev);
 	auxiliary_device_uninit(&sdev->auxdev);
 }
@@ -90,14 +93,24 @@ int sdca_dev_register_functions(struct sdw_slave *slave)
 {
 	struct sdca_device_data *sdca_data = &slave->sdca_data;
 	int i;
+	int ret;
 
 	for (i = 0; i < sdca_data->num_functions; i++) {
 		struct sdca_dev *func_dev;
 
 		func_dev = sdca_dev_register(&slave->dev,
 					     &sdca_data->function[i]);
-		if (IS_ERR(func_dev))
-			return PTR_ERR(func_dev);
+		if (IS_ERR(func_dev)) {
+			ret = PTR_ERR(func_dev);
+			/*
+			 * Unregister functions that were successfully
+			 * registered before this failure. This also
+			 * sets func_dev to NULL so the caller will not
+			 * try to unregister them again.
+			 */
+			sdca_dev_unregister_functions(slave);
+			return ret;
+		}
 
 		sdca_data->function[i].func_dev = func_dev;
 	}
@@ -111,7 +124,12 @@ void sdca_dev_unregister_functions(struct sdw_slave *slave)
 	struct sdca_device_data *sdca_data = &slave->sdca_data;
 	int i;
 
-	for (i = 0; i < sdca_data->num_functions; i++)
+	for (i = 0; i < sdca_data->num_functions; i++) {
+		if (!sdca_data->function[i].func_dev)
+			continue;
+
 		sdca_dev_unregister(sdca_data->function[i].func_dev);
+		sdca_data->function[i].func_dev = NULL;
+	}
 }
 EXPORT_SYMBOL_NS(sdca_dev_unregister_functions, "SND_SOC_SDCA");
-- 
2.47.3


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] ASoC: SDCA: fix NULL pointer dereference in sdca_dev_unregister_functions
  2026-06-11  2:37 [PATCH] ASoC: SDCA: fix NULL pointer dereference in sdca_dev_unregister_functions Kean Ren
@ 2026-06-11  9:07 ` Charles Keepax
  2026-06-11 14:55 ` Mark Brown
  1 sibling, 0 replies; 3+ messages in thread
From: Charles Keepax @ 2026-06-11  9:07 UTC (permalink / raw)
  To: Kean Ren
  Cc: Maciej Strozek, Bard Liao, Pierre-Louis Bossart, Liam Girdwood,
	Mark Brown, Jaroslav Kysela, Takashi Iwai, Vinod Koul,
	Peter Ujfalusi, linux-sound, patches, linux-kernel

On Thu, Jun 11, 2026 at 10:37:57AM +0800, Kean Ren wrote:
> sdca_dev_unregister_functions() iterates over all SDCA function
> descriptors and calls sdca_dev_unregister() on each func_dev without
> checking for NULL. When a function registration has failed partway
> through, or the device cleanup races with probe deferral, func_dev
> entries may be NULL, leading to a kernel oops:
> 
>   BUG: kernel NULL pointer dereference, address: 0000000000000040
>   RIP: 0010:device_del+0x1e/0x3e0
>   Call Trace:
>    sdca_dev_unregister_functions+0x37/0x60 [snd_soc_sdca]
>    release_nodes+0x35/0xb0
>    devres_release_all+0x90/0x100
>    device_unbind_cleanup+0xe/0x80
>    device_release_driver_internal+0x1c1/0x200
>    bus_remove_device+0xc6/0x130
>    device_del+0x161/0x3e0
>    device_unregister+0x17/0x60
>    sdw_delete_slave+0xb6/0xd0 [soundwire_bus]
>    sdw_bus_master_delete+0x1e/0x50 [soundwire_bus]
>    ...
>    sof_probe_work+0x19/0x30 [snd_sof]
> 
> This was observed on a Lenovo ThinkPad X1 Carbon G14 (Panther Lake)
> with the SOF audio driver probe failing due to missing Panther Lake
> firmware, causing the subsequent cleanup of SoundWire devices to
> trigger the crash.
> 
> Fix this with three changes:
> 
> 1) Add a NULL guard in sdca_dev_unregister() so that callers do not
>    need to pre-validate the pointer (defense in depth).
> 
> 2) In sdca_dev_unregister_functions(), skip NULL func_dev entries
>    and clear func_dev to NULL after unregistration, making the
>    function idempotent and safe against double-invocation.
> 
> 3) In sdca_dev_register_functions(), roll back all previously
>    registered functions when a later one fails, so the function
>    array is never left in a partially-populated state.
> 
> Fixes: 4496d1c65bad ("ASoC: SDCA: add function devices")
> Signed-off-by: Kean Ren <rh_king@163.com>
> ---

Reviewed-by: Charles Keepax <ckeepax@opensource.cirrus.com>

Thanks,
Charles

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] ASoC: SDCA: fix NULL pointer dereference in sdca_dev_unregister_functions
  2026-06-11  2:37 [PATCH] ASoC: SDCA: fix NULL pointer dereference in sdca_dev_unregister_functions Kean Ren
  2026-06-11  9:07 ` Charles Keepax
@ 2026-06-11 14:55 ` Mark Brown
  1 sibling, 0 replies; 3+ messages in thread
From: Mark Brown @ 2026-06-11 14:55 UTC (permalink / raw)
  To: Charles Keepax, Kean Ren
  Cc: Maciej Strozek, Bard Liao, Pierre-Louis Bossart, Liam Girdwood,
	Jaroslav Kysela, Takashi Iwai, Vinod Koul, Peter Ujfalusi,
	linux-sound, patches, linux-kernel

On Thu, 11 Jun 2026 10:37:57 +0800, Kean Ren wrote:
> ASoC: SDCA: fix NULL pointer dereference in sdca_dev_unregister_functions

Applied to

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-7.1

Thanks!

[1/1] ASoC: SDCA: fix NULL pointer dereference in sdca_dev_unregister_functions
      https://git.kernel.org/broonie/sound/c/e4c60a1d4b6c

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-06-11 18:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-06-11  2:37 [PATCH] ASoC: SDCA: fix NULL pointer dereference in sdca_dev_unregister_functions Kean Ren
2026-06-11  9:07 ` Charles Keepax
2026-06-11 14:55 ` Mark Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®