Files – Difference Between Owner/Root and RUID/EUID

filespermissionsprocessrootsetuid

I am relatively new to the concepts mentioned in the question and reading about them from different sources only makes them more confusing. So this is what I understood so far:

When we are given permissions for a file, they look like this:

-rwsr-xr-- 1 user1 users 190 Oct 12 14:23 file.bin

We assume that a user user2 who is in the group users tries to execute file.bin. If the setuid bit were not set, this would mean that both the RUID and EUID of file.bin were equal to the UID of user2. But since the setuid bit is set, this means that the RUID is now equal to the UID of user2, while EUID is the UID of the owner of the file, user1.

My questions are:

  1. What is the difference between the owner of the file and root? Does root have the same permissions as the owner? Or would we need a separate entry in the permissions list for root?
  2. Difference between RUID and EUID?
    • As I understand it the RUID and EUID are applied only to processes. If that is the case, why do they have the value of user id's?
    • If RUID is the user who creates the process, and EUID is the user who is currently running the process, then the first sentence of the first answer in this question does not make any sense to me.
    • Did I understand correctly what the setuid bit does?

Best Answer

Here are the answers:

  1. root has always full access to files and directories. The owner of the file usually has them too, but this is not always true. For example:

    -r-xr----- 1 user1 users 199 Oct 14 18:42 otherfile.bin
    

    user1 is the owner; however they can only read and execute, but root still has full access (rwx) to the file.

  2. RUID is the Real User ID and it never (almost) changes. If user2 logs in to the system, the shell is then launched with its real ID set to user2. All processes they start from the shell will inherit the real ID user2 as their real ID.

    EUID is the Effective User ID, it changes for processes (not for the user) that the user executes that have set the setuid bit.

    If user2 executes file.bin, the RUID will be user2 and the EUID of the process started will be user1.

Let's use the case of passwd:

-rwsr-xr-x 1 root root 45396 may 25  2012 /usr/bin/passwd
  • When user2 wants to change their password, they execute /usr/bin/passwd.

  • The RUID will be user2 but the EUID of that process will be root.

  • user2 can use passwd to change only their own password because internally passwd checks the RUID and, if it is not root, its actions will be limited to real user's password.

  • It's neccesary that the EUID becomes root in the case of passwd because the process needs to write to /etc/passwd and/or /etc/shadow.

Related Question