Skip to content

Commit c236b6d

Browse files
committed
Merge tag 'keys-request-20190626' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs
Pull request_key improvements from David Howells: "These are all request_key()-related, including a fix and some improvements: - Fix the lack of a Link permission check on a key found by request_key(), thereby enabling request_key() to link keys that don't grant this permission to the target keyring (which must still grant Write permission). Note that the key must be in the caller's keyrings already to be found. - Invalidate used request_key authentication keys rather than revoking them, so that they get cleaned up immediately rather than hanging around till the expiry time is passed. - Move the RCU locks outwards from the keyring search functions so that a request_key_rcu() can be provided. This can be called in RCU mode, so it can't sleep and can't upcall - but it can be called from LOOKUP_RCU pathwalk mode. - Cache the latest positive result of request_key*() temporarily in task_struct so that filesystems that make a lot of request_key() calls during pathwalk can take advantage of it to avoid having to redo the searching. This requires CONFIG_KEYS_REQUEST_CACHE=y. It is assumed that the key just found is likely to be used multiple times in each step in an RCU pathwalk, and is likely to be reused for the next step too. Note that the cleanup of the cache is done on TIF_NOTIFY_RESUME, just before userspace resumes, and on exit" * tag 'keys-request-20190626' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs: keys: Kill off request_key_async{,_with_auxdata} keys: Cache result of request_key*() temporarily in task_struct keys: Provide request_key_rcu() keys: Move the RCU locks outwards from the keyring search functions keys: Invalidate used request_key authentication keys keys: Fix request_key() lack of Link perm check on found key
2 parents d44a627 + 3b8c4a0 commit c236b6d

15 files changed

Lines changed: 229 additions & 164 deletions

File tree

‎Documentation/security/keys/core.rst‎

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,10 @@ The main syscalls are:
433433
/sbin/request-key will be invoked in an attempt to obtain a key. The
434434
callout_info string will be passed as an argument to the program.
435435

436+
To link a key into the destination keyring the key must grant link
437+
permission on the key to the caller and the keyring must grant write
438+
permission.
439+
436440
See also Documentation/security/keys/request-key.rst.
437441

438442

@@ -1111,36 +1115,14 @@ payload contents" for more information.
11111115
is a blob of length callout_len, if given (the length may be 0).
11121116

11131117

1114-
* A key can be requested asynchronously by calling one of::
1115-
1116-
struct key *request_key_async(const struct key_type *type,
1117-
const char *description,
1118-
const void *callout_info,
1119-
size_t callout_len);
1120-
1121-
or::
1122-
1123-
struct key *request_key_async_with_auxdata(const struct key_type *type,
1124-
const char *description,
1125-
const char *callout_info,
1126-
size_t callout_len,
1127-
void *aux);
1128-
1129-
which are asynchronous equivalents of request_key() and
1130-
request_key_with_auxdata() respectively.
1131-
1132-
These two functions return with the key potentially still under
1133-
construction. To wait for construction completion, the following should be
1134-
called::
1135-
1136-
int wait_for_key_construction(struct key *key, bool intr);
1118+
* To search for a key under RCU conditions, call::
11371119

1138-
The function will wait for the key to finish being constructed and then
1139-
invokes key_validate() to return an appropriate value to indicate the state
1140-
of the key (0 indicates the key is usable).
1120+
struct key *request_key_rcu(const struct key_type *type,
1121+
const char *description);
11411122
1142-
If intr is true, then the wait can be interrupted by a signal, in which
1143-
case error ERESTARTSYS will be returned.
1123+
which is similar to request_key() except that it does not check for keys
1124+
that are under construction and it will not call out to userspace to
1125+
construct a key if it can't find a match.
11441126

11451127

11461128
* When it is no longer required, the key should be released using::

‎Documentation/security/keys/request-key.rst‎

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,8 @@ or::
2323

2424
or::
2525

26-
struct key *request_key_async(const struct key_type *type,
27-
const char *description,
28-
const char *callout_info,
29-
size_t callout_len);
30-
31-
or::
32-
33-
struct key *request_key_async_with_auxdata(const struct key_type *type,
34-
const char *description,
35-
const char *callout_info,
36-
size_t callout_len,
37-
void *aux);
26+
struct key *request_key_rcu(const struct key_type *type,
27+
const char *description);
3828

