Skip to contentSkip to conversation

Xen Security Advisory 303 v4 (CVE-2019-18422) - ARM: Interrupts are unconditionally unmasked in exception handlers

Xen-devel Xen.org security team 1 participant 1 message Oct 31, 2019

lore
Share & tools
lore
#0 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

            Xen Security Advisory CVE-2019-18422 / XSA-303
                               version 4

ARM: Interrupts are unconditionally unmasked in exception handlers

UPDATES IN VERSION 4
====================

Fix typoes in the series and add more reviewed-by tag.

Public release.

ISSUE DESCRIPTION
=================

When an exception occurs on an ARM system which is handled without changing processor level, some interrupts are unconditionally enabled during exception entry. So exceptions which occur when interrupts are masked will effectively unmask the interrupts.

IMPACT
======

A malicious guest might contrive to arrange for critical Xen code to run with interrupts erroneously enabled. This could lead to data corruption, denial of service, or possibly even privilege escalation. However a precise attack technique has not been identified.

VULNERABLE SYSTEMS
==================

All ARM systems are vulnerable. x86 systems are not vulnerable.

On some platforms this issue could possibly be exploited by an unprivileged userspace attacker.

CREDITS
=======

This issue was discovered by Julian Grall of Arm.

RESOLUTION
==========

Applying the appropriate attached patch resolves this issue.

xsa303/*.patch xen-unstable .. Xen 4.9
xsa303-4.8/*.patch Xen 4.8

$ sha256sum xsa303* xsa303*/*
66b3eb28cfa633999da7480a37cd919293eb87aa730e7bc58b12c47bcdb0c9c0  xsa303.meta
7769eee9b876cdb7dde2ec664d34a5067f9b639d5c543ee89ff2eda818f04cab  xsa303-4.8/0001-Revert-xen-arm32-entry-Consolidate-DEFINE_TRAP_ENTRY.patch
f1337aa8c4b38f4ab61e7206c7bd8f5c782583947d9b9e1e8c6f139db73ca2cb  xsa303-4.8/0002-xen-arm32-entry-Consolidate-DEFINE_TRAP_ENTRY-macros.patch
160ea6acfba85faf1cbb670b0a3873f025c0dab388f73018a22a61104e1a5fe1  xsa303-4.8/0003-xen-arm32-entry-Fold-the-macro-SAVE_ALL-in-the-macro.patch
2cc1e3282263f03c6b9c6e05039f84173b8dbc893a2cd88f80ce2275ff7478d8  xsa303-4.8/0004-xen-arm32-Don-t-blindly-unmask-interrupts-on-trap-wi.patch
63c4a90c45ae28032e0149353cafd495cce5caa8c84ad022d21b8078710e996d  xsa303-4.8/0005-xen-arm64-Don-t-blindly-unmask-interrupts-on-trap-wi.patch
4da48a29aaad85a410021952b2b3cb4dae14365c688e724ed7fc80feea1334df  xsa303/0001-xen-arm32-entry-Split-__DEFINE_ENTRY_TRAP-in-two.patch
99773cbfb6f0df5f0c83477c9dcd39127cb361213455bd2cb1f6bcfe4566d5a2  xsa303/0002-xen-arm32-entry-Fold-the-macro-SAVE_ALL-in-the-macro.patch
9e8241c311aa8da7fcb1da09b9d8b5a55c26a10f02355e37e97d1e7a3b6db7be  xsa303/0003-xen-arm32-Don-t-blindly-unmask-interrupts-on-trap-wi.patch
4c9bc0d0b27eff06f65f1a679263ffbcc8aa4c65117840284dc115ae49e7966d  xsa303/0004-xen-arm64-Don-t-blindly-unmask-interrupts-on-trap-wi.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/4UyVfoK9kFAl26014MHHBncEB4ZW4u b3JnAAoJEIP+FMlX6CvZ79IIALZ04OSkaCmDXeNhb7mXPjqyNPGY8bqXwD1TQd51 W7yLo+DM/cnkj+u3UR96Mkma3eAj8sJLKeuQGRMScyyfNCj/b0pY0M1h6XRi5NLN zV6EWk7rR/87ID4Z82nwAq4lhsTgfgglH4I39oKZzFflHQtmij4DKuf/5K9g+6qT 8lc70ylgBep3Q3e73qJ1aLEvBYVnhs0lxY8QJDHOIS9GWY6/kqSVoWzK1dUtJDhD vB/MPBtG1WxJETrCjC1Fat6lmfErjqiqX/tunFVFASFPL4aOTSVA7Oo9IJYX9XSY 6f3le7BYj8xJUp7A0z2vv0YBQvOQ/bsvs4ONMRpRswwDA+Q=
=eraI
-----END PGP SIGNATURE-----

