Skip to contentSkip to conversation

target/s390x: Make PRNO TRNG interruptible

PATCH Superseded Ilya Leoshkevich 2 participants 4 messages Jul 14

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover target/s390x: Make PRNO TRNG interruptible

Hi,

Christian has reported that one can make a TCG guest stuck by running PRNO TRNG with a huge buffer. This series fixes this by making PRNO TRNG always end after generating 16KB.

Patch 1 is the fix, patch 2 is the test.

Cc Harald, because this may conflict with the CPACF series.

Best regards,
Ilya

Ilya Leoshkevich (2):
target/s390x: Make PRNO TRNG interruptible tests/tcg/s390x: Test PRNO TRNG interruptibility

 target/s390x/tcg/crypto_helper.c | 25 +++++++++---
 tests/tcg/s390x/Makefile.target  |  1 +
 tests/tcg/s390x/prno-trng.c      | 67 ++++++++++++++++++++++++++++++++
 3 files changed, 87 insertions(+), 6 deletions(-)
 create mode 100644 tests/tcg/s390x/prno-trng.c
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/2 target/s390x: Make PRNO TRNG interruptible

fill_buf_random() writes the entire guest-requested amount of random bytes in one go. Since the length is a full 64-bit value, a guest can request several gigabytes and keep the vCPU spinning inside the helper, without a chance to react to interrupts.

Do the same thing as cpacf_sha512(): process only a limited amount of data per instruction execution and report partial completion with condition code 3. This way the guest reissues the instruction, and pending interrupts can be handled in between.

Fixes 3dbc5fdacb5a ("target/s390x: support PRNO_TRNG instruction")
---
 target/s390x/tcg/crypto_helper.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
target/s390x/tcg/crypto_helper.c +19 -6
index 8fe0a222198..4902c4e93ba 100644
@@ -242,12 +242,13 @@ static int cpacf_sha512(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     return !len ? 0 : 3;
 }
 
