1 /*
   2  * Author: Tatu Ylonen <ylo@cs.hut.fi>
   3  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
   4  *                    All rights reserved
   5  * Code for uid-swapping.
   6  *
   7  * As far as I am concerned, the code I have written for this software
   8  * can be used freely for any purpose.  Any derived versions of this
   9  * software must be clearly marked as such, and if the derived work is
  10  * incompatible with the protocol description in the RFC file, it must be
  11  * called by a name other than "ssh" or "Secure Shell".
  12  */
  13 /*
  14  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  15  * Use is subject to license terms.
  16  */
  17 
  18 #include "includes.h"
  19 RCSID("$OpenBSD: uidswap.c,v 1.23 2002/07/15 17:15:31 stevesk Exp $");
  20 
  21 #include <priv.h>
  22 
  23 #include "log.h"
  24 #include "uidswap.h"
  25 #include "buffer.h"
  26 #include "servconf.h"
  27 
  28 /*
  29  * Note: all these functions must work in all of the following cases:
  30  *    1. euid=0, ruid=0
  31  *    2. euid=0, ruid!=0
  32  *    3. euid!=0, ruid!=0
  33  * Additionally, they must work regardless of whether the system has
  34  * POSIX saved uids or not.
  35  */
  36 
  37 #if defined(_POSIX_SAVED_IDS) && !defined(BROKEN_SAVED_UIDS)
  38 /* Lets assume that posix saved ids also work with seteuid, even though that
  39    is not part of the posix specification. */
  40 #define SAVED_IDS_WORK
  41 /* Saved effective uid. */
  42 static uid_t    saved_euid = 0;
  43 static gid_t    saved_egid = 0;
  44 #endif
  45 
  46 /* Saved effective uid. */
  47 static int      privileged = 0;
  48 static int      temporarily_use_uid_effective = 0;
  49 static int      ngroups_max = -1;
  50 static gid_t    *saved_egroups, *user_groups;
  51 static int      saved_egroupslen = -1, user_groupslen = -1;
  52 
  53 /*
  54  * Temporarily changes to the given uid.  If the effective user
  55  * id is not root, this does nothing.  This call cannot be nested.
  56  */
  57 void
  58 temporarily_use_uid(struct passwd *pw)
  59 {
  60         /* Save the current euid, and egroups. */
  61 #ifdef SAVED_IDS_WORK
  62         saved_euid = geteuid();
  63         saved_egid = getegid();
  64         debug("temporarily_use_uid: %u/%u (e=%u/%u)",
  65             (u_int)pw->pw_uid, (u_int)pw->pw_gid,
  66             (u_int)saved_euid, (u_int)saved_egid);
  67         if (saved_euid != 0) {
  68                 privileged = 0;
  69                 return;
  70         }
  71 #else
  72         if (geteuid() != 0) {
  73                 privileged = 0;
  74                 return;
  75         }
  76 #endif /* SAVED_IDS_WORK */
  77 
  78         privileged = 1;
  79         temporarily_use_uid_effective = 1;
  80 
  81         if (ngroups_max < 0) {
  82                 ngroups_max = sysconf(_SC_NGROUPS_MAX);
  83                 saved_egroups = malloc(ngroups_max * sizeof (gid_t));
  84                 user_groups = malloc(ngroups_max * sizeof (gid_t));
  85                 if (saved_egroups == NULL || user_groups == NULL)
  86                         fatal("malloc(gid array): %.100s", strerror(errno));
  87         }
  88 
  89         saved_egroupslen = getgroups(ngroups_max, saved_egroups);
  90         if (saved_egroupslen < 0)
  91                 fatal("getgroups: %.100s", strerror(errno));
  92 
  93         /* set and save the user's groups */
  94         if (user_groupslen == -1) {
  95                 if (initgroups(pw->pw_name, pw->pw_gid) < 0)
  96                         fatal("initgroups: %s: %.100s", pw->pw_name,
  97                             strerror(errno));
  98                 user_groupslen = getgroups(ngroups_max, user_groups);
  99                 if (user_groupslen < 0)
 100                         fatal("getgroups: %.100s", strerror(errno));
 101         }
 102         /* Set the effective uid to the given (unprivileged) uid. */
 103         if (setgroups(user_groupslen, user_groups) < 0)
 104                 fatal("setgroups: %.100s", strerror(errno));
 105 #ifdef SAVED_IDS_WORK
 106         /* Set saved gid and set real gid */
 107         if (setregid(pw->pw_gid, -1) == -1)
 108                 debug("setregid(%u, -1): %.100s", (uint_t)pw->pw_gid, strerror(errno));
 109         /* Set saved uid and set real uid */
 110         if (setreuid(pw->pw_uid, -1) == -1)
 111                 debug("setreuid(%u, -1): %.100s", (uint_t)pw->pw_uid, strerror(errno));
 112 #else
 113         /* Propagate the privileged gid to all of our gids. */
 114         if (setgid(getegid()) < 0)
 115                 debug("setgid %u: %.100s", (u_int) getegid(), strerror(errno));
 116         /* Propagate the privileged uid to all of our uids. */
 117         if (setuid(geteuid()) < 0)
 118                 debug("setuid %u: %.100s", (u_int) geteuid(), strerror(errno));
 119 #endif /* SAVED_IDS_WORK */
 120         /* Set effective gid */
 121         if (setegid(pw->pw_gid) == -1)
 122                 fatal("setegid %u: %.100s", (u_int)pw->pw_uid,
 123                     strerror(errno));
 124         /* Set effective uid */
 125         if (seteuid(pw->pw_uid) == -1)
 126                 fatal("seteuid %u: %.100s", (u_int)pw->pw_uid,
 127                     strerror(errno));
 128         /*
 129          * If saved set ids work then
 130          *
 131          *      ruid == euid == pw->pw_uid
 132          *      saved uid = previous euid
 133          *      rgid == egid == pw->pw_gid
 134          *      saved gid = previous egid
 135          */
 136 }
 137 
 138 /*
 139  * Restores to the original (privileged) uid.
 140  */
 141 void
 142 restore_uid(void)
 143 {
 144         /* it's a no-op unless privileged */
 145         if (!privileged) {
 146                 debug("restore_uid: (unprivileged)");
 147                 return;
 148         }
 149         if (!temporarily_use_uid_effective)
 150                 fatal("restore_uid: temporarily_use_uid not effective");
 151 
 152 #ifdef SAVED_IDS_WORK
 153         debug("restore_uid: %u/%u", (u_int)saved_euid, (u_int)saved_egid);
 154         /* Set the effective uid back to the saved privileged uid. */
 155         if (seteuid(saved_euid) < 0)
 156                 fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror(errno));
 157         if (setuid(saved_euid) < 0)
 158                 fatal("setuid %u: %.100s", (u_int)saved_euid, strerror(errno));
 159         if (setegid(saved_egid) < 0)
 160                 fatal("setegid %u: %.100s", (u_int)saved_egid, strerror(errno));
 161         if (setgid(saved_egid) < 0)
 162                 fatal("setgid %u: %.100s", (u_int)saved_egid, strerror(errno));
 163 #else /* SAVED_IDS_WORK */
 164         /*
 165          * We are unable to restore the real uid to its unprivileged value.
 166          * Propagate the real uid (usually more privileged) to effective uid
 167          * as well.
 168          */
 169         setuid(getuid());
 170         setgid(getgid());
 171 #endif /* SAVED_IDS_WORK */
 172 
 173         if (setgroups(saved_egroupslen, saved_egroups) < 0)
 174                 fatal("setgroups: %.100s", strerror(errno));
 175         temporarily_use_uid_effective = 0;
 176 }
 177 
 178 /*
 179  * Permanently sets all uids to the given uid. This cannot be called while
 180  * temporarily_use_uid is effective. Note that when the ChrootDirectory option
 181  * is in use we keep a few privileges so that we can call chroot(2) later while
 182  * already running under UIDs of a connecting user.
 183  */
 184 void
 185 permanently_set_uid(struct passwd *pw, char *chroot_directory)
 186 {
 187         priv_set_t *pset;
 188 
 189         if (temporarily_use_uid_effective)
 190                 fatal("%s: temporarily_use_uid effective", __func__);
 191 
 192         debug("%s: %u/%u", __func__, (u_int)pw->pw_uid, (u_int)pw->pw_gid);
 193 
 194         if (initgroups(pw->pw_name, pw->pw_gid) < 0)
 195                 fatal("initgroups: %s: %.100s", pw->pw_name,
 196                     strerror(errno));
 197 
 198         if (setgid(pw->pw_gid) < 0)
 199                 fatal("setgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
 200 
 201         /*
 202          * If root is connecting we are done now. Note that we must have called
 203          * setgid() in case that the SSH server was run under a group other than
 204          * root.
 205          */
 206         if (pw->pw_uid == 0)
 207                 return;
 208 
 209         /*
 210          * This means we will keep all privileges after the UID change.
 211          */
 212         if (setpflags(PRIV_AWARE, 1) != 0)
 213                 fatal("setpflags: %s", strerror(errno));
 214 
 215         /* Now we are running under UID of the user. */
 216         if (setuid(pw->pw_uid) < 0)
 217                 fatal("setuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
 218 
 219         /*
 220          * We will run with the privileges from the Inheritable set as
 221          * we would have after exec(2) if we had stayed in NPA mode
 222          * before setuid(2) call (see privileges(5), user_attr(4), and
 223          * pam_unix_cred(5)). We want to run with P = E = I, with I as
 224          * set by pam_unix_cred(5). We also add PRIV_PROC_CHROOT,
 225          * obviously, and then PRIV_PROC_FORK and PRIV_PROC_EXEC, since
 226          * those two might have been removed from the I set. Note that
 227          * we are expected to finish the login process without them in
 228          * the I set, the important thing is that those not be passed on
 229          * to a shell or a subsystem later if they were not set in
 230          * pam_unix_cred(5).
 231          */
 232         if ((pset = priv_allocset()) == NULL)
 233                 fatal("priv_allocset: %s", strerror(errno));
 234         if (getppriv(PRIV_INHERITABLE, pset) != 0)
 235                 fatal("getppriv: %s", strerror(errno));
 236 
 237         /* We do not need PRIV_PROC_CHROOT unless chroot()ing. */
 238         if (chroot_requested(chroot_directory) &&
 239             priv_addset(pset, PRIV_PROC_CHROOT) == -1) {
 240                 fatal("%s: priv_addset failed", __func__);
 241         }
 242 
 243         if (priv_addset(pset, PRIV_PROC_FORK) == -1 ||
 244             priv_addset(pset, PRIV_PROC_EXEC) == -1) {
 245                 fatal("%s: priv_addset failed", __func__);
 246         }
 247 
 248         /* Set only P; this will also set E. */
 249         if (setppriv(PRIV_SET, PRIV_PERMITTED, pset) == -1)
 250                 fatal("setppriv: %s", strerror(errno));
 251 
 252         /* We don't need the PA flag anymore. */
 253         if (setpflags(PRIV_AWARE, 0) == -1)
 254                 fatal("setpflags: %s", strerror(errno));
 255 
 256         priv_freeset(pset);
 257 }