1 /* $OpenBSD: uidswap.c,v 1.39 2015/06/24 01:49:19 dtucker Exp $ */ 2 /* 3 * Author: Tatu Ylonen <ylo (at) cs.hut.fi> 4 * Copyright (c) 1995 Tatu Ylonen <ylo (at) 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 <errno.h> 18 #include <pwd.h> 19 #include <string.h> 20 #include <unistd.h> 21 #include <limits.h> 22 #include <stdarg.h> 23 #include <stdlib.h> 24 25 #include <grp.h> 26 27 #include "log.h" 28 #include "uidswap.h" 29 #include "xmalloc.h" 30 31 #ifdef ANDROID 32 #include <private/android_filesystem_config.h> 33 #if !defined(GCE_PLATFORM_SDK_VERSION) || (GCE_PLATFORM_SDK_VERSION > 17) 34 #include <sys/capability.h> 35 #else 36 #include <linux/capability.h> 37 #endif 38 #include <sys/prctl.h> 39 #endif 40 41 /* 42 * Note: all these functions must work in all of the following cases: 43 * 1. euid=0, ruid=0 44 * 2. euid=0, ruid!=0 45 * 3. euid!=0, ruid!=0 46 * Additionally, they must work regardless of whether the system has 47 * POSIX saved uids or not. 48 */ 49 50 #if defined(_POSIX_SAVED_IDS) && !defined(BROKEN_SAVED_UIDS) 51 /* Lets assume that posix saved ids also work with seteuid, even though that 52 is not part of the posix specification. */ 53 #define SAVED_IDS_WORK_WITH_SETEUID 54 /* Saved effective uid. */ 55 static uid_t saved_euid = 0; 56 static gid_t saved_egid = 0; 57 #endif 58 59 /* Saved effective uid. */ 60 static int privileged = 0; 61 static int temporarily_use_uid_effective = 0; 62 static gid_t *saved_egroups = NULL, *user_groups = NULL; 63 static int saved_egroupslen = -1, user_groupslen = -1; 64 65 /* 66 * Temporarily changes to the given uid. If the effective user 67 * id is not root, this does nothing. This call cannot be nested. 68 */ 69 void 70 temporarily_use_uid(struct passwd *pw) 71 { 72 /* Save the current euid, and egroups. */ 73 #ifdef SAVED_IDS_WORK_WITH_SETEUID 74 saved_euid = geteuid(); 75 saved_egid = getegid(); 76 debug("temporarily_use_uid: %u/%u (e=%u/%u)", 77 (u_int)pw->pw_uid, (u_int)pw->pw_gid, 78 (u_int)saved_euid, (u_int)saved_egid); 79 #ifndef HAVE_CYGWIN 80 if (saved_euid != 0) { 81 privileged = 0; 82 return; 83 } 84 #endif 85 #else 86 if (geteuid() != 0) { 87 privileged = 0; 88 return; 89 } 90 #endif /* SAVED_IDS_WORK_WITH_SETEUID */ 91 92 privileged = 1; 93 temporarily_use_uid_effective = 1; 94 95 saved_egroupslen = getgroups(0, NULL); 96 if (saved_egroupslen < 0) 97 fatal("getgroups: %.100s", strerror(errno)); 98 if (saved_egroupslen > 0) { 99 saved_egroups = xreallocarray(saved_egroups, 100 saved_egroupslen, sizeof(gid_t)); 101 if (getgroups(saved_egroupslen, saved_egroups) < 0) 102 fatal("getgroups: %.100s", strerror(errno)); 103 } else { /* saved_egroupslen == 0 */ 104 free(saved_egroups); 105 } 106 107 /* set and save the user's groups */ 108 if (user_groupslen == -1) { 109 if (initgroups(pw->pw_name, pw->pw_gid) < 0) 110 fatal("initgroups: %s: %.100s", pw->pw_name, 111 strerror(errno)); 112 113 user_groupslen = getgroups(0, NULL); 114 if (user_groupslen < 0) 115 fatal("getgroups: %.100s", strerror(errno)); 116 if (user_groupslen > 0) { 117 user_groups = xreallocarray(user_groups, 118 user_groupslen, sizeof(gid_t)); 119 if (getgroups(user_groupslen, user_groups) < 0) 120 fatal("getgroups: %.100s", strerror(errno)); 121 } else { /* user_groupslen == 0 */ 122 free(user_groups); 123 } 124 } 125 /* Set the effective uid to the given (unprivileged) uid. */ 126 if (setgroups(user_groupslen, user_groups) < 0) 127 fatal("setgroups: %.100s", strerror(errno)); 128 #ifndef SAVED_IDS_WORK_WITH_SETEUID 129 /* Propagate the privileged gid to all of our gids. */ 130 if (setgid(getegid()) < 0) 131 debug("setgid %u: %.100s", (u_int) getegid(), strerror(errno)); 132 /* Propagate the privileged uid to all of our uids. */ 133 if (setuid(geteuid()) < 0) 134 debug("setuid %u: %.100s", (u_int) geteuid(), strerror(errno)); 135 #endif /* SAVED_IDS_WORK_WITH_SETEUID */ 136 if (setegid(pw->pw_gid) < 0) 137 fatal("setegid %u: %.100s", (u_int)pw->pw_gid, 138 strerror(errno)); 139 if (seteuid(pw->pw_uid) == -1) 140 fatal("seteuid %u: %.100s", (u_int)pw->pw_uid, 141 strerror(errno)); 142 } 143 144 void 145 permanently_drop_suid(uid_t uid) 146 { 147 #ifndef HAVE_CYGWIN 148 uid_t old_uid = getuid(); 149 #endif 150 151 debug("permanently_drop_suid: %u", (u_int)uid); 152 if (setresuid(uid, uid, uid) < 0) 153 fatal("setresuid %u: %.100s", (u_int)uid, strerror(errno)); 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 169 /* 170 * Restores to the original (privileged) uid. 171 */ 172 void 173 restore_uid(void) 174 { 175 /* it's a no-op unless privileged */ 176 if (!privileged) { 177 debug("restore_uid: (unprivileged)"); 178 return; 179 } 180 if (!temporarily_use_uid_effective) 181 fatal("restore_uid: temporarily_use_uid not effective"); 182 183 #ifdef SAVED_IDS_WORK_WITH_SETEUID 184 debug("restore_uid: %u/%u", (u_int)saved_euid, (u_int)saved_egid); 185 /* Set the effective uid back to the saved privileged uid. */ 186 if (seteuid(saved_euid) < 0) 187 fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror(errno)); 188 if (setegid(saved_egid) < 0) 189 fatal("setegid %u: %.100s", (u_int)saved_egid, strerror(errno)); 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 200 if (setgroups(saved_egroupslen, saved_egroups) < 0) 201 fatal("setgroups: %.100s", strerror(errno)); 202 temporarily_use_uid_effective = 0; 203 } 204 205 /* 206 * Permanently sets all uids to the given uid. This cannot be 207 * called while temporarily_use_uid is effective. 208 */ 209 void 210 permanently_set_uid(struct passwd *pw) 211 { 212 #ifndef HAVE_CYGWIN 213 uid_t old_uid = getuid(); 214 gid_t old_gid = getgid(); 215 #endif 216 #if defined(ANDROID) 217 struct __user_cap_header_struct header; 218 struct __user_cap_data_struct cap; 219 #endif 220 221 if (pw == NULL) 222 fatal("permanently_set_uid: no user given"); 223 if (temporarily_use_uid_effective) 224 fatal("permanently_set_uid: temporarily_use_uid effective"); 225 debug("permanently_set_uid: %u/%u", (u_int)pw->pw_uid, 226 (u_int)pw->pw_gid); 227 228 #if defined(ANDROID) 229 if (pw->pw_uid == AID_SHELL) { 230 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0); 231 232 /* add extra groups needed for shell user: 233 * - AID_LOG to read system logs (adb logcat) 234 * - AID_INPUT to diagnose input issues (getevent) 235 * - AID_INET to diagnose network issues (netcfg, ping) 236 * - AID_GRAPHICS to access the frame buffer 237 * - AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump) 238 * - AID_SDCARD_RW to allow writing to the SD card 239 * - AID_MOUNT to allow unmounting the SD card before rebooting 240 * - AID_NET_BW_STATS to read out qtaguid statistics. */ 241 gid_t groups[] = {AID_LOG, AID_INPUT, AID_INET, 242 AID_GRAPHICS, AID_NET_BT, AID_NET_BT_ADMIN, 243 AID_SDCARD_RW, AID_MOUNT, AID_NET_BW_STATS}; 244 setgroups(sizeof(groups)/sizeof(groups[0]), groups); 245 } 246 #endif 247 248 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) < 0) 249 fatal("setresgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno)); 250 251 #ifdef __APPLE__ 252 /* 253 * OS X requires initgroups after setgid to opt back into 254 * memberd support for >16 supplemental groups. 255 */ 256 if (initgroups(pw->pw_name, pw->pw_gid) < 0) 257 fatal("initgroups %.100s %u: %.100s", 258 pw->pw_name, (u_int)pw->pw_gid, strerror(errno)); 259 #endif 260 261 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) < 0) 262 fatal("setresuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno)); 263 264 #ifndef HAVE_CYGWIN 265 /* Try restoration of GID if changed (test clearing of saved gid) */ 266 if (old_gid != pw->pw_gid && pw->pw_uid != 0 && 267 (setgid(old_gid) != -1 || setegid(old_gid) != -1)) 268 fatal("%s: was able to restore old [e]gid", __func__); 269 #endif 270 271 /* Verify GID drop was successful */ 272 if (getgid() != pw->pw_gid || getegid() != pw->pw_gid) { 273 fatal("%s: egid incorrect gid:%u egid:%u (should be %u)", 274 __func__, (u_int)getgid(), (u_int)getegid(), 275 (u_int)pw->pw_gid); 276 } 277 278 #ifndef HAVE_CYGWIN 279 /* Try restoration of UID if changed (test clearing of saved uid) */ 280 if (old_uid != pw->pw_uid && 281 (setuid(old_uid) != -1 || seteuid(old_uid) != -1)) 282 fatal("%s: was able to restore old [e]uid", __func__); 283 #endif 284 285 /* Verify UID drop was successful */ 286 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) { 287 fatal("%s: euid incorrect uid:%u euid:%u (should be %u)", 288 __func__, (u_int)getuid(), (u_int)geteuid(), 289 (u_int)pw->pw_uid); 290 } 291 292 #ifdef ANDROID 293 if (pw->pw_uid == AID_SHELL) { 294 /* set CAP_SYS_BOOT capability, so "adb reboot" will succeed */ 295 header.version = _LINUX_CAPABILITY_VERSION; 296 header.pid = 0; 297 cap.effective = cap.permitted = (1 << CAP_SYS_BOOT); 298 cap.inheritable = 0; 299 capset(&header, &cap); 300 } 301 #endif 302 303 } 304