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