Linux 4.4: key_payload has no member 'value'
[openafs.git] / src / afs / LINUX / osi_groups.c
index bf9f8a0..a83bcb9 100644 (file)
 #include "afsincludes.h"
 #include "afs/afs_stats.h"     /* statistics */
 #include "afs/nfsclient.h"
-#ifdef AFS_LINUX22_ENV
-#include "h/smp_lock.h"
-#endif
+#include "osi_compat.h"
 
 #ifdef AFS_LINUX26_ONEGROUP_ENV
-#define NUMPAGGROUPS 1
-#else
-#define NUMPAGGROUPS 2
-#endif
+# define NUMPAGGROUPS 1
 
-#if defined(AFS_LINUX26_ENV)
-static int
-afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
-{
-    struct group_info *old_info;
-
-    AFS_STATCNT(afs_setgroups);
-
-    old_info = (*cr)->cr_group_info;
-    get_group_info(group_info);
-    (*cr)->cr_group_info = group_info;
-    put_group_info(old_info);
+static afs_uint32
+afs_linux_pag_from_groups(struct group_info *group_info) {
+    afs_uint32 g0 = 0;
+    afs_uint32 i;
 
-    crset(*cr);
+    if (group_info->ngroups < NUMPAGGROUPS)
+       return NOPAG;
 
-#if defined(STRUCT_TASK_STRUCT_HAS_PARENT) && !defined(STRUCT_TASK_HAS_CRED)
-    if (change_parent) {
-       old_info = current->parent->group_info;
-       get_group_info(group_info);
-       current->parent->group_info = group_info;
-       put_group_info(old_info);
+    for (i = 0; i < group_info->ngroups; i++) {
+       g0 = afs_from_kgid(GROUP_AT(group_info, i));
+       if (((g0 >> 24) & 0xff) == 'A')
+           return g0;
     }
-#endif
-
-    return (0);
+    return NOPAG;
 }
-#else
-static int
-afs_setgroups(cred_t **cr, int ngroups, gid_t * gidset, int change_parent)
-{
-    int ngrps;
-    int i;
-    gid_t *gp;
 
-    AFS_STATCNT(afs_setgroups);
+static inline void
+afs_linux_pag_to_groups(afs_uint32 newpag,
+                       struct group_info *old, struct group_info **new) {
+    int need_space = 0;
+    int i;
+    int j;
+    afs_kgid_t newkgid = afs_make_kgid(newpag);
 
-    if (ngroups > NGROUPS)
-       return EINVAL;
+    if (afs_linux_pag_from_groups(old) == NOPAG)
+       need_space = NUMPAGGROUPS;
 
-    gp = (*cr)->cr_groups;
-    if (ngroups < NGROUPS)
-       gp[ngroups] = (gid_t) NOGROUP;
+    *new = groups_alloc(old->ngroups + need_space);
 
-    for (i = ngroups; i > 0; i--) {
-       *gp++ = *gidset++;
+    for (i = 0, j = 0; i < old->ngroups; ++i) {
+       afs_kgid_t ths = GROUP_AT(old, i);
+       if ((afs_from_kgid(ths) >> 24) == 'A')
+           continue;
+       if ((i == 0 || !gid_lt(newkgid, GROUP_AT(old, i-1))) &&
+           gid_lt(newkgid, ths)) {
+          GROUP_AT(*new, j) = newkgid;
+          j++;
+       }
+       GROUP_AT(*new, j) = ths;
+       j++;
     }
-
-    (*cr)->cr_ngroups = ngroups;
-    crset(*cr);
-    return (0);
+    if (j != i + need_space)
+        GROUP_AT(*new, j) = newkgid;
 }
-#endif
 
-#if defined(AFS_LINUX26_ENV)
-static struct group_info *
-afs_getgroups(cred_t * cr)
-{
-    AFS_STATCNT(afs_getgroups);
+#else
+# define NUMPAGGROUPS 2
+
+static inline afs_uint32
+afs_linux_pag_from_groups(struct group_info *group_info) {
 
-    get_group_info(cr->cr_group_info);
-    return cr->cr_group_info;
+    if (group_info->ngroups < NUMPAGGROUPS)
+       return NOPAG;
+
+    return afs_get_pag_from_groups(GROUP_AT(group_info, 0), GROUP_AT(group_info, 1));
 }
-#else
-/* Returns number of groups. And we trust groups to be large enough to
- * hold all the groups.
- */
-static int
-afs_getgroups(cred_t *cr, gid_t *groups)
-{
+
+static inline void
+afs_linux_pag_to_groups(afs_uint32 newpag,
+                       struct group_info *old, struct group_info **new) {
+    int need_space = 0;
     int i;
-    int n;
-    gid_t *gp;
+    gid_t g0;
+    gid_t g1;
+
+    if (afs_linux_pag_from_groups(old) == NOPAG)
+       need_space = NUMPAGGGROUPS;
 
-    AFS_STATCNT(afs_getgroups);
+    *new = groups_alloc(old->ngroups + need_space);
 
-    gp = cr->cr_groups;
-    n = cr->cr_ngroups;
+    for (i = 0; i < old->ngroups; ++i)
+          GROUP_AT(new, i + need_space) = GROUP_AT(old, i);
 
-    for (i = 0; (i < n) && (*gp != (gid_t) NOGROUP); i++)
-       *groups++ = *gp++;
-    return i;
+    afs_get_groups_from_pag(newpag, &g0, g1);
+    GROUP_AT(new, 0) = g0;
+    GROUP_AT(new, 1) = g1;
 }
 #endif
 
-#if !defined(AFS_LINUX26_ENV)
-/* Only propogate the PAG to the parent process. Unix's propogate to 
- * all processes sharing the cred.
- */
-int
-set_pag_in_parent(int pag, int g0, int g1)
+afs_int32
+osi_get_group_pag(afs_ucred_t *cred) {
+    return afs_linux_pag_from_groups(afs_cr_group_info(cred));
+}
+
+
+static int
+afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
 {
-    int i;
-#ifdef STRUCT_TASK_STRUCT_HAS_PARENT
-    gid_t *gp = current->parent->groups;
-    int ngroups = current->parent->ngroups;
-#else
-    gid_t *gp = current->p_pptr->groups;
-    int ngroups = current->p_pptr->ngroups;
-#endif
+    struct group_info *old_info;
 
-    if ((ngroups < 2) || (afs_get_pag_from_groups(gp[0], gp[1]) == NOPAG)) {
-       /* We will have to shift grouplist to make room for pag */
-       if (ngroups + 2 > NGROUPS) {
-           return EINVAL;
-       }
-       for (i = ngroups - 1; i >= 0; i--) {
-           gp[i + 2] = gp[i];
-       }
-       ngroups += 2;
-    }
-    gp[0] = g0;
-    gp[1] = g1;
-    if (ngroups < NGROUPS)
-       gp[ngroups] = NOGROUP;
+    AFS_STATCNT(afs_setgroups);
 
-#ifdef STRUCT_TASK_STRUCT_HAS_PARENT
-    current->parent->ngroups = ngroups;
-#else
-    current->p_pptr->ngroups = ngroups;
+    old_info = afs_cr_group_info(*cr);
+    get_group_info(group_info);
+    afs_set_cr_group_info(*cr, group_info);
+    put_group_info(old_info);
+
+    crset(*cr);
+
+#if defined(STRUCT_TASK_STRUCT_HAS_PARENT) && !defined(STRUCT_TASK_STRUCT_HAS_CRED)
+    if (change_parent) {
+       old_info = current->parent->group_info;
+       get_group_info(group_info);
+       current->parent->group_info = group_info;
+       put_group_info(old_info);
+    }
 #endif
-    return 0;
+
+    return (0);
 }
-#endif
 
-#if defined(AFS_LINUX26_ENV)
 int
 __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
-         int change_parent)
+         int change_parent, struct group_info **old_groups)
 {
     struct group_info *group_info;
-#ifndef AFS_LINUX26_ONEGROUP_ENV
-    gid_t g0, g1;
-#endif
     struct group_info *tmp;
-    int i;
-#ifdef AFS_LINUX26_ONEGROUP_ENV
-    int j;
-#endif
-    int need_space = 0;
 
-    group_info = afs_getgroups(*cr);
-    if (group_info->ngroups < NUMPAGGROUPS
-       ||  afs_get_pag_from_groups(
-#ifdef AFS_LINUX26_ONEGROUP_ENV
-           group_info
-#else
-           GROUP_AT(group_info, 0) ,GROUP_AT(group_info, 1)
-#endif
-                                   ) == NOPAG) 
-       /* We will have to make sure group_info is big enough for pag */
-       need_space = NUMPAGGROUPS;
+    get_group_info(afs_cr_group_info(*cr));
+    group_info = afs_cr_group_info(*cr);
 
-    tmp = groups_alloc(group_info->ngroups + need_space);
-    
     *newpag = (pagvalue == -1 ? genpag() : pagvalue);
-#ifdef AFS_LINUX26_ONEGROUP_ENV
-    for (i = 0, j = 0; i < group_info->ngroups; ++i) {
-       int ths = GROUP_AT(group_info, i);
-       int last = i > 0 ? GROUP_AT(group_info, i-1) : 0;
-       if ((ths >> 24) == 'A')
-           continue;
-       if (last <= *newpag && ths > *newpag) {
-          GROUP_AT(tmp, j) = *newpag;
-          j++;
-       }
-       GROUP_AT(tmp, j) = ths;
-       j++;
-    }
-    if (j != i + need_space)
-        GROUP_AT(tmp, j) = *newpag;
-#else
-    for (i = 0; i < group_info->ngroups; ++i)
-      GROUP_AT(tmp, i + need_space) = GROUP_AT(group_info, i);
-#endif
-    put_group_info(group_info);
-    group_info = tmp;
+    afs_linux_pag_to_groups(*newpag, group_info, &tmp);
 
-#ifndef AFS_LINUX26_ONEGROUP_ENV
-    afs_get_groups_from_pag(*newpag, &g0, &g1);
-    GROUP_AT(group_info, 0) = g0;
-    GROUP_AT(group_info, 1) = g1;
-#endif
+    if (old_groups) {
+       *old_groups = group_info;
+    } else {
+       put_group_info(group_info);
+       group_info = NULL;
+    }
 
-    afs_setgroups(cr, group_info, change_parent);
+    afs_setgroups(cr, tmp, change_parent);
 
-    put_group_info(group_info);
+    put_group_info(tmp);
 
     return 0;
 }
@@ -228,13 +171,14 @@ __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
 extern struct key_type key_type_keyring __attribute__((weak));
 static struct key_type *__key_type_keyring = &key_type_keyring;
 
+/* install_session_keyring returns negative error values */
 static int
 install_session_keyring(struct key *keyring)
 {
     struct key *old;
     char desc[20];
-    unsigned long not_in_quota;
     int code = -EINVAL;
+    unsigned long flags;
 
     if (!__key_type_keyring)
        return code;
@@ -242,25 +186,22 @@ install_session_keyring(struct key *keyring)
     if (!keyring) {
 
        /* create an empty session keyring */
-       not_in_quota = KEY_ALLOC_IN_QUOTA;
        sprintf(desc, "_ses.%u", current->tgid);
 
-#if defined(KEY_ALLOC_NEEDS_STRUCT_TASK)
-       keyring = key_alloc(__key_type_keyring, desc,
-                           current_uid(), current_gid(), current,
-                           (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
-                           not_in_quota);
-#elif defined(KEY_ALLOC_NEEDS_CRED)
-       keyring = key_alloc(__key_type_keyring, desc,
-                           current_uid(), current_gid(), current_cred(),
-                           (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
-                           not_in_quota);
-#else
-       keyring = key_alloc(__key_type_keyring, desc,
+       /* if we're root, don't count the keyring against our quota. This
+        * avoids starvation issues when dealing with PAM modules that always
+        * setpag() as root */
+       if (capable(CAP_SYS_ADMIN))
+           flags = KEY_ALLOC_NOT_IN_QUOTA;
+       else
+           flags = KEY_ALLOC_IN_QUOTA;
+
+       keyring = afs_linux_key_alloc(
+                           __key_type_keyring, desc,
                            current_uid(), current_gid(),
                            (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
-                           not_in_quota);
-#endif
+                           flags);
+
        if (IS_ERR(keyring)) {
            code = PTR_ERR(keyring);
            goto out;
@@ -274,98 +215,69 @@ install_session_keyring(struct key *keyring)
     }
 
     /* install the keyring */
-    spin_lock_irq(&current->sighand->siglock);
-    old = task_session_keyring(current);
-    smp_wmb();
-    task_session_keyring(current) = keyring;
-    spin_unlock_irq(&current->sighand->siglock);
-
+    old = afs_set_session_keyring(keyring);
     if (old)
-           key_put(old);
+       key_put(old);
 
 out:
     return code;
 }
 #endif /* LINUX_KEYRING_SUPPORT */
 
-#else
-int
-__setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
-         int change_parent)
-{
-    gid_t *gidset;
-    afs_int32 ngroups, code = 0;
-    int j;
-
-    gidset = (gid_t *) osi_Alloc(NGROUPS * sizeof(gidset[0]));
-    ngroups = afs_getgroups(*cr, gidset);
-
-    if (afs_get_pag_from_groups(gidset[0], gidset[1]) == NOPAG) {
-       /* We will have to shift grouplist to make room for pag */
-       if (ngroups + 2 > NGROUPS) {
-           osi_Free((char *)gidset, NGROUPS * sizeof(int));
-           return EINVAL;
-       }
-       for (j = ngroups - 1; j >= 0; j--) {
-           gidset[j + 2] = gidset[j];
-       }
-       ngroups += 2;
-    }
-    *newpag = (pagvalue == -1 ? genpag() : pagvalue);
-    afs_get_groups_from_pag(*newpag, &gidset[0], &gidset[1]);
-    code = afs_setgroups(cr, ngroups, gidset, change_parent);
-
-    /* If change_parent is set, then we should set the pag in the parent as
-     * well.
-     */
-    if (change_parent && !code) {
-       code = set_pag_in_parent(*newpag, gidset[0], gidset[1]);
-    }
-
-    osi_Free((char *)gidset, NGROUPS * sizeof(int));
-    return code;
-}
-#endif
-
-
+/* Error codes from setpag must be positive, otherwise they don't
+ * make it back into userspace properly. Error codes from the
+ * Linux keyring utilities, and from install_session_keyring()
+ * are negative. So we need to be careful to convert them correctly
+ * here
+ */
 int
 setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
        int change_parent)
 {
     int code;
+    struct group_info *old_groups = NULL;
 
     AFS_STATCNT(setpag);
 
-    code = __setpag(cr, pagvalue, newpag, change_parent);
+    code = __setpag(cr, pagvalue, newpag, change_parent, &old_groups);
 
 #ifdef LINUX_KEYRING_SUPPORT
-    if (code == 0 && (*cr)->cr_rgid != NFSXLATOR_CRED) {
-       (void) install_session_keyring(NULL);
-
-       if (current_session_keyring()) {
+    if (code == 0 && afs_cr_rgid(*cr) != NFSXLATOR_CRED) {
+       code = install_session_keyring(NULL);
+       if (code == 0 && current_session_keyring()) {
            struct key *key;
            key_perm_t perm;
 
            perm = KEY_POS_VIEW | KEY_POS_SEARCH;
            perm |= KEY_USR_VIEW | KEY_USR_SEARCH;
 
-#if defined(KEY_ALLOC_NEEDS_STRUCT_TASK)
-           key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, current, perm, 1);
-#elif defined(KEY_ALLOC_NEEDS_CRED)
-           key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, current_cred(), perm, 1);
-#else
-           key = key_alloc(&key_type_afs_pag, "_pag", 0, 0, perm, 1);
-#endif
+           key = afs_linux_key_alloc(&key_type_afs_pag, "_pag", GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, perm, KEY_ALLOC_NOT_IN_QUOTA);
 
            if (!IS_ERR(key)) {
-               key_instantiate_and_link(key, (void *) newpag, sizeof(afs_uint32),
+               code = key_instantiate_and_link(key, (void *) newpag, sizeof(afs_uint32),
                                         current_session_keyring(), NULL);
                key_put(key);
+           } else {
+               code = PTR_ERR(key);
            }
        }
+       if (code)
+           code = -code;
     }
 #endif /* LINUX_KEYRING_SUPPORT */
 
+    if (code) {
+       if (old_groups) {
+           afs_setgroups(cr, old_groups, change_parent);
+           put_group_info(old_groups);
+           old_groups = NULL;
+       }
+       if (*newpag > -1) {
+           afs_MarkUserExpired(*newpag);
+           *newpag = -1;
+       }
+    }
+
     return code;
 }
 
@@ -380,32 +292,28 @@ afs_xsetgroups(int gidsetsize, gid_t * grouplist)
     afs_uint32 junk;
     int old_pag;
 
-    lock_kernel();
     old_pag = PagInCred(cr);
     crfree(cr);
-    unlock_kernel();
 
     code = (*sys_setgroupsp) (gidsetsize, grouplist);
     if (code) {
        return code;
     }
 
-    lock_kernel();
     cr = crref();
     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
        /* re-install old pag if there's room. */
-       code = __setpag(&cr, old_pag, &junk, 0);
+       code = __setpag(&cr, old_pag, &junk, 0, NULL);
     }
     crfree(cr);
-    unlock_kernel();
 
     /* Linux syscall ABI returns errno as negative */
     return (-code);
 }
 
-#if defined(AFS_LINUX24_ENV)
 /* Intercept the standard uid32 system call. */
-extern asmlinkage long (*sys_setgroups32p) (int gidsetsize, gid_t * grouplist);
+extern asmlinkage int (*sys_setgroups32p) (int gidsetsize,
+                                          __kernel_gid32_t * grouplist);
 asmlinkage long
 afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
 {
@@ -414,10 +322,8 @@ afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
     afs_uint32 junk;
     int old_pag;
 
-    lock_kernel();
     old_pag = PagInCred(cr);
     crfree(cr);
-    unlock_kernel();
 
     code = (*sys_setgroups32p) (gidsetsize, grouplist);
 
@@ -425,23 +331,20 @@ afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
        return code;
     }
 
-    lock_kernel();
     cr = crref();
     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
        /* re-install old pag if there's room. */
-       code = __setpag(&cr, old_pag, &junk, 0);
+       code = __setpag(&cr, old_pag, &junk, 0, NULL);
     }
     crfree(cr);
-    unlock_kernel();
 
     /* Linux syscall ABI returns errno as negative */
     return (-code);
 }
-#endif
 
 #if defined(AFS_PPC64_LINUX20_ENV)
 /* Intercept the uid16 system call as used by 32bit programs. */
-extern long (*sys32_setgroupsp)(int gidsetsize, gid_t *grouplist);
+extern asmlinkage long (*sys32_setgroupsp)(int gidsetsize, gid_t *grouplist);
 asmlinkage long afs32_xsetgroups(int gidsetsize, gid_t *grouplist)
 {
     long code;
@@ -449,24 +352,20 @@ asmlinkage long afs32_xsetgroups(int gidsetsize, gid_t *grouplist)
     afs_uint32 junk;
     int old_pag;
     
-    lock_kernel();
     old_pag = PagInCred(cr);
     crfree(cr);
-    unlock_kernel();
     
     code = (*sys32_setgroupsp)(gidsetsize, grouplist);
     if (code) {
        return code;
     }
     
-    lock_kernel();
     cr = crref();
     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
        /* re-install old pag if there's room. */
-       code = __setpag(&cr, old_pag, &junk, 0);
+       code = __setpag(&cr, old_pag, &junk, 0, NULL);
     }
     crfree(cr);
-    unlock_kernel();
     
     /* Linux syscall ABI returns errno as negative */
     return (-code);
@@ -475,7 +374,13 @@ asmlinkage long afs32_xsetgroups(int gidsetsize, gid_t *grouplist)
 
 #if defined(AFS_SPARC64_LINUX20_ENV) || defined(AFS_AMD64_LINUX20_ENV)
 /* Intercept the uid16 system call as used by 32bit programs. */
-extern long (*sys32_setgroupsp) (int gidsetsize, u16 * grouplist);
+#ifdef AFS_AMD64_LINUX20_ENV
+extern asmlinkage long (*sys32_setgroupsp) (int gidsetsize, u16 * grouplist);
+#endif /* AFS_AMD64_LINUX20_ENV */
+#ifdef AFS_SPARC64_LINUX26_ENV
+extern asmlinkage int (*sys32_setgroupsp) (int gidsetsize,
+                                          __kernel_gid32_t * grouplist);
+#endif /* AFS_SPARC64_LINUX26_ENV */
 asmlinkage long
 afs32_xsetgroups(int gidsetsize, u16 * grouplist)
 {
@@ -484,32 +389,33 @@ afs32_xsetgroups(int gidsetsize, u16 * grouplist)
     afs_uint32 junk;
     int old_pag;
     
-    lock_kernel();
     old_pag = PagInCred(cr);
     crfree(cr);
-    unlock_kernel();
     
     code = (*sys32_setgroupsp) (gidsetsize, grouplist);
     if (code) {
        return code;
     }
     
-    lock_kernel();
     cr = crref();
     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
        /* re-install old pag if there's room. */
-       code = __setpag(&cr, old_pag, &junk, 0);
+       code = __setpag(&cr, old_pag, &junk, 0, NULL);
     }
     crfree(cr);
-    unlock_kernel();
     
     /* Linux syscall ABI returns errno as negative */
     return (-code);
 }
 
-#ifdef AFS_LINUX24_ENV
 /* Intercept the uid32 system call as used by 32bit programs. */
-extern long (*sys32_setgroups32p) (int gidsetsize, gid_t * grouplist);
+#ifdef AFS_AMD64_LINUX20_ENV
+extern asmlinkage long (*sys32_setgroups32p) (int gidsetsize, gid_t * grouplist);
+#endif /* AFS_AMD64_LINUX20_ENV */
+#ifdef AFS_SPARC64_LINUX26_ENV
+extern asmlinkage int (*sys32_setgroups32p) (int gidsetsize,
+                                            __kernel_gid32_t * grouplist);
+#endif /* AFS_SPARC64_LINUX26_ENV */
 asmlinkage long
 afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
 {
@@ -518,30 +424,25 @@ afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
     afs_uint32 junk;
     int old_pag;
 
-    lock_kernel();
     old_pag = PagInCred(cr);
     crfree(cr);
-    unlock_kernel();
 
     code = (*sys32_setgroups32p) (gidsetsize, grouplist);
     if (code) {
        return code;
     }
 
-    lock_kernel();
     cr = crref();
     if (old_pag != NOPAG && PagInCred(cr) == NOPAG) {
        /* re-install old pag if there's room. */
-       code = __setpag(&cr, old_pag, &junk, 0);
+       code = __setpag(&cr, old_pag, &junk, 0, NULL);
     }
     crfree(cr);
-    unlock_kernel();
 
     /* Linux syscall ABI returns errno as negative */
     return (-code);
 }
 #endif
-#endif
 
 
 #ifdef LINUX_KEYRING_SUPPORT
@@ -552,43 +453,47 @@ static void afs_pag_describe(const struct key *key, struct seq_file *m)
     seq_printf(m, ": %u", key->datalen);
 }
 
+#if defined(STRUCT_KEY_TYPE_HAS_PREPARSE)
+static int afs_pag_instantiate(struct key *key, struct key_preparsed_payload *prep)
+#else
 static int afs_pag_instantiate(struct key *key, const void *data, size_t datalen)
+#endif
 {
     int code;
     afs_uint32 *userpag, pag = NOPAG;
-#ifndef AFS_LINUX26_ONEGROUP_ENV
-    int g0, g1;
-#endif
 
-    if (key->uid != 0 || key->gid != 0)
+    if (!uid_eq(key->uid, GLOBAL_ROOT_UID) || !gid_eq(key->gid, GLOBAL_ROOT_GID))
        return -EPERM;
 
     code = -EINVAL;
     get_group_info(current_group_info());
 
+#if defined(STRUCT_KEY_TYPE_HAS_PREPARSE)
+    if (prep->datalen != sizeof(afs_uint32) || !prep->data)
+#else
     if (datalen != sizeof(afs_uint32) || !data)
-       goto error;
-
-    if (current_group_info()->ngroups < NUMPAGGROUPS)
+#endif
        goto error;
 
     /* ensure key being set matches current pag */
-#ifdef AFS_LINUX26_ONEGROUP_ENV
-    pag = afs_get_pag_from_groups(current_group_info());
-#else
-    g0 = GROUP_AT(current_group_info(), 0);
-    g1 = GROUP_AT(current_group_info(), 1);
+    pag = afs_linux_pag_from_groups(current_group_info());
 
-    pag = afs_get_pag_from_groups(g0, g1);
-#endif
     if (pag == NOPAG)
        goto error;
 
-    userpag = (afs_uint32 *) data;
+#if defined(STRUCT_KEY_TYPE_HAS_PREPARSE)
+    userpag = (afs_uint32 *)prep->data;
+#else
+    userpag = (afs_uint32 *)data;
+#endif
     if (*userpag != pag)
        goto error;
 
+#if defined(STRUCT_KEY_HAS_PAYLOAD_VALUE)
     key->payload.value = (unsigned long) *userpag;
+#else
+    memcpy(&key->payload, userpag, sizeof(afs_uint32));
+#endif
     key->datalen = sizeof(afs_uint32);
     code = 0;
 
@@ -597,25 +502,35 @@ error:
     return code;
 }
 
+#if !defined(STRUCT_KEY_TYPE_HAS_MATCH_PREPARSE)
+/* Note that we only define a ->match function if struct
+ * key_type.match_preparse does _not_ exist. If key_type.match_preparse does
+ * exist, we would use that to specify an alternative comparison function; but
+ * since we just rely on default behavior, we don't need to actually specify
+ * one. But for kernels with no such match_preparse function, we need to
+ * specify a 'match' function, since there is no default. */
 static int afs_pag_match(const struct key *key, const void *description)
 {
        return strcmp(key->description, description) == 0;
 }
+#endif
 
 static void afs_pag_destroy(struct key *key)
 {
-    afs_uint32 pag = key->payload.value;
-    struct unixuser *pu;
+    afs_uint32 pag;
     int locked = ISAFS_GLOCK();
 
+#if defined(STRUCT_KEY_HAS_PAYLOAD_VALUE)
+    pag = key->payload.value;
+#else
+    memcpy(&pag, &key->payload, sizeof(afs_uint32));
+#endif
+
     if (!locked)
        AFS_GLOCK();
-    pu = afs_FindUser(pag, -1, READ_LOCK);
-    if (pu) {
-       pu->ct.EndTimestamp = 0;
-       pu->tokenTime = 0;
-       afs_PutUser(pu, READ_LOCK);
-    }
+
+    afs_MarkUserExpired(pag);
+
     if (!locked)
        AFS_GUNLOCK();
 }
@@ -624,8 +539,15 @@ struct key_type key_type_afs_pag =
 {
     .name        = "afs_pag",
     .describe    = afs_pag_describe,
+#if defined(STRUCT_KEY_TYPE_HAS_INSTANTIATE_PREP)
+    .instantiate_prep = afs_pag_instantiate,
+    .instantiate = NULL,
+#else
     .instantiate = afs_pag_instantiate,
+#endif
+#if !defined(STRUCT_KEY_TYPE_HAS_MATCH_PREPARSE)
     .match       = afs_pag_match,
+#endif
     .destroy     = afs_pag_destroy,
 };
 
@@ -644,19 +566,19 @@ void osi_keyring_init(void)
      * If that's not available, then keyring based PAGs won't work.
      */
     
-#if defined(EXPORTED_TASKLIST_LOCK) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
+#if defined(EXPORTED_TASKLIST_LOCK) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
     if (__key_type_keyring == NULL) {
 # ifdef EXPORTED_TASKLIST_LOCK
        if (&tasklist_lock)
            read_lock(&tasklist_lock);
 # endif
-# if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
+# if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
 #  ifdef EXPORTED_TASKLIST_LOCK
        else
 #  endif
            rcu_read_lock();
 # endif
-#if defined(EXPORTED_FIND_TASK_BY_PID)
+#if defined(HAVE_LINUX_FIND_TASK_BY_PID)
        p = find_task_by_pid(1);
 #else
        p = find_task_by_vpid(1);
@@ -667,7 +589,7 @@ void osi_keyring_init(void)
        if (&tasklist_lock)
            read_unlock(&tasklist_lock);
 # endif
-# if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(EXPORTED_RCU_READ_LOCK))
+# if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) && defined(HAVE_LINUX_RCU_READ_LOCK))
 #  ifdef EXPORTED_TASKLIST_LOCK
        else
 #  endif
@@ -685,6 +607,37 @@ void osi_keyring_shutdown(void)
     unregister_key_type(&key_type_afs_pag);
 }
 
+afs_int32
+osi_get_keyring_pag(afs_ucred_t *cred)
+{
+    struct key *key;
+    afs_uint32 newpag;
+    afs_int32 keyring_pag = NOPAG;
+
+    if (afs_cr_rgid(cred) != NFSXLATOR_CRED) {
+       key = afs_linux_search_keyring(cred, &key_type_afs_pag);
+
+       if (!IS_ERR(key)) {
+           if (key_validate(key) == 0 && uid_eq(key->uid, GLOBAL_ROOT_UID)) {      /* also verify in the session keyring? */
+#if defined(STRUCT_KEY_HAS_PAYLOAD_VALUE)
+               keyring_pag = key->payload.value;
+#else
+               memcpy(&keyring_pag, &key->payload, sizeof(afs_int32));
+#endif
+               /* Only set PAG in groups if needed,
+                * and the creds are from the current process */
+               if (afs_linux_cred_is_current(cred) &&
+                    ((keyring_pag >> 24) & 0xff) == 'A' &&
+                   keyring_pag != afs_linux_pag_from_groups(current_group_info())) {
+                       __setpag(&cred, keyring_pag, &newpag, 0, NULL);
+               }
+           }
+           key_put(key);
+       }
+    }
+    return keyring_pag;
+}
+
 #else
 void osi_keyring_init(void)
 {