Skip to contentSkip to conversation

target/arm: mte+pauth fixes

PATCH Applied Richard Henderson 2 participants 5 messages Jul 24, 2020 – Jul 27, 2020

lore
Share & tools
lore
#0 lore raw

cover target/arm: mte+pauth fixes

A couple of last minute fixes for MTE:

(1) Peter pointed out that EL3's SCR.ATA needs to be set when we're booting a kernel directly. Similarly for API & APK.

(2) Vincenzo pointed out that with RRND=1, we can't rely on RGSR having being initialized.

I suppose the only follow-on question here is whether it is better to minimize the number of calls to qemu_guest_getrandom, or instead to name that our IMPDEF algorithm and use it for every call to IRG. We already have other user-space available RNG instructions that can drain the entropy pool, so this is not really different.

r~

Richard Henderson (3):
hw/arm/boot: Fix PAUTH for EL3 direct kernel boot hw/arm/boot: Fix MTE for EL3 direct kernel boot target/arm: Improve IMPDEF algorithm for IRG

 hw/arm/boot.c           |  6 ++++++
 target/arm/mte_helper.c | 37 ++++++++++++++++++++++++++++++-------
 2 files changed, 36 insertions(+), 7 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/3 hw/arm/boot: Fix PAUTH for EL3 direct kernel boot

When booting an EL3 cpu with -kernel, we set up EL3 and then drop down to EL2. We need to enable access to v8.3-PAuth keys and instructions at EL3 before doing so.

---
 hw/arm/boot.c | 3 +++
 1 file changed, 3 insertions(+)
hw/arm/boot.c +3 -0
index fef4072db1..c44fd3382d 100644
@@ -736,6 +736,9 @@ static void do_cpu_reset(void *opaque)
                     } else {
                         env->pstate = PSTATE_MODE_EL1h;
                     }
+                    if (cpu_isar_feature(aa64_pauth, cpu)) {
+                        env->cp15.scr_el3 |= SCR_API | SCR_APK;
+                    }
                     /* AArch64 kernels never boot in secure mode */
                     assert(!info->secure_boot);
                     /* This hook is only supported for AArch32 currently:
Signature · 1 line
2.25.1
#2 lore raw

PATCH 2/3 hw/arm/boot: Fix MTE for EL3 direct kernel boot

In reply to Richard Henderson

When booting an EL3 cpu with -kernel, we set up EL3 and then drop down to EL2. We need to enable access to v8.5-MemTag tag allocation at EL3 before doing so.

---
 hw/arm/boot.c | 3 +++
 1 file changed, 3 insertions(+)
hw/arm/boot.c +3 -0
index c44fd3382d..3e9816af80 100644
@@ -739,6 +739,9 @@ static void do_cpu_reset(void *opaque)
                     if (cpu_isar_feature(aa64_pauth, cpu)) {
                         env->cp15.scr_el3 |= SCR_API | SCR_APK;
                     }
+                    if (cpu_isar_feature(aa64_mte, cpu)) {
+                        env->cp15.scr_el3 |= SCR_ATA;
+                    }
                     /* AArch64 kernels never boot in secure mode */
                     assert(!info->secure_boot);
                     /* This hook is only supported for AArch32 currently:
Signature · 1 line
2.25.1
#3 lore raw

PATCH 3/3 target/arm: Improve IMPDEF algorithm for IRG

In reply to Richard Henderson

When GCR_EL1.RRND==1, the choosing of the random value is IMPDEF, and the kernel is not expected to have set RGSR_EL1. Force a non-zero value into SEED, so that we do not continually return the same tag.

---
 target/arm/mte_helper.c | 37 ++++++++++++++++++++++++++++++-------
 1 file changed, 30 insertions(+), 7 deletions(-)
target/arm/mte_helper.c +30 -7
index 5ea57d487a..104752041f 100644
@@ -24,6 +24,8 @@
 #include "exec/ram_addr.h"
 #include "exec/cpu_ldst.h"
 #include "exec/helper-proto.h"
+#include "qapi/error.h"
+#include "qemu/guest-random.h"
 
 
 static int choose_nonexcluded_tag(int tag, int offset, uint16_t exclude)
@@ -211,16 +213,37 @@ static uint8_t *allocation_tag_mem(CPUARMState *env, int ptr_mmu_idx,
 
 uint64_t HELPER(irg)(CPUARMState *env, uint64_t rn, uint64_t rm)
 {
-    int rtag;
-
-    /*
-     * Our IMPDEF choice for GCR_EL1.RRND==1 is to behave as if
-     * GCR_EL1.RRND==0, always producing deterministic results.
-     */
     uint16_t exclude = extract32(rm | env->cp15.gcr_el1, 0, 16);
+    int rrnd = extract32(env->cp15.gcr_el1, 16, 1);
     int start = extract32(env->cp15.rgsr_el1, 0, 4);
     int seed = extract32(env->cp15.rgsr_el1, 8, 16);
-    int offset, i;
+    int offset, i, rtag;
+
+    /*
+     * Our IMPDEF choice for GCR_EL1.RRND==1 is to continue to use the
+     * deterministic algorithm.  Except that with RRND==1 the kernel is
+     * not required to have set RGSR_EL1.SEED != 0, which is required for
+     * the deterministic algorithm to function.  So we force a non-zero
+     * SEED for that case.
+     */
+    if (unlikely(seed == 0) && rrnd) {
+        do {
+            Error *err = NULL;
+            uint16_t two;
+
+            if (qemu_guest_getrandom(&two, sizeof(two), &err) < 0) {
+                /*
+                 * Failed, for unknown reasons in the crypto subsystem.
+                 * Best we can do is log the reason and use a constant seed.
+                 */
+                qemu_log_mask(LOG_UNIMP, "IRG: Crypto failure: %s\n",
+                              error_get_pretty(err));
+                error_free(err);
+                two = 1;
+            }
+            seed = two;
+        } while (seed == 0);
+    }
 
     /* RandomTag */
     for (i = offset = 0; i < 4; ++i) {
Signature · 1 line
2.25.1
#4 lore raw
In reply to Richard Henderson
Quoted 9 lines · Richard Henderson

A couple of last minute fixes for MTE:

(1) Peter pointed out that EL3's SCR.ATA needs to be set when we're booting a kernel directly. Similarly for API & APK.

(2) Vincenzo pointed out that with RRND=1, we can't rely on RGSR having being initialized.

Quoted 7 lines

I suppose the only follow-on question here is whether it is better to minimize the number of calls to qemu_guest_getrandom, or instead to name that our IMPDEF algorithm and use it for every call to IRG. We already have other user-space available RNG instructions that can drain the entropy pool, so this is not really different.

Applied to target-arm.next, thanks.

-- PMM