]> andersk Git - openssh.git/blob - uidswap.c
2ac5d7f91d34ff21e1894dbd16bc1fec8d8a2b97
[openssh.git] / uidswap.c
1 /* $OpenBSD: uidswap.c,v 1.30 2006/07/06 16:03:53 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.
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/types.h>
18
19 #include <pwd.h>
20
21 #include <grp.h>
22
23 #include "log.h"
24 #include "uidswap.h"
25 #include "xmalloc.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_WITH_SETEUID
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 = NULL, *user_groups = NULL;
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_WITH_SETEUID
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 #ifndef HAVE_CYGWIN
66         if (saved_euid != 0) {
67                 privileged = 0;
68                 return;
69         }
70 #endif
71 #else
72         if (geteuid() != 0) {
73                 privileged = 0;
74                 return;
75         }
76 #endif /* SAVED_IDS_WORK_WITH_SETEUID */
77
78         privileged = 1;
79         temporarily_use_uid_effective = 1;
80
81         saved_egroupslen = getgroups(0, NULL);
82         if (saved_egroupslen < 0)
83                 fatal("getgroups: %.100s", strerror(errno));
84         if (saved_egroupslen > 0) {
85                 saved_egroups = xrealloc(saved_egroups,
86                     saved_egroupslen, sizeof(gid_t));
87                 if (getgroups(saved_egroupslen, saved_egroups) < 0)
88                         fatal("getgroups: %.100s", strerror(errno));
89         } else { /* saved_egroupslen == 0 */
90                 if (saved_egroups != NULL)
91                         xfree(saved_egroups);
92         }
93
94         /* set and save the user's groups */
95         if (user_groupslen == -1) {
96                 if (initgroups(pw->pw_name, pw->pw_gid) < 0)
97                         fatal("initgroups: %s: %.100s", pw->pw_name,
98                             strerror(errno));
99
100                 user_groupslen = getgroups(0, NULL);
101                 if (user_groupslen < 0)
102                         fatal("getgroups: %.100s", strerror(errno));
103                 if (user_groupslen > 0) {
104                         user_groups = xrealloc(user_groups,
105                             user_groupslen, sizeof(gid_t));
106                         if (getgroups(user_groupslen, user_groups) < 0)
107                                 fatal("getgroups: %.100s", strerror(errno));
108                 } else { /* user_groupslen == 0 */
109                         if (user_groups)
110                                 xfree(user_groups);
111                 }
112         }
113         /* Set the effective uid to the given (unprivileged) uid. */
114         if (setgroups(user_groupslen, user_groups) < 0)
115                 fatal("setgroups: %.100s", strerror(errno));
116 #ifndef SAVED_IDS_WORK_WITH_SETEUID
117         /* Propagate the privileged gid to all of our gids. */
118         if (setgid(getegid()) < 0)
119                 debug("setgid %u: %.100s", (u_int) getegid(), strerror(errno));
120         /* Propagate the privileged uid to all of our uids. */
121         if (setuid(geteuid()) < 0)
122                 debug("setuid %u: %.100s", (u_int) geteuid(), strerror(errno));
123 #endif /* SAVED_IDS_WORK_WITH_SETEUID */
124         if (setegid(pw->pw_gid) < 0)
125                 fatal("setegid %u: %.100s", (u_int)pw->pw_gid,
126                     strerror(errno));
127         if (seteuid(pw->pw_uid) == -1)
128                 fatal("seteuid %u: %.100s", (u_int)pw->pw_uid,
129                     strerror(errno));
130 }
131
132 void
133 permanently_drop_suid(uid_t uid)
134 {
135         uid_t old_uid = getuid();
136
137         debug("permanently_drop_suid: %u", (u_int)uid);
138 #if defined(HAVE_SETRESUID) && !defined(BROKEN_SETRESUID)
139         if (setresuid(uid, uid, uid) < 0)
140                 fatal("setresuid %u: %.100s", (u_int)uid, strerror(errno));
141 #elif defined(HAVE_SETREUID) && !defined(BROKEN_SETREUID)
142         if (setreuid(uid, uid) < 0)
143                 fatal("setreuid %u: %.100s", (u_int)uid, strerror(errno));
144 #else
145 # ifndef SETEUID_BREAKS_SETUID
146         if (seteuid(uid) < 0)
147                 fatal("seteuid %u: %.100s", (u_int)uid, strerror(errno));
148 # endif
149         if (setuid(uid) < 0)
150                 fatal("setuid %u: %.100s", (u_int)uid, strerror(errno));
151 #endif
152
153 #ifndef HAVE_CYGWIN
154         /* Try restoration of UID if changed (test clearing of saved uid) */
155         if (old_uid != uid &&
156             (setuid(old_uid) != -1 || seteuid(old_uid) != -1))
157                 fatal("%s: was able to restore old [e]uid", __func__);
158 #endif
159
160         /* Verify UID drop was successful */
161         if (getuid() != uid || geteuid() != uid) {
162                 fatal("%s: euid incorrect uid:%u euid:%u (should be %u)",
163                     __func__, (u_int)getuid(), (u_int)geteuid(), (u_int)uid);
164         }
165 }
166
167 /*
168  * Restores to the original (privileged) uid.
169  */
170 void
171 restore_uid(void)
172 {
173         /* it's a no-op unless privileged */
174         if (!privileged) {
175                 debug("restore_uid: (unprivileged)");
176                 return;
177         }
178         if (!temporarily_use_uid_effective)
179                 fatal("restore_uid: temporarily_use_uid not effective");
180
181 #ifdef SAVED_IDS_WORK_WITH_SETEUID
182         debug("restore_uid: %u/%u", (u_int)saved_euid, (u_int)saved_egid);
183         /* Set the effective uid back to the saved privileged uid. */
184         if (seteuid(saved_euid) < 0)
185                 fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror(errno));
186         if (setegid(saved_egid) < 0)
187                 fatal("setegid %u: %.100s", (u_int)saved_egid, strerror(errno));
188 #else /* SAVED_IDS_WORK_WITH_SETEUID */
189         /*
190          * We are unable to restore the real uid to its unprivileged value.
191          * Propagate the real uid (usually more privileged) to effective uid
192          * as well.
193          */
194         setuid(getuid());
195         setgid(getgid());
196 #endif /* SAVED_IDS_WORK_WITH_SETEUID */
197
198         if (setgroups(saved_egroupslen, saved_egroups) < 0)
199                 fatal("setgroups: %.100s", strerror(errno));
200         temporarily_use_uid_effective = 0;
201 }
202
203 /*
204  * Permanently sets all uids to the given uid.  This cannot be
205  * called while temporarily_use_uid is effective.
206  */
207 void
208 permanently_set_uid(struct passwd *pw)
209 {
210         uid_t old_uid = getuid();
211         gid_t old_gid = getgid();
212
213         if (pw == NULL)
214                 fatal("permanently_set_uid: no user given");
215         if (temporarily_use_uid_effective)
216                 fatal("permanently_set_uid: temporarily_use_uid effective");
217         debug("permanently_set_uid: %u/%u", (u_int)pw->pw_uid,
218             (u_int)pw->pw_gid);
219
220 #if defined(HAVE_SETRESGID) && !defined(BROKEN_SETRESGID)
221         if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) < 0)
222                 fatal("setresgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
223 #elif defined(HAVE_SETREGID) && !defined(BROKEN_SETREGID)
224         if (setregid(pw->pw_gid, pw->pw_gid) < 0)
225                 fatal("setregid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
226 #else
227         if (setegid(pw->pw_gid) < 0)
228                 fatal("setegid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
229         if (setgid(pw->pw_gid) < 0)
230                 fatal("setgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
231 #endif
232
233 #if defined(HAVE_SETRESUID) && !defined(BROKEN_SETRESUID)
234         if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) < 0)
235                 fatal("setresuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
236 #elif defined(HAVE_SETREUID) && !defined(BROKEN_SETREUID)
237         if (setreuid(pw->pw_uid, pw->pw_uid) < 0)
238                 fatal("setreuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
239 #else
240 # ifndef SETEUID_BREAKS_SETUID
241         if (seteuid(pw->pw_uid) < 0)
242                 fatal("seteuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
243 # endif
244         if (setuid(pw->pw_uid) < 0)
245                 fatal("setuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
246 #endif
247
248 #ifndef HAVE_CYGWIN
249         /* Try restoration of GID if changed (test clearing of saved gid) */
250         if (old_gid != pw->pw_gid && pw->pw_uid != 0 &&
251             (setgid(old_gid) != -1 || setegid(old_gid) != -1))
252                 fatal("%s: was able to restore old [e]gid", __func__);
253 #endif
254
255         /* Verify GID drop was successful */
256         if (getgid() != pw->pw_gid || getegid() != pw->pw_gid) {
257                 fatal("%s: egid incorrect gid:%u egid:%u (should be %u)",
258                     __func__, (u_int)getgid(), (u_int)getegid(),
259                     (u_int)pw->pw_gid);
260         }
261
262 #ifndef HAVE_CYGWIN
263         /* Try restoration of UID if changed (test clearing of saved uid) */
264         if (old_uid != pw->pw_uid &&
265             (setuid(old_uid) != -1 || seteuid(old_uid) != -1))
266                 fatal("%s: was able to restore old [e]uid", __func__);
267 #endif
268
269         /* Verify UID drop was successful */
270         if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) {
271                 fatal("%s: euid incorrect uid:%u euid:%u (should be %u)",
272                     __func__, (u_int)getuid(), (u_int)geteuid(),
273                     (u_int)pw->pw_uid);
274         }
275 }
This page took 0.044286 seconds and 3 git commands to generate.