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,
76 KEY_ALLOC_UID_KEYRING |
79 if (IS_ERR(uid_keyring)) {
80 ret = PTR_ERR(uid_keyring);
85 /* get a default session keyring (which might also exist
87 sprintf(buf, "_uid_ses.%u", user->uid);
89 session_keyring = find_keyring_by_name(buf, true);
90 if (IS_ERR(session_keyring)) {
92 keyring_alloc(buf, user->uid, (gid_t) -1,
94 KEY_ALLOC_UID_KEYRING |
97 if (IS_ERR(session_keyring)) {
98 ret = PTR_ERR(session_keyring);
102 /* we install a link from the user session keyring to
103 * the user keyring */
104 ret = key_link(session_keyring, uid_keyring);
106 goto error_release_both;
109 /* install the keyrings */
110 user->uid_keyring = uid_keyring;
111 user->session_keyring = session_keyring;
114 mutex_unlock(&key_user_keyring_mutex);
119 key_put(session_keyring);
121 key_put(uid_keyring);
123 mutex_unlock(&key_user_keyring_mutex);
124 kleave(" = %d", ret);
129 * Install a thread keyring to the given credentials struct if it didn't have
130 * one already. This is allowed to overrun the quota.
132 * Return: 0 if a thread keyring is now present; -errno on failure.
134 int install_thread_keyring_to_cred(struct cred *new)
138 if (new->thread_keyring)
141 keyring = keyring_alloc("_tid", new->uid, new->gid, new,
142 KEY_ALLOC_QUOTA_OVERRUN, NULL);
144 return PTR_ERR(keyring);
146 new->thread_keyring = keyring;
151 * Install a thread keyring to the current task if it didn't have one already.
153 * Return: 0 if a thread keyring is now present; -errno on failure.
155 static int install_thread_keyring(void)
160 new = prepare_creds();
164 ret = install_thread_keyring_to_cred(new);
170 return commit_creds(new);
174 * Install a process keyring to the given credentials struct if it didn't have
175 * one already. This is allowed to overrun the quota.
177 * Return: 0 if a process keyring is now present; -errno on failure.
179 int install_process_keyring_to_cred(struct cred *new)
184 if (new->tgcred->process_keyring)
187 keyring = keyring_alloc("_pid", new->uid, new->gid,
188 new, KEY_ALLOC_QUOTA_OVERRUN, NULL);
190 return PTR_ERR(keyring);
192 spin_lock_irq(&new->tgcred->lock);
193 if (!new->tgcred->process_keyring) {
194 new->tgcred->process_keyring = keyring;
200 spin_unlock_irq(&new->tgcred->lock);
206 * Install a process keyring to the current task if it didn't have one already.
208 * Return: 0 if a process keyring is now present; -errno on failure.
210 static int install_process_keyring(void)
215 new = prepare_creds();
219 ret = install_process_keyring_to_cred(new);
225 return commit_creds(new);
229 * Install the given keyring as the session keyring of the given credentials
230 * struct, replacing the existing one if any. If the given keyring is NULL,
231 * then install a new anonymous session keyring.
233 * Return: 0 on success; -errno on failure.
235 int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
242 /* create an empty session keyring */
244 flags = KEY_ALLOC_QUOTA_OVERRUN;
245 if (cred->tgcred->session_keyring)
246 flags = KEY_ALLOC_IN_QUOTA;
248 keyring = keyring_alloc("_ses", cred->uid, cred->gid,
251 return PTR_ERR(keyring);
253 atomic_inc(&keyring->usage);
256 /* install the keyring */
257 spin_lock_irq(&cred->tgcred->lock);
258 old = cred->tgcred->session_keyring;
259 rcu_assign_pointer(cred->tgcred->session_keyring, keyring);
260 spin_unlock_irq(&cred->tgcred->lock);
262 /* we're using RCU on the pointer, but there's no point synchronising
263 * on it if it didn't previously point to anything */
273 * Install the given keyring as the session keyring of the current task,
274 * replacing the existing one if any. If the given keyring is NULL, then
275 * install a new anonymous session keyring.
277 * Return: 0 on success; -errno on failure.
279 static int install_session_keyring(struct key *keyring)
284 new = prepare_creds();
288 ret = install_session_keyring_to_cred(new, keyring);
294 return commit_creds(new);
298 * Handle the fsuid changing.
300 void key_fsuid_changed(struct task_struct *tsk)
302 /* update the ownership of the thread keyring */
304 if (tsk->cred->thread_keyring) {
305 down_write(&tsk->cred->thread_keyring->sem);
306 tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
307 up_write(&tsk->cred->thread_keyring->sem);
312 * Handle the fsgid changing.
314 void key_fsgid_changed(struct task_struct *tsk)
316 /* update the ownership of the thread keyring */
318 if (tsk->cred->thread_keyring) {
319 down_write(&tsk->cred->thread_keyring->sem);
320 tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
321 up_write(&tsk->cred->thread_keyring->sem);
326 * Search the process keyrings attached to the supplied cred for the first
329 * The search criteria are the type and the match function. The description is
330 * given to the match function as a parameter, but doesn't otherwise influence
331 * the search. Typically the match function will compare the description
332 * parameter to the key's description.
334 * This can only search keyrings that grant Search permission to the supplied
335 * credentials. Keyrings linked to searched keyrings will also be searched if
336 * they grant Search permission too. Keys can only be found if they grant
337 * Search permission to the credentials.
339 * Returns a pointer to the key with the key usage count incremented if
340 * successful, -EAGAIN if we didn't find any matching key or -ENOKEY if we only
341 * matched negative keys.
343 * In the case of a successful return, the possession attribute is set on the
344 * returned key reference.
346 key_ref_t search_my_process_keyrings(struct key_type *type,
347 const void *description,
348 key_match_func_t match,
350 const struct cred *cred)
352 key_ref_t key_ref, ret, err;
354 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
355 * searchable, but we failed to find a key or we found a negative key;
356 * otherwise we want to return a sample error (probably -EACCES) if
357 * none of the keyrings were searchable
359 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
363 err = ERR_PTR(-EAGAIN);
365 /* search the thread keyring first */
366 if (cred->thread_keyring) {
367 key_ref = keyring_search_aux(
368 make_key_ref(cred->thread_keyring, 1),
369 cred, type, description, match, no_state_check);
370 if (!IS_ERR(key_ref))
373 switch (PTR_ERR(key_ref)) {
374 case -EAGAIN: /* no key */
377 case -ENOKEY: /* negative key */
386 /* search the process keyring second */
387 if (cred->tgcred->process_keyring) {
388 key_ref = keyring_search_aux(
389 make_key_ref(cred->tgcred->process_keyring, 1),
390 cred, type, description, match, no_state_check);
391 if (!IS_ERR(key_ref))
394 switch (PTR_ERR(key_ref)) {
395 case -EAGAIN: /* no key */
398 case -ENOKEY: /* negative key */
407 /* search the session keyring */
408 if (cred->tgcred->session_keyring) {
410 key_ref = keyring_search_aux(
411 make_key_ref(rcu_dereference(
412 cred->tgcred->session_keyring),
414 cred, type, description, match, no_state_check);
417 if (!IS_ERR(key_ref))
420 switch (PTR_ERR(key_ref)) {
421 case -EAGAIN: /* no key */
424 case -ENOKEY: /* negative key */
432 /* or search the user-session keyring */
433 else if (cred->user->session_keyring) {
434 key_ref = keyring_search_aux(
435 make_key_ref(cred->user->session_keyring, 1),
436 cred, type, description, match, no_state_check);
437 if (!IS_ERR(key_ref))
440 switch (PTR_ERR(key_ref)) {
441 case -EAGAIN: /* no key */
444 case -ENOKEY: /* negative key */
453 /* no key - decide on the error we're going to go for */
454 key_ref = ret ? ret : err;
461 * Search the process keyrings attached to the supplied cred for the first
462 * matching key in the manner of search_my_process_keyrings(), but also search
463 * the keys attached to the assumed authorisation key using its credentials if
466 * Return same as search_my_process_keyrings().
468 key_ref_t search_process_keyrings(struct key_type *type,
469 const void *description,
470 key_match_func_t match,
471 const struct cred *cred)
473 struct request_key_auth *rka;
474 key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
478 key_ref = search_my_process_keyrings(type, description, match,
480 if (!IS_ERR(key_ref))
484 /* if this process has an instantiation authorisation key, then we also
485 * search the keyrings of the process mentioned there
486 * - we don't permit access to request_key auth keys via this method
488 if (cred->request_key_auth &&
489 cred == current_cred() &&
490 type != &key_type_request_key_auth
492 /* defend against the auth key being revoked */
493 down_read(&cred->request_key_auth->sem);
495 if (key_validate(cred->request_key_auth) == 0) {
496 rka = cred->request_key_auth->payload.data;
498 key_ref = search_process_keyrings(type, description,
501 up_read(&cred->request_key_auth->sem);
503 if (!IS_ERR(key_ref))
508 up_read(&cred->request_key_auth->sem);
512 /* no key - decide on the error we're going to go for */
513 if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY))
514 key_ref = ERR_PTR(-ENOKEY);
515 else if (err == ERR_PTR(-EACCES))
525 * See if the key we're looking at is the target key.
527 int lookup_user_key_possessed(const struct key *key, const void *target)
529 return key == target;
533 * Look up a key ID given us by userspace with a given permissions mask to get
534 * the key it refers to.
536 * Flags can be passed to request that special keyrings be created if referred
537 * to directly, to permit partially constructed keys to be found and to skip
538 * validity and permission checks on the found key.
540 * Returns a pointer to the key with an incremented usage count if successful;
541 * -EINVAL if the key ID is invalid; -ENOKEY if the key ID does not correspond
542 * to a key or the best found key was a negative key; -EKEYREVOKED or
543 * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the
544 * found key doesn't grant the requested permit or the LSM denied access to it;
545 * or -ENOMEM if a special keyring couldn't be created.
547 * In the case of a successful return, the possession attribute is set on the
548 * returned key reference.
550 key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
553 struct request_key_auth *rka;
554 const struct cred *cred;
556 key_ref_t key_ref, skey_ref;
560 cred = get_current_cred();
561 key_ref = ERR_PTR(-ENOKEY);
564 case KEY_SPEC_THREAD_KEYRING:
565 if (!cred->thread_keyring) {
566 if (!(lflags & KEY_LOOKUP_CREATE))
569 ret = install_thread_keyring();
571 key_ref = ERR_PTR(ret);
577 key = cred->thread_keyring;
578 atomic_inc(&key->usage);
579 key_ref = make_key_ref(key, 1);
582 case KEY_SPEC_PROCESS_KEYRING:
583 if (!cred->tgcred->process_keyring) {
584 if (!(lflags & KEY_LOOKUP_CREATE))
587 ret = install_process_keyring();
589 key_ref = ERR_PTR(ret);
595 key = cred->tgcred->process_keyring;
596 atomic_inc(&key->usage);
597 key_ref = make_key_ref(key, 1);
600 case KEY_SPEC_SESSION_KEYRING:
601 if (!cred->tgcred->session_keyring) {
602 /* always install a session keyring upon access if one
603 * doesn't exist yet */
604 ret = install_user_keyrings();
607 if (lflags & KEY_LOOKUP_CREATE)
608 ret = join_session_keyring(NULL);
610 ret = install_session_keyring(
611 cred->user->session_keyring);
616 } else if (cred->tgcred->session_keyring ==
617 cred->user->session_keyring &&
618 lflags & KEY_LOOKUP_CREATE) {
619 ret = join_session_keyring(NULL);
626 key = rcu_dereference(cred->tgcred->session_keyring);
627 atomic_inc(&key->usage);
629 key_ref = make_key_ref(key, 1);
632 case KEY_SPEC_USER_KEYRING:
633 if (!cred->user->uid_keyring) {
634 ret = install_user_keyrings();
639 key = cred->user->uid_keyring;
640 atomic_inc(&key->usage);
641 key_ref = make_key_ref(key, 1);
644 case KEY_SPEC_USER_SESSION_KEYRING:
645 if (!cred->user->session_keyring) {
646 ret = install_user_keyrings();
651 key = cred->user->session_keyring;
652 atomic_inc(&key->usage);
653 key_ref = make_key_ref(key, 1);
656 case KEY_SPEC_GROUP_KEYRING:
657 /* group keyrings are not yet supported */
658 key_ref = ERR_PTR(-EINVAL);
661 case KEY_SPEC_REQKEY_AUTH_KEY:
662 key = cred->request_key_auth;
666 atomic_inc(&key->usage);
667 key_ref = make_key_ref(key, 1);
670 case KEY_SPEC_REQUESTOR_KEYRING:
671 if (!cred->request_key_auth)
674 down_read(&cred->request_key_auth->sem);
675 if (cred->request_key_auth->flags & KEY_FLAG_REVOKED) {
676 key_ref = ERR_PTR(-EKEYREVOKED);
679 rka = cred->request_key_auth->payload.data;
680 key = rka->dest_keyring;
681 atomic_inc(&key->usage);
683 up_read(&cred->request_key_auth->sem);
686 key_ref = make_key_ref(key, 1);
690 key_ref = ERR_PTR(-EINVAL);
694 key = key_lookup(id);
696 key_ref = ERR_CAST(key);
700 key_ref = make_key_ref(key, 0);
702 /* check to see if we possess the key */
703 skey_ref = search_process_keyrings(key->type, key,
704 lookup_user_key_possessed,
707 if (!IS_ERR(skey_ref)) {
715 /* unlink does not use the nominated key in any way, so can skip all
716 * the permission checks as it is only concerned with the keyring */
717 if (lflags & KEY_LOOKUP_FOR_UNLINK) {
722 if (!(lflags & KEY_LOOKUP_PARTIAL)) {
723 ret = wait_for_key_construction(key, true);
734 ret = key_validate(key);
740 if (!(lflags & KEY_LOOKUP_PARTIAL) &&
741 !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
744 /* check the permissions */
745 ret = key_task_permission(key_ref, cred, perm);
754 key_ref_put(key_ref);
755 key_ref = ERR_PTR(ret);
758 /* if we attempted to install a keyring, then it may have caused new
759 * creds to be installed */
766 * Join the named keyring as the session keyring if possible else attempt to
767 * create a new one of that name and join that.
769 * If the name is NULL, an empty anonymous keyring will be installed as the
772 * Named session keyrings are joined with a semaphore held to prevent the
773 * keyrings from going away whilst the attempt is made to going them and also
774 * to prevent a race in creating compatible session keyrings.
776 long join_session_keyring(const char *name)
778 const struct cred *old;
783 /* only permit this if there's a single thread in the thread group -
784 * this avoids us having to adjust the creds on all threads and risking
786 if (!current_is_single_threaded())
789 new = prepare_creds();
792 old = current_cred();
794 /* if no name is provided, install an anonymous keyring */
796 ret = install_session_keyring_to_cred(new, NULL);
800 serial = new->tgcred->session_keyring->serial;
801 ret = commit_creds(new);
807 /* allow the user to join or create a named keyring */
808 mutex_lock(&key_session_mutex);
810 /* look for an existing keyring of this name */
811 keyring = find_keyring_by_name(name, false);
812 if (PTR_ERR(keyring) == -ENOKEY) {
813 /* not found - try and create a new one */
814 keyring = keyring_alloc(name, old->uid, old->gid, old,
815 KEY_ALLOC_IN_QUOTA, NULL);
816 if (IS_ERR(keyring)) {
817 ret = PTR_ERR(keyring);
820 } else if (IS_ERR(keyring)) {
821 ret = PTR_ERR(keyring);
825 /* we've got a keyring - now to install it */
826 ret = install_session_keyring_to_cred(new, keyring);
831 mutex_unlock(&key_session_mutex);
833 ret = keyring->serial;
839 mutex_unlock(&key_session_mutex);
846 * Replace a process's session keyring on behalf of one of its children when
847 * the target process is about to resume userspace execution.
849 void key_replace_session_keyring(void)
851 const struct cred *old;
854 if (!current->replacement_session_keyring)
857 write_lock_irq(&tasklist_lock);
858 new = current->replacement_session_keyring;
859 current->replacement_session_keyring = NULL;
860 write_unlock_irq(&tasklist_lock);
865 old = current_cred();
866 new-> uid = old-> uid;
867 new-> euid = old-> euid;
868 new-> suid = old-> suid;
869 new->fsuid = old->fsuid;
870 new-> gid = old-> gid;
871 new-> egid = old-> egid;
872 new-> sgid = old-> sgid;
873 new->fsgid = old->fsgid;
874 new->user = get_uid(old->user);
875 new->user_ns = new->user->user_ns;
876 new->group_info = get_group_info(old->group_info);
878 new->securebits = old->securebits;
879 new->cap_inheritable = old->cap_inheritable;
880 new->cap_permitted = old->cap_permitted;
881 new->cap_effective = old->cap_effective;
882 new->cap_bset = old->cap_bset;
884 new->jit_keyring = old->jit_keyring;
885 new->thread_keyring = key_get(old->thread_keyring);
886 new->tgcred->tgid = old->tgcred->tgid;
887 new->tgcred->process_keyring = key_get(old->tgcred->process_keyring);
889 security_transfer_creds(new, old);