Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 99086 - net-www/apache 2 in portage breaks php safe mode restrictions
Summary: net-www/apache 2 in portage breaks php safe mode restrictions
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: High minor (vote)
Assignee: PHP Bugs
URL: http://orion.souepl.cz:8080/a/
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-07-15 02:23 UTC by Jakub Moc (RETIRED)
Modified: 2006-01-20 01:33 UTC (History)
3 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
php.ini mod_php/CLI (php.ini.txt,38.56 KB, text/plain)
2005-07-15 02:32 UTC, Miroslav Osladil
Details
emerge --info (info.txt,1.83 KB, text/plain)
2005-07-15 02:33 UTC, Miroslav Osladil
Details
screenshot of the bug (bug.png,163.93 KB, image/png)
2005-07-15 03:00 UTC, Miroslav Osladil
Details
portage based php (portage-php.png,67.08 KB, image/png)
2005-07-15 04:06 UTC, Miroslav Osladil
Details
source based php (source-php.png,94.96 KB, image/png)
2005-07-15 04:08 UTC, Miroslav Osladil
Details
strace of apache2 from portage (strace2.log,125.39 KB, text/plain)
2005-07-19 00:22 UTC, Jakub Moc (RETIRED)
Details
strace of apache2 compiled manually (strace2.log,38.82 KB, text/plain)
2005-07-19 00:45 UTC, Jakub Moc (RETIRED)
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jakub Moc (RETIRED) gentoo-dev 2005-07-15 02:23:32 UTC
A really weird problem with mod_php-4.4.0 was reported to me on IRC by a Gentoo
user:

mod_php-4.4.0 does not obey safe mode restrictions and happily includes
/etc/hostname. But it properly FAILS to include /etc/hosts or other files, so
this issue seems *filename* specific.


Reproducible: Always
Steps to Reproduce:
1. Setup mod_php-4.4.0 with safe mode ON and open_basedir which does NOT include
/etc and reload apache.

2. Create this test.php file and open in browser:

<?
echo 'include "/etc/hostname"';
include "/etc/hostname";
echo 'include "/etc/hosts"';
include "/etc/hosts";
phpinfo(); ?>

Actual Results:  
See http://orion.souepl.cz:8080/a/

1. mod_php does not obey safe mode restrictions and happily includes
/etc/hostname. But it properly FAILS to include /etc/hosts or other files so it
seems *filename* specific.

2. This problem does NOT manifest itself in CLI with identical configuration
file - i.e. something like '/usr/bin/php test.php'

3. This problem also disappears if you compile mod_php manually, so it seems
Gentoo ebuild specific.

4. I tested on 4.3.11 and mod_php-5.1.0_beta-r2 *from portage* and there is no
such problem as well.

5. There may be other filenames exhibiting this problem, but I failed to find
them so far.

Expected Results:  
mod_php-4.4.0 should always obey safe mode, open_basedir and other restrictions
for every filename.
Comment 1 Miroslav Osladil 2005-07-15 02:32:45 UTC
Created attachment 63447 [details]
php.ini mod_php/CLI
Comment 2 Miroslav Osladil 2005-07-15 02:33:08 UTC
Created attachment 63448 [details]
emerge --info
Comment 3 Sebastian Bergmann (RETIRED) gentoo-dev 2005-07-15 02:49:02 UTC
When I open the URL you gave I get the output shown below. From this I cannot
see that something is wrong:

include "/etc/hostname" =>
Warning: main(): open_basedir restriction in effect. File(/etc/hostname) is not
within the allowed path(s): (/var/www/localhost/htdocs) in
/var/www/localhost/htdocs/a/index.php on line 3

Warning: main(/etc/hostname): failed to open stream: Operation not permitted in
/var/www/localhost/htdocs/a/index.php on line 3

Warning: main(): Failed opening '/etc/hostname' for inclusion
(include_path='.:/usr/lib/php') in /var/www/localhost/htdocs/a/index.php on line 3

include "/etc/hosts" =>
Warning: main(): open_basedir restriction in effect. File(/etc/hosts) is not
within the allowed path(s): (/var/www/localhost/htdocs) in
/var/www/localhost/htdocs/a/index.php on line 6

