max_execution_time does not take effect in crypt function.
Description
set max_execution_time=5
The following code:
<?php
$i=1;
while($i<=50000000000000000)
{
echo "The number is " . $i . "<br>";
$i++;
}
?>
get msg:
Fatal error: Maximum execution time of 5 seconds exceeded in D:\Samples\a.php on line 5
test sample
--TEST--
crypt() SHA-256
--FILE--
<?php
$tests = array(
1 => array(
'$5$saltstring',
'Hello world!',
'$5$saltstring$5B8vYYiY.CVt1RlTTf8KbXBH3hsxY/GNooZaBBGWEc5'
),
2 => array(
'$5$rounds=10000$saltstringsaltstring',
'Hello world!',
'$5$rounds=10000$saltstringsaltst$3xv.VbSHBb41AL9AvLeujZkZRBAwqFMz2.opqey6IcA'
),
3 => array(
'$5$rounds=10000$saltstringsaltstring',
'Hello world!',
'$5$rounds=10000$saltstringsaltst$3xv.VbSHBb41AL9AvLeujZkZRBAwqFMz2.opqey6IcA'
),
4 => array(
'$5$rounds=5000$toolongsaltstring',
'This is just a test',
'$5$rounds=5000$toolongsaltstrin$Un/5jzAHMgOGZ5.mWJpuVolil07guHPvOW8mGRcvxa5'
),
5 => array(
'$5$rounds=1400$anotherlongsaltstring',
'a very much longer text to encrypt. This one even stretches over morethan one line.',
'$5$rounds=1400$anotherlongsalts$Rx.j8H.h8HjEDGomFU8bDkXm3XIUnzyxf12oP84Bnq1'
),
6 => array(
'$5$rounds=77777$short',
'we have a short salt string but not a short password',
'$5$rounds=77777$short$JiO1O3ZpDAxGJeaDIuqCoEFysAe1mZNJRs3pw0KQRd/'
),
7 => array(
'$5$rounds=123456$asaltof16chars..',
'a short string',
'$5$rounds=123456$asaltof16chars..$gP3VQ/6X7UUEW3HkBn2w1/Ptq2jxPyzV/cZKmF/wJvD'
),
8 => array(
'$5$rounds=10$roundstoolow',
'the number of rounds is too low',
'*0'
),
9 => array(
'$5$rounds=1000000000$roundstoohigh',
'the number of rounds is too high',
'*0'
)
);
foreach ($tests as $iter => $t) {
$res = crypt($t[1], $t[0]);
if ($res != $t[2]) echo "Iteration $iter failed.
Expected: <$t[2]>
Got <$res>\n";
}
echo "Passes.";?>
--EXPECT--
Passes.
Running time is more than 5 seconds
I don't know if this is php's strategy to let the crypt function not be limited by the max_execution_time
PHP Version
php 7.4.27
Operating System
No response
这是一封自动回复邮件。已经收到您的来信,我会尽快回复。
Works for me. Are you sure your max_execution_time is set to a low number and that your test script really is taking that long? On my fairly high-end PC I had to set test 7's number of rounds in the millions using max_execution_time=1 to get a timeout, but it did time out.
Works for me. Are you sure your max_execution_time is set to a low number and that your test script really is taking that long? On my fairly high-end PC I had to set test 7's number of rounds in the millions using max_execution_time=1 to get a timeout, but it did time out.
use the above code,because I found it is different to crypt_sha256.phpt

I modified code:
set max_execution_time=5

print time:

can't run finish:

my pc: CPU Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz 2.90 GHz RAM 32.0 GB (31.8 GB 可用)
You're compiling a custom PHP for this? Why?
I think I know what you're reporting: if max_execution_time is reached while crypt() is running then it will not abort, and only when the function returns will PHP finally report the timeout.
So this file
<?php
set_time_limit(1);
crypt('This is just a test', '$5$rounds=999999999$asaltof16chars..');
echo 'This should not be outputted';
?>
will take (a lot) more than 1 second.
Is that correct?
You're compiling a custom PHP for this? Why?
I think I know what you're reporting: if max_execution_time is reached while crypt() is running then it will not abort, and only when the function returns will PHP finally report the timeout.
So this file
<?php set_time_limit(1); crypt('This is just a test', '$5$rounds=999999999$asaltof16chars..'); echo 'This should not be outputted'; ?>will take (a lot) more than 1 second.
Is that correct?
yes,Is there any way to make the time limit take effect? I compile a private php program to detect malicious php files, but some malicious files will use the crypt function to make the php program block, so I set a time limit in my private php program, but it doesn't work
@alexlongguo PHP doesn't check for timeouts in internal functions, only in PHP code and there's no way to make it to. Checking timeout is triggered somewhere in the VM between instructions (I don't remember where exactly).
I don't believe max_execution_time is meant to be used as a security measure. It's only a simple defense against scripts running for an unexpectedly long time (like because of infinite loops) and it's notorious for being measured differently between Windows and 'nix.
If you need a security measure to prevent PHP from running for too long then that should probably be enforced by the operating system.