Users, Groups and setuid Explained for Systems Programmers
Learn real, effective and saved user IDs, what setuid does at execve, and the correct order to drop privileges in C with setgroups, setgid and setuid.

Every Linux process runs as someone. That someone is a number, and nearly every permission check the kernel makes starts from it. Systems programmers meet this the first time a daemon needs to bind a privileged port and then stop being root, and they usually meet it wrongly: they call setuid once and assume the job is done. This post defines the identities a process carries, explains what the setuid bit does when a file is executed, and shows the order in which a C program must give up privilege so it cannot get it back. It is the foundation for Linux capabilities and the rest of the sandboxing cluster. The interfaces are those of Linux 6.12 with glibc 2.40, documented in credentials(7) and setresuid(2).
A user is a number
The kernel does not know user names. It knows a 32-bit user ID (UID) and a group ID (GID). The names come from /etc/passwd and /etc/group, or from a directory service, and are looked up by libc functions like getpwnam(). File ownership is stored as the numbers, so deleting a user does not change who owns their files.
id
id rootuid=1000(alice) gid=1000(alice) groups=1000(alice),27(sudo),100(users)
uid=0(root) gid=0(root) groups=0(root)UID 0 is special by convention, and the special treatment comes from capabilities as covered in the previous post. Everything else is ordinary. A process also has supplementary groups, a list of extra GIDs that count for file access. The list above shows three for alice.
Three UIDs per process
A process does not have one user ID. It has three, and they exist to make privilege dropping reversible when you want it to be.
The real UID says who started the process. It is what
getuid()returns.The effective UID is what the kernel uses for permission checks. It is what
geteuid()returns.The saved set-user-ID is a copy of the effective UID taken at
execve(). It lets a process swap its effective UID back to a value it once had.
Groups follow the same pattern (real, effective and saved GID), and Linux adds a filesystem UID used only for file access, which tracks the effective UID unless you set it separately. For most code, ignore the filesystem UID.
The rules for changing them are what trip people up. An unprivileged process may set its effective UID to its real UID or its saved UID. A privileged process (one with CAP_SETUID) may set any of them to anything. That asymmetry is the whole design.
What the setuid bit does
A file with the setuid permission bit, shown as s in the owner execute position, changes the rules at execve(). The new process keeps its real UID, but its effective UID and saved UID become the file owner's.
ls -l /usr/bin/passwd-rwsr-xr-x 1 root root 72056 Apr 4 12:41 /usr/bin/passwdWhen alice runs passwd, the process has real UID 1000 and effective UID 0. It can edit /etc/shadow because the kernel checks the effective UID, but it can also find out who actually asked, because the real UID is still hers. The same mechanism exists for groups with setgid. A good setuid program uses that gap: it does its privileged work, drops to the real UID, and does the rest unprivileged.
You can see all three IDs for a running process in /proc/PID/status:
grep -E '^(Uid|Gid|Groups)' /proc/self/statusUid: 1000 1000 1000 1000
Gid: 1000 1000 1000 1000
Groups: 27 100 1000 The four Uid values are real, effective, saved and filesystem, in that order.
The correct drop, and why order matters
A daemon starts as root, binds a port, and should then run as an unprivileged account. Three calls do it, and they must run in this order.
#define _GNU_SOURCE
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
static void die(const char *msg) {
perror(msg);
exit(1);
}
int main(void) {
struct passwd *pw = getpwnam("nobody");
if (pw == NULL) {
fprintf(stderr, "no such user\n");
return 1;
}
/* 1. Drop supplementary groups while we still have the power to. */
if (setgroups(0, NULL) < 0) die("setgroups");
/* 2. Group before user: after setuid we could no longer change groups. */
if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) < 0) die("setresgid");
/* 3. Set real, effective and saved UID together so none is left as root. */
if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) < 0) die("setresuid");
/* 4. Verify the drop is irreversible. */
if (setuid(0) == 0) {
fprintf(stderr, "dropped privileges can be regained\n");
return 1;
}
printf("uid=%d euid=%d gid=%d\n",
(int)getuid(), (int)geteuid(), (int)getgid());
return 0;
}Run as root with sudo ./dropprivs on a system where nobody is UID 65534 and GID 65534 (Debian names that group nogroup, but the GID is the same):
uid=65534 euid=65534 gid=65534Three details earn their place. setgroups(0, NULL) clears the supplementary list; without it a process that was root keeps root's groups, including shadow or docker, and the drop is cosmetic. Group IDs are set before the user ID because changing groups needs privilege, and setuid removes it. And setresuid sets all three UIDs in one call. A plain setuid(uid) called by root also sets all three, but seteuid(uid) changes only the effective UID, leaving the saved UID at 0, and the process can climb back with seteuid(0). That bug has shipped in real software.
Why the verification step is there
The setuid(0) probe in step 4 is cheap and catches the failure that matters. After a correct drop it returns -1 with EPERM. If it ever succeeds, a saved UID or a capability survived, and the process is still privileged. We keep it as a development assertion in privilege-dropping code and let it fail the start-up, not as production logging.
Capabilities need their own attention. Switching all UIDs from 0 to non-zero clears the permitted and effective capability sets by default, as described in capabilities(7), so the code above leaves the process with none. A program that sets SECBIT_KEEP_CAPS or uses ambient capabilities to keep one is making a deliberate exception, and should say so in a comment.
Common mistakes
Dropping the user but not the groups. A process with UID 65534 and a stale shadow group can still read /etc/shadow on systems where the group has access to it.
**Using seteuid for a permanent drop.** It is a temporary switch. Use it only when the program must come back, such as a setuid tool that touches the user's files as the user and then returns to root.
Dropping after parsing untrusted input. Do the privileged setup, then drop, then read anything an attacker influences. Reversing that order puts the parser inside the privileged window.
**Forgetting chdir and file descriptors.** A directory handle or open file descriptor survives the drop with the access it was opened with. Close what the unprivileged code does not need before you drop.
Testing what you did
Without running anything, you can reason about whether a drop is complete by listing what the process can still do: its three UIDs, its group list and its capability sets. All of them are visible in /proc/PID/status, as the Uid, Gid, Groups and Cap* lines. A daemon that calls the code above should show the same UID in all four Uid columns, an empty or intended Groups line, and zero capability masks.


