posixuser-permissionsuid

Can I assume that nobody is 65534?


I'm writing a setuid root program. This program needs to open a file for writing and then write some content. It needs euid 0 only for opening the file, then it can drop privileges.

To drop privileges, I could seteuid to the current uid. But I was thinking at switching to nobody:nogroup.

Now, I was wondering: can I assume that nobody is 65534 on every system (and nogroup is 65534 too)? Is it defined by some standard (POSIX, maybe)?


Solution

  • You can't. nobody has had at least a few different IDs across distros and time:

    Historically, the user “nobody” was assigned UID -2 by several operating systems, although other values such as 2^(15)−1 = 32,767 are also in use, such as by OpenBSD. For compatibility between 16-bit and 32-bit UIDs, many Linux distributions now set it to be 2^(16)−2 = 65,534; the Linux kernel defaults to returning this value when a 32-bit UID does not fit into the return value of the 16-bit system calls. An alternative convention assigns the last UID of the range statically allocated for system use (0-99) to nobody: 99.


    You can however rely on the name (i.e. assume that nobody is called "nobody" everywhere it exists): this account is in the Linux Standard Base standard which is supported by all main distributions. So you can use things like getpwnam("nobody") (which is the way recommended by the LSB on the link above).