]>
Commit | Line | Data |
---|---|---|
5188ba17 | 1 | /* $OpenBSD: uidswap.c,v 1.32 2006/07/17 01:31:10 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 | ||
028094f4 | 19 | #include <errno.h> |
b1842393 | 20 | #include <pwd.h> |
5188ba17 | 21 | #include <unistd.h> |
b1842393 | 22 | |
16fbe330 | 23 | #include <grp.h> |
24 | ||
42f11eb2 | 25 | #include "log.h" |
8efc0c15 | 26 | #include "uidswap.h" |
13dff404 | 27 | #include "xmalloc.h" |
8efc0c15 | 28 | |
5260325f | 29 | /* |
30 | * Note: all these functions must work in all of the following cases: | |
31 | * 1. euid=0, ruid=0 | |
32 | * 2. euid=0, ruid!=0 | |
33 | * 3. euid!=0, ruid!=0 | |
34 | * Additionally, they must work regardless of whether the system has | |
35 | * POSIX saved uids or not. | |
36 | */ | |
8efc0c15 | 37 | |
0096ac62 | 38 | #if defined(_POSIX_SAVED_IDS) && !defined(BROKEN_SAVED_UIDS) |
8efc0c15 | 39 | /* Lets assume that posix saved ids also work with seteuid, even though that |
40 | is not part of the posix specification. */ | |
0096ac62 | 41 | #define SAVED_IDS_WORK_WITH_SETEUID |
42 | /* Saved effective uid. */ | |
43 | static uid_t saved_euid = 0; | |
44 | static gid_t saved_egid = 0; | |
45 | #endif | |
f8252c48 | 46 | |
8efc0c15 | 47 | /* Saved effective uid. */ |
63bd8c36 | 48 | static int privileged = 0; |
49 | static int temporarily_use_uid_effective = 0; | |
13dff404 | 50 | static gid_t *saved_egroups = NULL, *user_groups = NULL; |
63bd8c36 | 51 | static int saved_egroupslen = -1, user_groupslen = -1; |
f8252c48 | 52 | |
5260325f | 53 | /* |
54 | * Temporarily changes to the given uid. If the effective user | |
55 | * id is not root, this does nothing. This call cannot be nested. | |
56 | */ | |
6ae2364d | 57 | void |
63bd8c36 | 58 | temporarily_use_uid(struct passwd *pw) |
8efc0c15 | 59 | { |
f8252c48 | 60 | /* Save the current euid, and egroups. */ |
0096ac62 | 61 | #ifdef SAVED_IDS_WORK_WITH_SETEUID |
f8252c48 | 62 | saved_euid = geteuid(); |
0096ac62 | 63 | saved_egid = getegid(); |
0df3a240 | 64 | debug("temporarily_use_uid: %u/%u (e=%u/%u)", |
65 | (u_int)pw->pw_uid, (u_int)pw->pw_gid, | |
66 | (u_int)saved_euid, (u_int)saved_egid); | |
c2736f7f | 67 | #ifndef HAVE_CYGWIN |
f8252c48 | 68 | if (saved_euid != 0) { |
69 | privileged = 0; | |
70 | return; | |
71 | } | |
c2736f7f | 72 | #endif |
0096ac62 | 73 | #else |
74 | if (geteuid() != 0) { | |
75 | privileged = 0; | |
76 | return; | |
77 | } | |
78 | #endif /* SAVED_IDS_WORK_WITH_SETEUID */ | |
79 | ||
f8252c48 | 80 | privileged = 1; |
81 | temporarily_use_uid_effective = 1; | |
13dff404 | 82 | |
83 | saved_egroupslen = getgroups(0, NULL); | |
f8252c48 | 84 | if (saved_egroupslen < 0) |
85 | fatal("getgroups: %.100s", strerror(errno)); | |
13dff404 | 86 | if (saved_egroupslen > 0) { |
87 | saved_egroups = xrealloc(saved_egroups, | |
c5d10563 | 88 | saved_egroupslen, sizeof(gid_t)); |
13dff404 | 89 | if (getgroups(saved_egroupslen, saved_egroups) < 0) |
90 | fatal("getgroups: %.100s", strerror(errno)); | |
91 | } else { /* saved_egroupslen == 0 */ | |
972fc531 | 92 | if (saved_egroups != NULL) |
13dff404 | 93 | xfree(saved_egroups); |
94 | } | |
f8252c48 | 95 | |
96 | /* set and save the user's groups */ | |
97 | if (user_groupslen == -1) { | |
98 | if (initgroups(pw->pw_name, pw->pw_gid) < 0) | |
99 | fatal("initgroups: %s: %.100s", pw->pw_name, | |
100 | strerror(errno)); | |
13dff404 | 101 | |
102 | user_groupslen = getgroups(0, NULL); | |
f8252c48 | 103 | if (user_groupslen < 0) |
104 | fatal("getgroups: %.100s", strerror(errno)); | |
13dff404 | 105 | if (user_groupslen > 0) { |
106 | user_groups = xrealloc(user_groups, | |
c5d10563 | 107 | user_groupslen, sizeof(gid_t)); |
13dff404 | 108 | if (getgroups(user_groupslen, user_groups) < 0) |
109 | fatal("getgroups: %.100s", strerror(errno)); | |
110 | } else { /* user_groupslen == 0 */ | |
111 | if (user_groups) | |
112 | xfree(user_groups); | |
113 | } | |
f8252c48 | 114 | } |
115 | /* Set the effective uid to the given (unprivileged) uid. */ | |
d98d029a | 116 | if (setgroups(user_groupslen, user_groups) < 0) |
63bd8c36 | 117 | fatal("setgroups: %.100s", strerror(errno)); |
0096ac62 | 118 | #ifndef SAVED_IDS_WORK_WITH_SETEUID |
119 | /* Propagate the privileged gid to all of our gids. */ | |
120 | if (setgid(getegid()) < 0) | |
121 | debug("setgid %u: %.100s", (u_int) getegid(), strerror(errno)); | |
122 | /* Propagate the privileged uid to all of our uids. */ | |
123 | if (setuid(geteuid()) < 0) | |
124 | debug("setuid %u: %.100s", (u_int) geteuid(), strerror(errno)); | |
125 | #endif /* SAVED_IDS_WORK_WITH_SETEUID */ | |
d98d029a | 126 | if (setegid(pw->pw_gid) < 0) |
caa49784 | 127 | fatal("setegid %u: %.100s", (u_int)pw->pw_gid, |
f8252c48 | 128 | strerror(errno)); |
129 | if (seteuid(pw->pw_uid) == -1) | |
caa49784 | 130 | fatal("seteuid %u: %.100s", (u_int)pw->pw_uid, |
f8252c48 | 131 | strerror(errno)); |
8efc0c15 | 132 | } |
f8252c48 | 133 | |
3c33c1b6 | 134 | void |
135 | permanently_drop_suid(uid_t uid) | |
136 | { | |
137 | uid_t old_uid = getuid(); | |
138 | ||
139 | debug("permanently_drop_suid: %u", (u_int)uid); | |
140 | #if defined(HAVE_SETRESUID) && !defined(BROKEN_SETRESUID) | |
141 | if (setresuid(uid, uid, uid) < 0) | |
142 | fatal("setresuid %u: %.100s", (u_int)uid, strerror(errno)); | |
143 | #elif defined(HAVE_SETREUID) && !defined(BROKEN_SETREUID) | |
144 | if (setreuid(uid, uid) < 0) | |
145 | fatal("setreuid %u: %.100s", (u_int)uid, strerror(errno)); | |
146 | #else | |
147 | # ifndef SETEUID_BREAKS_SETUID | |
148 | if (seteuid(uid) < 0) | |
149 | fatal("seteuid %u: %.100s", (u_int)uid, strerror(errno)); | |
150 | # endif | |
151 | if (setuid(uid) < 0) | |
152 | fatal("setuid %u: %.100s", (u_int)uid, strerror(errno)); | |
153 | #endif | |
154 | ||
155 | #ifndef HAVE_CYGWIN | |
156 | /* Try restoration of UID if changed (test clearing of saved uid) */ | |
157 | if (old_uid != uid && | |
158 | (setuid(old_uid) != -1 || seteuid(old_uid) != -1)) | |
159 | fatal("%s: was able to restore old [e]uid", __func__); | |
160 | #endif | |
161 | ||
162 | /* Verify UID drop was successful */ | |
163 | if (getuid() != uid || geteuid() != uid) { | |
164 | fatal("%s: euid incorrect uid:%u euid:%u (should be %u)", | |
165 | __func__, (u_int)getuid(), (u_int)geteuid(), (u_int)uid); | |
166 | } | |
167 | } | |
168 | ||
5260325f | 169 | /* |
a3626e12 | 170 | * Restores to the original (privileged) uid. |
5260325f | 171 | */ |
6ae2364d | 172 | void |
1e3b8b07 | 173 | restore_uid(void) |
8efc0c15 | 174 | { |
63bd8c36 | 175 | /* it's a no-op unless privileged */ |
0df3a240 | 176 | if (!privileged) { |
177 | debug("restore_uid: (unprivileged)"); | |
63bd8c36 | 178 | return; |
0df3a240 | 179 | } |
63bd8c36 | 180 | if (!temporarily_use_uid_effective) |
181 | fatal("restore_uid: temporarily_use_uid not effective"); | |
0096ac62 | 182 | |
183 | #ifdef SAVED_IDS_WORK_WITH_SETEUID | |
13979d47 | 184 | debug("restore_uid: %u/%u", (u_int)saved_euid, (u_int)saved_egid); |
a3626e12 | 185 | /* Set the effective uid back to the saved privileged uid. */ |
5260325f | 186 | if (seteuid(saved_euid) < 0) |
caa49784 | 187 | fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror(errno)); |
0096ac62 | 188 | if (setegid(saved_egid) < 0) |
caa49784 | 189 | fatal("setegid %u: %.100s", (u_int)saved_egid, strerror(errno)); |
0096ac62 | 190 | #else /* SAVED_IDS_WORK_WITH_SETEUID */ |
191 | /* | |
192 | * We are unable to restore the real uid to its unprivileged value. | |
193 | * Propagate the real uid (usually more privileged) to effective uid | |
194 | * as well. | |
195 | */ | |
196 | setuid(getuid()); | |
197 | setgid(getgid()); | |
198 | #endif /* SAVED_IDS_WORK_WITH_SETEUID */ | |
199 | ||
d98d029a | 200 | if (setgroups(saved_egroupslen, saved_egroups) < 0) |
63bd8c36 | 201 | fatal("setgroups: %.100s", strerror(errno)); |
63bd8c36 | 202 | temporarily_use_uid_effective = 0; |
8efc0c15 | 203 | } |
204 | ||
5260325f | 205 | /* |
206 | * Permanently sets all uids to the given uid. This cannot be | |
207 | * called while temporarily_use_uid is effective. | |
208 | */ | |
6ae2364d | 209 | void |
63bd8c36 | 210 | permanently_set_uid(struct passwd *pw) |
8efc0c15 | 211 | { |
688eed4a | 212 | uid_t old_uid = getuid(); |
213 | gid_t old_gid = getgid(); | |
214 | ||
82aeee5d | 215 | if (pw == NULL) |
216 | fatal("permanently_set_uid: no user given"); | |
63bd8c36 | 217 | if (temporarily_use_uid_effective) |
95f20fe8 | 218 | fatal("permanently_set_uid: temporarily_use_uid effective"); |
0df3a240 | 219 | debug("permanently_set_uid: %u/%u", (u_int)pw->pw_uid, |
220 | (u_int)pw->pw_gid); | |
688eed4a | 221 | |
9a3fe0e2 | 222 | #if defined(HAVE_SETRESGID) && !defined(BROKEN_SETRESGID) |
688eed4a | 223 | if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) < 0) |
224 | fatal("setresgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno)); | |
3b8dff69 | 225 | #elif defined(HAVE_SETREGID) && !defined(BROKEN_SETREGID) |
688eed4a | 226 | if (setregid(pw->pw_gid, pw->pw_gid) < 0) |
227 | fatal("setregid %u: %.100s", (u_int)pw->pw_gid, strerror(errno)); | |
228 | #else | |
fa5120a0 | 229 | if (setegid(pw->pw_gid) < 0) |
230 | fatal("setegid %u: %.100s", (u_int)pw->pw_gid, strerror(errno)); | |
63bd8c36 | 231 | if (setgid(pw->pw_gid) < 0) |
caa49784 | 232 | fatal("setgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno)); |
688eed4a | 233 | #endif |
234 | ||
9a3fe0e2 | 235 | #if defined(HAVE_SETRESUID) && !defined(BROKEN_SETRESUID) |
688eed4a | 236 | if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) < 0) |
237 | fatal("setresuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno)); | |
3b8dff69 | 238 | #elif defined(HAVE_SETREUID) && !defined(BROKEN_SETREUID) |
f5db6a03 | 239 | if (setreuid(pw->pw_uid, pw->pw_uid) < 0) |
240 | fatal("setreuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno)); | |
688eed4a | 241 | #else |
242 | # ifndef SETEUID_BREAKS_SETUID | |
fa5120a0 | 243 | if (seteuid(pw->pw_uid) < 0) |
244 | fatal("seteuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno)); | |
688eed4a | 245 | # endif |
a3626e12 | 246 | if (setuid(pw->pw_uid) < 0) |
caa49784 | 247 | fatal("setuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno)); |
688eed4a | 248 | #endif |
249 | ||
8f817407 | 250 | #ifndef HAVE_CYGWIN |
688eed4a | 251 | /* Try restoration of GID if changed (test clearing of saved gid) */ |
8f817407 | 252 | if (old_gid != pw->pw_gid && pw->pw_uid != 0 && |
688eed4a | 253 | (setgid(old_gid) != -1 || setegid(old_gid) != -1)) |
f5db6a03 | 254 | fatal("%s: was able to restore old [e]gid", __func__); |
8f817407 | 255 | #endif |
688eed4a | 256 | |
257 | /* Verify GID drop was successful */ | |
258 | if (getgid() != pw->pw_gid || getegid() != pw->pw_gid) { | |
aff51935 | 259 | fatal("%s: egid incorrect gid:%u egid:%u (should be %u)", |
260 | __func__, (u_int)getgid(), (u_int)getegid(), | |
688eed4a | 261 | (u_int)pw->pw_gid); |
262 | } | |
263 | ||
51e7d820 | 264 | #ifndef HAVE_CYGWIN |
688eed4a | 265 | /* Try restoration of UID if changed (test clearing of saved uid) */ |
aff51935 | 266 | if (old_uid != pw->pw_uid && |
688eed4a | 267 | (setuid(old_uid) != -1 || seteuid(old_uid) != -1)) |
f5db6a03 | 268 | fatal("%s: was able to restore old [e]uid", __func__); |
51e7d820 | 269 | #endif |
688eed4a | 270 | |
271 | /* Verify UID drop was successful */ | |
272 | if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) { | |
aff51935 | 273 | fatal("%s: euid incorrect uid:%u euid:%u (should be %u)", |
274 | __func__, (u_int)getuid(), (u_int)geteuid(), | |
688eed4a | 275 | (u_int)pw->pw_uid); |
276 | } | |
8efc0c15 | 277 | } |