I'll try to explain step by step with some examples.
Short background
Each process has its own 'Process credentials' which include attributes like PID
, the PPID
, PGID
, session ID
and also the real and effective user and group IDs:
RUID
, EUID
, RGID
, EGID
.
We'll focus on those.
Part 1: Understand UID and GID
Now I'll log into a shell with my credentials and run:
$ grep $LOGNAME /etc/passwd
rotem:x:1000:1000:rotem,,,:/home/rotem:/bin/bash
You can see my logname (rotem), the UID and GID which are both 1000, and other details like the shell I'm logged into.
Part 2: Understand RUID and RGID
Every process has an owner and belongs to a group.
In our shell, every process that we'll now run will inherit the privileges of my user account and will run with the same UID and GID.
Let's run a simple command to check it:
$ sleep 10 & ps aux | grep 'sleep'
And check for the process UID and GID:
$ stat -c "%u %g" /proc/$pid/
1000 1000
Those are the real user ID (RUID
) and real group ID (RGID
) of the process.
(*) Check other options to view the UID and GID and ways to get this in a single line.
For now, accept the fact that the EUID
and EGID
attributes are 'redundant' and just equals to RUID
and RGID
behind the scenes.
Part 3: Understand EUID and EGID
Let's take the ping
command as an example.
Search for the binary location with the which
command then run ls -la
:
-rwsr-xr-x 1 root root 64424 Mar 10 2017 ping
You can see that the owner and the group of the file are root
. This is because the ping
command needs to open up a special socket and the Linux kernel demands root
privilege for that.
But how can I use ping
if I don't have root
privilege?
Notice the 's' letter instead of 'x' in the owner part of the file permission.
This is a special permission bit for specific binary executable files (like ping
and sudo
) which is known as setuid.
This is where EUID
and EGID
come into play.
What will happen is when a setuid binary like ping
executes, the process changes its Effective User ID (EUID
) from the default RUID
to the owner of this special binary executable file which in this case is - root
.
This is all done by the simple fact that this file has the setuid
bit.
The kernel makes the decision whether this process has the privilege by looking on the EUID
of the process. Because now the EUID
points to root
, the operation won't be rejected by the kernel.
Notice: On latest Linux releases the output of the ping
command will look different because of the fact that they adopted the Linux Capabilities approach instead of this setuid approach - for those who are not familiar - read here.
Part 4: What about SUID and SGID?
The Saved user ID (SUID
) is being used when a privileged process is running (as root
for example) and it needs to do some unprivileged tasks.
In that case, the effective UID (EUID
) from before will be saved inside SUID
and then changed to an unprivileged task. When the unprivileged task is completed, the EUID
will be taken from the value of SUID
and switch back to privileged account.