Repository navigation
Description
Description
Opcache saves values from ini_get() for PHP_INI_SYSTEM type directives in the opcache when optimization is enabled.
This is a problem when a shared file uses ini_get() for PHP_INI_SYSTEM.
Example of this is a server with multiple virtual hosts where the sites use a common/shared framework.
The framework has an image uploading part where it fetches the upload_tmp_dir setting from ini to use a place for temporary image manipulations.
The virtual hosts are set up using PHP_ADMIN_VALUE to set a specific upload folder for each site.
fastcgi_param PHP_ADMIN_VALUE "upload_tmp_dir=/tmp/uploads/site1";
In this scenario the framework uses ini_get('upload_tmp_dir') to get the tmp folder value, but the value it gets is always the value for the first site that included that php file. So if site "foo.example.com" was first, then ALL other sites will get the upload_tmp_dir from "foo.example.com".
I reproduced this in all versions >=7.2, and for cli, fpm and mod_php.
Security
Not sure this counts as a security problem but this means that information could leak between sites.
Note that using PHP_ADMIN_VALUE for any option results in this problem.
Ways to reproduce:
Setup php files
mkdir /tmp/test/ && cd /tmp/test/
echo '<?php echo "S: " . ini_get("upload_tmp_dir") . "\n";' > shared.php
echo '<?php echo "1: " . ini_get("upload_tmp_dir") . "\n"; include "./shared.php";' > 1.php
echo '<?php echo "2: " . ini_get("upload_tmp_dir") . "\n"; include "./shared.php";' > 2.phpCli
Create a cli-opcache.ini file in the "scan" additional .ini files directory:
zend_extension=opcache.so
[opcache]
opcache.enable=1
opcache.enable_cli=1
opcache.file_cache="/tmp/php-file-cache"
opcache.file_cache_only=1
opcache.file_cache_consistency_checks=1
Create file cache folder
mkdir /tmp/php-file-cacheRun the test
php -d upload_tmp_dir=/tmp/num1 /tmp/test/1.php
php -d upload_tmp_dir=/tmp/num2 /tmp/test/2.phpExpected result:
1: /tmp/num1
S: /tmp/num1
2: /tmp/num2
S: /tmp/num2
Actual result:
1: /tmp/num1
S: /tmp/num1
2: /tmp/num2
S: /tmp/num1
nginx + fpm
Make sure that opcache with optimizations is enabled.
Setup two virtual hosts and reload nginx:
server {
listen 80;
server_name p1;
root /tmp/test;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param PHP_ADMIN_VALUE "upload_tmp_dir=/tmp/cache1";
}
}
server {
listen 80;
server_name p2;
root /tmp/test;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param PHP_ADMIN_VALUE "upload_tmp_dir=/tmp/cache2";
}
}
Run the tests:
curl --resolve p1:80:127.0.0.1 http://p1/1.php
curl --resolve p2:80:127.0.0.1 http://p2/2.php
Expected result:
1: /tmp/cache1
S: /tmp/cache1
2: /tmp/cache2
S: /tmp/cache2
Actual result:
1: /tmp/cache1
S: /tmp/cache1
2: /tmp/cache2
S: /tmp/cache1
Workarounds
For me, I ended up with changing the shared framework, and now have to maintain my own fork of it.
You can turn off opcache optimizations.
Another way to work around this is to compile php yourself, with removing the ini_get if block, or even just
changing ini_get to ini_get_opcache_workaround in the zend_optimizer_eval_special_func_call function
in Zend/Optimizer/zend_optimizer.c.
PHP Version
7.2.0 - 8.1.6
Operating System
No response
Activity
arnaud-lb commentedon Jun 3, 2022
Does it happen only with
opcache.file_cacheenabled ? I would recommend to use a separate cache dir whenPHP_INI_SYSTEMentries differ.bix0r commentedon Jun 3, 2022
No, this always happens when opcache optimization is enabled.
arnaud-lb commentedon Jun 10, 2022
The problem seems to be that we allow
PHP_INI_SYSTEMentries to be modified on a per-request basis viafastcgi_param PHP_ADMIN_VALUE.The purpose of
PHP_ADMIN_VALUEis to make an ini setting non modifiable by the script (with e.g. ini_set), but apparently the way it is implemented allows to overridePHP_INI_SYSTEMentires.Since
PHP_INI_SYSTEMentries are not supposed to be modified, I think that the opcache makes the right assumption here.bix0r commentedon Jun 30, 2022
I guess you are correct that opcache is making the right assumption here based on that information. But, there must be a way to change settings based on the virtual host. Using the
php_admin_valueseems to be the best way to that.Using
php_admin_value(and friends) is also the recommended way, and it sounds quiet bad that doing things according to the documentation will result in leaking of information.The performance benefits from using opcache for these kind of settings seem insignificant enough that I can't see a reason for making this issue more complicated.
arnaud-lb commentedon Jun 30, 2022
I agree with this.
There are some
PHP_INI_SYSTEMentries that will really break PHP if they are changed like this, though.Maybe we should promote some ini settings to
PHP_INI_PERDIR(or introduce a new mode), and then preventPHP_INI_SYSTEMvalue from being modified viaPHP_ADMIN_VALUE.bukka commentedon Jul 6, 2022
That might break things potentially.
I added this to my big list of php_admin_value issues as there are lots of bugs related to overrides and similar problems so this looks like an another one to look at.
Add test for ini_get() opcache optimization
Remove opcache optimization for ini_get()
bukka commentedon Aug 29, 2022
As per discussion in #8997 I'm changing this to feature request as it is not an actual bug.
bukka commentedon Aug 29, 2022
@arnaud-lb
Just came back to this and want to check this with you:
What entries did you have in mind specifically? Please note that this can be already changed without opcache so if there some variables like this, it might be a bug...
bix0r commentedon Aug 31, 2022
I do agree with you all about changing how this ini + admin_value thing works, but this is still a bug! I am not asking for a new feature!
I am reporting behavior that has changed, is not (or at least wrongly) documented, and is quite disturbing (like listing temp files from different host on same machine (
sys_temp_dir)).I can not consider it a feature to be allowed to trust the information that is set in a documented way.
Note that setting PHP_INI_SYSTEM settings via php_admin_value on a virtual host works just as intended.
Specifying
upload_tmp_dirper vhost makes sure that uploaded files are indeed uploaded to the specified folder. It is just thatini_get()on a PHP_INI_SYSTEM value gets opcached per file, and thus can't be trusted.Also: Remember that this is not just a FPM issue, this is a problem with all sapis that support opcache optimization.
arnaud-lb commentedon Sep 2, 2022
I was not able to produce a test to demonstrate this in a reasonable time, but since INI_SYSTEM entries are not supposed to be changed after the startup phase, the code around them is probably not designed to handle changes later.
There could be initializations being made during the startup phase that would become inconsistent if we changed the value of the ini setting later.
I think that this is a valid bug, not necessarily an FPM or OPcache one.
bukka commentedon Sep 2, 2022
Ok I will change it back to bug and think we could maybe introduce that new ini type in 8.1. as it's not really ABI break or user visible change. We could just keep
ZEND_INI_SYSTEMand addZEND_INI_ADMINand let opcache just ignore that optimization if it's alsoZEND_INI_ADMIN. Basically it would be a flag that would just disable that optimization for selected inis. @bix0r Do you want to give it a try?bix0r commentedon Sep 6, 2022
@bukka By giving it a try you mean implementing this ADMIN flag?
Personally I still think that just skipping the opcache optimization is the right way to go because of the possible problems it creates without that much benefits. I think adding this ADMIN flag will make the core code more complicated for almost no reason.
But perhaps I am missing something here?
--
That said, I've taken a look at what would be needed to add this ADMIN flag.
I think "ADMIN" might be the wrong "name" for this. We have
php_ini_activate_per_host_config()andphp_ini_activate_per_dir_config()that also alter the ini entries withZEND_INI_SYSTEM. I assume these should also be included in this change, right? They are altering the ini config based on folder or host.Here are other working points:
I am not sure if I need to change anything in lightspeed and phpdbg sapis.
Is there any information about the stages of the INI system? I could not find it, and was struggling to figure out how they work. Looking at
zend_alter_ini_entry_ex()we have:It seems like it could matter. Not sure if this needs to be changed.
bukka commentedon Sep 30, 2022
Sorry for the late reply. I finally took a look into this and agree that it's a bit more work than I though.
I haven't done any changes in Zend ini so I might be wrong here but from looking to the code, it looks to me that
php_ini_activate_per_host_config()andphp_ini_activate_per_dir_config()are meant for loading the configuration during sapi activation. It means they use activate stage and allow modifying system. Think that partzend_alter_ini_entry_exis just allow modification of any entry basically so I wouldn't think there's any change required. But would have to try it to be sure. By the way I don't think there are any docs for stages. At least I'm not aware any.Otherwise your analysis seems correct to me. You will probably just see it if you start implementing it and things don't work... :)
I don't think disabling optimization is an option as Dmitry was against it. Also it's true that modifying some system ini should not be done after the init and might not even work for some options as it was mentioned. I think this is a better solution in general IMHO.
bix0r commentedon Oct 13, 2022
Just a quick update. I am working on this. I will create a draft PR soon(ish), but there will be some questions there.
arnaud-lb commentedon Oct 16, 2022
Related: #9722
arnaud-lb commentedon Dec 16, 2022
Probably related: #10117