Warning: main(/etc/hosts): failed to open stream: Operation not permitted in
/var/www/localhost/htdocs/a/index.php on line 6

Warning: main(): Failed opening '/etc/hosts' for inclusion
(include_path='.:/usr/lib/php') in /var/www/localhost/htdocs/a/index.php on line 6
Comment 4 Jakub Moc (RETIRED) gentoo-dev 2005-07-15 02:59:28 UTC
(In reply to comment #3)
> When I open the URL you gave I get the output shown below. From this I cannot
> see that something is wrong:

Sebastian, if I did not see the problem on my own eyes refreshing browser
windows quite a few time, I would not believe this either. :) The problem
vanished after:

mv /etc/hostname /etc/hostname.old && mv /etc/hostname.old /etc/hostname

Go figure... *really confused*
Comment 5 Miroslav Osladil 2005-07-15 03:00:26 UTC
Created attachment 63452 [details]
screenshot of the bug
Comment 6 Miroslav Osladil 2005-07-15 04:06:08 UTC
Created attachment 63453 [details]
portage based php

emerge php mod_php
Comment 7 Miroslav Osladil 2005-07-15 04:08:51 UTC
Created attachment 63454 [details]
source based php

for apache
./configure --prefix=/opt/apache2 --enable-so && make && make install

for php
./configure --prefix=/opt/php --with-apxs2=/opt/apache2/bin/apxs && make &&
make install
cp php.ini-dist /opt/php/lib/php.ini
in php.ini enabled safe_mode as in original php.ini from portage. the some
configuration
Comment 8 Miroslav Osladil 2005-07-15 04:10:48 UTC
the original portage apache/php is on http://orion.souepl.cz:8080/a/
the source apache/php is on http://orion.souepl.cz:8081/a/
Comment 9 Miroslav Osladil 2005-07-15 04:53:18 UTC
problem #2

portage apache 2.0.54-r8 doesn't work with php 4.4.0 and with original source php 4.4.0 too.
in the php scripts can be included files with another uid

index.php apache:apache
test.txt root:root
<? include "text.txt"; ?> doesn't work with php 4.4.0 in safe mode

in apache source, which i compile from original source without portage patches and then started with 
portage mod_php and again with mod_php from original php sources, all passed good. the php server 
cann't include the scripts with of other user :) or scipts with other uid. the safe_mode is working :)

index.php apache:apache
test.txt root:root
<? include "text.txt"; ?> work correctly with php 4.4.0 in safe mode
Comment 10 Jakub Moc (RETIRED) gentoo-dev 2005-07-15 04:57:25 UTC
I was able to verify problem described in comments #6 and following with
mod_php-5.1.0_beta-r2 and apache-2.0.54-r12 as well.

1/ 

# ls -la | grep test
-rw-r--r--   1 root   root     158 Jul 15 12:58 test.php
-rw-r--r--   1 apache apache   135 Jul 15 13:01 test.txt

results in:

Warning: main() [function.main]: SAFE MODE Restriction in effect. The script
whose uid is 0 is not allowed to access ./test.txt owned by uid 81 in
/var/www/localhost/htdocs/test.php on line 6

Warning: main(./test.txt) [function.main]: failed to open stream: Not a
directory in /var/www/localhost/htdocs/test.php on line 6

Warning: main() [function.include]: Failed opening './test.txt' for inclusion
(include_path='.:/usr/share/php') in /var/www/localhost/htdocs/test.php on line 6

2/ 

# ls -la | grep test
-rw-r--r--   1 apache apache   158 Jul 15 12:58 test.php
-rw-r--r--   1 root   root     135 Jul 15 13:01 test.txt

results in successful include.

Seems like we have a major problem with apache. :/
Comment 11 Miroslav Osladil 2005-07-15 05:17:51 UTC
the portage apache 2.0.54 daemon with portage php is running on http://orion.souepl.cz:8080/a/ and 
the second instance of source compiled apache 2.0.54 on http://orion.souepl.cz:8081/a/

The source compiled apache was also tested with mod_php which is running on port 8080
Comment 12 Thierry Carrez (RETIRED) gentoo-dev 2005-07-18 05:20:42 UTC
Apache herd, any hint ?
Comment 13 Michael Stewart (vericgar) (RETIRED) gentoo-dev 2005-07-18 22:02:32 UTC
I have been trying to reproduce this in a chroot, but I haven't been able to, so
I'm hoping the people who can reproduce will provide us more information.
Do you have any USE-flags for mod_php, php, or apache set in
/etc/portage/package.use?

