Phpopen_basedir security and performance trade-off open_basedir seriously affects PHPI/O performance. this is an article I saw yesterday. after reading this article, I did a test on the server and so on, which did have an impact. After all, open_basedir must first determine whether the file is in the executable directory for each request, include, and object determination. So .. Php open_basedir security and performance considerations
Open_basedir seriously affects php I/O performance
This is an article I saw yesterday. after reading it, I did a test on the server and so on, which did have an impact. After all, open_basedir must first determine whether the file is in the executable directory for each request, include, and object determination. So... The performance will decrease (if the framework is used, the performance may be worse). if it is your own machine, you can also set the file directory permission, in linux, users and user groups can be specified. In windows, you can set the directory owner (I was the most annoying in windows at that time, because you need to add a user to each virtual host, specify permissions, and then set users in the directory )]
The content of the article I see is as follows:
Today, when I was working on a project, I found that a few lines of statements actually took 0.04 seconds.
I was surprised to hear that I/O read and write took the most time for an is_file and actually took 0.003 seconds.
Later, I was puzzled and Google had no answer.
Then I put the project on another computer and found that the speed was rising from 0.04 seconds to 0.007 seconds.
I'm wondering if my book is so bad.
After my comparison, I found that open_basedir is set in the virtual host of this project on my machine.
We all know that this is essential for virtual hosts ..
But then I tested it again. Is that true?
My test code is very simple:
PHP code
01. $ time = microtime (true );???
02.is_file('1.html ');???
03. echo microtime (true)-$ time ;???
I found that an empty 1.html file exists in the current directory.
I created a VM and set open_basedir.
The result is about 0.0018570423126221.
Then, after I delete open_basedir in the apache configuration file
The result is 5.0067901611328E-5, which is smaller than the decimal point in PHP.
(Note: The above results are all cached results after multiple operations because the is_file results will be cached)
Now I finally understand why I/O performance in the probe of the VM is so small ..
Cup ..
You can also test it .. This is the actual test.
----------------------
Cancel this setting, or.../:./:/tmp :../../?
----------------------
?
The following is the original description and default configuration in php. ini:
; Open_basedir, if set, limits all file operations to the defined directory
; And below. This directive makes most sense if used in a per-directory or
; Per-virtualhost web server configuration file. This directive is
; * NOT * affected by whether Safe Mode is turned On or Off.
Open_basedir =.
Open_basedir can restrict the activity scope of files accessed by users to a specified region. it is usually the path of the home directory
The "." symbol can be used to represent the current directory. Note that the restriction specified by open_basedir is actually a prefix rather than a directory name.
For example, if "open_basedir =/dir/user", the directories "/dir/user" and "/dir/user1" are both
Accessible. Therefore, if you want to restrict access to a specified directory only, end the path with a slash. For example, set:
"Open_basedir =/dir/user /"
Open_basedir can also set multiple directories at the same time. in Windows, use semicolons to separate directories and use them in any other system.
Separate directories with colons. When it acts on the Apache module, the open_basedir path in the parent directory is automatically inherited.
There are three ways to make independent settings for specified users in Apache:
(A) how to set Directory in httpd. conf of Apache:
Php_admin_value open_basedir/usr/local/apache/htdocs/
# To set multiple directories, refer to the following:
Php_admin_value open_basedir/usr/local/apache/htdocs/:/tmp/
(B) how to set VirtualHost in httpd. conf of Apache:
Php_admin_value open_basedir/usr/local/apache/htdocs/
# To set multiple directories, refer to the following:
Php_admin_value open_basedir/var/www/html/:/var/tmp/
(C) because open_basedir is set in VirtualHost, this virtual user will not inherit php. ini automatically.
Open_basedir setting value in, which makes it difficult to achieve flexible configuration measures. Therefore, we recommend that you do not
For example, you can set open_basedir =.:/tmp/in php. ini.
Access the current directory (the directory where the PHP script file is located) and/tmp/directory.
Note: If the temporary directory for uploading files set in php. ini is/tmp/, open_basedir must be set.
Contains/tmp/, otherwise the upload will fail. The new php version will prompt "open_basedir restriction in effect"
Warning, but the move_uploaded_file () function can still successfully retrieve the uploaded files in the/tmp/directory.
This is a vulnerability or a new feature.
ShopEx472 configuration:
Open_basedir = "D:/Server ;.. /catalog ;.. /include ;.. /.. /home ;.. /syssite ;.. /templates ;.. /language ;.. /.. /language ;.. /.. /.. /language ;.. /.. /.. /.. /language"
WebsiteOfSecurity and performanceIt seems that the relationship between spear and shield. It is particularly difficult for a php websiteTrade-offs. For exampleOpen_basedirRestrict files that can be opened by PHP to the specified directory tree, including the file itself. Originally, this is very beneficial to website security. However, according to the information obtained from the Internet, open_basedir will have a great impact on the performance of php io operations. Research data shows that the script I/O execution speed with php_basedir configured is 10 times or more slower than that without php_basedir!
?
At first, I didn't quite trust this result, but the test data convinced me to admit it.
Create a simple script:
?
| 123456789101112131415 |
function microtime_float() { ????list($usec, $sec) = explode(" ", microtime()); ????return ((float)$usec + (float)$sec); } ??$time_start = microtime_float(); ??is_file('1.html');? // The contents directory contains the 1.html file.$time_end = microtime_float(); $time = $time_end - $time_start; ??echo "Did is_file in $time seconds\n"; ?> |
Test results for open_basedir
0.0006/5.0E-5
The gap is quite large, but smart friends should note that the author's website is configured with open_basedir. compared with this performance loss, I would rather sacrifice performance in exchange for security, what is your choice? You don't have to guess. you may be the same as me ~ After all, server security is more important.
Tips: how to configure open_basedir
When a script tries to open a file using fopen () or gzopen (), the file location will be checked. PHP rejects opening a file outside the specified directory tree. All symbolic connections are parsed, so it is impossible to avoid this restriction through symbolic connections.
Special value. indicates that the script's working directory will be used as the reference directory. But this is dangerous because the working directory of the script can be easily changed by chdir.
In the httpd. conf file, open_basedir can be disabled using the "php_admin_value open_basedir none" method like any other configuration options (for example, in some virtual hosts ).
In Windows, separate directories with semicolons. Use colons to separate directories in any other system. As an Apache module, the open_basedir path in the parent directory is automatically inherited.
The restriction specified by open_basedir is actually a prefix, not a directory name. That is to say, "open_basedir =/dir/incl" will also allow access to "/dir/include" and "/dir/incls" if they exist. If you want to restrict access to a specified directory only, end the path with a slash. For example, "open_basedir =/dir/incl /".
?
?
?
?
?
?
?