1 /* $NetBSD: uidswap.c,v 1.7 2017/04/18 18:41:46 christos Exp $ */ 2 /* $OpenBSD: uidswap.c,v 1.39 2015/06/24 01:49:19 dtucker Exp $ */ 3 /* 4 * Author: Tatu Ylonen <ylo@cs.hut.fi> 5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 6 * All rights reserved 7 * Code for uid-swapping. 8 * 9 * As far as I am concerned, the code I have written for this software 10 * can be used freely for any purpose. Any derived versions of this 11 * software must be clearly marked as such, and if the derived work is 12 * incompatible with the protocol description in the RFC file, it must be 13 * called by a name other than "ssh" or "Secure Shell". 14 */ 15 16 #include "includes.h" 17 __RCSID("$NetBSD: uidswap.c,v 1.7 2017/04/18 18:41:46 christos Exp $"); 18 #include <sys/param.h> 19 #include <errno.h> 20 #include <pwd.h> 21 #include <string.h> 22 #include <unistd.h> 23 #include <limits.h> 24 #include <stdarg.h> 25 #include <stdlib.h> 26 27 #include "log.h" 28 #include "uidswap.h" 29 30 /* 31 * Note: all these functions must work in all of the following cases: 32 * 1. euid=0, ruid=0 33 * 2. euid=0, ruid!=0 34 * 3. euid!=0, ruid!=0 35 * Additionally, they must work regardless of whether the system has 36 * POSIX saved uids or not. 37 */ 38 39 /* Lets assume that posix saved ids also work with seteuid, even though that 40 is not part of the posix specification. */ 41 42 /* Saved effective uid. */ 43 static int privileged = 0; 44 static int temporarily_use_uid_effective = 0; 45 static uid_t saved_euid = 0; 46 static gid_t saved_egid; 47 static gid_t saved_egroups[NGROUPS_MAX], user_groups[NGROUPS_MAX]; 48 static int saved_egroupslen = -1, user_groupslen = -1; 49 50 /* 51 * Temporarily changes to the given uid. If the effective user 52 * id is not root, this does nothing. This call cannot be nested. 53 */ 54 void 55 temporarily_use_uid(struct passwd *pw) 56 { 57 /* Save the current euid, and egroups. */ 58 saved_euid = geteuid(); 59 saved_egid = getegid(); 60 debug("temporarily_use_uid: %u/%u (e=%u/%u)", 61 (u_int)pw->pw_uid, (u_int)pw->pw_gid, 62 (u_int)saved_euid, (u_int)saved_egid); 63 if (saved_euid != 0) { 64 privileged = 0; 65 return; 66 } 67 privileged = 1; 68 temporarily_use_uid_effective = 1; 69 saved_egroupslen = getgroups(NGROUPS_MAX, saved_egroups); 70 if (saved_egroupslen < 0) 71 fatal("getgroups: %.100s", strerror(errno)); 72 73 /* set and save the user's groups */ 74 if (user_groupslen == -1) { 75 if (initgroups(pw->pw_name, pw->pw_gid) < 0) 76 fatal("initgroups: %s: %.100s", pw->pw_name, 77 strerror(errno)); 78 user_groupslen = getgroups(NGROUPS_MAX, user_groups); 79 if (user_groupslen < 0) 80 fatal("getgroups: %.100s", strerror(errno)); 81 } 82 /* Set the effective uid to the given (unprivileged) uid. */ 83 if (setgroups(user_groupslen, user_groups) < 0) 84 fatal("setgroups: %.100s", strerror(errno)); 85 if (setegid(pw->pw_gid) < 0) 86 fatal("setegid %u: %.100s", (u_int)pw->pw_gid, 87 strerror(errno)); 88 if (seteuid(pw->pw_uid) == -1) 89 fatal("seteuid %u: %.100s", (u_int)pw->pw_uid, 90 strerror(errno)); 91 } 92 93 /* 94 * Restores to the original (privileged) uid. 95 */ 96 void 97 restore_uid(void) 98 { 99 /* it's a no-op unless privileged */ 100 if (!privileged) { 101 debug("restore_uid: (unprivileged)"); 102 return; 103 } 104 if (!temporarily_use_uid_effective) 105 fatal("restore_uid: temporarily_use_uid not effective"); 106 debug("restore_uid: %u/%u", (u_int)saved_euid, (u_int)saved_egid); 107 /* Set the effective uid back to the saved privileged uid. */ 108 if (seteuid(saved_euid) < 0) 109 fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror(errno)); 110 if (setgroups(saved_egroupslen, saved_egroups) < 0) 111 fatal("setgroups: %.100s", strerror(errno)); 112 if (setegid(saved_egid) < 0) 113 fatal("setegid %u: %.100s", (u_int)saved_egid, strerror(errno)); 114 temporarily_use_uid_effective = 0; 115 } 116 117 /* 118 * Permanently sets all uids to the given uid. This cannot be 119 * called while temporarily_use_uid is effective. 120 */ 121 void 122 permanently_set_uid(struct passwd *pw) 123 { 124 if (pw == NULL) 125 fatal("permanently_set_uid: no user given"); 126 if (temporarily_use_uid_effective) 127 fatal("permanently_set_uid: temporarily_use_uid effective"); 128 debug("permanently_set_uid: %u/%u", (u_int)pw->pw_uid, 129 (u_int)pw->pw_gid); 130 131 if (setgid(pw->pw_gid) < 0) 132 fatal("setgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno)); 133 if (setegid(pw->pw_gid) < 0) 134 fatal("setegid %u: %.100s", (u_int)pw->pw_gid, strerror(errno)); 135 136 if (setuid(pw->pw_uid) < 0) 137 fatal("setuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno)); 138 if (seteuid(pw->pw_uid) < 0) 139 fatal("seteuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno)); 140 141 /* Verify GID drop was successful */ 142 if (getgid() != pw->pw_gid || getegid() != pw->pw_gid) { 143 fatal("%s: egid incorrect gid:%u egid:%u (should be %u)", 144 __func__, (u_int)getgid(), (u_int)getegid(), 145 (u_int)pw->pw_gid); 146 } 147 148 /* Verify UID drop was successful */ 149 if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) { 150 fatal("%s: euid incorrect uid:%u euid:%u (should be %u)", 151 __func__, (u_int)getuid(), (u_int)geteuid(), 152 (u_int)pw->pw_uid); 153 } 154 } 155 156 void 157 permanently_drop_suid(uid_t uid) 158 { 159 debug("permanently_drop_suid: %u", (u_int)uid); 160 if (seteuid(uid) < 0) 161 fatal("seteuid %u: %.100s", (u_int)uid, strerror(errno)); 162 if (setuid(uid) < 0) 163 fatal("setuid %u: %.100s", (u_int)uid, strerror(errno)); 164 165 /* Verify UID drop was successful */ 166 if (getuid() != uid || geteuid() != uid) { 167 fatal("%s: euid incorrect uid:%u euid:%u (should be %u)", 168 __func__, (u_int)getuid(), (u_int)geteuid(), (u_int)uid); 169 } 170 } 171