Skip to content

Commit

Permalink
Merge tag 'keys-request-20190626' of git://git.kernel.org/pub/scm/lin…
Browse files Browse the repository at this point in the history
…ux/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
  • Loading branch information
torvalds committed Jul 9, 2019
2 parents d44a627 + 3b8c4a0 commit c236b6d
Show file tree
Hide file tree
Showing 15 changed files with 229 additions and 164 deletions.
38 changes: 10 additions & 28 deletions Documentation/security/keys/core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,10 @@ The main syscalls are:
/sbin/request-key will be invoked in an attempt to obtain a key. The
callout_info string will be passed as an argument to the program.

To link a key into the destination keyring the key must grant link
permission on the key to the caller and the keyring must grant write
permission.

See also Documentation/security/keys/request-key.rst.


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


* A key can be requested asynchronously by calling one of::

struct key *request_key_async(const struct key_type *type,
const char *description,
const void *callout_info,
size_t callout_len);
or::

struct key *request_key_async_with_auxdata(const struct key_type *type,
const char *description,
const char *callout_info,
size_t callout_len,
void *aux);
which are asynchronous equivalents of request_key() and
request_key_with_auxdata() respectively.

These two functions return with the key potentially still under
construction. To wait for construction completion, the following should be
called::

int wait_for_key_construction(struct key *key, bool intr);
* To search for a key under RCU conditions, call::

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


* When it is no longer required, the key should be released using::
Expand Down
33 changes: 14 additions & 19 deletions Documentation/security/keys/request-key.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,8 @@ or::

or::

struct key *request_key_async(const struct key_type *type,
const char *description,
const char *callout_info,
size_t callout_len);

or::

struct key *request_key_async_with_auxdata(const struct key_type *type,
const char *description,
const char *callout_info,
size_t callout_len,
void *aux);
struct key *request_key_rcu(const struct key_type *type,
const char *description);

Or by userspace invoking the request_key system call::

