1SETREUID(2) Linux Programmer's Manual SETREUID(2)
2
3
4
6 setreuid, setregid - set real and/or effective user or group ID
7
9 #include <sys/types.h>
10 #include <unistd.h>
11
12 int setreuid(uid_t ruid, uid_t euid);
13 int setregid(gid_t rgid, gid_t egid);
14
15 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
16
17 setreuid(), setregid():
18 _XOPEN_SOURCE >= 500
19 || /* Since glibc 2.19: */ _DEFAULT_SOURCE
20 || /* Glibc versions <= 2.19: */ _BSD_SOURCE
21
23 setreuid() sets real and effective user IDs of the calling process.
24
25 Supplying a value of -1 for either the real or effective user ID forces
26 the system to leave that ID unchanged.
27
28 Unprivileged processes may only set the effective user ID to the real
29 user ID, the effective user ID, or the saved set-user-ID.
30
31 Unprivileged users may only set the real user ID to the real user ID or
32 the effective user ID.
33
34 If the real user ID is set (i.e., ruid is not -1) or the effective user
35 ID is set to a value not equal to the previous real user ID, the saved
36 set-user-ID will be set to the new effective user ID.
37
38 Completely analogously, setregid() sets real and effective group ID's
39 of the calling process, and all of the above holds with "group" instead
40 of "user".
41
43 On success, zero is returned. On error, -1 is returned, and errno is
44 set appropriately.
45
46 Note: there are cases where setreuid() can fail even when the caller is
47 UID 0; it is a grave security error to omit checking for a failure
48 return from setreuid().
49
51 EAGAIN The call would change the caller's real UID (i.e., ruid does not
52 match the caller's real UID), but there was a temporary failure
53 allocating the necessary kernel data structures.
54
55 EAGAIN ruid does not match the caller's real UID and this call would
56 bring the number of processes belonging to the real user ID ruid
57 over the caller's RLIMIT_NPROC resource limit. Since Linux 3.1,
58 this error case no longer occurs (but robust applications should
59 check for this error); see the description of EAGAIN in
60 execve(2).
61
62 EINVAL One or more of the target user or group IDs is not valid in this
63 user namespace.
64
65 EPERM The calling process is not privileged (on Linux, does not have
66 the necessary capability in its user namespace: CAP_SETUID in
67 the case of setreuid(), or CAP_SETGID in the case of setregid())
68 and a change other than (i) swapping the effective user (group)
69 ID with the real user (group) ID, or (ii) setting one to the
70 value of the other or (iii) setting the effective user (group)
71 ID to the value of the saved set-user-ID (saved set-group-ID)
72 was specified.
73
75 POSIX.1-2001, POSIX.1-2008, 4.3BSD (setreuid() and setregid() first
76 appeared in 4.2BSD).
77
79 Setting the effective user (group) ID to the saved set-user-ID (saved
80 set-group-ID) is possible since Linux 1.1.37 (1.1.38).
81
82 POSIX.1 does not specify all of the UID changes that Linux permits for
83 an unprivileged process. For setreuid(), the effective user ID can be
84 made the same as the real user ID or the saved set-user-ID, and it is
85 unspecified whether unprivileged processes may set the real user ID to
86 the real user ID, the effective user ID, or the saved set-user-ID. For
87 setregid(), the real group ID can be changed to the value of the saved
88 set-group-ID, and the effective group ID can be changed to the value of
89 the real group ID or the saved set-group-ID. The precise details of
90 what ID changes are permitted vary across implementations.
91
92 POSIX.1 makes no specification about the effect of these calls on the
93 saved set-user-ID and saved set-group-ID.
94
95 The original Linux setreuid() and setregid() system calls supported
96 only 16-bit user and group IDs. Subsequently, Linux 2.4 added
97 setreuid32() and setregid32(), supporting 32-bit IDs. The glibc
98 setreuid() and setregid() wrapper functions transparently deal with the
99 variations across kernel versions.
100
101 C library/kernel differences
102 At the kernel level, user IDs and group IDs are a per-thread attribute.
103 However, POSIX requires that all threads in a process share the same
104 credentials. The NPTL threading implementation handles the POSIX
105 requirements by providing wrapper functions for the various system
106 calls that change process UIDs and GIDs. These wrapper functions
107 (including those for setreuid() and setregid()) employ a signal-based
108 technique to ensure that when one thread changes credentials, all of
109 the other threads in the process also change their credentials. For
110 details, see nptl(7).
111
113 getgid(2), getuid(2), seteuid(2), setgid(2), setresuid(2), setuid(2),
114 capabilities(7), credentials(7), user_namespaces(7)
115
117 This page is part of release 4.15 of the Linux man-pages project. A
118 description of the project, information about reporting bugs, and the
119 latest version of this page, can be found at
120 https://www.kernel.org/doc/man-pages/.
121
122
123
124Linux 2017-09-15 SETREUID(2)