2 * Copyright 2000, International Business Machines Corporation and others.
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
17 * afs_get_pag_from_groups
18 * afs_get_groups_from_pag
22 #include <afsconfig.h>
23 #include "afs/param.h"
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"
34 /* Imported variables */
35 extern int afs_shuttingdown;
37 /* Exported variables */
39 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
40 afs_uint32 pagCounter = 1;
42 afs_uint32 pagCounter = 0;
43 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
45 #ifdef AFS_LINUX26_ONEGROUP_ENV
46 #define NUMPAGGROUPS 1
48 #define NUMPAGGROUPS 2
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.
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.
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.
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).
76 #if !defined(UKERNEL) || !defined(AFS_WEB_ENHANCEMENTS)
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 */
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));
97 return (('A' << 24) + (pagCounter & 0xffffff));
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. */
110 #ifdef AFS_LINUX20_ENV
111 return (pag_epoch + pagCounter++);
113 return (pagCounter++);
114 #endif /* AFS_LINUX20_ENV */
121 #ifdef AFS_LINUX20_ENV
122 /* Ensure unique PAG's (mod 200 days) when reloading the client. */
123 return (pag_epoch + pagCounter);
128 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
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.
145 static int afs_pag_sleepcnt = 0;
146 static int afs_pag_timewarn = 0;
149 afs_pag_sleep(afs_ucred_t **acred)
153 if (!afs_suser(acred)) {
154 if(osi_Time() - pag_epoch < pagCounter) {
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");
170 afs_pag_wait(afs_ucred_t **acred)
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);
181 /* XXX spins on EINTR */
182 afs_osi_Wait(1000, (struct afs_osi_WaitHandle *)0, 0);
183 } while (afs_pag_sleep(acred));
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_DARWIN_ENV) || defined(AFS_XBSD_ENV)
197 afs_setpag(afs_proc_t *p, void *args, int *retval)
203 #if defined(AFS_SUN5_ENV)
204 afs_ucred_t **acred = *credpp;
205 #elif defined(AFS_OBSD_ENV)
206 afs_ucred_t **acred = &p->p_ucred;
208 afs_ucred_t **acred = NULL;
213 #if defined(AFS_SGI53_ENV) && defined(MP)
214 /* This is our first chance to get the global lock. */
216 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
218 AFS_STATCNT(afs_setpag);
223 #if defined(AFS_SUN5_ENV)
224 code = AddPag(genpag(), credpp);
225 #elif defined(AFS_FBSD_ENV)
226 code = AddPag(td, genpag(), &td->td_ucred);
227 #elif defined(AFS_XBSD_ENV)
228 code = AddPag(p, genpag(), &p->p_rcred);
229 #elif defined(AFS_AIX41_ENV)
232 struct ucred *credp0;
236 code = AddPag(genpag(), &credp);
237 /* If AddPag() didn't make a new cred, then free our cred ref */
238 if (credp == credp0) {
242 #elif defined(AFS_HPUX110_ENV)
244 struct ucred *credp = p_cred(u.u_procp);
245 code = AddPag(genpag(), &credp);
247 #elif defined(AFS_SGI_ENV)
250 credp = OSI_GET_CURRENT_CRED();
251 code = AddPag(genpag(), &credp);
253 #elif defined(AFS_LINUX20_ENV)
255 afs_ucred_t *credp = crref();
256 code = AddPag(genpag(), &credp);
259 #elif defined(AFS_DARWIN80_ENV)
261 afs_ucred_t *credp = kauth_cred_proc_ref(p);
262 code = AddPag(p, genpag(), &credp);
265 #elif defined(AFS_DARWIN_ENV)
267 afs_ucred_t *credp = crdup(p->p_cred->pc_ucred);
268 code = AddPag(p, genpag(), &credp);
271 #elif defined(UKERNEL)
272 code = AddPag(genpag(), &(get_user_struct()->u_cred));
274 code = AddPag(genpag(), &u.u_cred);
277 afs_Trace1(afs_iclSetp, CM_TRACE_SETPAG, ICL_TYPE_INT32, code);
279 #if defined(KERNEL_HAVE_UERROR)
284 #if defined(AFS_SGI53_ENV) && defined(MP)
286 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
291 #if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
294 * This function is like setpag but sets the current thread's pag id to a
295 * caller-provided value instead of calling genpag(). This implements a
296 * form of token caching since the caller can recall a particular pag value
297 * for the thread to restore tokens, rather than reauthenticating.
300 #if defined(AFS_SUN5_ENV)
301 afs_setpag_val(afs_ucred_t **credpp, int pagval)
302 #elif defined(AFS_FBSD_ENV)
303 afs_setpag_val(struct thread *td, void *args, int pagval)
304 #elif defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
305 afs_setpag_val(afs_proc_t *p, void *args, int *retval, int pagval)
307 afs_setpag_val(int pagval)
311 #if defined(AFS_SUN5_ENV)
312 afs_ucred_t **acred = *credp;
313 #elif defined(AFS_OBSD_ENV)
314 afs_ucred_t **acred = &p->p_ucred;
316 afs_ucred_t **acred = NULL;
321 #if defined(AFS_SGI53_ENV) && defined(MP)
322 /* This is our first chance to get the global lock. */
324 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
326 AFS_STATCNT(afs_setpag);
330 #if defined(AFS_SUN5_ENV)
331 code = AddPag(pagval, credpp);
332 #elif defined(AFS_FBSD_ENV)
333 code = AddPag(td, pagval, &td->td_ucred);
334 #elif defined(AFS_XBSD_ENV)
335 code = AddPag(p, pagval, &p->p_rcred);
336 #elif defined(AFS_AIX41_ENV)
339 struct ucred *credp0;
343 code = AddPag(pagval, &credp);
344 /* If AddPag() didn't make a new cred, then free our cred ref */
345 if (credp == credp0) {
349 #elif defined(AFS_HPUX110_ENV)
351 struct ucred *credp = p_cred(u.u_procp);
352 code = AddPag(pagval, &credp);
354 #elif defined(AFS_SGI_ENV)
357 credp = OSI_GET_CURRENT_CRED();
358 code = AddPag(pagval, &credp);
360 #elif defined(AFS_LINUX20_ENV)
362 afs_ucred_t *credp = crref();
363 code = AddPag(pagval, &credp);
366 #elif defined(AFS_DARWIN_ENV)
368 struct ucred *credp = crdup(p->p_cred->pc_ucred);
369 code = AddPag(p, pagval, &credp);
372 #elif defined(UKERNEL)
373 code = AddPag(pagval, &(get_user_struct()->u_cred));
375 code = AddPag(pagval, &u.u_cred);
378 afs_Trace1(afs_iclSetp, CM_TRACE_SETPAG, ICL_TYPE_INT32, code);
379 #if defined(KERNEL_HAVE_UERROR)
383 #if defined(AFS_SGI53_ENV) && defined(MP)
385 #endif /* defined(AFS_SGI53_ENV) && defined(MP) */
389 #ifndef AFS_LINUX26_ONEGROUP_ENV
395 afs_ucred_t *credp = get_user_struct()->u_cred;
397 afs_ucred_t *credp = u.u_cred;
399 gid_t gidset0, gidset1;
400 #ifdef AFS_SUN510_ENV
403 gids = crgetgroups(*credp);
407 gidset0 = credp->cr_groups[0];
408 gidset1 = credp->cr_groups[1];
410 pagvalue = afs_get_pag_from_groups(gidset0, gidset1);
414 #endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
417 /* Note - needs to be available on AIX, others can be static - rework this */
419 #if defined(AFS_FBSD_ENV)
420 AddPag(struct thread *p, afs_int32 aval, afs_ucred_t **credpp)
421 #elif defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
422 AddPag(afs_proc_t *p, afs_int32 aval, afs_ucred_t **credpp)
424 AddPag(afs_int32 aval, afs_ucred_t **credpp)
431 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
432 if ((code = setpag(p, credpp, aval, &newpag, 0)))
434 if ((code = setpag(credpp, aval, &newpag, 0)))
436 #if defined(KERNEL_HAVE_UERROR)
437 return (setuerror(code), code);
446 afs_InitReq(register struct vrequest *av, afs_ucred_t *acred)
448 #if defined(AFS_LINUX26_ENV) && !defined(AFS_NONFSTRANS)
452 AFS_STATCNT(afs_InitReq);
453 memset(av, 0, sizeof(*av));
454 if (afs_shuttingdown)
457 #ifdef AFS_LINUX26_ENV
458 #if !defined(AFS_NONFSTRANS)
459 if (osi_linux_nfs_initreq(av, acred, &code))
464 av->uid = PagInCred(acred);
465 if (av->uid == NOPAG) {
466 /* Afs doesn't use the unix uid for anuthing except a handle
467 * with which to find the actual authentication tokens so I
468 * think it's ok to use the real uid to make setuid
469 * programs (without setpag) to work properly.
471 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
473 av->uid = -2; /* XXX nobody... ? */
475 av->uid = afs_cr_uid(acred); /* bsd creds don't have ruid */
476 #elif defined(AFS_SUN510_ENV)
477 av->uid = crgetruid(acred);
479 av->uid = afs_cr_uid(acred); /* default when no pag is set */
485 #ifndef AFS_LINUX26_ONEGROUP_ENV
487 afs_get_pag_from_groups(gid_t g0a, gid_t g1a)
491 afs_uint32 h, l, ret;
493 AFS_STATCNT(afs_get_pag_from_groups);
497 if (g0 < 0xc000 && g1 < 0xc000) {
498 l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
500 h = (g1 >> 14) + h + h + h;
501 ret = ((h << 28) | l);
502 # if defined(UKERNEL) && defined(AFS_WEB_ENHANCEMENTS)
505 /* Additional testing */
506 if (((ret >> 24) & 0xff) == 'A')
508 # endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
514 afs_get_groups_from_pag(afs_uint32 pag, gid_t * g0p, gid_t * g1p)
516 unsigned short g0, g1;
518 AFS_STATCNT(afs_get_groups_from_pag);
521 # if !defined(UKERNEL) || !defined(AFS_WEB_ENHANCEMENTS)
523 # endif /* UKERNEL && AFS_WEB_ENHANCEMENTS */
524 g0 = 0x3fff & (pag >> 14);
526 g0 |= ((pag >> 28) / 3) << 14;
527 g1 |= ((pag >> 28) % 3) << 14;
532 void afs_get_groups_from_pag(afs_uint32 pag, gid_t *g0p, gid_t *g1p)
534 AFS_STATCNT(afs_get_groups_from_pag);
540 #ifndef AFS_LINUX26_ENV
542 osi_get_group_pag(afs_ucred_t *cred)
544 afs_int32 pag = NOPAG;
546 #if defined(AFS_SUN510_ENV)
551 #if defined(AFS_SUN510_ENV)
552 gids = crgetgroups(cred);
553 ngroups = crgetngroups(cred);
555 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
556 if (cred == NOCRED || cred == FSCRED)
558 if (cred->cr_ngroups < 3)
560 /* gid is stored in cr_groups[0] */
561 g0 = cred->cr_groups[1];
562 g1 = cred->cr_groups[2];
564 # if defined(AFS_AIX_ENV)
565 if (cred->cr_ngrps < 2)
567 # elif defined(AFS_LINUX26_ENV)
568 if (afs_cr_group_info(cred)->ngroups < NUMPAGGROUPS)
570 # elif defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_LINUX20_ENV) || defined(AFS_XBSD_ENV)
571 # if defined(AFS_SUN510_ENV)
574 if (cred->cr_ngroups < 2) {
579 # if defined(AFS_AIX51_ENV)
580 g0 = cred->cr_groupset.gs_union.un_groups[0];
581 g1 = cred->cr_groupset.gs_union.un_groups[1];
582 #elif defined(AFS_SUN510_ENV)
586 g0 = cred->cr_groups[0];
587 g1 = cred->cr_groups[1];
590 pag = (afs_int32) afs_get_pag_from_groups(g0, g1);
597 PagInCred(afs_ucred_t *cred)
599 afs_int32 pag = NOPAG;
601 AFS_STATCNT(PagInCred);
602 if (cred == NULL || cred == afs_osi_credp) {
605 #if defined(AFS_LINUX26_ENV) && defined(LINUX_KEYRING_SUPPORT)
607 * If linux keyrings are in use and we carry the session keyring in our credentials
608 * structure, they should be the only criteria for determining
609 * if we're in a PAG. Groups are updated for legacy reasons only for now,
610 * and should not be used to infer PAG membership
611 * With keyrings but no kernel credentials, look at groups first and fall back
612 * to looking at the keyrings.
614 # if !defined(STRUCT_TASK_HAS_CRED)
615 pag = osi_get_group_pag(cred);
618 pag = osi_get_keyring_pag(cred);
620 pag = osi_get_group_pag(cred);