The Importance of the Set User/Group Bit in Linux Systems

KaustubhKaustubh
2 min read

Prerequisites:

  1. Basic Linux Knowledge like file permissions, users, and groups.

  2. Basic Privilege Management of root and standard user privileges.

Purpose: The setuid (Set User ID) and setgid (Set Group ID) bits are designed to allow a user or group to execute commands with the permissions of the command’s owner or group, respectively. This enables modification of restricted files and directories, but only through the specific logic defined in the command, ensuring controlled access.

Under the hood: When a user executes a command, the process generated by the command inherits the user’s real and effective UIDs (RUID and EUID). However, if the command has the setuid/setgid bit set, the process’s effective UID is temporarily changed to that of the command’s owner or group. This allows the process to perform actions that the user couldn’t normally do, but only according to the command’s intended functionality. Like, the process can modify the owner’s restricted file but only as per the logic written/approved by the owner for modification.

Example: I want to modify /etc/shadow to change my password and for that I need write access to it. But providing me with the write access means that I can change the passwords of others too. The solution to this is creating a command (/usr/bin/passwd or passwd) that can write to /etc/shadow by the logic defined by the command, and I should not be allowed to modify this trusted command. Hence, when using this command, our process gets owner (here root)'s access to /etc/shadow without giving the user the access to.

Word of Caution: While setuid/setgid are useful, they can be risky if misused. Any flaw in the command with these bits on will potentially jeopardize the entirety of owner’s data on the platform.

18
Subscribe to my newsletter

Read articles from Kaustubh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kaustubh
Kaustubh