KEYS: Make request_key() and co fundamentally asynchronous
Subsystems (6)
David Howells
maintainerPATCH 1/1 KEYS: Make request_key() and co fundamentally asynchronous
Make request_key() and co fundamentally asynchronous to make it easier for NFS to make use of them. There are now accessor functions that do asynchronous constructions, a wait function to wait for construction to complete, and a completion function for the key type to indicate completion of construction.
Note that the construction queue is now gone. Instead, keys under construction are linked in to the appropriate keyring in advance, and that anyone encountering one must wait for it to be complete before they can use it. This is done automatically for userspace.
The following auxiliary changes are also made:
(1) Key type implementation stuff is split from linux/key.h into linux/key-type.h.
(2) AF_RXRPC provides a way to allocate null rxrpc-type keys so that AFS does not need to call key_instantiate_and_link() directly.
(3) Documentation.
---
Documentation/keys-request-key.txt | 25 +- Documentation/keys.txt | 91 +++++- Documentation/networking/rxrpc.txt | 7 fs/afs/cell.c | 17 - include/keys/rxrpc-type.h | 2 include/linux/key-type.h | 112 +++++++ include/linux/key.h | 99 +----- net/rxrpc/af_rxrpc.c | 1 net/rxrpc/ar-key.c | 31 ++ security/keys/internal.h | 29 +- security/keys/key.c | 27 +- security/keys/process_keys.c | 16 + security/keys/request_key.c | 556 ++++++++++++++++++------------------ security/keys/request_key_auth.c | 9 + 14 files changed, 595 insertions(+), 427 deletions(-)
Documentation/keys-request-key.txt +21 -4
@@ -20,6 +20,19 @@ or: const char *callout_string, void *aux); +or: + + struct key *request_key_async(const struct key_type *type, + const char *description, + const char *callout_string); + +or: + + struct key *request_key_async_with_auxdata(const struct key_type *type, + const char *description, + const char *callout_string, + void *aux); + Or by userspace invoking the request_key system call: key_serial_t request_key(const char *type,
@@ -32,10 +45,14 @@ does not need to link the key to a keyring to prevent it from being immediately destroyed. The kernel interface returns a pointer directly to the key, and it's up to the caller to destroy the key. -The request_key_with_auxdata() call is like the in-kernel request_key() call, -except that it permits auxiliary data to be passed to the upcaller (the default -is NULL). This is only useful for those key types that define their own upcall -mechanism rather than using /sbin/request-key. +The request_key*_with_auxdata() calls are like the in-kernel request_key*() +calls, except that they permit auxiliary data to be passed to the upcaller (the +default is NULL). This is only useful for those key types that define their +own upcall mechanism rather than using /sbin/request-key. + +The two async in-kernel calls may return keys that are still in the process of +being constructed. The two non-async ones will wait for construction to +complete first. The userspace interface links the key to a keyring associated with the process to prevent the key from going away, and returns the serial number of the key to
Documentation/keys.txt +78 -13
119 lines not on this page · raw message
Documentation/networking/rxrpc.txt +7 -0
@@ -857,3 +857,10 @@ The kernel interface functions are as follows: This is used to extract the error number from a message indicating either a local error occurred or a network error occurred. + + (*) Allocate a null key for doing anonymous security. + + struct key *rxrpc_get_null_key(const char *keyname); + + This is used to allocate a null RxRPC key that can be used to indicate + anonymous security for a particular domain.
fs/afs/cell.c +6 -11
@@ -33,6 +33,7 @@ static struct afs_cell *afs_cell_root;static struct afs_cell *afs_cell_alloc (const char *name,char *vllist) {struct afs_cell *cell; +struct key *key;size_t namelen;char keyname[4 +AFS_MAXCELLNAME +1 ], *cp, *dp, *next;int ret;
@@ -89,20 +90,14 @@ static struct afs_cell *afs_cell_alloc(const char *name, char *vllist)do { *dp++ =toupper (*cp); }while (*cp++); - cell->anonymous_key =key_alloc (&key_type_rxrpc, keyname,0 ,0 , current, -KEY_POS_SEARCH ,KEY_ALLOC_NOT_IN_QUOTA ); -if (IS_ERR (cell->anonymous_key)) { -_debug ("no key" ); - ret =PTR_ERR (cell->anonymous_key); - goto error; - } - ret =key_instantiate_and_link (cell->anonymous_key,NULL ,0 , -NULL ,NULL ); -if (ret <0 ) { -_debug ("instantiate failed" ); + key =rxrpc_get_null_key (keyname); +if (IS_ERR (key)) { +_debug ("no key" ); + ret =PTR_ERR (key); goto error; } + cell->anonymous_key = key;_debug ("anon key %p{%x}" , cell->anonymous_key,key_serial (cell->anonymous_key));
include/keys/rxrpc-type.h +2 -0
@@ -19,4 +19,6 @@*/ extern struct key_type key_type_rxrpc; +extern struct key *rxrpc_get_null_key (const char *); +#endif /* _KEYS_USER_TYPE_H */
include/linux/key-type.h new file +112 -0
112 lines not on this page · raw message
include/linux/key.h +13 -86
127 lines not on this page · raw message
net/rxrpc/af_rxrpc.c +1 -0
@@ -14,6 +14,7 @@#include <linux/skbuff.h> #include <linux/poll.h> #include <linux/proc_fs.h> +#include <linux/key-type.h> #include <net/sock.h> #include <net/af_rxrpc.h> #include "ar-internal.h"
net/rxrpc/ar-key.c +30 -1
@@ -15,7 +15,7 @@#include <linux/module.h> #include <linux/net.h> #include <linux/skbuff.h> -#include <linux/key.h> +#include <linux/key-type.h> #include <linux/crypto.h> #include <net/sock.h> #include <net/af_rxrpc.h>
@@ -332,3 +332,32 @@ error: }EXPORT_SYMBOL (rxrpc_get_server_data_key); + +/** +* rxrpc_get_null_key - Generate a null RxRPC key +* @keyname: The name to give the key. +* +* Generate a null RxRPC key that can be used to indicate anonymous security is +* required for a particular domain. +*/ +struct key *rxrpc_get_null_key (const char *keyname) +{ +struct key *key; +int ret; + + key =key_alloc (&key_type_rxrpc, keyname,0 ,0 , current, +KEY_POS_SEARCH ,KEY_ALLOC_NOT_IN_QUOTA ); +if (IS_ERR (key)) +return key; + + ret =key_instantiate_and_link (key,NULL ,0 ,NULL ,NULL ); +if (ret <0 ) { +key_revoke (key); +key_put (key); +return ERR_PTR (ret); + } + +return key; +} + +EXPORT_SYMBOL (rxrpc_get_null_key);
security/keys/internal.h +17 -12
@@ -1,6 +1,6 @@/* internal.h: authentication token and access key management internal defs * -* Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved. +* Copyright (C) 2003-5, 2007 Red Hat, Inc. All Rights Reserved. * Written by David Howells ([email protected]) * * This program is free software; you can redistribute it and/or
@@ -12,17 +12,22 @@#ifndef _INTERNAL_H#define _INTERNAL_H -#include <linux/key.h> +#include <linux/key-type.h> #include <linux/key-ui.h> -#if 0 -#define kenter(FMT, a...) printk("==> %s("FMT")\n",__FUNCTION__ , ## a) -#define kleave(FMT, a...) printk("<== %s()"FMT"\n",__FUNCTION__ , ## a) -#define kdebug(FMT, a...) printk(FMT"\n" , ## a) +static inline __attribute__((format (printf,1 ,2 ))) +void no_printk (const char *fmt, ...) +{ +} + +#ifdef __KDEBUG +#define kenter (FMT , ...) printk("==> %s("FMT")\n",__FUNCTION__ ,##__VA_ARGS__) +#define kleave (FMT , ...) printk("<== %s()"FMT"\n",__FUNCTION__ ,##__VA_ARGS__) +#define kdebug (FMT , ...) printk(FMT"\n" ,##__VA_ARGS__)#else -#define kenter(FMT, a...) do {} while(0) -#define kleave(FMT, a...) do {} while(0) -#define kdebug(FMT, a...) do {} while(0) +#define kenter (FMT , ...) no_printk("==> %s("FMT")\n",__FUNCTION__ ,##__VA_ARGS__) +#define kleave (FMT , ...) no_printk("<== %s()"FMT"\n",__FUNCTION__ ,##__VA_ARGS__) +#define kdebug (FMT , ...) no_printk(FMT"\n" ,##__VA_ARGS__)#endif extern struct key_type key_type_user;
@@ -36,7 +41,7 @@ extern struct key_type key_type_user;*/ struct key_user {struct rb_node node; -struct list_head consq;/* construction queue */ +struct mutex cons_lock;/* construction initiation lock */ spinlock_t lock;atomic_t usage;/* for accessing qnkeys & qnbytes */ atomic_t nkeys;/* number of keys */
@@ -62,7 +67,7 @@ extern void key_user_put(struct key_user *user);extern struct rb_root key_serial_tree;extern spinlock_t key_serial_lock;extern struct semaphore key_alloc_sem; -extern struct rw_semaphore key_construction_sem; +extern struct mutex key_construction_mutex;extern wait_queue_head_t request_key_conswq;
@@ -109,7 +114,7 @@ extern struct key *request_key_and_link(struct key_type *type,struct request_key_auth {struct key *target_key;struct task_struct *context; -const char *callout_info; +char *callout_info;pid_t pid; };
security/keys/key.c +13 -14
@@ -1,6 +1,6 @@ -/* key.c: basic authentication token and access key management +/* Basic authentication token and access key management * -* Copyright (C) 2004-6 Red Hat, Inc. All Rights Reserved. +* Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved. * Written by David Howells ([email protected]) * * This program is free software; you can redistribute it and/or
@@ -34,7 +34,7 @@ static void key_cleanup(struct work_struct *work); static DECLARE_WORK(key_cleanup_task, key_cleanup);/* we serialise key instantiation and link */ -DECLARE_RWSEM (key_construction_sem); +DEFINE_MUTEX (key_construction_mutex);/* any key who's type gets unegistered will be re-typed to this */ static struct key_type key_type_dead = {
@@ -104,7 +104,7 @@ struct key_user *key_user_lookup(uid_t uid) candidate->qnkeys =0 ; candidate->qnbytes =0 ;spin_lock_init (&candidate->lock); -INIT_LIST_HEAD (&candidate->consq); +mutex_init (&candidate->cons_lock);rb_link_node (&candidate->node, parent, p);rb_insert_color (&candidate->node, &key_user_tree);
@@ -418,7 +418,7 @@ static int __key_instantiate_and_link(struct key *key, awaken =0 ; ret = -EBUSY ; -down_write (&key_construction_sem); +mutex_lock (&key_construction_mutex);/* can't instantiate twice */ if (!test_bit (KEY_FLAG_INSTANTIATED , &key->flags)) {
@@ -443,11 +443,11 @@ static int __key_instantiate_and_link(struct key *key, } } -up_write (&key_construction_sem); +mutex_unlock (&key_construction_mutex);/* wake up anyone waiting for a key to be constructed */ if (awaken) -wake_up_all (&request_key_conswq); +wake_up_bit (&key->flags,KEY_FLAG_USER_CONSTRUCT );return ret;
@@ -500,7 +500,7 @@ int key_negate_and_link(struct key *key,if (keyring)down_write (&keyring->sem); -down_write (&key_construction_sem); +mutex_lock (&key_construction_mutex);/* can't instantiate twice */ if (!test_bit (KEY_FLAG_INSTANTIATED , &key->flags)) {
@@ -525,14 +525,14 @@ int key_negate_and_link(struct key *key,key_revoke (instkey); } -up_write (&key_construction_sem); +mutex_unlock (&key_construction_mutex);if (keyring)up_write (&keyring->sem);/* wake up anyone waiting for a key to be constructed */ if (awaken) -wake_up_all (&request_key_conswq); +wake_up_bit (&key->flags,KEY_FLAG_USER_CONSTRUCT );return ret;
@@ -901,10 +901,9 @@ void key_revoke(struct key *key)/* make sure no one's trying to change or use the key when we mark * it */ -down_write (&key->sem); -set_bit (KEY_FLAG_REVOKED , &key->flags); - -if (key->type->revoke) +down_write_nested (&key->sem,1 ); +if (!test_and_set_bit (KEY_FLAG_REVOKED , &key->flags) && + key->type->revoke) key->type->revoke (key);up_write (&key->sem);
security/keys/process_keys.c +13 -3
@@ -26,7 +26,7 @@ static DEFINE_MUTEX(key_session_mutex);/* the root user's tracking struct */ struct key_user root_key_user = { .usage =ATOMIC_INIT (3 ), - .consq =LIST_HEAD_INIT (root_key_user.consq), + .cons_lock =__MUTEX_INITIALIZER (root_key_user.cons_lock), .lock =__SPIN_LOCK_UNLOCKED (root_key_user.lock), .nkeys =ATOMIC_INIT (2 ), .nikeys =ATOMIC_INIT (2 ),
@@ -679,8 +679,18 @@ key_ref_t lookup_user_key(struct task_struct *context, key_serial_t id,break ; } -/* check the status */ -if (perm) { +if (!partial) { + ret =wait_for_key_construction (key, true); +switch (ret) { +case -ERESTARTSYS : + goto invalid_key; +default : +if (perm) + goto invalid_key; +case 0 : +break ; + } + }else if (perm) { ret =key_validate (key);if (ret <0 ) goto invalid_key;
security/keys/request_key.c +274 -282
688 lines not on this page · raw message
security/keys/request_key_auth.c +8 -1
@@ -127,6 +127,7 @@ static void request_key_auth_destroy(struct key *key) }key_put (rka->target_key); +kfree (rka->callout_info);kfree (rka); }/* end request_key_auth_destroy() */
@@ -151,6 +152,12 @@ struct key *request_key_auth_new(struct key *target, const char *callout_info)kleave (" = -ENOMEM" );return ERR_PTR (-ENOMEM ); } + rka->callout_info =kmalloc (strlen (callout_info) +1 ,GFP_KERNEL ); +if (!rka->callout_info) { +kleave (" = -ENOMEM" ); +kfree (rka); +return ERR_PTR (-ENOMEM ); + }/* see if the calling process is already servicing the key request of * another process */
@@ -179,7 +186,7 @@ struct key *request_key_auth_new(struct key *target, const char *callout_info) } rka->target_key =key_get (target); - rka->callout_info = callout_info; +strcpy (rka->callout_info, callout_info);/* allocate the auth key */ sprintf (desc,"%x" , target->serial);
Quoted 23 lines · On Wed, 12 Sep 2007 15:25:08 +0100 David Howells <[email protected]> wrote:
Make
request_key()and co fundamentally asynchronous to make it easier for NFS to make use of them. There are now accessor functions that do asynchronous constructions, a wait function to wait for construction to complete, and a completion function for the key type to indicate completion of construction.Note that the construction queue is now gone. Instead, keys under construction are linked in to the appropriate keyring in advance, and that anyone encountering one must wait for it to be complete before they can use it. This is done automatically for userspace.
The following auxiliary changes are also made:
(1) Key type implementation stuff is split from
linux/key.hintolinux/key-type.h.(2)
AF_RXRPCprovides a way to allocate null rxrpc-type keys so that AFS does not need to callkey_instantiate_and_link()directly.(3) Documentation.
Signed-off-by David Howells <[email protected]>---
Quoted 15 lines
Documentation/keys-request-key.txt | 25 +- Documentation/keys.txt | 91 +++++- Documentation/networking/rxrpc.txt | 7 fs/afs/cell.c | 17 - include/keys/rxrpc-type.h | 2 include/linux/key-type.h | 112 +++++++ include/linux/key.h | 99 +----- net/rxrpc/af_rxrpc.c | 1 net/rxrpc/ar-key.c | 31 ++ security/keys/internal.h | 29 +- security/keys/key.c | 27 +- security/keys/process_keys.c | 16 + security/keys/request_key.c | 556 ++++++++++++++++++------------------ security/keys/request_key_auth.c | 9 + 14 files changed, 595 insertions(+), 427 deletions(-)
So who's going to review this? Nobody? Well gee, maybe it was my turn anyway.
checkpatch generates a pile of warnings, all of which afacit are legit.
security/keys/internal.h line 31
Quoted 12 lines
security/keys/internal.h +7
-3+#ifdef __KDEBUG +#define kenter (FMT , ...) printk("==> %s("FMT")\n",__FUNCTION__ ,##__VA_ARGS__) +#define kleave (FMT , ...) printk("<== %s()"FMT"\n",__FUNCTION__ ,##__VA_ARGS__) +#define kdebug (FMT , ...) printk(FMT"\n" ,##__VA_ARGS__)#else -#define kenter(FMT, a...) do {} while(0) -#define kleave(FMT, a...) do {} while(0) -#define kdebug(FMT, a...) do {} while(0) +#define kenter (FMT , ...) no_printk("==> %s("FMT")\n",__FUNCTION__ ,##__VA_ARGS__) +#define kleave (FMT , ...) no_printk("<== %s()"FMT"\n",__FUNCTION__ ,##__VA_ARGS__) +#define kdebug (FMT , ...) no_printk(FMT"\n" ,##__VA_ARGS__)#endif
How many different&private versions of this sort of thing do we have? Sigh.
The debug infrastructure changes don't appear to be related to the intent of this patch and aren't changelogged. Not that this matters a lot.
security/keys/key.c void key_revoke(struct key *key) · line 904
Quoted 9 lines
security/keys/key.c +1
-4@@ -901,10 +901,9 @@ void key_revoke(struct key *key)/* make sure no one's trying to change or use the key when we mark * it */ -down_write (&key->sem); -set_bit (KEY_FLAG_REVOKED , &key->flags); - -if (key->type->revoke) +down_write_nested (&key->sem,1 );
It'd be nice to add a comment explaining to the long-suffering reader why down_write_nested() is used here.
security/keys/request_key_auth.c struct key *request_key_auth_new(struct key *target, const char *callout_info) · line 160
Quoted 10 lines
security/keys/request_key_auth.c +6
-0@@ -151,6 +152,12 @@ struct key *request_key_auth_new(struct key *target, const char *callout_info)kleave (" = -ENOMEM" );return ERR_PTR (-ENOMEM ); } + rka->callout_info =kmalloc (strlen (callout_info) +1 ,GFP_KERNEL ); +if (!rka->callout_info) { +kleave (" = -ENOMEM" ); +kfree (rka); +return ERR_PTR (-ENOMEM ); + }
You could actually use kstrdup() here, I think.
rka->callout_info gets leaked later on (goto auth_key_revoked)
security/keys/request_key_auth.c struct key *request_key_auth_new(struct key *target, const char *callout_info) · line 192
Quoted 11 lines
security/keys/request_key_auth.c +1
-1/* see if the calling process is already servicing the key request of * another process */ @@ -179,7 +186,7 @@ struct key *request_key_auth_new(struct key *target, const char *callout_info) } rka->target_key =key_get (target); - rka->callout_info = callout_info; +strcpy (rka->callout_info, callout_info);/* allocate the auth key */ sprintf (desc,"%x" , target->serial);
Apart from that the changes look reasonable to me, but I am not a suitable reviewer for keys, NFS or rxrpc stuff. Who is??
David Howells
maintainerQuoted 2 lines · Andrew Morton
So who's going to review this? Nobody? Well gee, maybe it was my turn anyway.
Well, Kevin Coffman has reviewed it and tested it against his NFS keys patches.
Quoted 1 line
checkpatch generates a pile of warnings, all of which afacit are legit.
For this warning:
ERROR: need space after that ',' (ctx:WxV)
#627: FILE: security/keys/internal.h:28:
+#define kenter(FMT, ...) no_printk("==> %s("FMT")\n",__FUNCTION__ ,##__VA_ARGS__)
^
This is with good reason. Some versions of cpp get the ## resolution "wrong" if __VA_ARGS__ is empty (ie: there are no arguments to the macro that correspond to the "..."). This can be worked around by abutting the "," the "##" and the "__VA_ARGS__" with no spaces, and inserting a space before the comma.
If I remember correctly, the comma won't be removed if it doesn't abut the ##__VA_ARGS__, and the comma and everthing that abuts it on its LHS can be removed if there's also no space. Without the "##", the comma isn't removed.
Consider the result of doing x("a"); where x(y, ...) is #defined to each of the folowing:
DEFINITION RESULT =============================== ======================= printk(y, __VA_ARGS__) print(y, ); printk(y ,__VA_ARGS__) print(y ,); printk(y,##__VA_ARGS__) print(); printk(y, ##__VA_ARGS__) print(y, ); printk(y ,##__VA_ARGS__) print(y );
Quoted 2 lines
It'd be nice to add a comment explaining to the long-suffering reader why
down_write_nested()is used here.
I'll change the comment to:
/* make sure no one's trying to change or use the key when we mark it
* - we tell lockdep that we might nest because we might be revoking an
* authorisation key whilst holding the sem on a key we've just
* instantiated
*/
Quoted 1 line
You could actually use
kstrdup()here, I think.
I could, but that potentially wastes time in two ways: firstly, there can be an error before we've finished setting up, and that can lead to us having wasted the time spent making the copy; and secondly, the call to kstrdup itself wastes a bit of time because of the extra call made.
On the other hand, the code might end up a bit shorter, and isn't particularly fast path anyway.
Quoted 1 line
rka->callout_infogets leaked later on (gotoauth_key_revoked)
Fixed.
Quoted 2 lines
Apart from that the changes look reasonable to me, but I am not a suitable reviewer for keys, NFS or rxrpc stuff. Who is??
Kevin Coffman and Trond for the NFS stuff, both of whom are CC'd.
David
Quoted 8 lines · On Mon, 17 Sep 2007 11:56:00 +0100 David Howells <[email protected]> wrote:
Quoted 1 line
checkpatch generates a pile of warnings, all of which afacit are legit.
For this warning:
ERROR: need space after that ',' (ctx:WxV)
#627: FILE:security/keys/internal.h:28:
+#definekenter(FMT, ...)no_printk("==> %s("FMT")\n",__FUNCTION__ ,##__VA_ARGS__)
Quoted 7 lines
^This is with good reason. Some versions of cpp get the ## resolution "wrong" if
__VA_ARGS__is empty (ie: there are no arguments to the macro that correspond to the "..."). This can be worked around by abutting the "," the "##" and the "__VA_ARGS__" with no spaces, and inserting a space before the comma.
Tell me about it - I fixed that about 10000000000 times. Then we upped the minimum required gcc version so it is no longer a problem.