Linux – How does the ‘passwd’ command gain root user permissions

linuxpermissionsrootSecurity

How does Linux know that it has to give euid 0 (uid of root) to only certain process like passwd, setuid, etc.

If a process can gain root permissions, will it not lead to potential security breaches in the Linux platform?

For example, if I write a program that can can gain root user permissions like passwd, I may corrupt important system files like /etc/passwd, /etc/groups.

How does Linux manage to still be secure?

Best Answer

The passwd program has the setuid bit set, which you can see with ls -l:

-rwsr-xr-x 1 root root 39104 2009-12-06 05:35 /usr/bin/passwd

It's the s (the fourth character of the line).

All programs that have this permission bit set run as the owner of that program. In this example, the user is root (third word of the line).

These setuid programs need to make sure that they don't damage anything, since every user of the system can run them with effective root privileges. That's why you can only change your own password. Linux and other similar operating systems are still secure because the authors of these setuid programs take a lot of care.

See for example suexec.c from the Apache Web Server, which is a popular setuid program. There are unusually many comments in that source code.

Related Question