Search code examples
clinuxgccsetuid

linux, setuid when user id does not exists


  1. In gcc, as root user, I call setuid system call with invalid user id (3009). errno variable is 0 (successful).

  2. When the program is running, in another session I execute ps command and I see the value of uid and euid is 3009 (invalid user id).

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

int main () {
   int     i;
   i=setuid(3009);    // user id 3009 does not exists
   printf ("i=%d, err=%d(%s)\n", i, errno, strerror(errno));
   
   exit(0);
}

Is it ok or has some reasons? Platform is gcc 11.4 and Ubuntu 22.04.4.


Solution

    1. In gcc, as root user, I call setuid system call with invalid user id (3009). errno variable is 0 (successful).
    1. When the program is running, in another session I execute ps command and I see the value of uid and euid is 3009 (invalid user id).

    3009 is not (generally) an invalid user id. I presume that you mean that there is no user account with that uid, but the kernel does not know or care about mapping uid numbers to usernames or other account details. The absence of such mappings does not invalidate a uid. So yes, it's ok, in the sense that the kernel and the setuid() syscall wrapper will (typically) accept it.

    But that doesn't make it wise or good form.