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 | bb00307 | 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 | bb00307 | 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 | /* |
| 279 | * deal with execve() |
| 280 | */ |
| 281 | int exec_keys(struct task_struct *tsk) |
| 282 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 283 | struct thread_group_cred *tgcred = NULL; |
| 284 | struct cred *new; |
| 285 | |
| 286 | #ifdef CONFIG_KEYS |
| 287 | tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL); |
| 288 | if (!tgcred) |
| 289 | return -ENOMEM; |
| 290 | #endif |
| 291 | |
| 292 | new = prepare_creds(); |
| 293 | if (new < 0) |
| 294 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
| 296 | /* newly exec'd tasks don't get a thread keyring */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 297 | key_put(new->thread_keyring); |
| 298 | new->thread_keyring = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 300 | /* create a new per-thread-group creds for all this set of threads to |
| 301 | * share */ |
| 302 | memcpy(tgcred, new->tgcred, sizeof(struct thread_group_cred)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 304 | atomic_set(&tgcred->usage, 1); |
| 305 | spin_lock_init(&tgcred->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 307 | /* inherit the session keyring; new process keyring */ |
| 308 | key_get(tgcred->session_keyring); |
| 309 | tgcred->process_keyring = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 311 | release_tgcred(new); |
| 312 | new->tgcred = tgcred; |
| 313 | |
| 314 | commit_creds(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | return 0; |
| 316 | |
| 317 | } /* end exec_keys() */ |
| 318 | |
| 319 | /*****************************************************************************/ |
| 320 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | * the filesystem user ID changed |
| 322 | */ |
| 323 | void key_fsuid_changed(struct task_struct *tsk) |
| 324 | { |
| 325 | /* update the ownership of the thread keyring */ |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 326 | BUG_ON(!tsk->cred); |
| 327 | if (tsk->cred->thread_keyring) { |
| 328 | down_write(&tsk->cred->thread_keyring->sem); |
| 329 | tsk->cred->thread_keyring->uid = tsk->cred->fsuid; |
| 330 | up_write(&tsk->cred->thread_keyring->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | } /* end key_fsuid_changed() */ |
| 334 | |
| 335 | /*****************************************************************************/ |
| 336 | /* |
| 337 | * the filesystem group ID changed |
| 338 | */ |
| 339 | void key_fsgid_changed(struct task_struct *tsk) |
| 340 | { |
| 341 | /* update the ownership of the thread keyring */ |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 342 | BUG_ON(!tsk->cred); |
| 343 | if (tsk->cred->thread_keyring) { |
| 344 | down_write(&tsk->cred->thread_keyring->sem); |
| 345 | tsk->cred->thread_keyring->gid = tsk->cred->fsgid; |
| 346 | up_write(&tsk->cred->thread_keyring->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | } /* end key_fsgid_changed() */ |
| 350 | |
| 351 | /*****************************************************************************/ |
| 352 | /* |
| 353 | * search the process keyrings for the first matching key |
| 354 | * - we use the supplied match function to see if the description (or other |
| 355 | * feature of interest) matches |
| 356 | * - we return -EAGAIN if we didn't find any matching key |
| 357 | * - we return -ENOKEY if we found only negative matching keys |
| 358 | */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 359 | key_ref_t search_process_keyrings(struct key_type *type, |
| 360 | const void *description, |
| 361 | key_match_func_t match, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 362 | const struct cred *cred) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 364 | struct request_key_auth *rka; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 365 | key_ref_t key_ref, ret, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
David Howells | 04c567d | 2006-06-22 14:47:18 -0700 | [diff] [blame] | 367 | might_sleep(); |
| 368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were |
| 370 | * searchable, but we failed to find a key or we found a negative key; |
| 371 | * otherwise we want to return a sample error (probably -EACCES) if |
| 372 | * none of the keyrings were searchable |
| 373 | * |
| 374 | * in terms of priority: success > -ENOKEY > -EAGAIN > other error |
| 375 | */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 376 | key_ref = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | ret = NULL; |
| 378 | err = ERR_PTR(-EAGAIN); |
| 379 | |
| 380 | /* search the thread keyring first */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 381 | if (cred->thread_keyring) { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 382 | key_ref = keyring_search_aux( |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 383 | make_key_ref(cred->thread_keyring, 1), |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 384 | cred, type, description, match); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 385 | if (!IS_ERR(key_ref)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | goto found; |
| 387 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 388 | switch (PTR_ERR(key_ref)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | case -EAGAIN: /* no key */ |
| 390 | if (ret) |
| 391 | break; |
| 392 | case -ENOKEY: /* negative key */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 393 | ret = key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | break; |
| 395 | default: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 396 | err = key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | break; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /* search the process keyring second */ |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 402 | if (cred->tgcred->process_keyring) { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 403 | key_ref = keyring_search_aux( |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 404 | make_key_ref(cred->tgcred->process_keyring, 1), |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 405 | cred, type, description, match); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 406 | if (!IS_ERR(key_ref)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | goto found; |
| 408 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 409 | switch (PTR_ERR(key_ref)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | case -EAGAIN: /* no key */ |
| 411 | if (ret) |
| 412 | break; |
| 413 | case -ENOKEY: /* negative key */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 414 | ret = key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | break; |
| 416 | default: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 417 | err = key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | break; |
| 419 | } |
| 420 | } |
| 421 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 422 | /* search the session keyring */ |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 423 | if (cred->tgcred->session_keyring) { |
David Howells | 8589b4e | 2005-06-23 22:00:53 -0700 | [diff] [blame] | 424 | rcu_read_lock(); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 425 | key_ref = keyring_search_aux( |
| 426 | make_key_ref(rcu_dereference( |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 427 | cred->tgcred->session_keyring), |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 428 | 1), |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 429 | cred, type, description, match); |
David Howells | 8589b4e | 2005-06-23 22:00:53 -0700 | [diff] [blame] | 430 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 432 | if (!IS_ERR(key_ref)) |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 433 | goto found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 435 | switch (PTR_ERR(key_ref)) { |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 436 | case -EAGAIN: /* no key */ |
| 437 | if (ret) |
| 438 | break; |
| 439 | case -ENOKEY: /* negative key */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 440 | ret = key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | break; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 442 | default: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 443 | err = key_ref; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 444 | break; |
| 445 | } |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 446 | } |
| 447 | /* or search the user-session keyring */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 448 | else if (cred->user->session_keyring) { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 449 | key_ref = keyring_search_aux( |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 450 | make_key_ref(cred->user->session_keyring, 1), |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 451 | cred, type, description, match); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 452 | if (!IS_ERR(key_ref)) |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 453 | goto found; |
| 454 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 455 | switch (PTR_ERR(key_ref)) { |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 456 | case -EAGAIN: /* no key */ |
| 457 | if (ret) |
| 458 | break; |
| 459 | case -ENOKEY: /* negative key */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 460 | ret = key_ref; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 461 | break; |
| 462 | default: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 463 | err = key_ref; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 464 | break; |
| 465 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } |
| 467 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 468 | /* if this process has an instantiation authorisation key, then we also |
| 469 | * search the keyrings of the process mentioned there |
| 470 | * - we don't permit access to request_key auth keys via this method |
| 471 | */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 472 | if (cred->request_key_auth && |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 473 | cred == current_cred() && |
David Howells | 04c567d | 2006-06-22 14:47:18 -0700 | [diff] [blame] | 474 | type != &key_type_request_key_auth |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 475 | ) { |
David Howells | 04c567d | 2006-06-22 14:47:18 -0700 | [diff] [blame] | 476 | /* defend against the auth key being revoked */ |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 477 | down_read(&cred->request_key_auth->sem); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 478 | |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 479 | if (key_validate(cred->request_key_auth) == 0) { |
| 480 | rka = cred->request_key_auth->payload.data; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 481 | |
David Howells | 04c567d | 2006-06-22 14:47:18 -0700 | [diff] [blame] | 482 | key_ref = search_process_keyrings(type, description, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 483 | match, rka->cred); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 484 | |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 485 | up_read(&cred->request_key_auth->sem); |
David Howells | 04c567d | 2006-06-22 14:47:18 -0700 | [diff] [blame] | 486 | |
| 487 | if (!IS_ERR(key_ref)) |
| 488 | goto found; |
| 489 | |
| 490 | switch (PTR_ERR(key_ref)) { |
| 491 | case -EAGAIN: /* no key */ |
| 492 | if (ret) |
| 493 | break; |
| 494 | case -ENOKEY: /* negative key */ |
| 495 | ret = key_ref; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 496 | break; |
David Howells | 04c567d | 2006-06-22 14:47:18 -0700 | [diff] [blame] | 497 | default: |
| 498 | err = key_ref; |
| 499 | break; |
| 500 | } |
| 501 | } else { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 502 | up_read(&cred->request_key_auth->sem); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | /* no key - decide on the error we're going to go for */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 507 | key_ref = ret ? ret : err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 509 | found: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 510 | return key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } /* end search_process_keyrings() */ |
| 513 | |
| 514 | /*****************************************************************************/ |
| 515 | /* |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 516 | * see if the key we're looking at is the target key |
| 517 | */ |
| 518 | static int lookup_user_key_possessed(const struct key *key, const void *target) |
| 519 | { |
| 520 | return key == target; |
| 521 | |
| 522 | } /* end lookup_user_key_possessed() */ |
| 523 | |
| 524 | /*****************************************************************************/ |
| 525 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | * lookup a key given a key ID from userspace with a given permissions mask |
| 527 | * - don't create special keyrings unless so requested |
| 528 | * - partially constructed keys aren't found unless requested |
| 529 | */ |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 530 | key_ref_t lookup_user_key(key_serial_t id, int create, int partial, |
| 531 | key_perm_t perm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | { |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 533 | struct request_key_auth *rka; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 534 | const struct cred *cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | struct key *key; |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 536 | key_ref_t key_ref, skey_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | int ret; |
| 538 | |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 539 | try_again: |
| 540 | cred = get_current_cred(); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 541 | key_ref = ERR_PTR(-ENOKEY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | |
| 543 | switch (id) { |
| 544 | case KEY_SPEC_THREAD_KEYRING: |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 545 | if (!cred->thread_keyring) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | if (!create) |
| 547 | goto error; |
| 548 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 549 | ret = install_thread_keyring(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | if (ret < 0) { |
| 551 | key = ERR_PTR(ret); |
| 552 | goto error; |
| 553 | } |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 554 | goto reget_creds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | } |
| 556 | |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 557 | key = cred->thread_keyring; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | atomic_inc(&key->usage); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 559 | key_ref = make_key_ref(key, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | break; |
| 561 | |
| 562 | case KEY_SPEC_PROCESS_KEYRING: |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 563 | if (!cred->tgcred->process_keyring) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | if (!create) |
| 565 | goto error; |
| 566 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 567 | ret = install_process_keyring(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | if (ret < 0) { |
| 569 | key = ERR_PTR(ret); |
| 570 | goto error; |
| 571 | } |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 572 | goto reget_creds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | } |
| 574 | |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 575 | key = cred->tgcred->process_keyring; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | atomic_inc(&key->usage); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 577 | key_ref = make_key_ref(key, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | break; |
| 579 | |
| 580 | case KEY_SPEC_SESSION_KEYRING: |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 581 | if (!cred->tgcred->session_keyring) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | /* always install a session keyring upon access if one |
| 583 | * doesn't exist yet */ |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 584 | ret = install_user_keyrings(); |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 585 | if (ret < 0) |
| 586 | goto error; |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 587 | ret = install_session_keyring( |
| 588 | cred->user->session_keyring); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | if (ret < 0) |
| 591 | goto error; |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 592 | goto reget_creds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | } |
| 594 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 595 | rcu_read_lock(); |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 596 | key = rcu_dereference(cred->tgcred->session_keyring); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | atomic_inc(&key->usage); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 598 | rcu_read_unlock(); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 599 | key_ref = make_key_ref(key, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | break; |
| 601 | |
| 602 | case KEY_SPEC_USER_KEYRING: |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 603 | if (!cred->user->uid_keyring) { |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 604 | ret = install_user_keyrings(); |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 605 | if (ret < 0) |
| 606 | goto error; |
| 607 | } |
| 608 | |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 609 | key = cred->user->uid_keyring; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | atomic_inc(&key->usage); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 611 | key_ref = make_key_ref(key, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | break; |
| 613 | |
| 614 | case KEY_SPEC_USER_SESSION_KEYRING: |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 615 | if (!cred->user->session_keyring) { |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 616 | ret = install_user_keyrings(); |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 617 | if (ret < 0) |
| 618 | goto error; |
| 619 | } |
| 620 | |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 621 | key = cred->user->session_keyring; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | atomic_inc(&key->usage); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 623 | key_ref = make_key_ref(key, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | break; |
| 625 | |
| 626 | case KEY_SPEC_GROUP_KEYRING: |
| 627 | /* group keyrings are not yet supported */ |
| 628 | key = ERR_PTR(-EINVAL); |
| 629 | goto error; |
| 630 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 631 | case KEY_SPEC_REQKEY_AUTH_KEY: |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 632 | key = cred->request_key_auth; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 633 | if (!key) |
| 634 | goto error; |
| 635 | |
| 636 | atomic_inc(&key->usage); |
| 637 | key_ref = make_key_ref(key, 1); |
| 638 | break; |
| 639 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 640 | case KEY_SPEC_REQUESTOR_KEYRING: |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 641 | if (!cred->request_key_auth) |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 642 | goto error; |
| 643 | |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 644 | down_read(&cred->request_key_auth->sem); |
| 645 | if (cred->request_key_auth->flags & KEY_FLAG_REVOKED) { |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 646 | key_ref = ERR_PTR(-EKEYREVOKED); |
| 647 | key = NULL; |
| 648 | } else { |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 649 | rka = cred->request_key_auth->payload.data; |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 650 | key = rka->dest_keyring; |
| 651 | atomic_inc(&key->usage); |
| 652 | } |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 653 | up_read(&cred->request_key_auth->sem); |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 654 | if (!key) |
| 655 | goto error; |
| 656 | key_ref = make_key_ref(key, 1); |
| 657 | break; |
| 658 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | default: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 660 | key_ref = ERR_PTR(-EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | if (id < 1) |
| 662 | goto error; |
| 663 | |
| 664 | key = key_lookup(id); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 665 | if (IS_ERR(key)) { |
David Howells | e231c2e | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 666 | key_ref = ERR_CAST(key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | goto error; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | key_ref = make_key_ref(key, 0); |
| 671 | |
| 672 | /* check to see if we possess the key */ |
| 673 | skey_ref = search_process_keyrings(key->type, key, |
| 674 | lookup_user_key_possessed, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 675 | cred); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 676 | |
| 677 | if (!IS_ERR(skey_ref)) { |
| 678 | key_put(key); |
| 679 | key_ref = skey_ref; |
| 680 | } |
| 681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | break; |
| 683 | } |
| 684 | |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 685 | if (!partial) { |
| 686 | ret = wait_for_key_construction(key, true); |
| 687 | switch (ret) { |
| 688 | case -ERESTARTSYS: |
| 689 | goto invalid_key; |
| 690 | default: |
| 691 | if (perm) |
| 692 | goto invalid_key; |
| 693 | case 0: |
| 694 | break; |
| 695 | } |
| 696 | } else if (perm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | ret = key_validate(key); |
| 698 | if (ret < 0) |
| 699 | goto invalid_key; |
| 700 | } |
| 701 | |
| 702 | ret = -EIO; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 703 | if (!partial && !test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | goto invalid_key; |
| 705 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 706 | /* check the permissions */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 707 | ret = key_task_permission(key_ref, cred, perm); |
David Howells | 29db919 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 708 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | goto invalid_key; |
| 710 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 711 | error: |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 712 | put_cred(cred); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 713 | return key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 715 | invalid_key: |
| 716 | key_ref_put(key_ref); |
| 717 | key_ref = ERR_PTR(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | goto error; |
| 719 | |
David Howells | bb952bb | 2008-11-14 10:39:20 +1100 | [diff] [blame] | 720 | /* if we attempted to install a keyring, then it may have caused new |
| 721 | * creds to be installed */ |
| 722 | reget_creds: |
| 723 | put_cred(cred); |
| 724 | goto try_again; |
| 725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | } /* end lookup_user_key() */ |
| 727 | |
| 728 | /*****************************************************************************/ |
| 729 | /* |
| 730 | * join the named keyring as the session keyring if possible, or attempt to |
| 731 | * create a new one of that name if not |
| 732 | * - if the name is NULL, an empty anonymous keyring is installed instead |
| 733 | * - named session keyring joining is done with a semaphore held |
| 734 | */ |
| 735 | long join_session_keyring(const char *name) |
| 736 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 737 | const struct cred *old; |
| 738 | struct cred *new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | struct key *keyring; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 740 | long ret, serial; |
| 741 | |
| 742 | /* only permit this if there's a single thread in the thread group - |
| 743 | * this avoids us having to adjust the creds on all threads and risking |
| 744 | * ENOMEM */ |
| 745 | if (!is_single_threaded(current)) |
| 746 | return -EMLINK; |
| 747 | |
| 748 | new = prepare_creds(); |
| 749 | if (!new) |
| 750 | return -ENOMEM; |
| 751 | old = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | |
| 753 | /* if no name is provided, install an anonymous keyring */ |
| 754 | if (!name) { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 755 | ret = install_session_keyring_to_cred(new, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | if (ret < 0) |
| 757 | goto error; |
| 758 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 759 | serial = new->tgcred->session_keyring->serial; |
| 760 | ret = commit_creds(new); |
| 761 | if (ret == 0) |
| 762 | ret = serial; |
| 763 | goto okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | /* allow the user to join or create a named keyring */ |
Ingo Molnar | bb00307 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 767 | mutex_lock(&key_session_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
| 769 | /* look for an existing keyring of this name */ |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 770 | keyring = find_keyring_by_name(name, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | if (PTR_ERR(keyring) == -ENOKEY) { |
| 772 | /* not found - try and create a new one */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 773 | keyring = keyring_alloc(name, old->uid, old->gid, old, |
David Howells | 7e047ef | 2006-06-26 00:24:50 -0700 | [diff] [blame] | 774 | KEY_ALLOC_IN_QUOTA, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | if (IS_ERR(keyring)) { |
| 776 | ret = PTR_ERR(keyring); |
David Howells | bcf945d | 2005-08-04 13:07:06 -0700 | [diff] [blame] | 777 | goto error2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | } |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 779 | } else if (IS_ERR(keyring)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | ret = PTR_ERR(keyring); |
| 781 | goto error2; |
| 782 | } |
| 783 | |
| 784 | /* we've got a keyring - now to install it */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 785 | ret = install_session_keyring_to_cred(new, keyring); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | if (ret < 0) |
| 787 | goto error2; |
| 788 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 789 | commit_creds(new); |
| 790 | mutex_unlock(&key_session_mutex); |
| 791 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | ret = keyring->serial; |
| 793 | key_put(keyring); |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 794 | okay: |
| 795 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 797 | error2: |
Ingo Molnar | bb00307 | 2006-03-22 00:09:14 -0800 | [diff] [blame] | 798 | mutex_unlock(&key_session_mutex); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 799 | error: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 800 | abort_creds(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | return ret; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame^] | 802 | } |