r25142: Panic if setting the group list fails while switching security
[tprouty/samba.git] / source3 / smbd / sec_ctx.c
1 /* 
2    Unix SMB/CIFS implementation.
3    uid/user handling
4    Copyright (C) Tim Potter 2000
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21
22 extern struct current_user current_user;
23
24 struct sec_ctx {
25         UNIX_USER_TOKEN ut;
26         NT_USER_TOKEN *token;
27 };
28
29 /* A stack of security contexts.  We include the current context as being
30    the first one, so there is room for another MAX_SEC_CTX_DEPTH more. */
31
32 static struct sec_ctx sec_ctx_stack[MAX_SEC_CTX_DEPTH + 1];
33 static int sec_ctx_stack_ndx;
34
35 /****************************************************************************
36  Become the specified uid.
37 ****************************************************************************/
38
39 static BOOL become_uid(uid_t uid)
40 {
41         /* Check for dodgy uid values */
42
43         if (uid == (uid_t)-1 || 
44             ((sizeof(uid_t) == 2) && (uid == (uid_t)65535))) {
45                 static int done;
46  
47                 if (!done) {
48                         DEBUG(1,("WARNING: using uid %d is a security risk\n",
49                                  (int)uid));
50                         done = 1;
51                 }
52         }
53
54         /* Set effective user id */
55
56         set_effective_uid(uid);
57
58         DO_PROFILE_INC(uid_changes);
59         return True;
60 }
61
62 /****************************************************************************
63  Become the specified gid.
64 ****************************************************************************/
65
66 static BOOL become_gid(gid_t gid)
67 {
68         /* Check for dodgy gid values */
69
70         if (gid == (gid_t)-1 || ((sizeof(gid_t) == 2) && 
71                                  (gid == (gid_t)65535))) {
72                 static int done;
73                 
74                 if (!done) {
75                         DEBUG(1,("WARNING: using gid %d is a security risk\n",
76                                  (int)gid));  
77                         done = 1;
78                 }
79         }
80   
81         /* Set effective group id */
82
83         set_effective_gid(gid);
84         return True;
85 }
86
87 /****************************************************************************
88  Become the specified uid and gid.
89 ****************************************************************************/
90
91 static BOOL become_id(uid_t uid, gid_t gid)
92 {
93         return become_gid(gid) && become_uid(uid);
94 }
95
96 /****************************************************************************
97  Drop back to root privileges in order to change to another user.
98 ****************************************************************************/
99
100 static void gain_root(void)
101 {
102         if (non_root_mode()) {
103                 return;
104         }
105
106         if (geteuid() != 0) {
107                 set_effective_uid(0);
108
109                 if (geteuid() != 0) {
110                         DEBUG(0,
111                               ("Warning: You appear to have a trapdoor "
112                                "uid system\n"));
113                 }
114         }
115
116         if (getegid() != 0) {
117                 set_effective_gid(0);
118
119                 if (getegid() != 0) {
120                         DEBUG(0,
121                               ("Warning: You appear to have a trapdoor "
122                                "gid system\n"));
123                 }
124         }
125 }
126
127 /****************************************************************************
128  Get the list of current groups.
129 ****************************************************************************/
130
131 static int get_current_groups(gid_t gid, int *p_ngroups, gid_t **p_groups)
132 {
133         int i;
134         gid_t grp;
135         int ngroups;
136         gid_t *groups = NULL;
137
138         (*p_ngroups) = 0;
139         (*p_groups) = NULL;
140
141         /* this looks a little strange, but is needed to cope with
142            systems that put the current egid in the group list
143            returned from getgroups() (tridge) */
144         save_re_gid();
145         set_effective_gid(gid);
146         setgid(gid);
147
148         ngroups = sys_getgroups(0,&grp);
149         if (ngroups <= 0) {
150                 goto fail;
151         }
152
153         if((groups = SMB_MALLOC_ARRAY(gid_t, ngroups+1)) == NULL) {
154                 DEBUG(0,("setup_groups malloc fail !\n"));
155                 goto fail;
156         }
157
158         if ((ngroups = sys_getgroups(ngroups,groups)) == -1) {
159                 goto fail;
160         }
161
162         restore_re_gid();
163
164         (*p_ngroups) = ngroups;
165         (*p_groups) = groups;
166
167         DEBUG( 3, ( "get_current_groups: user is in %u groups: ", ngroups));
168         for (i = 0; i < ngroups; i++ ) {
169                 DEBUG( 3, ( "%s%d", (i ? ", " : ""), (int)groups[i] ) );
170         }
171         DEBUG( 3, ( "\n" ) );
172
173         return ngroups;
174
175 fail:
176         SAFE_FREE(groups);
177         restore_re_gid();
178         return -1;
179 }
180
181 /****************************************************************************
182  Create a new security context on the stack.  It is the same as the old
183  one.  User changes are done using the set_sec_ctx() function.
184 ****************************************************************************/
185
186 BOOL push_sec_ctx(void)
187 {
188         struct sec_ctx *ctx_p;
189
190         /* Check we don't overflow our stack */
191
192         if (sec_ctx_stack_ndx == MAX_SEC_CTX_DEPTH) {
193                 DEBUG(0, ("Security context stack overflow!\n"));
194                 smb_panic("Security context stack overflow!");
195         }
196
197         /* Store previous user context */
198
199         sec_ctx_stack_ndx++;
200
201         ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
202
203         ctx_p->ut.uid = geteuid();
204         ctx_p->ut.gid = getegid();
205
206         DEBUG(3, ("push_sec_ctx(%u, %u) : sec_ctx_stack_ndx = %d\n", 
207                   (unsigned int)ctx_p->ut.uid, (unsigned int)ctx_p->ut.gid, sec_ctx_stack_ndx ));
208
209         ctx_p->token = dup_nt_token(NULL,
210                                     sec_ctx_stack[sec_ctx_stack_ndx-1].token);
211
212         ctx_p->ut.ngroups = sys_getgroups(0, NULL);
213
214         if (ctx_p->ut.ngroups != 0) {
215                 if (!(ctx_p->ut.groups = SMB_MALLOC_ARRAY(gid_t, ctx_p->ut.ngroups))) {
216                         DEBUG(0, ("Out of memory in push_sec_ctx()\n"));
217                         TALLOC_FREE(ctx_p->token);
218                         return False;
219                 }
220
221                 sys_getgroups(ctx_p->ut.ngroups, ctx_p->ut.groups);
222         } else {
223                 ctx_p->ut.groups = NULL;
224         }
225
226         return True;
227 }
228
229 /****************************************************************************
230  Change UNIX security context. Calls panic if not successful so no return value.
231 ****************************************************************************/
232
233 #ifndef HAVE_DARWIN_INITGROUPS
234
235 /* Normal credential switch path. */
236
237 static void set_unix_security_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups)
238 {
239         /* Start context switch */
240         gain_root();
241 #ifdef HAVE_SETGROUPS
242         if (sys_setgroups(gid, ngroups, groups) != 0) {
243                 smb_panic("sys_setgroups failed");
244         }
245 #endif
246         become_id(uid, gid);
247         /* end context switch */
248 }
249
250 #else /* HAVE_DARWIN_INITGROUPS */
251
252 /* The Darwin groups implementation is a little unusual. The list of
253 * groups in the kernel credential is not exhaustive, but more like
254 * a cache. The full group list is held in userspace and checked
255 * dynamically.
256 *
257 * This is an optional mechanism, and setgroups(2) opts out
258 * of it. That is, if you call setgroups, then the list of groups you
259 * set are the only groups that are ever checked. This is not what we
260 * want. We want to opt in to the dynamic resolution mechanism, so we
261 * need to specify the uid of the user whose group list (cache) we are
262 * setting.
263 *
264 * The Darwin rules are:
265 *  1. Thou shalt setegid, initgroups and seteuid IN THAT ORDER
266 *  2. Thou shalt not pass more that NGROUPS_MAX to initgroups
267 *  3. Thou shalt leave the first entry in the groups list well alone
268 */
269
270 #include <sys/syscall.h>
271
272 static void set_unix_security_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups)
273 {
274         int max = groups_max();
275
276         /* Start context switch */
277         gain_root();
278
279         become_gid(gid);
280
281
282         if (syscall(SYS_initgroups, (ngroups > max) ? max : ngroups,
283                         groups, uid) == 1) {
284                 DEBUG(0, ("WARNING: failed to set group list "
285                         "(%d groups) for UID %ld: %s\n",
286                         ngroups, uid, strerror(errno)));
287                 smb_panic("sys_setgroups failed");
288         }
289
290         become_uid(uid);
291         /* end context switch */
292 }
293
294 #endif /* HAVE_DARWIN_INITGROUPS */
295
296 /****************************************************************************
297  Set the current security context to a given user.
298 ****************************************************************************/
299
300 void set_sec_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups, NT_USER_TOKEN *token)
301 {
302         struct sec_ctx *ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
303         
304         /* Set the security context */
305
306         DEBUG(3, ("setting sec ctx (%u, %u) - sec_ctx_stack_ndx = %d\n", 
307                 (unsigned int)uid, (unsigned int)gid, sec_ctx_stack_ndx));
308
309         debug_nt_user_token(DBGC_CLASS, 5, token);
310         debug_unix_user_token(DBGC_CLASS, 5, uid, gid, ngroups, groups);
311
312         /* Change uid, gid and supplementary group list. */
313         set_unix_security_ctx(uid, gid, ngroups, groups);
314
315         ctx_p->ut.ngroups = ngroups;
316
317         SAFE_FREE(ctx_p->ut.groups);
318         if (token && (token == ctx_p->token)) {
319                 smb_panic("DUPLICATE_TOKEN");
320         }
321
322         TALLOC_FREE(ctx_p->token);
323         
324         if (ngroups) {
325                 ctx_p->ut.groups = (gid_t *)memdup(groups,
326                                                    sizeof(gid_t) * ngroups);
327                 if (!ctx_p->ut.groups) {
328                         smb_panic("memdup failed");
329                 }
330         } else {
331                 ctx_p->ut.groups = NULL;
332         }
333
334         if (token) {
335                 ctx_p->token = dup_nt_token(NULL, token);
336                 if (!ctx_p->token) {
337                         smb_panic("dup_nt_token failed");
338                 }
339         } else {
340                 ctx_p->token = NULL;
341         }
342
343         ctx_p->ut.uid = uid;
344         ctx_p->ut.gid = gid;
345
346         /* Update current_user stuff */
347
348         current_user.ut.uid = uid;
349         current_user.ut.gid = gid;
350         current_user.ut.ngroups = ngroups;
351         current_user.ut.groups = groups;
352         current_user.nt_user_token = ctx_p->token;
353 }
354
355 /****************************************************************************
356  Become root context.
357 ****************************************************************************/
358
359 void set_root_sec_ctx(void)
360 {
361         /* May need to worry about supplementary groups at some stage */
362
363         set_sec_ctx(0, 0, 0, NULL, NULL);
364 }
365
366 /****************************************************************************
367  Pop a security context from the stack.
368 ****************************************************************************/
369
370 BOOL pop_sec_ctx(void)
371 {
372         struct sec_ctx *ctx_p;
373         struct sec_ctx *prev_ctx_p;
374
375         /* Check for stack underflow */
376
377         if (sec_ctx_stack_ndx == 0) {
378                 DEBUG(0, ("Security context stack underflow!\n"));
379                 smb_panic("Security context stack underflow!");
380         }
381
382         ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
383
384         /* Clear previous user info */
385
386         ctx_p->ut.uid = (uid_t)-1;
387         ctx_p->ut.gid = (gid_t)-1;
388
389         SAFE_FREE(ctx_p->ut.groups);
390         ctx_p->ut.ngroups = 0;
391
392         TALLOC_FREE(ctx_p->token);
393
394         /* Pop back previous user */
395
396         sec_ctx_stack_ndx--;
397
398         prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
399
400         /* Change uid, gid and supplementary group list. */
401         set_unix_security_ctx(prev_ctx_p->ut.uid,
402                         prev_ctx_p->ut.gid,
403                         prev_ctx_p->ut.ngroups,
404                         prev_ctx_p->ut.groups);
405
406         /* Update current_user stuff */
407
408         current_user.ut.uid = prev_ctx_p->ut.uid;
409         current_user.ut.gid = prev_ctx_p->ut.gid;
410         current_user.ut.ngroups = prev_ctx_p->ut.ngroups;
411         current_user.ut.groups = prev_ctx_p->ut.groups;
412         current_user.nt_user_token = prev_ctx_p->token;
413
414         DEBUG(3, ("pop_sec_ctx (%u, %u) - sec_ctx_stack_ndx = %d\n", 
415                 (unsigned int)geteuid(), (unsigned int)getegid(), sec_ctx_stack_ndx));
416
417         return True;
418 }
419
420 /* Initialise the security context system */
421
422 void init_sec_ctx(void)
423 {
424         int i;
425         struct sec_ctx *ctx_p;
426
427         /* Initialise security context stack */
428
429         memset(sec_ctx_stack, 0, sizeof(struct sec_ctx) * MAX_SEC_CTX_DEPTH);
430
431         for (i = 0; i < MAX_SEC_CTX_DEPTH; i++) {
432                 sec_ctx_stack[i].ut.uid = (uid_t)-1;
433                 sec_ctx_stack[i].ut.gid = (gid_t)-1;
434         }
435
436         /* Initialise first level of stack.  It is the current context */
437         ctx_p = &sec_ctx_stack[0];
438
439         ctx_p->ut.uid = geteuid();
440         ctx_p->ut.gid = getegid();
441
442         get_current_groups(ctx_p->ut.gid, &ctx_p->ut.ngroups, &ctx_p->ut.groups);
443
444         ctx_p->token = NULL; /* Maps to guest user. */
445
446         /* Initialise current_user global */
447
448         current_user.ut.uid = ctx_p->ut.uid;
449         current_user.ut.gid = ctx_p->ut.gid;
450         current_user.ut.ngroups = ctx_p->ut.ngroups;
451         current_user.ut.groups = ctx_p->ut.groups;
452
453         /* The conn and vuid are usually taken care of by other modules.
454            We initialise them here. */
455
456         current_user.conn = NULL;
457         current_user.vuid = UID_FIELD_INVALID;
458         current_user.nt_user_token = NULL;
459 }