Re: [PATCH] Re: sound: timer: snd_utimer_fops lacks .owner, so an open user timer fd does not pin snd-timer

From: Takashi Iwai

Date: Wed Nov 25 2026 - 09:18:00 EST


On Wed, 25 Nov 2026 13:48:16 +0100,
徐晗翔 wrote:
>
> snd_utimer_fops had no .owner, so the anonymous-inode fd created by
> SNDRV_TIMER_IOCTL_CREATE did not hold a reference to the snd-timer
> module: the module could be unloaded while the fd was open, and a
> later SNDRV_TIMER_IOCTL_TRIGGER or close(fd) executed
> snd_utimer_trigger()/snd_utimer_release() from unloaded module
> text. (utimer->timer->module is only consulted by snd_timer_open(),
> which the fd never goes through.)
>
> Add .owner = THIS_MODULE so anon_inode_getfile() pins the module
> for the lifetime of the fd, as upstream does.

Already fixed by the upstream commit f9e882ac8257.


thanks,

Takashi

> ---
> sound/core/timer.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/sound/core/timer.c b/sound/core/timer.c
> --- a/sound/core/timer.c
> +++ b/sound/core/timer.c
> @@ -2173,6 +2173,7 @@
> }
>
> static const struct file_operations snd_utimer_fops = {
> + .owner = THIS_MODULE,
> .llseek = noop_llseek,
> .release = snd_utimer_release,
> .unlocked_ioctl = snd_utimer_ioctl,
>
>
> > -----原始邮件-----
> > 发件人: 徐晗翔 <xuhx@xxxxxxxxxxx>
> > 发送时间:2026-11-22 15:57:29 (星期日)
> > 收件人: perex@xxxxxxxx, tiwai@xxxxxxxx
> > 抄送: linux-sound@xxxxxxxxxxxxxxx, linux-kernel@xxxxxxxxxxxxxxx
> > 主题: sound: timer: snd_utimer_fops lacks .owner, so an open user timer fd does not pin snd-timer
> >
> > SNDRV_TIMER_IOCTL_CREATE creates a user-driven timer and returns an
> > anonymous inode fd whose file_operations is snd_utimer_fops. That
> > structure has no .owner = THIS_MODULE. anon_inode_getfile() only calls
> > try_module_get(fops->owner) when owner is non-NULL, so the fd does not
> > hold a reference to the snd-timer module. The module can be unloaded
> > while the fd is still open; a later ioctl(SNDRV_TIMER_IOCTL_TRIGGER) or
> > close(fd) calls snd_utimer_trigger()/snd_utimer_release() in unloaded
> > module text.
> >
> > The module field does not save this path: utimer->timer->module is set
> > to THIS_MODULE in snd_timer_new(), but try_module_get() is only called by
> > snd_timer_open(), and a utimer fd never goes through snd_timer_open().
> > snd_utimer_create() also calls snd_timer_new(NULL, ...), so with card ==
> > NULL it does not go through snd_device_new(); snd_timer_global_register()
> > (timer.c:1188-1194) only does the device-list insertion and takes no
> > module reference.
> >
> > This is a static report: the missing .owner and the anon_inode lifetime
> > are unambiguous, but I did not build a dynamic reproducer (it requires
> > CONFIG_SND_TIMER=m together with CONFIG_SND_UTIMER=y).
> >
> > The detailed description:
> >
> > * Test environment / affected version
> >
> > Source analysis against mainline 7.3; no dynamic reproducer was run
> > for this case.
> >
> > * Root cause
> >
> > sound/core/timer.c:2175-2179:
> >
> > static const struct file_operations snd_utimer_fops = {
> > .llseek = noop_llseek,
> > .release = snd_utimer_release,
> > .unlocked_ioctl = snd_utimer_ioctl,
> > };
> >
> > Missing .owner = THIS_MODULE.
> >
> > sound/core/timer.c:2298:
> >
> > timer_fd = anon_inode_getfd(utimer->name, &snd_utimer_fops, utimer,
> > O_RDWR | O_CLOEXEC);
> >
> > fs/anon_inodes.c:141-142:
> >
> > if (fops->owner && !try_module_get(fops->owner))
> > return ERR_PTR(-ENOENT);
> >
> > owner == NULL -> no module reference. utimer->timer->module =
> > THIS_MODULE (timer.c:2252) is only consulted by snd_timer_open(),
> > which the fd does not use; snd_timer_global_register() is just
> > list_add_tail() and does not take a module reference.
> >
> > Upstream snd_utimer_fops has .owner = THIS_MODULE.
> >
> > * Failure model
> >
> > 1. user ioctl SNDRV_TIMER_IOCTL_CREATE -> snd_utimer_create() ->
> > anon_inode_getfd() with the ownerless fops;
> > 2. no other snd-timer user -> rmmod snd-timer succeeds;
> > 3. user calls ioctl(SNDRV_TIMER_IOCTL_TRIGGER) or close(fd);
> > 4. snd_utimer_ioctl()/snd_utimer_release() execute from the unloaded
> > module, and dereference utimer->timer / call snd_timer_interrupt().
> >
> > This is a module-lifetime race, not a data race on a shared field; the
> > concurrency is rmmod vs fd operation.
> >
> > * Trigger model
> >
> > - CONFIG_SND_TIMER=m (module) and CONFIG_SND_UTIMER=y;
> > - a user creates a utimer fd via /dev/snd/timer;
> > - the module is unloaded before the fd is closed;
> > - the fd is used (trigger ioctl) or closed.
> >
> > Requires local access to /dev/snd/timer; no hardware.
> >
> > * Impact
> >
> > - execution of unloaded module code: UAF execution, crash;
> > - if the module address range is reused, potential code execution;
> > - at minimum a local DoS / module-unload race.
> >
> > * Suggested direction
> >
> > Add .owner = THIS_MODULE to snd_utimer_fops. Also consider whether the
> > anon-inode path should hold an explicit module reference for clarity;
> > .owner is the standard fix and matches upstream.
> >
> >
> > Please keep me in Cc; I am not subscribed to the lists.
> >
> > Regards,
> > Hanxiang Xu
> > xuhx@xxxxxxxxxxx