Skip to content

Commit e4c60a1

Browse files
kean0048broonie
authored andcommitted
ASoC: SDCA: fix NULL pointer dereference in sdca_dev_unregister_functions
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: 4496d1c ("ASoC: SDCA: add function devices") Signed-off-by: Kean Ren <rh_king@163.com> Reviewed-by: Charles Keepax <ckeepax@opensource.cirrus.com> Link: https://patch.msgid.link/20260611023757.1553960-1-rh_king@163.com Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 6ad3914 commit e4c60a1

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

sound/soc/sdca/sdca_function_device.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ static struct sdca_dev *sdca_dev_register(struct device *parent,
8282

8383
static void sdca_dev_unregister(struct sdca_dev *sdev)
8484
{
85+
if (!sdev)
86+
return;
87+
8588
auxiliary_device_delete(&sdev->auxdev);
8689
auxiliary_device_uninit(&sdev->auxdev);
8790
}
@@ -90,14 +93,24 @@ int sdca_dev_register_functions(struct sdw_slave *slave)
9093
{
9194
struct sdca_device_data *sdca_data = &slave->sdca_data;
9295
int i;
96+
int ret;
9397

9498
for (i = 0; i < sdca_data->num_functions; i++) {
9599
struct sdca_dev *func_dev;
96100

97101
func_dev = sdca_dev_register(&slave->dev,
98102
&sdca_data->function[i]);
99-
if (IS_ERR(func_dev))
100-
return PTR_ERR(func_dev);
103+
if (IS_ERR(func_dev)) {
104+
ret = PTR_ERR(func_dev);
105+
/*
106+
* Unregister functions that were successfully
107+
* registered before this failure. This also
108+
* sets func_dev to NULL so the caller will not
109+
* try to unregister them again.
110+
*/
111+
sdca_dev_unregister_functions(slave);
112+
return ret;
113+
}
101114

102115
sdca_data->function[i].func_dev = func_dev;
103116
}
@@ -111,7 +124,12 @@ void sdca_dev_unregister_functions(struct sdw_slave *slave)
111124
struct sdca_device_data *sdca_data = &slave->sdca_data;
112125
int i;
113126

114-
for (i = 0; i < sdca_data->num_functions; i++)
127+
for (i = 0; i < sdca_data->num_functions; i++) {
128+
if (!sdca_data->function[i].func_dev)
129+
continue;
130+
115131
sdca_dev_unregister(sdca_data->function[i].func_dev);
132+
sdca_data->function[i].func_dev = NULL;
133+
}
116134
}
117135
EXPORT_SYMBOL_NS(sdca_dev_unregister_functions, "SND_SOC_SDCA");

0 commit comments

Comments
 (0)