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