When probing an HD-audio codec, hda_codec_probe() allocates the codec via hda_codec_device_init() -> snd_hda_codec_device_init() -> snd_hdac_device_init(), which initializes the embedded device reference count (kref == 1) and inserts the codec into bus->codec_list and bus->caddr_tbl[] without taking an additional device reference. hda_codec_probe() then calls hda_codec_load_module(), which calls snd_hdac_device_register(&codec->core). If snd_hdac_device_register() fails (either device_add() fails or hda_widget_sysfs_init() fails and rolls back via device_del()), hda_codec_load_module() calls put_device(&codec->core.dev) and returns the negative error code. That put_device() drops the sole reference and frees codec via snd_hda_codec_dev_release(). Back in hda_codec_probe() -- the sole caller of hda_codec_load_module() -- any negative return value enters the `out:` error path, which calls snd_hdac_device_unregister(&codec->core) and put_device(&codec->core.dev) a second time on the already-freed codec structure. Exercising hda_codec_probe_bus() in an x86_64 KASAN QEMU guest when snd_hdac_device_register() fails with -EEXIST reproduces the use-after-free and refcount underflow: hdaudio ehdaudio0D0: failed to register hdac device BUG: KASAN: slab-use-after-free in snd_hdac_device_unregister Call Trace: snd_hdac_device_unregister hda_codec_probe_bus followed immediately by: BUG: KASAN: slab-use-after-free in kobject_put refcount_t: underflow; use-after-free. Remove the redundant put_device(&codec->core.dev) call from hda_codec_load_module() so hda_codec_probe()'s `out:` handler owns device unregistration and the final put_device() uniformly across all error paths, matching how snd_hda_codec_configure() handles snd_hdac_device_register() failures. Fixes: c344ef36dbc2 ("ASoC: SOF: Intel: hda-codec: Delay the codec device registration") Cc: stable@vger.kernel.org Signed-off-by: Yuansheng Yang <0xbeac@gmail.com> --- Changes in v2: - Update Fixes tag from 9c25af250214 to c344ef36dbc2 ("ASoC: SOF: Intel: hda-codec: Delay the codec device registration"), which moved snd_hdac_device_register() and put_device() into hda_codec_load_module(). - Trim the KASAN backtrace in the commit message (drop hex offsets, addresses, PIDs, and Allocated/Freed sections). Testing: - Compiled sound/soc/sof/intel/hda-codec.o with W=1 on x86_64 (CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC=y, CONFIG_KASAN=y) with 0 warnings and 0 errors. - Verified in an x86_64 KASAN QEMU guest using a reproducer module that triggers hda_codec_probe_bus() when snd_hdac_device_register() fails with -EEXIST: before this fix it reliably triggered KASAN slab-use-after-free and refcount_t underflow; with this fix, 10 consecutive iterations returned -EEXIST cleanly with 0 KASAN, BUG, or WARNING messages and clean bus teardown on unload. sound/soc/sof/intel/hda-codec.c | 1 - 1 file changed, 1 deletion(-) diff --git a/sound/soc/sof/intel/hda-codec.c b/sound/soc/sof/intel/hda-codec.c index a2b2ecf1e..a0d97f4d5 100644 --- a/sound/soc/sof/intel/hda-codec.c +++ b/sound/soc/sof/intel/hda-codec.c @@ -57,7 +57,6 @@ static int hda_codec_load_module(struct hda_codec *codec) ret = snd_hdac_device_register(&codec->core); if (ret) { dev_err(&codec->core.dev, "failed to register hdac device\n"); - put_device(&codec->core.dev); return ret; }