提交 76d8aeab 编写于 作者: D David Howells 提交者: Linus Torvalds

[PATCH] keys: Discard key spinlock and use RCU for key payload

The attached patch changes the key implementation in a number of ways:

 (1) It removes the spinlock from the key structure.

 (2) The key flags are now accessed using atomic bitops instead of
     write-locking the key spinlock and using C bitwise operators.

     The three instantiation flags are dealt with with the construction
     semaphore held during the request_key/instantiate/negate sequence, thus
     rendering the spinlock superfluous.

     The key flags are also now bit numbers not bit masks.

 (3) The key payload is now accessed using RCU. This permits the recursive
     keyring search algorithm to be simplified greatly since no locks need be
     taken other than the usual RCU preemption disablement. Searching now does
     not require any locks or semaphores to be held; merely that the starting
     keyring be pinned.

 (4) The keyring payload now includes an RCU head so that it can be disposed
     of by call_rcu(). This requires that the payload be copied on unlink to
     prevent introducing races in copy-down vs search-up.

 (5) The user key payload is now a structure with the data following it. It
     includes an RCU head like the keyring payload and for the same reason. It
     also contains a data length because the data length in the key may be
     changed on another CPU whilst an RCU protected read is in progress on the
     payload. This would then see the supposed RCU payload and the on-key data
     length getting out of sync.

     I'm tempted to drop the key's datalen entirely, except that it's used in
     conjunction with quota management and so is a little tricky to get rid
     of.

 (6) Update the keys documentation.
