10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * Author: Tatu Ylonen <ylo@cs.hut.fi> 30Sstevel@tonic-gate * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 40Sstevel@tonic-gate * All rights reserved 50Sstevel@tonic-gate * Code for uid-swapping. 60Sstevel@tonic-gate * 70Sstevel@tonic-gate * As far as I am concerned, the code I have written for this software 80Sstevel@tonic-gate * can be used freely for any purpose. Any derived versions of this 90Sstevel@tonic-gate * software must be clearly marked as such, and if the derived work is 100Sstevel@tonic-gate * incompatible with the protocol description in the RFC file, it must be 110Sstevel@tonic-gate * called by a name other than "ssh" or "Secure Shell". 120Sstevel@tonic-gate */ 135600Sjp161948 /* 14*9139SJan.Pechanec@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 155600Sjp161948 * Use is subject to license terms. 165600Sjp161948 */ 170Sstevel@tonic-gate 180Sstevel@tonic-gate #include "includes.h" 190Sstevel@tonic-gate RCSID("$OpenBSD: uidswap.c,v 1.23 2002/07/15 17:15:31 stevesk Exp $"); 200Sstevel@tonic-gate 21*9139SJan.Pechanec@Sun.COM #include <priv.h> 220Sstevel@tonic-gate 230Sstevel@tonic-gate #include "log.h" 240Sstevel@tonic-gate #include "uidswap.h" 25*9139SJan.Pechanec@Sun.COM #include "servconf.h" 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* 280Sstevel@tonic-gate * Note: all these functions must work in all of the following cases: 290Sstevel@tonic-gate * 1. euid=0, ruid=0 300Sstevel@tonic-gate * 2. euid=0, ruid!=0 310Sstevel@tonic-gate * 3. euid!=0, ruid!=0 320Sstevel@tonic-gate * Additionally, they must work regardless of whether the system has 330Sstevel@tonic-gate * POSIX saved uids or not. 340Sstevel@tonic-gate */ 350Sstevel@tonic-gate 360Sstevel@tonic-gate #if defined(_POSIX_SAVED_IDS) && !defined(BROKEN_SAVED_UIDS) 370Sstevel@tonic-gate /* Lets assume that posix saved ids also work with seteuid, even though that 380Sstevel@tonic-gate is not part of the posix specification. */ 390Sstevel@tonic-gate #define SAVED_IDS_WORK 400Sstevel@tonic-gate /* Saved effective uid. */ 410Sstevel@tonic-gate static uid_t saved_euid = 0; 420Sstevel@tonic-gate static gid_t saved_egid = 0; 430Sstevel@tonic-gate #endif 440Sstevel@tonic-gate 450Sstevel@tonic-gate /* Saved effective uid. */ 460Sstevel@tonic-gate static int privileged = 0; 470Sstevel@tonic-gate static int temporarily_use_uid_effective = 0; 485600Sjp161948 static gid_t saved_egroups[NGROUPS_UMAX], user_groups[NGROUPS_UMAX]; 490Sstevel@tonic-gate static int saved_egroupslen = -1, user_groupslen = -1; 500Sstevel@tonic-gate 510Sstevel@tonic-gate /* 520Sstevel@tonic-gate * Temporarily changes to the given uid. If the effective user 530Sstevel@tonic-gate * id is not root, this does nothing. This call cannot be nested. 540Sstevel@tonic-gate */ 550Sstevel@tonic-gate void 560Sstevel@tonic-gate temporarily_use_uid(struct passwd *pw) 570Sstevel@tonic-gate { 580Sstevel@tonic-gate /* Save the current euid, and egroups. */ 590Sstevel@tonic-gate #ifdef SAVED_IDS_WORK 600Sstevel@tonic-gate saved_euid = geteuid(); 610Sstevel@tonic-gate saved_egid = getegid(); 620Sstevel@tonic-gate debug("temporarily_use_uid: %u/%u (e=%u/%u)", 630Sstevel@tonic-gate (u_int)pw->pw_uid, (u_int)pw->pw_gid, 640Sstevel@tonic-gate (u_int)saved_euid, (u_int)saved_egid); 650Sstevel@tonic-gate if (saved_euid != 0) { 660Sstevel@tonic-gate privileged = 0; 670Sstevel@tonic-gate return; 680Sstevel@tonic-gate } 690Sstevel@tonic-gate #else 700Sstevel@tonic-gate if (geteuid() != 0) { 710Sstevel@tonic-gate privileged = 0; 720Sstevel@tonic-gate return; 730Sstevel@tonic-gate } 740Sstevel@tonic-gate #endif /* SAVED_IDS_WORK */ 750Sstevel@tonic-gate 760Sstevel@tonic-gate privileged = 1; 770Sstevel@tonic-gate temporarily_use_uid_effective = 1; 785600Sjp161948 saved_egroupslen = getgroups(NGROUPS_UMAX, saved_egroups); 790Sstevel@tonic-gate if (saved_egroupslen < 0) 800Sstevel@tonic-gate fatal("getgroups: %.100s", strerror(errno)); 810Sstevel@tonic-gate 820Sstevel@tonic-gate /* set and save the user's groups */ 830Sstevel@tonic-gate if (user_groupslen == -1) { 840Sstevel@tonic-gate if (initgroups(pw->pw_name, pw->pw_gid) < 0) 850Sstevel@tonic-gate fatal("initgroups: %s: %.100s", pw->pw_name, 860Sstevel@tonic-gate strerror(errno)); 875600Sjp161948 user_groupslen = getgroups(NGROUPS_UMAX, user_groups); 880Sstevel@tonic-gate if (user_groupslen < 0) 890Sstevel@tonic-gate fatal("getgroups: %.100s", strerror(errno)); 900Sstevel@tonic-gate } 910Sstevel@tonic-gate /* Set the effective uid to the given (unprivileged) uid. */ 920Sstevel@tonic-gate if (setgroups(user_groupslen, user_groups) < 0) 930Sstevel@tonic-gate fatal("setgroups: %.100s", strerror(errno)); 940Sstevel@tonic-gate #ifdef SAVED_IDS_WORK 950Sstevel@tonic-gate /* Set saved gid and set real gid */ 960Sstevel@tonic-gate if (setregid(pw->pw_gid, -1) == -1) 970Sstevel@tonic-gate debug("setregid(%u, -1): %.100s", (uint_t)pw->pw_gid, strerror(errno)); 980Sstevel@tonic-gate /* Set saved uid and set real uid */ 990Sstevel@tonic-gate if (setreuid(pw->pw_uid, -1) == -1) 1000Sstevel@tonic-gate debug("setreuid(%u, -1): %.100s", (uint_t)pw->pw_uid, strerror(errno)); 1010Sstevel@tonic-gate #else 1020Sstevel@tonic-gate /* Propagate the privileged gid to all of our gids. */ 1030Sstevel@tonic-gate if (setgid(getegid()) < 0) 1040Sstevel@tonic-gate debug("setgid %u: %.100s", (u_int) getegid(), strerror(errno)); 1050Sstevel@tonic-gate /* Propagate the privileged uid to all of our uids. */ 1060Sstevel@tonic-gate if (setuid(geteuid()) < 0) 1070Sstevel@tonic-gate debug("setuid %u: %.100s", (u_int) geteuid(), strerror(errno)); 1080Sstevel@tonic-gate #endif /* SAVED_IDS_WORK */ 1090Sstevel@tonic-gate /* Set effective gid */ 1100Sstevel@tonic-gate if (setegid(pw->pw_gid) == -1) 1110Sstevel@tonic-gate fatal("setegid %u: %.100s", (u_int)pw->pw_uid, 1120Sstevel@tonic-gate strerror(errno)); 1130Sstevel@tonic-gate /* Set effective uid */ 1140Sstevel@tonic-gate if (seteuid(pw->pw_uid) == -1) 1150Sstevel@tonic-gate fatal("seteuid %u: %.100s", (u_int)pw->pw_uid, 1160Sstevel@tonic-gate strerror(errno)); 1170Sstevel@tonic-gate /* 1180Sstevel@tonic-gate * If saved set ids work then 1190Sstevel@tonic-gate * 1200Sstevel@tonic-gate * ruid == euid == pw->pw_uid 1210Sstevel@tonic-gate * saved uid = previous euid 1220Sstevel@tonic-gate * rgid == egid == pw->pw_gid 1230Sstevel@tonic-gate * saved gid = previous egid 1240Sstevel@tonic-gate */ 1250Sstevel@tonic-gate } 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate /* 1280Sstevel@tonic-gate * Restores to the original (privileged) uid. 1290Sstevel@tonic-gate */ 1300Sstevel@tonic-gate void 1310Sstevel@tonic-gate restore_uid(void) 1320Sstevel@tonic-gate { 1330Sstevel@tonic-gate /* it's a no-op unless privileged */ 1340Sstevel@tonic-gate if (!privileged) { 1350Sstevel@tonic-gate debug("restore_uid: (unprivileged)"); 1360Sstevel@tonic-gate return; 1370Sstevel@tonic-gate } 1380Sstevel@tonic-gate if (!temporarily_use_uid_effective) 1390Sstevel@tonic-gate fatal("restore_uid: temporarily_use_uid not effective"); 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate #ifdef SAVED_IDS_WORK 1420Sstevel@tonic-gate debug("restore_uid: %u/%u", (u_int)saved_euid, (u_int)saved_egid); 1430Sstevel@tonic-gate /* Set the effective uid back to the saved privileged uid. */ 1440Sstevel@tonic-gate if (seteuid(saved_euid) < 0) 1450Sstevel@tonic-gate fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror(errno)); 1460Sstevel@tonic-gate if (setuid(saved_euid) < 0) 1470Sstevel@tonic-gate fatal("setuid %u: %.100s", (u_int)saved_euid, strerror(errno)); 1480Sstevel@tonic-gate if (setegid(saved_egid) < 0) 1490Sstevel@tonic-gate fatal("setegid %u: %.100s", (u_int)saved_egid, strerror(errno)); 1500Sstevel@tonic-gate if (setgid(saved_egid) < 0) 1510Sstevel@tonic-gate fatal("setgid %u: %.100s", (u_int)saved_egid, strerror(errno)); 1520Sstevel@tonic-gate #else /* SAVED_IDS_WORK */ 1530Sstevel@tonic-gate /* 1540Sstevel@tonic-gate * We are unable to restore the real uid to its unprivileged value. 1550Sstevel@tonic-gate * Propagate the real uid (usually more privileged) to effective uid 1560Sstevel@tonic-gate * as well. 1570Sstevel@tonic-gate */ 1580Sstevel@tonic-gate setuid(getuid()); 1590Sstevel@tonic-gate setgid(getgid()); 1600Sstevel@tonic-gate #endif /* SAVED_IDS_WORK */ 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate if (setgroups(saved_egroupslen, saved_egroups) < 0) 1630Sstevel@tonic-gate fatal("setgroups: %.100s", strerror(errno)); 1640Sstevel@tonic-gate temporarily_use_uid_effective = 0; 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate /* 168*9139SJan.Pechanec@Sun.COM * Permanently sets all uids to the given uid. This cannot be called while 169*9139SJan.Pechanec@Sun.COM * temporarily_use_uid is effective. Note that when the ChrootDirectory option 170*9139SJan.Pechanec@Sun.COM * is in use we keep a few privileges so that we can call chroot(2) later while 171*9139SJan.Pechanec@Sun.COM * already running under UIDs of a connecting user. 1720Sstevel@tonic-gate */ 1730Sstevel@tonic-gate void 174*9139SJan.Pechanec@Sun.COM permanently_set_uid(struct passwd *pw, char *chroot_directory) 1750Sstevel@tonic-gate { 176*9139SJan.Pechanec@Sun.COM priv_set_t *pset; 177*9139SJan.Pechanec@Sun.COM 1780Sstevel@tonic-gate if (temporarily_use_uid_effective) 179*9139SJan.Pechanec@Sun.COM fatal("%s: temporarily_use_uid effective", __func__); 180*9139SJan.Pechanec@Sun.COM 181*9139SJan.Pechanec@Sun.COM debug("%s: %u/%u", __func__, (u_int)pw->pw_uid, (u_int)pw->pw_gid); 182*9139SJan.Pechanec@Sun.COM 1830Sstevel@tonic-gate if (initgroups(pw->pw_name, pw->pw_gid) < 0) 1840Sstevel@tonic-gate fatal("initgroups: %s: %.100s", pw->pw_name, 1850Sstevel@tonic-gate strerror(errno)); 186*9139SJan.Pechanec@Sun.COM 1870Sstevel@tonic-gate if (setgid(pw->pw_gid) < 0) 1880Sstevel@tonic-gate fatal("setgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno)); 189*9139SJan.Pechanec@Sun.COM 190*9139SJan.Pechanec@Sun.COM /* 191*9139SJan.Pechanec@Sun.COM * If root is connecting we are done now. Note that we must have called 192*9139SJan.Pechanec@Sun.COM * setgid() in case that the SSH server was run under a group other than 193*9139SJan.Pechanec@Sun.COM * root. 194*9139SJan.Pechanec@Sun.COM */ 195*9139SJan.Pechanec@Sun.COM if (pw->pw_uid == 0) 196*9139SJan.Pechanec@Sun.COM return; 197*9139SJan.Pechanec@Sun.COM 198*9139SJan.Pechanec@Sun.COM /* 199*9139SJan.Pechanec@Sun.COM * This means we will keep all privileges after the UID change. 200*9139SJan.Pechanec@Sun.COM */ 201*9139SJan.Pechanec@Sun.COM if (setpflags(PRIV_AWARE, 1) != 0) 202*9139SJan.Pechanec@Sun.COM fatal("setpflags: %s", strerror(errno)); 203*9139SJan.Pechanec@Sun.COM 204*9139SJan.Pechanec@Sun.COM /* Now we are running under UID of the user. */ 2050Sstevel@tonic-gate if (setuid(pw->pw_uid) < 0) 2060Sstevel@tonic-gate fatal("setuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno)); 207*9139SJan.Pechanec@Sun.COM 208*9139SJan.Pechanec@Sun.COM /* 209*9139SJan.Pechanec@Sun.COM * We will run with the privileges from the Inheritable set as 210*9139SJan.Pechanec@Sun.COM * we would have after exec(2) if we had stayed in NPA mode 211*9139SJan.Pechanec@Sun.COM * before setuid(2) call (see privileges(5), user_attr(4), and 212*9139SJan.Pechanec@Sun.COM * pam_unix_cred(5)). We want to run with P = E = I, with I as 213*9139SJan.Pechanec@Sun.COM * set by pam_unix_cred(5). We also add PRIV_PROC_CHROOT, 214*9139SJan.Pechanec@Sun.COM * obviously, and then PRIV_PROC_FORK and PRIV_PROC_EXEC, since 215*9139SJan.Pechanec@Sun.COM * those two might have been removed from the I set. Note that 216*9139SJan.Pechanec@Sun.COM * we are expected to finish the login process without them in 217*9139SJan.Pechanec@Sun.COM * the I set, the important thing is that those not be passed on 218*9139SJan.Pechanec@Sun.COM * to a shell or a subsystem later if they were not set in 219*9139SJan.Pechanec@Sun.COM * pam_unix_cred(5). 220*9139SJan.Pechanec@Sun.COM */ 221*9139SJan.Pechanec@Sun.COM if ((pset = priv_allocset()) == NULL) 222*9139SJan.Pechanec@Sun.COM fatal("priv_allocset: %s", strerror(errno)); 223*9139SJan.Pechanec@Sun.COM if (getppriv(PRIV_INHERITABLE, pset) != 0) 224*9139SJan.Pechanec@Sun.COM fatal("getppriv: %s", strerror(errno)); 225*9139SJan.Pechanec@Sun.COM 226*9139SJan.Pechanec@Sun.COM /* We do not need PRIV_PROC_CHROOT unless chroot()ing. */ 227*9139SJan.Pechanec@Sun.COM if (chroot_requested(chroot_directory) && 228*9139SJan.Pechanec@Sun.COM priv_addset(pset, PRIV_PROC_CHROOT) == -1) { 229*9139SJan.Pechanec@Sun.COM fatal("%s: priv_addset failed", __func__); 230*9139SJan.Pechanec@Sun.COM } 231*9139SJan.Pechanec@Sun.COM 232*9139SJan.Pechanec@Sun.COM if (priv_addset(pset, PRIV_PROC_FORK) == -1 || 233*9139SJan.Pechanec@Sun.COM priv_addset(pset, PRIV_PROC_EXEC) == -1) { 234*9139SJan.Pechanec@Sun.COM fatal("%s: priv_addset failed", __func__); 235*9139SJan.Pechanec@Sun.COM } 236*9139SJan.Pechanec@Sun.COM 237*9139SJan.Pechanec@Sun.COM /* Set only P; this will also set E. */ 238*9139SJan.Pechanec@Sun.COM if (setppriv(PRIV_SET, PRIV_PERMITTED, pset) == -1) 239*9139SJan.Pechanec@Sun.COM fatal("setppriv: %s", strerror(errno)); 240*9139SJan.Pechanec@Sun.COM 241*9139SJan.Pechanec@Sun.COM /* We don't need the PA flag anymore. */ 242*9139SJan.Pechanec@Sun.COM if (setpflags(PRIV_AWARE, 0) == -1) 243*9139SJan.Pechanec@Sun.COM fatal("setpflags: %s", strerror(errno)); 244*9139SJan.Pechanec@Sun.COM 245*9139SJan.Pechanec@Sun.COM priv_freeset(pset); 2460Sstevel@tonic-gate } 247