Re: [PATCH net v3] ptp: vmclock: keep the device state until the last file is closed

From: David Woodhouse

Date: Wed Nov 25 2026 - 10:01:58 EST


On Wed, 2026-11-25 at 12:38 +0100, Paolo Abeni wrote:
> On 11/24/26 20:44, David Woodhouse wrote:
> > On Thu, 2026-11-19 at 06:15 +0000, Chad Aldreda wrote:
> > > vmclock_probe() allocates struct vmclock_state with devm_kzalloc_obj()
> > > and maps the shared page with devm_memremap(), so both are released
> > > when the driver is unbound, but an open /dev/vmclockN keeps using them
> > > through its file state. After an unbind, read() runs on the freed
> > > struct and reads through the released mapping, and a poll() in
> > > progress sleeps on a wait queue in the freed struct. KASAN reports a
> > > slab-use-after-free in vmclock_miscdev_read(). In testing, the same
> > > read() faulted on the unmapped page once the device had been bound
> > > again.
> > >
> > > Allocate the state with kzalloc_obj() and map the page with memremap(),
> > > and release both on the last reference: open() takes a reference and
> > > release() drops it, and a devm action drops the device's own reference
> > > after vmclock_remove() has called misc_deregister(). misc_open() calls
> > > open() with misc_mtx held, and misc_deregister() takes misc_mtx, so
> > > open() always runs while the device's reference is held.
> > >
> > > vmclock_remove() now marks the device as removed and wakes every waiter
> > > on the wait queue. A read() or mmap() on a file that is still open fails
> > > with -ENODEV once it sees the mark. On a device that supports
> > > notifications, poll() returns EPOLLHUP | EPOLLERR, including a poll()
> > > that was already waiting; it tests the mark after poll_wait(), so it
> > > cannot miss the wake-up. The wake-up uses wake_up_interruptible_all(),
> > > because no later notification will reach a waiter that the remove-time
> > > wake-up skips, such as a second epoll instance that registered the file
> > > with EPOLLEXCLUSIVE. Without notifications poll() already returns
> > > POLLHUP.
> > >
> > > Separately from the report below, the bug was also found by a
> > > Coccinelle script that reports device nodes in devm-allocated memory,
> > > posted at
> > > https://lore.kernel.org/r/20261106193517.3267013-2-ca@xxxxxxxxxxxxxx,
> > > and reproduced in QEMU 10.2.4 with -device vmclock. There is no access
> > > to EC2 hardware with this device, so the fix has only been tested in
> > > QEMU. QEMU's device has no notifications, so the poll() path was tested
> > > with a local change that lets poll() wait without them.
> > >
> > > Fixes: 205032724226 ("ptp: Add support for the AMZNC10C 'vmclock' device")
> > > Reported-by: Shuangpeng Bai <shuangpeng.kernel@xxxxxxxxx>
> > > Closes: https://lore.kernel.org/all/178144969601.60470.14493569608271069160@xxxxxxxxx/
> > > Cc: stable@xxxxxxxxxxxxxxx # 7.0+
> > > Signed-off-by: Chad Aldreda <ca@xxxxxxxxxxxxxx>
> >
> > Reviewed-by: David Woodhouse <dwmw@xxxxxxxxxxxx>
> >
> > We did have a plan to pull the PHC-only drivers out into drivers/phc
> > and leave only the *real* PTP in drivers/ptp. I kind of stalled on that
> > because I wasn't sure I had consensus especially from Richard and the
> > net maintainers. Shall I revive that and repost it?
>
> I'm sorry but that conversation is way of my context window. Do you have a
> pointer to the prior discussion? why such separation would be useful?

I think
https://lore.kernel.org/netdev/20251127083610.6b66a728@xxxxxxxxxx/ is
the key answer to that.

The latest update (which I'm suggesting I should dust off) is at
https://lore.kernel.org/all/14969e704fb8e70deb549b2e1c8670f6756a8da7.camel@xxxxxxxxxxxxx/

Attachment: smime.p7s
Description: S/MIME cryptographic signature