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