xref: /openbsd-src/usr.bin/ssh/uidswap.c (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1 /* $OpenBSD: uidswap.c,v 1.39 2015/06/24 01:49:19 dtucker 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. */
39 static int	privileged = 0;
40 static int	temporarily_use_uid_effective = 0;
41 static uid_t	saved_euid = 0;
42 static gid_t	saved_egid;
43 static gid_t	saved_egroups[NGROUPS_MAX], user_groups[NGROUPS_MAX];
44 static 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  */
50 void
51 temporarily_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 < 0)
67 		fatal("getgroups: %.100s", strerror(errno));
68 
69 	/* set and save the user's groups */
70 	if (user_groupslen == -1) {
71 		if (initgroups(pw->pw_name, pw->pw_gid) < 0)
72 			fatal("initgroups: %s: %.100s", pw->pw_name,
73 			    strerror(errno));
74 		user_groupslen = getgroups(NGROUPS_MAX, user_groups);
75 		if (user_groupslen < 0)
76 			fatal("getgroups: %.100s", strerror(errno));
77 	}
78 	/* Set the effective uid to the given (unprivileged) uid. */
79 	if (setgroups(user_groupslen, user_groups) < 0)
80 		fatal("setgroups: %.100s", strerror(errno));
81 	if (setegid(pw->pw_gid) < 0)
82 		fatal("setegid %u: %.100s", (u_int)pw->pw_gid,
83 		    strerror(errno));
84 	if (seteuid(pw->pw_uid) == -1)
85 		fatal("seteuid %u: %.100s", (u_int)pw->pw_uid,
86 		    strerror(errno));
87 }
88 
89 /*
90  * Restores to the original (privileged) uid.
91  */
92 void
93 restore_uid(void)
94 {
95 	/* it's a no-op unless privileged */
96 	if (!privileged) {
97 		debug("restore_uid: (unprivileged)");
98 		return;
99 	}
100 	if (!temporarily_use_uid_effective)
101 		fatal("restore_uid: temporarily_use_uid not effective");
102 	debug("restore_uid: %u/%u", (u_int)saved_euid, (u_int)saved_egid);
103 	/* Set the effective uid back to the saved privileged uid. */
104 	if (seteuid(saved_euid) < 0)
105 		fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror(errno));
106 	if (setgroups(saved_egroupslen, saved_egroups) < 0)
107 		fatal("setgroups: %.100s", strerror(errno));
108 	if (setegid(saved_egid) < 0)
109 		fatal("setegid %u: %.100s", (u_int)saved_egid, strerror(errno));
110 	temporarily_use_uid_effective = 0;
111 }
112 
113 /*
114  * Permanently sets all uids to the given uid.  This cannot be
115  * called while temporarily_use_uid is effective.
116  */
117 void
118 permanently_set_uid(struct passwd *pw)
119 {
120 	if (temporarily_use_uid_effective)
121 		fatal("permanently_set_uid: temporarily_use_uid effective");
122 	debug("permanently_set_uid: %u/%u", (u_int)pw->pw_uid,
123 	    (u_int)pw->pw_gid);
124 	if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0)
125 		fatal("setresgid %u: %s", (u_int)pw->pw_gid, strerror(errno));
126 	if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0)
127 		fatal("setresuid %u: %s", (u_int)pw->pw_uid, strerror(errno));
128 }
129 
130 void
131 permanently_drop_suid(uid_t uid)
132 {
133 	debug("permanently_drop_suid: %u", (u_int)uid);
134 	if (setresuid(uid, uid, uid) != 0)
135 		fatal("setresuid %u: %s", (u_int)uid, strerror(errno));
136 }
137