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