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