1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Manage a process's keyrings
4 * Copyright (C) 2004-2005, 2008 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/init.h>
9 #include <linux/sched.h>
10 #include <linux/sched/user.h>
11 #include <linux/keyctl.h>
13 #include <linux/err.h>
14 #include <linux/mutex.h>
15 #include <linux/security.h>
16 #include <linux/user_namespace.h>
17 #include <linux/uaccess.h>
18 #include <linux/init_task.h>
19 #include <keys/request_key_auth-type.h>
22 /* Session keyring create vs join semaphore */
23 static DEFINE_MUTEX(key_session_mutex);
25 /* The root user's tracking struct */
26 struct key_user root_key_user = {
27 .usage = REFCOUNT_INIT(3),
28 .cons_lock = __MUTEX_INITIALIZER(root_key_user.cons_lock),
29 .lock = __SPIN_LOCK_UNLOCKED(root_key_user.lock),
30 .nkeys = ATOMIC_INIT(2),
31 .nikeys = ATOMIC_INIT(2),
32 .uid = GLOBAL_ROOT_UID,
35 static struct key_acl user_reg_keyring_acl = {
36 .usage = REFCOUNT_INIT(1),
37 .possessor_viewable = true,
40 KEY_POSSESSOR_ACE(KEY_ACE_WRITE | KEY_ACE_SEARCH),
41 KEY_OWNER_ACE(KEY_ACE_VIEW | KEY_ACE_READ),
45 static struct key_acl user_keyring_acl = {
46 .usage = REFCOUNT_INIT(1),
47 .possessor_viewable = true,
50 KEY_POSSESSOR_ACE(KEY_ACE_VIEW | KEY_ACE_READ | KEY_ACE_WRITE |
51 KEY_ACE_SEARCH | KEY_ACE_LINK),
52 KEY_OWNER_ACE(KEY_ACE__PERMS & ~(KEY_ACE_JOIN | KEY_ACE_SET_SECURITY)),
56 static struct key_acl session_keyring_acl = {
57 .usage = REFCOUNT_INIT(1),
58 .possessor_viewable = true,
61 KEY_POSSESSOR_ACE(KEY_ACE__PERMS & ~KEY_ACE_JOIN),
62 KEY_OWNER_ACE(KEY_ACE_VIEW | KEY_ACE_READ),
66 static struct key_acl thread_and_process_keyring_acl = {
67 .usage = REFCOUNT_INIT(1),
68 .possessor_viewable = true,
71 KEY_POSSESSOR_ACE(KEY_ACE__PERMS & ~(KEY_ACE_JOIN | KEY_ACE_SET_SECURITY)),
72 KEY_OWNER_ACE(KEY_ACE_VIEW),
77 * Get or create a user register keyring.
79 static struct key *get_user_register(struct user_namespace *user_ns)
81 struct key *reg_keyring = READ_ONCE(user_ns->user_keyring_register);
86 down_write(&user_ns->keyring_sem);
88 /* Make sure there's a register keyring. It gets owned by the
89 * user_namespace's owner.
91 reg_keyring = user_ns->user_keyring_register;
93 reg_keyring = keyring_alloc(".user_reg",
94 user_ns->owner, INVALID_GID,
95 &init_cred, &user_reg_keyring_acl,
97 if (!IS_ERR(reg_keyring))
98 smp_store_release(&user_ns->user_keyring_register,
102 up_write(&user_ns->keyring_sem);
104 /* We don't return a ref since the keyring is pinned by the user_ns */
109 * Look up the user and user session keyrings for the current process's UID,
110 * creating them if they don't exist.
112 int look_up_user_keyrings(struct key **_user_keyring,
113 struct key **_user_session_keyring)
115 const struct cred *cred = current_cred();
116 struct user_namespace *user_ns = current_user_ns();
117 struct key *reg_keyring, *uid_keyring, *session_keyring;
118 key_ref_t uid_keyring_r, session_keyring_r;
119 uid_t uid = from_kuid(user_ns, cred->user->uid);
125 reg_keyring = get_user_register(user_ns);
126 if (IS_ERR(reg_keyring))
127 return PTR_ERR(reg_keyring);
129 down_write(&user_ns->keyring_sem);
132 /* Get the user keyring. Note that there may be one in existence
133 * already as it may have been pinned by a session, but the user_struct
134 * pointing to it may have been destroyed by setuid.
136 snprintf(buf, sizeof(buf), "_uid.%u", uid);
137 uid_keyring_r = keyring_search(make_key_ref(reg_keyring, true),
138 &key_type_keyring, buf, false);
139 kdebug("_uid %p", uid_keyring_r);
140 if (uid_keyring_r == ERR_PTR(-EAGAIN)) {
141 uid_keyring = keyring_alloc(buf, cred->user->uid, INVALID_GID,
142 cred, &user_keyring_acl,
143 KEY_ALLOC_UID_KEYRING |
146 if (IS_ERR(uid_keyring)) {
147 ret = PTR_ERR(uid_keyring);
150 } else if (IS_ERR(uid_keyring_r)) {
151 ret = PTR_ERR(uid_keyring_r);
154 uid_keyring = key_ref_to_ptr(uid_keyring_r);
157 /* Get a default session keyring (which might also exist already) */
158 snprintf(buf, sizeof(buf), "_uid_ses.%u", uid);
159 session_keyring_r = keyring_search(make_key_ref(reg_keyring, true),
160 &key_type_keyring, buf, false);
161 kdebug("_uid_ses %p", session_keyring_r);
162 if (session_keyring_r == ERR_PTR(-EAGAIN)) {
163 session_keyring = keyring_alloc(buf, cred->user->uid, INVALID_GID,
164 cred, &user_keyring_acl,
165 KEY_ALLOC_UID_KEYRING |
168 if (IS_ERR(session_keyring)) {
169 ret = PTR_ERR(session_keyring);
173 /* We install a link from the user session keyring to
176 ret = key_link(session_keyring, uid_keyring);
178 goto error_release_session;
180 /* And only then link the user-session keyring to the
183 ret = key_link(reg_keyring, session_keyring);
185 goto error_release_session;
186 } else if (IS_ERR(session_keyring_r)) {
187 ret = PTR_ERR(session_keyring_r);
190 session_keyring = key_ref_to_ptr(session_keyring_r);
193 up_write(&user_ns->keyring_sem);
195 if (_user_session_keyring)
196 *_user_session_keyring = session_keyring;
198 key_put(session_keyring);
200 *_user_keyring = uid_keyring;
202 key_put(uid_keyring);
206 error_release_session:
207 key_put(session_keyring);
209 key_put(uid_keyring);
211 up_write(&user_ns->keyring_sem);
212 kleave(" = %d", ret);
217 * Get the user session keyring if it exists, but don't create it if it
220 struct key *get_user_session_keyring_rcu(const struct cred *cred)
222 struct key *reg_keyring = READ_ONCE(cred->user_ns->user_keyring_register);
223 key_ref_t session_keyring_r;
226 struct keyring_search_context ctx = {
227 .index_key.type = &key_type_keyring,
228 .index_key.description = buf,
230 .match_data.cmp = key_default_cmp,
231 .match_data.raw_data = buf,
232 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
233 .flags = KEYRING_SEARCH_DO_STATE_CHECK,
239 ctx.index_key.desc_len = snprintf(buf, sizeof(buf), "_uid_ses.%u",
240 from_kuid(cred->user_ns,
243 session_keyring_r = keyring_search_rcu(make_key_ref(reg_keyring, true),
245 if (IS_ERR(session_keyring_r))
247 return key_ref_to_ptr(session_keyring_r);
251 * Install a thread keyring to the given credentials struct if it didn't have
252 * one already. This is allowed to overrun the quota.
254 * Return: 0 if a thread keyring is now present; -errno on failure.
256 int install_thread_keyring_to_cred(struct cred *new)
260 if (new->thread_keyring)
263 keyring = keyring_alloc("_tid", new->uid, new->gid, new,
264 &thread_and_process_keyring_acl,
265 KEY_ALLOC_QUOTA_OVERRUN,
268 return PTR_ERR(keyring);
270 new->thread_keyring = keyring;
275 * Install a thread keyring to the current task if it didn't have one already.
277 * Return: 0 if a thread keyring is now present; -errno on failure.
279 static int install_thread_keyring(void)
284 new = prepare_creds();
288 ret = install_thread_keyring_to_cred(new);
294 return commit_creds(new);
298 * Install a process keyring to the given credentials struct if it didn't have
299 * one already. This is allowed to overrun the quota.
301 * Return: 0 if a process keyring is now present; -errno on failure.
303 int install_process_keyring_to_cred(struct cred *new)
307 if (new->process_keyring)
310 keyring = keyring_alloc("_pid", new->uid, new->gid, new,
311 &thread_and_process_keyring_acl,
312 KEY_ALLOC_QUOTA_OVERRUN,
315 return PTR_ERR(keyring);
317 new->process_keyring = keyring;
322 * Install a process keyring to the current task if it didn't have one already.
324 * Return: 0 if a process keyring is now present; -errno on failure.
326 static int install_process_keyring(void)
331 new = prepare_creds();
335 ret = install_process_keyring_to_cred(new);
341 return commit_creds(new);
345 * Install the given keyring as the session keyring of the given credentials
346 * struct, replacing the existing one if any. If the given keyring is NULL,
347 * then install a new anonymous session keyring.
348 * @cred can not be in use by any task yet.
350 * Return: 0 on success; -errno on failure.
352 int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
359 /* create an empty session keyring */
361 flags = KEY_ALLOC_QUOTA_OVERRUN;
362 if (cred->session_keyring)
363 flags = KEY_ALLOC_IN_QUOTA;
365 keyring = keyring_alloc("_ses", cred->uid, cred->gid, cred,
366 &session_keyring_acl, flags, NULL, NULL);
368 return PTR_ERR(keyring);
373 /* install the keyring */
374 old = cred->session_keyring;
375 cred->session_keyring = keyring;
384 * Install the given keyring as the session keyring of the current task,
385 * replacing the existing one if any. If the given keyring is NULL, then
386 * install a new anonymous session keyring.
388 * Return: 0 on success; -errno on failure.
390 static int install_session_keyring(struct key *keyring)
395 new = prepare_creds();
399 ret = install_session_keyring_to_cred(new, keyring);
405 return commit_creds(new);
409 * Handle the fsuid changing.
411 void key_fsuid_changed(struct cred *new_cred)
413 /* update the ownership of the thread keyring */
414 if (new_cred->thread_keyring) {
415 down_write(&new_cred->thread_keyring->sem);
416 new_cred->thread_keyring->uid = new_cred->fsuid;
417 up_write(&new_cred->thread_keyring->sem);
422 * Handle the fsgid changing.
424 void key_fsgid_changed(struct cred *new_cred)
426 /* update the ownership of the thread keyring */
427 if (new_cred->thread_keyring) {
428 down_write(&new_cred->thread_keyring->sem);
429 new_cred->thread_keyring->gid = new_cred->fsgid;
430 up_write(&new_cred->thread_keyring->sem);
435 * Search the process keyrings attached to the supplied cred for the first
436 * matching key under RCU conditions (the caller must be holding the RCU read
439 * The search criteria are the type and the match function. The description is
440 * given to the match function as a parameter, but doesn't otherwise influence
441 * the search. Typically the match function will compare the description
442 * parameter to the key's description.
444 * This can only search keyrings that grant Search permission to the supplied
445 * credentials. Keyrings linked to searched keyrings will also be searched if
446 * they grant Search permission too. Keys can only be found if they grant
447 * Search permission to the credentials.
449 * Returns a pointer to the key with the key usage count incremented if
450 * successful, -EAGAIN if we didn't find any matching key or -ENOKEY if we only
451 * matched negative keys.
453 * In the case of a successful return, the possession attribute is set on the
454 * returned key reference.
456 key_ref_t search_cred_keyrings_rcu(struct keyring_search_context *ctx)
458 struct key *user_session;
459 key_ref_t key_ref, ret, err;
460 const struct cred *cred = ctx->cred;
462 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
463 * searchable, but we failed to find a key or we found a negative key;
464 * otherwise we want to return a sample error (probably -EACCES) if
465 * none of the keyrings were searchable
467 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
471 err = ERR_PTR(-EAGAIN);
473 /* search the thread keyring first */
474 if (cred->thread_keyring) {
475 key_ref = keyring_search_rcu(
476 make_key_ref(cred->thread_keyring, 1), ctx);
477 if (!IS_ERR(key_ref))
480 switch (PTR_ERR(key_ref)) {
481 case -EAGAIN: /* no key */
482 case -ENOKEY: /* negative key */
491 /* search the process keyring second */
492 if (cred->process_keyring) {
493 key_ref = keyring_search_rcu(
494 make_key_ref(cred->process_keyring, 1), ctx);
495 if (!IS_ERR(key_ref))
498 switch (PTR_ERR(key_ref)) {
499 case -EAGAIN: /* no key */
503 case -ENOKEY: /* negative key */
512 /* search the session keyring */
513 if (cred->session_keyring) {
514 key_ref = keyring_search_rcu(
515 make_key_ref(cred->session_keyring, 1), ctx);
517 if (!IS_ERR(key_ref))
520 switch (PTR_ERR(key_ref)) {
521 case -EAGAIN: /* no key */
525 case -ENOKEY: /* negative key */
533 /* or search the user-session keyring */
534 else if ((user_session = get_user_session_keyring_rcu(cred))) {
535 key_ref = keyring_search_rcu(make_key_ref(user_session, 1),
537 key_put(user_session);
539 if (!IS_ERR(key_ref))
542 switch (PTR_ERR(key_ref)) {
543 case -EAGAIN: /* no key */
547 case -ENOKEY: /* negative key */
556 /* no key - decide on the error we're going to go for */
557 key_ref = ret ? ret : err;
564 * Search the process keyrings attached to the supplied cred for the first
565 * matching key in the manner of search_my_process_keyrings(), but also search
566 * the keys attached to the assumed authorisation key using its credentials if
569 * The caller must be holding the RCU read lock.
571 * Return same as search_cred_keyrings_rcu().
573 key_ref_t search_process_keyrings_rcu(struct keyring_search_context *ctx)
575 struct request_key_auth *rka;
576 key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
578 key_ref = search_cred_keyrings_rcu(ctx);
579 if (!IS_ERR(key_ref))
583 /* if this process has an instantiation authorisation key, then we also
584 * search the keyrings of the process mentioned there
585 * - we don't permit access to request_key auth keys via this method
587 if (ctx->cred->request_key_auth &&
588 ctx->cred == current_cred() &&
589 ctx->index_key.type != &key_type_request_key_auth
591 const struct cred *cred = ctx->cred;
593 if (key_validate(cred->request_key_auth) == 0) {
594 rka = ctx->cred->request_key_auth->payload.data[0];
596 //// was search_process_keyrings() [ie. recursive]
597 ctx->cred = rka->cred;
598 key_ref = search_cred_keyrings_rcu(ctx);
601 if (!IS_ERR(key_ref))
607 /* no key - decide on the error we're going to go for */
608 if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY))
609 key_ref = ERR_PTR(-ENOKEY);
610 else if (err == ERR_PTR(-EACCES))
619 * See if the key we're looking at is the target key.
621 bool lookup_user_key_possessed(const struct key *key,
622 const struct key_match_data *match_data)
624 return key == match_data->raw_data;
628 * Look up a key ID given us by userspace with a given permissions mask to get
629 * the key it refers to.
631 * Flags can be passed to request that special keyrings be created if referred
632 * to directly, to permit partially constructed keys to be found and to skip
633 * validity and permission checks on the found key.
635 * Returns a pointer to the key with an incremented usage count if successful;
636 * -EINVAL if the key ID is invalid; -ENOKEY if the key ID does not correspond
637 * to a key or the best found key was a negative key; -EKEYREVOKED or
638 * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the
639 * found key doesn't grant the requested permit or the LSM denied access to it;
640 * or -ENOMEM if a special keyring couldn't be created.
642 * In the case of a successful return, the possession attribute is set on the
643 * returned key reference.
645 key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
646 unsigned int desired_perm)
648 struct keyring_search_context ctx = {
649 .match_data.cmp = lookup_user_key_possessed,
650 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
651 .flags = (KEYRING_SEARCH_NO_STATE_CHECK |
652 KEYRING_SEARCH_RECURSE),
654 struct request_key_auth *rka;
655 struct key *key, *user_session;
656 key_ref_t key_ref, skey_ref;
660 ctx.cred = get_current_cred();
661 key_ref = ERR_PTR(-ENOKEY);
664 case KEY_SPEC_THREAD_KEYRING:
665 if (!ctx.cred->thread_keyring) {
666 if (!(lflags & KEY_LOOKUP_CREATE))
669 ret = install_thread_keyring();
671 key_ref = ERR_PTR(ret);
677 key = ctx.cred->thread_keyring;
679 key_ref = make_key_ref(key, 1);
682 case KEY_SPEC_PROCESS_KEYRING:
683 if (!ctx.cred->process_keyring) {
684 if (!(lflags & KEY_LOOKUP_CREATE))
687 ret = install_process_keyring();
689 key_ref = ERR_PTR(ret);
695 key = ctx.cred->process_keyring;
697 key_ref = make_key_ref(key, 1);
700 case KEY_SPEC_SESSION_KEYRING:
701 if (!ctx.cred->session_keyring) {
702 /* always install a session keyring upon access if one
703 * doesn't exist yet */
704 ret = look_up_user_keyrings(NULL, &user_session);
707 if (lflags & KEY_LOOKUP_CREATE)
708 ret = join_session_keyring(NULL);
710 ret = install_session_keyring(user_session);
712 key_put(user_session);
716 } else if (test_bit(KEY_FLAG_UID_KEYRING,
717 &ctx.cred->session_keyring->flags) &&
718 lflags & KEY_LOOKUP_CREATE) {
719 ret = join_session_keyring(NULL);
725 key = ctx.cred->session_keyring;
727 key_ref = make_key_ref(key, 1);
730 case KEY_SPEC_USER_KEYRING:
731 ret = look_up_user_keyrings(&key, NULL);
734 key_ref = make_key_ref(key, 1);
737 case KEY_SPEC_USER_SESSION_KEYRING:
738 ret = look_up_user_keyrings(NULL, &key);
741 key_ref = make_key_ref(key, 1);
744 case KEY_SPEC_GROUP_KEYRING:
745 /* group keyrings are not yet supported */
746 key_ref = ERR_PTR(-EINVAL);
749 case KEY_SPEC_REQKEY_AUTH_KEY:
750 key = ctx.cred->request_key_auth;
755 key_ref = make_key_ref(key, 1);
758 case KEY_SPEC_REQUESTOR_KEYRING:
759 if (!ctx.cred->request_key_auth)
762 down_read(&ctx.cred->request_key_auth->sem);
763 if (test_bit(KEY_FLAG_REVOKED,
764 &ctx.cred->request_key_auth->flags)) {
765 key_ref = ERR_PTR(-EKEYREVOKED);
768 rka = ctx.cred->request_key_auth->payload.data[0];
769 key = rka->dest_keyring;
772 up_read(&ctx.cred->request_key_auth->sem);
775 key_ref = make_key_ref(key, 1);
779 key_ref = ERR_PTR(-EINVAL);
783 key = key_lookup(id);
785 key_ref = ERR_CAST(key);
789 key_ref = make_key_ref(key, 0);
791 /* check to see if we possess the key */
792 ctx.index_key = key->index_key;
793 ctx.match_data.raw_data = key;
794 kdebug("check possessed");
796 skey_ref = search_process_keyrings_rcu(&ctx);
798 kdebug("possessed=%p", skey_ref);
800 if (!IS_ERR(skey_ref)) {
808 /* unlink does not use the nominated key in any way, so can skip all
809 * the permission checks as it is only concerned with the keyring */
810 if (lflags & KEY_LOOKUP_FOR_UNLINK) {
815 if (!(lflags & KEY_LOOKUP_PARTIAL)) {
816 ret = wait_for_key_construction(key, true);
826 } else if (desired_perm) {
827 ret = key_validate(key);
833 if (!(lflags & KEY_LOOKUP_PARTIAL) &&
834 key_read_state(key) == KEY_IS_UNINSTANTIATED)
837 /* check the permissions */
839 ret = key_task_permission(key_ref, ctx.cred, desired_perm);
844 key->last_used_at = ktime_get_real_seconds();
851 key_ref_put(key_ref);
852 key_ref = ERR_PTR(ret);
855 /* if we attempted to install a keyring, then it may have caused new
856 * creds to be installed */
861 EXPORT_SYMBOL(lookup_user_key);
864 * Join the named keyring as the session keyring if possible else attempt to
865 * create a new one of that name and join that.
867 * If the name is NULL, an empty anonymous keyring will be installed as the
870 * Named session keyrings are joined with a semaphore held to prevent the
871 * keyrings from going away whilst the attempt is made to going them and also
872 * to prevent a race in creating compatible session keyrings.
874 long join_session_keyring(const char *name)
876 const struct cred *old;
881 new = prepare_creds();
884 old = current_cred();
886 /* if no name is provided, install an anonymous keyring */
888 ret = install_session_keyring_to_cred(new, NULL);
892 serial = new->session_keyring->serial;
893 ret = commit_creds(new);
899 /* allow the user to join or create a named keyring */
900 mutex_lock(&key_session_mutex);
902 /* look for an existing keyring of this name */
903 keyring = find_keyring_by_name(name, false);
904 if (PTR_ERR(keyring) == -ENOKEY) {
905 /* not found - try and create a new one */
906 keyring = keyring_alloc(
907 name, old->uid, old->gid, old, &joinable_keyring_acl,
908 KEY_ALLOC_IN_QUOTA, NULL, NULL);
909 if (IS_ERR(keyring)) {
910 ret = PTR_ERR(keyring);
914 } else if (IS_ERR(keyring)) {
915 ret = PTR_ERR(keyring);
917 } else if (keyring == new->session_keyring) {
922 ret = key_task_permission(make_key_ref(keyring, false), old,
928 /* we've got a keyring - now to install it */
929 ret = install_session_keyring_to_cred(new, keyring);
934 mutex_unlock(&key_session_mutex);
936 ret = keyring->serial;
944 mutex_unlock(&key_session_mutex);
951 * Replace a process's session keyring on behalf of one of its children when
952 * the target process is about to resume userspace execution.
954 void key_change_session_keyring(struct callback_head *twork)
956 const struct cred *old = current_cred();
957 struct cred *new = container_of(twork, struct cred, rcu);
959 if (unlikely(current->flags & PF_EXITING)) {
964 new-> uid = old-> uid;
965 new-> euid = old-> euid;
966 new-> suid = old-> suid;
967 new->fsuid = old->fsuid;
968 new-> gid = old-> gid;
969 new-> egid = old-> egid;
970 new-> sgid = old-> sgid;
971 new->fsgid = old->fsgid;
972 new->user = get_uid(old->user);
973 new->user_ns = get_user_ns(old->user_ns);
974 new->group_info = get_group_info(old->group_info);
976 new->securebits = old->securebits;
977 new->cap_inheritable = old->cap_inheritable;
978 new->cap_permitted = old->cap_permitted;
979 new->cap_effective = old->cap_effective;
980 new->cap_ambient = old->cap_ambient;
981 new->cap_bset = old->cap_bset;
983 new->jit_keyring = old->jit_keyring;
984 new->thread_keyring = key_get(old->thread_keyring);
985 new->process_keyring = key_get(old->process_keyring);
987 security_transfer_creds(new, old);
993 * Make sure that root's user and user-session keyrings exist.
995 static int __init init_root_keyring(void)
997 return look_up_user_keyrings(NULL, NULL);
1000 late_initcall(init_root_keyring);