libafs: api to create and free vrequests
[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)
40 afs_uint32 pagCounter = 1;
41 #else
42 afs_uint32 pagCounter = 0;
43 #endif /* UKERNEL */
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)
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 */
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_t **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                 afs_warn("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_t **acred)
171 {
172     if (afs_pag_sleep(acred)) {
173         if (!afs_pag_sleepcnt) {
174             afs_warn("%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_t **credpp)
194 #elif   defined(AFS_FBSD_ENV)
195 afs_setpag(struct thread *td, void *args)
196 #elif   defined(AFS_NBSD_ENV)
197 afs_setpag(afs_proc_t *p, const void *args, register_t *retval)
198 #elif  defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
199 afs_setpag(afs_proc_t *p, void *args, int *retval)
200 #else
201 afs_setpag(void)
202 #endif
203 {
204
205 #if     defined(AFS_SUN5_ENV)
206     afs_ucred_t **acred = *credpp;
207 #elif  defined(AFS_OBSD_ENV)
208     afs_ucred_t **acred = &p->p_ucred;
209 #else
210     afs_ucred_t **acred = NULL;
211 #endif
212
213     int code = 0;
214
215 #if defined(AFS_SGI53_ENV) && defined(MP)
216     /* This is our first chance to get the global lock. */
217     AFS_GLOCK();
218 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
219
220     AFS_STATCNT(afs_setpag);
221
222     afs_pag_wait(acred);
223
224
225 #if     defined(AFS_SUN5_ENV)
226     code = AddPag(genpag(), credpp);
227 #elif   defined(AFS_FBSD_ENV)
228     code = AddPag(td, genpag(), &td->td_ucred);
229 #elif   defined(AFS_NBSD40_ENV)
230     code = AddPag(p, genpag(), &p->l_proc->p_cred);
231 #elif   defined(AFS_XBSD_ENV)
232     code = AddPag(p, genpag(), &p->p_rcred);
233 #elif   defined(AFS_AIX41_ENV)
234     {
235         struct ucred *credp;
236         struct ucred *credp0;
237
238         credp = crref();
239         credp0 = credp;
240         code = AddPag(genpag(), &credp);
241         /* If AddPag() didn't make a new cred, then free our cred ref */
242         if (credp == credp0) {
243             crfree(credp);
244         }
245     }
246 #elif   defined(AFS_HPUX110_ENV)
247     {
248         struct ucred *credp = p_cred(u.u_procp);
249         code = AddPag(genpag(), &credp);
250     }
251 #elif   defined(AFS_SGI_ENV)
252     {
253         cred_t *credp;
254         credp = OSI_GET_CURRENT_CRED();
255         code = AddPag(genpag(), &credp);
256     }
257 #elif   defined(AFS_LINUX20_ENV)
258     {
259         afs_ucred_t *credp = crref();
260         code = AddPag(genpag(), &credp);
261         crfree(credp);
262     }
263 #elif defined(AFS_DARWIN80_ENV)
264     {
265         afs_ucred_t *credp = kauth_cred_proc_ref(p);
266         code = AddPag(p, genpag(), &credp);
267         crfree(credp);
268     }
269 #elif defined(AFS_DARWIN_ENV)
270     {
271         afs_ucred_t *credp = crdup(p->p_cred->pc_ucred);
272         code = AddPag(p, genpag(), &credp);
273         crfree(credp);
274     }
275 #elif defined(UKERNEL)
276     code = AddPag(genpag(), &(get_user_struct()->u_cred));
277 #else
278     code = AddPag(genpag(), &u.u_cred);
279 #endif
280
281     afs_Trace1(afs_iclSetp, CM_TRACE_SETPAG, ICL_TYPE_INT32, code);
282
283 #if defined(KERNEL_HAVE_UERROR)
284     if (!getuerror())
285         setuerror(code);
286 #endif
287
288 #if defined(AFS_SGI53_ENV) && defined(MP)
289     AFS_GUNLOCK();
290 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
291
292     return (code);
293 }
294
295 #if defined(UKERNEL)
296 /*
297  * afs_setpag_val
298  * This function is like setpag but sets the current thread's pag id to a
299  * caller-provided value instead of calling genpag().  This implements a
300  * form of token caching since the caller can recall a particular pag value
301  * for the thread to restore tokens, rather than reauthenticating.
302  */
303 int
304 #if     defined(AFS_SUN5_ENV)
305 afs_setpag_val(afs_ucred_t **credpp, int pagval)
306 #elif   defined(AFS_FBSD_ENV)
307 afs_setpag_val(struct thread *td, void *args, int pagval)
308 #elif  defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
309 afs_setpag_val(afs_proc_t *p, void *args, int *retval, int pagval)
310 #else
311 afs_setpag_val(int pagval)
312 #endif
313 {
314
315 #if     defined(AFS_SUN5_ENV)
316     afs_ucred_t **acred = *credp;
317 #elif  defined(AFS_OBSD_ENV)
318     afs_ucred_t **acred = &p->p_ucred;
319 #else
320     afs_ucred_t **acred = NULL;
321 #endif
322
323     int code = 0;
324
325 #if defined(AFS_SGI53_ENV) && defined(MP)
326     /* This is our first chance to get the global lock. */
327     AFS_GLOCK();
328 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
329
330     AFS_STATCNT(afs_setpag);
331
332     afs_pag_wait(acred);
333
334 #if     defined(AFS_SUN5_ENV)
335     code = AddPag(pagval, credpp);
336 #elif   defined(AFS_FBSD_ENV)
337     code = AddPag(td, pagval, &td->td_ucred);
338 #elif   defined(AFS_XBSD_ENV)
339     code = AddPag(p, pagval, &p->p_rcred);
340 #elif   defined(AFS_AIX41_ENV)
341     {
342         struct ucred *credp;
343         struct ucred *credp0;
344
345         credp = crref();
346         credp0 = credp;
347         code = AddPag(pagval, &credp);
348         /* If AddPag() didn't make a new cred, then free our cred ref */
349         if (credp == credp0) {
350             crfree(credp);
351         }
352     }
353 #elif   defined(AFS_HPUX110_ENV)
354     {
355         struct ucred *credp = p_cred(u.u_procp);
356         code = AddPag(pagval, &credp);
357     }
358 #elif   defined(AFS_SGI_ENV)
359     {
360         cred_t *credp;
361         credp = OSI_GET_CURRENT_CRED();
362         code = AddPag(pagval, &credp);
363     }
364 #elif   defined(AFS_LINUX20_ENV)
365     {
366         afs_ucred_t *credp = crref();
367         code = AddPag(pagval, &credp);
368         crfree(credp);
369     }
370 #elif defined(AFS_DARWIN_ENV)
371     {
372         struct ucred *credp = crdup(p->p_cred->pc_ucred);
373         code = AddPag(p, pagval, &credp);
374         crfree(credp);
375     }
376 #elif defined(UKERNEL)
377     code = AddPag(pagval, &(get_user_struct()->u_cred));
378 #else
379     code = AddPag(pagval, &u.u_cred);
380 #endif
381
382     afs_Trace1(afs_iclSetp, CM_TRACE_SETPAG, ICL_TYPE_INT32, code);
383 #if defined(KERNEL_HAVE_UERROR)
384     if (!getuerror())
385         setuerror(code);
386 #endif
387 #if defined(AFS_SGI53_ENV) && defined(MP)
388     AFS_GUNLOCK();
389 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
390     return (code);
391 }
392
393 #ifndef AFS_LINUX26_ONEGROUP_ENV
394 int
395 afs_getpag_val(void)
396 {
397     int pagvalue;
398 #ifdef UKERNEL
399     afs_ucred_t *credp = get_user_struct()->u_cred;
400 #else
401     afs_ucred_t *credp = u.u_cred;
402 #endif
403     gid_t gidset0, gidset1;
404 #ifdef AFS_SUN510_ENV
405     const gid_t *gids;
406
407     gids = crgetgroups(*credp);
408     gidset0 = gids[0];
409     gidset1 = gids[1];
410 #else
411     gidset0 = credp->cr_groups[0];
412     gidset1 = credp->cr_groups[1];
413 #endif
414     pagvalue = afs_get_pag_from_groups(gidset0, gidset1);
415     return pagvalue;
416 }
417 #endif
418 #endif /* UKERNEL */
419
420
421 /* Note - needs to be available on AIX, others can be static - rework this */
422 int
423 #if defined(AFS_FBSD_ENV)
424 AddPag(struct thread *p, afs_int32 aval, afs_ucred_t **credpp)
425 #elif defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
426 AddPag(afs_proc_t *p, afs_int32 aval, afs_ucred_t **credpp)
427 #else
428 AddPag(afs_int32 aval, afs_ucred_t **credpp)
429 #endif
430 {
431     afs_int32 code;
432     afs_uint32 newpag;
433
434     AFS_STATCNT(AddPag);
435 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
436     if ((code = setpag(p, credpp, aval, &newpag, 0)))
437 #else
438     if ((code = setpag(credpp, aval, &newpag, 0)))
439 #endif
440 #if defined(KERNEL_HAVE_UERROR)
441         return (setuerror(code), code);
442 #else
443         return (code);
444 #endif
445     return 0;
446 }
447
448
449 int
450 afs_InitReq(struct vrequest *av, afs_ucred_t *acred)
451 {
452 #if defined(AFS_LINUX26_ENV) && !defined(AFS_NONFSTRANS)
453     int code;
454 #endif
455
456     AFS_STATCNT(afs_InitReq);
457     memset(av, 0, sizeof(*av));
458     if (afs_shuttingdown)
459         return EIO;
460
461 #ifdef AFS_LINUX26_ENV
462 #if !defined(AFS_NONFSTRANS)
463     if (osi_linux_nfs_initreq(av, acred, &code))
464         return code;
465 #endif
466 #endif
467
468     av->uid = PagInCred(acred);
469     if (av->uid == NOPAG) {
470         /* Afs doesn't use the unix uid for anuthing except a handle
471          * with which to find the actual authentication tokens so I
472          * think it's ok to use the real uid to make setuid
473          * programs (without setpag) to work properly.
474          */
475 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
476         if (acred == NOCRED)
477             av->uid = -2;       /* XXX nobody... ? */
478         else
479             av->uid = afs_cr_uid(acred);        /* bsd creds don't have ruid */
480 #elif defined(AFS_SUN510_ENV)
481         av->uid = crgetruid(acred);
482 #else
483         av->uid = afs_cr_ruid(acred);   /* default when no pag is set */
484 #endif
485     }
486     return 0;
487 }
488
489 /*!
490  * Allocate and setup a vrequest.
491  *
492  * \note The caller must free the allocated vrequest with
493  *       afs_DestroyReq() if this function returns successfully (zero).
494  *
495  * \note The GLOCK must be held on platforms which require the GLOCK
496  *       for osi_AllocSmallSpace() and osi_FreeSmallSpace().
497  *
498  * \param[out] avpp   address of the vrequest pointer
499  * \param[in]  acred  user credentials to setup the vrequest; may be NULL
500  * \return     0 on success
501  */
502 int
503 afs_CreateReq(struct vrequest **avpp, afs_ucred_t *acred)
504 {
505     int code;
506     struct vrequest *treq = NULL;
507
508     if (afs_shuttingdown) {
509         return EIO;
510     }
511     if (!avpp) {
512         return EINVAL;
513     }
514     treq = osi_AllocSmallSpace(sizeof(struct vrequest));
515     if (!treq) {
516         return ENOMEM;
517     }
518     if (!acred) {
519         memset(treq, 0, sizeof(struct vrequest));
520     } else {
521         code = afs_InitReq(treq, acred);
522         if (code != 0) {
523             osi_FreeSmallSpace(treq);
524             return code;
525         }
526     }
527     *avpp = treq;
528     return 0;
529 }
530
531 /*!
532  * Deallocate a vrequest.
533  *
534  * \note The GLOCK must be held on platforms which require the GLOCK
535  *       for osi_FreeSmallSpace().
536  *
537  * \param[in]  av  pointer to the vrequest to free; may be NULL
538  */
539 void
540 afs_DestroyReq(struct vrequest *av)
541 {
542     if (av) {
543         osi_FreeSmallSpace(av);
544     }
545 }
546
547 #ifndef AFS_LINUX26_ONEGROUP_ENV
548 afs_uint32
549 afs_get_pag_from_groups(gid_t g0a, gid_t g1a)
550 {
551     afs_uint32 g0 = g0a;
552     afs_uint32 g1 = g1a;
553     afs_uint32 h, l, ret;
554
555     AFS_STATCNT(afs_get_pag_from_groups);
556
557     g0 -= 0x3f00;
558     g1 -= 0x3f00;
559     if (g0 < 0xc000 && g1 < 0xc000) {
560         l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
561         h = (g0 >> 14);
562         h = (g1 >> 14) + h + h + h;
563         ret = ((h << 28) | l);
564 # if defined(UKERNEL)
565         return ret;
566 # else
567         /* Additional testing */
568         if (((ret >> 24) & 0xff) == 'A')
569             return ret;
570 # endif /* UKERNEL */
571     }
572     return NOPAG;
573 }
574
575 void
576 afs_get_groups_from_pag(afs_uint32 pag, gid_t * g0p, gid_t * g1p)
577 {
578     unsigned short g0, g1;
579
580     AFS_STATCNT(afs_get_groups_from_pag);
581     *g0p = pag;
582     *g1p = 0;
583 # if !defined(UKERNEL)
584     pag &= 0x7fffffff;
585 # endif /* UKERNEL */
586     g0 = 0x3fff & (pag >> 14);
587     g1 = 0x3fff & pag;
588     g0 |= ((pag >> 28) / 3) << 14;
589     g1 |= ((pag >> 28) % 3) << 14;
590     *g0p = g0 + 0x3f00;
591     *g1p = g1 + 0x3f00;
592 }
593 #else
594 void afs_get_groups_from_pag(afs_uint32 pag, gid_t *g0p, gid_t *g1p)
595 {
596     AFS_STATCNT(afs_get_groups_from_pag);
597     *g0p = pag;
598     *g1p = 0;
599 }
600 #endif
601
602 #if !defined(AFS_LINUX26_ENV) && !defined(AFS_DARWIN110_ENV)
603 static afs_int32
604 osi_get_group_pag(afs_ucred_t *cred)
605 {
606     afs_int32 pag = NOPAG;
607     gid_t g0, g1;
608 #if defined(AFS_SUN510_ENV)
609     const gid_t *gids;
610     int ngroups;
611 #endif
612
613 #if defined(AFS_SUN510_ENV)
614     gids = crgetgroups(cred);
615     ngroups = crgetngroups(cred);
616 #endif
617 #if defined(AFS_NBSD40_ENV)
618     if (cred == NOCRED || cred == FSCRED)
619       return NOPAG;
620     if (osi_crngroups(cred) < 3)
621       return NOPAG;
622     g0 = osi_crgroupbyid(cred, 1);
623     g1 = osi_crgroupbyid(cred, 2);
624 #elif defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
625     if (cred == NOCRED || cred == FSCRED)
626         return NOPAG;
627     if (cred->cr_ngroups < 3)
628         return NOPAG;
629     /* gid is stored in cr_groups[0] */
630     g0 = cred->cr_groups[1];
631     g1 = cred->cr_groups[2];
632 #else
633 # if defined(AFS_AIX_ENV)
634     if (cred->cr_ngrps < 2)
635         return NOPAG;
636 # elif defined(AFS_LINUX26_ENV)
637     if (afs_cr_group_info(cred)->ngroups < NUMPAGGROUPS)
638         return NOPAG;
639 # elif defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_LINUX20_ENV) || defined(AFS_XBSD_ENV)
640 #  if defined(AFS_SUN510_ENV)
641     if (ngroups < 2) {
642 #  else
643     if (cred->cr_ngroups < 2) {
644 #  endif
645         return NOPAG;
646     }
647 # endif
648 # if defined(AFS_AIX51_ENV)
649     g0 = cred->cr_groupset.gs_union.un_groups[0];
650     g1 = cred->cr_groupset.gs_union.un_groups[1];
651 #elif defined(AFS_SUN510_ENV)
652     g0 = gids[0];
653     g1 = gids[1];
654 #else
655     g0 = cred->cr_groups[0];
656     g1 = cred->cr_groups[1];
657 #endif
658 #endif
659     pag = (afs_int32) afs_get_pag_from_groups(g0, g1);
660     return pag;
661 }
662 #endif
663
664
665 afs_int32
666 PagInCred(afs_ucred_t *cred)
667 {
668     afs_int32 pag = NOPAG;
669
670     AFS_STATCNT(PagInCred);
671     if (cred == NULL || cred == afs_osi_credp) {
672         return NOPAG;
673     }
674 #ifndef AFS_DARWIN110_ENV
675 #if defined(AFS_LINUX26_ENV) && defined(LINUX_KEYRING_SUPPORT)
676     /*
677      * If linux keyrings are in use and we carry the session keyring in our credentials
678      * structure, they should be the only criteria for determining
679      * if we're in a PAG.  Groups are updated for legacy reasons only for now,
680      * and should not be used to infer PAG membership
681      * With keyrings but no kernel credentials, look at groups first and fall back
682      * to looking at the keyrings.
683      */
684 # if !defined(STRUCT_TASK_STRUCT_HAS_CRED)
685     pag = osi_get_group_pag(cred);
686 # endif
687     if (pag == NOPAG)
688         pag = osi_get_keyring_pag(cred);
689 #elif defined(AFS_AIX51_ENV)
690     if (kcred_getpag(cred, PAG_AFS, &pag) < 0 || pag == 0)
691         pag = NOPAG;
692 #else
693     pag = osi_get_group_pag(cred);
694 #endif
695 #endif
696     return pag;
697 }