xref: /openbmc/linux/security/keys/keyctl.c (revision 8c0637e9)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2973c9f4fSDavid Howells /* Userspace key control operations
31da177e4SLinus Torvalds  *
43e30148cSDavid Howells  * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved.
51da177e4SLinus Torvalds  * Written by David Howells (dhowells@redhat.com)
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include <linux/init.h>
91da177e4SLinus Torvalds #include <linux/sched.h>
1029930025SIngo Molnar #include <linux/sched/task.h>
111da177e4SLinus Torvalds #include <linux/slab.h>
121da177e4SLinus Torvalds #include <linux/syscalls.h>
1359e6b9c1SBryan Schumaker #include <linux/key.h>
141da177e4SLinus Torvalds #include <linux/keyctl.h>
151da177e4SLinus Torvalds #include <linux/fs.h>
16c59ede7bSRandy.Dunlap #include <linux/capability.h>
175b825c3aSIngo Molnar #include <linux/cred.h>
180cb409d9SDavi Arnaut #include <linux/string.h>
191da177e4SLinus Torvalds #include <linux/err.h>
2038bbca6bSDavid Howells #include <linux/vmalloc.h>
2170a5bb72SDavid Howells #include <linux/security.h>
22a27bb332SKent Overstreet #include <linux/uio.h>
237c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
24822ad64dSDavid Howells #include <keys/request_key_auth-type.h>
251da177e4SLinus Torvalds #include "internal.h"
261da177e4SLinus Torvalds 
27aa9d4437SDavid Howells #define KEY_MAX_DESC_SIZE 4096
28aa9d4437SDavid Howells 
29b206f281SDavid Howells static const unsigned char keyrings_capabilities[2] = {
3045e0f30cSDavid Howells 	[0] = (KEYCTL_CAPS0_CAPABILITIES |
3145e0f30cSDavid Howells 	       (IS_ENABLED(CONFIG_PERSISTENT_KEYRINGS)	? KEYCTL_CAPS0_PERSISTENT_KEYRINGS : 0) |
3245e0f30cSDavid Howells 	       (IS_ENABLED(CONFIG_KEY_DH_OPERATIONS)	? KEYCTL_CAPS0_DIFFIE_HELLMAN : 0) |
3345e0f30cSDavid Howells 	       (IS_ENABLED(CONFIG_ASYMMETRIC_KEY_TYPE)	? KEYCTL_CAPS0_PUBLIC_KEY : 0) |
3445e0f30cSDavid Howells 	       (IS_ENABLED(CONFIG_BIG_KEYS)		? KEYCTL_CAPS0_BIG_KEY : 0) |
3545e0f30cSDavid Howells 	       KEYCTL_CAPS0_INVALIDATE |
3645e0f30cSDavid Howells 	       KEYCTL_CAPS0_RESTRICT_KEYRING |
3745e0f30cSDavid Howells 	       KEYCTL_CAPS0_MOVE
3845e0f30cSDavid Howells 	       ),
393b6e4de0SDavid Howells 	[1] = (KEYCTL_CAPS1_NS_KEYRING_NAME |
40f7e47677SDavid Howells 	       KEYCTL_CAPS1_NS_KEY_TAG |
41f7e47677SDavid Howells 	       (IS_ENABLED(CONFIG_KEY_NOTIFICATIONS)	? KEYCTL_CAPS1_NOTIFICATIONS : 0)
42f7e47677SDavid Howells 	       ),
4345e0f30cSDavid Howells };
4445e0f30cSDavid Howells 
450cb409d9SDavi Arnaut static int key_get_type_from_user(char *type,
460cb409d9SDavi Arnaut 				  const char __user *_type,
470cb409d9SDavi Arnaut 				  unsigned len)
480cb409d9SDavi Arnaut {
490cb409d9SDavi Arnaut 	int ret;
500cb409d9SDavi Arnaut 
510cb409d9SDavi Arnaut 	ret = strncpy_from_user(type, _type, len);
520cb409d9SDavi Arnaut 	if (ret < 0)
534303ef19SDan Carpenter 		return ret;
540cb409d9SDavi Arnaut 	if (ret == 0 || ret >= len)
550cb409d9SDavi Arnaut 		return -EINVAL;
5654e2c2c1SDavid Howells 	if (type[0] == '.')
5754e2c2c1SDavid Howells 		return -EPERM;
580cb409d9SDavi Arnaut 	type[len - 1] = '\0';
590cb409d9SDavi Arnaut 	return 0;
600cb409d9SDavi Arnaut }
610cb409d9SDavi Arnaut 
621da177e4SLinus Torvalds /*
63973c9f4fSDavid Howells  * Extract the description of a new key from userspace and either add it as a
64973c9f4fSDavid Howells  * new key to the specified keyring or update a matching key in that keyring.
65973c9f4fSDavid Howells  *
66cf7f601cSDavid Howells  * If the description is NULL or an empty string, the key type is asked to
67cf7f601cSDavid Howells  * generate one from the payload.
68cf7f601cSDavid Howells  *
69973c9f4fSDavid Howells  * The keyring must be writable so that we can attach the key to it.
70973c9f4fSDavid Howells  *
71973c9f4fSDavid Howells  * If successful, the new key's serial number is returned, otherwise an error
72973c9f4fSDavid Howells  * code is returned.
731da177e4SLinus Torvalds  */
741e7bfb21SHeiko Carstens SYSCALL_DEFINE5(add_key, const char __user *, _type,
751e7bfb21SHeiko Carstens 		const char __user *, _description,
761e7bfb21SHeiko Carstens 		const void __user *, _payload,
771e7bfb21SHeiko Carstens 		size_t, plen,
781e7bfb21SHeiko Carstens 		key_serial_t, ringid)
791da177e4SLinus Torvalds {
80664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref;
811da177e4SLinus Torvalds 	char type[32], *description;
821da177e4SLinus Torvalds 	void *payload;
830cb409d9SDavi Arnaut 	long ret;
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 	ret = -EINVAL;
8638bbca6bSDavid Howells 	if (plen > 1024 * 1024 - 1)
871da177e4SLinus Torvalds 		goto error;
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 	/* draw all the data into kernel space */
900cb409d9SDavi Arnaut 	ret = key_get_type_from_user(type, _type, sizeof(type));
911da177e4SLinus Torvalds 	if (ret < 0)
921da177e4SLinus Torvalds 		goto error;
931da177e4SLinus Torvalds 
94cf7f601cSDavid Howells 	description = NULL;
95cf7f601cSDavid Howells 	if (_description) {
96aa9d4437SDavid Howells 		description = strndup_user(_description, KEY_MAX_DESC_SIZE);
970cb409d9SDavi Arnaut 		if (IS_ERR(description)) {
980cb409d9SDavi Arnaut 			ret = PTR_ERR(description);
993e30148cSDavid Howells 			goto error;
1000cb409d9SDavi Arnaut 		}
101cf7f601cSDavid Howells 		if (!*description) {
102cf7f601cSDavid Howells 			kfree(description);
103cf7f601cSDavid Howells 			description = NULL;
104a4e3b8d7SMimi Zohar 		} else if ((description[0] == '.') &&
105a4e3b8d7SMimi Zohar 			   (strncmp(type, "keyring", 7) == 0)) {
106a4e3b8d7SMimi Zohar 			ret = -EPERM;
107a4e3b8d7SMimi Zohar 			goto error2;
108cf7f601cSDavid Howells 		}
109cf7f601cSDavid Howells 	}
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
1121da177e4SLinus Torvalds 	payload = NULL;
1131da177e4SLinus Torvalds 
1145649645dSEric Biggers 	if (plen) {
1151da177e4SLinus Torvalds 		ret = -ENOMEM;
116752ade68SMichal Hocko 		payload = kvmalloc(plen, GFP_KERNEL);
1171da177e4SLinus Torvalds 		if (!payload)
1181da177e4SLinus Torvalds 			goto error2;
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 		ret = -EFAULT;
1211da177e4SLinus Torvalds 		if (copy_from_user(payload, _payload, plen) != 0)
1221da177e4SLinus Torvalds 			goto error3;
1231da177e4SLinus Torvalds 	}
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	/* find the target keyring (which must be writable) */
126f5895943SDavid Howells 	keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
127664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
128664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
1291da177e4SLinus Torvalds 		goto error3;
1301da177e4SLinus Torvalds 	}
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	/* create or update the requested key and add it to the target
1331da177e4SLinus Torvalds 	 * keyring */
134664cceb0SDavid Howells 	key_ref = key_create_or_update(keyring_ref, type, description,
135028db3e2SLinus Torvalds 				       payload, plen, KEY_PERM_UNDEF,
136028db3e2SLinus Torvalds 				       KEY_ALLOC_IN_QUOTA);
137664cceb0SDavid Howells 	if (!IS_ERR(key_ref)) {
138664cceb0SDavid Howells 		ret = key_ref_to_ptr(key_ref)->serial;
139664cceb0SDavid Howells 		key_ref_put(key_ref);
1401da177e4SLinus Torvalds 	}
1411da177e4SLinus Torvalds 	else {
142664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
1431da177e4SLinus Torvalds 	}
1441da177e4SLinus Torvalds 
145664cceb0SDavid Howells 	key_ref_put(keyring_ref);
1461da177e4SLinus Torvalds  error3:
14757070c85SEric Biggers 	if (payload) {
14857070c85SEric Biggers 		memzero_explicit(payload, plen);
149d0e0eba0SGeliang Tang 		kvfree(payload);
15057070c85SEric Biggers 	}
1511da177e4SLinus Torvalds  error2:
1521da177e4SLinus Torvalds 	kfree(description);
1531da177e4SLinus Torvalds  error:
1541da177e4SLinus Torvalds 	return ret;
155a8b17ed0SDavid Howells }
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds /*
158973c9f4fSDavid Howells  * Search the process keyrings and keyring trees linked from those for a
159973c9f4fSDavid Howells  * matching key.  Keyrings must have appropriate Search permission to be
160973c9f4fSDavid Howells  * searched.
161973c9f4fSDavid Howells  *
162973c9f4fSDavid Howells  * If a key is found, it will be attached to the destination keyring if there's
163973c9f4fSDavid Howells  * one specified and the serial number of the key will be returned.
164973c9f4fSDavid Howells  *
165973c9f4fSDavid Howells  * If no key is found, /sbin/request-key will be invoked if _callout_info is
166973c9f4fSDavid Howells  * non-NULL in an attempt to create a key.  The _callout_info string will be
167973c9f4fSDavid Howells  * passed to /sbin/request-key to aid with completing the request.  If the
168973c9f4fSDavid Howells  * _callout_info string is "" then it will be changed to "-".
1691da177e4SLinus Torvalds  */
1701e7bfb21SHeiko Carstens SYSCALL_DEFINE4(request_key, const char __user *, _type,
1711e7bfb21SHeiko Carstens 		const char __user *, _description,
1721e7bfb21SHeiko Carstens 		const char __user *, _callout_info,
1731e7bfb21SHeiko Carstens 		key_serial_t, destringid)
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	struct key_type *ktype;
176664cceb0SDavid Howells 	struct key *key;
177664cceb0SDavid Howells 	key_ref_t dest_ref;
1784a38e122SDavid Howells 	size_t callout_len;
1791da177e4SLinus Torvalds 	char type[32], *description, *callout_info;
1800cb409d9SDavi Arnaut 	long ret;
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	/* pull the type into kernel space */
1830cb409d9SDavi Arnaut 	ret = key_get_type_from_user(type, _type, sizeof(type));
1841da177e4SLinus Torvalds 	if (ret < 0)
1851da177e4SLinus Torvalds 		goto error;
1861260f801SDavid Howells 
1871da177e4SLinus Torvalds 	/* pull the description into kernel space */
188aa9d4437SDavid Howells 	description = strndup_user(_description, KEY_MAX_DESC_SIZE);
1890cb409d9SDavi Arnaut 	if (IS_ERR(description)) {
1900cb409d9SDavi Arnaut 		ret = PTR_ERR(description);
1911da177e4SLinus Torvalds 		goto error;
1920cb409d9SDavi Arnaut 	}
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	/* pull the callout info into kernel space */
1951da177e4SLinus Torvalds 	callout_info = NULL;
1964a38e122SDavid Howells 	callout_len = 0;
1971da177e4SLinus Torvalds 	if (_callout_info) {
1980cb409d9SDavi Arnaut 		callout_info = strndup_user(_callout_info, PAGE_SIZE);
1990cb409d9SDavi Arnaut 		if (IS_ERR(callout_info)) {
2000cb409d9SDavi Arnaut 			ret = PTR_ERR(callout_info);
2011da177e4SLinus Torvalds 			goto error2;
2020cb409d9SDavi Arnaut 		}
2034a38e122SDavid Howells 		callout_len = strlen(callout_info);
2041da177e4SLinus Torvalds 	}
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	/* get the destination keyring if specified */
207664cceb0SDavid Howells 	dest_ref = NULL;
2081da177e4SLinus Torvalds 	if (destringid) {
2095593122eSDavid Howells 		dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
210f5895943SDavid Howells 					   KEY_NEED_WRITE);
211664cceb0SDavid Howells 		if (IS_ERR(dest_ref)) {
212664cceb0SDavid Howells 			ret = PTR_ERR(dest_ref);
2131da177e4SLinus Torvalds 			goto error3;
2141da177e4SLinus Torvalds 		}
2151da177e4SLinus Torvalds 	}
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	/* find the key type */
2181da177e4SLinus Torvalds 	ktype = key_type_lookup(type);
2191da177e4SLinus Torvalds 	if (IS_ERR(ktype)) {
2201da177e4SLinus Torvalds 		ret = PTR_ERR(ktype);
2211da177e4SLinus Torvalds 		goto error4;
2221da177e4SLinus Torvalds 	}
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	/* do the search */
225a58946c1SDavid Howells 	key = request_key_and_link(ktype, description, NULL, callout_info,
226028db3e2SLinus Torvalds 				   callout_len, NULL, key_ref_to_ptr(dest_ref),
2277e047ef5SDavid Howells 				   KEY_ALLOC_IN_QUOTA);
2281da177e4SLinus Torvalds 	if (IS_ERR(key)) {
2291da177e4SLinus Torvalds 		ret = PTR_ERR(key);
2301da177e4SLinus Torvalds 		goto error5;
2311da177e4SLinus Torvalds 	}
2321da177e4SLinus Torvalds 
2334aab1e89SDavid Howells 	/* wait for the key to finish being constructed */
2344aab1e89SDavid Howells 	ret = wait_for_key_construction(key, 1);
2354aab1e89SDavid Howells 	if (ret < 0)
2364aab1e89SDavid Howells 		goto error6;
2374aab1e89SDavid Howells 
2381da177e4SLinus Torvalds 	ret = key->serial;
2391da177e4SLinus Torvalds 
2404aab1e89SDavid Howells error6:
2411da177e4SLinus Torvalds  	key_put(key);
2421da177e4SLinus Torvalds error5:
2431da177e4SLinus Torvalds 	key_type_put(ktype);
2441da177e4SLinus Torvalds error4:
245664cceb0SDavid Howells 	key_ref_put(dest_ref);
2461da177e4SLinus Torvalds error3:
2471da177e4SLinus Torvalds 	kfree(callout_info);
2481da177e4SLinus Torvalds error2:
2491da177e4SLinus Torvalds 	kfree(description);
2501da177e4SLinus Torvalds error:
2511da177e4SLinus Torvalds 	return ret;
252a8b17ed0SDavid Howells }
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds /*
255973c9f4fSDavid Howells  * Get the ID of the specified process keyring.
256973c9f4fSDavid Howells  *
257973c9f4fSDavid Howells  * The requested keyring must have search permission to be found.
258973c9f4fSDavid Howells  *
259973c9f4fSDavid Howells  * If successful, the ID of the requested keyring will be returned.
2601da177e4SLinus Torvalds  */
2611da177e4SLinus Torvalds long keyctl_get_keyring_ID(key_serial_t id, int create)
2621da177e4SLinus Torvalds {
263664cceb0SDavid Howells 	key_ref_t key_ref;
2645593122eSDavid Howells 	unsigned long lflags;
2651da177e4SLinus Torvalds 	long ret;
2661da177e4SLinus Torvalds 
2675593122eSDavid Howells 	lflags = create ? KEY_LOOKUP_CREATE : 0;
268f5895943SDavid Howells 	key_ref = lookup_user_key(id, lflags, KEY_NEED_SEARCH);
269664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
270664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
2711da177e4SLinus Torvalds 		goto error;
2721da177e4SLinus Torvalds 	}
2731da177e4SLinus Torvalds 
274664cceb0SDavid Howells 	ret = key_ref_to_ptr(key_ref)->serial;
275664cceb0SDavid Howells 	key_ref_put(key_ref);
2761da177e4SLinus Torvalds error:
2771da177e4SLinus Torvalds 	return ret;
278973c9f4fSDavid Howells }
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds /*
281973c9f4fSDavid Howells  * Join a (named) session keyring.
282973c9f4fSDavid Howells  *
283973c9f4fSDavid Howells  * Create and join an anonymous session keyring or join a named session
284973c9f4fSDavid Howells  * keyring, creating it if necessary.  A named session keyring must have Search
285973c9f4fSDavid Howells  * permission for it to be joined.  Session keyrings without this permit will
286ee8f844eSDavid Howells  * be skipped over.  It is not permitted for userspace to create or join
287ee8f844eSDavid Howells  * keyrings whose name begin with a dot.
288973c9f4fSDavid Howells  *
289973c9f4fSDavid Howells  * If successful, the ID of the joined session keyring will be returned.
2901da177e4SLinus Torvalds  */
2911da177e4SLinus Torvalds long keyctl_join_session_keyring(const char __user *_name)
2921da177e4SLinus Torvalds {
2931da177e4SLinus Torvalds 	char *name;
2940cb409d9SDavi Arnaut 	long ret;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	/* fetch the name from userspace */
2971da177e4SLinus Torvalds 	name = NULL;
2981da177e4SLinus Torvalds 	if (_name) {
299aa9d4437SDavid Howells 		name = strndup_user(_name, KEY_MAX_DESC_SIZE);
3000cb409d9SDavi Arnaut 		if (IS_ERR(name)) {
3010cb409d9SDavi Arnaut 			ret = PTR_ERR(name);
3021da177e4SLinus Torvalds 			goto error;
3030cb409d9SDavi Arnaut 		}
304ee8f844eSDavid Howells 
305ee8f844eSDavid Howells 		ret = -EPERM;
306ee8f844eSDavid Howells 		if (name[0] == '.')
307ee8f844eSDavid Howells 			goto error_name;
3081da177e4SLinus Torvalds 	}
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	/* join the session */
3111da177e4SLinus Torvalds 	ret = join_session_keyring(name);
312ee8f844eSDavid Howells error_name:
3130d54ee1cSVegard Nossum 	kfree(name);
3141da177e4SLinus Torvalds error:
3151da177e4SLinus Torvalds 	return ret;
316a8b17ed0SDavid Howells }
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds /*
319973c9f4fSDavid Howells  * Update a key's data payload from the given data.
320973c9f4fSDavid Howells  *
321973c9f4fSDavid Howells  * The key must grant the caller Write permission and the key type must support
322973c9f4fSDavid Howells  * updating for this to work.  A negative key can be positively instantiated
323973c9f4fSDavid Howells  * with this call.
324973c9f4fSDavid Howells  *
325973c9f4fSDavid Howells  * If successful, 0 will be returned.  If the key type does not support
326973c9f4fSDavid Howells  * updating, then -EOPNOTSUPP will be returned.
3271da177e4SLinus Torvalds  */
3281da177e4SLinus Torvalds long keyctl_update_key(key_serial_t id,
3291da177e4SLinus Torvalds 		       const void __user *_payload,
3301da177e4SLinus Torvalds 		       size_t plen)
3311da177e4SLinus Torvalds {
332664cceb0SDavid Howells 	key_ref_t key_ref;
3331da177e4SLinus Torvalds 	void *payload;
3341da177e4SLinus Torvalds 	long ret;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	ret = -EINVAL;
3371da177e4SLinus Torvalds 	if (plen > PAGE_SIZE)
3381da177e4SLinus Torvalds 		goto error;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
3411da177e4SLinus Torvalds 	payload = NULL;
3425649645dSEric Biggers 	if (plen) {
3431da177e4SLinus Torvalds 		ret = -ENOMEM;
3444f088249SWaiman Long 		payload = kvmalloc(plen, GFP_KERNEL);
3451da177e4SLinus Torvalds 		if (!payload)
3461da177e4SLinus Torvalds 			goto error;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 		ret = -EFAULT;
3491da177e4SLinus Torvalds 		if (copy_from_user(payload, _payload, plen) != 0)
3501da177e4SLinus Torvalds 			goto error2;
3511da177e4SLinus Torvalds 	}
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	/* find the target key (which must be writable) */
354f5895943SDavid Howells 	key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
355664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
356664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
3571da177e4SLinus Torvalds 		goto error2;
3581da177e4SLinus Torvalds 	}
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	/* update the key */
361664cceb0SDavid Howells 	ret = key_update(key_ref, payload, plen);
3621da177e4SLinus Torvalds 
363664cceb0SDavid Howells 	key_ref_put(key_ref);
3641da177e4SLinus Torvalds error2:
3654f088249SWaiman Long 	__kvzfree(payload, plen);
3661da177e4SLinus Torvalds error:
3671da177e4SLinus Torvalds 	return ret;
368a8b17ed0SDavid Howells }
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds /*
371973c9f4fSDavid Howells  * Revoke a key.
372973c9f4fSDavid Howells  *
373973c9f4fSDavid Howells  * The key must be grant the caller Write or Setattr permission for this to
374973c9f4fSDavid Howells  * work.  The key type should give up its quota claim when revoked.  The key
375973c9f4fSDavid Howells  * and any links to the key will be automatically garbage collected after a
376973c9f4fSDavid Howells  * certain amount of time (/proc/sys/kernel/keys/gc_delay).
377973c9f4fSDavid Howells  *
378d3600bcfSMimi Zohar  * Keys with KEY_FLAG_KEEP set should not be revoked.
379d3600bcfSMimi Zohar  *
380973c9f4fSDavid Howells  * If successful, 0 is returned.
3811da177e4SLinus Torvalds  */
3821da177e4SLinus Torvalds long keyctl_revoke_key(key_serial_t id)
3831da177e4SLinus Torvalds {
384664cceb0SDavid Howells 	key_ref_t key_ref;
385d3600bcfSMimi Zohar 	struct key *key;
3861da177e4SLinus Torvalds 	long ret;
3871da177e4SLinus Torvalds 
388028db3e2SLinus Torvalds 	key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
389028db3e2SLinus Torvalds 	if (IS_ERR(key_ref)) {
390028db3e2SLinus Torvalds 		ret = PTR_ERR(key_ref);
391028db3e2SLinus Torvalds 		if (ret != -EACCES)
392028db3e2SLinus Torvalds 			goto error;
393028db3e2SLinus Torvalds 		key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
3940c2c9a3fSDavid Howells 		if (IS_ERR(key_ref)) {
3950c2c9a3fSDavid Howells 			ret = PTR_ERR(key_ref);
3960c2c9a3fSDavid Howells 			goto error;
3970c2c9a3fSDavid Howells 		}
398028db3e2SLinus Torvalds 	}
3991da177e4SLinus Torvalds 
400d3600bcfSMimi Zohar 	key = key_ref_to_ptr(key_ref);
4011da177e4SLinus Torvalds 	ret = 0;
4021d6d167cSMimi Zohar 	if (test_bit(KEY_FLAG_KEEP, &key->flags))
4031d6d167cSMimi Zohar 		ret = -EPERM;
4041d6d167cSMimi Zohar 	else
4051d6d167cSMimi Zohar 		key_revoke(key);
4061da177e4SLinus Torvalds 
407664cceb0SDavid Howells 	key_ref_put(key_ref);
4081da177e4SLinus Torvalds error:
4091260f801SDavid Howells 	return ret;
410a8b17ed0SDavid Howells }
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds /*
413fd75815fSDavid Howells  * Invalidate a key.
414fd75815fSDavid Howells  *
415fd75815fSDavid Howells  * The key must be grant the caller Invalidate permission for this to work.
416fd75815fSDavid Howells  * The key and any links to the key will be automatically garbage collected
417fd75815fSDavid Howells  * immediately.
418fd75815fSDavid Howells  *
419d3600bcfSMimi Zohar  * Keys with KEY_FLAG_KEEP set should not be invalidated.
420d3600bcfSMimi Zohar  *
421fd75815fSDavid Howells  * If successful, 0 is returned.
422fd75815fSDavid Howells  */
423fd75815fSDavid Howells long keyctl_invalidate_key(key_serial_t id)
424fd75815fSDavid Howells {
425fd75815fSDavid Howells 	key_ref_t key_ref;
426d3600bcfSMimi Zohar 	struct key *key;
427fd75815fSDavid Howells 	long ret;
428fd75815fSDavid Howells 
429fd75815fSDavid Howells 	kenter("%d", id);
430fd75815fSDavid Howells 
431028db3e2SLinus Torvalds 	key_ref = lookup_user_key(id, 0, KEY_NEED_SEARCH);
432fd75815fSDavid Howells 	if (IS_ERR(key_ref)) {
433fd75815fSDavid Howells 		ret = PTR_ERR(key_ref);
4340c7774abSDavid Howells 
4350c7774abSDavid Howells 		/* Root is permitted to invalidate certain special keys */
4360c7774abSDavid Howells 		if (capable(CAP_SYS_ADMIN)) {
437*8c0637e9SDavid Howells 			key_ref = lookup_user_key(id, 0, KEY_SYSADMIN_OVERRIDE);
4380c7774abSDavid Howells 			if (IS_ERR(key_ref))
4390c7774abSDavid Howells 				goto error;
4400c7774abSDavid Howells 			if (test_bit(KEY_FLAG_ROOT_CAN_INVAL,
4410c7774abSDavid Howells 				     &key_ref_to_ptr(key_ref)->flags))
4420c7774abSDavid Howells 				goto invalidate;
4430c7774abSDavid Howells 			goto error_put;
4440c7774abSDavid Howells 		}
4450c7774abSDavid Howells 
446fd75815fSDavid Howells 		goto error;
447fd75815fSDavid Howells 	}
448fd75815fSDavid Howells 
4490c7774abSDavid Howells invalidate:
450d3600bcfSMimi Zohar 	key = key_ref_to_ptr(key_ref);
451fd75815fSDavid Howells 	ret = 0;
452d3600bcfSMimi Zohar 	if (test_bit(KEY_FLAG_KEEP, &key->flags))
453d3600bcfSMimi Zohar 		ret = -EPERM;
4541d6d167cSMimi Zohar 	else
455d3600bcfSMimi Zohar 		key_invalidate(key);
4560c7774abSDavid Howells error_put:
457fd75815fSDavid Howells 	key_ref_put(key_ref);
458fd75815fSDavid Howells error:
459fd75815fSDavid Howells 	kleave(" = %ld", ret);
460fd75815fSDavid Howells 	return ret;
461fd75815fSDavid Howells }
462fd75815fSDavid Howells 
463fd75815fSDavid Howells /*
464973c9f4fSDavid Howells  * Clear the specified keyring, creating an empty process keyring if one of the
465973c9f4fSDavid Howells  * special keyring IDs is used.
466973c9f4fSDavid Howells  *
467d3600bcfSMimi Zohar  * The keyring must grant the caller Write permission and not have
468d3600bcfSMimi Zohar  * KEY_FLAG_KEEP set for this to work.  If successful, 0 will be returned.
4691da177e4SLinus Torvalds  */
4701da177e4SLinus Torvalds long keyctl_keyring_clear(key_serial_t ringid)
4711da177e4SLinus Torvalds {
472664cceb0SDavid Howells 	key_ref_t keyring_ref;
473d3600bcfSMimi Zohar 	struct key *keyring;
4741da177e4SLinus Torvalds 	long ret;
4751da177e4SLinus Torvalds 
476028db3e2SLinus Torvalds 	keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
477664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
478664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
479700920ebSDavid Howells 
480700920ebSDavid Howells 		/* Root is permitted to invalidate certain special keyrings */
481700920ebSDavid Howells 		if (capable(CAP_SYS_ADMIN)) {
482*8c0637e9SDavid Howells 			keyring_ref = lookup_user_key(ringid, 0,
483*8c0637e9SDavid Howells 						      KEY_SYSADMIN_OVERRIDE);
484700920ebSDavid Howells 			if (IS_ERR(keyring_ref))
485700920ebSDavid Howells 				goto error;
486700920ebSDavid Howells 			if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
487700920ebSDavid Howells 				     &key_ref_to_ptr(keyring_ref)->flags))
488700920ebSDavid Howells 				goto clear;
489700920ebSDavid Howells 			goto error_put;
490700920ebSDavid Howells 		}
491700920ebSDavid Howells 
4921da177e4SLinus Torvalds 		goto error;
4931da177e4SLinus Torvalds 	}
4941da177e4SLinus Torvalds 
495700920ebSDavid Howells clear:
496d3600bcfSMimi Zohar 	keyring = key_ref_to_ptr(keyring_ref);
497d3600bcfSMimi Zohar 	if (test_bit(KEY_FLAG_KEEP, &keyring->flags))
498d3600bcfSMimi Zohar 		ret = -EPERM;
499d3600bcfSMimi Zohar 	else
500d3600bcfSMimi Zohar 		ret = keyring_clear(keyring);
501700920ebSDavid Howells error_put:
502664cceb0SDavid Howells 	key_ref_put(keyring_ref);
5031da177e4SLinus Torvalds error:
5041da177e4SLinus Torvalds 	return ret;
505a8b17ed0SDavid Howells }
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds /*
508973c9f4fSDavid Howells  * Create a link from a keyring to a key if there's no matching key in the
509973c9f4fSDavid Howells  * keyring, otherwise replace the link to the matching key with a link to the
510973c9f4fSDavid Howells  * new key.
511973c9f4fSDavid Howells  *
512973c9f4fSDavid Howells  * The key must grant the caller Link permission and the the keyring must grant
513973c9f4fSDavid Howells  * the caller Write permission.  Furthermore, if an additional link is created,
514973c9f4fSDavid Howells  * the keyring's quota will be extended.
515973c9f4fSDavid Howells  *
516973c9f4fSDavid Howells  * If successful, 0 will be returned.
5171da177e4SLinus Torvalds  */
5181da177e4SLinus Torvalds long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
5191da177e4SLinus Torvalds {
520664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref;
5211da177e4SLinus Torvalds 	long ret;
5221da177e4SLinus Torvalds 
523f5895943SDavid Howells 	keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
524664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
525664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
5261da177e4SLinus Torvalds 		goto error;
5271da177e4SLinus Torvalds 	}
5281da177e4SLinus Torvalds 
529f5895943SDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
530664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
531664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
5321da177e4SLinus Torvalds 		goto error2;
5331da177e4SLinus Torvalds 	}
5341da177e4SLinus Torvalds 
535664cceb0SDavid Howells 	ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
5361da177e4SLinus Torvalds 
537664cceb0SDavid Howells 	key_ref_put(key_ref);
5381da177e4SLinus Torvalds error2:
539664cceb0SDavid Howells 	key_ref_put(keyring_ref);
5401da177e4SLinus Torvalds error:
5411da177e4SLinus Torvalds 	return ret;
542a8b17ed0SDavid Howells }
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds /*
545973c9f4fSDavid Howells  * Unlink a key from a keyring.
546973c9f4fSDavid Howells  *
547973c9f4fSDavid Howells  * The keyring must grant the caller Write permission for this to work; the key
548973c9f4fSDavid Howells  * itself need not grant the caller anything.  If the last link to a key is
549973c9f4fSDavid Howells  * removed then that key will be scheduled for destruction.
550973c9f4fSDavid Howells  *
551d3600bcfSMimi Zohar  * Keys or keyrings with KEY_FLAG_KEEP set should not be unlinked.
552d3600bcfSMimi Zohar  *
553973c9f4fSDavid Howells  * If successful, 0 will be returned.
5541da177e4SLinus Torvalds  */
5551da177e4SLinus Torvalds long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
5561da177e4SLinus Torvalds {
557664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref;
558d3600bcfSMimi Zohar 	struct key *keyring, *key;
5591da177e4SLinus Torvalds 	long ret;
5601da177e4SLinus Torvalds 
561f5895943SDavid Howells 	keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE);
562664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
563664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
5641da177e4SLinus Torvalds 		goto error;
5651da177e4SLinus Torvalds 	}
5661da177e4SLinus Torvalds 
567*8c0637e9SDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_PARTIAL, KEY_NEED_UNLINK);
568664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
569664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
5701da177e4SLinus Torvalds 		goto error2;
5711da177e4SLinus Torvalds 	}
5721da177e4SLinus Torvalds 
573d3600bcfSMimi Zohar 	keyring = key_ref_to_ptr(keyring_ref);
574d3600bcfSMimi Zohar 	key = key_ref_to_ptr(key_ref);
575d3600bcfSMimi Zohar 	if (test_bit(KEY_FLAG_KEEP, &keyring->flags) &&
576d3600bcfSMimi Zohar 	    test_bit(KEY_FLAG_KEEP, &key->flags))
577d3600bcfSMimi Zohar 		ret = -EPERM;
578d3600bcfSMimi Zohar 	else
579d3600bcfSMimi Zohar 		ret = key_unlink(keyring, key);
5801da177e4SLinus Torvalds 
581664cceb0SDavid Howells 	key_ref_put(key_ref);
5821da177e4SLinus Torvalds error2:
583664cceb0SDavid Howells 	key_ref_put(keyring_ref);
5841da177e4SLinus Torvalds error:
5851da177e4SLinus Torvalds 	return ret;
586a8b17ed0SDavid Howells }
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds /*
589ed0ac5c7SDavid Howells  * Move a link to a key from one keyring to another, displacing any matching
590ed0ac5c7SDavid Howells  * key from the destination keyring.
591ed0ac5c7SDavid Howells  *
592ed0ac5c7SDavid Howells  * The key must grant the caller Link permission and both keyrings must grant
593ed0ac5c7SDavid Howells  * the caller Write permission.  There must also be a link in the from keyring
594ed0ac5c7SDavid Howells  * to the key.  If both keyrings are the same, nothing is done.
595ed0ac5c7SDavid Howells  *
596ed0ac5c7SDavid Howells  * If successful, 0 will be returned.
597ed0ac5c7SDavid Howells  */
598ed0ac5c7SDavid Howells long keyctl_keyring_move(key_serial_t id, key_serial_t from_ringid,
599ed0ac5c7SDavid Howells 			 key_serial_t to_ringid, unsigned int flags)
600ed0ac5c7SDavid Howells {
601ed0ac5c7SDavid Howells 	key_ref_t key_ref, from_ref, to_ref;
602ed0ac5c7SDavid Howells 	long ret;
603ed0ac5c7SDavid Howells 
604ed0ac5c7SDavid Howells 	if (flags & ~KEYCTL_MOVE_EXCL)
605ed0ac5c7SDavid Howells 		return -EINVAL;
606ed0ac5c7SDavid Howells 
607ed0ac5c7SDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
608ed0ac5c7SDavid Howells 	if (IS_ERR(key_ref))
609ed0ac5c7SDavid Howells 		return PTR_ERR(key_ref);
610ed0ac5c7SDavid Howells 
611ed0ac5c7SDavid Howells 	from_ref = lookup_user_key(from_ringid, 0, KEY_NEED_WRITE);
612ed0ac5c7SDavid Howells 	if (IS_ERR(from_ref)) {
613ed0ac5c7SDavid Howells 		ret = PTR_ERR(from_ref);
614ed0ac5c7SDavid Howells 		goto error2;
615ed0ac5c7SDavid Howells 	}
616ed0ac5c7SDavid Howells 
617ed0ac5c7SDavid Howells 	to_ref = lookup_user_key(to_ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
618ed0ac5c7SDavid Howells 	if (IS_ERR(to_ref)) {
619ed0ac5c7SDavid Howells 		ret = PTR_ERR(to_ref);
620ed0ac5c7SDavid Howells 		goto error3;
621ed0ac5c7SDavid Howells 	}
622ed0ac5c7SDavid Howells 
623ed0ac5c7SDavid Howells 	ret = key_move(key_ref_to_ptr(key_ref), key_ref_to_ptr(from_ref),
624ed0ac5c7SDavid Howells 		       key_ref_to_ptr(to_ref), flags);
625ed0ac5c7SDavid Howells 
626ed0ac5c7SDavid Howells 	key_ref_put(to_ref);
627ed0ac5c7SDavid Howells error3:
628ed0ac5c7SDavid Howells 	key_ref_put(from_ref);
629ed0ac5c7SDavid Howells error2:
630ed0ac5c7SDavid Howells 	key_ref_put(key_ref);
631ed0ac5c7SDavid Howells 	return ret;
632ed0ac5c7SDavid Howells }
633ed0ac5c7SDavid Howells 
634ed0ac5c7SDavid Howells /*
635973c9f4fSDavid Howells  * Return a description of a key to userspace.
636973c9f4fSDavid Howells  *
637973c9f4fSDavid Howells  * The key must grant the caller View permission for this to work.
638973c9f4fSDavid Howells  *
639973c9f4fSDavid Howells  * If there's a buffer, we place up to buflen bytes of data into it formatted
640973c9f4fSDavid Howells  * in the following way:
641973c9f4fSDavid Howells  *
6421da177e4SLinus Torvalds  *	type;uid;gid;perm;description<NUL>
643973c9f4fSDavid Howells  *
644973c9f4fSDavid Howells  * If successful, we return the amount of description available, irrespective
645973c9f4fSDavid Howells  * of how much we may have copied into the buffer.
6461da177e4SLinus Torvalds  */
6471da177e4SLinus Torvalds long keyctl_describe_key(key_serial_t keyid,
6481da177e4SLinus Torvalds 			 char __user *buffer,
6491da177e4SLinus Torvalds 			 size_t buflen)
6501da177e4SLinus Torvalds {
6513e30148cSDavid Howells 	struct key *key, *instkey;
652664cceb0SDavid Howells 	key_ref_t key_ref;
653aa9d4437SDavid Howells 	char *infobuf;
6541da177e4SLinus Torvalds 	long ret;
655aa9d4437SDavid Howells 	int desclen, infolen;
6561da177e4SLinus Torvalds 
657f5895943SDavid Howells 	key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
658664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
6593e30148cSDavid Howells 		/* viewing a key under construction is permitted if we have the
6603e30148cSDavid Howells 		 * authorisation token handy */
661664cceb0SDavid Howells 		if (PTR_ERR(key_ref) == -EACCES) {
6623e30148cSDavid Howells 			instkey = key_get_instantiation_authkey(keyid);
6633e30148cSDavid Howells 			if (!IS_ERR(instkey)) {
6643e30148cSDavid Howells 				key_put(instkey);
6658bbf4976SDavid Howells 				key_ref = lookup_user_key(keyid,
6665593122eSDavid Howells 							  KEY_LOOKUP_PARTIAL,
667*8c0637e9SDavid Howells 							  KEY_AUTHTOKEN_OVERRIDE);
668664cceb0SDavid Howells 				if (!IS_ERR(key_ref))
6693e30148cSDavid Howells 					goto okay;
6703e30148cSDavid Howells 			}
6713e30148cSDavid Howells 		}
6723e30148cSDavid Howells 
673664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
6741da177e4SLinus Torvalds 		goto error;
6751da177e4SLinus Torvalds 	}
6761da177e4SLinus Torvalds 
6773e30148cSDavid Howells okay:
678664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
679aa9d4437SDavid Howells 	desclen = strlen(key->description);
680664cceb0SDavid Howells 
681aa9d4437SDavid Howells 	/* calculate how much information we're going to return */
682aa9d4437SDavid Howells 	ret = -ENOMEM;
683aa9d4437SDavid Howells 	infobuf = kasprintf(GFP_KERNEL,
684aa9d4437SDavid Howells 			    "%s;%d;%d;%08x;",
68594fd8405SDavid Howells 			    key->type->name,
6869a56c2dbSEric W. Biederman 			    from_kuid_munged(current_user_ns(), key->uid),
6879a56c2dbSEric W. Biederman 			    from_kgid_munged(current_user_ns(), key->gid),
688028db3e2SLinus Torvalds 			    key->perm);
689aa9d4437SDavid Howells 	if (!infobuf)
690aa9d4437SDavid Howells 		goto error2;
691aa9d4437SDavid Howells 	infolen = strlen(infobuf);
692aa9d4437SDavid Howells 	ret = infolen + desclen + 1;
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 	/* consider returning the data */
695aa9d4437SDavid Howells 	if (buffer && buflen >= ret) {
696aa9d4437SDavid Howells 		if (copy_to_user(buffer, infobuf, infolen) != 0 ||
697aa9d4437SDavid Howells 		    copy_to_user(buffer + infolen, key->description,
698aa9d4437SDavid Howells 				 desclen + 1) != 0)
6991da177e4SLinus Torvalds 			ret = -EFAULT;
7001da177e4SLinus Torvalds 	}
7011da177e4SLinus Torvalds 
702aa9d4437SDavid Howells 	kfree(infobuf);
7031da177e4SLinus Torvalds error2:
704664cceb0SDavid Howells 	key_ref_put(key_ref);
7051da177e4SLinus Torvalds error:
7061da177e4SLinus Torvalds 	return ret;
707a8b17ed0SDavid Howells }
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds /*
710973c9f4fSDavid Howells  * Search the specified keyring and any keyrings it links to for a matching
711973c9f4fSDavid Howells  * key.  Only keyrings that grant the caller Search permission will be searched
712973c9f4fSDavid Howells  * (this includes the starting keyring).  Only keys with Search permission can
713973c9f4fSDavid Howells  * be found.
714973c9f4fSDavid Howells  *
715973c9f4fSDavid Howells  * If successful, the found key will be linked to the destination keyring if
716973c9f4fSDavid Howells  * supplied and the key has Link permission, and the found key ID will be
717973c9f4fSDavid Howells  * returned.
7181da177e4SLinus Torvalds  */
7191da177e4SLinus Torvalds long keyctl_keyring_search(key_serial_t ringid,
7201da177e4SLinus Torvalds 			   const char __user *_type,
7211da177e4SLinus Torvalds 			   const char __user *_description,
7221da177e4SLinus Torvalds 			   key_serial_t destringid)
7231da177e4SLinus Torvalds {
7241da177e4SLinus Torvalds 	struct key_type *ktype;
725664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref, dest_ref;
7261da177e4SLinus Torvalds 	char type[32], *description;
7270cb409d9SDavi Arnaut 	long ret;
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	/* pull the type and description into kernel space */
7300cb409d9SDavi Arnaut 	ret = key_get_type_from_user(type, _type, sizeof(type));
7311da177e4SLinus Torvalds 	if (ret < 0)
7321da177e4SLinus Torvalds 		goto error;
7331da177e4SLinus Torvalds 
734aa9d4437SDavid Howells 	description = strndup_user(_description, KEY_MAX_DESC_SIZE);
7350cb409d9SDavi Arnaut 	if (IS_ERR(description)) {
7360cb409d9SDavi Arnaut 		ret = PTR_ERR(description);
7371da177e4SLinus Torvalds 		goto error;
7380cb409d9SDavi Arnaut 	}
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	/* get the keyring at which to begin the search */
741f5895943SDavid Howells 	keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH);
742664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
743664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
7441da177e4SLinus Torvalds 		goto error2;
7451da177e4SLinus Torvalds 	}
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	/* get the destination keyring if specified */
748664cceb0SDavid Howells 	dest_ref = NULL;
7491da177e4SLinus Torvalds 	if (destringid) {
7505593122eSDavid Howells 		dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
751f5895943SDavid Howells 					   KEY_NEED_WRITE);
752664cceb0SDavid Howells 		if (IS_ERR(dest_ref)) {
753664cceb0SDavid Howells 			ret = PTR_ERR(dest_ref);
7541da177e4SLinus Torvalds 			goto error3;
7551da177e4SLinus Torvalds 		}
7561da177e4SLinus Torvalds 	}
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 	/* find the key type */
7591da177e4SLinus Torvalds 	ktype = key_type_lookup(type);
7601da177e4SLinus Torvalds 	if (IS_ERR(ktype)) {
7611da177e4SLinus Torvalds 		ret = PTR_ERR(ktype);
7621da177e4SLinus Torvalds 		goto error4;
7631da177e4SLinus Torvalds 	}
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	/* do the search */
766dcf49dbcSDavid Howells 	key_ref = keyring_search(keyring_ref, ktype, description, true);
767664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
768664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 		/* treat lack or presence of a negative key the same */
7711da177e4SLinus Torvalds 		if (ret == -EAGAIN)
7721da177e4SLinus Torvalds 			ret = -ENOKEY;
7731da177e4SLinus Torvalds 		goto error5;
7741da177e4SLinus Torvalds 	}
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	/* link the resulting key to the destination keyring if we can */
777664cceb0SDavid Howells 	if (dest_ref) {
778f5895943SDavid Howells 		ret = key_permission(key_ref, KEY_NEED_LINK);
77929db9190SDavid Howells 		if (ret < 0)
7801da177e4SLinus Torvalds 			goto error6;
7811da177e4SLinus Torvalds 
782664cceb0SDavid Howells 		ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
7831da177e4SLinus Torvalds 		if (ret < 0)
7841da177e4SLinus Torvalds 			goto error6;
7851da177e4SLinus Torvalds 	}
7861da177e4SLinus Torvalds 
787664cceb0SDavid Howells 	ret = key_ref_to_ptr(key_ref)->serial;
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds error6:
790664cceb0SDavid Howells 	key_ref_put(key_ref);
7911da177e4SLinus Torvalds error5:
7921da177e4SLinus Torvalds 	key_type_put(ktype);
7931da177e4SLinus Torvalds error4:
794664cceb0SDavid Howells 	key_ref_put(dest_ref);
7951da177e4SLinus Torvalds error3:
796664cceb0SDavid Howells 	key_ref_put(keyring_ref);
7971da177e4SLinus Torvalds error2:
7981da177e4SLinus Torvalds 	kfree(description);
7991da177e4SLinus Torvalds error:
8001da177e4SLinus Torvalds 	return ret;
801a8b17ed0SDavid Howells }
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds /*
804d3ec10aaSWaiman Long  * Call the read method
805d3ec10aaSWaiman Long  */
806d3ec10aaSWaiman Long static long __keyctl_read_key(struct key *key, char *buffer, size_t buflen)
807d3ec10aaSWaiman Long {
808d3ec10aaSWaiman Long 	long ret;
809d3ec10aaSWaiman Long 
810d3ec10aaSWaiman Long 	down_read(&key->sem);
811d3ec10aaSWaiman Long 	ret = key_validate(key);
812d3ec10aaSWaiman Long 	if (ret == 0)
813d3ec10aaSWaiman Long 		ret = key->type->read(key, buffer, buflen);
814d3ec10aaSWaiman Long 	up_read(&key->sem);
815d3ec10aaSWaiman Long 	return ret;
816d3ec10aaSWaiman Long }
817d3ec10aaSWaiman Long 
818d3ec10aaSWaiman Long /*
819973c9f4fSDavid Howells  * Read a key's payload.
820973c9f4fSDavid Howells  *
821973c9f4fSDavid Howells  * The key must either grant the caller Read permission, or it must grant the
822973c9f4fSDavid Howells  * caller Search permission when searched for from the process keyrings.
823973c9f4fSDavid Howells  *
824973c9f4fSDavid Howells  * If successful, we place up to buflen bytes of data into the buffer, if one
825973c9f4fSDavid Howells  * is provided, and return the amount of data that is available in the key,
826973c9f4fSDavid Howells  * irrespective of how much we copied into the buffer.
8271da177e4SLinus Torvalds  */
8281da177e4SLinus Torvalds long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
8291da177e4SLinus Torvalds {
830664cceb0SDavid Howells 	struct key *key;
831664cceb0SDavid Howells 	key_ref_t key_ref;
8321da177e4SLinus Torvalds 	long ret;
8334f088249SWaiman Long 	char *key_data = NULL;
8344f088249SWaiman Long 	size_t key_data_len;
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	/* find the key first */
837*8c0637e9SDavid Howells 	key_ref = lookup_user_key(keyid, 0, KEY_DEFER_PERM_CHECK);
838664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
839664cceb0SDavid Howells 		ret = -ENOKEY;
840d3ec10aaSWaiman Long 		goto out;
841664cceb0SDavid Howells 	}
842664cceb0SDavid Howells 
843664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
844664cceb0SDavid Howells 
845363b02daSDavid Howells 	ret = key_read_state(key);
846363b02daSDavid Howells 	if (ret < 0)
847d3ec10aaSWaiman Long 		goto key_put_out; /* Negatively instantiated */
84837863c43SEric Biggers 
8491da177e4SLinus Torvalds 	/* see if we can read it directly */
850f5895943SDavid Howells 	ret = key_permission(key_ref, KEY_NEED_READ);
85129db9190SDavid Howells 	if (ret == 0)
8521da177e4SLinus Torvalds 		goto can_read_key;
85329db9190SDavid Howells 	if (ret != -EACCES)
854d3ec10aaSWaiman Long 		goto key_put_out;
8551da177e4SLinus Torvalds 
856664cceb0SDavid Howells 	/* we can't; see if it's searchable from this process's keyrings
8573e30148cSDavid Howells 	 * - we automatically take account of the fact that it may be
8583e30148cSDavid Howells 	 *   dangling off an instantiation key
8593e30148cSDavid Howells 	 */
860664cceb0SDavid Howells 	if (!is_key_possessed(key_ref)) {
8611260f801SDavid Howells 		ret = -EACCES;
862d3ec10aaSWaiman Long 		goto key_put_out;
8631da177e4SLinus Torvalds 	}
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	/* the key is probably readable - now try to read it */
8661da177e4SLinus Torvalds can_read_key:
867d3ec10aaSWaiman Long 	if (!key->type->read) {
8681da177e4SLinus Torvalds 		ret = -EOPNOTSUPP;
869d3ec10aaSWaiman Long 		goto key_put_out;
8701da177e4SLinus Torvalds 	}
8711da177e4SLinus Torvalds 
872d3ec10aaSWaiman Long 	if (!buffer || !buflen) {
873d3ec10aaSWaiman Long 		/* Get the key length from the read method */
874d3ec10aaSWaiman Long 		ret = __keyctl_read_key(key, NULL, 0);
875d3ec10aaSWaiman Long 		goto key_put_out;
876d3ec10aaSWaiman Long 	}
877d3ec10aaSWaiman Long 
878d3ec10aaSWaiman Long 	/*
879d3ec10aaSWaiman Long 	 * Read the data with the semaphore held (since we might sleep)
880d3ec10aaSWaiman Long 	 * to protect against the key being updated or revoked.
881d3ec10aaSWaiman Long 	 *
882d3ec10aaSWaiman Long 	 * Allocating a temporary buffer to hold the keys before
883d3ec10aaSWaiman Long 	 * transferring them to user buffer to avoid potential
884d3ec10aaSWaiman Long 	 * deadlock involving page fault and mmap_sem.
8854f088249SWaiman Long 	 *
8864f088249SWaiman Long 	 * key_data_len = (buflen <= PAGE_SIZE)
8874f088249SWaiman Long 	 *		? buflen : actual length of key data
8884f088249SWaiman Long 	 *
8894f088249SWaiman Long 	 * This prevents allocating arbitrary large buffer which can
8904f088249SWaiman Long 	 * be much larger than the actual key length. In the latter case,
8914f088249SWaiman Long 	 * at least 2 passes of this loop is required.
892d3ec10aaSWaiman Long 	 */
8934f088249SWaiman Long 	key_data_len = (buflen <= PAGE_SIZE) ? buflen : 0;
8944f088249SWaiman Long 	for (;;) {
8954f088249SWaiman Long 		if (key_data_len) {
8964f088249SWaiman Long 			key_data = kvmalloc(key_data_len, GFP_KERNEL);
897d3ec10aaSWaiman Long 			if (!key_data) {
898d3ec10aaSWaiman Long 				ret = -ENOMEM;
899d3ec10aaSWaiman Long 				goto key_put_out;
900d3ec10aaSWaiman Long 			}
9014f088249SWaiman Long 		}
9024f088249SWaiman Long 
9034f088249SWaiman Long 		ret = __keyctl_read_key(key, key_data, key_data_len);
904d3ec10aaSWaiman Long 
905d3ec10aaSWaiman Long 		/*
906d3ec10aaSWaiman Long 		 * Read methods will just return the required length without
907d3ec10aaSWaiman Long 		 * any copying if the provided length isn't large enough.
908d3ec10aaSWaiman Long 		 */
9094f088249SWaiman Long 		if (ret <= 0 || ret > buflen)
9104f088249SWaiman Long 			break;
9114f088249SWaiman Long 
9124f088249SWaiman Long 		/*
9134f088249SWaiman Long 		 * The key may change (unlikely) in between 2 consecutive
9144f088249SWaiman Long 		 * __keyctl_read_key() calls. In this case, we reallocate
9154f088249SWaiman Long 		 * a larger buffer and redo the key read when
9164f088249SWaiman Long 		 * key_data_len < ret <= buflen.
9174f088249SWaiman Long 		 */
9184f088249SWaiman Long 		if (ret > key_data_len) {
9194f088249SWaiman Long 			if (unlikely(key_data))
9204f088249SWaiman Long 				__kvzfree(key_data, key_data_len);
9214f088249SWaiman Long 			key_data_len = ret;
9224f088249SWaiman Long 			continue;	/* Allocate buffer */
9234f088249SWaiman Long 		}
9244f088249SWaiman Long 
925d3ec10aaSWaiman Long 		if (copy_to_user(buffer, key_data, ret))
926d3ec10aaSWaiman Long 			ret = -EFAULT;
9274f088249SWaiman Long 		break;
928d3ec10aaSWaiman Long 	}
9294f088249SWaiman Long 	__kvzfree(key_data, key_data_len);
930d3ec10aaSWaiman Long 
931d3ec10aaSWaiman Long key_put_out:
9321da177e4SLinus Torvalds 	key_put(key);
933d3ec10aaSWaiman Long out:
9341da177e4SLinus Torvalds 	return ret;
935a8b17ed0SDavid Howells }
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds /*
938973c9f4fSDavid Howells  * Change the ownership of a key
939973c9f4fSDavid Howells  *
940973c9f4fSDavid Howells  * The key must grant the caller Setattr permission for this to work, though
941973c9f4fSDavid Howells  * the key need not be fully instantiated yet.  For the UID to be changed, or
942973c9f4fSDavid Howells  * for the GID to be changed to a group the caller is not a member of, the
943973c9f4fSDavid Howells  * caller must have sysadmin capability.  If either uid or gid is -1 then that
944973c9f4fSDavid Howells  * attribute is not changed.
945973c9f4fSDavid Howells  *
946973c9f4fSDavid Howells  * If the UID is to be changed, the new user must have sufficient quota to
947973c9f4fSDavid Howells  * accept the key.  The quota deduction will be removed from the old user to
948973c9f4fSDavid Howells  * the new user should the attribute be changed.
949973c9f4fSDavid Howells  *
950973c9f4fSDavid Howells  * If successful, 0 will be returned.
9511da177e4SLinus Torvalds  */
9529a56c2dbSEric W. Biederman long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
9531da177e4SLinus Torvalds {
9545801649dSFredrik Tolf 	struct key_user *newowner, *zapowner = NULL;
9551da177e4SLinus Torvalds 	struct key *key;
956664cceb0SDavid Howells 	key_ref_t key_ref;
9571da177e4SLinus Torvalds 	long ret;
9589a56c2dbSEric W. Biederman 	kuid_t uid;
9599a56c2dbSEric W. Biederman 	kgid_t gid;
9609a56c2dbSEric W. Biederman 
9619a56c2dbSEric W. Biederman 	uid = make_kuid(current_user_ns(), user);
9629a56c2dbSEric W. Biederman 	gid = make_kgid(current_user_ns(), group);
9639a56c2dbSEric W. Biederman 	ret = -EINVAL;
9649a56c2dbSEric W. Biederman 	if ((user != (uid_t) -1) && !uid_valid(uid))
9659a56c2dbSEric W. Biederman 		goto error;
9669a56c2dbSEric W. Biederman 	if ((group != (gid_t) -1) && !gid_valid(gid))
9679a56c2dbSEric W. Biederman 		goto error;
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	ret = 0;
9709a56c2dbSEric W. Biederman 	if (user == (uid_t) -1 && group == (gid_t) -1)
9711da177e4SLinus Torvalds 		goto error;
9721da177e4SLinus Torvalds 
9735593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
974028db3e2SLinus Torvalds 				  KEY_NEED_SETATTR);
975664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
976664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
9771da177e4SLinus Torvalds 		goto error;
9781da177e4SLinus Torvalds 	}
9791da177e4SLinus Torvalds 
980664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
981664cceb0SDavid Howells 
9821da177e4SLinus Torvalds 	/* make the changes with the locks held to prevent chown/chown races */
9831da177e4SLinus Torvalds 	ret = -EACCES;
9841da177e4SLinus Torvalds 	down_write(&key->sem);
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN)) {
9871da177e4SLinus Torvalds 		/* only the sysadmin can chown a key to some other UID */
9889a56c2dbSEric W. Biederman 		if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
9895801649dSFredrik Tolf 			goto error_put;
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 		/* only the sysadmin can set the key's GID to a group other
9921da177e4SLinus Torvalds 		 * than one of those that the current process subscribes to */
9939a56c2dbSEric W. Biederman 		if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
9945801649dSFredrik Tolf 			goto error_put;
9951da177e4SLinus Torvalds 	}
9961da177e4SLinus Torvalds 
9975801649dSFredrik Tolf 	/* change the UID */
9989a56c2dbSEric W. Biederman 	if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) {
9995801649dSFredrik Tolf 		ret = -ENOMEM;
10009a56c2dbSEric W. Biederman 		newowner = key_user_lookup(uid);
10015801649dSFredrik Tolf 		if (!newowner)
10025801649dSFredrik Tolf 			goto error_put;
10035801649dSFredrik Tolf 
10045801649dSFredrik Tolf 		/* transfer the quota burden to the new user */
10055801649dSFredrik Tolf 		if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
10069a56c2dbSEric W. Biederman 			unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
10070b77f5bfSDavid Howells 				key_quota_root_maxkeys : key_quota_maxkeys;
10089a56c2dbSEric W. Biederman 			unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
10090b77f5bfSDavid Howells 				key_quota_root_maxbytes : key_quota_maxbytes;
10100b77f5bfSDavid Howells 
10115801649dSFredrik Tolf 			spin_lock(&newowner->lock);
10122e356101SYang Xu 			if (newowner->qnkeys + 1 > maxkeys ||
10132e356101SYang Xu 			    newowner->qnbytes + key->quotalen > maxbytes ||
10140b77f5bfSDavid Howells 			    newowner->qnbytes + key->quotalen <
10150b77f5bfSDavid Howells 			    newowner->qnbytes)
10165801649dSFredrik Tolf 				goto quota_overrun;
10175801649dSFredrik Tolf 
10185801649dSFredrik Tolf 			newowner->qnkeys++;
10195801649dSFredrik Tolf 			newowner->qnbytes += key->quotalen;
10205801649dSFredrik Tolf 			spin_unlock(&newowner->lock);
10215801649dSFredrik Tolf 
10225801649dSFredrik Tolf 			spin_lock(&key->user->lock);
10235801649dSFredrik Tolf 			key->user->qnkeys--;
10245801649dSFredrik Tolf 			key->user->qnbytes -= key->quotalen;
10255801649dSFredrik Tolf 			spin_unlock(&key->user->lock);
10265801649dSFredrik Tolf 		}
10275801649dSFredrik Tolf 
10285801649dSFredrik Tolf 		atomic_dec(&key->user->nkeys);
10295801649dSFredrik Tolf 		atomic_inc(&newowner->nkeys);
10305801649dSFredrik Tolf 
1031363b02daSDavid Howells 		if (key->state != KEY_IS_UNINSTANTIATED) {
10325801649dSFredrik Tolf 			atomic_dec(&key->user->nikeys);
10335801649dSFredrik Tolf 			atomic_inc(&newowner->nikeys);
10345801649dSFredrik Tolf 		}
10355801649dSFredrik Tolf 
10365801649dSFredrik Tolf 		zapowner = key->user;
10375801649dSFredrik Tolf 		key->user = newowner;
10385801649dSFredrik Tolf 		key->uid = uid;
10391da177e4SLinus Torvalds 	}
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds 	/* change the GID */
10429a56c2dbSEric W. Biederman 	if (group != (gid_t) -1)
10431da177e4SLinus Torvalds 		key->gid = gid;
10441da177e4SLinus Torvalds 
1045f7e47677SDavid Howells 	notify_key(key, NOTIFY_KEY_SETATTR, 0);
10461da177e4SLinus Torvalds 	ret = 0;
10471da177e4SLinus Torvalds 
10485801649dSFredrik Tolf error_put:
10491da177e4SLinus Torvalds 	up_write(&key->sem);
10501da177e4SLinus Torvalds 	key_put(key);
10515801649dSFredrik Tolf 	if (zapowner)
10525801649dSFredrik Tolf 		key_user_put(zapowner);
10531da177e4SLinus Torvalds error:
10541da177e4SLinus Torvalds 	return ret;
10551da177e4SLinus Torvalds 
10565801649dSFredrik Tolf quota_overrun:
10575801649dSFredrik Tolf 	spin_unlock(&newowner->lock);
10585801649dSFredrik Tolf 	zapowner = newowner;
10595801649dSFredrik Tolf 	ret = -EDQUOT;
10605801649dSFredrik Tolf 	goto error_put;
1061a8b17ed0SDavid Howells }
10625801649dSFredrik Tolf 
10631da177e4SLinus Torvalds /*
1064973c9f4fSDavid Howells  * Change the permission mask on a key.
1065973c9f4fSDavid Howells  *
1066973c9f4fSDavid Howells  * The key must grant the caller Setattr permission for this to work, though
1067973c9f4fSDavid Howells  * the key need not be fully instantiated yet.  If the caller does not have
1068973c9f4fSDavid Howells  * sysadmin capability, it may only change the permission on keys that it owns.
10691da177e4SLinus Torvalds  */
1070028db3e2SLinus Torvalds long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
10711da177e4SLinus Torvalds {
10721da177e4SLinus Torvalds 	struct key *key;
1073664cceb0SDavid Howells 	key_ref_t key_ref;
10741da177e4SLinus Torvalds 	long ret;
10751da177e4SLinus Torvalds 
1076028db3e2SLinus Torvalds 	ret = -EINVAL;
1077664cceb0SDavid Howells 	if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
1078028db3e2SLinus Torvalds 		goto error;
10791da177e4SLinus Torvalds 
10805593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1081028db3e2SLinus Torvalds 				  KEY_NEED_SETATTR);
1082664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
1083664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
10841da177e4SLinus Torvalds 		goto error;
10851da177e4SLinus Torvalds 	}
10861da177e4SLinus Torvalds 
1087664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
1088664cceb0SDavid Howells 
10892e12256bSDavid Howells 	/* make the changes with the locks held to prevent chown/chmod races */
1090028db3e2SLinus Torvalds 	ret = -EACCES;
10912e12256bSDavid Howells 	down_write(&key->sem);
1092028db3e2SLinus Torvalds 
1093028db3e2SLinus Torvalds 	/* if we're not the sysadmin, we can only change a key that we own */
1094028db3e2SLinus Torvalds 	if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
1095028db3e2SLinus Torvalds 		key->perm = perm;
1096f7e47677SDavid Howells 		notify_key(key, NOTIFY_KEY_SETATTR, 0);
1097028db3e2SLinus Torvalds 		ret = 0;
1098028db3e2SLinus Torvalds 	}
1099028db3e2SLinus Torvalds 
11001da177e4SLinus Torvalds 	up_write(&key->sem);
11011da177e4SLinus Torvalds 	key_put(key);
11021da177e4SLinus Torvalds error:
11031da177e4SLinus Torvalds 	return ret;
1104a8b17ed0SDavid Howells }
11051da177e4SLinus Torvalds 
11068bbf4976SDavid Howells /*
1107973c9f4fSDavid Howells  * Get the destination keyring for instantiation and check that the caller has
1108973c9f4fSDavid Howells  * Write permission on it.
11098bbf4976SDavid Howells  */
11108bbf4976SDavid Howells static long get_instantiation_keyring(key_serial_t ringid,
11118bbf4976SDavid Howells 				      struct request_key_auth *rka,
11128bbf4976SDavid Howells 				      struct key **_dest_keyring)
11138bbf4976SDavid Howells {
11148bbf4976SDavid Howells 	key_ref_t dkref;
11158bbf4976SDavid Howells 
11168bbf4976SDavid Howells 	*_dest_keyring = NULL;
1117eca1bf5bSDavid Howells 
1118eca1bf5bSDavid Howells 	/* just return a NULL pointer if we weren't asked to make a link */
1119eca1bf5bSDavid Howells 	if (ringid == 0)
11208bbf4976SDavid Howells 		return 0;
11218bbf4976SDavid Howells 
11228bbf4976SDavid Howells 	/* if a specific keyring is nominated by ID, then use that */
11238bbf4976SDavid Howells 	if (ringid > 0) {
1124f5895943SDavid Howells 		dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
11258bbf4976SDavid Howells 		if (IS_ERR(dkref))
11268bbf4976SDavid Howells 			return PTR_ERR(dkref);
11278bbf4976SDavid Howells 		*_dest_keyring = key_ref_to_ptr(dkref);
11288bbf4976SDavid Howells 		return 0;
11298bbf4976SDavid Howells 	}
11308bbf4976SDavid Howells 
11318bbf4976SDavid Howells 	if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
11328bbf4976SDavid Howells 		return -EINVAL;
11338bbf4976SDavid Howells 
11348bbf4976SDavid Howells 	/* otherwise specify the destination keyring recorded in the
11358bbf4976SDavid Howells 	 * authorisation key (any KEY_SPEC_*_KEYRING) */
11368bbf4976SDavid Howells 	if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
113721279cfaSDavid Howells 		*_dest_keyring = key_get(rka->dest_keyring);
11388bbf4976SDavid Howells 		return 0;
11398bbf4976SDavid Howells 	}
11408bbf4976SDavid Howells 
11418bbf4976SDavid Howells 	return -ENOKEY;
11428bbf4976SDavid Howells }
11438bbf4976SDavid Howells 
1144d84f4f99SDavid Howells /*
1145973c9f4fSDavid Howells  * Change the request_key authorisation key on the current process.
1146d84f4f99SDavid Howells  */
1147d84f4f99SDavid Howells static int keyctl_change_reqkey_auth(struct key *key)
1148d84f4f99SDavid Howells {
1149d84f4f99SDavid Howells 	struct cred *new;
1150d84f4f99SDavid Howells 
1151d84f4f99SDavid Howells 	new = prepare_creds();
1152d84f4f99SDavid Howells 	if (!new)
1153d84f4f99SDavid Howells 		return -ENOMEM;
1154d84f4f99SDavid Howells 
1155d84f4f99SDavid Howells 	key_put(new->request_key_auth);
1156d84f4f99SDavid Howells 	new->request_key_auth = key_get(key);
1157d84f4f99SDavid Howells 
1158d84f4f99SDavid Howells 	return commit_creds(new);
1159d84f4f99SDavid Howells }
1160d84f4f99SDavid Howells 
11611da177e4SLinus Torvalds /*
1162973c9f4fSDavid Howells  * Instantiate a key with the specified payload and link the key into the
1163973c9f4fSDavid Howells  * destination keyring if one is given.
1164973c9f4fSDavid Howells  *
1165973c9f4fSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1166973c9f4fSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1167973c9f4fSDavid Howells  *
1168973c9f4fSDavid Howells  * If successful, 0 will be returned.
11691da177e4SLinus Torvalds  */
1170ee009e4aSDavid Howells long keyctl_instantiate_key_common(key_serial_t id,
1171b353a1f7SAl Viro 				   struct iov_iter *from,
11721da177e4SLinus Torvalds 				   key_serial_t ringid)
11731da177e4SLinus Torvalds {
1174d84f4f99SDavid Howells 	const struct cred *cred = current_cred();
11753e30148cSDavid Howells 	struct request_key_auth *rka;
11768bbf4976SDavid Howells 	struct key *instkey, *dest_keyring;
1177b353a1f7SAl Viro 	size_t plen = from ? iov_iter_count(from) : 0;
11781da177e4SLinus Torvalds 	void *payload;
11791da177e4SLinus Torvalds 	long ret;
11801da177e4SLinus Torvalds 
1181d84f4f99SDavid Howells 	kenter("%d,,%zu,%d", id, plen, ringid);
1182d84f4f99SDavid Howells 
1183b353a1f7SAl Viro 	if (!plen)
1184b353a1f7SAl Viro 		from = NULL;
1185b353a1f7SAl Viro 
11861da177e4SLinus Torvalds 	ret = -EINVAL;
118738bbca6bSDavid Howells 	if (plen > 1024 * 1024 - 1)
11881da177e4SLinus Torvalds 		goto error;
11891da177e4SLinus Torvalds 
1190b5f545c8SDavid Howells 	/* the appropriate instantiation authorisation key must have been
1191b5f545c8SDavid Howells 	 * assumed before calling this */
1192b5f545c8SDavid Howells 	ret = -EPERM;
1193d84f4f99SDavid Howells 	instkey = cred->request_key_auth;
1194b5f545c8SDavid Howells 	if (!instkey)
1195b5f545c8SDavid Howells 		goto error;
1196b5f545c8SDavid Howells 
1197146aa8b1SDavid Howells 	rka = instkey->payload.data[0];
1198b5f545c8SDavid Howells 	if (rka->target_key->serial != id)
1199b5f545c8SDavid Howells 		goto error;
1200b5f545c8SDavid Howells 
12011da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
12021da177e4SLinus Torvalds 	payload = NULL;
12031da177e4SLinus Torvalds 
1204b353a1f7SAl Viro 	if (from) {
12051da177e4SLinus Torvalds 		ret = -ENOMEM;
1206752ade68SMichal Hocko 		payload = kvmalloc(plen, GFP_KERNEL);
12071da177e4SLinus Torvalds 		if (!payload)
12081da177e4SLinus Torvalds 			goto error;
12091da177e4SLinus Torvalds 
1210b353a1f7SAl Viro 		ret = -EFAULT;
1211cbbd26b8SAl Viro 		if (!copy_from_iter_full(payload, plen, from))
12121da177e4SLinus Torvalds 			goto error2;
12131da177e4SLinus Torvalds 	}
12141da177e4SLinus Torvalds 
12153e30148cSDavid Howells 	/* find the destination keyring amongst those belonging to the
12163e30148cSDavid Howells 	 * requesting task */
12178bbf4976SDavid Howells 	ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
12188bbf4976SDavid Howells 	if (ret < 0)
1219b5f545c8SDavid Howells 		goto error2;
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 	/* instantiate the key and link it into a keyring */
12223e30148cSDavid Howells 	ret = key_instantiate_and_link(rka->target_key, payload, plen,
12238bbf4976SDavid Howells 				       dest_keyring, instkey);
12241da177e4SLinus Torvalds 
12258bbf4976SDavid Howells 	key_put(dest_keyring);
1226b5f545c8SDavid Howells 
1227b5f545c8SDavid Howells 	/* discard the assumed authority if it's just been disabled by
1228b5f545c8SDavid Howells 	 * instantiation of the key */
1229d84f4f99SDavid Howells 	if (ret == 0)
1230d84f4f99SDavid Howells 		keyctl_change_reqkey_auth(NULL);
1231b5f545c8SDavid Howells 
12321da177e4SLinus Torvalds error2:
123357070c85SEric Biggers 	if (payload) {
123457070c85SEric Biggers 		memzero_explicit(payload, plen);
1235b353a1f7SAl Viro 		kvfree(payload);
123657070c85SEric Biggers 	}
12371da177e4SLinus Torvalds error:
12381da177e4SLinus Torvalds 	return ret;
1239a8b17ed0SDavid Howells }
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds /*
1242ee009e4aSDavid Howells  * Instantiate a key with the specified payload and link the key into the
1243ee009e4aSDavid Howells  * destination keyring if one is given.
1244ee009e4aSDavid Howells  *
1245ee009e4aSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1246ee009e4aSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1247ee009e4aSDavid Howells  *
1248ee009e4aSDavid Howells  * If successful, 0 will be returned.
1249ee009e4aSDavid Howells  */
1250ee009e4aSDavid Howells long keyctl_instantiate_key(key_serial_t id,
1251ee009e4aSDavid Howells 			    const void __user *_payload,
1252ee009e4aSDavid Howells 			    size_t plen,
1253ee009e4aSDavid Howells 			    key_serial_t ringid)
1254ee009e4aSDavid Howells {
1255ee009e4aSDavid Howells 	if (_payload && plen) {
1256b353a1f7SAl Viro 		struct iovec iov;
1257b353a1f7SAl Viro 		struct iov_iter from;
1258b353a1f7SAl Viro 		int ret;
1259ee009e4aSDavid Howells 
1260b353a1f7SAl Viro 		ret = import_single_range(WRITE, (void __user *)_payload, plen,
1261b353a1f7SAl Viro 					  &iov, &from);
1262b353a1f7SAl Viro 		if (unlikely(ret))
1263b353a1f7SAl Viro 			return ret;
1264b353a1f7SAl Viro 
1265b353a1f7SAl Viro 		return keyctl_instantiate_key_common(id, &from, ringid);
1266ee009e4aSDavid Howells 	}
1267ee009e4aSDavid Howells 
1268b353a1f7SAl Viro 	return keyctl_instantiate_key_common(id, NULL, ringid);
1269ee009e4aSDavid Howells }
1270ee009e4aSDavid Howells 
1271ee009e4aSDavid Howells /*
1272ee009e4aSDavid Howells  * Instantiate a key with the specified multipart payload and link the key into
1273ee009e4aSDavid Howells  * the destination keyring if one is given.
1274ee009e4aSDavid Howells  *
1275ee009e4aSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1276ee009e4aSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1277ee009e4aSDavid Howells  *
1278ee009e4aSDavid Howells  * If successful, 0 will be returned.
1279ee009e4aSDavid Howells  */
1280ee009e4aSDavid Howells long keyctl_instantiate_key_iov(key_serial_t id,
1281ee009e4aSDavid Howells 				const struct iovec __user *_payload_iov,
1282ee009e4aSDavid Howells 				unsigned ioc,
1283ee009e4aSDavid Howells 				key_serial_t ringid)
1284ee009e4aSDavid Howells {
1285ee009e4aSDavid Howells 	struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1286b353a1f7SAl Viro 	struct iov_iter from;
1287ee009e4aSDavid Howells 	long ret;
1288ee009e4aSDavid Howells 
1289b353a1f7SAl Viro 	if (!_payload_iov)
1290b353a1f7SAl Viro 		ioc = 0;
1291ee009e4aSDavid Howells 
1292b353a1f7SAl Viro 	ret = import_iovec(WRITE, _payload_iov, ioc,
1293b353a1f7SAl Viro 				    ARRAY_SIZE(iovstack), &iov, &from);
1294ee009e4aSDavid Howells 	if (ret < 0)
1295b353a1f7SAl Viro 		return ret;
1296b353a1f7SAl Viro 	ret = keyctl_instantiate_key_common(id, &from, ringid);
1297ee009e4aSDavid Howells 	kfree(iov);
1298ee009e4aSDavid Howells 	return ret;
1299ee009e4aSDavid Howells }
1300ee009e4aSDavid Howells 
1301ee009e4aSDavid Howells /*
1302973c9f4fSDavid Howells  * Negatively instantiate the key with the given timeout (in seconds) and link
1303973c9f4fSDavid Howells  * the key into the destination keyring if one is given.
1304973c9f4fSDavid Howells  *
1305973c9f4fSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1306973c9f4fSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1307973c9f4fSDavid Howells  *
1308973c9f4fSDavid Howells  * The key and any links to the key will be automatically garbage collected
1309973c9f4fSDavid Howells  * after the timeout expires.
1310973c9f4fSDavid Howells  *
1311973c9f4fSDavid Howells  * Negative keys are used to rate limit repeated request_key() calls by causing
1312973c9f4fSDavid Howells  * them to return -ENOKEY until the negative key expires.
1313973c9f4fSDavid Howells  *
1314973c9f4fSDavid Howells  * If successful, 0 will be returned.
13151da177e4SLinus Torvalds  */
13161da177e4SLinus Torvalds long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
13171da177e4SLinus Torvalds {
1318fdd1b945SDavid Howells 	return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1319fdd1b945SDavid Howells }
1320fdd1b945SDavid Howells 
1321fdd1b945SDavid Howells /*
1322fdd1b945SDavid Howells  * Negatively instantiate the key with the given timeout (in seconds) and error
1323fdd1b945SDavid Howells  * code and link the key into the destination keyring if one is given.
1324fdd1b945SDavid Howells  *
1325fdd1b945SDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1326fdd1b945SDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1327fdd1b945SDavid Howells  *
1328fdd1b945SDavid Howells  * The key and any links to the key will be automatically garbage collected
1329fdd1b945SDavid Howells  * after the timeout expires.
1330fdd1b945SDavid Howells  *
1331fdd1b945SDavid Howells  * Negative keys are used to rate limit repeated request_key() calls by causing
1332fdd1b945SDavid Howells  * them to return the specified error code until the negative key expires.
1333fdd1b945SDavid Howells  *
1334fdd1b945SDavid Howells  * If successful, 0 will be returned.
1335fdd1b945SDavid Howells  */
1336fdd1b945SDavid Howells long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1337fdd1b945SDavid Howells 		       key_serial_t ringid)
1338fdd1b945SDavid Howells {
1339d84f4f99SDavid Howells 	const struct cred *cred = current_cred();
13403e30148cSDavid Howells 	struct request_key_auth *rka;
13418bbf4976SDavid Howells 	struct key *instkey, *dest_keyring;
13421da177e4SLinus Torvalds 	long ret;
13431da177e4SLinus Torvalds 
1344fdd1b945SDavid Howells 	kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1345fdd1b945SDavid Howells 
1346fdd1b945SDavid Howells 	/* must be a valid error code and mustn't be a kernel special */
1347fdd1b945SDavid Howells 	if (error <= 0 ||
1348fdd1b945SDavid Howells 	    error >= MAX_ERRNO ||
1349fdd1b945SDavid Howells 	    error == ERESTARTSYS ||
1350fdd1b945SDavid Howells 	    error == ERESTARTNOINTR ||
1351fdd1b945SDavid Howells 	    error == ERESTARTNOHAND ||
1352fdd1b945SDavid Howells 	    error == ERESTART_RESTARTBLOCK)
1353fdd1b945SDavid Howells 		return -EINVAL;
1354d84f4f99SDavid Howells 
1355b5f545c8SDavid Howells 	/* the appropriate instantiation authorisation key must have been
1356b5f545c8SDavid Howells 	 * assumed before calling this */
1357b5f545c8SDavid Howells 	ret = -EPERM;
1358d84f4f99SDavid Howells 	instkey = cred->request_key_auth;
1359b5f545c8SDavid Howells 	if (!instkey)
13601da177e4SLinus Torvalds 		goto error;
13611da177e4SLinus Torvalds 
1362146aa8b1SDavid Howells 	rka = instkey->payload.data[0];
1363b5f545c8SDavid Howells 	if (rka->target_key->serial != id)
1364b5f545c8SDavid Howells 		goto error;
13653e30148cSDavid Howells 
13661da177e4SLinus Torvalds 	/* find the destination keyring if present (which must also be
13671da177e4SLinus Torvalds 	 * writable) */
13688bbf4976SDavid Howells 	ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
13698bbf4976SDavid Howells 	if (ret < 0)
1370b5f545c8SDavid Howells 		goto error;
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds 	/* instantiate the key and link it into a keyring */
1373fdd1b945SDavid Howells 	ret = key_reject_and_link(rka->target_key, timeout, error,
13748bbf4976SDavid Howells 				  dest_keyring, instkey);
13751da177e4SLinus Torvalds 
13768bbf4976SDavid Howells 	key_put(dest_keyring);
1377b5f545c8SDavid Howells 
1378b5f545c8SDavid Howells 	/* discard the assumed authority if it's just been disabled by
1379b5f545c8SDavid Howells 	 * instantiation of the key */
1380d84f4f99SDavid Howells 	if (ret == 0)
1381d84f4f99SDavid Howells 		keyctl_change_reqkey_auth(NULL);
1382b5f545c8SDavid Howells 
13831da177e4SLinus Torvalds error:
13841da177e4SLinus Torvalds 	return ret;
1385a8b17ed0SDavid Howells }
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds /*
1388973c9f4fSDavid Howells  * Read or set the default keyring in which request_key() will cache keys and
1389973c9f4fSDavid Howells  * return the old setting.
1390973c9f4fSDavid Howells  *
1391c9f838d1SEric Biggers  * If a thread or process keyring is specified then it will be created if it
1392c9f838d1SEric Biggers  * doesn't yet exist.  The old setting will be returned if successful.
13933e30148cSDavid Howells  */
13943e30148cSDavid Howells long keyctl_set_reqkey_keyring(int reqkey_defl)
13953e30148cSDavid Howells {
1396d84f4f99SDavid Howells 	struct cred *new;
1397d84f4f99SDavid Howells 	int ret, old_setting;
1398d84f4f99SDavid Howells 
1399d84f4f99SDavid Howells 	old_setting = current_cred_xxx(jit_keyring);
1400d84f4f99SDavid Howells 
1401d84f4f99SDavid Howells 	if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1402d84f4f99SDavid Howells 		return old_setting;
1403d84f4f99SDavid Howells 
1404d84f4f99SDavid Howells 	new = prepare_creds();
1405d84f4f99SDavid Howells 	if (!new)
1406d84f4f99SDavid Howells 		return -ENOMEM;
14073e30148cSDavid Howells 
14083e30148cSDavid Howells 	switch (reqkey_defl) {
14093e30148cSDavid Howells 	case KEY_REQKEY_DEFL_THREAD_KEYRING:
1410d84f4f99SDavid Howells 		ret = install_thread_keyring_to_cred(new);
14113e30148cSDavid Howells 		if (ret < 0)
1412d84f4f99SDavid Howells 			goto error;
14133e30148cSDavid Howells 		goto set;
14143e30148cSDavid Howells 
14153e30148cSDavid Howells 	case KEY_REQKEY_DEFL_PROCESS_KEYRING:
1416d84f4f99SDavid Howells 		ret = install_process_keyring_to_cred(new);
1417c9f838d1SEric Biggers 		if (ret < 0)
1418d84f4f99SDavid Howells 			goto error;
1419d84f4f99SDavid Howells 		goto set;
14203e30148cSDavid Howells 
14213e30148cSDavid Howells 	case KEY_REQKEY_DEFL_DEFAULT:
14223e30148cSDavid Howells 	case KEY_REQKEY_DEFL_SESSION_KEYRING:
14233e30148cSDavid Howells 	case KEY_REQKEY_DEFL_USER_KEYRING:
14243e30148cSDavid Howells 	case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1425d84f4f99SDavid Howells 	case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1426d84f4f99SDavid Howells 		goto set;
14273e30148cSDavid Howells 
14283e30148cSDavid Howells 	case KEY_REQKEY_DEFL_NO_CHANGE:
14293e30148cSDavid Howells 	case KEY_REQKEY_DEFL_GROUP_KEYRING:
14303e30148cSDavid Howells 	default:
1431d84f4f99SDavid Howells 		ret = -EINVAL;
1432d84f4f99SDavid Howells 		goto error;
14333e30148cSDavid Howells 	}
14343e30148cSDavid Howells 
1435d84f4f99SDavid Howells set:
1436d84f4f99SDavid Howells 	new->jit_keyring = reqkey_defl;
1437d84f4f99SDavid Howells 	commit_creds(new);
1438d84f4f99SDavid Howells 	return old_setting;
1439d84f4f99SDavid Howells error:
1440d84f4f99SDavid Howells 	abort_creds(new);
14414303ef19SDan Carpenter 	return ret;
1442a8b17ed0SDavid Howells }
1443d84f4f99SDavid Howells 
14443e30148cSDavid Howells /*
1445973c9f4fSDavid Howells  * Set or clear the timeout on a key.
1446973c9f4fSDavid Howells  *
1447973c9f4fSDavid Howells  * Either the key must grant the caller Setattr permission or else the caller
1448973c9f4fSDavid Howells  * must hold an instantiation authorisation token for the key.
1449973c9f4fSDavid Howells  *
1450973c9f4fSDavid Howells  * The timeout is either 0 to clear the timeout, or a number of seconds from
1451973c9f4fSDavid Howells  * the current time.  The key and any links to the key will be automatically
1452973c9f4fSDavid Howells  * garbage collected after the timeout expires.
1453973c9f4fSDavid Howells  *
1454d3600bcfSMimi Zohar  * Keys with KEY_FLAG_KEEP set should not be timed out.
1455d3600bcfSMimi Zohar  *
1456973c9f4fSDavid Howells  * If successful, 0 is returned.
1457017679c4SDavid Howells  */
1458017679c4SDavid Howells long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1459017679c4SDavid Howells {
14609156235bSDavid Howells 	struct key *key, *instkey;
1461017679c4SDavid Howells 	key_ref_t key_ref;
1462017679c4SDavid Howells 	long ret;
1463017679c4SDavid Howells 
14645593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1465028db3e2SLinus Torvalds 				  KEY_NEED_SETATTR);
1466017679c4SDavid Howells 	if (IS_ERR(key_ref)) {
14679156235bSDavid Howells 		/* setting the timeout on a key under construction is permitted
14689156235bSDavid Howells 		 * if we have the authorisation token handy */
14699156235bSDavid Howells 		if (PTR_ERR(key_ref) == -EACCES) {
14709156235bSDavid Howells 			instkey = key_get_instantiation_authkey(id);
14719156235bSDavid Howells 			if (!IS_ERR(instkey)) {
14729156235bSDavid Howells 				key_put(instkey);
14739156235bSDavid Howells 				key_ref = lookup_user_key(id,
14749156235bSDavid Howells 							  KEY_LOOKUP_PARTIAL,
1475*8c0637e9SDavid Howells 							  KEY_AUTHTOKEN_OVERRIDE);
14769156235bSDavid Howells 				if (!IS_ERR(key_ref))
14779156235bSDavid Howells 					goto okay;
14789156235bSDavid Howells 			}
14799156235bSDavid Howells 		}
14809156235bSDavid Howells 
1481017679c4SDavid Howells 		ret = PTR_ERR(key_ref);
1482017679c4SDavid Howells 		goto error;
1483017679c4SDavid Howells 	}
1484017679c4SDavid Howells 
14859156235bSDavid Howells okay:
1486017679c4SDavid Howells 	key = key_ref_to_ptr(key_ref);
14871d6d167cSMimi Zohar 	ret = 0;
1488f7e47677SDavid Howells 	if (test_bit(KEY_FLAG_KEEP, &key->flags)) {
1489d3600bcfSMimi Zohar 		ret = -EPERM;
1490f7e47677SDavid Howells 	} else {
149159e6b9c1SBryan Schumaker 		key_set_timeout(key, timeout);
1492f7e47677SDavid Howells 		notify_key(key, NOTIFY_KEY_SETATTR, 0);
1493f7e47677SDavid Howells 	}
1494017679c4SDavid Howells 	key_put(key);
1495017679c4SDavid Howells 
1496017679c4SDavid Howells error:
1497017679c4SDavid Howells 	return ret;
1498a8b17ed0SDavid Howells }
1499017679c4SDavid Howells 
1500017679c4SDavid Howells /*
1501973c9f4fSDavid Howells  * Assume (or clear) the authority to instantiate the specified key.
1502973c9f4fSDavid Howells  *
1503973c9f4fSDavid Howells  * This sets the authoritative token currently in force for key instantiation.
1504973c9f4fSDavid Howells  * This must be done for a key to be instantiated.  It has the effect of making
1505973c9f4fSDavid Howells  * available all the keys from the caller of the request_key() that created a
1506973c9f4fSDavid Howells  * key to request_key() calls made by the caller of this function.
1507973c9f4fSDavid Howells  *
1508973c9f4fSDavid Howells  * The caller must have the instantiation key in their process keyrings with a
1509973c9f4fSDavid Howells  * Search permission grant available to the caller.
1510973c9f4fSDavid Howells  *
1511973c9f4fSDavid Howells  * If the ID given is 0, then the setting will be cleared and 0 returned.
1512973c9f4fSDavid Howells  *
1513973c9f4fSDavid Howells  * If the ID given has a matching an authorisation key, then that key will be
1514973c9f4fSDavid Howells  * set and its ID will be returned.  The authorisation key can be read to get
1515973c9f4fSDavid Howells  * the callout information passed to request_key().
1516b5f545c8SDavid Howells  */
1517b5f545c8SDavid Howells long keyctl_assume_authority(key_serial_t id)
1518b5f545c8SDavid Howells {
1519b5f545c8SDavid Howells 	struct key *authkey;
1520b5f545c8SDavid Howells 	long ret;
1521b5f545c8SDavid Howells 
1522b5f545c8SDavid Howells 	/* special key IDs aren't permitted */
1523b5f545c8SDavid Howells 	ret = -EINVAL;
1524b5f545c8SDavid Howells 	if (id < 0)
1525b5f545c8SDavid Howells 		goto error;
1526b5f545c8SDavid Howells 
1527b5f545c8SDavid Howells 	/* we divest ourselves of authority if given an ID of 0 */
1528b5f545c8SDavid Howells 	if (id == 0) {
1529d84f4f99SDavid Howells 		ret = keyctl_change_reqkey_auth(NULL);
1530b5f545c8SDavid Howells 		goto error;
1531b5f545c8SDavid Howells 	}
1532b5f545c8SDavid Howells 
1533b5f545c8SDavid Howells 	/* attempt to assume the authority temporarily granted to us whilst we
1534b5f545c8SDavid Howells 	 * instantiate the specified key
1535b5f545c8SDavid Howells 	 * - the authorisation key must be in the current task's keyrings
1536b5f545c8SDavid Howells 	 *   somewhere
1537b5f545c8SDavid Howells 	 */
1538b5f545c8SDavid Howells 	authkey = key_get_instantiation_authkey(id);
1539b5f545c8SDavid Howells 	if (IS_ERR(authkey)) {
1540b5f545c8SDavid Howells 		ret = PTR_ERR(authkey);
1541b5f545c8SDavid Howells 		goto error;
1542b5f545c8SDavid Howells 	}
1543b5f545c8SDavid Howells 
1544d84f4f99SDavid Howells 	ret = keyctl_change_reqkey_auth(authkey);
1545884bee02SEric Biggers 	if (ret == 0)
1546d84f4f99SDavid Howells 		ret = authkey->serial;
1547884bee02SEric Biggers 	key_put(authkey);
1548b5f545c8SDavid Howells error:
1549b5f545c8SDavid Howells 	return ret;
1550a8b17ed0SDavid Howells }
1551b5f545c8SDavid Howells 
155270a5bb72SDavid Howells /*
1553973c9f4fSDavid Howells  * Get a key's the LSM security label.
1554973c9f4fSDavid Howells  *
1555973c9f4fSDavid Howells  * The key must grant the caller View permission for this to work.
1556973c9f4fSDavid Howells  *
1557973c9f4fSDavid Howells  * If there's a buffer, then up to buflen bytes of data will be placed into it.
1558973c9f4fSDavid Howells  *
1559973c9f4fSDavid Howells  * If successful, the amount of information available will be returned,
1560973c9f4fSDavid Howells  * irrespective of how much was copied (including the terminal NUL).
156170a5bb72SDavid Howells  */
156270a5bb72SDavid Howells long keyctl_get_security(key_serial_t keyid,
156370a5bb72SDavid Howells 			 char __user *buffer,
156470a5bb72SDavid Howells 			 size_t buflen)
156570a5bb72SDavid Howells {
156670a5bb72SDavid Howells 	struct key *key, *instkey;
156770a5bb72SDavid Howells 	key_ref_t key_ref;
156870a5bb72SDavid Howells 	char *context;
156970a5bb72SDavid Howells 	long ret;
157070a5bb72SDavid Howells 
1571f5895943SDavid Howells 	key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
157270a5bb72SDavid Howells 	if (IS_ERR(key_ref)) {
157370a5bb72SDavid Howells 		if (PTR_ERR(key_ref) != -EACCES)
157470a5bb72SDavid Howells 			return PTR_ERR(key_ref);
157570a5bb72SDavid Howells 
157670a5bb72SDavid Howells 		/* viewing a key under construction is also permitted if we
157770a5bb72SDavid Howells 		 * have the authorisation token handy */
157870a5bb72SDavid Howells 		instkey = key_get_instantiation_authkey(keyid);
157970a5bb72SDavid Howells 		if (IS_ERR(instkey))
1580fa1cc7b5SRoel Kluin 			return PTR_ERR(instkey);
158170a5bb72SDavid Howells 		key_put(instkey);
158270a5bb72SDavid Howells 
1583*8c0637e9SDavid Howells 		key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL,
1584*8c0637e9SDavid Howells 					  KEY_AUTHTOKEN_OVERRIDE);
158570a5bb72SDavid Howells 		if (IS_ERR(key_ref))
158670a5bb72SDavid Howells 			return PTR_ERR(key_ref);
158770a5bb72SDavid Howells 	}
158870a5bb72SDavid Howells 
158970a5bb72SDavid Howells 	key = key_ref_to_ptr(key_ref);
159070a5bb72SDavid Howells 	ret = security_key_getsecurity(key, &context);
159170a5bb72SDavid Howells 	if (ret == 0) {
159270a5bb72SDavid Howells 		/* if no information was returned, give userspace an empty
159370a5bb72SDavid Howells 		 * string */
159470a5bb72SDavid Howells 		ret = 1;
159570a5bb72SDavid Howells 		if (buffer && buflen > 0 &&
159670a5bb72SDavid Howells 		    copy_to_user(buffer, "", 1) != 0)
159770a5bb72SDavid Howells 			ret = -EFAULT;
159870a5bb72SDavid Howells 	} else if (ret > 0) {
159970a5bb72SDavid Howells 		/* return as much data as there's room for */
160070a5bb72SDavid Howells 		if (buffer && buflen > 0) {
160170a5bb72SDavid Howells 			if (buflen > ret)
160270a5bb72SDavid Howells 				buflen = ret;
160370a5bb72SDavid Howells 
160470a5bb72SDavid Howells 			if (copy_to_user(buffer, context, buflen) != 0)
160570a5bb72SDavid Howells 				ret = -EFAULT;
160670a5bb72SDavid Howells 		}
160770a5bb72SDavid Howells 
160870a5bb72SDavid Howells 		kfree(context);
160970a5bb72SDavid Howells 	}
161070a5bb72SDavid Howells 
161170a5bb72SDavid Howells 	key_ref_put(key_ref);
161270a5bb72SDavid Howells 	return ret;
161370a5bb72SDavid Howells }
161470a5bb72SDavid Howells 
1615ee18d64cSDavid Howells /*
1616973c9f4fSDavid Howells  * Attempt to install the calling process's session keyring on the process's
1617973c9f4fSDavid Howells  * parent process.
1618973c9f4fSDavid Howells  *
1619028db3e2SLinus Torvalds  * The keyring must exist and must grant the caller LINK permission, and the
1620973c9f4fSDavid Howells  * parent process must be single-threaded and must have the same effective
1621973c9f4fSDavid Howells  * ownership as this process and mustn't be SUID/SGID.
1622973c9f4fSDavid Howells  *
1623973c9f4fSDavid Howells  * The keyring will be emplaced on the parent when it next resumes userspace.
1624973c9f4fSDavid Howells  *
1625973c9f4fSDavid Howells  * If successful, 0 will be returned.
1626ee18d64cSDavid Howells  */
1627ee18d64cSDavid Howells long keyctl_session_to_parent(void)
1628ee18d64cSDavid Howells {
1629ee18d64cSDavid Howells 	struct task_struct *me, *parent;
1630ee18d64cSDavid Howells 	const struct cred *mycred, *pcred;
163167d12145SAl Viro 	struct callback_head *newwork, *oldwork;
1632ee18d64cSDavid Howells 	key_ref_t keyring_r;
1633413cd3d9SOleg Nesterov 	struct cred *cred;
1634ee18d64cSDavid Howells 	int ret;
1635ee18d64cSDavid Howells 
1636028db3e2SLinus Torvalds 	keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK);
1637ee18d64cSDavid Howells 	if (IS_ERR(keyring_r))
1638ee18d64cSDavid Howells 		return PTR_ERR(keyring_r);
1639ee18d64cSDavid Howells 
1640413cd3d9SOleg Nesterov 	ret = -ENOMEM;
1641413cd3d9SOleg Nesterov 
1642ee18d64cSDavid Howells 	/* our parent is going to need a new cred struct, a new tgcred struct
1643ee18d64cSDavid Howells 	 * and new security data, so we allocate them here to prevent ENOMEM in
1644ee18d64cSDavid Howells 	 * our parent */
1645ee18d64cSDavid Howells 	cred = cred_alloc_blank();
1646ee18d64cSDavid Howells 	if (!cred)
164767d12145SAl Viro 		goto error_keyring;
164867d12145SAl Viro 	newwork = &cred->rcu;
1649ee18d64cSDavid Howells 
16503a50597dSDavid Howells 	cred->session_keyring = key_ref_to_ptr(keyring_r);
16513a50597dSDavid Howells 	keyring_r = NULL;
165267d12145SAl Viro 	init_task_work(newwork, key_change_session_keyring);
1653ee18d64cSDavid Howells 
1654ee18d64cSDavid Howells 	me = current;
16559d1ac65aSDavid Howells 	rcu_read_lock();
1656ee18d64cSDavid Howells 	write_lock_irq(&tasklist_lock);
1657ee18d64cSDavid Howells 
1658ee18d64cSDavid Howells 	ret = -EPERM;
1659413cd3d9SOleg Nesterov 	oldwork = NULL;
16607936d16dSDavid Howells 	parent = rcu_dereference_protected(me->real_parent,
16617936d16dSDavid Howells 					   lockdep_is_held(&tasklist_lock));
1662ee18d64cSDavid Howells 
1663ee18d64cSDavid Howells 	/* the parent mustn't be init and mustn't be a kernel thread */
1664ee18d64cSDavid Howells 	if (parent->pid <= 1 || !parent->mm)
1665413cd3d9SOleg Nesterov 		goto unlock;
1666ee18d64cSDavid Howells 
1667ee18d64cSDavid Howells 	/* the parent must be single threaded */
1668dd98acf7SOleg Nesterov 	if (!thread_group_empty(parent))
1669413cd3d9SOleg Nesterov 		goto unlock;
1670ee18d64cSDavid Howells 
1671ee18d64cSDavid Howells 	/* the parent and the child must have different session keyrings or
1672ee18d64cSDavid Howells 	 * there's no point */
1673ee18d64cSDavid Howells 	mycred = current_cred();
1674ee18d64cSDavid Howells 	pcred = __task_cred(parent);
1675ee18d64cSDavid Howells 	if (mycred == pcred ||
16763a50597dSDavid Howells 	    mycred->session_keyring == pcred->session_keyring) {
1677413cd3d9SOleg Nesterov 		ret = 0;
1678413cd3d9SOleg Nesterov 		goto unlock;
1679413cd3d9SOleg Nesterov 	}
1680ee18d64cSDavid Howells 
1681ee18d64cSDavid Howells 	/* the parent must have the same effective ownership and mustn't be
1682ee18d64cSDavid Howells 	 * SUID/SGID */
16839a56c2dbSEric W. Biederman 	if (!uid_eq(pcred->uid,	 mycred->euid) ||
16849a56c2dbSEric W. Biederman 	    !uid_eq(pcred->euid, mycred->euid) ||
16859a56c2dbSEric W. Biederman 	    !uid_eq(pcred->suid, mycred->euid) ||
16869a56c2dbSEric W. Biederman 	    !gid_eq(pcred->gid,	 mycred->egid) ||
16879a56c2dbSEric W. Biederman 	    !gid_eq(pcred->egid, mycred->egid) ||
16889a56c2dbSEric W. Biederman 	    !gid_eq(pcred->sgid, mycred->egid))
1689413cd3d9SOleg Nesterov 		goto unlock;
1690ee18d64cSDavid Howells 
1691ee18d64cSDavid Howells 	/* the keyrings must have the same UID */
16923a50597dSDavid Howells 	if ((pcred->session_keyring &&
16932a74dbb9SLinus Torvalds 	     !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
16942a74dbb9SLinus Torvalds 	    !uid_eq(mycred->session_keyring->uid, mycred->euid))
1695413cd3d9SOleg Nesterov 		goto unlock;
1696ee18d64cSDavid Howells 
1697413cd3d9SOleg Nesterov 	/* cancel an already pending keyring replacement */
1698413cd3d9SOleg Nesterov 	oldwork = task_work_cancel(parent, key_change_session_keyring);
1699ee18d64cSDavid Howells 
1700ee18d64cSDavid Howells 	/* the replacement session keyring is applied just prior to userspace
1701ee18d64cSDavid Howells 	 * restarting */
170267d12145SAl Viro 	ret = task_work_add(parent, newwork, true);
1703413cd3d9SOleg Nesterov 	if (!ret)
1704413cd3d9SOleg Nesterov 		newwork = NULL;
1705413cd3d9SOleg Nesterov unlock:
1706ee18d64cSDavid Howells 	write_unlock_irq(&tasklist_lock);
17079d1ac65aSDavid Howells 	rcu_read_unlock();
170867d12145SAl Viro 	if (oldwork)
170967d12145SAl Viro 		put_cred(container_of(oldwork, struct cred, rcu));
171067d12145SAl Viro 	if (newwork)
171167d12145SAl Viro 		put_cred(cred);
1712ee18d64cSDavid Howells 	return ret;
1713ee18d64cSDavid Howells 
1714ee18d64cSDavid Howells error_keyring:
1715ee18d64cSDavid Howells 	key_ref_put(keyring_r);
1716ee18d64cSDavid Howells 	return ret;
1717ee18d64cSDavid Howells }
1718ee18d64cSDavid Howells 
1719b5f545c8SDavid Howells /*
17206563c91fSMat Martineau  * Apply a restriction to a given keyring.
17216563c91fSMat Martineau  *
17226563c91fSMat Martineau  * The caller must have Setattr permission to change keyring restrictions.
17236563c91fSMat Martineau  *
17246563c91fSMat Martineau  * The requested type name may be a NULL pointer to reject all attempts
172518026d86SEric Biggers  * to link to the keyring.  In this case, _restriction must also be NULL.
172618026d86SEric Biggers  * Otherwise, both _type and _restriction must be non-NULL.
17276563c91fSMat Martineau  *
17286563c91fSMat Martineau  * Returns 0 if successful.
17296563c91fSMat Martineau  */
17306563c91fSMat Martineau long keyctl_restrict_keyring(key_serial_t id, const char __user *_type,
17316563c91fSMat Martineau 			     const char __user *_restriction)
17326563c91fSMat Martineau {
17336563c91fSMat Martineau 	key_ref_t key_ref;
17346563c91fSMat Martineau 	char type[32];
17356563c91fSMat Martineau 	char *restriction = NULL;
17366563c91fSMat Martineau 	long ret;
17376563c91fSMat Martineau 
1738028db3e2SLinus Torvalds 	key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
17396563c91fSMat Martineau 	if (IS_ERR(key_ref))
17406563c91fSMat Martineau 		return PTR_ERR(key_ref);
17416563c91fSMat Martineau 
174218026d86SEric Biggers 	ret = -EINVAL;
17436563c91fSMat Martineau 	if (_type) {
174418026d86SEric Biggers 		if (!_restriction)
174518026d86SEric Biggers 			goto error;
174618026d86SEric Biggers 
17476563c91fSMat Martineau 		ret = key_get_type_from_user(type, _type, sizeof(type));
17486563c91fSMat Martineau 		if (ret < 0)
17496563c91fSMat Martineau 			goto error;
17506563c91fSMat Martineau 
17516563c91fSMat Martineau 		restriction = strndup_user(_restriction, PAGE_SIZE);
17526563c91fSMat Martineau 		if (IS_ERR(restriction)) {
17536563c91fSMat Martineau 			ret = PTR_ERR(restriction);
17546563c91fSMat Martineau 			goto error;
17556563c91fSMat Martineau 		}
175618026d86SEric Biggers 	} else {
175718026d86SEric Biggers 		if (_restriction)
175818026d86SEric Biggers 			goto error;
17596563c91fSMat Martineau 	}
17606563c91fSMat Martineau 
176118026d86SEric Biggers 	ret = keyring_restrict(key_ref, _type ? type : NULL, restriction);
17626563c91fSMat Martineau 	kfree(restriction);
17636563c91fSMat Martineau error:
17646563c91fSMat Martineau 	key_ref_put(key_ref);
17656563c91fSMat Martineau 	return ret;
17666563c91fSMat Martineau }
17676563c91fSMat Martineau 
1768f7e47677SDavid Howells #ifdef CONFIG_KEY_NOTIFICATIONS
1769f7e47677SDavid Howells /*
1770f7e47677SDavid Howells  * Watch for changes to a key.
1771f7e47677SDavid Howells  *
1772f7e47677SDavid Howells  * The caller must have View permission to watch a key or keyring.
1773f7e47677SDavid Howells  */
1774f7e47677SDavid Howells long keyctl_watch_key(key_serial_t id, int watch_queue_fd, int watch_id)
1775f7e47677SDavid Howells {
1776f7e47677SDavid Howells 	struct watch_queue *wqueue;
1777f7e47677SDavid Howells 	struct watch_list *wlist = NULL;
1778f7e47677SDavid Howells 	struct watch *watch = NULL;
1779f7e47677SDavid Howells 	struct key *key;
1780f7e47677SDavid Howells 	key_ref_t key_ref;
1781f7e47677SDavid Howells 	long ret;
1782f7e47677SDavid Howells 
1783f7e47677SDavid Howells 	if (watch_id < -1 || watch_id > 0xff)
1784f7e47677SDavid Howells 		return -EINVAL;
1785f7e47677SDavid Howells 
1786f7e47677SDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_VIEW);
1787f7e47677SDavid Howells 	if (IS_ERR(key_ref))
1788f7e47677SDavid Howells 		return PTR_ERR(key_ref);
1789f7e47677SDavid Howells 	key = key_ref_to_ptr(key_ref);
1790f7e47677SDavid Howells 
1791f7e47677SDavid Howells 	wqueue = get_watch_queue(watch_queue_fd);
1792f7e47677SDavid Howells 	if (IS_ERR(wqueue)) {
1793f7e47677SDavid Howells 		ret = PTR_ERR(wqueue);
1794f7e47677SDavid Howells 		goto err_key;
1795f7e47677SDavid Howells 	}
1796f7e47677SDavid Howells 
1797f7e47677SDavid Howells 	if (watch_id >= 0) {
1798f7e47677SDavid Howells 		ret = -ENOMEM;
1799f7e47677SDavid Howells 		if (!key->watchers) {
1800f7e47677SDavid Howells 			wlist = kzalloc(sizeof(*wlist), GFP_KERNEL);
1801f7e47677SDavid Howells 			if (!wlist)
1802f7e47677SDavid Howells 				goto err_wqueue;
1803f7e47677SDavid Howells 			init_watch_list(wlist, NULL);
1804f7e47677SDavid Howells 		}
1805f7e47677SDavid Howells 
1806f7e47677SDavid Howells 		watch = kzalloc(sizeof(*watch), GFP_KERNEL);
1807f7e47677SDavid Howells 		if (!watch)
1808f7e47677SDavid Howells 			goto err_wlist;
1809f7e47677SDavid Howells 
1810f7e47677SDavid Howells 		init_watch(watch, wqueue);
1811f7e47677SDavid Howells 		watch->id	= key->serial;
1812f7e47677SDavid Howells 		watch->info_id	= (u32)watch_id << WATCH_INFO_ID__SHIFT;
1813f7e47677SDavid Howells 
1814f7e47677SDavid Howells 		ret = security_watch_key(key);
1815f7e47677SDavid Howells 		if (ret < 0)
1816f7e47677SDavid Howells 			goto err_watch;
1817f7e47677SDavid Howells 
1818f7e47677SDavid Howells 		down_write(&key->sem);
1819f7e47677SDavid Howells 		if (!key->watchers) {
1820f7e47677SDavid Howells 			key->watchers = wlist;
1821f7e47677SDavid Howells 			wlist = NULL;
1822f7e47677SDavid Howells 		}
1823f7e47677SDavid Howells 
1824f7e47677SDavid Howells 		ret = add_watch_to_object(watch, key->watchers);
1825f7e47677SDavid Howells 		up_write(&key->sem);
1826f7e47677SDavid Howells 
1827f7e47677SDavid Howells 		if (ret == 0)
1828f7e47677SDavid Howells 			watch = NULL;
1829f7e47677SDavid Howells 	} else {
1830f7e47677SDavid Howells 		ret = -EBADSLT;
1831f7e47677SDavid Howells 		if (key->watchers) {
1832f7e47677SDavid Howells 			down_write(&key->sem);
1833f7e47677SDavid Howells 			ret = remove_watch_from_object(key->watchers,
1834f7e47677SDavid Howells 						       wqueue, key_serial(key),
1835f7e47677SDavid Howells 						       false);
1836f7e47677SDavid Howells 			up_write(&key->sem);
1837f7e47677SDavid Howells 		}
1838f7e47677SDavid Howells 	}
1839f7e47677SDavid Howells 
1840f7e47677SDavid Howells err_watch:
1841f7e47677SDavid Howells 	kfree(watch);
1842f7e47677SDavid Howells err_wlist:
1843f7e47677SDavid Howells 	kfree(wlist);
1844f7e47677SDavid Howells err_wqueue:
1845f7e47677SDavid Howells 	put_watch_queue(wqueue);
1846f7e47677SDavid Howells err_key:
1847f7e47677SDavid Howells 	key_put(key);
1848f7e47677SDavid Howells 	return ret;
1849f7e47677SDavid Howells }
1850f7e47677SDavid Howells #endif /* CONFIG_KEY_NOTIFICATIONS */
1851f7e47677SDavid Howells 
18526563c91fSMat Martineau /*
185345e0f30cSDavid Howells  * Get keyrings subsystem capabilities.
185445e0f30cSDavid Howells  */
185545e0f30cSDavid Howells long keyctl_capabilities(unsigned char __user *_buffer, size_t buflen)
185645e0f30cSDavid Howells {
185745e0f30cSDavid Howells 	size_t size = buflen;
185845e0f30cSDavid Howells 
185945e0f30cSDavid Howells 	if (size > 0) {
186045e0f30cSDavid Howells 		if (size > sizeof(keyrings_capabilities))
186145e0f30cSDavid Howells 			size = sizeof(keyrings_capabilities);
186245e0f30cSDavid Howells 		if (copy_to_user(_buffer, keyrings_capabilities, size) != 0)
186345e0f30cSDavid Howells 			return -EFAULT;
186445e0f30cSDavid Howells 		if (size < buflen &&
186545e0f30cSDavid Howells 		    clear_user(_buffer + size, buflen - size) != 0)
186645e0f30cSDavid Howells 			return -EFAULT;
186745e0f30cSDavid Howells 	}
186845e0f30cSDavid Howells 
186945e0f30cSDavid Howells 	return sizeof(keyrings_capabilities);
187045e0f30cSDavid Howells }
187145e0f30cSDavid Howells 
187245e0f30cSDavid Howells /*
1873973c9f4fSDavid Howells  * The key control system call
18741da177e4SLinus Torvalds  */
1875938bb9f5SHeiko Carstens SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1876938bb9f5SHeiko Carstens 		unsigned long, arg4, unsigned long, arg5)
18771da177e4SLinus Torvalds {
18781da177e4SLinus Torvalds 	switch (option) {
18791da177e4SLinus Torvalds 	case KEYCTL_GET_KEYRING_ID:
18801da177e4SLinus Torvalds 		return keyctl_get_keyring_ID((key_serial_t) arg2,
18811da177e4SLinus Torvalds 					     (int) arg3);
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds 	case KEYCTL_JOIN_SESSION_KEYRING:
18841da177e4SLinus Torvalds 		return keyctl_join_session_keyring((const char __user *) arg2);
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds 	case KEYCTL_UPDATE:
18871da177e4SLinus Torvalds 		return keyctl_update_key((key_serial_t) arg2,
18881da177e4SLinus Torvalds 					 (const void __user *) arg3,
18891da177e4SLinus Torvalds 					 (size_t) arg4);
18901da177e4SLinus Torvalds 
18911da177e4SLinus Torvalds 	case KEYCTL_REVOKE:
18921da177e4SLinus Torvalds 		return keyctl_revoke_key((key_serial_t) arg2);
18931da177e4SLinus Torvalds 
18941da177e4SLinus Torvalds 	case KEYCTL_DESCRIBE:
18951da177e4SLinus Torvalds 		return keyctl_describe_key((key_serial_t) arg2,
18961da177e4SLinus Torvalds 					   (char __user *) arg3,
18971da177e4SLinus Torvalds 					   (unsigned) arg4);
18981da177e4SLinus Torvalds 
18991da177e4SLinus Torvalds 	case KEYCTL_CLEAR:
19001da177e4SLinus Torvalds 		return keyctl_keyring_clear((key_serial_t) arg2);
19011da177e4SLinus Torvalds 
19021da177e4SLinus Torvalds 	case KEYCTL_LINK:
19031da177e4SLinus Torvalds 		return keyctl_keyring_link((key_serial_t) arg2,
19041da177e4SLinus Torvalds 					   (key_serial_t) arg3);
19051da177e4SLinus Torvalds 
19061da177e4SLinus Torvalds 	case KEYCTL_UNLINK:
19071da177e4SLinus Torvalds 		return keyctl_keyring_unlink((key_serial_t) arg2,
19081da177e4SLinus Torvalds 					     (key_serial_t) arg3);
19091da177e4SLinus Torvalds 
19101da177e4SLinus Torvalds 	case KEYCTL_SEARCH:
19111da177e4SLinus Torvalds 		return keyctl_keyring_search((key_serial_t) arg2,
19121da177e4SLinus Torvalds 					     (const char __user *) arg3,
19131da177e4SLinus Torvalds 					     (const char __user *) arg4,
19141da177e4SLinus Torvalds 					     (key_serial_t) arg5);
19151da177e4SLinus Torvalds 
19161da177e4SLinus Torvalds 	case KEYCTL_READ:
19171da177e4SLinus Torvalds 		return keyctl_read_key((key_serial_t) arg2,
19181da177e4SLinus Torvalds 				       (char __user *) arg3,
19191da177e4SLinus Torvalds 				       (size_t) arg4);
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds 	case KEYCTL_CHOWN:
19221da177e4SLinus Torvalds 		return keyctl_chown_key((key_serial_t) arg2,
19231da177e4SLinus Torvalds 					(uid_t) arg3,
19241da177e4SLinus Torvalds 					(gid_t) arg4);
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 	case KEYCTL_SETPERM:
19271da177e4SLinus Torvalds 		return keyctl_setperm_key((key_serial_t) arg2,
1928028db3e2SLinus Torvalds 					  (key_perm_t) arg3);
19291da177e4SLinus Torvalds 
19301da177e4SLinus Torvalds 	case KEYCTL_INSTANTIATE:
19311da177e4SLinus Torvalds 		return keyctl_instantiate_key((key_serial_t) arg2,
19321da177e4SLinus Torvalds 					      (const void __user *) arg3,
19331da177e4SLinus Torvalds 					      (size_t) arg4,
19341da177e4SLinus Torvalds 					      (key_serial_t) arg5);
19351da177e4SLinus Torvalds 
19361da177e4SLinus Torvalds 	case KEYCTL_NEGATE:
19371da177e4SLinus Torvalds 		return keyctl_negate_key((key_serial_t) arg2,
19381da177e4SLinus Torvalds 					 (unsigned) arg3,
19391da177e4SLinus Torvalds 					 (key_serial_t) arg4);
19401da177e4SLinus Torvalds 
19413e30148cSDavid Howells 	case KEYCTL_SET_REQKEY_KEYRING:
19423e30148cSDavid Howells 		return keyctl_set_reqkey_keyring(arg2);
19433e30148cSDavid Howells 
1944017679c4SDavid Howells 	case KEYCTL_SET_TIMEOUT:
1945017679c4SDavid Howells 		return keyctl_set_timeout((key_serial_t) arg2,
1946017679c4SDavid Howells 					  (unsigned) arg3);
1947017679c4SDavid Howells 
1948b5f545c8SDavid Howells 	case KEYCTL_ASSUME_AUTHORITY:
1949b5f545c8SDavid Howells 		return keyctl_assume_authority((key_serial_t) arg2);
1950b5f545c8SDavid Howells 
195170a5bb72SDavid Howells 	case KEYCTL_GET_SECURITY:
195270a5bb72SDavid Howells 		return keyctl_get_security((key_serial_t) arg2,
195390bd49abSJames Morris 					   (char __user *) arg3,
195470a5bb72SDavid Howells 					   (size_t) arg4);
195570a5bb72SDavid Howells 
1956ee18d64cSDavid Howells 	case KEYCTL_SESSION_TO_PARENT:
1957ee18d64cSDavid Howells 		return keyctl_session_to_parent();
1958ee18d64cSDavid Howells 
1959fdd1b945SDavid Howells 	case KEYCTL_REJECT:
1960fdd1b945SDavid Howells 		return keyctl_reject_key((key_serial_t) arg2,
1961fdd1b945SDavid Howells 					 (unsigned) arg3,
1962fdd1b945SDavid Howells 					 (unsigned) arg4,
1963fdd1b945SDavid Howells 					 (key_serial_t) arg5);
1964fdd1b945SDavid Howells 
1965ee009e4aSDavid Howells 	case KEYCTL_INSTANTIATE_IOV:
1966ee009e4aSDavid Howells 		return keyctl_instantiate_key_iov(
1967ee009e4aSDavid Howells 			(key_serial_t) arg2,
1968ee009e4aSDavid Howells 			(const struct iovec __user *) arg3,
1969ee009e4aSDavid Howells 			(unsigned) arg4,
1970ee009e4aSDavid Howells 			(key_serial_t) arg5);
1971ee009e4aSDavid Howells 
1972fd75815fSDavid Howells 	case KEYCTL_INVALIDATE:
1973fd75815fSDavid Howells 		return keyctl_invalidate_key((key_serial_t) arg2);
1974fd75815fSDavid Howells 
1975f36f8c75SDavid Howells 	case KEYCTL_GET_PERSISTENT:
1976f36f8c75SDavid Howells 		return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3);
1977f36f8c75SDavid Howells 
1978ddbb4114SMat Martineau 	case KEYCTL_DH_COMPUTE:
1979ddbb4114SMat Martineau 		return keyctl_dh_compute((struct keyctl_dh_params __user *) arg2,
19804693fc73SStephan Mueller 					 (char __user *) arg3, (size_t) arg4,
1981f1c316a3SStephan Mueller 					 (struct keyctl_kdf_params __user *) arg5);
1982ddbb4114SMat Martineau 
19836563c91fSMat Martineau 	case KEYCTL_RESTRICT_KEYRING:
19846563c91fSMat Martineau 		return keyctl_restrict_keyring((key_serial_t) arg2,
19856563c91fSMat Martineau 					       (const char __user *) arg3,
19866563c91fSMat Martineau 					       (const char __user *) arg4);
19871da177e4SLinus Torvalds 
198800d60fd3SDavid Howells 	case KEYCTL_PKEY_QUERY:
198900d60fd3SDavid Howells 		if (arg3 != 0)
199000d60fd3SDavid Howells 			return -EINVAL;
199100d60fd3SDavid Howells 		return keyctl_pkey_query((key_serial_t)arg2,
199200d60fd3SDavid Howells 					 (const char __user *)arg4,
1993468e91ceSBen Dooks 					 (struct keyctl_pkey_query __user *)arg5);
199400d60fd3SDavid Howells 
199500d60fd3SDavid Howells 	case KEYCTL_PKEY_ENCRYPT:
199600d60fd3SDavid Howells 	case KEYCTL_PKEY_DECRYPT:
199700d60fd3SDavid Howells 	case KEYCTL_PKEY_SIGN:
199800d60fd3SDavid Howells 		return keyctl_pkey_e_d_s(
199900d60fd3SDavid Howells 			option,
200000d60fd3SDavid Howells 			(const struct keyctl_pkey_params __user *)arg2,
200100d60fd3SDavid Howells 			(const char __user *)arg3,
200200d60fd3SDavid Howells 			(const void __user *)arg4,
200300d60fd3SDavid Howells 			(void __user *)arg5);
200400d60fd3SDavid Howells 
200500d60fd3SDavid Howells 	case KEYCTL_PKEY_VERIFY:
200600d60fd3SDavid Howells 		return keyctl_pkey_verify(
200700d60fd3SDavid Howells 			(const struct keyctl_pkey_params __user *)arg2,
200800d60fd3SDavid Howells 			(const char __user *)arg3,
200900d60fd3SDavid Howells 			(const void __user *)arg4,
201000d60fd3SDavid Howells 			(const void __user *)arg5);
201100d60fd3SDavid Howells 
2012ed0ac5c7SDavid Howells 	case KEYCTL_MOVE:
2013ed0ac5c7SDavid Howells 		return keyctl_keyring_move((key_serial_t)arg2,
2014ed0ac5c7SDavid Howells 					   (key_serial_t)arg3,
2015ed0ac5c7SDavid Howells 					   (key_serial_t)arg4,
2016ed0ac5c7SDavid Howells 					   (unsigned int)arg5);
2017ed0ac5c7SDavid Howells 
201845e0f30cSDavid Howells 	case KEYCTL_CAPABILITIES:
201945e0f30cSDavid Howells 		return keyctl_capabilities((unsigned char __user *)arg2, (size_t)arg3);
202045e0f30cSDavid Howells 
2021f7e47677SDavid Howells 	case KEYCTL_WATCH_KEY:
2022f7e47677SDavid Howells 		return keyctl_watch_key((key_serial_t)arg2, (int)arg3, (int)arg4);
2023f7e47677SDavid Howells 
20241da177e4SLinus Torvalds 	default:
20251da177e4SLinus Torvalds 		return -EOPNOTSUPP;
20261da177e4SLinus Torvalds 	}
20271da177e4SLinus Torvalds }
2028