From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796181596; b=ZIdVn88HzpPbDp5KCr6lBZ6C9lZRjzXJM8OEN+bB2lWpX8uRxSqWS+20IAHnmV2vKxvu 2aF1rO62zgvZhtTwQcJNGAERLaHJYlAfdWz6fh7QAYyz1p5Y4u9JA48rfAVLAltPgxwC2 e99Fg+75EfKEPAyO9LmrJA8sSRHlWISpcoSmRZhL9xgEShOpa1dwmNs/JuhFVJc2+1uyt zCEyl7Wm6wsbFS7m5WRb7L2fBq8ZJzrtH1dpLKheHGAoT37z5r3vjLznMU3FW2/5SnwMc DKkIGO7kv+ApcCByo7dZyt8qD10SxQRGot+EvsO6kvSODog0UUY9OgOQzSZAwnm0YAA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796181596; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=yNwmIb6AKjRC/bCMYVMDuzwbhwfhHxVnHh2nSEF2SEc=; b=M27I7e+O618SuBIBS65zju+ulHrYJV/JMADrC2eepdOwumniUUoOzK6Sxe8zMI2JqVQO buB9JY4Fkd9VlQzFbBUCeZWExoR88Vh3j7b8wBFQ5k0i9siEVX2VGRRYAj9VPq8mpSVDd nfhFA00ooNk3XddyeJ/sBZBIBIsHQ09ObiV8nJJKvGpKTwskFXcky7GtjhS3NhUId8V7I vGUeIxTJHLh+vB5ginMbwAtS+ZyVGgYNsHnyMbD0H3jS7nkB4ICvi5+f72PcPg7rqTH1p PIf1JAQmom0R8z4oRApnAIKmlsf+qV/P3mQdFRhDBOal5aBJJsF/bdxwfqC6PXBYMuQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=TZSGu1+I; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EDD92606A323 for ; Wed, 2 Dec 2026 03:19:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=TZSGu1+I Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8B5B91F000C1; Wed, 2 Dec 2026 03:19:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796181595; bh=yNwmIb6AKjRC/bCMYVMDuzwbhwfhHxVnHh2nSEF2SEc=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=TZSGu1+IY8Xu8PgvfMcZffug0E0jfEGpRIRqgkS8xy8MMirtmoG9pA22SJhPtDkls pyjILS03wa0KsQbDXCv+AjZvsZhQTgDSN7a8sw67HxfBT/CyzBrujf6E18ae34Dgxw zCtPgiYuSX3vzjY1Mtedd8HMqppf1QKkBETKwArbtcIViWhE/lGvlzl+JjAv3qsjk5 4qP0e2Lz52GetIpigeWZkAaHTnhQ0Iuvr0dh2RDnxIv35olH6IayZPCubqvvhf9iTL gyuGMFePDm7E07z8+8iudDxIvXSCawpSt0t4758WhjK7lP1UeTOKF1v0KOMxm+TMUY J6O2QAVC2FsFA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH] ALSA: pcm: free struct snd_pcm only after the last open file is closed Reply-To: sashiko-reviews@lists.linux.dev To: "Hanxiang Xu" Cc: linux-sound@vger.kernel.org In-Reply-To: <20261202103200.3-1-xuhx@hust.edu.cn> References: <20261202103200.3-1-xuhx@hust.edu.cn> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 03:19:55 +0000 X-Mailing-List: linux-sound@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 7 potential issues to consider: - [Critical] ALSA: pcm: unbalanced kref_put() and pcm->card UAF in snd_pcm_open() - [Critical] ALSA: pcm: missing kref_get() on O_APPEND path in snd_pcm_attach_substream() - [High] ALSA: pcm: static snd_pcm_free_up() undeclared in sound/core/pcm_native.c - [High] ALSA: pcm: leaked pcm->ref in snd_pcm_detach_substream() and OSS emulation - [High] ALSA: pcm: struct snd_card UAF in snd_pcm_free_up() from snd_pcm_release() - [High] ALSA: pcm: unreferenced struct snd_pcm in snd_pcm_open() races with free - [High] ALSA: pcm: deferred snd_pcm_free_up() races with sub-driver teardown -- commit ee4fa7452126fcba1071f25254147a73508e58cb Author: Hanxiang Xu ALSA: pcm: free struct snd_pcm only after the last open file is closed This commit adds a kref to struct snd_pcm so that snd_pcm_free() defers teardown and freeing of the PCM instance until both the device entry and all open files have dropped their references. > diff --git a/sound/core/pcm.c b/sound/core/pcm.c > index 6c8274414c226..c59c5e2fa34fa 100644 > --- a/sound/core/pcm.c > +++ b/sound/core/pcm.c > @@ -857,10 +858,10 @@ static void snd_pcm_free_stream(struct snd_pcm_str = * pstr) > #define pcm_call_notify(pcm, call) do {} while (0) > #endif > =20 > -static int snd_pcm_free(struct snd_pcm *pcm) > +static void snd_pcm_free_up(struct kref *ref) > { > - if (!pcm) > - return 0; > + struct snd_pcm *pcm =3D container_of(ref, struct snd_pcm, ref); > + [Severity: High] Does sound/core/pcm_native.c fail to compile because snd_pcm_free_up() is defined static here in sound/core/pcm.c without a declaration in sound/core/pcm_local.h? Both snd_pcm_open() and snd_pcm_release() in sound/core/pcm_native.c pass snd_pcm_free_up to kref_put(). > if (!pcm->internal) { > scoped_guard(mutex, ®ister_mutex) > pcm_call_notify(pcm, n_unregister); > @@ -871,6 +872,13 @@ static int snd_pcm_free(struct snd_pcm *pcm) > snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]); > snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]); > kfree(pcm); > +} > + > +static int snd_pcm_free(struct snd_pcm *pcm) > +{ > + if (!pcm) > + return 0; > + kref_put(&pcm->ref, snd_pcm_free_up); > return 0; > } [Severity: High] Can deferring snd_pcm_free_up() and substream close callbacks past snd_device_free() cause a use-after-free in sub-drivers that free the driver structures backing pcm->private_data and substream->ops as soon as snd_device_free() returns? CPU0 CPU1 codec_release_pcms() snd_device_free(card, pcm->pcm) ... snd_pcm_free() kref_put(&pcm->ref, ...) kfree(pcm) /* frees apcm->info */ snd_pcm_release() ... kref_put(&pcm->ref, snd_pcm_free_up) snd_pcm_free_up() azx_pcm_free() apcm->info->pcm =3D NULL; <-- use-after-free Similarly, snd_emu8000_remove() calls snd_device_free(dev->card, hw->pcm) and immediately frees hw->emu and hw->memhdr, so a later snd_pcm_release() -> snd_pcm_release_substream() -> emu8k_pcm_hw_free() passes a NULL rec->emu->memhdr and an already-freed rec->block to snd_util_mem_free(). > @@ -981,6 +989,7 @@ int snd_pcm_attach_substream(struct snd_pcm *pcm, int= stream, > substream->f_flags =3D file->f_flags; > substream->pid =3D get_pid(task_pid(current)); > pstr->substream_opened++; > + kref_get(&pcm->ref); > *rsubstream =3D substream; > #ifdef CONFIG_SND_PCM_XRUN_DEBUG > substream->xrun_counter =3D 0; [Severity: Critical] Does the O_APPEND path in snd_pcm_attach_substream() return 0 without calling kref_get(&pcm->ref)? sound/core/pcm.c:snd_pcm_attach_substream() { ... if (file->f_flags & O_APPEND) { ... substream->ref_count++; *rsubstream =3D substream; return 0; } ... } When a file opened with O_APPEND is later closed, snd_pcm_release() still calls kref_put(&pcm->ref, snd_pcm_free_up), dropping an unacquired reference and freeing struct snd_pcm while the primary file descriptor is still using it. [Severity: High] Does taking pcm->ref in snd_pcm_attach_substream() without a matching kref_put() in snd_pcm_detach_substream() leak pcm->ref on every OSS PCM open? snd_pcm_oss_open_file() opens playback and capture substreams via snd_pcm_open_substream() -> snd_pcm_attach_substream(), acquiring up to two references on pcm->ref, but snd_pcm_oss_release_file() releases them via snd_pcm_release_substream() -> snd_pcm_detach_substream() without ever calling kref_put(&pcm->ref, snd_pcm_free_up). As a result, pcm->ref never reaches zero and struct snd_pcm and its preallocated DMA buffers are leaked. > diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c > index 897975e43a9bd..4fef3020328fb 100644 > --- a/sound/core/pcm_native.c > +++ b/sound/core/pcm_native.c > @@ -3037,6 +3037,7 @@ static int snd_pcm_open(struct file *file, struct s= nd_pcm *pcm, int stream) > =20 > __error: > module_put(pcm->card->module); > + kref_put(&pcm->ref, snd_pcm_free_up); > __error2: > snd_card_file_remove(pcm->card, file); > __error1: [Severity: Critical] Can jumping to __error in snd_pcm_open() drop pcm->ref when kref_get(&pcm->ref) was never called, and then dereference pcm->card after pcm is freed? If snd_pcm_open_file() fails before the end of snd_pcm_attach_substream() (for instance with -EAGAIN when a substream is busy under O_NONBLOCK, or when the wait loop aborts with -ERESTARTSYS on a signal), kref_get() has not been called, so kref_put(&pcm->ref, snd_pcm_free_up) drops the device entry's reference and frees pcm. Immediately afterwards, __error2 and the caller snd_pcm_playback_open() / snd_pcm_capture_open() dereference pcm->card in snd_card_file_remove(pcm->card, file) and snd_card_unref(pcm->card). Conversely, if snd_pcm_attach_substream() succeeds (calling kref_get()) and substream->ops->open() then returns -EAGAIN on a blocking open, snd_pcm_open_substream() calls snd_pcm_release_substream() without dropping pcm->ref and the wait loop retries snd_pcm_open_file(), leaking one reference on every retry. [Severity: High] Can snd_device_free(card, pcm) free struct snd_pcm before snd_pcm_open() takes a reference on pcm->ref? snd_lookup_minor_data() in snd_pcm_playback_open() and snd_pcm_capture_open() returns a raw struct snd_pcm pointer after dropping sound_mutex, and pcm->ref is not incremented until snd_pcm_attach_substream() succeeds. CPU0 CPU1 snd_pcm_playback_open() pcm =3D snd_lookup_minor_data(...) snd_pcm_open(file, pcm, ...) ... /* substream busy: -EAGAIN */ mutex_unlock(&pcm->open_mutex) /* active file closes */ snd_device_free(card, pcm) snd_pcm_dev_disconnect() snd_pcm_dev_free() snd_pcm_free(pcm) kref_put(..., snd_pcm_free_up) kfree(pcm) schedule() mutex_lock(&pcm->open_mutex) <-- use-after-free Even if pcm is still alive on wakeup, snd_device_free() on a single PCM device leaves pcm->card->shutdown as 0, so a waiter woken by snd_pcm_dev_disconnect() passes the pcm->card->shutdown check and can open a substream on an already-disconnected PCM. > @@ -3065,6 +3066,7 @@ static int snd_pcm_release(struct inode *inode, str= uct file *file) > wake_up(&pcm->open_wait); > module_put(pcm->card->module); > snd_card_file_remove(pcm->card, file); > + kref_put(&pcm->ref, snd_pcm_free_up); > return 0; > } [Severity: High] When the last open PCM file is closed after card disconnect, does snd_card_file_remove() free struct snd_card before kref_put() runs snd_pcm_free_up()? After snd_card_free_when_closed(), the open file holds the last reference on card->card_dev and one reference on pcm->ref. In snd_pcm_release(), snd_card_file_remove(pcm->card, file) calls put_device(&card->card_dev) -> snd_card_do_free(card): snd_card_do_free(card) snd_device_free_all(card) snd_pcm_dev_free() snd_pcm_free(pcm) kref_put(&pcm->ref, snd_pcm_free_up) /* 2 -> 1, deferred */ snd_info_card_free(card) /* frees card->proc_root and pstr->proc_root */ kfree(card) When snd_pcm_release() then executes kref_put(&pcm->ref, snd_pcm_free_up) on the next line, snd_pcm_free_up() runs after module_put() and after card has been freed: snd_pcm_lib_preallocate_free_for_all() -> decrease_allocated_size() locks card->memory_mutex on the freed struct snd_card, snd_pcm_free_stream() -> snd_pcm_stream_proc_done() double frees pstr->proc_root, and snd_pcm_timer_done() accesses card->devices on the freed card. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261202103200.3-1-= xuhx@hust.edu.cn?part=3D1