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, KEY_LOOKUP_CREATE, 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, KEY_LOOKUP_CREATE,
190 if (IS_ERR(dest_ref)) {
191 ret = PTR_ERR(dest_ref);
196 /* find the key type */
197 ktype = key_type_lookup(type);
199 ret = PTR_ERR(ktype);
204 key = request_key_and_link(ktype, description, callout_info,
205 callout_len, NULL, key_ref_to_ptr(dest_ref),
218 key_ref_put(dest_ref);
226 } /* end sys_request_key() */
228 /*****************************************************************************/
230 * get the ID of the specified process keyring
231 * - the keyring must have search permission to be found
232 * - implements keyctl(KEYCTL_GET_KEYRING_ID)
234 long keyctl_get_keyring_ID(key_serial_t id, int create)
237 unsigned long lflags;
240 lflags = create ? KEY_LOOKUP_CREATE : 0;
241 key_ref = lookup_user_key(id, lflags, KEY_SEARCH);
242 if (IS_ERR(key_ref)) {
243 ret = PTR_ERR(key_ref);
247 ret = key_ref_to_ptr(key_ref)->serial;
248 key_ref_put(key_ref);
252 } /* end keyctl_get_keyring_ID() */
254 /*****************************************************************************/
256 * join the session keyring
257 * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING)
259 long keyctl_join_session_keyring(const char __user *_name)
264 /* fetch the name from userspace */
267 name = strndup_user(_name, PAGE_SIZE);
274 /* join the session */
275 ret = join_session_keyring(name);
281 } /* end keyctl_join_session_keyring() */
283 /*****************************************************************************/
285 * update a key's data payload
286 * - the key must be writable
287 * - implements keyctl(KEYCTL_UPDATE)
289 long keyctl_update_key(key_serial_t id,
290 const void __user *_payload,
298 if (plen > PAGE_SIZE)
301 /* pull the payload in if one was supplied */
305 payload = kmalloc(plen, GFP_KERNEL);
310 if (copy_from_user(payload, _payload, plen) != 0)
314 /* find the target key (which must be writable) */
315 key_ref = lookup_user_key(id, 0, KEY_WRITE);
316 if (IS_ERR(key_ref)) {
317 ret = PTR_ERR(key_ref);
322 ret = key_update(key_ref, payload, plen);
324 key_ref_put(key_ref);
330 } /* end keyctl_update_key() */
332 /*****************************************************************************/
335 * - the key must be writable
336 * - implements keyctl(KEYCTL_REVOKE)
338 long keyctl_revoke_key(key_serial_t id)
343 key_ref = lookup_user_key(id, 0, KEY_WRITE);
344 if (IS_ERR(key_ref)) {
345 ret = PTR_ERR(key_ref);
348 key_ref = lookup_user_key(id, 0, KEY_SETATTR);
349 if (IS_ERR(key_ref)) {
350 ret = PTR_ERR(key_ref);
355 key_revoke(key_ref_to_ptr(key_ref));
358 key_ref_put(key_ref);
362 } /* end keyctl_revoke_key() */
364 /*****************************************************************************/
366 * clear the specified process keyring
367 * - the keyring must be writable
368 * - implements keyctl(KEYCTL_CLEAR)
370 long keyctl_keyring_clear(key_serial_t ringid)
372 key_ref_t keyring_ref;
375 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
376 if (IS_ERR(keyring_ref)) {
377 ret = PTR_ERR(keyring_ref);
381 ret = keyring_clear(key_ref_to_ptr(keyring_ref));
383 key_ref_put(keyring_ref);
387 } /* end keyctl_keyring_clear() */
389 /*****************************************************************************/
391 * link a key into a keyring
392 * - the keyring must be writable
393 * - the key must be linkable
394 * - implements keyctl(KEYCTL_LINK)
396 long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
398 key_ref_t keyring_ref, key_ref;
401 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
402 if (IS_ERR(keyring_ref)) {
403 ret = PTR_ERR(keyring_ref);
407 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_LINK);
408 if (IS_ERR(key_ref)) {
409 ret = PTR_ERR(key_ref);
413 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
415 key_ref_put(key_ref);
417 key_ref_put(keyring_ref);
421 } /* end keyctl_keyring_link() */
423 /*****************************************************************************/
425 * unlink the first attachment of a key from a keyring
426 * - the keyring must be writable
427 * - we don't need any permissions on the key
428 * - implements keyctl(KEYCTL_UNLINK)
430 long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
432 key_ref_t keyring_ref, key_ref;
435 keyring_ref = lookup_user_key(ringid, 0, KEY_WRITE);
436 if (IS_ERR(keyring_ref)) {
437 ret = PTR_ERR(keyring_ref);
441 key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
442 if (IS_ERR(key_ref)) {
443 ret = PTR_ERR(key_ref);
447 ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
449 key_ref_put(key_ref);
451 key_ref_put(keyring_ref);
455 } /* end keyctl_keyring_unlink() */
457 /*****************************************************************************/
459 * describe a user key
460 * - the key must have view permission
461 * - if there's a buffer, we place up to buflen bytes of data into it
462 * - unless there's an error, we return the amount of description available,
463 * irrespective of how much we may have copied
464 * - the description is formatted thus:
465 * type;uid;gid;perm;description<NUL>
466 * - implements keyctl(KEYCTL_DESCRIBE)
468 long keyctl_describe_key(key_serial_t keyid,
472 struct key *key, *instkey;
477 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
478 if (IS_ERR(key_ref)) {
479 /* viewing a key under construction is permitted if we have the
480 * authorisation token handy */
481 if (PTR_ERR(key_ref) == -EACCES) {
482 instkey = key_get_instantiation_authkey(keyid);
483 if (!IS_ERR(instkey)) {
485 key_ref = lookup_user_key(keyid,
488 if (!IS_ERR(key_ref))
493 ret = PTR_ERR(key_ref);
498 /* calculate how much description we're going to return */
500 tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
504 key = key_ref_to_ptr(key_ref);
506 ret = snprintf(tmpbuf, PAGE_SIZE - 1,
508 key_ref_to_ptr(key_ref)->type->name,
509 key_ref_to_ptr(key_ref)->uid,
510 key_ref_to_ptr(key_ref)->gid,
511 key_ref_to_ptr(key_ref)->perm,
512 key_ref_to_ptr(key_ref)->description ?
513 key_ref_to_ptr(key_ref)->description : ""
516 /* include a NUL char at the end of the data */
517 if (ret > PAGE_SIZE - 1)
522 /* consider returning the data */
523 if (buffer && buflen > 0) {
527 if (copy_to_user(buffer, tmpbuf, buflen) != 0)
533 key_ref_put(key_ref);
537 } /* end keyctl_describe_key() */
539 /*****************************************************************************/
541 * search the specified keyring for a matching key
542 * - the start keyring must be searchable
543 * - nested keyrings may also be searched if they are searchable
544 * - only keys with search permission may be found
545 * - if a key is found, it will be attached to the destination keyring if
546 * there's one specified
547 * - implements keyctl(KEYCTL_SEARCH)
549 long keyctl_keyring_search(key_serial_t ringid,
550 const char __user *_type,
551 const char __user *_description,
552 key_serial_t destringid)
554 struct key_type *ktype;
555 key_ref_t keyring_ref, key_ref, dest_ref;
556 char type[32], *description;
559 /* pull the type and description into kernel space */
560 ret = key_get_type_from_user(type, _type, sizeof(type));
564 description = strndup_user(_description, PAGE_SIZE);
565 if (IS_ERR(description)) {
566 ret = PTR_ERR(description);
570 /* get the keyring at which to begin the search */
571 keyring_ref = lookup_user_key(ringid, 0, KEY_SEARCH);
572 if (IS_ERR(keyring_ref)) {
573 ret = PTR_ERR(keyring_ref);
577 /* get the destination keyring if specified */
580 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
582 if (IS_ERR(dest_ref)) {
583 ret = PTR_ERR(dest_ref);
588 /* find the key type */
589 ktype = key_type_lookup(type);
591 ret = PTR_ERR(ktype);
596 key_ref = keyring_search(keyring_ref, ktype, description);
597 if (IS_ERR(key_ref)) {
598 ret = PTR_ERR(key_ref);
600 /* treat lack or presence of a negative key the same */
606 /* link the resulting key to the destination keyring if we can */
608 ret = key_permission(key_ref, KEY_LINK);
612 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
617 ret = key_ref_to_ptr(key_ref)->serial;
620 key_ref_put(key_ref);
624 key_ref_put(dest_ref);
626 key_ref_put(keyring_ref);
632 } /* end keyctl_keyring_search() */
634 /*****************************************************************************/
636 * read a user key's payload
637 * - the keyring must be readable or the key must be searchable from the
639 * - if there's a buffer, we place up to buflen bytes of data into it
640 * - unless there's an error, we return the amount of data in the key,
641 * irrespective of how much we may have copied
642 * - implements keyctl(KEYCTL_READ)
644 long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
650 /* find the key first */
651 key_ref = lookup_user_key(keyid, 0, 0);
652 if (IS_ERR(key_ref)) {
657 key = key_ref_to_ptr(key_ref);
659 /* see if we can read it directly */
660 ret = key_permission(key_ref, KEY_READ);
666 /* we can't; see if it's searchable from this process's keyrings
667 * - we automatically take account of the fact that it may be
668 * dangling off an instantiation key
670 if (!is_key_possessed(key_ref)) {
675 /* the key is probably readable - now try to read it */
677 ret = key_validate(key);
680 if (key->type->read) {
681 /* read the data with the semaphore held (since we
683 down_read(&key->sem);
684 ret = key->type->read(key, buffer, buflen);
694 } /* end keyctl_read_key() */
696 /*****************************************************************************/
698 * change the ownership of a key
699 * - the keyring owned by the changer
700 * - if the uid or gid is -1, then that parameter is not changed
701 * - implements keyctl(KEYCTL_CHOWN)
703 long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
705 struct key_user *newowner, *zapowner = NULL;
711 if (uid == (uid_t) -1 && gid == (gid_t) -1)
714 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
716 if (IS_ERR(key_ref)) {
717 ret = PTR_ERR(key_ref);
721 key = key_ref_to_ptr(key_ref);
723 /* make the changes with the locks held to prevent chown/chown races */
725 down_write(&key->sem);
727 if (!capable(CAP_SYS_ADMIN)) {
728 /* only the sysadmin can chown a key to some other UID */
729 if (uid != (uid_t) -1 && key->uid != uid)
732 /* only the sysadmin can set the key's GID to a group other
733 * than one of those that the current process subscribes to */
734 if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
739 if (uid != (uid_t) -1 && uid != key->uid) {
741 newowner = key_user_lookup(uid, current_user_ns());
745 /* transfer the quota burden to the new user */
746 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
747 unsigned maxkeys = (uid == 0) ?
748 key_quota_root_maxkeys : key_quota_maxkeys;
749 unsigned maxbytes = (uid == 0) ?
750 key_quota_root_maxbytes : key_quota_maxbytes;
752 spin_lock(&newowner->lock);
753 if (newowner->qnkeys + 1 >= maxkeys ||
754 newowner->qnbytes + key->quotalen >= maxbytes ||
755 newowner->qnbytes + key->quotalen <
760 newowner->qnbytes += key->quotalen;
761 spin_unlock(&newowner->lock);
763 spin_lock(&key->user->lock);
765 key->user->qnbytes -= key->quotalen;
766 spin_unlock(&key->user->lock);
769 atomic_dec(&key->user->nkeys);
770 atomic_inc(&newowner->nkeys);
772 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
773 atomic_dec(&key->user->nikeys);
774 atomic_inc(&newowner->nikeys);
777 zapowner = key->user;
778 key->user = newowner;
783 if (gid != (gid_t) -1)
792 key_user_put(zapowner);
797 spin_unlock(&newowner->lock);
802 } /* end keyctl_chown_key() */
804 /*****************************************************************************/
806 * change the permission mask on a key
807 * - the keyring owned by the changer
808 * - implements keyctl(KEYCTL_SETPERM)
810 long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
817 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
820 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
822 if (IS_ERR(key_ref)) {
823 ret = PTR_ERR(key_ref);
827 key = key_ref_to_ptr(key_ref);
829 /* make the changes with the locks held to prevent chown/chmod races */
831 down_write(&key->sem);
833 /* if we're not the sysadmin, we can only change a key that we own */
834 if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) {
844 } /* end keyctl_setperm_key() */
847 * get the destination keyring for instantiation
849 static long get_instantiation_keyring(key_serial_t ringid,
850 struct request_key_auth *rka,
851 struct key **_dest_keyring)
855 *_dest_keyring = NULL;
857 /* just return a NULL pointer if we weren't asked to make a link */
861 /* if a specific keyring is nominated by ID, then use that */
863 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
865 return PTR_ERR(dkref);
866 *_dest_keyring = key_ref_to_ptr(dkref);
870 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
873 /* otherwise specify the destination keyring recorded in the
874 * authorisation key (any KEY_SPEC_*_KEYRING) */
875 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
876 *_dest_keyring = key_get(rka->dest_keyring);
884 * change the request_key authorisation key on the current process
886 static int keyctl_change_reqkey_auth(struct key *key)
890 new = prepare_creds();
894 key_put(new->request_key_auth);
895 new->request_key_auth = key_get(key);
897 return commit_creds(new);
900 /*****************************************************************************/
902 * instantiate the key with the specified payload, and, if one is given, link
903 * the key into the keyring
905 long keyctl_instantiate_key(key_serial_t id,
906 const void __user *_payload,
910 const struct cred *cred = current_cred();
911 struct request_key_auth *rka;
912 struct key *instkey, *dest_keyring;
917 kenter("%d,,%zu,%d", id, plen, ringid);
920 if (plen > 1024 * 1024 - 1)
923 /* the appropriate instantiation authorisation key must have been
924 * assumed before calling this */
926 instkey = cred->request_key_auth;
930 rka = instkey->payload.data;
931 if (rka->target_key->serial != id)
934 /* pull the payload in if one was supplied */
939 payload = kmalloc(plen, GFP_KERNEL);
941 if (plen <= PAGE_SIZE)
944 payload = vmalloc(plen);
950 if (copy_from_user(payload, _payload, plen) != 0)
954 /* find the destination keyring amongst those belonging to the
956 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
960 /* instantiate the key and link it into a keyring */
961 ret = key_instantiate_and_link(rka->target_key, payload, plen,
962 dest_keyring, instkey);
964 key_put(dest_keyring);
966 /* discard the assumed authority if it's just been disabled by
967 * instantiation of the key */
969 keyctl_change_reqkey_auth(NULL);
979 } /* end keyctl_instantiate_key() */
981 /*****************************************************************************/
983 * negatively instantiate the key with the given timeout (in seconds), and, if
984 * one is given, link the key into the keyring
986 long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
988 const struct cred *cred = current_cred();
989 struct request_key_auth *rka;
990 struct key *instkey, *dest_keyring;
993 kenter("%d,%u,%d", id, timeout, ringid);
995 /* the appropriate instantiation authorisation key must have been
996 * assumed before calling this */
998 instkey = cred->request_key_auth;
1002 rka = instkey->payload.data;
1003 if (rka->target_key->serial != id)
1006 /* find the destination keyring if present (which must also be
1008 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1012 /* instantiate the key and link it into a keyring */
1013 ret = key_negate_and_link(rka->target_key, timeout,
1014 dest_keyring, instkey);
1016 key_put(dest_keyring);
1018 /* discard the assumed authority if it's just been disabled by
1019 * instantiation of the key */
1021 keyctl_change_reqkey_auth(NULL);
1026 } /* end keyctl_negate_key() */
1028 /*****************************************************************************/
1030 * set the default keyring in which request_key() will cache keys
1031 * - return the old setting
1033 long keyctl_set_reqkey_keyring(int reqkey_defl)
1036 int ret, old_setting;
1038 old_setting = current_cred_xxx(jit_keyring);
1040 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1043 new = prepare_creds();
1047 switch (reqkey_defl) {
1048 case KEY_REQKEY_DEFL_THREAD_KEYRING:
1049 ret = install_thread_keyring_to_cred(new);
1054 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
1055 ret = install_process_keyring_to_cred(new);
1063 case KEY_REQKEY_DEFL_DEFAULT:
1064 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1065 case KEY_REQKEY_DEFL_USER_KEYRING:
1066 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1067 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1070 case KEY_REQKEY_DEFL_NO_CHANGE:
1071 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1078 new->jit_keyring = reqkey_defl;
1085 } /* end keyctl_set_reqkey_keyring() */
1087 /*****************************************************************************/
1089 * set or clear the timeout for a key
1091 long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1093 struct timespec now;
1099 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1101 if (IS_ERR(key_ref)) {
1102 ret = PTR_ERR(key_ref);
1106 key = key_ref_to_ptr(key_ref);
1108 /* make the changes with the locks held to prevent races */
1109 down_write(&key->sem);
1113 now = current_kernel_time();
1114 expiry = now.tv_sec + timeout;
1117 key->expiry = expiry;
1118 key_schedule_gc(key->expiry + key_gc_delay);
1120 up_write(&key->sem);
1127 } /* end keyctl_set_timeout() */
1129 /*****************************************************************************/
1131 * assume the authority to instantiate the specified key
1133 long keyctl_assume_authority(key_serial_t id)
1135 struct key *authkey;
1138 /* special key IDs aren't permitted */
1143 /* we divest ourselves of authority if given an ID of 0 */
1145 ret = keyctl_change_reqkey_auth(NULL);
1149 /* attempt to assume the authority temporarily granted to us whilst we
1150 * instantiate the specified key
1151 * - the authorisation key must be in the current task's keyrings
1154 authkey = key_get_instantiation_authkey(id);
1155 if (IS_ERR(authkey)) {
1156 ret = PTR_ERR(authkey);
1160 ret = keyctl_change_reqkey_auth(authkey);
1165 ret = authkey->serial;
1169 } /* end keyctl_assume_authority() */
1172 * get the security label of a key
1173 * - the key must grant us view permission
1174 * - if there's a buffer, we place up to buflen bytes of data into it
1175 * - unless there's an error, we return the amount of information available,
1176 * irrespective of how much we may have copied (including the terminal NUL)
1177 * - implements keyctl(KEYCTL_GET_SECURITY)
1179 long keyctl_get_security(key_serial_t keyid,
1180 char __user *buffer,
1183 struct key *key, *instkey;
1188 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
1189 if (IS_ERR(key_ref)) {
1190 if (PTR_ERR(key_ref) != -EACCES)
1191 return PTR_ERR(key_ref);
1193 /* viewing a key under construction is also permitted if we
1194 * have the authorisation token handy */
1195 instkey = key_get_instantiation_authkey(keyid);
1196 if (IS_ERR(instkey))
1197 return PTR_ERR(instkey);
1200 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
1201 if (IS_ERR(key_ref))
1202 return PTR_ERR(key_ref);
1205 key = key_ref_to_ptr(key_ref);
1206 ret = security_key_getsecurity(key, &context);
1208 /* if no information was returned, give userspace an empty
1211 if (buffer && buflen > 0 &&
1212 copy_to_user(buffer, "", 1) != 0)
1214 } else if (ret > 0) {
1215 /* return as much data as there's room for */
1216 if (buffer && buflen > 0) {
1220 if (copy_to_user(buffer, context, buflen) != 0)
1227 key_ref_put(key_ref);
1232 * attempt to install the calling process's session keyring on the process's
1234 * - the keyring must exist and must grant us LINK permission
1235 * - implements keyctl(KEYCTL_SESSION_TO_PARENT)
1237 long keyctl_session_to_parent(void)
1239 #ifdef TIF_NOTIFY_RESUME
1240 struct task_struct *me, *parent;
1241 const struct cred *mycred, *pcred;
1242 struct cred *cred, *oldcred;
1243 key_ref_t keyring_r;
1246 keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_LINK);
1247 if (IS_ERR(keyring_r))
1248 return PTR_ERR(keyring_r);
1250 /* our parent is going to need a new cred struct, a new tgcred struct
1251 * and new security data, so we allocate them here to prevent ENOMEM in
1254 cred = cred_alloc_blank();
1258 cred->tgcred->session_keyring = key_ref_to_ptr(keyring_r);
1262 write_lock_irq(&tasklist_lock);
1264 parent = me->real_parent;
1267 /* the parent mustn't be init and mustn't be a kernel thread */
1268 if (parent->pid <= 1 || !parent->mm)
1271 /* the parent must be single threaded */
1272 if (atomic_read(&parent->signal->count) != 1)
1275 /* the parent and the child must have different session keyrings or
1276 * there's no point */
1277 mycred = current_cred();
1278 pcred = __task_cred(parent);
1279 if (mycred == pcred ||
1280 mycred->tgcred->session_keyring == pcred->tgcred->session_keyring)
1283 /* the parent must have the same effective ownership and mustn't be
1285 if (pcred-> uid != mycred->euid ||
1286 pcred->euid != mycred->euid ||
1287 pcred->suid != mycred->euid ||
1288 pcred-> gid != mycred->egid ||
1289 pcred->egid != mycred->egid ||
1290 pcred->sgid != mycred->egid)
1293 /* the keyrings must have the same UID */
1294 if (pcred ->tgcred->session_keyring->uid != mycred->euid ||
1295 mycred->tgcred->session_keyring->uid != mycred->euid)
1298 /* the LSM must permit the replacement of the parent's keyring with the
1299 * keyring from this process */
1300 ret = security_key_session_to_parent(mycred, pcred,
1301 key_ref_to_ptr(keyring_r));
1305 /* if there's an already pending keyring replacement, then we replace
1307 oldcred = parent->replacement_session_keyring;
1309 /* the replacement session keyring is applied just prior to userspace
1311 parent->replacement_session_keyring = cred;
1313 set_ti_thread_flag(task_thread_info(parent), TIF_NOTIFY_RESUME);
1315 write_unlock_irq(&tasklist_lock);
1323 write_unlock_irq(&tasklist_lock);
1328 key_ref_put(keyring_r);
1331 #else /* !TIF_NOTIFY_RESUME */
1333 * To be removed when TIF_NOTIFY_RESUME has been implemented on
1336 #warning TIF_NOTIFY_RESUME not implemented
1338 #endif /* !TIF_NOTIFY_RESUME */
1341 /*****************************************************************************/
1343 * the key control system call
1345 SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1346 unsigned long, arg4, unsigned long, arg5)
1349 case KEYCTL_GET_KEYRING_ID:
1350 return keyctl_get_keyring_ID((key_serial_t) arg2,
1353 case KEYCTL_JOIN_SESSION_KEYRING:
1354 return keyctl_join_session_keyring((const char __user *) arg2);
1357 return keyctl_update_key((key_serial_t) arg2,
1358 (const void __user *) arg3,
1362 return keyctl_revoke_key((key_serial_t) arg2);
1364 case KEYCTL_DESCRIBE:
1365 return keyctl_describe_key((key_serial_t) arg2,
1366 (char __user *) arg3,
1370 return keyctl_keyring_clear((key_serial_t) arg2);
1373 return keyctl_keyring_link((key_serial_t) arg2,
1374 (key_serial_t) arg3);
1377 return keyctl_keyring_unlink((key_serial_t) arg2,
1378 (key_serial_t) arg3);
1381 return keyctl_keyring_search((key_serial_t) arg2,
1382 (const char __user *) arg3,
1383 (const char __user *) arg4,
1384 (key_serial_t) arg5);
1387 return keyctl_read_key((key_serial_t) arg2,
1388 (char __user *) arg3,
1392 return keyctl_chown_key((key_serial_t) arg2,
1396 case KEYCTL_SETPERM:
1397 return keyctl_setperm_key((key_serial_t) arg2,
1400 case KEYCTL_INSTANTIATE:
1401 return keyctl_instantiate_key((key_serial_t) arg2,
1402 (const void __user *) arg3,
1404 (key_serial_t) arg5);
1407 return keyctl_negate_key((key_serial_t) arg2,
1409 (key_serial_t) arg4);
1411 case KEYCTL_SET_REQKEY_KEYRING:
1412 return keyctl_set_reqkey_keyring(arg2);
1414 case KEYCTL_SET_TIMEOUT:
1415 return keyctl_set_timeout((key_serial_t) arg2,
1418 case KEYCTL_ASSUME_AUTHORITY:
1419 return keyctl_assume_authority((key_serial_t) arg2);
1421 case KEYCTL_GET_SECURITY:
1422 return keyctl_get_security((key_serial_t) arg2,
1423 (char __user *) arg3,
1426 case KEYCTL_SESSION_TO_PARENT:
1427 return keyctl_session_to_parent();
1433 } /* end sys_keyctl() */