Skip to content

Commit 193e5e4

Browse files
committed
Add a warning about the footgun that is combining legacy MT random functions and pcntl_fork
Fixes php/php-src#21351 Replaces php/php-src#21352
1 parent 63bd68a commit 193e5e4

9 files changed

Lines changed: 50 additions & 0 deletions

File tree

language-snippets.ent

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,28 @@ highly discouraged.</simpara></warning>'>
3131
</para>
3232
</caution>'>
3333

34+
<!ENTITY caution.mt19937-fork-unsafe '<caution xmlns="http://docbook.org/ns/docbook">
35+
<para>
36+
This method uses a lazy-initialized, global Mt19937 state.
37+
</para>
38+
<para>
39+
If your code seeds the Mt19937 engine before <function>pcntl_fork</function>
40+
(including indirectly via calls to
41+
<function>rand</function>,
42+
<function>mt_rand</function>,
43+
<function>shuffle</function>,
44+
<function>array_rand</function> or
45+
<function>str_shuffle</function>),
46+
forked children will inherit the parent seed and produce identical sequences.
47+
</para>
48+
<para>
49+
If your code calls <function>pcntl_fork</function> without prior calls, every child will have a unique seed and produce a unique sequence.
50+
</para>
51+
<para>
52+
Prefer using <classname>Random\Randomizer</classname> methods in all newly written code.
53+
</para>
54+
</caution>'>
55+
3456
<!ENTITY caution.mt19937-tiny-seed '<caution xmlns="http://docbook.org/ns/docbook">
3557
<para>
3658
Because the Mt19937 (“Mersenne Twister”) engine accepts only a single 32 bit integer as the

reference/array/functions/array-rand.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
key (or keys) of the random entries.
1818
</para>
1919
&caution.cryptographically-insecure;
20+
&caution.mt19937-fork-unsafe;
2021
</refsect1>
2122
<refsect1 role="parameters">
2223
&reftitle.parameters;

reference/array/functions/shuffle.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
This function shuffles (randomizes the order of the elements in) an array.
1616
</para>
1717
&caution.cryptographically-insecure;
18+
&caution.mt19937-fork-unsafe;
1819
</refsect1>
1920
<refsect1 role="parameters">
2021
&reftitle.parameters;

reference/pcntl/functions/pcntl-fork.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,27 @@ if ($pid == -1) {
6262
</para>
6363
</refsect1>
6464

65+
<refsect1 role="notes">
66+
&reftitle.notes;
67+
<caution>
68+
<para>
69+
The addition or removal of legacy random functions
70+
(<function>srand</function>,
71+
<function>mt_srand</function>,
72+
<function>rand</function>,
73+
<function>mt_rand</function>,
74+
<function>shuffle</function>,
75+
<function>array_rand</function> or
76+
<function>str_shuffle</function>)
77+
in any code executed prior to <function>pcntl_fork</function> can drastically
78+
alter the behavior of these legacy random functions after forking.
79+
</para>
80+
<para>
81+
Prefer using <classname>Random\Randomizer</classname> methods in all newly written code.
82+
</para>
83+
</caution>
84+
</refsect1>
85+
6586
<refsect1 role="seealso">
6687
&reftitle.seealso;
6788
<para>

reference/random/functions/mt-rand.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
15)</literal>.
3636
</simpara>
3737
&caution.cryptographically-insecure;
38+
&caution.mt19937-fork-unsafe;
3839
</refsect1>
3940
<refsect1 role="parameters">
4041
&reftitle.parameters;

reference/random/functions/mt-srand.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
&note.randomseed;
2222
&caution.mt19937-tiny-seed;
23+
&caution.mt19937-fork-unsafe;
2324

2425
</refsect1>
2526
<refsect1 role="parameters">

reference/random/functions/rand.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
15)</literal>.
2626
</simpara>
2727
&caution.cryptographically-insecure;
28+
&caution.mt19937-fork-unsafe;
2829
<note>
2930
<simpara>
3031
Prior to PHP 7.1.0, <function>getrandmax</function> was only 32767 on some

reference/random/functions/srand.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
&note.randomseed;
2222
&caution.mt19937-tiny-seed;
23+
&caution.mt19937-fork-unsafe;
2324
<note><simpara>As of PHP 7.1.0, <function>srand</function> has been made
2425
an alias of <function>mt_srand</function>.</simpara>
2526
</note>

reference/strings/functions/str-shuffle.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
of all possible is created.
1818
</simpara>
1919
&caution.cryptographically-insecure;
20+
&caution.mt19937-fork-unsafe;
2021
</refsect1>
2122

2223
<refsect1 role="parameters">

0 commit comments

Comments
 (0)