linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Al Viro <viro@zeniv.linux.org.uk>
To: Fei Li <fei1.li@intel.com>, Alex Williamson <alex.williamson@redhat.com>
Cc: kvm@vger.kernel.org, linux-fsdevel@vger.kernel.org
Subject: Re: [RFC] UAF in acrn_irqfd_assign() and vfio_virqfd_enable()
Date: Mon, 10 Jun 2024 18:03:21 +0100	[thread overview]
Message-ID: <20240610170321.GA2069166@ZenIV> (raw)
In-Reply-To: <20240610051206.GD1629371@ZenIV>

On Mon, Jun 10, 2024 at 06:12:06AM +0100, Al Viro wrote:

> vfio_virqfd_enable() has the same problem, except that there we
> definitely can't move vfs_poll() under the lock - it's a spinlock.
> 
> Could we move vfs_poll() + inject to _before_ making the thing
> public?  We'd need to delay POLLHUP handling there, but then
> we need it until the moment with do inject anyway.  Something
> like replacing
>         if (!list_empty(&irqfd->list))
> 		hsm_irqfd_shutdown(irqfd);
> in hsm_irqfd_shutdown_work() with
>         if (!list_empty(&irqfd->list))
> 		hsm_irqfd_shutdown(irqfd);
> 	else
> 		irqfd->need_shutdown = true;
> and doing
> 	if (unlikely(irqfd->need_shutdown))
> 		hsm_irqfd_shutdown(irqfd);
> 	else
> 		list_add_tail(&irqfd->list, &vm->irqfds);
> when the sucker is made visible.
> 
> I'm *not* familiar with the area, though, so that might be unfeasible
> for any number of reasons.

Hmm...  OK, so we rely upon EPOLLHUP being generated only upon the final
close of eventfd file.  And vfio seems to have an exclusion in all callers
of vfio_virqfd_{en,dis}able(), which ought to be enough.

For drivers/virt/acrn/irqfd.c EPOLLHUP is not a problem for the same
reasons, but there's no exclusion between acrn_irqfd_assign() and
acrn_irqfd_deassign() calls.  So the scenario with explicit deassign
racing with assign and leading to vfs_poll(file, <freed memory>) is
possible.

And it looks like drivers/xen/privcmd.c:privcmd_irqfd_assign() has
a similar problem...

How about the following for acrn side of things?  Does anybody see
a problem with that "do vfs_poll() before making the thing visible"
approach?

diff --git a/drivers/virt/acrn/irqfd.c b/drivers/virt/acrn/irqfd.c
index d4ad211dce7a..71c431506a9b 100644
--- a/drivers/virt/acrn/irqfd.c
+++ b/drivers/virt/acrn/irqfd.c
@@ -133,7 +133,7 @@ static int acrn_irqfd_assign(struct acrn_vm *vm, struct acrn_irqfd *args)
 	eventfd = eventfd_ctx_fileget(f.file);
 	if (IS_ERR(eventfd)) {
 		ret = PTR_ERR(eventfd);
-		goto fail;
+		goto out_file;
 	}
 
 	irqfd->eventfd = eventfd;
@@ -145,29 +145,26 @@ static int acrn_irqfd_assign(struct acrn_vm *vm, struct acrn_irqfd *args)
 	init_waitqueue_func_entry(&irqfd->wait, hsm_irqfd_wakeup);
 	init_poll_funcptr(&irqfd->pt, hsm_irqfd_poll_func);
 
+	/* Check the pending event in this stage */
+	events = vfs_poll(f.file, &irqfd->pt);
+
+	if (events & EPOLLIN)
+		acrn_irqfd_inject(irqfd);
+
 	mutex_lock(&vm->irqfds_lock);
 	list_for_each_entry(tmp, &vm->irqfds, list) {
 		if (irqfd->eventfd != tmp->eventfd)
 			continue;
-		ret = -EBUSY;
+		hsm_irqfd_shutdown(irqfd);
 		mutex_unlock(&vm->irqfds_lock);
-		goto fail;
+		irqfd = NULL;	// consumed by hsm_irqfd_shutdown()
+		ret = -EBUSY;
+		goto out_file;
 	}
 	list_add_tail(&irqfd->list, &vm->irqfds);
+	irqfd = NULL;	// not for us to free...
 	mutex_unlock(&vm->irqfds_lock);
