]>
Commit | Line | Data |
---|---|---|
b1842393 | 1 | /* $OpenBSD: uidswap.c,v 1.30 2006/07/06 16:03:53 stevesk Exp $ */ |
8efc0c15 | 2 | /* |
5260325f | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | |
5 | * All rights reserved | |
5260325f | 6 | * Code for uid-swapping. |
bcbf86ec | 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". | |
5260325f | 13 | */ |
8efc0c15 | 14 | |
15 | #include "includes.h" | |
8efc0c15 | 16 | |
b1842393 | 17 | #include <sys/types.h> |
18 | ||
19 | #include <pwd.h> | |
20 | ||
16fbe330 | 21 | #include <grp.h> |
22 | ||
42f11eb2 | 23 | #include "log.h" |
8efc0c15 | 24 | #include "uidswap.h" |
13dff404 | 25 | #include "xmalloc.h" |
8efc0c15 | 26 | |
5260325f | 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 | */ | |
8efc0c15 | 35 | |
0096ac62 | 36 | #if defined(_POSIX_SAVED_IDS) && !defined(BROKEN_SAVED_UIDS) |
8efc0c15 | 37 | /* Lets assume that posix saved ids also work with seteuid, even though that |
38 | is not part of the posix specification. */ | |
0096ac62 | 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 | |
f8252c48 | 44 | |
8efc0c15 | 45 | /* Saved effective uid. */ |
63bd8c36 | 46 | static int privileged = 0; |
47 | static int temporarily_use_uid_effective = 0; | |
13dff404 | 48 | static gid_t *saved_egroups = NULL, *user_groups = NULL; |
63bd8c36 | 49 | static int saved_egroupslen = -1, user_groupslen = -1; |
f8252c48 | 50 | |
5260325f | 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 | */ | |
6ae2364d | 55 | void |
63bd8c36 | 56 | temporarily_use_uid(struct passwd *pw) |
8efc0c15 | 57 | { |
f8252c48 | 58 | /* Save the current euid, and egroups. */ |
0096ac62 | 59 | #ifdef SAVED_IDS_WORK_WITH_SETEUID |
f8252c48 | 60 | saved_euid = geteuid(); |
0096ac62 | 61 | saved_egid = getegid(); |
0df3a240 | 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); | |
c2736f7f | 65 | #ifndef HAVE_CYGWIN |
f8252c48 | 66 | if (saved_euid != 0) { |
67 | privileged = 0; | |
68 | return; | |
69 | } | |
c2736f7f | 70 | #endif |
0096ac62 | 71 | #else |
72 | if (geteuid() != 0) { | |
73 | privileged = 0; | |
74 | return; | |
75 | } | |
76 | #endif /* SAVED_IDS_WORK_WITH_SETEUID */ | |
77 | ||
f8252c48 | 78 | privileged = 1; |
79 | temporarily_use_uid_effective = 1; | |
13dff404 | 80 | |
81 | saved_egroupslen = getgroups(0, NULL); | |
f8252c48 | 82 | if (saved_egroupslen < 0) |
83 | fatal("getgroups: %.100s", strerror(errno)); | |
13dff404 | 84 | if (saved_egroupslen > 0) { |
85 | saved_egroups = xrealloc(saved_egroups, | |
c5d10563 | 86 | saved_egroupslen, sizeof(gid_t)); |
13dff404 | 87 | if (getgroups(saved_egroupslen, saved_egroups) < 0) |
88 | fatal("getgroups: %.100s", strerror(errno)); | |
89 | } else { /* saved_egroupslen == 0 */ | |
972fc531 | 90 | if (saved_egroups != NULL) |
13dff404 | 91 | xfree(saved_egroups); |
92 | } | |
f8252c48 | 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)); | |
13dff404 | 99 | |
100 | user_groupslen = getgroups(0, NULL); | |
f8252c48 | 101 | if (user_groupslen < 0) |
102 | fatal("getgroups: %.100s", strerror(errno)); | |
13dff404 | 103 | if (user_groupslen > 0) { |
104 | user_groups = xrealloc(user_groups, | |
c5d10563 | 105 | user_groupslen, sizeof(gid_t)); |
13dff404 | 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 | } | |
f8252c48 | 112 | } |
113 | /* Set the effective uid to the given (unprivileged) uid. */ | |
d98d029a | 114 | if (setgroups(user_groupslen, user_groups) < 0) |
63bd8c36 | 115 | fatal("setgroups: %.100s", strerror(errno)); |
0096ac62 | 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 */ | |
d98d029a | 124 | if (setegid(pw->pw_gid) < 0) |
caa49784 | 125 | fatal("setegid %u: %.100s", (u_int)pw->pw_gid, |
f8252c48 | 126 | strerror(errno)); |
127 | if (seteuid(pw->pw_uid) == -1) | |
caa49784 | 128 | fatal("seteuid %u: %.100s", (u_int)pw->pw_uid, |
f8252c48 | 129 | strerror(errno)); |
8efc0c15 | 130 | } |
f8252c48 | 131 | |
3c33c1b6 | 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 | ||
5260325f | 167 | /* |
a3626e12 | 168 | * Restores to the original (privileged) uid. |
5260325f | 169 | */ |
6ae2364d | 170 | void |
1e3b8b07 | 171 | restore_uid(void) |
8efc0c15 | 172 | { |
63bd8c36 | 173 | /* it's a no-op unless privileged */ |
0df3a240 | 174 | if (!privileged) { |
175 | debug("restore_uid: (unprivileged)"); | |
63bd8c36 | 176 | return; |
0df3a240 | 177 | } |
63bd8c36 | 178 | if (!temporarily_use_uid_effective) |
179 | fatal("restore_uid: temporarily_use_uid not effective"); | |
0096ac62 | 180 | |
181 | #ifdef SAVED_IDS_WORK_WITH_SETEUID | |
13979d47 | 182 | debug("restore_uid: %u/%u", (u_int)saved_euid, (u_int)saved_egid); |
a3626e12 | 183 | /* Set the effective uid back to the saved privileged uid. */ |
5260325f | 184 | if (seteuid(saved_euid) < 0) |
caa49784 | 185 | fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror(errno)); |
0096ac62 | 186 | if (setegid(saved_egid) < 0) |
caa49784 | 187 | fatal("setegid %u: %.100s", (u_int)saved_egid, strerror(errno)); |
0096ac62 | 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 | ||
d98d029a | 198 | if (setgroups(saved_egroupslen, saved_egroups) < 0) |
63bd8c36 | 199 | fatal("setgroups: %.100s", strerror(errno)); |
63bd8c36 | 200 | temporarily_use_uid_effective = 0; |
8efc0c15 | 201 | } |
202 | ||
5260325f | 203 | /* |
204 | * Permanently sets all uids to the given uid. This cannot be | |
205 | * called while temporarily_use_uid is effective. | |
206 | */ | |
6ae2364d | 207 | void |
63bd8c36 | 208 | permanently_set_uid(struct passwd *pw) |
8efc0c15 | 209 | { |
688eed4a | 210 | uid_t old_uid = getuid(); |
211 | gid_t old_gid = getgid(); | |
212 | ||
82aeee5d | 213 | if (pw == NULL) |
214 | fatal("permanently_set_uid: no user given"); | |
63bd8c36 | 215 | if (temporarily_use_uid_effective) |
95f20fe8 | 216 | fatal("permanently_set_uid: temporarily_use_uid effective"); |
0df3a240 | 217 | debug("permanently_set_uid: %u/%u", (u_int)pw->pw_uid, |
218 | (u_int)pw->pw_gid); | |
688eed4a | 219 | |
9a3fe0e2 | 220 | #if defined(HAVE_SETRESGID) && !defined(BROKEN_SETRESGID) |
688eed4a | 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)); | |
3b8dff69 | 223 | #elif defined(HAVE_SETREGID) && !defined(BROKEN_SETREGID) |
688eed4a | 224 | if (setregid(pw->pw_gid, pw->pw_gid) < 0) |
225 | fatal("setregid %u: %.100s", (u_int)pw->pw_gid, strerror(errno)); | |
226 | #else | |
fa5120a0 | 227 | if (setegid(pw->pw_gid) < 0) |
228 | fatal("setegid %u: %.100s", (u_int)pw->pw_gid, strerror(errno)); | |
63bd8c36 | 229 | if (setgid(pw->pw_gid) < 0) |
caa49784 | 230 | fatal("setgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno)); |
688eed4a | 231 | #endif |
232 | ||
9a3fe0e2 | 233 | #if defined(HAVE_SETRESUID) && !defined(BROKEN_SETRESUID) |
688eed4a | 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)); | |
3b8dff69 | 236 | #elif defined(HAVE_SETREUID) && !defined(BROKEN_SETREUID) |
f5db6a03 | 237 | if (setreuid(pw->pw_uid, pw->pw_uid) < 0) |
238 | fatal("setreuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno)); | |
688eed4a | 239 | #else |
240 | # ifndef SETEUID_BREAKS_SETUID | |
fa5120a0 | 241 | if (seteuid(pw->pw_uid) < 0) |
242 | fatal("seteuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno)); | |
688eed4a | 243 | # endif |
a3626e12 | 244 | if (setuid(pw->pw_uid) < 0) |
caa49784 | 245 | fatal("setuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno)); |
688eed4a | 246 | #endif |
247 | ||
8f817407 | 248 | #ifndef HAVE_CYGWIN |
688eed4a | 249 | /* Try restoration of GID if changed (test clearing of saved gid) */ |
8f817407 | 250 | if (old_gid != pw->pw_gid && pw->pw_uid != 0 && |
688eed4a | 251 | (setgid(old_gid) != -1 || setegid(old_gid) != -1)) |
f5db6a03 | 252 | fatal("%s: was able to restore old [e]gid", __func__); |
8f817407 | 253 | #endif |
688eed4a | 254 | |
255 | /* Verify GID drop was successful */ | |
256 | if (getgid() != pw->pw_gid || getegid() != pw->pw_gid) { | |
aff51935 | 257 | fatal("%s: egid incorrect gid:%u egid:%u (should be %u)", |
258 | __func__, (u_int)getgid(), (u_int)getegid(), | |
688eed4a | 259 | (u_int)pw->pw_gid); |
260 | } | |
261 | ||
51e7d820 | 262 | #ifndef HAVE_CYGWIN |
688eed4a | 263 | /* Try restoration of UID if changed (test clearing of saved uid) */ |
aff51935 | 264 | if (old_uid != pw->pw_uid && |
688eed4a | 265 | (setuid(old_uid) != -1 || seteuid(old_uid) != -1)) |
f5db6a03 | 266 | fatal("%s: was able to restore old [e]uid", __func__); |
51e7d820 | 267 | #endif |
688eed4a | 268 | |
269 | /* Verify UID drop was successful */ | |
270 | if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) { | |
aff51935 | 271 | fatal("%s: euid incorrect uid:%u euid:%u (should be %u)", |
272 | __func__, (u_int)getuid(), (u_int)geteuid(), | |
688eed4a | 273 | (u_int)pw->pw_uid); |
274 | } | |
8efc0c15 | 275 | } |