Can you run a strace on the requests? (Might as well do one on the working one
as well as the buggy one, to compare the two)

to do a strace with apache, add -X to the command line
for example, in my trying to reproduce this, I used:
strace /usr/sbin/apache2 -X -k start -D PHP4

I would also test it using telnet instead of a web browser. The request should
take the form:

GET /test.php HTTP/1.1
Host: localhost
Connection: close
Comment 14 Jakub Moc (RETIRED) gentoo-dev 2005-07-18 23:56:44 UTC
(In reply to comment #13)
> Do you have any USE-flags for mod_php, php, or apache set in
> /etc/portage/package.use?

No. If it helps, here are the flags used:

emerge -pv apache mod_php php

These are the packages that I would merge, in order:

Calculating dependencies ...done!
[ebuild   R   ] net-www/apache-2.0.54-r8  +berkdb -doc +gdbm +ipv6 -ldap
(-selinux) +ssl -static -threads 0 kB
[ebuild   R   ] dev-php/mod_php-4.4.0  +X +apache2 +berkdb +crypt +curl -debug
-doc -fdftk -firebird -flash -freetds +gd -gd-external +gdbm -gmp -hardenedphp
+imap -informix +ipv6 -java +jpeg -kerberos -ldap -mcal -memlimit -mssql +mysql
+nls -oci8 -odbc +pam +png -postgres -snmp +spell +ssl +tiff +truetype +xml2
-yaz 0 kB
[ebuild   R   ] dev-php/php-4.4.0  +X +berkdb +crypt +curl -debug -doc -fdftk
-firebird -flash -freetds +gd -gd-external +gdbm -gmp -hardenedphp +imap
-informix +ipv6 -java +jpeg -kerberos -ldap -mcal -memlimit -mssql +mysql
+ncurses +nls -oci8 -odbc +pam +png -postgres +readline -snmp +spell +ssl +tiff
+truetype +xml2 -yaz 0 kB


> Can you run a strace on the requests? (Might as well do one on the working one
> as well as the buggy one, to compare the two)

Will do later.
 
> I would also test it using telnet instead of a web browser. The request should
> take the form:
> 
> GET /test.php HTTP/1.1
> Host: localhost
> Connection: close

Same result as shown in the report above:

1/ apache2 from portage:

# telnet localhost 80 >& test1.log
GET /a/index.php HTTP/1.1
Host: localhost
Connection: close

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
HTTP/1.1 200 OK
Date: Tue, 19 Jul 2005 06:45:47 GMT
Server: Apache/2.0.54 (Gentoo/Linux) mod_ssl/2.0.54 OpenSSL/0.9.7e DAV/2 PHP/4.4.0
X-Powered-By: PHP/4.4.0
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=ISO-8859-1
X-Pad: avoid browser bug

20a1
include file with other uid from portage sources....<br>index.php
apache:apache<br>file.txt root:root<hr>hello world...<hr><!DOCTYPE html PUB
LIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">

---

2/ apache2 compiled manually:

# telnet localhost 8080 >& test2.log
GET /a/index.php HTTP/1.1
Host: localhost
Connection: close


Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
HTTP/1.1 200 OK
Date: Tue, 19 Jul 2005 06:46:58 GMT
Server: Apache/2.0.54 (Unix) PHP/4.4.0
X-Powered-By: PHP/4.4.0
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html

2294
include file with other uid from original sources....<br>index.php
apache:apache<br>file.txt root:root<hr><br />
<b>Warning</b>:  main(): SAFE MODE Restriction in effect.  The script whose uid
is 81 is not allowed to access ./file.txt owned by uid 0 in <
b>/opt/apache2/htdocs/a/index.php</b> on line <b>6</b><br />
<br />
<b>Warning</b>:  main(file.txt): failed to open stream: Resource temporarily
unavailable in <b>/opt/apache2/htdocs/a/index.php</b> on line <b
>6</b><br />
<br />
<b>Warning</b>:  main(): Failed opening 'file.txt' for inclusion
(include_path='.:/opt/php/lib/php') in <b>/opt/apache2/htdocs/a/index.php</b
> on line <b>6</b><br />
Comment 15 Jakub Moc (RETIRED) gentoo-dev 2005-07-19 00:22:27 UTC
Created attachment 63754 [details]
strace of apache2 from portage

'strace -ostrace1.log /usr/sbin/apache2 -X -k start -D PHP4' and load index.php
file from browser
Comment 16 Jakub Moc (RETIRED) gentoo-dev 2005-07-19 00:45:12 UTC
Created attachment 63755 [details]
strace of apache2 compiled manually

'strace -ostrace2.log /opt/apache2/bin/httpd -X -k start -D PHP' and load
index.php file from browser
Comment 17 Michael Stewart (vericgar) (RETIRED) gentoo-dev 2005-07-19 15:45:08 UTC
I was hoping for a strace of the including /etc/hostname but not /etc/hosts to
see  how it's treating those two files differently.

Also, there are far too many differences between your portage-compiled and
source-compiled apache and mod_php, can you make sure your source-compiled is
using the same modules and configurations?

===
> I would also test it using telnet instead of a web browser. The request should
> take the form:
> 
> GET /test.php HTTP/1.1
> Host: localhost
> Connection: close

Same result as shown in the report above:
===

The point of this wasn't as another way to reproduce it, but to make the strace
cleaner. Browsers leave the connection open and run other requests on the same
connection. Browsers also tend to request favicon.ico.
Comment 18 Jakub Moc (RETIRED) gentoo-dev 2005-07-19 16:01:59 UTC
(In reply to comment #17)
> I was hoping for a strace of the including /etc/hostname but not /etc/hosts to
> see  how it's treating those two files differently.

Scratch that part of the bug report; as stated in comment #4, it suddenly went
away after renaming the file back and forth and cannot be reproduced any more,
so I have no clue what caused that. The problem w/ apache:apache owned php
script including root:root owned files persists and can be reproduced every time.

> Also, there are far too many differences between your portage-compiled and
> source-compiled apache and mod_php, can you make sure your source-compiled is
> using the same modules and configurations?

I can comment out the other modules in apache version from portage, will try
that tomorrow, but as I see in the strace log, it happily includes that file:

xyZw("/var/www/localhost/htdocs/a/file.txt", O_RDONLY) = 9
fstat64(9, {st_mode=S_IFREG|0644, st_size=14, ...}) = 0
fstat64(9, {st_mode=S_IFREG|0644, st_size=14, ...}) = 0
lseek(9, 0, SEEK_CUR)                   = 0
lseek(9, 0, SEEK_SET)                   = 0
read(9, "hello world...", 8192)         = 14
read(9, "", 8192)                       = 0
close(9)                                = 0
Comment 19 Sune Kloppenborg Jeppesen (RETIRED) gentoo-dev 2005-07-21 00:57:42 UTC
Any news on this one? 
Comment 20 Thierry Carrez (RETIRED) gentoo-dev 2005-07-29 03:01:52 UTC
In my tests:

apache-2.0.52-r1 or 2.0.54-r8, mod_php 4.4.0
-rw-r--r--  1 apache apache 26 Jul 29 10:21 text.php
-rw-r--r--  1 root   root   17 Jul 29 10:21 text.txt
--> include of text.txt from text.php is forbidden by way of safe mode

So my results differ.
Comment 21 Thierry Carrez (RETIRED) gentoo-dev 2005-07-30 07:00:58 UTC
This needs more confirmation, as I can't reproduce and the maintainer can't
either... Setting it to Auditing.
Comment 22 Tavis Ormandy (RETIRED) gentoo-dev 2005-09-02 00:34:24 UTC
Reassigning to php herd, the security team does not usually process safe_mode 
bugs.

http://www.php.net/security-note.php
Comment 23 Sebastian Bergmann (RETIRED) gentoo-dev 2006-01-20 01:33:06 UTC
dev-php/php, dev-php/mod_php, and dev-php/php-cgi have been replaced by
dev-lang/php.

Please upgrade (following the guide at
http://svn.gnqs.org/projects/gentoo-php-overlay/file/docs/php-upgrading.html?format=raw)
to the new-style PHP package and open a new bug if the problem persists.

Thank you.