xref: /titanic_44/usr/src/cmd/ssh/libssh/common/uidswap.c (revision aa8cf21aa2aaa2df3db469354ccc0c47f8cdaab9)
1 /*
2  * Author: Tatu Ylonen <ylo@cs.hut.fi>
3  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4  *                    All rights reserved
5  * Code for uid-swapping.
6  *
7  * As far as I am concerned, the code I have written for this software
8  * can be used freely for any purpose.  Any derived versions of this
9  * software must be clearly marked as such, and if the derived work is
10  * incompatible with the protocol description in the RFC file, it must be
11  * called by a name other than "ssh" or "Secure Shell".
12  */
13 /*
14  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
15  * Use is subject to license terms.
16  */
17 
18 #include "includes.h"
19 RCSID("$OpenBSD: uidswap.c,v 1.23 2002/07/15 17:15:31 stevesk Exp $");
20 
21 #include <priv.h>
22 
23 #include "log.h"
24 #include "uidswap.h"
25 #include "servconf.h"
26 
27 /*
28  * Note: all these functions must work in all of the following cases:
29  *    1. euid=0, ruid=0
30  *    2. euid=0, ruid!=0
31  *    3. euid!=0, ruid!=0
32  * Additionally, they must work regardless of whether the system has
33  * POSIX saved uids or not.
34  */
35 
36 #if defined(_POSIX_SAVED_IDS) && !defined(BROKEN_SAVED_UIDS)
37 /* Lets assume that posix saved ids also work with seteuid, even though that
38    is not part of the posix specification. */
39 #define SAVED_IDS_WORK
40 /* Saved effective uid. */
41 static uid_t 	saved_euid = 0;
42 static gid_t	saved_egid = 0;
43 #endif
44 
45 /* Saved effective uid. */
46 static int	privileged = 0;
47 static int	temporarily_use_uid_effective = 0;
48 static gid_t	saved_egroups[NGROUPS_UMAX], user_groups[NGROUPS_UMAX];
49 static int	saved_egroupslen = -1, user_groupslen = -1;
50 
51 /*
52  * Temporarily changes to the given uid.  If the effective user
53  * id is not root, this does nothing.  This call cannot be nested.
54  */
55 void
56 temporarily_use_uid(struct passwd *pw)
57 {
58 	/* Save the current euid, and egroups. */
59 #ifdef SAVED_IDS_WORK
60 	saved_euid = geteuid();
61 	saved_egid = getegid();
62 	debug("temporarily_use_uid: %u/%u (e=%u/%u)",
63 	    (u_int)pw->pw_uid, (u_int)pw->pw_gid,
64 	    (u_int)saved_euid, (u_int)saved_egid);
65 	if (saved_euid != 0) {
66 		privileged = 0;
67 		return;
68 	}
69 #else
70 	if (geteuid() != 0) {
71 		privileged = 0;
72 		return;
73 	}
74 #endif /* SAVED_IDS_WORK */
75 
76 	privileged = 1;
77 	temporarily_use_uid_effective = 1;
78 	saved_egroupslen = getgroups(NGROUPS_UMAX, saved_egroups);
79 	if (saved_egroupslen < 0)
80 		fatal("getgroups: %.100s", strerror(errno));
81 
82 	/* set and save the user's groups */
83 	if (user_groupslen == -1) {
84 		if (initgroups(pw->pw_name, pw->pw_gid) < 0)
85 			fatal("initgroups: %s: %.100s", pw->pw_name,
86 			    strerror(errno));
87 		user_groupslen = getgroups(NGROUPS_UMAX, user_groups);
88 		if (user_groupslen < 0)
89 			fatal("getgroups: %.100s", strerror(errno));
90 	}
91 	/* Set the effective uid to the given (unprivileged) uid. */
92 	if (setgroups(user_groupslen, user_groups) < 0)
93 		fatal("setgroups: %.100s", strerror(errno));
94 #ifdef SAVED_IDS_WORK
95 	/* Set saved gid and set real gid */
96 	if (setregid(pw->pw_gid, -1) == -1)
97 		debug("setregid(%u, -1): %.100s", (uint_t)pw->pw_gid, strerror(errno));
98 	/* Set saved uid and set real uid */
99 	if (setreuid(pw->pw_uid, -1) == -1)
100 		debug("setreuid(%u, -1): %.100s", (uint_t)pw->pw_uid, strerror(errno));
101 #else
102 	/* Propagate the privileged gid to all of our gids. */
103 	if (setgid(getegid()) < 0)
104 		debug("setgid %u: %.100s", (u_int) getegid(), strerror(errno));
105 	/* Propagate the privileged uid to all of our uids. */
106 	if (setuid(geteuid()) < 0)
107 		debug("setuid %u: %.100s", (u_int) geteuid(), strerror(errno));
108 #endif /* SAVED_IDS_WORK */
109 	/* Set effective gid */
110 	if (setegid(pw->pw_gid) == -1)
111 		fatal("setegid %u: %.100s", (u_int)pw->pw_uid,
112 		    strerror(errno));
113 	/* Set effective uid */
114 	if (seteuid(pw->pw_uid) == -1)
115 		fatal("seteuid %u: %.100s", (u_int)pw->pw_uid,
116 		    strerror(errno));
117 	/*
118 	 * If saved set ids work then
119 	 *
120 	 *	ruid == euid == pw->pw_uid
121 	 *	saved uid = previous euid
122 	 *	rgid == egid == pw->pw_gid
123 	 *	saved gid = previous egid
124 	 */
125 }
126 
127 /*
128  * Restores to the original (privileged) uid.
129  */
130 void
131 restore_uid(void)
132 {
133 	/* it's a no-op unless privileged */
134 	if (!privileged) {
135 		debug("restore_uid: (unprivileged)");
136 		return;
137 	}
138 	if (!temporarily_use_uid_effective)
139 		fatal("restore_uid: temporarily_use_uid not effective");
140 
141 #ifdef SAVED_IDS_WORK
142 	debug("restore_uid: %u/%u", (u_int)saved_euid, (u_int)saved_egid);
143 	/* Set the effective uid back to the saved privileged uid. */
144 	if (seteuid(saved_euid) < 0)
145 		fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror(errno));
146 	if (setuid(saved_euid) < 0)
147 		fatal("setuid %u: %.100s", (u_int)saved_euid, strerror(errno));
148 	if (setegid(saved_egid) < 0)
149 		fatal("setegid %u: %.100s", (u_int)saved_egid, strerror(errno));
150 	if (setgid(saved_egid) < 0)
151 		fatal("setgid %u: %.100s", (u_int)saved_egid, strerror(errno));
152 #else /* SAVED_IDS_WORK */
153 	/*
154 	 * We are unable to restore the real uid to its unprivileged value.
155 	 * Propagate the real uid (usually more privileged) to effective uid
156 	 * as well.
157 	 */
158 	setuid(getuid());
159 	setgid(getgid());
160 #endif /* SAVED_IDS_WORK */
161 
162 	if (setgroups(saved_egroupslen, saved_egroups) < 0)
163 		fatal("setgroups: %.100s", strerror(errno));
164 	temporarily_use_uid_effective = 0;
165 }
166 
167 /*
168  * Permanently sets all uids to the given uid. This cannot be called while
169  * temporarily_use_uid is effective. Note that when the ChrootDirectory option
170  * is in use we keep a few privileges so that we can call chroot(2) later while
171  * already running under UIDs of a connecting user.
172  */
173 void
174 permanently_set_uid(struct passwd *pw, char *chroot_directory)
175 {
176 	priv_set_t *pset;
177 
178 	if (temporarily_use_uid_effective)
179 		fatal("%s: temporarily_use_uid effective", __func__);
180 
181 	debug("%s: %u/%u", __func__, (u_int)pw->pw_uid, (u_int)pw->pw_gid);
182 
183 	if (initgroups(pw->pw_name, pw->pw_gid) < 0)
184 		fatal("initgroups: %s: %.100s", pw->pw_name,
185 		    strerror(errno));
186 
187 	if (setgid(pw->pw_gid) < 0)
188 		fatal("setgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
189 
190 	/*
191 	 * If root is connecting we are done now. Note that we must have called
192 	 * setgid() in case that the SSH server was run under a group other than
193 	 * root.
194 	 */
195 	if (pw->pw_uid == 0)
196 		return;
197 
198 	/*
199 	 * This means we will keep all privileges after the UID change.
200 	 */
201 	if (setpflags(PRIV_AWARE, 1) != 0)
202 		fatal("setpflags: %s", strerror(errno));
203 
204 	/* Now we are running under UID of the user. */
205 	if (setuid(pw->pw_uid) < 0)
206 		fatal("setuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
207 
208 	/*
209 	 * We will run with the privileges from the Inheritable set as
210 	 * we would have after exec(2) if we had stayed in NPA mode
211 	 * before setuid(2) call (see privileges(5), user_attr(4), and
212 	 * pam_unix_cred(5)). We want to run with P = E = I, with I as
213 	 * set by pam_unix_cred(5). We also add PRIV_PROC_CHROOT,
214 	 * obviously, and then PRIV_PROC_FORK and PRIV_PROC_EXEC, since
215 	 * those two might have been removed from the I set. Note that
216 	 * we are expected to finish the login process without them in
217 	 * the I set, the important thing is that those not be passed on
218 	 * to a shell or a subsystem later if they were not set in
219 	 * pam_unix_cred(5).
220 	 */
221 	if ((pset = priv_allocset()) == NULL)
222 		fatal("priv_allocset: %s", strerror(errno));
223 	if (getppriv(PRIV_INHERITABLE, pset) != 0)
224 		fatal("getppriv: %s", strerror(errno));
225 
226 	/* We do not need PRIV_PROC_CHROOT unless chroot()ing. */
227 	if (chroot_requested(chroot_directory) &&
228 	    priv_addset(pset, PRIV_PROC_CHROOT) == -1) {
229 		fatal("%s: priv_addset failed", __func__);
230 	}
231 
232 	if (priv_addset(pset, PRIV_PROC_FORK) == -1 ||
233 	    priv_addset(pset, PRIV_PROC_EXEC) == -1) {
234 		fatal("%s: priv_addset failed", __func__);
235 	}
236 
237 	/* Set only P; this will also set E. */
238 	if (setppriv(PRIV_SET, PRIV_PERMITTED, pset) == -1)
239 		fatal("setppriv: %s", strerror(errno));
240 
241 	/* We don't need the PA flag anymore. */
242 	if (setpflags(PRIV_AWARE, 0) == -1)
243 		fatal("setpflags: %s", strerror(errno));
244 
245 	priv_freeset(pset);
246 }
247