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