Re: [PATCH v3 4/5] i3c: add i3cdev module to expose i3c dev in /dev

From: Boris Brezillon
Date: Fri Feb 21 2020 - 17:32:22 EST


On Wed, 19 Feb 2020 01:20:42 +0100
Vitor Soares <Vitor.Soares@xxxxxxxxxxxx> wrote:

> +static int i3cdev_detach(struct device *dev, void *dummy)
> +{
> + struct i3cdev_data *i3cdev;
> + struct i3c_device *i3c;
> +
> + if (dev->type == &i3c_masterdev_type)
> + return 0;
> +
> + i3c = dev_to_i3cdev(dev);
> +
> + i3cdev = i3cdev_get_drvdata(i3c);
> + if (!i3cdev)
> + return 0;
> +
> + /* Prevent transfers while cdev removal */
> + mutex_lock(&i3cdev->xfer_lock);
> + cdev_del(&i3cdev->cdev);

When cdev_del() returns there might be opened FDs pointing to your
i3cdev [1] ...

> + device_destroy(i3cdev_class, MKDEV(MAJOR(i3cdev_number), i3cdev->id));
> + mutex_unlock(&i3cdev->xfer_lock);
> +
> + ida_simple_remove(&i3cdev_ida, i3cdev->id);
> + put_i3cdev(i3cdev);

... and you call put_i3cdev() here which frees the i3cdev object,
leading to potential use-after-free if any of the fops (ioctl, read,
write) are called on those dangling FDs. That's exactly the kind of
nightmare I'd like to avoid.

> +
> + pr_debug("i3cdev: device [%s] unregistered\n", dev_name(&i3c->dev));
> +
> + return 0;
> +}
> +

[1]https://elixir.bootlin.com/linux/latest/source/fs/char_dev.c#L587