Giving up privileges

From the previous discussion, it seems as if the set*id() system calls (setuid(2), seteuid(2), setreuid(2), setresuid(2)) are only useful to root, as only with root privileges can we use the system calls to change the process credentials. Well, that's not really the full truth; there's another important case, for non-privileged processes.

Consider this scenario: our program specification requires the initialization code to run with root privileges; the rest of the code does not. Obviously, we don't want to give the end user root access just to run our program. How do we solve this?

Making the program setuid-root would nicely do the trick. As we've seen, a setuid-root process will always run as root; but after the initialization ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.