David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 1 | /* Management of 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 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/keyctl.h> |
| 17 | #include <linux/fs.h> |
| 18 | #include <linux/err.h> |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 19 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/uaccess.h> |
| 21 | #include "internal.h" |
| 22 | |
| 23 | /* session keyring create vs join semaphore */ |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 24 | static DEFINE_MUTEX(key_session_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 26 | /* user keyring creation semaphore */ |
| 27 | static DEFINE_MUTEX(key_user_keyring_mutex); |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | /* the root user's tracking struct */ |
| 30 | struct key_user root_key_user = { |
| 31 | .usage = ATOMIC_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), |
| 36 | .uid = 0, |
| 37 | }; |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | /*****************************************************************************/ |
| 40 | /* |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 41 | * install user and user session keyrings for a particular UID |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | */ |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 43 | int install_user_keyrings(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 45 | struct user_struct *user; |
| 46 | const struct cred *cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | struct key *uid_keyring, *session_keyring; |
| 48 | char buf[20]; |
| 49 | int ret; |
| 50 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 51 | cred = current_cred(); |
| 52 | user = cred->user; |
| 53 | |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 54 | kenter("%p{%u}", user, user->uid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 56 | if (user->uid_keyring) { |
| 57 | kleave(" = 0 [exist]"); |
| 58 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } |
| 60 | |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 61 | mutex_lock(&key_user_keyring_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | ret = 0; |
| 63 | |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 64 | if (!user->uid_keyring) { |
| 65 | /* get the UID-specific keyring |
| 66 | * - there may be one in existence already as it may have been |
| 67 | * pinned by a session, but the user_struct pointing to it |
| 68 | * may have been destroyed by setuid */ |
| 69 | sprintf(buf, "_uid.%u", user->uid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 71 | uid_keyring = find_keyring_by_name(buf, true); |
| 72 | if (IS_ERR(uid_keyring)) { |
| 73 | uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 74 | cred, KEY_ALLOC_IN_QUOTA, |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 75 | NULL); |
| 76 | if (IS_ERR(uid_keyring)) { |
| 77 | ret = PTR_ERR(uid_keyring); |
| 78 | goto error; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /* get a default session keyring (which might also exist |
| 83 | * already) */ |
| 84 | sprintf(buf, "_uid_ses.%u", user->uid); |
| 85 | |
| 86 | session_keyring = find_keyring_by_name(buf, true); |
| 87 | if (IS_ERR(session_keyring)) { |
| 88 | session_keyring = |
| 89 | keyring_alloc(buf, user->uid, (gid_t) -1, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 90 | cred, KEY_ALLOC_IN_QUOTA, NULL); |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 91 | if (IS_ERR(session_keyring)) { |
| 92 | ret = PTR_ERR(session_keyring); |
| 93 | goto error_release; |
| 94 | } |
| 95 | |
| 96 | /* we install a link from the user session keyring to |
| 97 | * the user keyring */ |
| 98 | ret = key_link(session_keyring, uid_keyring); |
| 99 | if (ret < 0) |
| 100 | goto error_release_both; |
| 101 | } |
| 102 | |
| 103 | /* install the keyrings */ |
| 104 | user->uid_keyring = uid_keyring; |
| 105 | user->session_keyring = session_keyring; |
| 106 | } |
| 107 | |
| 108 | mutex_unlock(&key_user_keyring_mutex); |
| 109 | kleave(" = 0"); |
| 110 | return 0; |
| 111 | |
| 112 | error_release_both: |
| 113 | key_put(session_keyring); |
| 114 | error_release: |
| 115 | key_put(uid_keyring); |
| 116 | error: |
| 117 | mutex_unlock(&key_user_keyring_mutex); |
| 118 | kleave(" = %d", ret); |
| 119 | return ret; |
| 120 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | /* |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 123 | * install a fresh thread keyring directly to new credentials |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 125 | int install_thread_keyring_to_cred(struct cred *new) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 127 | struct key *keyring; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 129 | keyring = keyring_alloc("_tid", new->uid, new->gid, new, |
| 130 | KEY_ALLOC_QUOTA_OVERRUN, NULL); |
| 131 | if (IS_ERR(keyring)) |
| 132 | return PTR_ERR(keyring); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 134 | new->thread_keyring = keyring; |
| 135 | return 0; |
| 136 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | /* |
| 139 | * install a fresh thread keyring, discarding the old one |
| 140 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 141 | static int install_thread_keyring(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 143 | struct cred *new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | int ret; |
| 145 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 146 | new = prepare_creds(); |
| 147 | if (!new) |
| 148 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 150 | BUG_ON(new->thread_keyring); |
| 151 | |
| 152 | ret = install_thread_keyring_to_cred(new); |
| 153 | if (ret < 0) { |
| 154 | abort_creds(new); |
| 155 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 158 | return commit_creds(new); |
| 159 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 161 | /* |
| 162 | * install a process keyring directly to a credentials struct |
| 163 | * - returns -EEXIST if there was already a process keyring, 0 if one installed, |
| 164 | * and other -ve on any other error |
| 165 | */ |
| 166 | int install_process_keyring_to_cred(struct cred *new) |
| 167 | { |
| 168 | struct key *keyring; |
| 169 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 171 | if (new->tgcred->process_keyring) |
| 172 | return -EEXIST; |
| 173 | |
| 174 | keyring = keyring_alloc("_pid", new->uid, new->gid, |
| 175 | new, KEY_ALLOC_QUOTA_OVERRUN, NULL); |
| 176 | if (IS_ERR(keyring)) |
| 177 | return PTR_ERR(keyring); |
| 178 | |
| 179 | spin_lock_irq(&new->tgcred->lock); |
| 180 | if (!new->tgcred->process_keyring) { |
| 181 | new->tgcred->process_keyring = keyring; |
| 182 | keyring = NULL; |
| 183 | ret = 0; |
| 184 | } else { |
| 185 | ret = -EEXIST; |
| 186 | } |
| 187 | spin_unlock_irq(&new->tgcred->lock); |
| 188 | key_put(keyring); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | return ret; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 190 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | /* |
| 193 | * make sure a process keyring is installed |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 194 | * - we |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 196 | static int install_process_keyring(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 198 | struct cred *new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | int ret; |
| 200 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 201 | new = prepare_creds(); |
| 202 | if (!new) |
| 203 | return -ENOMEM; |
David Howells | 1a26feb | 2006-04-10 22:54:26 -0700 | [diff] [blame] | 204 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 205 | ret = install_process_keyring_to_cred(new); |
| 206 | if (ret < 0) { |
| 207 | abort_creds(new); |
| 208 | return ret != -EEXIST ?: 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 211 | return commit_creds(new); |
| 212 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | /* |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 215 | * install a session keyring directly to a credentials struct |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 217 | static int install_session_keyring_to_cred(struct cred *cred, |
| 218 | struct key *keyring) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
David Howells | 7e047ef | 2006-06-26 00:24:50 -0700 | [diff] [blame] | 220 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | struct key *old; |
David Howells | 1a26feb | 2006-04-10 22:54:26 -0700 | [diff] [blame] | 222 | |
| 223 | might_sleep(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 225 | /* create an empty session keyring */ |
| 226 | if (!keyring) { |
David Howells | 7e047ef | 2006-06-26 00:24:50 -0700 | [diff] [blame] | 227 | flags = KEY_ALLOC_QUOTA_OVERRUN; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 228 | if (cred->tgcred->session_keyring) |
David Howells | 7e047ef | 2006-06-26 00:24:50 -0700 | [diff] [blame] | 229 | flags = KEY_ALLOC_IN_QUOTA; |
| 230 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 231 | keyring = keyring_alloc("_ses", cred->uid, cred->gid, |
| 232 | cred, flags, NULL); |
David Howells | 1a26feb | 2006-04-10 22:54:26 -0700 | [diff] [blame] | 233 | if (IS_ERR(keyring)) |
| 234 | return PTR_ERR(keyring); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 235 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | atomic_inc(&keyring->usage); |
| 237 | } |
| 238 | |
| 239 | /* install the keyring */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 240 | spin_lock_irq(&cred->tgcred->lock); |
| 241 | old = cred->tgcred->session_keyring; |
| 242 | rcu_assign_pointer(cred->tgcred->session_keyring, keyring); |
| 243 | spin_unlock_irq(&cred->tgcred->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
David Howells | 1a26feb | 2006-04-10 22:54:26 -0700 | [diff] [blame] | 245 | /* we're using RCU on the pointer, but there's no point synchronising |
| 246 | * on it if it didn't previously point to anything */ |
| 247 | if (old) { |
| 248 | synchronize_rcu(); |
| 249 | key_put(old); |
| 250 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
David Howells | 1a26feb | 2006-04-10 22:54:26 -0700 | [diff] [blame] | 252 | return 0; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 253 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
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 | * install a session keyring, discarding the old one |
| 257 | * - if a keyring is not supplied, an empty one is invented |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 259 | static int install_session_keyring(struct key *keyring) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 261 | struct cred *new; |
| 262 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 264 | new = prepare_creds(); |
| 265 | if (!new) |
| 266 | return -ENOMEM; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 267 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 268 | ret = install_session_keyring_to_cred(new, NULL); |
| 269 | if (ret < 0) { |
| 270 | abort_creds(new); |
| 271 | return ret; |
| 272 | } |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 273 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 274 | return commit_creds(new); |
| 275 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
| 277 | /*****************************************************************************/ |
| 278 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | * the filesystem user ID changed |
| 280 | */ |
| 281 | void key_fsuid_changed(struct task_struct *tsk) |
| 282 | { |
| 283 | /* update the ownership of the thread keyring */ |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 284 | BUG_ON(!tsk->cred); |
| 285 | if (tsk->cred->thread_keyring) { |
| 286 | down_write(&tsk->cred->thread_keyring->sem); |
| 287 | tsk->cred->thread_keyring->uid = tsk->cred->fsuid; |
| 288 | up_write(&tsk->cred->thread_keyring->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | } /* end key_fsuid_changed() */ |
| 292 | |
| 293 | /*****************************************************************************/ |
| 294 | /* |
| 295 | * the filesystem group ID changed |
| 296 | */ |
| 297 | void key_fsgid_changed(struct task_struct *tsk) |
| 298 | { |
| 299 | /* update the ownership of the thread keyring */ |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 300 | BUG_ON(!tsk->cred); |
| 301 | if (tsk->cred->thread_keyring) { |
| 302 | down_write(&tsk->cred->thread_keyring->sem); |
| 303 | tsk->cred->thread_keyring->gid = tsk->cred->fsgid; |
| 304 | up_write(&tsk->cred->thread_keyring->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | } /* end key_fsgid_changed() */ |
| 308 | |
| 309 | /*****************************************************************************/ |
| 310 | /* |
| 311 | * search the process keyrings for the first matching key |
| 312 | * - we use the supplied match function to see if the description (or other |
| 313 | * feature of interest) matches |
| 314 | * - we return -EAGAIN if we didn't find any matching key |
| 315 | * - we return -ENOKEY if we found only negative matching keys |
| 316 | */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 317 | key_ref_t search_process_keyrings(struct key_type *type, |
| 318 | const void *description, |
| 319 | key_match_func_t match, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 320 | const struct cred *cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | { |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 322 | struct request_key_auth *rka; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 323 | key_ref_t key_ref, ret, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
David Howells | 04c567d | 2006-06-22 14:47:18 -0700 | [diff] [blame] | 325 | might_sleep(); |
| 326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were |
| 328 | * searchable, but we failed to find a key or we found a negative key; |
| 329 | * otherwise we want to return a sample error (probably -EACCES) if |
| 330 | * none of the keyrings were searchable |
| 331 | * |
| 332 | * in terms of priority: success > -ENOKEY > -EAGAIN > other error |
| 333 | */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 334 | key_ref = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | ret = NULL; |
| 336 | err = ERR_PTR(-EAGAIN); |
| 337 | |
| 338 | /* search the thread keyring first */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 339 | if (cred->thread_keyring) { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 340 | key_ref = keyring_search_aux( |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 341 | make_key_ref(cred->thread_keyring, 1), |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 342 | cred, type, description, match); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 343 | if (!IS_ERR(key_ref)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | goto found; |
| 345 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 346 | switch (PTR_ERR(key_ref)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | case -EAGAIN: /* no key */ |
| 348 | if (ret) |
| 349 | break; |
| 350 | case -ENOKEY: /* negative key */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 351 | ret = key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | break; |
| 353 | default: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 354 | err = key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | break; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /* search the process keyring second */ |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 360 | if (cred->tgcred->process_keyring) { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 361 | key_ref = keyring_search_aux( |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 362 | make_key_ref(cred->tgcred->process_keyring, 1), |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 363 | cred, type, description, match); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 364 | if (!IS_ERR(key_ref)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | goto found; |
| 366 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 367 | switch (PTR_ERR(key_ref)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | case -EAGAIN: /* no key */ |
| 369 | if (ret) |
| 370 | break; |
| 371 | case -ENOKEY: /* negative key */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 372 | ret = key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | break; |
| 374 | default: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 375 | err = key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | break; |
| 377 | } |
| 378 | } |
| 379 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 380 | /* search the session keyring */ |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 381 | if (cred->tgcred->session_keyring) { |
David Howells | 8589b4e | 2005-06-23 22:00:53 -0700 | [diff] [blame] | 382 | rcu_read_lock(); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 383 | key_ref = keyring_search_aux( |
| 384 | make_key_ref(rcu_dereference( |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 385 | cred->tgcred->session_keyring), |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 386 | 1), |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 387 | cred, type, description, match); |
David Howells | 8589b4e | 2005-06-23 22:00:53 -0700 | [diff] [blame] | 388 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 390 | if (!IS_ERR(key_ref)) |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 391 | goto found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 393 | switch (PTR_ERR(key_ref)) { |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 394 | case -EAGAIN: /* no key */ |
| 395 | if (ret) |
| 396 | break; |
| 397 | case -ENOKEY: /* negative key */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 398 | ret = key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | break; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 400 | default: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 401 | err = key_ref; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 402 | break; |
| 403 | } |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 404 | } |
| 405 | /* or search the user-session keyring */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 406 | else if (cred->user->session_keyring) { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 407 | key_ref = keyring_search_aux( |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 408 | make_key_ref(cred->user->session_keyring, 1), |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 409 | cred, type, description, match); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 410 | if (!IS_ERR(key_ref)) |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 411 | goto found; |
| 412 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 413 | switch (PTR_ERR(key_ref)) { |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 414 | case -EAGAIN: /* no key */ |
| 415 | if (ret) |
| 416 | break; |
| 417 | case -ENOKEY: /* negative key */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 418 | ret = key_ref; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 419 | break; |
| 420 | default: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 421 | err = key_ref; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 422 | break; |
| 423 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } |
| 425 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 426 | /* if this process has an instantiation authorisation key, then we also |
| 427 | * search the keyrings of the process mentioned there |
| 428 | * - we don't permit access to request_key auth keys via this method |
| 429 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 430 | if (cred->request_key_auth && |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 431 | cred == current_cred() && |
David Howells | 04c567d | 2006-06-22 14:47:18 -0700 | [diff] [blame] | 432 | type != &key_type_request_key_auth |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 433 | ) { |
David Howells | 04c567d | 2006-06-22 14:47:18 -0700 | [diff] [blame] | 434 | /* defend against the auth key being revoked */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 435 | down_read(&cred->request_key_auth->sem); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 436 | |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 437 | if (key_validate(cred->request_key_auth) == 0) { |
| 438 | rka = cred->request_key_auth->payload.data; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 439 | |
David Howells | 04c567d | 2006-06-22 14:47:18 -0700 | [diff] [blame] | 440 | key_ref = search_process_keyrings(type, description, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 441 | match, rka->cred); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 442 | |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 443 | up_read(&cred->request_key_auth->sem); |
David Howells | 04c567d | 2006-06-22 14:47:18 -0700 | [diff] [blame] | 444 | |
| 445 | if (!IS_ERR(key_ref)) |
| 446 | goto found; |
| 447 | |
| 448 | switch (PTR_ERR(key_ref)) { |
| 449 | case -EAGAIN: /* no key */ |
| 450 | if (ret) |
| 451 | break; |
| 452 | case -ENOKEY: /* negative key */ |
| 453 | ret = key_ref; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 454 | break; |
David Howells | 04c567d | 2006-06-22 14:47:18 -0700 | [diff] [blame] | 455 | default: |
| 456 | err = key_ref; |
| 457 | break; |
| 458 | } |
| 459 | } else { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 460 | up_read(&cred->request_key_auth->sem); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | /* no key - decide on the error we're going to go for */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 465 | key_ref = ret ? ret : err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 467 | found: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 468 | return key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | } /* end search_process_keyrings() */ |
| 471 | |
| 472 | /*****************************************************************************/ |
| 473 | /* |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 474 | * see if the key we're looking at is the target key |
| 475 | */ |
| 476 | static int lookup_user_key_possessed(const struct key *key, const void *target) |
| 477 | { |
| 478 | return key == target; |
| 479 | |
| 480 | } /* end lookup_user_key_possessed() */ |
| 481 | |
| 482 | /*****************************************************************************/ |
| 483 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | * lookup a key given a key ID from userspace with a given permissions mask |
| 485 | * - don't create special keyrings unless so requested |
| 486 | * - partially constructed keys aren't found unless requested |
| 487 | */ |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 488 | key_ref_t lookup_user_key(key_serial_t id, int create, int partial, |
| 489 | key_perm_t perm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | { |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 491 | struct request_key_auth *rka; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 492 | const struct cred *cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | struct key *key; |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 494 | key_ref_t key_ref, skey_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | int ret; |
| 496 | |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 497 | try_again: |
| 498 | cred = get_current_cred(); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 499 | key_ref = ERR_PTR(-ENOKEY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
| 501 | switch (id) { |
| 502 | case KEY_SPEC_THREAD_KEYRING: |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 503 | if (!cred->thread_keyring) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | if (!create) |
| 505 | goto error; |
| 506 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 507 | ret = install_thread_keyring(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | if (ret < 0) { |
| 509 | key = ERR_PTR(ret); |
| 510 | goto error; |
| 511 | } |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 512 | goto reget_creds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | } |
| 514 | |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 515 | key = cred->thread_keyring; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | atomic_inc(&key->usage); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 517 | key_ref = make_key_ref(key, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | break; |
| 519 | |
| 520 | case KEY_SPEC_PROCESS_KEYRING: |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 521 | if (!cred->tgcred->process_keyring) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | if (!create) |
| 523 | goto error; |
| 524 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 525 | ret = install_process_keyring(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | if (ret < 0) { |
| 527 | key = ERR_PTR(ret); |
| 528 | goto error; |
| 529 | } |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 530 | goto reget_creds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | } |
| 532 | |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 533 | key = cred->tgcred->process_keyring; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | atomic_inc(&key->usage); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 535 | key_ref = make_key_ref(key, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | break; |
| 537 | |
| 538 | case KEY_SPEC_SESSION_KEYRING: |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 539 | if (!cred->tgcred->session_keyring) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | /* always install a session keyring upon access if one |
| 541 | * doesn't exist yet */ |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 542 | ret = install_user_keyrings(); |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 543 | if (ret < 0) |
| 544 | goto error; |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 545 | ret = install_session_keyring( |
| 546 | cred->user->session_keyring); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | if (ret < 0) |
| 549 | goto error; |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 550 | goto reget_creds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } |
| 552 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 553 | rcu_read_lock(); |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 554 | key = rcu_dereference(cred->tgcred->session_keyring); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | atomic_inc(&key->usage); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 556 | rcu_read_unlock(); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 557 | key_ref = make_key_ref(key, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | break; |
| 559 | |
| 560 | case KEY_SPEC_USER_KEYRING: |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 561 | if (!cred->user->uid_keyring) { |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 562 | ret = install_user_keyrings(); |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 563 | if (ret < 0) |
| 564 | goto error; |
| 565 | } |
| 566 | |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 567 | key = cred->user->uid_keyring; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | atomic_inc(&key->usage); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 569 | key_ref = make_key_ref(key, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | break; |
| 571 | |
| 572 | case KEY_SPEC_USER_SESSION_KEYRING: |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 573 | if (!cred->user->session_keyring) { |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 574 | ret = install_user_keyrings(); |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 575 | if (ret < 0) |
| 576 | goto error; |
| 577 | } |
| 578 | |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 579 | key = cred->user->session_keyring; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | atomic_inc(&key->usage); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 581 | key_ref = make_key_ref(key, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | break; |
| 583 | |
| 584 | case KEY_SPEC_GROUP_KEYRING: |
| 585 | /* group keyrings are not yet supported */ |
| 586 | key = ERR_PTR(-EINVAL); |
| 587 | goto error; |
| 588 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 589 | case KEY_SPEC_REQKEY_AUTH_KEY: |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 590 | key = cred->request_key_auth; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 591 | if (!key) |
| 592 | goto error; |
| 593 | |
| 594 | atomic_inc(&key->usage); |
| 595 | key_ref = make_key_ref(key, 1); |
| 596 | break; |
| 597 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 598 | case KEY_SPEC_REQUESTOR_KEYRING: |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 599 | if (!cred->request_key_auth) |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 600 | goto error; |
| 601 | |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 602 | down_read(&cred->request_key_auth->sem); |
| 603 | if (cred->request_key_auth->flags & KEY_FLAG_REVOKED) { |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 604 | key_ref = ERR_PTR(-EKEYREVOKED); |
| 605 | key = NULL; |
| 606 | } else { |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 607 | rka = cred->request_key_auth->payload.data; |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 608 | key = rka->dest_keyring; |
| 609 | atomic_inc(&key->usage); |
| 610 | } |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 611 | up_read(&cred->request_key_auth->sem); |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 612 | if (!key) |
| 613 | goto error; |
| 614 | key_ref = make_key_ref(key, 1); |
| 615 | break; |
| 616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | default: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 618 | key_ref = ERR_PTR(-EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | if (id < 1) |
| 620 | goto error; |
| 621 | |
| 622 | key = key_lookup(id); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 623 | if (IS_ERR(key)) { |
David Howells | e231c2e | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 624 | key_ref = ERR_CAST(key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | goto error; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | key_ref = make_key_ref(key, 0); |
| 629 | |
| 630 | /* check to see if we possess the key */ |
| 631 | skey_ref = search_process_keyrings(key->type, key, |
| 632 | lookup_user_key_possessed, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 633 | cred); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 634 | |
| 635 | if (!IS_ERR(skey_ref)) { |
| 636 | key_put(key); |
| 637 | key_ref = skey_ref; |
| 638 | } |
| 639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | break; |
| 641 | } |
| 642 | |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 643 | if (!partial) { |
| 644 | ret = wait_for_key_construction(key, true); |
| 645 | switch (ret) { |
| 646 | case -ERESTARTSYS: |
| 647 | goto invalid_key; |
| 648 | default: |
| 649 | if (perm) |
| 650 | goto invalid_key; |
| 651 | case 0: |
| 652 | break; |
| 653 | } |
| 654 | } else if (perm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | ret = key_validate(key); |
| 656 | if (ret < 0) |
| 657 | goto invalid_key; |
| 658 | } |
| 659 | |
| 660 | ret = -EIO; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 661 | if (!partial && !test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | goto invalid_key; |
| 663 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 664 | /* check the permissions */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 665 | ret = key_task_permission(key_ref, cred, perm); |
David Howells | 29db919 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 666 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | goto invalid_key; |
| 668 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 669 | error: |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 670 | put_cred(cred); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 671 | return key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 673 | invalid_key: |
| 674 | key_ref_put(key_ref); |
| 675 | key_ref = ERR_PTR(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | goto error; |
| 677 | |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 678 | /* if we attempted to install a keyring, then it may have caused new |
| 679 | * creds to be installed */ |
| 680 | reget_creds: |
| 681 | put_cred(cred); |
| 682 | goto try_again; |
| 683 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | } /* end lookup_user_key() */ |
| 685 | |
| 686 | /*****************************************************************************/ |
| 687 | /* |
| 688 | * join the named keyring as the session keyring if possible, or attempt to |
| 689 | * create a new one of that name if not |
| 690 | * - if the name is NULL, an empty anonymous keyring is installed instead |
| 691 | * - named session keyring joining is done with a semaphore held |
| 692 | */ |
| 693 | long join_session_keyring(const char *name) |
| 694 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 695 | const struct cred *old; |
| 696 | struct cred *new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | struct key *keyring; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 698 | long ret, serial; |
| 699 | |
| 700 | /* only permit this if there's a single thread in the thread group - |
| 701 | * this avoids us having to adjust the creds on all threads and risking |
| 702 | * ENOMEM */ |
| 703 | if (!is_single_threaded(current)) |
| 704 | return -EMLINK; |
| 705 | |
| 706 | new = prepare_creds(); |
| 707 | if (!new) |
| 708 | return -ENOMEM; |
| 709 | old = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | |
| 711 | /* if no name is provided, install an anonymous keyring */ |
| 712 | if (!name) { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 713 | ret = install_session_keyring_to_cred(new, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | if (ret < 0) |
| 715 | goto error; |
| 716 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 717 | serial = new->tgcred->session_keyring->serial; |
| 718 | ret = commit_creds(new); |
| 719 | if (ret == 0) |
| 720 | ret = serial; |
| 721 | goto okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | /* allow the user to join or create a named keyring */ |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 725 | mutex_lock(&key_session_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
| 727 | /* look for an existing keyring of this name */ |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 728 | keyring = find_keyring_by_name(name, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | if (PTR_ERR(keyring) == -ENOKEY) { |
| 730 | /* not found - try and create a new one */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 731 | keyring = keyring_alloc(name, old->uid, old->gid, old, |
David Howells | 7e047ef | 2006-06-26 00:24:50 -0700 | [diff] [blame] | 732 | KEY_ALLOC_IN_QUOTA, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | if (IS_ERR(keyring)) { |
| 734 | ret = PTR_ERR(keyring); |
David Howells | bcf945d | 2005-08-04 13:07:06 -0700 | [diff] [blame] | 735 | goto error2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | } |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 737 | } else if (IS_ERR(keyring)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | ret = PTR_ERR(keyring); |
| 739 | goto error2; |
| 740 | } |
| 741 | |
| 742 | /* we've got a keyring - now to install it */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 743 | ret = install_session_keyring_to_cred(new, keyring); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | if (ret < 0) |
| 745 | goto error2; |
| 746 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 747 | commit_creds(new); |
| 748 | mutex_unlock(&key_session_mutex); |
| 749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | ret = keyring->serial; |
| 751 | key_put(keyring); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 752 | okay: |
| 753 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 755 | error2: |
Ingo Molnar | bb003079 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 756 | mutex_unlock(&key_session_mutex); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 757 | error: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 758 | abort_creds(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | return ret; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 760 | } |