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