Req #26158 [Bgs->Csd]: Open arbitrary file descriptor with fopen

From: Date: Sat, 11 Dec 2010 01:55:10 +0000
Subject: Req #26158 [Bgs->Csd]: Open arbitrary file descriptor with fopen
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-156888@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=26158&edit=1

 ID:                 26158
 Updated by:         cataphract@php.net
 Reported by:        mattias at sudac dot org
 Summary:            Open arbitrary file descriptor with fopen
-Status:             Bogus
+Status:             Closed
 Type:               Feature/Change Request
-Package:            Feature/Change Request
+Package:            *General Issues
 Operating System:   Linux
 PHP Version:        4.3.3
-Assigned To:        
+Assigned To:        cataphract
 Block user comment: N
 Private report:     N

 New Comment:

Marking as closed as this was implemented for 5.3/trunk.

See also bug #53465.


Previous Comments:
------------------------------------------------------------------------
[2010-12-11 02:52:16] cataphract@php.net

Automatic comment from SVN on behalf of cataphract
Revision: http://svn.php.net/viewvc/?view=revision&revision=306215
Log: - Implemented request #26158/bug #53465 (open arbitrary file
descriptor with fopen)

------------------------------------------------------------------------
[2007-07-24 15:38:37] jani@php.net

Linux bugs are not PHP bugs..

------------------------------------------------------------------------
[2003-11-21 11:16:31] mattias at sudac dot org

I think this is unrelated to PHP and very Linux specific, I wrote a C
prgoram and run it with tcpserver on FreeBSD and Linux. On FreeBSD it
works fine, but on Linux i get this error:
open in: No such device or address
open out: No such device or address

I also found this, so maybe this is solved in Linux 2.6?
http://www.geocrawler.com/mail/msg.php3?msg_id=3374944&list=35

#include <unistd.h>                                                     
             #include <sys/types.h>                                     
                          #include <sys/stat.h>                         
                                       #include <fcntl.h>               
                                                    
int main()
{
    int in;
    int out;
    
    in = open("/dev/fd/0", O_RDONLY);
    if(in == -1)
        perror("open in");
    
    out = open("/dev/fd/1", O_WRONLY);
    if(out == -1)
        perror("open out");

    if(out != -1)
        write(out, "test\n", 5);
    
    return 0;
}

------------------------------------------------------------------------
[2003-11-07 07:46:06] mattias at sudac dot org

It seams to work if the file descriptor is a pipe, but it fails on
network sockets.

Example:

testfd.php:
#!/usr/local/bin/php -q
<?php

echo ls -l /dev/fd/;

$stdin = fopen('/dev/fd/0', 'r');
$stdout = fopen('/dev/fd/1', 'w');

?>

Server:
tcpserver 127.0.0.1 8000 ./testfd.php

Client:
mattias@icebox:~$ nc localhost 8000
total 0
lrwx------    1 mattias  mattias        64 2003-11-07 13:35 0 ->
socket:[2611215]
l-wx------    1 mattias  mattias        64 2003-11-07 13:35 1 ->
pipe:[2611234]
lrwx------    1 mattias  mattias        64 2003-11-07 13:35 2 ->
/dev/pts/3
lr-x------    1 mattias  mattias        64 2003-11-07 13:35 3 ->
/proc/12706/fd
lrwx------    1 mattias  mattias        64 2003-11-07 13:35 4 ->
socket:[2611215]
lrwx------    1 mattias  mattias        64 2003-11-07 13:35 5 ->
socket:[2611215]
lrwx------    1 mattias  mattias        64 2003-11-07 13:35 6 ->
/dev/pts/3

Warning: fopen(/dev/fd/0): failed to open stream: No such device or
address in /home/mattias/projects/jssocket/tests/testfd on line 6

Warning: fopen(/dev/fd/1): failed to open stream: No such device or
address in /home/mattias/projects/jssocket/tests/testfd on line 7


If we run the script without tcpserver we get this:

mattias@icebox:~$ ./testfd
total 0
lrwx------    1 mattias  mattias        64 2003-11-07 13:37 0 ->
/dev/pts/3
l-wx------    1 mattias  mattias        64 2003-11-07 13:37 1 ->
pipe:[2611379]
lrwx------    1 mattias  mattias        64 2003-11-07 13:37 2 ->
/dev/pts/3
lr-x------    1 mattias  mattias        64 2003-11-07 13:37 3 ->
/proc/12709/fd
lrwx------    1 mattias  mattias        64 2003-11-07 13:37 4 ->
/dev/pts/3
lrwx------    1 mattias  mattias        64 2003-11-07 13:37 5 ->
/dev/pts/3
lrwx------    1 mattias  mattias        64 2003-11-07 13:37 6 ->
/dev/pts/3


Notice the difference, also notice that /dev/fd is a symlink  to
/proc/self/fd:
mattias@icebox:~$ ls -l /dev/fd
lrwxrwxrwx    1 root     root           13 2001-12-21 18:29 /dev/fd ->
/proc/self/fd


This is on Debian unstable, Linux 2.4.21. I noticed this when i moved a
script from FreeBSD 4.8 to Linux.

------------------------------------------------------------------------
[2003-11-07 06:44:39] wez@php.net

Why does fopen("/dev/fd/x", "r") fail ?


------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    http://bugs.php.net/bug.php?id=26158


-- 
Edit this bug report at http://bugs.php.net/bug.php?id=26158&edit=1


Thread (6 messages)

« previous php.bugs (#156888) next »