GNU Linux-libre 4.19.245-gnu1
[releases.git] / security / keys / process_keys.c
1 /* Manage a process's keyrings
2  *
3  * Copyright (C) 2004-2005, 2008 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
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.
10  */
11
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>
17 #include <linux/fs.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>
24 #include "internal.h"
25
26 /* Session keyring create vs join semaphore */
27 static DEFINE_MUTEX(key_session_mutex);
28
29 /* User keyring creation semaphore */
30 static DEFINE_MUTEX(key_user_keyring_mutex);
31
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,
40 };
41
42 /*
43  * Install the user and user session keyrings for the current process's UID.
44  */
45 int install_user_keyrings(void)
46 {
47         struct user_struct *user;
48         const struct cred *cred;
49         struct key *uid_keyring, *session_keyring;
50         key_perm_t user_keyring_perm;
51         char buf[20];
52         int ret;
53         uid_t uid;
54
55         user_keyring_perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL;
56         cred = current_cred();
57         user = cred->user;
58         uid = from_kuid(cred->user_ns, user->uid);
59
60         kenter("%p{%u}", user, uid);
61
62         if (user->uid_keyring && user->session_keyring) {
63                 kleave(" = 0 [exist]");
64                 return 0;
65         }
66
67         mutex_lock(&key_user_keyring_mutex);
68         ret = 0;
69
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);
76
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 |
82                                                         KEY_ALLOC_IN_QUOTA,
83                                                     NULL, NULL);
84                         if (IS_ERR(uid_keyring)) {
85                                 ret = PTR_ERR(uid_keyring);
86                                 goto error;
87                         }
88                 }
89
90                 /* get a default session keyring (which might also exist
91                  * already) */
92                 sprintf(buf, "_uid_ses.%u", uid);
93
94                 session_keyring = find_keyring_by_name(buf, true);
95                 if (IS_ERR(session_keyring)) {
96                         session_keyring =
97                                 keyring_alloc(buf, user->uid, INVALID_GID,
98                                               cred, user_keyring_perm,
99                                               KEY_ALLOC_UID_KEYRING |
100                                                   KEY_ALLOC_IN_QUOTA,
101                                               NULL, NULL);
102                         if (IS_ERR(session_keyring)) {
103                                 ret = PTR_ERR(session_keyring);
104                                 goto error_release;
105                         }
106
107                         /* we install a link from the user session keyring to
108                          * the user keyring */
109                         ret = key_link(session_keyring, uid_keyring);
110                         if (ret < 0)
111                                 goto error_release_both;
112                 }
113
114                 /* install the keyrings */
115                 user->uid_keyring = uid_keyring;
116                 user->session_keyring = session_keyring;
117         }
118
119         mutex_unlock(&key_user_keyring_mutex);
120         kleave(" = 0");
121         return 0;
122
123 error_release_both:
124         key_put(session_keyring);
125 error_release:
126         key_put(uid_keyring);
127 error:
128         mutex_unlock(&key_user_keyring_mutex);
129         kleave(" = %d", ret);
130         return ret;
131 }
132
133 /*
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.
136  *
137  * Return: 0 if a thread keyring is now present; -errno on failure.
138  */
139 int install_thread_keyring_to_cred(struct cred *new)
140 {
141         struct key *keyring;
142
143         if (new->thread_keyring)
144                 return 0;
145
146         keyring = keyring_alloc("_tid", new->uid, new->gid, new,
147                                 KEY_POS_ALL | KEY_USR_VIEW,
148                                 KEY_ALLOC_QUOTA_OVERRUN,
149                                 NULL, NULL);
150         if (IS_ERR(keyring))
151                 return PTR_ERR(keyring);
152
153         new->thread_keyring = keyring;
154         return 0;
155 }
156
157 /*
158  * Install a thread keyring to the current task if it didn't have one already.
159  *
160  * Return: 0 if a thread keyring is now present; -errno on failure.
161  */
162 static int install_thread_keyring(void)
163 {
164         struct cred *new;
165         int ret;
166
167         new = prepare_creds();
168         if (!new)
169                 return -ENOMEM;
170
171         ret = install_thread_keyring_to_cred(new);
172         if (ret < 0) {
173                 abort_creds(new);
174                 return ret;
175         }
176
177         return commit_creds(new);
178 }
179
180 /*
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.
183  *
184  * Return: 0 if a process keyring is now present; -errno on failure.
185  */
186 int install_process_keyring_to_cred(struct cred *new)
187 {
188         struct key *keyring;
189
190         if (new->process_keyring)
191                 return 0;
192
193         keyring = keyring_alloc("_pid", new->uid, new->gid, new,
194                                 KEY_POS_ALL | KEY_USR_VIEW,
195                                 KEY_ALLOC_QUOTA_OVERRUN,
196                                 NULL, NULL);
197         if (IS_ERR(keyring))
198                 return PTR_ERR(keyring);
199
200         new->process_keyring = keyring;
201         return 0;
202 }
203
204 /*
205  * Install a process keyring to the current task if it didn't have one already.
206  *
207  * Return: 0 if a process keyring is now present; -errno on failure.
208  */
209 static int install_process_keyring(void)
210 {
211         struct cred *new;
212         int ret;
213
214         new = prepare_creds();
215         if (!new)
216                 return -ENOMEM;
217
218         ret = install_process_keyring_to_cred(new);
219         if (ret < 0) {
220                 abort_creds(new);
221                 return ret;
222         }
223
224         return commit_creds(new);
225 }
226
227 /*
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.
231  *
232  * Return: 0 on success; -errno on failure.
233  */
234 int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
235 {
236         unsigned long flags;
237         struct key *old;
238
239         might_sleep();
240
241         /* create an empty session keyring */
242         if (!keyring) {
243                 flags = KEY_ALLOC_QUOTA_OVERRUN;
244                 if (cred->session_keyring)
245                         flags = KEY_ALLOC_IN_QUOTA;
246
247                 keyring = keyring_alloc("_ses", cred->uid, cred->gid, cred,
248                                         KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ,
249                                         flags, NULL, NULL);
250                 if (IS_ERR(keyring))
251                         return PTR_ERR(keyring);
252         } else {
253                 __key_get(keyring);
254         }
255
256         /* install the keyring */
257         old = cred->session_keyring;
258         rcu_assign_pointer(cred->session_keyring, keyring);
259
260         if (old)
261                 key_put(old);
262
263         return 0;
264 }
265
266 /*
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.
270  *
271  * Return: 0 on success; -errno on failure.
272  */
273 static int install_session_keyring(struct key *keyring)
274 {
275         struct cred *new;
276         int ret;
277
278         new = prepare_creds();
279         if (!new)
280                 return -ENOMEM;
281
282         ret = install_session_keyring_to_cred(new, keyring);
283         if (ret < 0) {
284                 abort_creds(new);
285                 return ret;
286         }
287
288         return commit_creds(new);
289 }
290
291 /*
292  * Handle the fsuid changing.
293  */
294 void key_fsuid_changed(struct task_struct *tsk)
295 {
296         /* update the ownership of the thread keyring */
297         BUG_ON(!tsk->cred);
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);
302         }
303 }
304
305 /*
306  * Handle the fsgid changing.
307  */
308 void key_fsgid_changed(struct task_struct *tsk)
309 {
310         /* update the ownership of the thread keyring */
311         BUG_ON(!tsk->cred);
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);
316         }
317 }
318
319 /*
320  * Search the process keyrings attached to the supplied cred for the first
321  * matching key.
322  *
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.
327  *
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.
332  *
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.
336  *
337  * In the case of a successful return, the possession attribute is set on the
338  * returned key reference.
339  */
340 key_ref_t search_my_process_keyrings(struct keyring_search_context *ctx)
341 {
342         key_ref_t key_ref, ret, err;
343
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
348          *
349          * in terms of priority: success > -ENOKEY > -EAGAIN > other error
350          */
351         key_ref = NULL;
352         ret = NULL;
353         err = ERR_PTR(-EAGAIN);
354
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))
360                         goto found;
361
362                 switch (PTR_ERR(key_ref)) {
363                 case -EAGAIN: /* no key */
364                 case -ENOKEY: /* negative key */
365                         ret = key_ref;
366                         break;
367                 default:
368                         err = key_ref;
369                         break;
370                 }
371         }
372
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))
378                         goto found;
379
380                 switch (PTR_ERR(key_ref)) {
381                 case -EAGAIN: /* no key */
382                         if (ret)
383                                 break;
384                 case -ENOKEY: /* negative key */
385                         ret = key_ref;
386                         break;
387                 default:
388                         err = key_ref;
389                         break;
390                 }
391         }
392
393         /* search the session keyring */
394         if (ctx->cred->session_keyring) {
395                 rcu_read_lock();
396                 key_ref = keyring_search_aux(
397                         make_key_ref(rcu_dereference(ctx->cred->session_keyring), 1),
398                         ctx);
399                 rcu_read_unlock();
400
401                 if (!IS_ERR(key_ref))
402                         goto found;
403
404                 switch (PTR_ERR(key_ref)) {
405                 case -EAGAIN: /* no key */
406                         if (ret)
407                                 break;
408                 case -ENOKEY: /* negative key */
409                         ret = key_ref;
410                         break;
411                 default:
412                         err = key_ref;
413                         break;
414                 }
415         }
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),
420                         ctx);
421                 if (!IS_ERR(key_ref))
422                         goto found;
423
424                 switch (PTR_ERR(key_ref)) {
425                 case -EAGAIN: /* no key */
426                         if (ret)
427                                 break;
428                 case -ENOKEY: /* negative key */
429                         ret = key_ref;
430                         break;
431                 default:
432                         err = key_ref;
433                         break;
434                 }
435         }
436
437         /* no key - decide on the error we're going to go for */
438         key_ref = ret ? ret : err;
439
440 found:
441         return key_ref;
442 }
443
444 /*
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
448  * one is available.
449  *
450  * Return same as search_my_process_keyrings().
451  */
452 key_ref_t search_process_keyrings(struct keyring_search_context *ctx)
453 {
454         struct request_key_auth *rka;
455         key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
456
457         might_sleep();
458
459         key_ref = search_my_process_keyrings(ctx);
460         if (!IS_ERR(key_ref))
461                 goto found;
462         err = key_ref;
463
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
467          */
468         if (ctx->cred->request_key_auth &&
469             ctx->cred == current_cred() &&
470             ctx->index_key.type != &key_type_request_key_auth
471             ) {
472                 const struct cred *cred = ctx->cred;
473
474                 /* defend against the auth key being revoked */
475                 down_read(&cred->request_key_auth->sem);
476
477                 if (key_validate(ctx->cred->request_key_auth) == 0) {
478                         rka = ctx->cred->request_key_auth->payload.data[0];
479
480                         ctx->cred = rka->cred;
481                         key_ref = search_process_keyrings(ctx);
482                         ctx->cred = cred;
483
484                         up_read(&cred->request_key_auth->sem);
485
486                         if (!IS_ERR(key_ref))
487                                 goto found;
488
489                         ret = key_ref;
490                 } else {
491                         up_read(&cred->request_key_auth->sem);
492                 }
493         }
494
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))
499                 key_ref = ret;
500         else
501                 key_ref = err;
502
503 found:
504         return key_ref;
505 }
506
507 /*
508  * See if the key we're looking at is the target key.
509  */
510 bool lookup_user_key_possessed(const struct key *key,
511                                const struct key_match_data *match_data)
512 {
513         return key == match_data->raw_data;
514 }
515
516 /*
517  * Look up a key ID given us by userspace with a given permissions mask to get
518  * the key it refers to.
519  *
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.
523  *
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.
530  *
531  * In the case of a successful return, the possession attribute is set on the
532  * returned key reference.
533  */
534 key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
535                           key_perm_t perm)
536 {
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,
541         };
542         struct request_key_auth *rka;
543         struct key *key;
544         key_ref_t key_ref, skey_ref;
545         int ret;
546
547 try_again:
548         ctx.cred = get_current_cred();
549         key_ref = ERR_PTR(-ENOKEY);
550
551         switch (id) {
552         case KEY_SPEC_THREAD_KEYRING:
553                 if (!ctx.cred->thread_keyring) {
554                         if (!(lflags & KEY_LOOKUP_CREATE))
555                                 goto error;
556
557                         ret = install_thread_keyring();
558                         if (ret < 0) {
559                                 key_ref = ERR_PTR(ret);
560                                 goto error;
561                         }
562                         goto reget_creds;
563                 }
564
565                 key = ctx.cred->thread_keyring;
566                 __key_get(key);
567                 key_ref = make_key_ref(key, 1);
568                 break;
569
570         case KEY_SPEC_PROCESS_KEYRING:
571                 if (!ctx.cred->process_keyring) {
572                         if (!(lflags & KEY_LOOKUP_CREATE))
573                                 goto error;
574
575                         ret = install_process_keyring();
576                         if (ret < 0) {
577                                 key_ref = ERR_PTR(ret);
578                                 goto error;
579                         }
580                         goto reget_creds;
581                 }
582
583                 key = ctx.cred->process_keyring;
584                 __key_get(key);
585                 key_ref = make_key_ref(key, 1);
586                 break;
587
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();
593                         if (ret < 0)
594                                 goto error;
595                         if (lflags & KEY_LOOKUP_CREATE)
596                                 ret = join_session_keyring(NULL);
597                         else
598                                 ret = install_session_keyring(
599                                         ctx.cred->user->session_keyring);
600
601                         if (ret < 0)
602                                 goto error;
603                         goto reget_creds;
604                 } else if (ctx.cred->session_keyring ==
605                            ctx.cred->user->session_keyring &&
606                            lflags & KEY_LOOKUP_CREATE) {
607                         ret = join_session_keyring(NULL);
608                         if (ret < 0)
609                                 goto error;
610                         goto reget_creds;
611                 }
612
613                 rcu_read_lock();
614                 key = rcu_dereference(ctx.cred->session_keyring);
615                 __key_get(key);
616                 rcu_read_unlock();
617                 key_ref = make_key_ref(key, 1);
618                 break;
619
620         case KEY_SPEC_USER_KEYRING:
621                 if (!ctx.cred->user->uid_keyring) {
622                         ret = install_user_keyrings();
623                         if (ret < 0)
624                                 goto error;
625                 }
626
627                 key = ctx.cred->user->uid_keyring;
628                 __key_get(key);
629                 key_ref = make_key_ref(key, 1);
630                 break;
631
632         case KEY_SPEC_USER_SESSION_KEYRING:
633                 if (!ctx.cred->user->session_keyring) {
634                         ret = install_user_keyrings();
635                         if (ret < 0)
636                                 goto error;
637                 }
638
639                 key = ctx.cred->user->session_keyring;
640                 __key_get(key);
641                 key_ref = make_key_ref(key, 1);
642                 break;
643
644         case KEY_SPEC_GROUP_KEYRING:
645                 /* group keyrings are not yet supported */
646                 key_ref = ERR_PTR(-EINVAL);
647                 goto error;
648
649         case KEY_SPEC_REQKEY_AUTH_KEY:
650                 key = ctx.cred->request_key_auth;
651                 if (!key)
652                         goto error;
653
654                 __key_get(key);
655                 key_ref = make_key_ref(key, 1);
656                 break;
657
658         case KEY_SPEC_REQUESTOR_KEYRING:
659                 if (!ctx.cred->request_key_auth)
660                         goto error;
661
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);
666                         key = NULL;
667                 } else {
668                         rka = ctx.cred->request_key_auth->payload.data[0];
669                         key = rka->dest_keyring;
670                         __key_get(key);
671                 }
672                 up_read(&ctx.cred->request_key_auth->sem);
673                 if (!key)
674                         goto error;
675                 key_ref = make_key_ref(key, 1);
676                 break;
677
678         default:
679                 key_ref = ERR_PTR(-EINVAL);
680                 if (id < 1)
681                         goto error;
682
683                 key = key_lookup(id);
684                 if (IS_ERR(key)) {
685                         key_ref = ERR_CAST(key);
686                         goto error;
687                 }
688
689                 key_ref = make_key_ref(key, 0);
690
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);
699
700                 if (!IS_ERR(skey_ref)) {
701                         key_put(key);
702                         key_ref = skey_ref;
703                 }
704
705                 break;
706         }
707
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) {
711                 ret = 0;
712                 goto error;
713         }
714
715         if (!(lflags & KEY_LOOKUP_PARTIAL)) {
716                 ret = wait_for_key_construction(key, true);
717                 switch (ret) {
718                 case -ERESTARTSYS:
719                         goto invalid_key;
720                 default:
721                         if (perm)
722                                 goto invalid_key;
723                 case 0:
724                         break;
725                 }
726         } else if (perm) {
727                 ret = key_validate(key);
728                 if (ret < 0)
729                         goto invalid_key;
730         }
731
732         ret = -EIO;
733         if (!(lflags & KEY_LOOKUP_PARTIAL) &&
734             key_read_state(key) == KEY_IS_UNINSTANTIATED)
735                 goto invalid_key;
736
737         /* check the permissions */
738         ret = key_task_permission(key_ref, ctx.cred, perm);
739         if (ret < 0)
740                 goto invalid_key;
741
742         key->last_used_at = ktime_get_real_seconds();
743
744 error:
745         put_cred(ctx.cred);
746         return key_ref;
747
748 invalid_key:
749         key_ref_put(key_ref);
750         key_ref = ERR_PTR(ret);
751         goto error;
752
753         /* if we attempted to install a keyring, then it may have caused new
754          * creds to be installed */
755 reget_creds:
756         put_cred(ctx.cred);
757         goto try_again;
758 }
759
760 /*
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.
763  *
764  * If the name is NULL, an empty anonymous keyring will be installed as the
765  * session keyring.
766  *
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.
770  */
771 long join_session_keyring(const char *name)
772 {
773         const struct cred *old;
774         struct cred *new;
775         struct key *keyring;
776         long ret, serial;
777
778         new = prepare_creds();
779         if (!new)
780                 return -ENOMEM;
781         old = current_cred();
782
783         /* if no name is provided, install an anonymous keyring */
784         if (!name) {
785                 ret = install_session_keyring_to_cred(new, NULL);
786                 if (ret < 0)
787                         goto error;
788
789                 serial = new->session_keyring->serial;
790                 ret = commit_creds(new);
791                 if (ret == 0)
792                         ret = serial;
793                 goto okay;
794         }
795
796         /* allow the user to join or create a named keyring */
797         mutex_lock(&key_session_mutex);
798
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);
809                         goto error2;
810                 }
811         } else if (IS_ERR(keyring)) {
812                 ret = PTR_ERR(keyring);
813                 goto error2;
814         } else if (keyring == new->session_keyring) {
815                 ret = 0;
816                 goto error3;
817         }
818
819         /* we've got a keyring - now to install it */
820         ret = install_session_keyring_to_cred(new, keyring);
821         if (ret < 0)
822                 goto error3;
823
824         commit_creds(new);
825         mutex_unlock(&key_session_mutex);
826
827         ret = keyring->serial;
828         key_put(keyring);
829 okay:
830         return ret;
831
832 error3:
833         key_put(keyring);
834 error2:
835         mutex_unlock(&key_session_mutex);
836 error:
837         abort_creds(new);
838         return ret;
839 }
840
841 /*
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.
844  */
845 void key_change_session_keyring(struct callback_head *twork)
846 {
847         const struct cred *old = current_cred();
848         struct cred *new = container_of(twork, struct cred, rcu);
849
850         if (unlikely(current->flags & PF_EXITING)) {
851                 put_cred(new);
852                 return;
853         }
854
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);
866
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;
873
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);
877
878         security_transfer_creds(new, old);
879
880         commit_creds(new);
881 }
882
883 /*
884  * Make sure that root's user and user-session keyrings exist.
885  */
886 static int __init init_root_keyring(void)
887 {
888         return install_user_keyrings();
889 }
890
891 late_initcall(init_root_keyring);