Commit ccc3e6d9 authored by David Howells's avatar David Howells

KEYS: Define a __key_get() wrapper to use rather than atomic_inc()

Define a __key_get() wrapper to use rather than atomic_inc() on the key usage
count as this makes it easier to hook in refcount error debugging.
Signed-off-by: default avatarDavid Howells <dhowells@redhat.com>
parent d0a059ca
...@@ -960,14 +960,17 @@ payload contents" for more information. ...@@ -960,14 +960,17 @@ payload contents" for more information.
the argument will not be parsed. the argument will not be parsed.
(*) Extra references can be made to a key by calling the following function: (*) Extra references can be made to a key by calling one of the following
functions:
struct key *__key_get(struct key *key);
struct key *key_get(struct key *key); struct key *key_get(struct key *key);
These need to be disposed of by calling key_put() when they've been Keys so references will need to be disposed of by calling key_put() when
finished with. The key pointer passed in will be returned. If the pointer they've been finished with. The key pointer passed in will be returned.
is NULL or CONFIG_KEYS is not set then the key will not be dereferenced and
no increment will take place. In the case of key_get(), if the pointer is NULL or CONFIG_KEYS is not set
then the key will not be dereferenced and no increment will take place.
(*) A key's serial number can be obtained by calling: (*) A key's serial number can be obtained by calling:
......
...@@ -219,13 +219,17 @@ extern void key_revoke(struct key *key); ...@@ -219,13 +219,17 @@ extern void key_revoke(struct key *key);
extern void key_invalidate(struct key *key); extern void key_invalidate(struct key *key);
extern void key_put(struct key *key); extern void key_put(struct key *key);
static inline struct key *key_get(struct key *key) static inline struct key *__key_get(struct key *key)
{ {
if (key)
atomic_inc(&key->usage); atomic_inc(&key->usage);
return key; return key;
} }
static inline struct key *key_get(struct key *key)
{
return key ? __key_get(key) : key;
}
static inline void key_ref_put(key_ref_t key_ref) static inline void key_ref_put(key_ref_t key_ref)
{ {
key_put(key_ref_to_ptr(key_ref)); key_put(key_ref_to_ptr(key_ref));
......
...@@ -644,7 +644,7 @@ struct key *key_lookup(key_serial_t id) ...@@ -644,7 +644,7 @@ struct key *key_lookup(key_serial_t id)
/* this races with key_put(), but that doesn't matter since key_put() /* this races with key_put(), but that doesn't matter since key_put()
* doesn't actually change the key * doesn't actually change the key
*/ */
atomic_inc(&key->usage); __key_get(key);
error: error:
spin_unlock(&key_serial_lock); spin_unlock(&key_serial_lock);
......
...@@ -479,7 +479,7 @@ key_ref_t keyring_search_aux(key_ref_t keyring_ref, ...@@ -479,7 +479,7 @@ key_ref_t keyring_search_aux(key_ref_t keyring_ref,
/* we found a viable match */ /* we found a viable match */
found: found:
atomic_inc(&key->usage); __key_get(key);
key->last_used_at = ctx->now.tv_sec; key->last_used_at = ctx->now.tv_sec;
keyring->last_used_at = ctx->now.tv_sec; keyring->last_used_at = ctx->now.tv_sec;
while (sp > 0) while (sp > 0)
...@@ -573,7 +573,7 @@ key_ref_t __keyring_search_one(key_ref_t keyring_ref, ...@@ -573,7 +573,7 @@ key_ref_t __keyring_search_one(key_ref_t keyring_ref,
return ERR_PTR(-ENOKEY); return ERR_PTR(-ENOKEY);
found: found:
atomic_inc(&key->usage); __key_get(key);
keyring->last_used_at = key->last_used_at = keyring->last_used_at = key->last_used_at =
current_kernel_time().tv_sec; current_kernel_time().tv_sec;
rcu_read_unlock(); rcu_read_unlock();
...@@ -909,7 +909,7 @@ void __key_link(struct key *keyring, struct key *key, ...@@ -909,7 +909,7 @@ void __key_link(struct key *keyring, struct key *key,
klist = rcu_dereference_locked_keyring(keyring); klist = rcu_dereference_locked_keyring(keyring);
atomic_inc(&key->usage); __key_get(key);
keyring->last_used_at = key->last_used_at = keyring->last_used_at = key->last_used_at =
current_kernel_time().tv_sec; current_kernel_time().tv_sec;
......
...@@ -235,7 +235,7 @@ int install_session_keyring_to_cred(struct cred *cred, struct key *keyring) ...@@ -235,7 +235,7 @@ int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
if (IS_ERR(keyring)) if (IS_ERR(keyring))
return PTR_ERR(keyring); return PTR_ERR(keyring);
} else { } else {
atomic_inc(&keyring->usage); __key_get(keyring);
} }
/* install the keyring */ /* install the keyring */
...@@ -544,7 +544,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags, ...@@ -544,7 +544,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
} }
key = ctx.cred->thread_keyring; key = ctx.cred->thread_keyring;
atomic_inc(&key->usage); __key_get(key);
key_ref = make_key_ref(key, 1); key_ref = make_key_ref(key, 1);
break; break;
...@@ -562,7 +562,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags, ...@@ -562,7 +562,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
} }
key = ctx.cred->process_keyring; key = ctx.cred->process_keyring;
atomic_inc(&key->usage); __key_get(key);
key_ref = make_key_ref(key, 1); key_ref = make_key_ref(key, 1);
break; break;
...@@ -593,7 +593,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags, ...@@ -593,7 +593,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
rcu_read_lock(); rcu_read_lock();
key = rcu_dereference(ctx.cred->session_keyring); key = rcu_dereference(ctx.cred->session_keyring);
atomic_inc(&key->usage); __key_get(key);
rcu_read_unlock(); rcu_read_unlock();
key_ref = make_key_ref(key, 1); key_ref = make_key_ref(key, 1);
break; break;
...@@ -606,7 +606,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags, ...@@ -606,7 +606,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
} }
key = ctx.cred->user->uid_keyring; key = ctx.cred->user->uid_keyring;
atomic_inc(&key->usage); __key_get(key);
key_ref = make_key_ref(key, 1); key_ref = make_key_ref(key, 1);
break; break;
...@@ -618,7 +618,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags, ...@@ -618,7 +618,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
} }
key = ctx.cred->user->session_keyring; key = ctx.cred->user->session_keyring;
atomic_inc(&key->usage); __key_get(key);
key_ref = make_key_ref(key, 1); key_ref = make_key_ref(key, 1);
break; break;
...@@ -632,7 +632,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags, ...@@ -632,7 +632,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
if (!key) if (!key)
goto error; goto error;
atomic_inc(&key->usage); __key_get(key);
key_ref = make_key_ref(key, 1); key_ref = make_key_ref(key, 1);
break; break;
...@@ -648,7 +648,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags, ...@@ -648,7 +648,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
} else { } else {
rka = ctx.cred->request_key_auth->payload.data; rka = ctx.cred->request_key_auth->payload.data;
key = rka->dest_keyring; key = rka->dest_keyring;
atomic_inc(&key->usage); __key_get(key);
} }
up_read(&ctx.cred->request_key_auth->sem); up_read(&ctx.cred->request_key_auth->sem);
if (!key) if (!key)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment