Skip to content

Commit ccc3e6d

Browse files
committed
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: David Howells <dhowells@redhat.com>
1 parent d0a059c commit ccc3e6d

File tree

5 files changed

+27
-20
lines changed

5 files changed

+27
-20
lines changed

Documentation/security/keys.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -960,14 +960,17 @@ payload contents" for more information.
960960
the argument will not be parsed.
961961

962962

963-
(*) Extra references can be made to a key by calling the following function:
963+
(*) Extra references can be made to a key by calling one of the following
964+
functions:
964965

966+
struct key *__key_get(struct key *key);
965967
struct key *key_get(struct key *key);
966968

967-
These need to be disposed of by calling key_put() when they've been
968-
finished with. The key pointer passed in will be returned. If the pointer
969-
is NULL or CONFIG_KEYS is not set then the key will not be dereferenced and
970-
no increment will take place.
969+
Keys so references will need to be disposed of by calling key_put() when
970+
they've been finished with. The key pointer passed in will be returned.
971+
972+
In the case of key_get(), if the pointer is NULL or CONFIG_KEYS is not set
973+
then the key will not be dereferenced and no increment will take place.
971974

972975

973976
(*) A key's serial number can be obtained by calling:

include/linux/key.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,17 @@ extern void key_revoke(struct key *key);
219219
extern void key_invalidate(struct key *key);
220220
extern void key_put(struct key *key);
221221

222-
static inline struct key *key_get(struct key *key)
222+
static inline struct key *__key_get(struct key *key)
223223
{
224-
if (key)
225-
atomic_inc(&key->usage);
224+
atomic_inc(&key->usage);
226225
return key;
227226
}
228227

228+
static inline struct key *key_get(struct key *key)
229+
{
230+
return key ? __key_get(key) : key;
231+
}
232+
229233
static inline void key_ref_put(key_ref_t key_ref)
230234
{
231235
key_put(key_ref_to_ptr(key_ref));

security/keys/key.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ struct key *key_lookup(key_serial_t id)
644644
/* this races with key_put(), but that doesn't matter since key_put()
645645
* doesn't actually change the key
646646
*/
647-
atomic_inc(&key->usage);
647+
__key_get(key);
648648

649649
error:
650650
spin_unlock(&key_serial_lock);

security/keys/keyring.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ key_ref_t keyring_search_aux(key_ref_t keyring_ref,
479479

480480
/* we found a viable match */
481481
found:
482-
atomic_inc(&key->usage);
482+
__key_get(key);
483483
key->last_used_at = ctx->now.tv_sec;
484484
keyring->last_used_at = ctx->now.tv_sec;
485485
while (sp > 0)
@@ -573,7 +573,7 @@ key_ref_t __keyring_search_one(key_ref_t keyring_ref,
573573
return ERR_PTR(-ENOKEY);
574574

575575
found:
576-
atomic_inc(&key->usage);
576+
__key_get(key);
577577
keyring->last_used_at = key->last_used_at =
578578
current_kernel_time().tv_sec;
579579
rcu_read_unlock();
@@ -909,7 +909,7 @@ void __key_link(struct key *keyring, struct key *key,
909909

910910
klist = rcu_dereference_locked_keyring(keyring);
911911

912-
atomic_inc(&key->usage);
912+
__key_get(key);
913913
keyring->last_used_at = key->last_used_at =
914914
current_kernel_time().tv_sec;
915915

security/keys/process_keys.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
235235
if (IS_ERR(keyring))
236236
return PTR_ERR(keyring);
237237
} else {
238-
atomic_inc(&keyring->usage);
238+
__key_get(keyring);
239239
}
240240

241241
/* install the keyring */
@@ -544,7 +544,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
544544
}
545545

546546
key = ctx.cred->thread_keyring;
547-
atomic_inc(&key->usage);
547+
__key_get(key);
548548
key_ref = make_key_ref(key, 1);
549549
break;
550550

@@ -562,7 +562,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
562562
}
563563

564564
key = ctx.cred->process_keyring;
565-
atomic_inc(&key->usage);
565+
__key_get(key);
566566
key_ref = make_key_ref(key, 1);
567567
break;
568568

@@ -593,7 +593,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
593593

594594
rcu_read_lock();
595595
key = rcu_dereference(ctx.cred->session_keyring);
596-
atomic_inc(&key->usage);
596+
__key_get(key);
597597
rcu_read_unlock();
598598
key_ref = make_key_ref(key, 1);
599599
break;
@@ -606,7 +606,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
606606
}
607607

608608
key = ctx.cred->user->uid_keyring;
609-
atomic_inc(&key->usage);
609+
__key_get(key);
610610
key_ref = make_key_ref(key, 1);
611611
break;
612612

@@ -618,7 +618,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
618618
}
619619

620620
key = ctx.cred->user->session_keyring;
621-
atomic_inc(&key->usage);
621+
__key_get(key);
622622
key_ref = make_key_ref(key, 1);
623623
break;
624624

@@ -632,7 +632,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
632632
if (!key)
633633
goto error;
634634

635-
atomic_inc(&key->usage);
635+
__key_get(key);
636636
key_ref = make_key_ref(key, 1);
637637
break;
638638

@@ -648,7 +648,7 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
648648
} else {
649649
rka = ctx.cred->request_key_auth->payload.data;
650650
key = rka->dest_keyring;
651-
atomic_inc(&key->usage);
651+
__key_get(key);
652652
}
653653
up_read(&ctx.cred->request_key_auth->sem);
654654
if (!key)

0 commit comments

Comments
 (0)