Skip to content

Commit 76181c1

Browse files
dhowellsLinus Torvalds
authored andcommitted
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) Adjust the debugging macros so that they're -Wformat checked even if they are disabled, and make it so they can be enabled simply by defining __KDEBUG to be consistent with other code of mine. (3) Documentation. [alan@lxorguk.ukuu.org.uk: keys: missing word in documentation] Signed-off-by: David Howells <dhowells@redhat.com> Signed-off-by: Alan Cox <alan@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 398c95b commit 76181c1

14 files changed

Lines changed: 605 additions & 435 deletions

File tree

‎Documentation/keys-request-key.txt‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ or:
2020
const char *callout_string,
2121
void *aux);
2222

23+
or:
24+
25+
struct key *request_key_async(const struct key_type *type,
26+
const char *description,
27+
const char *callout_string);
28+
29+
or:
30+
31+
struct key *request_key_async_with_auxdata(const struct key_type *type,
32+
const char *description,
33+
const char *callout_string,
34+
void *aux);
35+
2336
Or by userspace invoking the request_key system call:
2437

2538
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
3245
destroyed. The kernel interface returns a pointer directly to the key, and
3346
it's up to the caller to destroy the key.
3447

35-
The request_key_with_auxdata() call is like the in-kernel request_key() call,
36-
except that it permits auxiliary data to be passed to the upcaller (the default
37-
is NULL). This is only useful for those key types that define their own upcall
38-
mechanism rather than using /sbin/request-key.
48+
The request_key*_with_auxdata() calls are like the in-kernel request_key*()
49+
calls, except that they permit auxiliary data to be passed to the upcaller (the
50+
default is NULL). This is only useful for those key types that define their
51+
own upcall mechanism rather than using /sbin/request-key.
52+
53+
The two async in-kernel calls may return keys that are still in the process of
54+
being constructed. The two non-async ones will wait for construction to
55+
complete first.
3956

4057
The userspace interface links the key to a keyring associated with the process
4158
to prevent the key from going away, and returns the serial number of the key to

‎Documentation/keys.txt‎

Lines changed: 79 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
This service allows cryptographic keys, authentication tokens, cross-domain
66
user mappings, and similar to be cached in the kernel for the use of
7-
filesystems other kernel services.
7+
filesystems and other kernel services.
88

99
Keyrings are permitted; these are a special type of key that can hold links to
1010
other keys. Processes each have three standard keyring subscriptions that a
@@ -726,6 +726,15 @@ call, and the key released upon close. How to deal with conflicting keys due to
726726
two different users opening the same file is left to the filesystem author to
727727
solve.
728728

729+
To access the key manager, the following header must be #included:
730+
731+
<linux/key.h>
732+
733+
Specific key types should have a header file under include/keys/ that should be
734+
used to access that type. For keys of type "user", for example, that would be:
735+
736+
<keys/user-type.h>
737+
729738
Note that there are two different types of pointers to keys that may be
730739
encountered:
731740

@@ -791,6 +800,36 @@ payload contents" for more information.
791800
passed to the key_type->request_key() op if it exists.
792801

793802

803+
(*) A key can be requested asynchronously by calling one of:
804+
805+
struct key *request_key_async(const struct key_type *type,
806+
const char *description,
807+
const char *callout_string);
808+
809+
or:
810+
811+
struct key *request_key_async_with_auxdata(const struct key_type *type,
812+
const char *description,
813+
const char *callout_string,
814+
void *aux);
815+
816+
which are asynchronous equivalents of request_key() and
817+
request_key_with_auxdata() respectively.
818+
819+
These two functions return with the key potentially still under
820+
construction. To wait for contruction completion, the following should be
821+
called:
822+
823+
int wait_for_key_construction(struct key *key, bool intr);
824+
825+
The function will wait for the key to finish being constructed and then
826+
invokes key_validate() to return an appropriate value to indicate the state
827+
of the key (0 indicates the key is usable).
828+
829+
If intr is true, then the wait can be interrupted by a signal, in which
830+
case error ERESTARTSYS will be returned.
831+
832+
794833
(*) When it is no longer required, the key should be released using:
795834

796835
void key_put(struct key *key);
@@ -924,7 +963,11 @@ DEFINING A KEY TYPE
924963

