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