Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:97495 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 20303 invoked from network); 30 Dec 2016 23:21:42 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Dec 2016 23:21:42 -0000 Authentication-Results: pb1.pair.com smtp.mail=yohgaki@ohgaki.net; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=yohgaki@ohgaki.net; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ohgaki.net designates 180.42.98.130 as permitted sender) X-PHP-List-Original-Sender: yohgaki@ohgaki.net X-Host-Fingerprint: 180.42.98.130 ns1.es-i.jp Received: from [180.42.98.130] ([180.42.98.130:35826] helo=es-i.jp) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 8E/AD-04761-DFBE6685 for ; Fri, 30 Dec 2016 18:21:38 -0500 Received: (qmail 16738 invoked by uid 89); 30 Dec 2016 23:21:30 -0000 Received: from unknown (HELO mail-wm0-f52.google.com) (yohgaki@ohgaki.net@74.125.82.52) by 0 with ESMTPA; 30 Dec 2016 23:21:30 -0000 Received: by mail-wm0-f52.google.com with SMTP id a197so330133357wmd.0 for ; Fri, 30 Dec 2016 15:21:29 -0800 (PST) X-Gm-Message-State: AIkVDXIkjd4DDlMCpFABfSF27rf13MSYOXz1Nqc2DZzBglVzXHiJLUSSJOxWQ5kMX//5JwZ0THLqztMII4O3yg== X-Received: by 10.28.67.69 with SMTP id q66mr40041867wma.22.1483140083225; Fri, 30 Dec 2016 15:21:23 -0800 (PST) MIME-Version: 1.0 Received: by 10.194.38.7 with HTTP; Fri, 30 Dec 2016 15:20:42 -0800 (PST) Date: Sat, 31 Dec 2016 08:20:42 +0900 X-Gmail-Original-Message-ID: Message-ID: To: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=94eb2c0d4582d020ac0544e87562 Subject: Use decent entropy for uniqid($prefix, TRUE) From: yohgaki@ohgaki.net (Yasuo Ohgaki) --94eb2c0d4582d020ac0544e87562 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi all, I thought we must fix due to proposed PHPMailer bug fix patch. (See below for detail) Previous discussion went wrong because of compatibility misunderstanding. There is _no_ additional BC issue. Please keep in mind this. This is simple change proposal replacing weak entropy to string one. Background: uniqid() returns timestamp or timestamp+random value which is based on current timestamp. It is obvious that current uniqid() has fatal problems, uniqid() does not produce acceptable unique ID unlike the name implies. https://php.net/uniqid What are the fatal problems: 1) uniqid() (w/o more_entropy option) returns timestamp simply, tries to guarantee uniqueness by sleeping short amount of time for a process. This behavior may create duplicates among processes and by system clock adjustment. i.e. NTP or like. This issue is not addressed by proposed patch. 2) uniqid('', TRUE) (w/ more_entropy option) should return acceptable unique ID, but it's not. This is due to more_entropy is generated by timestamp, i.e. combined_lcg(), and issue like 1) exists. Proposed patch fixes this issue. Use of combined LCG for uniqid() entropy does not make sense now. Solution: Current PHP has php_random_int/bytes() which are much better entropy than combined_lcg(). php_random_int/bytes() use CSPRNG. Note: There is no additional compatibility issue at all with php_random_int/bytes(). Previously merged patch discussion regarding uniqid() improvement was ruined compatibility FUD. open_basedir restriction is irrelevant to internal functions. PRNG failure is unlikely, and the system is unusable anyway if it fails. e.g. random functions, session, crypt related functions do not work at all. Although stronger entropy is needed, uniqid('', TRUE) will have acceptable uniqueness similar to UUID by replacing CSPRNG entropy. https://en.wikipedia.org/wiki/Universally_unique_identifier This kind of change is simple enough change to be merged as "Simple improvement for new release" or even "Simple bug fix for released versions", IMO. I'm posting this mail to ask comments because of previous discussion for this issue. Following patch is basically the same as patch I committed and reverted before. I suppose nobody insists RFC for this change now. I'll merge the patch to master (7.2) if there is no comment. Patch: $ git diff diff --git a/ext/standard/uniqid.c b/ext/standard/uniqid.c index f429e6d..80dacdb 100644 --- a/ext/standard/uniqid.c +++ b/ext/standard/uniqid.c @@ -35,7 +35,7 @@ #include #endif -#include "php_lcg.h" +#include "php_random.h" #include "uniqid.h" /* {{{ proto string uniqid([string prefix [, bool more_entropy]]) @@ -77,7 +77,9 @@ PHP_FUNCTION(uniqid) * digits for usecs. */ if (more_entropy) { - uniqid =3D strpprintf(0, "%s%08x%05x%.8F", prefix, sec, usec, php_combined_lcg() * 10); + zend_long rand; + php_random_int(1000000000, 9999999999, &rand, 1); + uniqid =3D strpprintf(0, "%s%08x%05x%.8F", prefix, sec, usec, (double)rand/10000000000); } else { uniqid =3D strpprintf(0, "%s%08x%05x", prefix, sec, usec); } Proposed patch for PHPMailer command injection issue: I found following code(patch) for PHPMailer security issue. https://core.trac.wordpress.org/attachment/ticket/37210/0001 -Upgrade-PHPMailer-from-5.2.14-to-5.2.19.patch 2086 * Create unique ID 2087 * @return string 2088 */ 2089 protected function generateId() { 2090 return md5(uniqid(time())); 2024 2091 } 2025 2092 2026 2093 /** =E2=80=A6=E2=80=A6class PHPMailer 2034 2101 { 2035 2102 $body =3D ''; 2036 2103 //Create unique IDs and preset boundaries 2037 $this->uniqueid =3D md5(uniqid(time())); 2104 $this->uniqueid =3D $this->generateId(); 2038 2105 $this->boundary[1] =3D 'b1_' . $this->uniqueid; 2039 2106 $this->boundary[2] =3D 'b2_' . $this->uniqueid; 2040 2107 $this->boundary[3] =3D 'b3_' . $this->uniqueid; Although I never recommend such code, the ID is good enough for this specific usage. I think we should remove the goccha, "uniqid() is not unique". This code explains why. Related RFC: Improve uniqid() uniqueness https://wiki.php.net/rfc/uniqid IMHO, we should enable more_entropy by default, with stronger entropy prefered. Regards, -- Yasuo Ohgaki yohgaki@ohgaki.net --94eb2c0d4582d020ac0544e87562--