1SETREUID(2)                Linux Programmer's Manual               SETREUID(2)
2
3
4

NAME

6       setreuid, setregid - set real and/or effective user or group ID
7

SYNOPSIS

9       #include <unistd.h>
10
11       int setreuid(uid_t ruid, uid_t euid);
12       int setregid(gid_t rgid, gid_t egid);
13
14   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
15
16       setreuid(), setregid():
17           _XOPEN_SOURCE >= 500
18               || /* Since glibc 2.19: */ _DEFAULT_SOURCE
19               || /* Glibc <= 2.19: */ _BSD_SOURCE
20

DESCRIPTION

22       setreuid() sets real and effective user IDs of the calling process.
23
24       Supplying a value of -1 for either the real or effective user ID forces
25       the system to leave that ID unchanged.
26
27       Unprivileged processes may only set the effective user ID to  the  real
28       user ID, the effective user ID, or the saved set-user-ID.
29
30       Unprivileged users may only set the real user ID to the real user ID or
31       the effective user ID.
32
33       If the real user ID is set (i.e., ruid is not -1) or the effective user
34       ID  is set to a value not equal to the previous real user ID, the saved
35       set-user-ID will be set to the new effective user ID.
36
37       Completely analogously, setregid() sets real and effective  group  ID's
38       of the calling process, and all of the above holds with "group" instead
39       of "user".
40

RETURN VALUE

42       On success, zero is returned.  On error, -1 is returned, and  errno  is
43       set to indicate the error.
44
45       Note: there are cases where setreuid() can fail even when the caller is
46       UID 0; it is a grave security error to omit checking for a failure  re‐
47       turn from setreuid().
48

ERRORS

50       EAGAIN The call would change the caller's real UID (i.e., ruid does not
51              match the caller's real UID), but there was a temporary  failure
52              allocating the necessary kernel data structures.
53
54       EAGAIN ruid  does  not  match the caller's real UID and this call would
55              bring the number of processes belonging to the real user ID ruid
56              over the caller's RLIMIT_NPROC resource limit.  Since Linux 3.1,
57              this error case no longer occurs (but robust applications should
58              check  for  this  error);  see  the description of EAGAIN in ex‐
59              ecve(2).
60
61       EINVAL One or more of the target user or group IDs is not valid in this
62              user namespace.
63
64       EPERM  The  calling  process is not privileged (on Linux, does not have
65              the necessary capability in its user  namespace:  CAP_SETUID  in
66              the case of setreuid(), or CAP_SETGID in the case of setregid())
67              and a change other than (i) swapping the effective user  (group)
68              ID  with  the  real  user (group) ID, or (ii) setting one to the
69              value of the other or (iii) setting the effective  user  (group)
70              ID  to  the  value of the saved set-user-ID (saved set-group-ID)
71              was specified.
72

CONFORMING TO

74       POSIX.1-2001, POSIX.1-2008, 4.3BSD (setreuid() and setregid() first ap‐
75       peared in 4.2BSD).
76

NOTES

78       Setting  the  effective user (group) ID to the saved set-user-ID (saved
79       set-group-ID) is possible since Linux 1.1.37 (1.1.38).
80
81       POSIX.1 does not specify all of the UID changes that Linux permits  for
82       an  unprivileged process.  For setreuid(), the effective user ID can be
83       made the same as the real user ID or the saved set-user-ID, and  it  is
84       unspecified  whether unprivileged processes may set the real user ID to
85       the real user ID, the effective user ID, or the saved set-user-ID.  For
86       setregid(),  the real group ID can be changed to the value of the saved
87       set-group-ID, and the effective group ID can be changed to the value of
88       the  real  group  ID or the saved set-group-ID.  The precise details of
89       what ID changes are permitted vary across implementations.
90
91       POSIX.1 makes no specification about the effect of these calls  on  the
92       saved set-user-ID and saved set-group-ID.
93
94       The  original  Linux  setreuid()  and setregid() system calls supported
95       only 16-bit user and group IDs.   Subsequently,  Linux  2.4  added  se‐
96       treuid32()  and  setregid32(),  supporting  32-bit  IDs.  The glibc se‐
97       treuid() and setregid() wrapper functions transparently deal  with  the
98       variations across kernel versions.
99
100   C library/kernel differences
101       At the kernel level, user IDs and group IDs are a per-thread attribute.
102       However, POSIX requires that all threads in a process  share  the  same
103       credentials.   The  NPTL threading implementation handles the POSIX re‐
104       quirements by providing wrapper functions for the various system  calls
105       that  change process UIDs and GIDs.  These wrapper functions (including
106       those for setreuid() and setregid()) employ a signal-based technique to
107       ensure  that  when  one  thread  changes  credentials, all of the other
108       threads in the process also change their credentials.  For details, see
109       nptl(7).
110

SEE ALSO

112       getgid(2),  getuid(2),  seteuid(2), setgid(2), setresuid(2), setuid(2),
113       capabilities(7), credentials(7), user_namespaces(7)
114

COLOPHON

116       This page is part of release 5.13 of the Linux  man-pages  project.   A
117       description  of  the project, information about reporting bugs, and the
118       latest    version    of    this    page,    can     be     found     at
119       https://www.kernel.org/doc/man-pages/.
120
121
122
123Linux                             2021-03-22                       SETREUID(2)
Impressum