From 3c295a1ea51bdcd31704e960a8847ef41abb8a60 Mon Sep 17 00:00:00 2001
From: Julien Grall <[email protected]>
Date: Thu, 17 Oct 2019 14:25:05 +0100
Subject: [PATCH 1/5] Revert "xen/arm32: entry: Consolidate DEFINE_TRAP_ENTRY_*
macros"

This reverts commit 6082e3ba8941b3d10c3cb73f445759c19e89afc9.

The patch was meant to only consolidate the code but it also re-enables Abort interrupt. Follow-up patch will introduce a different way to consolidate the code.

This is part of XSA-303.

---
 xen/arch/arm/arm32/entry.S | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
xen/arch/arm/arm32/entry.S +11 -11
index b66705ff50..3e320dc8ac 100644
@@ -116,15 +116,11 @@ abort_guest_exit_end:
 
         mov pc, lr
 
-/*
- * Macro to define trap entry. The iflags corresponds to the list of
- * interrupts (Asynchronous Abort, IRQ, FIQ) to unmask.
- */
-#define __DEFINE_TRAP_ENTRY(trap, iflags)                               \
+#define DEFINE_TRAP_ENTRY(trap)                                         \
         ALIGN;                                                          \
 trap_##trap:                                                            \
         SAVE_ALL;                                                       \
-        cpsie iflags;                                                   \
+        cpsie i;        /* local_irq_enable */                          \
         adr lr, return_from_trap;                                       \
         mov r0, sp;                                                     \
         /*                                                              \
@@ -135,11 +131,15 @@ trap_##trap:                                                            \
         bic sp, #7; /* Align the stack pointer (noop on guest trap) */  \
         b do_trap_##trap
 
-/* Trap handler which unmask IRQ/Abort, keep FIQ masked */
-#define DEFINE_TRAP_ENTRY(trap) __DEFINE_TRAP_ENTRY(trap, ai)
-
-/* Trap handler which unmask Abort, keep IRQ/FIQ masked */
-#define DEFINE_TRAP_ENTRY_NOIRQ(trap) __DEFINE_TRAP_ENTRY(trap, a)
+#define DEFINE_TRAP_ENTRY_NOIRQ(trap)                                   \
+        ALIGN;                                                          \
+trap_##trap:                                                            \
+        SAVE_ALL;                                                       \
+        adr lr, return_from_trap;                                       \
+        mov r0, sp;                                                     \
+        mov r11, sp;                                                    \
+        bic sp, #7; /* Align the stack pointer (noop on guest trap) */  \
+        b do_trap_##trap
 
         .align 5
 GLOBAL(hyp_traps_vector)
Signature · 1 line
2.11.0

From f9879f4ed198ca03a03bf69152f0158be6580252 Mon Sep 17 00:00:00 2001
From: Julien Grall <[email protected]>
Date: Tue, 1 Oct 2019 13:07:53 +0100
Subject: [PATCH 2/5] xen/arm32: entry: Consolidate DEFINE_TRAP_ENTRY*() macros

The only difference between the two macros DEFINE_TRAP_ENTRY() and DEFINE_TRAP_ENTRY_NOIRQ() is the list of interrupts to be unmasked.

While the macros are fairly small today, it will be necessary to add the same code twice in follow-up patches.

To prevent too much duplication, a new assembly macros is introduced to generate the body of a trap.

This is part of XSA-303.

---
 xen/arch/arm/arm32/entry.S | 41 ++++++++++++++++++++++++-----------------
 1 file changed, 24 insertions(+), 17 deletions(-)
xen/arch/arm/arm32/entry.S +24 -17

54 lines not on this page · raw message

Signature · 1 line
2.11.0

From 8db4a93dc6e2ac66bad0dac254e49789a8cd9fd2 Mon Sep 17 00:00:00 2001
From: Julien Grall <[email protected]>
Date: Tue, 1 Oct 2019 13:15:48 +0100
Subject: [PATCH 3/5] xen/arm32: entry: Fold the macro SAVE_ALL in the macro
vector

Follow-up rework will require the macro vector to distinguish between a trap from a guest vs while in the hypervisor.

The macro SAVE_ALL already has code to distinguish between the two and it is only called by the vector macro. So fold the former into the latter. This will help to avoid duplicating the check.

This is part of XSA-303.

---
 xen/arch/arm/arm32/entry.S | 46 +++++++++++++++++++++++-----------------------
 1 file changed, 23 insertions(+), 23 deletions(-)
xen/arch/arm/arm32/entry.S +23 -23

64 lines not on this page · raw message

Signature · 1 line
2.11.0

From 8b66a1104d920c478f0c35e6360154e06025f060 Mon Sep 17 00:00:00 2001
From: Julien Grall <[email protected]>
Date: Fri, 11 Oct 2019 17:49:28 +0100
Subject: [PATCH 4/5] xen/arm32: Don't blindly unmask interrupts on trap
without a change of level

Exception vectors will unmask interrupts regardless the state of them in the interrupted context.

One of the consequences is IRQ will be unmasked when receiving an undefined instruction exception (used by WARN*) from the hypervisor. This could result to unexpected behavior such as deadlock (if a lock was shared with interrupts).

In a nutshell, interrupts should only be unmasked when it is safe to do. Xen only unmask IRQ interrupts, so the logic can stay simple.

As vectors exceptions may be shared between guest and hypervisor, we now need to have a different policy for the interrupts.

On exception from hypervisor, each vector will select the list of interrupts to inherit from the interrupted context. Any interrupts not listed will be kept masked.

On exception from the guest, the IRQ will be unmasked depending on the exact vector.

The interrupts will be kept unmasked when the vector cannot used by either guest or hypervisor.

Note that each vector is not anymore preceded by ALIGN. This is fine because the alignment is already bigger than what we need.

This is part of XSA-303.

---
 xen/arch/arm/arm32/entry.S | 136 +++++++++++++++++++++++++++++++++++----------
 1 file changed, 107 insertions(+), 29 deletions(-)
xen/arch/arm/arm32/entry.S +107 -29

170 lines not on this page · raw message

Signature · 1 line
2.11.0

From aa342e4af814eab107b11f0b7abca0457b616fbc Mon Sep 17 00:00:00 2001
From: Julien Grall <[email protected]>
Date: Mon, 7 Oct 2019 18:10:56 +0100
Subject: [PATCH 5/5] xen/arm64: Don't blindly unmask interrupts on trap
without a change of level

Some of the traps without a change of the level (i.e. hypervisor -> hypervisor) will unmask interrupts regardless the state of them in the interrupted context.

One of the consequences is IRQ will be unmasked when receiving a synchronous exception (used by WARN*()). This could result to unexpected behavior such as deadlock (if a lock was shared with interrupts).

In a nutshell, interrupts should only be unmasked when it is safe to do. Xen only unmask IRQ interrupts, so the logic can stay simple:
- hyp_error: All the interrupts are now kept masked. SError should be pretty rare and if ever happen then we most likely want to avoid any other interrupts to be generated. The potential main "caller" is during virtual SError synchronization on the exit path from the guest (see check_pending_vserror).

- hyp_sync: The IRQ state is inherited from the interrupted context.

- hyp_irq: All interrupts are now kept masked.

This is part of XSA-303.

---
 xen/arch/arm/arm64/entry.S | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
xen/arch/arm/arm64/entry.S +24 -3
index 0e7ddde9ed..7fd55300db 100644
@@ -177,6 +177,14 @@ hyp_error_invalid:
         entry   hyp=1
         invalid BAD_ERROR
 
