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/sched/user.h>
16 #include <linux/keyctl.h>
18 #include <linux/err.h>
19 #include <linux/mutex.h>
20 #include <linux/security.h>
21 #include <linux/user_namespace.h>
22 #include <linux/uaccess.h>
23 #include <keys/request_key_auth-type.h>
26 /* Session keyring create vs join semaphore */
27 static DEFINE_MUTEX(key_session_mutex);
29 /* User keyring creation semaphore */
30 static DEFINE_MUTEX(key_user_keyring_mutex);
32 /* The root user's tracking struct */
33 struct key_user root_key_user = {
34 .usage = REFCOUNT_INIT(3),
35 .cons_lock = __MUTEX_INITIALIZER(root_key_user.cons_lock),
36 .lock = __SPIN_LOCK_UNLOCKED(root_key_user.lock),
37 .nkeys = ATOMIC_INIT(2),
38 .nikeys = ATOMIC_INIT(2),
39 .uid = GLOBAL_ROOT_UID,
43 * Install the user and user session keyrings for the current process's UID.
45 int install_user_keyrings(void)
47 struct user_struct *user;
48 const struct cred *cred;
49 struct key *uid_keyring, *session_keyring;
50 key_perm_t user_keyring_perm;
55 user_keyring_perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL;
56 cred = current_cred();
58 uid = from_kuid(cred->user_ns, user->uid);
60 kenter("%p{%u}", user, uid);
62 if (user->uid_keyring && user->session_keyring) {
63 kleave(" = 0 [exist]");
67 mutex_lock(&key_user_keyring_mutex);
70 if (!user->uid_keyring) {
71 /* get the UID-specific keyring
72 * - there may be one in existence already as it may have been
73 * pinned by a session, but the user_struct pointing to it
74 * may have been destroyed by setuid */
75 sprintf(buf, "_uid.%u", uid);
77 uid_keyring = find_keyring_by_name(buf, true);
78 if (IS_ERR(uid_keyring)) {
79 uid_keyring = keyring_alloc(buf, user->uid, INVALID_GID,
80 cred, user_keyring_perm,
81 KEY_ALLOC_UID_KEYRING |
84 if (IS_ERR(uid_keyring)) {
85 ret = PTR_ERR(uid_keyring);
90 /* get a default session keyring (which might also exist
92 sprintf(buf, "_uid_ses.%u", uid);
94 session_keyring = find_keyring_by_name(buf, true);
95 if (IS_ERR(session_keyring)) {
97 keyring_alloc(buf, user->uid, INVALID_GID,
98 cred, user_keyring_perm,
99 KEY_ALLOC_UID_KEYRING |
102 if (IS_ERR(session_keyring)) {
103 ret = PTR_ERR(session_keyring);
107 /* we install a link from the user session keyring to
108 * the user keyring */
109 ret = key_link(session_keyring, uid_keyring);
111 goto error_release_both;
114 /* install the keyrings */
115 user->uid_keyring = uid_keyring;
116 user->session_keyring = session_keyring;
119 mutex_unlock(&key_user_keyring_mutex);
124 key_put(session_keyring);
126 key_put(uid_keyring);
128 mutex_unlock(&key_user_keyring_mutex);
129 kleave(" = %d", ret);
134 * Install a thread keyring to the given credentials struct if it didn't have
135 * one already. This is allowed to overrun the quota.
137 * Return: 0 if a thread keyring is now present; -errno on failure.
139 int install_thread_keyring_to_cred(struct cred *new)
143 if (new->thread_keyring)
146 keyring = keyring_alloc("_tid", new->uid, new->gid, new,
147 KEY_POS_ALL | KEY_USR_VIEW,
148 KEY_ALLOC_QUOTA_OVERRUN,
151 return PTR_ERR(keyring);
153 new->thread_keyring = keyring;
158 * Install a thread keyring to the current task if it didn't have one already.
160 * Return: 0 if a thread keyring is now present; -errno on failure.
162 static int install_thread_keyring(void)
167 new = prepare_creds();
171 ret = install_thread_keyring_to_cred(new);
177 return commit_creds(new);
181 * Install a process keyring to the given credentials struct if it didn't have
182 * one already. This is allowed to overrun the quota.
184 * Return: 0 if a process keyring is now present; -errno on failure.
186 int install_process_keyring_to_cred(struct cred *new)
190 if (new->process_keyring)
193 keyring = keyring_alloc("_pid", new->uid, new->gid, new,
194 KEY_POS_ALL | KEY_USR_VIEW,
195 KEY_ALLOC_QUOTA_OVERRUN,
198 return PTR_ERR(keyring);
200 new->process_keyring = keyring;
205 * Install a process keyring to the current task if it didn't have one already.
207 * Return: 0 if a process keyring is now present; -errno on failure.
209 static int install_process_keyring(void)
214 new = prepare_creds();
218 ret = install_process_keyring_to_cred(new);
224 return commit_creds(new);
228 * Install the given keyring as the session keyring of the given credentials
229 * struct, replacing the existing one if any. If the given keyring is NULL,
230 * then install a new anonymous session keyring.
232 * Return: 0 on success; -errno on failure.
234 int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
241 /* create an empty session keyring */
243 flags = KEY_ALLOC_QUOTA_OVERRUN;
244 if (cred->session_keyring)
245 flags = KEY_ALLOC_IN_QUOTA;
247 keyring = keyring_alloc("_ses", cred->uid, cred->gid, cred,
248 KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ,
251 return PTR_ERR(keyring);
256 /* install the keyring */
257 old = cred->session_keyring;
258 rcu_assign_pointer(cred->session_keyring, keyring);
267 * Install the given keyring as the session keyring of the current task,
268 * replacing the existing one if any. If the given keyring is NULL, then
269 * install a new anonymous session keyring.
271 * Return: 0 on success; -errno on failure.
273 static int install_session_keyring(struct key *keyring)
278 new = prepare_creds();
282 ret = install_session_keyring_to_cred(new, keyring);
288 return commit_creds(new);
292 * Handle the fsuid changing.
294 void key_fsuid_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->uid = tsk->cred->fsuid;
301 up_write(&tsk->cred->thread_keyring->sem);
306 * Handle the fsgid changing.
308 void key_fsgid_changed(struct task_struct *tsk)
310 /* update the ownership of the thread keyring */
312 if (tsk->cred->thread_keyring) {
313 down_write(&tsk->cred->thread_keyring->sem);
314 tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
315 up_write(&tsk->cred->thread_keyring->sem);
320 * Search the process keyrings attached to the supplied cred for the first
323 * The search criteria are the type and the match function. The description is
324 * given to the match function as a parameter, but doesn't otherwise influence
325 * the search. Typically the match function will compare the description
326 * parameter to the key's description.
328 * This can only search keyrings that grant Search permission to the supplied
329 * credentials. Keyrings linked to searched keyrings will also be searched if
330 * they grant Search permission too. Keys can only be found if they grant
331 * Search permission to the credentials.
333 * Returns a pointer to the key with the key usage count incremented if
334 * successful, -EAGAIN if we didn't find any matching key or -ENOKEY if we only
335 * matched negative keys.
337 * In the case of a successful return, the possession attribute is set on the
338 * returned key reference.
340 key_ref_t search_my_process_keyrings(struct keyring_search_context *ctx)
342 key_ref_t key_ref, ret, err;
344 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
345 * searchable, but we failed to find a key or we found a negative key;
346 * otherwise we want to return a sample error (probably -EACCES) if
347 * none of the keyrings were searchable
349 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
353 err = ERR_PTR(-EAGAIN);
355 /* search the thread keyring first */
356 if (ctx->cred->thread_keyring) {
357 key_ref = keyring_search_aux(
358 make_key_ref(ctx->cred->thread_keyring, 1), ctx);
359 if (!IS_ERR(key_ref))
362 switch (PTR_ERR(key_ref)) {
363 case -EAGAIN: /* no key */
364 case -ENOKEY: /* negative key */
373 /* search the process keyring second */
374 if (ctx->cred->process_keyring) {
375 key_ref = keyring_search_aux(
376 make_key_ref(ctx->cred->process_keyring, 1), ctx);
377 if (!IS_ERR(key_ref))
380 switch (PTR_ERR(key_ref)) {
381 case -EAGAIN: /* no key */
384 case -ENOKEY: /* negative key */
393 /* search the session keyring */
394 if (ctx->cred->session_keyring) {
396 key_ref = keyring_search_aux(
397 make_key_ref(rcu_dereference(ctx->cred->session_keyring), 1),
401 if (!IS_ERR(key_ref))
404 switch (PTR_ERR(key_ref)) {
405 case -EAGAIN: /* no key */
408 case -ENOKEY: /* negative key */
416 /* or search the user-session keyring */
417 else if (ctx->cred->user->session_keyring) {
418 key_ref = keyring_search_aux(
419 make_key_ref(ctx->cred->user->session_keyring, 1),
421 if (!IS_ERR(key_ref))
424 switch (PTR_ERR(key_ref)) {
425 case -EAGAIN: /* no key */
428 case -ENOKEY: /* negative key */
437 /* no key - decide on the error we're going to go for */
438 key_ref = ret ? ret : err;
445 * Search the process keyrings attached to the supplied cred for the first
446 * matching key in the manner of search_my_process_keyrings(), but also search
447 * the keys attached to the assumed authorisation key using its credentials if
450 * Return same as search_my_process_keyrings().
452 key_ref_t search_process_keyrings(struct keyring_search_context *ctx)
454 struct request_key_auth *rka;
455 key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
459 key_ref = search_my_process_keyrings(ctx);
460 if (!IS_ERR(key_ref))
464 /* if this process has an instantiation authorisation key, then we also
465 * search the keyrings of the process mentioned there
466 * - we don't permit access to request_key auth keys via this method
468 if (ctx->cred->request_key_auth &&
469 ctx->cred == current_cred() &&
470 ctx->index_key.type != &key_type_request_key_auth
472 const struct cred *cred = ctx->cred;
474 /* defend against the auth key being revoked */
475 down_read(&cred->request_key_auth->sem);
477 if (key_validate(ctx->cred->request_key_auth) == 0) {
478 rka = ctx->cred->request_key_auth->payload.data[0];
480 ctx->cred = rka->cred;
481 key_ref = search_process_keyrings(ctx);
484 up_read(&cred->request_key_auth->sem);
486 if (!IS_ERR(key_ref))
491 up_read(&cred->request_key_auth->sem);
495 /* no key - decide on the error we're going to go for */
496 if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY))
497 key_ref = ERR_PTR(-ENOKEY);
498 else if (err == ERR_PTR(-EACCES))
508 * See if the key we're looking at is the target key.
510 bool lookup_user_key_possessed(const struct key *key,
511 const struct key_match_data *match_data)
513 return key == match_data->raw_data;
517 * Look up a key ID given us by userspace with a given permissions mask to get
518 * the key it refers to.
520 * Flags can be passed to request that special keyrings be created if referred
521 * to directly, to permit partially constructed keys to be found and to skip
522 * validity and permission checks on the found key.
524 * Returns a pointer to the key with an incremented usage count if successful;
525 * -EINVAL if the key ID is invalid; -ENOKEY if the key ID does not correspond
526 * to a key or the best found key was a negative key; -EKEYREVOKED or
527 * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the
528 * found key doesn't grant the requested permit or the LSM denied access to it;
529 * or -ENOMEM if a special keyring couldn't be created.
531 * In the case of a successful return, the possession attribute is set on the
532 * returned key reference.
534 key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
537 struct keyring_search_context ctx = {
538 .match_data.cmp = lookup_user_key_possessed,
539 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
540 .flags = KEYRING_SEARCH_NO_STATE_CHECK,
542 struct request_key_auth *rka;
544 key_ref_t key_ref, skey_ref;
548 ctx.cred = get_current_cred();
549 key_ref = ERR_PTR(-ENOKEY);
552 case KEY_SPEC_THREAD_KEYRING:
553 if (!ctx.cred->thread_keyring) {
554 if (!(lflags & KEY_LOOKUP_CREATE))
557 ret = install_thread_keyring();
559 key_ref = ERR_PTR(ret);
565 key = ctx.cred->thread_keyring;
567 key_ref = make_key_ref(key, 1);
570 case KEY_SPEC_PROCESS_KEYRING:
571 if (!ctx.cred->process_keyring) {
572 if (!(lflags & KEY_LOOKUP_CREATE))
575 ret = install_process_keyring();
577 key_ref = ERR_PTR(ret);
583 key = ctx.cred->process_keyring;
585 key_ref = make_key_ref(key, 1);
588 case KEY_SPEC_SESSION_KEYRING:
589 if (!ctx.cred->session_keyring) {
590 /* always install a session keyring upon access if one
591 * doesn't exist yet */
592 ret = install_user_keyrings();
595 if (lflags & KEY_LOOKUP_CREATE)
596 ret = join_session_keyring(NULL);
598 ret = install_session_keyring(
599 ctx.cred->user->session_keyring);
604 } else if (ctx.cred->session_keyring ==
605 ctx.cred->user->session_keyring &&
606 lflags & KEY_LOOKUP_CREATE) {
607 ret = join_session_keyring(NULL);
614 key = rcu_dereference(ctx.cred->session_keyring);
617 key_ref = make_key_ref(key, 1);
620 case KEY_SPEC_USER_KEYRING:
621 if (!ctx.cred->user->uid_keyring) {
622 ret = install_user_keyrings();
627 key = ctx.cred->user->uid_keyring;
629 key_ref = make_key_ref(key, 1);
632 case KEY_SPEC_USER_SESSION_KEYRING:
633 if (!ctx.cred->user->session_keyring) {
634 ret = install_user_keyrings();
639 key = ctx.cred->user->session_keyring;
641 key_ref = make_key_ref(key, 1);
644 case KEY_SPEC_GROUP_KEYRING:
645 /* group keyrings are not yet supported */
646 key_ref = ERR_PTR(-EINVAL);
649 case KEY_SPEC_REQKEY_AUTH_KEY:
650 key = ctx.cred->request_key_auth;
655 key_ref = make_key_ref(key, 1);
658 case KEY_SPEC_REQUESTOR_KEYRING:
659 if (!ctx.cred->request_key_auth)
662 down_read(&ctx.cred->request_key_auth->sem);
663 if (test_bit(KEY_FLAG_REVOKED,
664 &ctx.cred->request_key_auth->flags)) {
665 key_ref = ERR_PTR(-EKEYREVOKED);
668 rka = ctx.cred->request_key_auth->payload.data[0];
669 key = rka->dest_keyring;
672 up_read(&ctx.cred->request_key_auth->sem);
675 key_ref = make_key_ref(key, 1);
679 key_ref = ERR_PTR(-EINVAL);
683 key = key_lookup(id);
685 key_ref = ERR_CAST(key);
689 key_ref = make_key_ref(key, 0);
691 /* check to see if we possess the key */
692 ctx.index_key.type = key->type;
693 ctx.index_key.description = key->description;
694 ctx.index_key.desc_len = strlen(key->description);
695 ctx.match_data.raw_data = key;
696 kdebug("check possessed");
697 skey_ref = search_process_keyrings(&ctx);
698 kdebug("possessed=%p", skey_ref);
700 if (!IS_ERR(skey_ref)) {
708 /* unlink does not use the nominated key in any way, so can skip all
709 * the permission checks as it is only concerned with the keyring */
710 if (lflags & KEY_LOOKUP_FOR_UNLINK) {
715 if (!(lflags & KEY_LOOKUP_PARTIAL)) {
716 ret = wait_for_key_construction(key, true);
727 ret = key_validate(key);
733 if (!(lflags & KEY_LOOKUP_PARTIAL) &&
734 key_read_state(key) == KEY_IS_UNINSTANTIATED)
737 /* check the permissions */
738 ret = key_task_permission(key_ref, ctx.cred, perm);
742 key->last_used_at = current_kernel_time().tv_sec;
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 new = prepare_creds();
781 old = current_cred();
783 /* if no name is provided, install an anonymous keyring */
785 ret = install_session_keyring_to_cred(new, NULL);
789 serial = new->session_keyring->serial;
790 ret = commit_creds(new);
796 /* allow the user to join or create a named keyring */
797 mutex_lock(&key_session_mutex);
799 /* look for an existing keyring of this name */
800 keyring = find_keyring_by_name(name, false);
801 if (PTR_ERR(keyring) == -ENOKEY) {
802 /* not found - try and create a new one */
803 keyring = keyring_alloc(
804 name, old->uid, old->gid, old,
805 KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ | KEY_USR_LINK,
806 KEY_ALLOC_IN_QUOTA, NULL, NULL);
807 if (IS_ERR(keyring)) {
808 ret = PTR_ERR(keyring);
811 } else if (IS_ERR(keyring)) {
812 ret = PTR_ERR(keyring);
814 } else if (keyring == new->session_keyring) {
819 /* we've got a keyring - now to install it */
820 ret = install_session_keyring_to_cred(new, keyring);
825 mutex_unlock(&key_session_mutex);
827 ret = keyring->serial;
835 mutex_unlock(&key_session_mutex);
842 * Replace a process's session keyring on behalf of one of its children when
843 * the target process is about to resume userspace execution.
845 void key_change_session_keyring(struct callback_head *twork)
847 const struct cred *old = current_cred();
848 struct cred *new = container_of(twork, struct cred, rcu);
850 if (unlikely(current->flags & PF_EXITING)) {
855 new-> uid = old-> uid;
856 new-> euid = old-> euid;
857 new-> suid = old-> suid;
858 new->fsuid = old->fsuid;
859 new-> gid = old-> gid;
860 new-> egid = old-> egid;
861 new-> sgid = old-> sgid;
862 new->fsgid = old->fsgid;
863 new->user = get_uid(old->user);
864 new->user_ns = get_user_ns(old->user_ns);
865 new->group_info = get_group_info(old->group_info);
867 new->securebits = old->securebits;
868 new->cap_inheritable = old->cap_inheritable;
869 new->cap_permitted = old->cap_permitted;
870 new->cap_effective = old->cap_effective;
871 new->cap_ambient = old->cap_ambient;
872 new->cap_bset = old->cap_bset;
874 new->jit_keyring = old->jit_keyring;
875 new->thread_keyring = key_get(old->thread_keyring);
876 new->process_keyring = key_get(old->process_keyring);
878 security_transfer_creds(new, old);
884 * Make sure that root's user and user-session keyrings exist.
886 static int __init init_root_keyring(void)
888 return install_user_keyrings();
891 late_initcall(init_root_keyring);