1 /* keyctl.c: userspace keyctl operations
3 * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
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>
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>
27 static int key_get_type_from_user(char *type
,
28 const char __user
*_type
,
33 ret
= strncpy_from_user(type
, _type
, len
);
38 if (ret
== 0 || ret
>= len
)
49 /*****************************************************************************/
51 * extract the description of a new key from userspace and either add it as a
52 * new key to the specified keyring or update a matching key in that keyring
53 * - the keyring must be writable
54 * - returns the new key's serial number
55 * - implements add_key()
57 SYSCALL_DEFINE5(add_key
, const char __user
*, _type
,
58 const char __user
*, _description
,
59 const void __user
*, _payload
,
63 key_ref_t keyring_ref
, key_ref
;
64 char type
[32], *description
;
70 if (plen
> 1024 * 1024 - 1)
73 /* draw all the data into kernel space */
74 ret
= key_get_type_from_user(type
, _type
, sizeof(type
));
78 description
= strndup_user(_description
, PAGE_SIZE
);
79 if (IS_ERR(description
)) {
80 ret
= PTR_ERR(description
);
84 /* pull the payload in if one was supplied */
90 payload
= kmalloc(plen
, GFP_KERNEL
);
92 if (plen
<= PAGE_SIZE
)
95 payload
= vmalloc(plen
);
101 if (copy_from_user(payload
, _payload
, plen
) != 0)
105 /* find the target keyring (which must be writable) */
106 keyring_ref
= lookup_user_key(ringid
, 1, 0, KEY_WRITE
);
107 if (IS_ERR(keyring_ref
)) {
108 ret
= PTR_ERR(keyring_ref
);
112 /* create or update the requested key and add it to the target
114 key_ref
= key_create_or_update(keyring_ref
, type
, description
,
115 payload
, plen
, KEY_PERM_UNDEF
,
117 if (!IS_ERR(key_ref
)) {
118 ret
= key_ref_to_ptr(key_ref
)->serial
;
119 key_ref_put(key_ref
);
122 ret
= PTR_ERR(key_ref
);
125 key_ref_put(keyring_ref
);
136 } /* end sys_add_key() */
138 /*****************************************************************************/
140 * search the process keyrings for a matching key
141 * - nested keyrings may also be searched if they have Search permission
142 * - if a key is found, it will be attached to the destination keyring if
143 * there's one specified
144 * - /sbin/request-key will be invoked if _callout_info is non-NULL
145 * - the _callout_info string will be passed to /sbin/request-key
146 * - if the _callout_info string is empty, it will be rendered as "-"
147 * - implements request_key()
149 SYSCALL_DEFINE4(request_key
, const char __user
*, _type
,
150 const char __user
*, _description
,
151 const char __user
*, _callout_info
,
152 key_serial_t
, destringid
)
154 struct key_type
*ktype
;
158 char type
[32], *description
, *callout_info
;
161 /* pull the type into kernel space */
162 ret
= key_get_type_from_user(type
, _type
, sizeof(type
));
166 /* pull the description into kernel space */
167 description
= strndup_user(_description
, PAGE_SIZE
);
168 if (IS_ERR(description
)) {
169 ret
= PTR_ERR(description
);
173 /* pull the callout info into kernel space */
177 callout_info
= strndup_user(_callout_info
, PAGE_SIZE
);
178 if (IS_ERR(callout_info
)) {
179 ret
= PTR_ERR(callout_info
);
182 callout_len
= strlen(callout_info
);
185 /* get the destination keyring if specified */
188 dest_ref
= lookup_user_key(destringid
, 1, 0, KEY_WRITE
);
189 if (IS_ERR(dest_ref
)) {
190 ret
= PTR_ERR(dest_ref
);
195 /* find the key type */
196 ktype
= key_type_lookup(type
);
198 ret
= PTR_ERR(ktype
);
203 key
= request_key_and_link(ktype
, description
, callout_info
,
204 callout_len
, NULL
, key_ref_to_ptr(dest_ref
),
217 key_ref_put(dest_ref
);
225 } /* end sys_request_key() */
227 /*****************************************************************************/
229 * get the ID of the specified process keyring
230 * - the keyring must have search permission to be found
231 * - implements keyctl(KEYCTL_GET_KEYRING_ID)
233 long keyctl_get_keyring_ID(key_serial_t id
, int create
)
238 key_ref
= lookup_user_key(id
, create
, 0, KEY_SEARCH
);
239 if (IS_ERR(key_ref
)) {
240 ret
= PTR_ERR(key_ref
);
244 ret
= key_ref_to_ptr(key_ref
)->serial
;
245 key_ref_put(key_ref
);
249 } /* end keyctl_get_keyring_ID() */
251 /*****************************************************************************/
253 * join the session keyring
254 * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING)
256 long keyctl_join_session_keyring(const char __user
*_name
)
261 /* fetch the name from userspace */
264 name
= strndup_user(_name
, PAGE_SIZE
);
271 /* join the session */
272 ret
= join_session_keyring(name
);
277 } /* end keyctl_join_session_keyring() */
279 /*****************************************************************************/
281 * update a key's data payload
282 * - the key must be writable
283 * - implements keyctl(KEYCTL_UPDATE)
285 long keyctl_update_key(key_serial_t id
,
286 const void __user
*_payload
,
294 if (plen
> PAGE_SIZE
)
297 /* pull the payload in if one was supplied */
301 payload
= kmalloc(plen
, GFP_KERNEL
);
306 if (copy_from_user(payload
, _payload
, plen
) != 0)
310 /* find the target key (which must be writable) */
311 key_ref
= lookup_user_key(id
, 0, 0, KEY_WRITE
);
312 if (IS_ERR(key_ref
)) {
313 ret
= PTR_ERR(key_ref
);
318 ret
= key_update(key_ref
, payload
, plen
);
320 key_ref_put(key_ref
);
326 } /* end keyctl_update_key() */
328 /*****************************************************************************/
331 * - the key must be writable
332 * - implements keyctl(KEYCTL_REVOKE)
334 long keyctl_revoke_key(key_serial_t id
)
339 key_ref
= lookup_user_key(id
, 0, 0, KEY_WRITE
);
340 if (IS_ERR(key_ref
)) {
341 ret
= PTR_ERR(key_ref
);
345 key_revoke(key_ref_to_ptr(key_ref
));
348 key_ref_put(key_ref
);
352 } /* end keyctl_revoke_key() */
354 /*****************************************************************************/
356 * clear the specified process keyring
357 * - the keyring must be writable
358 * - implements keyctl(KEYCTL_CLEAR)
360 long keyctl_keyring_clear(key_serial_t ringid
)
362 key_ref_t keyring_ref
;
365 keyring_ref
= lookup_user_key(ringid
, 1, 0, KEY_WRITE
);
366 if (IS_ERR(keyring_ref
)) {
367 ret
= PTR_ERR(keyring_ref
);
371 ret
= keyring_clear(key_ref_to_ptr(keyring_ref
));
373 key_ref_put(keyring_ref
);
377 } /* end keyctl_keyring_clear() */
379 /*****************************************************************************/
381 * link a key into a keyring
382 * - the keyring must be writable
383 * - the key must be linkable
384 * - implements keyctl(KEYCTL_LINK)
386 long keyctl_keyring_link(key_serial_t id
, key_serial_t ringid
)
388 key_ref_t keyring_ref
, key_ref
;
391 keyring_ref
= lookup_user_key(ringid
, 1, 0, KEY_WRITE
);
392 if (IS_ERR(keyring_ref
)) {
393 ret
= PTR_ERR(keyring_ref
);
397 key_ref
= lookup_user_key(id
, 1, 0, KEY_LINK
);
398 if (IS_ERR(key_ref
)) {
399 ret
= PTR_ERR(key_ref
);
403 ret
= key_link(key_ref_to_ptr(keyring_ref
), key_ref_to_ptr(key_ref
));
405 key_ref_put(key_ref
);
407 key_ref_put(keyring_ref
);
411 } /* end keyctl_keyring_link() */
413 /*****************************************************************************/
415 * unlink the first attachment of a key from a keyring
416 * - the keyring must be writable
417 * - we don't need any permissions on the key
418 * - implements keyctl(KEYCTL_UNLINK)
420 long keyctl_keyring_unlink(key_serial_t id
, key_serial_t ringid
)
422 key_ref_t keyring_ref
, key_ref
;
425 keyring_ref
= lookup_user_key(ringid
, 0, 0, KEY_WRITE
);
426 if (IS_ERR(keyring_ref
)) {
427 ret
= PTR_ERR(keyring_ref
);
431 key_ref
= lookup_user_key(id
, 0, 0, 0);
432 if (IS_ERR(key_ref
)) {
433 ret
= PTR_ERR(key_ref
);
437 ret
= key_unlink(key_ref_to_ptr(keyring_ref
), key_ref_to_ptr(key_ref
));
439 key_ref_put(key_ref
);
441 key_ref_put(keyring_ref
);
445 } /* end keyctl_keyring_unlink() */
447 /*****************************************************************************/
449 * describe a user key
450 * - the key must have view permission
451 * - if there's a buffer, we place up to buflen bytes of data into it
452 * - unless there's an error, we return the amount of description available,
453 * irrespective of how much we may have copied
454 * - the description is formatted thus:
455 * type;uid;gid;perm;description<NUL>
456 * - implements keyctl(KEYCTL_DESCRIBE)
458 long keyctl_describe_key(key_serial_t keyid
,
462 struct key
*key
, *instkey
;
467 key_ref
= lookup_user_key(keyid
, 0, 1, KEY_VIEW
);
468 if (IS_ERR(key_ref
)) {
469 /* viewing a key under construction is permitted if we have the
470 * authorisation token handy */
471 if (PTR_ERR(key_ref
) == -EACCES
) {
472 instkey
= key_get_instantiation_authkey(keyid
);
473 if (!IS_ERR(instkey
)) {
475 key_ref
= lookup_user_key(keyid
,
477 if (!IS_ERR(key_ref
))
482 ret
= PTR_ERR(key_ref
);
487 /* calculate how much description we're going to return */
489 tmpbuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
493 key
= key_ref_to_ptr(key_ref
);
495 ret
= snprintf(tmpbuf
, PAGE_SIZE
- 1,
497 key_ref_to_ptr(key_ref
)->type
->name
,
498 key_ref_to_ptr(key_ref
)->uid
,
499 key_ref_to_ptr(key_ref
)->gid
,
500 key_ref_to_ptr(key_ref
)->perm
,
501 key_ref_to_ptr(key_ref
)->description
?
502 key_ref_to_ptr(key_ref
)->description
: ""
505 /* include a NUL char at the end of the data */
506 if (ret
> PAGE_SIZE
- 1)
511 /* consider returning the data */
512 if (buffer
&& buflen
> 0) {
516 if (copy_to_user(buffer
, tmpbuf
, buflen
) != 0)
522 key_ref_put(key_ref
);
526 } /* end keyctl_describe_key() */
528 /*****************************************************************************/
530 * search the specified keyring for a matching key
531 * - the start keyring must be searchable
532 * - nested keyrings may also be searched if they are searchable
533 * - only keys with search permission may be found
534 * - if a key is found, it will be attached to the destination keyring if
535 * there's one specified
536 * - implements keyctl(KEYCTL_SEARCH)
538 long keyctl_keyring_search(key_serial_t ringid
,
539 const char __user
*_type
,
540 const char __user
*_description
,
541 key_serial_t destringid
)
543 struct key_type
*ktype
;
544 key_ref_t keyring_ref
, key_ref
, dest_ref
;
545 char type
[32], *description
;
548 /* pull the type and description into kernel space */
549 ret
= key_get_type_from_user(type
, _type
, sizeof(type
));
553 description
= strndup_user(_description
, PAGE_SIZE
);
554 if (IS_ERR(description
)) {
555 ret
= PTR_ERR(description
);
559 /* get the keyring at which to begin the search */
560 keyring_ref
= lookup_user_key(ringid
, 0, 0, KEY_SEARCH
);
561 if (IS_ERR(keyring_ref
)) {
562 ret
= PTR_ERR(keyring_ref
);
566 /* get the destination keyring if specified */
569 dest_ref
= lookup_user_key(destringid
, 1, 0, KEY_WRITE
);
570 if (IS_ERR(dest_ref
)) {
571 ret
= PTR_ERR(dest_ref
);
576 /* find the key type */
577 ktype
= key_type_lookup(type
);
579 ret
= PTR_ERR(ktype
);
584 key_ref
= keyring_search(keyring_ref
, ktype
, description
);
585 if (IS_ERR(key_ref
)) {
586 ret
= PTR_ERR(key_ref
);
588 /* treat lack or presence of a negative key the same */
594 /* link the resulting key to the destination keyring if we can */
596 ret
= key_permission(key_ref
, KEY_LINK
);
600 ret
= key_link(key_ref_to_ptr(dest_ref
), key_ref_to_ptr(key_ref
));
605 ret
= key_ref_to_ptr(key_ref
)->serial
;
608 key_ref_put(key_ref
);
612 key_ref_put(dest_ref
);
614 key_ref_put(keyring_ref
);
620 } /* end keyctl_keyring_search() */
622 /*****************************************************************************/
624 * read a user key's payload
625 * - the keyring must be readable or the key must be searchable from the
627 * - if there's a buffer, we place up to buflen bytes of data into it
628 * - unless there's an error, we return the amount of data in the key,
629 * irrespective of how much we may have copied
630 * - implements keyctl(KEYCTL_READ)
632 long keyctl_read_key(key_serial_t keyid
, char __user
*buffer
, size_t buflen
)
638 /* find the key first */
639 key_ref
= lookup_user_key(keyid
, 0, 0, 0);
640 if (IS_ERR(key_ref
)) {
645 key
= key_ref_to_ptr(key_ref
);
647 /* see if we can read it directly */
648 ret
= key_permission(key_ref
, KEY_READ
);
654 /* we can't; see if it's searchable from this process's keyrings
655 * - we automatically take account of the fact that it may be
656 * dangling off an instantiation key
658 if (!is_key_possessed(key_ref
)) {
663 /* the key is probably readable - now try to read it */
665 ret
= key_validate(key
);
668 if (key
->type
->read
) {
669 /* read the data with the semaphore held (since we
671 down_read(&key
->sem
);
672 ret
= key
->type
->read(key
, buffer
, buflen
);
682 } /* end keyctl_read_key() */
684 /*****************************************************************************/
686 * change the ownership of a key
687 * - the keyring owned by the changer
688 * - if the uid or gid is -1, then that parameter is not changed
689 * - implements keyctl(KEYCTL_CHOWN)
691 long keyctl_chown_key(key_serial_t id
, uid_t uid
, gid_t gid
)
693 struct key_user
*newowner
, *zapowner
= NULL
;
699 if (uid
== (uid_t
) -1 && gid
== (gid_t
) -1)
702 key_ref
= lookup_user_key(id
, 1, 1, KEY_SETATTR
);
703 if (IS_ERR(key_ref
)) {
704 ret
= PTR_ERR(key_ref
);
708 key
= key_ref_to_ptr(key_ref
);
710 /* make the changes with the locks held to prevent chown/chown races */
712 down_write(&key
->sem
);
714 if (!capable(CAP_SYS_ADMIN
)) {
715 /* only the sysadmin can chown a key to some other UID */
716 if (uid
!= (uid_t
) -1 && key
->uid
!= uid
)
719 /* only the sysadmin can set the key's GID to a group other
720 * than one of those that the current process subscribes to */
721 if (gid
!= (gid_t
) -1 && gid
!= key
->gid
&& !in_group_p(gid
))
726 if (uid
!= (uid_t
) -1 && uid
!= key
->uid
) {
728 newowner
= key_user_lookup(uid
);
732 /* transfer the quota burden to the new user */
733 if (test_bit(KEY_FLAG_IN_QUOTA
, &key
->flags
)) {
734 unsigned maxkeys
= (uid
== 0) ?
735 key_quota_root_maxkeys
: key_quota_maxkeys
;
736 unsigned maxbytes
= (uid
== 0) ?
737 key_quota_root_maxbytes
: key_quota_maxbytes
;
739 spin_lock(&newowner
->lock
);
740 if (newowner
->qnkeys
+ 1 >= maxkeys
||
741 newowner
->qnbytes
+ key
->quotalen
>= maxbytes
||
742 newowner
->qnbytes
+ key
->quotalen
<
747 newowner
->qnbytes
+= key
->quotalen
;
748 spin_unlock(&newowner
->lock
);
750 spin_lock(&key
->user
->lock
);
752 key
->user
->qnbytes
-= key
->quotalen
;
753 spin_unlock(&key
->user
->lock
);
756 atomic_dec(&key
->user
->nkeys
);
757 atomic_inc(&newowner
->nkeys
);
759 if (test_bit(KEY_FLAG_INSTANTIATED
, &key
->flags
)) {
760 atomic_dec(&key
->user
->nikeys
);
761 atomic_inc(&newowner
->nikeys
);
764 zapowner
= key
->user
;
765 key
->user
= newowner
;
770 if (gid
!= (gid_t
) -1)
779 key_user_put(zapowner
);
784 spin_unlock(&newowner
->lock
);
789 } /* end keyctl_chown_key() */
791 /*****************************************************************************/
793 * change the permission mask on a key
794 * - the keyring owned by the changer
795 * - implements keyctl(KEYCTL_SETPERM)
797 long keyctl_setperm_key(key_serial_t id
, key_perm_t perm
)
804 if (perm
& ~(KEY_POS_ALL
| KEY_USR_ALL
| KEY_GRP_ALL
| KEY_OTH_ALL
))
807 key_ref
= lookup_user_key(id
, 1, 1, KEY_SETATTR
);
808 if (IS_ERR(key_ref
)) {
809 ret
= PTR_ERR(key_ref
);
813 key
= key_ref_to_ptr(key_ref
);
815 /* make the changes with the locks held to prevent chown/chmod races */
817 down_write(&key
->sem
);
819 /* if we're not the sysadmin, we can only change a key that we own */
820 if (capable(CAP_SYS_ADMIN
) || key
->uid
== current_fsuid()) {
830 } /* end keyctl_setperm_key() */
833 * get the destination keyring for instantiation
835 static long get_instantiation_keyring(key_serial_t ringid
,
836 struct request_key_auth
*rka
,
837 struct key
**_dest_keyring
)
841 *_dest_keyring
= NULL
;
843 /* just return a NULL pointer if we weren't asked to make a link */
847 /* if a specific keyring is nominated by ID, then use that */
849 dkref
= lookup_user_key(ringid
, 1, 0, KEY_WRITE
);
851 return PTR_ERR(dkref
);
852 *_dest_keyring
= key_ref_to_ptr(dkref
);
856 if (ringid
== KEY_SPEC_REQKEY_AUTH_KEY
)
859 /* otherwise specify the destination keyring recorded in the
860 * authorisation key (any KEY_SPEC_*_KEYRING) */
861 if (ringid
>= KEY_SPEC_REQUESTOR_KEYRING
) {
862 *_dest_keyring
= rka
->dest_keyring
;
870 * change the request_key authorisation key on the current process
872 static int keyctl_change_reqkey_auth(struct key
*key
)
876 new = prepare_creds();
880 key_put(new->request_key_auth
);
881 new->request_key_auth
= key_get(key
);
883 return commit_creds(new);
886 /*****************************************************************************/
888 * instantiate the key with the specified payload, and, if one is given, link
889 * the key into the keyring
891 long keyctl_instantiate_key(key_serial_t id
,
892 const void __user
*_payload
,
896 const struct cred
*cred
= current_cred();
897 struct request_key_auth
*rka
;
898 struct key
*instkey
, *dest_keyring
;
903 kenter("%d,,%zu,%d", id
, plen
, ringid
);
906 if (plen
> 1024 * 1024 - 1)
909 /* the appropriate instantiation authorisation key must have been
910 * assumed before calling this */
912 instkey
= cred
->request_key_auth
;
916 rka
= instkey
->payload
.data
;
917 if (rka
->target_key
->serial
!= id
)
920 /* pull the payload in if one was supplied */
925 payload
= kmalloc(plen
, GFP_KERNEL
);
927 if (plen
<= PAGE_SIZE
)
930 payload
= vmalloc(plen
);
936 if (copy_from_user(payload
, _payload
, plen
) != 0)
940 /* find the destination keyring amongst those belonging to the
942 ret
= get_instantiation_keyring(ringid
, rka
, &dest_keyring
);
946 /* instantiate the key and link it into a keyring */
947 ret
= key_instantiate_and_link(rka
->target_key
, payload
, plen
,
948 dest_keyring
, instkey
);
950 key_put(dest_keyring
);
952 /* discard the assumed authority if it's just been disabled by
953 * instantiation of the key */
955 keyctl_change_reqkey_auth(NULL
);
965 } /* end keyctl_instantiate_key() */
967 /*****************************************************************************/
969 * negatively instantiate the key with the given timeout (in seconds), and, if
970 * one is given, link the key into the keyring
972 long keyctl_negate_key(key_serial_t id
, unsigned timeout
, key_serial_t ringid
)
974 const struct cred
*cred
= current_cred();
975 struct request_key_auth
*rka
;
976 struct key
*instkey
, *dest_keyring
;
979 kenter("%d,%u,%d", id
, timeout
, ringid
);
981 /* the appropriate instantiation authorisation key must have been
982 * assumed before calling this */
984 instkey
= cred
->request_key_auth
;
988 rka
= instkey
->payload
.data
;
989 if (rka
->target_key
->serial
!= id
)
992 /* find the destination keyring if present (which must also be
994 ret
= get_instantiation_keyring(ringid
, rka
, &dest_keyring
);
998 /* instantiate the key and link it into a keyring */
999 ret
= key_negate_and_link(rka
->target_key
, timeout
,
1000 dest_keyring
, instkey
);
1002 key_put(dest_keyring
);
1004 /* discard the assumed authority if it's just been disabled by
1005 * instantiation of the key */
1007 keyctl_change_reqkey_auth(NULL
);
1012 } /* end keyctl_negate_key() */
1014 /*****************************************************************************/
1016 * set the default keyring in which request_key() will cache keys
1017 * - return the old setting
1019 long keyctl_set_reqkey_keyring(int reqkey_defl
)
1022 int ret
, old_setting
;
1024 old_setting
= current_cred_xxx(jit_keyring
);
1026 if (reqkey_defl
== KEY_REQKEY_DEFL_NO_CHANGE
)
1029 new = prepare_creds();
1033 switch (reqkey_defl
) {
1034 case KEY_REQKEY_DEFL_THREAD_KEYRING
:
1035 ret
= install_thread_keyring_to_cred(new);
1040 case KEY_REQKEY_DEFL_PROCESS_KEYRING
:
1041 ret
= install_process_keyring_to_cred(new);
1049 case KEY_REQKEY_DEFL_DEFAULT
:
1050 case KEY_REQKEY_DEFL_SESSION_KEYRING
:
1051 case KEY_REQKEY_DEFL_USER_KEYRING
:
1052 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING
:
1053 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING
:
1056 case KEY_REQKEY_DEFL_NO_CHANGE
:
1057 case KEY_REQKEY_DEFL_GROUP_KEYRING
:
1064 new->jit_keyring
= reqkey_defl
;
1071 } /* end keyctl_set_reqkey_keyring() */
1073 /*****************************************************************************/
1075 * set or clear the timeout for a key
1077 long keyctl_set_timeout(key_serial_t id
, unsigned timeout
)
1079 struct timespec now
;
1085 key_ref
= lookup_user_key(id
, 1, 1, KEY_SETATTR
);
1086 if (IS_ERR(key_ref
)) {
1087 ret
= PTR_ERR(key_ref
);
1091 key
= key_ref_to_ptr(key_ref
);
1093 /* make the changes with the locks held to prevent races */
1094 down_write(&key
->sem
);
1098 now
= current_kernel_time();
1099 expiry
= now
.tv_sec
+ timeout
;
1102 key
->expiry
= expiry
;
1104 up_write(&key
->sem
);
1111 } /* end keyctl_set_timeout() */
1113 /*****************************************************************************/
1115 * assume the authority to instantiate the specified key
1117 long keyctl_assume_authority(key_serial_t id
)
1119 struct key
*authkey
;
1122 /* special key IDs aren't permitted */
1127 /* we divest ourselves of authority if given an ID of 0 */
1129 ret
= keyctl_change_reqkey_auth(NULL
);
1133 /* attempt to assume the authority temporarily granted to us whilst we
1134 * instantiate the specified key
1135 * - the authorisation key must be in the current task's keyrings
1138 authkey
= key_get_instantiation_authkey(id
);
1139 if (IS_ERR(authkey
)) {
1140 ret
= PTR_ERR(authkey
);
1144 ret
= keyctl_change_reqkey_auth(authkey
);
1149 ret
= authkey
->serial
;
1153 } /* end keyctl_assume_authority() */
1156 * get the security label of a key
1157 * - the key must grant us view permission
1158 * - if there's a buffer, we place up to buflen bytes of data into it
1159 * - unless there's an error, we return the amount of information available,
1160 * irrespective of how much we may have copied (including the terminal NUL)
1161 * - implements keyctl(KEYCTL_GET_SECURITY)
1163 long keyctl_get_security(key_serial_t keyid
,
1164 char __user
*buffer
,
1167 struct key
*key
, *instkey
;
1172 key_ref
= lookup_user_key(keyid
, 0, 1, KEY_VIEW
);
1173 if (IS_ERR(key_ref
)) {
1174 if (PTR_ERR(key_ref
) != -EACCES
)
1175 return PTR_ERR(key_ref
);
1177 /* viewing a key under construction is also permitted if we
1178 * have the authorisation token handy */
1179 instkey
= key_get_instantiation_authkey(keyid
);
1180 if (IS_ERR(instkey
))
1181 return PTR_ERR(key_ref
);
1184 key_ref
= lookup_user_key(keyid
, 0, 1, 0);
1185 if (IS_ERR(key_ref
))
1186 return PTR_ERR(key_ref
);
1189 key
= key_ref_to_ptr(key_ref
);
1190 ret
= security_key_getsecurity(key
, &context
);
1192 /* if no information was returned, give userspace an empty
1195 if (buffer
&& buflen
> 0 &&
1196 copy_to_user(buffer
, "", 1) != 0)
1198 } else if (ret
> 0) {
1199 /* return as much data as there's room for */
1200 if (buffer
&& buflen
> 0) {
1204 if (copy_to_user(buffer
, context
, buflen
) != 0)
1211 key_ref_put(key_ref
);
1215 /*****************************************************************************/
1217 * the key control system call
1219 SYSCALL_DEFINE5(keyctl
, int, option
, unsigned long, arg2
, unsigned long, arg3
,
1220 unsigned long, arg4
, unsigned long, arg5
)
1223 case KEYCTL_GET_KEYRING_ID
:
1224 return keyctl_get_keyring_ID((key_serial_t
) arg2
,
1227 case KEYCTL_JOIN_SESSION_KEYRING
:
1228 return keyctl_join_session_keyring((const char __user
*) arg2
);
1231 return keyctl_update_key((key_serial_t
) arg2
,
1232 (const void __user
*) arg3
,
1236 return keyctl_revoke_key((key_serial_t
) arg2
);
1238 case KEYCTL_DESCRIBE
:
1239 return keyctl_describe_key((key_serial_t
) arg2
,
1240 (char __user
*) arg3
,
1244 return keyctl_keyring_clear((key_serial_t
) arg2
);
1247 return keyctl_keyring_link((key_serial_t
) arg2
,
1248 (key_serial_t
) arg3
);
1251 return keyctl_keyring_unlink((key_serial_t
) arg2
,
1252 (key_serial_t
) arg3
);
1255 return keyctl_keyring_search((key_serial_t
) arg2
,
1256 (const char __user
*) arg3
,
1257 (const char __user
*) arg4
,
1258 (key_serial_t
) arg5
);
1261 return keyctl_read_key((key_serial_t
) arg2
,
1262 (char __user
*) arg3
,
1266 return keyctl_chown_key((key_serial_t
) arg2
,
1270 case KEYCTL_SETPERM
:
1271 return keyctl_setperm_key((key_serial_t
) arg2
,
1274 case KEYCTL_INSTANTIATE
:
1275 return keyctl_instantiate_key((key_serial_t
) arg2
,
1276 (const void __user
*) arg3
,
1278 (key_serial_t
) arg5
);
1281 return keyctl_negate_key((key_serial_t
) arg2
,
1283 (key_serial_t
) arg4
);
1285 case KEYCTL_SET_REQKEY_KEYRING
:
1286 return keyctl_set_reqkey_keyring(arg2
);
1288 case KEYCTL_SET_TIMEOUT
:
1289 return keyctl_set_timeout((key_serial_t
) arg2
,
1292 case KEYCTL_ASSUME_AUTHORITY
:
1293 return keyctl_assume_authority((key_serial_t
) arg2
);
1295 case KEYCTL_GET_SECURITY
:
1296 return keyctl_get_security((key_serial_t
) arg2
,
1297 (char __user
*) arg3
,
1304 } /* end sys_keyctl() */