note 75459 added to function.umask
| From: | sean at awesomeplay dot com | Date: | Thu, 31 May 2007 04:48:54 +0000 |
| Subject: | note 75459 added to function.umask | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-127045@lists.php.net to get a copy of this message | ||
"It is better to change the file permissions with chmod() after creating the file."
The usual lacking of security knowledge within the PHP team rears its head once again. You *always*
want to have the file created with the proper permission. Let me illustrate why:
(a) you create new file with read permissions
(b) an attacking script opens the file
(c) you chmod the file to remove read permissions
(d) you write sensitive data to the file
Now, you might think that the changes of an attacking script getting to open the file before you
chmod them are low. And you're right. But low changes are never low enough - you want zero
chance.
When creating a file that needs increased permissions, you always need to create the file with the
proper permissions, and also create it with O_EXCL set. If you don't do an exclusive create,
you end up with this scenario:
(a) attacker creates the file, makes it writable to everyone
(b) you open the file with restricted permissions, but since it already exists, the file is merely
opened and the permissions left alone
(c) you write sensitive data into the insecure file
Detecting the latter scenario is possible, but it requires a bit of work. You have to check that
the file's owner and group match the script's (that is, posix_geteuid(), not myuid()) and
check the permissions - if any of those are incorrect, then the file is insecure - you can attempt
to unlink() it and try again while logging a warning, of course.
The only time when it is reasonable or safe to chmod() a file after creating it is when you want to
grant extra permissions instead of removing them. For example, it is completely safe to set the
umask to 0077 and then chmoding the files you create afterward.
Doing truly secure programming in PHP is difficult as is, and advice like this in the documentation
just makes things worse. Remember, kids, anything that applies to security in the C or UNIX worlds
is 100% applicable to PHP. The best thing you can possibly do for yourself as a PHP programmer is
to learn and understand secure C and UNIX programming techniques.
----
Server IP: 69.147.83.197
Probable Submitter: 71.238.87.27
----
Manual Page -- http://www.php.net/manual/en/function.umask.php
Edit -- https://master.php.net/note/edit/75459
Del: integrated -- https://master.php.net/note/delete/75459/integrated
Del: useless -- https://master.php.net/note/delete/75459/useless
Del: bad code -- https://master.php.net/note/delete/75459/bad+code
Del: spam -- https://master.php.net/note/delete/75459/spam
Del: non-english -- https://master.php.net/note/delete/75459/non-english
Del: in docs -- https://master.php.net/note/delete/75459/in+docs
Del: other reasons-- https://master.php.net/note/delete/75459
Reject -- https://master.php.net/note/reject/75459
Search -- https://master.php.net/manage/user-notes.php