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
);
278 } /* end keyctl_join_session_keyring() */
280 /*****************************************************************************/
282 * update a key's data payload
283 * - the key must be writable
284 * - implements keyctl(KEYCTL_UPDATE)
286 long keyctl_update_key(key_serial_t id
,
287 const void __user
*_payload
,
295 if (plen
> PAGE_SIZE
)
298 /* pull the payload in if one was supplied */
302 payload
= kmalloc(plen
, GFP_KERNEL
);
307 if (copy_from_user(payload
, _payload
, plen
) != 0)
311 /* find the target key (which must be writable) */
312 key_ref
= lookup_user_key(id
, 0, 0, KEY_WRITE
);
313 if (IS_ERR(key_ref
)) {
314 ret
= PTR_ERR(key_ref
);
319 ret
= key_update(key_ref
, payload
, plen
);
321 key_ref_put(key_ref
);
327 } /* end keyctl_update_key() */
329 /*****************************************************************************/
332 * - the key must be writable
333 * - implements keyctl(KEYCTL_REVOKE)
335 long keyctl_revoke_key(key_serial_t id
)
340 key_ref
= lookup_user_key(id
, 0, 0, KEY_WRITE
);
341 if (IS_ERR(key_ref
)) {
342 ret
= PTR_ERR(key_ref
);
346 key_revoke(key_ref_to_ptr(key_ref
));
349 key_ref_put(key_ref
);
353 } /* end keyctl_revoke_key() */
355 /*****************************************************************************/
357 * clear the specified process keyring
358 * - the keyring must be writable
359 * - implements keyctl(KEYCTL_CLEAR)
361 long keyctl_keyring_clear(key_serial_t ringid
)
363 key_ref_t keyring_ref
;
366 keyring_ref
= lookup_user_key(ringid
, 1, 0, KEY_WRITE
);
367 if (IS_ERR(keyring_ref
)) {
368 ret
= PTR_ERR(keyring_ref
);
372 ret
= keyring_clear(key_ref_to_ptr(keyring_ref
));
374 key_ref_put(keyring_ref
);
378 } /* end keyctl_keyring_clear() */
380 /*****************************************************************************/
382 * link a key into a keyring
383 * - the keyring must be writable
384 * - the key must be linkable
385 * - implements keyctl(KEYCTL_LINK)
387 long keyctl_keyring_link(key_serial_t id
, key_serial_t ringid
)
389 key_ref_t keyring_ref
, key_ref
;
392 keyring_ref
= lookup_user_key(ringid
, 1, 0, KEY_WRITE
);
393 if (IS_ERR(keyring_ref
)) {
394 ret
= PTR_ERR(keyring_ref
);
398 key_ref
= lookup_user_key(id
, 1, 0, KEY_LINK
);
399 if (IS_ERR(key_ref
)) {
400 ret
= PTR_ERR(key_ref
);
404 ret
= key_link(key_ref_to_ptr(keyring_ref
), key_ref_to_ptr(key_ref
));
406 key_ref_put(key_ref
);
408 key_ref_put(keyring_ref
);
412 } /* end keyctl_keyring_link() */
414 /*****************************************************************************/
416 * unlink the first attachment of a key from a keyring
417 * - the keyring must be writable
418 * - we don't need any permissions on the key
419 * - implements keyctl(KEYCTL_UNLINK)
421 long keyctl_keyring_unlink(key_serial_t id
, key_serial_t ringid
)
423 key_ref_t keyring_ref
, key_ref
;
426 keyring_ref
= lookup_user_key(ringid
, 0, 0, KEY_WRITE
);
427 if (IS_ERR(keyring_ref
)) {
428 ret
= PTR_ERR(keyring_ref
);
432 key_ref
= lookup_user_key(id
, 0, 0, 0);
433 if (IS_ERR(key_ref
)) {
434 ret
= PTR_ERR(key_ref
);
438 ret
= key_unlink(key_ref_to_ptr(keyring_ref
), key_ref_to_ptr(key_ref
));
440 key_ref_put(key_ref
);
442 key_ref_put(keyring_ref
);
446 } /* end keyctl_keyring_unlink() */
448 /*****************************************************************************/
450 * describe a user key
451 * - the key must have view permission
452 * - if there's a buffer, we place up to buflen bytes of data into it
453 * - unless there's an error, we return the amount of description available,
454 * irrespective of how much we may have copied
455 * - the description is formatted thus:
456 * type;uid;gid;perm;description<NUL>
457 * - implements keyctl(KEYCTL_DESCRIBE)
459 long keyctl_describe_key(key_serial_t keyid
,
463 struct key
*key
, *instkey
;
468 key_ref
= lookup_user_key(keyid
, 0, 1, KEY_VIEW
);
469 if (IS_ERR(key_ref
)) {
470 /* viewing a key under construction is permitted if we have the
471 * authorisation token handy */
472 if (PTR_ERR(key_ref
) == -EACCES
) {
473 instkey
= key_get_instantiation_authkey(keyid
);
474 if (!IS_ERR(instkey
)) {
476 key_ref
= lookup_user_key(keyid
,
478 if (!IS_ERR(key_ref
))
483 ret
= PTR_ERR(key_ref
);
488 /* calculate how much description we're going to return */
490 tmpbuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
494 key
= key_ref_to_ptr(key_ref
);
496 ret
= snprintf(tmpbuf
, PAGE_SIZE
- 1,
498 key_ref_to_ptr(key_ref
)->type
->name
,
499 key_ref_to_ptr(key_ref
)->uid
,
500 key_ref_to_ptr(key_ref
)->gid
,
501 key_ref_to_ptr(key_ref
)->perm
,
502 key_ref_to_ptr(key_ref
)->description
?
503 key_ref_to_ptr(key_ref
)->description
: ""
506 /* include a NUL char at the end of the data */
507 if (ret
> PAGE_SIZE
- 1)
512 /* consider returning the data */
513 if (buffer
&& buflen
> 0) {
517 if (copy_to_user(buffer
, tmpbuf
, buflen
) != 0)
523 key_ref_put(key_ref
);
527 } /* end keyctl_describe_key() */
529 /*****************************************************************************/
531 * search the specified keyring for a matching key
532 * - the start keyring must be searchable
533 * - nested keyrings may also be searched if they are searchable
534 * - only keys with search permission may be found
535 * - if a key is found, it will be attached to the destination keyring if
536 * there's one specified
537 * - implements keyctl(KEYCTL_SEARCH)
539 long keyctl_keyring_search(key_serial_t ringid
,
540 const char __user
*_type
,
541 const char __user
*_description
,
542 key_serial_t destringid
)
544 struct key_type
*ktype
;
545 key_ref_t keyring_ref
, key_ref
, dest_ref
;
546 char type
[32], *description
;
549 /* pull the type and description into kernel space */
550 ret
= key_get_type_from_user(type
, _type
, sizeof(type
));
554 description
= strndup_user(_description
, PAGE_SIZE
);
555 if (IS_ERR(description
)) {
556 ret
= PTR_ERR(description
);
560 /* get the keyring at which to begin the search */
561 keyring_ref
= lookup_user_key(ringid
, 0, 0, KEY_SEARCH
);
562 if (IS_ERR(keyring_ref
)) {
563 ret
= PTR_ERR(keyring_ref
);
567 /* get the destination keyring if specified */
570 dest_ref
= lookup_user_key(destringid
, 1, 0, KEY_WRITE
);
571 if (IS_ERR(dest_ref
)) {
572 ret
= PTR_ERR(dest_ref
);
577 /* find the key type */
578 ktype
= key_type_lookup(type
);
580 ret
= PTR_ERR(ktype
);
585 key_ref
= keyring_search(keyring_ref
, ktype
, description
);
586 if (IS_ERR(key_ref
)) {
587 ret
= PTR_ERR(key_ref
);
589 /* treat lack or presence of a negative key the same */
595 /* link the resulting key to the destination keyring if we can */
597 ret
= key_permission(key_ref
, KEY_LINK
);
601 ret
= key_link(key_ref_to_ptr(dest_ref
), key_ref_to_ptr(key_ref
));
606 ret
= key_ref_to_ptr(key_ref
)->serial
;
609 key_ref_put(key_ref
);
613 key_ref_put(dest_ref
);
615 key_ref_put(keyring_ref
);
621 } /* end keyctl_keyring_search() */
623 /*****************************************************************************/
625 * read a user key's payload
626 * - the keyring must be readable or the key must be searchable from the
628 * - if there's a buffer, we place up to buflen bytes of data into it
629 * - unless there's an error, we return the amount of data in the key,
630 * irrespective of how much we may have copied
631 * - implements keyctl(KEYCTL_READ)
633 long keyctl_read_key(key_serial_t keyid
, char __user
*buffer
, size_t buflen
)
639 /* find the key first */
640 key_ref
= lookup_user_key(keyid
, 0, 0, 0);
641 if (IS_ERR(key_ref
)) {
646 key
= key_ref_to_ptr(key_ref
);
648 /* see if we can read it directly */
649 ret
= key_permission(key_ref
, KEY_READ
);
655 /* we can't; see if it's searchable from this process's keyrings
656 * - we automatically take account of the fact that it may be
657 * dangling off an instantiation key
659 if (!is_key_possessed(key_ref
)) {
664 /* the key is probably readable - now try to read it */
666 ret
= key_validate(key
);
669 if (key
->type
->read
) {
670 /* read the data with the semaphore held (since we
672 down_read(&key
->sem
);
673 ret
= key
->type
->read(key
, buffer
, buflen
);
683 } /* end keyctl_read_key() */
685 /*****************************************************************************/
687 * change the ownership of a key
688 * - the keyring owned by the changer
689 * - if the uid or gid is -1, then that parameter is not changed
690 * - implements keyctl(KEYCTL_CHOWN)
692 long keyctl_chown_key(key_serial_t id
, uid_t uid
, gid_t gid
)
694 struct key_user
*newowner
, *zapowner
= NULL
;
700 if (uid
== (uid_t
) -1 && gid
== (gid_t
) -1)
703 key_ref
= lookup_user_key(id
, 1, 1, KEY_SETATTR
);
704 if (IS_ERR(key_ref
)) {
705 ret
= PTR_ERR(key_ref
);
709 key
= key_ref_to_ptr(key_ref
);
711 /* make the changes with the locks held to prevent chown/chown races */
713 down_write(&key
->sem
);
715 if (!capable(CAP_SYS_ADMIN
)) {
716 /* only the sysadmin can chown a key to some other UID */
717 if (uid
!= (uid_t
) -1 && key
->uid
!= uid
)
720 /* only the sysadmin can set the key's GID to a group other
721 * than one of those that the current process subscribes to */
722 if (gid
!= (gid_t
) -1 && gid
!= key
->gid
&& !in_group_p(gid
))
727 if (uid
!= (uid_t
) -1 && uid
!= key
->uid
) {
729 newowner
= key_user_lookup(uid
);
733 /* transfer the quota burden to the new user */
734 if (test_bit(KEY_FLAG_IN_QUOTA
, &key
->flags
)) {
735 unsigned maxkeys
= (uid
== 0) ?
736 key_quota_root_maxkeys
: key_quota_maxkeys
;
737 unsigned maxbytes
= (uid
== 0) ?
738 key_quota_root_maxbytes
: key_quota_maxbytes
;
740 spin_lock(&newowner
->lock
);
741 if (newowner
->qnkeys
+ 1 >= maxkeys
||
742 newowner
->qnbytes
+ key
->quotalen
>= maxbytes
||
743 newowner
->qnbytes
+ key
->quotalen
<
748 newowner
->qnbytes
+= key
->quotalen
;
749 spin_unlock(&newowner
->lock
);
751 spin_lock(&key
->user
->lock
);
753 key
->user
->qnbytes
-= key
->quotalen
;
754 spin_unlock(&key
->user
->lock
);
757 atomic_dec(&key
->user
->nkeys
);
758 atomic_inc(&newowner
->nkeys
);
760 if (test_bit(KEY_FLAG_INSTANTIATED
, &key
->flags
)) {
761 atomic_dec(&key
->user
->nikeys
);
762 atomic_inc(&newowner
->nikeys
);
765 zapowner
= key
->user
;
766 key
->user
= newowner
;
771 if (gid
!= (gid_t
) -1)
780 key_user_put(zapowner
);
785 spin_unlock(&newowner
->lock
);
790 } /* end keyctl_chown_key() */
792 /*****************************************************************************/
794 * change the permission mask on a key
795 * - the keyring owned by the changer
796 * - implements keyctl(KEYCTL_SETPERM)
798 long keyctl_setperm_key(key_serial_t id
, key_perm_t perm
)
805 if (perm
& ~(KEY_POS_ALL
| KEY_USR_ALL
| KEY_GRP_ALL
| KEY_OTH_ALL
))
808 key_ref
= lookup_user_key(id
, 1, 1, KEY_SETATTR
);
809 if (IS_ERR(key_ref
)) {
810 ret
= PTR_ERR(key_ref
);
814 key
= key_ref_to_ptr(key_ref
);
816 /* make the changes with the locks held to prevent chown/chmod races */
818 down_write(&key
->sem
);
820 /* if we're not the sysadmin, we can only change a key that we own */
821 if (capable(CAP_SYS_ADMIN
) || key
->uid
== current_fsuid()) {
831 } /* end keyctl_setperm_key() */
834 * get the destination keyring for instantiation
836 static long get_instantiation_keyring(key_serial_t ringid
,
837 struct request_key_auth
*rka
,
838 struct key
**_dest_keyring
)
842 *_dest_keyring
= NULL
;
844 /* just return a NULL pointer if we weren't asked to make a link */
848 /* if a specific keyring is nominated by ID, then use that */
850 dkref
= lookup_user_key(ringid
, 1, 0, KEY_WRITE
);
852 return PTR_ERR(dkref
);
853 *_dest_keyring
= key_ref_to_ptr(dkref
);
857 if (ringid
== KEY_SPEC_REQKEY_AUTH_KEY
)
860 /* otherwise specify the destination keyring recorded in the
861 * authorisation key (any KEY_SPEC_*_KEYRING) */
862 if (ringid
>= KEY_SPEC_REQUESTOR_KEYRING
) {
863 *_dest_keyring
= rka
->dest_keyring
;
871 * change the request_key authorisation key on the current process
873 static int keyctl_change_reqkey_auth(struct key
*key
)
877 new = prepare_creds();
881 key_put(new->request_key_auth
);
882 new->request_key_auth
= key_get(key
);
884 return commit_creds(new);
887 /*****************************************************************************/
889 * instantiate the key with the specified payload, and, if one is given, link
890 * the key into the keyring
892 long keyctl_instantiate_key(key_serial_t id
,
893 const void __user
*_payload
,
897 const struct cred
*cred
= current_cred();
898 struct request_key_auth
*rka
;
899 struct key
*instkey
, *dest_keyring
;
904 kenter("%d,,%zu,%d", id
, plen
, ringid
);
907 if (plen
> 1024 * 1024 - 1)
910 /* the appropriate instantiation authorisation key must have been
911 * assumed before calling this */
913 instkey
= cred
->request_key_auth
;
917 rka
= instkey
->payload
.data
;
918 if (rka
->target_key
->serial
!= id
)
921 /* pull the payload in if one was supplied */
926 payload
= kmalloc(plen
, GFP_KERNEL
);
928 if (plen
<= PAGE_SIZE
)
931 payload
= vmalloc(plen
);
937 if (copy_from_user(payload
, _payload
, plen
) != 0)
941 /* find the destination keyring amongst those belonging to the
943 ret
= get_instantiation_keyring(ringid
, rka
, &dest_keyring
);
947 /* instantiate the key and link it into a keyring */
948 ret
= key_instantiate_and_link(rka
->target_key
, payload
, plen
,
949 dest_keyring
, instkey
);
951 key_put(dest_keyring
);
953 /* discard the assumed authority if it's just been disabled by
954 * instantiation of the key */
956 keyctl_change_reqkey_auth(NULL
);
966 } /* end keyctl_instantiate_key() */
968 /*****************************************************************************/
970 * negatively instantiate the key with the given timeout (in seconds), and, if
971 * one is given, link the key into the keyring
973 long keyctl_negate_key(key_serial_t id
, unsigned timeout
, key_serial_t ringid
)
975 const struct cred
*cred
= current_cred();
976 struct request_key_auth
*rka
;
977 struct key
*instkey
, *dest_keyring
;
980 kenter("%d,%u,%d", id
, timeout
, ringid
);
982 /* the appropriate instantiation authorisation key must have been
983 * assumed before calling this */
985 instkey
= cred
->request_key_auth
;
989 rka
= instkey
->payload
.data
;
990 if (rka
->target_key
->serial
!= id
)
993 /* find the destination keyring if present (which must also be
995 ret
= get_instantiation_keyring(ringid
, rka
, &dest_keyring
);
999 /* instantiate the key and link it into a keyring */
1000 ret
= key_negate_and_link(rka
->target_key
, timeout
,
1001 dest_keyring
, instkey
);
1003 key_put(dest_keyring
);
1005 /* discard the assumed authority if it's just been disabled by
1006 * instantiation of the key */
1008 keyctl_change_reqkey_auth(NULL
);
1013 } /* end keyctl_negate_key() */
1015 /*****************************************************************************/
1017 * set the default keyring in which request_key() will cache keys
1018 * - return the old setting
1020 long keyctl_set_reqkey_keyring(int reqkey_defl
)
1023 int ret
, old_setting
;
1025 old_setting
= current_cred_xxx(jit_keyring
);
1027 if (reqkey_defl
== KEY_REQKEY_DEFL_NO_CHANGE
)
1030 new = prepare_creds();
1034 switch (reqkey_defl
) {
1035 case KEY_REQKEY_DEFL_THREAD_KEYRING
:
1036 ret
= install_thread_keyring_to_cred(new);
1041 case KEY_REQKEY_DEFL_PROCESS_KEYRING
:
1042 ret
= install_process_keyring_to_cred(new);
1050 case KEY_REQKEY_DEFL_DEFAULT
:
1051 case KEY_REQKEY_DEFL_SESSION_KEYRING
:
1052 case KEY_REQKEY_DEFL_USER_KEYRING
:
1053 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING
:
1054 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING
:
1057 case KEY_REQKEY_DEFL_NO_CHANGE
:
1058 case KEY_REQKEY_DEFL_GROUP_KEYRING
:
1065 new->jit_keyring
= reqkey_defl
;
1072 } /* end keyctl_set_reqkey_keyring() */
1074 /*****************************************************************************/
1076 * set or clear the timeout for a key
1078 long keyctl_set_timeout(key_serial_t id
, unsigned timeout
)
1080 struct timespec now
;
1086 key_ref
= lookup_user_key(id
, 1, 1, KEY_SETATTR
);
1087 if (IS_ERR(key_ref
)) {
1088 ret
= PTR_ERR(key_ref
);
1092 key
= key_ref_to_ptr(key_ref
);
1094 /* make the changes with the locks held to prevent races */
1095 down_write(&key
->sem
);
1099 now
= current_kernel_time();
1100 expiry
= now
.tv_sec
+ timeout
;
1103 key
->expiry
= expiry
;
1105 up_write(&key
->sem
);
1112 } /* end keyctl_set_timeout() */
1114 /*****************************************************************************/
1116 * assume the authority to instantiate the specified key
1118 long keyctl_assume_authority(key_serial_t id
)
1120 struct key
*authkey
;
1123 /* special key IDs aren't permitted */
1128 /* we divest ourselves of authority if given an ID of 0 */
1130 ret
= keyctl_change_reqkey_auth(NULL
);
1134 /* attempt to assume the authority temporarily granted to us whilst we
1135 * instantiate the specified key
1136 * - the authorisation key must be in the current task's keyrings
1139 authkey
= key_get_instantiation_authkey(id
);
1140 if (IS_ERR(authkey
)) {
1141 ret
= PTR_ERR(authkey
);
1145 ret
= keyctl_change_reqkey_auth(authkey
);
1150 ret
= authkey
->serial
;
1154 } /* end keyctl_assume_authority() */
1157 * get the security label of a key
1158 * - the key must grant us view permission
1159 * - if there's a buffer, we place up to buflen bytes of data into it
1160 * - unless there's an error, we return the amount of information available,
1161 * irrespective of how much we may have copied (including the terminal NUL)
1162 * - implements keyctl(KEYCTL_GET_SECURITY)
1164 long keyctl_get_security(key_serial_t keyid
,
1165 char __user
*buffer
,
1168 struct key
*key
, *instkey
;
1173 key_ref
= lookup_user_key(keyid
, 0, 1, KEY_VIEW
);
1174 if (IS_ERR(key_ref
)) {
1175 if (PTR_ERR(key_ref
) != -EACCES
)
1176 return PTR_ERR(key_ref
);
1178 /* viewing a key under construction is also permitted if we
1179 * have the authorisation token handy */
1180 instkey
= key_get_instantiation_authkey(keyid
);
1181 if (IS_ERR(instkey
))
1182 return PTR_ERR(key_ref
);
1185 key_ref
= lookup_user_key(keyid
, 0, 1, 0);
1186 if (IS_ERR(key_ref
))
1187 return PTR_ERR(key_ref
);
1190 key
= key_ref_to_ptr(key_ref
);
1191 ret
= security_key_getsecurity(key
, &context
);
1193 /* if no information was returned, give userspace an empty
1196 if (buffer
&& buflen
> 0 &&
1197 copy_to_user(buffer
, "", 1) != 0)
1199 } else if (ret
> 0) {
1200 /* return as much data as there's room for */
1201 if (buffer
&& buflen
> 0) {
1205 if (copy_to_user(buffer
, context
, buflen
) != 0)
1212 key_ref_put(key_ref
);
1216 /*****************************************************************************/
1218 * the key control system call
1220 SYSCALL_DEFINE5(keyctl
, int, option
, unsigned long, arg2
, unsigned long, arg3
,
1221 unsigned long, arg4
, unsigned long, arg5
)
1224 case KEYCTL_GET_KEYRING_ID
:
1225 return keyctl_get_keyring_ID((key_serial_t
) arg2
,
1228 case KEYCTL_JOIN_SESSION_KEYRING
:
1229 return keyctl_join_session_keyring((const char __user
*) arg2
);
1232 return keyctl_update_key((key_serial_t
) arg2
,
1233 (const void __user
*) arg3
,
1237 return keyctl_revoke_key((key_serial_t
) arg2
);
1239 case KEYCTL_DESCRIBE
:
1240 return keyctl_describe_key((key_serial_t
) arg2
,
1241 (char __user
*) arg3
,
1245 return keyctl_keyring_clear((key_serial_t
) arg2
);
1248 return keyctl_keyring_link((key_serial_t
) arg2
,
1249 (key_serial_t
) arg3
);
1252 return keyctl_keyring_unlink((key_serial_t
) arg2
,
1253 (key_serial_t
) arg3
);
1256 return keyctl_keyring_search((key_serial_t
) arg2
,
1257 (const char __user
*) arg3
,
1258 (const char __user
*) arg4
,
1259 (key_serial_t
) arg5
);
1262 return keyctl_read_key((key_serial_t
) arg2
,
1263 (char __user
*) arg3
,
1267 return keyctl_chown_key((key_serial_t
) arg2
,
1271 case KEYCTL_SETPERM
:
1272 return keyctl_setperm_key((key_serial_t
) arg2
,
1275 case KEYCTL_INSTANTIATE
:
1276 return keyctl_instantiate_key((key_serial_t
) arg2
,
1277 (const void __user
*) arg3
,
1279 (key_serial_t
) arg5
);
1282 return keyctl_negate_key((key_serial_t
) arg2
,
1284 (key_serial_t
) arg4
);
1286 case KEYCTL_SET_REQKEY_KEYRING
:
1287 return keyctl_set_reqkey_keyring(arg2
);
1289 case KEYCTL_SET_TIMEOUT
:
1290 return keyctl_set_timeout((key_serial_t
) arg2
,
1293 case KEYCTL_ASSUME_AUTHORITY
:
1294 return keyctl_assume_authority((key_serial_t
) arg2
);
1296 case KEYCTL_GET_SECURITY
:
1297 return keyctl_get_security((key_serial_t
) arg2
,
1298 (char __user
*) arg3
,
1305 } /* end sys_keyctl() */