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=1796181442; b=E3QDY4uOd2co4mPeQyUuSfFNEJ4HnyPmaFEBUcYzKvcMz0KRFk8UBsQEuVIOQSE81Bd9 5/h+IgaIEWRN+J9Ti5T4fApl7AeJK/sn1bh90MzJq77IrOIVMZwIVWVW1XYKwYGBTtNQ6 gY0+ST1J68p+nv1StRwtsX8teTl/yiQascNj9Cg5EDaxm/z37aYQ8FOxVFxJxplQLiSEU NYcPpqvgUTdrCkcx2oVadigg7Sd9e2NhuNPv+kDeuYPMpsdDUCF5uFPwwtCSlwW4vn3SZ ZErul+bG3e8RGEYS+AxLwjjGrnpliu0HJsUY0+RLGCBnjPNYXNLIAsMbRPVRJiPJzow== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796181442; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=A8UWyMjxNDKoNMv0pi9mkD2AXTNBti2WzzO0ThMXWW0=; b=hkMMtdBuRBewmyLLuPa4xRfuG3saVdyajxywoPSZN727N3hkuGXeafLQktKbhGtPwRFb WY5jgH6aQ1OIb6gz1sqoy0p6DHT7Hbp3GjjbwpXswf7+CKqTMA71Zdq/3sz5amz4oH9Ky IeR6RFTKMtfYch0jzSQmLicpDGKn5jRM3XrCErGz1HDPc4CkOc1vwQ9b5TwkfOFF4GTq8 HEdANiT3IDrMiqGmYxVv/N5i9oofGXz20B7qam1S4NBSy2NmI1Y736AlDs0zrEVCNKD/I rjc9XTZsA/BJ1LZswwgGR4q2l2YSjtisseHFRdNPPblMO4FfhCrzh0rguyS/vMWzb5Q== 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=GzTQKWlp; 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 3AD00606A683 for ; Wed, 2 Dec 2026 03:17:21 +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=GzTQKWlp Received: by smtp.kernel.org (Postfix) with ESMTPSA id 64C411F008A0; Wed, 2 Dec 2026 03:17:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796181441; bh=A8UWyMjxNDKoNMv0pi9mkD2AXTNBti2WzzO0ThMXWW0=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=GzTQKWlprlBLFxQZIHu0nTUM/0TOUW2rA6SERPRGTo+cvri+S+Dbp8uqiBGkrNTKB aLS95rqCa9ERWoDrr6O1iXYZ6+tmmKjSGS6slEok6phdKC3cOIQjUSomKKctkrVRHe O/NPVP+HefJCUzdIB0qOzfjQJ7I7KCuvT7pCRii+tRnB7/LjhBz+BnzZh6S/nycCQe xPAkDZRrkF6HiVOlnSEI6I2OZY7YpqGaeaIPa+6z8VK4GKI8HhkKDKg5IzsG4DPB1J MLvF/jkpHnt4DFvNJVm1JTWI2sKi2Xyf0dn8K1uA5bY1kqvEfrXp34H1OUpYa4vyrp TTP5N7o+kOlTw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH] ALSA: hwdep: keep struct snd_hwdep alive until 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: <20261202103000.1-1-xuhx@hust.edu.cn> References: <20261202103000.1-1-xuhx@hust.edu.cn> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 03:17:20 +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 4 potential issues to consider: - [High] ALSA: hwdep: container_of() on pointer member in snd_hwdep_dev_release() - [High] ALSA: hwdep: use-after-free of private_data on open files in snd_hwdep_free() - [Medium] ALSA: hwdep: struct snd_hwdep leak in snd_hwdep_new() on device alloc error - [Medium] ALSA: hwdep: race between minor lookup and get_device() in snd_hwdep_open() -- commit e7fc06a622ec98bf6bee98cb33f2ea175648caa4 Author: Hanxiang Xu ALSA: hwdep: keep struct snd_hwdep alive until the last open file is closed This commit ties the lifetime of struct snd_hwdep to its device reference count by taking a device reference on open, dropping it on close, and deferring kfree(hwdep) to a device release callback. > diff --git a/sound/core/hwdep.c b/sound/core/hwdep.c > index fb9034897c561..bddd74ee1014e 100644 > --- a/sound/core/hwdep.c > +++ b/sound/core/hwdep.c > @@ -84,8 +84,10 @@ static int snd_hwdep_open(struct inode *inode, struct = file * file) > return -ENXIO; > if (hw =3D=3D NULL) > return -ENODEV; > + get_device(hw->dev); [Severity: Medium] Can snd_device_free() drop the last reference on hw->dev before get_device(hw->dev) runs here? snd_lookup_minor_data() and snd_lookup_oss_minor_data() only take a reference on card->card_dev and release sound_mutex / sound_oss_mutex before returning hw. When a sub-driver removes its hwdep device from a live card, card->card_dev does not keep hw alive: CPU0 CPU1 snd_hwdep_open() hw =3D snd_lookup_minor_data(...) lock(&sound_mutex) get_device(&card->card_dev) unlock(&sound_mutex) snd_device_free(card, hw) snd_hwdep_dev_disconnect() snd_hwdep_dev_free() snd_hwdep_free() put_device(hw->dev) get_device(hw->dev) <-- use-after-free [ ... ] > @@ -337,6 +342,13 @@ static const struct file_operations snd_hwdep_f_ops = =3D { > .mmap =3D snd_hwdep_mmap, > }; > =20 > +static void snd_hwdep_dev_release(struct device *dev) > +{ > + struct snd_hwdep *hwdep =3D container_of(dev, struct snd_hwdep, dev); > + > + kfree(hwdep); > +} [Severity: High] Does container_of(dev, struct snd_hwdep, dev) compile or compute a valid struct snd_hwdep pointer here? Since commit 897c8882df58 ("ALSA: hwdep: Don't embed device"), struct snd_hwdep::dev is a struct device * allocated separately by snd_device_alloc(), not an embedded struct device: include/sound/hwdep.h: struct snd_hwdep { ... void *private_data; void (*private_free) (struct snd_hwdep *hwdep); struct device *dev; Passing a pointer member to container_of() trips its compile-time type static_assert, and subtracting offsetof(struct snd_hwdep, dev) from the standalone struct device address passes a bogus pointer to kfree() while leaking both allocations. > + > static void snd_hwdep_free(struct snd_hwdep *hwdep) > { > if (!hwdep) > @@ -344,7 +356,6 @@ static void snd_hwdep_free(struct snd_hwdep *hwdep) > if (hwdep->private_free) > hwdep->private_free(hwdep); > put_device(hwdep->dev); > - kfree(hwdep); > } [Severity: High] Even if struct snd_hwdep stays allocated until the last file is closed, doesn't running hwdep->private_free(hwdep) and returning from snd_device_free() while files are still open leave hw->ops callbacks dereferencing freed hw->private_data? When a sub-driver removes a hwdep device from a live card (for example, snd_emux_free() -> snd_emux_delete_hwdep() -> snd_device_free()), snd_hwdep_dev_disconnect() does not replace open files' file->f_op or wait for ongoing operations to finish, and snd_hwdep_free() immediately runs hwdep->private_free() before the caller frees its private state. Any subsequent ioctl, read, write, poll, mmap or release on an open file still invokes hw->ops with the freed hw->private_data. In addition, a thread woken from hw->open_wait in snd_hwdep_open() only checks hw->card->shutdown (which remains 0 on a live card) and proceeds to call hw->ops.open() after disconnect. > =20 > /** > @@ -392,6 +403,7 @@ int snd_hwdep_new(struct snd_card *card, char *id, in= t device, > snd_hwdep_free(hwdep); > return err; > } > + hwdep->dev->release =3D snd_hwdep_dev_release; [Severity: Medium] Does the snd_device_alloc() error path right above this leak hwdep? When snd_device_alloc(&hwdep->dev, card) fails, hwdep->dev is NULL and hwdep->dev->release has not been set, so snd_hwdep_free(hwdep) calls put_device(NULL) and returns without freeing hwdep. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261202103000.1-1-= xuhx@hust.edu.cn?part=3D1