Ben McRae
Ben McRae

Reputation: 3531

CHMOD and the security for the directories on my server

I have a folder on my server on which I have changed the permissions to 777 (read, write and execute all) to allow users to upload their pictures.

So I want to know, what are the security risks involved in this?

I have implemented code to restrict what file formats can be uploaded, but what would happen if someone was to find the location of the directory, can this pose any threat to my server?

Can they start uploading any files they desire?

Thanks.

Upvotes: 2

Views: 1192

Answers (4)

user3850
user3850

Reputation:

When users are uploading files to your server through a web form and some PHP script, the disk access on the server happens with the user id the web server is running under (usually nobody, www-data, apache, _httpd or even root).

Note here, that this single user id is used, regardless of which user uploads the file.

As long as there are no local users accessing the system by other means (ssh, for example), setting the upload directories permissions to 0777 would make not much of a difference -- appart from somebody exploiting a security vulnerability somewhere else in your system there's no one those permissions apply to anyway, and such an attacker would probably just use /tmp.

It is always good practice to set only those permissions on a file or directory that are actually needed. In this case that means probably something like:

drwxrws--- 5 www-data www-data          4096 Nov 17 16:44 upload/

I'm assuming that other local users besides the web server will want to access those files, like the sysadmin or a web designer. Add those users to the group your web server runs under and they don't need sudo or root privileges to access that directory. Also, the +s means that new files and directories in upload/ will automatically be owned by the same group.

As to your last question: just because an attacker knows where the directory is, doesn't mean he can magically make files appear there. There still has to be some sort of service running that accepts files and stores them there... so no, setting the permissions to 0777 doesn't directly make it any less safe.

Still, there are several more dimensions to "safety" and "security" that you cannot address with file permissions in this whole setup:

  • uploaders can still overwrite each others files because they all work with the same user id
  • somebody can upload a malicious PHP script to the upload directory and run it from there, possibly exploit other vulnerabilities on your system and gain root access
  • somebody can use your server to distribute child porn
  • somebody could run a phishing site from your server after uploading a lookalike of paypal.com

...and there are probably more. Some of those problems you may have addressed in your upload script, but then again, understanding of unix file permissions and where they apply comes usually waaaay at the beginning when learning about security issues, which shows that you are probably not ready yet to tackle all of the possible problems.

Have your code looked at by somebody!

Upvotes: 3

Zachary Hamm
Zachary Hamm

Reputation: 628

By what means are these users uploading their pictures? If it's over the web, then you only need to give the web server or the CGI script user access to the folder.

The biggest danger here is that users can overwrite other users files, or delete other users files. Nobody without access to this folder will be able to write to it (unless you have some kind of guest/anonymous user).

If you need a directory that everyone can create files in, what you want is to mimic the permissions of the /tmp directory.

$ chown root:root dir; chmod 777 dir; chmod +t dir;

This way any user can create a file, but they cannot delete files owned by other users.

Contrary to what others have said, the executable bit on a directory in unix systems means you can make that directory your current directory (cd to it). It has nothing to do with executing (execution of a directory is meaningless). If you remove the executable bit, nobody will be able to 'cd' to it.

Upvotes: 1

Jeremy L
Jeremy L

Reputation: 7797

You do not want the executable bit on. As far as *nix goes, the executable bit means you can actually run the file. So, for example, php scripts can be uploaded as type JPEG, and then someone can run that script if they know the location and it's within the web directory.

Upvotes: -1

foobarfuzzbizz
foobarfuzzbizz

Reputation: 58687

If nothing else, I would remove the executable permissions for all users (if not owner and group as well). With this enabled, someone could upload a file that looks like a picture but is really an executable, which might cause no end of damage.

Possibly remove the read and write permissions for all users as well and restrict it to just owner and group, unless you need anonymous access.

Upvotes: 0

Related Questions