18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* Manage a process's keyrings 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright (C) 2004-2005, 2008 Red Hat, Inc. All Rights Reserved. 58c2ecf20Sopenharmony_ci * Written by David Howells (dhowells@redhat.com) 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/init.h> 98c2ecf20Sopenharmony_ci#include <linux/sched.h> 108c2ecf20Sopenharmony_ci#include <linux/sched/user.h> 118c2ecf20Sopenharmony_ci#include <linux/keyctl.h> 128c2ecf20Sopenharmony_ci#include <linux/fs.h> 138c2ecf20Sopenharmony_ci#include <linux/err.h> 148c2ecf20Sopenharmony_ci#include <linux/mutex.h> 158c2ecf20Sopenharmony_ci#include <linux/security.h> 168c2ecf20Sopenharmony_ci#include <linux/user_namespace.h> 178c2ecf20Sopenharmony_ci#include <linux/uaccess.h> 188c2ecf20Sopenharmony_ci#include <linux/init_task.h> 198c2ecf20Sopenharmony_ci#include <keys/request_key_auth-type.h> 208c2ecf20Sopenharmony_ci#include "internal.h" 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci/* Session keyring create vs join semaphore */ 238c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(key_session_mutex); 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci/* The root user's tracking struct */ 268c2ecf20Sopenharmony_cistruct key_user root_key_user = { 278c2ecf20Sopenharmony_ci .usage = REFCOUNT_INIT(3), 288c2ecf20Sopenharmony_ci .cons_lock = __MUTEX_INITIALIZER(root_key_user.cons_lock), 298c2ecf20Sopenharmony_ci .lock = __SPIN_LOCK_UNLOCKED(root_key_user.lock), 308c2ecf20Sopenharmony_ci .nkeys = ATOMIC_INIT(2), 318c2ecf20Sopenharmony_ci .nikeys = ATOMIC_INIT(2), 328c2ecf20Sopenharmony_ci .uid = GLOBAL_ROOT_UID, 338c2ecf20Sopenharmony_ci}; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci/* 368c2ecf20Sopenharmony_ci * Get or create a user register keyring. 378c2ecf20Sopenharmony_ci */ 388c2ecf20Sopenharmony_cistatic struct key *get_user_register(struct user_namespace *user_ns) 398c2ecf20Sopenharmony_ci{ 408c2ecf20Sopenharmony_ci struct key *reg_keyring = READ_ONCE(user_ns->user_keyring_register); 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci if (reg_keyring) 438c2ecf20Sopenharmony_ci return reg_keyring; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci down_write(&user_ns->keyring_sem); 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci /* Make sure there's a register keyring. It gets owned by the 488c2ecf20Sopenharmony_ci * user_namespace's owner. 498c2ecf20Sopenharmony_ci */ 508c2ecf20Sopenharmony_ci reg_keyring = user_ns->user_keyring_register; 518c2ecf20Sopenharmony_ci if (!reg_keyring) { 528c2ecf20Sopenharmony_ci reg_keyring = keyring_alloc(".user_reg", 538c2ecf20Sopenharmony_ci user_ns->owner, INVALID_GID, 548c2ecf20Sopenharmony_ci &init_cred, 558c2ecf20Sopenharmony_ci KEY_POS_WRITE | KEY_POS_SEARCH | 568c2ecf20Sopenharmony_ci KEY_USR_VIEW | KEY_USR_READ, 578c2ecf20Sopenharmony_ci 0, 588c2ecf20Sopenharmony_ci NULL, NULL); 598c2ecf20Sopenharmony_ci if (!IS_ERR(reg_keyring)) 608c2ecf20Sopenharmony_ci smp_store_release(&user_ns->user_keyring_register, 618c2ecf20Sopenharmony_ci reg_keyring); 628c2ecf20Sopenharmony_ci } 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci up_write(&user_ns->keyring_sem); 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci /* We don't return a ref since the keyring is pinned by the user_ns */ 678c2ecf20Sopenharmony_ci return reg_keyring; 688c2ecf20Sopenharmony_ci} 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci/* 718c2ecf20Sopenharmony_ci * Look up the user and user session keyrings for the current process's UID, 728c2ecf20Sopenharmony_ci * creating them if they don't exist. 738c2ecf20Sopenharmony_ci */ 748c2ecf20Sopenharmony_ciint look_up_user_keyrings(struct key **_user_keyring, 758c2ecf20Sopenharmony_ci struct key **_user_session_keyring) 768c2ecf20Sopenharmony_ci{ 778c2ecf20Sopenharmony_ci const struct cred *cred = current_cred(); 788c2ecf20Sopenharmony_ci struct user_namespace *user_ns = current_user_ns(); 798c2ecf20Sopenharmony_ci struct key *reg_keyring, *uid_keyring, *session_keyring; 808c2ecf20Sopenharmony_ci key_perm_t user_keyring_perm; 818c2ecf20Sopenharmony_ci key_ref_t uid_keyring_r, session_keyring_r; 828c2ecf20Sopenharmony_ci uid_t uid = from_kuid(user_ns, cred->user->uid); 838c2ecf20Sopenharmony_ci char buf[20]; 848c2ecf20Sopenharmony_ci int ret; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci user_keyring_perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci kenter("%u", uid); 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci reg_keyring = get_user_register(user_ns); 918c2ecf20Sopenharmony_ci if (IS_ERR(reg_keyring)) 928c2ecf20Sopenharmony_ci return PTR_ERR(reg_keyring); 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci down_write(&user_ns->keyring_sem); 958c2ecf20Sopenharmony_ci ret = 0; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci /* Get the user keyring. Note that there may be one in existence 988c2ecf20Sopenharmony_ci * already as it may have been pinned by a session, but the user_struct 998c2ecf20Sopenharmony_ci * pointing to it may have been destroyed by setuid. 1008c2ecf20Sopenharmony_ci */ 1018c2ecf20Sopenharmony_ci snprintf(buf, sizeof(buf), "_uid.%u", uid); 1028c2ecf20Sopenharmony_ci uid_keyring_r = keyring_search(make_key_ref(reg_keyring, true), 1038c2ecf20Sopenharmony_ci &key_type_keyring, buf, false); 1048c2ecf20Sopenharmony_ci kdebug("_uid %p", uid_keyring_r); 1058c2ecf20Sopenharmony_ci if (uid_keyring_r == ERR_PTR(-EAGAIN)) { 1068c2ecf20Sopenharmony_ci uid_keyring = keyring_alloc(buf, cred->user->uid, INVALID_GID, 1078c2ecf20Sopenharmony_ci cred, user_keyring_perm, 1088c2ecf20Sopenharmony_ci KEY_ALLOC_UID_KEYRING | 1098c2ecf20Sopenharmony_ci KEY_ALLOC_IN_QUOTA, 1108c2ecf20Sopenharmony_ci NULL, reg_keyring); 1118c2ecf20Sopenharmony_ci if (IS_ERR(uid_keyring)) { 1128c2ecf20Sopenharmony_ci ret = PTR_ERR(uid_keyring); 1138c2ecf20Sopenharmony_ci goto error; 1148c2ecf20Sopenharmony_ci } 1158c2ecf20Sopenharmony_ci } else if (IS_ERR(uid_keyring_r)) { 1168c2ecf20Sopenharmony_ci ret = PTR_ERR(uid_keyring_r); 1178c2ecf20Sopenharmony_ci goto error; 1188c2ecf20Sopenharmony_ci } else { 1198c2ecf20Sopenharmony_ci uid_keyring = key_ref_to_ptr(uid_keyring_r); 1208c2ecf20Sopenharmony_ci } 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci /* Get a default session keyring (which might also exist already) */ 1238c2ecf20Sopenharmony_ci snprintf(buf, sizeof(buf), "_uid_ses.%u", uid); 1248c2ecf20Sopenharmony_ci session_keyring_r = keyring_search(make_key_ref(reg_keyring, true), 1258c2ecf20Sopenharmony_ci &key_type_keyring, buf, false); 1268c2ecf20Sopenharmony_ci kdebug("_uid_ses %p", session_keyring_r); 1278c2ecf20Sopenharmony_ci if (session_keyring_r == ERR_PTR(-EAGAIN)) { 1288c2ecf20Sopenharmony_ci session_keyring = keyring_alloc(buf, cred->user->uid, INVALID_GID, 1298c2ecf20Sopenharmony_ci cred, user_keyring_perm, 1308c2ecf20Sopenharmony_ci KEY_ALLOC_UID_KEYRING | 1318c2ecf20Sopenharmony_ci KEY_ALLOC_IN_QUOTA, 1328c2ecf20Sopenharmony_ci NULL, NULL); 1338c2ecf20Sopenharmony_ci if (IS_ERR(session_keyring)) { 1348c2ecf20Sopenharmony_ci ret = PTR_ERR(session_keyring); 1358c2ecf20Sopenharmony_ci goto error_release; 1368c2ecf20Sopenharmony_ci } 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci /* We install a link from the user session keyring to 1398c2ecf20Sopenharmony_ci * the user keyring. 1408c2ecf20Sopenharmony_ci */ 1418c2ecf20Sopenharmony_ci ret = key_link(session_keyring, uid_keyring); 1428c2ecf20Sopenharmony_ci if (ret < 0) 1438c2ecf20Sopenharmony_ci goto error_release_session; 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci /* And only then link the user-session keyring to the 1468c2ecf20Sopenharmony_ci * register. 1478c2ecf20Sopenharmony_ci */ 1488c2ecf20Sopenharmony_ci ret = key_link(reg_keyring, session_keyring); 1498c2ecf20Sopenharmony_ci if (ret < 0) 1508c2ecf20Sopenharmony_ci goto error_release_session; 1518c2ecf20Sopenharmony_ci } else if (IS_ERR(session_keyring_r)) { 1528c2ecf20Sopenharmony_ci ret = PTR_ERR(session_keyring_r); 1538c2ecf20Sopenharmony_ci goto error_release; 1548c2ecf20Sopenharmony_ci } else { 1558c2ecf20Sopenharmony_ci session_keyring = key_ref_to_ptr(session_keyring_r); 1568c2ecf20Sopenharmony_ci } 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci up_write(&user_ns->keyring_sem); 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci if (_user_session_keyring) 1618c2ecf20Sopenharmony_ci *_user_session_keyring = session_keyring; 1628c2ecf20Sopenharmony_ci else 1638c2ecf20Sopenharmony_ci key_put(session_keyring); 1648c2ecf20Sopenharmony_ci if (_user_keyring) 1658c2ecf20Sopenharmony_ci *_user_keyring = uid_keyring; 1668c2ecf20Sopenharmony_ci else 1678c2ecf20Sopenharmony_ci key_put(uid_keyring); 1688c2ecf20Sopenharmony_ci kleave(" = 0"); 1698c2ecf20Sopenharmony_ci return 0; 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_cierror_release_session: 1728c2ecf20Sopenharmony_ci key_put(session_keyring); 1738c2ecf20Sopenharmony_cierror_release: 1748c2ecf20Sopenharmony_ci key_put(uid_keyring); 1758c2ecf20Sopenharmony_cierror: 1768c2ecf20Sopenharmony_ci up_write(&user_ns->keyring_sem); 1778c2ecf20Sopenharmony_ci kleave(" = %d", ret); 1788c2ecf20Sopenharmony_ci return ret; 1798c2ecf20Sopenharmony_ci} 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci/* 1828c2ecf20Sopenharmony_ci * Get the user session keyring if it exists, but don't create it if it 1838c2ecf20Sopenharmony_ci * doesn't. 1848c2ecf20Sopenharmony_ci */ 1858c2ecf20Sopenharmony_cistruct key *get_user_session_keyring_rcu(const struct cred *cred) 1868c2ecf20Sopenharmony_ci{ 1878c2ecf20Sopenharmony_ci struct key *reg_keyring = READ_ONCE(cred->user_ns->user_keyring_register); 1888c2ecf20Sopenharmony_ci key_ref_t session_keyring_r; 1898c2ecf20Sopenharmony_ci char buf[20]; 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci struct keyring_search_context ctx = { 1928c2ecf20Sopenharmony_ci .index_key.type = &key_type_keyring, 1938c2ecf20Sopenharmony_ci .index_key.description = buf, 1948c2ecf20Sopenharmony_ci .cred = cred, 1958c2ecf20Sopenharmony_ci .match_data.cmp = key_default_cmp, 1968c2ecf20Sopenharmony_ci .match_data.raw_data = buf, 1978c2ecf20Sopenharmony_ci .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT, 1988c2ecf20Sopenharmony_ci .flags = KEYRING_SEARCH_DO_STATE_CHECK, 1998c2ecf20Sopenharmony_ci }; 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci if (!reg_keyring) 2028c2ecf20Sopenharmony_ci return NULL; 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci ctx.index_key.desc_len = snprintf(buf, sizeof(buf), "_uid_ses.%u", 2058c2ecf20Sopenharmony_ci from_kuid(cred->user_ns, 2068c2ecf20Sopenharmony_ci cred->user->uid)); 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci session_keyring_r = keyring_search_rcu(make_key_ref(reg_keyring, true), 2098c2ecf20Sopenharmony_ci &ctx); 2108c2ecf20Sopenharmony_ci if (IS_ERR(session_keyring_r)) 2118c2ecf20Sopenharmony_ci return NULL; 2128c2ecf20Sopenharmony_ci return key_ref_to_ptr(session_keyring_r); 2138c2ecf20Sopenharmony_ci} 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci/* 2168c2ecf20Sopenharmony_ci * Install a thread keyring to the given credentials struct if it didn't have 2178c2ecf20Sopenharmony_ci * one already. This is allowed to overrun the quota. 2188c2ecf20Sopenharmony_ci * 2198c2ecf20Sopenharmony_ci * Return: 0 if a thread keyring is now present; -errno on failure. 2208c2ecf20Sopenharmony_ci */ 2218c2ecf20Sopenharmony_ciint install_thread_keyring_to_cred(struct cred *new) 2228c2ecf20Sopenharmony_ci{ 2238c2ecf20Sopenharmony_ci struct key *keyring; 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci if (new->thread_keyring) 2268c2ecf20Sopenharmony_ci return 0; 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci keyring = keyring_alloc("_tid", new->uid, new->gid, new, 2298c2ecf20Sopenharmony_ci KEY_POS_ALL | KEY_USR_VIEW, 2308c2ecf20Sopenharmony_ci KEY_ALLOC_QUOTA_OVERRUN, 2318c2ecf20Sopenharmony_ci NULL, NULL); 2328c2ecf20Sopenharmony_ci if (IS_ERR(keyring)) 2338c2ecf20Sopenharmony_ci return PTR_ERR(keyring); 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci new->thread_keyring = keyring; 2368c2ecf20Sopenharmony_ci return 0; 2378c2ecf20Sopenharmony_ci} 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci/* 2408c2ecf20Sopenharmony_ci * Install a thread keyring to the current task if it didn't have one already. 2418c2ecf20Sopenharmony_ci * 2428c2ecf20Sopenharmony_ci * Return: 0 if a thread keyring is now present; -errno on failure. 2438c2ecf20Sopenharmony_ci */ 2448c2ecf20Sopenharmony_cistatic int install_thread_keyring(void) 2458c2ecf20Sopenharmony_ci{ 2468c2ecf20Sopenharmony_ci struct cred *new; 2478c2ecf20Sopenharmony_ci int ret; 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci new = prepare_creds(); 2508c2ecf20Sopenharmony_ci if (!new) 2518c2ecf20Sopenharmony_ci return -ENOMEM; 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci ret = install_thread_keyring_to_cred(new); 2548c2ecf20Sopenharmony_ci if (ret < 0) { 2558c2ecf20Sopenharmony_ci abort_creds(new); 2568c2ecf20Sopenharmony_ci return ret; 2578c2ecf20Sopenharmony_ci } 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci return commit_creds(new); 2608c2ecf20Sopenharmony_ci} 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci/* 2638c2ecf20Sopenharmony_ci * Install a process keyring to the given credentials struct if it didn't have 2648c2ecf20Sopenharmony_ci * one already. This is allowed to overrun the quota. 2658c2ecf20Sopenharmony_ci * 2668c2ecf20Sopenharmony_ci * Return: 0 if a process keyring is now present; -errno on failure. 2678c2ecf20Sopenharmony_ci */ 2688c2ecf20Sopenharmony_ciint install_process_keyring_to_cred(struct cred *new) 2698c2ecf20Sopenharmony_ci{ 2708c2ecf20Sopenharmony_ci struct key *keyring; 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci if (new->process_keyring) 2738c2ecf20Sopenharmony_ci return 0; 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci keyring = keyring_alloc("_pid", new->uid, new->gid, new, 2768c2ecf20Sopenharmony_ci KEY_POS_ALL | KEY_USR_VIEW, 2778c2ecf20Sopenharmony_ci KEY_ALLOC_QUOTA_OVERRUN, 2788c2ecf20Sopenharmony_ci NULL, NULL); 2798c2ecf20Sopenharmony_ci if (IS_ERR(keyring)) 2808c2ecf20Sopenharmony_ci return PTR_ERR(keyring); 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci new->process_keyring = keyring; 2838c2ecf20Sopenharmony_ci return 0; 2848c2ecf20Sopenharmony_ci} 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ci/* 2878c2ecf20Sopenharmony_ci * Install a process keyring to the current task if it didn't have one already. 2888c2ecf20Sopenharmony_ci * 2898c2ecf20Sopenharmony_ci * Return: 0 if a process keyring is now present; -errno on failure. 2908c2ecf20Sopenharmony_ci */ 2918c2ecf20Sopenharmony_cistatic int install_process_keyring(void) 2928c2ecf20Sopenharmony_ci{ 2938c2ecf20Sopenharmony_ci struct cred *new; 2948c2ecf20Sopenharmony_ci int ret; 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_ci new = prepare_creds(); 2978c2ecf20Sopenharmony_ci if (!new) 2988c2ecf20Sopenharmony_ci return -ENOMEM; 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_ci ret = install_process_keyring_to_cred(new); 3018c2ecf20Sopenharmony_ci if (ret < 0) { 3028c2ecf20Sopenharmony_ci abort_creds(new); 3038c2ecf20Sopenharmony_ci return ret; 3048c2ecf20Sopenharmony_ci } 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_ci return commit_creds(new); 3078c2ecf20Sopenharmony_ci} 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ci/* 3108c2ecf20Sopenharmony_ci * Install the given keyring as the session keyring of the given credentials 3118c2ecf20Sopenharmony_ci * struct, replacing the existing one if any. If the given keyring is NULL, 3128c2ecf20Sopenharmony_ci * then install a new anonymous session keyring. 3138c2ecf20Sopenharmony_ci * @cred can not be in use by any task yet. 3148c2ecf20Sopenharmony_ci * 3158c2ecf20Sopenharmony_ci * Return: 0 on success; -errno on failure. 3168c2ecf20Sopenharmony_ci */ 3178c2ecf20Sopenharmony_ciint install_session_keyring_to_cred(struct cred *cred, struct key *keyring) 3188c2ecf20Sopenharmony_ci{ 3198c2ecf20Sopenharmony_ci unsigned long flags; 3208c2ecf20Sopenharmony_ci struct key *old; 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_ci might_sleep(); 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci /* create an empty session keyring */ 3258c2ecf20Sopenharmony_ci if (!keyring) { 3268c2ecf20Sopenharmony_ci flags = KEY_ALLOC_QUOTA_OVERRUN; 3278c2ecf20Sopenharmony_ci if (cred->session_keyring) 3288c2ecf20Sopenharmony_ci flags = KEY_ALLOC_IN_QUOTA; 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ci keyring = keyring_alloc("_ses", cred->uid, cred->gid, cred, 3318c2ecf20Sopenharmony_ci KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ, 3328c2ecf20Sopenharmony_ci flags, NULL, NULL); 3338c2ecf20Sopenharmony_ci if (IS_ERR(keyring)) 3348c2ecf20Sopenharmony_ci return PTR_ERR(keyring); 3358c2ecf20Sopenharmony_ci } else { 3368c2ecf20Sopenharmony_ci __key_get(keyring); 3378c2ecf20Sopenharmony_ci } 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ci /* install the keyring */ 3408c2ecf20Sopenharmony_ci old = cred->session_keyring; 3418c2ecf20Sopenharmony_ci cred->session_keyring = keyring; 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci if (old) 3448c2ecf20Sopenharmony_ci key_put(old); 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_ci return 0; 3478c2ecf20Sopenharmony_ci} 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ci/* 3508c2ecf20Sopenharmony_ci * Install the given keyring as the session keyring of the current task, 3518c2ecf20Sopenharmony_ci * replacing the existing one if any. If the given keyring is NULL, then 3528c2ecf20Sopenharmony_ci * install a new anonymous session keyring. 3538c2ecf20Sopenharmony_ci * 3548c2ecf20Sopenharmony_ci * Return: 0 on success; -errno on failure. 3558c2ecf20Sopenharmony_ci */ 3568c2ecf20Sopenharmony_cistatic int install_session_keyring(struct key *keyring) 3578c2ecf20Sopenharmony_ci{ 3588c2ecf20Sopenharmony_ci struct cred *new; 3598c2ecf20Sopenharmony_ci int ret; 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_ci new = prepare_creds(); 3628c2ecf20Sopenharmony_ci if (!new) 3638c2ecf20Sopenharmony_ci return -ENOMEM; 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_ci ret = install_session_keyring_to_cred(new, keyring); 3668c2ecf20Sopenharmony_ci if (ret < 0) { 3678c2ecf20Sopenharmony_ci abort_creds(new); 3688c2ecf20Sopenharmony_ci return ret; 3698c2ecf20Sopenharmony_ci } 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ci return commit_creds(new); 3728c2ecf20Sopenharmony_ci} 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_ci/* 3758c2ecf20Sopenharmony_ci * Handle the fsuid changing. 3768c2ecf20Sopenharmony_ci */ 3778c2ecf20Sopenharmony_civoid key_fsuid_changed(struct cred *new_cred) 3788c2ecf20Sopenharmony_ci{ 3798c2ecf20Sopenharmony_ci /* update the ownership of the thread keyring */ 3808c2ecf20Sopenharmony_ci if (new_cred->thread_keyring) { 3818c2ecf20Sopenharmony_ci down_write(&new_cred->thread_keyring->sem); 3828c2ecf20Sopenharmony_ci new_cred->thread_keyring->uid = new_cred->fsuid; 3838c2ecf20Sopenharmony_ci up_write(&new_cred->thread_keyring->sem); 3848c2ecf20Sopenharmony_ci } 3858c2ecf20Sopenharmony_ci} 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci/* 3888c2ecf20Sopenharmony_ci * Handle the fsgid changing. 3898c2ecf20Sopenharmony_ci */ 3908c2ecf20Sopenharmony_civoid key_fsgid_changed(struct cred *new_cred) 3918c2ecf20Sopenharmony_ci{ 3928c2ecf20Sopenharmony_ci /* update the ownership of the thread keyring */ 3938c2ecf20Sopenharmony_ci if (new_cred->thread_keyring) { 3948c2ecf20Sopenharmony_ci down_write(&new_cred->thread_keyring->sem); 3958c2ecf20Sopenharmony_ci new_cred->thread_keyring->gid = new_cred->fsgid; 3968c2ecf20Sopenharmony_ci up_write(&new_cred->thread_keyring->sem); 3978c2ecf20Sopenharmony_ci } 3988c2ecf20Sopenharmony_ci} 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_ci/* 4018c2ecf20Sopenharmony_ci * Search the process keyrings attached to the supplied cred for the first 4028c2ecf20Sopenharmony_ci * matching key under RCU conditions (the caller must be holding the RCU read 4038c2ecf20Sopenharmony_ci * lock). 4048c2ecf20Sopenharmony_ci * 4058c2ecf20Sopenharmony_ci * The search criteria are the type and the match function. The description is 4068c2ecf20Sopenharmony_ci * given to the match function as a parameter, but doesn't otherwise influence 4078c2ecf20Sopenharmony_ci * the search. Typically the match function will compare the description 4088c2ecf20Sopenharmony_ci * parameter to the key's description. 4098c2ecf20Sopenharmony_ci * 4108c2ecf20Sopenharmony_ci * This can only search keyrings that grant Search permission to the supplied 4118c2ecf20Sopenharmony_ci * credentials. Keyrings linked to searched keyrings will also be searched if 4128c2ecf20Sopenharmony_ci * they grant Search permission too. Keys can only be found if they grant 4138c2ecf20Sopenharmony_ci * Search permission to the credentials. 4148c2ecf20Sopenharmony_ci * 4158c2ecf20Sopenharmony_ci * Returns a pointer to the key with the key usage count incremented if 4168c2ecf20Sopenharmony_ci * successful, -EAGAIN if we didn't find any matching key or -ENOKEY if we only 4178c2ecf20Sopenharmony_ci * matched negative keys. 4188c2ecf20Sopenharmony_ci * 4198c2ecf20Sopenharmony_ci * In the case of a successful return, the possession attribute is set on the 4208c2ecf20Sopenharmony_ci * returned key reference. 4218c2ecf20Sopenharmony_ci */ 4228c2ecf20Sopenharmony_cikey_ref_t search_cred_keyrings_rcu(struct keyring_search_context *ctx) 4238c2ecf20Sopenharmony_ci{ 4248c2ecf20Sopenharmony_ci struct key *user_session; 4258c2ecf20Sopenharmony_ci key_ref_t key_ref, ret, err; 4268c2ecf20Sopenharmony_ci const struct cred *cred = ctx->cred; 4278c2ecf20Sopenharmony_ci 4288c2ecf20Sopenharmony_ci /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were 4298c2ecf20Sopenharmony_ci * searchable, but we failed to find a key or we found a negative key; 4308c2ecf20Sopenharmony_ci * otherwise we want to return a sample error (probably -EACCES) if 4318c2ecf20Sopenharmony_ci * none of the keyrings were searchable 4328c2ecf20Sopenharmony_ci * 4338c2ecf20Sopenharmony_ci * in terms of priority: success > -ENOKEY > -EAGAIN > other error 4348c2ecf20Sopenharmony_ci */ 4358c2ecf20Sopenharmony_ci key_ref = NULL; 4368c2ecf20Sopenharmony_ci ret = NULL; 4378c2ecf20Sopenharmony_ci err = ERR_PTR(-EAGAIN); 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci /* search the thread keyring first */ 4408c2ecf20Sopenharmony_ci if (cred->thread_keyring) { 4418c2ecf20Sopenharmony_ci key_ref = keyring_search_rcu( 4428c2ecf20Sopenharmony_ci make_key_ref(cred->thread_keyring, 1), ctx); 4438c2ecf20Sopenharmony_ci if (!IS_ERR(key_ref)) 4448c2ecf20Sopenharmony_ci goto found; 4458c2ecf20Sopenharmony_ci 4468c2ecf20Sopenharmony_ci switch (PTR_ERR(key_ref)) { 4478c2ecf20Sopenharmony_ci case -EAGAIN: /* no key */ 4488c2ecf20Sopenharmony_ci case -ENOKEY: /* negative key */ 4498c2ecf20Sopenharmony_ci ret = key_ref; 4508c2ecf20Sopenharmony_ci break; 4518c2ecf20Sopenharmony_ci default: 4528c2ecf20Sopenharmony_ci err = key_ref; 4538c2ecf20Sopenharmony_ci break; 4548c2ecf20Sopenharmony_ci } 4558c2ecf20Sopenharmony_ci } 4568c2ecf20Sopenharmony_ci 4578c2ecf20Sopenharmony_ci /* search the process keyring second */ 4588c2ecf20Sopenharmony_ci if (cred->process_keyring) { 4598c2ecf20Sopenharmony_ci key_ref = keyring_search_rcu( 4608c2ecf20Sopenharmony_ci make_key_ref(cred->process_keyring, 1), ctx); 4618c2ecf20Sopenharmony_ci if (!IS_ERR(key_ref)) 4628c2ecf20Sopenharmony_ci goto found; 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_ci switch (PTR_ERR(key_ref)) { 4658c2ecf20Sopenharmony_ci case -EAGAIN: /* no key */ 4668c2ecf20Sopenharmony_ci if (ret) 4678c2ecf20Sopenharmony_ci break; 4688c2ecf20Sopenharmony_ci fallthrough; 4698c2ecf20Sopenharmony_ci case -ENOKEY: /* negative key */ 4708c2ecf20Sopenharmony_ci ret = key_ref; 4718c2ecf20Sopenharmony_ci break; 4728c2ecf20Sopenharmony_ci default: 4738c2ecf20Sopenharmony_ci err = key_ref; 4748c2ecf20Sopenharmony_ci break; 4758c2ecf20Sopenharmony_ci } 4768c2ecf20Sopenharmony_ci } 4778c2ecf20Sopenharmony_ci 4788c2ecf20Sopenharmony_ci /* search the session keyring */ 4798c2ecf20Sopenharmony_ci if (cred->session_keyring) { 4808c2ecf20Sopenharmony_ci key_ref = keyring_search_rcu( 4818c2ecf20Sopenharmony_ci make_key_ref(cred->session_keyring, 1), ctx); 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci if (!IS_ERR(key_ref)) 4848c2ecf20Sopenharmony_ci goto found; 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_ci switch (PTR_ERR(key_ref)) { 4878c2ecf20Sopenharmony_ci case -EAGAIN: /* no key */ 4888c2ecf20Sopenharmony_ci if (ret) 4898c2ecf20Sopenharmony_ci break; 4908c2ecf20Sopenharmony_ci fallthrough; 4918c2ecf20Sopenharmony_ci case -ENOKEY: /* negative key */ 4928c2ecf20Sopenharmony_ci ret = key_ref; 4938c2ecf20Sopenharmony_ci break; 4948c2ecf20Sopenharmony_ci default: 4958c2ecf20Sopenharmony_ci err = key_ref; 4968c2ecf20Sopenharmony_ci break; 4978c2ecf20Sopenharmony_ci } 4988c2ecf20Sopenharmony_ci } 4998c2ecf20Sopenharmony_ci /* or search the user-session keyring */ 5008c2ecf20Sopenharmony_ci else if ((user_session = get_user_session_keyring_rcu(cred))) { 5018c2ecf20Sopenharmony_ci key_ref = keyring_search_rcu(make_key_ref(user_session, 1), 5028c2ecf20Sopenharmony_ci ctx); 5038c2ecf20Sopenharmony_ci key_put(user_session); 5048c2ecf20Sopenharmony_ci 5058c2ecf20Sopenharmony_ci if (!IS_ERR(key_ref)) 5068c2ecf20Sopenharmony_ci goto found; 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_ci switch (PTR_ERR(key_ref)) { 5098c2ecf20Sopenharmony_ci case -EAGAIN: /* no key */ 5108c2ecf20Sopenharmony_ci if (ret) 5118c2ecf20Sopenharmony_ci break; 5128c2ecf20Sopenharmony_ci fallthrough; 5138c2ecf20Sopenharmony_ci case -ENOKEY: /* negative key */ 5148c2ecf20Sopenharmony_ci ret = key_ref; 5158c2ecf20Sopenharmony_ci break; 5168c2ecf20Sopenharmony_ci default: 5178c2ecf20Sopenharmony_ci err = key_ref; 5188c2ecf20Sopenharmony_ci break; 5198c2ecf20Sopenharmony_ci } 5208c2ecf20Sopenharmony_ci } 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci /* no key - decide on the error we're going to go for */ 5238c2ecf20Sopenharmony_ci key_ref = ret ? ret : err; 5248c2ecf20Sopenharmony_ci 5258c2ecf20Sopenharmony_cifound: 5268c2ecf20Sopenharmony_ci return key_ref; 5278c2ecf20Sopenharmony_ci} 5288c2ecf20Sopenharmony_ci 5298c2ecf20Sopenharmony_ci/* 5308c2ecf20Sopenharmony_ci * Search the process keyrings attached to the supplied cred for the first 5318c2ecf20Sopenharmony_ci * matching key in the manner of search_my_process_keyrings(), but also search 5328c2ecf20Sopenharmony_ci * the keys attached to the assumed authorisation key using its credentials if 5338c2ecf20Sopenharmony_ci * one is available. 5348c2ecf20Sopenharmony_ci * 5358c2ecf20Sopenharmony_ci * The caller must be holding the RCU read lock. 5368c2ecf20Sopenharmony_ci * 5378c2ecf20Sopenharmony_ci * Return same as search_cred_keyrings_rcu(). 5388c2ecf20Sopenharmony_ci */ 5398c2ecf20Sopenharmony_cikey_ref_t search_process_keyrings_rcu(struct keyring_search_context *ctx) 5408c2ecf20Sopenharmony_ci{ 5418c2ecf20Sopenharmony_ci struct request_key_auth *rka; 5428c2ecf20Sopenharmony_ci key_ref_t key_ref, ret = ERR_PTR(-EACCES), err; 5438c2ecf20Sopenharmony_ci 5448c2ecf20Sopenharmony_ci key_ref = search_cred_keyrings_rcu(ctx); 5458c2ecf20Sopenharmony_ci if (!IS_ERR(key_ref)) 5468c2ecf20Sopenharmony_ci goto found; 5478c2ecf20Sopenharmony_ci err = key_ref; 5488c2ecf20Sopenharmony_ci 5498c2ecf20Sopenharmony_ci /* if this process has an instantiation authorisation key, then we also 5508c2ecf20Sopenharmony_ci * search the keyrings of the process mentioned there 5518c2ecf20Sopenharmony_ci * - we don't permit access to request_key auth keys via this method 5528c2ecf20Sopenharmony_ci */ 5538c2ecf20Sopenharmony_ci if (ctx->cred->request_key_auth && 5548c2ecf20Sopenharmony_ci ctx->cred == current_cred() && 5558c2ecf20Sopenharmony_ci ctx->index_key.type != &key_type_request_key_auth 5568c2ecf20Sopenharmony_ci ) { 5578c2ecf20Sopenharmony_ci const struct cred *cred = ctx->cred; 5588c2ecf20Sopenharmony_ci 5598c2ecf20Sopenharmony_ci if (key_validate(cred->request_key_auth) == 0) { 5608c2ecf20Sopenharmony_ci rka = ctx->cred->request_key_auth->payload.data[0]; 5618c2ecf20Sopenharmony_ci 5628c2ecf20Sopenharmony_ci //// was search_process_keyrings() [ie. recursive] 5638c2ecf20Sopenharmony_ci ctx->cred = rka->cred; 5648c2ecf20Sopenharmony_ci key_ref = search_cred_keyrings_rcu(ctx); 5658c2ecf20Sopenharmony_ci ctx->cred = cred; 5668c2ecf20Sopenharmony_ci 5678c2ecf20Sopenharmony_ci if (!IS_ERR(key_ref)) 5688c2ecf20Sopenharmony_ci goto found; 5698c2ecf20Sopenharmony_ci ret = key_ref; 5708c2ecf20Sopenharmony_ci } 5718c2ecf20Sopenharmony_ci } 5728c2ecf20Sopenharmony_ci 5738c2ecf20Sopenharmony_ci /* no key - decide on the error we're going to go for */ 5748c2ecf20Sopenharmony_ci if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY)) 5758c2ecf20Sopenharmony_ci key_ref = ERR_PTR(-ENOKEY); 5768c2ecf20Sopenharmony_ci else if (err == ERR_PTR(-EACCES)) 5778c2ecf20Sopenharmony_ci key_ref = ret; 5788c2ecf20Sopenharmony_ci else 5798c2ecf20Sopenharmony_ci key_ref = err; 5808c2ecf20Sopenharmony_ci 5818c2ecf20Sopenharmony_cifound: 5828c2ecf20Sopenharmony_ci return key_ref; 5838c2ecf20Sopenharmony_ci} 5848c2ecf20Sopenharmony_ci/* 5858c2ecf20Sopenharmony_ci * See if the key we're looking at is the target key. 5868c2ecf20Sopenharmony_ci */ 5878c2ecf20Sopenharmony_cibool lookup_user_key_possessed(const struct key *key, 5888c2ecf20Sopenharmony_ci const struct key_match_data *match_data) 5898c2ecf20Sopenharmony_ci{ 5908c2ecf20Sopenharmony_ci return key == match_data->raw_data; 5918c2ecf20Sopenharmony_ci} 5928c2ecf20Sopenharmony_ci 5938c2ecf20Sopenharmony_ci/* 5948c2ecf20Sopenharmony_ci * Look up a key ID given us by userspace with a given permissions mask to get 5958c2ecf20Sopenharmony_ci * the key it refers to. 5968c2ecf20Sopenharmony_ci * 5978c2ecf20Sopenharmony_ci * Flags can be passed to request that special keyrings be created if referred 5988c2ecf20Sopenharmony_ci * to directly, to permit partially constructed keys to be found and to skip 5998c2ecf20Sopenharmony_ci * validity and permission checks on the found key. 6008c2ecf20Sopenharmony_ci * 6018c2ecf20Sopenharmony_ci * Returns a pointer to the key with an incremented usage count if successful; 6028c2ecf20Sopenharmony_ci * -EINVAL if the key ID is invalid; -ENOKEY if the key ID does not correspond 6038c2ecf20Sopenharmony_ci * to a key or the best found key was a negative key; -EKEYREVOKED or 6048c2ecf20Sopenharmony_ci * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the 6058c2ecf20Sopenharmony_ci * found key doesn't grant the requested permit or the LSM denied access to it; 6068c2ecf20Sopenharmony_ci * or -ENOMEM if a special keyring couldn't be created. 6078c2ecf20Sopenharmony_ci * 6088c2ecf20Sopenharmony_ci * In the case of a successful return, the possession attribute is set on the 6098c2ecf20Sopenharmony_ci * returned key reference. 6108c2ecf20Sopenharmony_ci */ 6118c2ecf20Sopenharmony_cikey_ref_t lookup_user_key(key_serial_t id, unsigned long lflags, 6128c2ecf20Sopenharmony_ci enum key_need_perm need_perm) 6138c2ecf20Sopenharmony_ci{ 6148c2ecf20Sopenharmony_ci struct keyring_search_context ctx = { 6158c2ecf20Sopenharmony_ci .match_data.cmp = lookup_user_key_possessed, 6168c2ecf20Sopenharmony_ci .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT, 6178c2ecf20Sopenharmony_ci .flags = (KEYRING_SEARCH_NO_STATE_CHECK | 6188c2ecf20Sopenharmony_ci KEYRING_SEARCH_RECURSE), 6198c2ecf20Sopenharmony_ci }; 6208c2ecf20Sopenharmony_ci struct request_key_auth *rka; 6218c2ecf20Sopenharmony_ci struct key *key, *user_session; 6228c2ecf20Sopenharmony_ci key_ref_t key_ref, skey_ref; 6238c2ecf20Sopenharmony_ci int ret; 6248c2ecf20Sopenharmony_ci 6258c2ecf20Sopenharmony_citry_again: 6268c2ecf20Sopenharmony_ci ctx.cred = get_current_cred(); 6278c2ecf20Sopenharmony_ci key_ref = ERR_PTR(-ENOKEY); 6288c2ecf20Sopenharmony_ci 6298c2ecf20Sopenharmony_ci switch (id) { 6308c2ecf20Sopenharmony_ci case KEY_SPEC_THREAD_KEYRING: 6318c2ecf20Sopenharmony_ci if (!ctx.cred->thread_keyring) { 6328c2ecf20Sopenharmony_ci if (!(lflags & KEY_LOOKUP_CREATE)) 6338c2ecf20Sopenharmony_ci goto error; 6348c2ecf20Sopenharmony_ci 6358c2ecf20Sopenharmony_ci ret = install_thread_keyring(); 6368c2ecf20Sopenharmony_ci if (ret < 0) { 6378c2ecf20Sopenharmony_ci key_ref = ERR_PTR(ret); 6388c2ecf20Sopenharmony_ci goto error; 6398c2ecf20Sopenharmony_ci } 6408c2ecf20Sopenharmony_ci goto reget_creds; 6418c2ecf20Sopenharmony_ci } 6428c2ecf20Sopenharmony_ci 6438c2ecf20Sopenharmony_ci key = ctx.cred->thread_keyring; 6448c2ecf20Sopenharmony_ci __key_get(key); 6458c2ecf20Sopenharmony_ci key_ref = make_key_ref(key, 1); 6468c2ecf20Sopenharmony_ci break; 6478c2ecf20Sopenharmony_ci 6488c2ecf20Sopenharmony_ci case KEY_SPEC_PROCESS_KEYRING: 6498c2ecf20Sopenharmony_ci if (!ctx.cred->process_keyring) { 6508c2ecf20Sopenharmony_ci if (!(lflags & KEY_LOOKUP_CREATE)) 6518c2ecf20Sopenharmony_ci goto error; 6528c2ecf20Sopenharmony_ci 6538c2ecf20Sopenharmony_ci ret = install_process_keyring(); 6548c2ecf20Sopenharmony_ci if (ret < 0) { 6558c2ecf20Sopenharmony_ci key_ref = ERR_PTR(ret); 6568c2ecf20Sopenharmony_ci goto error; 6578c2ecf20Sopenharmony_ci } 6588c2ecf20Sopenharmony_ci goto reget_creds; 6598c2ecf20Sopenharmony_ci } 6608c2ecf20Sopenharmony_ci 6618c2ecf20Sopenharmony_ci key = ctx.cred->process_keyring; 6628c2ecf20Sopenharmony_ci __key_get(key); 6638c2ecf20Sopenharmony_ci key_ref = make_key_ref(key, 1); 6648c2ecf20Sopenharmony_ci break; 6658c2ecf20Sopenharmony_ci 6668c2ecf20Sopenharmony_ci case KEY_SPEC_SESSION_KEYRING: 6678c2ecf20Sopenharmony_ci if (!ctx.cred->session_keyring) { 6688c2ecf20Sopenharmony_ci /* always install a session keyring upon access if one 6698c2ecf20Sopenharmony_ci * doesn't exist yet */ 6708c2ecf20Sopenharmony_ci ret = look_up_user_keyrings(NULL, &user_session); 6718c2ecf20Sopenharmony_ci if (ret < 0) 6728c2ecf20Sopenharmony_ci goto error; 6738c2ecf20Sopenharmony_ci if (lflags & KEY_LOOKUP_CREATE) 6748c2ecf20Sopenharmony_ci ret = join_session_keyring(NULL); 6758c2ecf20Sopenharmony_ci else 6768c2ecf20Sopenharmony_ci ret = install_session_keyring(user_session); 6778c2ecf20Sopenharmony_ci 6788c2ecf20Sopenharmony_ci key_put(user_session); 6798c2ecf20Sopenharmony_ci if (ret < 0) 6808c2ecf20Sopenharmony_ci goto error; 6818c2ecf20Sopenharmony_ci goto reget_creds; 6828c2ecf20Sopenharmony_ci } else if (test_bit(KEY_FLAG_UID_KEYRING, 6838c2ecf20Sopenharmony_ci &ctx.cred->session_keyring->flags) && 6848c2ecf20Sopenharmony_ci lflags & KEY_LOOKUP_CREATE) { 6858c2ecf20Sopenharmony_ci ret = join_session_keyring(NULL); 6868c2ecf20Sopenharmony_ci if (ret < 0) 6878c2ecf20Sopenharmony_ci goto error; 6888c2ecf20Sopenharmony_ci goto reget_creds; 6898c2ecf20Sopenharmony_ci } 6908c2ecf20Sopenharmony_ci 6918c2ecf20Sopenharmony_ci key = ctx.cred->session_keyring; 6928c2ecf20Sopenharmony_ci __key_get(key); 6938c2ecf20Sopenharmony_ci key_ref = make_key_ref(key, 1); 6948c2ecf20Sopenharmony_ci break; 6958c2ecf20Sopenharmony_ci 6968c2ecf20Sopenharmony_ci case KEY_SPEC_USER_KEYRING: 6978c2ecf20Sopenharmony_ci ret = look_up_user_keyrings(&key, NULL); 6988c2ecf20Sopenharmony_ci if (ret < 0) 6998c2ecf20Sopenharmony_ci goto error; 7008c2ecf20Sopenharmony_ci key_ref = make_key_ref(key, 1); 7018c2ecf20Sopenharmony_ci break; 7028c2ecf20Sopenharmony_ci 7038c2ecf20Sopenharmony_ci case KEY_SPEC_USER_SESSION_KEYRING: 7048c2ecf20Sopenharmony_ci ret = look_up_user_keyrings(NULL, &key); 7058c2ecf20Sopenharmony_ci if (ret < 0) 7068c2ecf20Sopenharmony_ci goto error; 7078c2ecf20Sopenharmony_ci key_ref = make_key_ref(key, 1); 7088c2ecf20Sopenharmony_ci break; 7098c2ecf20Sopenharmony_ci 7108c2ecf20Sopenharmony_ci case KEY_SPEC_GROUP_KEYRING: 7118c2ecf20Sopenharmony_ci /* group keyrings are not yet supported */ 7128c2ecf20Sopenharmony_ci key_ref = ERR_PTR(-EINVAL); 7138c2ecf20Sopenharmony_ci goto error; 7148c2ecf20Sopenharmony_ci 7158c2ecf20Sopenharmony_ci case KEY_SPEC_REQKEY_AUTH_KEY: 7168c2ecf20Sopenharmony_ci key = ctx.cred->request_key_auth; 7178c2ecf20Sopenharmony_ci if (!key) 7188c2ecf20Sopenharmony_ci goto error; 7198c2ecf20Sopenharmony_ci 7208c2ecf20Sopenharmony_ci __key_get(key); 7218c2ecf20Sopenharmony_ci key_ref = make_key_ref(key, 1); 7228c2ecf20Sopenharmony_ci break; 7238c2ecf20Sopenharmony_ci 7248c2ecf20Sopenharmony_ci case KEY_SPEC_REQUESTOR_KEYRING: 7258c2ecf20Sopenharmony_ci if (!ctx.cred->request_key_auth) 7268c2ecf20Sopenharmony_ci goto error; 7278c2ecf20Sopenharmony_ci 7288c2ecf20Sopenharmony_ci down_read(&ctx.cred->request_key_auth->sem); 7298c2ecf20Sopenharmony_ci if (test_bit(KEY_FLAG_REVOKED, 7308c2ecf20Sopenharmony_ci &ctx.cred->request_key_auth->flags)) { 7318c2ecf20Sopenharmony_ci key_ref = ERR_PTR(-EKEYREVOKED); 7328c2ecf20Sopenharmony_ci key = NULL; 7338c2ecf20Sopenharmony_ci } else { 7348c2ecf20Sopenharmony_ci rka = ctx.cred->request_key_auth->payload.data[0]; 7358c2ecf20Sopenharmony_ci key = rka->dest_keyring; 7368c2ecf20Sopenharmony_ci __key_get(key); 7378c2ecf20Sopenharmony_ci } 7388c2ecf20Sopenharmony_ci up_read(&ctx.cred->request_key_auth->sem); 7398c2ecf20Sopenharmony_ci if (!key) 7408c2ecf20Sopenharmony_ci goto error; 7418c2ecf20Sopenharmony_ci key_ref = make_key_ref(key, 1); 7428c2ecf20Sopenharmony_ci break; 7438c2ecf20Sopenharmony_ci 7448c2ecf20Sopenharmony_ci default: 7458c2ecf20Sopenharmony_ci key_ref = ERR_PTR(-EINVAL); 7468c2ecf20Sopenharmony_ci if (id < 1) 7478c2ecf20Sopenharmony_ci goto error; 7488c2ecf20Sopenharmony_ci 7498c2ecf20Sopenharmony_ci key = key_lookup(id); 7508c2ecf20Sopenharmony_ci if (IS_ERR(key)) { 7518c2ecf20Sopenharmony_ci key_ref = ERR_CAST(key); 7528c2ecf20Sopenharmony_ci goto error; 7538c2ecf20Sopenharmony_ci } 7548c2ecf20Sopenharmony_ci 7558c2ecf20Sopenharmony_ci key_ref = make_key_ref(key, 0); 7568c2ecf20Sopenharmony_ci 7578c2ecf20Sopenharmony_ci /* check to see if we possess the key */ 7588c2ecf20Sopenharmony_ci ctx.index_key = key->index_key; 7598c2ecf20Sopenharmony_ci ctx.match_data.raw_data = key; 7608c2ecf20Sopenharmony_ci kdebug("check possessed"); 7618c2ecf20Sopenharmony_ci rcu_read_lock(); 7628c2ecf20Sopenharmony_ci skey_ref = search_process_keyrings_rcu(&ctx); 7638c2ecf20Sopenharmony_ci rcu_read_unlock(); 7648c2ecf20Sopenharmony_ci kdebug("possessed=%p", skey_ref); 7658c2ecf20Sopenharmony_ci 7668c2ecf20Sopenharmony_ci if (!IS_ERR(skey_ref)) { 7678c2ecf20Sopenharmony_ci key_put(key); 7688c2ecf20Sopenharmony_ci key_ref = skey_ref; 7698c2ecf20Sopenharmony_ci } 7708c2ecf20Sopenharmony_ci 7718c2ecf20Sopenharmony_ci break; 7728c2ecf20Sopenharmony_ci } 7738c2ecf20Sopenharmony_ci 7748c2ecf20Sopenharmony_ci /* unlink does not use the nominated key in any way, so can skip all 7758c2ecf20Sopenharmony_ci * the permission checks as it is only concerned with the keyring */ 7768c2ecf20Sopenharmony_ci if (need_perm != KEY_NEED_UNLINK) { 7778c2ecf20Sopenharmony_ci if (!(lflags & KEY_LOOKUP_PARTIAL)) { 7788c2ecf20Sopenharmony_ci ret = wait_for_key_construction(key, true); 7798c2ecf20Sopenharmony_ci switch (ret) { 7808c2ecf20Sopenharmony_ci case -ERESTARTSYS: 7818c2ecf20Sopenharmony_ci goto invalid_key; 7828c2ecf20Sopenharmony_ci default: 7838c2ecf20Sopenharmony_ci if (need_perm != KEY_AUTHTOKEN_OVERRIDE && 7848c2ecf20Sopenharmony_ci need_perm != KEY_DEFER_PERM_CHECK) 7858c2ecf20Sopenharmony_ci goto invalid_key; 7868c2ecf20Sopenharmony_ci case 0: 7878c2ecf20Sopenharmony_ci break; 7888c2ecf20Sopenharmony_ci } 7898c2ecf20Sopenharmony_ci } else if (need_perm != KEY_DEFER_PERM_CHECK) { 7908c2ecf20Sopenharmony_ci ret = key_validate(key); 7918c2ecf20Sopenharmony_ci if (ret < 0) 7928c2ecf20Sopenharmony_ci goto invalid_key; 7938c2ecf20Sopenharmony_ci } 7948c2ecf20Sopenharmony_ci 7958c2ecf20Sopenharmony_ci ret = -EIO; 7968c2ecf20Sopenharmony_ci if (!(lflags & KEY_LOOKUP_PARTIAL) && 7978c2ecf20Sopenharmony_ci key_read_state(key) == KEY_IS_UNINSTANTIATED) 7988c2ecf20Sopenharmony_ci goto invalid_key; 7998c2ecf20Sopenharmony_ci } 8008c2ecf20Sopenharmony_ci 8018c2ecf20Sopenharmony_ci /* check the permissions */ 8028c2ecf20Sopenharmony_ci ret = key_task_permission(key_ref, ctx.cred, need_perm); 8038c2ecf20Sopenharmony_ci if (ret < 0) 8048c2ecf20Sopenharmony_ci goto invalid_key; 8058c2ecf20Sopenharmony_ci 8068c2ecf20Sopenharmony_ci key->last_used_at = ktime_get_real_seconds(); 8078c2ecf20Sopenharmony_ci 8088c2ecf20Sopenharmony_cierror: 8098c2ecf20Sopenharmony_ci put_cred(ctx.cred); 8108c2ecf20Sopenharmony_ci return key_ref; 8118c2ecf20Sopenharmony_ci 8128c2ecf20Sopenharmony_ciinvalid_key: 8138c2ecf20Sopenharmony_ci key_ref_put(key_ref); 8148c2ecf20Sopenharmony_ci key_ref = ERR_PTR(ret); 8158c2ecf20Sopenharmony_ci goto error; 8168c2ecf20Sopenharmony_ci 8178c2ecf20Sopenharmony_ci /* if we attempted to install a keyring, then it may have caused new 8188c2ecf20Sopenharmony_ci * creds to be installed */ 8198c2ecf20Sopenharmony_cireget_creds: 8208c2ecf20Sopenharmony_ci put_cred(ctx.cred); 8218c2ecf20Sopenharmony_ci goto try_again; 8228c2ecf20Sopenharmony_ci} 8238c2ecf20Sopenharmony_ciEXPORT_SYMBOL(lookup_user_key); 8248c2ecf20Sopenharmony_ci 8258c2ecf20Sopenharmony_ci/* 8268c2ecf20Sopenharmony_ci * Join the named keyring as the session keyring if possible else attempt to 8278c2ecf20Sopenharmony_ci * create a new one of that name and join that. 8288c2ecf20Sopenharmony_ci * 8298c2ecf20Sopenharmony_ci * If the name is NULL, an empty anonymous keyring will be installed as the 8308c2ecf20Sopenharmony_ci * session keyring. 8318c2ecf20Sopenharmony_ci * 8328c2ecf20Sopenharmony_ci * Named session keyrings are joined with a semaphore held to prevent the 8338c2ecf20Sopenharmony_ci * keyrings from going away whilst the attempt is made to going them and also 8348c2ecf20Sopenharmony_ci * to prevent a race in creating compatible session keyrings. 8358c2ecf20Sopenharmony_ci */ 8368c2ecf20Sopenharmony_cilong join_session_keyring(const char *name) 8378c2ecf20Sopenharmony_ci{ 8388c2ecf20Sopenharmony_ci const struct cred *old; 8398c2ecf20Sopenharmony_ci struct cred *new; 8408c2ecf20Sopenharmony_ci struct key *keyring; 8418c2ecf20Sopenharmony_ci long ret, serial; 8428c2ecf20Sopenharmony_ci 8438c2ecf20Sopenharmony_ci new = prepare_creds(); 8448c2ecf20Sopenharmony_ci if (!new) 8458c2ecf20Sopenharmony_ci return -ENOMEM; 8468c2ecf20Sopenharmony_ci old = current_cred(); 8478c2ecf20Sopenharmony_ci 8488c2ecf20Sopenharmony_ci /* if no name is provided, install an anonymous keyring */ 8498c2ecf20Sopenharmony_ci if (!name) { 8508c2ecf20Sopenharmony_ci ret = install_session_keyring_to_cred(new, NULL); 8518c2ecf20Sopenharmony_ci if (ret < 0) 8528c2ecf20Sopenharmony_ci goto error; 8538c2ecf20Sopenharmony_ci 8548c2ecf20Sopenharmony_ci serial = new->session_keyring->serial; 8558c2ecf20Sopenharmony_ci ret = commit_creds(new); 8568c2ecf20Sopenharmony_ci if (ret == 0) 8578c2ecf20Sopenharmony_ci ret = serial; 8588c2ecf20Sopenharmony_ci goto okay; 8598c2ecf20Sopenharmony_ci } 8608c2ecf20Sopenharmony_ci 8618c2ecf20Sopenharmony_ci /* allow the user to join or create a named keyring */ 8628c2ecf20Sopenharmony_ci mutex_lock(&key_session_mutex); 8638c2ecf20Sopenharmony_ci 8648c2ecf20Sopenharmony_ci /* look for an existing keyring of this name */ 8658c2ecf20Sopenharmony_ci keyring = find_keyring_by_name(name, false); 8668c2ecf20Sopenharmony_ci if (PTR_ERR(keyring) == -ENOKEY) { 8678c2ecf20Sopenharmony_ci /* not found - try and create a new one */ 8688c2ecf20Sopenharmony_ci keyring = keyring_alloc( 8698c2ecf20Sopenharmony_ci name, old->uid, old->gid, old, 8708c2ecf20Sopenharmony_ci KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ | KEY_USR_LINK, 8718c2ecf20Sopenharmony_ci KEY_ALLOC_IN_QUOTA, NULL, NULL); 8728c2ecf20Sopenharmony_ci if (IS_ERR(keyring)) { 8738c2ecf20Sopenharmony_ci ret = PTR_ERR(keyring); 8748c2ecf20Sopenharmony_ci goto error2; 8758c2ecf20Sopenharmony_ci } 8768c2ecf20Sopenharmony_ci } else if (IS_ERR(keyring)) { 8778c2ecf20Sopenharmony_ci ret = PTR_ERR(keyring); 8788c2ecf20Sopenharmony_ci goto error2; 8798c2ecf20Sopenharmony_ci } else if (keyring == new->session_keyring) { 8808c2ecf20Sopenharmony_ci ret = 0; 8818c2ecf20Sopenharmony_ci goto error3; 8828c2ecf20Sopenharmony_ci } 8838c2ecf20Sopenharmony_ci 8848c2ecf20Sopenharmony_ci /* we've got a keyring - now to install it */ 8858c2ecf20Sopenharmony_ci ret = install_session_keyring_to_cred(new, keyring); 8868c2ecf20Sopenharmony_ci if (ret < 0) 8878c2ecf20Sopenharmony_ci goto error3; 8888c2ecf20Sopenharmony_ci 8898c2ecf20Sopenharmony_ci commit_creds(new); 8908c2ecf20Sopenharmony_ci mutex_unlock(&key_session_mutex); 8918c2ecf20Sopenharmony_ci 8928c2ecf20Sopenharmony_ci ret = keyring->serial; 8938c2ecf20Sopenharmony_ci key_put(keyring); 8948c2ecf20Sopenharmony_ciokay: 8958c2ecf20Sopenharmony_ci return ret; 8968c2ecf20Sopenharmony_ci 8978c2ecf20Sopenharmony_cierror3: 8988c2ecf20Sopenharmony_ci key_put(keyring); 8998c2ecf20Sopenharmony_cierror2: 9008c2ecf20Sopenharmony_ci mutex_unlock(&key_session_mutex); 9018c2ecf20Sopenharmony_cierror: 9028c2ecf20Sopenharmony_ci abort_creds(new); 9038c2ecf20Sopenharmony_ci return ret; 9048c2ecf20Sopenharmony_ci} 9058c2ecf20Sopenharmony_ci 9068c2ecf20Sopenharmony_ci/* 9078c2ecf20Sopenharmony_ci * Replace a process's session keyring on behalf of one of its children when 9088c2ecf20Sopenharmony_ci * the target process is about to resume userspace execution. 9098c2ecf20Sopenharmony_ci */ 9108c2ecf20Sopenharmony_civoid key_change_session_keyring(struct callback_head *twork) 9118c2ecf20Sopenharmony_ci{ 9128c2ecf20Sopenharmony_ci const struct cred *old = current_cred(); 9138c2ecf20Sopenharmony_ci struct cred *new = container_of(twork, struct cred, rcu); 9148c2ecf20Sopenharmony_ci 9158c2ecf20Sopenharmony_ci if (unlikely(current->flags & PF_EXITING)) { 9168c2ecf20Sopenharmony_ci put_cred(new); 9178c2ecf20Sopenharmony_ci return; 9188c2ecf20Sopenharmony_ci } 9198c2ecf20Sopenharmony_ci 9208c2ecf20Sopenharmony_ci new-> uid = old-> uid; 9218c2ecf20Sopenharmony_ci new-> euid = old-> euid; 9228c2ecf20Sopenharmony_ci new-> suid = old-> suid; 9238c2ecf20Sopenharmony_ci new->fsuid = old->fsuid; 9248c2ecf20Sopenharmony_ci new-> gid = old-> gid; 9258c2ecf20Sopenharmony_ci new-> egid = old-> egid; 9268c2ecf20Sopenharmony_ci new-> sgid = old-> sgid; 9278c2ecf20Sopenharmony_ci new->fsgid = old->fsgid; 9288c2ecf20Sopenharmony_ci new->user = get_uid(old->user); 9298c2ecf20Sopenharmony_ci new->user_ns = get_user_ns(old->user_ns); 9308c2ecf20Sopenharmony_ci new->group_info = get_group_info(old->group_info); 9318c2ecf20Sopenharmony_ci 9328c2ecf20Sopenharmony_ci new->securebits = old->securebits; 9338c2ecf20Sopenharmony_ci new->cap_inheritable = old->cap_inheritable; 9348c2ecf20Sopenharmony_ci new->cap_permitted = old->cap_permitted; 9358c2ecf20Sopenharmony_ci new->cap_effective = old->cap_effective; 9368c2ecf20Sopenharmony_ci new->cap_ambient = old->cap_ambient; 9378c2ecf20Sopenharmony_ci new->cap_bset = old->cap_bset; 9388c2ecf20Sopenharmony_ci 9398c2ecf20Sopenharmony_ci new->jit_keyring = old->jit_keyring; 9408c2ecf20Sopenharmony_ci new->thread_keyring = key_get(old->thread_keyring); 9418c2ecf20Sopenharmony_ci new->process_keyring = key_get(old->process_keyring); 9428c2ecf20Sopenharmony_ci 9438c2ecf20Sopenharmony_ci security_transfer_creds(new, old); 9448c2ecf20Sopenharmony_ci 9458c2ecf20Sopenharmony_ci commit_creds(new); 9468c2ecf20Sopenharmony_ci} 9478c2ecf20Sopenharmony_ci 9488c2ecf20Sopenharmony_ci/* 9498c2ecf20Sopenharmony_ci * Make sure that root's user and user-session keyrings exist. 9508c2ecf20Sopenharmony_ci */ 9518c2ecf20Sopenharmony_cistatic int __init init_root_keyring(void) 9528c2ecf20Sopenharmony_ci{ 9538c2ecf20Sopenharmony_ci return look_up_user_keyrings(NULL, NULL); 9548c2ecf20Sopenharmony_ci} 9558c2ecf20Sopenharmony_ci 9568c2ecf20Sopenharmony_cilate_initcall(init_root_keyring); 957