Open In App

SetUID, SetGID, and Sticky Bits in Linux File Permissions

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

As explained in the article Permissions in Linux, Linux uses a combination of bits to store the permissions of a file. We can change the permissions using the chmod command, which essentially changes the ‘r’, ‘w’ and ‘x’ characters associated with the file.

Further, the ownership of files also depends on the uid (user ID) and the gid (group ID) of the creator, as discussed in this article. Similarly, when we launch a process, it runs with the uid and gid of the user who launched it.

1. The setuid bit
This bit is present for files which have executable permissions. The setuid bit simply indicates that when running the executable, it will set its permissions to that of the user who created it (owner), instead of setting it to the user who launched it. Similarly, there is a setgid bit which does the same for the gid.

To locate the setuid, look for an ‘s’ instead of an ‘x’ in the executable bit of the file permissions.

An example of an executable with setuid permission is passwd, as can be seen in the following output.

ls -l /etc/passwd

This returns the following output:

-rwsr-xr-x root root 2447 Aug 29  2018 /etc/passwd

As we can observe, the ‘x’ is replaced by an ‘s’ in the user section of the file permissions.

To set the setuid bit, use the following command.

chmod u+s 

To remove the setuid bit, use the following command.

chmod u-s 

2. The setgid bit

The setgid affects both files as well as directories. When used on a file, it executes with the privileges of the group of the user who owns it instead of executing with those of the group of the user who executed it.
When the bit is set for a directory, the set of files in that directory will have the same group as the group of the parent directory, and not that of the user who created those files. This is used for file sharing since they can be now modified by all the users who are part of the group of the parent directory.

To locate the setgid bit, look for an ‘s’ in the group section of the file permissions, as shown in the example below.

-rwxrwsr-x root root 1427 Aug 2 2019 sample_file

To set the setgid bit, use the following command.

chmod g+s 

To remove the setgid bit, use the following command.

chmod g-s 

Security Risks

The setuid bit is indeed quite useful in various applications, however, the executable programs supporting this feature should be carefully designed so as to not compromise on any security risks that follow, such as buffer overruns and path injection. If a vulnerable program runs with root privileges, the attacker could gain root access to the system through it. To dodge such possibilities, some operating systems ignore the setuid bit for executable shell scripts.

3. The sticky bit
The sticky bit was initially introduced to ‘stick’ an executable program’s text segment in the swap space even after the program has completed execution, to speed up the subsequent runs of the same program. However, these days the sticky bit means something entirely different.

When a directory has the sticky bit set, its files can be deleted or renamed only by the file owner, directory owner and the root user. The command below shows how the sticky bit can be set.

chmod +t 

Simply look for a ‘t’ character in the file permissions to locate the sticky bit. The snippet below shows how we can set the sticky bit for some directory “Gatos”, and how it prevents the new user from deleting a file in the directory.

To remove the sticky bit, simply use the following command.

chmod -t 

Since deleting a file is controlled by the write permission of the file, practical uses of the sticky bit involve world-writable directories such as ‘/tmp’ so that the delete permissions are reserved only for the owners of the file.


Last Updated : 07 Aug, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads