1 /* $OpenBSD: uidswap.c,v 1.42 2019/06/28 13:35:04 deraadt 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 #ifdef __FreeBSD__ 18 #include <assert.h> 19 #endif 20 #include <errno.h> 21 #include <pwd.h> 22 #include <string.h> 23 #include <unistd.h> 24 #include <limits.h> 25 #include <stdarg.h> 26 #include <stdlib.h> 27 28 #include <grp.h> 29 30 #include "log.h" 31 #include "uidswap.h" 32 #include "xmalloc.h" 33 34 /* 35 * Note: all these functions must work in all of the following cases: 36 * 1. euid=0, ruid=0 37 * 2. euid=0, ruid!=0 38 * 3. euid!=0, ruid!=0 39 * Additionally, they must work regardless of whether the system has 40 * POSIX saved uids or not. 41 */ 42 43 #if defined(_POSIX_SAVED_IDS) && !defined(BROKEN_SAVED_UIDS) 44 /* Lets assume that posix saved ids also work with seteuid, even though that 45 is not part of the posix specification. */ 46 #define SAVED_IDS_WORK_WITH_SETEUID 47 /* Saved effective uid. */ 48 static uid_t saved_euid = 0; 49 static gid_t saved_egid = 0; 50 #endif 51 52 /* Saved effective uid. */ 53 static int privileged = 0; 54 static int temporarily_use_uid_effective = 0; 55 static uid_t user_groups_uid; 56 static gid_t *saved_egroups = NULL, *user_groups = NULL; 57 static int saved_egroupslen = -1, user_groupslen = -1; 58 59 /* 60 * Temporarily changes to the given uid. If the effective user 61 * id is not root, this does nothing. This call cannot be nested. 62 */ 63 void 64 temporarily_use_uid(struct passwd *pw) 65 { 66 /* Save the current euid, and egroups. */ 67 #ifdef SAVED_IDS_WORK_WITH_SETEUID 68 saved_euid = geteuid(); 69 saved_egid = getegid(); 70 debug("temporarily_use_uid: %u/%u (e=%u/%u)", 71 (u_int)pw->pw_uid, (u_int)pw->pw_gid, 72 (u_int)saved_euid, (u_int)saved_egid); 73 #ifndef HAVE_CYGWIN 74 if (saved_euid != 0) { 75 privileged = 0; 76 return; 77 } 78 #endif 79 #else 80 if (geteuid() != 0) { 81 privileged = 0; 82 return; 83 } 84 #endif /* SAVED_IDS_WORK_WITH_SETEUID */ 85 86 privileged = 1; 87 temporarily_use_uid_effective = 1; 88 89 saved_egroupslen = getgroups(0, NULL); 90 if (saved_egroupslen == -1) 91 fatal("getgroups: %.100s", strerror(errno)); 92 if (saved_egroupslen > 0) { 93 saved_egroups = xreallocarray(saved_egroups, 94 saved_egroupslen, sizeof(gid_t)); 95 if (getgroups(saved_egroupslen, saved_egroups) == -1) 96 fatal("getgroups: %.100s", strerror(errno)); 97 } else { /* saved_egroupslen == 0 */ 98 free(saved_egroups); 99 saved_egroups = NULL; 100 } 101 102 /* set and save the user's groups */ 103 if (user_groupslen == -1 || user_groups_uid != pw->pw_uid) { 104 if (initgroups(pw->pw_name, pw->pw_gid) == -1) 105 fatal("initgroups: %s: %.100s", pw->pw_name, 106 strerror(errno)); 107 108 user_groupslen = getgroups(0, NULL); 109 if (user_groupslen == -1) 110 fatal("getgroups: %.100s", strerror(errno)); 111 if (user_groupslen > 0) { 112 user_groups = xreallocarray(user_groups, 113 user_groupslen, sizeof(gid_t)); 114 if (getgroups(user_groupslen, user_groups) == -1) 115 fatal("getgroups: %.100s", strerror(errno)); 116 } else { /* user_groupslen == 0 */ 117 free(user_groups); 118 user_groups = NULL; 119 } 120 user_groups_uid = pw->pw_uid; 121 } 122 /* Set the effective uid to the given (unprivileged) uid. */ 123 if (setgroups(user_groupslen, user_groups) == -1) 124 fatal("setgroups: %.100s", strerror(errno)); 125 #ifndef SAVED_IDS_WORK_WITH_SETEUID 126 /* Propagate the privileged gid to all of our gids. */ 127 #ifdef __FreeBSD__ 128 /* 129 * FreeBSD traditionally includes the egid as the first element. If we 130 * use getegid() here then we effectively propagate user_groups[0], 131 * which is probably pw->pw_gid. Fix it to work as intended by using 132 * the egid we already have stashed off. 133 */ 134 assert(saved_egroupslen > 0); 135 if (setgid(saved_egroups[0]) == -1) 136 debug("setgid %u: %.100s", (u_int) saved_egroups[0], strerror(errno)); 137 #else 138 if (setgid(getegid()) == -1) 139 debug("setgid %u: %.100s", (u_int) getegid(), strerror(errno)); 140 #endif 141 /* Propagate the privileged uid to all of our uids. */ 142 if (setuid(geteuid()) == -1) 143 debug("setuid %u: %.100s", (u_int) geteuid(), strerror(errno)); 144 #endif /* SAVED_IDS_WORK_WITH_SETEUID */ 145 if (setegid(pw->pw_gid) == -1) 146 fatal("setegid %u: %.100s", (u_int)pw->pw_gid, 147 strerror(errno)); 148 if (seteuid(pw->pw_uid) == -1) 149 fatal("seteuid %u: %.100s", (u_int)pw->pw_uid, 150 strerror(errno)); 151 } 152 153 /* 154 * Restores to the original (privileged) uid. 155 */ 156 void 157 restore_uid(void) 158 { 159 /* it's a no-op unless privileged */ 160 if (!privileged) { 161 debug("restore_uid: (unprivileged)"); 162 return; 163 } 164 if (!temporarily_use_uid_effective) 165 fatal("restore_uid: temporarily_use_uid not effective"); 166 167 #ifdef SAVED_IDS_WORK_WITH_SETEUID 168 debug("restore_uid: %u/%u", (u_int)saved_euid, (u_int)saved_egid); 169 /* Set the effective uid back to the saved privileged uid. */ 170 if (seteuid(saved_euid) == -1) 171 fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror(errno)); 172 if (setegid(saved_egid) == -1) 173 fatal("setegid %u: %.100s", (u_int)saved_egid, strerror(errno)); 174 #else /* SAVED_IDS_WORK_WITH_SETEUID */ 175 /* 176 * We are unable to restore the real uid to its unprivileged value. 177 * Propagate the real uid (usually more privileged) to effective uid 178 * as well. 179 */ 180 if (setuid(getuid()) == -1) 181 fatal("%s: setuid failed: %s", __func__, strerror(errno)); 182 if (setgid(getgid()) == -1) 183 fatal("%s: setgid failed: %s", __func__, strerror(errno)); 184 #endif /* SAVED_IDS_WORK_WITH_SETEUID */ 185 186 if (setgroups(saved_egroupslen, saved_egroups) == -1) 187 fatal("setgroups: %.100s", strerror(errno)); 188 temporarily_use_uid_effective = 0; 189 } 190 191 /* 192 * Permanently sets all uids to the given uid. This cannot be 193 * called while temporarily_use_uid is effective. 194 */ 195 void 196 permanently_set_uid(struct passwd *pw) 197 { 198 #ifndef NO_UID_RESTORATION_TEST 199 uid_t old_uid = getuid(); 200 gid_t old_gid = getgid(); 201 #endif 202 203 if (pw == NULL) 204 fatal("permanently_set_uid: no user given"); 205 if (temporarily_use_uid_effective) 206 fatal("permanently_set_uid: temporarily_use_uid effective"); 207 debug("permanently_set_uid: %u/%u", (u_int)pw->pw_uid, 208 (u_int)pw->pw_gid); 209 210 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1) 211 fatal("setresgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno)); 212 213 #ifdef __APPLE__ 214 /* 215 * OS X requires initgroups after setgid to opt back into 216 * memberd support for >16 supplemental groups. 217 */ 218 if (initgroups(pw->pw_name, pw->pw_gid) == -1) 219 fatal("initgroups %.100s %u: %.100s", 220 pw->pw_name, (u_int)pw->pw_gid, strerror(errno)); 221 #endif 222 223 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1) 224 fatal("setresuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno)); 225 226 #ifndef NO_UID_RESTORATION_TEST 227 /* Try restoration of GID if changed (test clearing of saved gid) */ 228 if (old_gid != pw->pw_gid && pw->pw_uid != 0 && 229 (setgid(old_gid) != -1 || setegid(old_gid) != -1)) 230 fatal("%s: was able to restore old [e]gid", __func__); 231 #endif 232 233 /* Verify GID drop was successful */ 234 if (getgid() != pw->pw_gid || getegid() != pw->pw_gid) { 235 fatal("%s: egid incorrect gid:%u egid:%u (should be %u)", 236 __func__, (u_int)getgid(), (u_int)getegid(), 237 (u_int)pw->pw_gid); 238 } 239 240 #ifndef NO_UID_RESTORATION_TEST 241 /* Try restoration of UID if changed (test clearing of saved uid) */ 242 if (old_uid != pw->pw_uid && 243 (setuid(old_uid) != -1 || seteuid(old_uid) != -1)) 244 fatal("%s: was able to restore old [e]uid", __func__); 245 #endif 246 247 /* Verify UID drop was successful */ 248 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) { 249 fatal("%s: euid incorrect uid:%u euid:%u (should be %u)", 250 __func__, (u_int)getuid(), (u_int)geteuid(), 251 (u_int)pw->pw_uid); 252 } 253 } 254