1setreuid(2)                   System Calls Manual                  setreuid(2)
2
3
4

NAME

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

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

VERSIONS

77       POSIX.1 does not specify all of the UID changes that Linux permits  for
78       an  unprivileged process.  For setreuid(), the effective user ID can be
79       made the same as the real user ID or the saved set-user-ID, and  it  is
80       unspecified  whether unprivileged processes may set the real user ID to
81       the real user ID, the effective user ID, or the saved set-user-ID.  For
82       setregid(),  the real group ID can be changed to the value of the saved
83       set-group-ID, and the effective group ID can be changed to the value of
84       the  real  group  ID or the saved set-group-ID.  The precise details of
85       what ID changes are permitted vary across implementations.
86
87       POSIX.1 makes no specification about the effect of these calls  on  the
88       saved set-user-ID and saved set-group-ID.
89

STANDARDS

91       POSIX.1-2008.
92

HISTORY

94       POSIX.1-2001, 4.3BSD (first appeared in 4.2BSD).
95
96       Setting  the  effective user (group) ID to the saved set-user-ID (saved
97       set-group-ID) is possible since Linux 1.1.37 (1.1.38).
98
99       The original Linux setreuid() and  setregid()  system  calls  supported
100       only  16-bit  user  and  group  IDs.  Subsequently, Linux 2.4 added se‐
101       treuid32() and setregid32(), supporting  32-bit  IDs.   The  glibc  se‐
102       treuid()  and  setregid() wrapper functions transparently deal with the
103       variations across kernel versions.
104
105   C library/kernel differences
106       At the kernel level, user IDs and group IDs are a per-thread attribute.
107       However,  POSIX  requires  that all threads in a process share the same
108       credentials.  The NPTL threading implementation handles the  POSIX  re‐
109       quirements  by providing wrapper functions for the various system calls
110       that change process UIDs and GIDs.  These wrapper functions  (including
111       those for setreuid() and setregid()) employ a signal-based technique to
112       ensure that when one thread  changes  credentials,  all  of  the  other
113       threads in the process also change their credentials.  For details, see
114       nptl(7).
115

SEE ALSO

117       getgid(2), getuid(2), seteuid(2), setgid(2),  setresuid(2),  setuid(2),
118       capabilities(7), credentials(7), user_namespaces(7)
119
120
121
122Linux man-pages 6.05              2023-03-30                       setreuid(2)
Impressum