]> andersk Git - openssh.git/blob - uidswap.c
ca089480605af35056ff26999a5bae99cb31f704
[openssh.git] / uidswap.c
1 /*
2  * Author: Tatu Ylonen <ylo@cs.hut.fi>
3  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4  *                    All rights reserved
5  * Code for uid-swapping.
6  *
7  * As far as I am concerned, the code I have written for this software
8  * can be used freely for any purpose.  Any derived versions of this
9  * software must be clearly marked as such, and if the derived work is
10  * incompatible with the protocol description in the RFC file, it must be
11  * called by a name other than "ssh" or "Secure Shell".
12  */
13
14 #include "includes.h"
15
16 #include "log.h"
17 #include "uidswap.h"
18 #include "xmalloc.h"
19
20 /*
21  * Note: all these functions must work in all of the following cases:
22  *    1. euid=0, ruid=0
23  *    2. euid=0, ruid!=0
24  *    3. euid!=0, ruid!=0
25  * Additionally, they must work regardless of whether the system has
26  * POSIX saved uids or not.
27  */
28
29 #if defined(_POSIX_SAVED_IDS) && !defined(BROKEN_SAVED_UIDS)
30 /* Lets assume that posix saved ids also work with seteuid, even though that
31    is not part of the posix specification. */
32 #define SAVED_IDS_WORK_WITH_SETEUID
33 /* Saved effective uid. */
34 static uid_t    saved_euid = 0;
35 static gid_t    saved_egid = 0;
36 #endif
37
38 /* Saved effective uid. */
39 static int      privileged = 0;
40 static int      temporarily_use_uid_effective = 0;
41 static gid_t    *saved_egroups = NULL, *user_groups = NULL;
42 static int      saved_egroupslen = -1, user_groupslen = -1;
43
44 /*
45  * Temporarily changes to the given uid.  If the effective user
46  * id is not root, this does nothing.  This call cannot be nested.
47  */
48 void
49 temporarily_use_uid(struct passwd *pw)
50 {
51         /* Save the current euid, and egroups. */
52 #ifdef SAVED_IDS_WORK_WITH_SETEUID
53         saved_euid = geteuid();
54         saved_egid = getegid();
55         debug("temporarily_use_uid: %u/%u (e=%u/%u)",
56             (u_int)pw->pw_uid, (u_int)pw->pw_gid,
57             (u_int)saved_euid, (u_int)saved_egid);
58 #ifndef HAVE_CYGWIN
59         if (saved_euid != 0) {
60                 privileged = 0;
61                 return;
62         }
63 #endif
64 #else
65         if (geteuid() != 0) {
66                 privileged = 0;
67                 return;
68         }
69 #endif /* SAVED_IDS_WORK_WITH_SETEUID */
70
71         privileged = 1;
72         temporarily_use_uid_effective = 1;
73
74         saved_egroupslen = getgroups(0, NULL);
75         if (saved_egroupslen < 0)
76                 fatal("getgroups: %.100s", strerror(errno));
77         if (saved_egroupslen > 0) {
78                 saved_egroups = xrealloc(saved_egroups,
79                     saved_egroupslen * sizeof(gid_t));
80                 if (getgroups(saved_egroupslen, saved_egroups) < 0)
81                         fatal("getgroups: %.100s", strerror(errno));
82         } else { /* saved_egroupslen == 0 */
83                 if (saved_egroups != NULL)
84                         xfree(saved_egroups);
85         }
86
87         /* set and save the user's groups */
88         if (user_groupslen == -1) {
89                 if (initgroups(pw->pw_name, pw->pw_gid) < 0)
90                         fatal("initgroups: %s: %.100s", pw->pw_name,
91                             strerror(errno));
92
93                 user_groupslen = getgroups(0, NULL);
94                 if (user_groupslen < 0)
95                         fatal("getgroups: %.100s", strerror(errno));
96                 if (user_groupslen > 0) {
97                         user_groups = xrealloc(user_groups,
98                             user_groupslen * sizeof(gid_t));
99                         if (getgroups(user_groupslen, user_groups) < 0)
100                                 fatal("getgroups: %.100s", strerror(errno));
101                 } else { /* user_groupslen == 0 */
102                         if (user_groups)
103                                 xfree(user_groups);
104                 }
105         }
106         /* Set the effective uid to the given (unprivileged) uid. */
107         if (setgroups(user_groupslen, user_groups) < 0)
108                 fatal("setgroups: %.100s", strerror(errno));
109 #ifndef SAVED_IDS_WORK_WITH_SETEUID
110         /* Propagate the privileged gid to all of our gids. */
111         if (setgid(getegid()) < 0)
112                 debug("setgid %u: %.100s", (u_int) getegid(), strerror(errno));
113         /* Propagate the privileged uid to all of our uids. */
114         if (setuid(geteuid()) < 0)
115                 debug("setuid %u: %.100s", (u_int) geteuid(), strerror(errno));
116 #endif /* SAVED_IDS_WORK_WITH_SETEUID */
117         if (setegid(pw->pw_gid) < 0)
118                 fatal("setegid %u: %.100s", (u_int)pw->pw_gid,
119                     strerror(errno));
120         if (seteuid(pw->pw_uid) == -1)
121                 fatal("seteuid %u: %.100s", (u_int)pw->pw_uid,
122                     strerror(errno));
123 }
124
125 /*
126  * Restores to the original (privileged) uid.
127  */
128 void
129 restore_uid(void)
130 {
131         /* it's a no-op unless privileged */
132         if (!privileged) {
133                 debug("restore_uid: (unprivileged)");
134                 return;
135         }
136         if (!temporarily_use_uid_effective)
137                 fatal("restore_uid: temporarily_use_uid not effective");
138
139 #ifdef SAVED_IDS_WORK_WITH_SETEUID
140         debug("restore_uid: %u/%u", (u_int)saved_euid, (u_int)saved_egid);
141         /* Set the effective uid back to the saved privileged uid. */
142         if (seteuid(saved_euid) < 0)
143                 fatal("seteuid %u: %.100s", (u_int)saved_euid, strerror(errno));
144         if (setegid(saved_egid) < 0)
145                 fatal("setegid %u: %.100s", (u_int)saved_egid, strerror(errno));
146 #else /* SAVED_IDS_WORK_WITH_SETEUID */
147         /*
148          * We are unable to restore the real uid to its unprivileged value.
149          * Propagate the real uid (usually more privileged) to effective uid
150          * as well.
151          */
152         setuid(getuid());
153         setgid(getgid());
154 #endif /* SAVED_IDS_WORK_WITH_SETEUID */
155
156         if (setgroups(saved_egroupslen, saved_egroups) < 0)
157                 fatal("setgroups: %.100s", strerror(errno));
158         temporarily_use_uid_effective = 0;
159 }
160
161 /*
162  * Permanently sets all uids to the given uid.  This cannot be
163  * called while temporarily_use_uid is effective.
164  */
165 void
166 permanently_set_uid(struct passwd *pw)
167 {
168         uid_t old_uid = getuid();
169         gid_t old_gid = getgid();
170
171         if (temporarily_use_uid_effective)
172                 fatal("permanently_set_uid: temporarily_use_uid effective");
173         debug("permanently_set_uid: %u/%u", (u_int)pw->pw_uid,
174             (u_int)pw->pw_gid);
175
176 #if defined(HAVE_SETRESGID) && !defined(BROKEN_SETRESGID)
177         if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) < 0)
178                 fatal("setresgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
179 #elif defined(HAVE_SETREGID) && !defined(BROKEN_SETREGID)
180         if (setregid(pw->pw_gid, pw->pw_gid) < 0)
181                 fatal("setregid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
182 #else
183         if (setegid(pw->pw_gid) < 0)
184                 fatal("setegid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
185         if (setgid(pw->pw_gid) < 0)
186                 fatal("setgid %u: %.100s", (u_int)pw->pw_gid, strerror(errno));
187 #endif
188
189 #if defined(HAVE_SETRESUID) && !defined(BROKEN_SETRESUID)
190         if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) < 0)
191                 fatal("setresuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
192 #elif defined(HAVE_SETREUID) && !defined(BROKEN_SETREUID)
193         if (setreuid(pw->pw_uid, pw->pw_uid) < 0)
194                 fatal("setreuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
195 #else
196 # ifndef SETEUID_BREAKS_SETUID
197         if (seteuid(pw->pw_uid) < 0)
198                 fatal("seteuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
199 # endif
200         if (setuid(pw->pw_uid) < 0)
201                 fatal("setuid %u: %.100s", (u_int)pw->pw_uid, strerror(errno));
202 #endif
203
204 #ifndef HAVE_CYGWIN
205         /* Try restoration of GID if changed (test clearing of saved gid) */
206         if (old_gid != pw->pw_gid && pw->pw_uid != 0 &&
207             (setgid(old_gid) != -1 || setegid(old_gid) != -1))
208                 fatal("%s: was able to restore old [e]gid", __func__);
209 #endif
210
211         /* Verify GID drop was successful */
212         if (getgid() != pw->pw_gid || getegid() != pw->pw_gid) {
213                 fatal("%s: egid incorrect gid:%u egid:%u (should be %u)",
214                     __func__, (u_int)getgid(), (u_int)getegid(),
215                     (u_int)pw->pw_gid);
216         }
217
218 #ifndef HAVE_CYGWIN
219         /* Try restoration of UID if changed (test clearing of saved uid) */
220         if (old_uid != pw->pw_uid &&
221             (setuid(old_uid) != -1 || seteuid(old_uid) != -1))
222                 fatal("%s: was able to restore old [e]uid", __func__);
223 #endif
224
225         /* Verify UID drop was successful */
226         if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid) {
227                 fatal("%s: euid incorrect uid:%u euid:%u (should be %u)",
228                     __func__, (u_int)getuid(), (u_int)geteuid(),
229                     (u_int)pw->pw_uid);
230         }
231 }
This page took 0.082247 seconds and 3 git commands to generate.