Skip to content

Commit 34574dd

Browse files
dhowellstorvalds
authored andcommitted
keys: Handle there being no fallback destination keyring for request_key()
When request_key() is called, without there being any standard process keyrings on which to fall back if a destination keyring is not specified, an oops is liable to occur when construct_alloc_key() calls down_write() on dest_keyring's semaphore. Due to function inlining this may be seen as an oops in down_write() as called from request_key_and_link(). This situation crops up during boot, where request_key() is called from within the kernel (such as in CIFS mounts) where nobody is actually logged in, and so PAM has not had a chance to create a session keyring and user keyrings to act as the fallback. To fix this, make construct_alloc_key() not attempt to cache a key if there is no fallback key if no destination keyring is given specifically. Signed-off-by: David Howells <[email protected]> Tested-by: Jeff Layton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 11ff5f6 commit 34574dd

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

security/keys/request_key.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ static int construct_alloc_key(struct key_type *type,
311311

312312
set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
313313

314-
down_write(&dest_keyring->sem);
314+
if (dest_keyring)
315+
down_write(&dest_keyring->sem);
315316

316317
/* attach the key to the destination keyring under lock, but we do need
317318
* to do another check just in case someone beat us to it whilst we
@@ -322,10 +323,12 @@ static int construct_alloc_key(struct key_type *type,
322323
if (!IS_ERR(key_ref))
323324
goto key_already_present;
324325

325-
__key_link(dest_keyring, key);
326+
if (dest_keyring)
327+
__key_link(dest_keyring, key);
326328

327329
mutex_unlock(&key_construction_mutex);
328-
up_write(&dest_keyring->sem);
330+
if (dest_keyring)
331+
up_write(&dest_keyring->sem);
329332
mutex_unlock(&user->cons_lock);
330333
*_key = key;
331334
kleave(" = 0 [%d]", key_serial(key));

0 commit comments

Comments
 (0)