uid and euid

What uid/euid does a process get when it is started? I have misunderstood this issue for long time. In the past I thought a process gets its uid/euid from the owner of the program file. You can use “ls -l program” to see the owner of the program. But the fact is process inherits the uid/euid of it process owner, not file owner. What is the process owner of a process? A process owner is the process that creates the process in question. If you run a program in bash, the process owner of the run process is the bash process. The bash process gets its uid/euid from /etc/passwd. The login process sets the uid/euid of the bash based on the uid/euid in /etc/passwd for the login user.

You can run another program in your program by calling system or exec function. The difference between system and exec is well explained in this post. Creating a new process always uses the system call fork. The system call also calls fork. Exec does not create new process. The system call equals fork+exec(“bash your program”). There is a subtle difference that nobody notices: the new process created by system() does not inherit the euid of parent process, it only inherits the uid and the euid of the child process is set to its uid. The child process created by ordinary fork/exec inherits both uid and euid from parent process.

The setuid bit of file permission is used to change the euid if the file is executed. Instead of inheriting euid from parent, the setuid process’s euid is set to the file owner of the program at starting time. You do not need to call setuid function to set the euid of the process.

Posted in tips of hosting