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