3929
Or by userspace invoking the request_key system call::
4030

@@ -48,14 +38,14 @@ does not need to link the key to a keyring to prevent it from being immediately
4838
destroyed. The kernel interface returns a pointer directly to the key, and
4939
it's up to the caller to destroy the key.
5040

51-
The request_key*_with_auxdata() calls are like the in-kernel request_key*()
52-
calls, except that they permit auxiliary data to be passed to the upcaller (the
41+
The request_key_with_auxdata() calls is like the in-kernel request_key() call,
42+
except that they permit auxiliary data to be passed to the upcaller (the
5343
default is NULL). This is only useful for those key types that define their
5444
own upcall mechanism rather than using /sbin/request-key.
5545

56-
The two async in-kernel calls may return keys that are still in the process of
57-
being constructed. The two non-async ones will wait for construction to
58-
complete first.
46+
The request_key_rcu() call is like the in-kernel request_key() call, except
47+
that it doesn't check for keys that are under construction and doesn't attempt
48+
to construct missing keys.
5949

6050
The userspace interface links the key to a keyring associated with the process
6151
to prevent the key from going away, and returns the serial number of the key to
@@ -148,7 +138,7 @@ The Search Algorithm
148138

149139
A search of any particular keyring proceeds in the following fashion:
150140

151-
1) When the key management code searches for a key (keyring_search_aux) it
141+
1) When the key management code searches for a key (keyring_search_rcu) it
152142
firstly calls key_permission(SEARCH) on the keyring it's starting with,
153143
if this denies permission, it doesn't search further.
154144

@@ -167,6 +157,9 @@ The process stops immediately a valid key is found with permission granted to
167157
use it. Any error from a previous match attempt is discarded and the key is
168158
returned.
169159

160+
When request_key() is invoked, if CONFIG_KEYS_REQUEST_CACHE=y, a per-task
161+
one-key cache is first checked for a match.
162+
170163
When search_process_keyrings() is invoked, it performs the following searches
171164
until one succeeds:
172165

@@ -186,7 +179,9 @@ until one succeeds:
186179
c) The calling process's session keyring is searched.
187180

188181
The moment one succeeds, all pending errors are discarded and the found key is
189-
returned.
182+
returned. If CONFIG_KEYS_REQUEST_CACHE=y, then that key is placed in the
183+
per-task cache, displacing the previous key. The cache is cleared on exit or
184+
just prior to resumption of userspace.
190185

191186
Only if all these fail does the whole thing fail with the highest priority
192187
error. Note that several errors may have come from LSM.