925964
A kernel service may want to define its own key type. For instance, an AFS
926965
filesystem might want to define a Kerberos 5 ticket key type. To do this, it
927-
author fills in a struct key_type and registers it with the system.
966+
author fills in a key_type struct and registers it with the system.
967+
968+
Source files that implement key types should include the following header file:
969+
970+
<linux/key-type.h>
928971

929972
The structure has a number of fields, some of which are mandatory:
930973

@@ -1053,22 +1096,44 @@ The structure has a number of fields, some of which are mandatory:
10531096
as might happen when the userspace buffer is accessed.
10541097

10551098

1056-
(*) int (*request_key)(struct key *key, struct key *authkey, const char *op,
1099+
(*) int (*request_key)(struct key_construction *cons, const char *op,
10571100
void *aux);
10581101

1059-
This method is optional. If provided, request_key() and
1060-
request_key_with_auxdata() will invoke this function rather than
1061-
upcalling to /sbin/request-key to operate upon a key of this type.
1102+
This method is optional. If provided, request_key() and friends will
1103+
invoke this function rather than upcalling to /sbin/request-key to operate
1104+
upon a key of this type.
1105+
1106+
The aux parameter is as passed to request_key_async_with_auxdata() and
1107+
similar or is NULL otherwise. Also passed are the construction record for
1108+
the key to be operated upon and the operation type (currently only
1109+
"create").
1110+
1111+
This method is permitted to return before the upcall is complete, but the
1112+
following function must be called under all circumstances to complete the
1113+
instantiation process, whether or not it succeeds, whether or not there's
1114+
an error:
1115+
1116+
void complete_request_key(struct key_construction *cons, int error);
1117+
1118+
The error parameter should be 0 on success, -ve on error. The
1119+
construction record is destroyed by this action and the authorisation key
1120+
will be revoked. If an error is indicated, the key under construction
1121+
will be negatively instantiated if it wasn't already instantiated.
1122+
1123+
If this method returns an error, that error will be returned to the
1124+
caller of request_key*(). complete_request_key() must be called prior to
1125+
returning.
1126+
1127+
The key under construction and the authorisation key can be found in the
1128+
key_construction struct pointed to by cons:
1129+
1130+
(*) struct key *key;
1131+
1132+
The key under construction.
10621133

1063-
The aux parameter is as passed to request_key_with_auxdata() or is NULL
1064-
otherwise. Also passed are the key to be operated upon, the
1065-
authorisation key for this operation and the operation type (currently
1066-
only "create").
1134+
(*) struct key *authkey;
10671135

1068-
This function should return only when the upcall is complete. Upon return
1069-
the authorisation key will be revoked, and the target key will be
1070-
negatively instantiated if it is still uninstantiated. The error will be
1071-
returned to the caller of request_key*().
1136+
The authorisation key.
10721137

10731138

10741139
============================

‎Documentation/networking/rxrpc.txt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,3 +857,10 @@ The kernel interface functions are as follows:
857857

858858
This is used to extract the error number from a message indicating either
859859
a local error occurred or a network error occurred.
860+
861+
(*) Allocate a null key for doing anonymous security.
862+
863+
struct key *rxrpc_get_null_key(const char *keyname);
864+
865+
This is used to allocate a null RxRPC key that can be used to indicate
866+
anonymous security for a particular domain.

‎fs/afs/cell.c‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ static struct afs_cell *afs_cell_root;
3333
static struct afs_cell *afs_cell_alloc(const char *name, char *vllist)
3434
{
3535
struct afs_cell *cell;
36+
struct key *key;
3637
size_t namelen;
3738
char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp, *next;
3839
int ret;
@@ -89,20 +90,14 @@ static struct afs_cell *afs_cell_alloc(const char *name, char *vllist)
8990
do {
9091
*dp++ = toupper(*cp);
9192
} while (*cp++);
92-
cell->anonymous_key = key_alloc(&key_type_rxrpc, keyname, 0, 0, current,
93-
KEY_POS_SEARCH, KEY_ALLOC_NOT_IN_QUOTA);
94-
if (IS_ERR(cell->anonymous_key)) {
95-
_debug("no key");
96-
ret = PTR_ERR(cell->anonymous_key);
97-
goto error;
98-
}
9993

100-
ret = key_instantiate_and_link(cell->anonymous_key, NULL, 0,
101-
NULL, NULL);
102-
if (ret < 0) {
103-
_debug("instantiate failed");
94+
key = rxrpc_get_null_key(keyname);
95+
if (IS_ERR(key)) {
96+
_debug("no key");
97+
ret = PTR_ERR(key);
10498
goto error;
10599
}
100+
cell->anonymous_key = key;
106101

107102
_debug("anon key %p{%x}",
108103
cell->anonymous_key, key_serial(cell->anonymous_key));

‎include/keys/rxrpc-type.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@
1919
*/
2020
extern struct key_type key_type_rxrpc;
2121

22+
extern struct key *rxrpc_get_null_key(const char *);
23+
2224
#endif /* _KEYS_USER_TYPE_H */

‎include/linux/key-type.h‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/* Definitions for key type implementations
2+
*
3+
* Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4+
* Written by David Howells (dhowells@redhat.com)
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU General Public Licence
8+
* as published by the Free Software Foundation; either version
9+
* 2 of the Licence, or (at your option) any later version.
10+
*/
11+
12+
#ifndef _LINUX_KEY_TYPE_H
13+
#define _LINUX_KEY_TYPE_H
14+
15+
#include <linux/key.h>
16+
17+
#ifdef CONFIG_KEYS
18+
19+
/*
20+
* key under-construction record
21+
* - passed to the request_key actor if supplied
22+
*/
23+
struct key_construction {
24+
struct key *key; /* key being constructed */
25+
struct key *authkey;/* authorisation for key being constructed */
26+
};
27+
28+
typedef int (*request_key_actor_t)(struct key_construction *key,
29+
const char *op, void *aux);
30+
31+
/*
32+
* kernel managed key type definition
33+
*/
34+
struct key_type {
35+
/* name of the type */
36+
const char *name;
37+
38+
/* default payload length for quota precalculation (optional)
39+
* - this can be used instead of calling key_payload_reserve(), that
40+
* function only needs to be called if the real datalen is different
41+
*/
42+
size_t def_datalen;
43+
44+
/* instantiate a key of this type
45+
* - this method should call key_payload_reserve() to determine if the
46+
* user's quota will hold the payload
47+
*/
48+
int (*instantiate)(struct key *key, const void *data, size_t datalen);
49+
50+
/* update a key of this type (optional)
51+
* - this method should call key_payload_reserve() to recalculate the
52+
* quota consumption
53+
* - the key must be locked against read when modifying
54+
*/
55+
int (*update)(struct key *key, const void *data, size_t datalen);
56+
57+
/* match a key against a description */
58+
int (*match)(const struct key *key, const void *desc);
59+
60+
/* clear some of the data from a key on revokation (optional)
61+
* - the key's semaphore will be write-locked by the caller
62+
*/
63+
void (*revoke)(struct key *key);
64+
65+
/* clear the data from a key (optional) */
66+
void (*destroy)(struct key *key);
67+
68+
/* describe a key */
69+
void (*describe)(const struct key *key, struct seq_file *p);
70+
71+
/* read a key's data (optional)
72+
* - permission checks will be done by the caller
73+
* - the key's semaphore will be readlocked by the caller
74+
* - should return the amount of data that could be read, no matter how
75+
* much is copied into the buffer
76+
* - shouldn't do the copy if the buffer is NULL
77+
*/
78+
long (*read)(const struct key *key, char __user *buffer, size_t buflen);
79+
80+
/* handle request_key() for this type instead of invoking
81+
* /sbin/request-key (optional)
82+
* - key is the key to instantiate
83+
* - authkey is the authority to assume when instantiating this key
84+
* - op is the operation to be done, usually "create"
85+
* - the call must not return until the instantiation process has run
86+
* its course
87+
*/
88+
request_key_actor_t request_key;
89+
90+
/* internal fields */
91+
struct list_head link; /* link in types list */
92+
};
93+
94+
extern struct key_type key_type_keyring;
95+
96+
extern int register_key_type(struct key_type *ktype);
97+
extern void unregister_key_type(struct key_type *ktype);
98+
99+
extern int key_payload_reserve(struct key *key, size_t datalen);
100+
extern int key_instantiate_and_link(struct key *key,
101+
const void *data,
102+
size_t datalen,
103+
struct key *keyring,
104+
struct key *instkey);
105+
extern int key_negate_and_link(struct key *key,
106+
unsigned timeout,
107+
struct key *keyring,
108+
struct key *instkey);
109+
extern void complete_request_key(struct key_construction *cons, int error);
110+
111+
#endif /* CONFIG_KEYS */
112+
#endif /* _LINUX_KEY_TYPE_H */

0 commit comments

Comments
 (0)