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/keyctl.h>
17 #include <linux/err.h>
18 #include <linux/mutex.h>
19 #include <linux/security.h>
20 #include <linux/user_namespace.h>
21 #include <asm/uaccess.h>
24 /* session keyring create vs join semaphore */
25 static DEFINE_MUTEX(key_session_mutex);
27 /* user keyring creation semaphore */
28 static DEFINE_MUTEX(key_user_keyring_mutex);
30 /* the root user's tracking struct */
31 struct key_user root_key_user = {
32 .usage = ATOMIC_INIT(3),
33 .cons_lock = __MUTEX_INITIALIZER(root_key_user.cons_lock),
34 .lock = __SPIN_LOCK_UNLOCKED(root_key_user.lock),
35 .nkeys = ATOMIC_INIT(2),
36 .nikeys = ATOMIC_INIT(2),
38 .user_ns = &init_user_ns,
41 /*****************************************************************************/
43 * install user and user session keyrings for a particular UID
45 int install_user_keyrings(void)
47 struct user_struct *user;
48 const struct cred *cred;
49 struct key *uid_keyring, *session_keyring;
53 cred = current_cred();
56 kenter("%p{%u}", user, user->uid);
58 if (user->uid_keyring) {
59 kleave(" = 0 [exist]");
63 mutex_lock(&key_user_keyring_mutex);
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);
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,
76 cred, KEY_ALLOC_IN_QUOTA,
78 if (IS_ERR(uid_keyring)) {
79 ret = PTR_ERR(uid_keyring);
84 /* get a default session keyring (which might also exist
86 sprintf(buf, "_uid_ses.%u", user->uid);
88 session_keyring = find_keyring_by_name(buf, true);
89 if (IS_ERR(session_keyring)) {
91 keyring_alloc(buf, user->uid, (gid_t) -1,
92 cred, KEY_ALLOC_IN_QUOTA, NULL);
93 if (IS_ERR(session_keyring)) {
94 ret = PTR_ERR(session_keyring);
98 /* we install a link from the user session keyring to
100 ret = key_link(session_keyring, uid_keyring);
102 goto error_release_both;
105 /* install the keyrings */
106 user->uid_keyring = uid_keyring;
107 user->session_keyring = session_keyring;
110 mutex_unlock(&key_user_keyring_mutex);
115 key_put(session_keyring);
117 key_put(uid_keyring);
119 mutex_unlock(&key_user_keyring_mutex);
120 kleave(" = %d", ret);
125 * install a fresh thread keyring directly to new credentials
127 int install_thread_keyring_to_cred(struct cred *new)
131 keyring = keyring_alloc("_tid", new->uid, new->gid, new,
132 KEY_ALLOC_QUOTA_OVERRUN, NULL);
134 return PTR_ERR(keyring);
136 new->thread_keyring = keyring;
141 * install a fresh thread keyring, discarding the old one
143 static int install_thread_keyring(void)
148 new = prepare_creds();
152 BUG_ON(new->thread_keyring);
154 ret = install_thread_keyring_to_cred(new);
160 return commit_creds(new);
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
168 int install_process_keyring_to_cred(struct cred *new)
173 if (new->tgcred->process_keyring)
176 keyring = keyring_alloc("_pid", new->uid, new->gid,
177 new, KEY_ALLOC_QUOTA_OVERRUN, NULL);
179 return PTR_ERR(keyring);
181 spin_lock_irq(&new->tgcred->lock);
182 if (!new->tgcred->process_keyring) {
183 new->tgcred->process_keyring = keyring;
189 spin_unlock_irq(&new->tgcred->lock);
195 * make sure a process keyring is installed
198 static int install_process_keyring(void)
203 new = prepare_creds();
207 ret = install_process_keyring_to_cred(new);
210 return ret != -EEXIST ?: 0;
213 return commit_creds(new);
217 * install a session keyring directly to a credentials struct
219 int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
226 /* create an empty session keyring */
228 flags = KEY_ALLOC_QUOTA_OVERRUN;
229 if (cred->tgcred->session_keyring)
230 flags = KEY_ALLOC_IN_QUOTA;
232 keyring = keyring_alloc("_ses", cred->uid, cred->gid,
235 return PTR_ERR(keyring);
237 atomic_inc(&keyring->usage);
240 /* install the keyring */
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);
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 */
257 * install a session keyring, discarding the old one
258 * - if a keyring is not supplied, an empty one is invented
260 static int install_session_keyring(struct key *keyring)
265 new = prepare_creds();
269 ret = install_session_keyring_to_cred(new, NULL);
275 return commit_creds(new);
278 /*****************************************************************************/
280 * the filesystem user ID changed
282 void key_fsuid_changed(struct task_struct *tsk)
284 /* update the ownership of the thread keyring */
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);
292 } /* end key_fsuid_changed() */
294 /*****************************************************************************/
296 * the filesystem group ID changed
298 void key_fsgid_changed(struct task_struct *tsk)
300 /* update the ownership of the thread keyring */
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);
308 } /* end key_fsgid_changed() */
310 /*****************************************************************************/
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
318 key_ref_t search_process_keyrings(struct key_type *type,
319 const void *description,
320 key_match_func_t match,
321 const struct cred *cred)
323 struct request_key_auth *rka;
324 key_ref_t key_ref, ret, err;
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
333 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
337 err = ERR_PTR(-EAGAIN);
339 /* search the thread keyring first */
340 if (cred->thread_keyring) {
341 key_ref = keyring_search_aux(
342 make_key_ref(cred->thread_keyring, 1),
343 cred, type, description, match);
344 if (!IS_ERR(key_ref))
347 switch (PTR_ERR(key_ref)) {
348 case -EAGAIN: /* no key */
351 case -ENOKEY: /* negative key */
360 /* search the process keyring second */
361 if (cred->tgcred->process_keyring) {
362 key_ref = keyring_search_aux(
363 make_key_ref(cred->tgcred->process_keyring, 1),
364 cred, type, description, match);
365 if (!IS_ERR(key_ref))
368 switch (PTR_ERR(key_ref)) {
369 case -EAGAIN: /* no key */
372 case -ENOKEY: /* negative key */
381 /* search the session keyring */
382 if (cred->tgcred->session_keyring) {
384 key_ref = keyring_search_aux(
385 make_key_ref(rcu_dereference(
386 cred->tgcred->session_keyring),
388 cred, type, description, match);
391 if (!IS_ERR(key_ref))
394 switch (PTR_ERR(key_ref)) {
395 case -EAGAIN: /* no key */
398 case -ENOKEY: /* negative key */
406 /* or search the user-session keyring */
407 else if (cred->user->session_keyring) {
408 key_ref = keyring_search_aux(
409 make_key_ref(cred->user->session_keyring, 1),
410 cred, type, description, match);
411 if (!IS_ERR(key_ref))
414 switch (PTR_ERR(key_ref)) {
415 case -EAGAIN: /* no key */
418 case -ENOKEY: /* negative key */
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
431 if (cred->request_key_auth &&
432 cred == current_cred() &&
433 type != &key_type_request_key_auth
435 /* defend against the auth key being revoked */
436 down_read(&cred->request_key_auth->sem);
438 if (key_validate(cred->request_key_auth) == 0) {
439 rka = cred->request_key_auth->payload.data;
441 key_ref = search_process_keyrings(type, description,
444 up_read(&cred->request_key_auth->sem);
446 if (!IS_ERR(key_ref))
449 switch (PTR_ERR(key_ref)) {
450 case -EAGAIN: /* no key */
453 case -ENOKEY: /* negative key */
461 up_read(&cred->request_key_auth->sem);
465 /* no key - decide on the error we're going to go for */
466 key_ref = ret ? ret : err;
471 } /* end search_process_keyrings() */
473 /*****************************************************************************/
475 * see if the key we're looking at is the target key
477 static int lookup_user_key_possessed(const struct key *key, const void *target)
479 return key == target;
481 } /* end lookup_user_key_possessed() */
483 /*****************************************************************************/
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
489 key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
492 struct request_key_auth *rka;
493 const struct cred *cred;
495 key_ref_t key_ref, skey_ref;
499 cred = get_current_cred();
500 key_ref = ERR_PTR(-ENOKEY);
503 case KEY_SPEC_THREAD_KEYRING:
504 if (!cred->thread_keyring) {
505 if (!(lflags & KEY_LOOKUP_CREATE))
508 ret = install_thread_keyring();
510 key_ref = ERR_PTR(ret);
516 key = cred->thread_keyring;
517 atomic_inc(&key->usage);
518 key_ref = make_key_ref(key, 1);
521 case KEY_SPEC_PROCESS_KEYRING:
522 if (!cred->tgcred->process_keyring) {
523 if (!(lflags & KEY_LOOKUP_CREATE))
526 ret = install_process_keyring();
528 key_ref = ERR_PTR(ret);
534 key = cred->tgcred->process_keyring;
535 atomic_inc(&key->usage);
536 key_ref = make_key_ref(key, 1);
539 case KEY_SPEC_SESSION_KEYRING:
540 if (!cred->tgcred->session_keyring) {
541 /* always install a session keyring upon access if one
542 * doesn't exist yet */
543 ret = install_user_keyrings();
546 ret = install_session_keyring(
547 cred->user->session_keyring);
555 key = rcu_dereference(cred->tgcred->session_keyring);
556 atomic_inc(&key->usage);
558 key_ref = make_key_ref(key, 1);
561 case KEY_SPEC_USER_KEYRING:
562 if (!cred->user->uid_keyring) {
563 ret = install_user_keyrings();
568 key = cred->user->uid_keyring;
569 atomic_inc(&key->usage);
570 key_ref = make_key_ref(key, 1);
573 case KEY_SPEC_USER_SESSION_KEYRING:
574 if (!cred->user->session_keyring) {
575 ret = install_user_keyrings();
580 key = cred->user->session_keyring;
581 atomic_inc(&key->usage);
582 key_ref = make_key_ref(key, 1);
585 case KEY_SPEC_GROUP_KEYRING:
586 /* group keyrings are not yet supported */
587 key_ref = ERR_PTR(-EINVAL);
590 case KEY_SPEC_REQKEY_AUTH_KEY:
591 key = cred->request_key_auth;
595 atomic_inc(&key->usage);
596 key_ref = make_key_ref(key, 1);
599 case KEY_SPEC_REQUESTOR_KEYRING:
600 if (!cred->request_key_auth)
603 down_read(&cred->request_key_auth->sem);
604 if (cred->request_key_auth->flags & KEY_FLAG_REVOKED) {
605 key_ref = ERR_PTR(-EKEYREVOKED);
608 rka = cred->request_key_auth->payload.data;
609 key = rka->dest_keyring;
610 atomic_inc(&key->usage);
612 up_read(&cred->request_key_auth->sem);
615 key_ref = make_key_ref(key, 1);
619 key_ref = ERR_PTR(-EINVAL);
623 key = key_lookup(id);
625 key_ref = ERR_CAST(key);
629 key_ref = make_key_ref(key, 0);
631 /* check to see if we possess the key */
632 skey_ref = search_process_keyrings(key->type, key,
633 lookup_user_key_possessed,
636 if (!IS_ERR(skey_ref)) {
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) {
651 if (!(lflags & KEY_LOOKUP_PARTIAL)) {
652 ret = wait_for_key_construction(key, true);
663 ret = key_validate(key);
669 if (!(lflags & KEY_LOOKUP_PARTIAL) &&
670 !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
673 /* check the permissions */
674 ret = key_task_permission(key_ref, cred, perm);
683 key_ref_put(key_ref);
684 key_ref = ERR_PTR(ret);
687 /* if we attempted to install a keyring, then it may have caused new
688 * creds to be installed */
693 } /* end lookup_user_key() */
695 /*****************************************************************************/
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
702 long join_session_keyring(const char *name)
704 const struct cred *old;
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
712 if (!current_is_single_threaded())
715 new = prepare_creds();
718 old = current_cred();
720 /* if no name is provided, install an anonymous keyring */
722 ret = install_session_keyring_to_cred(new, NULL);
726 serial = new->tgcred->session_keyring->serial;
727 ret = commit_creds(new);
733 /* allow the user to join or create a named keyring */
734 mutex_lock(&key_session_mutex);
736 /* look for an existing keyring of this name */
737 keyring = find_keyring_by_name(name, false);
738 if (PTR_ERR(keyring) == -ENOKEY) {
739 /* not found - try and create a new one */
740 keyring = keyring_alloc(name, old->uid, old->gid, old,
741 KEY_ALLOC_IN_QUOTA, NULL);
742 if (IS_ERR(keyring)) {
743 ret = PTR_ERR(keyring);
746 } else if (IS_ERR(keyring)) {
747 ret = PTR_ERR(keyring);
751 /* we've got a keyring - now to install it */
752 ret = install_session_keyring_to_cred(new, keyring);
757 mutex_unlock(&key_session_mutex);
759 ret = keyring->serial;
765 mutex_unlock(&key_session_mutex);
772 * Replace a process's session keyring when that process resumes userspace on
773 * behalf of one of its children
775 void key_replace_session_keyring(void)
777 const struct cred *old;
780 if (!current->replacement_session_keyring)
783 write_lock_irq(&tasklist_lock);
784 new = current->replacement_session_keyring;
785 current->replacement_session_keyring = NULL;
786 write_unlock_irq(&tasklist_lock);
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);
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;
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);
814 security_transfer_creds(new, old);