note 78554 added to function.posix-setuid
| From: | fmatfarhad dot ca at osu1 dot php dot net | Date: | Wed, 17 Oct 2007 10:44:27 +0000 |
| Subject: | note 78554 added to function.posix-setuid | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131447@lists.php.net to get a copy of this message | ||
When you do a posix_setuid from root to some other users you will not have access to files owned by
root according to their permissions. For instance if you change owner of the process and still need
to open a file for read or write with 600 permission owned by root you will receive a permission
denied.
There are some ways to do this (i.e. a unix socket or tcp daemon etc), but probably the most easiest
way is:
Open the file before changing ownership of process, save the file pointer in a global variable and
use it after changing ownership.
For example assume /root/test_file is a file owned by root:root and have a permission of 600 and you
are running this script under root. This code will not work:
<?php
// Change ownership of process to nobody
posix_setgid(99);
posix_setuid(99);
$fd = fopen('/root/test_file','a');
fwrite($fd,"some test strings");
fclose();
?>
But this one will work:
<?php
$fd = fopen('/root/test_file','a');
// Change ownership of process to nobody
posix_setgid(99);
posix_setuid(99);
fwrite($fd,"some test strings");
fclose();
?>
Hope this helps some one.
[Tested on CentOS 5 - Linux 2.6.x - PHP 5.2.x]
----
Server IP: 66.207.199.35
Probable Submitter: 70.68.54.184
----
Manual Page -- http://www.php.net/manual/en/function.posix-setuid.php
Edit -- https://master.php.net/note/edit/78554
Del: integrated -- https://master.php.net/note/delete/78554/integrated
Del: useless -- https://master.php.net/note/delete/78554/useless
Del: bad code -- https://master.php.net/note/delete/78554/bad+code
Del: spam -- https://master.php.net/note/delete/78554/spam
Del: non-english -- https://master.php.net/note/delete/78554/non-english
Del: in docs -- https://master.php.net/note/delete/78554/in+docs
Del: other reasons-- https://master.php.net/note/delete/78554
Reject -- https://master.php.net/note/reject/78554
Search -- https://master.php.net/manage/user-notes.php