Signed-Off-By: NDavid Howells <dhowells@redhat.com>
Signed-off-by: NAndrew Morton <akpm@osdl.org>
Signed-off-by: NLinus Torvalds <torvalds@osdl.org>
上级 7286aa9b
此差异已折叠。
...@@ -31,8 +31,10 @@ extern spinlock_t key_serial_lock; ...@@ -31,8 +31,10 @@ extern spinlock_t key_serial_lock;
* subscribed * subscribed
*/ */
struct keyring_list { struct keyring_list {
unsigned maxkeys; /* max keys this list can hold */ struct rcu_head rcu; /* RCU deletion hook */
unsigned nkeys; /* number of keys currently held */ unsigned short maxkeys; /* max keys this list can hold */
unsigned short nkeys; /* number of keys currently held */
unsigned short delkey; /* key to be unlinked by RCU */
struct key *keys[0]; struct key *keys[0];
}; };
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
#include <linux/types.h> #include <linux/types.h>
#include <linux/list.h> #include <linux/list.h>
#include <linux/rbtree.h> #include <linux/rbtree.h>
#include <linux/spinlock.h> #include <linux/rcupdate.h>
#include <asm/atomic.h> #include <asm/atomic.h>
#ifdef __KERNEL__ #ifdef __KERNEL__
...@@ -78,7 +78,6 @@ struct key { ...@@ -78,7 +78,6 @@ struct key {
key_serial_t serial; /* key serial number */ key_serial_t serial; /* key serial number */
struct rb_node serial_node; struct rb_node serial_node;
struct key_type *type; /* type of key */ struct key_type *type; /* type of key */
rwlock_t lock; /* examination vs change lock */
struct rw_semaphore sem; /* change vs change sem */ struct rw_semaphore sem; /* change vs change sem */
struct key_user *user; /* owner of this key */ struct key_user *user; /* owner of this key */
time_t expiry; /* time at which key expires (or 0) */ time_t expiry; /* time at which key expires (or 0) */
...@@ -86,14 +85,10 @@ struct key { ...@@ -86,14 +85,10 @@ struct key {
gid_t gid; gid_t gid;
key_perm_t perm; /* access permissions */ key_perm_t perm; /* access permissions */
unsigned short quotalen; /* length added to quota */ unsigned short quotalen; /* length added to quota */
unsigned short datalen; /* payload data length */ unsigned short datalen; /* payload data length
unsigned short flags; /* status flags (change with lock writelocked) */ * - may not match RCU dereferenced payload
#define KEY_FLAG_INSTANTIATED 0x00000001 /* set if key has been instantiated */ * - payload should contain own length
#define KEY_FLAG_DEAD 0x00000002 /* set if key type has been deleted */ */
#define KEY_FLAG_REVOKED 0x00000004 /* set if key had been revoked */
#define KEY_FLAG_IN_QUOTA 0x00000008 /* set if key consumes quota */
#define KEY_FLAG_USER_CONSTRUCT 0x00000010 /* set if key is being constructed in userspace */
#define KEY_FLAG_NEGATIVE 0x00000020 /* set if key is negative */
#ifdef KEY_DEBUGGING #ifdef KEY_DEBUGGING
unsigned magic; unsigned magic;
...@@ -101,6 +96,14 @@ struct key { ...@@ -101,6 +96,14 @@ struct key {
#define KEY_DEBUG_MAGIC_X 0xf8e9dacbu #define KEY_DEBUG_MAGIC_X 0xf8e9dacbu
#endif #endif
unsigned long flags; /* status flags (change with bitops) */
#define KEY_FLAG_INSTANTIATED 0 /* set if key has been instantiated */
#define KEY_FLAG_DEAD 1 /* set if key type has been deleted */
#define KEY_FLAG_REVOKED 2 /* set if key had been revoked */
#define KEY_FLAG_IN_QUOTA 3 /* set if key consumes quota */
#define KEY_FLAG_USER_CONSTRUCT 4 /* set if key is being constructed in userspace */
#define KEY_FLAG_NEGATIVE 5 /* set if key is negative */
/* the description string /* the description string
* - this is used to match a key against search criteria * - this is used to match a key against search criteria
* - this should be a printable string * - this should be a printable string
...@@ -250,6 +253,8 @@ extern int keyring_add_key(struct key *keyring, ...@@ -250,6 +253,8 @@ extern int keyring_add_key(struct key *keyring,
extern struct key *key_lookup(key_serial_t id); extern struct key *key_lookup(key_serial_t id);
extern void keyring_replace_payload(struct key *key, void *replacement);
#define key_serial(key) ((key) ? (key)->serial : 0) #define key_serial(key) ((key) ? (key)->serial : 0)
/* /*
......
...@@ -294,7 +294,6 @@ struct key *key_alloc(struct key_type *type, const char *desc, ...@@ -294,7 +294,6 @@ struct key *key_alloc(struct key_type *type, const char *desc,
} }
atomic_set(&key->usage, 1); atomic_set(&key->usage, 1);
rwlock_init(&key->lock);
init_rwsem(&key->sem); init_rwsem(&key->sem);
key->type = type; key->type = type;
key->user = user; key->user = user;
...@@ -308,7 +307,7 @@ struct key *key_alloc(struct key_type *type, const char *desc, ...@@ -308,7 +307,7 @@ struct key *key_alloc(struct key_type *type, const char *desc,
key->payload.data = NULL; key->payload.data = NULL;
if (!not_in_quota) if (!not_in_quota)
key->flags |= KEY_FLAG_IN_QUOTA; key->flags |= 1 << KEY_FLAG_IN_QUOTA;
memset(&key->type_data, 0, sizeof(key->type_data)); memset(&key->type_data, 0, sizeof(key->type_data));
...@@ -359,7 +358,7 @@ int key_payload_reserve(struct key *key, size_t datalen) ...@@ -359,7 +358,7 @@ int key_payload_reserve(struct key *key, size_t datalen)
key_check(key); key_check(key);
/* contemplate the quota adjustment */ /* contemplate the quota adjustment */
if (delta != 0 && key->flags & KEY_FLAG_IN_QUOTA) { if (delta != 0 && test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
spin_lock(&key->user->lock); spin_lock(&key->user->lock);
if (delta > 0 && if (delta > 0 &&
...@@ -405,23 +404,17 @@ static int __key_instantiate_and_link(struct key *key, ...@@ -405,23 +404,17 @@ static int __key_instantiate_and_link(struct key *key,
down_write(&key_construction_sem); down_write(&key_construction_sem);
/* can't instantiate twice */ /* can't instantiate twice */
if (!(key->flags & KEY_FLAG_INSTANTIATED)) { if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
/* instantiate the key */ /* instantiate the key */
ret = key->type->instantiate(key, data, datalen); ret = key->type->instantiate(key, data, datalen);
if (ret == 0) { if (ret == 0) {
/* mark the key as being instantiated */ /* mark the key as being instantiated */
write_lock(&key->lock);
atomic_inc(&key->user->nikeys); atomic_inc(&key->user->nikeys);
key->flags |= KEY_FLAG_INSTANTIATED; set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
if (key->flags & KEY_FLAG_USER_CONSTRUCT) { if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
key->flags &= ~KEY_FLAG_USER_CONSTRUCT;
awaken = 1; awaken = 1;
}
write_unlock(&key->lock);
/* and link it into the destination keyring */ /* and link it into the destination keyring */
if (keyring) if (keyring)
...@@ -486,21 +479,17 @@ int key_negate_and_link(struct key *key, ...@@ -486,21 +479,17 @@ int key_negate_and_link(struct key *key,
down_write(&key_construction_sem); down_write(&key_construction_sem);
/* can't instantiate twice */ /* can't instantiate twice */
if (!(key->flags & KEY_FLAG_INSTANTIATED)) { if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
/* mark the key as being negatively instantiated */ /* mark the key as being negatively instantiated */
write_lock(&key->lock);
atomic_inc(&key->user->nikeys); atomic_inc(&key->user->nikeys);
key->flags |= KEY_FLAG_INSTANTIATED | KEY_FLAG_NEGATIVE; set_bit(KEY_FLAG_NEGATIVE, &key->flags);
set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
now = current_kernel_time(); now = current_kernel_time();
key->expiry = now.tv_sec + timeout; key->expiry = now.tv_sec + timeout;
if (key->flags & KEY_FLAG_USER_CONSTRUCT) { if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
key->flags &= ~KEY_FLAG_USER_CONSTRUCT;
awaken = 1; awaken = 1;
}
write_unlock(&key->lock);
ret = 0; ret = 0;
/* and link it into the destination keyring */ /* and link it into the destination keyring */
...@@ -553,8 +542,10 @@ static void key_cleanup(void *data) ...@@ -553,8 +542,10 @@ static void key_cleanup(void *data)
rb_erase(&key->serial_node, &key_serial_tree); rb_erase(&key->serial_node, &key_serial_tree);
spin_unlock(&key_serial_lock); spin_unlock(&key_serial_lock);
key_check(key);
/* deal with the user's key tracking and quota */ /* deal with the user's key tracking and quota */
if (key->flags & KEY_FLAG_IN_QUOTA) { if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
spin_lock(&key->user->lock); spin_lock(&key->user->lock);
key->user->qnkeys--; key->user->qnkeys--;
key->user->qnbytes -= key->quotalen; key->user->qnbytes -= key->quotalen;
...@@ -562,7 +553,7 @@ static void key_cleanup(void *data) ...@@ -562,7 +553,7 @@ static void key_cleanup(void *data)
} }
atomic_dec(&key->user->nkeys); atomic_dec(&key->user->nkeys);
if (key->flags & KEY_FLAG_INSTANTIATED) if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
atomic_dec(&key->user->nikeys); atomic_dec(&key->user->nikeys);
key_user_put(key->user); key_user_put(key->user);
...@@ -631,9 +622,9 @@ struct key *key_lookup(key_serial_t id) ...@@ -631,9 +622,9 @@ struct key *key_lookup(key_serial_t id)
goto error; goto error;
found: found:
/* pretent doesn't exist if it's dead */ /* pretend it doesn't exist if it's dead */
if (atomic_read(&key->usage) == 0 || if (atomic_read(&key->usage) == 0 ||
(key->flags & KEY_FLAG_DEAD) || test_bit(KEY_FLAG_DEAD, &key->flags) ||
key->type == &key_type_dead) key->type == &key_type_dead)
goto not_found; goto not_found;
...@@ -708,12 +699,9 @@ static inline struct key *__key_update(struct key *key, const void *payload, ...@@ -708,12 +699,9 @@ static inline struct key *__key_update(struct key *key, const void *payload,
ret = key->type->update(key, payload, plen); ret = key->type->update(key, payload, plen);
if (ret == 0) { if (ret == 0)
/* updating a negative key instantiates it */ /* updating a negative key instantiates it */
write_lock(&key->lock); clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
key->flags &= ~KEY_FLAG_NEGATIVE;
write_unlock(&key->lock);
}
up_write(&key->sem); up_write(&key->sem);
...@@ -841,12 +829,9 @@ int key_update(struct key *key, const void *payload, size_t plen) ...@@ -841,12 +829,9 @@ int key_update(struct key *key, const void *payload, size_t plen)
down_write(&key->sem); down_write(&key->sem);
ret = key->type->update(key, payload, plen); ret = key->type->update(key, payload, plen);
if (ret == 0) { if (ret == 0)
/* updating a negative key instantiates it */ /* updating a negative key instantiates it */
write_lock(&key->lock); clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
key->flags &= ~KEY_FLAG_NEGATIVE;
write_unlock(&key->lock);
}
up_write(&key->sem); up_write(&key->sem);
} }
...@@ -892,10 +877,7 @@ struct key *key_duplicate(struct key *source, const char *desc) ...@@ -892,10 +877,7 @@ struct key *key_duplicate(struct key *source, const char *desc)
goto error2; goto error2;
atomic_inc(&key->user->nikeys); atomic_inc(&key->user->nikeys);
set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
write_lock(&key->lock);
key->flags |= KEY_FLAG_INSTANTIATED;
write_unlock(&key->lock);
error_k: error_k:
up_read(&key_types_sem); up_read(&key_types_sem);
...@@ -922,9 +904,7 @@ void key_revoke(struct key *key) ...@@ -922,9 +904,7 @@ void key_revoke(struct key *key)
/* make sure no one's trying to change or use the key when we mark /* make sure no one's trying to change or use the key when we mark
* it */ * it */
down_write(&key->sem); down_write(&key->sem);
write_lock(&key->lock); set_bit(KEY_FLAG_REVOKED, &key->flags);
key->flags |= KEY_FLAG_REVOKED;
write_unlock(&key->lock);
up_write(&key->sem); up_write(&key->sem);
} /* end key_revoke() */ } /* end key_revoke() */
...@@ -975,24 +955,33 @@ void unregister_key_type(struct key_type *ktype) ...@@ -975,24 +955,33 @@ void unregister_key_type(struct key_type *ktype)
/* withdraw the key type */ /* withdraw the key type */
list_del_init(&ktype->link); list_del_init(&ktype->link);
/* need to withdraw all keys of this type */ /* mark all the keys of this type dead */
spin_lock(&key_serial_lock); spin_lock(&key_serial_lock);
for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) { for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
key = rb_entry(_n, struct key, serial_node); key = rb_entry(_n, struct key, serial_node);
if (key->type != ktype) if (key->type == ktype)
continue; key->type = &key_type_dead;
}
spin_unlock(&key_serial_lock);
/* make sure everyone revalidates their keys */
synchronize_kernel();
/* we should now be able to destroy the payloads of all the keys of
* this type with impunity */
spin_lock(&key_serial_lock);
write_lock(&key->lock); for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
key->type = &key_type_dead; key = rb_entry(_n, struct key, serial_node);
write_unlock(&key->lock);
/* there shouldn't be anyone looking at the description or if (key->type == ktype) {
* payload now */ if (ktype->destroy)
if (ktype->destroy) ktype->destroy(key);
ktype->destroy(key); memset(&key->payload, 0xbd, sizeof(key->payload));
memset(&key->payload, 0xbd, sizeof(key->payload)); }
} }
spin_unlock(&key_serial_lock); spin_unlock(&key_serial_lock);
...@@ -1037,4 +1026,5 @@ void __init key_init(void) ...@@ -1037,4 +1026,5 @@ void __init key_init(void)
/* link the two root keyrings together */ /* link the two root keyrings together */
key_link(&root_session_keyring, &root_user_keyring); key_link(&root_session_keyring, &root_user_keyring);
} /* end key_init() */ } /* end key_init() */
...@@ -728,7 +728,6 @@ long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid) ...@@ -728,7 +728,6 @@ long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
/* make the changes with the locks held to prevent chown/chown races */ /* make the changes with the locks held to prevent chown/chown races */
ret = -EACCES; ret = -EACCES;
down_write(&key->sem); down_write(&key->sem);
write_lock(&key->lock);
if (!capable(CAP_SYS_ADMIN)) { if (!capable(CAP_SYS_ADMIN)) {
/* only the sysadmin can chown a key to some other UID */ /* only the sysadmin can chown a key to some other UID */
...@@ -755,7 +754,6 @@ long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid) ...@@ -755,7 +754,6 @@ long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
ret = 0; ret = 0;
no_access: no_access:
write_unlock(&key->lock);
up_write(&key->sem); up_write(&key->sem);
key_put(key); key_put(key);
error: error:
...@@ -784,26 +782,19 @@ long keyctl_setperm_key(key_serial_t id, key_perm_t perm) ...@@ -784,26 +782,19 @@ long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
goto error; goto error;
} }
/* make the changes with the locks held to prevent chown/chmod /* make the changes with the locks held to prevent chown/chmod races */
* races */
ret = -EACCES; ret = -EACCES;
down_write(&key->sem); down_write(&key->sem);
write_lock(&key->lock);
/* if we're not the sysadmin, we can only chmod a key that we /* if we're not the sysadmin, we can only change a key that we own */
* own */ if (capable(CAP_SYS_ADMIN) || key->uid == current->fsuid) {
if (!capable(CAP_SYS_ADMIN) && key->uid != current->fsuid) key->perm = perm;
goto no_access; ret = 0;
}
/* changing the permissions mask */
key->perm = perm;
ret = 0;
no_access:
write_unlock(&key->lock);
up_write(&key->sem); up_write(&key->sem);
key_put(key); key_put(key);
error: error:
return ret; return ret;
} /* end keyctl_setperm_key() */ } /* end keyctl_setperm_key() */
......
...@@ -132,10 +132,17 @@ static int keyring_duplicate(struct key *keyring, const struct key *source) ...@@ -132,10 +132,17 @@ static int keyring_duplicate(struct key *keyring, const struct key *source)
(PAGE_SIZE - sizeof(*klist)) / sizeof(struct key); (PAGE_SIZE - sizeof(*klist)) / sizeof(struct key);
ret = 0; ret = 0;
sklist = source->payload.subscriptions;
if (sklist && sklist->nkeys > 0) { /* find out how many keys are currently linked */
rcu_read_lock();
sklist = rcu_dereference(source->payload.subscriptions);
max = 0;
if (sklist)
max = sklist->nkeys; max = sklist->nkeys;
rcu_read_unlock();
/* allocate a new payload and stuff load with key links */
if (max > 0) {
BUG_ON(max > limit); BUG_ON(max > limit);
max = (max + 3) & ~3; max = (max + 3) & ~3;
...@@ -148,6 +155,10 @@ static int keyring_duplicate(struct key *keyring, const struct key *source) ...@@ -148,6 +155,10 @@ static int keyring_duplicate(struct key *keyring, const struct key *source)
if (!klist) if (!klist)
goto error; goto error;
/* set links */
rcu_read_lock();
sklist = rcu_dereference(source->payload.subscriptions);
klist->maxkeys = max; klist->maxkeys = max;
klist->nkeys = sklist->nkeys; klist->nkeys = sklist->nkeys;
memcpy(klist->keys, memcpy(klist->keys,
...@@ -157,7 +168,9 @@ static int keyring_duplicate(struct key *keyring, const struct key *source) ...@@ -157,7 +168,9 @@ static int keyring_duplicate(struct key *keyring, const struct key *source)
for (loop = klist->nkeys - 1; loop >= 0; loop--) for (loop = klist->nkeys - 1; loop >= 0; loop--)
atomic_inc(&klist->keys[loop]->usage); atomic_inc(&klist->keys[loop]->usage);
keyring->payload.subscriptions = klist; rcu_read_unlock();
rcu_assign_pointer(keyring->payload.subscriptions, klist);
ret = 0; ret = 0;
} }
...@@ -192,7 +205,7 @@ static void keyring_destroy(struct key *keyring) ...@@ -192,7 +205,7 @@ static void keyring_destroy(struct key *keyring)
write_unlock(&keyring_name_lock); write_unlock(&keyring_name_lock);
} }
klist = keyring->payload.subscriptions; klist = rcu_dereference(keyring->payload.subscriptions);
if (klist) { if (klist) {
for (loop = klist->nkeys - 1; loop >= 0; loop--) for (loop = klist->nkeys - 1; loop >= 0; loop--)
key_put(klist->keys[loop]); key_put(klist->keys[loop]);
...@@ -216,17 +229,20 @@ static void keyring_describe(const struct key *keyring, struct seq_file *m) ...@@ -216,17 +229,20 @@ static void keyring_describe(const struct key *keyring, struct seq_file *m)
seq_puts(m, "[anon]"); seq_puts(m, "[anon]");
} }
klist = keyring->payload.subscriptions; rcu_read_lock();
klist = rcu_dereference(keyring->payload.subscriptions);
if (klist) if (klist)
seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys); seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys);
else else
seq_puts(m, ": empty"); seq_puts(m, ": empty");
rcu_read_unlock();
} /* end keyring_describe() */ } /* end keyring_describe() */
/*****************************************************************************/ /*****************************************************************************/
/* /*
* read a list of key IDs from the keyring's contents * read a list of key IDs from the keyring's contents
* - the keyring's semaphore is read-locked
*/ */
static long keyring_read(const struct key *keyring, static long keyring_read(const struct key *keyring,
char __user *buffer, size_t buflen) char __user *buffer, size_t buflen)
...@@ -237,7 +253,7 @@ static long keyring_read(const struct key *keyring, ...@@ -237,7 +253,7 @@ static long keyring_read(const struct key *keyring,
int loop, ret; int loop, ret;
ret = 0; ret = 0;
klist = keyring->payload.subscriptions; klist = rcu_dereference(keyring->payload.subscriptions);
if (klist) { if (klist) {
/* calculate how much data we could return */ /* calculate how much data we could return */
...@@ -320,7 +336,7 @@ struct key *keyring_search_aux(struct key *keyring, ...@@ -320,7 +336,7 @@ struct key *keyring_search_aux(struct key *keyring,
key_match_func_t match) key_match_func_t match)
{ {
struct { struct {
struct key *keyring; struct keyring_list *keylist;
int kix; int kix;
} stack[KEYRING_SEARCH_MAX_DEPTH]; } stack[KEYRING_SEARCH_MAX_DEPTH];
...@@ -328,10 +344,12 @@ struct key *keyring_search_aux(struct key *keyring, ...@@ -328,10 +344,12 @@ struct key *keyring_search_aux(struct key *keyring,
struct timespec now; struct timespec now;
struct key *key; struct key *key;
long err; long err;
int sp, psp, kix; int sp, kix;
key_check(keyring); key_check(keyring);
rcu_read_lock();
/* top keyring must have search permission to begin the search */ /* top keyring must have search permission to begin the search */
key = ERR_PTR(-EACCES); key = ERR_PTR(-EACCES);
if (!key_permission(keyring, KEY_SEARCH)) if (!key_permission(keyring, KEY_SEARCH))
...@@ -347,11 +365,10 @@ struct key *keyring_search_aux(struct key *keyring, ...@@ -347,11 +365,10 @@ struct key *keyring_search_aux(struct key *keyring,
/* start processing a new keyring */ /* start processing a new keyring */
descend: descend:
read_lock(&keyring->lock); if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
if (keyring->flags & KEY_FLAG_REVOKED)
goto not_this_keyring; goto not_this_keyring;
keylist = keyring->payload.subscriptions; keylist = rcu_dereference(keyring->payload.subscriptions);
if (!keylist) if (!keylist)
goto not_this_keyring; goto not_this_keyring;
...@@ -364,7 +381,7 @@ struct key *keyring_search_aux(struct key *keyring, ...@@ -364,7 +381,7 @@ struct key *keyring_search_aux(struct key *keyring,
continue; continue;
/* skip revoked keys and expired keys */ /* skip revoked keys and expired keys */
if (key->flags & KEY_FLAG_REVOKED) if (test_bit(KEY_FLAG_REVOKED, &key->flags))
continue; continue;
if (key->expiry && now.tv_sec >= key->expiry) if (key->expiry && now.tv_sec >= key->expiry)
...@@ -379,7 +396,7 @@ struct key *keyring_search_aux(struct key *keyring, ...@@ -379,7 +396,7 @@ struct key *keyring_search_aux(struct key *keyring,
continue; continue;
/* we set a different error code if we find a negative key */ /* we set a different error code if we find a negative key */
if (key->flags & KEY_FLAG_NEGATIVE) { if (test_bit(KEY_FLAG_NEGATIVE, &key->flags)) {
err = -ENOKEY; err = -ENOKEY;
continue; continue;
} }
...@@ -390,48 +407,37 @@ struct key *keyring_search_aux(struct key *keyring, ...@@ -390,48 +407,37 @@ struct key *keyring_search_aux(struct key *keyring,
/* search through the keyrings nested in this one */ /* search through the keyrings nested in this one */
kix = 0; kix = 0;
ascend: ascend:
while (kix < keylist->nkeys) { for (; kix < keylist->nkeys; kix++) {
key = keylist->keys[kix]; key = keylist->keys[kix];
if (key->type != &key_type_keyring) if (key->type != &key_type_keyring)
goto next; continue;
/* recursively search nested keyrings /* recursively search nested keyrings
* - only search keyrings for which we have search permission * - only search keyrings for which we have search permission
*/ */
if (sp >= KEYRING_SEARCH_MAX_DEPTH) if (sp >= KEYRING_SEARCH_MAX_DEPTH)
goto next; continue;
if (!key_permission(key, KEY_SEARCH)) if (!key_permission(key, KEY_SEARCH))
goto next; continue;
/* evade loops in the keyring tree */
for (psp = 0; psp < sp; psp++)
if (stack[psp].keyring == keyring)
goto next;
/* stack the current position */ /* stack the current position */
stack[sp].keyring = keyring; stack[sp].keylist = keylist;
stack[sp].kix = kix; stack[sp].kix = kix;
sp++; sp++;
/* begin again with the new keyring */ /* begin again with the new keyring */
keyring = key; keyring = key;
goto descend; goto descend;
next:
kix++;
} }
/* the keyring we're looking at was disqualified or didn't contain a /* the keyring we're looking at was disqualified or didn't contain a
* matching key */ * matching key */
not_this_keyring: not_this_keyring:
read_unlock(&keyring->lock);
if (sp > 0) { if (sp > 0) {
/* resume the processing of a keyring higher up in the tree */ /* resume the processing of a keyring higher up in the tree */
sp--; sp--;
keyring = stack[sp].keyring; keylist = stack[sp].keylist;
keylist = keyring->payload.subscriptions;
kix = stack[sp].kix + 1; kix = stack[sp].kix + 1;
goto ascend; goto ascend;
} }
...@@ -442,16 +448,9 @@ struct key *keyring_search_aux(struct key *keyring, ...@@ -442,16 +448,9 @@ struct key *keyring_search_aux(struct key *keyring,
/* we found a viable match */ /* we found a viable match */
found: found:
atomic_inc(&key->usage); atomic_inc(&key->usage);
read_unlock(&keyring->lock);
/* unwind the keyring stack */
while (sp > 0) {
sp--;
read_unlock(&stack[sp].keyring->lock);
}
key_check(key); key_check(key);
error: error:
rcu_read_unlock();
return key; return key;
} /* end keyring_search_aux() */ } /* end keyring_search_aux() */
...@@ -489,7 +488,9 @@ struct key *__keyring_search_one(struct key *keyring, ...@@ -489,7 +488,9 @@ struct key *__keyring_search_one(struct key *keyring,
struct key *key; struct key *key;
int loop; int loop;
klist = keyring->payload.subscriptions; rcu_read_lock();
klist = rcu_dereference(keyring->payload.subscriptions);
if (klist) { if (klist) {
for (loop = 0; loop < klist->nkeys; loop++) { for (loop = 0; loop < klist->nkeys; loop++) {
key = klist->keys[loop]; key = klist->keys[loop];
...@@ -497,7 +498,7 @@ struct key *__keyring_search_one(struct key *keyring, ...@@ -497,7 +498,7 @@ struct key *__keyring_search_one(struct key *keyring,
if (key->type == ktype && if (key->type == ktype &&
key->type->match(key, description) && key->type->match(key, description) &&
key_permission(key, perm) && key_permission(key, perm) &&
!(key->flags & KEY_FLAG_REVOKED) !test_bit(KEY_FLAG_REVOKED, &key->flags)
) )
goto found; goto found;
} }
...@@ -509,6 +510,7 @@ struct key *__keyring_search_one(struct key *keyring, ...@@ -509,6 +510,7 @@ struct key *__keyring_search_one(struct key *keyring,
found: found:
atomic_inc(&key->usage); atomic_inc(&key->usage);
error: error:
rcu_read_unlock();
return key; return key;
} /* end __keyring_search_one() */ } /* end __keyring_search_one() */
...@@ -540,7 +542,7 @@ struct key *find_keyring_by_name(const char *name, key_serial_t bound) ...@@ -540,7 +542,7 @@ struct key *find_keyring_by_name(const char *name, key_serial_t bound)
&keyring_name_hash[bucket], &keyring_name_hash[bucket],
type_data.link type_data.link
) { ) {
if (keyring->flags & KEY_FLAG_REVOKED) if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
continue; continue;
if (strcmp(keyring->description, name) != 0) if (strcmp(keyring->description, name) != 0)
...@@ -579,7 +581,7 @@ struct key *find_keyring_by_name(const char *name, key_serial_t bound) ...@@ -579,7 +581,7 @@ struct key *find_keyring_by_name(const char *name, key_serial_t bound)
static int keyring_detect_cycle(struct key *A, struct key *B) static int keyring_detect_cycle(struct key *A, struct key *B)
{ {
struct { struct {
struct key *subtree; struct keyring_list *keylist;
int kix; int kix;
} stack[KEYRING_SEARCH_MAX_DEPTH]; } stack[KEYRING_SEARCH_MAX_DEPTH];
...@@ -587,20 +589,21 @@ static int keyring_detect_cycle(struct key *A, struct key *B) ...@@ -587,20 +589,21 @@ static int keyring_detect_cycle(struct key *A, struct key *B)
struct key *subtree, *key; struct key *subtree, *key;
int sp, kix, ret; int sp, kix, ret;
rcu_read_lock();
ret = -EDEADLK; ret = -EDEADLK;
if (A == B) if (A == B)
goto error; goto cycle_detected;
subtree = B; subtree = B;
sp = 0; sp = 0;
/* start processing a new keyring */ /* start processing a new keyring */
descend: descend:
read_lock(&subtree->lock); if (test_bit(KEY_FLAG_REVOKED, &subtree->flags))
if (subtree->flags & KEY_FLAG_REVOKED)
goto not_this_keyring; goto not_this_keyring;
keylist = subtree->payload.subscriptions; keylist = rcu_dereference(subtree->payload.subscriptions);
if (!keylist) if (!keylist)
goto not_this_keyring; goto not_this_keyring;
kix = 0; kix = 0;
...@@ -619,7 +622,7 @@ static int keyring_detect_cycle(struct key *A, struct key *B) ...@@ -619,7 +622,7 @@ static int keyring_detect_cycle(struct key *A, struct key *B)
goto too_deep; goto too_deep;
/* stack the current position */ /* stack the current position */
stack[sp].subtree = subtree; stack[sp].keylist = keylist;
stack[sp].kix = kix; stack[sp].kix = kix;
sp++; sp++;
...@@ -632,13 +635,10 @@ static int keyring_detect_cycle(struct key *A, struct key *B) ...@@ -632,13 +635,10 @@ static int keyring_detect_cycle(struct key *A, struct key *B)
/* the keyring we're looking at was disqualified or didn't contain a /* the keyring we're looking at was disqualified or didn't contain a
* matching key */ * matching key */
not_this_keyring: not_this_keyring:
read_unlock(&subtree->lock);
if (sp > 0) { if (sp > 0) {
/* resume the checking of a keyring higher up in the tree */ /* resume the checking of a keyring higher up in the tree */
sp--; sp--;
subtree = stack[sp].subtree; keylist = stack[sp].keylist;
keylist = subtree->payload.subscriptions;
kix = stack[sp].kix + 1; kix = stack[sp].kix + 1;
goto ascend; goto ascend;
} }
...@@ -646,30 +646,36 @@ static int keyring_detect_cycle(struct key *A, struct key *B) ...@@ -646,30 +646,36 @@ static int keyring_detect_cycle(struct key *A, struct key *B)
ret = 0; /* no cycles detected */ ret = 0; /* no cycles detected */
error: error:
rcu_read_unlock();
return ret; return ret;
too_deep: too_deep:
ret = -ELOOP; ret = -ELOOP;
goto error_unwind; goto error;
cycle_detected: cycle_detected:
ret = -EDEADLK; ret = -EDEADLK;
error_unwind:
read_unlock(&subtree->lock);
/* unwind the keyring stack */
while (sp > 0) {
sp--;
read_unlock(&stack[sp].subtree->lock);
}
goto error; goto error;
} /* end keyring_detect_cycle() */ } /* end keyring_detect_cycle() */
/*****************************************************************************/
/*
* dispose of a keyring list after the RCU grace period
*/
static void keyring_link_rcu_disposal(struct rcu_head *rcu)
{
struct keyring_list *klist =
container_of(rcu, struct keyring_list, rcu);
kfree(klist);
} /* end keyring_link_rcu_disposal() */
/*****************************************************************************/ /*****************************************************************************/
/* /*
* link a key into to a keyring * link a key into to a keyring
* - must be called with the keyring's semaphore held * - must be called with the keyring's semaphore write-locked
*/ */
int __key_link(struct key *keyring, struct key *key) int __key_link(struct key *keyring, struct key *key)
{ {
...@@ -679,7 +685,7 @@ int __key_link(struct key *keyring, struct key *key) ...@@ -679,7 +685,7 @@ int __key_link(struct key *keyring, struct key *key)
int ret; int ret;
ret = -EKEYREVOKED; ret = -EKEYREVOKED;
if (keyring->flags & KEY_FLAG_REVOKED) if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))
goto error; goto error;
ret = -ENOTDIR; ret = -ENOTDIR;
...@@ -710,9 +716,10 @@ int __key_link(struct key *keyring, struct key *key) ...@@ -710,9 +716,10 @@ int __key_link(struct key *keyring, struct key *key)
/* there's sufficient slack space to add directly */ /* there's sufficient slack space to add directly */
atomic_inc(&key->usage); atomic_inc(&key->usage);
write_lock(&keyring->lock); klist->keys[klist->nkeys] = key;
klist->keys[klist->nkeys++] = key; smp_wmb();
write_unlock(&keyring->lock); klist->nkeys++;
smp_wmb();
ret = 0; ret = 0;
} }
...@@ -723,6 +730,8 @@ int __key_link(struct key *keyring, struct key *key) ...@@ -723,6 +730,8 @@ int __key_link(struct key *keyring, struct key *key)
max += klist->maxkeys; max += klist->maxkeys;
ret = -ENFILE; ret = -ENFILE;
if (max > 65535)
goto error3;
size = sizeof(*klist) + sizeof(*key) * max; size = sizeof(*klist) + sizeof(*key) * max;
if (size > PAGE_SIZE) if (size > PAGE_SIZE)
goto error3; goto error3;
...@@ -743,14 +752,13 @@ int __key_link(struct key *keyring, struct key *key) ...@@ -743,14 +752,13 @@ int __key_link(struct key *keyring, struct key *key)
/* add the key into the new space */ /* add the key into the new space */
atomic_inc(&key->usage); atomic_inc(&key->usage);
write_lock(&keyring->lock);
keyring->payload.subscriptions = nklist;
nklist->keys[nklist->nkeys++] = key; nklist->keys[nklist->nkeys++] = key;
write_unlock(&keyring->lock);
rcu_assign_pointer(keyring->payload.subscriptions, nklist);
/* dispose of the old keyring list */ /* dispose of the old keyring list */
kfree(klist); if (klist)
call_rcu(&klist->rcu, keyring_link_rcu_disposal);
ret = 0; ret = 0;
} }
...@@ -789,13 +797,28 @@ int key_link(struct key *keyring, struct key *key) ...@@ -789,13 +797,28 @@ int key_link(struct key *keyring, struct key *key)
EXPORT_SYMBOL(key_link); EXPORT_SYMBOL(key_link);
/*****************************************************************************/
/*
* dispose of a keyring list after the RCU grace period, freeing the unlinked
* key
*/
static void keyring_unlink_rcu_disposal(struct rcu_head *rcu)
{
struct keyring_list *klist =
container_of(rcu, struct keyring_list, rcu);
key_put(klist->keys[klist->delkey]);
kfree(klist);
} /* end keyring_unlink_rcu_disposal() */
/*****************************************************************************/ /*****************************************************************************/
/* /*
* unlink the first link to a key from a keyring * unlink the first link to a key from a keyring
*/ */
int key_unlink(struct key *keyring, struct key *key) int key_unlink(struct key *keyring, struct key *key)
{ {
struct keyring_list *klist; struct keyring_list *klist, *nklist;
int loop, ret; int loop, ret;
key_check(keyring); key_check(keyring);
...@@ -819,36 +842,69 @@ int key_unlink(struct key *keyring, struct key *key) ...@@ -819,36 +842,69 @@ int key_unlink(struct key *keyring, struct key *key)
ret = -ENOENT; ret = -ENOENT;
goto error; goto error;
key_is_present: key_is_present:
/* we need to copy the key list for RCU purposes */
nklist = kmalloc(sizeof(*klist) + sizeof(*key) * klist->maxkeys,
GFP_KERNEL);
if (!nklist)
goto nomem;
nklist->maxkeys = klist->maxkeys;
nklist->nkeys = klist->nkeys - 1;
if (loop > 0)
memcpy(&nklist->keys[0],
&klist->keys[0],
loop * sizeof(klist->keys[0]));
if (loop < nklist->nkeys)
memcpy(&nklist->keys[loop],
&klist->keys[loop + 1],
(nklist->nkeys - loop) * sizeof(klist->keys[0]));
/* adjust the user's quota */ /* adjust the user's quota */
key_payload_reserve(keyring, key_payload_reserve(keyring,
keyring->datalen - KEYQUOTA_LINK_BYTES); keyring->datalen - KEYQUOTA_LINK_BYTES);
/* shuffle down the key pointers rcu_assign_pointer(keyring->payload.subscriptions, nklist);
* - it might be worth shrinking the allocated memory, but that runs
* the risk of ENOMEM as we would have to copy
*/
write_lock(&keyring->lock);
klist->nkeys--; up_write(&keyring->sem);
if (loop < klist->nkeys)
memcpy(&klist->keys[loop],
&klist->keys[loop + 1],
(klist->nkeys - loop) * sizeof(struct key *));
write_unlock(&keyring->lock); /* schedule for later cleanup */
klist->delkey = loop;
call_rcu(&klist->rcu, keyring_unlink_rcu_disposal);
up_write(&keyring->sem);
key_put(key);
ret = 0; ret = 0;
error: error:
return ret; return ret;
nomem:
ret = -ENOMEM;
up_write(&keyring->sem);
goto error;
} /* end key_unlink() */ } /* end key_unlink() */
EXPORT_SYMBOL(key_unlink); EXPORT_SYMBOL(key_unlink);
/*****************************************************************************/
/*
* dispose of a keyring list after the RCU grace period, releasing the keys it
* links to
*/
static void keyring_clear_rcu_disposal(struct rcu_head *rcu)
{
struct keyring_list *klist;
int loop;
klist = container_of(rcu, struct keyring_list, rcu);
for (loop = klist->nkeys - 1; loop >= 0; loop--)
key_put(klist->keys[loop]);
kfree(klist);
} /* end keyring_clear_rcu_disposal() */
/*****************************************************************************/ /*****************************************************************************/
/* /*
* clear the specified process keyring * clear the specified process keyring
...@@ -857,7 +913,7 @@ EXPORT_SYMBOL(key_unlink); ...@@ -857,7 +913,7 @@ EXPORT_SYMBOL(key_unlink);
int keyring_clear(struct key *keyring) int keyring_clear(struct key *keyring)
{ {
struct keyring_list *klist; struct keyring_list *klist;
int loop, ret; int ret;
ret = -ENOTDIR; ret = -ENOTDIR;
if (keyring->type == &key_type_keyring) { if (keyring->type == &key_type_keyring) {
...@@ -870,20 +926,15 @@ int keyring_clear(struct key *keyring) ...@@ -870,20 +926,15 @@ int keyring_clear(struct key *keyring)
key_payload_reserve(keyring, key_payload_reserve(keyring,
sizeof(struct keyring_list)); sizeof(struct keyring_list));
write_lock(&keyring->lock); rcu_assign_pointer(keyring->payload.subscriptions,
keyring->payload.subscriptions = NULL; NULL);
write_unlock(&keyring->lock);
} }
up_write(&keyring->sem); up_write(&keyring->sem);
/* free the keys after the locks have been dropped */ /* free the keys after the locks have been dropped */
if (klist) { if (klist)
for (loop = klist->nkeys - 1; loop >= 0; loop--) call_rcu(&klist->rcu, keyring_clear_rcu_disposal);
key_put(klist->keys[loop]);
kfree(klist);
}
ret = 0; ret = 0;
} }
......
...@@ -140,7 +140,7 @@ static int proc_keys_show(struct seq_file *m, void *v) ...@@ -140,7 +140,7 @@ static int proc_keys_show(struct seq_file *m, void *v)
now = current_kernel_time(); now = current_kernel_time();
read_lock(&key->lock); rcu_read_lock();
/* come up with a suitable timeout value */ /* come up with a suitable timeout value */
if (key->expiry == 0) { if (key->expiry == 0) {
...@@ -164,14 +164,17 @@ static int proc_keys_show(struct seq_file *m, void *v) ...@@ -164,14 +164,17 @@ static int proc_keys_show(struct seq_file *m, void *v)
sprintf(xbuf, "%luw", timo / (60*60*24*7)); sprintf(xbuf, "%luw", timo / (60*60*24*7));
} }
#define showflag(KEY, LETTER, FLAG) \
(test_bit(FLAG, &(KEY)->flags) ? LETTER : '-')
seq_printf(m, "%08x %c%c%c%c%c%c %5d %4s %06x %5d %5d %-9.9s ", seq_printf(m, "%08x %c%c%c%c%c%c %5d %4s %06x %5d %5d %-9.9s ",
key->serial, key->serial,
key->flags & KEY_FLAG_INSTANTIATED ? 'I' : '-', showflag(key, 'I', KEY_FLAG_INSTANTIATED),
key->flags & KEY_FLAG_REVOKED ? 'R' : '-', showflag(key, 'R', KEY_FLAG_REVOKED),
key->flags & KEY_FLAG_DEAD ? 'D' : '-', showflag(key, 'D', KEY_FLAG_DEAD),
key->flags & KEY_FLAG_IN_QUOTA ? 'Q' : '-', showflag(key, 'Q', KEY_FLAG_IN_QUOTA),
key->flags & KEY_FLAG_USER_CONSTRUCT ? 'U' : '-', showflag(key, 'U', KEY_FLAG_USER_CONSTRUCT),
key->flags & KEY_FLAG_NEGATIVE ? 'N' : '-', showflag(key, 'N', KEY_FLAG_NEGATIVE),
atomic_read(&key->usage), atomic_read(&key->usage),
xbuf, xbuf,
key->perm, key->perm,
...@@ -179,11 +182,13 @@ static int proc_keys_show(struct seq_file *m, void *v) ...@@ -179,11 +182,13 @@ static int proc_keys_show(struct seq_file *m, void *v)
key->gid, key->gid,
key->type->name); key->type->name);
#undef showflag
if (key->type->describe) if (key->type->describe)
key->type->describe(key, m); key->type->describe(key, m);
seq_putc(m, '\n'); seq_putc(m, '\n');
read_unlock(&key->lock); rcu_read_unlock();
return 0; return 0;
......
...@@ -38,10 +38,9 @@ struct key root_user_keyring = { ...@@ -38,10 +38,9 @@ struct key root_user_keyring = {
.serial = 2, .serial = 2,
.type = &key_type_keyring, .type = &key_type_keyring,
.user = &root_key_user, .user = &root_key_user,
.lock = RW_LOCK_UNLOCKED,
.sem = __RWSEM_INITIALIZER(root_user_keyring.sem), .sem = __RWSEM_INITIALIZER(root_user_keyring.sem),
.perm = KEY_USR_ALL, .perm = KEY_USR_ALL,
.flags = KEY_FLAG_INSTANTIATED, .flags = 1 << KEY_FLAG_INSTANTIATED,
.description = "_uid.0", .description = "_uid.0",
#ifdef KEY_DEBUGGING #ifdef KEY_DEBUGGING
.magic = KEY_DEBUG_MAGIC, .magic = KEY_DEBUG_MAGIC,
...@@ -54,10 +53,9 @@ struct key root_session_keyring = { ...@@ -54,10 +53,9 @@ struct key root_session_keyring = {
.serial = 1, .serial = 1,
.type = &key_type_keyring, .type = &key_type_keyring,
.user = &root_key_user, .user = &root_key_user,
.lock = RW_LOCK_UNLOCKED,
.sem = __RWSEM_INITIALIZER(root_session_keyring.sem), .sem = __RWSEM_INITIALIZER(root_session_keyring.sem),
.perm = KEY_USR_ALL, .perm = KEY_USR_ALL,
.flags = KEY_FLAG_INSTANTIATED, .flags = 1 << KEY_FLAG_INSTANTIATED,
.description = "_uid_ses.0", .description = "_uid_ses.0",
#ifdef KEY_DEBUGGING #ifdef KEY_DEBUGGING
.magic = KEY_DEBUG_MAGIC, .magic = KEY_DEBUG_MAGIC,
...@@ -349,9 +347,7 @@ void key_fsuid_changed(struct task_struct *tsk) ...@@ -349,9 +347,7 @@ void key_fsuid_changed(struct task_struct *tsk)
/* update the ownership of the thread keyring */ /* update the ownership of the thread keyring */
if (tsk->thread_keyring) { if (tsk->thread_keyring) {
down_write(&tsk->thread_keyring->sem); down_write(&tsk->thread_keyring->sem);
write_lock(&tsk->thread_keyring->lock);
tsk->thread_keyring->uid = tsk->fsuid; tsk->thread_keyring->uid = tsk->fsuid;
write_unlock(&tsk->thread_keyring->lock);
up_write(&tsk->thread_keyring->sem); up_write(&tsk->thread_keyring->sem);
} }
...@@ -366,9 +362,7 @@ void key_fsgid_changed(struct task_struct *tsk) ...@@ -366,9 +362,7 @@ void key_fsgid_changed(struct task_struct *tsk)
/* update the ownership of the thread keyring */ /* update the ownership of the thread keyring */
if (tsk->thread_keyring) { if (tsk->thread_keyring) {
down_write(&tsk->thread_keyring->sem); down_write(&tsk->thread_keyring->sem);
write_lock(&tsk->thread_keyring->lock);
tsk->thread_keyring->gid = tsk->fsgid; tsk->thread_keyring->gid = tsk->fsgid;
write_unlock(&tsk->thread_keyring->lock);
up_write(&tsk->thread_keyring->sem); up_write(&tsk->thread_keyring->sem);
} }
...@@ -588,7 +582,7 @@ struct key *lookup_user_key(key_serial_t id, int create, int partial, ...@@ -588,7 +582,7 @@ struct key *lookup_user_key(key_serial_t id, int create, int partial,
} }
ret = -EIO; ret = -EIO;
if (!partial && !(key->flags & KEY_FLAG_INSTANTIATED)) if (!partial && !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
goto invalid_key; goto invalid_key;
ret = -EACCES; ret = -EACCES;
......
...@@ -105,7 +105,7 @@ static struct key *__request_key_construction(struct key_type *type, ...@@ -105,7 +105,7 @@ static struct key *__request_key_construction(struct key_type *type,
struct key_construction cons; struct key_construction cons;
struct timespec now; struct timespec now;
struct key *key; struct key *key;
int ret, negative; int ret, negated;
/* create a key and add it to the queue */ /* create a key and add it to the queue */
key = key_alloc(type, description, key = key_alloc(type, description,
...@@ -113,9 +113,7 @@ static struct key *__request_key_construction(struct key_type *type, ...@@ -113,9 +113,7 @@ static struct key *__request_key_construction(struct key_type *type,
if (IS_ERR(key)) if (IS_ERR(key))
goto alloc_failed; goto alloc_failed;
write_lock(&key->lock); set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
key->flags |= KEY_FLAG_USER_CONSTRUCT;
write_unlock(&key->lock);
cons.key = key; cons.key = key;
list_add_tail(&cons.link, &key->user->consq); list_add_tail(&cons.link, &key->user->consq);
...@@ -130,7 +128,7 @@ static struct key *__request_key_construction(struct key_type *type, ...@@ -130,7 +128,7 @@ static struct key *__request_key_construction(struct key_type *type,
/* if the key wasn't instantiated, then we want to give an error */ /* if the key wasn't instantiated, then we want to give an error */
ret = -ENOKEY; ret = -ENOKEY;
if (!(key->flags & KEY_FLAG_INSTANTIATED)) if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
goto request_failed; goto request_failed;
down_write(&key_construction_sem); down_write(&key_construction_sem);
...@@ -139,7 +137,7 @@ static struct key *__request_key_construction(struct key_type *type, ...@@ -139,7 +137,7 @@ static struct key *__request_key_construction(struct key_type *type,
/* also give an error if the key was negatively instantiated */ /* also give an error if the key was negatively instantiated */
check_not_negative: check_not_negative:
if (key->flags & KEY_FLAG_NEGATIVE) { if (test_bit(KEY_FLAG_NEGATIVE, &key->flags)) {
key_put(key); key_put(key);
key = ERR_PTR(-ENOKEY); key = ERR_PTR(-ENOKEY);
} }
...@@ -152,24 +150,23 @@ static struct key *__request_key_construction(struct key_type *type, ...@@ -152,24 +150,23 @@ static struct key *__request_key_construction(struct key_type *type,
* - remove from construction queue * - remove from construction queue
* - mark the key as dead * - mark the key as dead
*/ */
negative = 0; negated = 0;
down_write(&key_construction_sem); down_write(&key_construction_sem);
list_del(&cons.link); list_del(&cons.link);
write_lock(&key->lock);
key->flags &= ~KEY_FLAG_USER_CONSTRUCT;
/* check it didn't get instantiated between the check and the down */ /* check it didn't get instantiated between the check and the down */
if (!(key->flags & KEY_FLAG_INSTANTIATED)) { if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
key->flags |= KEY_FLAG_INSTANTIATED | KEY_FLAG_NEGATIVE; set_bit(KEY_FLAG_NEGATIVE, &key->flags);
negative = 1; set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
negated = 1;
} }
write_unlock(&key->lock); clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
up_write(&key_construction_sem); up_write(&key_construction_sem);
if (!negative) if (!negated)
goto check_not_negative; /* surprisingly, the key got goto check_not_negative; /* surprisingly, the key got
* instantiated */ * instantiated */
...@@ -250,7 +247,7 @@ static struct key *request_key_construction(struct key_type *type, ...@@ -250,7 +247,7 @@ static struct key *request_key_construction(struct key_type *type,
for (;;) { for (;;) {
set_current_state(TASK_UNINTERRUPTIBLE); set_current_state(TASK_UNINTERRUPTIBLE);
if (!(ckey->flags & KEY_FLAG_USER_CONSTRUCT)) if (!test_bit(KEY_FLAG_USER_CONSTRUCT, &ckey->flags))
break; break;
schedule(); schedule();
} }
...@@ -339,7 +336,8 @@ int key_validate(struct key *key) ...@@ -339,7 +336,8 @@ int key_validate(struct key *key)
if (key) { if (key) {
/* check it's still accessible */ /* check it's still accessible */
ret = -EKEYREVOKED; ret = -EKEYREVOKED;
if (key->flags & (KEY_FLAG_REVOKED | KEY_FLAG_DEAD)) if (test_bit(KEY_FLAG_REVOKED, &key->flags) ||
test_bit(KEY_FLAG_DEAD, &key->flags))
goto error; goto error;
/* check it hasn't expired */ /* check it hasn't expired */
......
...@@ -42,12 +42,19 @@ struct key_type key_type_user = { ...@@ -42,12 +42,19 @@ struct key_type key_type_user = {
.read = user_read, .read = user_read,
}; };
struct user_key_payload {
struct rcu_head rcu; /* RCU destructor */
unsigned short datalen; /* length of this data */
char data[0]; /* actual data */
};
/*****************************************************************************/ /*****************************************************************************/
/* /*
* instantiate a user defined key * instantiate a user defined key
*/ */
static int user_instantiate(struct key *key, const void *data, size_t datalen) static int user_instantiate(struct key *key, const void *data, size_t datalen)
{ {
struct user_key_payload *upayload;
int ret; int ret;
ret = -EINVAL; ret = -EINVAL;
...@@ -58,13 +65,15 @@ static int user_instantiate(struct key *key, const void *data, size_t datalen) ...@@ -58,13 +65,15 @@ static int user_instantiate(struct key *key, const void *data, size_t datalen)
if (ret < 0) if (ret < 0)
goto error; goto error;
/* attach the data */
ret = -ENOMEM; ret = -ENOMEM;
key->payload.data = kmalloc(datalen, GFP_KERNEL); upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
if (!key->payload.data) if (!upayload)
goto error; goto error;
memcpy(key->payload.data, data, datalen); /* attach the data */
upayload->datalen = datalen;
memcpy(upayload->data, data, datalen);
rcu_assign_pointer(key->payload.data, upayload);
ret = 0; ret = 0;
error: error:
...@@ -75,18 +84,25 @@ static int user_instantiate(struct key *key, const void *data, size_t datalen) ...@@ -75,18 +84,25 @@ static int user_instantiate(struct key *key, const void *data, size_t datalen)
/*****************************************************************************/ /*****************************************************************************/
/* /*
* duplicate a user defined key * duplicate a user defined key
* - both keys' semaphores are locked against further modification
* - the new key cannot yet be accessed
*/ */
static int user_duplicate(struct key *key, const struct key *source) static int user_duplicate(struct key *key, const struct key *source)
{ {
struct user_key_payload *upayload, *spayload;
int ret; int ret;
/* just copy the payload */ /* just copy the payload */
ret = -ENOMEM; ret = -ENOMEM;
key->payload.data = kmalloc(source->datalen, GFP_KERNEL); upayload = kmalloc(sizeof(*upayload) + source->datalen, GFP_KERNEL);
if (upayload) {
spayload = rcu_dereference(source->payload.data);
BUG_ON(source->datalen != spayload->datalen);
if (key->payload.data) { upayload->datalen = key->datalen = spayload->datalen;
key->datalen = source->datalen; memcpy(upayload->data, spayload->data, key->datalen);
memcpy(key->payload.data, source->payload.data, source->datalen);
key->payload.data = upayload;
ret = 0; ret = 0;
} }
...@@ -94,42 +110,56 @@ static int user_duplicate(struct key *key, const struct key *source) ...@@ -94,42 +110,56 @@ static int user_duplicate(struct key *key, const struct key *source)
} /* end user_duplicate() */ } /* end user_duplicate() */
/*****************************************************************************/
/*
* dispose of the old data from an updated user defined key
*/
static void user_update_rcu_disposal(struct rcu_head *rcu)
{
struct user_key_payload *upayload;
upayload = container_of(rcu, struct user_key_payload, rcu);
kfree(upayload);
} /* end user_update_rcu_disposal() */
/*****************************************************************************/ /*****************************************************************************/
/* /*
* update a user defined key * update a user defined key
* - the key's semaphore is write-locked
*/ */
static int user_update(struct key *key, const void *data, size_t datalen) static int user_update(struct key *key, const void *data, size_t datalen)
{ {
void *new, *zap; struct user_key_payload *upayload, *zap;
int ret; int ret;
ret = -EINVAL; ret = -EINVAL;
if (datalen <= 0 || datalen > 32767 || !data) if (datalen <= 0 || datalen > 32767 || !data)
goto error; goto error;
/* copy the data */ /* construct a replacement payload */
ret = -ENOMEM; ret = -ENOMEM;
new = kmalloc(datalen, GFP_KERNEL); upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
if (!new) if (!upayload)
goto error; goto error;
memcpy(new, data, datalen); upayload->datalen = datalen;
memcpy(upayload->data, data, datalen);
/* check the quota and attach the new data */ /* check the quota and attach the new data */
zap = new; zap = upayload;
write_lock(&key->lock);
ret = key_payload_reserve(key, datalen); ret = key_payload_reserve(key, datalen);
if (ret == 0) { if (ret == 0) {
/* attach the new data, displacing the old */ /* attach the new data, displacing the old */
zap = key->payload.data; zap = key->payload.data;
key->payload.data = new; rcu_assign_pointer(key->payload.data, upayload);
key->expiry = 0; key->expiry = 0;
} }
write_unlock(&key->lock); call_rcu(&zap->rcu, user_update_rcu_disposal);
kfree(zap);
error: error:
return ret; return ret;
...@@ -152,13 +182,15 @@ static int user_match(const struct key *key, const void *description) ...@@ -152,13 +182,15 @@ static int user_match(const struct key *key, const void *description)
*/ */
static void user_destroy(struct key *key) static void user_destroy(struct key *key)
{ {
kfree(key->payload.data); struct user_key_payload *upayload = key->payload.data;
kfree(upayload);
} /* end user_destroy() */ } /* end user_destroy() */
/*****************************************************************************/ /*****************************************************************************/
/* /*
* describe the user * describe the user key
*/ */
static void user_describe(const struct key *key, struct seq_file *m) static void user_describe(const struct key *key, struct seq_file *m)
{ {
...@@ -171,18 +203,23 @@ static void user_describe(const struct key *key, struct seq_file *m) ...@@ -171,18 +203,23 @@ static void user_describe(const struct key *key, struct seq_file *m)
/*****************************************************************************/ /*****************************************************************************/
/* /*
* read the key data * read the key data
* - the key's semaphore is read-locked
*/ */
static long user_read(const struct key *key, static long user_read(const struct key *key,
char __user *buffer, size_t buflen) char __user *buffer, size_t buflen)
{ {
long ret = key->datalen; struct user_key_payload *upayload;
long ret;
upayload = rcu_dereference(key->payload.data);
ret = upayload->datalen;
/* we can return the data as is */ /* we can return the data as is */
if (buffer && buflen > 0) { if (buffer && buflen > 0) {
if (buflen > key->datalen) if (buflen > upayload->datalen)
buflen = key->datalen; buflen = upayload->datalen;
if (copy_to_user(buffer, key->payload.data, buflen) != 0) if (copy_to_user(buffer, upayload->data, buflen) != 0)
ret = -EFAULT; ret = -EFAULT;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册