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)
50 * extract the description of a new key from userspace and either add it as a
51 * new key to the specified keyring or update a matching key in that keyring
52 * - the keyring must be writable
53 * - returns the new key's serial number
54 * - implements add_key()
56 SYSCALL_DEFINE5(add_key, const char __user *, _type,
57 const char __user *, _description,
58 const void __user *, _payload,
62 key_ref_t keyring_ref, key_ref;
63 char type[32], *description;
69 if (plen > 1024 * 1024 - 1)
72 /* draw all the data into kernel space */
73 ret = key_get_type_from_user(type, _type, sizeof(type));
77 description = strndup_user(_description, PAGE_SIZE);
78 if (IS_ERR(description)) {
79 ret = PTR_ERR(description);
83 /* pull the payload in if one was supplied */
89 payload = kmalloc(plen, GFP_KERNEL);
91 if (plen <= PAGE_SIZE)
94 payload = vmalloc(plen);
100 if (copy_from_user(payload, _payload, plen) != 0)
104 /* find the target keyring (which must be writable) */
105 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
106 if (IS_ERR(keyring_ref)) {
107 ret = PTR_ERR(keyring_ref);
111 /* create or update the requested key and add it to the target
113 key_ref = key_create_or_update(keyring_ref, type, description,
114 payload, plen, KEY_PERM_UNDEF,
116 if (!IS_ERR(key_ref)) {
117 ret = key_ref_to_ptr(key_ref)->serial;
118 key_ref_put(key_ref);
121 ret = PTR_ERR(key_ref);
124 key_ref_put(keyring_ref);
137 * search the process keyrings for a matching key
138 * - nested keyrings may also be searched if they have Search permission
139 * - if a key is found, it will be attached to the destination keyring if
140 * there's one specified
141 * - /sbin/request-key will be invoked if _callout_info is non-NULL
142 * - the _callout_info string will be passed to /sbin/request-key
143 * - if the _callout_info string is empty, it will be rendered as "-"
144 * - implements request_key()
146 SYSCALL_DEFINE4(request_key, const char __user *, _type,
147 const char __user *, _description,
148 const char __user *, _callout_info,
149 key_serial_t, destringid)
151 struct key_type *ktype;
155 char type[32], *description, *callout_info;
158 /* pull the type into kernel space */
159 ret = key_get_type_from_user(type, _type, sizeof(type));
163 /* pull the description into kernel space */
164 description = strndup_user(_description, PAGE_SIZE);
165 if (IS_ERR(description)) {
166 ret = PTR_ERR(description);
170 /* pull the callout info into kernel space */
174 callout_info = strndup_user(_callout_info, PAGE_SIZE);
175 if (IS_ERR(callout_info)) {
176 ret = PTR_ERR(callout_info);
179 callout_len = strlen(callout_info);
182 /* get the destination keyring if specified */
185 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
187 if (IS_ERR(dest_ref)) {
188 ret = PTR_ERR(dest_ref);
193 /* find the key type */
194 ktype = key_type_lookup(type);
196 ret = PTR_ERR(ktype);
201 key = request_key_and_link(ktype, description, callout_info,
202 callout_len, NULL, key_ref_to_ptr(dest_ref),
215 key_ref_put(dest_ref);
225 * get the ID of the specified process keyring
226 * - the keyring must have search permission to be found
227 * - implements keyctl(KEYCTL_GET_KEYRING_ID)
229 long keyctl_get_keyring_ID(key_serial_t id, int create)
232 unsigned long lflags;
235 lflags = create ? KEY_LOOKUP_CREATE : 0;
236 key_ref = lookup_user_key(id, lflags, KEY_SEARCH);
237 if (IS_ERR(key_ref)) {
238 ret = PTR_ERR(key_ref);
242 ret = key_ref_to_ptr(key_ref)->serial;
243 key_ref_put(key_ref);
247 } /* end keyctl_get_keyring_ID() */
250 * join the session keyring
251 * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING)
253 long keyctl_join_session_keyring(const char __user *_name)
258 /* fetch the name from userspace */
261 name = strndup_user(_name, PAGE_SIZE);
268 /* join the session */
269 ret = join_session_keyring(name);
277 * update a key's data payload
278 * - the key must be writable
279 * - implements keyctl(KEYCTL_UPDATE)
281 long keyctl_update_key(key_serial_t id,
282 const void __user *_payload,
290 if (plen > PAGE_SIZE)
293 /* pull the payload in if one was supplied */
297 payload = kmalloc(plen, GFP_KERNEL);
302 if (copy_from_user(payload, _payload, plen) != 0)
306 /* find the target key (which must be writable) */
307 key_ref = lookup_user_key(id, 0, KEY_WRITE);
308 if (IS_ERR(key_ref)) {
309 ret = PTR_ERR(key_ref);
314 ret = key_update(key_ref, payload, plen);
316 key_ref_put(key_ref);
325 * - the key must be writable
326 * - implements keyctl(KEYCTL_REVOKE)
328 long keyctl_revoke_key(key_serial_t id)
333 key_ref = lookup_user_key(id, 0, KEY_WRITE);
334 if (IS_ERR(key_ref)) {
335 ret = PTR_ERR(key_ref);
338 key_ref = lookup_user_key(id, 0, KEY_SETATTR);
339 if (IS_ERR(key_ref)) {
340 ret = PTR_ERR(key_ref);
345 key_revoke(key_ref_to_ptr(key_ref));
348 key_ref_put(key_ref);
354 * clear the specified process keyring
355 * - the keyring must be writable
356 * - implements keyctl(KEYCTL_CLEAR)
358 long keyctl_keyring_clear(key_serial_t ringid)
360 key_ref_t keyring_ref;
363 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
364 if (IS_ERR(keyring_ref)) {
365 ret = PTR_ERR(keyring_ref);
369 ret = keyring_clear(key_ref_to_ptr(keyring_ref));
371 key_ref_put(keyring_ref);
377 * link a key into a keyring
378 * - the keyring must be writable
379 * - the key must be linkable
380 * - implements keyctl(KEYCTL_LINK)
382 long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
384 key_ref_t keyring_ref, key_ref;
387 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
388 if (IS_ERR(keyring_ref)) {
389 ret = PTR_ERR(keyring_ref);
393 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_LINK);
394 if (IS_ERR(key_ref)) {
395 ret = PTR_ERR(key_ref);
399 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
401 key_ref_put(key_ref);
403 key_ref_put(keyring_ref);
409 * unlink the first attachment of a key from a keyring
410 * - the keyring must be writable
411 * - we don't need any permissions on the key
412 * - implements keyctl(KEYCTL_UNLINK)
414 long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
416 key_ref_t keyring_ref, key_ref;
419 keyring_ref = lookup_user_key(ringid, 0, KEY_WRITE);
420 if (IS_ERR(keyring_ref)) {
421 ret = PTR_ERR(keyring_ref);
425 key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
426 if (IS_ERR(key_ref)) {
427 ret = PTR_ERR(key_ref);
431 ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
433 key_ref_put(key_ref);
435 key_ref_put(keyring_ref);
441 * describe a user key
442 * - the key must have view permission
443 * - if there's a buffer, we place up to buflen bytes of data into it
444 * - unless there's an error, we return the amount of description available,
445 * irrespective of how much we may have copied
446 * - the description is formatted thus:
447 * type;uid;gid;perm;description<NUL>
448 * - implements keyctl(KEYCTL_DESCRIBE)
450 long keyctl_describe_key(key_serial_t keyid,
454 struct key *key, *instkey;
459 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
460 if (IS_ERR(key_ref)) {
461 /* viewing a key under construction is permitted if we have the
462 * authorisation token handy */
463 if (PTR_ERR(key_ref) == -EACCES) {
464 instkey = key_get_instantiation_authkey(keyid);
465 if (!IS_ERR(instkey)) {
467 key_ref = lookup_user_key(keyid,
470 if (!IS_ERR(key_ref))
475 ret = PTR_ERR(key_ref);
480 /* calculate how much description we're going to return */
482 tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
486 key = key_ref_to_ptr(key_ref);
488 ret = snprintf(tmpbuf, PAGE_SIZE - 1,
494 key->description ?: "");
496 /* include a NUL char at the end of the data */
497 if (ret > PAGE_SIZE - 1)
502 /* consider returning the data */
503 if (buffer && buflen > 0) {
507 if (copy_to_user(buffer, tmpbuf, buflen) != 0)
513 key_ref_put(key_ref);
519 * search the specified keyring for a matching key
520 * - the start keyring must be searchable
521 * - nested keyrings may also be searched if they are searchable
522 * - only keys with search permission may be found
523 * - if a key is found, it will be attached to the destination keyring if
524 * there's one specified
525 * - implements keyctl(KEYCTL_SEARCH)
527 long keyctl_keyring_search(key_serial_t ringid,
528 const char __user *_type,
529 const char __user *_description,
530 key_serial_t destringid)
532 struct key_type *ktype;
533 key_ref_t keyring_ref, key_ref, dest_ref;
534 char type[32], *description;
537 /* pull the type and description into kernel space */
538 ret = key_get_type_from_user(type, _type, sizeof(type));
542 description = strndup_user(_description, PAGE_SIZE);
543 if (IS_ERR(description)) {
544 ret = PTR_ERR(description);
548 /* get the keyring at which to begin the search */
549 keyring_ref = lookup_user_key(ringid, 0, KEY_SEARCH);
550 if (IS_ERR(keyring_ref)) {
551 ret = PTR_ERR(keyring_ref);
555 /* get the destination keyring if specified */
558 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
560 if (IS_ERR(dest_ref)) {
561 ret = PTR_ERR(dest_ref);
566 /* find the key type */
567 ktype = key_type_lookup(type);
569 ret = PTR_ERR(ktype);
574 key_ref = keyring_search(keyring_ref, ktype, description);
575 if (IS_ERR(key_ref)) {
576 ret = PTR_ERR(key_ref);
578 /* treat lack or presence of a negative key the same */
584 /* link the resulting key to the destination keyring if we can */
586 ret = key_permission(key_ref, KEY_LINK);
590 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
595 ret = key_ref_to_ptr(key_ref)->serial;
598 key_ref_put(key_ref);
602 key_ref_put(dest_ref);
604 key_ref_put(keyring_ref);
612 * read a user key's payload
613 * - the keyring must be readable or the key must be searchable from the
615 * - if there's a buffer, we place up to buflen bytes of data into it
616 * - unless there's an error, we return the amount of data in the key,
617 * irrespective of how much we may have copied
618 * - implements keyctl(KEYCTL_READ)
620 long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
626 /* find the key first */
627 key_ref = lookup_user_key(keyid, 0, 0);
628 if (IS_ERR(key_ref)) {
633 key = key_ref_to_ptr(key_ref);
635 /* see if we can read it directly */
636 ret = key_permission(key_ref, KEY_READ);
642 /* we can't; see if it's searchable from this process's keyrings
643 * - we automatically take account of the fact that it may be
644 * dangling off an instantiation key
646 if (!is_key_possessed(key_ref)) {
651 /* the key is probably readable - now try to read it */
653 ret = key_validate(key);
656 if (key->type->read) {
657 /* read the data with the semaphore held (since we
659 down_read(&key->sem);
660 ret = key->type->read(key, buffer, buflen);
672 * change the ownership of a key
673 * - the keyring owned by the changer
674 * - if the uid or gid is -1, then that parameter is not changed
675 * - implements keyctl(KEYCTL_CHOWN)
677 long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
679 struct key_user *newowner, *zapowner = NULL;
685 if (uid == (uid_t) -1 && gid == (gid_t) -1)
688 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
690 if (IS_ERR(key_ref)) {
691 ret = PTR_ERR(key_ref);
695 key = key_ref_to_ptr(key_ref);
697 /* make the changes with the locks held to prevent chown/chown races */
699 down_write(&key->sem);
701 if (!capable(CAP_SYS_ADMIN)) {
702 /* only the sysadmin can chown a key to some other UID */
703 if (uid != (uid_t) -1 && key->uid != uid)
706 /* only the sysadmin can set the key's GID to a group other
707 * than one of those that the current process subscribes to */
708 if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
713 if (uid != (uid_t) -1 && uid != key->uid) {
715 newowner = key_user_lookup(uid, current_user_ns());
719 /* transfer the quota burden to the new user */
720 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
721 unsigned maxkeys = (uid == 0) ?
722 key_quota_root_maxkeys : key_quota_maxkeys;
723 unsigned maxbytes = (uid == 0) ?
724 key_quota_root_maxbytes : key_quota_maxbytes;
726 spin_lock(&newowner->lock);
727 if (newowner->qnkeys + 1 >= maxkeys ||
728 newowner->qnbytes + key->quotalen >= maxbytes ||
729 newowner->qnbytes + key->quotalen <
734 newowner->qnbytes += key->quotalen;
735 spin_unlock(&newowner->lock);
737 spin_lock(&key->user->lock);
739 key->user->qnbytes -= key->quotalen;
740 spin_unlock(&key->user->lock);
743 atomic_dec(&key->user->nkeys);
744 atomic_inc(&newowner->nkeys);
746 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
747 atomic_dec(&key->user->nikeys);
748 atomic_inc(&newowner->nikeys);
751 zapowner = key->user;
752 key->user = newowner;
757 if (gid != (gid_t) -1)
766 key_user_put(zapowner);
771 spin_unlock(&newowner->lock);
778 * change the permission mask on a key
779 * - the keyring owned by the changer
780 * - implements keyctl(KEYCTL_SETPERM)
782 long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
789 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
792 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
794 if (IS_ERR(key_ref)) {
795 ret = PTR_ERR(key_ref);
799 key = key_ref_to_ptr(key_ref);
801 /* make the changes with the locks held to prevent chown/chmod races */
803 down_write(&key->sem);
805 /* if we're not the sysadmin, we can only change a key that we own */
806 if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) {
818 * get the destination keyring for instantiation
820 static long get_instantiation_keyring(key_serial_t ringid,
821 struct request_key_auth *rka,
822 struct key **_dest_keyring)
826 *_dest_keyring = NULL;
828 /* just return a NULL pointer if we weren't asked to make a link */
832 /* if a specific keyring is nominated by ID, then use that */
834 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
836 return PTR_ERR(dkref);
837 *_dest_keyring = key_ref_to_ptr(dkref);
841 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
844 /* otherwise specify the destination keyring recorded in the
845 * authorisation key (any KEY_SPEC_*_KEYRING) */
846 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
847 *_dest_keyring = key_get(rka->dest_keyring);
855 * change the request_key authorisation key on the current process
857 static int keyctl_change_reqkey_auth(struct key *key)
861 new = prepare_creds();
865 key_put(new->request_key_auth);
866 new->request_key_auth = key_get(key);
868 return commit_creds(new);
872 * instantiate the key with the specified payload, and, if one is given, link
873 * the key into the keyring
875 long keyctl_instantiate_key(key_serial_t id,
876 const void __user *_payload,
880 const struct cred *cred = current_cred();
881 struct request_key_auth *rka;
882 struct key *instkey, *dest_keyring;
887 kenter("%d,,%zu,%d", id, plen, ringid);
890 if (plen > 1024 * 1024 - 1)
893 /* the appropriate instantiation authorisation key must have been
894 * assumed before calling this */
896 instkey = cred->request_key_auth;
900 rka = instkey->payload.data;
901 if (rka->target_key->serial != id)
904 /* pull the payload in if one was supplied */
909 payload = kmalloc(plen, GFP_KERNEL);
911 if (plen <= PAGE_SIZE)
914 payload = vmalloc(plen);
920 if (copy_from_user(payload, _payload, plen) != 0)
924 /* find the destination keyring amongst those belonging to the
926 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
930 /* instantiate the key and link it into a keyring */
931 ret = key_instantiate_and_link(rka->target_key, payload, plen,
932 dest_keyring, instkey);
934 key_put(dest_keyring);
936 /* discard the assumed authority if it's just been disabled by
937 * instantiation of the key */
939 keyctl_change_reqkey_auth(NULL);
951 * negatively instantiate the key with the given timeout (in seconds), and, if
952 * one is given, link the key into the keyring
954 long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
956 const struct cred *cred = current_cred();
957 struct request_key_auth *rka;
958 struct key *instkey, *dest_keyring;
961 kenter("%d,%u,%d", id, timeout, ringid);
963 /* the appropriate instantiation authorisation key must have been
964 * assumed before calling this */
966 instkey = cred->request_key_auth;
970 rka = instkey->payload.data;
971 if (rka->target_key->serial != id)
974 /* find the destination keyring if present (which must also be
976 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
980 /* instantiate the key and link it into a keyring */
981 ret = key_negate_and_link(rka->target_key, timeout,
982 dest_keyring, instkey);
984 key_put(dest_keyring);
986 /* discard the assumed authority if it's just been disabled by
987 * instantiation of the key */
989 keyctl_change_reqkey_auth(NULL);
996 * set the default keyring in which request_key() will cache keys
997 * - return the old setting
999 long keyctl_set_reqkey_keyring(int reqkey_defl)
1002 int ret, old_setting;
1004 old_setting = current_cred_xxx(jit_keyring);
1006 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1009 new = prepare_creds();
1013 switch (reqkey_defl) {
1014 case KEY_REQKEY_DEFL_THREAD_KEYRING:
1015 ret = install_thread_keyring_to_cred(new);
1020 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
1021 ret = install_process_keyring_to_cred(new);
1029 case KEY_REQKEY_DEFL_DEFAULT:
1030 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1031 case KEY_REQKEY_DEFL_USER_KEYRING:
1032 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1033 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1036 case KEY_REQKEY_DEFL_NO_CHANGE:
1037 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1044 new->jit_keyring = reqkey_defl;
1053 * set or clear the timeout for a key
1055 long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1057 struct timespec now;
1058 struct key *key, *instkey;
1063 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1065 if (IS_ERR(key_ref)) {
1066 /* setting the timeout on a key under construction is permitted
1067 * if we have the authorisation token handy */
1068 if (PTR_ERR(key_ref) == -EACCES) {
1069 instkey = key_get_instantiation_authkey(id);
1070 if (!IS_ERR(instkey)) {
1072 key_ref = lookup_user_key(id,
1075 if (!IS_ERR(key_ref))
1080 ret = PTR_ERR(key_ref);
1085 key = key_ref_to_ptr(key_ref);
1087 /* make the changes with the locks held to prevent races */
1088 down_write(&key->sem);
1092 now = current_kernel_time();
1093 expiry = now.tv_sec + timeout;
1096 key->expiry = expiry;
1097 key_schedule_gc(key->expiry + key_gc_delay);
1099 up_write(&key->sem);
1108 * assume the authority to instantiate the specified key
1110 long keyctl_assume_authority(key_serial_t id)
1112 struct key *authkey;
1115 /* special key IDs aren't permitted */
1120 /* we divest ourselves of authority if given an ID of 0 */
1122 ret = keyctl_change_reqkey_auth(NULL);
1126 /* attempt to assume the authority temporarily granted to us whilst we
1127 * instantiate the specified key
1128 * - the authorisation key must be in the current task's keyrings
1131 authkey = key_get_instantiation_authkey(id);
1132 if (IS_ERR(authkey)) {
1133 ret = PTR_ERR(authkey);
1137 ret = keyctl_change_reqkey_auth(authkey);
1142 ret = authkey->serial;
1148 * get the security label of a key
1149 * - the key must grant us view permission
1150 * - if there's a buffer, we place up to buflen bytes of data into it
1151 * - unless there's an error, we return the amount of information available,
1152 * irrespective of how much we may have copied (including the terminal NUL)
1153 * - implements keyctl(KEYCTL_GET_SECURITY)
1155 long keyctl_get_security(key_serial_t keyid,
1156 char __user *buffer,
1159 struct key *key, *instkey;
1164 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
1165 if (IS_ERR(key_ref)) {
1166 if (PTR_ERR(key_ref) != -EACCES)
1167 return PTR_ERR(key_ref);
1169 /* viewing a key under construction is also permitted if we
1170 * have the authorisation token handy */
1171 instkey = key_get_instantiation_authkey(keyid);
1172 if (IS_ERR(instkey))
1173 return PTR_ERR(instkey);
1176 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
1177 if (IS_ERR(key_ref))
1178 return PTR_ERR(key_ref);
1181 key = key_ref_to_ptr(key_ref);
1182 ret = security_key_getsecurity(key, &context);
1184 /* if no information was returned, give userspace an empty
1187 if (buffer && buflen > 0 &&
1188 copy_to_user(buffer, "", 1) != 0)
1190 } else if (ret > 0) {
1191 /* return as much data as there's room for */
1192 if (buffer && buflen > 0) {
1196 if (copy_to_user(buffer, context, buflen) != 0)
1203 key_ref_put(key_ref);
1208 * attempt to install the calling process's session keyring on the process's
1210 * - the keyring must exist and must grant us LINK permission
1211 * - implements keyctl(KEYCTL_SESSION_TO_PARENT)
1213 long keyctl_session_to_parent(void)
1215 #ifdef TIF_NOTIFY_RESUME
1216 struct task_struct *me, *parent;
1217 const struct cred *mycred, *pcred;
1218 struct cred *cred, *oldcred;
1219 key_ref_t keyring_r;
1222 keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_LINK);
1223 if (IS_ERR(keyring_r))
1224 return PTR_ERR(keyring_r);
1226 /* our parent is going to need a new cred struct, a new tgcred struct
1227 * and new security data, so we allocate them here to prevent ENOMEM in
1230 cred = cred_alloc_blank();
1234 cred->tgcred->session_keyring = key_ref_to_ptr(keyring_r);
1239 write_lock_irq(&tasklist_lock);
1241 parent = me->real_parent;
1244 /* the parent mustn't be init and mustn't be a kernel thread */
1245 if (parent->pid <= 1 || !parent->mm)
1248 /* the parent must be single threaded */
1249 if (!thread_group_empty(parent))
1252 /* the parent and the child must have different session keyrings or
1253 * there's no point */
1254 mycred = current_cred();
1255 pcred = __task_cred(parent);
1256 if (mycred == pcred ||
1257 mycred->tgcred->session_keyring == pcred->tgcred->session_keyring)
1260 /* the parent must have the same effective ownership and mustn't be
1262 if (pcred->uid != mycred->euid ||
1263 pcred->euid != mycred->euid ||
1264 pcred->suid != mycred->euid ||
1265 pcred->gid != mycred->egid ||
1266 pcred->egid != mycred->egid ||
1267 pcred->sgid != mycred->egid)
1270 /* the keyrings must have the same UID */
1271 if ((pcred->tgcred->session_keyring &&
1272 pcred->tgcred->session_keyring->uid != mycred->euid) ||
1273 mycred->tgcred->session_keyring->uid != mycred->euid)
1276 /* if there's an already pending keyring replacement, then we replace
1278 oldcred = parent->replacement_session_keyring;
1280 /* the replacement session keyring is applied just prior to userspace
1282 parent->replacement_session_keyring = cred;
1284 set_ti_thread_flag(task_thread_info(parent), TIF_NOTIFY_RESUME);
1286 write_unlock_irq(&tasklist_lock);
1295 write_unlock_irq(&tasklist_lock);
1301 key_ref_put(keyring_r);
1304 #else /* !TIF_NOTIFY_RESUME */
1306 * To be removed when TIF_NOTIFY_RESUME has been implemented on
1309 #warning TIF_NOTIFY_RESUME not implemented
1311 #endif /* !TIF_NOTIFY_RESUME */
1315 * the key control system call
1317 SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1318 unsigned long, arg4, unsigned long, arg5)
1321 case KEYCTL_GET_KEYRING_ID:
1322 return keyctl_get_keyring_ID((key_serial_t) arg2,
1325 case KEYCTL_JOIN_SESSION_KEYRING:
1326 return keyctl_join_session_keyring((const char __user *) arg2);
1329 return keyctl_update_key((key_serial_t) arg2,
1330 (const void __user *) arg3,
1334 return keyctl_revoke_key((key_serial_t) arg2);
1336 case KEYCTL_DESCRIBE:
1337 return keyctl_describe_key((key_serial_t) arg2,
1338 (char __user *) arg3,
1342 return keyctl_keyring_clear((key_serial_t) arg2);
1345 return keyctl_keyring_link((key_serial_t) arg2,
1346 (key_serial_t) arg3);
1349 return keyctl_keyring_unlink((key_serial_t) arg2,
1350 (key_serial_t) arg3);
1353 return keyctl_keyring_search((key_serial_t) arg2,
1354 (const char __user *) arg3,
1355 (const char __user *) arg4,
1356 (key_serial_t) arg5);
1359 return keyctl_read_key((key_serial_t) arg2,
1360 (char __user *) arg3,
1364 return keyctl_chown_key((key_serial_t) arg2,
1368 case KEYCTL_SETPERM:
1369 return keyctl_setperm_key((key_serial_t) arg2,
1372 case KEYCTL_INSTANTIATE:
1373 return keyctl_instantiate_key((key_serial_t) arg2,
1374 (const void __user *) arg3,
1376 (key_serial_t) arg5);
1379 return keyctl_negate_key((key_serial_t) arg2,
1381 (key_serial_t) arg4);
1383 case KEYCTL_SET_REQKEY_KEYRING:
1384 return keyctl_set_reqkey_keyring(arg2);
1386 case KEYCTL_SET_TIMEOUT:
1387 return keyctl_set_timeout((key_serial_t) arg2,
1390 case KEYCTL_ASSUME_AUTHORITY:
1391 return keyctl_assume_authority((key_serial_t) arg2);
1393 case KEYCTL_GET_SECURITY:
1394 return keyctl_get_security((key_serial_t) arg2,
1395 (char __user *) arg3,
1398 case KEYCTL_SESSION_TO_PARENT:
1399 return keyctl_session_to_parent();