Skip to content

Commit 896f195

Browse files
committed
keys: Provide request_key_rcu()
Provide a request_key_rcu() function that can be used to request a key under RCU conditions. It can only search and check permissions; it cannot allocate a new key, upcall or wait for an upcall to complete. It may return a partially constructed key. Signed-off-by: David Howells <[email protected]>
1 parent e59428f commit 896f195

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

Documentation/security/keys/core.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,16 @@ payload contents" for more information.
11471147
case error ERESTARTSYS will be returned.
11481148

11491149

1150+
* To search for a key under RCU conditions, call::
1151+
1152+
struct key *request_key_rcu(const struct key_type *type,
1153+
const char *description);
1154+
1155+
which is similar to request_key() except that it does not check for keys
1156+
that are under construction and it will not call out to userspace to
1157+
construct a key if it can't find a match.
1158+
1159+
11501160
* When it is no longer required, the key should be released using::
11511161

11521162
void key_put(struct key *key);

Documentation/security/keys/request-key.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ or::
3636
size_t callout_len,
3737
void *aux);
3838

39+
or::
40+
41+
struct key *request_key_rcu(const struct key_type *type,
42+
const char *description);
43+
3944
Or by userspace invoking the request_key system call::
4045

4146
key_serial_t request_key(const char *type,
@@ -57,6 +62,10 @@ The two async in-kernel calls may return keys that are still in the process of
5762
being constructed. The two non-async ones will wait for construction to
5863
complete first.
5964

65+
The request_key_rcu() call is like the in-kernel request_key() call, except
66+
that it doesn't check for keys that are under construction and doesn't attempt
67+
to construct missing keys.
68+
6069
The userspace interface links the key to a keyring associated with the process
6170
to prevent the key from going away, and returns the serial number of the key to
6271
the caller.

include/linux/key.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ extern struct key *request_key(struct key_type *type,
274274
const char *description,
275275
const char *callout_info);
276276

277+
extern struct key *request_key_rcu(struct key_type *type,
278+
const char *description);
279+
277280
extern struct key *request_key_with_auxdata(struct key_type *type,
278281
const char *description,
279282
const void *callout_info,

security/keys/request_key.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,3 +756,47 @@ struct key *request_key_async_with_auxdata(struct key_type *type,
756756
callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA);
757757
}
758758
EXPORT_SYMBOL(request_key_async_with_auxdata);
759+
760+
/**
761+
* request_key_rcu - Request key from RCU-read-locked context
762+
* @type: The type of key we want.
763+
* @description: The name of the key we want.
764+
*
765+
* Request a key from a context that we may not sleep in (such as RCU-mode
766+
* pathwalk). Keys under construction are ignored.
767+
*
768+
* Return a pointer to the found key if successful, -ENOKEY if we couldn't find
769+
* a key or some other error if the key found was unsuitable or inaccessible.
770+
*/
771+
struct key *request_key_rcu(struct key_type *type, const char *description)
772+
{
773+
struct keyring_search_context ctx = {
774+
.index_key.type = type,
775+
.index_key.description = description,
776+
.index_key.desc_len = strlen(description),
777+
.cred = current_cred(),
778+
.match_data.cmp = key_default_cmp,
779+
.match_data.raw_data = description,
780+
.match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
781+
.flags = (KEYRING_SEARCH_DO_STATE_CHECK |
782+
KEYRING_SEARCH_SKIP_EXPIRED),
783+
};
784+
struct key *key;
785+
key_ref_t key_ref;
786+
787+
kenter("%s,%s", type->name, description);
788+
789+
/* search all the process keyrings for a key */
790+
key_ref = search_process_keyrings_rcu(&ctx);
791+
if (IS_ERR(key_ref)) {
792+
key = ERR_CAST(key_ref);
793+
if (PTR_ERR(key_ref) == -EAGAIN)
794+
key = ERR_PTR(-ENOKEY);
795+
} else {
796+
key = key_ref_to_ptr(key_ref);
797+
}
798+
799+
kleave(" = %p", key);
800+
return key;
801+
}
802+
EXPORT_SYMBOL(request_key_rcu);

0 commit comments

Comments
 (0)