All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yuansheng Yang <0xbeac@gmail.com>
To: Liam Girdwood <lgirdwood@gmail.com>,
	Peter Ujfalusi <peter.ujfalusi@linux.intel.com>,
	Bard Liao <yung-chuan.liao@linux.intel.com>,
	Daniel Baluta <daniel.baluta@nxp.com>,
	Mark Brown <broonie@kernel.org>
Cc: Kai Vehmanen <kai.vehmanen@linux.intel.com>,
	Pierre-Louis Bossart <pierre-louis.bossart@linux.dev>,
	Vijendar Mukunda <Vijendar.Mukunda@amd.com>,
	Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.com>,
	sound-open-firmware@alsa-project.org,
	linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: [PATCH] ASoC: SOF: Intel: hda-codec: fix double put_device() on registration error
Date: Fri,  4 Dec 2026 03:00:51 +0800	[thread overview]
Message-ID: <20261203190051.456176-1-0xbeac@gmail.com> (raw)

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+0xc6/0xe0
  Read of size 1 at addr ffff88800e15803c by task insmod/342
  Call Trace:
   snd_hdac_device_unregister+0xc6/0xe0
   hda_codec_probe_bus+0x4d7/0x600
  Allocated by task 342:
   snd_hda_codec_device_init+0x139/0x8e0
   hda_codec_probe_bus+0x2c9/0x600
  Freed by task 342:
   kfree+0x154/0x420
   device_release+0xd2/0x270
   kobject_put+0x1b8/0x340
   put_device+0x1f/0x30
   hda_codec_probe_bus.cold+0x1c/0xbe

followed immediately by:

  BUG: KASAN: slab-use-after-free in kobject_put+0x81/0x340
  Write of size 4 at addr ffff88800e158038 by task insmod/342
  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: 9c25af250214 ("ASoC: SOF: Intel: fix page fault at probe if i915 init fails")
Cc: stable@vger.kernel.org
Signed-off-by: Yuansheng Yang <0xbeac@gmail.com>
---
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;
 	}
 

             reply	other threads:[~2026-12-03 19:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 19:00 Yuansheng Yang [this message]
2026-12-03 19:23 ` [PATCH] ASoC: SOF: Intel: hda-codec: fix double put_device() on registration error sashiko-bot
2026-12-04 10:34 ` Kai Vehmanen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203190051.456176-1-0xbeac@gmail.com \
    --to=0xbeac@gmail.com \
    --cc=Vijendar.Mukunda@amd.com \
    --cc=broonie@kernel.org \
    --cc=daniel.baluta@nxp.com \
    --cc=kai.vehmanen@linux.intel.com \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=peter.ujfalusi@linux.intel.com \
    --cc=pierre-louis.bossart@linux.dev \
    --cc=sound-open-firmware@alsa-project.org \
    --cc=stable@vger.kernel.org \
    --cc=tiwai@suse.com \
    --cc=yung-chuan.liao@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.