+/*
+ * SError received while running in the hypervisor mode.
+ *
+ * Technically, we could unmask the IRQ if it were unmasked in the
+ * interrupted context. However, this require to check the PSTATE. For
+ * simplicity, as SError should be rare and potentially fatal,
+ * all interrupts are kept masked.
+ */
 hyp_error:
         /*
          * Only two possibilities:
@@ -186,7 +194,6 @@ hyp_error:
          * 2) Or we come from anywhere else, and that's a bug: we panic.
          */
         entry   hyp=1
-        msr     daifclr, #2
 
         /*
          * The ELR_EL2 may be modified by an interrupt, so we have to use the
@@ -214,14 +221,28 @@ hyp_error:
         bl      do_trap_guest_error
         exit    hyp=1
 
-/* Traps taken in Current EL with SP_ELx */
+/*
+ * Synchronous exception received while running in the hypervisor mode.
+ *
+ * While the exception can be executed with IRQ unmasked, the interrupt
+ * context may have purposefully masked it. So we want to inherit the
+ * state from the interrupted context.
+ */
 hyp_sync:
         entry   hyp=1
-        msr     daifclr, #2
+
+        /* Inherit IRQ state and keep the other interrupts masked. */
+        mrs     x0, SPSR_el2
+        and     x0, x0, #PSR_IRQ_MASK
+        mov     x1, #(PSR_DBG_MASK | PSR_ABT_MASK | PSR_FIQ_MASK)
+        orr     x0, x0, x1
+        msr     daif, x0
+
         mov     x0, sp
         bl      do_trap_hyp_sync
         exit    hyp=1
 
+/* IRQ received while running in the hypervisor mode. */
 hyp_irq:
         entry   hyp=1
         mov     x0, sp
Signature · 1 line
2.11.0

From c8cb33fa64c9ccbfa2a494a9dad2e0a763c09176 Mon Sep 17 00:00:00 2001
From: Julien Grall <[email protected]>
Date: Tue, 1 Oct 2019 13:07:53 +0100
Subject: [PATCH 1/4] xen/arm32: entry: Split __DEFINE_ENTRY_TRAP in two

The preprocessing macro __DEFINE_ENTRY_TRAP is used to generate trap entry function. While the macro is fairly small today, follow-up patches will increase the size signicantly.

In general, assembly macros are more readable as they allow you to name parameters and avoid '\'. So the actual implementation of the trap is now switched to an assembly macro.

This is part of XSA-303.

---
 xen/arch/arm/arm32/entry.S | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
xen/arch/arm/arm32/entry.S +19 -15
index 0b4cd19abd..4a762e04f1 100644
@@ -126,24 +126,28 @@ abort_guest_exit_end:
 skip_check:
         mov pc, lr
 
-/*
- * Macro to define trap entry. The iflags corresponds to the list of
- * interrupts (Asynchronous Abort, IRQ, FIQ) to unmask.
- */
+        /*
+         * Macro to define trap entry. The iflags corresponds to the list of
+         * interrupts (Asynchronous Abort, IRQ, FIQ) to unmask.
+         */
+        .macro vector trap, iflags
+        SAVE_ALL
+        cpsie   \iflags
+        adr     lr, return_from_trap
+        mov     r0, sp
+        /*
+         * Save the stack pointer in r11. It will be restored after the
+         * trap has been handled (see return_from_trap).
+         */
+        mov     r11, sp
+        bic     sp, #7      /* Align the stack pointer (noop on guest trap) */
+        b       do_trap_\trap
+        .endm
+
 #define __DEFINE_TRAP_ENTRY(trap, iflags)                               \
         ALIGN;                                                          \
 trap_##trap:                                                            \
-        SAVE_ALL;                                                       \
-        cpsie iflags;                                                   \
-        adr lr, return_from_trap;                                       \
-        mov r0, sp;                                                     \
-        /*                                                              \
-         * Save the stack pointer in r11. It will be restored after the \
-         * trap has been handled (see return_from_trap).                \
-         */                                                             \
-        mov r11, sp;                                                    \
-        bic sp, #7; /* Align the stack pointer (noop on guest trap) */  \
-        b do_trap_##trap
+        vector trap, iflags
 
 /* Trap handler which unmask IRQ/Abort, keep FIQ masked */
 #define DEFINE_TRAP_ENTRY(trap) __DEFINE_TRAP_ENTRY(trap, ai)
