uidswap.c revision 192595
136285Sbrian/* $OpenBSD: uidswap.c,v 1.35 2006/08/03 03:34:42 deraadt Exp $ */
236285Sbrian/*
336285Sbrian * Author: Tatu Ylonen <ylo@cs.hut.fi>
436285Sbrian * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
536285Sbrian *                    All rights reserved
636285Sbrian * Code for uid-swapping.
736285Sbrian *
836285Sbrian * As far as I am concerned, the code I have written for this software
936285Sbrian * can be used freely for any purpose.  Any derived versions of this
1036285Sbrian * software must be clearly marked as such, and if the derived work is
1136285Sbrian * incompatible with the protocol description in the RFC file, it must be
1236285Sbrian * called by a name other than "ssh" or "Secure Shell".
1336285Sbrian */
1436285Sbrian
1536285Sbrian#include "includes.h"
1636285Sbrian
1736285Sbrian#include <sys/param.h>
1836285Sbrian#include <errno.h>
1936285Sbrian#include <pwd.h>
2036285Sbrian#include <string.h>
2136285Sbrian#include <unistd.h>
2236285Sbrian#include <stdarg.h>
2336285Sbrian
2436285Sbrian#include <grp.h>
2536285Sbrian
2650479Speter#include "log.h"
2736285Sbrian#include "uidswap.h"
2836285Sbrian#include "xmalloc.h"
2936285Sbrian
3036285Sbrian/*
3136285Sbrian * Note: all these functions must work in all of the following cases:
3236285Sbrian *    1. euid=0, ruid=0
3336285Sbrian *    2. euid=0, ruid!=0
3436285Sbrian *    3. euid!=0, ruid!=0
3536285Sbrian * Additionally, they must work regardless of whether the system has
3636285Sbrian * POSIX saved uids or not.
3736285Sbrian */
3836285Sbrian
3936285Sbrian#if defined(_POSIX_SAVED_IDS) && !defined(BROKEN_SAVED_UIDS)
4036285Sbrian/* Lets assume that posix saved ids also work with seteuid, even though that
4136285Sbrian   is not part of the posix specification. */
4236285Sbrian#define SAVED_IDS_WORK_WITH_SETEUID
4336285Sbrian/* Saved effective uid. */
4446686Sbrianstatic uid_t 	saved_euid = 0;
4536285Sbrianstatic gid_t	saved_egid = 0;
4636285Sbrian#endif
4736285Sbrian
4836285Sbrian/* Saved effective uid. */
4936285Sbrianstatic int	privileged = 0;
5036285Sbrianstatic int	temporarily_use_uid_effective = 0;
5136285Sbrianstatic gid_t	*saved_egroups = NULL, *user_groups = NULL;
5236285Sbrianstatic int	saved_egroupslen = -1, user_groupslen = -1;
5336285Sbrian
5436285Sbrian/*
5536285Sbrian * Temporarily changes to the given uid.  If the effective user
5636285Sbrian * id is not root, this does nothing.  This call cannot be nested.
5738557Sbrian */
5838557Sbrianvoid
5938557Sbriantemporarily_use_uid(struct passwd *pw)
6036285Sbrian{
6136285Sbrian	/* Save the current euid, and egroups. */
6236285Sbrian#ifdef SAVED_IDS_WORK_WITH_SETEUID
6336285Sbrian	saved_euid = geteuid();
6436285Sbrian	saved_egid = getegid();
6536285Sbrian	debug("temporarily_use_uid: %u/%u (e=%u/%u)",
6636285Sbrian	    (u_int)pw->pw_uid, (u_int)pw->pw_gid,
6743313Sbrian	    (u_int)saved_euid, (u_int)saved_egid);
6843313Sbrian#ifndef HAVE_CYGWIN
6943313Sbrian	if (saved_euid != 0) {
7036285Sbrian		privileged = 0;
7136285Sbrian		return;
7236285Sbrian	}
7338174Sbrian#endif
7436285Sbrian#else
7536285Sbrian	if (geteuid() != 0) {
7637386Sbrian		privileged = 0;
7736285Sbrian		return;
7836285Sbrian	}
7936285Sbrian#endif /* SAVED_IDS_WORK_WITH_SETEUID */
8036285Sbrian
8137010Sbrian	privileged = 1;
8236285Sbrian	temporarily_use_uid_effective = 1;
8336285Sbrian
8436285Sbrian	saved_egroupslen = getgroups(0, NULL);
8536285Sbrian	if (saved_egroupslen < 0)
8636285Sbrian		fatal("getgroups: %.100s", strerror(errno));
8736285Sbrian	if (saved_egroupslen > 0) {
8836285Sbrian		saved_egroups = xrealloc(saved_egroups,
8936285Sbrian		    saved_egroupslen, sizeof(gid_t));
9036285Sbrian		if (getgroups(saved_egroupslen, saved_egroups) < 0)
9136285Sbrian			fatal("getgroups: %.100s", strerror(errno));
9236285Sbrian	} else { /* saved_egroupslen == 0 */
9336285Sbrian		if (saved_egroups != NULL)
9436285Sbrian			xfree(saved_egroups);
9536285Sbrian	}
9636285Sbrian
9736285Sbrian	/* set and save the user's groups */
9836285Sbrian	if (user_groupslen == -1) {
9936285Sbrian		if (initgroups(pw->pw_name, pw->pw_gid) < 0)
10036285Sbrian			fatal("initgroups: %s: %.100s", pw->pw_name,
10136285Sbrian			    strerror(errno));
10236285Sbrian
10336285Sbrian		user_groupslen = getgroups(0, NULL);
10436285Sbrian		if (user_groupslen < 0)
10536285Sbrian			fatal("getgroups: %.100s", strerror(errno));
10636285Sbrian		if (user_groupslen > 0) {
10736285Sbrian			user_groups = xrealloc(user_groups,
10836285Sbrian			    user_groupslen, sizeof(gid_t));
10936285Sbrian			if (getgroups(user_groupslen, user_groups) < 0)
11036285Sbrian				fatal("getgroups: %.100s", strerror(errno));
11136285Sbrian		} else { /* user_groupslen == 0 */
11236285Sbrian			if (user_groups)
11336285Sbrian				xfree(user_groups);
11436285Sbrian		}
11536285Sbrian	}
11636285Sbrian	/* Set the effective uid to the given (unprivileged) uid. */
11736285Sbrian	if (setgroups(user_groupslen, user_groups) < 0)
11836285Sbrian		fatal("setgroups: %.100s", strerror(errno));
11936285Sbrian#ifndef SAVED_IDS_WORK_WITH_SETEUID
12036285Sbrian	/* Propagate the privileged gid to all of our gids. */
12136285Sbrian	if (setgid(getegid()) < 0)
12236285Sbrian		debug("setgid %u: %.100s", (u_int) getegid(), strerror(errno));
12336285Sbrian	/* Propagate the privileged uid to all of our uids. */
12436285Sbrian	if (setuid(geteuid()) < 0)
12536285Sbrian		debug("setuid %u: %.100s", (u_int) geteuid(), strerror(errno));
12636285Sbrian#endif /* SAVED_IDS_WORK_WITH_SETEUID */
12736285Sbrian	if (setegid(pw->pw_gid) < 0)
12836285Sbrian		fatal("setegid %u: %.100s", (u_int)pw->pw_gid,
12936285Sbrian		    strerror(errno));
13036285Sbrian	if (seteuid(pw->pw_uid) == -1)
13136285Sbrian		fatal("seteuid %u: %.100s", (u_int)pw->pw_uid,
13236285Sbrian		    strerror(errno));
13336285Sbrian}
13436285Sbrian
13536285Sbrianvoid
13636285Sbrianpermanently_drop_suid(uid_t uid)
13736285Sbrian{
13836285Sbrian	uid_t old_uid = getuid();
13936285Sbrian
14036285Sbrian	debug("permanently_drop_suid: %u", (u_int)uid);
14136285Sbrian#if defined(HAVE_SETRESUID) && !defined(BROKEN_SETRESUID)
14236285Sbrian	if (setresuid(uid, uid, uid) < 0)
14336285Sbrian		fatal("setresuid %u: %.100s", (u_int)uid, strerror(errno));
14436285Sbrian#elif defined(HAVE_SETREUID) && !defined(BROKEN_SETREUID)
14536285Sbrian	if (setreuid(uid, uid) < 0)
14636285Sbrian		fatal("setreuid %u: %.100s", (u_int)uid, strerror(errno));
14736285Sbrian#else
14836285Sbrian# ifndef SETEUID_BREAKS_SETUID
14936285Sbrian	if (seteuid(uid) < 0)
15036285Sbrian		fatal("seteuid %u: %.100s", (u_int)uid, strerror(errno));
15136285Sbrian# endif
15236285Sbrian	if (setuid(uid) < 0)
15336285Sbrian		fatal("setuid %u: %.100s", (u_int)uid, strerror(errno));
15436285Sbrian#endif
15536285Sbrian
15636285Sbrian#ifndef HAVE_CYGWIN
15736285Sbrian	/* Try restoration of UID if changed (test clearing of saved uid) */
15836285Sbrian	if (old_uid != uid &&
15936285Sbrian	    (setuid(old_uid) != -1 || seteuid(old_uid) != -1))
16036285Sbrian		fatal("%s: was able to restore old [e]uid", __func__);
16136285Sbrian#endif
16236285Sbrian
16336285Sbrian	/* Verify UID drop was successful */
16437011Sbrian	if (getuid() != uid || geteuid() != uid) {
16537011Sbrian		fatal("%s: euid incorrect uid:%u euid:%u (should be %u)",
16637011Sbrian		    __func__, (u_int)getuid(), (u_int)geteuid(), (u_int)uid);
16737011Sbrian	}
16837011Sbrian}
16937011Sbrian
17036285Sbrian/*
17136285Sbrian * Restores to the original (privileged) uid.
17236285Sbrian */
17336285Sbrianvoid
17436285Sbrianrestore_uid(void)
17536285Sbrian{
17636285Sbrian	/* it's a no-op unless privileged */
17736285Sbrian	if (!privileged) {
17836285Sbrian		debug("restore_uid: (unprivileged)");
17936285Sbrian		return;
18036285Sbrian	}
18136285Sbrian	if (!temporarily_use_uid_effective)
18236285Sbrian		fatal("restore_uid: temporarily_use_uid not effective");
18336285Sbrian
18436285Sbrian#ifdef SAVED_IDS_WORK_WITH_SETEUID
18536285Sbrian	debug("restore_uid: %u/%u", (u_int)saved_euid, (u_int)saved_egid);
18636285Sbrian	/* Set the effective uid back to the saved privileged uid. */
18736285Sbrian	if (seteuid(saved_euid) < 0)
18836285Sbrian		fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror(errno));
18936285Sbrian	if (setegid(saved_egid) < 0)
19036285Sbrian		fatal("setegid %u: %.100s", (u_int)saved_egid, strerror(errno));
19136285Sbrian#else /* SAVED_IDS_WORK_WITH_SETEUID */
19236314Sbrian	/*
19337386Sbrian	 * We are unable to restore the real uid to its unprivileged value.
19437386Sbrian	 * Propagate the real uid (usually more privileged) to effective uid
19536285Sbrian	 * as well.
19636285Sbrian	 */
19736285Sbrian	setuid(getuid());
19836285Sbrian	setgid(getgid());
19936285Sbrian#endif /* SAVED_IDS_WORK_WITH_SETEUID */
20036285Sbrian
20136285Sbrian	if (setgroups(saved_egroupslen, saved_egroups) < 0)
20236285Sbrian		fatal("setgroups: %.100s", strerror(errno));
20336285Sbrian	temporarily_use_uid_effective = 0;
20436285Sbrian}
20536285Sbrian
20636285Sbrian/*
20736285Sbrian * Permanently sets all uids to the given uid.  This cannot be
20836285Sbrian * called while temporarily_use_uid is effective.
20936285Sbrian */
21036285Sbrianvoid
21136285Sbrianpermanently_set_uid(struct passwd *pw)
21236285Sbrian{
21336285Sbrian	uid_t old_uid = getuid();
21436285Sbrian	gid_t old_gid = getgid();
21536285Sbrian
21636285Sbrian	if (pw == NULL)
21736285Sbrian		fatal("permanently_set_uid: no user given");
21836285Sbrian	if (temporarily_use_uid_effective)
21936285Sbrian		fatal("permanently_set_uid: temporarily_use_uid effective");
22036285Sbrian	debug("permanently_set_uid: %u/%u", (u_int)pw->pw_uid,
22136285Sbrian	    (u_int)pw->pw_gid);
22236285Sbrian
22336285Sbrian#if defined(HAVE_SETRESGID) && !defined(BROKEN_SETRESGID)
22436285Sbrian	if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) < 0)
22536285Sbrian		fatal("setresgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
22636285Sbrian#elif defined(HAVE_SETREGID) && !defined(BROKEN_SETREGID)
22736285Sbrian	if (setregid(pw->pw_gid, pw->pw_gid) < 0)
22836285Sbrian		fatal("setregid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
22936285Sbrian#else
23037010Sbrian	if (setegid(pw->pw_gid) < 0)
23136285Sbrian		fatal("setegid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
23236285Sbrian	if (setgid(pw->pw_gid) < 0)
23337010Sbrian		fatal("setgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
23436285Sbrian#endif
23536285Sbrian
23637019Sbrian#ifdef __APPLE__
23736285Sbrian	/*
23836285Sbrian	 * OS X requires initgroups after setgid to opt back into
23936285Sbrian	 * memberd support for >16 supplemental groups.
24036285Sbrian	 */
24136285Sbrian	if (initgroups(pw->pw_name, pw->pw_gid) < 0)
24236285Sbrian		fatal("initgroups %.100s %u: %.100s",
24336285Sbrian		    pw->pw_name, (u_int)pw->pw_gid, strerror(errno));
24436285Sbrian#endif
24536285Sbrian
24636285Sbrian#if defined(HAVE_SETRESUID) && !defined(BROKEN_SETRESUID)
24736285Sbrian	if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) < 0)
24836285Sbrian		fatal("setresuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
24936285Sbrian#elif defined(HAVE_SETREUID) && !defined(BROKEN_SETREUID)
25036285Sbrian	if (setreuid(pw->pw_uid, pw->pw_uid) < 0)
25136285Sbrian		fatal("setreuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
25236285Sbrian#else
25336285Sbrian# ifndef SETEUID_BREAKS_SETUID
25436285Sbrian	if (seteuid(pw->pw_uid) < 0)
25536285Sbrian		fatal("seteuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
25636285Sbrian# endif
25736285Sbrian	if (setuid(pw->pw_uid) < 0)
25836285Sbrian		fatal("setuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
25937011Sbrian#endif
26037011Sbrian
26137011Sbrian#ifndef HAVE_CYGWIN
26237011Sbrian	/* Try restoration of GID if changed (test clearing of saved gid) */
26337011Sbrian	if (old_gid != pw->pw_gid && pw->pw_uid != 0 &&
26437011Sbrian	    (setgid(old_gid) != -1 || setegid(old_gid) != -1))
26537011Sbrian		fatal("%s: was able to restore old [e]gid", __func__);
26637011Sbrian#endif
26737011Sbrian
26837011Sbrian	/* Verify GID drop was successful */
26937011Sbrian	if (getgid() != pw->pw_gid || getegid() != pw->pw_gid) {
27037011Sbrian		fatal("%s: egid incorrect gid:%u egid:%u (should be %u)",
27137011Sbrian		    __func__, (u_int)getgid(), (u_int)getegid(),
27237011Sbrian		    (u_int)pw->pw_gid);
27336285Sbrian	}
27436285Sbrian
27536285Sbrian#ifndef HAVE_CYGWIN
27637019Sbrian	/* Try restoration of UID if changed (test clearing of saved uid) */
27736285Sbrian	if (old_uid != pw->pw_uid &&
27836285Sbrian	    (setuid(old_uid) != -1 || seteuid(old_uid) != -1))
27936285Sbrian		fatal("%s: was able to restore old [e]uid", __func__);
28036285Sbrian#endif
28137010Sbrian
28236285Sbrian	/* Verify UID drop was successful */
28336285Sbrian	if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) {
28436285Sbrian		fatal("%s: euid incorrect uid:%u euid:%u (should be %u)",
28536285Sbrian		    __func__, (u_int)getuid(), (u_int)geteuid(),
28636285Sbrian		    (u_int)pw->pw_uid);
28737141Sbrian	}
28836285Sbrian}
28936285Sbrian