‎include/keys/request_key_auth-type.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* Authorisation record for request_key().
1515
*/
1616
struct request_key_auth {
17+
struct rcu_head rcu;
1718
struct key *target_key;
1819
struct key *dest_keyring;
1920
const struct cred *cred;

‎include/linux/key.h‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -269,23 +269,15 @@ extern struct key *request_key(struct key_type *type,
269269
const char *description,
270270
const char *callout_info);
271271

272+
extern struct key *request_key_rcu(struct key_type *type,
273+
const char *description);
274+
272275
extern struct key *request_key_with_auxdata(struct key_type *type,
273276
const char *description,
274277
const void *callout_info,
275278
size_t callout_len,
276279
void *aux);
277280

278-
extern struct key *request_key_async(struct key_type *type,
279-
const char *description,
280-
const void *callout_info,
281-
size_t callout_len);
282-
283-
extern struct key *request_key_async_with_auxdata(struct key_type *type,
284-
const char *description,
285-
const void *callout_info,
286-
size_t callout_len,
287-
void *aux);
288-
289281
extern int wait_for_key_construction(struct key *key, bool intr);
290282

291283
extern int key_validate(const struct key *key);

‎include/linux/sched.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,11 @@ struct task_struct {
892892
/* Effective (overridable) subjective task credentials (COW): */
893893
const struct cred __rcu *cred;
894894

895+
#ifdef CONFIG_KEYS
896+
/* Cached requested key. */
897+
struct key *cached_requested_key;
898+
#endif
899+
895900
/*
896901
* executable name, excluding path.
897902
*

‎include/linux/tracehook.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,13 @@ static inline void tracehook_notify_resume(struct pt_regs *regs)
184184
if (unlikely(current->task_works))
185185
task_work_run();
186186

187+
#ifdef CONFIG_KEYS_REQUEST_CACHE
188+
if (unlikely(current->cached_requested_key)) {
189+
key_put(current->cached_requested_key);
190+
current->cached_requested_key = NULL;
191+
}
192+
#endif
193+
187194
mem_cgroup_handle_over_high();
188195
blkcg_maybe_throttle_current();
189196
}

‎kernel/cred.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ void exit_creds(struct task_struct *tsk)
170170
validate_creds(cred);
171171
alter_cred_subscribers(cred, -1);
172172
put_cred(cred);
173+
174+
#ifdef CONFIG_KEYS_REQUEST_CACHE
175+
key_put(current->cached_requested_key);
176+
current->cached_requested_key = NULL;
177+
#endif
173178
}
174179

175180
/**
@@ -323,6 +328,10 @@ int copy_creds(struct task_struct *p, unsigned long clone_flags)
323328
struct cred *new;
324329
int ret;
325330

331+
#ifdef CONFIG_KEYS_REQUEST_CACHE
332+
p->cached_requested_key = NULL;
333+
#endif
334+
326335
if (
327336
#ifdef CONFIG_KEYS
328337
!p->cred->thread_keyring &&

‎security/keys/Kconfig‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ config KEYS_COMPAT
2525
def_bool y
2626
depends on COMPAT && KEYS
2727

28+
config KEYS_REQUEST_CACHE
29+
bool "Enable temporary caching of the last request_key() result"
30+
depends on KEYS
31+
help
32+
This option causes the result of the last successful request_key()
33+
call that didn't upcall to the kernel to be cached temporarily in the
34+
task_struct. The cache is cleared by exit and just prior to the
35+
resumption of userspace.
36+
37+
This allows the key used for multiple step processes where each step
38+
wants to request a key that is likely the same as the one requested
39+
by the last step to save on the searching.
40+
41+
An example of such a process is a pathwalk through a network
42+
filesystem in which each method needs to request an authentication
43+
key. Pathwalk will call multiple methods for each dentry traversed
44+
(permission, d_revalidate, lookup, getxattr, getacl, ...).
45+
2846
config PERSISTENT_KEYRINGS
2947
bool "Enable register of persistent per-UID keyrings"
3048
depends on KEYS

‎security/keys/internal.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ struct keyring_search_context {
135135

136136
extern bool key_default_cmp(const struct key *key,
137137
const struct key_match_data *match_data);
138-
extern key_ref_t keyring_search_aux(key_ref_t keyring_ref,
138+
extern key_ref_t keyring_search_rcu(key_ref_t keyring_ref,
139139
struct keyring_search_context *ctx);
140140

141-
extern key_ref_t search_my_process_keyrings(struct keyring_search_context *ctx);
142-
extern key_ref_t search_process_keyrings(struct keyring_search_context *ctx);
141+
extern key_ref_t search_cred_keyrings_rcu(struct keyring_search_context *ctx);
142+
extern key_ref_t search_process_keyrings_rcu(struct keyring_search_context *ctx);
143143

144144
extern struct key *find_keyring_by_name(const char *name, bool uid_keyring);
145145

‎security/keys/key.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ static int __key_instantiate_and_link(struct key *key,
455455

456456
/* disable the authorisation key */
457457
if (authkey)
458-
key_revoke(authkey);
458+
key_invalidate(authkey);
459459

460460
if (prep->expiry != TIME64_MAX) {
461461
key->expiry = prep->expiry;
@@ -612,7 +612,7 @@ int key_reject_and_link(struct key *key,
612612

613613
/* disable the authorisation key */
614614
if (authkey)
615-
key_revoke(authkey);
615+
key_invalidate(authkey);
616616
}
617617

618618
mutex_unlock(&key_construction_mutex);

0 commit comments

Comments
 (0)