fe5719f793daa0d70b745f42edbc735beb71a36c
[pandora-kernel.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/keyctl.h>
16 #include <linux/fs.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>
22 #include "internal.h"
23
24 /* Session keyring create vs join semaphore */
25 static DEFINE_MUTEX(key_session_mutex);
26
27 /* User keyring creation semaphore */
28 static DEFINE_MUTEX(key_user_keyring_mutex);
29
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),
37         .uid            = 0,
38         .user_ns        = &init_user_ns,
39 };
40
41 /*
42  * Install the user and user session keyrings for the current process's UID.
43  */
44 int install_user_keyrings(void)
45 {
46         struct user_struct *user;
47         const struct cred *cred;
48         struct key *uid_keyring, *session_keyring;
49         char buf[20];
50         int ret;
51
52         cred = current_cred();
53         user = cred->user;
54
55         kenter("%p{%u}", user, user->uid);
56
57         if (user->uid_keyring && user->session_keyring) {
58                 kleave(" = 0 [exist]");
59                 return 0;
60         }
61
62         mutex_lock(&key_user_keyring_mutex);
63         ret = 0;
64
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);
71
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,
76                                                     NULL);
77                         if (IS_ERR(uid_keyring)) {
78                                 ret = PTR_ERR(uid_keyring);
79                                 goto error;
80                         }
81                 }
82
83                 /* get a default session keyring (which might also exist
84                  * already) */
85                 sprintf(buf, "_uid_ses.%u", user->uid);
86
87                 session_keyring = find_keyring_by_name(buf, true);
88                 if (IS_ERR(session_keyring)) {
89                         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);
94                                 goto error_release;
95                         }
96
97                         /* we install a link from the user session keyring to
98                          * the user keyring */
99                         ret = key_link(session_keyring, uid_keyring);
100                         if (ret < 0)
101                                 goto error_release_both;
102                 }
103
104                 /* install the keyrings */
105                 user->uid_keyring = uid_keyring;
106                 user->session_keyring = session_keyring;
107         }
108
109         mutex_unlock(&key_user_keyring_mutex);
110         kleave(" = 0");
111         return 0;
112
113 error_release_both:
114         key_put(session_keyring);
115 error_release:
116         key_put(uid_keyring);
117 error:
118         mutex_unlock(&key_user_keyring_mutex);
119         kleave(" = %d", ret);
120         return ret;
121 }
122
123 /*
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.
126  *
127  * Return: 0 if a thread keyring is now present; -errno on failure.
128  */
129 int install_thread_keyring_to_cred(struct cred *new)
130 {
131         struct key *keyring;
132
133         if (new->thread_keyring)
134                 return 0;
135
136         keyring = keyring_alloc("_tid", new->uid, new->gid, new,
137                                 KEY_ALLOC_QUOTA_OVERRUN, NULL);
138         if (IS_ERR(keyring))
139                 return PTR_ERR(keyring);
140
141         new->thread_keyring = keyring;
142         return 0;
143 }
144
145 /*
146  * Install a thread keyring to the current task if it didn't have one already.
147  *
148  * Return: 0 if a thread keyring is now present; -errno on failure.
149  */
150 static int install_thread_keyring(void)
151 {
152         struct cred *new;
153         int ret;
154
155         new = prepare_creds();
156         if (!new)
157                 return -ENOMEM;
158
159         ret = install_thread_keyring_to_cred(new);
160         if (ret < 0) {
161                 abort_creds(new);
162                 return ret;
163         }
164
165         return commit_creds(new);
166 }
167
168 /*
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.
171  *
172  * Return: 0 if a process keyring is now present; -errno on failure.
173  */
174 int install_process_keyring_to_cred(struct cred *new)
175 {
176         struct key *keyring;
177         int ret;
178
179         if (new->tgcred->process_keyring)
180                 return 0;
181
182         keyring = keyring_alloc("_pid", new->uid, new->gid,
183                                 new, KEY_ALLOC_QUOTA_OVERRUN, NULL);
184         if (IS_ERR(keyring))
185                 return PTR_ERR(keyring);
186
187         spin_lock_irq(&new->tgcred->lock);
188         if (!new->tgcred->process_keyring) {
189                 new->tgcred->process_keyring = keyring;
190                 keyring = NULL;
191                 ret = 0;
192         } else {
193                 ret = -EEXIST;
194         }
195         spin_unlock_irq(&new->tgcred->lock);
196         key_put(keyring);
197         return ret;
198 }
199
200 /*
201  * Install a process keyring to the current task if it didn't have one already.
202  *
203  * Return: 0 if a process keyring is now present; -errno on failure.
204  */
205 static int install_process_keyring(void)
206 {
207         struct cred *new;
208         int ret;
209
210         new = prepare_creds();
211         if (!new)
212                 return -ENOMEM;
213
214         ret = install_process_keyring_to_cred(new);
215         if (ret < 0) {
216                 abort_creds(new);
217                 return ret;
218         }
219
220         return commit_creds(new);
221 }
222
223 /*
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.
227  *
228  * Return: 0 on success; -errno on failure.
229  */
230 int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
231 {
232         unsigned long flags;
233         struct key *old;
234
235         might_sleep();
236
237         /* create an empty session keyring */
238         if (!keyring) {
239                 flags = KEY_ALLOC_QUOTA_OVERRUN;
240                 if (cred->tgcred->session_keyring)
241                         flags = KEY_ALLOC_IN_QUOTA;
242
243                 keyring = keyring_alloc("_ses", cred->uid, cred->gid,
244                                         cred, flags, NULL);
245                 if (IS_ERR(keyring))
246                         return PTR_ERR(keyring);
247         } else {
248                 atomic_inc(&keyring->usage);
249         }
250
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);
256
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 */
259         if (old) {
260                 synchronize_rcu();
261                 key_put(old);
262         }
263
264         return 0;
265 }
266
267 /*
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.
271  *
272  * Return: 0 on success; -errno on failure.
273  */
274 static int install_session_keyring(struct key *keyring)
275 {
276         struct cred *new;
277         int ret;
278
279         new = prepare_creds();
280         if (!new)
281                 return -ENOMEM;
282
283         ret = install_session_keyring_to_cred(new, keyring);
284         if (ret < 0) {
285                 abort_creds(new);
286                 return ret;
287         }
288
289         return commit_creds(new);
290 }
291
292 /*
293  * Handle the fsuid changing.
294  */
295 void key_fsuid_changed(struct task_struct *tsk)
296 {
297         /* update the ownership of the thread keyring */
298         BUG_ON(!tsk->cred);
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);
303         }
304 }
305
306 /*
307  * Handle the fsgid changing.
308  */
309 void key_fsgid_changed(struct task_struct *tsk)
310 {
311         /* update the ownership of the thread keyring */
312         BUG_ON(!tsk->cred);
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);
317         }
318 }
319
320 /*
321  * Search the process keyrings attached to the supplied cred for the first
322  * matching key.
323  *
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.
328  *
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.
333  *
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.
337  *
338  * In the case of a successful return, the possession attribute is set on the
339  * returned key reference.
340  */
341 key_ref_t search_my_process_keyrings(struct key_type *type,
342                                      const void *description,
343                                      key_match_func_t match,
344                                      bool no_state_check,
345                                      const struct cred *cred)
346 {
347         key_ref_t key_ref, ret, err;
348
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
353          *
354          * in terms of priority: success > -ENOKEY > -EAGAIN > other error
355          */
356         key_ref = NULL;
357         ret = NULL;
358         err = ERR_PTR(-EAGAIN);
359
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))
366                         goto found;
367
368                 switch (PTR_ERR(key_ref)) {
369                 case -EAGAIN: /* no key */
370                         if (ret)
371                                 break;
372                 case -ENOKEY: /* negative key */
373                         ret = key_ref;
374                         break;
375                 default:
376                         err = key_ref;
377                         break;
378                 }
379         }
380
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))
387                         goto found;
388
389                 switch (PTR_ERR(key_ref)) {
390                 case -EAGAIN: /* no key */
391                         if (ret)
392                                 break;
393                 case -ENOKEY: /* negative key */
394                         ret = key_ref;
395                         break;
396                 default:
397                         err = key_ref;
398                         break;
399                 }
400         }
401
402         /* search the session keyring */
403         if (cred->tgcred->session_keyring) {
404                 rcu_read_lock();
405                 key_ref = keyring_search_aux(
406                         make_key_ref(rcu_dereference(
407                                              cred->tgcred->session_keyring),
408                                      1),
409                         cred, type, description, match, no_state_check);
410                 rcu_read_unlock();
411
412                 if (!IS_ERR(key_ref))
413                         goto found;
414
415                 switch (PTR_ERR(key_ref)) {
416                 case -EAGAIN: /* no key */
417                         if (ret)
418                                 break;
419                 case -ENOKEY: /* negative key */
420                         ret = key_ref;
421                         break;
422                 default:
423                         err = key_ref;
424                         break;
425                 }
426         }
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))
433                         goto found;
434
435                 switch (PTR_ERR(key_ref)) {
436                 case -EAGAIN: /* no key */
437                         if (ret)
438                                 break;
439                 case -ENOKEY: /* negative key */
440                         ret = key_ref;
441                         break;
442                 default:
443                         err = key_ref;
444                         break;
445                 }
446         }
447
448         /* no key - decide on the error we're going to go for */
449         key_ref = ret ? ret : err;
450
451 found:
452         return key_ref;
453 }
454
455 /*
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
459  * one is available.
460  *
461  * Return same as search_my_process_keyrings().
462  */
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)
467 {
468         struct request_key_auth *rka;
469         key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
470
471         might_sleep();
472
473         key_ref = search_my_process_keyrings(type, description, match,
474                                              false, cred);
475         if (!IS_ERR(key_ref))
476                 goto found;
477         err = key_ref;
478
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
482          */
483         if (cred->request_key_auth &&
484             cred == current_cred() &&
485             type != &key_type_request_key_auth
486             ) {
487                 /* defend against the auth key being revoked */
488                 down_read(&cred->request_key_auth->sem);
489
490                 if (key_validate(cred->request_key_auth) == 0) {
491                         rka = cred->request_key_auth->payload.data;
492
493                         key_ref = search_process_keyrings(type, description,
494                                                           match, rka->cred);
495
496                         up_read(&cred->request_key_auth->sem);
497
498                         if (!IS_ERR(key_ref))
499                                 goto found;
500
501                         ret = key_ref;
502                 } else {
503                         up_read(&cred->request_key_auth->sem);
504                 }
505         }
506
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))
511                 key_ref = ret;
512         else
513                 key_ref = err;
514
515 found:
516         return key_ref;
517 }
518
519 /*
520  * See if the key we're looking at is the target key.
521  */
522 int lookup_user_key_possessed(const struct key *key, const void *target)
523 {
524         return key == target;
525 }
526
527 /*
528  * Look up a key ID given us by userspace with a given permissions mask to get
529  * the key it refers to.
530  *
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.
534  *
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.
541  *
542  * In the case of a successful return, the possession attribute is set on the
543  * returned key reference.
544  */
545 key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
546                           key_perm_t perm)
547 {
548         struct request_key_auth *rka;
549         const struct cred *cred;
550         struct key *key;
551         key_ref_t key_ref, skey_ref;
552         int ret;
553
554 try_again:
555         cred = get_current_cred();
556         key_ref = ERR_PTR(-ENOKEY);
557
558         switch (id) {
559         case KEY_SPEC_THREAD_KEYRING:
560                 if (!cred->thread_keyring) {
561                         if (!(lflags & KEY_LOOKUP_CREATE))
562                                 goto error;
563
564                         ret = install_thread_keyring();
565                         if (ret < 0) {
566                                 key_ref = ERR_PTR(ret);
567                                 goto error;
568                         }
569                         goto reget_creds;
570                 }
571
572                 key = cred->thread_keyring;
573                 atomic_inc(&key->usage);
574                 key_ref = make_key_ref(key, 1);
575                 break;
576
577         case KEY_SPEC_PROCESS_KEYRING:
578                 if (!cred->tgcred->process_keyring) {
579                         if (!(lflags & KEY_LOOKUP_CREATE))
580                                 goto error;
581
582                         ret = install_process_keyring();
583                         if (ret < 0) {
584                                 key_ref = ERR_PTR(ret);
585                                 goto error;
586                         }
587                         goto reget_creds;
588                 }
589
590                 key = cred->tgcred->process_keyring;
591                 atomic_inc(&key->usage);
592                 key_ref = make_key_ref(key, 1);
593                 break;
594
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();
600                         if (ret < 0)
601                                 goto error;
602                         if (lflags & KEY_LOOKUP_CREATE)
603                                 ret = join_session_keyring(NULL);
604                         else
605                                 ret = install_session_keyring(
606                                         cred->user->session_keyring);
607
608                         if (ret < 0)
609                                 goto error;
610                         goto reget_creds;
611                 } else if (cred->tgcred->session_keyring ==
612                            cred->user->session_keyring &&
613                            lflags & KEY_LOOKUP_CREATE) {
614                         ret = join_session_keyring(NULL);
615                         if (ret < 0)
616                                 goto error;
617                         goto reget_creds;
618                 }
619
620                 rcu_read_lock();
621                 key = rcu_dereference(cred->tgcred->session_keyring);
622                 atomic_inc(&key->usage);
623                 rcu_read_unlock();
624                 key_ref = make_key_ref(key, 1);
625                 break;
626
627         case KEY_SPEC_USER_KEYRING:
628                 if (!cred->user->uid_keyring) {
629                         ret = install_user_keyrings();
630                         if (ret < 0)
631                                 goto error;
632                 }
633
634                 key = cred->user->uid_keyring;
635                 atomic_inc(&key->usage);
636                 key_ref = make_key_ref(key, 1);
637                 break;
638
639         case KEY_SPEC_USER_SESSION_KEYRING:
640                 if (!cred->user->session_keyring) {
641                         ret = install_user_keyrings();
642                         if (ret < 0)
643                                 goto error;
644                 }
645
646                 key = cred->user->session_keyring;
647                 atomic_inc(&key->usage);
648                 key_ref = make_key_ref(key, 1);
649                 break;
650
651         case KEY_SPEC_GROUP_KEYRING:
652                 /* group keyrings are not yet supported */
653                 key_ref = ERR_PTR(-EINVAL);
654                 goto error;
655
656         case KEY_SPEC_REQKEY_AUTH_KEY:
657                 key = cred->request_key_auth;
658                 if (!key)
659                         goto error;
660
661                 atomic_inc(&key->usage);
662                 key_ref = make_key_ref(key, 1);
663                 break;
664
665         case KEY_SPEC_REQUESTOR_KEYRING:
666                 if (!cred->request_key_auth)
667                         goto error;
668
669                 down_read(&cred->request_key_auth->sem);
670                 if (cred->request_key_auth->flags & KEY_FLAG_REVOKED) {
671                         key_ref = ERR_PTR(-EKEYREVOKED);
672                         key = NULL;
673                 } else {
674                         rka = cred->request_key_auth->payload.data;
675                         key = rka->dest_keyring;
676                         atomic_inc(&key->usage);
677                 }
678                 up_read(&cred->request_key_auth->sem);
679                 if (!key)
680                         goto error;
681                 key_ref = make_key_ref(key, 1);
682                 break;
683
684         default:
685                 key_ref = ERR_PTR(-EINVAL);
686                 if (id < 1)
687                         goto error;
688
689                 key = key_lookup(id);
690                 if (IS_ERR(key)) {
691                         key_ref = ERR_CAST(key);
692                         goto error;
693                 }
694
695                 key_ref = make_key_ref(key, 0);
696
697                 /* check to see if we possess the key */
698                 skey_ref = search_process_keyrings(key->type, key,
699                                                    lookup_user_key_possessed,
700                                                    cred);
701
702                 if (!IS_ERR(skey_ref)) {
703                         key_put(key);
704                         key_ref = skey_ref;
705                 }
706
707                 break;
708         }
709
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) {
713                 ret = 0;
714                 goto error;
715         }
716
717         if (!(lflags & KEY_LOOKUP_PARTIAL)) {
718                 ret = wait_for_key_construction(key, true);
719                 switch (ret) {
720                 case -ERESTARTSYS:
721                         goto invalid_key;
722                 default:
723                         if (perm)
724                                 goto invalid_key;
725                 case 0:
726                         break;
727                 }
728         } else if (perm) {
729                 ret = key_validate(key);
730                 if (ret < 0)
731                         goto invalid_key;
732         }
733
734         ret = -EIO;
735         if (!(lflags & KEY_LOOKUP_PARTIAL) &&
736             !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
737                 goto invalid_key;
738
739         /* check the permissions */
740         ret = key_task_permission(key_ref, cred, perm);
741         if (ret < 0)
742                 goto invalid_key;
743
744 error:
745         put_cred(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(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         /* 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
780          * ENOMEM */
781         if (!current_is_single_threaded())
782                 return -EMLINK;
783
784         new = prepare_creds();
785         if (!new)
786                 return -ENOMEM;
787         old = current_cred();
788
789         /* if no name is provided, install an anonymous keyring */
790         if (!name) {
791                 ret = install_session_keyring_to_cred(new, NULL);
792                 if (ret < 0)
793                         goto error;
794
795                 serial = new->tgcred->session_keyring->serial;
796                 ret = commit_creds(new);
797                 if (ret == 0)
798                         ret = serial;
799                 goto okay;
800         }
801
802         /* allow the user to join or create a named keyring */
803         mutex_lock(&key_session_mutex);
804
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);
813                         goto error2;
814                 }
815         } else if (IS_ERR(keyring)) {
816                 ret = PTR_ERR(keyring);
817                 goto error2;
818         }
819
820         /* we've got a keyring - now to install it */
821         ret = install_session_keyring_to_cred(new, keyring);
822         if (ret < 0)
823                 goto error2;
824
825         commit_creds(new);
826         mutex_unlock(&key_session_mutex);
827
828         ret = keyring->serial;
829         key_put(keyring);
830 okay:
831         return ret;
832
833 error2:
834         mutex_unlock(&key_session_mutex);
835 error:
836         abort_creds(new);
837         return ret;
838 }
839
840 /*
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.
843  */
844 void key_replace_session_keyring(void)
845 {
846         const struct cred *old;
847         struct cred *new;
848
849         if (!current->replacement_session_keyring)
850                 return;
851
852         write_lock_irq(&tasklist_lock);
853         new = current->replacement_session_keyring;
854         current->replacement_session_keyring = NULL;
855         write_unlock_irq(&tasklist_lock);
856
857         if (!new)
858                 return;
859
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);
872
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;
878
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);
883
884         security_transfer_creds(new, old);
885
886         commit_creds(new);
887 }