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