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