s390/qeth: fix SETIP command handling
[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,
76                                                     KEY_ALLOC_UID_KEYRING |
77                                                         KEY_ALLOC_IN_QUOTA,
78                                                     NULL);
79                         if (IS_ERR(uid_keyring)) {
80                                 ret = PTR_ERR(uid_keyring);
81                                 goto error;
82                         }
83                 }
84
85                 /* get a default session keyring (which might also exist
86                  * already) */
87                 sprintf(buf, "_uid_ses.%u", user->uid);
88
89                 session_keyring = find_keyring_by_name(buf, true);
90                 if (IS_ERR(session_keyring)) {
91                         session_keyring =
92                                 keyring_alloc(buf, user->uid, (gid_t) -1,
93                                               cred,
94                                               KEY_ALLOC_UID_KEYRING |
95                                                   KEY_ALLOC_IN_QUOTA,
96                                               NULL);
97                         if (IS_ERR(session_keyring)) {
98                                 ret = PTR_ERR(session_keyring);
99                                 goto error_release;
100                         }
101
102                         /* we install a link from the user session keyring to
103                          * the user keyring */
104                         ret = key_link(session_keyring, uid_keyring);
105                         if (ret < 0)
106                                 goto error_release_both;
107                 }
108
109                 /* install the keyrings */
110                 user->uid_keyring = uid_keyring;
111                 user->session_keyring = session_keyring;
112         }
113
114         mutex_unlock(&key_user_keyring_mutex);
115         kleave(" = 0");
116         return 0;
117
118 error_release_both:
119         key_put(session_keyring);
120 error_release:
121         key_put(uid_keyring);
122 error:
123         mutex_unlock(&key_user_keyring_mutex);
124         kleave(" = %d", ret);
125         return ret;
126 }
127
128 /*
129  * Install a thread keyring to the given credentials struct if it didn't have
130  * one already.  This is allowed to overrun the quota.
131  *
132  * Return: 0 if a thread keyring is now present; -errno on failure.
133  */
134 int install_thread_keyring_to_cred(struct cred *new)
135 {
136         struct key *keyring;
137
138         if (new->thread_keyring)
139                 return 0;
140
141         keyring = keyring_alloc("_tid", new->uid, new->gid, new,
142                                 KEY_ALLOC_QUOTA_OVERRUN, NULL);
143         if (IS_ERR(keyring))
144                 return PTR_ERR(keyring);
145
146         new->thread_keyring = keyring;
147         return 0;
148 }
149
150 /*
151  * Install a thread keyring to the current task if it didn't have one already.
152  *
153  * Return: 0 if a thread keyring is now present; -errno on failure.
154  */
155 static int install_thread_keyring(void)
156 {
157         struct cred *new;
158         int ret;
159
160         new = prepare_creds();
161         if (!new)
162                 return -ENOMEM;
163
164         ret = install_thread_keyring_to_cred(new);
165         if (ret < 0) {
166                 abort_creds(new);
167                 return ret;
168         }
169
170         return commit_creds(new);
171 }
172
173 /*
174  * Install a process keyring to the given credentials struct if it didn't have
175  * one already.  This is allowed to overrun the quota.
176  *
177  * Return: 0 if a process keyring is now present; -errno on failure.
178  */
179 int install_process_keyring_to_cred(struct cred *new)
180 {
181         struct key *keyring;
182         int ret;
183
184         if (new->tgcred->process_keyring)
185                 return 0;
186
187         keyring = keyring_alloc("_pid", new->uid, new->gid,
188                                 new, KEY_ALLOC_QUOTA_OVERRUN, NULL);
189         if (IS_ERR(keyring))
190                 return PTR_ERR(keyring);
191
192         spin_lock_irq(&new->tgcred->lock);
193         if (!new->tgcred->process_keyring) {
194                 new->tgcred->process_keyring = keyring;
195                 keyring = NULL;
196                 ret = 0;
197         } else {
198                 ret = -EEXIST;
199         }
200         spin_unlock_irq(&new->tgcred->lock);
201         key_put(keyring);
202         return ret;
203 }
204
205 /*
206  * Install a process keyring to the current task if it didn't have one already.
207  *
208  * Return: 0 if a process keyring is now present; -errno on failure.
209  */
210 static int install_process_keyring(void)
211 {
212         struct cred *new;
213         int ret;
214
215         new = prepare_creds();
216         if (!new)
217                 return -ENOMEM;
218
219         ret = install_process_keyring_to_cred(new);
220         if (ret < 0) {
221                 abort_creds(new);
222                 return ret;
223         }
224
225         return commit_creds(new);
226 }
227
228 /*
229  * Install the given keyring as the session keyring of the given credentials
230  * struct, replacing the existing one if any.  If the given keyring is NULL,
231  * then install a new anonymous session keyring.
232  *
233  * Return: 0 on success; -errno on failure.
234  */
235 int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
236 {
237         unsigned long flags;
238         struct key *old;
239
240         might_sleep();
241
242         /* create an empty session keyring */
243         if (!keyring) {
244                 flags = KEY_ALLOC_QUOTA_OVERRUN;
245                 if (cred->tgcred->session_keyring)
246                         flags = KEY_ALLOC_IN_QUOTA;
247
248                 keyring = keyring_alloc("_ses", cred->uid, cred->gid,
249                                         cred, flags, NULL);
250                 if (IS_ERR(keyring))
251                         return PTR_ERR(keyring);
252         } else {
253                 atomic_inc(&keyring->usage);
254         }
255
256         /* install the keyring */
257         spin_lock_irq(&cred->tgcred->lock);
258         old = cred->tgcred->session_keyring;
259         rcu_assign_pointer(cred->tgcred->session_keyring, keyring);
260         spin_unlock_irq(&cred->tgcred->lock);
261
262         /* we're using RCU on the pointer, but there's no point synchronising
263          * on it if it didn't previously point to anything */
264         if (old) {
265                 synchronize_rcu();
266                 key_put(old);
267         }
268
269         return 0;
270 }
271
272 /*
273  * Install the given keyring as the session keyring of the current task,
274  * replacing the existing one if any.  If the given keyring is NULL, then
275  * install a new anonymous session keyring.
276  *
277  * Return: 0 on success; -errno on failure.
278  */
279 static int install_session_keyring(struct key *keyring)
280 {
281         struct cred *new;
282         int ret;
283
284         new = prepare_creds();
285         if (!new)
286                 return -ENOMEM;
287
288         ret = install_session_keyring_to_cred(new, keyring);
289         if (ret < 0) {
290                 abort_creds(new);
291                 return ret;
292         }
293
294         return commit_creds(new);
295 }
296
297 /*
298  * Handle the fsuid changing.
299  */
300 void key_fsuid_changed(struct task_struct *tsk)
301 {
302         /* update the ownership of the thread keyring */
303         BUG_ON(!tsk->cred);
304         if (tsk->cred->thread_keyring) {
305                 down_write(&tsk->cred->thread_keyring->sem);
306                 tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
307                 up_write(&tsk->cred->thread_keyring->sem);
308         }
309 }
310
311 /*
312  * Handle the fsgid changing.
313  */
314 void key_fsgid_changed(struct task_struct *tsk)
315 {
316         /* update the ownership of the thread keyring */
317         BUG_ON(!tsk->cred);
318         if (tsk->cred->thread_keyring) {
319                 down_write(&tsk->cred->thread_keyring->sem);
320                 tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
321                 up_write(&tsk->cred->thread_keyring->sem);
322         }
323 }
324
325 /*
326  * Search the process keyrings attached to the supplied cred for the first
327  * matching key.
328  *
329  * The search criteria are the type and the match function.  The description is
330  * given to the match function as a parameter, but doesn't otherwise influence
331  * the search.  Typically the match function will compare the description
332  * parameter to the key's description.
333  *
334  * This can only search keyrings that grant Search permission to the supplied
335  * credentials.  Keyrings linked to searched keyrings will also be searched if
336  * they grant Search permission too.  Keys can only be found if they grant
337  * Search permission to the credentials.
338  *
339  * Returns a pointer to the key with the key usage count incremented if
340  * successful, -EAGAIN if we didn't find any matching key or -ENOKEY if we only
341  * matched negative keys.
342  *
343  * In the case of a successful return, the possession attribute is set on the
344  * returned key reference.
345  */
346 key_ref_t search_my_process_keyrings(struct key_type *type,
347                                      const void *description,
348                                      key_match_func_t match,
349                                      bool no_state_check,
350                                      const struct cred *cred)
351 {
352         key_ref_t key_ref, ret, err;
353
354         /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
355          * searchable, but we failed to find a key or we found a negative key;
356          * otherwise we want to return a sample error (probably -EACCES) if
357          * none of the keyrings were searchable
358          *
359          * in terms of priority: success > -ENOKEY > -EAGAIN > other error
360          */
361         key_ref = NULL;
362         ret = NULL;
363         err = ERR_PTR(-EAGAIN);
364
365         /* search the thread keyring first */
366         if (cred->thread_keyring) {
367                 key_ref = keyring_search_aux(
368                         make_key_ref(cred->thread_keyring, 1),
369                         cred, type, description, match, no_state_check);
370                 if (!IS_ERR(key_ref))
371                         goto found;
372
373                 switch (PTR_ERR(key_ref)) {
374                 case -EAGAIN: /* no key */
375                         if (ret)
376                                 break;
377                 case -ENOKEY: /* negative key */
378                         ret = key_ref;
379                         break;
380                 default:
381                         err = key_ref;
382                         break;
383                 }
384         }
385
386         /* search the process keyring second */
387         if (cred->tgcred->process_keyring) {
388                 key_ref = keyring_search_aux(
389                         make_key_ref(cred->tgcred->process_keyring, 1),
390                         cred, type, description, match, no_state_check);
391                 if (!IS_ERR(key_ref))
392                         goto found;
393
394                 switch (PTR_ERR(key_ref)) {
395                 case -EAGAIN: /* no key */
396                         if (ret)
397                                 break;
398                 case -ENOKEY: /* negative key */
399                         ret = key_ref;
400                         break;
401                 default:
402                         err = key_ref;
403                         break;
404                 }
405         }
406
407         /* search the session keyring */
408         if (cred->tgcred->session_keyring) {
409                 rcu_read_lock();
410                 key_ref = keyring_search_aux(
411                         make_key_ref(rcu_dereference(
412                                              cred->tgcred->session_keyring),
413                                      1),
414                         cred, type, description, match, no_state_check);
415                 rcu_read_unlock();
416
417                 if (!IS_ERR(key_ref))
418                         goto found;
419
420                 switch (PTR_ERR(key_ref)) {
421                 case -EAGAIN: /* no key */
422                         if (ret)
423                                 break;
424                 case -ENOKEY: /* negative key */
425                         ret = key_ref;
426                         break;
427                 default:
428                         err = key_ref;
429                         break;
430                 }
431         }
432         /* or search the user-session keyring */
433         else if (cred->user->session_keyring) {
434                 key_ref = keyring_search_aux(
435                         make_key_ref(cred->user->session_keyring, 1),
436                         cred, type, description, match, no_state_check);
437                 if (!IS_ERR(key_ref))
438                         goto found;
439
440                 switch (PTR_ERR(key_ref)) {
441                 case -EAGAIN: /* no key */
442                         if (ret)
443                                 break;
444                 case -ENOKEY: /* negative key */
445                         ret = key_ref;
446                         break;
447                 default:
448                         err = key_ref;
449                         break;
450                 }
451         }
452
453         /* no key - decide on the error we're going to go for */
454         key_ref = ret ? ret : err;
455
456 found:
457         return key_ref;
458 }
459
460 /*
461  * Search the process keyrings attached to the supplied cred for the first
462  * matching key in the manner of search_my_process_keyrings(), but also search
463  * the keys attached to the assumed authorisation key using its credentials if
464  * one is available.
465  *
466  * Return same as search_my_process_keyrings().
467  */
468 key_ref_t search_process_keyrings(struct key_type *type,
469                                   const void *description,
470                                   key_match_func_t match,
471                                   const struct cred *cred)
472 {
473         struct request_key_auth *rka;
474         key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
475
476         might_sleep();
477
478         key_ref = search_my_process_keyrings(type, description, match,
479                                              false, cred);
480         if (!IS_ERR(key_ref))
481                 goto found;
482         err = key_ref;
483
484         /* if this process has an instantiation authorisation key, then we also
485          * search the keyrings of the process mentioned there
486          * - we don't permit access to request_key auth keys via this method
487          */
488         if (cred->request_key_auth &&
489             cred == current_cred() &&
490             type != &key_type_request_key_auth
491             ) {
492                 /* defend against the auth key being revoked */
493                 down_read(&cred->request_key_auth->sem);
494
495                 if (key_validate(cred->request_key_auth) == 0) {
496                         rka = cred->request_key_auth->payload.data;
497
498                         key_ref = search_process_keyrings(type, description,
499                                                           match, rka->cred);
500
501                         up_read(&cred->request_key_auth->sem);
502
503                         if (!IS_ERR(key_ref))
504                                 goto found;
505
506                         ret = key_ref;
507                 } else {
508                         up_read(&cred->request_key_auth->sem);
509                 }
510         }
511
512         /* no key - decide on the error we're going to go for */
513         if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY))
514                 key_ref = ERR_PTR(-ENOKEY);
515         else if (err == ERR_PTR(-EACCES))
516                 key_ref = ret;
517         else
518                 key_ref = err;
519
520 found:
521         return key_ref;
522 }
523
524 /*
525  * See if the key we're looking at is the target key.
526  */
527 int lookup_user_key_possessed(const struct key *key, const void *target)
528 {
529         return key == target;
530 }
531
532 /*
533  * Look up a key ID given us by userspace with a given permissions mask to get
534  * the key it refers to.
535  *
536  * Flags can be passed to request that special keyrings be created if referred
537  * to directly, to permit partially constructed keys to be found and to skip
538  * validity and permission checks on the found key.
539  *
540  * Returns a pointer to the key with an incremented usage count if successful;
541  * -EINVAL if the key ID is invalid; -ENOKEY if the key ID does not correspond
542  * to a key or the best found key was a negative key; -EKEYREVOKED or
543  * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the
544  * found key doesn't grant the requested permit or the LSM denied access to it;
545  * or -ENOMEM if a special keyring couldn't be created.
546  *
547  * In the case of a successful return, the possession attribute is set on the
548  * returned key reference.
549  */
550 key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
551                           key_perm_t perm)
552 {
553         struct request_key_auth *rka;
554         const struct cred *cred;
555         struct key *key;
556         key_ref_t key_ref, skey_ref;
557         int ret;
558
559 try_again:
560         cred = get_current_cred();
561         key_ref = ERR_PTR(-ENOKEY);
562
563         switch (id) {
564         case KEY_SPEC_THREAD_KEYRING:
565                 if (!cred->thread_keyring) {
566                         if (!(lflags & KEY_LOOKUP_CREATE))
567                                 goto error;
568
569                         ret = install_thread_keyring();
570                         if (ret < 0) {
571                                 key_ref = ERR_PTR(ret);
572                                 goto error;
573                         }
574                         goto reget_creds;
575                 }
576
577                 key = cred->thread_keyring;
578                 atomic_inc(&key->usage);
579                 key_ref = make_key_ref(key, 1);
580                 break;
581
582         case KEY_SPEC_PROCESS_KEYRING:
583                 if (!cred->tgcred->process_keyring) {
584                         if (!(lflags & KEY_LOOKUP_CREATE))
585                                 goto error;
586
587                         ret = install_process_keyring();
588                         if (ret < 0) {
589                                 key_ref = ERR_PTR(ret);
590                                 goto error;
591                         }
592                         goto reget_creds;
593                 }
594
595                 key = cred->tgcred->process_keyring;
596                 atomic_inc(&key->usage);
597                 key_ref = make_key_ref(key, 1);
598                 break;
599
600         case KEY_SPEC_SESSION_KEYRING:
601                 if (!cred->tgcred->session_keyring) {
602                         /* always install a session keyring upon access if one
603                          * doesn't exist yet */
604                         ret = install_user_keyrings();
605                         if (ret < 0)
606                                 goto error;
607                         if (lflags & KEY_LOOKUP_CREATE)
608                                 ret = join_session_keyring(NULL);
609                         else
610                                 ret = install_session_keyring(
611                                         cred->user->session_keyring);
612
613                         if (ret < 0)
614                                 goto error;
615                         goto reget_creds;
616                 } else if (cred->tgcred->session_keyring ==
617                            cred->user->session_keyring &&
618                            lflags & KEY_LOOKUP_CREATE) {
619                         ret = join_session_keyring(NULL);
620                         if (ret < 0)
621                                 goto error;
622                         goto reget_creds;
623                 }
624
625                 rcu_read_lock();
626                 key = rcu_dereference(cred->tgcred->session_keyring);
627                 atomic_inc(&key->usage);
628                 rcu_read_unlock();
629                 key_ref = make_key_ref(key, 1);
630                 break;
631
632         case KEY_SPEC_USER_KEYRING:
633                 if (!cred->user->uid_keyring) {
634                         ret = install_user_keyrings();
635                         if (ret < 0)
636                                 goto error;
637                 }
638
639                 key = cred->user->uid_keyring;
640                 atomic_inc(&key->usage);
641                 key_ref = make_key_ref(key, 1);
642                 break;
643
644         case KEY_SPEC_USER_SESSION_KEYRING:
645                 if (!cred->user->session_keyring) {
646                         ret = install_user_keyrings();
647                         if (ret < 0)
648                                 goto error;
649                 }
650
651                 key = cred->user->session_keyring;
652                 atomic_inc(&key->usage);
653                 key_ref = make_key_ref(key, 1);
654                 break;
655
656         case KEY_SPEC_GROUP_KEYRING:
657                 /* group keyrings are not yet supported */
658                 key_ref = ERR_PTR(-EINVAL);
659                 goto error;
660
661         case KEY_SPEC_REQKEY_AUTH_KEY:
662                 key = cred->request_key_auth;
663                 if (!key)
664                         goto error;
665
666                 atomic_inc(&key->usage);
667                 key_ref = make_key_ref(key, 1);
668                 break;
669
670         case KEY_SPEC_REQUESTOR_KEYRING:
671                 if (!cred->request_key_auth)
672                         goto error;
673
674                 down_read(&cred->request_key_auth->sem);
675                 if (cred->request_key_auth->flags & KEY_FLAG_REVOKED) {
676                         key_ref = ERR_PTR(-EKEYREVOKED);
677                         key = NULL;
678                 } else {
679                         rka = cred->request_key_auth->payload.data;
680                         key = rka->dest_keyring;
681                         atomic_inc(&key->usage);
682                 }
683                 up_read(&cred->request_key_auth->sem);
684                 if (!key)
685                         goto error;
686                 key_ref = make_key_ref(key, 1);
687                 break;
688
689         default:
690                 key_ref = ERR_PTR(-EINVAL);
691                 if (id < 1)
692                         goto error;
693
694                 key = key_lookup(id);
695                 if (IS_ERR(key)) {
696                         key_ref = ERR_CAST(key);
697                         goto error;
698                 }
699
700                 key_ref = make_key_ref(key, 0);
701
702                 /* check to see if we possess the key */
703                 skey_ref = search_process_keyrings(key->type, key,
704                                                    lookup_user_key_possessed,
705                                                    cred);
706
707                 if (!IS_ERR(skey_ref)) {
708                         key_put(key);
709                         key_ref = skey_ref;
710                 }
711
712                 break;
713         }
714
715         /* unlink does not use the nominated key in any way, so can skip all
716          * the permission checks as it is only concerned with the keyring */
717         if (lflags & KEY_LOOKUP_FOR_UNLINK) {
718                 ret = 0;
719                 goto error;
720         }
721
722         if (!(lflags & KEY_LOOKUP_PARTIAL)) {
723                 ret = wait_for_key_construction(key, true);
724                 switch (ret) {
725                 case -ERESTARTSYS:
726                         goto invalid_key;
727                 default:
728                         if (perm)
729                                 goto invalid_key;
730                 case 0:
731                         break;
732                 }
733         } else if (perm) {
734                 ret = key_validate(key);
735                 if (ret < 0)
736                         goto invalid_key;
737         }
738
739         ret = -EIO;
740         if (!(lflags & KEY_LOOKUP_PARTIAL) &&
741             !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
742                 goto invalid_key;
743
744         /* check the permissions */
745         ret = key_task_permission(key_ref, cred, perm);
746         if (ret < 0)
747                 goto invalid_key;
748
749 error:
750         put_cred(cred);
751         return key_ref;
752
753 invalid_key:
754         key_ref_put(key_ref);
755         key_ref = ERR_PTR(ret);
756         goto error;
757
758         /* if we attempted to install a keyring, then it may have caused new
759          * creds to be installed */
760 reget_creds:
761         put_cred(cred);
762         goto try_again;
763 }
764
765 /*
766  * Join the named keyring as the session keyring if possible else attempt to
767  * create a new one of that name and join that.
768  *
769  * If the name is NULL, an empty anonymous keyring will be installed as the
770  * session keyring.
771  *
772  * Named session keyrings are joined with a semaphore held to prevent the
773  * keyrings from going away whilst the attempt is made to going them and also
774  * to prevent a race in creating compatible session keyrings.
775  */
776 long join_session_keyring(const char *name)
777 {
778         const struct cred *old;
779         struct cred *new;
780         struct key *keyring;
781         long ret, serial;
782
783         /* only permit this if there's a single thread in the thread group -
784          * this avoids us having to adjust the creds on all threads and risking
785          * ENOMEM */
786         if (!current_is_single_threaded())
787                 return -EMLINK;
788
789         new = prepare_creds();
790         if (!new)
791                 return -ENOMEM;
792         old = current_cred();
793
794         /* if no name is provided, install an anonymous keyring */
795         if (!name) {
796                 ret = install_session_keyring_to_cred(new, NULL);
797                 if (ret < 0)
798                         goto error;
799
800                 serial = new->tgcred->session_keyring->serial;
801                 ret = commit_creds(new);
802                 if (ret == 0)
803                         ret = serial;
804                 goto okay;
805         }
806
807         /* allow the user to join or create a named keyring */
808         mutex_lock(&key_session_mutex);
809
810         /* look for an existing keyring of this name */
811         keyring = find_keyring_by_name(name, false);
812         if (PTR_ERR(keyring) == -ENOKEY) {
813                 /* not found - try and create a new one */
814                 keyring = keyring_alloc(name, old->uid, old->gid, old,
815                                         KEY_ALLOC_IN_QUOTA, NULL);
816                 if (IS_ERR(keyring)) {
817                         ret = PTR_ERR(keyring);
818                         goto error2;
819                 }
820         } else if (IS_ERR(keyring)) {
821                 ret = PTR_ERR(keyring);
822                 goto error2;
823         }
824
825         /* we've got a keyring - now to install it */
826         ret = install_session_keyring_to_cred(new, keyring);
827         if (ret < 0)
828                 goto error2;
829
830         commit_creds(new);
831         mutex_unlock(&key_session_mutex);
832
833         ret = keyring->serial;
834         key_put(keyring);
835 okay:
836         return ret;
837
838 error2:
839         mutex_unlock(&key_session_mutex);
840 error:
841         abort_creds(new);
842         return ret;
843 }
844
845 /*
846  * Replace a process's session keyring on behalf of one of its children when
847  * the target  process is about to resume userspace execution.
848  */
849 void key_replace_session_keyring(void)
850 {
851         const struct cred *old;
852         struct cred *new;
853
854         if (!current->replacement_session_keyring)
855                 return;
856
857         write_lock_irq(&tasklist_lock);
858         new = current->replacement_session_keyring;
859         current->replacement_session_keyring = NULL;
860         write_unlock_irq(&tasklist_lock);
861
862         if (!new)
863                 return;
864
865         old = current_cred();
866         new->  uid      = old->  uid;
867         new-> euid      = old-> euid;
868         new-> suid      = old-> suid;
869         new->fsuid      = old->fsuid;
870         new->  gid      = old->  gid;
871         new-> egid      = old-> egid;
872         new-> sgid      = old-> sgid;
873         new->fsgid      = old->fsgid;
874         new->user       = get_uid(old->user);
875         new->user_ns    = new->user->user_ns;
876         new->group_info = get_group_info(old->group_info);
877
878         new->securebits = old->securebits;
879         new->cap_inheritable    = old->cap_inheritable;
880         new->cap_permitted      = old->cap_permitted;
881         new->cap_effective      = old->cap_effective;
882         new->cap_bset           = old->cap_bset;
883
884         new->jit_keyring        = old->jit_keyring;
885         new->thread_keyring     = key_get(old->thread_keyring);
886         new->tgcred->tgid       = old->tgcred->tgid;
887         new->tgcred->process_keyring = key_get(old->tgcred->process_keyring);
888
889         security_transfer_creds(new, old);
890
891         commit_creds(new);
892 }