Signature · 1 line
2.11.0

From be7379207c83fa74f8a6c22a8ea213f02714776f Mon Sep 17 00:00:00 2001
From: Julien Grall <[email protected]>
Date: Tue, 1 Oct 2019 13:15:48 +0100
Subject: [PATCH 2/4] xen/arm32: entry: Fold the macro SAVE_ALL in the macro
vector

Follow-up rework will require the macro vector to distinguish between a trap from a guest vs while in the hypervisor.

The macro SAVE_ALL already has code to distinguish between the two and it is only called by the vector macro. So fold the former into the latter. This will help to avoid duplicating the check.

This is part of XSA-303.

---
 xen/arch/arm/arm32/entry.S | 46 +++++++++++++++++++++++-----------------------
 1 file changed, 23 insertions(+), 23 deletions(-)
xen/arch/arm/arm32/entry.S +23 -23

64 lines not on this page · raw message

Signature · 1 line
2.11.0

From 098fe877967870ffda2dfd9629a5fd272f6aacdc Mon Sep 17 00:00:00 2001
From: Julien Grall <[email protected]>
Date: Fri, 11 Oct 2019 17:49:28 +0100
Subject: [PATCH 3/4] xen/arm32: Don't blindly unmask interrupts on trap
without a change of level

Exception vectors will unmask interrupts regardless the state of them in the interrupted context.

One of the consequences is IRQ will be unmasked when receiving an undefined instruction exception (used by WARN*) from the hypervisor. This could result to unexpected behavior such as deadlock (if a lock was shared with interrupts).

In a nutshell, interrupts should only be unmasked when it is safe to do. Xen only unmask IRQ and Abort interrupts, so the logic can stay simple.

As vectors exceptions may be shared between guest and hypervisor, we now need to have a different policy for the interrupts.

On exception from hypervisor, each vector will select the list of interrupts to inherit from the interrupted context. Any interrupts not listed will be kept masked.

On exception from the guest, the Abort and IRQ will be unmasked depending on the exact vector.

The interrupts will be kept unmasked when the vector cannot used by either guest or hypervisor.

Note that each vector is not anymore preceded by ALIGN. This is fine because the alignment is already bigger than what we need.

This is part of XSA-303.

---
 xen/arch/arm/arm32/entry.S | 138 +++++++++++++++++++++++++++++++++++----------
 1 file changed, 109 insertions(+), 29 deletions(-)
xen/arch/arm/arm32/entry.S +109 -29

171 lines not on this page · raw message

Signature · 1 line
2.11.0

From c6d290ce157a044dec417fdda8db71e41a37d744 Mon Sep 17 00:00:00 2001
From: Julien Grall <[email protected]>
Date: Mon, 7 Oct 2019 18:10:56 +0100
Subject: [PATCH 4/4] xen/arm64: Don't blindly unmask interrupts on trap
without a change of level

Some of the traps without a change of the level (i.e. hypervisor -> hypervisor) will unmask interrupts regardless the state of them in the interrupted context.

One of the consequences is IRQ will be unmasked when receiving a synchronous exception (used by WARN*()). This could result to unexpected behavior such as deadlock (if a lock was shared with interrupts).

In a nutshell, interrupts should only be unmasked when it is safe to do. Xen only unmask IRQ and Abort interrupts, so the logic can stay simple:
- hyp_error: All the interrupts are now kept masked. SError should be pretty rare and if ever happen then we most likely want to avoid any other interrupts to be generated. The potential main "caller" is during virtual SError synchronization on the exit path from the guest (see check_pending_vserror).

- hyp_sync: The interrupts state is inherited from the interrupted context.

- hyp_irq: All the interrupts but IRQ state are inherited from the interrupted context. IRQ is kept masked.

This is part of XSA-303.

---
 xen/arch/arm/arm64/entry.S | 47 ++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 43 insertions(+), 4 deletions(-)
xen/arch/arm/arm64/entry.S +43 -4

67 lines not on this page · raw message

Signature · 1 line
2.11.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel