From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796366854; b=2dNvHTlrmfGUoAqd00/yk1RsouplcccmAY1np3irpR2KF/ZhTjJrn+ddISDmjPGWZ0+e cjOqth5vfTg8ej2uncpHMR+TteqoI4BvFcvdUD7Omqsj/JkwF0A6y4F74zWLVMedRBxJA cIkcw6Gtaar7bycWk3heGfi0qXMmhlj5bfkXMucVKbDN9ucR2QyaLHNXtqoa42rbZXf1a oJOQJ1UqUuTV920QFf5ne3VmLHG/PBfGtRMxNeWYSWa3sOgFA5Bt1tAL/2G930LG1yiHk ubjMmBXZ+d9Ku14sDt9Adk0DqdHqol8tY/fmnsJF2RGNy33ikxituyQ75a6S+u6lulA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796366854; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=7v6lrVb9q8Y4IRLG92sfoTZjz2AQiXvErjF8UcL2j18=; b=XYD+eHDvISQelQvi0xSR81SE9jpD5fR0ke4o0bPwCZOvaLFNEbjZCv5pqR4LXpBLH6Qz lahCT0meKnvmMsBBRTKQT6nL4jguOGsnJX/RmzoZWCG6FRVGXyLvjNL5eoUjw5psPpt5F zZLJv8njBdjUoeEVEWM7yPwrwY1f90k2Vsxb2dtgl1TtsDBxmCMhxfOhG18CVeyNbIBbe 11TpwdxUwqDjJTECPIU11wemqNvNqy6rAuyySgxEtlJ0PPQeIWjp7DiWqohNECKbXuSyM LoqIGeiiUZnZjGDgWtGyzKzOvq7tKFz+gSWgZF+17amSePRnj6iyDMnjafKlnJG+qtA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=ZHeI1cMh; arc=none smtp.remote-ip=209.85.214.172 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 11A41606A322 for ; Fri, 4 Dec 2026 06:47:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 11A41606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=ZHeI1cMh Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2f11330e550so4049545ad.0 for ; Thu, 03 Dec 2026 22:47:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796366853; x=1796971653; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=7v6lrVb9q8Y4IRLG92sfoTZjz2AQiXvErjF8UcL2j18=; b=ZHeI1cMhMtYfrysfaprR3wxWMdAfcIux6yoDVDwrcEpRqgRFIGjloYT74ePc2soCiB Nhb+rmSXW2bFj8lvMBxg+z7VoHjbSxE5JLuNN84B3EMu+sD4SGsTWxroPgwBHLRX8hmY uJ6TICNEqlSvXqhwljvGoLz54ZqEXFw29mjMzG4Y0cm999Nyy+wMPGSGCzWPidMtPAiq 6VIrDeA8oLWgucYMRwMc5TRBvRmMZC1askCjfb24OFt9OMnH+ULa08pgLtXNWkwqbAdG Ur0K1rj6VKoYM1kqUqNrZ4ixVcADISHLfxv3+xfqXtr5mN/q22dr2dDObd42ZyLPaojZ gpNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796366853; x=1796971653; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=7v6lrVb9q8Y4IRLG92sfoTZjz2AQiXvErjF8UcL2j18=; b=TKbewfF5m5eVp5NUjqlgBQndiaXuWhrOMV1X+XPJ6KpjGgaoVPBDV2zPxOzBpXqIu6 HOH1JLf85S3iK5aakvDzhnNX/1moY3ApvrL9cpmXuEj7TWgjmYTfXVafpuXcjPZdbF6v zJvacH5X7BoEymyf3xMsJKeWd+Kt7wtZVewp6xwSl8WY6iOyIHazawiegB72viOn/Vij XcQmyQO8R0LxPsCUGRzacpwsbSODTVA1INS/LEStq7+KVyfS8GUBfDD7XmLP6ixV5xgi IfaUriLUntl+COLMy0gH8Xd/i1On+pHk7WC2uJF9sl71+txVHjbYmTRmZT5O+RRY6t1l SY8A== X-Forwarded-Encrypted: i=1; AL+hErHZLxOgpcdDo0PGm0JqEkHNC+1nm2CbWUaZx5B2pkVh3na1R2Wo28xD0pdL8OW2DGpKyCllHe9cCFIkgnA=@vger.kernel.org X-Gm-Message-State: APuU4Z+vDOvwpADOK8yvl4tcJ4s6ttzjcHiH0LaEE0jCm13+ry5+simh LibjEc6xTEFStICdGZ8Goh0NhoYd0OVr4fr+2xCP5Hxyk49zqa8Unn1j X-Gm-Gg: AZFdTZfmQEh29JEIMNLpfj8C6spTeQOWvq6qlmQcmQZFnaJHKAz/bG95jzblaaQ4VHA PI/1vMzEBo+aqfbuYHTuX2y86Cc8hLUDsoIIcAEgp6Wn3ab3c7X9COnW22/7OuCuGLQNX0E19Zc nQgU7gpPME3pURc4h2t/kSraav0Su66P2Zng/n2Ec0qnaCrFEE4p2zOS88gCq1BSmdfNp6ZE2BC 08a1CnGBXU4c4G8bOAu5ko75OHsWr1O4DHtAUO4gkbY+gHusFZUxS8Hs5nwZFHSDB7qWWc2s6QT YnOs5l+uDtb5WtzmyOXhJmaDlCpF/cydHIAN+sKG01ZqOWJkjkqd1uJGZtIVue0YkYwr980jTKO FSyTdpK8yIFYIQFAVt62V//f6Q2Q1b7+s5kxfVhPIcsS0AxCeaP0WaL3Hnab0tEFdWmJ4pbwiPv u36rR6C24ueox0ONBtKFkvtltA4YmUskfn7nIRKFbVjuv6x8fMfd+KSTjFZFwU7BJLTgyC++S2c FfnPA51ws5XviRl/V7JLLVmNKPDUtjWQIE+IgehmcTuqQAxObLC47itrdKAOzGuZrONyEGA0LRc tKOe4r9MeCOXAkE= X-Received: by 2002:a17:903:1904:b0:2f0:3c38:9133 with SMTP id d9443c01a7336-2f1ba1ca3c9mr8970895ad.43.1796366853235; Thu, 03 Dec 2026 22:47:33 -0800 (PST) Received: from dell ([58.246.183.50]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f1b78faa54sm5893565ad.24.2026.12.03.22.47.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 22:47:32 -0800 (PST) From: Yuansheng Yang <0xbeac@gmail.com> To: Liam Girdwood , Peter Ujfalusi , Bard Liao , Daniel Baluta , Mark Brown Cc: Kai Vehmanen , Pierre-Louis Bossart , Vijendar Mukunda , Jaroslav Kysela , Takashi Iwai , sound-open-firmware@alsa-project.org, linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH v2] ASoC: SOF: Intel: hda-codec: fix double put_device() on registration error Date: Fri, 4 Dec 2026 14:47:17 +0800 Message-ID: <20261204064717.851431-1-0xbeac@gmail.com> X-Mailer: git-send-email 2.53.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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; }