Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98043 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 93994 invoked from network); 30 Jan 2017 13:20:45 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Jan 2017 13:20:45 -0000 Authentication-Results: pb1.pair.com smtp.mail=cmbecker69@gmx.de; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=cmbecker69@gmx.de; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmx.de designates 212.227.17.22 as permitted sender) X-PHP-List-Original-Sender: cmbecker69@gmx.de X-Host-Fingerprint: 212.227.17.22 mout.gmx.net Received: from [212.227.17.22] ([212.227.17.22:60801] helo=mout.gmx.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5A/45-51557-CAD3F885 for ; Mon, 30 Jan 2017 08:20:45 -0500 Received: from [192.168.2.109] ([217.82.227.219]) by mail.gmx.com (mrgmx102 [212.227.17.168]) with ESMTPSA (Nemesis) id 0M6P5z-1cI9EM1g55-00yNgq; Mon, 30 Jan 2017 14:20:21 +0100 To: Yasuo Ohgaki , "internals@lists.php.net" References: Message-ID: Date: Mon, 30 Jan 2017 14:20:37 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Provags-ID: V03:K0:Z/9KdqAijzPNh6/+nRNdNOJESZ+wQF7V7yxNv033TNDeqaITMsH LzUQVxc42xN0JtKjACY9MsHqeDrvwLXf08WrI9H/mZ0MpGsm8ljKc8RUthJfnAoBWrjwOhh q7VHpsHC0sh6imKO/8lrUpBF8BFGFys9D+KhNvwS6+UOfna2tWg31WWHHe1CKlMSuc4J0u/ HncmBz8Ire0y/tC8CA91Q== X-UI-Out-Filterresults: notjunk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kefOiWMffO3MM7xjMcGdc4WQs1ItSN/s9HWD6psQDYNQoAubaxMbOM58wAEdw== Subject: Re: Reseeding rand()/mt_rand() From: cmbecker69@gmx.de ("Christoph M. Becker") On 30.01.2017 at 13:32, Yasuo Ohgaki wrote: > Hi all, > > Following code is problematic and needs proper reseeding to work. > > // We need the same random numbers here > mt_srand(1234); > for ($i=0; $i < 10; $i++) { > $my_rand[] = mt_rand(); > } > > Somewhere later in code > > // We need somewhat random numbers for non CS purpose > for ($i=0; $i < 10; $i++) { > $my_other_rand[] = mt_rand(); > } > > $my_other_rand array is not random at all for the app. This applies to > rand()/srand() as well as all functions that use rand and rand/MT rand > internally. e.g. shuffle(). rand() is alias of mt_rand() in PHP 7.1, it can > cause serious problem. i.e. srand(1234) forces mt_rand() to generate non > random numbers. > > In addition, this behavior persists across requests because once > BG(mt_rand_is_seeded)/BG(rand_is_seeded) are set to 1, they are kept for > the process. Therefore, subsequent mt_rand()/rand(), including any > functions use rand/MT rand internally such as shuffle(), call in other > requests are not random. > > In order to get random numbers, we need to reseed RNG. > > Currently, it is possible to reseed like (int overflow is ignored) > > $seed = unpack("l", file_get_contents("/dev/urandom", false, NULL, 0, 4)); > mt_srand($seed[1]); > for ($i=0; $i < 10; $i++) { > $my_other_rand[] = mt_rand(); > } > > OR for PHP 7 > > mt_srand(random_int(PHP_INT_MIN, PHP_INT_MAX)); > for ($i=0; $i < 10; $i++) { > $my_other_rand[] = mt_rand(); > } > > Requiring these reseeding code for most mt_rand()/rand() calls is not > preferred behavior. > > Proposal: > 1) Add BG(mt_rand_is_user_seeded) and BG(rand_is_user_seeded). If they are > 1, set BG(mt_rand_is_seeded)=0 and BG(rand_is_seeded)=0. > 2) Make srand(0) and mt_srand(0) to force RNG reseeding by PHP. > > Outcome: > 1) resolves "across requests" non random numbers. > 2) simplifies reseeding. > > Problem: > 1) Added new BG values are BC for released versions. Simply reseeding by > current GENERATE_SEED() macro is weak and too easy to be guessed even with > MT rand. i.e. Setting BG(mt_rand_is_seeded)=0 and BG(rand_is_seeded)=0 at > RINIT is not exactly a good idea. (There is improvement discussion in > "Improving mt_rand() seed" thread) > 2) Manual reseeding API, srand(0)/mt_srand(0), is not compatible with older > versions. > > Open Issue: > 1) and 2), apply these to released versions or not. > > This idea is acceptable, but I don't like this idea myself. > > It seems we should do something for this, documentation for released > versions at least. > Any better ideas are appreciated. Just a quick idea: