afs: define NUMPAGGROUPS once
[openafs.git] / src / afs / LINUX / osi_groups.c
index baaff78..ed6ab5a 100644 (file)
 #include "afs/nfsclient.h"
 #include "osi_compat.h"
 
-#include <linux/smp_lock.h>
-
-#if defined(LINUX_KEYRING_SUPPORT)
-# include <linux/rwsem.h>
-# include <linux/key.h>
-# if defined(HAVE_LINUX_KEY_TYPE_H)
-#  include <linux/key-type.h>
-# endif
-# ifndef KEY_ALLOC_IN_QUOTA
-#  define KEY_ALLOC_IN_QUOTA 1
-# endif
-#endif
-
 #ifdef AFS_LINUX26_ONEGROUP_ENV
-# define NUMPAGGROUPS 1
 
 static afs_uint32
 afs_linux_pag_from_groups(struct group_info *group_info) {
     afs_uint32 g0 = 0;
     afs_uint32 i;
 
-    if (group_info->ngroups < NUMPAGGROUPS)
+    if (group_info->ngroups < AFS_NUMPAGGROUPS)
        return NOPAG;
 
-    for (i = 0; (i < group_info->ngroups &&
-                (g0 = GROUP_AT(group_info, i)) != (gid_t) NOGROUP); i++) {
+    for (i = 0; i < group_info->ngroups; i++) {
+       g0 = afs_from_kgid(GROUP_AT(group_info, i));
        if (((g0 >> 24) & 0xff) == 'A')
            return g0;
     }
@@ -64,35 +50,35 @@ afs_linux_pag_to_groups(afs_uint32 newpag,
     int need_space = 0;
     int i;
     int j;
+    afs_kgid_t newkgid = afs_make_kgid(newpag);
 
     if (afs_linux_pag_from_groups(old) == NOPAG)
-       need_space = NUMPAGGROUPS;
+       need_space = AFS_NUMPAGGROUPS;
 
     *new = groups_alloc(old->ngroups + need_space);
 
     for (i = 0, j = 0; i < old->ngroups; ++i) {
-       int ths = GROUP_AT(old, i);
-       int last = i > 0 ? GROUP_AT(old, i-1) : 0;
-       if ((ths >> 24) == 'A')
+       afs_kgid_t ths = GROUP_AT(old, i);
+       if ((afs_from_kgid(ths) >> 24) == 'A')
            continue;
-       if (last <= newpag && ths > newpag) {
-          GROUP_AT(*new, j) = newpag;
+       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++;
     }
     if (j != i + need_space)
-        GROUP_AT(*new, j) = newpag;
+        GROUP_AT(*new, j) = newkgid;
 }
 
 #else
-# define NUMPAGGROUPS 2
 
 static inline afs_uint32
 afs_linux_pag_from_groups(struct group_info *group_info) {
 
-    if (group_info->ngroups < NUMPAGGROUPS)
+    if (group_info->ngroups < AFS_NUMPAGGROUPS)
        return NOPAG;
 
     return afs_get_pag_from_groups(GROUP_AT(group_info, 0), GROUP_AT(group_info, 1));
@@ -100,14 +86,14 @@ afs_linux_pag_from_groups(struct group_info *group_info) {
 
 static inline void
 afs_linux_pag_to_groups(afs_uint32 newpag,
-                       struct group_info *old, struct group_info *new) {
+                       struct group_info *old, struct group_info **new) {
     int need_space = 0;
     int i;
     gid_t g0;
     gid_t g1;
 
     if (afs_linux_pag_from_groups(old) == NOPAG)
-       need_space = NUMPAGGGROUPS;
+       need_space = AFS_NUMPAGGROUPS;
 
     *new = groups_alloc(old->ngroups + need_space);
 
@@ -154,7 +140,7 @@ afs_setgroups(cred_t **cr, struct group_info *group_info, int change_parent)
 
 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;
     struct group_info *tmp;
@@ -165,12 +151,16 @@ __setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
     *newpag = (pagvalue == -1 ? genpag() : pagvalue);
     afs_linux_pag_to_groups(*newpag, group_info, &tmp);
 
-    put_group_info(group_info);
-    group_info = tmp;
+    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;
 }
@@ -179,13 +169,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];
     int code = -EINVAL;
-    int flags;
+    unsigned long flags;
 
     if (!__key_type_keyring)
        return code;
@@ -198,7 +189,7 @@ install_session_keyring(struct key *keyring)
        /* 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 (current_uid() == 0)
+       if (capable(CAP_SYS_ADMIN))
            flags = KEY_ALLOC_NOT_IN_QUOTA;
        else
            flags = KEY_ALLOC_IN_QUOTA;
@@ -222,29 +213,31 @@ 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 */
 
+/* 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 && afs_cr_rgid(*cr) != NFSXLATOR_CRED) {
@@ -256,19 +249,33 @@ setpag(cred_t **cr, afs_uint32 pagvalue, afs_uint32 *newpag,
            perm = KEY_POS_VIEW | KEY_POS_SEARCH;
            perm |= KEY_USR_VIEW | KEY_USR_SEARCH;
 
-           key = afs_linux_key_alloc(&key_type_afs_pag, "_pag", 0, 0, perm, KEY_ALLOC_NOT_IN_QUOTA);
+           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;
 }
 
@@ -294,7 +301,7 @@ afs_xsetgroups(int gidsetsize, gid_t * grouplist)
     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);
 
@@ -325,7 +332,7 @@ afs_xsetgroups32(int gidsetsize, gid_t * grouplist)
     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);
 
@@ -354,7 +361,7 @@ asmlinkage long afs32_xsetgroups(int gidsetsize, gid_t *grouplist)
     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);
     
@@ -391,7 +398,7 @@ afs32_xsetgroups(int gidsetsize, u16 * grouplist)
     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);
     
@@ -426,7 +433,7 @@ afs32_xsetgroups32(int gidsetsize, gid_t * grouplist)
     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);
 
@@ -444,18 +451,26 @@ 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;
 
-    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)
+#endif
        goto error;
 
     /* ensure key being set matches current pag */
@@ -464,11 +479,19 @@ static int afs_pag_instantiate(struct key *key, const void *data, size_t datalen
     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;
 
@@ -477,16 +500,30 @@ 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;
+    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();
 
@@ -500,8 +537,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,
 };
 
@@ -572,14 +616,18 @@ osi_get_keyring_pag(afs_ucred_t *cred)
        key = afs_linux_search_keyring(cred, &key_type_afs_pag);
 
        if (!IS_ERR(key)) {
-           if (key_validate(key) == 0 && key->uid == 0) {      /* also verify in the session keyring? */
+           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);
+                       __setpag(&cred, keyring_pag, &newpag, 0, NULL);
                }
            }
            key_put(key);