-
-	/* Check the pending event in this stage */
-	events = vfs_poll(f.file, &irqfd->pt);
-
-	if (events & EPOLLIN)
-		acrn_irqfd_inject(irqfd);
-
-	fdput(f);
-	return 0;
-fail:
-	if (eventfd && !IS_ERR(eventfd))
-		eventfd_ctx_put(eventfd);
-
+out_file:
 	fdput(f);
 out:
 	kfree(irqfd);

  reply	other threads:[~2024-06-10 17:03 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-07  1:56 [PATCHES][RFC] rework of struct fd handling Al Viro
2024-06-07  1:59 ` [PATCH 01/19] powerpc: fix a file leak in kvm_vcpu_ioctl_enable_cap() Al Viro
2024-06-07  1:59   ` [PATCH 02/19] lirc: rc_dev_get_from_fd(): fix file leak Al Viro
2024-06-07 15:17     ` Christian Brauner
2024-06-07  1:59   ` [PATCH 03/19] introduce fd_file(), convert all accessors to it Al Viro
2024-06-07  1:59   ` [PATCH 04/19] struct fd: representation change Al Viro
2024-06-07  5:55     ` Amir Goldstein
2024-06-07  1:59   ` [PATCH 05/19] add struct fd constructors, get rid of __to_fd() Al Viro
2024-06-07  1:59   ` [PATCH 06/19] net/socket.c: switch to CLASS(fd) Al Viro
2024-06-07  1:59   ` [PATCH 07/19] introduce struct fderr, convert overlayfs uses to that Al Viro
2024-06-07  1:59   ` [PATCH 08/19] fdget_raw() users: switch to CLASS(fd_raw, ...) Al Viro
2024-06-07 15:20     ` Christian Brauner
2024-06-07  1:59   ` [PATCH 09/19] css_set_fork(): " Al Viro
2024-06-07 15:21     ` Christian Brauner
2024-06-07  1:59   ` [PATCH 10/19] introduce "fd_pos" class Al Viro
2024-06-07 15:21     ` Christian Brauner
2024-06-07  1:59   ` [PATCH 11/19] switch simple users of fdget() to CLASS(fd, ...) Al Viro
2024-06-07 15:26     ` Christian Brauner
2024-06-07 16:10       ` Al Viro
2024-06-07 16:11         ` Al Viro
2024-06-07 21:08         ` Al Viro
2024-06-10  2:44           ` [RFC] potential UAF in kvm_spapr_tce_attach_iommu_group() (was Re: [PATCH 11/19] switch simple users of fdget() to CLASS(fd, ...)) Al Viro
2024-06-12 16:36             ` Linus Torvalds
2024-06-13 10:56               ` Michael Ellerman
2024-06-10  5:12           ` [RFC] UAF in acrn_irqfd_assign() and vfio_virqfd_enable() Al Viro
2024-06-10 17:03             ` Al Viro [this message]
2024-06-10 20:09             ` Alex Williamson
2024-06-10 20:53               ` Al Viro
2024-06-11 23:04                 ` Alex Williamson
2024-06-12  2:16                   ` Al Viro
2024-06-07  1:59   ` [PATCH 12/19] bpf: switch to CLASS(fd, ...) Al Viro
2024-06-07 15:27     ` Christian Brauner
2024-06-07  1:59   ` [PATCH 13/19] convert vmsplice() " Al Viro
2024-06-07  1:59   ` [PATCH 14/19] finit_module(): convert " Al Viro
2024-06-07  1:59   ` [PATCH 15/19] timerfd: switch " Al Viro
2024-06-07  1:59   ` [PATCH 16/19] do_mq_notify(): " Al Viro
2024-06-07  1:59   ` [PATCH 17/19] simplify xfs_find_handle() a bit Al Viro
2024-06-07  1:59   ` [PATCH 18/19] convert kernel/events/core.c Al Viro
2024-06-07  1:59   ` [PATCH 19/19] deal with the last remaing boolean uses of fd_file() Al Viro
2024-06-07 15:16   ` [PATCH 01/19] powerpc: fix a file leak in kvm_vcpu_ioctl_enable_cap() Christian Brauner
2024-06-07 15:30 ` [PATCHES][RFC] rework of struct fd handling Christian Brauner

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=20240610170321.GA2069166@ZenIV \
    --to=viro@zeniv.linux.org.uk \
    --cc=alex.williamson@redhat.com \
    --cc=fei1.li@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-fsdevel@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;
as well as URLs for NNTP newsgroup(s).