Expand All @@ -48,14 +38,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() calls are like the in-kernel request_key*()
calls, except that they permit auxiliary data to be passed to the upcaller (the
The request_key_with_auxdata() calls is like the in-kernel request_key() call,
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 request_key_rcu() call is like the in-kernel request_key() call, except
that it doesn't check for keys that are under construction and doesn't attempt
to construct missing keys.

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
Expand Down Expand Up @@ -148,7 +138,7 @@ The Search Algorithm

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

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

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

When request_key() is invoked, if CONFIG_KEYS_REQUEST_CACHE=y, a per-task
one-key cache is first checked for a match.

When search_process_keyrings() is invoked, it performs the following searches
until one succeeds:

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

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

Only if all these fail does the whole thing fail with the highest priority
error. Note that several errors may have come from LSM.
Expand Down
1 change: 1 addition & 0 deletions include/keys/request_key_auth-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* Authorisation record for request_key().
*/
struct request_key_auth {
struct rcu_head rcu;
struct key *target_key;
struct key *dest_keyring;
const struct cred *cred;
Expand Down
14 changes: 3 additions & 11 deletions include/linux/key.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,23 +269,15 @@ extern struct key *request_key(struct key_type *type,
const char *description,
const char *callout_info);

extern struct key *request_key_rcu(struct key_type *type,
const char *description);

extern struct key *request_key_with_auxdata(struct key_type *type,
const char *description,
const void *callout_info,
size_t callout_len,
void *aux);

extern struct key *request_key_async(struct key_type *type,
const char *description,
const void *callout_info,
size_t callout_len);

extern struct key *request_key_async_with_auxdata(struct key_type *type,
const char *description,
const void *callout_info,
size_t callout_len,
void *aux);

extern int wait_for_key_construction(struct key *key, bool intr);

extern int key_validate(const struct key *key);
Expand Down
5 changes: 5 additions & 0 deletions include/linux/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,11 @@ struct task_struct {
/* Effective (overridable) subjective task credentials (COW): */
const struct cred __rcu *cred;

#ifdef CONFIG_KEYS
/* Cached requested key. */
struct key *cached_requested_key;
#endif

/*
* executable name, excluding path.
*
Expand Down
7 changes: 7 additions & 0 deletions include/linux/tracehook.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ static inline void tracehook_notify_resume(struct pt_regs *regs)
if (unlikely(current->task_works))
task_work_run();

#ifdef CONFIG_KEYS_REQUEST_CACHE
if (unlikely(current->cached_requested_key)) {
key_put(current->cached_requested_key);
current->cached_requested_key = NULL;
}
#endif

mem_cgroup_handle_over_high();
blkcg_maybe_throttle_current();
}
Expand Down
9 changes: 9 additions & 0 deletions kernel/cred.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ void exit_creds(struct task_struct *tsk)
validate_creds(cred);
alter_cred_subscribers(cred, -1);
put_cred(cred);

#ifdef CONFIG_KEYS_REQUEST_CACHE
key_put(current->cached_requested_key);
current->cached_requested_key = NULL;
#endif
}

/**
Expand Down Expand Up @@ -323,6 +328,10 @@ int copy_creds(struct task_struct *p, unsigned long clone_flags)
struct cred *new;
int ret;

#ifdef CONFIG_KEYS_REQUEST_CACHE
p->cached_requested_key = NULL;
#endif

if (
#ifdef CONFIG_KEYS
!p->cred->thread_keyring &&
Expand Down
18 changes: 18 additions & 0 deletions security/keys/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ config KEYS_COMPAT
def_bool y
depends on COMPAT && KEYS

config KEYS_REQUEST_CACHE
bool "Enable temporary caching of the last request_key() result"
depends on KEYS
help
This option causes the result of the last successful request_key()
call that didn't upcall to the kernel to be cached temporarily in the
task_struct. The cache is cleared by exit and just prior to the
resumption of userspace.

This allows the key used for multiple step processes where each step
wants to request a key that is likely the same as the one requested
by the last step to save on the searching.

An example of such a process is a pathwalk through a network
filesystem in which each method needs to request an authentication
key. Pathwalk will call multiple methods for each dentry traversed
(permission, d_revalidate, lookup, getxattr, getacl, ...).

config PERSISTENT_KEYRINGS
bool "Enable register of persistent per-UID keyrings"
depends on KEYS
Expand Down
6 changes: 3 additions & 3 deletions security/keys/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ struct keyring_search_context {

extern bool key_default_cmp(const struct key *key,
const struct key_match_data *match_data);
extern key_ref_t keyring_search_aux(key_ref_t keyring_ref,
extern key_ref_t keyring_search_rcu(key_ref_t keyring_ref,
struct keyring_search_context *ctx);

extern key_ref_t search_my_process_keyrings(struct keyring_search_context *ctx);
extern key_ref_t search_process_keyrings(struct keyring_search_context *ctx);
extern key_ref_t search_cred_keyrings_rcu(struct keyring_search_context *ctx);
extern key_ref_t search_process_keyrings_rcu(struct keyring_search_context *ctx);

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

Expand Down
4 changes: 2 additions & 2 deletions security/keys/key.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ static int __key_instantiate_and_link(struct key *key,

/* disable the authorisation key */
if (authkey)
key_revoke(authkey);
key_invalidate(authkey);

if (prep->expiry != TIME64_MAX) {
key->expiry = prep->expiry;
Expand Down Expand Up @@ -612,7 +612,7 @@ int key_reject_and_link(struct key *key,

/* disable the authorisation key */
if (authkey)
key_revoke(authkey);
key_invalidate(authkey);
}

mutex_unlock(&key_construction_mutex);
Expand Down
16 changes: 9 additions & 7 deletions security/keys/keyring.c
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ static bool search_nested_keyrings(struct key *keyring,
}

/**
* keyring_search_aux - Search a keyring tree for a key matching some criteria
* keyring_search_rcu - Search a keyring tree for a matching key under RCU
* @keyring_ref: A pointer to the keyring with possession indicator.
* @ctx: The keyring search context.
*
Expand All @@ -843,7 +843,9 @@ static bool search_nested_keyrings(struct key *keyring,
* addition, the LSM gets to forbid keyring searches and key matches.
*
* The search is performed as a breadth-then-depth search up to the prescribed
* limit (KEYRING_SEARCH_MAX_DEPTH).
* limit (KEYRING_SEARCH_MAX_DEPTH). The caller must hold the RCU read lock to
* prevent keyrings from being destroyed or rearranged whilst they are being
* searched.
*
* Keys are matched to the type provided and are then filtered by the match
* function, which is given the description to use in any way it sees fit. The
Expand All @@ -862,7 +864,7 @@ static bool search_nested_keyrings(struct key *keyring,
* In the case of a successful return, the possession attribute from
* @keyring_ref is propagated to the returned key reference.
*/
key_ref_t keyring_search_aux(key_ref_t keyring_ref,
key_ref_t keyring_search_rcu(key_ref_t keyring_ref,
struct keyring_search_context *ctx)
{
struct key *keyring;
Expand All @@ -884,11 +886,9 @@ key_ref_t keyring_search_aux(key_ref_t keyring_ref,
return ERR_PTR(err);
}

rcu_read_lock();
ctx->now = ktime_get_real_seconds();
if (search_nested_keyrings(keyring, ctx))
__key_get(key_ref_to_ptr(ctx->result));
rcu_read_unlock();
return ctx->result;
}

Expand All @@ -898,7 +898,7 @@ key_ref_t keyring_search_aux(key_ref_t keyring_ref,
* @type: The type of keyring we want to find.
* @description: The name of the keyring we want to find.
*
* As keyring_search_aux() above, but using the current task's credentials and
* As keyring_search_rcu() above, but using the current task's credentials and
* type's default matching function and preferred search method.
*/
key_ref_t keyring_search(key_ref_t keyring,
Expand All @@ -924,7 +924,9 @@ key_ref_t keyring_search(key_ref_t keyring,
return ERR_PTR(ret);
}

key = keyring_search_aux(keyring, &ctx);
rcu_read_lock();
key = keyring_search_rcu(keyring, &ctx);
rcu_read_unlock();

if (type->match_free)
type->match_free(&ctx.match_data);
Expand Down
4 changes: 3 additions & 1 deletion security/keys/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ static int proc_keys_show(struct seq_file *m, void *v)
* skip if the key does not indicate the possessor can view it
*/
if (key->perm & KEY_POS_VIEW) {
skey_ref = search_my_process_keyrings(&ctx);
rcu_read_lock();
skey_ref = search_cred_keyrings_rcu(&ctx);
rcu_read_unlock();
if (!IS_ERR(skey_ref)) {
key_ref_put(skey_ref);
key_ref = make_key_ref(key, 1);
Expand Down
Loading

0 comments on commit c236b6d

Please sign in to comment.