Skip to content

Wrong value from ini_get() for shared files because of opcache optimization #8699

Open

Description

@bix0r

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.php
Cli

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-cache

Run the test

php -d upload_tmp_dir=/tmp/num1 /tmp/test/1.php
php -d upload_tmp_dir=/tmp/num2 /tmp/test/2.php

Expected 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

  1. arnaud-lb commented on Jun 3, 2022

    @arnaud-lb
    Member

    Does it happen only with opcache.file_cache enabled ? I would recommend to use a separate cache dir when PHP_INI_SYSTEM entries differ.

  2. bix0r commented on Jun 3, 2022

    @bix0r
    Author

    No, this always happens when opcache optimization is enabled.

  3. arnaud-lb commented on Jun 10, 2022

    @arnaud-lb
    Member

    The problem seems to be that we allow PHP_INI_SYSTEM entries to be modified on a per-request basis via fastcgi_param PHP_ADMIN_VALUE.

    The purpose of PHP_ADMIN_VALUE is to make an ini setting non modifiable by the script (with e.g. ini_set), but apparently the way it is implemented allows to override PHP_INI_SYSTEM entires.

    Since PHP_INI_SYSTEM entries are not supposed to be modified, I think that the opcache makes the right assumption here.

  4. bix0r commented on Jun 30, 2022

    @bix0r
    Author

    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_value seems 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.

  5. arnaud-lb commented on Jun 30, 2022

    @arnaud-lb
    Member

    I agree with this.

    There are some PHP_INI_SYSTEM entries 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 prevent PHP_INI_SYSTEM value from being modified via PHP_ADMIN_VALUE.

  6. bukka commented on Jul 6, 2022

    @bukka
    Member

    Maybe we should promote some ini settings to PHP_INI_PERDIR (or introduce a new mode), and then prevent PHP_INI_SYSTEM value from being modified via PHP_ADMIN_VALUE.

    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.

  7. bukka commented on Aug 29, 2022

    @bukka
    Member

    As per discussion in #8997 I'm changing this to feature request as it is not an actual bug.

  8. added and removed on Aug 29, 2022
  9. bukka commented on Aug 29, 2022

    @bukka
    Member

    @arnaud-lb

    Just came back to this and want to check this with you:

    There are some PHP_INI_SYSTEM entries that will really break PHP if they are changed like this, though.

    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...

  10. bix0r commented on Aug 31, 2022

    @bix0r
    Author

    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_dir per vhost makes sure that uploaded files are indeed uploaded to the specified folder. It is just that ini_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.

  11. arnaud-lb commented on Sep 2, 2022

    @arnaud-lb
    Member
    There are some PHP_INI_SYSTEM entries that will really break PHP if they are changed like this, though.
    

    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...

    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.

  12. bukka commented on Sep 2, 2022

    @bukka
    Member

    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_SYSTEM and add ZEND_INI_ADMIN and let opcache just ignore that optimization if it's also ZEND_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?

  13. added and removed on Sep 2, 2022
  14. bix0r commented on Sep 6, 2022

    @bix0r
    Author

    @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() and php_ini_activate_per_dir_config() that also alter the ini entries with ZEND_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:

    • Add ZEND_INI_ADMIN and PHP_INI_ADMIN to Zend/zend_ini.h and main/php_ini.h
    • Add ZEND_INI_ADMIN and PHP_INI_ADMIN to ZEND_INI_ALL and PHP_INI_ALL
    • Make php_admin_value set ZEND_INI_ADMIN instead ZEND_INI_SYSTEM in apache2handler and fpm
    • Add the ADMIN value to reflection
    • Change the check in the Opcache Optimizer

    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:

        if (stage == ZEND_INI_STAGE_ACTIVATE && modify_type == ZEND_INI_SYSTEM) {
            ini_entry->modifiable = ZEND_INI_SYSTEM;
        }
    

    It seems like it could matter. Not sure if this needs to be changed.

  15. bukka commented on Sep 30, 2022

    @bukka
    Member

    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() and php_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 part zend_alter_ini_entry_ex is 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.

  16. bix0r commented on Oct 13, 2022

    @bix0r
    Author

    Just a quick update. I am working on this. I will create a draft PR soon(ish), but there will be some questions there.

  17. arnaud-lb commented on Oct 16, 2022

    @arnaud-lb
    Member

    Related: #9722

  18. arnaud-lb commented on Dec 16, 2022

    @arnaud-lb
    Member

    Probably related: #10117

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions