1/* $OpenBSD: uidswap.c,v 1.35 2006/08/03 03:34:42 deraadt 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 <sys/param.h>
18#include <errno.h>
19#include <pwd.h>
20#include <string.h>
21#include <unistd.h>
22#include <stdarg.h>
23
24#include <grp.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. */
44static uid_t 	saved_euid = 0;
45static gid_t	saved_egid = 0;
46#endif
47
48/* Saved effective uid. */
49static int	privileged = 0;
50static int	temporarily_use_uid_effective = 0;
51static gid_t	*saved_egroups = NULL, *user_groups = NULL;
52static int	saved_egroupslen = -1, user_groupslen = -1;
53
54/*
55 * Temporarily changes to the given uid.  If the effective user
56 * id is not root, this does nothing.  This call cannot be nested.
57 */
58void
59temporarily_use_uid(struct passwd *pw)
60{
61	/* Save the current euid, and egroups. */
62#ifdef SAVED_IDS_WORK_WITH_SETEUID
63	saved_euid = geteuid();
64	saved_egid = getegid();
65	debug("temporarily_use_uid: %u/%u (e=%u/%u)",
66	    (u_int)pw->pw_uid, (u_int)pw->pw_gid,
67	    (u_int)saved_euid, (u_int)saved_egid);
68#ifndef HAVE_CYGWIN
69	if (saved_euid != 0) {
70		privileged = 0;
71		return;
72	}
73#endif
74#else
75	if (geteuid() != 0) {
76		privileged = 0;
77		return;
78	}
79#endif /* SAVED_IDS_WORK_WITH_SETEUID */
80
81	privileged = 1;
82	temporarily_use_uid_effective = 1;
83
84	saved_egroupslen = getgroups(0, NULL);
85	if (saved_egroupslen < 0)
86		fatal("getgroups: %.100s", strerror(errno));
87	if (saved_egroupslen > 0) {
88		saved_egroups = xrealloc(saved_egroups,
89		    saved_egroupslen, sizeof(gid_t));
90		if (getgroups(saved_egroupslen, saved_egroups) < 0)
91			fatal("getgroups: %.100s", strerror(errno));
92	} else { /* saved_egroupslen == 0 */
93		if (saved_egroups != NULL)
94			xfree(saved_egroups);
95	}
96#if __APPLE__
97	if (initgroups(pw->pw_name, pw->pw_gid) < 0)
98		fatal("initgroups: %s: %.100s", pw->pw_name,
99		    strerror(errno));
100#else
101	/* set and save the user's groups */
102	if (user_groupslen == -1) {
103		if (initgroups(pw->pw_name, pw->pw_gid) < 0)
104			fatal("initgroups: %s: %.100s", pw->pw_name,
105			    strerror(errno));
106
107		user_groupslen = getgroups(0, NULL);
108		if (user_groupslen < 0)
109			fatal("getgroups: %.100s", strerror(errno));
110		if (user_groupslen > 0) {
111			user_groups = xrealloc(user_groups,
112			    user_groupslen, sizeof(gid_t));
113			if (getgroups(user_groupslen, user_groups) < 0)
114				fatal("getgroups: %.100s", strerror(errno));
115		} else { /* user_groupslen == 0 */
116			if (user_groups)
117				xfree(user_groups);
118		}
119	}
120	/* Set the effective uid to the given (unprivileged) uid. */
121	if (setgroups(user_groupslen, user_groups) < 0)
122		fatal("setgroups: %.100s", strerror(errno));
123#endif
124#ifndef SAVED_IDS_WORK_WITH_SETEUID
125	/* Propagate the privileged gid to all of our gids. */
126	if (setgid(getegid()) < 0)
127		debug("setgid %u: %.100s", (u_int) getegid(), strerror(errno));
128	/* Propagate the privileged uid to all of our uids. */
129	if (setuid(geteuid()) < 0)
130		debug("setuid %u: %.100s", (u_int) geteuid(), strerror(errno));
131#endif /* SAVED_IDS_WORK_WITH_SETEUID */
132	if (setegid(pw->pw_gid) < 0)
133		fatal("setegid %u: %.100s", (u_int)pw->pw_gid,
134		    strerror(errno));
135	if (seteuid(pw->pw_uid) == -1)
136		fatal("seteuid %u: %.100s", (u_int)pw->pw_uid,
137		    strerror(errno));
138}
139
140void
141permanently_drop_suid(uid_t uid)
142{
143	uid_t old_uid = getuid();
144
145	debug("permanently_drop_suid: %u", (u_int)uid);
146	if (setresuid(uid, uid, uid) < 0)
147		fatal("setresuid %u: %.100s", (u_int)uid, strerror(errno));
148
149#ifndef HAVE_CYGWIN
150	/* Try restoration of UID if changed (test clearing of saved uid) */
151	if (old_uid != uid &&
152	    (setuid(old_uid) != -1 || seteuid(old_uid) != -1))
153		fatal("%s: was able to restore old [e]uid", __func__);
154#endif
155
156	/* Verify UID drop was successful */
157	if (getuid() != uid || geteuid() != uid) {
158		fatal("%s: euid incorrect uid:%u euid:%u (should be %u)",
159		    __func__, (u_int)getuid(), (u_int)geteuid(), (u_int)uid);
160	}
161}
162
163/*
164 * Restores to the original (privileged) uid.
165 */
166void
167restore_uid(void)
168{
169	/* it's a no-op unless privileged */
170	if (!privileged) {
171		debug("restore_uid: (unprivileged)");
172		return;
173	}
174	if (!temporarily_use_uid_effective)
175		fatal("restore_uid: temporarily_use_uid not effective");
176
177#ifdef SAVED_IDS_WORK_WITH_SETEUID
178	debug("restore_uid: %u/%u", (u_int)saved_euid, (u_int)saved_egid);
179	/* Set the effective uid back to the saved privileged uid. */
180	if (seteuid(saved_euid) < 0)
181		fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror(errno));
182	if (setegid(saved_egid) < 0)
183		fatal("setegid %u: %.100s", (u_int)saved_egid, strerror(errno));
184#else /* SAVED_IDS_WORK_WITH_SETEUID */
185	/*
186	 * We are unable to restore the real uid to its unprivileged value.
187	 * Propagate the real uid (usually more privileged) to effective uid
188	 * as well.
189	 */
190	setuid(getuid());
191	setgid(getgid());
192#endif /* SAVED_IDS_WORK_WITH_SETEUID */
193
194	if (setgroups(saved_egroupslen, saved_egroups) < 0)
195		fatal("setgroups: %.100s", strerror(errno));
196	temporarily_use_uid_effective = 0;
197}
198
199/*
200 * Permanently sets all uids to the given uid.  This cannot be
201 * called while temporarily_use_uid is effective.
202 */
203void
204permanently_set_uid(struct passwd *pw)
205{
206	uid_t old_uid = getuid();
207	gid_t old_gid = getgid();
208
209	if (pw == NULL)
210		fatal("permanently_set_uid: no user given");
211	if (temporarily_use_uid_effective)
212		fatal("permanently_set_uid: temporarily_use_uid effective");
213	debug("permanently_set_uid: %u/%u", (u_int)pw->pw_uid,
214	    (u_int)pw->pw_gid);
215
216	if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) < 0)
217		fatal("setresgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
218
219#ifdef __APPLE__
220	/*
221	 * OS X requires initgroups after setgid to opt back into
222	 * memberd support for >16 supplemental groups.
223	 *
224	 * initgroups is not needed for the network child because it has
225	 * reached a steady-state and will not need to allocate any new
226	 * resources. Allows for a cleaner sandbox profile.
227	 *
228	 * initgroups is needed for all other children so that anything spawned
229	 * or used by this child (i.e. the user shell) will opt into membership
230	 * services.
231	 */
232	if ( 0 != strcmp(pw->pw_name, SSH_PRIVSEP_USER) &&
233		initgroups(pw->pw_name, pw->pw_gid) < 0)
234			fatal("initgroups %.100s %u: %.100s",
235				pw->pw_name, (u_int)pw->pw_gid, strerror(errno));
236#endif
237
238	if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) < 0)
239		fatal("setresuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
240
241#ifndef HAVE_CYGWIN
242	/* Try restoration of GID if changed (test clearing of saved gid) */
243	if (old_gid != pw->pw_gid && pw->pw_uid != 0 &&
244	    (setgid(old_gid) != -1 || setegid(old_gid) != -1))
245		fatal("%s: was able to restore old [e]gid", __func__);
246#endif
247
248	/* Verify GID drop was successful */
249	if (getgid() != pw->pw_gid || getegid() != pw->pw_gid) {
250		fatal("%s: egid incorrect gid:%u egid:%u (should be %u)",
251		    __func__, (u_int)getgid(), (u_int)getegid(),
252		    (u_int)pw->pw_gid);
253	}
254
255#ifndef HAVE_CYGWIN
256	/* Try restoration of UID if changed (test clearing of saved uid) */
257	if (old_uid != pw->pw_uid &&
258	    (setuid(old_uid) != -1 || seteuid(old_uid) != -1))
259		fatal("%s: was able to restore old [e]uid", __func__);
260#endif
261
262	/* Verify UID drop was successful */
263	if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) {
264		fatal("%s: euid incorrect uid:%u euid:%u (should be %u)",
265		    __func__, (u_int)getuid(), (u_int)geteuid(),
266		    (u_int)pw->pw_uid);
267	}
268}
269