ALPHA!
webmaster@virusexperts.com
Site Navigation:

Filesystem Security

Posted 2004.09.07 16:49 UTC

Amir Malik (amir@virusexperts.com) Webmaster

Plain Old Vanilla

Almost everyone these days has had to install an operating system. After a typical installation, you are left with an uncustomized system that is begging to be tweaked to your heart's content. Many of the tweaks we make include user-interface themes, plugins, additional applications, games, etc. However, the customizations that really need to be put in place are those that deal with some of the lowest levels of the operating system. The filesystem.

The security mechanisms offered by the Unix filesystem (not filesystem type such as ext2, reiserfs, or ufs) are inherently weak. The security offered is by way of file ownership and permissions. Firstly, a file (or a directory, since a directory is really just a different "type" of file) contains two types of ownership: user and group. A file may only have one owner. The file also belongs to a group, usually the group of the owner. Access to the file is controlled through user, group, and world permissions. The access for entity (user, group, or world) is determined by the combination of three bits: read, write, and execute. Let's make this more believable by providing a directory listing.

$ ls -l /etc/passwd
-rw-r--r--    1 root     root         2946 Aug 11 17:07 /etc/passwd
The first column contains the permission information for the password file. The third and fourth columns indicate the user and group owners, respectively. Let's decipher the first column. It is made up three permissions; let's use "-xxxyyyzzz" as an example. Note that the leading minus sign is used for something else, hence we are not worried about it. Then xxx would represent permissions for the owner of the file, yyy the permissions for the group owner, and zzz the permissions for everyone else. You would belong to the everyone or "world" group if you are not the owner or are not in the owner group. Let's examine the types of permissions more closely.
  • r: read permission
  • w: write permission
  • x: execute permission
Note that in order to be able to execute a file, you need to have both read and execute permissions (the read permission is required for the operating system to be able to read the code to be executed).

Remember how I said earlier that a directory is like a file? Well that is not exactly true. Read permission on a directory means you are able to view its contents (note that this is not recursive). Write permission to a directory means you are allowed to create files (or directories) inside it. Finally, execute permission allows you to enter a directory, which you will need in order to be able to read or write files that reside inside the directory.

System-level Security

Most fresh installations are not secure by default because the vendor chose compatibility over security. By making some part of a system more secure, you are potentially causing an incompatibility with some software that requires that particular insecurity to be present. The first tip is an example of just this.

Securing $HOME/..
Although this is very trivial, removing the read bits for the world permissions, will not allow the home directory (/home, /usr/home, or equivalent) to be read. This gives many people an illusion of security, rather than true comfort because if someone is trying to get the list of users on your system, their destination would not be /home, rather it would be the world-readable file, /etc/passwd. But this does give some security when you are storing more than users' home directories in /home. This can also be useful in shared hosting environments, where you have all of your domains under a common directory, and don't want users snooping in there to find out what you are hosting.

# chmod o-r /home
This is letting chmod (change permission mode) know that we want to remove (note the minus sign, as opposed to a plus sign) the read bit from the "other," or world group. Let's verify it, look for the entry for the current directory, "." (with respect to /home).
# ls -al /home
drwxr-x--x   4 root root   120 Sep  2 16:58 .
The first column confirms that only root and members of the root group (root is a member of the root group) are able to read the contents of /home. Note that if you want some special users (such as yourself) to be able to view the contents of /home without being root, you might want to add yourself to the root group (thereby elevating yourself to being right below root himself). Traditionally, such users are added to the "wheel" or "root" group.

Resource Limits
When you are on a multi-user system, it is important to define sane resource limits so that no one user may hog all of the CPU, memory, or disk resources. It is difficult to give examples that work all- around, but let's take a look at the general limits you should start off with.

  • core file size: Ideally should be set to zero since 99% of users will never need to keep core files laying around unless they are debugging something. Furthermore, since core files can be quite large, it is a good idea to not have them created.
  • maximum file size: Setting this to a reasonable large value will limit users from creating arbitrarily large files which could eat up valuable disk space. The situation could become worse if disk quotas are not enabled, so make sure you set this to a good value depending on your needs.
  • maximum number of open files: On fresh installations, this value is usually set to 1024. You should not expect regular users to have over a thousand files open unless they are running a server. In fact, for users running servers, you would want to increase this limit, since the maximum number of open files directly correlates to the maximum number of simultaneous TCP connections available for that user. A reasonable limit is 256, but your mileage may vary.
  • maximum number of processes: This is an important setting that limits the number of processes per user. Setting it too high may give users too much wiggle room to launch as many processes as they want; increasing overall system latency. Setting it too low may inhibit many programs from running. A reasonable setting is 40.
  • maximum number of logins: The default setting is usually unlimited. A more reasonable setting would be 5.
  • maximum locked memory: There is no defined default for this one because it depends on each user. This should be set to a few megabytes at most.
  • maximum pipe size: The default on most systems is 8*512 bytes, so 4 kilobytes. This is a reasonable default. However, you may want to increase this in certain situations, for example, when you are compiling software in parallel and the compiler is making use of pipes, rather than temporary files.
  • maximum stack size: A common default for this setting is 8 megabytes. You may lower this at your own discretion, but again, each situation is different. Lower this only if you plan on running applications that don't require much memory.
You may want (or need) to increase some of the above limits if you have a great deal of RAM, fewer users, or are providing virtual desktops for remote users who need to run heavy applications.

To find out how to change these resource limits on your system, do the following:

  • Linux users: Edit /etc/security/limits.conf or /etc/limits. Any changes you make take effect the next time users log in.
  • BSD users: Edit /etc/login.conf. Then you must rebuild the database /etc/login.conf.db by executing:
    cap_mkdb /etc/login.conf
    Changes take effect upon users' next login.
Other users should consult their system manual to determine how to set up resource limits.

User-level security

So now that you have set some reasonable defaults on your system, what do your users need to do to make their environment more safe? I'll try to give some tips for users.

Securing $HOME
It is vital to secure one's home directory because any valuable information can be found there. By default, when a new user is added to the system, the permissions on the user's home directory are usually read and execute for everyone. That means everyone will be able to read the contents of his home directory. Armed with that, they will be able to determine what other files (and directories) they are able to access. Thus it is important to secure at least the base of your home directory by setting its permissions to a more secure setting.

chmod g-r,o-r $HOME
This tells chmod to remove the read permission from the group permission, and also remove the read permission from everyone else. If your system's chmod does not like the symbolic notation for permissions, you may need to use octal notation, which works as follows. Each permission (user, group, and other), is assigned one number from 0 to 7, made by adding up the following values:
  • 4: read permission
  • 2: write permission
  • 1: execute permission
So if we wanted to create a permission for read and write permissions, we would add 4 and 2 to get 6. This is done three times to get permission values for the user, group, and other. Whereas the previous execution of chmod worked on the existing permissions of $HOME (by removing or adding extra permissions), we will set an explicit permission mode for $HOME. We want the user to have all (read, write, execute) permissions, so the user field will have a value of 4 + 2 + 1 = 7, the group will only have execute permission (1), and so will everyone else. Taking this knowledge and applying it on the command line, we have:
chmod 711 $HOME
You may be asking yourself, "Why do I even want to give everyone else execute permission into my home directory?" This is a requirement if a web server is running on the same machine because it will need to access your personal files under $HOME/public_html. If you are not hosting your web space under this account, you do not need to give everyone else execute permission to your directory, making it much more secure.

File and directory permissions are a simple way to keep nosy visitors to your website away from content you do not want them to see without the use of Apache's .htaccess files. However, these permissions will apply to all applications and processes.

  • Removing the read bit for the world permission will cause a file to be inaccessible by Apache, resulting in a 403 Forbidden error message.
  • Removing the execute bit for the world permission will cause a directory and its subdirectories to be in accessible, resulting in a 403 Forbidden error mesage.

See Also

Internet Security 101

Resources

Google

Projects

Find out more about our various projects.

Documentation