Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: David Woodhouse <dwmw2@infradead.org>
Cc: qemu-devel@nongnu.org, xen-devel@lists.xenproject.org,
	kvm@vger.kernel.org, "Paolo Bonzini" <pbonzini@redhat.com>,
	"Peter Maydell" <peter.maydell@linaro.org>,
	"Paul Durrant" <paul@xen.org>,
	"Stefano Stabellini" <sstabellini@kernel.org>,
	"Anthony PERARD" <anthony@xenproject.org>,
	"Edgar E . Iglesias" <edgar.iglesias@gmail.com>,
	"Michael S . Tsirkin" <mst@redhat.com>,
	"Richard Henderson" <richard.henderson@linaro.org>,
	"Marcelo Tosatti" <mtosatti@redhat.com>,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>,
	"Jason Wang" <jasowangio@gmail.com>,
	"Hui Peng" <benquike@gmail.com>,
	"Mark Syms" <mark.syms@citrix.com>,
	"Feifan Qian" <bea1e@proton.me>,
	"David Woodhouse" <dwmw@amazon.co.uk>
Subject: Re: [PATCH v1 05/21] hw/i386/kvm/xen_gnttab: Fix error handling in xen_be_gnttab_map_refs()
Date: Mon, 30 Nov 2026 13:17:30 +0000	[thread overview]
Message-ID: <87bj765uhh.fsf@draig.linaro.org> (raw)
In-Reply-To: <20261124232247.23169-6-dwmw2@infradead.org> (David Woodhouse's message of "Tue, 24 Nov 2026 23:22:31 +0000")

David Woodhouse <dwmw2@infradead.org> writes:

> From: David Woodhouse <dwmw@amazon.co.uk>
>
> When the grant's target page cannot be mapped, the failure path calls
> gnt_unref(), which asserts that the map_track[] refcount is non-zero —
> but the refcount was only incremented after a successful mapping. Take
> the reference as soon as gnt_ref() succeeds, so the failure path stays
> balanced.
>
> Refuse writable mappings, including a write upgrade of an existing
> mapping, when the underlying memory region is read-only, and set errno
> when the upgrade fails.
>
> Fixes: b08d88e30f06 ("hw/xen: Add emulated implementation of grant table operations")
> Reported-by: Hui Peng <benquike@gmail.com>
> Closes: https://gitlab.com/qemu-project/qemu/-/work_items/4525

Again Resolves:

> Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
> ---
>  hw/i386/kvm/xen_gnttab.c | 12 ++++++++++--
>  1 file changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/hw/i386/kvm/xen_gnttab.c b/hw/i386/kvm/xen_gnttab.c
> index 85b324e62d0..26d1febac4d 100644
> --- a/hw/i386/kvm/xen_gnttab.c
> +++ b/hw/i386/kvm/xen_gnttab.c
> @@ -363,7 +363,9 @@ static void *xen_be_gnttab_map_refs(struct xengntdev_handle *xgt,
>      act = g_hash_table_lookup(xgt->active_maps, GINT_TO_POINTER(refs[0]));
>      if (act) {
>          if ((prot & PROT_WRITE) && !(act->prot & PROT_WRITE)) {
> -            if (gnt_ref(s, refs[0], prot) == INVALID_GPA) {
> +            if (act->mrs.readonly ||
> +                gnt_ref(s, refs[0], prot) == INVALID_GPA) {
> +                errno = EINVAL;
>                  return NULL;
>              }
>              act->prot |= PROT_WRITE;
> @@ -376,12 +378,19 @@ static void *xen_be_gnttab_map_refs(struct xengntdev_handle *xgt,
>              return NULL;
>          }
>  
> +        /*
> +         * Keep the map_track[] count balanced with the gnt_unref() in the
> +         * failure path below, which asserts that it never underflows.
> +         */
> +        s->map_track[refs[0]]++;
> +
>          act = g_new0(struct active_ref, 1);
>          act->prot = prot;
>          act->refcnt = 1;
>          act->mrs = memory_region_find(get_system_memory(), gpa, XEN_PAGE_SIZE);
>  
>          if (act->mrs.mr &&
> +            !(act->mrs.readonly && (prot & PROT_WRITE)) &&
>              !int128_lt(act->mrs.size, int128_make64(XEN_PAGE_SIZE)) &&
>              memory_region_get_ram_addr(act->mrs.mr) != RAM_ADDR_INVALID) {
>              act->virtaddr = qemu_map_ram_ptr(act->mrs.mr->ram_block,
> @@ -394,7 +403,6 @@ static void *xen_be_gnttab_map_refs(struct xengntdev_handle *xgt,
>              return NULL;
>          }
>  
> -        s->map_track[refs[0]]++;
>          g_hash_table_insert(xgt->active_maps, GINT_TO_POINTER(refs[0]), act);
>      }

-- 
Alex Bennée
Virtualisation Tech Lead @ Linaro

  parent reply	other threads:[~2026-11-30 13:17 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 23:22 [PATCH v1 00/21] hw/xen, i386/xen: security and robustness fixes David Woodhouse
2026-11-24 23:22 ` [PATCH v1 01/21] hw/xen: Fix partial request header handling in emulated XenStore David Woodhouse
2026-11-25 10:57   ` Paul Durrant
2026-11-30 13:17   ` Alex Bennée
2026-11-24 23:22 ` [PATCH v1 02/21] i386/xen: Don't resample GSI when per-vCPU upcall vector is in use David Woodhouse
2026-11-25 10:58   ` Paul Durrant
2026-11-24 23:22 ` [PATCH v1 03/21] i386/xen: Free Xen vCPU timers when a vCPU is destroyed David Woodhouse
2026-11-25 11:00   ` Paul Durrant
2026-11-24 23:22 ` [PATCH v1 04/21] hw/xen: Publish backend features before setting state to InitWait David Woodhouse
2026-11-25 11:05   ` Paul Durrant
2026-11-24 23:22 ` [PATCH v1 05/21] hw/i386/kvm/xen_gnttab: Fix error handling in xen_be_gnttab_map_refs() David Woodhouse
2026-11-25 11:07   ` Paul Durrant
2026-11-30 13:17   ` Alex Bennée [this message]
2026-11-24 23:22 ` [PATCH v1 06/21] hw/xen: Validate grant copy segments against the page size David Woodhouse
2026-11-25 11:09   ` Paul Durrant
2026-11-24 23:22 ` [PATCH v1 07/21] hw/i386/kvm/xen_evtchn: Validate PIRQ numbers in physdev operations David Woodhouse
2026-11-25 11:12   ` Paul Durrant
2026-11-24 23:22 ` [PATCH v1 08/21] hw/i386/kvm/xen_evtchn: Fix off-by-one in do_remove_pci_vector() David Woodhouse
2026-11-25 11:16   ` Paul Durrant
2026-11-25 12:03     ` David Woodhouse
2026-11-25 13:31       ` Paul Durrant
2026-11-25 13:43         ` David Woodhouse
2026-11-26 10:19           ` David Woodhouse
2026-11-30 12:04         ` Paul Durrant
2026-11-24 23:22 ` [PATCH v1 09/21] i386/xen: Don't inject event on invalid port in qmp_xen_event_inject() David Woodhouse
2026-11-24 23:31   ` Philippe Mathieu-Daudé
2026-11-25 11:17   ` Paul Durrant
2026-11-24 23:22 ` [PATCH v1 10/21] hw/i386/kvm/xen_evtchn: Reuse existing backend port in find_be_port() David Woodhouse
2026-11-25 11:18   ` Paul Durrant
2026-11-24 23:22 ` [PATCH v1 11/21] hw/i386/kvm/xen: Don't leak gnttab handle on reset David Woodhouse
2026-11-25 11:20   ` Paul Durrant
2026-11-24 23:22 ` [PATCH v1 12/21] hw/i386/kvm/xen_xenstore: Apply offset before truncation in xs_append_strings() David Woodhouse
2026-11-25 11:22   ` Paul Durrant
2026-11-24 23:22 ` [PATCH v1 13/21] hw/char/xen_console: Fix buffer trim when size exceeds twice the limit David Woodhouse
2026-11-25 11:24   ` Paul Durrant
2026-11-24 23:22 ` [PATCH v1 14/21] hw/9pfs/xen-9p-backend: Use validated snapshot for request iovecs David Woodhouse
2026-11-25 11:26   ` Paul Durrant
2026-11-24 23:22 ` [PATCH v1 15/21] hw/9pfs/xen-9p-backend: Validate the request header size field David Woodhouse
2026-11-25 11:27   ` Paul Durrant
2026-11-24 23:22 ` [PATCH v1 16/21] hw/9pfs/xen-9p-backend: Validate frontend-supplied connect parameters David Woodhouse
2026-11-25 11:29   ` Paul Durrant
2026-11-24 23:22 ` [PATCH v1 17/21] hw/9pfs/xen-9p-backend: Close event channel handle on disconnect David Woodhouse
2026-11-25 11:30   ` Paul Durrant
2026-11-24 23:22 ` [PATCH v1 18/21] hw/net/xen_nic: Bound the TX and RX rings' producer index David Woodhouse
2026-11-25 11:32   ` Paul Durrant
2026-11-24 23:22 ` [PATCH v1 19/21] hw/xen: Bound the rep count of PIO and MMIO ioreqs David Woodhouse
2026-11-25 11:34   ` Paul Durrant
2026-11-24 23:22 ` [PATCH v1 20/21] hw/i386/kvm/xen_evtchn: Hold port_lock for whole of bind_pirq_op David Woodhouse
2026-11-25 11:35   ` Paul Durrant
2026-11-24 23:22 ` [PATCH v1 21/21] hw/i386/kvm/xen_evtchn: Allow unmapping an emulated MSI PIRQ David Woodhouse
2026-11-25 11:36   ` Paul Durrant

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=87bj765uhh.fsf@draig.linaro.org \
    --to=alex.bennee@linaro.org \
    --cc=anthony@xenproject.org \
    --cc=bea1e@proton.me \
    --cc=benquike@gmail.com \
    --cc=dwmw2@infradead.org \
    --cc=dwmw@amazon.co.uk \
    --cc=edgar.iglesias@gmail.com \
    --cc=jasowangio@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=marcandre.lureau@redhat.com \
    --cc=mark.syms@citrix.com \
    --cc=mst@redhat.com \
    --cc=mtosatti@redhat.com \
    --cc=paul@xen.org \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=sstabellini@kernel.org \
    --cc=xen-devel@lists.xenproject.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