mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Greg KH <gregkh@linuxfoundation.org>
To: Vu Nguyen Anh Khoa <khoavna.tin.2225@gmail.com>
Cc: jirislaby@kernel.org, arnd@arndb.de, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] misc: phantom: fix open file UAF after device removal
Date: Tue, 1 Sep 2026 06:41:10 +0200	[thread overview]
Message-ID: <2026090121-bloomers-egomaniac-776b@gregkh> (raw)
In-Reply-To: <20260831151326.131296-1-khoavna.tin.2225@gmail.com>

On Mon, Aug 31, 2026 at 10:13:26PM +0700, Vu Nguyen Anh Khoa wrote:
> phantom_remove() tears down the character device and frees struct
> phantom_device immediately. Already-open file descriptors still keep
> file->private_data pointing at that object, and their ioctl(), poll(),
> and release() paths remain callable after cdev_del() returns. The VFS
> also performs cdev_put() after ->release(), so freeing a container with
> an embedded cdev leaves an open-file UAF behind.
> 
> Keep the runtime state alive until the last open file is closed, store
> the cdev separately so it can outlive the device state, and block new
> opens while removal is in progress. Mark removed devices so file
> operations fail cleanly without touching unmapped MMIO.
> 
> Assisted-by: LLM (Codex, GPT-5)
> Signed-off-by: Vu Nguyen Anh Khoa <khoavna.tin.2225@gmail.com>
> ---
>  drivers/misc/phantom.c | 123 +++++++++++++++++++++++++++++++----------
>  1 file changed, 93 insertions(+), 30 deletions(-)

Cool, how was this found and tested?

And how was remove() called while the device node was open?  How can
this device be removed while the system is running?


> 
> diff --git a/drivers/misc/phantom.c b/drivers/misc/phantom.c
> index 331cae539290..dcec9c02c426 100644
> --- a/drivers/misc/phantom.c
> +++ b/drivers/misc/phantom.c
> @@ -48,9 +48,11 @@ struct phantom_device {
>  	u32 __iomem *oaddr;
>  	unsigned long status;
>  	atomic_t counter;
> +	unsigned int minor;
> +	bool removed;
>  
>  	wait_queue_head_t wait;
> -	struct cdev cdev;
> +	struct cdev *cdev;
>  
>  	struct mutex open_lock;
>  	spinlock_t regs_lock;
> @@ -60,7 +62,7 @@ struct phantom_device {
>  	u32 ctl_reg;
>  };
>  
> -static unsigned char phantom_devices[PHANTOM_MAX_MINORS];
> +static struct phantom_device *phantom_devices[PHANTOM_MAX_MINORS];
>  
>  static int phantom_status(struct phantom_device *dev, unsigned long newstat)
>  {
> @@ -94,21 +96,31 @@ static long phantom_ioctl(struct file *file, unsigned int cmd,
>  	void __user *argp = (void __user *)arg;
>  	unsigned long flags;
>  	unsigned int i;
> +	long retval = 0;
> +
> +	if (mutex_lock_interruptible(&dev->open_lock))
> +		return -ERESTARTSYS;

guard?

thanks,

greg k-h

  reply	other threads:[~2026-09-01  4:41 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-31 15:13 Vu Nguyen Anh Khoa
2026-09-01  4:41 ` Greg KH [this message]
     [not found]   ` <CAK5ebCne_H-p9yaastoJnMk7k8SXtiaRohZruXN3xZGvsRa8zg@mail.gmail.com>
2026-09-01 15:56     ` Greg KH

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2026090121-bloomers-egomaniac-776b@gregkh \
    --to=gregkh@linuxfoundation.org \
    --cc=arnd@arndb.de \
    --cc=jirislaby@kernel.org \
    --cc=khoavna.tin.2225@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®