[PATCH] posix-clock: initialize cdev state before publishing
From: Haoli Zhang
Date: Sun Nov 15 2026 - 22:29:32 EST
cdev_device_add() may expose the cdev before it returns. Initialize
clk->cdev.owner and clk->dev before calling it, so an open racing with
registration takes the matching module and device references.
If cdev_device_add() fails after cdev_add() made the cdev available,
open files may still exist. Mark the clock as a zombie under rwsem before
returning the error, so subsequent read, poll, ioctl, and dynamic clock
operations on those files fail while the caller tears down registration
state. Keep clk->dev valid so the release path can drop any reference
acquired by a successful open.
Fixes: a33121e5487b ("ptp: fix the race between the release of ptp_clock and cdev")
Reported-by: syzbot+667788430769ef9ddf25@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzbot.org/bug?extid=667788430769ef9ddf25
Cc: stable@xxxxxxxxxxxxxxx
Co-developed-by: Jinning Zhang <25031212117@xxxxxxxxxxxxxxxxx>
Signed-off-by: Jinning Zhang <25031212117@xxxxxxxxxxxxxxxxx>
Co-developed-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Haoli Zhang <26031212468@xxxxxxxxxxxxxxxxx>
---
--- a/kernel/time/posix-clock.c
+++ b/kernel/time/posix-clock.c
@@ -164,14 +164,18 @@
init_rwsem(&clk->rwsem);
cdev_init(&clk->cdev, &posix_clock_file_operations);
+ /* cdev_device_add() can expose the cdev before it returns. */
+ clk->cdev.owner = clk->ops.owner;
+ clk->dev = dev;
err = cdev_device_add(&clk->cdev, dev);
if (err) {
pr_err("%s unable to add device %d:%d\n",
dev_name(dev), MAJOR(dev->devt), MINOR(dev->devt));
- return err;
- }
- clk->cdev.owner = clk->ops.owner;
- clk->dev = dev;
+ down_write(&clk->rwsem);
+ clk->zombie = true;
+ up_write(&clk->rwsem);
+ return err;
+ }
return 0;
}