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@syzkaller.appspotmail.com Closes: https://syzbot.org/bug?extid=667788430769ef9ddf25 Cc: stable@vger.kernel.org Co-developed-by: Jinning Zhang <25031212117@stu.xidian.edu.cn> Signed-off-by: Jinning Zhang <25031212117@stu.xidian.edu.cn> Co-developed-by: Bin Yu Signed-off-by: Bin Yu Signed-off-by: Haoli Zhang <26031212468@stu.xidian.edu.cn> --- --- 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; }