views:

447

answers:

4

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. 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, Ben

A: 

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.

samoz
Hi samoz, thanks for the reply. When it says user/group and then everyone - who falls under what group? i am assuming i am the owner?
Ben McRae
what does the eXecutable flag on a directory have to do with disguising executables as images?
hop
also what does it mean by executable, as in executing programs?
Ben McRae
The executable flag decides whether or not you can execute code or not. If this flag is off, even if you disguise an executable as an image, you can't run it, which is ideal.
samoz
a) this is not true. there are tricks to execute code even without +x; b) we are talking about the permissions of a directory, not a file.
hop
A: 

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.

Nerdling
The folder permissions are 777 - not necessarily the file permissions. I agree, files do not need execute in general; folders do as you cannot access the files inside without execute (search) permission.
Jonathan Leffler
+1  A: 

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.

ebencooke
Not quite right on the directory x-bit, but close enough.
paxdiablo
I know chdir() will fail with EACCES if the dir is -x. It might control other things?
ebencooke
Thank you for the reply. The user's will be uploading through a form in a php document. When you say without access to this folder, theoretically any who has an account on my website will have access to upload there pictures?Who falls under the category of a guest/anonymous user?Thanks for your help
Ben McRae
@eben, it basically controls the ability to look inside the directory "file" so "cd dir" will fail. Also "find dir -print" will not work, or "ls dir/*". You can still get at the *files* if you know their names (such as "cat dir/known_file").
paxdiablo
Assuming the file permissions allow it of course :-)
paxdiablo
@Pax: not quite; you need x permission on a directory to access the files; you need r permission on a directory to read the directory (and list the files with 'ls', for example). You're right that if you know the name, you only need x permission on the directory and the name/permissions on the file.
Jonathan Leffler
Sorry, @JL, you're right. R gets you the ability to read the directory, you still need X to get at the files within.
paxdiablo
@Ben: If you're using a PHP script, then the only account on the server that needs write permission to that folder is the one under which the PHP script is running. Probably this is 'www' or some such. By an account, I meant a login shell, or an ftp account. 'Account on website' is too vague.
ebencooke
@ebencooke, sorry you are right, i didnt really explain my question to well in the first place. No one except from myself will have access to shell, ftp or anything like that! I didnt really understand how far this question could really go before i posted it. i will edit the original question!
Ben McRae
+1  A: 

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!

hop
Thank you for that great post, i am 19 years old and have only been coding PHP for 9 months - im not trying to use that as an excuse though, i just never really understood to what extent security goes, but its alot bigger than i thought. Thank you, i have alot of research to do!
Ben McRae
ooo and one more thing, with regards to a user uploading a malicousa php file. do you know any good ways i can prevent this? would be very very helpful indeed! thanks again.
Ben McRae
ask this in another question, referencing this one, and i'll try to answer it. there are a few things you can do to avoid trouble.
hop