Bug #12456 [Fbk->Opn]: srand() causing complication on hardware with broken clock
| From: | daniel dot oconnor at gmail dot com | Date: | Mon, 23 Mar 2009 17:00:08 +0000 |
| Subject: | Bug #12456 [Fbk->Opn]: srand() causing complication on hardware with broken clock | ||
| References: | 1 | Groups: | php.pear.bugs |
| Request: | Send a blank email to pear-bugs+get-238@lists.php.net to get a copy of this message | ||
Edit report at http://pear.php.net/bugs/bug.php?id=12456&edit=1
ID: 12456
Updated by: daniel.oconnor@gmail.com
Reported By: jonlogic at gmail dot com
Summary: srand() causing complication on hardware with broken
clock
-Status: Feedback
+Status: Open
Type: Bug
Package: Mail_Mime
Operating System: Linux 2.6
Package Version: 1.5.2
PHP Version: 5.2.4
Assigned To: walter
Roadmap Versions:
New Comment:
-Status: Feedback
+Status: Open
Previous Comments:
------------------------------------------------------------------------
[2008-08-27 09:42:44] jonl
> Jon, could you please let us know if the patch solves
> the problem?
Walter, after I initially removed the
"srand((double)microtime()*1000000);" line I did not experience the
problem again, even though $boundary was still generated with
microtime().
The patch works but keep the above in mind. As an experiment I can
change the $boundary to use uniqid() as per the patch but keep the
"srand((double)microtime()*1000000);" line. I believe the problem will
reoccur since uniqid() uses the same entropy (as you already said). Let
me know if this is necessary and I will try it out. Otherwise thanks
for your time!
------------------------------------------------------------------------
[2008-03-23 12:25:05] walter
I am not sure if the suggested patch would solve the problem. From a
quick look of the PHP source, lcg (which is used by uniqid(.., true))
seems to use the pid/threadid and... microtime as sources of entropy.
So, I think that also the call to uniqid would produce the same boundary
string twice. Jon, could you please let us know if the patch solves the
problem?
There are some alternatives if it doesn't. We don't need a
cryptographically secure random number so we can do any trick.
I agree that srand() can go since we depend on PHP 4.3.0. The wrong
version number in package.xml is split off to another bug and will be
fixed.
------------------------------------------------------------------------
[2007-11-17 02:59:22] cwiedmann
> And the PHP manual says that since PHP 4.2.0, srand() no
> longer needs to be called prior to rand() as rand() now
> automatically seeds the generator.
You are right. In addition, the minimum PHP version for this package is
not 4.0.0, like you can read in "package.xml", instead it is 4.3.0
(which is another bug).
So it's save to remove the call to srand().
In the attached patch (against CVS), I have also changed the boundary
from:
| '=_' . md5(rand() . microtime());
to:
| uniqid(md5(rand()), true);
(And, of course, the PHP versino in "package.xml")
Regards,
Carsten
------------------------------------------------------------------------
[2007-11-15 14:44:35] jonl
Description:
------------
Mail/mimePart.php, function encode():
srand((double)microtime()*1000000);
$boundary = '=_' . md5(rand() . microtime());
My development server has a slow/stale clock due a broken SMI
implementation (see
http://lists.us.dell.com/pipermail/linux-poweredge/2004-July/015554.html)
so microtime() often returns the same value across multiple
microsecond.
Therefore, the "randomness" of $boundary depends on rand() to return
different values during successive iterations of encode().
However, rand() is returning the same value (probably due to this same
hardware interrupt problem affecting microtime())
This is causing a problem with multiple encodings within the same
message. For example, a message containing plaintext, HTML and a file
attachment should have 2 different boundary strings like this:
"
MIME-Version: 1.0
From: MY_EMAIL_ADDRESS
Subject: Test message: Thu, 15 Nov 07 14:31:10 -0500
Content-Type: multipart/mixed;
boundary="=_fb952fbfe782713c888ca0ab55c41f19"
--=_fb952fbfe782713c888ca0ab55c41f19
Content-Type: multipart/alternative;
boundary="=_72d79573e8e611e59014b59ee526ad5f"
--=_72d79573e8e611e59014b59ee526ad5f
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset="ISO-8859-1"
This is the plaintext body.
--=_72d79573e8e611e59014b59ee526ad5f
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset="ISO-8859-1"
<b>This is the HTML body.</b>
--=_72d79573e8e611e59014b59ee526ad5f--
--=_fb952fbfe782713c888ca0ab55c41f19
Content-Transfer-Encoding: base64
Content-Type: text/plain;
name="TestFile1.txt";
Content-Disposition: attachment;
filename="TestFile1.txt";
Q29udGVudHMgb2YgVGVzdEZpbGUxLnR4dAo=
--=_fb952fbfe782713c888ca0ab55c41f19--
"
But (intermittently) the message has the same boundary strings for both
levels of encoding like this:
"
MIME-Version: 1.0
From: MY_EMAIL_ADDRESS
Subject: Test message: Thu, 15 Nov 07 14:31:12 -0500
Content-Type: multipart/mixed;
boundary="=_25b8085bab879c9b936de3ad92f5bee3"
--=_25b8085bab879c9b936de3ad92f5bee3
Content-Type: multipart/alternative;
boundary="=_25b8085bab879c9b936de3ad92f5bee3"
--=_25b8085bab879c9b936de3ad92f5bee3--
--=_25b8085bab879c9b936de3ad92f5bee3
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset="ISO-8859-1"
This is the plaintext body.
--=_25b8085bab879c9b936de3ad92f5bee3
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset="ISO-8859-1"
<b>This is the HTML body.</b>
--=_25b8085bab879c9b936de3ad92f5bee3--
"
Removing the call to srand() (in the line prior) stops this
intermittent and problematic behavior:
//srand((double)microtime()*1000000);
And the PHP manual says that since PHP 4.2.0, srand() no longer needs
to be called prior to rand() as rand() now automatically seeds the
generator.
------------------------------------------------------------------------
--
Edit this bug report at http://pear.php.net/bugs/bug.php?id=12456&edit=1