A brief explanation of the function of setting user identification number in C language

  • 2020-04-02 03:19:43
  • OfStack

C language setuid() function: sets the real user identification number

The header file:


#include <unistd.h>

Definition function:


int setuid(uid_t uid);

Function description:
Setuid () is used to reset the execution the user identification process at present. However, has an effect to make this function, the valid user identification number must be 0 (root). Under Linux, when using root setuid () to transform into other user identification number, root access will be abandoned, completely into the user's identity, that is to say, the process in the future will no longer be setuid () the right, if only to temporarily abandon root, later you want to retrieve the permissions, You must use seteuid().

Return value: 0 on success, -1 on failure, error code in errno.

Additional notes: generally, when writing programs with setuid root, in order to reduce the system security risk brought by such programs, it is recommended to execute setuid(getuid()) immediately after using root rights; In addition, Linux systems will not produce core dumps when process uids and euids are inconsistent.

C language setreuid() function: set the real and valid user identification number

The header file:


#include <unistd.h>

Definition function:


int setreuid(uid_t ruid, uid_t euid);

Setreuid () is used to set the parameter ruid to the real user identification code of the current process, and the parameter euid to the valid user identification code of the current process. If the parameter ruid or euid value is -1, the corresponding identification code will not change.

Return value: 0 on success, -1 on failure, error code in errno.

C language setfsuid() function: sets the user identification number of the file system
The header file:


 #include <unistd.h>

Definition function:


int setfsuid(uid_t fsuid);

Function description: setfsuid () is used to reset the current process of the file system's user id. In general, the file system's user identification number (fsuid) with a valid user identification number (euid) is the same. If it is super users to call this function, parameter fsuid can be any value, otherwise parameters fsuid must be one of the real/effective/saved user identification number.

Return value: 0 on success, -1 on failure, error code in errno, additional note that this function is Linux specific

Error code:
EPERM: insufficient permissions to complete setup.


Related articles: