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,
42 * install user and user session keyrings for a particular UID
44 int install_user_keyrings(void)
46 struct user_struct *user;
47 const struct cred *cred;
48 struct key *uid_keyring, *session_keyring;
52 cred = current_cred();
55 kenter("%p{%u}", user, user->uid);
57 if (user->uid_keyring) {
58 kleave(" = 0 [exist]");
62 mutex_lock(&key_user_keyring_mutex);
65 if (!user->uid_keyring) {
66 /* get the UID-specific keyring
67 * - there may be one in existence already as it may have been
68 * pinned by a session, but the user_struct pointing to it
69 * may have been destroyed by setuid */
70 sprintf(buf, "_uid.%u", user->uid);
72 uid_keyring = find_keyring_by_name(buf, true);
73 if (IS_ERR(uid_keyring)) {
74 uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1,
75 cred, KEY_ALLOC_IN_QUOTA,
77 if (IS_ERR(uid_keyring)) {
78 ret = PTR_ERR(uid_keyring);
83 /* get a default session keyring (which might also exist
85 sprintf(buf, "_uid_ses.%u", user->uid);
87 session_keyring = find_keyring_by_name(buf, true);
88 if (IS_ERR(session_keyring)) {
90 keyring_alloc(buf, user->uid, (gid_t) -1,
91 cred, KEY_ALLOC_IN_QUOTA, NULL);
92 if (IS_ERR(session_keyring)) {
93 ret = PTR_ERR(session_keyring);
97 /* we install a link from the user session keyring to
99 ret = key_link(session_keyring, uid_keyring);
101 goto error_release_both;
104 /* install the keyrings */
105 user->uid_keyring = uid_keyring;
106 user->session_keyring = session_keyring;
109 mutex_unlock(&key_user_keyring_mutex);
114 key_put(session_keyring);
116 key_put(uid_keyring);
118 mutex_unlock(&key_user_keyring_mutex);
119 kleave(" = %d", ret);
124 * install a fresh thread keyring directly to new credentials
126 int install_thread_keyring_to_cred(struct cred *new)
130 keyring = keyring_alloc("_tid", new->uid, new->gid, new,
131 KEY_ALLOC_QUOTA_OVERRUN, NULL);
133 return PTR_ERR(keyring);
135 new->thread_keyring = keyring;
140 * install a fresh thread keyring, discarding the old one
142 static int install_thread_keyring(void)
147 new = prepare_creds();
151 BUG_ON(new->thread_keyring);
153 ret = install_thread_keyring_to_cred(new);
159 return commit_creds(new);
163 * install a process keyring directly to a credentials struct
164 * - returns -EEXIST if there was already a process keyring, 0 if one installed,
165 * and other -ve on any other error
167 int install_process_keyring_to_cred(struct cred *new)
172 if (new->tgcred->process_keyring)
175 keyring = keyring_alloc("_pid", new->uid, new->gid,
176 new, KEY_ALLOC_QUOTA_OVERRUN, NULL);
178 return PTR_ERR(keyring);
180 spin_lock_irq(&new->tgcred->lock);
181 if (!new->tgcred->process_keyring) {
182 new->tgcred->process_keyring = keyring;
188 spin_unlock_irq(&new->tgcred->lock);
194 * make sure a process keyring is installed
197 static int install_process_keyring(void)
202 new = prepare_creds();
206 ret = install_process_keyring_to_cred(new);
209 return ret != -EEXIST ? ret : 0;
212 return commit_creds(new);
216 * install a session keyring directly to a credentials struct
218 int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
225 /* create an empty session keyring */
227 flags = KEY_ALLOC_QUOTA_OVERRUN;
228 if (cred->tgcred->session_keyring)
229 flags = KEY_ALLOC_IN_QUOTA;
231 keyring = keyring_alloc("_ses", cred->uid, cred->gid,
234 return PTR_ERR(keyring);
236 atomic_inc(&keyring->usage);
239 /* install the keyring */
240 spin_lock_irq(&cred->tgcred->lock);
241 old = cred->tgcred->session_keyring;
242 rcu_assign_pointer(cred->tgcred->session_keyring, keyring);
243 spin_unlock_irq(&cred->tgcred->lock);
245 /* we're using RCU on the pointer, but there's no point synchronising
246 * on it if it didn't previously point to anything */
256 * install a session keyring, discarding the old one
257 * - if a keyring is not supplied, an empty one is invented
259 static int install_session_keyring(struct key *keyring)
264 new = prepare_creds();
268 ret = install_session_keyring_to_cred(new, NULL);
274 return commit_creds(new);
278 * the filesystem user ID changed
280 void key_fsuid_changed(struct task_struct *tsk)
282 /* update the ownership of the thread keyring */
284 if (tsk->cred->thread_keyring) {
285 down_write(&tsk->cred->thread_keyring->sem);
286 tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
287 up_write(&tsk->cred->thread_keyring->sem);
292 * the filesystem group ID changed
294 void key_fsgid_changed(struct task_struct *tsk)
296 /* update the ownership of the thread keyring */
298 if (tsk->cred->thread_keyring) {
299 down_write(&tsk->cred->thread_keyring->sem);
300 tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
301 up_write(&tsk->cred->thread_keyring->sem);
306 * search only my process keyrings for the first matching key
307 * - we use the supplied match function to see if the description (or other
308 * feature of interest) matches
309 * - we return -EAGAIN if we didn't find any matching key
310 * - we return -ENOKEY if we found only negative matching keys
312 key_ref_t search_my_process_keyrings(struct key_type *type,
313 const void *description,
314 key_match_func_t match,
315 const struct cred *cred)
317 key_ref_t key_ref, ret, err;
319 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
320 * searchable, but we failed to find a key or we found a negative key;
321 * otherwise we want to return a sample error (probably -EACCES) if
322 * none of the keyrings were searchable
324 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
328 err = ERR_PTR(-EAGAIN);
330 /* search the thread keyring first */
331 if (cred->thread_keyring) {
332 key_ref = keyring_search_aux(
333 make_key_ref(cred->thread_keyring, 1),
334 cred, type, description, match);
335 if (!IS_ERR(key_ref))
338 switch (PTR_ERR(key_ref)) {
339 case -EAGAIN: /* no key */
342 case -ENOKEY: /* negative key */
351 /* search the process keyring second */
352 if (cred->tgcred->process_keyring) {
353 key_ref = keyring_search_aux(
354 make_key_ref(cred->tgcred->process_keyring, 1),
355 cred, type, description, match);
356 if (!IS_ERR(key_ref))
359 switch (PTR_ERR(key_ref)) {
360 case -EAGAIN: /* no key */
363 case -ENOKEY: /* negative key */
372 /* search the session keyring */
373 if (cred->tgcred->session_keyring) {
375 key_ref = keyring_search_aux(
376 make_key_ref(rcu_dereference(
377 cred->tgcred->session_keyring),
379 cred, type, description, match);
382 if (!IS_ERR(key_ref))
385 switch (PTR_ERR(key_ref)) {
386 case -EAGAIN: /* no key */
389 case -ENOKEY: /* negative key */
397 /* or search the user-session keyring */
398 else if (cred->user->session_keyring) {
399 key_ref = keyring_search_aux(
400 make_key_ref(cred->user->session_keyring, 1),
401 cred, type, description, match);
402 if (!IS_ERR(key_ref))
405 switch (PTR_ERR(key_ref)) {
406 case -EAGAIN: /* no key */
409 case -ENOKEY: /* negative key */
418 /* no key - decide on the error we're going to go for */
419 key_ref = ret ? ret : err;
426 * search the process keyrings for the first matching key
427 * - we use the supplied match function to see if the description (or other
428 * feature of interest) matches
429 * - we return -EAGAIN if we didn't find any matching key
430 * - we return -ENOKEY if we found only negative matching keys
432 key_ref_t search_process_keyrings(struct key_type *type,
433 const void *description,
434 key_match_func_t match,
435 const struct cred *cred)
437 struct request_key_auth *rka;
438 key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
442 key_ref = search_my_process_keyrings(type, description, match, cred);
443 if (!IS_ERR(key_ref))
447 /* if this process has an instantiation authorisation key, then we also
448 * search the keyrings of the process mentioned there
449 * - we don't permit access to request_key auth keys via this method
451 if (cred->request_key_auth &&
452 cred == current_cred() &&
453 type != &key_type_request_key_auth
455 /* defend against the auth key being revoked */
456 down_read(&cred->request_key_auth->sem);
458 if (key_validate(cred->request_key_auth) == 0) {
459 rka = cred->request_key_auth->payload.data;
461 key_ref = search_process_keyrings(type, description,
464 up_read(&cred->request_key_auth->sem);
466 if (!IS_ERR(key_ref))
471 up_read(&cred->request_key_auth->sem);
475 /* no key - decide on the error we're going to go for */
476 if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY))
477 key_ref = ERR_PTR(-ENOKEY);
478 else if (err == ERR_PTR(-EACCES))
488 * see if the key we're looking at is the target key
490 int lookup_user_key_possessed(const struct key *key, const void *target)
492 return key == target;
496 * lookup a key given a key ID from userspace with a given permissions mask
497 * - don't create special keyrings unless so requested
498 * - partially constructed keys aren't found unless requested
500 key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
503 struct request_key_auth *rka;
504 const struct cred *cred;
506 key_ref_t key_ref, skey_ref;
510 cred = get_current_cred();
511 key_ref = ERR_PTR(-ENOKEY);
514 case KEY_SPEC_THREAD_KEYRING:
515 if (!cred->thread_keyring) {
516 if (!(lflags & KEY_LOOKUP_CREATE))
519 ret = install_thread_keyring();
521 key_ref = ERR_PTR(ret);
527 key = cred->thread_keyring;
528 atomic_inc(&key->usage);
529 key_ref = make_key_ref(key, 1);
532 case KEY_SPEC_PROCESS_KEYRING:
533 if (!cred->tgcred->process_keyring) {
534 if (!(lflags & KEY_LOOKUP_CREATE))
537 ret = install_process_keyring();
539 key_ref = ERR_PTR(ret);
545 key = cred->tgcred->process_keyring;
546 atomic_inc(&key->usage);
547 key_ref = make_key_ref(key, 1);
550 case KEY_SPEC_SESSION_KEYRING:
551 if (!cred->tgcred->session_keyring) {
552 /* always install a session keyring upon access if one
553 * doesn't exist yet */
554 ret = install_user_keyrings();
557 ret = install_session_keyring(
558 cred->user->session_keyring);
566 key = rcu_dereference(cred->tgcred->session_keyring);
567 atomic_inc(&key->usage);
569 key_ref = make_key_ref(key, 1);
572 case KEY_SPEC_USER_KEYRING:
573 if (!cred->user->uid_keyring) {
574 ret = install_user_keyrings();
579 key = cred->user->uid_keyring;
580 atomic_inc(&key->usage);
581 key_ref = make_key_ref(key, 1);
584 case KEY_SPEC_USER_SESSION_KEYRING:
585 if (!cred->user->session_keyring) {
586 ret = install_user_keyrings();
591 key = cred->user->session_keyring;
592 atomic_inc(&key->usage);
593 key_ref = make_key_ref(key, 1);
596 case KEY_SPEC_GROUP_KEYRING:
597 /* group keyrings are not yet supported */
598 key_ref = ERR_PTR(-EINVAL);
601 case KEY_SPEC_REQKEY_AUTH_KEY:
602 key = cred->request_key_auth;
606 atomic_inc(&key->usage);
607 key_ref = make_key_ref(key, 1);
610 case KEY_SPEC_REQUESTOR_KEYRING:
611 if (!cred->request_key_auth)
614 down_read(&cred->request_key_auth->sem);
615 if (cred->request_key_auth->flags & KEY_FLAG_REVOKED) {
616 key_ref = ERR_PTR(-EKEYREVOKED);
619 rka = cred->request_key_auth->payload.data;
620 key = rka->dest_keyring;
621 atomic_inc(&key->usage);
623 up_read(&cred->request_key_auth->sem);
626 key_ref = make_key_ref(key, 1);
630 key_ref = ERR_PTR(-EINVAL);
634 key = key_lookup(id);
636 key_ref = ERR_CAST(key);
640 key_ref = make_key_ref(key, 0);
642 /* check to see if we possess the key */
643 skey_ref = search_process_keyrings(key->type, key,
644 lookup_user_key_possessed,
647 if (!IS_ERR(skey_ref)) {
655 /* unlink does not use the nominated key in any way, so can skip all
656 * the permission checks as it is only concerned with the keyring */
657 if (lflags & KEY_LOOKUP_FOR_UNLINK) {
662 if (!(lflags & KEY_LOOKUP_PARTIAL)) {
663 ret = wait_for_key_construction(key, true);
674 ret = key_validate(key);
680 if (!(lflags & KEY_LOOKUP_PARTIAL) &&
681 !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
684 /* check the permissions */
685 ret = key_task_permission(key_ref, cred, perm);
694 key_ref_put(key_ref);
695 key_ref = ERR_PTR(ret);
698 /* if we attempted to install a keyring, then it may have caused new
699 * creds to be installed */
706 * join the named keyring as the session keyring if possible, or attempt to
707 * create a new one of that name if not
708 * - if the name is NULL, an empty anonymous keyring is installed instead
709 * - named session keyring joining is done with a semaphore held
711 long join_session_keyring(const char *name)
713 const struct cred *old;
718 /* only permit this if there's a single thread in the thread group -
719 * this avoids us having to adjust the creds on all threads and risking
721 if (!current_is_single_threaded())
724 new = prepare_creds();
727 old = current_cred();
729 /* if no name is provided, install an anonymous keyring */
731 ret = install_session_keyring_to_cred(new, NULL);
735 serial = new->tgcred->session_keyring->serial;
736 ret = commit_creds(new);
742 /* allow the user to join or create a named keyring */
743 mutex_lock(&key_session_mutex);
745 /* look for an existing keyring of this name */
746 keyring = find_keyring_by_name(name, false);
747 if (PTR_ERR(keyring) == -ENOKEY) {
748 /* not found - try and create a new one */
749 keyring = keyring_alloc(name, old->uid, old->gid, old,
750 KEY_ALLOC_IN_QUOTA, NULL);
751 if (IS_ERR(keyring)) {
752 ret = PTR_ERR(keyring);
755 } else if (IS_ERR(keyring)) {
756 ret = PTR_ERR(keyring);
760 /* we've got a keyring - now to install it */
761 ret = install_session_keyring_to_cred(new, keyring);
766 mutex_unlock(&key_session_mutex);
768 ret = keyring->serial;
774 mutex_unlock(&key_session_mutex);
781 * Replace a process's session keyring when that process resumes userspace on
782 * behalf of one of its children
784 void key_replace_session_keyring(void)
786 const struct cred *old;
789 if (!current->replacement_session_keyring)
792 write_lock_irq(&tasklist_lock);
793 new = current->replacement_session_keyring;
794 current->replacement_session_keyring = NULL;
795 write_unlock_irq(&tasklist_lock);
800 old = current_cred();
801 new-> uid = old-> uid;
802 new-> euid = old-> euid;
803 new-> suid = old-> suid;
804 new->fsuid = old->fsuid;
805 new-> gid = old-> gid;
806 new-> egid = old-> egid;
807 new-> sgid = old-> sgid;
808 new->fsgid = old->fsgid;
809 new->user = get_uid(old->user);
810 new->group_info = get_group_info(old->group_info);
812 new->securebits = old->securebits;
813 new->cap_inheritable = old->cap_inheritable;
814 new->cap_permitted = old->cap_permitted;
815 new->cap_effective = old->cap_effective;
816 new->cap_bset = old->cap_bset;
818 new->jit_keyring = old->jit_keyring;
819 new->thread_keyring = key_get(old->thread_keyring);
820 new->tgcred->tgid = old->tgcred->tgid;
821 new->tgcred->process_keyring = key_get(old->tgcred->process_keyring);
823 security_transfer_creds(new, old);