212843ab4dda36c45438831f92cc9974602bcd70
[openafs.git] / src / afs / afs_osi_pag.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * Implements:
12  * genpag
13  * getpag
14  * afs_setpag
15  * AddPag
16  * afs_InitReq
17  * afs_get_pag_from_groups
18  * afs_get_groups_from_pag
19  * PagInCred
20  */
21
22 #include <afsconfig.h>
23 #include "afs/param.h"
24
25
26 #include "afs/sysincludes.h"    /* Standard vendor system headers */
27 #include "afsincludes.h"        /* Afs-based standard headers */
28 #include "afs/afs_stats.h"      /* statistics */
29 #include "afs/afs_cbqueue.h"
30 #include "afs/nfsclient.h"
31 #include "afs/afs_osidnlc.h"
32
33
34 /* Imported variables */
35 extern int afs_shuttingdown;
36
37 /* Exported variables */
38 afs_uint32 pag_epoch;
39 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
40 afs_uint32 pagCounter = 1;
41 #else
42 afs_uint32 pagCounter = 0;
43 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
44
45 #ifdef AFS_LINUX26_ONEGROUP_ENV
46 #define NUMPAGGROUPS 1
47 #else
48 #define NUMPAGGROUPS 2
49 #endif
50 /* Local variables */
51
52 /*
53  * Pags are implemented as follows: the set of groups whose long
54  * representation is '41XXXXXX' hex are used to represent the pags.
55  * Being a member of such a group means you are authenticated as pag
56  * XXXXXX (0x41 == 'A', for Andrew).  You are never authenticated as
57  * multiple pags at once.
58  *
59  * The function afs_InitReq takes a credential field and formats the
60  * corresponding venus request structure.  The uid field in the
61  * vrequest structure is set to the *pag* you are authenticated as, or
62  * the uid, if you aren't authenticated with a pag.
63  *
64  * The basic motivation behind pags is this: just because your unix
65  * uid is N doesn't mean that you should have the same privileges as
66  * anyone logged in on the machine as user N, since this would enable
67  * the superuser on the machine to sneak in and make use of anyone's
68  * authentication info, even that which is only accidentally left
69  * behind when someone leaves a public workstation.
70  *
71  * AFS doesn't use the unix uid for anything except
72  * a handle with which to find the actual authentication tokens
73  * anyway, so the pag is an alternative handle which is somewhat more
74  * secure (although of course not absolutely secure).
75 */
76 #if !defined(UKERNEL) || !defined(AFS_WEB_ENHANCEMENTS)
77 afs_uint32
78 genpag(void)
79 {
80     AFS_STATCNT(genpag);
81 #ifdef AFS_LINUX20_ENV
82     /* Ensure unique PAG's (mod 200 days) when reloading the client. */
83     return (('A' << 24) + ((pag_epoch + pagCounter++) & 0xffffff));
84 #else /* AFS_LINUX20_ENV */
85     return (('A' << 24) + (pagCounter++ & 0xffffff));
86 #endif /* AFS_LINUX20_ENV */
87 }
88
89 afs_uint32
90 getpag(void)
91 {
92     AFS_STATCNT(getpag);
93 #ifdef AFS_LINUX20_ENV
94     /* Ensure unique PAG's (mod 200 days) when reloading the client. */
95     return (('A' << 24) + ((pag_epoch + pagCounter) & 0xffffff));
96 #else
97     return (('A' << 24) + (pagCounter & 0xffffff));
98 #endif
99 }
100
101 #else
102
103 /* Web enhancement: we don't need to restrict pags to 41XXXXXX since
104  * we are not sharing the space with anyone.  So we use the full 32 bits. */
105
106 afs_uint32
107 genpag(void)
108 {
109     AFS_STATCNT(genpag);
110 #ifdef AFS_LINUX20_ENV
111     return (pag_epoch + pagCounter++);
112 #else
113     return (pagCounter++);
114 #endif /* AFS_LINUX20_ENV */
115 }
116
117 afs_uint32
118 getpag(void)
119 {
120     AFS_STATCNT(getpag);
121 #ifdef AFS_LINUX20_ENV
122     /* Ensure unique PAG's (mod 200 days) when reloading the client. */
123     return (pag_epoch + pagCounter);
124 #else
125     return (pagCounter);
126 #endif
127 }
128 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
129
130 /* used to require 10 seconds between each setpag to guarantee that
131  * PAGs never wrap - which would be a security hole.  If we presume
132  * that in ordinary operation, the average rate of PAG allocation
133  * will not exceed one per second, the 24 bits provided will be
134  * sufficient for ~200 days.  Unfortunately, if we merely assume that,
135  * there will be an opportunity for attack.  So we must enforce it.
136  * If we need to increase the average rate of PAG allocation, we
137  * should increase the number of bits in a PAG, and/or reduce our
138  * window in which we guarantee that the PAG counter won't wrap.
139  * By permitting an average of one new PAG per second, new PAGs can
140  * be allocated VERY frequently over a short period relative to total uptime.
141  * Of course, there's only an issue here if one user stays logged (and re-
142  * activates tokens repeatedly) for that entire period.
143  */
144
145 static int afs_pag_sleepcnt = 0;
146 static int afs_pag_timewarn = 0;
147
148 static int
149 afs_pag_sleep(AFS_UCRED **acred)
150 {
151     int rv = 0;
152
153     if (!afs_suser(acred)) {
154         if(osi_Time() - pag_epoch < pagCounter) {
155             rv = 1;
156         }
157         if (rv && (osi_Time() < pag_epoch)) {
158             if (!afs_pag_timewarn) {
159                 afs_pag_timewarn = 1;
160                 printf("clock went backwards, not PAG throttling");
161             }
162             rv = 0;
163         }
164     }
165
166     return rv;
167 }
168
169 static int
170 afs_pag_wait(AFS_UCRED **acred)
171 {
172     if (afs_pag_sleep(acred)) {
173         if (!afs_pag_sleepcnt) {
174             printf("%s() PAG throttling triggered, pid %d... sleeping.  sleepcnt %d\n",
175                    "afs_pag_wait", osi_getpid(), afs_pag_sleepcnt);
176         }
177
178         afs_pag_sleepcnt++;
179
180         do {
181             /* XXX spins on EINTR */
182             afs_osi_Wait(1000, (struct afs_osi_WaitHandle *)0, 0);
183         } while (afs_pag_sleep(acred));
184
185         afs_pag_sleepcnt--;
186     }
187
188     return 0;
189 }
190
191 int
192 #if     defined(AFS_SUN5_ENV)
193 afs_setpag(AFS_UCRED **credpp)
194 #elif  defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
195 afs_setpag(struct proc *p, void *args, int *retval)
196 #else
197 afs_setpag(void)
198 #endif
199 {
200
201 #if     defined(AFS_SUN5_ENV)
202     AFS_UCRED **acred = *credpp;
203 #elif  defined(AFS_OBSD_ENV)
204     AFS_UCRED **acred = &p->p_ucred;
205 #else
206     AFS_UCRED **acred = NULL;
207 #endif
208
209     int code = 0;
210
211 #if defined(AFS_SGI53_ENV) && defined(MP)
212     /* This is our first chance to get the global lock. */
213     AFS_GLOCK();
214 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
215
216     AFS_STATCNT(afs_setpag);
217
218     afs_pag_wait(acred);
219
220
221 #if     defined(AFS_SUN5_ENV)
222     code = AddPag(genpag(), credpp);
223 #elif   defined(AFS_OSF_ENV) || defined(AFS_XBSD_ENV)
224     code = AddPag(p, genpag(), &p->p_rcred);
225 #elif   defined(AFS_AIX41_ENV)
226     {
227         struct ucred *credp;
228         struct ucred *credp0;
229
230         credp = crref();
231         credp0 = credp;
232         code = AddPag(genpag(), &credp);
233         /* If AddPag() didn't make a new cred, then free our cred ref */
234         if (credp == credp0) {
235             crfree(credp);
236         }
237     }
238 #elif   defined(AFS_HPUX110_ENV)
239     {
240         struct ucred *credp = p_cred(u.u_procp);
241         code = AddPag(genpag(), &credp);
242     }
243 #elif   defined(AFS_SGI_ENV)
244     {
245         cred_t *credp;
246         credp = OSI_GET_CURRENT_CRED();
247         code = AddPag(genpag(), &credp);
248     }
249 #elif   defined(AFS_LINUX20_ENV)
250     {
251         AFS_UCRED *credp = crref();
252         code = AddPag(genpag(), &credp);
253         crfree(credp);
254     }
255 #elif defined(AFS_DARWIN80_ENV)
256     {
257         AFS_UCRED *credp = kauth_cred_proc_ref(p);
258         code = AddPag(p, genpag(), &credp);
259         crfree(credp);
260     }
261 #elif defined(AFS_DARWIN_ENV)
262     {
263         AFS_UCRED *credp = crdup(p->p_cred->pc_ucred);
264         code = AddPag(p, genpag(), &credp);
265         crfree(credp);
266     }
267 #else
268     code = AddPag(genpag(), &u.u_cred);
269 #endif
270
271     afs_Trace1(afs_iclSetp, CM_TRACE_SETPAG, ICL_TYPE_INT32, code);
272
273 #if defined(KERNEL_HAVE_UERROR)
274     if (!getuerror())
275         setuerror(code);
276 #endif
277
278 #if defined(AFS_SGI53_ENV) && defined(MP)
279     AFS_GUNLOCK();
280 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
281
282     return (code);
283 }
284
285 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
286 /*
287  * afs_setpag_val
288  * This function is like setpag but sets the current thread's pag id to a
289  * caller-provided value instead of calling genpag().  This implements a
290  * form of token caching since the caller can recall a particular pag value
291  * for the thread to restore tokens, rather than reauthenticating.
292  */
293 int
294 #if     defined(AFS_SUN5_ENV)
295 afs_setpag_val(AFS_UCRED **credpp, int pagval)
296 #elif  defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
297 afs_setpag_val(struct proc *p, void *args, int *retval, int pagval)
298 #else
299 afs_setpag_val(int pagval)
300 #endif
301 {
302
303 #if     defined(AFS_SUN5_ENV)
304     AFS_UCRED **acred = *credp;
305 #elif  defined(AFS_OBSD_ENV)
306     AFS_UCRED **acred = &p->p_ucred;
307 #else
308     AFS_UCRED **acred = NULL;
309 #endif
310
311     int code = 0;
312
313 #if defined(AFS_SGI53_ENV) && defined(MP)
314     /* This is our first chance to get the global lock. */
315     AFS_GLOCK();
316 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
317
318     AFS_STATCNT(afs_setpag);
319
320     afs_pag_wait(acred);
321
322 #if     defined(AFS_SUN5_ENV)
323     code = AddPag(pagval, credpp);
324 #elif   defined(AFS_OSF_ENV) || defined(AFS_XBSD_ENV)
325     code = AddPag(p, pagval, &p->p_rcred);
326 #elif   defined(AFS_AIX41_ENV)
327     {
328         struct ucred *credp;
329         struct ucred *credp0;
330
331         credp = crref();
332         credp0 = credp;
333         code = AddPag(pagval, &credp);
334         /* If AddPag() didn't make a new cred, then free our cred ref */
335         if (credp == credp0) {
336             crfree(credp);
337         }
338     }
339 #elif   defined(AFS_HPUX110_ENV)
340     {
341         struct ucred *credp = p_cred(u.u_procp);
342         code = AddPag(pagval, &credp);
343     }
344 #elif   defined(AFS_SGI_ENV)
345     {
346         cred_t *credp;
347         credp = OSI_GET_CURRENT_CRED();
348         code = AddPag(pagval, &credp);
349     }
350 #elif   defined(AFS_LINUX20_ENV)
351     {
352         AFS_UCRED *credp = crref();
353         code = AddPag(pagval, &credp);
354         crfree(credp);
355     }
356 #elif defined(AFS_DARWIN_ENV)
357     {
358         struct ucred *credp = crdup(p->p_cred->pc_ucred);
359         code = AddPag(p, pagval, &credp);
360         crfree(credp);
361     }
362 #else
363     code = AddPag(pagval, &u.u_cred);
364 #endif
365
366     afs_Trace1(afs_iclSetp, CM_TRACE_SETPAG, ICL_TYPE_INT32, code);
367 #if defined(KERNEL_HAVE_UERROR)
368     if (!getuerror())
369         setuerror(code);
370 #endif
371 #if defined(AFS_SGI53_ENV) && defined(MP)
372     AFS_GUNLOCK();
373 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
374     return (code);
375 }
376
377 #ifndef AFS_LINUX26_ONEGROUP_ENV
378 int
379 afs_getpag_val(void)
380 {
381     int pagvalue;
382     AFS_UCRED *credp = u.u_cred;
383     gid_t gidset0, gidset1;
384 #ifdef AFS_SUN510_ENV
385     const gid_t *gids;
386
387     gids = crgetgroups(*credp);
388     gidset0 = gids[0];
389     gidset1 = gids[1];
390 #else
391     gidset0 = credp->cr_groups[0];
392     gidset1 = credp->cr_groups[1];
393 #endif
394     pagvalue = afs_get_pag_from_groups(gidset0, gidset1);
395     return pagvalue;
396 }
397 #endif
398 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
399
400
401 /* Note - needs to be available on AIX, others can be static - rework this */
402 #if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
403 int
404 AddPag(struct proc *p, afs_int32 aval, AFS_UCRED **credpp)
405 #else
406 int
407 AddPag(afs_int32 aval, AFS_UCRED **credpp)
408 #endif
409 {
410     afs_int32 code;
411     afs_uint32 newpag;
412
413     AFS_STATCNT(AddPag);
414 #if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
415     if ((code = setpag(p, credpp, aval, &newpag, 0)))
416 #else
417     if ((code = setpag(credpp, aval, &newpag, 0)))
418 #endif
419 #if defined(KERNEL_HAVE_UERROR)
420         return (setuerror(code), code);
421 #else
422         return (code);
423 #endif
424     return 0;
425 }
426
427
428 int
429 afs_InitReq(register struct vrequest *av, AFS_UCRED *acred)
430 {
431 #if defined(AFS_LINUX26_ENV) && !defined(AFS_NONFSTRANS)
432     int code;
433 #endif
434     int i = 0;
435
436     AFS_STATCNT(afs_InitReq);
437     memset(av, 0, sizeof(*av));
438     if (afs_shuttingdown)
439         return EIO;
440
441     av->idleError = 0;
442     av->tokenError = 0;
443     while (i < MAXHOSTS) {
444       av->skipserver[i] = 0;
445       i++;
446     }
447 #ifdef AFS_LINUX26_ENV
448 #if !defined(AFS_NONFSTRANS)
449     if (osi_linux_nfs_initreq(av, acred, &code))
450         return code;
451 #endif
452 #endif
453
454     av->uid = PagInCred(acred);
455     if (av->uid == NOPAG) {
456         /* Afs doesn't use the unix uid for anuthing except a handle
457          * with which to find the actual authentication tokens so I
458          * think it's ok to use the real uid to make setuid
459          * programs (without setpag) to work properly.
460          */
461 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
462         if (acred == NOCRED)
463             av->uid = -2;       /* XXX nobody... ? */
464         else
465             av->uid = acred->cr_uid;    /* bsd creds don't have ruid */
466 #elif defined(AFS_SUN510_ENV)
467         av->uid = crgetruid(acred);
468 #else
469         av->uid = acred->cr_ruid;       /* default when no pag is set */
470 #endif
471     }
472     av->initd = 0;
473     return 0;
474 }
475
476
477 #ifdef AFS_LINUX26_ONEGROUP_ENV
478 afs_uint32
479 afs_get_pag_from_groups(struct group_info *group_info)
480 {
481     afs_uint32 g0 = 0;
482     afs_uint32 i;
483
484     AFS_STATCNT(afs_get_pag_from_groups);
485     for (i = 0; (i < group_info->ngroups && 
486                  (g0 = GROUP_AT(group_info, i)) != (gid_t) NOGROUP); i++) {
487         if (((g0 >> 24) & 0xff) == 'A')
488             return g0;
489     }
490     return NOPAG;
491 }
492 #else
493 afs_uint32
494 afs_get_pag_from_groups(gid_t g0a, gid_t g1a)
495 {
496     afs_uint32 g0 = g0a;
497     afs_uint32 g1 = g1a;
498     afs_uint32 h, l, ret;
499
500     AFS_STATCNT(afs_get_pag_from_groups);
501
502     g0 -= 0x3f00;
503     g1 -= 0x3f00;
504     if (g0 < 0xc000 && g1 < 0xc000) {
505         l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
506         h = (g0 >> 14);
507         h = (g1 >> 14) + h + h + h;
508         ret = ((h << 28) | l);
509 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
510         return ret;
511 #else
512         /* Additional testing */
513         if (((ret >> 24) & 0xff) == 'A')
514             return ret;
515 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
516     }
517     return NOPAG;
518 }
519 #endif
520
521 void
522 afs_get_groups_from_pag(afs_uint32 pag, gid_t * g0p, gid_t * g1p)
523 {
524 #ifndef AFS_LINUX26_ONEGROUP_ENV
525     unsigned short g0, g1;
526 #endif
527
528
529     AFS_STATCNT(afs_get_groups_from_pag);
530 #ifdef AFS_LINUX26_ONEGROUP_ENV
531     *g0p = pag;
532     *g1p = 0;
533 #else
534 #if !defined(UKERNEL) || !defined(AFS_WEB_ENHANCEMENTS)
535     pag &= 0x7fffffff;
536 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
537     g0 = 0x3fff & (pag >> 14);
538     g1 = 0x3fff & pag;
539     g0 |= ((pag >> 28) / 3) << 14;
540     g1 |= ((pag >> 28) % 3) << 14;
541     *g0p = g0 + 0x3f00;
542     *g1p = g1 + 0x3f00;
543 #endif
544 }
545
546
547 afs_int32
548 PagInCred(AFS_UCRED *cred)
549 {
550     afs_int32 pag;
551 #if !defined(AFS_LINUX26_ONEGROUP_ENV)
552     gid_t g0, g1;
553 #endif
554 #if defined(AFS_SUN510_ENV)
555     const gid_t *gids;
556     int ngroups;
557 #endif
558
559     AFS_STATCNT(PagInCred);
560     if (cred == NULL || cred == afs_osi_credp) {
561         return NOPAG;
562     }
563 #if defined(AFS_SUN510_ENV)
564     gids = crgetgroups(cred);
565     ngroups = crgetngroups(cred);
566 #endif
567 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
568     if (cred == NOCRED || cred == FSCRED) {
569         return NOPAG;
570     }
571     if (cred->cr_ngroups < 3)
572         return NOPAG;
573     /* gid is stored in cr_groups[0] */
574     g0 = cred->cr_groups[1];
575     g1 = cred->cr_groups[2];
576 #else
577 #if defined(AFS_AIX_ENV)
578     if (cred->cr_ngrps < 2) {
579         return NOPAG;
580     }
581 #elif defined(AFS_LINUX26_ENV)
582     if (cred->cr_group_info->ngroups < NUMPAGGROUPS) {
583         pag = NOPAG;
584         goto out;
585     }
586 #elif defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_DUX40_ENV) || defined(AFS_LINUX20_ENV) || defined(AFS_XBSD_ENV)
587 #if defined(AFS_SUN510_ENV)
588     if (ngroups < 2) {
589 #else
590     if (cred->cr_ngroups < 2) {
591 #endif
592         pag = NOPAG;
593         goto out;
594     }
595 #endif
596 #if defined(AFS_AIX51_ENV)
597     g0 = cred->cr_groupset.gs_union.un_groups[0];
598     g1 = cred->cr_groupset.gs_union.un_groups[1];
599 #elif defined(AFS_LINUX26_ONEGROUP_ENV)
600 #elif defined(AFS_LINUX26_ENV)
601     g0 = GROUP_AT(cred->cr_group_info, 0);
602     g1 = GROUP_AT(cred->cr_group_info, 1);
603 #elif defined(AFS_SUN510_ENV)
604     g0 = gids[0];
605     g1 = gids[1];
606 #else
607     g0 = cred->cr_groups[0];
608     g1 = cred->cr_groups[1];
609 #endif
610 #endif
611 #if defined(AFS_LINUX26_ONEGROUP_ENV)
612     pag = (afs_int32) afs_get_pag_from_groups(cred->cr_group_info);
613 #else
614     pag = (afs_int32) afs_get_pag_from_groups(g0, g1);
615 #endif
616 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_DUX40_ENV) || defined(AFS_LINUX20_ENV) || defined(AFS_XBSD_ENV)
617 out:
618 #endif
619 #if defined(AFS_LINUX26_ENV) && defined(LINUX_KEYRING_SUPPORT)
620     if (pag == NOPAG && cred->cr_rgid != NFSXLATOR_CRED) {
621         struct key *key;
622         afs_uint32 upag, newpag;
623
624         key = request_key(&key_type_afs_pag, "_pag", NULL);
625         if (!IS_ERR(key)) {
626             if (key_validate(key) == 0 && key->uid == 0) {      /* also verify in the session keyring? */
627                 upag = (afs_uint32) key->payload.value;
628                 if (((upag >> 24) & 0xff) == 'A') {
629                     __setpag(&cred, upag, &newpag, 0);
630                     pag = (afs_int32) upag;
631                 }
632             }
633             key_put(key);
634         } 
635     }
636 #endif
637     return pag;
638 }