Xen Security Advisory 331 v2 - Race condition in Linux event handler may crash dom0
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Xen Security Advisory XSA-331
version 2
Race condition in Linux event handler may crash dom0
UPDATES IN VERSION 2
====================
Public release.
ISSUE DESCRIPTION
=================
The Linux kernel event channel handling code doesn't defend the handling of an event against the same event channel being removed in parallel.
This can result in accesses to already freed memory areas or NULL pointer dereferences in the event handling code, leading to misbehaviour of the system or even crashes.
IMPACT
======
A misbehaving guest can trigger a dom0 crash by sending events for a paravirtualized device while simultaneously reconfiguring it.
VULNERABLE SYSTEMS
==================
All systems with a Linux dom0 are vulnerable.
All Linux kernel versions are vulnerable.
MITIGATION
==========
There is no known mitigation.
CREDITS
=======
This issue was discovered by Jinoh Kang of Theori.
RESOLUTION
==========
Applying the appropriate attached patch resolves this issue.
Note that patches for released versions are generally prepared to apply to the stable branches, and may not apply cleanly to the most recent release tarball. Downstreams are encouraged to update to the tip of the stable branch before applying these patches.
xsa331-linux.patch Linux
$ sha256sum xsa331* 8583392c0c573f7baa85e41c9afbdf74dcb04aea1be992d78991f0787230a193 xsa331-linux.patch $
DEPLOYMENT DURING EMBARGO
=========================
Deployment of the patches and/or mitigations described above (or others which are substantially similar) is permitted during the embargo, even on public-facing systems with untrusted guest users and administrators.
But: Distribution of updated software is prohibited (except to other members of the predisclosure list).
Predisclosure list members who wish to deploy significantly different patches and/or mitigations, please contact the Xen Project Security Team.
(Note: this during-embargo deployment notice is retained in post-embargo publicly released Xen Project advisories, even though it is then no longer applicable. This is to enable the community to have oversight of the Xen Project Security Team's decisionmaking.)
For more information about permissible uses of embargoed information, consult the Xen Project community's agreed Security Policy:
http://www.xenproject.org/security-policy.html
-----BEGIN PGP SIGNATURE-----
iQFABAEBCAAqFiEEI+MiLBRfRHX6gGCng/4UyVfoK9kFAl+OzqMMHHBncEB4ZW4u b3JnAAoJEIP+FMlX6CvZuo4H/R4b4Z7ZTMwwpL4u3PrguNZduaTc3vy9R+Gd0+5z hY0Zfif7SfhJ2apN4Ihs1eAGxyWLI/I8kQQGE4xKgZy2ygciMbTK0OCsoGxfEr6v bi4RKV9I03g3fQHy48z+lOt4XKTY8+OpHw8LYY3W7jdnQ0YJrPCOmap0Xkv91QhP +EkmxzahVQv0T16cP4fxZFUvY0M9gijEjE9h9Gv23M+tLP9SGkW9Hd11qM135AKh vVSYUIuvyd20zb5uiqXono9qP1CeKyCOXHL+YQ+K7eOjYCVbEDdREneBegFlS9By jaFukH/psQDdemQDT4amzOmtBzdImIzkGhflvj+b5axRlrw=
=FLDG
-----END PGP SIGNATURE-----
From a5ab1d78befeb53c3b632721aa5b7b4b5ec9b648 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Mon, 7 Sep 2020 15:47:27 +0200
Subject: [PATCH] xen/events: avoid removing an event channel while
handling it
Today it can happen that an event channel is being removed from the system while the event handling loop is active. This can lead to a race resulting in crashes or WARN() splats when trying to access the irq_info structure related to the event channel.
Fix this problem by using a rwlock taken as reader in the event handling loop and as writer when deallocating the irq_info structure.
As the observed problem was a NULL dereference in evtchn_from_irq() make this function more robust against races by testing the irq_info pointer to be not NULL before dereferencing it.
And finally make all accesses to evtchn_to_irq[row][col] atomic ones in order to avoid seeing partial updates of an array element in irq handling. Note that irq handling can be entered only for event channels which have been valid before, so any not populated row isn't a problem in this regard, as rows are only ever added and never removed.
This is XSA-331.
--- drivers/xen/events/events_base.c | 41 ++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 5 deletions(-)
drivers/xen/events/events_base.c +36 -5
@@ -33,6 +33,7 @@#include <linux/slab.h> #include <linux/irqnr.h> #include <linux/pci.h> +#include <linux/spinlock.h> #ifdef CONFIG_X86 #include <asm/desc.h>
@@ -71,6 +72,23 @@ const struct evtchn_ops *evtchn_ops;*/ static DEFINE_MUTEX (irq_mapping_update_lock); +/* +* Lock protecting event handling loop against removing event channels. +* Adding of event channels is no issue as the associated IRQ becomes active +* only after everything is setup (before request_[threaded_]irq() the handler +* can't be entered for an event, as the event channel will be unmasked only +* then). +*/ +static DEFINE_RWLOCK (evtchn_rwlock); + +/* +* Lock hierarchy: +* +* irq_mapping_update_lock +* evtchn_rwlock +* IRQ-desc lock +*/ +static LIST_HEAD (xen_irq_list_head);/* IRQ <-> VIRQ mapping. */
@@ -105,7 +123,7 @@ static void clear_evtchn_to_irq_row(unsigned row)unsigned col;for (col =0 ; col <EVTCHN_PER_ROW ; col++) - evtchn_to_irq[row][col] =-1 ; +WRITE_ONCE (evtchn_to_irq[row][col],-1 ); }static void clear_evtchn_to_irq_all (void )
@@ -142,7 +160,7 @@ static int set_evtchn_to_irq(evtchn_port_t evtchn, unsigned int irq)clear_evtchn_to_irq_row (row); } - evtchn_to_irq[row][col] = irq; +WRITE_ONCE (evtchn_to_irq[row][col], irq);return 0 ; }
@@ -152,7 +170,7 @@ int get_evtchn_to_irq(evtchn_port_t evtchn)return -1 ;if (evtchn_to_irq[EVTCHN_ROW (evtchn)] ==NULL )return -1 ; -return evtchn_to_irq[EVTCHN_ROW (evtchn)][EVTCHN_COL (evtchn)]; +return READ_ONCE (evtchn_to_irq[EVTCHN_ROW (evtchn)][EVTCHN_COL (evtchn)]); }/* Get info for IRQ */
@@ -261,10 +279,14 @@ static void xen_irq_info_cleanup(struct irq_info *info)*/ evtchn_port_t evtchn_from_irq (unsigned irq) { -if (WARN (irq >= nr_irqs,"Invalid irq %d!\n" , irq)) +const struct irq_info *info =NULL ; + +if (likely (irq < nr_irqs)) + info =info_for_irq (irq); +if (!info)return 0 ; -return info_for_irq (irq)->evtchn; +return info->evtchn; }unsigned int irq_from_evtchn (evtchn_port_t evtchn)
@@ -440,16 +462,21 @@ static int __must_check xen_allocate_irq_gsi(unsigned gsi)static void xen_free_irq (unsigned irq) {struct irq_info *info =info_for_irq (irq); +unsigned long flags;if (WARN_ON (!info))return ; +write_lock_irqsave (&evtchn_rwlock, flags); +list_del (&info->list);set_info_for_irq (irq,NULL );WARN_ON (info->refcnt >0 ); +write_unlock_irqrestore (&evtchn_rwlock, flags); +kfree (info);/* Legacy IRQ descriptors are managed by the arch. */
@@ -1233,6 +1260,8 @@ static void __xen_evtchn_do_upcall(void)struct vcpu_info *vcpu_info =__this_cpu_read (xen_vcpu);int cpu =smp_processor_id (); +read_lock (&evtchn_rwlock); + do { vcpu_info->evtchn_upcall_pending = 0;
@@ -1243,6 +1272,8 @@ static void __xen_evtchn_do_upcall(void)virt_rmb ();/* Hypervisor can set upcall pending. */ }while (vcpu_info->evtchn_upcall_pending); + +read_unlock (&evtchn_rwlock); }void xen_evtchn_do_upcall (struct pt_regs *regs)
Signature · 1 line
2.26.2
Quoted 4 lines · Xen.org security team
Xen Security Advisory XSA-331
version 2Race condition in Linux event handler may crash dom0
(...)
Quoted 1 line
xsa331-linux.patch Linux
Do you know when it will land in longterm Linux releases? I see it is included in 5.10-rc1 (released on Oct 25), but not in any longterm releases (latest published on Oct 29/30).
Signature · 5 lines
Best Regards, Marek Marczykowski-Górecki Invisible Things Lab A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing?