From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39851) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XcJ8t-0006ao-HJ for qemu-devel@nongnu.org; Thu, 09 Oct 2014 15:12:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XcJ8o-0004gG-11 for qemu-devel@nongnu.org; Thu, 09 Oct 2014 15:12:51 -0400 Received: from mail-lb0-x236.google.com ([2a00:1450:4010:c04::236]:58003) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XcJ8n-0004gA-Pu for qemu-devel@nongnu.org; Thu, 09 Oct 2014 15:12:45 -0400 Received: by mail-lb0-f182.google.com with SMTP id z11so1803862lbi.13 for ; Thu, 09 Oct 2014 12:12:44 -0700 (PDT) From: Magnus Reftel Date: Thu, 9 Oct 2014 21:12:18 +0200 Message-Id: <1412881938-3757-2-git-send-email-reftel@spotify.com> In-Reply-To: <1412881938-3757-1-git-send-email-reftel@spotify.com> References: <1412881938-3757-1-git-send-email-reftel@spotify.com> Subject: [Qemu-devel] [PATCH] linux-user: Let user specify random seed List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Magnus Reftel This patch introduces the -seed command line option and the QEMU_RAND_SEED environment variable for setting the random seed, which is used for the AT_RANDOM ELF aux entry. Signed-off-by: Magnus Reftel --- linux-user/elfload.c | 1 - linux-user/main.c | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 1c04fcf..f2e2197 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -1539,7 +1539,6 @@ static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc, * Generate 16 random bytes for userspace PRNG seeding (not * cryptically secure but it's not the aim of QEMU). */ - srand((unsigned int) time(NULL)); for (i = 0; i < 16; i++) { k_rand_bytes[i] = rand(); } diff --git a/linux-user/main.c b/linux-user/main.c index 483eb3f..56e0e28 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -3546,6 +3546,18 @@ static void handle_arg_pagesize(const char *arg) } } +static void handle_arg_randseed(const char *arg) +{ + unsigned long long seed; + char *end; + + if (parse_uint(arg, &seed, &end, 0) != 0 || *end != 0 || seed > UINT_MAX) { + fprintf(stderr, "Invalid seed number: %s\n", arg); + exit(1); + } + srand(seed); +} + static void handle_arg_gdb(const char *arg) { gdbstub_port = atoi(arg); @@ -3674,6 +3686,8 @@ static const struct qemu_argument arg_table[] = { "", "run in singlestep mode"}, {"strace", "QEMU_STRACE", false, handle_arg_strace, "", "log system calls"}, + {"seed", "QEMU_RAND_SEED", true, handle_arg_randseed, + "", "Seed for pseudo-random number generator"}, {"version", "QEMU_VERSION", false, handle_arg_version, "", "display version information and exit"}, {NULL, NULL, false, NULL, NULL, NULL} @@ -3856,6 +3870,8 @@ int main(int argc, char **argv, char **envp) cpudef_setup(); /* parse cpu definitions in target config file (TBD) */ #endif + srand(time(NULL)); + optind = parse_args(argc, argv); /* Zero out regs */ @@ -3926,6 +3942,10 @@ int main(int argc, char **argv, char **envp) do_strace = 1; } + if (getenv("QEMU_RAND_SEED")) { + handle_arg_randseed(getenv("QEMU_RAND_SEED")); + } + target_environ = envlist_to_environ(envlist, NULL); envlist_free(envlist); -- 1.9.1