-static void fill_buf_random(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
-                            uint64_t *buf_reg, uint64_t *len_reg)
+static int fill_buf_random(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
+                           uint64_t *buf_reg, uint64_t *len_reg)
 {
+    enum { MAX_BYTES_PER_RUN = 8192 }; /* Arbitrary: keep interactivity. */
     const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
     uint8_t tmp[256];
-    uint64_t len = *len_reg;
+    uint64_t len = *len_reg, processed = 0;
     int buf_reg_len = 64;
 
     if (!(env->psw.mask & PSW_MASK_64)) {
@@ -258,6 +259,10 @@ static void fill_buf_random(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
     while (len) {
         size_t block = MIN(len, sizeof(tmp));
 
+        if (processed >= MAX_BYTES_PER_RUN) {
+            break;
+        }
+
         qemu_guest_getrandom_nofail(tmp, block);
         for (size_t i = 0; i < block; ++i) {
             cpu_stb_mmu(env, wrap_address(env, *buf_reg), tmp[i], oi, ra);
@@ -265,7 +270,10 @@ static void fill_buf_random(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
             --*len_reg;
         }
         len -= block;
+        processed += block;
     }
+
+    return !len ? 0 : 3;
 }
 
 uint32_t HELPER(msa)(CPUS390XState *env, uint32_t r1, uint32_t r2, uint32_t r3,
@@ -278,6 +286,7 @@ uint32_t HELPER(msa)(CPUS390XState *env, uint32_t r1, uint32_t r2, uint32_t r3,
     uint8_t subfunc[16] = { 0 };
     uint64_t param_addr;
     MemOpIdx oi;
+    int cc;
 
     switch (type) {
     case S390_FEAT_TYPE_KMAC:
@@ -308,9 +317,13 @@ uint32_t HELPER(msa)(CPUS390XState *env, uint32_t r1, uint32_t r2, uint32_t r3,
         return cpacf_sha512(env, mmu_idx, ra, env->regs[1], &env->regs[r2],
                             &env->regs[r2 + 1], type);
     case 114: /* CPACF_PRNO_TRNG */
-        fill_buf_random(env, mmu_idx, ra, &env->regs[r1], &env->regs[r1 + 1]);
-        fill_buf_random(env, mmu_idx, ra, &env->regs[r2], &env->regs[r2 + 1]);
-        break;
+        cc = fill_buf_random(env, mmu_idx, ra,
+                             &env->regs[r1], &env->regs[r1 + 1]);
+        if (cc == 0) {
+            cc = fill_buf_random(env, mmu_idx, ra,
+                                 &env->regs[r2], &env->regs[r2 + 1]);
+        }
+        return cc;
     default:
         /* we don't implement any other subfunction yet */
         g_assert_not_reached();
Signature · 1 line
2.55.0
Reviewed-by on v1 1/2
#2 lore raw
Quoted 40 lines · Ilya Leoshkevich

fill_buf_random() writes the entire guest-requested amount of random bytes in one go. Since the length is a full 64-bit value, a guest can request several gigabytes and keep the vCPU spinning inside the helper, without a chance to react to interrupts.

Do the same thing as cpacf_sha512(): process only a limited amount of data per instruction execution and report partial completion with condition code 3. This way the guest reissues the instruction, and pending interrupts can be handled in between.

Fixes 3dbc5fdacb5a ("target/s390x: support PRNO_TRNG instruction")
---
  target/s390x/tcg/crypto_helper.c | 25 +++++++++++++++++++------
  1 file changed, 19 insertions(+), 6 deletions(-)
target/s390x/tcg/crypto_helper.c +4 -3
index 8fe0a222198..4902c4e93ba 100644
@@ -242,12 +242,13 @@ static int cpacf_sha512(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
      return !len ? 0 : 3;
  }
 
-static void fill_buf_random(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
-                            uint64_t *buf_reg, uint64_t *len_reg)
+static int fill_buf_random(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
+                           uint64_t *buf_reg, uint64_t *len_reg)
  {
+    enum { MAX_BYTES_PER_RUN = 8192 }; /* Arbitrary: keep interactivity. */
      const MemOpIdx oi = make_memop_idx(MO_8, mmu_idx);
      uint8_t tmp[256];
-    uint64_t len = *len_reg;
+    uint64_t len = *len_reg, processed = 0;
      int buf_reg_len = 64;
 
      if (!(env->psw.mask & PSW_MASK_64)) {

target/s390x/tcg/crypto_helper.c static void fill_buf_random(CPUS390XState *env, const int mmu_idx, uintptr_t ra, · line 264

Quoted 6 lines
target/s390x/tcg/crypto_helper.c +3 -0
@@ -258,6 +259,10 @@ static void fill_buf_random(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
      while (len) {
          size_t block = MIN(len, sizeof(tmp));
 
+        if (processed >= MAX_BYTES_PER_RUN) {
+            break;
+        }

Alternately, stop with cpu_loop_exit_requested() at the bottom of the loop.

Either way,

r~

#3 lore raw

PATCH 2/2 tests/tcg/s390x: Test PRNO TRNG interruptibility

In reply to Ilya Leoshkevich

Add a small test that issues a large PRNO TRNG request while a timer is running, and checks that the timer interrupts it several times.

---
 tests/tcg/s390x/Makefile.target |  1 +
 tests/tcg/s390x/prno-trng.c     | 67 +++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+)
 create mode 100644 tests/tcg/s390x/prno-trng.c
tests/tcg/s390x/Makefile.target +1 -0
index 0ca030ded01..48cba4cdfa4 100644
@@ -75,6 +75,7 @@ Z13_TESTS+=vcksm
 Z13_TESTS+=vstl
 Z13_TESTS+=vrep
 Z13_TESTS+=precise-smc-user
+Z13_TESTS+=prno-trng
 $(Z13_TESTS): CFLAGS+=-march=z13 -O2
 TESTS+=$(Z13_TESTS)
 
tests/tcg/s390x/prno-trng.c new file +67 -0

67 lines not on this page · raw message

Signature · 1 line
2.55.0