1 /* Manage 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 the user and user session keyrings for the current process's 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 && user->session_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 thread keyring to the given credentials struct if it didn't have
125 * one already. This is allowed to overrun the quota.
127 * Return: 0 if a thread keyring is now present; -errno on failure.
129 int install_thread_keyring_to_cred(struct cred *new)
133 if (new->thread_keyring)
136 keyring = keyring_alloc("_tid", new->uid, new->gid, new,
137 KEY_ALLOC_QUOTA_OVERRUN, NULL);
139 return PTR_ERR(keyring);
141 new->thread_keyring = keyring;
146 * Install a thread keyring to the current task if it didn't have one already.
148 * Return: 0 if a thread keyring is now present; -errno on failure.
150 static int install_thread_keyring(void)
155 new = prepare_creds();
159 ret = install_thread_keyring_to_cred(new);
165 return commit_creds(new);
169 * Install a process keyring to the given credentials struct if it didn't have
170 * one already. This is allowed to overrun the quota.
172 * Return: 0 if a process keyring is now present; -errno on failure.
174 int install_process_keyring_to_cred(struct cred *new)
179 if (new->tgcred->process_keyring)
182 keyring = keyring_alloc("_pid", new->uid, new->gid,
183 new, KEY_ALLOC_QUOTA_OVERRUN, NULL);
185 return PTR_ERR(keyring);
187 spin_lock_irq(&new->tgcred->lock);
188 if (!new->tgcred->process_keyring) {
189 new->tgcred->process_keyring = keyring;
195 spin_unlock_irq(&new->tgcred->lock);
201 * Install a process keyring to the current task if it didn't have one already.
203 * Return: 0 if a process keyring is now present; -errno on failure.
205 static int install_process_keyring(void)
210 new = prepare_creds();
214 ret = install_process_keyring_to_cred(new);
220 return commit_creds(new);
224 * Install the given keyring as the session keyring of the given credentials
225 * struct, replacing the existing one if any. If the given keyring is NULL,
226 * then install a new anonymous session keyring.
228 * Return: 0 on success; -errno on failure.
230 int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
237 /* create an empty session keyring */
239 flags = KEY_ALLOC_QUOTA_OVERRUN;
240 if (cred->tgcred->session_keyring)
241 flags = KEY_ALLOC_IN_QUOTA;
243 keyring = keyring_alloc("_ses", cred->uid, cred->gid,
246 return PTR_ERR(keyring);
248 atomic_inc(&keyring->usage);
251 /* install the keyring */
252 spin_lock_irq(&cred->tgcred->lock);
253 old = cred->tgcred->session_keyring;
254 rcu_assign_pointer(cred->tgcred->session_keyring, keyring);
255 spin_unlock_irq(&cred->tgcred->lock);
257 /* we're using RCU on the pointer, but there's no point synchronising
258 * on it if it didn't previously point to anything */
268 * Install the given keyring as the session keyring of the current task,
269 * replacing the existing one if any. If the given keyring is NULL, then
270 * install a new anonymous session keyring.
272 * Return: 0 on success; -errno on failure.
274 static int install_session_keyring(struct key *keyring)
279 new = prepare_creds();
283 ret = install_session_keyring_to_cred(new, keyring);
289 return commit_creds(new);
293 * Handle the fsuid changing.
295 void key_fsuid_changed(struct task_struct *tsk)
297 /* update the ownership of the thread keyring */
299 if (tsk->cred->thread_keyring) {
300 down_write(&tsk->cred->thread_keyring->sem);
301 tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
302 up_write(&tsk->cred->thread_keyring->sem);
307 * Handle the fsgid changing.
309 void key_fsgid_changed(struct task_struct *tsk)
311 /* update the ownership of the thread keyring */
313 if (tsk->cred->thread_keyring) {
314 down_write(&tsk->cred->thread_keyring->sem);
315 tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
316 up_write(&tsk->cred->thread_keyring->sem);
321 * Search the process keyrings attached to the supplied cred for the first
324 * The search criteria are the type and the match function. The description is
325 * given to the match function as a parameter, but doesn't otherwise influence
326 * the search. Typically the match function will compare the description
327 * parameter to the key's description.
329 * This can only search keyrings that grant Search permission to the supplied
330 * credentials. Keyrings linked to searched keyrings will also be searched if
331 * they grant Search permission too. Keys can only be found if they grant
332 * Search permission to the credentials.
334 * Returns a pointer to the key with the key usage count incremented if
335 * successful, -EAGAIN if we didn't find any matching key or -ENOKEY if we only
336 * matched negative keys.
338 * In the case of a successful return, the possession attribute is set on the
339 * returned key reference.
341 key_ref_t search_my_process_keyrings(struct key_type *type,
342 const void *description,
343 key_match_func_t match,
345 const struct cred *cred)
347 key_ref_t key_ref, ret, err;
349 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
350 * searchable, but we failed to find a key or we found a negative key;
351 * otherwise we want to return a sample error (probably -EACCES) if
352 * none of the keyrings were searchable
354 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
358 err = ERR_PTR(-EAGAIN);
360 /* search the thread keyring first */
361 if (cred->thread_keyring) {
362 key_ref = keyring_search_aux(
363 make_key_ref(cred->thread_keyring, 1),
364 cred, type, description, match, no_state_check);
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 process keyring second */
382 if (cred->tgcred->process_keyring) {
383 key_ref = keyring_search_aux(
384 make_key_ref(cred->tgcred->process_keyring, 1),
385 cred, type, description, match, no_state_check);
386 if (!IS_ERR(key_ref))
389 switch (PTR_ERR(key_ref)) {
390 case -EAGAIN: /* no key */
393 case -ENOKEY: /* negative key */
402 /* search the session keyring */
403 if (cred->tgcred->session_keyring) {
405 key_ref = keyring_search_aux(
406 make_key_ref(rcu_dereference(
407 cred->tgcred->session_keyring),
409 cred, type, description, match, no_state_check);
412 if (!IS_ERR(key_ref))
415 switch (PTR_ERR(key_ref)) {
416 case -EAGAIN: /* no key */
419 case -ENOKEY: /* negative key */
427 /* or search the user-session keyring */
428 else if (cred->user->session_keyring) {
429 key_ref = keyring_search_aux(
430 make_key_ref(cred->user->session_keyring, 1),
431 cred, type, description, match, no_state_check);
432 if (!IS_ERR(key_ref))
435 switch (PTR_ERR(key_ref)) {
436 case -EAGAIN: /* no key */
439 case -ENOKEY: /* negative key */
448 /* no key - decide on the error we're going to go for */
449 key_ref = ret ? ret : err;
456 * Search the process keyrings attached to the supplied cred for the first
457 * matching key in the manner of search_my_process_keyrings(), but also search
458 * the keys attached to the assumed authorisation key using its credentials if
461 * Return same as search_my_process_keyrings().
463 key_ref_t search_process_keyrings(struct key_type *type,
464 const void *description,
465 key_match_func_t match,
466 const struct cred *cred)
468 struct request_key_auth *rka;
469 key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
473 key_ref = search_my_process_keyrings(type, description, match,
475 if (!IS_ERR(key_ref))
479 /* if this process has an instantiation authorisation key, then we also
480 * search the keyrings of the process mentioned there
481 * - we don't permit access to request_key auth keys via this method
483 if (cred->request_key_auth &&
484 cred == current_cred() &&
485 type != &key_type_request_key_auth
487 /* defend against the auth key being revoked */
488 down_read(&cred->request_key_auth->sem);
490 if (key_validate(cred->request_key_auth) == 0) {
491 rka = cred->request_key_auth->payload.data;
493 key_ref = search_process_keyrings(type, description,
496 up_read(&cred->request_key_auth->sem);
498 if (!IS_ERR(key_ref))
503 up_read(&cred->request_key_auth->sem);
507 /* no key - decide on the error we're going to go for */
508 if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY))
509 key_ref = ERR_PTR(-ENOKEY);
510 else if (err == ERR_PTR(-EACCES))
520 * See if the key we're looking at is the target key.
522 int lookup_user_key_possessed(const struct key *key, const void *target)
524 return key == target;
528 * Look up a key ID given us by userspace with a given permissions mask to get
529 * the key it refers to.
531 * Flags can be passed to request that special keyrings be created if referred
532 * to directly, to permit partially constructed keys to be found and to skip
533 * validity and permission checks on the found key.
535 * Returns a pointer to the key with an incremented usage count if successful;
536 * -EINVAL if the key ID is invalid; -ENOKEY if the key ID does not correspond
537 * to a key or the best found key was a negative key; -EKEYREVOKED or
538 * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the
539 * found key doesn't grant the requested permit or the LSM denied access to it;
540 * or -ENOMEM if a special keyring couldn't be created.
542 * In the case of a successful return, the possession attribute is set on the
543 * returned key reference.
545 key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
548 struct request_key_auth *rka;
549 const struct cred *cred;
551 key_ref_t key_ref, skey_ref;
555 cred = get_current_cred();
556 key_ref = ERR_PTR(-ENOKEY);
559 case KEY_SPEC_THREAD_KEYRING:
560 if (!cred->thread_keyring) {
561 if (!(lflags & KEY_LOOKUP_CREATE))
564 ret = install_thread_keyring();
566 key_ref = ERR_PTR(ret);
572 key = cred->thread_keyring;
573 atomic_inc(&key->usage);
574 key_ref = make_key_ref(key, 1);
577 case KEY_SPEC_PROCESS_KEYRING:
578 if (!cred->tgcred->process_keyring) {
579 if (!(lflags & KEY_LOOKUP_CREATE))
582 ret = install_process_keyring();
584 key_ref = ERR_PTR(ret);
590 key = cred->tgcred->process_keyring;
591 atomic_inc(&key->usage);
592 key_ref = make_key_ref(key, 1);
595 case KEY_SPEC_SESSION_KEYRING:
596 if (!cred->tgcred->session_keyring) {
597 /* always install a session keyring upon access if one
598 * doesn't exist yet */
599 ret = install_user_keyrings();
602 if (lflags & KEY_LOOKUP_CREATE)
603 ret = join_session_keyring(NULL);
605 ret = install_session_keyring(
606 cred->user->session_keyring);
611 } else if (cred->tgcred->session_keyring ==
612 cred->user->session_keyring &&
613 lflags & KEY_LOOKUP_CREATE) {
614 ret = join_session_keyring(NULL);
621 key = rcu_dereference(cred->tgcred->session_keyring);
622 atomic_inc(&key->usage);
624 key_ref = make_key_ref(key, 1);
627 case KEY_SPEC_USER_KEYRING:
628 if (!cred->user->uid_keyring) {
629 ret = install_user_keyrings();
634 key = cred->user->uid_keyring;
635 atomic_inc(&key->usage);
636 key_ref = make_key_ref(key, 1);
639 case KEY_SPEC_USER_SESSION_KEYRING:
640 if (!cred->user->session_keyring) {
641 ret = install_user_keyrings();
646 key = cred->user->session_keyring;
647 atomic_inc(&key->usage);
648 key_ref = make_key_ref(key, 1);
651 case KEY_SPEC_GROUP_KEYRING:
652 /* group keyrings are not yet supported */
653 key_ref = ERR_PTR(-EINVAL);
656 case KEY_SPEC_REQKEY_AUTH_KEY:
657 key = cred->request_key_auth;
661 atomic_inc(&key->usage);
662 key_ref = make_key_ref(key, 1);
665 case KEY_SPEC_REQUESTOR_KEYRING:
666 if (!cred->request_key_auth)
669 down_read(&cred->request_key_auth->sem);
670 if (cred->request_key_auth->flags & KEY_FLAG_REVOKED) {
671 key_ref = ERR_PTR(-EKEYREVOKED);
674 rka = cred->request_key_auth->payload.data;
675 key = rka->dest_keyring;
676 atomic_inc(&key->usage);
678 up_read(&cred->request_key_auth->sem);
681 key_ref = make_key_ref(key, 1);
685 key_ref = ERR_PTR(-EINVAL);
689 key = key_lookup(id);
691 key_ref = ERR_CAST(key);
695 key_ref = make_key_ref(key, 0);
697 /* check to see if we possess the key */
698 skey_ref = search_process_keyrings(key->type, key,
699 lookup_user_key_possessed,
702 if (!IS_ERR(skey_ref)) {
710 /* unlink does not use the nominated key in any way, so can skip all
711 * the permission checks as it is only concerned with the keyring */
712 if (lflags & KEY_LOOKUP_FOR_UNLINK) {
717 if (!(lflags & KEY_LOOKUP_PARTIAL)) {
718 ret = wait_for_key_construction(key, true);
729 ret = key_validate(key);
735 if (!(lflags & KEY_LOOKUP_PARTIAL) &&
736 !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
739 /* check the permissions */
740 ret = key_task_permission(key_ref, cred, perm);
749 key_ref_put(key_ref);
750 key_ref = ERR_PTR(ret);
753 /* if we attempted to install a keyring, then it may have caused new
754 * creds to be installed */
761 * Join the named keyring as the session keyring if possible else attempt to
762 * create a new one of that name and join that.
764 * If the name is NULL, an empty anonymous keyring will be installed as the
767 * Named session keyrings are joined with a semaphore held to prevent the
768 * keyrings from going away whilst the attempt is made to going them and also
769 * to prevent a race in creating compatible session keyrings.
771 long join_session_keyring(const char *name)
773 const struct cred *old;
778 /* only permit this if there's a single thread in the thread group -
779 * this avoids us having to adjust the creds on all threads and risking
781 if (!current_is_single_threaded())
784 new = prepare_creds();
787 old = current_cred();
789 /* if no name is provided, install an anonymous keyring */
791 ret = install_session_keyring_to_cred(new, NULL);
795 serial = new->tgcred->session_keyring->serial;
796 ret = commit_creds(new);
802 /* allow the user to join or create a named keyring */
803 mutex_lock(&key_session_mutex);
805 /* look for an existing keyring of this name */
806 keyring = find_keyring_by_name(name, false);
807 if (PTR_ERR(keyring) == -ENOKEY) {
808 /* not found - try and create a new one */
809 keyring = keyring_alloc(name, old->uid, old->gid, old,
810 KEY_ALLOC_IN_QUOTA, NULL);
811 if (IS_ERR(keyring)) {
812 ret = PTR_ERR(keyring);
815 } else if (IS_ERR(keyring)) {
816 ret = PTR_ERR(keyring);
820 /* we've got a keyring - now to install it */
821 ret = install_session_keyring_to_cred(new, keyring);
826 mutex_unlock(&key_session_mutex);
828 ret = keyring->serial;
834 mutex_unlock(&key_session_mutex);
841 * Replace a process's session keyring on behalf of one of its children when
842 * the target process is about to resume userspace execution.
844 void key_replace_session_keyring(void)
846 const struct cred *old;
849 if (!current->replacement_session_keyring)
852 write_lock_irq(&tasklist_lock);
853 new = current->replacement_session_keyring;
854 current->replacement_session_keyring = NULL;
855 write_unlock_irq(&tasklist_lock);
860 old = current_cred();
861 new-> uid = old-> uid;
862 new-> euid = old-> euid;
863 new-> suid = old-> suid;
864 new->fsuid = old->fsuid;
865 new-> gid = old-> gid;
866 new-> egid = old-> egid;
867 new-> sgid = old-> sgid;
868 new->fsgid = old->fsgid;
869 new->user = get_uid(old->user);
870 new->user_ns = new->user->user_ns;
871 new->group_info = get_group_info(old->group_info);
873 new->securebits = old->securebits;
874 new->cap_inheritable = old->cap_inheritable;
875 new->cap_permitted = old->cap_permitted;
876 new->cap_effective = old->cap_effective;
877 new->cap_bset = old->cap_bset;
879 new->jit_keyring = old->jit_keyring;
880 new->thread_keyring = key_get(old->thread_keyring);
881 new->tgcred->tgid = old->tgcred->tgid;
882 new->tgcred->process_keyring = key_get(old->tgcred->process_keyring);
884 security_transfer_creds(new, old);