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 <asm/uaccess.h>
25 static int key_get_type_from_user(char *type
,
26 const char __user
*_type
,
31 ret
= strncpy_from_user(type
, _type
, len
);
36 if (ret
== 0 || ret
>= len
)
47 /*****************************************************************************/
49 * extract the description of a new key from userspace and either add it as a
50 * new key to the specified keyring or update a matching key in that keyring
51 * - the keyring must be writable
52 * - returns the new key's serial number
53 * - implements add_key()
55 asmlinkage
long sys_add_key(const char __user
*_type
,
56 const char __user
*_description
,
57 const void __user
*_payload
,
61 key_ref_t keyring_ref
, key_ref
;
62 char type
[32], *description
;
70 /* draw all the data into kernel space */
71 ret
= key_get_type_from_user(type
, _type
, sizeof(type
));
75 description
= strndup_user(_description
, PAGE_SIZE
);
76 if (IS_ERR(description
)) {
77 ret
= PTR_ERR(description
);
81 /* pull the payload in if one was supplied */
86 payload
= kmalloc(plen
, GFP_KERNEL
);
91 if (copy_from_user(payload
, _payload
, plen
) != 0)
95 /* find the target keyring (which must be writable) */
96 keyring_ref
= lookup_user_key(NULL
, ringid
, 1, 0, KEY_WRITE
);
97 if (IS_ERR(keyring_ref
)) {
98 ret
= PTR_ERR(keyring_ref
);
102 /* create or update the requested key and add it to the target
104 key_ref
= key_create_or_update(keyring_ref
, type
, description
,
106 if (!IS_ERR(key_ref
)) {
107 ret
= key_ref_to_ptr(key_ref
)->serial
;
108 key_ref_put(key_ref
);
111 ret
= PTR_ERR(key_ref
);
114 key_ref_put(keyring_ref
);
122 } /* end sys_add_key() */
124 /*****************************************************************************/
126 * search the process keyrings for a matching key
127 * - nested keyrings may also be searched if they have Search permission
128 * - if a key is found, it will be attached to the destination keyring if
129 * there's one specified
130 * - /sbin/request-key will be invoked if _callout_info is non-NULL
131 * - the _callout_info string will be passed to /sbin/request-key
132 * - if the _callout_info string is empty, it will be rendered as "-"
133 * - implements request_key()
135 asmlinkage
long sys_request_key(const char __user
*_type
,
136 const char __user
*_description
,
137 const char __user
*_callout_info
,
138 key_serial_t destringid
)
140 struct key_type
*ktype
;
143 char type
[32], *description
, *callout_info
;
146 /* pull the type into kernel space */
147 ret
= key_get_type_from_user(type
, _type
, sizeof(type
));
151 /* pull the description into kernel space */
152 description
= strndup_user(_description
, PAGE_SIZE
);
153 if (IS_ERR(description
)) {
154 ret
= PTR_ERR(description
);
158 /* pull the callout info into kernel space */
161 callout_info
= strndup_user(_callout_info
, PAGE_SIZE
);
162 if (IS_ERR(callout_info
)) {
163 ret
= PTR_ERR(callout_info
);
168 /* get the destination keyring if specified */
171 dest_ref
= lookup_user_key(NULL
, destringid
, 1, 0, KEY_WRITE
);
172 if (IS_ERR(dest_ref
)) {
173 ret
= PTR_ERR(dest_ref
);
178 /* find the key type */
179 ktype
= key_type_lookup(type
);
181 ret
= PTR_ERR(ktype
);
186 key
= request_key_and_link(ktype
, description
, callout_info
,
187 key_ref_to_ptr(dest_ref
));
199 key_ref_put(dest_ref
);
207 } /* end sys_request_key() */
209 /*****************************************************************************/
211 * get the ID of the specified process keyring
212 * - the keyring must have search permission to be found
213 * - implements keyctl(KEYCTL_GET_KEYRING_ID)
215 long keyctl_get_keyring_ID(key_serial_t id
, int create
)
220 key_ref
= lookup_user_key(NULL
, id
, create
, 0, KEY_SEARCH
);
221 if (IS_ERR(key_ref
)) {
222 ret
= PTR_ERR(key_ref
);
226 ret
= key_ref_to_ptr(key_ref
)->serial
;
227 key_ref_put(key_ref
);
231 } /* end keyctl_get_keyring_ID() */
233 /*****************************************************************************/
235 * join the session keyring
236 * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING)
238 long keyctl_join_session_keyring(const char __user
*_name
)
243 /* fetch the name from userspace */
246 name
= strndup_user(_name
, PAGE_SIZE
);
253 /* join the session */
254 ret
= join_session_keyring(name
);
259 } /* end keyctl_join_session_keyring() */
261 /*****************************************************************************/
263 * update a key's data payload
264 * - the key must be writable
265 * - implements keyctl(KEYCTL_UPDATE)
267 long keyctl_update_key(key_serial_t id
,
268 const void __user
*_payload
,
276 if (plen
> PAGE_SIZE
)
279 /* pull the payload in if one was supplied */
283 payload
= kmalloc(plen
, GFP_KERNEL
);
288 if (copy_from_user(payload
, _payload
, plen
) != 0)
292 /* find the target key (which must be writable) */
293 key_ref
= lookup_user_key(NULL
, id
, 0, 0, KEY_WRITE
);
294 if (IS_ERR(key_ref
)) {
295 ret
= PTR_ERR(key_ref
);
300 ret
= key_update(key_ref
, payload
, plen
);
302 key_ref_put(key_ref
);
308 } /* end keyctl_update_key() */
310 /*****************************************************************************/
313 * - the key must be writable
314 * - implements keyctl(KEYCTL_REVOKE)
316 long keyctl_revoke_key(key_serial_t id
)
321 key_ref
= lookup_user_key(NULL
, id
, 0, 0, KEY_WRITE
);
322 if (IS_ERR(key_ref
)) {
323 ret
= PTR_ERR(key_ref
);
327 key_revoke(key_ref_to_ptr(key_ref
));
330 key_ref_put(key_ref
);
334 } /* end keyctl_revoke_key() */
336 /*****************************************************************************/
338 * clear the specified process keyring
339 * - the keyring must be writable
340 * - implements keyctl(KEYCTL_CLEAR)
342 long keyctl_keyring_clear(key_serial_t ringid
)
344 key_ref_t keyring_ref
;
347 keyring_ref
= lookup_user_key(NULL
, ringid
, 1, 0, KEY_WRITE
);
348 if (IS_ERR(keyring_ref
)) {
349 ret
= PTR_ERR(keyring_ref
);
353 ret
= keyring_clear(key_ref_to_ptr(keyring_ref
));
355 key_ref_put(keyring_ref
);
359 } /* end keyctl_keyring_clear() */
361 /*****************************************************************************/
363 * link a key into a keyring
364 * - the keyring must be writable
365 * - the key must be linkable
366 * - implements keyctl(KEYCTL_LINK)
368 long keyctl_keyring_link(key_serial_t id
, key_serial_t ringid
)
370 key_ref_t keyring_ref
, key_ref
;
373 keyring_ref
= lookup_user_key(NULL
, ringid
, 1, 0, KEY_WRITE
);
374 if (IS_ERR(keyring_ref
)) {
375 ret
= PTR_ERR(keyring_ref
);
379 key_ref
= lookup_user_key(NULL
, id
, 1, 0, KEY_LINK
);
380 if (IS_ERR(key_ref
)) {
381 ret
= PTR_ERR(key_ref
);
385 ret
= key_link(key_ref_to_ptr(keyring_ref
), key_ref_to_ptr(key_ref
));
387 key_ref_put(key_ref
);
389 key_ref_put(keyring_ref
);
393 } /* end keyctl_keyring_link() */
395 /*****************************************************************************/
397 * unlink the first attachment of a key from a keyring
398 * - the keyring must be writable
399 * - we don't need any permissions on the key
400 * - implements keyctl(KEYCTL_UNLINK)
402 long keyctl_keyring_unlink(key_serial_t id
, key_serial_t ringid
)
404 key_ref_t keyring_ref
, key_ref
;
407 keyring_ref
= lookup_user_key(NULL
, ringid
, 0, 0, KEY_WRITE
);
408 if (IS_ERR(keyring_ref
)) {
409 ret
= PTR_ERR(keyring_ref
);
413 key_ref
= lookup_user_key(NULL
, id
, 0, 0, 0);
414 if (IS_ERR(key_ref
)) {
415 ret
= PTR_ERR(key_ref
);
419 ret
= key_unlink(key_ref_to_ptr(keyring_ref
), key_ref_to_ptr(key_ref
));
421 key_ref_put(key_ref
);
423 key_ref_put(keyring_ref
);
427 } /* end keyctl_keyring_unlink() */
429 /*****************************************************************************/
431 * describe a user key
432 * - the key must have view permission
433 * - if there's a buffer, we place up to buflen bytes of data into it
434 * - unless there's an error, we return the amount of description available,
435 * irrespective of how much we may have copied
436 * - the description is formatted thus:
437 * type;uid;gid;perm;description<NUL>
438 * - implements keyctl(KEYCTL_DESCRIBE)
440 long keyctl_describe_key(key_serial_t keyid
,
444 struct key
*key
, *instkey
;
449 key_ref
= lookup_user_key(NULL
, keyid
, 0, 1, KEY_VIEW
);
450 if (IS_ERR(key_ref
)) {
451 /* viewing a key under construction is permitted if we have the
452 * authorisation token handy */
453 if (PTR_ERR(key_ref
) == -EACCES
) {
454 instkey
= key_get_instantiation_authkey(keyid
);
455 if (!IS_ERR(instkey
)) {
457 key_ref
= lookup_user_key(NULL
, keyid
,
459 if (!IS_ERR(key_ref
))
464 ret
= PTR_ERR(key_ref
);
469 /* calculate how much description we're going to return */
471 tmpbuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
475 key
= key_ref_to_ptr(key_ref
);
477 ret
= snprintf(tmpbuf
, PAGE_SIZE
- 1,
479 key_ref_to_ptr(key_ref
)->type
->name
,
480 key_ref_to_ptr(key_ref
)->uid
,
481 key_ref_to_ptr(key_ref
)->gid
,
482 key_ref_to_ptr(key_ref
)->perm
,
483 key_ref_to_ptr(key_ref
)->description
?
484 key_ref_to_ptr(key_ref
)->description
: ""
487 /* include a NUL char at the end of the data */
488 if (ret
> PAGE_SIZE
- 1)
493 /* consider returning the data */
494 if (buffer
&& buflen
> 0) {
498 if (copy_to_user(buffer
, tmpbuf
, buflen
) != 0)
504 key_ref_put(key_ref
);
508 } /* end keyctl_describe_key() */
510 /*****************************************************************************/
512 * search the specified keyring for a matching key
513 * - the start keyring must be searchable
514 * - nested keyrings may also be searched if they are searchable
515 * - only keys with search permission may be found
516 * - if a key is found, it will be attached to the destination keyring if
517 * there's one specified
518 * - implements keyctl(KEYCTL_SEARCH)
520 long keyctl_keyring_search(key_serial_t ringid
,
521 const char __user
*_type
,
522 const char __user
*_description
,
523 key_serial_t destringid
)
525 struct key_type
*ktype
;
526 key_ref_t keyring_ref
, key_ref
, dest_ref
;
527 char type
[32], *description
;
530 /* pull the type and description into kernel space */
531 ret
= key_get_type_from_user(type
, _type
, sizeof(type
));
535 description
= strndup_user(_description
, PAGE_SIZE
);
536 if (IS_ERR(description
)) {
537 ret
= PTR_ERR(description
);
541 /* get the keyring at which to begin the search */
542 keyring_ref
= lookup_user_key(NULL
, ringid
, 0, 0, KEY_SEARCH
);
543 if (IS_ERR(keyring_ref
)) {
544 ret
= PTR_ERR(keyring_ref
);
548 /* get the destination keyring if specified */
551 dest_ref
= lookup_user_key(NULL
, destringid
, 1, 0, KEY_WRITE
);
552 if (IS_ERR(dest_ref
)) {
553 ret
= PTR_ERR(dest_ref
);
558 /* find the key type */
559 ktype
= key_type_lookup(type
);
561 ret
= PTR_ERR(ktype
);
566 key_ref
= keyring_search(keyring_ref
, ktype
, description
);
567 if (IS_ERR(key_ref
)) {
568 ret
= PTR_ERR(key_ref
);
570 /* treat lack or presence of a negative key the same */
576 /* link the resulting key to the destination keyring if we can */
578 ret
= key_permission(key_ref
, KEY_LINK
);
582 ret
= key_link(key_ref_to_ptr(dest_ref
), key_ref_to_ptr(key_ref
));
587 ret
= key_ref_to_ptr(key_ref
)->serial
;
590 key_ref_put(key_ref
);
594 key_ref_put(dest_ref
);
596 key_ref_put(keyring_ref
);
602 } /* end keyctl_keyring_search() */
604 /*****************************************************************************/
606 * read a user key's payload
607 * - the keyring must be readable or the key must be searchable from the
609 * - if there's a buffer, we place up to buflen bytes of data into it
610 * - unless there's an error, we return the amount of data in the key,
611 * irrespective of how much we may have copied
612 * - implements keyctl(KEYCTL_READ)
614 long keyctl_read_key(key_serial_t keyid
, char __user
*buffer
, size_t buflen
)
620 /* find the key first */
621 key_ref
= lookup_user_key(NULL
, keyid
, 0, 0, 0);
622 if (IS_ERR(key_ref
)) {
627 key
= key_ref_to_ptr(key_ref
);
629 /* see if we can read it directly */
630 ret
= key_permission(key_ref
, KEY_READ
);
636 /* we can't; see if it's searchable from this process's keyrings
637 * - we automatically take account of the fact that it may be
638 * dangling off an instantiation key
640 if (!is_key_possessed(key_ref
)) {
645 /* the key is probably readable - now try to read it */
647 ret
= key_validate(key
);
650 if (key
->type
->read
) {
651 /* read the data with the semaphore held (since we
653 down_read(&key
->sem
);
654 ret
= key
->type
->read(key
, buffer
, buflen
);
664 } /* end keyctl_read_key() */
666 /*****************************************************************************/
668 * change the ownership of a key
669 * - the keyring owned by the changer
670 * - if the uid or gid is -1, then that parameter is not changed
671 * - implements keyctl(KEYCTL_CHOWN)
673 long keyctl_chown_key(key_serial_t id
, uid_t uid
, gid_t gid
)
680 if (uid
== (uid_t
) -1 && gid
== (gid_t
) -1)
683 key_ref
= lookup_user_key(NULL
, id
, 1, 1, KEY_SETATTR
);
684 if (IS_ERR(key_ref
)) {
685 ret
= PTR_ERR(key_ref
);
689 key
= key_ref_to_ptr(key_ref
);
691 /* make the changes with the locks held to prevent chown/chown races */
693 down_write(&key
->sem
);
695 if (!capable(CAP_SYS_ADMIN
)) {
696 /* only the sysadmin can chown a key to some other UID */
697 if (uid
!= (uid_t
) -1 && key
->uid
!= uid
)
700 /* only the sysadmin can set the key's GID to a group other
701 * than one of those that the current process subscribes to */
702 if (gid
!= (gid_t
) -1 && gid
!= key
->gid
&& !in_group_p(gid
))
706 /* change the UID (have to update the quotas) */
707 if (uid
!= (uid_t
) -1 && uid
!= key
->uid
) {
708 /* don't support UID changing yet */
714 if (gid
!= (gid_t
) -1)
725 } /* end keyctl_chown_key() */
727 /*****************************************************************************/
729 * change the permission mask on a key
730 * - the keyring owned by the changer
731 * - implements keyctl(KEYCTL_SETPERM)
733 long keyctl_setperm_key(key_serial_t id
, key_perm_t perm
)
740 if (perm
& ~(KEY_POS_ALL
| KEY_USR_ALL
| KEY_GRP_ALL
| KEY_OTH_ALL
))
743 key_ref
= lookup_user_key(NULL
, id
, 1, 1, KEY_SETATTR
);
744 if (IS_ERR(key_ref
)) {
745 ret
= PTR_ERR(key_ref
);
749 key
= key_ref_to_ptr(key_ref
);
751 /* make the changes with the locks held to prevent chown/chmod races */
753 down_write(&key
->sem
);
755 /* if we're not the sysadmin, we can only change a key that we own */
756 if (capable(CAP_SYS_ADMIN
) || key
->uid
== current
->fsuid
) {
766 } /* end keyctl_setperm_key() */
768 /*****************************************************************************/
770 * instantiate the key with the specified payload, and, if one is given, link
771 * the key into the keyring
773 long keyctl_instantiate_key(key_serial_t id
,
774 const void __user
*_payload
,
778 struct request_key_auth
*rka
;
780 key_ref_t keyring_ref
;
788 /* the appropriate instantiation authorisation key must have been
789 * assumed before calling this */
791 instkey
= current
->request_key_auth
;
795 rka
= instkey
->payload
.data
;
796 if (rka
->target_key
->serial
!= id
)
799 /* pull the payload in if one was supplied */
804 payload
= kmalloc(plen
, GFP_KERNEL
);
809 if (copy_from_user(payload
, _payload
, plen
) != 0)
813 /* find the destination keyring amongst those belonging to the
817 keyring_ref
= lookup_user_key(rka
->context
, ringid
, 1, 0,
819 if (IS_ERR(keyring_ref
)) {
820 ret
= PTR_ERR(keyring_ref
);
825 /* instantiate the key and link it into a keyring */
826 ret
= key_instantiate_and_link(rka
->target_key
, payload
, plen
,
827 key_ref_to_ptr(keyring_ref
), instkey
);
829 key_ref_put(keyring_ref
);
831 /* discard the assumed authority if it's just been disabled by
832 * instantiation of the key */
834 key_put(current
->request_key_auth
);
835 current
->request_key_auth
= NULL
;
843 } /* end keyctl_instantiate_key() */
845 /*****************************************************************************/
847 * negatively instantiate the key with the given timeout (in seconds), and, if
848 * one is given, link the key into the keyring
850 long keyctl_negate_key(key_serial_t id
, unsigned timeout
, key_serial_t ringid
)
852 struct request_key_auth
*rka
;
854 key_ref_t keyring_ref
;
857 /* the appropriate instantiation authorisation key must have been
858 * assumed before calling this */
860 instkey
= current
->request_key_auth
;
864 rka
= instkey
->payload
.data
;
865 if (rka
->target_key
->serial
!= id
)
868 /* find the destination keyring if present (which must also be
872 keyring_ref
= lookup_user_key(NULL
, ringid
, 1, 0, KEY_WRITE
);
873 if (IS_ERR(keyring_ref
)) {
874 ret
= PTR_ERR(keyring_ref
);
879 /* instantiate the key and link it into a keyring */
880 ret
= key_negate_and_link(rka
->target_key
, timeout
,
881 key_ref_to_ptr(keyring_ref
), instkey
);
883 key_ref_put(keyring_ref
);
885 /* discard the assumed authority if it's just been disabled by
886 * instantiation of the key */
888 key_put(current
->request_key_auth
);
889 current
->request_key_auth
= NULL
;
895 } /* end keyctl_negate_key() */
897 /*****************************************************************************/
899 * set the default keyring in which request_key() will cache keys
900 * - return the old setting
902 long keyctl_set_reqkey_keyring(int reqkey_defl
)
906 switch (reqkey_defl
) {
907 case KEY_REQKEY_DEFL_THREAD_KEYRING
:
908 ret
= install_thread_keyring(current
);
913 case KEY_REQKEY_DEFL_PROCESS_KEYRING
:
914 ret
= install_process_keyring(current
);
918 case KEY_REQKEY_DEFL_DEFAULT
:
919 case KEY_REQKEY_DEFL_SESSION_KEYRING
:
920 case KEY_REQKEY_DEFL_USER_KEYRING
:
921 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING
:
923 current
->jit_keyring
= reqkey_defl
;
925 case KEY_REQKEY_DEFL_NO_CHANGE
:
926 return current
->jit_keyring
;
928 case KEY_REQKEY_DEFL_GROUP_KEYRING
:
933 } /* end keyctl_set_reqkey_keyring() */
935 /*****************************************************************************/
937 * set or clear the timeout for a key
939 long keyctl_set_timeout(key_serial_t id
, unsigned timeout
)
947 key_ref
= lookup_user_key(NULL
, id
, 1, 1, KEY_SETATTR
);
948 if (IS_ERR(key_ref
)) {
949 ret
= PTR_ERR(key_ref
);
953 key
= key_ref_to_ptr(key_ref
);
955 /* make the changes with the locks held to prevent races */
956 down_write(&key
->sem
);
960 now
= current_kernel_time();
961 expiry
= now
.tv_sec
+ timeout
;
964 key
->expiry
= expiry
;
973 } /* end keyctl_set_timeout() */
975 /*****************************************************************************/
977 * assume the authority to instantiate the specified key
979 long keyctl_assume_authority(key_serial_t id
)
984 /* special key IDs aren't permitted */
989 /* we divest ourselves of authority if given an ID of 0 */
991 key_put(current
->request_key_auth
);
992 current
->request_key_auth
= NULL
;
997 /* attempt to assume the authority temporarily granted to us whilst we
998 * instantiate the specified key
999 * - the authorisation key must be in the current task's keyrings
1002 authkey
= key_get_instantiation_authkey(id
);
1003 if (IS_ERR(authkey
)) {
1004 ret
= PTR_ERR(authkey
);
1008 key_put(current
->request_key_auth
);
1009 current
->request_key_auth
= authkey
;
1010 ret
= authkey
->serial
;
1015 } /* end keyctl_assume_authority() */
1017 /*****************************************************************************/
1019 * the key control system call
1021 asmlinkage
long sys_keyctl(int option
, unsigned long arg2
, unsigned long arg3
,
1022 unsigned long arg4
, unsigned long arg5
)
1025 case KEYCTL_GET_KEYRING_ID
:
1026 return keyctl_get_keyring_ID((key_serial_t
) arg2
,
1029 case KEYCTL_JOIN_SESSION_KEYRING
:
1030 return keyctl_join_session_keyring((const char __user
*) arg2
);
1033 return keyctl_update_key((key_serial_t
) arg2
,
1034 (const void __user
*) arg3
,
1038 return keyctl_revoke_key((key_serial_t
) arg2
);
1040 case KEYCTL_DESCRIBE
:
1041 return keyctl_describe_key((key_serial_t
) arg2
,
1042 (char __user
*) arg3
,
1046 return keyctl_keyring_clear((key_serial_t
) arg2
);
1049 return keyctl_keyring_link((key_serial_t
) arg2
,
1050 (key_serial_t
) arg3
);
1053 return keyctl_keyring_unlink((key_serial_t
) arg2
,
1054 (key_serial_t
) arg3
);
1057 return keyctl_keyring_search((key_serial_t
) arg2
,
1058 (const char __user
*) arg3
,
1059 (const char __user
*) arg4
,
1060 (key_serial_t
) arg5
);
1063 return keyctl_read_key((key_serial_t
) arg2
,
1064 (char __user
*) arg3
,
1068 return keyctl_chown_key((key_serial_t
) arg2
,
1072 case KEYCTL_SETPERM
:
1073 return keyctl_setperm_key((key_serial_t
) arg2
,
1076 case KEYCTL_INSTANTIATE
:
1077 return keyctl_instantiate_key((key_serial_t
) arg2
,
1078 (const void __user
*) arg3
,
1080 (key_serial_t
) arg5
);
1083 return keyctl_negate_key((key_serial_t
) arg2
,
1085 (key_serial_t
) arg4
);
1087 case KEYCTL_SET_REQKEY_KEYRING
:
1088 return keyctl_set_reqkey_keyring(arg2
);
1090 case KEYCTL_SET_TIMEOUT
:
1091 return keyctl_set_timeout((key_serial_t
) arg2
,
1094 case KEYCTL_ASSUME_AUTHORITY
:
1095 return keyctl_assume_authority((key_serial_t
) arg2
);
1101 } /* end sys_keyctl() */