Skip to content

Commit 94c4554

Browse files
committed
KEYS: Fix race between key destruction and finding a keyring by name
There appears to be a race between: (1) key_gc_unused_keys() which frees key->security and then calls keyring_destroy() to unlink the name from the name list (2) find_keyring_by_name() which calls key_permission(), thus accessing key->security, on a key before checking to see whether the key usage is 0 (ie. the key is dead and might be cleaned up). Fix this by calling ->destroy() before cleaning up the core key data - including key->security. Reported-by: Petr Matousek <pmatouse@redhat.com> Signed-off-by: David Howells <dhowells@redhat.com>
1 parent ced255c commit 94c4554

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

‎security/keys/gc.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ static noinline void key_gc_unused_keys(struct list_head *keys)
134134
kdebug("- %u", key->serial);
135135
key_check(key);
136136

137+
/* Throw away the key data */
138+
if (key->type->destroy)
139+
key->type->destroy(key);
140+
137141
security_key_free(key);
138142

139143
/* deal with the user's key tracking and quota */
@@ -148,10 +152,6 @@ static noinline void key_gc_unused_keys(struct list_head *keys)
148152
if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
149153
atomic_dec(&key->user->nikeys);
150154

151-
/* now throw away the key memory */
152-
if (key->type->destroy)
153-
key->type->destroy(key);
154-
155155
key_user_put(key->user);
156156

157157
kfree(key->description);

0 commit comments

Comments
 (0)