RHCSA Rapid Track
Objectives
After completing this section, students should be able to:
Control the default permissions of new files created by users.
Explain the effect of special permissions.
Use special permissions and default permissions to set the group owner of files created in a particular directory.
Special Permissions
Special permissions constitute a fourth permission type in addition to the basic user, group, and other types. As the name implies, these permissions provide additional access-related features over and above what the basic permission types allow. This section details the impact of special permissions, summarized in the table below.
Table 4.1. Effects of Special Permissions on Files and Directories
|
Special permission |
Effect on files |
Effect on directories |
|---|---|---|
|
|
File executes as the user that owns the file, not the user that ran the file. |
No effect. |
|
|
File executes as the group that owns the file. |
Files newly created in the directory have their group owner set to match the group owner of the directory. |
|
|
No effect. |
Users with write access to the directory can only remove files that they own; they cannot remove or force saves to files owned by other users. |
The setuid permission on an executable file means that commands run as the user owning the file, not as the user that ran the command. One example is the passwd command:
[user@host ~]$ls -l /usr/bin/passwd-rwsr-xr-x. 1 root root 35504 Jul 16 2010 /usr/bin/passwd
In a long listing, you can identify the setuid permissions by a lowercase s where you would normally expect the x (owner execute permissions) to be.
If the owner does not have execute permissions, this is replaced by an uppercase S.
The special permission setgid on a directory means that files created in the directory inherit their group ownership from the directory, rather than inheriting it from the creating user.
This is commonly used on group collaborative directories to automatically change a file from the default private group to the shared group, or if files in a directory should be always owned by a specific group.
An example of this is the /run/log/journal directory:
[user@host ~]$ls -ld /run/log/journaldrwxr-sr-x. 3 root systemd-journal 60 May 18 09:15 /run/log/journal
If setgid is set on an executable file, commands run as the group that owns that file, not as the user that ran the command, in a similar way to setuid works. One example is the locate command:
[user@host ~]$ls -ld /usr/bin/locate-rwx--s--x. 1 root slocate 47128 Aug 12 17:17 /usr/bin/locate
In a long listing, you can identify the setgid permissions by a lowercase s where you would normally expect the x (group execute permissions) to be.
If the group does not have execute permissions, this is replaced by an uppercase S.
Lastly, the sticky bit for a directory sets a special restriction on deletion of files.
Only the owner of the file (and root) can delete files within the directory.
An example is /tmp:
[user@host ~]$ls -ld /tmpdrwxrwxrwt. 39 root root 4096 Feb 8 20:52 /tmp
In a long listing, you can identify the sticky permissions by a lowercase t where you would normally expect the x (other execute permissions) to be.
If other does not have execute permissions, this is replaced by an uppercase T.
Setting Special Permissions
Symbolically: setuid =
u+s; setgid =g+s; sticky =o+tNumerically (fourth preceding digit): setuid = 4; setgid = 2; sticky = 1
Examples
Add the setgid bit on
directory:[user@host ~]#chmod g+s directorySet the setgid bit and add read/write/execute permissions for user and group, with no access for others, on
directory:[user@host ~]#chmod 2770 directory
Default File Permissions
When you create a new file or directory, it is assigned initial permissions. There are two things that affect these initial permissions. The first is whether you are creating a regular file or a directory. The second is the current umask.
If you create a new directory, the operating system starts by assigning it octal permissions 0777 (drwxrwxrwx).
If you create a new regular file, the operating system assignes it octal permissions 0666 (-rw-rw-rw-).
You always have to explicitly add execute permission to a regular file.
This makes it harder for an attacker to compromise a network service so that it creates a new file and immediately executes it as a program.
However, the shell session will also set a umask to further restrict the permissions that are initially set. This is an octal bitmask used to clear the permissions of new files and directories created by a process. If a bit is set in the umask, then the corresponding permission is cleared on new files. For example, the umask 0002 clears the write bit for other users. The leading zeros indicate the special, user, and group permissions are not cleared. A umask of 0077 clears all the group and other permissions of newly created files.
The umask command without arguments will display the current value of the shell's umask:
[user@host ~]$umask0002
Use the umask command with a single numeric argument to change the umask of the current shell. The numeric argument should be an octal value corresponding to the new umask value. You can omit any leading zeros in the umask.
The system's default umask values for Bash shell users are defined in the /etc/profile and /etc/bashrc files.
Users can override the system defaults in the .bash_profile and .bashrc files in their home directories.
umask Example
The following example explains how the umask affects the permissions of files and directories. Look at the default umask permissions for both files and directories in the current shell. The owner and group both have read and write permission on files, and other is set to read. The owner and group both have read, write, and execute permissions on directories. The only permission for other is read.
[user@host ~]$umask0002[user@host ~]$touch default[user@host ~]$ls -l default.txt-rw-rw-r--. 1 user user 0 May 9 01:54 default.txt[user@host ~]$mkdir default[user@host ~]$ls -ld defaultdrwxrwxr-x. 2 user user 0 May 9 01:54 default
By setting the umask value to 0, the file permissions for other change from read to read and write. The directory permissions for other changes from read and execute to read, write, and execute.
[user@host ~]$umask 0[user@host ~]$touch zero.txt[user@host ~]$ls -l zero.txt-rw-rw-rw-. 1 user user 0 May 9 01:54 zero.txt[user@host ~]$mkdir zero[user@host ~]$ls -ld zerodrwxrwxrwx. 2 user user 0 May 9 01:54 zero
To mask all file and directory permissions for other, set the umask value to 007.
[user@host ~]$umask 007[user@host ~]$touch seven.txt[user@host ~]$ls -l seven.txt-rw-rw----. 1 user user 0 May 9 01:55 seven.txt[user@host ~]$mkdir seven[user@host ~]$ls -ld sevendrwxrwx---. 2 user user 0 May 9 01:54 seven
A umask of 027 ensures that new files have read and write permissions for user and read permission for group. New directories have read and write access for group and no permissions for other.
[user@host ~]$umask 027[user@host ~]$touch two-seven.txt[user@host ~]$ls -l two-seven.txt-rw-r-----. 1 user user 0 May 9 01:55 two-seven.txt[user@host ~]$mkdir two-seven[user@host ~]$ls -ld two-sevendrwxr-x---. 2 user user 0 May 9 01:54 two-seven
The default umask for users is set by the shell startup scripts. By default, if your account's UID is 200 or more and your username and primary group name are the same, you will be assigned a umask of 002. Otherwise, your umask will be 022.
As root, you can change this by adding a shell startup script named /etc/profile.d/local-umask.sh that looks something like the output in this example:
[root@host ~]#cat /etc/profile.d/local-umask.sh# Overrides default umask configuration if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then umask 007 else umask 022 fi
The preceding example will set the umask to 007 for users with a UID greater than 199 and with a username and primary group name that match, and to 022 for everyone else. If you just wanted to set the umask for everyone to 022, you could create that file with just the following content:
# Overrides default umask configuration umask 022
To ensure that global umask changes take effect you must log out of the shell and log back in. Until that time the umask configured in the current shell is still in effect.
References
bash(1), ls(1), chmod(1), and umask(1) man pages