Skip to content

NFSN PHP file write permissions in safe_mode

I’ve been causing some (security) concerns for myself by thoughtlessly using the dreaded 777 permissions for upload directories to allow the various PHP-based websites that I host at nearlyfreespeech.net to write files there. What this drastic anti-security measure didn’t allow me to is to manage these uploaded files through SSH (and SCP/Rsync). In the chroot jail which I’m allowed to enter through SSH, I am ‘me’, while the files created from PHP end up being owned by user ‘web’. However, for some reason these files didn’t get owned by group ‘web’ of which the ‘me’ user is a member. Also, I got into trouble with new directories that were being created by the upload scripts.

Writing files in PHP more securely

When I make a very basic test script in PHP, which writes a new file to a 777 directory owned by ‘me’, the files simply end up being owned by web:web with 775 permissions. To make this work a little more securely, I have to change the authorization for two entities:

  1. The directory to which I want to write needs to be group writable (775) and owned by group ‘web’.
  2. The PHP file that does the writing needs to also be owned by group ‘web’.

This will also allow the PHP file to write files in subdirectories that it creates. In the 777 scenario above, it would be possible to create these dirs, but not to create files within them. The PHP safe_mode restrictions in effect won’t allow a script owned by user ‘me’ and another group than ‘web’ to write files in a directory owned by ‘web:web’ and 775 permissions set.

All in accordance with the advice on writing files in PHP by the NFSN team.

Application headaches

There’s a little more to it, though. The stuff that I uploaded through MediaWiki and WordPress with my super-liberal 777 permission set on the upload dir (owned by ‘me’) somehow never ended up with the same group write permissions as the files in PHP test described above.

WordPress on NearlyFreeSpeech.Net

Uploads created by WordPress did end up with the permissions of the beast (666) set. However, directories created by WordPress (the year/month no. subdirectories) ended up with ‘web:web’ ownership, which regardless of their 777 mode, didn’t allow PHP in safe_mode to create any files within these directories. This is easy enough to solve by changing the ownership of the PHP files doing the writing to group ‘web’. Of course, this is best coupled with making the same changes to the upload directories and also changing the mode of these to 775.

The WordPress installation notes on the NFSN member wiki [for members only] has some more details.

I reviewed the code in WordPress responsible for writing files and I noticed that, whether creating files or directory, it actually looks at the permissions of the parent directory to decide on the mode of the newly created entity (using something along the lines of “$new_file_perms = $parent_perms & 0000666” for files and “$new_dir_perms = $parent_perms & 0007777” for directories).

MediaWiki on NearlyFreeSpeech.Net

The NFSN member wiki offers some NFSN-specific instruction for setting up MediaWiki [in their walled garden].

As with WordPress, I changed the ownership of the top-level PHP files and the upload directories to group ‘web’, as well as changing the permissions of the upload directories to 775.

However, uploaded files are being created with the mode 644 instead of 664. This is hugely annoying, because, still, I’m not allowed to access these files through SSH. I have yet to find out how I can best remedy this. Probably, I’ll end up with writing a simple PHP script that I can call just to chmod everything within the upload directory when the urge to manipulate these files strikes me.

Another beef with MediaWiki is that it creates subdirectories in the uploads directory with mode 777 instead of looking at the mode of the parent dir as WordPress does so neatly.


    12 Comments ( Add comment / trackback )

    1. (permalink) Trackback from startup boise
      On Feb 20, 2016 at 12:58

      startup boise

      BigSmoke » NFSN PHP file write permissions in safe_mode

    2. (permalink) Trackback from subliminalx
      On Feb 20, 2016 at 23:32

      subliminalx

      BigSmoke » NFSN PHP file write permissions in safe_mode

    3. http://www.zunitim.com/comment/html/index.php?page=1&id=20105

      BigSmoke » NFSN PHP file write permissions in safe_mode

    4. (permalink) Trackback from growveg.info
      On Feb 22, 2016 at 10:09

      growveg.info

      BigSmoke » NFSN PHP file write permissions in safe_mode

    5. On Feb 27, 2016 at 19:03

      http://www1.wtbidc.com

      BigSmoke » NFSN PHP file write permissions in safe_mode

    6. (permalink) Trackback from www.ra728.com
      On Mar 03, 2016 at 18:08

      http://www.ra728.com

      BigSmoke » NFSN PHP file write permissions in safe_mode

    7. (permalink) Trackback from hdfilm.eu5.org
      On Mar 04, 2016 at 16:28

      hdfilm.eu5.org

      BigSmoke » NFSN PHP file write permissions in safe_mode

    8. (permalink) Trackback from www.24hx365.com
      On Mar 04, 2016 at 20:11

      http://www.24hx365.com

      BigSmoke » NFSN PHP file write permissions in safe_mode

    9. (permalink) Trackback from maxup.biz
      On Mar 05, 2016 at 04:50

      maxup.biz

      BigSmoke » NFSN PHP file write permissions in safe_mode

    10. On Mar 06, 2016 at 22:49

      http://www.publisherdatabase.com/forums/weblog.php?w=74

      BigSmoke » NFSN PHP file write permissions in safe_mode

    11. (permalink) Trackback from www.listfree.org
      On Mar 08, 2016 at 01:16

      http://www.listfree.org

      BigSmoke » NFSN PHP file write permissions in safe_mode

    12. (permalink) Trackback from http://rudegram.com/
      On Mar 10, 2016 at 07:37

      http://rudegram.com/

      BigSmoke » NFSN PHP file write permissions in safe_mode

    Post a comment

    (required)
    (required)

    Your email is never published nor shared.

    (optional)
    Allowed HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>