6b414b74bb866a55bd988a0ca8c59c423a11f02e
[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.  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 (_payload) {
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 process keyring is specified then this will be created if it doesn't
1187  * 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                         if (ret != -EEXIST)
1214                                 goto error;
1215                         ret = 0;
1216                 }
1217                 goto set;
1218
1219         case KEY_REQKEY_DEFL_DEFAULT:
1220         case KEY_REQKEY_DEFL_SESSION_KEYRING:
1221         case KEY_REQKEY_DEFL_USER_KEYRING:
1222         case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1223         case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1224                 goto set;
1225
1226         case KEY_REQKEY_DEFL_NO_CHANGE:
1227         case KEY_REQKEY_DEFL_GROUP_KEYRING:
1228         default:
1229                 ret = -EINVAL;
1230                 goto error;
1231         }
1232
1233 set:
1234         new->jit_keyring = reqkey_defl;
1235         commit_creds(new);
1236         return old_setting;
1237 error:
1238         abort_creds(new);
1239         return ret;
1240 }
1241
1242 /*
1243  * Set or clear the timeout on a key.
1244  *
1245  * Either the key must grant the caller Setattr permission or else the caller
1246  * must hold an instantiation authorisation token for the key.
1247  *
1248  * The timeout is either 0 to clear the timeout, or a number of seconds from
1249  * the current time.  The key and any links to the key will be automatically
1250  * garbage collected after the timeout expires.
1251  *
1252  * If successful, 0 is returned.
1253  */
1254 long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1255 {
1256         struct timespec now;
1257         struct key *key, *instkey;
1258         key_ref_t key_ref;
1259         time_t expiry;
1260         long ret;
1261
1262         key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1263                                   KEY_SETATTR);
1264         if (IS_ERR(key_ref)) {
1265                 /* setting the timeout on a key under construction is permitted
1266                  * if we have the authorisation token handy */
1267                 if (PTR_ERR(key_ref) == -EACCES) {
1268                         instkey = key_get_instantiation_authkey(id);
1269                         if (!IS_ERR(instkey)) {
1270                                 key_put(instkey);
1271                                 key_ref = lookup_user_key(id,
1272                                                           KEY_LOOKUP_PARTIAL,
1273                                                           0);
1274                                 if (!IS_ERR(key_ref))
1275                                         goto okay;
1276                         }
1277                 }
1278
1279                 ret = PTR_ERR(key_ref);
1280                 goto error;
1281         }
1282
1283 okay:
1284         key = key_ref_to_ptr(key_ref);
1285
1286         /* make the changes with the locks held to prevent races */
1287         down_write(&key->sem);
1288
1289         expiry = 0;
1290         if (timeout > 0) {
1291                 now = current_kernel_time();
1292                 expiry = now.tv_sec + timeout;
1293         }
1294
1295         key->expiry = expiry;
1296         key_schedule_gc(key->expiry + key_gc_delay);
1297
1298         up_write(&key->sem);
1299         key_put(key);
1300
1301         ret = 0;
1302 error:
1303         return ret;
1304 }
1305
1306 /*
1307  * Assume (or clear) the authority to instantiate the specified key.
1308  *
1309  * This sets the authoritative token currently in force for key instantiation.
1310  * This must be done for a key to be instantiated.  It has the effect of making
1311  * available all the keys from the caller of the request_key() that created a
1312  * key to request_key() calls made by the caller of this function.
1313  *
1314  * The caller must have the instantiation key in their process keyrings with a
1315  * Search permission grant available to the caller.
1316  *
1317  * If the ID given is 0, then the setting will be cleared and 0 returned.
1318  *
1319  * If the ID given has a matching an authorisation key, then that key will be
1320  * set and its ID will be returned.  The authorisation key can be read to get
1321  * the callout information passed to request_key().
1322  */
1323 long keyctl_assume_authority(key_serial_t id)
1324 {
1325         struct key *authkey;
1326         long ret;
1327
1328         /* special key IDs aren't permitted */
1329         ret = -EINVAL;
1330         if (id < 0)
1331                 goto error;
1332
1333         /* we divest ourselves of authority if given an ID of 0 */
1334         if (id == 0) {
1335                 ret = keyctl_change_reqkey_auth(NULL);
1336                 goto error;
1337         }
1338
1339         /* attempt to assume the authority temporarily granted to us whilst we
1340          * instantiate the specified key
1341          * - the authorisation key must be in the current task's keyrings
1342          *   somewhere
1343          */
1344         authkey = key_get_instantiation_authkey(id);
1345         if (IS_ERR(authkey)) {
1346                 ret = PTR_ERR(authkey);
1347                 goto error;
1348         }
1349
1350         ret = keyctl_change_reqkey_auth(authkey);
1351         if (ret < 0)
1352                 goto error;
1353         key_put(authkey);
1354
1355         ret = authkey->serial;
1356 error:
1357         return ret;
1358 }
1359
1360 /*
1361  * Get a key's the LSM security label.
1362  *
1363  * The key must grant the caller View permission for this to work.
1364  *
1365  * If there's a buffer, then up to buflen bytes of data will be placed into it.
1366  *
1367  * If successful, the amount of information available will be returned,
1368  * irrespective of how much was copied (including the terminal NUL).
1369  */
1370 long keyctl_get_security(key_serial_t keyid,
1371                          char __user *buffer,
1372                          size_t buflen)
1373 {
1374         struct key *key, *instkey;
1375         key_ref_t key_ref;
1376         char *context;
1377         long ret;
1378
1379         key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
1380         if (IS_ERR(key_ref)) {
1381                 if (PTR_ERR(key_ref) != -EACCES)
1382                         return PTR_ERR(key_ref);
1383
1384                 /* viewing a key under construction is also permitted if we
1385                  * have the authorisation token handy */
1386                 instkey = key_get_instantiation_authkey(keyid);
1387                 if (IS_ERR(instkey))
1388                         return PTR_ERR(instkey);
1389                 key_put(instkey);
1390
1391                 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
1392                 if (IS_ERR(key_ref))
1393                         return PTR_ERR(key_ref);
1394         }
1395
1396         key = key_ref_to_ptr(key_ref);
1397         ret = security_key_getsecurity(key, &context);
1398         if (ret == 0) {
1399                 /* if no information was returned, give userspace an empty
1400                  * string */
1401                 ret = 1;
1402                 if (buffer && buflen > 0 &&
1403                     copy_to_user(buffer, "", 1) != 0)
1404                         ret = -EFAULT;
1405         } else if (ret > 0) {
1406                 /* return as much data as there's room for */
1407                 if (buffer && buflen > 0) {
1408                         if (buflen > ret)
1409                                 buflen = ret;
1410
1411                         if (copy_to_user(buffer, context, buflen) != 0)
1412                                 ret = -EFAULT;
1413                 }
1414
1415                 kfree(context);
1416         }
1417
1418         key_ref_put(key_ref);
1419         return ret;
1420 }
1421
1422 /*
1423  * Attempt to install the calling process's session keyring on the process's
1424  * parent process.
1425  *
1426  * The keyring must exist and must grant the caller LINK permission, and the
1427  * parent process must be single-threaded and must have the same effective
1428  * ownership as this process and mustn't be SUID/SGID.
1429  *
1430  * The keyring will be emplaced on the parent when it next resumes userspace.
1431  *
1432  * If successful, 0 will be returned.
1433  */
1434 long keyctl_session_to_parent(void)
1435 {
1436 #ifdef TIF_NOTIFY_RESUME
1437         struct task_struct *me, *parent;
1438         const struct cred *mycred, *pcred;
1439         struct cred *cred, *oldcred;
1440         key_ref_t keyring_r;
1441         int ret;
1442
1443         keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_LINK);
1444         if (IS_ERR(keyring_r))
1445                 return PTR_ERR(keyring_r);
1446
1447         /* our parent is going to need a new cred struct, a new tgcred struct
1448          * and new security data, so we allocate them here to prevent ENOMEM in
1449          * our parent */
1450         ret = -ENOMEM;
1451         cred = cred_alloc_blank();
1452         if (!cred)
1453                 goto error_keyring;
1454
1455         cred->tgcred->session_keyring = key_ref_to_ptr(keyring_r);
1456         keyring_r = NULL;
1457
1458         me = current;
1459         rcu_read_lock();
1460         write_lock_irq(&tasklist_lock);
1461
1462         parent = me->real_parent;
1463         ret = -EPERM;
1464
1465         /* the parent mustn't be init and mustn't be a kernel thread */
1466         if (parent->pid <= 1 || !parent->mm)
1467                 goto not_permitted;
1468
1469         /* the parent must be single threaded */
1470         if (!thread_group_empty(parent))
1471                 goto not_permitted;
1472
1473         /* the parent and the child must have different session keyrings or
1474          * there's no point */
1475         mycred = current_cred();
1476         pcred = __task_cred(parent);
1477         if (mycred == pcred ||
1478             mycred->tgcred->session_keyring == pcred->tgcred->session_keyring)
1479                 goto already_same;
1480
1481         /* the parent must have the same effective ownership and mustn't be
1482          * SUID/SGID */
1483         if (pcred->uid  != mycred->euid ||
1484             pcred->euid != mycred->euid ||
1485             pcred->suid != mycred->euid ||
1486             pcred->gid  != mycred->egid ||
1487             pcred->egid != mycred->egid ||
1488             pcred->sgid != mycred->egid)
1489                 goto not_permitted;
1490
1491         /* the keyrings must have the same UID */
1492         if ((pcred->tgcred->session_keyring &&
1493              pcred->tgcred->session_keyring->uid != mycred->euid) ||
1494             mycred->tgcred->session_keyring->uid != mycred->euid)
1495                 goto not_permitted;
1496
1497         /* if there's an already pending keyring replacement, then we replace
1498          * that */
1499         oldcred = parent->replacement_session_keyring;
1500
1501         /* the replacement session keyring is applied just prior to userspace
1502          * restarting */
1503         parent->replacement_session_keyring = cred;
1504         cred = NULL;
1505         set_ti_thread_flag(task_thread_info(parent), TIF_NOTIFY_RESUME);
1506
1507         write_unlock_irq(&tasklist_lock);
1508         rcu_read_unlock();
1509         if (oldcred)
1510                 put_cred(oldcred);
1511         return 0;
1512
1513 already_same:
1514         ret = 0;
1515 not_permitted:
1516         write_unlock_irq(&tasklist_lock);
1517         rcu_read_unlock();
1518         put_cred(cred);
1519         return ret;
1520
1521 error_keyring:
1522         key_ref_put(keyring_r);
1523         return ret;
1524
1525 #else /* !TIF_NOTIFY_RESUME */
1526         /*
1527          * To be removed when TIF_NOTIFY_RESUME has been implemented on
1528          * m68k/xtensa
1529          */
1530 #warning TIF_NOTIFY_RESUME not implemented
1531         return -EOPNOTSUPP;
1532 #endif /* !TIF_NOTIFY_RESUME */
1533 }
1534
1535 /*
1536  * The key control system call
1537  */
1538 SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1539                 unsigned long, arg4, unsigned long, arg5)
1540 {
1541         switch (option) {
1542         case KEYCTL_GET_KEYRING_ID:
1543                 return keyctl_get_keyring_ID((key_serial_t) arg2,
1544                                              (int) arg3);
1545
1546         case KEYCTL_JOIN_SESSION_KEYRING:
1547                 return keyctl_join_session_keyring((const char __user *) arg2);
1548
1549         case KEYCTL_UPDATE:
1550                 return keyctl_update_key((key_serial_t) arg2,
1551                                          (const void __user *) arg3,
1552                                          (size_t) arg4);
1553
1554         case KEYCTL_REVOKE:
1555                 return keyctl_revoke_key((key_serial_t) arg2);
1556
1557         case KEYCTL_DESCRIBE:
1558                 return keyctl_describe_key((key_serial_t) arg2,
1559                                            (char __user *) arg3,
1560                                            (unsigned) arg4);
1561
1562         case KEYCTL_CLEAR:
1563                 return keyctl_keyring_clear((key_serial_t) arg2);
1564
1565         case KEYCTL_LINK:
1566                 return keyctl_keyring_link((key_serial_t) arg2,
1567                                            (key_serial_t) arg3);
1568
1569         case KEYCTL_UNLINK:
1570                 return keyctl_keyring_unlink((key_serial_t) arg2,
1571                                              (key_serial_t) arg3);
1572
1573         case KEYCTL_SEARCH:
1574                 return keyctl_keyring_search((key_serial_t) arg2,
1575                                              (const char __user *) arg3,
1576                                              (const char __user *) arg4,
1577                                              (key_serial_t) arg5);
1578
1579         case KEYCTL_READ:
1580                 return keyctl_read_key((key_serial_t) arg2,
1581                                        (char __user *) arg3,
1582                                        (size_t) arg4);
1583
1584         case KEYCTL_CHOWN:
1585                 return keyctl_chown_key((key_serial_t) arg2,
1586                                         (uid_t) arg3,
1587                                         (gid_t) arg4);
1588
1589         case KEYCTL_SETPERM:
1590                 return keyctl_setperm_key((key_serial_t) arg2,
1591                                           (key_perm_t) arg3);
1592
1593         case KEYCTL_INSTANTIATE:
1594                 return keyctl_instantiate_key((key_serial_t) arg2,
1595                                               (const void __user *) arg3,
1596                                               (size_t) arg4,
1597                                               (key_serial_t) arg5);
1598
1599         case KEYCTL_NEGATE:
1600                 return keyctl_negate_key((key_serial_t) arg2,
1601                                          (unsigned) arg3,
1602                                          (key_serial_t) arg4);
1603
1604         case KEYCTL_SET_REQKEY_KEYRING:
1605                 return keyctl_set_reqkey_keyring(arg2);
1606
1607         case KEYCTL_SET_TIMEOUT:
1608                 return keyctl_set_timeout((key_serial_t) arg2,
1609                                           (unsigned) arg3);
1610
1611         case KEYCTL_ASSUME_AUTHORITY:
1612                 return keyctl_assume_authority((key_serial_t) arg2);
1613
1614         case KEYCTL_GET_SECURITY:
1615                 return keyctl_get_security((key_serial_t) arg2,
1616                                            (char __user *) arg3,
1617                                            (size_t) arg4);
1618
1619         case KEYCTL_SESSION_TO_PARENT:
1620                 return keyctl_session_to_parent();
1621
1622         case KEYCTL_REJECT:
1623                 return keyctl_reject_key((key_serial_t) arg2,
1624                                          (unsigned) arg3,
1625                                          (unsigned) arg4,
1626                                          (key_serial_t) arg5);
1627
1628         case KEYCTL_INSTANTIATE_IOV:
1629                 return keyctl_instantiate_key_iov(
1630                         (key_serial_t) arg2,
1631                         (const struct iovec __user *) arg3,
1632                         (unsigned) arg4,
1633                         (key_serial_t) arg5);
1634
1635         default:
1636                 return -EOPNOTSUPP;
1637         }
1638 }