1 /* Management of a process's keyrings
3 * Copyright (C) 2004-2005, 2008 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
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>
18 #include <linux/err.h>
19 #include <linux/mutex.h>
20 #include <asm/uaccess.h>
23 /* session keyring create vs join semaphore */
24 static DEFINE_MUTEX(key_session_mutex);
26 /* user keyring creation semaphore */
27 static DEFINE_MUTEX(key_user_keyring_mutex);
29 /* the root user's tracking struct */
30 struct key_user root_key_user = {
31 .usage = ATOMIC_INIT(3),
32 .cons_lock = __MUTEX_INITIALIZER(root_key_user.cons_lock),
33 .lock = __SPIN_LOCK_UNLOCKED(root_key_user.lock),
34 .nkeys = ATOMIC_INIT(2),
35 .nikeys = ATOMIC_INIT(2),
39 /*****************************************************************************/
41 * install user and user session keyrings for a particular UID
43 int install_user_keyrings(void)
45 struct user_struct *user = current->cred->user;
46 struct key *uid_keyring, *session_keyring;
50 kenter("%p{%u}", user, user->uid);
52 if (user->uid_keyring) {
53 kleave(" = 0 [exist]");
57 mutex_lock(&key_user_keyring_mutex);
60 if (!user->uid_keyring) {
61 /* get the UID-specific keyring
62 * - there may be one in existence already as it may have been
63 * pinned by a session, but the user_struct pointing to it
64 * may have been destroyed by setuid */
65 sprintf(buf, "_uid.%u", user->uid);
67 uid_keyring = find_keyring_by_name(buf, true);
68 if (IS_ERR(uid_keyring)) {
69 uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1,
70 current, KEY_ALLOC_IN_QUOTA,
72 if (IS_ERR(uid_keyring)) {
73 ret = PTR_ERR(uid_keyring);
78 /* get a default session keyring (which might also exist
80 sprintf(buf, "_uid_ses.%u", user->uid);
82 session_keyring = find_keyring_by_name(buf, true);
83 if (IS_ERR(session_keyring)) {
85 keyring_alloc(buf, user->uid, (gid_t) -1,
86 current, KEY_ALLOC_IN_QUOTA,
88 if (IS_ERR(session_keyring)) {
89 ret = PTR_ERR(session_keyring);
93 /* we install a link from the user session keyring to
95 ret = key_link(session_keyring, uid_keyring);
97 goto error_release_both;
100 /* install the keyrings */
101 user->uid_keyring = uid_keyring;
102 user->session_keyring = session_keyring;
105 mutex_unlock(&key_user_keyring_mutex);
110 key_put(session_keyring);
112 key_put(uid_keyring);
114 mutex_unlock(&key_user_keyring_mutex);
115 kleave(" = %d", ret);
119 /*****************************************************************************/
121 * deal with the UID changing
123 void switch_uid_keyring(struct user_struct *new_user)
125 #if 0 /* do nothing for now */
128 /* switch to the new user's session keyring if we were running under
129 * root's default session keyring */
130 if (new_user->uid != 0 &&
131 current->session_keyring == &root_session_keyring
133 atomic_inc(&new_user->session_keyring->usage);
136 old = current->session_keyring;
137 current->session_keyring = new_user->session_keyring;
138 task_unlock(current);
144 } /* end switch_uid_keyring() */
146 /*****************************************************************************/
148 * install a fresh thread keyring, discarding the old one
150 int install_thread_keyring(void)
152 struct task_struct *tsk = current;
153 struct key *keyring, *old;
157 sprintf(buf, "_tid.%u", tsk->pid);
159 keyring = keyring_alloc(buf, tsk->cred->uid, tsk->cred->gid, tsk,
160 KEY_ALLOC_QUOTA_OVERRUN, NULL);
161 if (IS_ERR(keyring)) {
162 ret = PTR_ERR(keyring);
167 old = tsk->cred->thread_keyring;
168 tsk->cred->thread_keyring = keyring;
177 } /* end install_thread_keyring() */
179 /*****************************************************************************/
181 * make sure a process keyring is installed
183 int install_process_keyring(void)
185 struct task_struct *tsk = current;
192 if (!tsk->signal->process_keyring) {
193 sprintf(buf, "_pid.%u", tsk->tgid);
195 keyring = keyring_alloc(buf, tsk->cred->uid, tsk->cred->gid, tsk,
196 KEY_ALLOC_QUOTA_OVERRUN, NULL);
197 if (IS_ERR(keyring)) {
198 ret = PTR_ERR(keyring);
203 spin_lock_irq(&tsk->sighand->siglock);
204 if (!tsk->signal->process_keyring) {
205 tsk->signal->process_keyring = keyring;
208 spin_unlock_irq(&tsk->sighand->siglock);
217 } /* end install_process_keyring() */
219 /*****************************************************************************/
221 * install a session keyring, discarding the old one
222 * - if a keyring is not supplied, an empty one is invented
224 static int install_session_keyring(struct key *keyring)
226 struct task_struct *tsk = current;
233 /* create an empty session keyring */
235 sprintf(buf, "_ses.%u", tsk->tgid);
237 flags = KEY_ALLOC_QUOTA_OVERRUN;
238 if (tsk->signal->session_keyring)
239 flags = KEY_ALLOC_IN_QUOTA;
241 keyring = keyring_alloc(buf, tsk->cred->uid, tsk->cred->gid, tsk,
244 return PTR_ERR(keyring);
247 atomic_inc(&keyring->usage);
250 /* install the keyring */
251 spin_lock_irq(&tsk->sighand->siglock);
252 old = tsk->signal->session_keyring;
253 rcu_assign_pointer(tsk->signal->session_keyring, keyring);
254 spin_unlock_irq(&tsk->sighand->siglock);
256 /* we're using RCU on the pointer, but there's no point synchronising
257 * on it if it didn't previously point to anything */
265 } /* end install_session_keyring() */
267 /*****************************************************************************/
269 * copy the keys in a thread group for fork without CLONE_THREAD
271 int copy_thread_group_keys(struct task_struct *tsk)
273 key_check(current->thread_group->session_keyring);
274 key_check(current->thread_group->process_keyring);
276 /* no process keyring yet */
277 tsk->signal->process_keyring = NULL;
279 /* same session keyring */
281 tsk->signal->session_keyring =
282 key_get(rcu_dereference(current->signal->session_keyring));
287 } /* end copy_thread_group_keys() */
289 /*****************************************************************************/
291 * copy the keys for fork
293 int copy_keys(unsigned long clone_flags, struct task_struct *tsk)
295 key_check(tsk->cred->thread_keyring);
296 key_check(tsk->cred->request_key_auth);
298 /* no thread keyring yet */
299 tsk->cred->thread_keyring = NULL;
301 /* copy the request_key() authorisation for this thread */
302 key_get(tsk->cred->request_key_auth);
306 } /* end copy_keys() */
308 /*****************************************************************************/
310 * dispose of thread group keys upon thread group destruction
312 void exit_thread_group_keys(struct signal_struct *tg)
314 key_put(tg->session_keyring);
315 key_put(tg->process_keyring);
317 } /* end exit_thread_group_keys() */
319 /*****************************************************************************/
321 * dispose of per-thread keys upon thread exit
323 void exit_keys(struct task_struct *tsk)
325 key_put(tsk->cred->thread_keyring);
326 key_put(tsk->cred->request_key_auth);
328 } /* end exit_keys() */
330 /*****************************************************************************/
334 int exec_keys(struct task_struct *tsk)
338 /* newly exec'd tasks don't get a thread keyring */
340 old = tsk->cred->thread_keyring;
341 tsk->cred->thread_keyring = NULL;
346 /* discard the process keyring from a newly exec'd task */
347 spin_lock_irq(&tsk->sighand->siglock);
348 old = tsk->signal->process_keyring;
349 tsk->signal->process_keyring = NULL;
350 spin_unlock_irq(&tsk->sighand->siglock);
356 } /* end exec_keys() */
358 /*****************************************************************************/
360 * deal with SUID programs
361 * - we might want to make this invent a new session keyring
363 int suid_keys(struct task_struct *tsk)
367 } /* end suid_keys() */
369 /*****************************************************************************/
371 * the filesystem user ID changed
373 void key_fsuid_changed(struct task_struct *tsk)
375 /* update the ownership of the thread keyring */
377 if (tsk->cred->thread_keyring) {
378 down_write(&tsk->cred->thread_keyring->sem);
379 tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
380 up_write(&tsk->cred->thread_keyring->sem);
383 } /* end key_fsuid_changed() */
385 /*****************************************************************************/
387 * the filesystem group ID changed
389 void key_fsgid_changed(struct task_struct *tsk)
391 /* update the ownership of the thread keyring */
393 if (tsk->cred->thread_keyring) {
394 down_write(&tsk->cred->thread_keyring->sem);
395 tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
396 up_write(&tsk->cred->thread_keyring->sem);
399 } /* end key_fsgid_changed() */
401 /*****************************************************************************/
403 * search the process keyrings for the first matching key
404 * - we use the supplied match function to see if the description (or other
405 * feature of interest) matches
406 * - we return -EAGAIN if we didn't find any matching key
407 * - we return -ENOKEY if we found only negative matching keys
409 key_ref_t search_process_keyrings(struct key_type *type,
410 const void *description,
411 key_match_func_t match,
412 struct task_struct *context)
414 struct request_key_auth *rka;
416 key_ref_t key_ref, ret, err;
420 cred = get_task_cred(context);
422 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
423 * searchable, but we failed to find a key or we found a negative key;
424 * otherwise we want to return a sample error (probably -EACCES) if
425 * none of the keyrings were searchable
427 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
431 err = ERR_PTR(-EAGAIN);
433 /* search the thread keyring first */
434 if (cred->thread_keyring) {
435 key_ref = keyring_search_aux(
436 make_key_ref(cred->thread_keyring, 1),
437 context, type, description, match);
438 if (!IS_ERR(key_ref))
441 switch (PTR_ERR(key_ref)) {
442 case -EAGAIN: /* no key */
445 case -ENOKEY: /* negative key */
454 /* search the process keyring second */
455 if (context->signal->process_keyring) {
456 key_ref = keyring_search_aux(
457 make_key_ref(context->signal->process_keyring, 1),
458 context, type, description, match);
459 if (!IS_ERR(key_ref))
462 switch (PTR_ERR(key_ref)) {
463 case -EAGAIN: /* no key */
466 case -ENOKEY: /* negative key */
475 /* search the session keyring */
476 if (context->signal->session_keyring) {
478 key_ref = keyring_search_aux(
479 make_key_ref(rcu_dereference(
480 context->signal->session_keyring),
482 context, type, description, match);
485 if (!IS_ERR(key_ref))
488 switch (PTR_ERR(key_ref)) {
489 case -EAGAIN: /* no key */
492 case -ENOKEY: /* negative key */
500 /* or search the user-session keyring */
501 else if (cred->user->session_keyring) {
502 key_ref = keyring_search_aux(
503 make_key_ref(cred->user->session_keyring, 1),
504 context, type, description, match);
505 if (!IS_ERR(key_ref))
508 switch (PTR_ERR(key_ref)) {
509 case -EAGAIN: /* no key */
512 case -ENOKEY: /* negative key */
521 /* if this process has an instantiation authorisation key, then we also
522 * search the keyrings of the process mentioned there
523 * - we don't permit access to request_key auth keys via this method
525 if (cred->request_key_auth &&
526 context == current &&
527 type != &key_type_request_key_auth
529 /* defend against the auth key being revoked */
530 down_read(&cred->request_key_auth->sem);
532 if (key_validate(cred->request_key_auth) == 0) {
533 rka = cred->request_key_auth->payload.data;
535 key_ref = search_process_keyrings(type, description,
536 match, rka->context);
538 up_read(&cred->request_key_auth->sem);
540 if (!IS_ERR(key_ref))
543 switch (PTR_ERR(key_ref)) {
544 case -EAGAIN: /* no key */
547 case -ENOKEY: /* negative key */
555 up_read(&cred->request_key_auth->sem);
559 /* no key - decide on the error we're going to go for */
560 key_ref = ret ? ret : err;
566 } /* end search_process_keyrings() */
568 /*****************************************************************************/
570 * see if the key we're looking at is the target key
572 static int lookup_user_key_possessed(const struct key *key, const void *target)
574 return key == target;
576 } /* end lookup_user_key_possessed() */
578 /*****************************************************************************/
580 * lookup a key given a key ID from userspace with a given permissions mask
581 * - don't create special keyrings unless so requested
582 * - partially constructed keys aren't found unless requested
584 key_ref_t lookup_user_key(key_serial_t id, int create, int partial,
587 struct request_key_auth *rka;
588 struct task_struct *t = current;
589 struct cred *cred = current_cred();
591 key_ref_t key_ref, skey_ref;
594 key_ref = ERR_PTR(-ENOKEY);
597 case KEY_SPEC_THREAD_KEYRING:
598 if (!cred->thread_keyring) {
602 ret = install_thread_keyring();
609 key = cred->thread_keyring;
610 atomic_inc(&key->usage);
611 key_ref = make_key_ref(key, 1);
614 case KEY_SPEC_PROCESS_KEYRING:
615 if (!t->signal->process_keyring) {
619 ret = install_process_keyring();
626 key = t->signal->process_keyring;
627 atomic_inc(&key->usage);
628 key_ref = make_key_ref(key, 1);
631 case KEY_SPEC_SESSION_KEYRING:
632 if (!t->signal->session_keyring) {
633 /* always install a session keyring upon access if one
634 * doesn't exist yet */
635 ret = install_user_keyrings();
638 ret = install_session_keyring(
639 cred->user->session_keyring);
645 key = rcu_dereference(t->signal->session_keyring);
646 atomic_inc(&key->usage);
648 key_ref = make_key_ref(key, 1);
651 case KEY_SPEC_USER_KEYRING:
652 if (!cred->user->uid_keyring) {
653 ret = install_user_keyrings();
658 key = cred->user->uid_keyring;
659 atomic_inc(&key->usage);
660 key_ref = make_key_ref(key, 1);
663 case KEY_SPEC_USER_SESSION_KEYRING:
664 if (!cred->user->session_keyring) {
665 ret = install_user_keyrings();
670 key = cred->user->session_keyring;
671 atomic_inc(&key->usage);
672 key_ref = make_key_ref(key, 1);
675 case KEY_SPEC_GROUP_KEYRING:
676 /* group keyrings are not yet supported */
677 key = ERR_PTR(-EINVAL);
680 case KEY_SPEC_REQKEY_AUTH_KEY:
681 key = cred->request_key_auth;
685 atomic_inc(&key->usage);
686 key_ref = make_key_ref(key, 1);
689 case KEY_SPEC_REQUESTOR_KEYRING:
690 if (!cred->request_key_auth)
693 down_read(&cred->request_key_auth->sem);
694 if (cred->request_key_auth->flags & KEY_FLAG_REVOKED) {
695 key_ref = ERR_PTR(-EKEYREVOKED);
698 rka = cred->request_key_auth->payload.data;
699 key = rka->dest_keyring;
700 atomic_inc(&key->usage);
702 up_read(&cred->request_key_auth->sem);
705 key_ref = make_key_ref(key, 1);
709 key_ref = ERR_PTR(-EINVAL);
713 key = key_lookup(id);
715 key_ref = ERR_CAST(key);
719 key_ref = make_key_ref(key, 0);
721 /* check to see if we possess the key */
722 skey_ref = search_process_keyrings(key->type, key,
723 lookup_user_key_possessed,
726 if (!IS_ERR(skey_ref)) {
735 ret = wait_for_key_construction(key, true);
746 ret = key_validate(key);
752 if (!partial && !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
755 /* check the permissions */
756 ret = key_task_permission(key_ref, t, perm);
764 key_ref_put(key_ref);
765 key_ref = ERR_PTR(ret);
768 } /* end lookup_user_key() */
770 /*****************************************************************************/
772 * join the named keyring as the session keyring if possible, or attempt to
773 * create a new one of that name if not
774 * - if the name is NULL, an empty anonymous keyring is installed instead
775 * - named session keyring joining is done with a semaphore held
777 long join_session_keyring(const char *name)
779 struct task_struct *tsk = current;
783 /* if no name is provided, install an anonymous keyring */
785 ret = install_session_keyring(NULL);
790 ret = rcu_dereference(tsk->signal->session_keyring)->serial;
795 /* allow the user to join or create a named keyring */
796 mutex_lock(&key_session_mutex);
798 /* look for an existing keyring of this name */
799 keyring = find_keyring_by_name(name, false);
800 if (PTR_ERR(keyring) == -ENOKEY) {
801 /* not found - try and create a new one */
802 keyring = keyring_alloc(name, tsk->cred->uid, tsk->cred->gid, tsk,
803 KEY_ALLOC_IN_QUOTA, NULL);
804 if (IS_ERR(keyring)) {
805 ret = PTR_ERR(keyring);
809 else if (IS_ERR(keyring)) {
810 ret = PTR_ERR(keyring);
814 /* we've got a keyring - now to install it */
815 ret = install_session_keyring(keyring);
819 ret = keyring->serial;
823 mutex_unlock(&key_session_mutex);
827 } /* end join_session_keyring() */