Commit | Line | Data |
---|---|---|
69664cf1 | 1 | /* Management of a process's keyrings |
1da177e4 | 2 | * |
69664cf1 | 3 | * Copyright (C) 2004-2005, 2008 Red Hat, Inc. All Rights Reserved. |
1da177e4 LT |
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> | |
1da177e4 LT |
15 | #include <linux/keyctl.h> |
16 | #include <linux/fs.h> | |
17 | #include <linux/err.h> | |
bb003079 | 18 | #include <linux/mutex.h> |
ee18d64c | 19 | #include <linux/security.h> |
1d1e9756 | 20 | #include <linux/user_namespace.h> |
1da177e4 LT |
21 | #include <asm/uaccess.h> |
22 | #include "internal.h" | |
23 | ||
24 | /* session keyring create vs join semaphore */ | |
bb003079 | 25 | static DEFINE_MUTEX(key_session_mutex); |
1da177e4 | 26 | |
69664cf1 DH |
27 | /* user keyring creation semaphore */ |
28 | static DEFINE_MUTEX(key_user_keyring_mutex); | |
29 | ||
1da177e4 LT |
30 | /* the root user's tracking struct */ |
31 | struct key_user root_key_user = { | |
32 | .usage = ATOMIC_INIT(3), | |
76181c13 | 33 | .cons_lock = __MUTEX_INITIALIZER(root_key_user.cons_lock), |
6cfd76a2 | 34 | .lock = __SPIN_LOCK_UNLOCKED(root_key_user.lock), |
1da177e4 LT |
35 | .nkeys = ATOMIC_INIT(2), |
36 | .nikeys = ATOMIC_INIT(2), | |
37 | .uid = 0, | |
1d1e9756 | 38 | .user_ns = &init_user_ns, |
1da177e4 LT |
39 | }; |
40 | ||
1da177e4 LT |
41 | /*****************************************************************************/ |
42 | /* | |
69664cf1 | 43 | * install user and user session keyrings for a particular UID |
1da177e4 | 44 | */ |
8bbf4976 | 45 | int install_user_keyrings(void) |
1da177e4 | 46 | { |
d84f4f99 DH |
47 | struct user_struct *user; |
48 | const struct cred *cred; | |
1da177e4 LT |
49 | struct key *uid_keyring, *session_keyring; |
50 | char buf[20]; | |
51 | int ret; | |
52 | ||
d84f4f99 DH |
53 | cred = current_cred(); |
54 | user = cred->user; | |
55 | ||
69664cf1 | 56 | kenter("%p{%u}", user, user->uid); |
1da177e4 | 57 | |
69664cf1 DH |
58 | if (user->uid_keyring) { |
59 | kleave(" = 0 [exist]"); | |
60 | return 0; | |
1da177e4 LT |
61 | } |
62 | ||
69664cf1 DH |
63 | mutex_lock(&key_user_keyring_mutex); |
64 | ret = 0; | |
1da177e4 | 65 | |
69664cf1 DH |
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); | |
72 | ||
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, | |
d84f4f99 | 76 | cred, KEY_ALLOC_IN_QUOTA, |
69664cf1 DH |
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, | |
d84f4f99 | 92 | cred, KEY_ALLOC_IN_QUOTA, NULL); |
69664cf1 DH |
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; | |
1da177e4 LT |
108 | } |
109 | ||
69664cf1 DH |
110 | mutex_unlock(&key_user_keyring_mutex); |
111 | kleave(" = 0"); | |
112 | return 0; | |
1da177e4 | 113 | |
69664cf1 DH |
114 | error_release_both: |
115 | key_put(session_keyring); | |
116 | error_release: | |
117 | key_put(uid_keyring); | |
664cceb0 | 118 | error: |
69664cf1 DH |
119 | mutex_unlock(&key_user_keyring_mutex); |
120 | kleave(" = %d", ret); | |
1da177e4 | 121 | return ret; |
69664cf1 | 122 | } |
1da177e4 | 123 | |
1da177e4 | 124 | /* |
d84f4f99 | 125 | * install a fresh thread keyring directly to new credentials |
1da177e4 | 126 | */ |
d84f4f99 | 127 | int install_thread_keyring_to_cred(struct cred *new) |
1da177e4 | 128 | { |
d84f4f99 | 129 | struct key *keyring; |
1da177e4 | 130 | |
d84f4f99 DH |
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); | |
1da177e4 | 135 | |
d84f4f99 DH |
136 | new->thread_keyring = keyring; |
137 | return 0; | |
138 | } | |
1da177e4 | 139 | |
1da177e4 LT |
140 | /* |
141 | * install a fresh thread keyring, discarding the old one | |
142 | */ | |
d84f4f99 | 143 | static int install_thread_keyring(void) |
1da177e4 | 144 | { |
d84f4f99 | 145 | struct cred *new; |
1da177e4 LT |
146 | int ret; |
147 | ||
d84f4f99 DH |
148 | new = prepare_creds(); |
149 | if (!new) | |
150 | return -ENOMEM; | |
1da177e4 | 151 | |
d84f4f99 DH |
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; | |
1da177e4 LT |
158 | } |
159 | ||
d84f4f99 DH |
160 | return commit_creds(new); |
161 | } | |
1da177e4 | 162 | |
d84f4f99 DH |
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; | |
1da177e4 | 172 | |
d84f4f99 DH |
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); | |
1da177e4 | 191 | return ret; |
d84f4f99 | 192 | } |
1da177e4 | 193 | |
1da177e4 LT |
194 | /* |
195 | * make sure a process keyring is installed | |
d84f4f99 | 196 | * - we |
1da177e4 | 197 | */ |
d84f4f99 | 198 | static int install_process_keyring(void) |
1da177e4 | 199 | { |
d84f4f99 | 200 | struct cred *new; |
1da177e4 LT |
201 | int ret; |
202 | ||
d84f4f99 DH |
203 | new = prepare_creds(); |
204 | if (!new) | |
205 | return -ENOMEM; | |
1da177e4 | 206 | |
d84f4f99 DH |
207 | ret = install_process_keyring_to_cred(new); |
208 | if (ret < 0) { | |
209 | abort_creds(new); | |
210 | return ret != -EEXIST ?: 0; | |
1da177e4 LT |
211 | } |
212 | ||
d84f4f99 DH |
213 | return commit_creds(new); |
214 | } | |
1da177e4 | 215 | |
1da177e4 | 216 | /* |
d84f4f99 | 217 | * install a session keyring directly to a credentials struct |
1da177e4 | 218 | */ |
685bfd2c | 219 | int install_session_keyring_to_cred(struct cred *cred, struct key *keyring) |
1da177e4 | 220 | { |
7e047ef5 | 221 | unsigned long flags; |
1da177e4 | 222 | struct key *old; |
1a26feb9 DH |
223 | |
224 | might_sleep(); | |
1da177e4 LT |
225 | |
226 | /* create an empty session keyring */ | |
227 | if (!keyring) { | |
7e047ef5 | 228 | flags = KEY_ALLOC_QUOTA_OVERRUN; |
d84f4f99 | 229 | if (cred->tgcred->session_keyring) |
7e047ef5 DH |
230 | flags = KEY_ALLOC_IN_QUOTA; |
231 | ||
d84f4f99 DH |
232 | keyring = keyring_alloc("_ses", cred->uid, cred->gid, |
233 | cred, flags, NULL); | |
1a26feb9 DH |
234 | if (IS_ERR(keyring)) |
235 | return PTR_ERR(keyring); | |
d84f4f99 | 236 | } else { |
1da177e4 LT |
237 | atomic_inc(&keyring->usage); |
238 | } | |
239 | ||
240 | /* install the keyring */ | |
d84f4f99 DH |
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); | |
1da177e4 | 245 | |
1a26feb9 DH |
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 | } | |
1da177e4 | 252 | |
1a26feb9 | 253 | return 0; |
d84f4f99 | 254 | } |
1da177e4 | 255 | |
1da177e4 | 256 | /* |
d84f4f99 DH |
257 | * install a session keyring, discarding the old one |
258 | * - if a keyring is not supplied, an empty one is invented | |
1da177e4 | 259 | */ |
d84f4f99 | 260 | static int install_session_keyring(struct key *keyring) |
1da177e4 | 261 | { |
d84f4f99 DH |
262 | struct cred *new; |
263 | int ret; | |
1da177e4 | 264 | |
d84f4f99 DH |
265 | new = prepare_creds(); |
266 | if (!new) | |
267 | return -ENOMEM; | |
1da177e4 | 268 | |
d84f4f99 DH |
269 | ret = install_session_keyring_to_cred(new, NULL); |
270 | if (ret < 0) { | |
271 | abort_creds(new); | |
272 | return ret; | |
273 | } | |
1da177e4 | 274 | |
d84f4f99 DH |
275 | return commit_creds(new); |
276 | } | |
1da177e4 | 277 | |
1da177e4 LT |
278 | /*****************************************************************************/ |
279 | /* | |
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 */ | |
b6dff3ec DH |
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); | |
1da177e4 LT |
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 */ | |
b6dff3ec DH |
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); | |
1da177e4 LT |
306 | } |
307 | ||
308 | } /* end key_fsgid_changed() */ | |
309 | ||
310 | /*****************************************************************************/ | |
311 | /* | |
312 | * search the process keyrings for the first matching key | |
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 | */ | |
664cceb0 DH |
318 | key_ref_t search_process_keyrings(struct key_type *type, |
319 | const void *description, | |
320 | key_match_func_t match, | |
d84f4f99 | 321 | const struct cred *cred) |
1da177e4 | 322 | { |
3e30148c | 323 | struct request_key_auth *rka; |
b5f545c8 | 324 | key_ref_t key_ref, ret, err; |
1da177e4 | 325 | |
04c567d9 DH |
326 | might_sleep(); |
327 | ||
1da177e4 LT |
328 | /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were |
329 | * searchable, but we failed to find a key or we found a negative key; | |
330 | * otherwise we want to return a sample error (probably -EACCES) if | |
331 | * none of the keyrings were searchable | |
332 | * | |
333 | * in terms of priority: success > -ENOKEY > -EAGAIN > other error | |
334 | */ | |
664cceb0 | 335 | key_ref = NULL; |
1da177e4 LT |
336 | ret = NULL; |
337 | err = ERR_PTR(-EAGAIN); | |
338 | ||
339 | /* search the thread keyring first */ | |
c69e8d9c | 340 | if (cred->thread_keyring) { |
664cceb0 | 341 | key_ref = keyring_search_aux( |
c69e8d9c | 342 | make_key_ref(cred->thread_keyring, 1), |
d84f4f99 | 343 | cred, type, description, match); |
664cceb0 | 344 | if (!IS_ERR(key_ref)) |
1da177e4 LT |
345 | goto found; |
346 | ||
664cceb0 | 347 | switch (PTR_ERR(key_ref)) { |
1da177e4 LT |
348 | case -EAGAIN: /* no key */ |
349 | if (ret) | |
350 | break; | |
351 | case -ENOKEY: /* negative key */ | |
664cceb0 | 352 | ret = key_ref; |
1da177e4 LT |
353 | break; |
354 | default: | |
664cceb0 | 355 | err = key_ref; |
1da177e4 LT |
356 | break; |
357 | } | |
358 | } | |
359 | ||
360 | /* search the process keyring second */ | |
bb952bb9 | 361 | if (cred->tgcred->process_keyring) { |
664cceb0 | 362 | key_ref = keyring_search_aux( |
bb952bb9 | 363 | make_key_ref(cred->tgcred->process_keyring, 1), |
d84f4f99 | 364 | cred, type, description, match); |
664cceb0 | 365 | if (!IS_ERR(key_ref)) |
1da177e4 LT |
366 | goto found; |
367 | ||
664cceb0 | 368 | switch (PTR_ERR(key_ref)) { |
1da177e4 LT |
369 | case -EAGAIN: /* no key */ |
370 | if (ret) | |
371 | break; | |
372 | case -ENOKEY: /* negative key */ | |
664cceb0 | 373 | ret = key_ref; |
1da177e4 LT |
374 | break; |
375 | default: | |
664cceb0 | 376 | err = key_ref; |
1da177e4 LT |
377 | break; |
378 | } | |
379 | } | |
380 | ||
3e30148c | 381 | /* search the session keyring */ |
bb952bb9 | 382 | if (cred->tgcred->session_keyring) { |
8589b4e0 | 383 | rcu_read_lock(); |
664cceb0 DH |
384 | key_ref = keyring_search_aux( |
385 | make_key_ref(rcu_dereference( | |
bb952bb9 | 386 | cred->tgcred->session_keyring), |
664cceb0 | 387 | 1), |
d84f4f99 | 388 | cred, type, description, match); |
8589b4e0 | 389 | rcu_read_unlock(); |
3e30148c | 390 | |
664cceb0 | 391 | if (!IS_ERR(key_ref)) |
3e30148c DH |
392 | goto found; |
393 | ||
664cceb0 | 394 | switch (PTR_ERR(key_ref)) { |
3e30148c DH |
395 | case -EAGAIN: /* no key */ |
396 | if (ret) | |
397 | break; | |
398 | case -ENOKEY: /* negative key */ | |
664cceb0 | 399 | ret = key_ref; |
3e30148c DH |
400 | break; |
401 | default: | |
664cceb0 | 402 | err = key_ref; |
3e30148c DH |
403 | break; |
404 | } | |
b5f545c8 DH |
405 | } |
406 | /* or search the user-session keyring */ | |
c69e8d9c | 407 | else if (cred->user->session_keyring) { |
b5f545c8 | 408 | key_ref = keyring_search_aux( |
c69e8d9c | 409 | make_key_ref(cred->user->session_keyring, 1), |
d84f4f99 | 410 | cred, type, description, match); |
664cceb0 | 411 | if (!IS_ERR(key_ref)) |
3e30148c DH |
412 | goto found; |
413 | ||
664cceb0 | 414 | switch (PTR_ERR(key_ref)) { |
3e30148c DH |
415 | case -EAGAIN: /* no key */ |
416 | if (ret) | |
417 | break; | |
418 | case -ENOKEY: /* negative key */ | |
664cceb0 | 419 | ret = key_ref; |
3e30148c DH |
420 | break; |
421 | default: | |
664cceb0 | 422 | err = key_ref; |
3e30148c DH |
423 | break; |
424 | } | |
8589b4e0 | 425 | } |
b5f545c8 DH |
426 | |
427 | /* if this process has an instantiation authorisation key, then we also | |
428 | * search the keyrings of the process mentioned there | |
429 | * - we don't permit access to request_key auth keys via this method | |
430 | */ | |
c69e8d9c | 431 | if (cred->request_key_auth && |
d84f4f99 | 432 | cred == current_cred() && |
04c567d9 | 433 | type != &key_type_request_key_auth |
b5f545c8 | 434 | ) { |
04c567d9 | 435 | /* defend against the auth key being revoked */ |
c69e8d9c | 436 | down_read(&cred->request_key_auth->sem); |
b5f545c8 | 437 | |
c69e8d9c DH |
438 | if (key_validate(cred->request_key_auth) == 0) { |
439 | rka = cred->request_key_auth->payload.data; | |
b5f545c8 | 440 | |
04c567d9 | 441 | key_ref = search_process_keyrings(type, description, |
d84f4f99 | 442 | match, rka->cred); |
1da177e4 | 443 | |
c69e8d9c | 444 | up_read(&cred->request_key_auth->sem); |
04c567d9 DH |
445 | |
446 | if (!IS_ERR(key_ref)) | |
447 | goto found; | |
448 | ||
449 | switch (PTR_ERR(key_ref)) { | |
450 | case -EAGAIN: /* no key */ | |
451 | if (ret) | |
452 | break; | |
453 | case -ENOKEY: /* negative key */ | |
454 | ret = key_ref; | |
3e30148c | 455 | break; |
04c567d9 DH |
456 | default: |
457 | err = key_ref; | |
458 | break; | |
459 | } | |
460 | } else { | |
c69e8d9c | 461 | up_read(&cred->request_key_auth->sem); |
3e30148c | 462 | } |
1da177e4 LT |
463 | } |
464 | ||
465 | /* no key - decide on the error we're going to go for */ | |
664cceb0 | 466 | key_ref = ret ? ret : err; |
1da177e4 | 467 | |
3e30148c | 468 | found: |
664cceb0 | 469 | return key_ref; |
1da177e4 | 470 | |
1da177e4 LT |
471 | } /* end search_process_keyrings() */ |
472 | ||
664cceb0 DH |
473 | /*****************************************************************************/ |
474 | /* | |
475 | * see if the key we're looking at is the target key | |
476 | */ | |
477 | static int lookup_user_key_possessed(const struct key *key, const void *target) | |
478 | { | |
479 | return key == target; | |
480 | ||
481 | } /* end lookup_user_key_possessed() */ | |
482 | ||
1da177e4 LT |
483 | /*****************************************************************************/ |
484 | /* | |
485 | * lookup a key given a key ID from userspace with a given permissions mask | |
486 | * - don't create special keyrings unless so requested | |
487 | * - partially constructed keys aren't found unless requested | |
488 | */ | |
5593122e | 489 | key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags, |
8bbf4976 | 490 | key_perm_t perm) |
1da177e4 | 491 | { |
8bbf4976 | 492 | struct request_key_auth *rka; |
d84f4f99 | 493 | const struct cred *cred; |
1da177e4 | 494 | struct key *key; |
b6dff3ec | 495 | key_ref_t key_ref, skey_ref; |
1da177e4 LT |
496 | int ret; |
497 | ||
bb952bb9 DH |
498 | try_again: |
499 | cred = get_current_cred(); | |
664cceb0 | 500 | key_ref = ERR_PTR(-ENOKEY); |
1da177e4 LT |
501 | |
502 | switch (id) { | |
503 | case KEY_SPEC_THREAD_KEYRING: | |
b6dff3ec | 504 | if (!cred->thread_keyring) { |
5593122e | 505 | if (!(lflags & KEY_LOOKUP_CREATE)) |
1da177e4 LT |
506 | goto error; |
507 | ||
8bbf4976 | 508 | ret = install_thread_keyring(); |
1da177e4 | 509 | if (ret < 0) { |
4d09ec0f | 510 | key_ref = ERR_PTR(ret); |
1da177e4 LT |
511 | goto error; |
512 | } | |
bb952bb9 | 513 | goto reget_creds; |
1da177e4 LT |
514 | } |
515 | ||
b6dff3ec | 516 | key = cred->thread_keyring; |
1da177e4 | 517 | atomic_inc(&key->usage); |
664cceb0 | 518 | key_ref = make_key_ref(key, 1); |
1da177e4 LT |
519 | break; |
520 | ||
521 | case KEY_SPEC_PROCESS_KEYRING: | |
bb952bb9 | 522 | if (!cred->tgcred->process_keyring) { |
5593122e | 523 | if (!(lflags & KEY_LOOKUP_CREATE)) |
1da177e4 LT |
524 | goto error; |
525 | ||
8bbf4976 | 526 | ret = install_process_keyring(); |
1da177e4 | 527 | if (ret < 0) { |
4d09ec0f | 528 | key_ref = ERR_PTR(ret); |
1da177e4 LT |
529 | goto error; |
530 | } | |
bb952bb9 | 531 | goto reget_creds; |
1da177e4 LT |
532 | } |
533 | ||
bb952bb9 | 534 | key = cred->tgcred->process_keyring; |
1da177e4 | 535 | atomic_inc(&key->usage); |
664cceb0 | 536 | key_ref = make_key_ref(key, 1); |
1da177e4 LT |
537 | break; |
538 | ||
539 | case KEY_SPEC_SESSION_KEYRING: | |
bb952bb9 | 540 | if (!cred->tgcred->session_keyring) { |
1da177e4 LT |
541 | /* always install a session keyring upon access if one |
542 | * doesn't exist yet */ | |
8bbf4976 | 543 | ret = install_user_keyrings(); |
69664cf1 DH |
544 | if (ret < 0) |
545 | goto error; | |
b6dff3ec DH |
546 | ret = install_session_keyring( |
547 | cred->user->session_keyring); | |
d84f4f99 | 548 | |
1da177e4 LT |
549 | if (ret < 0) |
550 | goto error; | |
bb952bb9 | 551 | goto reget_creds; |
1da177e4 LT |
552 | } |
553 | ||
3e30148c | 554 | rcu_read_lock(); |
bb952bb9 | 555 | key = rcu_dereference(cred->tgcred->session_keyring); |
1da177e4 | 556 | atomic_inc(&key->usage); |
3e30148c | 557 | rcu_read_unlock(); |
664cceb0 | 558 | key_ref = make_key_ref(key, 1); |
1da177e4 LT |
559 | break; |
560 | ||
561 | case KEY_SPEC_USER_KEYRING: | |
b6dff3ec | 562 | if (!cred->user->uid_keyring) { |
8bbf4976 | 563 | ret = install_user_keyrings(); |
69664cf1 DH |
564 | if (ret < 0) |
565 | goto error; | |
566 | } | |
567 | ||
b6dff3ec | 568 | key = cred->user->uid_keyring; |
1da177e4 | 569 | atomic_inc(&key->usage); |
664cceb0 | 570 | key_ref = make_key_ref(key, 1); |
1da177e4 LT |
571 | break; |
572 | ||
573 | case KEY_SPEC_USER_SESSION_KEYRING: | |
b6dff3ec | 574 | if (!cred->user->session_keyring) { |
8bbf4976 | 575 | ret = install_user_keyrings(); |
69664cf1 DH |
576 | if (ret < 0) |
577 | goto error; | |
578 | } | |
579 | ||
b6dff3ec | 580 | key = cred->user->session_keyring; |
1da177e4 | 581 | atomic_inc(&key->usage); |
664cceb0 | 582 | key_ref = make_key_ref(key, 1); |
1da177e4 LT |
583 | break; |
584 | ||
585 | case KEY_SPEC_GROUP_KEYRING: | |
586 | /* group keyrings are not yet supported */ | |
4d09ec0f | 587 | key_ref = ERR_PTR(-EINVAL); |
1da177e4 LT |
588 | goto error; |
589 | ||
b5f545c8 | 590 | case KEY_SPEC_REQKEY_AUTH_KEY: |
b6dff3ec | 591 | key = cred->request_key_auth; |
b5f545c8 DH |
592 | if (!key) |
593 | goto error; | |
594 | ||
595 | atomic_inc(&key->usage); | |
596 | key_ref = make_key_ref(key, 1); | |
597 | break; | |
598 | ||
8bbf4976 | 599 | case KEY_SPEC_REQUESTOR_KEYRING: |
b6dff3ec | 600 | if (!cred->request_key_auth) |
8bbf4976 DH |
601 | goto error; |
602 | ||
b6dff3ec DH |
603 | down_read(&cred->request_key_auth->sem); |
604 | if (cred->request_key_auth->flags & KEY_FLAG_REVOKED) { | |
8bbf4976 DH |
605 | key_ref = ERR_PTR(-EKEYREVOKED); |
606 | key = NULL; | |
607 | } else { | |
b6dff3ec | 608 | rka = cred->request_key_auth->payload.data; |
8bbf4976 DH |
609 | key = rka->dest_keyring; |
610 | atomic_inc(&key->usage); | |
611 | } | |
b6dff3ec | 612 | up_read(&cred->request_key_auth->sem); |
8bbf4976 DH |
613 | if (!key) |
614 | goto error; | |
615 | key_ref = make_key_ref(key, 1); | |
616 | break; | |
617 | ||
1da177e4 | 618 | default: |
664cceb0 | 619 | key_ref = ERR_PTR(-EINVAL); |
1da177e4 LT |
620 | if (id < 1) |
621 | goto error; | |
622 | ||
623 | key = key_lookup(id); | |
664cceb0 | 624 | if (IS_ERR(key)) { |
e231c2ee | 625 | key_ref = ERR_CAST(key); |
1da177e4 | 626 | goto error; |
664cceb0 DH |
627 | } |
628 | ||
629 | key_ref = make_key_ref(key, 0); | |
630 | ||
631 | /* check to see if we possess the key */ | |
632 | skey_ref = search_process_keyrings(key->type, key, | |
633 | lookup_user_key_possessed, | |
d84f4f99 | 634 | cred); |
664cceb0 DH |
635 | |
636 | if (!IS_ERR(skey_ref)) { | |
637 | key_put(key); | |
638 | key_ref = skey_ref; | |
639 | } | |
640 | ||
1da177e4 LT |
641 | break; |
642 | } | |
643 | ||
5593122e DH |
644 | /* unlink does not use the nominated key in any way, so can skip all |
645 | * the permission checks as it is only concerned with the keyring */ | |
646 | if (lflags & KEY_LOOKUP_FOR_UNLINK) { | |
647 | ret = 0; | |
648 | goto error; | |
649 | } | |
650 | ||
651 | if (!(lflags & KEY_LOOKUP_PARTIAL)) { | |
76181c13 DH |
652 | ret = wait_for_key_construction(key, true); |
653 | switch (ret) { | |
654 | case -ERESTARTSYS: | |
655 | goto invalid_key; | |
656 | default: | |
657 | if (perm) | |
658 | goto invalid_key; | |
659 | case 0: | |
660 | break; | |
661 | } | |
662 | } else if (perm) { | |
1da177e4 LT |
663 | ret = key_validate(key); |
664 | if (ret < 0) | |
665 | goto invalid_key; | |
666 | } | |
667 | ||
668 | ret = -EIO; | |
5593122e DH |
669 | if (!(lflags & KEY_LOOKUP_PARTIAL) && |
670 | !test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) | |
1da177e4 LT |
671 | goto invalid_key; |
672 | ||
3e30148c | 673 | /* check the permissions */ |
d84f4f99 | 674 | ret = key_task_permission(key_ref, cred, perm); |
29db9190 | 675 | if (ret < 0) |
1da177e4 LT |
676 | goto invalid_key; |
677 | ||
664cceb0 | 678 | error: |
bb952bb9 | 679 | put_cred(cred); |
664cceb0 | 680 | return key_ref; |
1da177e4 | 681 | |
664cceb0 DH |
682 | invalid_key: |
683 | key_ref_put(key_ref); | |
684 | key_ref = ERR_PTR(ret); | |
1da177e4 LT |
685 | goto error; |
686 | ||
bb952bb9 DH |
687 | /* if we attempted to install a keyring, then it may have caused new |
688 | * creds to be installed */ | |
689 | reget_creds: | |
690 | put_cred(cred); | |
691 | goto try_again; | |
692 | ||
1da177e4 LT |
693 | } /* end lookup_user_key() */ |
694 | ||
695 | /*****************************************************************************/ | |
696 | /* | |
697 | * join the named keyring as the session keyring if possible, or attempt to | |
698 | * create a new one of that name if not | |
699 | * - if the name is NULL, an empty anonymous keyring is installed instead | |
700 | * - named session keyring joining is done with a semaphore held | |
701 | */ | |
702 | long join_session_keyring(const char *name) | |
703 | { | |
d84f4f99 DH |
704 | const struct cred *old; |
705 | struct cred *new; | |
1da177e4 | 706 | struct key *keyring; |
d84f4f99 DH |
707 | long ret, serial; |
708 | ||
709 | /* only permit this if there's a single thread in the thread group - | |
710 | * this avoids us having to adjust the creds on all threads and risking | |
711 | * ENOMEM */ | |
5bb459bb | 712 | if (!current_is_single_threaded()) |
d84f4f99 DH |
713 | return -EMLINK; |
714 | ||
715 | new = prepare_creds(); | |
716 | if (!new) | |
717 | return -ENOMEM; | |
718 | old = current_cred(); | |
1da177e4 LT |
719 | |
720 | /* if no name is provided, install an anonymous keyring */ | |
721 | if (!name) { | |
d84f4f99 | 722 | ret = install_session_keyring_to_cred(new, NULL); |
1da177e4 LT |
723 | if (ret < 0) |
724 | goto error; | |
725 | ||
d84f4f99 DH |
726 | serial = new->tgcred->session_keyring->serial; |
727 | ret = commit_creds(new); | |
728 | if (ret == 0) | |
729 | ret = serial; | |
730 | goto okay; | |
1da177e4 LT |
731 | } |
732 | ||
733 | /* allow the user to join or create a named keyring */ | |
bb003079 | 734 | mutex_lock(&key_session_mutex); |
1da177e4 LT |
735 | |
736 | /* look for an existing keyring of this name */ | |
69664cf1 | 737 | keyring = find_keyring_by_name(name, false); |
1da177e4 LT |
738 | if (PTR_ERR(keyring) == -ENOKEY) { |
739 | /* not found - try and create a new one */ | |
d84f4f99 | 740 | keyring = keyring_alloc(name, old->uid, old->gid, old, |
7e047ef5 | 741 | KEY_ALLOC_IN_QUOTA, NULL); |
1da177e4 LT |
742 | if (IS_ERR(keyring)) { |
743 | ret = PTR_ERR(keyring); | |
bcf945d3 | 744 | goto error2; |
1da177e4 | 745 | } |
d84f4f99 | 746 | } else if (IS_ERR(keyring)) { |
1da177e4 LT |
747 | ret = PTR_ERR(keyring); |
748 | goto error2; | |
749 | } | |
750 | ||
751 | /* we've got a keyring - now to install it */ | |
d84f4f99 | 752 | ret = install_session_keyring_to_cred(new, keyring); |
1da177e4 LT |
753 | if (ret < 0) |
754 | goto error2; | |
755 | ||
d84f4f99 DH |
756 | commit_creds(new); |
757 | mutex_unlock(&key_session_mutex); | |
758 | ||
1da177e4 LT |
759 | ret = keyring->serial; |
760 | key_put(keyring); | |
d84f4f99 DH |
761 | okay: |
762 | return ret; | |
1da177e4 | 763 | |
664cceb0 | 764 | error2: |
bb003079 | 765 | mutex_unlock(&key_session_mutex); |
664cceb0 | 766 | error: |
d84f4f99 | 767 | abort_creds(new); |
1da177e4 | 768 | return ret; |
d84f4f99 | 769 | } |
ee18d64c DH |
770 | |
771 | /* | |
772 | * Replace a process's session keyring when that process resumes userspace on | |
773 | * behalf of one of its children | |
774 | */ | |
775 | void key_replace_session_keyring(void) | |
776 | { | |
777 | const struct cred *old; | |
778 | struct cred *new; | |
779 | ||
780 | if (!current->replacement_session_keyring) | |
781 | return; | |
782 | ||
783 | write_lock_irq(&tasklist_lock); | |
784 | new = current->replacement_session_keyring; | |
785 | current->replacement_session_keyring = NULL; | |
786 | write_unlock_irq(&tasklist_lock); | |
787 | ||
788 | if (!new) | |
789 | return; | |
790 | ||
791 | old = current_cred(); | |
792 | new-> uid = old-> uid; | |
793 | new-> euid = old-> euid; | |
794 | new-> suid = old-> suid; | |
795 | new->fsuid = old->fsuid; | |
796 | new-> gid = old-> gid; | |
797 | new-> egid = old-> egid; | |
798 | new-> sgid = old-> sgid; | |
799 | new->fsgid = old->fsgid; | |
800 | new->user = get_uid(old->user); | |
801 | new->group_info = get_group_info(old->group_info); | |
802 | ||
803 | new->securebits = old->securebits; | |
804 | new->cap_inheritable = old->cap_inheritable; | |
805 | new->cap_permitted = old->cap_permitted; | |
806 | new->cap_effective = old->cap_effective; | |
807 | new->cap_bset = old->cap_bset; | |
808 | ||
809 | new->jit_keyring = old->jit_keyring; | |
810 | new->thread_keyring = key_get(old->thread_keyring); | |
811 | new->tgcred->tgid = old->tgcred->tgid; | |
812 | new->tgcred->process_keyring = key_get(old->tgcred->process_keyring); | |
813 | ||
814 | security_transfer_creds(new, old); | |
815 | ||
816 | commit_creds(new); | |
817 | } |