1 /* $OpenBSD: uidswap.c,v 1.43 2026/02/11 17:05:32 dtucker Exp $ */
2 /*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
6 * Code for uid-swapping.
7 *
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 */
14
15 #include "includes.h"
16
17 #include <errno.h>
18 #include <grp.h>
19 #include <pwd.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <limits.h>
23 #include <stdarg.h>
24 #include <stdlib.h>
25
26 #include "log.h"
27 #include "uidswap.h"
28 #include "xmalloc.h"
29
30 /*
31 * Note: all these functions must work in all of the following cases:
32 * 1. euid=0, ruid=0
33 * 2. euid=0, ruid!=0
34 * 3. euid!=0, ruid!=0
35 * Additionally, they must work regardless of whether the system has
36 * POSIX saved uids or not.
37 */
38
39 #if defined(_POSIX_SAVED_IDS) && !defined(BROKEN_SAVED_UIDS)
40 /* Lets assume that posix saved ids also work with seteuid, even though that
41 is not part of the posix specification. */
42 #define SAVED_IDS_WORK_WITH_SETEUID
43 /* Saved effective uid. */
44 static uid_t saved_euid = 0;
45 static gid_t saved_egid = 0;
46 #endif
47
48 /* Saved effective uid. */
49 static int privileged = 0;
50 static int temporarily_use_uid_effective = 0;
51 static uid_t user_groups_uid;
52 static gid_t *saved_egroups = NULL, *user_groups = NULL;
53 static int saved_egroupslen = -1, user_groupslen = -1;
54
55 /*
56 * Temporarily changes to the given uid. If the effective user
57 * id is not root, this does nothing. This call cannot be nested.
58 */
59 void
temporarily_use_uid(struct passwd * pw)60 temporarily_use_uid(struct passwd *pw)
61 {
62 /* Save the current euid, and egroups. */
63 #ifdef SAVED_IDS_WORK_WITH_SETEUID
64 saved_euid = geteuid();
65 saved_egid = getegid();
66 debug("temporarily_use_uid: %u/%u (e=%u/%u)",
67 (u_int)pw->pw_uid, (u_int)pw->pw_gid,
68 (u_int)saved_euid, (u_int)saved_egid);
69 #ifndef HAVE_CYGWIN
70 if (saved_euid != 0) {
71 privileged = 0;
72 return;
73 }
74 #endif
75 #else
76 if (geteuid() != 0) {
77 privileged = 0;
78 return;
79 }
80 #endif /* SAVED_IDS_WORK_WITH_SETEUID */
81
82 privileged = 1;
83 temporarily_use_uid_effective = 1;
84
85 saved_egroupslen = getgroups(0, NULL);
86 if (saved_egroupslen == -1)
87 fatal("getgroups: %.100s", strerror(errno));
88 if (saved_egroupslen > 0) {
89 saved_egroups = xreallocarray(saved_egroups,
90 saved_egroupslen, sizeof(gid_t));
91 if (getgroups(saved_egroupslen, saved_egroups) == -1)
92 fatal("getgroups: %.100s", strerror(errno));
93 } else { /* saved_egroupslen == 0 */
94 free(saved_egroups);
95 saved_egroups = NULL;
96 }
97
98 /* set and save the user's groups */
99 if (user_groupslen == -1 || user_groups_uid != pw->pw_uid) {
100 if (initgroups(pw->pw_name, pw->pw_gid) == -1)
101 fatal("initgroups: %s: %.100s", pw->pw_name,
102 strerror(errno));
103
104 user_groupslen = getgroups(0, NULL);
105 if (user_groupslen == -1)
106 fatal("getgroups: %.100s", strerror(errno));
107 if (user_groupslen > 0) {
108 user_groups = xreallocarray(user_groups,
109 user_groupslen, sizeof(gid_t));
110 if (getgroups(user_groupslen, user_groups) == -1)
111 fatal("getgroups: %.100s", strerror(errno));
112 } else { /* user_groupslen == 0 */
113 free(user_groups);
114 user_groups = NULL;
115 }
116 user_groups_uid = pw->pw_uid;
117 }
118 /* Set the effective uid to the given (unprivileged) uid. */
119 if (setgroups(user_groupslen, user_groups) == -1)
120 fatal("setgroups: %.100s", strerror(errno));
121 #ifndef SAVED_IDS_WORK_WITH_SETEUID
122 /* Propagate the privileged gid to all of our gids. */
123 if (setgid(getegid()) == -1)
124 debug("setgid %u: %.100s", (u_int) getegid(), strerror(errno));
125 /* Propagate the privileged uid to all of our uids. */
126 if (setuid(geteuid()) == -1)
127 debug("setuid %u: %.100s", (u_int) geteuid(), strerror(errno));
128 #endif /* SAVED_IDS_WORK_WITH_SETEUID */
129 if (setegid(pw->pw_gid) == -1)
130 fatal("setegid %u: %.100s", (u_int)pw->pw_gid,
131 strerror(errno));
132 if (seteuid(pw->pw_uid) == -1)
133 fatal("seteuid %u: %.100s", (u_int)pw->pw_uid,
134 strerror(errno));
135 }
136
137 /*
138 * Restores to the original (privileged) uid.
139 */
140 void
restore_uid(void)141 restore_uid(void)
142 {
143 /* it's a no-op unless privileged */
144 if (!privileged) {
145 debug("restore_uid: (unprivileged)");
146 return;
147 }
148 if (!temporarily_use_uid_effective)
149 fatal("restore_uid: temporarily_use_uid not effective");
150
151 #ifdef SAVED_IDS_WORK_WITH_SETEUID
152 debug("restore_uid: %u/%u", (u_int)saved_euid, (u_int)saved_egid);
153 /* Set the effective uid back to the saved privileged uid. */
154 if (seteuid(saved_euid) == -1)
155 fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror(errno));
156 if (setegid(saved_egid) == -1)
157 fatal("setegid %u: %.100s", (u_int)saved_egid, strerror(errno));
158 #else /* SAVED_IDS_WORK_WITH_SETEUID */
159 /*
160 * We are unable to restore the real uid to its unprivileged value.
161 * Propagate the real uid (usually more privileged) to effective uid
162 * as well.
163 */
164 if (setuid(getuid()) == -1)
165 fatal_f("setuid failed: %s", strerror(errno));
166 if (setgid(getgid()) == -1)
167 fatal_f("setgid failed: %s", strerror(errno));
168 #endif /* SAVED_IDS_WORK_WITH_SETEUID */
169
170 if (setgroups(saved_egroupslen, saved_egroups) == -1)
171 fatal("setgroups: %.100s", strerror(errno));
172 temporarily_use_uid_effective = 0;
173 }
174
175 /*
176 * Permanently sets all uids to the given uid. This cannot be
177 * called while temporarily_use_uid is effective.
178 */
179 void
permanently_set_uid(struct passwd * pw)180 permanently_set_uid(struct passwd *pw)
181 {
182 #ifndef NO_UID_RESTORATION_TEST
183 uid_t old_uid = getuid();
184 gid_t old_gid = getgid();
185 #endif
186
187 if (pw == NULL)
188 fatal("permanently_set_uid: no user given");
189 if (temporarily_use_uid_effective)
190 fatal("permanently_set_uid: temporarily_use_uid effective");
191 debug("permanently_set_uid: %u/%u", (u_int)pw->pw_uid,
192 (u_int)pw->pw_gid);
193
194 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1)
195 fatal("setresgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
196
197 #ifdef __APPLE__
198 /*
199 * OS X requires initgroups after setgid to opt back into
200 * memberd support for >16 supplemental groups.
201 */
202 if (initgroups(pw->pw_name, pw->pw_gid) == -1)
203 fatal("initgroups %.100s %u: %.100s",
204 pw->pw_name, (u_int)pw->pw_gid, strerror(errno));
205 #endif
206
207 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
208 fatal("setresuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
209
210 #ifndef NO_UID_RESTORATION_TEST
211 /* Try restoration of GID if changed (test clearing of saved gid) */
212 if (old_gid != pw->pw_gid && pw->pw_uid != 0 &&
213 (setgid(old_gid) != -1 || setegid(old_gid) != -1))
214 fatal_f("was able to restore old [e]gid");
215 #endif
216
217 /* Verify GID drop was successful */
218 if (getgid() != pw->pw_gid || getegid() != pw->pw_gid) {
219 fatal("%s: egid incorrect gid:%u egid:%u (should be %u)",
220 __func__, (u_int)getgid(), (u_int)getegid(),
221 (u_int)pw->pw_gid);
222 }
223
224 #ifndef NO_UID_RESTORATION_TEST
225 /* Try restoration of UID if changed (test clearing of saved uid) */
226 if (old_uid != pw->pw_uid &&
227 (setuid(old_uid) != -1 || seteuid(old_uid) != -1))
228 fatal_f("was able to restore old [e]uid");
229 #endif
230
231 /* Verify UID drop was successful */
232 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) {
233 fatal("%s: euid incorrect uid:%u euid:%u (should be %u)",
234 __func__, (u_int)getuid(), (u_int)geteuid(),
235 (u_int)pw->pw_uid);
236 }
237 }
238