I always thought that "sudo" just starts another login-process, specifically using "root" as a username and asking for the Password to login. After running some program, it ends and logs out (or: keeps the open session for some time and then quits it). No idea where I heard that from, but that seemed reasonable to me.
But now I discovered this sudoers-option:
ALL ALL = (root) NOPASSWD: /my/command
This allows me to run any program from any user as a root without asking for any password. This left me wondering:
How does that work? How does a program gain user-rights? Does sudo need a server (running as root as daemon or so) and connects to that server, sends it what should be executed and that server executes it with root-rights?
How about other users?
Is there any good and simple explanation how this works?
ls -l $(which sudo)
will tell you thatsudo
itself is setuid root. Once you're root, you can change to any user's security context, then execute any code you like.