17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Author: Tatu Ylonen <ylo@cs.hut.fi>
37c478bd9Sstevel@tonic-gate * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
47c478bd9Sstevel@tonic-gate * All rights reserved
57c478bd9Sstevel@tonic-gate * Code for uid-swapping.
67c478bd9Sstevel@tonic-gate *
77c478bd9Sstevel@tonic-gate * As far as I am concerned, the code I have written for this software
87c478bd9Sstevel@tonic-gate * can be used freely for any purpose. Any derived versions of this
97c478bd9Sstevel@tonic-gate * software must be clearly marked as such, and if the derived work is
107c478bd9Sstevel@tonic-gate * incompatible with the protocol description in the RFC file, it must be
117c478bd9Sstevel@tonic-gate * called by a name other than "ssh" or "Secure Shell".
127c478bd9Sstevel@tonic-gate */
13743541abSjp161948 /*
146f8d59d8SJan Pechanec * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
15743541abSjp161948 * Use is subject to license terms.
16743541abSjp161948 */
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate #include "includes.h"
197c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: uidswap.c,v 1.23 2002/07/15 17:15:31 stevesk Exp $");
207c478bd9Sstevel@tonic-gate
216f8d59d8SJan Pechanec #include <priv.h>
227c478bd9Sstevel@tonic-gate
237c478bd9Sstevel@tonic-gate #include "log.h"
247c478bd9Sstevel@tonic-gate #include "uidswap.h"
25b07b2f5cSHuie-Ying Lee #include "buffer.h"
266f8d59d8SJan Pechanec #include "servconf.h"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate * Note: all these functions must work in all of the following cases:
307c478bd9Sstevel@tonic-gate * 1. euid=0, ruid=0
317c478bd9Sstevel@tonic-gate * 2. euid=0, ruid!=0
327c478bd9Sstevel@tonic-gate * 3. euid!=0, ruid!=0
337c478bd9Sstevel@tonic-gate * Additionally, they must work regardless of whether the system has
347c478bd9Sstevel@tonic-gate * POSIX saved uids or not.
357c478bd9Sstevel@tonic-gate */
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate #if defined(_POSIX_SAVED_IDS) && !defined(BROKEN_SAVED_UIDS)
387c478bd9Sstevel@tonic-gate /* Lets assume that posix saved ids also work with seteuid, even though that
397c478bd9Sstevel@tonic-gate is not part of the posix specification. */
407c478bd9Sstevel@tonic-gate #define SAVED_IDS_WORK
417c478bd9Sstevel@tonic-gate /* Saved effective uid. */
427c478bd9Sstevel@tonic-gate static uid_t saved_euid = 0;
437c478bd9Sstevel@tonic-gate static gid_t saved_egid = 0;
447c478bd9Sstevel@tonic-gate #endif
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate /* Saved effective uid. */
477c478bd9Sstevel@tonic-gate static int privileged = 0;
487c478bd9Sstevel@tonic-gate static int temporarily_use_uid_effective = 0;
49*67dbe2beSCasper H.S. Dik static int ngroups_max = -1;
50*67dbe2beSCasper H.S. Dik static gid_t *saved_egroups, *user_groups;
517c478bd9Sstevel@tonic-gate static int saved_egroupslen = -1, user_groupslen = -1;
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate * Temporarily changes to the given uid. If the effective user
557c478bd9Sstevel@tonic-gate * id is not root, this does nothing. This call cannot be nested.
567c478bd9Sstevel@tonic-gate */
577c478bd9Sstevel@tonic-gate void
temporarily_use_uid(struct passwd * pw)587c478bd9Sstevel@tonic-gate temporarily_use_uid(struct passwd *pw)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate /* Save the current euid, and egroups. */
617c478bd9Sstevel@tonic-gate #ifdef SAVED_IDS_WORK
627c478bd9Sstevel@tonic-gate saved_euid = geteuid();
637c478bd9Sstevel@tonic-gate saved_egid = getegid();
647c478bd9Sstevel@tonic-gate debug("temporarily_use_uid: %u/%u (e=%u/%u)",
657c478bd9Sstevel@tonic-gate (u_int)pw->pw_uid, (u_int)pw->pw_gid,
667c478bd9Sstevel@tonic-gate (u_int)saved_euid, (u_int)saved_egid);
677c478bd9Sstevel@tonic-gate if (saved_euid != 0) {
687c478bd9Sstevel@tonic-gate privileged = 0;
697c478bd9Sstevel@tonic-gate return;
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate #else
727c478bd9Sstevel@tonic-gate if (geteuid() != 0) {
737c478bd9Sstevel@tonic-gate privileged = 0;
747c478bd9Sstevel@tonic-gate return;
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate #endif /* SAVED_IDS_WORK */
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate privileged = 1;
797c478bd9Sstevel@tonic-gate temporarily_use_uid_effective = 1;
80*67dbe2beSCasper H.S. Dik
81*67dbe2beSCasper H.S. Dik if (ngroups_max < 0) {
82*67dbe2beSCasper H.S. Dik ngroups_max = sysconf(_SC_NGROUPS_MAX);
83*67dbe2beSCasper H.S. Dik saved_egroups = malloc(ngroups_max * sizeof (gid_t));
84*67dbe2beSCasper H.S. Dik user_groups = malloc(ngroups_max * sizeof (gid_t));
85*67dbe2beSCasper H.S. Dik if (saved_egroups == NULL || user_groups == NULL)
86*67dbe2beSCasper H.S. Dik fatal("malloc(gid array): %.100s", strerror(errno));
87*67dbe2beSCasper H.S. Dik }
88*67dbe2beSCasper H.S. Dik
89*67dbe2beSCasper H.S. Dik saved_egroupslen = getgroups(ngroups_max, saved_egroups);
907c478bd9Sstevel@tonic-gate if (saved_egroupslen < 0)
917c478bd9Sstevel@tonic-gate fatal("getgroups: %.100s", strerror(errno));
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate /* set and save the user's groups */
947c478bd9Sstevel@tonic-gate if (user_groupslen == -1) {
957c478bd9Sstevel@tonic-gate if (initgroups(pw->pw_name, pw->pw_gid) < 0)
967c478bd9Sstevel@tonic-gate fatal("initgroups: %s: %.100s", pw->pw_name,
977c478bd9Sstevel@tonic-gate strerror(errno));
98*67dbe2beSCasper H.S. Dik user_groupslen = getgroups(ngroups_max, user_groups);
997c478bd9Sstevel@tonic-gate if (user_groupslen < 0)
1007c478bd9Sstevel@tonic-gate fatal("getgroups: %.100s", strerror(errno));
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate /* Set the effective uid to the given (unprivileged) uid. */
1037c478bd9Sstevel@tonic-gate if (setgroups(user_groupslen, user_groups) < 0)
1047c478bd9Sstevel@tonic-gate fatal("setgroups: %.100s", strerror(errno));
1057c478bd9Sstevel@tonic-gate #ifdef SAVED_IDS_WORK
1067c478bd9Sstevel@tonic-gate /* Set saved gid and set real gid */
1077c478bd9Sstevel@tonic-gate if (setregid(pw->pw_gid, -1) == -1)
1087c478bd9Sstevel@tonic-gate debug("setregid(%u, -1): %.100s", (uint_t)pw->pw_gid, strerror(errno));
1097c478bd9Sstevel@tonic-gate /* Set saved uid and set real uid */
1107c478bd9Sstevel@tonic-gate if (setreuid(pw->pw_uid, -1) == -1)
1117c478bd9Sstevel@tonic-gate debug("setreuid(%u, -1): %.100s", (uint_t)pw->pw_uid, strerror(errno));
1127c478bd9Sstevel@tonic-gate #else
1137c478bd9Sstevel@tonic-gate /* Propagate the privileged gid to all of our gids. */
1147c478bd9Sstevel@tonic-gate if (setgid(getegid()) < 0)
1157c478bd9Sstevel@tonic-gate debug("setgid %u: %.100s", (u_int) getegid(), strerror(errno));
1167c478bd9Sstevel@tonic-gate /* Propagate the privileged uid to all of our uids. */
1177c478bd9Sstevel@tonic-gate if (setuid(geteuid()) < 0)
1187c478bd9Sstevel@tonic-gate debug("setuid %u: %.100s", (u_int) geteuid(), strerror(errno));
1197c478bd9Sstevel@tonic-gate #endif /* SAVED_IDS_WORK */
1207c478bd9Sstevel@tonic-gate /* Set effective gid */
1217c478bd9Sstevel@tonic-gate if (setegid(pw->pw_gid) == -1)
1227c478bd9Sstevel@tonic-gate fatal("setegid %u: %.100s", (u_int)pw->pw_uid,
1237c478bd9Sstevel@tonic-gate strerror(errno));
1247c478bd9Sstevel@tonic-gate /* Set effective uid */
1257c478bd9Sstevel@tonic-gate if (seteuid(pw->pw_uid) == -1)
1267c478bd9Sstevel@tonic-gate fatal("seteuid %u: %.100s", (u_int)pw->pw_uid,
1277c478bd9Sstevel@tonic-gate strerror(errno));
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate * If saved set ids work then
1307c478bd9Sstevel@tonic-gate *
1317c478bd9Sstevel@tonic-gate * ruid == euid == pw->pw_uid
1327c478bd9Sstevel@tonic-gate * saved uid = previous euid
1337c478bd9Sstevel@tonic-gate * rgid == egid == pw->pw_gid
1347c478bd9Sstevel@tonic-gate * saved gid = previous egid
1357c478bd9Sstevel@tonic-gate */
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate * Restores to the original (privileged) uid.
1407c478bd9Sstevel@tonic-gate */
1417c478bd9Sstevel@tonic-gate void
restore_uid(void)1427c478bd9Sstevel@tonic-gate restore_uid(void)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate /* it's a no-op unless privileged */
1457c478bd9Sstevel@tonic-gate if (!privileged) {
1467c478bd9Sstevel@tonic-gate debug("restore_uid: (unprivileged)");
1477c478bd9Sstevel@tonic-gate return;
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate if (!temporarily_use_uid_effective)
1507c478bd9Sstevel@tonic-gate fatal("restore_uid: temporarily_use_uid not effective");
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate #ifdef SAVED_IDS_WORK
1537c478bd9Sstevel@tonic-gate debug("restore_uid: %u/%u", (u_int)saved_euid, (u_int)saved_egid);
1547c478bd9Sstevel@tonic-gate /* Set the effective uid back to the saved privileged uid. */
1557c478bd9Sstevel@tonic-gate if (seteuid(saved_euid) < 0)
1567c478bd9Sstevel@tonic-gate fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror(errno));
1577c478bd9Sstevel@tonic-gate if (setuid(saved_euid) < 0)
1587c478bd9Sstevel@tonic-gate fatal("setuid %u: %.100s", (u_int)saved_euid, strerror(errno));
1597c478bd9Sstevel@tonic-gate if (setegid(saved_egid) < 0)
1607c478bd9Sstevel@tonic-gate fatal("setegid %u: %.100s", (u_int)saved_egid, strerror(errno));
1617c478bd9Sstevel@tonic-gate if (setgid(saved_egid) < 0)
1627c478bd9Sstevel@tonic-gate fatal("setgid %u: %.100s", (u_int)saved_egid, strerror(errno));
1637c478bd9Sstevel@tonic-gate #else /* SAVED_IDS_WORK */
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate * We are unable to restore the real uid to its unprivileged value.
1667c478bd9Sstevel@tonic-gate * Propagate the real uid (usually more privileged) to effective uid
1677c478bd9Sstevel@tonic-gate * as well.
1687c478bd9Sstevel@tonic-gate */
1697c478bd9Sstevel@tonic-gate setuid(getuid());
1707c478bd9Sstevel@tonic-gate setgid(getgid());
1717c478bd9Sstevel@tonic-gate #endif /* SAVED_IDS_WORK */
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate if (setgroups(saved_egroupslen, saved_egroups) < 0)
1747c478bd9Sstevel@tonic-gate fatal("setgroups: %.100s", strerror(errno));
1757c478bd9Sstevel@tonic-gate temporarily_use_uid_effective = 0;
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate /*
1796f8d59d8SJan Pechanec * Permanently sets all uids to the given uid. This cannot be called while
1806f8d59d8SJan Pechanec * temporarily_use_uid is effective. Note that when the ChrootDirectory option
1816f8d59d8SJan Pechanec * is in use we keep a few privileges so that we can call chroot(2) later while
1826f8d59d8SJan Pechanec * already running under UIDs of a connecting user.
1837c478bd9Sstevel@tonic-gate */
1847c478bd9Sstevel@tonic-gate void
permanently_set_uid(struct passwd * pw,char * chroot_directory)1856f8d59d8SJan Pechanec permanently_set_uid(struct passwd *pw, char *chroot_directory)
1867c478bd9Sstevel@tonic-gate {
1876f8d59d8SJan Pechanec priv_set_t *pset;
1886f8d59d8SJan Pechanec
1897c478bd9Sstevel@tonic-gate if (temporarily_use_uid_effective)
1906f8d59d8SJan Pechanec fatal("%s: temporarily_use_uid effective", __func__);
1916f8d59d8SJan Pechanec
1926f8d59d8SJan Pechanec debug("%s: %u/%u", __func__, (u_int)pw->pw_uid, (u_int)pw->pw_gid);
1936f8d59d8SJan Pechanec
1947c478bd9Sstevel@tonic-gate if (initgroups(pw->pw_name, pw->pw_gid) < 0)
1957c478bd9Sstevel@tonic-gate fatal("initgroups: %s: %.100s", pw->pw_name,
1967c478bd9Sstevel@tonic-gate strerror(errno));
1976f8d59d8SJan Pechanec
1987c478bd9Sstevel@tonic-gate if (setgid(pw->pw_gid) < 0)
1997c478bd9Sstevel@tonic-gate fatal("setgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
2006f8d59d8SJan Pechanec
2016f8d59d8SJan Pechanec /*
2026f8d59d8SJan Pechanec * If root is connecting we are done now. Note that we must have called
2036f8d59d8SJan Pechanec * setgid() in case that the SSH server was run under a group other than
2046f8d59d8SJan Pechanec * root.
2056f8d59d8SJan Pechanec */
2066f8d59d8SJan Pechanec if (pw->pw_uid == 0)
2076f8d59d8SJan Pechanec return;
2086f8d59d8SJan Pechanec
2096f8d59d8SJan Pechanec /*
2106f8d59d8SJan Pechanec * This means we will keep all privileges after the UID change.
2116f8d59d8SJan Pechanec */
2126f8d59d8SJan Pechanec if (setpflags(PRIV_AWARE, 1) != 0)
2136f8d59d8SJan Pechanec fatal("setpflags: %s", strerror(errno));
2146f8d59d8SJan Pechanec
2156f8d59d8SJan Pechanec /* Now we are running under UID of the user. */
2167c478bd9Sstevel@tonic-gate if (setuid(pw->pw_uid) < 0)
2177c478bd9Sstevel@tonic-gate fatal("setuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
2186f8d59d8SJan Pechanec
2196f8d59d8SJan Pechanec /*
2206f8d59d8SJan Pechanec * We will run with the privileges from the Inheritable set as
2216f8d59d8SJan Pechanec * we would have after exec(2) if we had stayed in NPA mode
2226f8d59d8SJan Pechanec * before setuid(2) call (see privileges(5), user_attr(4), and
2236f8d59d8SJan Pechanec * pam_unix_cred(5)). We want to run with P = E = I, with I as
2246f8d59d8SJan Pechanec * set by pam_unix_cred(5). We also add PRIV_PROC_CHROOT,
2256f8d59d8SJan Pechanec * obviously, and then PRIV_PROC_FORK and PRIV_PROC_EXEC, since
2266f8d59d8SJan Pechanec * those two might have been removed from the I set. Note that
2276f8d59d8SJan Pechanec * we are expected to finish the login process without them in
2286f8d59d8SJan Pechanec * the I set, the important thing is that those not be passed on
2296f8d59d8SJan Pechanec * to a shell or a subsystem later if they were not set in
2306f8d59d8SJan Pechanec * pam_unix_cred(5).
2316f8d59d8SJan Pechanec */
2326f8d59d8SJan Pechanec if ((pset = priv_allocset()) == NULL)
2336f8d59d8SJan Pechanec fatal("priv_allocset: %s", strerror(errno));
2346f8d59d8SJan Pechanec if (getppriv(PRIV_INHERITABLE, pset) != 0)
2356f8d59d8SJan Pechanec fatal("getppriv: %s", strerror(errno));
2366f8d59d8SJan Pechanec
2376f8d59d8SJan Pechanec /* We do not need PRIV_PROC_CHROOT unless chroot()ing. */
2386f8d59d8SJan Pechanec if (chroot_requested(chroot_directory) &&
2396f8d59d8SJan Pechanec priv_addset(pset, PRIV_PROC_CHROOT) == -1) {
2406f8d59d8SJan Pechanec fatal("%s: priv_addset failed", __func__);
2416f8d59d8SJan Pechanec }
2426f8d59d8SJan Pechanec
2436f8d59d8SJan Pechanec if (priv_addset(pset, PRIV_PROC_FORK) == -1 ||
2446f8d59d8SJan Pechanec priv_addset(pset, PRIV_PROC_EXEC) == -1) {
2456f8d59d8SJan Pechanec fatal("%s: priv_addset failed", __func__);
2466f8d59d8SJan Pechanec }
2476f8d59d8SJan Pechanec
2486f8d59d8SJan Pechanec /* Set only P; this will also set E. */
2496f8d59d8SJan Pechanec if (setppriv(PRIV_SET, PRIV_PERMITTED, pset) == -1)
2506f8d59d8SJan Pechanec fatal("setppriv: %s", strerror(errno));
2516f8d59d8SJan Pechanec
2526f8d59d8SJan Pechanec /* We don't need the PA flag anymore. */
2536f8d59d8SJan Pechanec if (setpflags(PRIV_AWARE, 0) == -1)
2546f8d59d8SJan Pechanec fatal("setpflags: %s", strerror(errno));
2556f8d59d8SJan Pechanec
2566f8d59d8SJan Pechanec priv_freeset(pset);
2577c478bd9Sstevel@tonic-gate }
258