[Qemu-devel] [PATCH v2 12/14] linux-user/aarch64: Use qemu_getrandom for arm_init_pauth_key

Richard Henderson posted 14 patches 6 years, 11 months ago
Maintainers: Richard Henderson <rth@twiddle.net>, Peter Maydell <peter.maydell@linaro.org>, Paolo Bonzini <pbonzini@redhat.com>, Riku Voipio <riku.voipio@iki.fi>, Gerd Hoffmann <kraxel@redhat.com>, Laurent Vivier <laurent@vivier.eu>
[Qemu-devel] [PATCH v2 12/14] linux-user/aarch64: Use qemu_getrandom for arm_init_pauth_key
Posted by Richard Henderson 6 years, 11 months ago
Use a better interface for random numbers than rand * 3.

Cc: Laurent Vivier <laurent@vivier.eu>
Cc: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
 linux-user/aarch64/cpu_loop.c | 16 ++--------------
 1 file changed, 2 insertions(+), 14 deletions(-)

diff --git a/linux-user/aarch64/cpu_loop.c b/linux-user/aarch64/cpu_loop.c
index d75fd9d3e2..ad30cab52d 100644
--- a/linux-user/aarch64/cpu_loop.c
+++ b/linux-user/aarch64/cpu_loop.c
@@ -20,6 +20,7 @@
 #include "qemu/osdep.h"
 #include "qemu.h"
 #include "cpu_loop-common.h"
+#include "qemu/random.h"
 
 #define get_user_code_u32(x, gaddr, env)                \
     ({ abi_long __r = get_user_u32((x), (gaddr));       \
@@ -147,22 +148,9 @@ void cpu_loop(CPUARMState *env)
     }
 }
 
-static uint64_t arm_rand64(void)
-{
-    int shift = 64 - clz64(RAND_MAX);
-    int i, n = 64 / shift + (64 % shift != 0);
-    uint64_t ret = 0;
-
-    for (i = 0; i < n; i++) {
-        ret = (ret << shift) | rand();
-    }
-    return ret;
-}
-
 void arm_init_pauth_key(ARMPACKey *key)
 {
-    key->lo = arm_rand64();
-    key->hi = arm_rand64();
+    qemu_getrandom(key, sizeof(*key), false);
 }
 
 void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
-- 
2.17.1


Re: [Qemu-devel] [PATCH v2 12/14] linux-user/aarch64: Use qemu_getrandom for arm_init_pauth_key
Posted by Philippe Mathieu-Daudé 6 years, 10 months ago
On 3/14/19 5:55 AM, Richard Henderson wrote:
> Use a better interface for random numbers than rand * 3.
> 
> Cc: Laurent Vivier <laurent@vivier.eu>
> Cc: Peter Maydell <peter.maydell@linaro.org>
> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
> ---
>  linux-user/aarch64/cpu_loop.c | 16 ++--------------
>  1 file changed, 2 insertions(+), 14 deletions(-)
> 
> diff --git a/linux-user/aarch64/cpu_loop.c b/linux-user/aarch64/cpu_loop.c
> index d75fd9d3e2..ad30cab52d 100644
> --- a/linux-user/aarch64/cpu_loop.c
> +++ b/linux-user/aarch64/cpu_loop.c
> @@ -20,6 +20,7 @@
>  #include "qemu/osdep.h"
>  #include "qemu.h"
>  #include "cpu_loop-common.h"
> +#include "qemu/random.h"
>  
>  #define get_user_code_u32(x, gaddr, env)                \
>      ({ abi_long __r = get_user_u32((x), (gaddr));       \
> @@ -147,22 +148,9 @@ void cpu_loop(CPUARMState *env)
>      }
>  }
>  
> -static uint64_t arm_rand64(void)
> -{
> -    int shift = 64 - clz64(RAND_MAX);
> -    int i, n = 64 / shift + (64 % shift != 0);
> -    uint64_t ret = 0;
> -
> -    for (i = 0; i < n; i++) {
> -        ret = (ret << shift) | rand();
> -    }
> -    return ret;
> -}
> -
>  void arm_init_pauth_key(ARMPACKey *key)
>  {
> -    key->lo = arm_rand64();
> -    key->hi = arm_rand64();
> +    qemu_getrandom(key, sizeof(*key), false);

PAC keys are define by a AA64 spec so are unlikely to have their
structure modified, thus:

Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>

>  }
>  
>  void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
>