Skip to contentSkip to conversation

target/s390x/tcg: fix register update on guest memory fault in cpacf_aes_ecb

PATCH In review Andrew Holden 3 participants 4 messages Oct 8 – Oct 14

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/s390x/tcg: fix register update on guest memory fault in cpacf_aes_ecb

The KM instruction loop in cpacf_aes_ecb() updated the source pointer, destination pointer, and length registers only after the entire loop completed, using a 'done' accumulator. If a guest memory access fault fires mid-loop, siglongjmp unwinds the C stack before the register update is reached. The registers then reflect block 0 rather than the block being processed, causing the instruction to restart from the beginning on every fault. This results in quadratic reprocessing and violates the PoP requirement that registers reflect partial completion per block processed.

Fix this by updating the registers inside the loop body after each successfully processed block, and removing the 'done' accumulator.

---
 target/s390x/tcg/cpacf_aes.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
target/s390x/tcg/cpacf_aes.c +8 -10
index 92884c4120..41ebc57087 100644
@@ -62,7 +62,7 @@ int cpacf_aes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
 {
     enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE };
     uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE];
-    uint64_t len = *src_len_reg, done = 0;
+    uint64_t len = *src_len_reg;
     int i, keysize, addr_reg_size;
     uint8_t key[32];
     AES_KEY exkey;
@@ -116,23 +116,21 @@ int cpacf_aes_ecb(CPUS390XState *env, const int mmu_idx, uintptr_t ra,
 
     /* process up to MAX_BLOCKS_PER_RUN aes blocks */
     for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) {
-        aes_read_block(env, mmu_idx, ra, *src_ptr_reg + done, in);
+        aes_read_block(env, mmu_idx, ra, *src_ptr_reg, in);
         if (mod) {
             AES_decrypt(in, out, &exkey);
         } else {
             AES_encrypt(in, out, &exkey);
         }
-        aes_write_block(env, mmu_idx, ra, *dst_ptr_reg + done, out);
+        aes_write_block(env, mmu_idx, ra, *dst_ptr_reg, out);
+        *src_ptr_reg = deposit64(*src_ptr_reg, 0, addr_reg_size,
+                                 *src_ptr_reg + AES_BLOCK_SIZE);
+        *dst_ptr_reg = deposit64(*dst_ptr_reg, 0, addr_reg_size,
+                                 *dst_ptr_reg + AES_BLOCK_SIZE);
+        *src_len_reg -= AES_BLOCK_SIZE;
         len -= AES_BLOCK_SIZE;
-        done += AES_BLOCK_SIZE;
     }
 
-    *src_ptr_reg = deposit64(*src_ptr_reg, 0, addr_reg_size,
-                             *src_ptr_reg + done);
-    *dst_ptr_reg = deposit64(*dst_ptr_reg, 0, addr_reg_size,
-                             *dst_ptr_reg + done);
-    *src_len_reg -= done;
-
     return !len ? 0 : 3;
 }
 
Signature · 1 line
2.56.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 31 lines · Andrew Holden

The KM instruction loop in cpacf_aes_ecb() updated the source pointer, destination pointer, and length registers only after the entire loop completed, using a 'done' accumulator. If a guest memory access fault fires mid-loop, siglongjmp unwinds the C stack before the register update is reached. The registers then reflect block 0 rather than the block being processed, causing the instruction to restart from the beginning on every fault. This results in quadratic reprocessing and violates the PoP requirement that registers reflect partial completion per block processed.

Fix this by updating the registers inside the loop body after each successfully processed block, and removing the 'done' accumulator.

---
 target/s390x/tcg/cpacf_aes.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
target/s390x/tcg/cpacf_aes.c +1 -1
index 92884c4120..41ebc57087 100644
@@ -62,7 +62,7 @@ int cpacf_aes_ecb(CPUS390XState *env, const int
 mmu_idx, uintptr_t ra,
 {
     enum { MAX_BLOCKS_PER_RUN = 8192 / AES_BLOCK_SIZE };
     uint8_t in[AES_BLOCK_SIZE], out[AES_BLOCK_SIZE];
-    uint64_t len = *src_len_reg, done = 0;
+    uint64_t len = *src_len_reg;
     int i, keysize, addr_reg_size;
     uint8_t key[32];
Quoted 1 line
     AES_KEY exkey;

Try to hold this bunch of variable declarations as a "reverse Christmas tree". I am not sure if this is a strong coding guideline for qemu but there are a lot of open source projects insisting on this.

Quoted 5 lines
target/s390x/tcg/cpacf_aes.c +0 -0
@@ -116,23 +116,21 @@ int cpacf_aes_ecb(CPUS390XState *env, const int
 mmu_idx, uintptr_t ra,
 
     /* process up to MAX_BLOCKS_PER_RUN aes blocks */
     for (i = 0; i < MAX_BLOCKS_PER_RUN && len >= AES_BLOCK_SIZE; i++) 

target/s390x/tcg/cpacf_aes.c int cpacf_aes_ecb(CPUS390XState *env, const int · line 127

Quoted 10 lines
target/s390x/tcg/cpacf_aes.c +2 -2
 {
-        aes_read_block(env, mmu_idx, ra, *src_ptr_reg + done, in);
+        aes_read_block(env, mmu_idx, ra, *src_ptr_reg, in);
         if (mod) {
             AES_decrypt(in, out, &exkey);
         } else {
             AES_encrypt(in, out, &exkey);
         }
-        aes_write_block(env, mmu_idx, ra, *dst_ptr_reg + done, out);
+        aes_write_block(env, mmu_idx, ra, *dst_ptr_reg, out);

A comment here like
/* update registers at end of each loop */
or so would be nice.

Quoted 16 lines
target/s390x/tcg/cpacf_aes.c +5 -7
+        *src_ptr_reg = deposit64(*src_ptr_reg, 0, addr_reg_size,
+                                 *src_ptr_reg + AES_BLOCK_SIZE);
+        *dst_ptr_reg = deposit64(*dst_ptr_reg, 0, addr_reg_size,
+                                 *dst_ptr_reg + AES_BLOCK_SIZE);
+        *src_len_reg -= AES_BLOCK_SIZE;
         len -= AES_BLOCK_SIZE;
-        done += AES_BLOCK_SIZE;
     }
 
-    *src_ptr_reg = deposit64(*src_ptr_reg, 0, addr_reg_size,
-                             *src_ptr_reg + done);
-    *dst_ptr_reg = deposit64(*dst_ptr_reg, 0, addr_reg_size,
-                             *dst_ptr_reg + done);
-    *src_len_reg -= done;
-
     return !len ? 0 : 3;
Quoted 1 line

}

Nice starting point.

#2 lore raw
Quoted 4 lines · Harald Freudenberger

Try to hold this bunch of variable declarations as a "reverse Christmas tree". I am not sure if this is a strong coding guideline for qemu but there are a lot of open source projects insisting on this.

[...]

Quoted 3 lines

A comment here like
  /* update registers at end of each loop */
or so would be nice.

Will do both in v2, thanks.

Signature · 4 lines
Le gach dea-ghuí,
Andrew H

IBM, Waterford IE
#3 lore raw
In reply to Andrew Holden
Quoted 10 lines · Andrew Holden

The KM instruction loop in cpacf_aes_ecb() updated the source pointer, destination pointer, and length registers only after the entire loop completed, using a 'done' accumulator. If a guest memory access fault fires mid-loop, siglongjmp unwinds the C stack before the register update is reached. The registers then reflect block 0 rather than the block being processed, causing the instruction to restart from the beginning on every fault. This results in quadratic reprocessing and violates the PoP requirement that registers reflect partial completion per block processed.

Quoted 7 lines

Fix this by updating the registers inside the loop body after each successfully processed block, and removing the 'done' accumulator.

---
  target/s390x/tcg/cpacf_aes.c | 18 ++++++++----------
  1 file changed, 8 insertions(+), 10 deletions(-)

In addition to what Harald already mentioned, it would be good to contribute a linux-user test.