1 /* Userspace key control 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/key.h>
18 #include <linux/keyctl.h>
20 #include <linux/capability.h>
21 #include <linux/string.h>
22 #include <linux/err.h>
23 #include <linux/vmalloc.h>
24 #include <linux/security.h>
25 #include <linux/uio.h>
26 #include <asm/uaccess.h>
29 static int key_get_type_from_user(char *type
,
30 const char __user
*_type
,
35 ret
= strncpy_from_user(type
, _type
, len
);
38 if (ret
== 0 || ret
>= len
)
47 * Extract the description of a new key from userspace and either add it as a
48 * new key to the specified keyring or update a matching key in that keyring.
50 * If the description is NULL or an empty string, the key type is asked to
51 * generate one from the payload.
53 * The keyring must be writable so that we can attach the key to it.
55 * If successful, the new key's serial number is returned, otherwise an error
58 SYSCALL_DEFINE5(add_key
, const char __user
*, _type
,
59 const char __user
*, _description
,
60 const void __user
*, _payload
,
64 key_ref_t keyring_ref
, key_ref
;
65 char type
[32], *description
;
71 if (plen
> 1024 * 1024 - 1)
74 /* draw all the data into kernel space */
75 ret
= key_get_type_from_user(type
, _type
, sizeof(type
));
81 description
= strndup_user(_description
, PAGE_SIZE
);
82 if (IS_ERR(description
)) {
83 ret
= PTR_ERR(description
);
89 } else if ((description
[0] == '.') &&
90 (strncmp(type
, "keyring", 7) == 0)) {
96 /* pull the payload in if one was supplied */
102 payload
= kmalloc(plen
, GFP_KERNEL
| __GFP_NOWARN
);
104 if (plen
<= PAGE_SIZE
)
107 payload
= vmalloc(plen
);
113 if (copy_from_user(payload
, _payload
, plen
) != 0)
117 /* find the target keyring (which must be writable) */
118 keyring_ref
= lookup_user_key(ringid
, KEY_LOOKUP_CREATE
, KEY_NEED_WRITE
);
119 if (IS_ERR(keyring_ref
)) {
120 ret
= PTR_ERR(keyring_ref
);
124 /* create or update the requested key and add it to the target
126 key_ref
= key_create_or_update(keyring_ref
, type
, description
,
127 payload
, plen
, KEY_PERM_UNDEF
,
129 if (!IS_ERR(key_ref
)) {
130 ret
= key_ref_to_ptr(key_ref
)->serial
;
131 key_ref_put(key_ref
);
134 ret
= PTR_ERR(key_ref
);
137 key_ref_put(keyring_ref
);
150 * Search the process keyrings and keyring trees linked from those for a
151 * matching key. Keyrings must have appropriate Search permission to be
154 * If a key is found, it will be attached to the destination keyring if there's
155 * one specified and the serial number of the key will be returned.
157 * If no key is found, /sbin/request-key will be invoked if _callout_info is
158 * non-NULL in an attempt to create a key. The _callout_info string will be
159 * passed to /sbin/request-key to aid with completing the request. If the
160 * _callout_info string is "" then it will be changed to "-".
162 SYSCALL_DEFINE4(request_key
, const char __user
*, _type
,
163 const char __user
*, _description
,
164 const char __user
*, _callout_info
,
165 key_serial_t
, destringid
)
167 struct key_type
*ktype
;
171 char type
[32], *description
, *callout_info
;
174 /* pull the type into kernel space */
175 ret
= key_get_type_from_user(type
, _type
, sizeof(type
));
179 /* pull the description into kernel space */
180 description
= strndup_user(_description
, PAGE_SIZE
);
181 if (IS_ERR(description
)) {
182 ret
= PTR_ERR(description
);
186 /* pull the callout info into kernel space */
190 callout_info
= strndup_user(_callout_info
, PAGE_SIZE
);
191 if (IS_ERR(callout_info
)) {
192 ret
= PTR_ERR(callout_info
);
195 callout_len
= strlen(callout_info
);
198 /* get the destination keyring if specified */
201 dest_ref
= lookup_user_key(destringid
, KEY_LOOKUP_CREATE
,
203 if (IS_ERR(dest_ref
)) {
204 ret
= PTR_ERR(dest_ref
);
209 /* find the key type */
210 ktype
= key_type_lookup(type
);
212 ret
= PTR_ERR(ktype
);
217 key
= request_key_and_link(ktype
, description
, callout_info
,
218 callout_len
, NULL
, key_ref_to_ptr(dest_ref
),
225 /* wait for the key to finish being constructed */
226 ret
= wait_for_key_construction(key
, 1);
237 key_ref_put(dest_ref
);
247 * Get the ID of the specified process keyring.
249 * The requested keyring must have search permission to be found.
251 * If successful, the ID of the requested keyring will be returned.
253 long keyctl_get_keyring_ID(key_serial_t id
, int create
)
256 unsigned long lflags
;
259 lflags
= create
? KEY_LOOKUP_CREATE
: 0;
260 key_ref
= lookup_user_key(id
, lflags
, KEY_NEED_SEARCH
);
261 if (IS_ERR(key_ref
)) {
262 ret
= PTR_ERR(key_ref
);
266 ret
= key_ref_to_ptr(key_ref
)->serial
;
267 key_ref_put(key_ref
);
273 * Join a (named) session keyring.
275 * Create and join an anonymous session keyring or join a named session
276 * keyring, creating it if necessary. A named session keyring must have Search
277 * permission for it to be joined. Session keyrings without this permit will
280 * If successful, the ID of the joined session keyring will be returned.
282 long keyctl_join_session_keyring(const char __user
*_name
)
287 /* fetch the name from userspace */
290 name
= strndup_user(_name
, PAGE_SIZE
);
297 /* join the session */
298 ret
= join_session_keyring(name
);
306 * Update a key's data payload from the given data.
308 * The key must grant the caller Write permission and the key type must support
309 * updating for this to work. A negative key can be positively instantiated
312 * If successful, 0 will be returned. If the key type does not support
313 * updating, then -EOPNOTSUPP will be returned.
315 long keyctl_update_key(key_serial_t id
,
316 const void __user
*_payload
,
324 if (plen
> PAGE_SIZE
)
327 /* pull the payload in if one was supplied */
331 payload
= kmalloc(plen
, GFP_KERNEL
);
336 if (copy_from_user(payload
, _payload
, plen
) != 0)
340 /* find the target key (which must be writable) */
341 key_ref
= lookup_user_key(id
, 0, KEY_NEED_WRITE
);
342 if (IS_ERR(key_ref
)) {
343 ret
= PTR_ERR(key_ref
);
348 ret
= key_update(key_ref
, payload
, plen
);
350 key_ref_put(key_ref
);
360 * The key must be grant the caller Write or Setattr permission for this to
361 * work. The key type should give up its quota claim when revoked. The key
362 * and any links to the key will be automatically garbage collected after a
363 * certain amount of time (/proc/sys/kernel/keys/gc_delay).
365 * If successful, 0 is returned.
367 long keyctl_revoke_key(key_serial_t id
)
372 key_ref
= lookup_user_key(id
, 0, KEY_NEED_WRITE
);
373 if (IS_ERR(key_ref
)) {
374 ret
= PTR_ERR(key_ref
);
377 key_ref
= lookup_user_key(id
, 0, KEY_NEED_SETATTR
);
378 if (IS_ERR(key_ref
)) {
379 ret
= PTR_ERR(key_ref
);
384 key_revoke(key_ref_to_ptr(key_ref
));
387 key_ref_put(key_ref
);
395 * The key must be grant the caller Invalidate permission for this to work.
396 * The key and any links to the key will be automatically garbage collected
399 * If successful, 0 is returned.
401 long keyctl_invalidate_key(key_serial_t id
)
408 key_ref
= lookup_user_key(id
, 0, KEY_NEED_SEARCH
);
409 if (IS_ERR(key_ref
)) {
410 ret
= PTR_ERR(key_ref
);
412 /* Root is permitted to invalidate certain special keys */
413 if (capable(CAP_SYS_ADMIN
)) {
414 key_ref
= lookup_user_key(id
, 0, 0);
417 if (test_bit(KEY_FLAG_ROOT_CAN_INVAL
,
418 &key_ref_to_ptr(key_ref
)->flags
))
427 key_invalidate(key_ref_to_ptr(key_ref
));
430 key_ref_put(key_ref
);
432 kleave(" = %ld", ret
);
437 * Clear the specified keyring, creating an empty process keyring if one of the
438 * special keyring IDs is used.
440 * The keyring must grant the caller Write permission for this to work. If
441 * successful, 0 will be returned.
443 long keyctl_keyring_clear(key_serial_t ringid
)
445 key_ref_t keyring_ref
;
448 keyring_ref
= lookup_user_key(ringid
, KEY_LOOKUP_CREATE
, KEY_NEED_WRITE
);
449 if (IS_ERR(keyring_ref
)) {
450 ret
= PTR_ERR(keyring_ref
);
452 /* Root is permitted to invalidate certain special keyrings */
453 if (capable(CAP_SYS_ADMIN
)) {
454 keyring_ref
= lookup_user_key(ringid
, 0, 0);
455 if (IS_ERR(keyring_ref
))
457 if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR
,
458 &key_ref_to_ptr(keyring_ref
)->flags
))
467 ret
= keyring_clear(key_ref_to_ptr(keyring_ref
));
469 key_ref_put(keyring_ref
);
475 * Create a link from a keyring to a key if there's no matching key in the
476 * keyring, otherwise replace the link to the matching key with a link to the
479 * The key must grant the caller Link permission and the the keyring must grant
480 * the caller Write permission. Furthermore, if an additional link is created,
481 * the keyring's quota will be extended.
483 * If successful, 0 will be returned.
485 long keyctl_keyring_link(key_serial_t id
, key_serial_t ringid
)
487 key_ref_t keyring_ref
, key_ref
;
490 keyring_ref
= lookup_user_key(ringid
, KEY_LOOKUP_CREATE
, KEY_NEED_WRITE
);
491 if (IS_ERR(keyring_ref
)) {
492 ret
= PTR_ERR(keyring_ref
);
496 key_ref
= lookup_user_key(id
, KEY_LOOKUP_CREATE
, KEY_NEED_LINK
);
497 if (IS_ERR(key_ref
)) {
498 ret
= PTR_ERR(key_ref
);
502 ret
= key_link(key_ref_to_ptr(keyring_ref
), key_ref_to_ptr(key_ref
));
504 key_ref_put(key_ref
);
506 key_ref_put(keyring_ref
);
512 * Unlink a key from a keyring.
514 * The keyring must grant the caller Write permission for this to work; the key
515 * itself need not grant the caller anything. If the last link to a key is
516 * removed then that key will be scheduled for destruction.
518 * If successful, 0 will be returned.
520 long keyctl_keyring_unlink(key_serial_t id
, key_serial_t ringid
)
522 key_ref_t keyring_ref
, key_ref
;
525 keyring_ref
= lookup_user_key(ringid
, 0, KEY_NEED_WRITE
);
526 if (IS_ERR(keyring_ref
)) {
527 ret
= PTR_ERR(keyring_ref
);
531 key_ref
= lookup_user_key(id
, KEY_LOOKUP_FOR_UNLINK
, 0);
532 if (IS_ERR(key_ref
)) {
533 ret
= PTR_ERR(key_ref
);
537 ret
= key_unlink(key_ref_to_ptr(keyring_ref
), key_ref_to_ptr(key_ref
));
539 key_ref_put(key_ref
);
541 key_ref_put(keyring_ref
);
547 * Return a description of a key to userspace.
549 * The key must grant the caller View permission for this to work.
551 * If there's a buffer, we place up to buflen bytes of data into it formatted
552 * in the following way:
554 * type;uid;gid;perm;description<NUL>
556 * If successful, we return the amount of description available, irrespective
557 * of how much we may have copied into the buffer.
559 long keyctl_describe_key(key_serial_t keyid
,
563 struct key
*key
, *instkey
;
568 key_ref
= lookup_user_key(keyid
, KEY_LOOKUP_PARTIAL
, KEY_NEED_VIEW
);
569 if (IS_ERR(key_ref
)) {
570 /* viewing a key under construction is permitted if we have the
571 * authorisation token handy */
572 if (PTR_ERR(key_ref
) == -EACCES
) {
573 instkey
= key_get_instantiation_authkey(keyid
);
574 if (!IS_ERR(instkey
)) {
576 key_ref
= lookup_user_key(keyid
,
579 if (!IS_ERR(key_ref
))
584 ret
= PTR_ERR(key_ref
);
589 /* calculate how much description we're going to return */
591 tmpbuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
595 key
= key_ref_to_ptr(key_ref
);
597 ret
= snprintf(tmpbuf
, PAGE_SIZE
- 1,
600 from_kuid_munged(current_user_ns(), key
->uid
),
601 from_kgid_munged(current_user_ns(), key
->gid
),
603 key
->description
?: "");
605 /* include a NUL char at the end of the data */
606 if (ret
> PAGE_SIZE
- 1)
611 /* consider returning the data */
612 if (buffer
&& buflen
> 0) {
616 if (copy_to_user(buffer
, tmpbuf
, buflen
) != 0)
622 key_ref_put(key_ref
);
628 * Search the specified keyring and any keyrings it links to for a matching
629 * key. Only keyrings that grant the caller Search permission will be searched
630 * (this includes the starting keyring). Only keys with Search permission can
633 * If successful, the found key will be linked to the destination keyring if
634 * supplied and the key has Link permission, and the found key ID will be
637 long keyctl_keyring_search(key_serial_t ringid
,
638 const char __user
*_type
,
639 const char __user
*_description
,
640 key_serial_t destringid
)
642 struct key_type
*ktype
;
643 key_ref_t keyring_ref
, key_ref
, dest_ref
;
644 char type
[32], *description
;
647 /* pull the type and description into kernel space */
648 ret
= key_get_type_from_user(type
, _type
, sizeof(type
));
652 description
= strndup_user(_description
, PAGE_SIZE
);
653 if (IS_ERR(description
)) {
654 ret
= PTR_ERR(description
);
658 /* get the keyring at which to begin the search */
659 keyring_ref
= lookup_user_key(ringid
, 0, KEY_NEED_SEARCH
);
660 if (IS_ERR(keyring_ref
)) {
661 ret
= PTR_ERR(keyring_ref
);
665 /* get the destination keyring if specified */
668 dest_ref
= lookup_user_key(destringid
, KEY_LOOKUP_CREATE
,
670 if (IS_ERR(dest_ref
)) {
671 ret
= PTR_ERR(dest_ref
);
676 /* find the key type */
677 ktype
= key_type_lookup(type
);
679 ret
= PTR_ERR(ktype
);
684 key_ref
= keyring_search(keyring_ref
, ktype
, description
);
685 if (IS_ERR(key_ref
)) {
686 ret
= PTR_ERR(key_ref
);
688 /* treat lack or presence of a negative key the same */
694 /* link the resulting key to the destination keyring if we can */
696 ret
= key_permission(key_ref
, KEY_NEED_LINK
);
700 ret
= key_link(key_ref_to_ptr(dest_ref
), key_ref_to_ptr(key_ref
));
705 ret
= key_ref_to_ptr(key_ref
)->serial
;
708 key_ref_put(key_ref
);
712 key_ref_put(dest_ref
);
714 key_ref_put(keyring_ref
);
722 * Read a key's payload.
724 * The key must either grant the caller Read permission, or it must grant the
725 * caller Search permission when searched for from the process keyrings.
727 * If successful, we place up to buflen bytes of data into the buffer, if one
728 * is provided, and return the amount of data that is available in the key,
729 * irrespective of how much we copied into the buffer.
731 long keyctl_read_key(key_serial_t keyid
, char __user
*buffer
, size_t buflen
)
737 /* find the key first */
738 key_ref
= lookup_user_key(keyid
, 0, 0);
739 if (IS_ERR(key_ref
)) {
744 key
= key_ref_to_ptr(key_ref
);
746 /* see if we can read it directly */
747 ret
= key_permission(key_ref
, KEY_NEED_READ
);
753 /* we can't; see if it's searchable from this process's keyrings
754 * - we automatically take account of the fact that it may be
755 * dangling off an instantiation key
757 if (!is_key_possessed(key_ref
)) {
762 /* the key is probably readable - now try to read it */
764 ret
= key_validate(key
);
767 if (key
->type
->read
) {
768 /* read the data with the semaphore held (since we
770 down_read(&key
->sem
);
771 ret
= key
->type
->read(key
, buffer
, buflen
);
783 * Change the ownership of a key
785 * The key must grant the caller Setattr permission for this to work, though
786 * the key need not be fully instantiated yet. For the UID to be changed, or
787 * for the GID to be changed to a group the caller is not a member of, the
788 * caller must have sysadmin capability. If either uid or gid is -1 then that
789 * attribute is not changed.
791 * If the UID is to be changed, the new user must have sufficient quota to
792 * accept the key. The quota deduction will be removed from the old user to
793 * the new user should the attribute be changed.
795 * If successful, 0 will be returned.
797 long keyctl_chown_key(key_serial_t id
, uid_t user
, gid_t group
)
799 struct key_user
*newowner
, *zapowner
= NULL
;
806 uid
= make_kuid(current_user_ns(), user
);
807 gid
= make_kgid(current_user_ns(), group
);
809 if ((user
!= (uid_t
) -1) && !uid_valid(uid
))
811 if ((group
!= (gid_t
) -1) && !gid_valid(gid
))
815 if (user
== (uid_t
) -1 && group
== (gid_t
) -1)
818 key_ref
= lookup_user_key(id
, KEY_LOOKUP_CREATE
| KEY_LOOKUP_PARTIAL
,
820 if (IS_ERR(key_ref
)) {
821 ret
= PTR_ERR(key_ref
);
825 key
= key_ref_to_ptr(key_ref
);
827 /* make the changes with the locks held to prevent chown/chown races */
829 down_write(&key
->sem
);
831 if (!capable(CAP_SYS_ADMIN
)) {
832 /* only the sysadmin can chown a key to some other UID */
833 if (user
!= (uid_t
) -1 && !uid_eq(key
->uid
, uid
))
836 /* only the sysadmin can set the key's GID to a group other
837 * than one of those that the current process subscribes to */
838 if (group
!= (gid_t
) -1 && !gid_eq(gid
, key
->gid
) && !in_group_p(gid
))
843 if (user
!= (uid_t
) -1 && !uid_eq(uid
, key
->uid
)) {
845 newowner
= key_user_lookup(uid
);
849 /* transfer the quota burden to the new user */
850 if (test_bit(KEY_FLAG_IN_QUOTA
, &key
->flags
)) {
851 unsigned maxkeys
= uid_eq(uid
, GLOBAL_ROOT_UID
) ?
852 key_quota_root_maxkeys
: key_quota_maxkeys
;
853 unsigned maxbytes
= uid_eq(uid
, GLOBAL_ROOT_UID
) ?
854 key_quota_root_maxbytes
: key_quota_maxbytes
;
856 spin_lock(&newowner
->lock
);
857 if (newowner
->qnkeys
+ 1 >= maxkeys
||
858 newowner
->qnbytes
+ key
->quotalen
>= maxbytes
||
859 newowner
->qnbytes
+ key
->quotalen
<
864 newowner
->qnbytes
+= key
->quotalen
;
865 spin_unlock(&newowner
->lock
);
867 spin_lock(&key
->user
->lock
);
869 key
->user
->qnbytes
-= key
->quotalen
;
870 spin_unlock(&key
->user
->lock
);
873 atomic_dec(&key
->user
->nkeys
);
874 atomic_inc(&newowner
->nkeys
);
876 if (test_bit(KEY_FLAG_INSTANTIATED
, &key
->flags
)) {
877 atomic_dec(&key
->user
->nikeys
);
878 atomic_inc(&newowner
->nikeys
);
881 zapowner
= key
->user
;
882 key
->user
= newowner
;
887 if (group
!= (gid_t
) -1)
896 key_user_put(zapowner
);
901 spin_unlock(&newowner
->lock
);
908 * Change the permission mask on a key.
910 * The key must grant the caller Setattr permission for this to work, though
911 * the key need not be fully instantiated yet. If the caller does not have
912 * sysadmin capability, it may only change the permission on keys that it owns.
914 long keyctl_setperm_key(key_serial_t id
, key_perm_t perm
)
921 if (perm
& ~(KEY_POS_ALL
| KEY_USR_ALL
| KEY_GRP_ALL
| KEY_OTH_ALL
))
924 key_ref
= lookup_user_key(id
, KEY_LOOKUP_CREATE
| KEY_LOOKUP_PARTIAL
,
926 if (IS_ERR(key_ref
)) {
927 ret
= PTR_ERR(key_ref
);
931 key
= key_ref_to_ptr(key_ref
);
933 /* make the changes with the locks held to prevent chown/chmod races */
935 down_write(&key
->sem
);
937 /* if we're not the sysadmin, we can only change a key that we own */
938 if (capable(CAP_SYS_ADMIN
) || uid_eq(key
->uid
, current_fsuid())) {
950 * Get the destination keyring for instantiation and check that the caller has
951 * Write permission on it.
953 static long get_instantiation_keyring(key_serial_t ringid
,
954 struct request_key_auth
*rka
,
955 struct key
**_dest_keyring
)
959 *_dest_keyring
= NULL
;
961 /* just return a NULL pointer if we weren't asked to make a link */
965 /* if a specific keyring is nominated by ID, then use that */
967 dkref
= lookup_user_key(ringid
, KEY_LOOKUP_CREATE
, KEY_NEED_WRITE
);
969 return PTR_ERR(dkref
);
970 *_dest_keyring
= key_ref_to_ptr(dkref
);
974 if (ringid
== KEY_SPEC_REQKEY_AUTH_KEY
)
977 /* otherwise specify the destination keyring recorded in the
978 * authorisation key (any KEY_SPEC_*_KEYRING) */
979 if (ringid
>= KEY_SPEC_REQUESTOR_KEYRING
) {
980 *_dest_keyring
= key_get(rka
->dest_keyring
);
988 * Change the request_key authorisation key on the current process.
990 static int keyctl_change_reqkey_auth(struct key
*key
)
994 new = prepare_creds();
998 key_put(new->request_key_auth
);
999 new->request_key_auth
= key_get(key
);
1001 return commit_creds(new);
1005 * Copy the iovec data from userspace
1007 static long copy_from_user_iovec(void *buffer
, const struct iovec
*iov
,
1010 for (; ioc
> 0; ioc
--) {
1011 if (copy_from_user(buffer
, iov
->iov_base
, iov
->iov_len
) != 0)
1013 buffer
+= iov
->iov_len
;
1020 * Instantiate a key with the specified payload and link the key into the
1021 * destination keyring if one is given.
1023 * The caller must have the appropriate instantiation permit set for this to
1024 * work (see keyctl_assume_authority). No other permissions are required.
1026 * If successful, 0 will be returned.
1028 long keyctl_instantiate_key_common(key_serial_t id
,
1029 const struct iovec
*payload_iov
,
1032 key_serial_t ringid
)
1034 const struct cred
*cred
= current_cred();
1035 struct request_key_auth
*rka
;
1036 struct key
*instkey
, *dest_keyring
;
1041 kenter("%d,,%zu,%d", id
, plen
, ringid
);
1044 if (plen
> 1024 * 1024 - 1)
1047 /* the appropriate instantiation authorisation key must have been
1048 * assumed before calling this */
1050 instkey
= cred
->request_key_auth
;
1054 rka
= instkey
->payload
.data
;
1055 if (rka
->target_key
->serial
!= id
)
1058 /* pull the payload in if one was supplied */
1063 payload
= kmalloc(plen
, GFP_KERNEL
);
1065 if (plen
<= PAGE_SIZE
)
1068 payload
= vmalloc(plen
);
1073 ret
= copy_from_user_iovec(payload
, payload_iov
, ioc
);
1078 /* find the destination keyring amongst those belonging to the
1079 * requesting task */
1080 ret
= get_instantiation_keyring(ringid
, rka
, &dest_keyring
);
1084 /* instantiate the key and link it into a keyring */
1085 ret
= key_instantiate_and_link(rka
->target_key
, payload
, plen
,
1086 dest_keyring
, instkey
);
1088 key_put(dest_keyring
);
1090 /* discard the assumed authority if it's just been disabled by
1091 * instantiation of the key */
1093 keyctl_change_reqkey_auth(NULL
);
1105 * Instantiate a key with the specified payload and link the key into the
1106 * destination keyring if one is given.
1108 * The caller must have the appropriate instantiation permit set for this to
1109 * work (see keyctl_assume_authority). No other permissions are required.
1111 * If successful, 0 will be returned.
1113 long keyctl_instantiate_key(key_serial_t id
,
1114 const void __user
*_payload
,
1116 key_serial_t ringid
)
1118 if (_payload
&& plen
) {
1119 struct iovec iov
[1] = {
1120 [0].iov_base
= (void __user
*)_payload
,
1124 return keyctl_instantiate_key_common(id
, iov
, 1, plen
, ringid
);
1127 return keyctl_instantiate_key_common(id
, NULL
, 0, 0, ringid
);
1131 * Instantiate a key with the specified multipart payload and link the key into
1132 * the destination keyring if one is given.
1134 * The caller must have the appropriate instantiation permit set for this to
1135 * work (see keyctl_assume_authority). No other permissions are required.
1137 * If successful, 0 will be returned.
1139 long keyctl_instantiate_key_iov(key_serial_t id
,
1140 const struct iovec __user
*_payload_iov
,
1142 key_serial_t ringid
)
1144 struct iovec iovstack
[UIO_FASTIOV
], *iov
= iovstack
;
1147 if (!_payload_iov
|| !ioc
)
1150 ret
= rw_copy_check_uvector(WRITE
, _payload_iov
, ioc
,
1151 ARRAY_SIZE(iovstack
), iovstack
, &iov
);
1155 goto no_payload_free
;
1157 ret
= keyctl_instantiate_key_common(id
, iov
, ioc
, ret
, ringid
);
1159 if (iov
!= iovstack
)
1164 if (iov
!= iovstack
)
1167 return keyctl_instantiate_key_common(id
, NULL
, 0, 0, ringid
);
1171 * Negatively instantiate the key with the given timeout (in seconds) and link
1172 * the key into the destination keyring if one is given.
1174 * The caller must have the appropriate instantiation permit set for this to
1175 * work (see keyctl_assume_authority). No other permissions are required.
1177 * The key and any links to the key will be automatically garbage collected
1178 * after the timeout expires.
1180 * Negative keys are used to rate limit repeated request_key() calls by causing
1181 * them to return -ENOKEY until the negative key expires.
1183 * If successful, 0 will be returned.
1185 long keyctl_negate_key(key_serial_t id
, unsigned timeout
, key_serial_t ringid
)
1187 return keyctl_reject_key(id
, timeout
, ENOKEY
, ringid
);
1191 * Negatively instantiate the key with the given timeout (in seconds) and error
1192 * code and link the key into the destination keyring if one is given.
1194 * The caller must have the appropriate instantiation permit set for this to
1195 * work (see keyctl_assume_authority). No other permissions are required.
1197 * The key and any links to the key will be automatically garbage collected
1198 * after the timeout expires.
1200 * Negative keys are used to rate limit repeated request_key() calls by causing
1201 * them to return the specified error code until the negative key expires.
1203 * If successful, 0 will be returned.
1205 long keyctl_reject_key(key_serial_t id
, unsigned timeout
, unsigned error
,
1206 key_serial_t ringid
)
1208 const struct cred
*cred
= current_cred();
1209 struct request_key_auth
*rka
;
1210 struct key
*instkey
, *dest_keyring
;
1213 kenter("%d,%u,%u,%d", id
, timeout
, error
, ringid
);
1215 /* must be a valid error code and mustn't be a kernel special */
1217 error
>= MAX_ERRNO
||
1218 error
== ERESTARTSYS
||
1219 error
== ERESTARTNOINTR
||
1220 error
== ERESTARTNOHAND
||
1221 error
== ERESTART_RESTARTBLOCK
)
1224 /* the appropriate instantiation authorisation key must have been
1225 * assumed before calling this */
1227 instkey
= cred
->request_key_auth
;
1231 rka
= instkey
->payload
.data
;
1232 if (rka
->target_key
->serial
!= id
)
1235 /* find the destination keyring if present (which must also be
1237 ret
= get_instantiation_keyring(ringid
, rka
, &dest_keyring
);
1241 /* instantiate the key and link it into a keyring */
1242 ret
= key_reject_and_link(rka
->target_key
, timeout
, error
,
1243 dest_keyring
, instkey
);
1245 key_put(dest_keyring
);
1247 /* discard the assumed authority if it's just been disabled by
1248 * instantiation of the key */
1250 keyctl_change_reqkey_auth(NULL
);
1257 * Read or set the default keyring in which request_key() will cache keys and
1258 * return the old setting.
1260 * If a process keyring is specified then this will be created if it doesn't
1261 * yet exist. The old setting will be returned if successful.
1263 long keyctl_set_reqkey_keyring(int reqkey_defl
)
1266 int ret
, old_setting
;
1268 old_setting
= current_cred_xxx(jit_keyring
);
1270 if (reqkey_defl
== KEY_REQKEY_DEFL_NO_CHANGE
)
1273 new = prepare_creds();
1277 switch (reqkey_defl
) {
1278 case KEY_REQKEY_DEFL_THREAD_KEYRING
:
1279 ret
= install_thread_keyring_to_cred(new);
1284 case KEY_REQKEY_DEFL_PROCESS_KEYRING
:
1285 ret
= install_process_keyring_to_cred(new);
1293 case KEY_REQKEY_DEFL_DEFAULT
:
1294 case KEY_REQKEY_DEFL_SESSION_KEYRING
:
1295 case KEY_REQKEY_DEFL_USER_KEYRING
:
1296 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING
:
1297 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING
:
1300 case KEY_REQKEY_DEFL_NO_CHANGE
:
1301 case KEY_REQKEY_DEFL_GROUP_KEYRING
:
1308 new->jit_keyring
= reqkey_defl
;
1317 * Set or clear the timeout on a key.
1319 * Either the key must grant the caller Setattr permission or else the caller
1320 * must hold an instantiation authorisation token for the key.
1322 * The timeout is either 0 to clear the timeout, or a number of seconds from
1323 * the current time. The key and any links to the key will be automatically
1324 * garbage collected after the timeout expires.
1326 * If successful, 0 is returned.
1328 long keyctl_set_timeout(key_serial_t id
, unsigned timeout
)
1330 struct key
*key
, *instkey
;
1334 key_ref
= lookup_user_key(id
, KEY_LOOKUP_CREATE
| KEY_LOOKUP_PARTIAL
,
1336 if (IS_ERR(key_ref
)) {
1337 /* setting the timeout on a key under construction is permitted
1338 * if we have the authorisation token handy */
1339 if (PTR_ERR(key_ref
) == -EACCES
) {
1340 instkey
= key_get_instantiation_authkey(id
);
1341 if (!IS_ERR(instkey
)) {
1343 key_ref
= lookup_user_key(id
,
1346 if (!IS_ERR(key_ref
))
1351 ret
= PTR_ERR(key_ref
);
1356 key
= key_ref_to_ptr(key_ref
);
1357 key_set_timeout(key
, timeout
);
1366 * Assume (or clear) the authority to instantiate the specified key.
1368 * This sets the authoritative token currently in force for key instantiation.
1369 * This must be done for a key to be instantiated. It has the effect of making
1370 * available all the keys from the caller of the request_key() that created a
1371 * key to request_key() calls made by the caller of this function.
1373 * The caller must have the instantiation key in their process keyrings with a
1374 * Search permission grant available to the caller.
1376 * If the ID given is 0, then the setting will be cleared and 0 returned.
1378 * If the ID given has a matching an authorisation key, then that key will be
1379 * set and its ID will be returned. The authorisation key can be read to get
1380 * the callout information passed to request_key().
1382 long keyctl_assume_authority(key_serial_t id
)
1384 struct key
*authkey
;
1387 /* special key IDs aren't permitted */
1392 /* we divest ourselves of authority if given an ID of 0 */
1394 ret
= keyctl_change_reqkey_auth(NULL
);
1398 /* attempt to assume the authority temporarily granted to us whilst we
1399 * instantiate the specified key
1400 * - the authorisation key must be in the current task's keyrings
1403 authkey
= key_get_instantiation_authkey(id
);
1404 if (IS_ERR(authkey
)) {
1405 ret
= PTR_ERR(authkey
);
1409 ret
= keyctl_change_reqkey_auth(authkey
);
1414 ret
= authkey
->serial
;
1420 * Get a key's the LSM security label.
1422 * The key must grant the caller View permission for this to work.
1424 * If there's a buffer, then up to buflen bytes of data will be placed into it.
1426 * If successful, the amount of information available will be returned,
1427 * irrespective of how much was copied (including the terminal NUL).
1429 long keyctl_get_security(key_serial_t keyid
,
1430 char __user
*buffer
,
1433 struct key
*key
, *instkey
;
1438 key_ref
= lookup_user_key(keyid
, KEY_LOOKUP_PARTIAL
, KEY_NEED_VIEW
);
1439 if (IS_ERR(key_ref
)) {
1440 if (PTR_ERR(key_ref
) != -EACCES
)
1441 return PTR_ERR(key_ref
);
1443 /* viewing a key under construction is also permitted if we
1444 * have the authorisation token handy */
1445 instkey
= key_get_instantiation_authkey(keyid
);
1446 if (IS_ERR(instkey
))
1447 return PTR_ERR(instkey
);
1450 key_ref
= lookup_user_key(keyid
, KEY_LOOKUP_PARTIAL
, 0);
1451 if (IS_ERR(key_ref
))
1452 return PTR_ERR(key_ref
);
1455 key
= key_ref_to_ptr(key_ref
);
1456 ret
= security_key_getsecurity(key
, &context
);
1458 /* if no information was returned, give userspace an empty
1461 if (buffer
&& buflen
> 0 &&
1462 copy_to_user(buffer
, "", 1) != 0)
1464 } else if (ret
> 0) {
1465 /* return as much data as there's room for */
1466 if (buffer
&& buflen
> 0) {
1470 if (copy_to_user(buffer
, context
, buflen
) != 0)
1477 key_ref_put(key_ref
);
1482 * Attempt to install the calling process's session keyring on the process's
1485 * The keyring must exist and must grant the caller LINK permission, and the
1486 * parent process must be single-threaded and must have the same effective
1487 * ownership as this process and mustn't be SUID/SGID.
1489 * The keyring will be emplaced on the parent when it next resumes userspace.
1491 * If successful, 0 will be returned.
1493 long keyctl_session_to_parent(void)
1495 struct task_struct
*me
, *parent
;
1496 const struct cred
*mycred
, *pcred
;
1497 struct callback_head
*newwork
, *oldwork
;
1498 key_ref_t keyring_r
;
1502 keyring_r
= lookup_user_key(KEY_SPEC_SESSION_KEYRING
, 0, KEY_NEED_LINK
);
1503 if (IS_ERR(keyring_r
))
1504 return PTR_ERR(keyring_r
);
1508 /* our parent is going to need a new cred struct, a new tgcred struct
1509 * and new security data, so we allocate them here to prevent ENOMEM in
1511 cred
= cred_alloc_blank();
1514 newwork
= &cred
->rcu
;
1516 cred
->session_keyring
= key_ref_to_ptr(keyring_r
);
1518 init_task_work(newwork
, key_change_session_keyring
);
1522 write_lock_irq(&tasklist_lock
);
1526 parent
= me
->real_parent
;
1528 /* the parent mustn't be init and mustn't be a kernel thread */
1529 if (parent
->pid
<= 1 || !parent
->mm
)
1532 /* the parent must be single threaded */
1533 if (!thread_group_empty(parent
))
1536 /* the parent and the child must have different session keyrings or
1537 * there's no point */
1538 mycred
= current_cred();
1539 pcred
= __task_cred(parent
);
1540 if (mycred
== pcred
||
1541 mycred
->session_keyring
== pcred
->session_keyring
) {
1546 /* the parent must have the same effective ownership and mustn't be
1548 if (!uid_eq(pcred
->uid
, mycred
->euid
) ||
1549 !uid_eq(pcred
->euid
, mycred
->euid
) ||
1550 !uid_eq(pcred
->suid
, mycred
->euid
) ||
1551 !gid_eq(pcred
->gid
, mycred
->egid
) ||
1552 !gid_eq(pcred
->egid
, mycred
->egid
) ||
1553 !gid_eq(pcred
->sgid
, mycred
->egid
))
1556 /* the keyrings must have the same UID */
1557 if ((pcred
->session_keyring
&&
1558 !uid_eq(pcred
->session_keyring
->uid
, mycred
->euid
)) ||
1559 !uid_eq(mycred
->session_keyring
->uid
, mycred
->euid
))
1562 /* cancel an already pending keyring replacement */
1563 oldwork
= task_work_cancel(parent
, key_change_session_keyring
);
1565 /* the replacement session keyring is applied just prior to userspace
1567 ret
= task_work_add(parent
, newwork
, true);
1571 write_unlock_irq(&tasklist_lock
);
1574 put_cred(container_of(oldwork
, struct cred
, rcu
));
1580 key_ref_put(keyring_r
);
1585 * The key control system call
1587 SYSCALL_DEFINE5(keyctl
, int, option
, unsigned long, arg2
, unsigned long, arg3
,
1588 unsigned long, arg4
, unsigned long, arg5
)
1591 case KEYCTL_GET_KEYRING_ID
:
1592 return keyctl_get_keyring_ID((key_serial_t
) arg2
,
1595 case KEYCTL_JOIN_SESSION_KEYRING
:
1596 return keyctl_join_session_keyring((const char __user
*) arg2
);
1599 return keyctl_update_key((key_serial_t
) arg2
,
1600 (const void __user
*) arg3
,
1604 return keyctl_revoke_key((key_serial_t
) arg2
);
1606 case KEYCTL_DESCRIBE
:
1607 return keyctl_describe_key((key_serial_t
) arg2
,
1608 (char __user
*) arg3
,
1612 return keyctl_keyring_clear((key_serial_t
) arg2
);
1615 return keyctl_keyring_link((key_serial_t
) arg2
,
1616 (key_serial_t
) arg3
);
1619 return keyctl_keyring_unlink((key_serial_t
) arg2
,
1620 (key_serial_t
) arg3
);
1623 return keyctl_keyring_search((key_serial_t
) arg2
,
1624 (const char __user
*) arg3
,
1625 (const char __user
*) arg4
,
1626 (key_serial_t
) arg5
);
1629 return keyctl_read_key((key_serial_t
) arg2
,
1630 (char __user
*) arg3
,
1634 return keyctl_chown_key((key_serial_t
) arg2
,
1638 case KEYCTL_SETPERM
:
1639 return keyctl_setperm_key((key_serial_t
) arg2
,
1642 case KEYCTL_INSTANTIATE
:
1643 return keyctl_instantiate_key((key_serial_t
) arg2
,
1644 (const void __user
*) arg3
,
1646 (key_serial_t
) arg5
);
1649 return keyctl_negate_key((key_serial_t
) arg2
,
1651 (key_serial_t
) arg4
);
1653 case KEYCTL_SET_REQKEY_KEYRING
:
1654 return keyctl_set_reqkey_keyring(arg2
);
1656 case KEYCTL_SET_TIMEOUT
:
1657 return keyctl_set_timeout((key_serial_t
) arg2
,
1660 case KEYCTL_ASSUME_AUTHORITY
:
1661 return keyctl_assume_authority((key_serial_t
) arg2
);
1663 case KEYCTL_GET_SECURITY
:
1664 return keyctl_get_security((key_serial_t
) arg2
,
1665 (char __user
*) arg3
,
1668 case KEYCTL_SESSION_TO_PARENT
:
1669 return keyctl_session_to_parent();
1672 return keyctl_reject_key((key_serial_t
) arg2
,
1675 (key_serial_t
) arg5
);
1677 case KEYCTL_INSTANTIATE_IOV
:
1678 return keyctl_instantiate_key_iov(
1679 (key_serial_t
) arg2
,
1680 (const struct iovec __user
*) arg3
,
1682 (key_serial_t
) arg5
);
1684 case KEYCTL_INVALIDATE
:
1685 return keyctl_invalidate_key((key_serial_t
) arg2
);
1687 case KEYCTL_GET_PERSISTENT
:
1688 return keyctl_get_persistent((uid_t
)arg2
, (key_serial_t
)arg3
);