Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* Manage a process's keyrings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2004-2005, 2008 Red Hat, Inc. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Written by David Howells (dhowells@redhat.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/sched/user.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/keyctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/user_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/init_task.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <keys/request_key_auth-type.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) /* Session keyring create vs join semaphore */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static DEFINE_MUTEX(key_session_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) /* The root user's tracking struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) struct key_user root_key_user = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	.usage		= REFCOUNT_INIT(3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	.cons_lock	= __MUTEX_INITIALIZER(root_key_user.cons_lock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	.lock		= __SPIN_LOCK_UNLOCKED(root_key_user.lock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	.nkeys		= ATOMIC_INIT(2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	.nikeys		= ATOMIC_INIT(2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	.uid		= GLOBAL_ROOT_UID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * Get or create a user register keyring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static struct key *get_user_register(struct user_namespace *user_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct key *reg_keyring = READ_ONCE(user_ns->user_keyring_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	if (reg_keyring)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		return reg_keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	down_write(&user_ns->keyring_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	/* Make sure there's a register keyring.  It gets owned by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	 * user_namespace's owner.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	reg_keyring = user_ns->user_keyring_register;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	if (!reg_keyring) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		reg_keyring = keyring_alloc(".user_reg",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 					    user_ns->owner, INVALID_GID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 					    &init_cred,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 					    KEY_POS_WRITE | KEY_POS_SEARCH |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 					    KEY_USR_VIEW | KEY_USR_READ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 					    0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 					    NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		if (!IS_ERR(reg_keyring))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 			smp_store_release(&user_ns->user_keyring_register,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 					  reg_keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	up_write(&user_ns->keyring_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	/* We don't return a ref since the keyring is pinned by the user_ns */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	return reg_keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  * Look up the user and user session keyrings for the current process's UID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  * creating them if they don't exist.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) int look_up_user_keyrings(struct key **_user_keyring,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 			  struct key **_user_session_keyring)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	const struct cred *cred = current_cred();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	struct user_namespace *user_ns = current_user_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	struct key *reg_keyring, *uid_keyring, *session_keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	key_perm_t user_keyring_perm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	key_ref_t uid_keyring_r, session_keyring_r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	uid_t uid = from_kuid(user_ns, cred->user->uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	char buf[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	user_keyring_perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	kenter("%u", uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	reg_keyring = get_user_register(user_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	if (IS_ERR(reg_keyring))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		return PTR_ERR(reg_keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	down_write(&user_ns->keyring_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	/* Get the user keyring.  Note that there may be one in existence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	 * already as it may have been pinned by a session, but the user_struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	 * pointing to it may have been destroyed by setuid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	snprintf(buf, sizeof(buf), "_uid.%u", uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	uid_keyring_r = keyring_search(make_key_ref(reg_keyring, true),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 				       &key_type_keyring, buf, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	kdebug("_uid %p", uid_keyring_r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (uid_keyring_r == ERR_PTR(-EAGAIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		uid_keyring = keyring_alloc(buf, cred->user->uid, INVALID_GID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 					    cred, user_keyring_perm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 					    KEY_ALLOC_UID_KEYRING |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 					    KEY_ALLOC_IN_QUOTA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 					    NULL, reg_keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		if (IS_ERR(uid_keyring)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			ret = PTR_ERR(uid_keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	} else if (IS_ERR(uid_keyring_r)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		ret = PTR_ERR(uid_keyring_r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		uid_keyring = key_ref_to_ptr(uid_keyring_r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	/* Get a default session keyring (which might also exist already) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	snprintf(buf, sizeof(buf), "_uid_ses.%u", uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	session_keyring_r = keyring_search(make_key_ref(reg_keyring, true),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 					   &key_type_keyring, buf, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	kdebug("_uid_ses %p", session_keyring_r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (session_keyring_r == ERR_PTR(-EAGAIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		session_keyring = keyring_alloc(buf, cred->user->uid, INVALID_GID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 						cred, user_keyring_perm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 						KEY_ALLOC_UID_KEYRING |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 						KEY_ALLOC_IN_QUOTA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 						NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		if (IS_ERR(session_keyring)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			ret = PTR_ERR(session_keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			goto error_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		/* We install a link from the user session keyring to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		 * the user keyring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		ret = key_link(session_keyring, uid_keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			goto error_release_session;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		/* And only then link the user-session keyring to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		 * register.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		ret = key_link(reg_keyring, session_keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			goto error_release_session;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	} else if (IS_ERR(session_keyring_r)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		ret = PTR_ERR(session_keyring_r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		goto error_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		session_keyring = key_ref_to_ptr(session_keyring_r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	up_write(&user_ns->keyring_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	if (_user_session_keyring)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		*_user_session_keyring = session_keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		key_put(session_keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	if (_user_keyring)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		*_user_keyring = uid_keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		key_put(uid_keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	kleave(" = 0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) error_release_session:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	key_put(session_keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) error_release:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	key_put(uid_keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	up_write(&user_ns->keyring_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	kleave(" = %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)  * Get the user session keyring if it exists, but don't create it if it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)  * doesn't.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct key *get_user_session_keyring_rcu(const struct cred *cred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	struct key *reg_keyring = READ_ONCE(cred->user_ns->user_keyring_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	key_ref_t session_keyring_r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	char buf[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	struct keyring_search_context ctx = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		.index_key.type		= &key_type_keyring,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		.index_key.description	= buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		.cred			= cred,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		.match_data.cmp		= key_default_cmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		.match_data.raw_data	= buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		.match_data.lookup_type	= KEYRING_SEARCH_LOOKUP_DIRECT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		.flags			= KEYRING_SEARCH_DO_STATE_CHECK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	if (!reg_keyring)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	ctx.index_key.desc_len = snprintf(buf, sizeof(buf), "_uid_ses.%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 					  from_kuid(cred->user_ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 						    cred->user->uid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	session_keyring_r = keyring_search_rcu(make_key_ref(reg_keyring, true),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 					       &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	if (IS_ERR(session_keyring_r))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	return key_ref_to_ptr(session_keyring_r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)  * Install a thread keyring to the given credentials struct if it didn't have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)  * one already.  This is allowed to overrun the quota.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)  * Return: 0 if a thread keyring is now present; -errno on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) int install_thread_keyring_to_cred(struct cred *new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	struct key *keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (new->thread_keyring)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	keyring = keyring_alloc("_tid", new->uid, new->gid, new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 				KEY_POS_ALL | KEY_USR_VIEW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 				KEY_ALLOC_QUOTA_OVERRUN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 				NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if (IS_ERR(keyring))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		return PTR_ERR(keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	new->thread_keyring = keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)  * Install a thread keyring to the current task if it didn't have one already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)  * Return: 0 if a thread keyring is now present; -errno on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) static int install_thread_keyring(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	struct cred *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	new = prepare_creds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	if (!new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	ret = install_thread_keyring_to_cred(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		abort_creds(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	return commit_creds(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)  * Install a process keyring to the given credentials struct if it didn't have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)  * one already.  This is allowed to overrun the quota.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)  * Return: 0 if a process keyring is now present; -errno on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) int install_process_keyring_to_cred(struct cred *new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	struct key *keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	if (new->process_keyring)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	keyring = keyring_alloc("_pid", new->uid, new->gid, new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 				KEY_POS_ALL | KEY_USR_VIEW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 				KEY_ALLOC_QUOTA_OVERRUN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 				NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	if (IS_ERR(keyring))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		return PTR_ERR(keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	new->process_keyring = keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)  * Install a process keyring to the current task if it didn't have one already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)  * Return: 0 if a process keyring is now present; -errno on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static int install_process_keyring(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	struct cred *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	new = prepare_creds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	if (!new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	ret = install_process_keyring_to_cred(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		abort_creds(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	return commit_creds(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)  * Install the given keyring as the session keyring of the given credentials
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)  * struct, replacing the existing one if any.  If the given keyring is NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)  * then install a new anonymous session keyring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)  * @cred can not be in use by any task yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)  * Return: 0 on success; -errno on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	struct key *old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	might_sleep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	/* create an empty session keyring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	if (!keyring) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		flags = KEY_ALLOC_QUOTA_OVERRUN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		if (cred->session_keyring)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			flags = KEY_ALLOC_IN_QUOTA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		keyring = keyring_alloc("_ses", cred->uid, cred->gid, cred,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 					KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 					flags, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		if (IS_ERR(keyring))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			return PTR_ERR(keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		__key_get(keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	/* install the keyring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	old = cred->session_keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	cred->session_keyring = keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	if (old)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		key_put(old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)  * Install the given keyring as the session keyring of the current task,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)  * replacing the existing one if any.  If the given keyring is NULL, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)  * install a new anonymous session keyring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)  * Return: 0 on success; -errno on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) static int install_session_keyring(struct key *keyring)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	struct cred *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	new = prepare_creds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	if (!new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	ret = install_session_keyring_to_cred(new, keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		abort_creds(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	return commit_creds(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)  * Handle the fsuid changing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) void key_fsuid_changed(struct cred *new_cred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	/* update the ownership of the thread keyring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	if (new_cred->thread_keyring) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		down_write(&new_cred->thread_keyring->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		new_cred->thread_keyring->uid = new_cred->fsuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		up_write(&new_cred->thread_keyring->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)  * Handle the fsgid changing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) void key_fsgid_changed(struct cred *new_cred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	/* update the ownership of the thread keyring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	if (new_cred->thread_keyring) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		down_write(&new_cred->thread_keyring->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		new_cred->thread_keyring->gid = new_cred->fsgid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		up_write(&new_cred->thread_keyring->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)  * Search the process keyrings attached to the supplied cred for the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)  * matching key under RCU conditions (the caller must be holding the RCU read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)  * lock).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)  * The search criteria are the type and the match function.  The description is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)  * given to the match function as a parameter, but doesn't otherwise influence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)  * the search.  Typically the match function will compare the description
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)  * parameter to the key's description.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)  * This can only search keyrings that grant Search permission to the supplied
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)  * credentials.  Keyrings linked to searched keyrings will also be searched if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)  * they grant Search permission too.  Keys can only be found if they grant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)  * Search permission to the credentials.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)  * Returns a pointer to the key with the key usage count incremented if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)  * successful, -EAGAIN if we didn't find any matching key or -ENOKEY if we only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)  * matched negative keys.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)  * In the case of a successful return, the possession attribute is set on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)  * returned key reference.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) key_ref_t search_cred_keyrings_rcu(struct keyring_search_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	struct key *user_session;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	key_ref_t key_ref, ret, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	const struct cred *cred = ctx->cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	/* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	 * searchable, but we failed to find a key or we found a negative key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	 * otherwise we want to return a sample error (probably -EACCES) if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	 * none of the keyrings were searchable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	key_ref = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	err = ERR_PTR(-EAGAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	/* search the thread keyring first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	if (cred->thread_keyring) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		key_ref = keyring_search_rcu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 			make_key_ref(cred->thread_keyring, 1), ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		if (!IS_ERR(key_ref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 			goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		switch (PTR_ERR(key_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		case -EAGAIN: /* no key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		case -ENOKEY: /* negative key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 			ret = key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 			err = key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	/* search the process keyring second */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	if (cred->process_keyring) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		key_ref = keyring_search_rcu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 			make_key_ref(cred->process_keyring, 1), ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		if (!IS_ERR(key_ref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 			goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		switch (PTR_ERR(key_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		case -EAGAIN: /* no key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 			fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		case -ENOKEY: /* negative key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 			ret = key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 			err = key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	/* search the session keyring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	if (cred->session_keyring) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		key_ref = keyring_search_rcu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 			make_key_ref(cred->session_keyring, 1), ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		if (!IS_ERR(key_ref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 			goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		switch (PTR_ERR(key_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		case -EAGAIN: /* no key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 			fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		case -ENOKEY: /* negative key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 			ret = key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 			err = key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	/* or search the user-session keyring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	else if ((user_session = get_user_session_keyring_rcu(cred))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		key_ref = keyring_search_rcu(make_key_ref(user_session, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 					     ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		key_put(user_session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		if (!IS_ERR(key_ref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 			goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		switch (PTR_ERR(key_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		case -EAGAIN: /* no key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 			fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		case -ENOKEY: /* negative key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 			ret = key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 			err = key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	/* no key - decide on the error we're going to go for */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	key_ref = ret ? ret : err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	return key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)  * Search the process keyrings attached to the supplied cred for the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)  * matching key in the manner of search_my_process_keyrings(), but also search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)  * the keys attached to the assumed authorisation key using its credentials if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)  * one is available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)  * The caller must be holding the RCU read lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)  * Return same as search_cred_keyrings_rcu().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) key_ref_t search_process_keyrings_rcu(struct keyring_search_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	struct request_key_auth *rka;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	key_ref = search_cred_keyrings_rcu(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	if (!IS_ERR(key_ref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	err = key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	/* if this process has an instantiation authorisation key, then we also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	 * search the keyrings of the process mentioned there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	 * - we don't permit access to request_key auth keys via this method
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	if (ctx->cred->request_key_auth &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	    ctx->cred == current_cred() &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	    ctx->index_key.type != &key_type_request_key_auth
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	    ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		const struct cred *cred = ctx->cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		if (key_validate(cred->request_key_auth) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 			rka = ctx->cred->request_key_auth->payload.data[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 			//// was search_process_keyrings() [ie. recursive]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 			ctx->cred = rka->cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 			key_ref = search_cred_keyrings_rcu(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 			ctx->cred = cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 			if (!IS_ERR(key_ref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 				goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 			ret = key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	/* no key - decide on the error we're going to go for */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 		key_ref = ERR_PTR(-ENOKEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	else if (err == ERR_PTR(-EACCES))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 		key_ref = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 		key_ref = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	return key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)  * See if the key we're looking at is the target key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) bool lookup_user_key_possessed(const struct key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 			       const struct key_match_data *match_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	return key == match_data->raw_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)  * Look up a key ID given us by userspace with a given permissions mask to get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)  * the key it refers to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)  * Flags can be passed to request that special keyrings be created if referred
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)  * to directly, to permit partially constructed keys to be found and to skip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)  * validity and permission checks on the found key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)  * Returns a pointer to the key with an incremented usage count if successful;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)  * -EINVAL if the key ID is invalid; -ENOKEY if the key ID does not correspond
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)  * to a key or the best found key was a negative key; -EKEYREVOKED or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)  * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)  * found key doesn't grant the requested permit or the LSM denied access to it;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)  * or -ENOMEM if a special keyring couldn't be created.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)  * In the case of a successful return, the possession attribute is set on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)  * returned key reference.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 			  enum key_need_perm need_perm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	struct keyring_search_context ctx = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 		.match_data.cmp		= lookup_user_key_possessed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 		.match_data.lookup_type	= KEYRING_SEARCH_LOOKUP_DIRECT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 		.flags			= (KEYRING_SEARCH_NO_STATE_CHECK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 					   KEYRING_SEARCH_RECURSE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	struct request_key_auth *rka;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	struct key *key, *user_session;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	key_ref_t key_ref, skey_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) try_again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	ctx.cred = get_current_cred();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	key_ref = ERR_PTR(-ENOKEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	switch (id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	case KEY_SPEC_THREAD_KEYRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 		if (!ctx.cred->thread_keyring) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 			if (!(lflags & KEY_LOOKUP_CREATE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 				goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 			ret = install_thread_keyring();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 			if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 				key_ref = ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 				goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 			goto reget_creds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 		key = ctx.cred->thread_keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 		__key_get(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 		key_ref = make_key_ref(key, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	case KEY_SPEC_PROCESS_KEYRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 		if (!ctx.cred->process_keyring) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 			if (!(lflags & KEY_LOOKUP_CREATE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 				goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 			ret = install_process_keyring();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 			if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 				key_ref = ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 				goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 			goto reget_creds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 		key = ctx.cred->process_keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 		__key_get(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 		key_ref = make_key_ref(key, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	case KEY_SPEC_SESSION_KEYRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 		if (!ctx.cred->session_keyring) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 			/* always install a session keyring upon access if one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 			 * doesn't exist yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 			ret = look_up_user_keyrings(NULL, &user_session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 				goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 			if (lflags & KEY_LOOKUP_CREATE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 				ret = join_session_keyring(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 				ret = install_session_keyring(user_session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 			key_put(user_session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 				goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 			goto reget_creds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 		} else if (test_bit(KEY_FLAG_UID_KEYRING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 				    &ctx.cred->session_keyring->flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 			   lflags & KEY_LOOKUP_CREATE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 			ret = join_session_keyring(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 				goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 			goto reget_creds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 		key = ctx.cred->session_keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 		__key_get(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 		key_ref = make_key_ref(key, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	case KEY_SPEC_USER_KEYRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 		ret = look_up_user_keyrings(&key, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 		key_ref = make_key_ref(key, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	case KEY_SPEC_USER_SESSION_KEYRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 		ret = look_up_user_keyrings(NULL, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 		key_ref = make_key_ref(key, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	case KEY_SPEC_GROUP_KEYRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 		/* group keyrings are not yet supported */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 		key_ref = ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	case KEY_SPEC_REQKEY_AUTH_KEY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 		key = ctx.cred->request_key_auth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 		if (!key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 		__key_get(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 		key_ref = make_key_ref(key, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	case KEY_SPEC_REQUESTOR_KEYRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 		if (!ctx.cred->request_key_auth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 		down_read(&ctx.cred->request_key_auth->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 		if (test_bit(KEY_FLAG_REVOKED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 			     &ctx.cred->request_key_auth->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 			key_ref = ERR_PTR(-EKEYREVOKED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 			key = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 			rka = ctx.cred->request_key_auth->payload.data[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 			key = rka->dest_keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 			__key_get(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 		up_read(&ctx.cred->request_key_auth->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 		if (!key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 		key_ref = make_key_ref(key, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 		key_ref = ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 		if (id < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 		key = key_lookup(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 		if (IS_ERR(key)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 			key_ref = ERR_CAST(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 		key_ref = make_key_ref(key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 		/* check to see if we possess the key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 		ctx.index_key			= key->index_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 		ctx.match_data.raw_data		= key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 		kdebug("check possessed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 		rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 		skey_ref = search_process_keyrings_rcu(&ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 		kdebug("possessed=%p", skey_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 		if (!IS_ERR(skey_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 			key_put(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 			key_ref = skey_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 	/* unlink does not use the nominated key in any way, so can skip all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 	 * the permission checks as it is only concerned with the keyring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 	if (need_perm != KEY_NEED_UNLINK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 		if (!(lflags & KEY_LOOKUP_PARTIAL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 			ret = wait_for_key_construction(key, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 			switch (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 			case -ERESTARTSYS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 				goto invalid_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 				if (need_perm != KEY_AUTHTOKEN_OVERRIDE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 				    need_perm != KEY_DEFER_PERM_CHECK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 					goto invalid_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 			case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 		} else if (need_perm != KEY_DEFER_PERM_CHECK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 			ret = key_validate(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 				goto invalid_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 		if (!(lflags & KEY_LOOKUP_PARTIAL) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 		    key_read_state(key) == KEY_IS_UNINSTANTIATED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 			goto invalid_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 	/* check the permissions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 	ret = key_task_permission(key_ref, ctx.cred, need_perm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 		goto invalid_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 	key->last_used_at = ktime_get_real_seconds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 	put_cred(ctx.cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 	return key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) invalid_key:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 	key_ref_put(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 	key_ref = ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 	goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 	/* if we attempted to install a keyring, then it may have caused new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 	 * creds to be installed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) reget_creds:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 	put_cred(ctx.cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 	goto try_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) EXPORT_SYMBOL(lookup_user_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)  * Join the named keyring as the session keyring if possible else attempt to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)  * create a new one of that name and join that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829)  * If the name is NULL, an empty anonymous keyring will be installed as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)  * session keyring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832)  * Named session keyrings are joined with a semaphore held to prevent the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833)  * keyrings from going away whilst the attempt is made to going them and also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834)  * to prevent a race in creating compatible session keyrings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) long join_session_keyring(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 	const struct cred *old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 	struct cred *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 	struct key *keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) 	long ret, serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 	new = prepare_creds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 	if (!new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 	old = current_cred();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 	/* if no name is provided, install an anonymous keyring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 	if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 		ret = install_session_keyring_to_cred(new, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 		serial = new->session_keyring->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 		ret = commit_creds(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 		if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 			ret = serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 		goto okay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 	/* allow the user to join or create a named keyring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) 	mutex_lock(&key_session_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) 	/* look for an existing keyring of this name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 	keyring = find_keyring_by_name(name, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) 	if (PTR_ERR(keyring) == -ENOKEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 		/* not found - try and create a new one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) 		keyring = keyring_alloc(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 			name, old->uid, old->gid, old,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 			KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ | KEY_USR_LINK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 			KEY_ALLOC_IN_QUOTA, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 		if (IS_ERR(keyring)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) 			ret = PTR_ERR(keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 			goto error2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 	} else if (IS_ERR(keyring)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 		ret = PTR_ERR(keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 		goto error2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 	} else if (keyring == new->session_keyring) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 		goto error3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 	/* we've got a keyring - now to install it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 	ret = install_session_keyring_to_cred(new, keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) 		goto error3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) 	commit_creds(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) 	mutex_unlock(&key_session_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) 	ret = keyring->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) 	key_put(keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) okay:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) error3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) 	key_put(keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) error2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) 	mutex_unlock(&key_session_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) 	abort_creds(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907)  * Replace a process's session keyring on behalf of one of its children when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)  * the target  process is about to resume userspace execution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) void key_change_session_keyring(struct callback_head *twork)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) 	const struct cred *old = current_cred();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) 	struct cred *new = container_of(twork, struct cred, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) 	if (unlikely(current->flags & PF_EXITING)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) 		put_cred(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) 	new->  uid	= old->  uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) 	new-> euid	= old-> euid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) 	new-> suid	= old-> suid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) 	new->fsuid	= old->fsuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) 	new->  gid	= old->  gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) 	new-> egid	= old-> egid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) 	new-> sgid	= old-> sgid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) 	new->fsgid	= old->fsgid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) 	new->user	= get_uid(old->user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) 	new->user_ns	= get_user_ns(old->user_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) 	new->group_info	= get_group_info(old->group_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) 	new->securebits	= old->securebits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) 	new->cap_inheritable	= old->cap_inheritable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) 	new->cap_permitted	= old->cap_permitted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) 	new->cap_effective	= old->cap_effective;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) 	new->cap_ambient	= old->cap_ambient;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) 	new->cap_bset		= old->cap_bset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) 	new->jit_keyring	= old->jit_keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) 	new->thread_keyring	= key_get(old->thread_keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) 	new->process_keyring	= key_get(old->process_keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) 	security_transfer_creds(new, old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) 	commit_creds(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949)  * Make sure that root's user and user-session keyrings exist.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) static int __init init_root_keyring(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) 	return look_up_user_keyrings(NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) late_initcall(init_root_keyring);