KEYS: Do some style cleanup in the key management code.
[pandora-kernel.git] / security / keys / keyctl.c
1 /* keyctl.c: userspace keyctl operations
2  *
3  * Copyright (C) 2004-5 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/slab.h>
16 #include <linux/syscalls.h>
17 #include <linux/keyctl.h>
18 #include <linux/fs.h>
19 #include <linux/capability.h>
20 #include <linux/string.h>
21 #include <linux/err.h>
22 #include <linux/vmalloc.h>
23 #include <linux/security.h>
24 #include <asm/uaccess.h>
25 #include "internal.h"
26
27 static int key_get_type_from_user(char *type,
28                                   const char __user *_type,
29                                   unsigned len)
30 {
31         int ret;
32
33         ret = strncpy_from_user(type, _type, len);
34
35         if (ret < 0)
36                 return ret;
37
38         if (ret == 0 || ret >= len)
39                 return -EINVAL;
40
41         if (type[0] == '.')
42                 return -EPERM;
43
44         type[len - 1] = '\0';
45
46         return 0;
47 }
48
49 /*
50  * extract the description of a new key from userspace and either add it as a
51  * new key to the specified keyring or update a matching key in that keyring
52  * - the keyring must be writable
53  * - returns the new key's serial number
54  * - implements add_key()
55  */
56 SYSCALL_DEFINE5(add_key, const char __user *, _type,
57                 const char __user *, _description,
58                 const void __user *, _payload,
59                 size_t, plen,
60                 key_serial_t, ringid)
61 {
62         key_ref_t keyring_ref, key_ref;
63         char type[32], *description;
64         void *payload;
65         long ret;
66         bool vm;
67
68         ret = -EINVAL;
69         if (plen > 1024 * 1024 - 1)
70                 goto error;
71
72         /* draw all the data into kernel space */
73         ret = key_get_type_from_user(type, _type, sizeof(type));
74         if (ret < 0)
75                 goto error;
76
77         description = strndup_user(_description, PAGE_SIZE);
78         if (IS_ERR(description)) {
79                 ret = PTR_ERR(description);
80                 goto error;
81         }
82
83         /* pull the payload in if one was supplied */
84         payload = NULL;
85
86         vm = false;
87         if (_payload) {
88                 ret = -ENOMEM;
89                 payload = kmalloc(plen, GFP_KERNEL);
90                 if (!payload) {
91                         if (plen <= PAGE_SIZE)
92                                 goto error2;
93                         vm = true;
94                         payload = vmalloc(plen);
95                         if (!payload)
96                                 goto error2;
97                 }
98
99                 ret = -EFAULT;
100                 if (copy_from_user(payload, _payload, plen) != 0)
101                         goto error3;
102         }
103
104         /* find the target keyring (which must be writable) */
105         keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
106         if (IS_ERR(keyring_ref)) {
107                 ret = PTR_ERR(keyring_ref);
108                 goto error3;
109         }
110
111         /* create or update the requested key and add it to the target
112          * keyring */
113         key_ref = key_create_or_update(keyring_ref, type, description,
114                                        payload, plen, KEY_PERM_UNDEF,
115                                        KEY_ALLOC_IN_QUOTA);
116         if (!IS_ERR(key_ref)) {
117                 ret = key_ref_to_ptr(key_ref)->serial;
118                 key_ref_put(key_ref);
119         }
120         else {
121                 ret = PTR_ERR(key_ref);
122         }
123
124         key_ref_put(keyring_ref);
125  error3:
126         if (!vm)
127                 kfree(payload);
128         else
129                 vfree(payload);
130  error2:
131         kfree(description);
132  error:
133         return ret;
134 }
135
136 /*
137  * search the process keyrings for a matching key
138  * - nested keyrings may also be searched if they have Search permission
139  * - if a key is found, it will be attached to the destination keyring if
140  *   there's one specified
141  * - /sbin/request-key will be invoked if _callout_info is non-NULL
142  *   - the _callout_info string will be passed to /sbin/request-key
143  *   - if the _callout_info string is empty, it will be rendered as "-"
144  * - implements request_key()
145  */
146 SYSCALL_DEFINE4(request_key, const char __user *, _type,
147                 const char __user *, _description,
148                 const char __user *, _callout_info,
149                 key_serial_t, destringid)
150 {
151         struct key_type *ktype;
152         struct key *key;
153         key_ref_t dest_ref;
154         size_t callout_len;
155         char type[32], *description, *callout_info;
156         long ret;
157
158         /* pull the type into kernel space */
159         ret = key_get_type_from_user(type, _type, sizeof(type));
160         if (ret < 0)
161                 goto error;
162
163         /* pull the description into kernel space */
164         description = strndup_user(_description, PAGE_SIZE);
165         if (IS_ERR(description)) {
166                 ret = PTR_ERR(description);
167                 goto error;
168         }
169
170         /* pull the callout info into kernel space */
171         callout_info = NULL;
172         callout_len = 0;
173         if (_callout_info) {
174                 callout_info = strndup_user(_callout_info, PAGE_SIZE);
175                 if (IS_ERR(callout_info)) {
176                         ret = PTR_ERR(callout_info);
177                         goto error2;
178                 }
179                 callout_len = strlen(callout_info);
180         }
181
182         /* get the destination keyring if specified */
183         dest_ref = NULL;
184         if (destringid) {
185                 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
186                                            KEY_WRITE);
187                 if (IS_ERR(dest_ref)) {
188                         ret = PTR_ERR(dest_ref);
189                         goto error3;
190                 }
191         }
192
193         /* find the key type */
194         ktype = key_type_lookup(type);
195         if (IS_ERR(ktype)) {
196                 ret = PTR_ERR(ktype);
197                 goto error4;
198         }
199
200         /* do the search */
201         key = request_key_and_link(ktype, description, callout_info,
202                                    callout_len, NULL, key_ref_to_ptr(dest_ref),
203                                    KEY_ALLOC_IN_QUOTA);
204         if (IS_ERR(key)) {
205                 ret = PTR_ERR(key);
206                 goto error5;
207         }
208
209         ret = key->serial;
210
211         key_put(key);
212 error5:
213         key_type_put(ktype);
214 error4:
215         key_ref_put(dest_ref);
216 error3:
217         kfree(callout_info);
218 error2:
219         kfree(description);
220 error:
221         return ret;
222 }
223
224 /*
225  * get the ID of the specified process keyring
226  * - the keyring must have search permission to be found
227  * - implements keyctl(KEYCTL_GET_KEYRING_ID)
228  */
229 long keyctl_get_keyring_ID(key_serial_t id, int create)
230 {
231         key_ref_t key_ref;
232         unsigned long lflags;
233         long ret;
234
235         lflags = create ? KEY_LOOKUP_CREATE : 0;
236         key_ref = lookup_user_key(id, lflags, KEY_SEARCH);
237         if (IS_ERR(key_ref)) {
238                 ret = PTR_ERR(key_ref);
239                 goto error;
240         }
241
242         ret = key_ref_to_ptr(key_ref)->serial;
243         key_ref_put(key_ref);
244 error:
245         return ret;
246
247 } /* end keyctl_get_keyring_ID() */
248
249 /*
250  * join the session keyring
251  * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING)
252  */
253 long keyctl_join_session_keyring(const char __user *_name)
254 {
255         char *name;
256         long ret;
257
258         /* fetch the name from userspace */
259         name = NULL;
260         if (_name) {
261                 name = strndup_user(_name, PAGE_SIZE);
262                 if (IS_ERR(name)) {
263                         ret = PTR_ERR(name);
264                         goto error;
265                 }
266         }
267
268         /* join the session */
269         ret = join_session_keyring(name);
270         kfree(name);
271
272 error:
273         return ret;
274 }
275
276 /*
277  * update a key's data payload
278  * - the key must be writable
279  * - implements keyctl(KEYCTL_UPDATE)
280  */
281 long keyctl_update_key(key_serial_t id,
282                        const void __user *_payload,
283                        size_t plen)
284 {
285         key_ref_t key_ref;
286         void *payload;
287         long ret;
288
289         ret = -EINVAL;
290         if (plen > PAGE_SIZE)
291                 goto error;
292
293         /* pull the payload in if one was supplied */
294         payload = NULL;
295         if (_payload) {
296                 ret = -ENOMEM;
297                 payload = kmalloc(plen, GFP_KERNEL);
298                 if (!payload)
299                         goto error;
300
301                 ret = -EFAULT;
302                 if (copy_from_user(payload, _payload, plen) != 0)
303                         goto error2;
304         }
305
306         /* find the target key (which must be writable) */
307         key_ref = lookup_user_key(id, 0, KEY_WRITE);
308         if (IS_ERR(key_ref)) {
309                 ret = PTR_ERR(key_ref);
310                 goto error2;
311         }
312
313         /* update the key */
314         ret = key_update(key_ref, payload, plen);
315
316         key_ref_put(key_ref);
317 error2:
318         kfree(payload);
319 error:
320         return ret;
321 }
322
323 /*
324  * revoke a key
325  * - the key must be writable
326  * - implements keyctl(KEYCTL_REVOKE)
327  */
328 long keyctl_revoke_key(key_serial_t id)
329 {
330         key_ref_t key_ref;
331         long ret;
332
333         key_ref = lookup_user_key(id, 0, KEY_WRITE);
334         if (IS_ERR(key_ref)) {
335                 ret = PTR_ERR(key_ref);
336                 if (ret != -EACCES)
337                         goto error;
338                 key_ref = lookup_user_key(id, 0, KEY_SETATTR);
339                 if (IS_ERR(key_ref)) {
340                         ret = PTR_ERR(key_ref);
341                         goto error;
342                 }
343         }
344
345         key_revoke(key_ref_to_ptr(key_ref));
346         ret = 0;
347
348         key_ref_put(key_ref);
349 error:
350         return ret;
351 }
352
353 /*
354  * clear the specified process keyring
355  * - the keyring must be writable
356  * - implements keyctl(KEYCTL_CLEAR)
357  */
358 long keyctl_keyring_clear(key_serial_t ringid)
359 {
360         key_ref_t keyring_ref;
361         long ret;
362
363         keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
364         if (IS_ERR(keyring_ref)) {
365                 ret = PTR_ERR(keyring_ref);
366                 goto error;
367         }
368
369         ret = keyring_clear(key_ref_to_ptr(keyring_ref));
370
371         key_ref_put(keyring_ref);
372 error:
373         return ret;
374 }
375
376 /*
377  * link a key into a keyring
378  * - the keyring must be writable
379  * - the key must be linkable
380  * - implements keyctl(KEYCTL_LINK)
381  */
382 long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
383 {
384         key_ref_t keyring_ref, key_ref;
385         long ret;
386
387         keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
388         if (IS_ERR(keyring_ref)) {
389                 ret = PTR_ERR(keyring_ref);
390                 goto error;
391         }
392
393         key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_LINK);
394         if (IS_ERR(key_ref)) {
395                 ret = PTR_ERR(key_ref);
396                 goto error2;
397         }
398
399         ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
400
401         key_ref_put(key_ref);
402 error2:
403         key_ref_put(keyring_ref);
404 error:
405         return ret;
406 }
407
408 /*
409  * unlink the first attachment of a key from a keyring
410  * - the keyring must be writable
411  * - we don't need any permissions on the key
412  * - implements keyctl(KEYCTL_UNLINK)
413  */
414 long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
415 {
416         key_ref_t keyring_ref, key_ref;
417         long ret;
418
419         keyring_ref = lookup_user_key(ringid, 0, KEY_WRITE);
420         if (IS_ERR(keyring_ref)) {
421                 ret = PTR_ERR(keyring_ref);
422                 goto error;
423         }
424
425         key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
426         if (IS_ERR(key_ref)) {
427                 ret = PTR_ERR(key_ref);
428                 goto error2;
429         }
430
431         ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
432
433         key_ref_put(key_ref);
434 error2:
435         key_ref_put(keyring_ref);
436 error:
437         return ret;
438 }
439
440 /*
441  * describe a user key
442  * - the key must have view permission
443  * - if there's a buffer, we place up to buflen bytes of data into it
444  * - unless there's an error, we return the amount of description available,
445  *   irrespective of how much we may have copied
446  * - the description is formatted thus:
447  *      type;uid;gid;perm;description<NUL>
448  * - implements keyctl(KEYCTL_DESCRIBE)
449  */
450 long keyctl_describe_key(key_serial_t keyid,
451                          char __user *buffer,
452                          size_t buflen)
453 {
454         struct key *key, *instkey;
455         key_ref_t key_ref;
456         char *tmpbuf;
457         long ret;
458
459         key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
460         if (IS_ERR(key_ref)) {
461                 /* viewing a key under construction is permitted if we have the
462                  * authorisation token handy */
463                 if (PTR_ERR(key_ref) == -EACCES) {
464                         instkey = key_get_instantiation_authkey(keyid);
465                         if (!IS_ERR(instkey)) {
466                                 key_put(instkey);
467                                 key_ref = lookup_user_key(keyid,
468                                                           KEY_LOOKUP_PARTIAL,
469                                                           0);
470                                 if (!IS_ERR(key_ref))
471                                         goto okay;
472                         }
473                 }
474
475                 ret = PTR_ERR(key_ref);
476                 goto error;
477         }
478
479 okay:
480         /* calculate how much description we're going to return */
481         ret = -ENOMEM;
482         tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
483         if (!tmpbuf)
484                 goto error2;
485
486         key = key_ref_to_ptr(key_ref);
487
488         ret = snprintf(tmpbuf, PAGE_SIZE - 1,
489                        "%s;%d;%d;%08x;%s",
490                        key->type->name,
491                        key->uid,
492                        key->gid,
493                        key->perm,
494                        key->description ?: "");
495
496         /* include a NUL char at the end of the data */
497         if (ret > PAGE_SIZE - 1)
498                 ret = PAGE_SIZE - 1;
499         tmpbuf[ret] = 0;
500         ret++;
501
502         /* consider returning the data */
503         if (buffer && buflen > 0) {
504                 if (buflen > ret)
505                         buflen = ret;
506
507                 if (copy_to_user(buffer, tmpbuf, buflen) != 0)
508                         ret = -EFAULT;
509         }
510
511         kfree(tmpbuf);
512 error2:
513         key_ref_put(key_ref);
514 error:
515         return ret;
516 }
517
518 /*
519  * search the specified keyring for a matching key
520  * - the start keyring must be searchable
521  * - nested keyrings may also be searched if they are searchable
522  * - only keys with search permission may be found
523  * - if a key is found, it will be attached to the destination keyring if
524  *   there's one specified
525  * - implements keyctl(KEYCTL_SEARCH)
526  */
527 long keyctl_keyring_search(key_serial_t ringid,
528                            const char __user *_type,
529                            const char __user *_description,
530                            key_serial_t destringid)
531 {
532         struct key_type *ktype;
533         key_ref_t keyring_ref, key_ref, dest_ref;
534         char type[32], *description;
535         long ret;
536
537         /* pull the type and description into kernel space */
538         ret = key_get_type_from_user(type, _type, sizeof(type));
539         if (ret < 0)
540                 goto error;
541
542         description = strndup_user(_description, PAGE_SIZE);
543         if (IS_ERR(description)) {
544                 ret = PTR_ERR(description);
545                 goto error;
546         }
547
548         /* get the keyring at which to begin the search */
549         keyring_ref = lookup_user_key(ringid, 0, KEY_SEARCH);
550         if (IS_ERR(keyring_ref)) {
551                 ret = PTR_ERR(keyring_ref);
552                 goto error2;
553         }
554
555         /* get the destination keyring if specified */
556         dest_ref = NULL;
557         if (destringid) {
558                 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
559                                            KEY_WRITE);
560                 if (IS_ERR(dest_ref)) {
561                         ret = PTR_ERR(dest_ref);
562                         goto error3;
563                 }
564         }
565
566         /* find the key type */
567         ktype = key_type_lookup(type);
568         if (IS_ERR(ktype)) {
569                 ret = PTR_ERR(ktype);
570                 goto error4;
571         }
572
573         /* do the search */
574         key_ref = keyring_search(keyring_ref, ktype, description);
575         if (IS_ERR(key_ref)) {
576                 ret = PTR_ERR(key_ref);
577
578                 /* treat lack or presence of a negative key the same */
579                 if (ret == -EAGAIN)
580                         ret = -ENOKEY;
581                 goto error5;
582         }
583
584         /* link the resulting key to the destination keyring if we can */
585         if (dest_ref) {
586                 ret = key_permission(key_ref, KEY_LINK);
587                 if (ret < 0)
588                         goto error6;
589
590                 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
591                 if (ret < 0)
592                         goto error6;
593         }
594
595         ret = key_ref_to_ptr(key_ref)->serial;
596
597 error6:
598         key_ref_put(key_ref);
599 error5:
600         key_type_put(ktype);
601 error4:
602         key_ref_put(dest_ref);
603 error3:
604         key_ref_put(keyring_ref);
605 error2:
606         kfree(description);
607 error:
608         return ret;
609 }
610
611 /*
612  * read a user key's payload
613  * - the keyring must be readable or the key must be searchable from the
614  *   process's keyrings
615  * - if there's a buffer, we place up to buflen bytes of data into it
616  * - unless there's an error, we return the amount of data in the key,
617  *   irrespective of how much we may have copied
618  * - implements keyctl(KEYCTL_READ)
619  */
620 long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
621 {
622         struct key *key;
623         key_ref_t key_ref;
624         long ret;
625
626         /* find the key first */
627         key_ref = lookup_user_key(keyid, 0, 0);
628         if (IS_ERR(key_ref)) {
629                 ret = -ENOKEY;
630                 goto error;
631         }
632
633         key = key_ref_to_ptr(key_ref);
634
635         /* see if we can read it directly */
636         ret = key_permission(key_ref, KEY_READ);
637         if (ret == 0)
638                 goto can_read_key;
639         if (ret != -EACCES)
640                 goto error;
641
642         /* we can't; see if it's searchable from this process's keyrings
643          * - we automatically take account of the fact that it may be
644          *   dangling off an instantiation key
645          */
646         if (!is_key_possessed(key_ref)) {
647                 ret = -EACCES;
648                 goto error2;
649         }
650
651         /* the key is probably readable - now try to read it */
652 can_read_key:
653         ret = key_validate(key);
654         if (ret == 0) {
655                 ret = -EOPNOTSUPP;
656                 if (key->type->read) {
657                         /* read the data with the semaphore held (since we
658                          * might sleep) */
659                         down_read(&key->sem);
660                         ret = key->type->read(key, buffer, buflen);
661                         up_read(&key->sem);
662                 }
663         }
664
665 error2:
666         key_put(key);
667 error:
668         return ret;
669 }
670
671 /*
672  * change the ownership of a key
673  * - the keyring owned by the changer
674  * - if the uid or gid is -1, then that parameter is not changed
675  * - implements keyctl(KEYCTL_CHOWN)
676  */
677 long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
678 {
679         struct key_user *newowner, *zapowner = NULL;
680         struct key *key;
681         key_ref_t key_ref;
682         long ret;
683
684         ret = 0;
685         if (uid == (uid_t) -1 && gid == (gid_t) -1)
686                 goto error;
687
688         key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
689                                   KEY_SETATTR);
690         if (IS_ERR(key_ref)) {
691                 ret = PTR_ERR(key_ref);
692                 goto error;
693         }
694
695         key = key_ref_to_ptr(key_ref);
696
697         /* make the changes with the locks held to prevent chown/chown races */
698         ret = -EACCES;
699         down_write(&key->sem);
700
701         if (!capable(CAP_SYS_ADMIN)) {
702                 /* only the sysadmin can chown a key to some other UID */
703                 if (uid != (uid_t) -1 && key->uid != uid)
704                         goto error_put;
705
706                 /* only the sysadmin can set the key's GID to a group other
707                  * than one of those that the current process subscribes to */
708                 if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
709                         goto error_put;
710         }
711
712         /* change the UID */
713         if (uid != (uid_t) -1 && uid != key->uid) {
714                 ret = -ENOMEM;
715                 newowner = key_user_lookup(uid, current_user_ns());
716                 if (!newowner)
717                         goto error_put;
718
719                 /* transfer the quota burden to the new user */
720                 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
721                         unsigned maxkeys = (uid == 0) ?
722                                 key_quota_root_maxkeys : key_quota_maxkeys;
723                         unsigned maxbytes = (uid == 0) ?
724                                 key_quota_root_maxbytes : key_quota_maxbytes;
725
726                         spin_lock(&newowner->lock);
727                         if (newowner->qnkeys + 1 >= maxkeys ||
728                             newowner->qnbytes + key->quotalen >= maxbytes ||
729                             newowner->qnbytes + key->quotalen <
730                             newowner->qnbytes)
731                                 goto quota_overrun;
732
733                         newowner->qnkeys++;
734                         newowner->qnbytes += key->quotalen;
735                         spin_unlock(&newowner->lock);
736
737                         spin_lock(&key->user->lock);
738                         key->user->qnkeys--;
739                         key->user->qnbytes -= key->quotalen;
740                         spin_unlock(&key->user->lock);
741                 }
742
743                 atomic_dec(&key->user->nkeys);
744                 atomic_inc(&newowner->nkeys);
745
746                 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
747                         atomic_dec(&key->user->nikeys);
748                         atomic_inc(&newowner->nikeys);
749                 }
750
751                 zapowner = key->user;
752                 key->user = newowner;
753                 key->uid = uid;
754         }
755
756         /* change the GID */
757         if (gid != (gid_t) -1)
758                 key->gid = gid;
759
760         ret = 0;
761
762 error_put:
763         up_write(&key->sem);
764         key_put(key);
765         if (zapowner)
766                 key_user_put(zapowner);
767 error:
768         return ret;
769
770 quota_overrun:
771         spin_unlock(&newowner->lock);
772         zapowner = newowner;
773         ret = -EDQUOT;
774         goto error_put;
775 }
776
777 /*
778  * change the permission mask on a key
779  * - the keyring owned by the changer
780  * - implements keyctl(KEYCTL_SETPERM)
781  */
782 long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
783 {
784         struct key *key;
785         key_ref_t key_ref;
786         long ret;
787
788         ret = -EINVAL;
789         if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
790                 goto error;
791
792         key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
793                                   KEY_SETATTR);
794         if (IS_ERR(key_ref)) {
795                 ret = PTR_ERR(key_ref);
796                 goto error;
797         }
798
799         key = key_ref_to_ptr(key_ref);
800
801         /* make the changes with the locks held to prevent chown/chmod races */
802         ret = -EACCES;
803         down_write(&key->sem);
804
805         /* if we're not the sysadmin, we can only change a key that we own */
806         if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) {
807                 key->perm = perm;
808                 ret = 0;
809         }
810
811         up_write(&key->sem);
812         key_put(key);
813 error:
814         return ret;
815 }
816
817 /*
818  * get the destination keyring for instantiation
819  */
820 static long get_instantiation_keyring(key_serial_t ringid,
821                                       struct request_key_auth *rka,
822                                       struct key **_dest_keyring)
823 {
824         key_ref_t dkref;
825
826         *_dest_keyring = NULL;
827
828         /* just return a NULL pointer if we weren't asked to make a link */
829         if (ringid == 0)
830                 return 0;
831
832         /* if a specific keyring is nominated by ID, then use that */
833         if (ringid > 0) {
834                 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
835                 if (IS_ERR(dkref))
836                         return PTR_ERR(dkref);
837                 *_dest_keyring = key_ref_to_ptr(dkref);
838                 return 0;
839         }
840
841         if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
842                 return -EINVAL;
843
844         /* otherwise specify the destination keyring recorded in the
845          * authorisation key (any KEY_SPEC_*_KEYRING) */
846         if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
847                 *_dest_keyring = key_get(rka->dest_keyring);
848                 return 0;
849         }
850
851         return -ENOKEY;
852 }
853
854 /*
855  * change the request_key authorisation key on the current process
856  */
857 static int keyctl_change_reqkey_auth(struct key *key)
858 {
859         struct cred *new;
860
861         new = prepare_creds();
862         if (!new)
863                 return -ENOMEM;
864
865         key_put(new->request_key_auth);
866         new->request_key_auth = key_get(key);
867
868         return commit_creds(new);
869 }
870
871 /*
872  * instantiate the key with the specified payload, and, if one is given, link
873  * the key into the keyring
874  */
875 long keyctl_instantiate_key(key_serial_t id,
876                             const void __user *_payload,
877                             size_t plen,
878                             key_serial_t ringid)
879 {
880         const struct cred *cred = current_cred();
881         struct request_key_auth *rka;
882         struct key *instkey, *dest_keyring;
883         void *payload;
884         long ret;
885         bool vm = false;
886
887         kenter("%d,,%zu,%d", id, plen, ringid);
888
889         ret = -EINVAL;
890         if (plen > 1024 * 1024 - 1)
891                 goto error;
892
893         /* the appropriate instantiation authorisation key must have been
894          * assumed before calling this */
895         ret = -EPERM;
896         instkey = cred->request_key_auth;
897         if (!instkey)
898                 goto error;
899
900         rka = instkey->payload.data;
901         if (rka->target_key->serial != id)
902                 goto error;
903
904         /* pull the payload in if one was supplied */
905         payload = NULL;
906
907         if (_payload) {
908                 ret = -ENOMEM;
909                 payload = kmalloc(plen, GFP_KERNEL);
910                 if (!payload) {
911                         if (plen <= PAGE_SIZE)
912                                 goto error;
913                         vm = true;
914                         payload = vmalloc(plen);
915                         if (!payload)
916                                 goto error;
917                 }
918
919                 ret = -EFAULT;
920                 if (copy_from_user(payload, _payload, plen) != 0)
921                         goto error2;
922         }
923
924         /* find the destination keyring amongst those belonging to the
925          * requesting task */
926         ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
927         if (ret < 0)
928                 goto error2;
929
930         /* instantiate the key and link it into a keyring */
931         ret = key_instantiate_and_link(rka->target_key, payload, plen,
932                                        dest_keyring, instkey);
933
934         key_put(dest_keyring);
935
936         /* discard the assumed authority if it's just been disabled by
937          * instantiation of the key */
938         if (ret == 0)
939                 keyctl_change_reqkey_auth(NULL);
940
941 error2:
942         if (!vm)
943                 kfree(payload);
944         else
945                 vfree(payload);
946 error:
947         return ret;
948 }
949
950 /*
951  * negatively instantiate the key with the given timeout (in seconds), and, if
952  * one is given, link the key into the keyring
953  */
954 long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
955 {
956         const struct cred *cred = current_cred();
957         struct request_key_auth *rka;
958         struct key *instkey, *dest_keyring;
959         long ret;
960
961         kenter("%d,%u,%d", id, timeout, ringid);
962
963         /* the appropriate instantiation authorisation key must have been
964          * assumed before calling this */
965         ret = -EPERM;
966         instkey = cred->request_key_auth;
967         if (!instkey)
968                 goto error;
969
970         rka = instkey->payload.data;
971         if (rka->target_key->serial != id)
972                 goto error;
973
974         /* find the destination keyring if present (which must also be
975          * writable) */
976         ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
977         if (ret < 0)
978                 goto error;
979
980         /* instantiate the key and link it into a keyring */
981         ret = key_negate_and_link(rka->target_key, timeout,
982                                   dest_keyring, instkey);
983
984         key_put(dest_keyring);
985
986         /* discard the assumed authority if it's just been disabled by
987          * instantiation of the key */
988         if (ret == 0)
989                 keyctl_change_reqkey_auth(NULL);
990
991 error:
992         return ret;
993 }
994
995 /*
996  * set the default keyring in which request_key() will cache keys
997  * - return the old setting
998  */
999 long keyctl_set_reqkey_keyring(int reqkey_defl)
1000 {
1001         struct cred *new;
1002         int ret, old_setting;
1003
1004         old_setting = current_cred_xxx(jit_keyring);
1005
1006         if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1007                 return old_setting;
1008
1009         new = prepare_creds();
1010         if (!new)
1011                 return -ENOMEM;
1012
1013         switch (reqkey_defl) {
1014         case KEY_REQKEY_DEFL_THREAD_KEYRING:
1015                 ret = install_thread_keyring_to_cred(new);
1016                 if (ret < 0)
1017                         goto error;
1018                 goto set;
1019
1020         case KEY_REQKEY_DEFL_PROCESS_KEYRING:
1021                 ret = install_process_keyring_to_cred(new);
1022                 if (ret < 0) {
1023                         if (ret != -EEXIST)
1024                                 goto error;
1025                         ret = 0;
1026                 }
1027                 goto set;
1028
1029         case KEY_REQKEY_DEFL_DEFAULT:
1030         case KEY_REQKEY_DEFL_SESSION_KEYRING:
1031         case KEY_REQKEY_DEFL_USER_KEYRING:
1032         case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1033         case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1034                 goto set;
1035
1036         case KEY_REQKEY_DEFL_NO_CHANGE:
1037         case KEY_REQKEY_DEFL_GROUP_KEYRING:
1038         default:
1039                 ret = -EINVAL;
1040                 goto error;
1041         }
1042
1043 set:
1044         new->jit_keyring = reqkey_defl;
1045         commit_creds(new);
1046         return old_setting;
1047 error:
1048         abort_creds(new);
1049         return ret;
1050 }
1051
1052 /*
1053  * set or clear the timeout for a key
1054  */
1055 long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1056 {
1057         struct timespec now;
1058         struct key *key, *instkey;
1059         key_ref_t key_ref;
1060         time_t expiry;
1061         long ret;
1062
1063         key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1064                                   KEY_SETATTR);
1065         if (IS_ERR(key_ref)) {
1066                 /* setting the timeout on a key under construction is permitted
1067                  * if we have the authorisation token handy */
1068                 if (PTR_ERR(key_ref) == -EACCES) {
1069                         instkey = key_get_instantiation_authkey(id);
1070                         if (!IS_ERR(instkey)) {
1071                                 key_put(instkey);
1072                                 key_ref = lookup_user_key(id,
1073                                                           KEY_LOOKUP_PARTIAL,
1074                                                           0);
1075                                 if (!IS_ERR(key_ref))
1076                                         goto okay;
1077                         }
1078                 }
1079
1080                 ret = PTR_ERR(key_ref);
1081                 goto error;
1082         }
1083
1084 okay:
1085         key = key_ref_to_ptr(key_ref);
1086
1087         /* make the changes with the locks held to prevent races */
1088         down_write(&key->sem);
1089
1090         expiry = 0;
1091         if (timeout > 0) {
1092                 now = current_kernel_time();
1093                 expiry = now.tv_sec + timeout;
1094         }
1095
1096         key->expiry = expiry;
1097         key_schedule_gc(key->expiry + key_gc_delay);
1098
1099         up_write(&key->sem);
1100         key_put(key);
1101
1102         ret = 0;
1103 error:
1104         return ret;
1105 }
1106
1107 /*
1108  * assume the authority to instantiate the specified key
1109  */
1110 long keyctl_assume_authority(key_serial_t id)
1111 {
1112         struct key *authkey;
1113         long ret;
1114
1115         /* special key IDs aren't permitted */
1116         ret = -EINVAL;
1117         if (id < 0)
1118                 goto error;
1119
1120         /* we divest ourselves of authority if given an ID of 0 */
1121         if (id == 0) {
1122                 ret = keyctl_change_reqkey_auth(NULL);
1123                 goto error;
1124         }
1125
1126         /* attempt to assume the authority temporarily granted to us whilst we
1127          * instantiate the specified key
1128          * - the authorisation key must be in the current task's keyrings
1129          *   somewhere
1130          */
1131         authkey = key_get_instantiation_authkey(id);
1132         if (IS_ERR(authkey)) {
1133                 ret = PTR_ERR(authkey);
1134                 goto error;
1135         }
1136
1137         ret = keyctl_change_reqkey_auth(authkey);
1138         if (ret < 0)
1139                 goto error;
1140         key_put(authkey);
1141
1142         ret = authkey->serial;
1143 error:
1144         return ret;
1145 }
1146
1147 /*
1148  * get the security label of a key
1149  * - the key must grant us view permission
1150  * - if there's a buffer, we place up to buflen bytes of data into it
1151  * - unless there's an error, we return the amount of information available,
1152  *   irrespective of how much we may have copied (including the terminal NUL)
1153  * - implements keyctl(KEYCTL_GET_SECURITY)
1154  */
1155 long keyctl_get_security(key_serial_t keyid,
1156                          char __user *buffer,
1157                          size_t buflen)
1158 {
1159         struct key *key, *instkey;
1160         key_ref_t key_ref;
1161         char *context;
1162         long ret;
1163
1164         key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
1165         if (IS_ERR(key_ref)) {
1166                 if (PTR_ERR(key_ref) != -EACCES)
1167                         return PTR_ERR(key_ref);
1168
1169                 /* viewing a key under construction is also permitted if we
1170                  * have the authorisation token handy */
1171                 instkey = key_get_instantiation_authkey(keyid);
1172                 if (IS_ERR(instkey))
1173                         return PTR_ERR(instkey);
1174                 key_put(instkey);
1175
1176                 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
1177                 if (IS_ERR(key_ref))
1178                         return PTR_ERR(key_ref);
1179         }
1180
1181         key = key_ref_to_ptr(key_ref);
1182         ret = security_key_getsecurity(key, &context);
1183         if (ret == 0) {
1184                 /* if no information was returned, give userspace an empty
1185                  * string */
1186                 ret = 1;
1187                 if (buffer && buflen > 0 &&
1188                     copy_to_user(buffer, "", 1) != 0)
1189                         ret = -EFAULT;
1190         } else if (ret > 0) {
1191                 /* return as much data as there's room for */
1192                 if (buffer && buflen > 0) {
1193                         if (buflen > ret)
1194                                 buflen = ret;
1195
1196                         if (copy_to_user(buffer, context, buflen) != 0)
1197                                 ret = -EFAULT;
1198                 }
1199
1200                 kfree(context);
1201         }
1202
1203         key_ref_put(key_ref);
1204         return ret;
1205 }
1206
1207 /*
1208  * attempt to install the calling process's session keyring on the process's
1209  * parent process
1210  * - the keyring must exist and must grant us LINK permission
1211  * - implements keyctl(KEYCTL_SESSION_TO_PARENT)
1212  */
1213 long keyctl_session_to_parent(void)
1214 {
1215 #ifdef TIF_NOTIFY_RESUME
1216         struct task_struct *me, *parent;
1217         const struct cred *mycred, *pcred;
1218         struct cred *cred, *oldcred;
1219         key_ref_t keyring_r;
1220         int ret;
1221
1222         keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_LINK);
1223         if (IS_ERR(keyring_r))
1224                 return PTR_ERR(keyring_r);
1225
1226         /* our parent is going to need a new cred struct, a new tgcred struct
1227          * and new security data, so we allocate them here to prevent ENOMEM in
1228          * our parent */
1229         ret = -ENOMEM;
1230         cred = cred_alloc_blank();
1231         if (!cred)
1232                 goto error_keyring;
1233
1234         cred->tgcred->session_keyring = key_ref_to_ptr(keyring_r);
1235         keyring_r = NULL;
1236
1237         me = current;
1238         rcu_read_lock();
1239         write_lock_irq(&tasklist_lock);
1240
1241         parent = me->real_parent;
1242         ret = -EPERM;
1243
1244         /* the parent mustn't be init and mustn't be a kernel thread */
1245         if (parent->pid <= 1 || !parent->mm)
1246                 goto not_permitted;
1247
1248         /* the parent must be single threaded */
1249         if (!thread_group_empty(parent))
1250                 goto not_permitted;
1251
1252         /* the parent and the child must have different session keyrings or
1253          * there's no point */
1254         mycred = current_cred();
1255         pcred = __task_cred(parent);
1256         if (mycred == pcred ||
1257             mycred->tgcred->session_keyring == pcred->tgcred->session_keyring)
1258                 goto already_same;
1259
1260         /* the parent must have the same effective ownership and mustn't be
1261          * SUID/SGID */
1262         if (pcred->uid  != mycred->euid ||
1263             pcred->euid != mycred->euid ||
1264             pcred->suid != mycred->euid ||
1265             pcred->gid  != mycred->egid ||
1266             pcred->egid != mycred->egid ||
1267             pcred->sgid != mycred->egid)
1268                 goto not_permitted;
1269
1270         /* the keyrings must have the same UID */
1271         if ((pcred->tgcred->session_keyring &&
1272              pcred->tgcred->session_keyring->uid != mycred->euid) ||
1273             mycred->tgcred->session_keyring->uid != mycred->euid)
1274                 goto not_permitted;
1275
1276         /* if there's an already pending keyring replacement, then we replace
1277          * that */
1278         oldcred = parent->replacement_session_keyring;
1279
1280         /* the replacement session keyring is applied just prior to userspace
1281          * restarting */
1282         parent->replacement_session_keyring = cred;
1283         cred = NULL;
1284         set_ti_thread_flag(task_thread_info(parent), TIF_NOTIFY_RESUME);
1285
1286         write_unlock_irq(&tasklist_lock);
1287         rcu_read_unlock();
1288         if (oldcred)
1289                 put_cred(oldcred);
1290         return 0;
1291
1292 already_same:
1293         ret = 0;
1294 not_permitted:
1295         write_unlock_irq(&tasklist_lock);
1296         rcu_read_unlock();
1297         put_cred(cred);
1298         return ret;
1299
1300 error_keyring:
1301         key_ref_put(keyring_r);
1302         return ret;
1303
1304 #else /* !TIF_NOTIFY_RESUME */
1305         /*
1306          * To be removed when TIF_NOTIFY_RESUME has been implemented on
1307          * m68k/xtensa
1308          */
1309 #warning TIF_NOTIFY_RESUME not implemented
1310         return -EOPNOTSUPP;
1311 #endif /* !TIF_NOTIFY_RESUME */
1312 }
1313
1314 /*
1315  * the key control system call
1316  */
1317 SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1318                 unsigned long, arg4, unsigned long, arg5)
1319 {
1320         switch (option) {
1321         case KEYCTL_GET_KEYRING_ID:
1322                 return keyctl_get_keyring_ID((key_serial_t) arg2,
1323                                              (int) arg3);
1324
1325         case KEYCTL_JOIN_SESSION_KEYRING:
1326                 return keyctl_join_session_keyring((const char __user *) arg2);
1327
1328         case KEYCTL_UPDATE:
1329                 return keyctl_update_key((key_serial_t) arg2,
1330                                          (const void __user *) arg3,
1331                                          (size_t) arg4);
1332
1333         case KEYCTL_REVOKE:
1334                 return keyctl_revoke_key((key_serial_t) arg2);
1335
1336         case KEYCTL_DESCRIBE:
1337                 return keyctl_describe_key((key_serial_t) arg2,
1338                                            (char __user *) arg3,
1339                                            (unsigned) arg4);
1340
1341         case KEYCTL_CLEAR:
1342                 return keyctl_keyring_clear((key_serial_t) arg2);
1343
1344         case KEYCTL_LINK:
1345                 return keyctl_keyring_link((key_serial_t) arg2,
1346                                            (key_serial_t) arg3);
1347
1348         case KEYCTL_UNLINK:
1349                 return keyctl_keyring_unlink((key_serial_t) arg2,
1350                                              (key_serial_t) arg3);
1351
1352         case KEYCTL_SEARCH:
1353                 return keyctl_keyring_search((key_serial_t) arg2,
1354                                              (const char __user *) arg3,
1355                                              (const char __user *) arg4,
1356                                              (key_serial_t) arg5);
1357
1358         case KEYCTL_READ:
1359                 return keyctl_read_key((key_serial_t) arg2,
1360                                        (char __user *) arg3,
1361                                        (size_t) arg4);
1362
1363         case KEYCTL_CHOWN:
1364                 return keyctl_chown_key((key_serial_t) arg2,
1365                                         (uid_t) arg3,
1366                                         (gid_t) arg4);
1367
1368         case KEYCTL_SETPERM:
1369                 return keyctl_setperm_key((key_serial_t) arg2,
1370                                           (key_perm_t) arg3);
1371
1372         case KEYCTL_INSTANTIATE:
1373                 return keyctl_instantiate_key((key_serial_t) arg2,
1374                                               (const void __user *) arg3,
1375                                               (size_t) arg4,
1376                                               (key_serial_t) arg5);
1377
1378         case KEYCTL_NEGATE:
1379                 return keyctl_negate_key((key_serial_t) arg2,
1380                                          (unsigned) arg3,
1381                                          (key_serial_t) arg4);
1382
1383         case KEYCTL_SET_REQKEY_KEYRING:
1384                 return keyctl_set_reqkey_keyring(arg2);
1385
1386         case KEYCTL_SET_TIMEOUT:
1387                 return keyctl_set_timeout((key_serial_t) arg2,
1388                                           (unsigned) arg3);
1389
1390         case KEYCTL_ASSUME_AUTHORITY:
1391                 return keyctl_assume_authority((key_serial_t) arg2);
1392
1393         case KEYCTL_GET_SECURITY:
1394                 return keyctl_get_security((key_serial_t) arg2,
1395                                            (char __user *) arg3,
1396                                            (size_t) arg4);
1397
1398         case KEYCTL_SESSION_TO_PARENT:
1399                 return keyctl_session_to_parent();
1400
1401         default:
1402                 return -EOPNOTSUPP;
1403         }
1404 }