- jmc@cvs.openbsd.org 2006/07/18 07:50:40
[openssh-git.git] / uidswap.c
blob2fe5feaed409892793393f79466d1376778d6057
1 /* $OpenBSD: uidswap.c,v 1.32 2006/07/17 01:31:10 stevesk 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.
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".
15 #include "includes.h"
17 #include <sys/types.h>
19 #include <errno.h>
20 #include <pwd.h>
21 #include <unistd.h>
23 #include <grp.h>
25 #include "log.h"
26 #include "uidswap.h"
27 #include "xmalloc.h"
30 * Note: all these functions must work in all of the following cases:
31 * 1. euid=0, ruid=0
32 * 2. euid=0, ruid!=0
33 * 3. euid!=0, ruid!=0
34 * Additionally, they must work regardless of whether the system has
35 * POSIX saved uids or not.
38 #if defined(_POSIX_SAVED_IDS) && !defined(BROKEN_SAVED_UIDS)
39 /* Lets assume that posix saved ids also work with seteuid, even though that
40 is not part of the posix specification. */
41 #define SAVED_IDS_WORK_WITH_SETEUID
42 /* Saved effective uid. */
43 static uid_t saved_euid = 0;
44 static gid_t saved_egid = 0;
45 #endif
47 /* Saved effective uid. */
48 static int privileged = 0;
49 static int temporarily_use_uid_effective = 0;
50 static gid_t *saved_egroups = NULL, *user_groups = NULL;
51 static int saved_egroupslen = -1, user_groupslen = -1;
54 * Temporarily changes to the given uid. If the effective user
55 * id is not root, this does nothing. This call cannot be nested.
57 void
58 temporarily_use_uid(struct passwd *pw)
60 /* Save the current euid, and egroups. */
61 #ifdef SAVED_IDS_WORK_WITH_SETEUID
62 saved_euid = geteuid();
63 saved_egid = getegid();
64 debug("temporarily_use_uid: %u/%u (e=%u/%u)",
65 (u_int)pw->pw_uid, (u_int)pw->pw_gid,
66 (u_int)saved_euid, (u_int)saved_egid);
67 #ifndef HAVE_CYGWIN
68 if (saved_euid != 0) {
69 privileged = 0;
70 return;
72 #endif
73 #else
74 if (geteuid() != 0) {
75 privileged = 0;
76 return;
78 #endif /* SAVED_IDS_WORK_WITH_SETEUID */
80 privileged = 1;
81 temporarily_use_uid_effective = 1;
83 saved_egroupslen = getgroups(0, NULL);
84 if (saved_egroupslen < 0)
85 fatal("getgroups: %.100s", strerror(errno));
86 if (saved_egroupslen > 0) {
87 saved_egroups = xrealloc(saved_egroups,
88 saved_egroupslen, sizeof(gid_t));
89 if (getgroups(saved_egroupslen, saved_egroups) < 0)
90 fatal("getgroups: %.100s", strerror(errno));
91 } else { /* saved_egroupslen == 0 */
92 if (saved_egroups != NULL)
93 xfree(saved_egroups);
96 /* set and save the user's groups */
97 if (user_groupslen == -1) {
98 if (initgroups(pw->pw_name, pw->pw_gid) < 0)
99 fatal("initgroups: %s: %.100s", pw->pw_name,
100 strerror(errno));
102 user_groupslen = getgroups(0, NULL);
103 if (user_groupslen < 0)
104 fatal("getgroups: %.100s", strerror(errno));
105 if (user_groupslen > 0) {
106 user_groups = xrealloc(user_groups,
107 user_groupslen, sizeof(gid_t));
108 if (getgroups(user_groupslen, user_groups) < 0)
109 fatal("getgroups: %.100s", strerror(errno));
110 } else { /* user_groupslen == 0 */
111 if (user_groups)
112 xfree(user_groups);
115 /* Set the effective uid to the given (unprivileged) uid. */
116 if (setgroups(user_groupslen, user_groups) < 0)
117 fatal("setgroups: %.100s", strerror(errno));
118 #ifndef SAVED_IDS_WORK_WITH_SETEUID
119 /* Propagate the privileged gid to all of our gids. */
120 if (setgid(getegid()) < 0)
121 debug("setgid %u: %.100s", (u_int) getegid(), strerror(errno));
122 /* Propagate the privileged uid to all of our uids. */
123 if (setuid(geteuid()) < 0)
124 debug("setuid %u: %.100s", (u_int) geteuid(), strerror(errno));
125 #endif /* SAVED_IDS_WORK_WITH_SETEUID */
126 if (setegid(pw->pw_gid) < 0)
127 fatal("setegid %u: %.100s", (u_int)pw->pw_gid,
128 strerror(errno));
129 if (seteuid(pw->pw_uid) == -1)
130 fatal("seteuid %u: %.100s", (u_int)pw->pw_uid,
131 strerror(errno));
134 void
135 permanently_drop_suid(uid_t uid)
137 uid_t old_uid = getuid();
139 debug("permanently_drop_suid: %u", (u_int)uid);
140 #if defined(HAVE_SETRESUID) && !defined(BROKEN_SETRESUID)
141 if (setresuid(uid, uid, uid) < 0)
142 fatal("setresuid %u: %.100s", (u_int)uid, strerror(errno));
143 #elif defined(HAVE_SETREUID) && !defined(BROKEN_SETREUID)
144 if (setreuid(uid, uid) < 0)
145 fatal("setreuid %u: %.100s", (u_int)uid, strerror(errno));
146 #else
147 # ifndef SETEUID_BREAKS_SETUID
148 if (seteuid(uid) < 0)
149 fatal("seteuid %u: %.100s", (u_int)uid, strerror(errno));
150 # endif
151 if (setuid(uid) < 0)
152 fatal("setuid %u: %.100s", (u_int)uid, strerror(errno));
153 #endif
155 #ifndef HAVE_CYGWIN
156 /* Try restoration of UID if changed (test clearing of saved uid) */
157 if (old_uid != uid &&
158 (setuid(old_uid) != -1 || seteuid(old_uid) != -1))
159 fatal("%s: was able to restore old [e]uid", __func__);
160 #endif
162 /* Verify UID drop was successful */
163 if (getuid() != uid || geteuid() != uid) {
164 fatal("%s: euid incorrect uid:%u euid:%u (should be %u)",
165 __func__, (u_int)getuid(), (u_int)geteuid(), (u_int)uid);
170 * Restores to the original (privileged) uid.
172 void
173 restore_uid(void)
175 /* it's a no-op unless privileged */
176 if (!privileged) {
177 debug("restore_uid: (unprivileged)");
178 return;
180 if (!temporarily_use_uid_effective)
181 fatal("restore_uid: temporarily_use_uid not effective");
183 #ifdef SAVED_IDS_WORK_WITH_SETEUID
184 debug("restore_uid: %u/%u", (u_int)saved_euid, (u_int)saved_egid);
185 /* Set the effective uid back to the saved privileged uid. */
186 if (seteuid(saved_euid) < 0)
187 fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror(errno));
188 if (setegid(saved_egid) < 0)
189 fatal("setegid %u: %.100s", (u_int)saved_egid, strerror(errno));
190 #else /* SAVED_IDS_WORK_WITH_SETEUID */
192 * We are unable to restore the real uid to its unprivileged value.
193 * Propagate the real uid (usually more privileged) to effective uid
194 * as well.
196 setuid(getuid());
197 setgid(getgid());
198 #endif /* SAVED_IDS_WORK_WITH_SETEUID */
200 if (setgroups(saved_egroupslen, saved_egroups) < 0)
201 fatal("setgroups: %.100s", strerror(errno));
202 temporarily_use_uid_effective = 0;
206 * Permanently sets all uids to the given uid. This cannot be
207 * called while temporarily_use_uid is effective.
209 void
210 permanently_set_uid(struct passwd *pw)
212 uid_t old_uid = getuid();
213 gid_t old_gid = getgid();
215 if (pw == NULL)
216 fatal("permanently_set_uid: no user given");
217 if (temporarily_use_uid_effective)
218 fatal("permanently_set_uid: temporarily_use_uid effective");
219 debug("permanently_set_uid: %u/%u", (u_int)pw->pw_uid,
220 (u_int)pw->pw_gid);
222 #if defined(HAVE_SETRESGID) && !defined(BROKEN_SETRESGID)
223 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) < 0)
224 fatal("setresgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
225 #elif defined(HAVE_SETREGID) && !defined(BROKEN_SETREGID)
226 if (setregid(pw->pw_gid, pw->pw_gid) < 0)
227 fatal("setregid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
228 #else
229 if (setegid(pw->pw_gid) < 0)
230 fatal("setegid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
231 if (setgid(pw->pw_gid) < 0)
232 fatal("setgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
233 #endif
235 #if defined(HAVE_SETRESUID) && !defined(BROKEN_SETRESUID)
236 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) < 0)
237 fatal("setresuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
238 #elif defined(HAVE_SETREUID) && !defined(BROKEN_SETREUID)
239 if (setreuid(pw->pw_uid, pw->pw_uid) < 0)
240 fatal("setreuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
241 #else
242 # ifndef SETEUID_BREAKS_SETUID
243 if (seteuid(pw->pw_uid) < 0)
244 fatal("seteuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
245 # endif
246 if (setuid(pw->pw_uid) < 0)
247 fatal("setuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
248 #endif
250 #ifndef HAVE_CYGWIN
251 /* Try restoration of GID if changed (test clearing of saved gid) */
252 if (old_gid != pw->pw_gid && pw->pw_uid != 0 &&
253 (setgid(old_gid) != -1 || setegid(old_gid) != -1))
254 fatal("%s: was able to restore old [e]gid", __func__);
255 #endif
257 /* Verify GID drop was successful */
258 if (getgid() != pw->pw_gid || getegid() != pw->pw_gid) {
259 fatal("%s: egid incorrect gid:%u egid:%u (should be %u)",
260 __func__, (u_int)getgid(), (u_int)getegid(),
261 (u_int)pw->pw_gid);
264 #ifndef HAVE_CYGWIN
265 /* Try restoration of UID if changed (test clearing of saved uid) */
266 if (old_uid != pw->pw_uid &&
267 (setuid(old_uid) != -1 || seteuid(old_uid) != -1))
268 fatal("%s: was able to restore old [e]uid", __func__);
269 #endif
271 /* Verify UID drop was successful */
272 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) {
273 fatal("%s: euid incorrect uid:%u euid:%u (should be %u)",
274 __func__, (u_int)getuid(), (u_int)geteuid(),
275 (u_int)pw->pw_uid);