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