xref: /openbmc/linux/security/keys/keyctl.c (revision 2e12256b)
1973c9f4fSDavid Howells /* Userspace key control operations
21da177e4SLinus Torvalds  *
33e30148cSDavid Howells  * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved.
41da177e4SLinus Torvalds  * Written by David Howells (dhowells@redhat.com)
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or
71da177e4SLinus Torvalds  * modify it under the terms of the GNU General Public License
81da177e4SLinus Torvalds  * as published by the Free Software Foundation; either version
91da177e4SLinus Torvalds  * 2 of the License, or (at your option) any later version.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/init.h>
131da177e4SLinus Torvalds #include <linux/sched.h>
1429930025SIngo Molnar #include <linux/sched/task.h>
151da177e4SLinus Torvalds #include <linux/slab.h>
161da177e4SLinus Torvalds #include <linux/syscalls.h>
1759e6b9c1SBryan Schumaker #include <linux/key.h>
181da177e4SLinus Torvalds #include <linux/keyctl.h>
191da177e4SLinus Torvalds #include <linux/fs.h>
20c59ede7bSRandy.Dunlap #include <linux/capability.h>
215b825c3aSIngo Molnar #include <linux/cred.h>
220cb409d9SDavi Arnaut #include <linux/string.h>
231da177e4SLinus Torvalds #include <linux/err.h>
2438bbca6bSDavid Howells #include <linux/vmalloc.h>
2570a5bb72SDavid Howells #include <linux/security.h>
26a27bb332SKent Overstreet #include <linux/uio.h>
277c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
28822ad64dSDavid Howells #include <keys/request_key_auth-type.h>
291da177e4SLinus Torvalds #include "internal.h"
301da177e4SLinus Torvalds 
31aa9d4437SDavid Howells #define KEY_MAX_DESC_SIZE 4096
32aa9d4437SDavid Howells 
33b206f281SDavid Howells static const unsigned char keyrings_capabilities[2] = {
3445e0f30cSDavid Howells 	[0] = (KEYCTL_CAPS0_CAPABILITIES |
3545e0f30cSDavid Howells 	       (IS_ENABLED(CONFIG_PERSISTENT_KEYRINGS)	? KEYCTL_CAPS0_PERSISTENT_KEYRINGS : 0) |
3645e0f30cSDavid Howells 	       (IS_ENABLED(CONFIG_KEY_DH_OPERATIONS)	? KEYCTL_CAPS0_DIFFIE_HELLMAN : 0) |
3745e0f30cSDavid Howells 	       (IS_ENABLED(CONFIG_ASYMMETRIC_KEY_TYPE)	? KEYCTL_CAPS0_PUBLIC_KEY : 0) |
3845e0f30cSDavid Howells 	       (IS_ENABLED(CONFIG_BIG_KEYS)		? KEYCTL_CAPS0_BIG_KEY : 0) |
3945e0f30cSDavid Howells 	       KEYCTL_CAPS0_INVALIDATE |
4045e0f30cSDavid Howells 	       KEYCTL_CAPS0_RESTRICT_KEYRING |
4145e0f30cSDavid Howells 	       KEYCTL_CAPS0_MOVE
4245e0f30cSDavid Howells 	       ),
433b6e4de0SDavid Howells 	[1] = (KEYCTL_CAPS1_NS_KEYRING_NAME |
443b6e4de0SDavid Howells 	       KEYCTL_CAPS1_NS_KEY_TAG),
4545e0f30cSDavid Howells };
4645e0f30cSDavid Howells 
470cb409d9SDavi Arnaut static int key_get_type_from_user(char *type,
480cb409d9SDavi Arnaut 				  const char __user *_type,
490cb409d9SDavi Arnaut 				  unsigned len)
500cb409d9SDavi Arnaut {
510cb409d9SDavi Arnaut 	int ret;
520cb409d9SDavi Arnaut 
530cb409d9SDavi Arnaut 	ret = strncpy_from_user(type, _type, len);
540cb409d9SDavi Arnaut 	if (ret < 0)
554303ef19SDan Carpenter 		return ret;
560cb409d9SDavi Arnaut 	if (ret == 0 || ret >= len)
570cb409d9SDavi Arnaut 		return -EINVAL;
5854e2c2c1SDavid Howells 	if (type[0] == '.')
5954e2c2c1SDavid Howells 		return -EPERM;
600cb409d9SDavi Arnaut 	type[len - 1] = '\0';
610cb409d9SDavi Arnaut 	return 0;
620cb409d9SDavi Arnaut }
630cb409d9SDavi Arnaut 
641da177e4SLinus Torvalds /*
65973c9f4fSDavid Howells  * Extract the description of a new key from userspace and either add it as a
66973c9f4fSDavid Howells  * new key to the specified keyring or update a matching key in that keyring.
67973c9f4fSDavid Howells  *
68cf7f601cSDavid Howells  * If the description is NULL or an empty string, the key type is asked to
69cf7f601cSDavid Howells  * generate one from the payload.
70cf7f601cSDavid Howells  *
71973c9f4fSDavid Howells  * The keyring must be writable so that we can attach the key to it.
72973c9f4fSDavid Howells  *
73973c9f4fSDavid Howells  * If successful, the new key's serial number is returned, otherwise an error
74973c9f4fSDavid Howells  * code is returned.
751da177e4SLinus Torvalds  */
761e7bfb21SHeiko Carstens SYSCALL_DEFINE5(add_key, const char __user *, _type,
771e7bfb21SHeiko Carstens 		const char __user *, _description,
781e7bfb21SHeiko Carstens 		const void __user *, _payload,
791e7bfb21SHeiko Carstens 		size_t, plen,
801e7bfb21SHeiko Carstens 		key_serial_t, ringid)
811da177e4SLinus Torvalds {
82664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref;
831da177e4SLinus Torvalds 	char type[32], *description;
841da177e4SLinus Torvalds 	void *payload;
850cb409d9SDavi Arnaut 	long ret;
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	ret = -EINVAL;
8838bbca6bSDavid Howells 	if (plen > 1024 * 1024 - 1)
891da177e4SLinus Torvalds 		goto error;
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	/* draw all the data into kernel space */
920cb409d9SDavi Arnaut 	ret = key_get_type_from_user(type, _type, sizeof(type));
931da177e4SLinus Torvalds 	if (ret < 0)
941da177e4SLinus Torvalds 		goto error;
951da177e4SLinus Torvalds 
96cf7f601cSDavid Howells 	description = NULL;
97cf7f601cSDavid Howells 	if (_description) {
98aa9d4437SDavid Howells 		description = strndup_user(_description, KEY_MAX_DESC_SIZE);
990cb409d9SDavi Arnaut 		if (IS_ERR(description)) {
1000cb409d9SDavi Arnaut 			ret = PTR_ERR(description);
1013e30148cSDavid Howells 			goto error;
1020cb409d9SDavi Arnaut 		}
103cf7f601cSDavid Howells 		if (!*description) {
104cf7f601cSDavid Howells 			kfree(description);
105cf7f601cSDavid Howells 			description = NULL;
106a4e3b8d7SMimi Zohar 		} else if ((description[0] == '.') &&
107a4e3b8d7SMimi Zohar 			   (strncmp(type, "keyring", 7) == 0)) {
108a4e3b8d7SMimi Zohar 			ret = -EPERM;
109a4e3b8d7SMimi Zohar 			goto error2;
110cf7f601cSDavid Howells 		}
111cf7f601cSDavid Howells 	}
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
1141da177e4SLinus Torvalds 	payload = NULL;
1151da177e4SLinus Torvalds 
1165649645dSEric Biggers 	if (plen) {
1171da177e4SLinus Torvalds 		ret = -ENOMEM;
118752ade68SMichal Hocko 		payload = kvmalloc(plen, GFP_KERNEL);
1191da177e4SLinus Torvalds 		if (!payload)
1201da177e4SLinus Torvalds 			goto error2;
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 		ret = -EFAULT;
1231da177e4SLinus Torvalds 		if (copy_from_user(payload, _payload, plen) != 0)
1241da177e4SLinus Torvalds 			goto error3;
1251da177e4SLinus Torvalds 	}
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	/* find the target keyring (which must be writable) */
128f5895943SDavid Howells 	keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
129664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
130664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
1311da177e4SLinus Torvalds 		goto error3;
1321da177e4SLinus Torvalds 	}
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	/* create or update the requested key and add it to the target
1351da177e4SLinus Torvalds 	 * keyring */
136664cceb0SDavid Howells 	key_ref = key_create_or_update(keyring_ref, type, description,
137*2e12256bSDavid Howells 				       payload, plen, NULL, KEY_ALLOC_IN_QUOTA);
138664cceb0SDavid Howells 	if (!IS_ERR(key_ref)) {
139664cceb0SDavid Howells 		ret = key_ref_to_ptr(key_ref)->serial;
140664cceb0SDavid Howells 		key_ref_put(key_ref);
1411da177e4SLinus Torvalds 	}
1421da177e4SLinus Torvalds 	else {
143664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
1441da177e4SLinus Torvalds 	}
1451da177e4SLinus Torvalds 
146664cceb0SDavid Howells 	key_ref_put(keyring_ref);
1471da177e4SLinus Torvalds  error3:
14857070c85SEric Biggers 	if (payload) {
14957070c85SEric Biggers 		memzero_explicit(payload, plen);
150d0e0eba0SGeliang Tang 		kvfree(payload);
15157070c85SEric Biggers 	}
1521da177e4SLinus Torvalds  error2:
1531da177e4SLinus Torvalds 	kfree(description);
1541da177e4SLinus Torvalds  error:
1551da177e4SLinus Torvalds 	return ret;
156a8b17ed0SDavid Howells }
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds /*
159973c9f4fSDavid Howells  * Search the process keyrings and keyring trees linked from those for a
160973c9f4fSDavid Howells  * matching key.  Keyrings must have appropriate Search permission to be
161973c9f4fSDavid Howells  * searched.
162973c9f4fSDavid Howells  *
163973c9f4fSDavid Howells  * If a key is found, it will be attached to the destination keyring if there's
164973c9f4fSDavid Howells  * one specified and the serial number of the key will be returned.
165973c9f4fSDavid Howells  *
166973c9f4fSDavid Howells  * If no key is found, /sbin/request-key will be invoked if _callout_info is
167973c9f4fSDavid Howells  * non-NULL in an attempt to create a key.  The _callout_info string will be
168973c9f4fSDavid Howells  * passed to /sbin/request-key to aid with completing the request.  If the
169973c9f4fSDavid Howells  * _callout_info string is "" then it will be changed to "-".
1701da177e4SLinus Torvalds  */
1711e7bfb21SHeiko Carstens SYSCALL_DEFINE4(request_key, const char __user *, _type,
1721e7bfb21SHeiko Carstens 		const char __user *, _description,
1731e7bfb21SHeiko Carstens 		const char __user *, _callout_info,
1741e7bfb21SHeiko Carstens 		key_serial_t, destringid)
1751da177e4SLinus Torvalds {
1761da177e4SLinus Torvalds 	struct key_type *ktype;
177664cceb0SDavid Howells 	struct key *key;
178664cceb0SDavid Howells 	key_ref_t dest_ref;
1794a38e122SDavid Howells 	size_t callout_len;
1801da177e4SLinus Torvalds 	char type[32], *description, *callout_info;
1810cb409d9SDavi Arnaut 	long ret;
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	/* pull the type into kernel space */
1840cb409d9SDavi Arnaut 	ret = key_get_type_from_user(type, _type, sizeof(type));
1851da177e4SLinus Torvalds 	if (ret < 0)
1861da177e4SLinus Torvalds 		goto error;
1871260f801SDavid Howells 
1881da177e4SLinus Torvalds 	/* pull the description into kernel space */
189aa9d4437SDavid Howells 	description = strndup_user(_description, KEY_MAX_DESC_SIZE);
1900cb409d9SDavi Arnaut 	if (IS_ERR(description)) {
1910cb409d9SDavi Arnaut 		ret = PTR_ERR(description);
1921da177e4SLinus Torvalds 		goto error;
1930cb409d9SDavi Arnaut 	}
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	/* pull the callout info into kernel space */
1961da177e4SLinus Torvalds 	callout_info = NULL;
1974a38e122SDavid Howells 	callout_len = 0;
1981da177e4SLinus Torvalds 	if (_callout_info) {
1990cb409d9SDavi Arnaut 		callout_info = strndup_user(_callout_info, PAGE_SIZE);
2000cb409d9SDavi Arnaut 		if (IS_ERR(callout_info)) {
2010cb409d9SDavi Arnaut 			ret = PTR_ERR(callout_info);
2021da177e4SLinus Torvalds 			goto error2;
2030cb409d9SDavi Arnaut 		}
2044a38e122SDavid Howells 		callout_len = strlen(callout_info);
2051da177e4SLinus Torvalds 	}
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	/* get the destination keyring if specified */
208664cceb0SDavid Howells 	dest_ref = NULL;
2091da177e4SLinus Torvalds 	if (destringid) {
2105593122eSDavid Howells 		dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
211f5895943SDavid Howells 					   KEY_NEED_WRITE);
212664cceb0SDavid Howells 		if (IS_ERR(dest_ref)) {
213664cceb0SDavid Howells 			ret = PTR_ERR(dest_ref);
2141da177e4SLinus Torvalds 			goto error3;
2151da177e4SLinus Torvalds 		}
2161da177e4SLinus Torvalds 	}
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	/* find the key type */
2191da177e4SLinus Torvalds 	ktype = key_type_lookup(type);
2201da177e4SLinus Torvalds 	if (IS_ERR(ktype)) {
2211da177e4SLinus Torvalds 		ret = PTR_ERR(ktype);
2221da177e4SLinus Torvalds 		goto error4;
2231da177e4SLinus Torvalds 	}
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	/* do the search */
226a58946c1SDavid Howells 	key = request_key_and_link(ktype, description, NULL, callout_info,
227*2e12256bSDavid Howells 				   callout_len, NULL, NULL,
228*2e12256bSDavid Howells 				   key_ref_to_ptr(dest_ref),
2297e047ef5SDavid Howells 				   KEY_ALLOC_IN_QUOTA);
2301da177e4SLinus Torvalds 	if (IS_ERR(key)) {
2311da177e4SLinus Torvalds 		ret = PTR_ERR(key);
2321da177e4SLinus Torvalds 		goto error5;
2331da177e4SLinus Torvalds 	}
2341da177e4SLinus Torvalds 
2354aab1e89SDavid Howells 	/* wait for the key to finish being constructed */
2364aab1e89SDavid Howells 	ret = wait_for_key_construction(key, 1);
2374aab1e89SDavid Howells 	if (ret < 0)
2384aab1e89SDavid Howells 		goto error6;
2394aab1e89SDavid Howells 
2401da177e4SLinus Torvalds 	ret = key->serial;
2411da177e4SLinus Torvalds 
2424aab1e89SDavid Howells error6:
2431da177e4SLinus Torvalds  	key_put(key);
2441da177e4SLinus Torvalds error5:
2451da177e4SLinus Torvalds 	key_type_put(ktype);
2461da177e4SLinus Torvalds error4:
247664cceb0SDavid Howells 	key_ref_put(dest_ref);
2481da177e4SLinus Torvalds error3:
2491da177e4SLinus Torvalds 	kfree(callout_info);
2501da177e4SLinus Torvalds error2:
2511da177e4SLinus Torvalds 	kfree(description);
2521da177e4SLinus Torvalds error:
2531da177e4SLinus Torvalds 	return ret;
254a8b17ed0SDavid Howells }
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds /*
257973c9f4fSDavid Howells  * Get the ID of the specified process keyring.
258973c9f4fSDavid Howells  *
259973c9f4fSDavid Howells  * The requested keyring must have search permission to be found.
260973c9f4fSDavid Howells  *
261973c9f4fSDavid Howells  * If successful, the ID of the requested keyring will be returned.
2621da177e4SLinus Torvalds  */
2631da177e4SLinus Torvalds long keyctl_get_keyring_ID(key_serial_t id, int create)
2641da177e4SLinus Torvalds {
265664cceb0SDavid Howells 	key_ref_t key_ref;
2665593122eSDavid Howells 	unsigned long lflags;
2671da177e4SLinus Torvalds 	long ret;
2681da177e4SLinus Torvalds 
2695593122eSDavid Howells 	lflags = create ? KEY_LOOKUP_CREATE : 0;
270f5895943SDavid Howells 	key_ref = lookup_user_key(id, lflags, KEY_NEED_SEARCH);
271664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
272664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
2731da177e4SLinus Torvalds 		goto error;
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds 
276664cceb0SDavid Howells 	ret = key_ref_to_ptr(key_ref)->serial;
277664cceb0SDavid Howells 	key_ref_put(key_ref);
2781da177e4SLinus Torvalds error:
2791da177e4SLinus Torvalds 	return ret;
280973c9f4fSDavid Howells }
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds /*
283973c9f4fSDavid Howells  * Join a (named) session keyring.
284973c9f4fSDavid Howells  *
285973c9f4fSDavid Howells  * Create and join an anonymous session keyring or join a named session
286973c9f4fSDavid Howells  * keyring, creating it if necessary.  A named session keyring must have Search
287973c9f4fSDavid Howells  * permission for it to be joined.  Session keyrings without this permit will
288ee8f844eSDavid Howells  * be skipped over.  It is not permitted for userspace to create or join
289ee8f844eSDavid Howells  * keyrings whose name begin with a dot.
290973c9f4fSDavid Howells  *
291973c9f4fSDavid Howells  * If successful, the ID of the joined session keyring will be returned.
2921da177e4SLinus Torvalds  */
2931da177e4SLinus Torvalds long keyctl_join_session_keyring(const char __user *_name)
2941da177e4SLinus Torvalds {
2951da177e4SLinus Torvalds 	char *name;
2960cb409d9SDavi Arnaut 	long ret;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	/* fetch the name from userspace */
2991da177e4SLinus Torvalds 	name = NULL;
3001da177e4SLinus Torvalds 	if (_name) {
301aa9d4437SDavid Howells 		name = strndup_user(_name, KEY_MAX_DESC_SIZE);
3020cb409d9SDavi Arnaut 		if (IS_ERR(name)) {
3030cb409d9SDavi Arnaut 			ret = PTR_ERR(name);
3041da177e4SLinus Torvalds 			goto error;
3050cb409d9SDavi Arnaut 		}
306ee8f844eSDavid Howells 
307ee8f844eSDavid Howells 		ret = -EPERM;
308ee8f844eSDavid Howells 		if (name[0] == '.')
309ee8f844eSDavid Howells 			goto error_name;
3101da177e4SLinus Torvalds 	}
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	/* join the session */
3131da177e4SLinus Torvalds 	ret = join_session_keyring(name);
314ee8f844eSDavid Howells error_name:
3150d54ee1cSVegard Nossum 	kfree(name);
3161da177e4SLinus Torvalds error:
3171da177e4SLinus Torvalds 	return ret;
318a8b17ed0SDavid Howells }
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds /*
321973c9f4fSDavid Howells  * Update a key's data payload from the given data.
322973c9f4fSDavid Howells  *
323973c9f4fSDavid Howells  * The key must grant the caller Write permission and the key type must support
324973c9f4fSDavid Howells  * updating for this to work.  A negative key can be positively instantiated
325973c9f4fSDavid Howells  * with this call.
326973c9f4fSDavid Howells  *
327973c9f4fSDavid Howells  * If successful, 0 will be returned.  If the key type does not support
328973c9f4fSDavid Howells  * updating, then -EOPNOTSUPP will be returned.
3291da177e4SLinus Torvalds  */
3301da177e4SLinus Torvalds long keyctl_update_key(key_serial_t id,
3311da177e4SLinus Torvalds 		       const void __user *_payload,
3321da177e4SLinus Torvalds 		       size_t plen)
3331da177e4SLinus Torvalds {
334664cceb0SDavid Howells 	key_ref_t key_ref;
3351da177e4SLinus Torvalds 	void *payload;
3361da177e4SLinus Torvalds 	long ret;
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	ret = -EINVAL;
3391da177e4SLinus Torvalds 	if (plen > PAGE_SIZE)
3401da177e4SLinus Torvalds 		goto error;
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
3431da177e4SLinus Torvalds 	payload = NULL;
3445649645dSEric Biggers 	if (plen) {
3451da177e4SLinus Torvalds 		ret = -ENOMEM;
3461da177e4SLinus Torvalds 		payload = kmalloc(plen, GFP_KERNEL);
3471da177e4SLinus Torvalds 		if (!payload)
3481da177e4SLinus Torvalds 			goto error;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 		ret = -EFAULT;
3511da177e4SLinus Torvalds 		if (copy_from_user(payload, _payload, plen) != 0)
3521da177e4SLinus Torvalds 			goto error2;
3531da177e4SLinus Torvalds 	}
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	/* find the target key (which must be writable) */
356f5895943SDavid Howells 	key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
357664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
358664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
3591da177e4SLinus Torvalds 		goto error2;
3601da177e4SLinus Torvalds 	}
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	/* update the key */
363664cceb0SDavid Howells 	ret = key_update(key_ref, payload, plen);
3641da177e4SLinus Torvalds 
365664cceb0SDavid Howells 	key_ref_put(key_ref);
3661da177e4SLinus Torvalds error2:
36757070c85SEric Biggers 	kzfree(payload);
3681da177e4SLinus Torvalds error:
3691da177e4SLinus Torvalds 	return ret;
370a8b17ed0SDavid Howells }
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds /*
373973c9f4fSDavid Howells  * Revoke a key.
374973c9f4fSDavid Howells  *
375973c9f4fSDavid Howells  * The key must be grant the caller Write or Setattr permission for this to
376973c9f4fSDavid Howells  * work.  The key type should give up its quota claim when revoked.  The key
377973c9f4fSDavid Howells  * and any links to the key will be automatically garbage collected after a
378973c9f4fSDavid Howells  * certain amount of time (/proc/sys/kernel/keys/gc_delay).
379973c9f4fSDavid Howells  *
380d3600bcfSMimi Zohar  * Keys with KEY_FLAG_KEEP set should not be revoked.
381d3600bcfSMimi Zohar  *
382973c9f4fSDavid Howells  * If successful, 0 is returned.
3831da177e4SLinus Torvalds  */
3841da177e4SLinus Torvalds long keyctl_revoke_key(key_serial_t id)
3851da177e4SLinus Torvalds {
386664cceb0SDavid Howells 	key_ref_t key_ref;
387d3600bcfSMimi Zohar 	struct key *key;
3881da177e4SLinus Torvalds 	long ret;
3891da177e4SLinus Torvalds 
390*2e12256bSDavid Howells 	key_ref = lookup_user_key(id, 0, KEY_NEED_REVOKE);
3910c2c9a3fSDavid Howells 	if (IS_ERR(key_ref)) {
3920c2c9a3fSDavid Howells 		ret = PTR_ERR(key_ref);
3930c2c9a3fSDavid Howells 		goto error;
3940c2c9a3fSDavid Howells 	}
3951da177e4SLinus Torvalds 
396d3600bcfSMimi Zohar 	key = key_ref_to_ptr(key_ref);
3971da177e4SLinus Torvalds 	ret = 0;
3981d6d167cSMimi Zohar 	if (test_bit(KEY_FLAG_KEEP, &key->flags))
3991d6d167cSMimi Zohar 		ret = -EPERM;
4001d6d167cSMimi Zohar 	else
4011d6d167cSMimi Zohar 		key_revoke(key);
4021da177e4SLinus Torvalds 
403664cceb0SDavid Howells 	key_ref_put(key_ref);
4041da177e4SLinus Torvalds error:
4051260f801SDavid Howells 	return ret;
406a8b17ed0SDavid Howells }
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds /*
409fd75815fSDavid Howells  * Invalidate a key.
410fd75815fSDavid Howells  *
411fd75815fSDavid Howells  * The key must be grant the caller Invalidate permission for this to work.
412fd75815fSDavid Howells  * The key and any links to the key will be automatically garbage collected
413fd75815fSDavid Howells  * immediately.
414fd75815fSDavid Howells  *
415d3600bcfSMimi Zohar  * Keys with KEY_FLAG_KEEP set should not be invalidated.
416d3600bcfSMimi Zohar  *
417fd75815fSDavid Howells  * If successful, 0 is returned.
418fd75815fSDavid Howells  */
419fd75815fSDavid Howells long keyctl_invalidate_key(key_serial_t id)
420fd75815fSDavid Howells {
421fd75815fSDavid Howells 	key_ref_t key_ref;
422d3600bcfSMimi Zohar 	struct key *key;
423fd75815fSDavid Howells 	long ret;
424fd75815fSDavid Howells 
425fd75815fSDavid Howells 	kenter("%d", id);
426fd75815fSDavid Howells 
427*2e12256bSDavid Howells 	key_ref = lookup_user_key(id, 0, KEY_NEED_INVAL);
428fd75815fSDavid Howells 	if (IS_ERR(key_ref)) {
429fd75815fSDavid Howells 		ret = PTR_ERR(key_ref);
4300c7774abSDavid Howells 
4310c7774abSDavid Howells 		/* Root is permitted to invalidate certain special keys */
4320c7774abSDavid Howells 		if (capable(CAP_SYS_ADMIN)) {
4330c7774abSDavid Howells 			key_ref = lookup_user_key(id, 0, 0);
4340c7774abSDavid Howells 			if (IS_ERR(key_ref))
4350c7774abSDavid Howells 				goto error;
4360c7774abSDavid Howells 			if (test_bit(KEY_FLAG_ROOT_CAN_INVAL,
4370c7774abSDavid Howells 				     &key_ref_to_ptr(key_ref)->flags))
4380c7774abSDavid Howells 				goto invalidate;
4390c7774abSDavid Howells 			goto error_put;
4400c7774abSDavid Howells 		}
4410c7774abSDavid Howells 
442fd75815fSDavid Howells 		goto error;
443fd75815fSDavid Howells 	}
444fd75815fSDavid Howells 
4450c7774abSDavid Howells invalidate:
446d3600bcfSMimi Zohar 	key = key_ref_to_ptr(key_ref);
447fd75815fSDavid Howells 	ret = 0;
448d3600bcfSMimi Zohar 	if (test_bit(KEY_FLAG_KEEP, &key->flags))
449d3600bcfSMimi Zohar 		ret = -EPERM;
4501d6d167cSMimi Zohar 	else
451d3600bcfSMimi Zohar 		key_invalidate(key);
4520c7774abSDavid Howells error_put:
453fd75815fSDavid Howells 	key_ref_put(key_ref);
454fd75815fSDavid Howells error:
455fd75815fSDavid Howells 	kleave(" = %ld", ret);
456fd75815fSDavid Howells 	return ret;
457fd75815fSDavid Howells }
458fd75815fSDavid Howells 
459fd75815fSDavid Howells /*
460973c9f4fSDavid Howells  * Clear the specified keyring, creating an empty process keyring if one of the
461973c9f4fSDavid Howells  * special keyring IDs is used.
462973c9f4fSDavid Howells  *
463d3600bcfSMimi Zohar  * The keyring must grant the caller Write permission and not have
464d3600bcfSMimi Zohar  * KEY_FLAG_KEEP set for this to work.  If successful, 0 will be returned.
4651da177e4SLinus Torvalds  */
4661da177e4SLinus Torvalds long keyctl_keyring_clear(key_serial_t ringid)
4671da177e4SLinus Torvalds {
468664cceb0SDavid Howells 	key_ref_t keyring_ref;
469d3600bcfSMimi Zohar 	struct key *keyring;
4701da177e4SLinus Torvalds 	long ret;
4711da177e4SLinus Torvalds 
472*2e12256bSDavid Howells 	keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_CLEAR);
473664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
474664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
475700920ebSDavid Howells 
476700920ebSDavid Howells 		/* Root is permitted to invalidate certain special keyrings */
477700920ebSDavid Howells 		if (capable(CAP_SYS_ADMIN)) {
478700920ebSDavid Howells 			keyring_ref = lookup_user_key(ringid, 0, 0);
479700920ebSDavid Howells 			if (IS_ERR(keyring_ref))
480700920ebSDavid Howells 				goto error;
481700920ebSDavid Howells 			if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
482700920ebSDavid Howells 				     &key_ref_to_ptr(keyring_ref)->flags))
483700920ebSDavid Howells 				goto clear;
484700920ebSDavid Howells 			goto error_put;
485700920ebSDavid Howells 		}
486700920ebSDavid Howells 
4871da177e4SLinus Torvalds 		goto error;
4881da177e4SLinus Torvalds 	}
4891da177e4SLinus Torvalds 
490700920ebSDavid Howells clear:
491d3600bcfSMimi Zohar 	keyring = key_ref_to_ptr(keyring_ref);
492d3600bcfSMimi Zohar 	if (test_bit(KEY_FLAG_KEEP, &keyring->flags))
493d3600bcfSMimi Zohar 		ret = -EPERM;
494d3600bcfSMimi Zohar 	else
495d3600bcfSMimi Zohar 		ret = keyring_clear(keyring);
496700920ebSDavid Howells error_put:
497664cceb0SDavid Howells 	key_ref_put(keyring_ref);
4981da177e4SLinus Torvalds error:
4991da177e4SLinus Torvalds 	return ret;
500a8b17ed0SDavid Howells }
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds /*
503973c9f4fSDavid Howells  * Create a link from a keyring to a key if there's no matching key in the
504973c9f4fSDavid Howells  * keyring, otherwise replace the link to the matching key with a link to the
505973c9f4fSDavid Howells  * new key.
506973c9f4fSDavid Howells  *
507973c9f4fSDavid Howells  * The key must grant the caller Link permission and the the keyring must grant
508973c9f4fSDavid Howells  * the caller Write permission.  Furthermore, if an additional link is created,
509973c9f4fSDavid Howells  * the keyring's quota will be extended.
510973c9f4fSDavid Howells  *
511973c9f4fSDavid Howells  * If successful, 0 will be returned.
5121da177e4SLinus Torvalds  */
5131da177e4SLinus Torvalds long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
5141da177e4SLinus Torvalds {
515664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref;
5161da177e4SLinus Torvalds 	long ret;
5171da177e4SLinus Torvalds 
518f5895943SDavid Howells 	keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
519664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
520664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
5211da177e4SLinus Torvalds 		goto error;
5221da177e4SLinus Torvalds 	}
5231da177e4SLinus Torvalds 
524f5895943SDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
525664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
526664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
5271da177e4SLinus Torvalds 		goto error2;
5281da177e4SLinus Torvalds 	}
5291da177e4SLinus Torvalds 
530664cceb0SDavid Howells 	ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
5311da177e4SLinus Torvalds 
532664cceb0SDavid Howells 	key_ref_put(key_ref);
5331da177e4SLinus Torvalds error2:
534664cceb0SDavid Howells 	key_ref_put(keyring_ref);
5351da177e4SLinus Torvalds error:
5361da177e4SLinus Torvalds 	return ret;
537a8b17ed0SDavid Howells }
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds /*
540973c9f4fSDavid Howells  * Unlink a key from a keyring.
541973c9f4fSDavid Howells  *
542973c9f4fSDavid Howells  * The keyring must grant the caller Write permission for this to work; the key
543973c9f4fSDavid Howells  * itself need not grant the caller anything.  If the last link to a key is
544973c9f4fSDavid Howells  * removed then that key will be scheduled for destruction.
545973c9f4fSDavid Howells  *
546d3600bcfSMimi Zohar  * Keys or keyrings with KEY_FLAG_KEEP set should not be unlinked.
547d3600bcfSMimi Zohar  *
548973c9f4fSDavid Howells  * If successful, 0 will be returned.
5491da177e4SLinus Torvalds  */
5501da177e4SLinus Torvalds long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
5511da177e4SLinus Torvalds {
552664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref;
553d3600bcfSMimi Zohar 	struct key *keyring, *key;
5541da177e4SLinus Torvalds 	long ret;
5551da177e4SLinus Torvalds 
556f5895943SDavid Howells 	keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE);
557664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
558664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
5591da177e4SLinus Torvalds 		goto error;
5601da177e4SLinus Torvalds 	}
5611da177e4SLinus Torvalds 
5625593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
563664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
564664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
5651da177e4SLinus Torvalds 		goto error2;
5661da177e4SLinus Torvalds 	}
5671da177e4SLinus Torvalds 
568d3600bcfSMimi Zohar 	keyring = key_ref_to_ptr(keyring_ref);
569d3600bcfSMimi Zohar 	key = key_ref_to_ptr(key_ref);
570d3600bcfSMimi Zohar 	if (test_bit(KEY_FLAG_KEEP, &keyring->flags) &&
571d3600bcfSMimi Zohar 	    test_bit(KEY_FLAG_KEEP, &key->flags))
572d3600bcfSMimi Zohar 		ret = -EPERM;
573d3600bcfSMimi Zohar 	else
574d3600bcfSMimi Zohar 		ret = key_unlink(keyring, key);
5751da177e4SLinus Torvalds 
576664cceb0SDavid Howells 	key_ref_put(key_ref);
5771da177e4SLinus Torvalds error2:
578664cceb0SDavid Howells 	key_ref_put(keyring_ref);
5791da177e4SLinus Torvalds error:
5801da177e4SLinus Torvalds 	return ret;
581a8b17ed0SDavid Howells }
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds /*
584ed0ac5c7SDavid Howells  * Move a link to a key from one keyring to another, displacing any matching
585ed0ac5c7SDavid Howells  * key from the destination keyring.
586ed0ac5c7SDavid Howells  *
587ed0ac5c7SDavid Howells  * The key must grant the caller Link permission and both keyrings must grant
588ed0ac5c7SDavid Howells  * the caller Write permission.  There must also be a link in the from keyring
589ed0ac5c7SDavid Howells  * to the key.  If both keyrings are the same, nothing is done.
590ed0ac5c7SDavid Howells  *
591ed0ac5c7SDavid Howells  * If successful, 0 will be returned.
592ed0ac5c7SDavid Howells  */
593ed0ac5c7SDavid Howells long keyctl_keyring_move(key_serial_t id, key_serial_t from_ringid,
594ed0ac5c7SDavid Howells 			 key_serial_t to_ringid, unsigned int flags)
595ed0ac5c7SDavid Howells {
596ed0ac5c7SDavid Howells 	key_ref_t key_ref, from_ref, to_ref;
597ed0ac5c7SDavid Howells 	long ret;
598ed0ac5c7SDavid Howells 
599ed0ac5c7SDavid Howells 	if (flags & ~KEYCTL_MOVE_EXCL)
600ed0ac5c7SDavid Howells 		return -EINVAL;
601ed0ac5c7SDavid Howells 
602ed0ac5c7SDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
603ed0ac5c7SDavid Howells 	if (IS_ERR(key_ref))
604ed0ac5c7SDavid Howells 		return PTR_ERR(key_ref);
605ed0ac5c7SDavid Howells 
606ed0ac5c7SDavid Howells 	from_ref = lookup_user_key(from_ringid, 0, KEY_NEED_WRITE);
607ed0ac5c7SDavid Howells 	if (IS_ERR(from_ref)) {
608ed0ac5c7SDavid Howells 		ret = PTR_ERR(from_ref);
609ed0ac5c7SDavid Howells 		goto error2;
610ed0ac5c7SDavid Howells 	}
611ed0ac5c7SDavid Howells 
612ed0ac5c7SDavid Howells 	to_ref = lookup_user_key(to_ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
613ed0ac5c7SDavid Howells 	if (IS_ERR(to_ref)) {
614ed0ac5c7SDavid Howells 		ret = PTR_ERR(to_ref);
615ed0ac5c7SDavid Howells 		goto error3;
616ed0ac5c7SDavid Howells 	}
617ed0ac5c7SDavid Howells 
618ed0ac5c7SDavid Howells 	ret = key_move(key_ref_to_ptr(key_ref), key_ref_to_ptr(from_ref),
619ed0ac5c7SDavid Howells 		       key_ref_to_ptr(to_ref), flags);
620ed0ac5c7SDavid Howells 
621ed0ac5c7SDavid Howells 	key_ref_put(to_ref);
622ed0ac5c7SDavid Howells error3:
623ed0ac5c7SDavid Howells 	key_ref_put(from_ref);
624ed0ac5c7SDavid Howells error2:
625ed0ac5c7SDavid Howells 	key_ref_put(key_ref);
626ed0ac5c7SDavid Howells 	return ret;
627ed0ac5c7SDavid Howells }
628ed0ac5c7SDavid Howells 
629ed0ac5c7SDavid Howells /*
630973c9f4fSDavid Howells  * Return a description of a key to userspace.
631973c9f4fSDavid Howells  *
632973c9f4fSDavid Howells  * The key must grant the caller View permission for this to work.
633973c9f4fSDavid Howells  *
634973c9f4fSDavid Howells  * If there's a buffer, we place up to buflen bytes of data into it formatted
635973c9f4fSDavid Howells  * in the following way:
636973c9f4fSDavid Howells  *
6371da177e4SLinus Torvalds  *	type;uid;gid;perm;description<NUL>
638973c9f4fSDavid Howells  *
639973c9f4fSDavid Howells  * If successful, we return the amount of description available, irrespective
640973c9f4fSDavid Howells  * of how much we may have copied into the buffer.
6411da177e4SLinus Torvalds  */
6421da177e4SLinus Torvalds long keyctl_describe_key(key_serial_t keyid,
6431da177e4SLinus Torvalds 			 char __user *buffer,
6441da177e4SLinus Torvalds 			 size_t buflen)
6451da177e4SLinus Torvalds {
6463e30148cSDavid Howells 	struct key *key, *instkey;
647*2e12256bSDavid Howells 	unsigned int perm;
648664cceb0SDavid Howells 	key_ref_t key_ref;
649aa9d4437SDavid Howells 	char *infobuf;
6501da177e4SLinus Torvalds 	long ret;
651aa9d4437SDavid Howells 	int desclen, infolen;
6521da177e4SLinus Torvalds 
653f5895943SDavid Howells 	key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
654664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
6553e30148cSDavid Howells 		/* viewing a key under construction is permitted if we have the
6563e30148cSDavid Howells 		 * authorisation token handy */
657664cceb0SDavid Howells 		if (PTR_ERR(key_ref) == -EACCES) {
6583e30148cSDavid Howells 			instkey = key_get_instantiation_authkey(keyid);
6593e30148cSDavid Howells 			if (!IS_ERR(instkey)) {
6603e30148cSDavid Howells 				key_put(instkey);
6618bbf4976SDavid Howells 				key_ref = lookup_user_key(keyid,
6625593122eSDavid Howells 							  KEY_LOOKUP_PARTIAL,
6635593122eSDavid Howells 							  0);
664664cceb0SDavid Howells 				if (!IS_ERR(key_ref))
6653e30148cSDavid Howells 					goto okay;
6663e30148cSDavid Howells 			}
6673e30148cSDavid Howells 		}
6683e30148cSDavid Howells 
669664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
6701da177e4SLinus Torvalds 		goto error;
6711da177e4SLinus Torvalds 	}
6721da177e4SLinus Torvalds 
6733e30148cSDavid Howells okay:
674664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
675aa9d4437SDavid Howells 	desclen = strlen(key->description);
676664cceb0SDavid Howells 
677*2e12256bSDavid Howells 	rcu_read_lock();
678*2e12256bSDavid Howells 	perm = key_acl_to_perm(rcu_dereference(key->acl));
679*2e12256bSDavid Howells 	rcu_read_unlock();
680*2e12256bSDavid 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),
688*2e12256bSDavid Howells 			    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 /*
804973c9f4fSDavid Howells  * Read a key's payload.
805973c9f4fSDavid Howells  *
806973c9f4fSDavid Howells  * The key must either grant the caller Read permission, or it must grant the
807973c9f4fSDavid Howells  * caller Search permission when searched for from the process keyrings.
808973c9f4fSDavid Howells  *
809973c9f4fSDavid Howells  * If successful, we place up to buflen bytes of data into the buffer, if one
810973c9f4fSDavid Howells  * is provided, and return the amount of data that is available in the key,
811973c9f4fSDavid Howells  * irrespective of how much we copied into the buffer.
8121da177e4SLinus Torvalds  */
8131da177e4SLinus Torvalds long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
8141da177e4SLinus Torvalds {
815664cceb0SDavid Howells 	struct key *key;
816664cceb0SDavid Howells 	key_ref_t key_ref;
8171da177e4SLinus Torvalds 	long ret;
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	/* find the key first */
8205593122eSDavid Howells 	key_ref = lookup_user_key(keyid, 0, 0);
821664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
822664cceb0SDavid Howells 		ret = -ENOKEY;
823664cceb0SDavid Howells 		goto error;
824664cceb0SDavid Howells 	}
825664cceb0SDavid Howells 
826664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
827664cceb0SDavid Howells 
828363b02daSDavid Howells 	ret = key_read_state(key);
829363b02daSDavid Howells 	if (ret < 0)
830363b02daSDavid Howells 		goto error2; /* Negatively instantiated */
83137863c43SEric Biggers 
8321da177e4SLinus Torvalds 	/* see if we can read it directly */
833f5895943SDavid Howells 	ret = key_permission(key_ref, KEY_NEED_READ);
83429db9190SDavid Howells 	if (ret == 0)
8351da177e4SLinus Torvalds 		goto can_read_key;
83629db9190SDavid Howells 	if (ret != -EACCES)
8377fc0786dSEric Biggers 		goto error2;
8381da177e4SLinus Torvalds 
839664cceb0SDavid Howells 	/* we can't; see if it's searchable from this process's keyrings
8403e30148cSDavid Howells 	 * - we automatically take account of the fact that it may be
8413e30148cSDavid Howells 	 *   dangling off an instantiation key
8423e30148cSDavid Howells 	 */
843664cceb0SDavid Howells 	if (!is_key_possessed(key_ref)) {
8441260f801SDavid Howells 		ret = -EACCES;
8451da177e4SLinus Torvalds 		goto error2;
8461da177e4SLinus Torvalds 	}
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 	/* the key is probably readable - now try to read it */
8491da177e4SLinus Torvalds can_read_key:
8501da177e4SLinus Torvalds 	ret = -EOPNOTSUPP;
8511da177e4SLinus Torvalds 	if (key->type->read) {
852b4a1b4f5SDavid Howells 		/* Read the data with the semaphore held (since we might sleep)
853b4a1b4f5SDavid Howells 		 * to protect against the key being updated or revoked.
854b4a1b4f5SDavid Howells 		 */
8551da177e4SLinus Torvalds 		down_read(&key->sem);
856b4a1b4f5SDavid Howells 		ret = key_validate(key);
857b4a1b4f5SDavid Howells 		if (ret == 0)
8581da177e4SLinus Torvalds 			ret = key->type->read(key, buffer, buflen);
8591da177e4SLinus Torvalds 		up_read(&key->sem);
8601da177e4SLinus Torvalds 	}
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds error2:
8631da177e4SLinus Torvalds 	key_put(key);
8641da177e4SLinus Torvalds error:
8651da177e4SLinus Torvalds 	return ret;
866a8b17ed0SDavid Howells }
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds /*
869973c9f4fSDavid Howells  * Change the ownership of a key
870973c9f4fSDavid Howells  *
871973c9f4fSDavid Howells  * The key must grant the caller Setattr permission for this to work, though
872973c9f4fSDavid Howells  * the key need not be fully instantiated yet.  For the UID to be changed, or
873973c9f4fSDavid Howells  * for the GID to be changed to a group the caller is not a member of, the
874973c9f4fSDavid Howells  * caller must have sysadmin capability.  If either uid or gid is -1 then that
875973c9f4fSDavid Howells  * attribute is not changed.
876973c9f4fSDavid Howells  *
877973c9f4fSDavid Howells  * If the UID is to be changed, the new user must have sufficient quota to
878973c9f4fSDavid Howells  * accept the key.  The quota deduction will be removed from the old user to
879973c9f4fSDavid Howells  * the new user should the attribute be changed.
880973c9f4fSDavid Howells  *
881973c9f4fSDavid Howells  * If successful, 0 will be returned.
8821da177e4SLinus Torvalds  */
8839a56c2dbSEric W. Biederman long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
8841da177e4SLinus Torvalds {
8855801649dSFredrik Tolf 	struct key_user *newowner, *zapowner = NULL;
8861da177e4SLinus Torvalds 	struct key *key;
887664cceb0SDavid Howells 	key_ref_t key_ref;
8881da177e4SLinus Torvalds 	long ret;
8899a56c2dbSEric W. Biederman 	kuid_t uid;
8909a56c2dbSEric W. Biederman 	kgid_t gid;
8919a56c2dbSEric W. Biederman 
8929a56c2dbSEric W. Biederman 	uid = make_kuid(current_user_ns(), user);
8939a56c2dbSEric W. Biederman 	gid = make_kgid(current_user_ns(), group);
8949a56c2dbSEric W. Biederman 	ret = -EINVAL;
8959a56c2dbSEric W. Biederman 	if ((user != (uid_t) -1) && !uid_valid(uid))
8969a56c2dbSEric W. Biederman 		goto error;
8979a56c2dbSEric W. Biederman 	if ((group != (gid_t) -1) && !gid_valid(gid))
8989a56c2dbSEric W. Biederman 		goto error;
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 	ret = 0;
9019a56c2dbSEric W. Biederman 	if (user == (uid_t) -1 && group == (gid_t) -1)
9021da177e4SLinus Torvalds 		goto error;
9031da177e4SLinus Torvalds 
9045593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
905*2e12256bSDavid Howells 				  KEY_NEED_SETSEC);
906664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
907664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
9081da177e4SLinus Torvalds 		goto error;
9091da177e4SLinus Torvalds 	}
9101da177e4SLinus Torvalds 
911664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
912664cceb0SDavid Howells 
9131da177e4SLinus Torvalds 	/* make the changes with the locks held to prevent chown/chown races */
9141da177e4SLinus Torvalds 	ret = -EACCES;
9151da177e4SLinus Torvalds 	down_write(&key->sem);
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN)) {
9181da177e4SLinus Torvalds 		/* only the sysadmin can chown a key to some other UID */
9199a56c2dbSEric W. Biederman 		if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
9205801649dSFredrik Tolf 			goto error_put;
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 		/* only the sysadmin can set the key's GID to a group other
9231da177e4SLinus Torvalds 		 * than one of those that the current process subscribes to */
9249a56c2dbSEric W. Biederman 		if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
9255801649dSFredrik Tolf 			goto error_put;
9261da177e4SLinus Torvalds 	}
9271da177e4SLinus Torvalds 
9285801649dSFredrik Tolf 	/* change the UID */
9299a56c2dbSEric W. Biederman 	if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) {
9305801649dSFredrik Tolf 		ret = -ENOMEM;
9319a56c2dbSEric W. Biederman 		newowner = key_user_lookup(uid);
9325801649dSFredrik Tolf 		if (!newowner)
9335801649dSFredrik Tolf 			goto error_put;
9345801649dSFredrik Tolf 
9355801649dSFredrik Tolf 		/* transfer the quota burden to the new user */
9365801649dSFredrik Tolf 		if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
9379a56c2dbSEric W. Biederman 			unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
9380b77f5bfSDavid Howells 				key_quota_root_maxkeys : key_quota_maxkeys;
9399a56c2dbSEric W. Biederman 			unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
9400b77f5bfSDavid Howells 				key_quota_root_maxbytes : key_quota_maxbytes;
9410b77f5bfSDavid Howells 
9425801649dSFredrik Tolf 			spin_lock(&newowner->lock);
9430b77f5bfSDavid Howells 			if (newowner->qnkeys + 1 >= maxkeys ||
9440b77f5bfSDavid Howells 			    newowner->qnbytes + key->quotalen >= maxbytes ||
9450b77f5bfSDavid Howells 			    newowner->qnbytes + key->quotalen <
9460b77f5bfSDavid Howells 			    newowner->qnbytes)
9475801649dSFredrik Tolf 				goto quota_overrun;
9485801649dSFredrik Tolf 
9495801649dSFredrik Tolf 			newowner->qnkeys++;
9505801649dSFredrik Tolf 			newowner->qnbytes += key->quotalen;
9515801649dSFredrik Tolf 			spin_unlock(&newowner->lock);
9525801649dSFredrik Tolf 
9535801649dSFredrik Tolf 			spin_lock(&key->user->lock);
9545801649dSFredrik Tolf 			key->user->qnkeys--;
9555801649dSFredrik Tolf 			key->user->qnbytes -= key->quotalen;
9565801649dSFredrik Tolf 			spin_unlock(&key->user->lock);
9575801649dSFredrik Tolf 		}
9585801649dSFredrik Tolf 
9595801649dSFredrik Tolf 		atomic_dec(&key->user->nkeys);
9605801649dSFredrik Tolf 		atomic_inc(&newowner->nkeys);
9615801649dSFredrik Tolf 
962363b02daSDavid Howells 		if (key->state != KEY_IS_UNINSTANTIATED) {
9635801649dSFredrik Tolf 			atomic_dec(&key->user->nikeys);
9645801649dSFredrik Tolf 			atomic_inc(&newowner->nikeys);
9655801649dSFredrik Tolf 		}
9665801649dSFredrik Tolf 
9675801649dSFredrik Tolf 		zapowner = key->user;
9685801649dSFredrik Tolf 		key->user = newowner;
9695801649dSFredrik Tolf 		key->uid = uid;
9701da177e4SLinus Torvalds 	}
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 	/* change the GID */
9739a56c2dbSEric W. Biederman 	if (group != (gid_t) -1)
9741da177e4SLinus Torvalds 		key->gid = gid;
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds 	ret = 0;
9771da177e4SLinus Torvalds 
9785801649dSFredrik Tolf error_put:
9791da177e4SLinus Torvalds 	up_write(&key->sem);
9801da177e4SLinus Torvalds 	key_put(key);
9815801649dSFredrik Tolf 	if (zapowner)
9825801649dSFredrik Tolf 		key_user_put(zapowner);
9831da177e4SLinus Torvalds error:
9841da177e4SLinus Torvalds 	return ret;
9851da177e4SLinus Torvalds 
9865801649dSFredrik Tolf quota_overrun:
9875801649dSFredrik Tolf 	spin_unlock(&newowner->lock);
9885801649dSFredrik Tolf 	zapowner = newowner;
9895801649dSFredrik Tolf 	ret = -EDQUOT;
9905801649dSFredrik Tolf 	goto error_put;
991a8b17ed0SDavid Howells }
9925801649dSFredrik Tolf 
9931da177e4SLinus Torvalds /*
994973c9f4fSDavid Howells  * Change the permission mask on a key.
995973c9f4fSDavid Howells  *
996973c9f4fSDavid Howells  * The key must grant the caller Setattr permission for this to work, though
997973c9f4fSDavid Howells  * the key need not be fully instantiated yet.  If the caller does not have
998973c9f4fSDavid Howells  * sysadmin capability, it may only change the permission on keys that it owns.
9991da177e4SLinus Torvalds  */
1000*2e12256bSDavid Howells long keyctl_setperm_key(key_serial_t id, unsigned int perm)
10011da177e4SLinus Torvalds {
1002*2e12256bSDavid Howells 	struct key_acl *acl;
10031da177e4SLinus Torvalds 	struct key *key;
1004664cceb0SDavid Howells 	key_ref_t key_ref;
10051da177e4SLinus Torvalds 	long ret;
1006*2e12256bSDavid Howells 	int nr, i, j;
10071da177e4SLinus Torvalds 
1008664cceb0SDavid Howells 	if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
1009*2e12256bSDavid Howells 		return -EINVAL;
1010*2e12256bSDavid Howells 
1011*2e12256bSDavid Howells 	nr = 0;
1012*2e12256bSDavid Howells 	if (perm & KEY_POS_ALL) nr++;
1013*2e12256bSDavid Howells 	if (perm & KEY_USR_ALL) nr++;
1014*2e12256bSDavid Howells 	if (perm & KEY_GRP_ALL) nr++;
1015*2e12256bSDavid Howells 	if (perm & KEY_OTH_ALL) nr++;
10161da177e4SLinus Torvalds 
10175593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1018*2e12256bSDavid Howells 				  KEY_NEED_SETSEC);
1019664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
1020664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
10211da177e4SLinus Torvalds 		goto error;
10221da177e4SLinus Torvalds 	}
10231da177e4SLinus Torvalds 
1024664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
1025664cceb0SDavid Howells 
1026*2e12256bSDavid Howells 	ret = -EOPNOTSUPP;
1027*2e12256bSDavid Howells 	if (test_bit(KEY_FLAG_HAS_ACL, &key->flags))
1028*2e12256bSDavid Howells 		goto error_key;
10291da177e4SLinus Torvalds 
1030*2e12256bSDavid Howells 	ret = -ENOMEM;
1031*2e12256bSDavid Howells 	acl = kzalloc(struct_size(acl, aces, nr), GFP_KERNEL);
1032*2e12256bSDavid Howells 	if (!acl)
1033*2e12256bSDavid Howells 		goto error_key;
1034*2e12256bSDavid Howells 
1035*2e12256bSDavid Howells 	refcount_set(&acl->usage, 1);
1036*2e12256bSDavid Howells 	acl->nr_ace = nr;
1037*2e12256bSDavid Howells 	j = 0;
1038*2e12256bSDavid Howells 	for (i = 0; i < 4; i++) {
1039*2e12256bSDavid Howells 		struct key_ace *ace = &acl->aces[j];
1040*2e12256bSDavid Howells 		unsigned int subset = (perm >> (i * 8)) & KEY_OTH_ALL;
1041*2e12256bSDavid Howells 
1042*2e12256bSDavid Howells 		if (!subset)
1043*2e12256bSDavid Howells 			continue;
1044*2e12256bSDavid Howells 		ace->type = KEY_ACE_SUBJ_STANDARD;
1045*2e12256bSDavid Howells 		ace->subject_id = KEY_ACE_EVERYONE + i;
1046*2e12256bSDavid Howells 		ace->perm = subset;
1047*2e12256bSDavid Howells 		if (subset & (KEY_OTH_WRITE | KEY_OTH_SETATTR))
1048*2e12256bSDavid Howells 			ace->perm |= KEY_ACE_REVOKE;
1049*2e12256bSDavid Howells 		if (subset & KEY_OTH_SEARCH)
1050*2e12256bSDavid Howells 			ace->perm |= KEY_ACE_INVAL;
1051*2e12256bSDavid Howells 		if (key->type == &key_type_keyring) {
1052*2e12256bSDavid Howells 			if (subset & KEY_OTH_SEARCH)
1053*2e12256bSDavid Howells 				ace->perm |= KEY_ACE_JOIN;
1054*2e12256bSDavid Howells 			if (subset & KEY_OTH_WRITE)
1055*2e12256bSDavid Howells 				ace->perm |= KEY_ACE_CLEAR;
1056*2e12256bSDavid Howells 		}
1057*2e12256bSDavid Howells 		j++;
105876d8aeabSDavid Howells 	}
10591da177e4SLinus Torvalds 
1060*2e12256bSDavid Howells 	/* make the changes with the locks held to prevent chown/chmod races */
1061*2e12256bSDavid Howells 	down_write(&key->sem);
1062*2e12256bSDavid Howells 	ret = key_set_acl(key, acl);
10631da177e4SLinus Torvalds 	up_write(&key->sem);
1064*2e12256bSDavid Howells error_key:
10651da177e4SLinus Torvalds 	key_put(key);
10661da177e4SLinus Torvalds error:
10671da177e4SLinus Torvalds 	return ret;
1068a8b17ed0SDavid Howells }
10691da177e4SLinus Torvalds 
10708bbf4976SDavid Howells /*
1071973c9f4fSDavid Howells  * Get the destination keyring for instantiation and check that the caller has
1072973c9f4fSDavid Howells  * Write permission on it.
10738bbf4976SDavid Howells  */
10748bbf4976SDavid Howells static long get_instantiation_keyring(key_serial_t ringid,
10758bbf4976SDavid Howells 				      struct request_key_auth *rka,
10768bbf4976SDavid Howells 				      struct key **_dest_keyring)
10778bbf4976SDavid Howells {
10788bbf4976SDavid Howells 	key_ref_t dkref;
10798bbf4976SDavid Howells 
10808bbf4976SDavid Howells 	*_dest_keyring = NULL;
1081eca1bf5bSDavid Howells 
1082eca1bf5bSDavid Howells 	/* just return a NULL pointer if we weren't asked to make a link */
1083eca1bf5bSDavid Howells 	if (ringid == 0)
10848bbf4976SDavid Howells 		return 0;
10858bbf4976SDavid Howells 
10868bbf4976SDavid Howells 	/* if a specific keyring is nominated by ID, then use that */
10878bbf4976SDavid Howells 	if (ringid > 0) {
1088f5895943SDavid Howells 		dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
10898bbf4976SDavid Howells 		if (IS_ERR(dkref))
10908bbf4976SDavid Howells 			return PTR_ERR(dkref);
10918bbf4976SDavid Howells 		*_dest_keyring = key_ref_to_ptr(dkref);
10928bbf4976SDavid Howells 		return 0;
10938bbf4976SDavid Howells 	}
10948bbf4976SDavid Howells 
10958bbf4976SDavid Howells 	if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
10968bbf4976SDavid Howells 		return -EINVAL;
10978bbf4976SDavid Howells 
10988bbf4976SDavid Howells 	/* otherwise specify the destination keyring recorded in the
10998bbf4976SDavid Howells 	 * authorisation key (any KEY_SPEC_*_KEYRING) */
11008bbf4976SDavid Howells 	if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
110121279cfaSDavid Howells 		*_dest_keyring = key_get(rka->dest_keyring);
11028bbf4976SDavid Howells 		return 0;
11038bbf4976SDavid Howells 	}
11048bbf4976SDavid Howells 
11058bbf4976SDavid Howells 	return -ENOKEY;
11068bbf4976SDavid Howells }
11078bbf4976SDavid Howells 
1108d84f4f99SDavid Howells /*
1109973c9f4fSDavid Howells  * Change the request_key authorisation key on the current process.
1110d84f4f99SDavid Howells  */
1111d84f4f99SDavid Howells static int keyctl_change_reqkey_auth(struct key *key)
1112d84f4f99SDavid Howells {
1113d84f4f99SDavid Howells 	struct cred *new;
1114d84f4f99SDavid Howells 
1115d84f4f99SDavid Howells 	new = prepare_creds();
1116d84f4f99SDavid Howells 	if (!new)
1117d84f4f99SDavid Howells 		return -ENOMEM;
1118d84f4f99SDavid Howells 
1119d84f4f99SDavid Howells 	key_put(new->request_key_auth);
1120d84f4f99SDavid Howells 	new->request_key_auth = key_get(key);
1121d84f4f99SDavid Howells 
1122d84f4f99SDavid Howells 	return commit_creds(new);
1123d84f4f99SDavid Howells }
1124d84f4f99SDavid Howells 
11251da177e4SLinus Torvalds /*
1126973c9f4fSDavid Howells  * Instantiate a key with the specified payload and link the key into the
1127973c9f4fSDavid Howells  * destination keyring if one is given.
1128973c9f4fSDavid Howells  *
1129973c9f4fSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1130973c9f4fSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1131973c9f4fSDavid Howells  *
1132973c9f4fSDavid Howells  * If successful, 0 will be returned.
11331da177e4SLinus Torvalds  */
1134ee009e4aSDavid Howells long keyctl_instantiate_key_common(key_serial_t id,
1135b353a1f7SAl Viro 				   struct iov_iter *from,
11361da177e4SLinus Torvalds 				   key_serial_t ringid)
11371da177e4SLinus Torvalds {
1138d84f4f99SDavid Howells 	const struct cred *cred = current_cred();
11393e30148cSDavid Howells 	struct request_key_auth *rka;
11408bbf4976SDavid Howells 	struct key *instkey, *dest_keyring;
1141b353a1f7SAl Viro 	size_t plen = from ? iov_iter_count(from) : 0;
11421da177e4SLinus Torvalds 	void *payload;
11431da177e4SLinus Torvalds 	long ret;
11441da177e4SLinus Torvalds 
1145d84f4f99SDavid Howells 	kenter("%d,,%zu,%d", id, plen, ringid);
1146d84f4f99SDavid Howells 
1147b353a1f7SAl Viro 	if (!plen)
1148b353a1f7SAl Viro 		from = NULL;
1149b353a1f7SAl Viro 
11501da177e4SLinus Torvalds 	ret = -EINVAL;
115138bbca6bSDavid Howells 	if (plen > 1024 * 1024 - 1)
11521da177e4SLinus Torvalds 		goto error;
11531da177e4SLinus Torvalds 
1154b5f545c8SDavid Howells 	/* the appropriate instantiation authorisation key must have been
1155b5f545c8SDavid Howells 	 * assumed before calling this */
1156b5f545c8SDavid Howells 	ret = -EPERM;
1157d84f4f99SDavid Howells 	instkey = cred->request_key_auth;
1158b5f545c8SDavid Howells 	if (!instkey)
1159b5f545c8SDavid Howells 		goto error;
1160b5f545c8SDavid Howells 
1161146aa8b1SDavid Howells 	rka = instkey->payload.data[0];
1162b5f545c8SDavid Howells 	if (rka->target_key->serial != id)
1163b5f545c8SDavid Howells 		goto error;
1164b5f545c8SDavid Howells 
11651da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
11661da177e4SLinus Torvalds 	payload = NULL;
11671da177e4SLinus Torvalds 
1168b353a1f7SAl Viro 	if (from) {
11691da177e4SLinus Torvalds 		ret = -ENOMEM;
1170752ade68SMichal Hocko 		payload = kvmalloc(plen, GFP_KERNEL);
11711da177e4SLinus Torvalds 		if (!payload)
11721da177e4SLinus Torvalds 			goto error;
11731da177e4SLinus Torvalds 
1174b353a1f7SAl Viro 		ret = -EFAULT;
1175cbbd26b8SAl Viro 		if (!copy_from_iter_full(payload, plen, from))
11761da177e4SLinus Torvalds 			goto error2;
11771da177e4SLinus Torvalds 	}
11781da177e4SLinus Torvalds 
11793e30148cSDavid Howells 	/* find the destination keyring amongst those belonging to the
11803e30148cSDavid Howells 	 * requesting task */
11818bbf4976SDavid Howells 	ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
11828bbf4976SDavid Howells 	if (ret < 0)
1183b5f545c8SDavid Howells 		goto error2;
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 	/* instantiate the key and link it into a keyring */
11863e30148cSDavid Howells 	ret = key_instantiate_and_link(rka->target_key, payload, plen,
11878bbf4976SDavid Howells 				       dest_keyring, instkey);
11881da177e4SLinus Torvalds 
11898bbf4976SDavid Howells 	key_put(dest_keyring);
1190b5f545c8SDavid Howells 
1191b5f545c8SDavid Howells 	/* discard the assumed authority if it's just been disabled by
1192b5f545c8SDavid Howells 	 * instantiation of the key */
1193d84f4f99SDavid Howells 	if (ret == 0)
1194d84f4f99SDavid Howells 		keyctl_change_reqkey_auth(NULL);
1195b5f545c8SDavid Howells 
11961da177e4SLinus Torvalds error2:
119757070c85SEric Biggers 	if (payload) {
119857070c85SEric Biggers 		memzero_explicit(payload, plen);
1199b353a1f7SAl Viro 		kvfree(payload);
120057070c85SEric Biggers 	}
12011da177e4SLinus Torvalds error:
12021da177e4SLinus Torvalds 	return ret;
1203a8b17ed0SDavid Howells }
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds /*
1206ee009e4aSDavid Howells  * Instantiate a key with the specified payload and link the key into the
1207ee009e4aSDavid Howells  * destination keyring if one is given.
1208ee009e4aSDavid Howells  *
1209ee009e4aSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1210ee009e4aSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1211ee009e4aSDavid Howells  *
1212ee009e4aSDavid Howells  * If successful, 0 will be returned.
1213ee009e4aSDavid Howells  */
1214ee009e4aSDavid Howells long keyctl_instantiate_key(key_serial_t id,
1215ee009e4aSDavid Howells 			    const void __user *_payload,
1216ee009e4aSDavid Howells 			    size_t plen,
1217ee009e4aSDavid Howells 			    key_serial_t ringid)
1218ee009e4aSDavid Howells {
1219ee009e4aSDavid Howells 	if (_payload && plen) {
1220b353a1f7SAl Viro 		struct iovec iov;
1221b353a1f7SAl Viro 		struct iov_iter from;
1222b353a1f7SAl Viro 		int ret;
1223ee009e4aSDavid Howells 
1224b353a1f7SAl Viro 		ret = import_single_range(WRITE, (void __user *)_payload, plen,
1225b353a1f7SAl Viro 					  &iov, &from);
1226b353a1f7SAl Viro 		if (unlikely(ret))
1227b353a1f7SAl Viro 			return ret;
1228b353a1f7SAl Viro 
1229b353a1f7SAl Viro 		return keyctl_instantiate_key_common(id, &from, ringid);
1230ee009e4aSDavid Howells 	}
1231ee009e4aSDavid Howells 
1232b353a1f7SAl Viro 	return keyctl_instantiate_key_common(id, NULL, ringid);
1233ee009e4aSDavid Howells }
1234ee009e4aSDavid Howells 
1235ee009e4aSDavid Howells /*
1236ee009e4aSDavid Howells  * Instantiate a key with the specified multipart payload and link the key into
1237ee009e4aSDavid Howells  * the destination keyring if one is given.
1238ee009e4aSDavid Howells  *
1239ee009e4aSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1240ee009e4aSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1241ee009e4aSDavid Howells  *
1242ee009e4aSDavid Howells  * If successful, 0 will be returned.
1243ee009e4aSDavid Howells  */
1244ee009e4aSDavid Howells long keyctl_instantiate_key_iov(key_serial_t id,
1245ee009e4aSDavid Howells 				const struct iovec __user *_payload_iov,
1246ee009e4aSDavid Howells 				unsigned ioc,
1247ee009e4aSDavid Howells 				key_serial_t ringid)
1248ee009e4aSDavid Howells {
1249ee009e4aSDavid Howells 	struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1250b353a1f7SAl Viro 	struct iov_iter from;
1251ee009e4aSDavid Howells 	long ret;
1252ee009e4aSDavid Howells 
1253b353a1f7SAl Viro 	if (!_payload_iov)
1254b353a1f7SAl Viro 		ioc = 0;
1255ee009e4aSDavid Howells 
1256b353a1f7SAl Viro 	ret = import_iovec(WRITE, _payload_iov, ioc,
1257b353a1f7SAl Viro 				    ARRAY_SIZE(iovstack), &iov, &from);
1258ee009e4aSDavid Howells 	if (ret < 0)
1259b353a1f7SAl Viro 		return ret;
1260b353a1f7SAl Viro 	ret = keyctl_instantiate_key_common(id, &from, ringid);
1261ee009e4aSDavid Howells 	kfree(iov);
1262ee009e4aSDavid Howells 	return ret;
1263ee009e4aSDavid Howells }
1264ee009e4aSDavid Howells 
1265ee009e4aSDavid Howells /*
1266973c9f4fSDavid Howells  * Negatively instantiate the key with the given timeout (in seconds) and link
1267973c9f4fSDavid Howells  * the key into the destination keyring if one is given.
1268973c9f4fSDavid Howells  *
1269973c9f4fSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1270973c9f4fSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1271973c9f4fSDavid Howells  *
1272973c9f4fSDavid Howells  * The key and any links to the key will be automatically garbage collected
1273973c9f4fSDavid Howells  * after the timeout expires.
1274973c9f4fSDavid Howells  *
1275973c9f4fSDavid Howells  * Negative keys are used to rate limit repeated request_key() calls by causing
1276973c9f4fSDavid Howells  * them to return -ENOKEY until the negative key expires.
1277973c9f4fSDavid Howells  *
1278973c9f4fSDavid Howells  * If successful, 0 will be returned.
12791da177e4SLinus Torvalds  */
12801da177e4SLinus Torvalds long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
12811da177e4SLinus Torvalds {
1282fdd1b945SDavid Howells 	return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1283fdd1b945SDavid Howells }
1284fdd1b945SDavid Howells 
1285fdd1b945SDavid Howells /*
1286fdd1b945SDavid Howells  * Negatively instantiate the key with the given timeout (in seconds) and error
1287fdd1b945SDavid Howells  * code and link the key into the destination keyring if one is given.
1288fdd1b945SDavid Howells  *
1289fdd1b945SDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1290fdd1b945SDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1291fdd1b945SDavid Howells  *
1292fdd1b945SDavid Howells  * The key and any links to the key will be automatically garbage collected
1293fdd1b945SDavid Howells  * after the timeout expires.
1294fdd1b945SDavid Howells  *
1295fdd1b945SDavid Howells  * Negative keys are used to rate limit repeated request_key() calls by causing
1296fdd1b945SDavid Howells  * them to return the specified error code until the negative key expires.
1297fdd1b945SDavid Howells  *
1298fdd1b945SDavid Howells  * If successful, 0 will be returned.
1299fdd1b945SDavid Howells  */
1300fdd1b945SDavid Howells long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1301fdd1b945SDavid Howells 		       key_serial_t ringid)
1302fdd1b945SDavid Howells {
1303d84f4f99SDavid Howells 	const struct cred *cred = current_cred();
13043e30148cSDavid Howells 	struct request_key_auth *rka;
13058bbf4976SDavid Howells 	struct key *instkey, *dest_keyring;
13061da177e4SLinus Torvalds 	long ret;
13071da177e4SLinus Torvalds 
1308fdd1b945SDavid Howells 	kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1309fdd1b945SDavid Howells 
1310fdd1b945SDavid Howells 	/* must be a valid error code and mustn't be a kernel special */
1311fdd1b945SDavid Howells 	if (error <= 0 ||
1312fdd1b945SDavid Howells 	    error >= MAX_ERRNO ||
1313fdd1b945SDavid Howells 	    error == ERESTARTSYS ||
1314fdd1b945SDavid Howells 	    error == ERESTARTNOINTR ||
1315fdd1b945SDavid Howells 	    error == ERESTARTNOHAND ||
1316fdd1b945SDavid Howells 	    error == ERESTART_RESTARTBLOCK)
1317fdd1b945SDavid Howells 		return -EINVAL;
1318d84f4f99SDavid Howells 
1319b5f545c8SDavid Howells 	/* the appropriate instantiation authorisation key must have been
1320b5f545c8SDavid Howells 	 * assumed before calling this */
1321b5f545c8SDavid Howells 	ret = -EPERM;
1322d84f4f99SDavid Howells 	instkey = cred->request_key_auth;
1323b5f545c8SDavid Howells 	if (!instkey)
13241da177e4SLinus Torvalds 		goto error;
13251da177e4SLinus Torvalds 
1326146aa8b1SDavid Howells 	rka = instkey->payload.data[0];
1327b5f545c8SDavid Howells 	if (rka->target_key->serial != id)
1328b5f545c8SDavid Howells 		goto error;
13293e30148cSDavid Howells 
13301da177e4SLinus Torvalds 	/* find the destination keyring if present (which must also be
13311da177e4SLinus Torvalds 	 * writable) */
13328bbf4976SDavid Howells 	ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
13338bbf4976SDavid Howells 	if (ret < 0)
1334b5f545c8SDavid Howells 		goto error;
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds 	/* instantiate the key and link it into a keyring */
1337fdd1b945SDavid Howells 	ret = key_reject_and_link(rka->target_key, timeout, error,
13388bbf4976SDavid Howells 				  dest_keyring, instkey);
13391da177e4SLinus Torvalds 
13408bbf4976SDavid Howells 	key_put(dest_keyring);
1341b5f545c8SDavid Howells 
1342b5f545c8SDavid Howells 	/* discard the assumed authority if it's just been disabled by
1343b5f545c8SDavid Howells 	 * instantiation of the key */
1344d84f4f99SDavid Howells 	if (ret == 0)
1345d84f4f99SDavid Howells 		keyctl_change_reqkey_auth(NULL);
1346b5f545c8SDavid Howells 
13471da177e4SLinus Torvalds error:
13481da177e4SLinus Torvalds 	return ret;
1349a8b17ed0SDavid Howells }
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds /*
1352973c9f4fSDavid Howells  * Read or set the default keyring in which request_key() will cache keys and
1353973c9f4fSDavid Howells  * return the old setting.
1354973c9f4fSDavid Howells  *
1355c9f838d1SEric Biggers  * If a thread or process keyring is specified then it will be created if it
1356c9f838d1SEric Biggers  * doesn't yet exist.  The old setting will be returned if successful.
13573e30148cSDavid Howells  */
13583e30148cSDavid Howells long keyctl_set_reqkey_keyring(int reqkey_defl)
13593e30148cSDavid Howells {
1360d84f4f99SDavid Howells 	struct cred *new;
1361d84f4f99SDavid Howells 	int ret, old_setting;
1362d84f4f99SDavid Howells 
1363d84f4f99SDavid Howells 	old_setting = current_cred_xxx(jit_keyring);
1364d84f4f99SDavid Howells 
1365d84f4f99SDavid Howells 	if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1366d84f4f99SDavid Howells 		return old_setting;
1367d84f4f99SDavid Howells 
1368d84f4f99SDavid Howells 	new = prepare_creds();
1369d84f4f99SDavid Howells 	if (!new)
1370d84f4f99SDavid Howells 		return -ENOMEM;
13713e30148cSDavid Howells 
13723e30148cSDavid Howells 	switch (reqkey_defl) {
13733e30148cSDavid Howells 	case KEY_REQKEY_DEFL_THREAD_KEYRING:
1374d84f4f99SDavid Howells 		ret = install_thread_keyring_to_cred(new);
13753e30148cSDavid Howells 		if (ret < 0)
1376d84f4f99SDavid Howells 			goto error;
13773e30148cSDavid Howells 		goto set;
13783e30148cSDavid Howells 
13793e30148cSDavid Howells 	case KEY_REQKEY_DEFL_PROCESS_KEYRING:
1380d84f4f99SDavid Howells 		ret = install_process_keyring_to_cred(new);
1381c9f838d1SEric Biggers 		if (ret < 0)
1382d84f4f99SDavid Howells 			goto error;
1383d84f4f99SDavid Howells 		goto set;
13843e30148cSDavid Howells 
13853e30148cSDavid Howells 	case KEY_REQKEY_DEFL_DEFAULT:
13863e30148cSDavid Howells 	case KEY_REQKEY_DEFL_SESSION_KEYRING:
13873e30148cSDavid Howells 	case KEY_REQKEY_DEFL_USER_KEYRING:
13883e30148cSDavid Howells 	case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1389d84f4f99SDavid Howells 	case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1390d84f4f99SDavid Howells 		goto set;
13913e30148cSDavid Howells 
13923e30148cSDavid Howells 	case KEY_REQKEY_DEFL_NO_CHANGE:
13933e30148cSDavid Howells 	case KEY_REQKEY_DEFL_GROUP_KEYRING:
13943e30148cSDavid Howells 	default:
1395d84f4f99SDavid Howells 		ret = -EINVAL;
1396d84f4f99SDavid Howells 		goto error;
13973e30148cSDavid Howells 	}
13983e30148cSDavid Howells 
1399d84f4f99SDavid Howells set:
1400d84f4f99SDavid Howells 	new->jit_keyring = reqkey_defl;
1401d84f4f99SDavid Howells 	commit_creds(new);
1402d84f4f99SDavid Howells 	return old_setting;
1403d84f4f99SDavid Howells error:
1404d84f4f99SDavid Howells 	abort_creds(new);
14054303ef19SDan Carpenter 	return ret;
1406a8b17ed0SDavid Howells }
1407d84f4f99SDavid Howells 
14083e30148cSDavid Howells /*
1409973c9f4fSDavid Howells  * Set or clear the timeout on a key.
1410973c9f4fSDavid Howells  *
1411973c9f4fSDavid Howells  * Either the key must grant the caller Setattr permission or else the caller
1412973c9f4fSDavid Howells  * must hold an instantiation authorisation token for the key.
1413973c9f4fSDavid Howells  *
1414973c9f4fSDavid Howells  * The timeout is either 0 to clear the timeout, or a number of seconds from
1415973c9f4fSDavid Howells  * the current time.  The key and any links to the key will be automatically
1416973c9f4fSDavid Howells  * garbage collected after the timeout expires.
1417973c9f4fSDavid Howells  *
1418d3600bcfSMimi Zohar  * Keys with KEY_FLAG_KEEP set should not be timed out.
1419d3600bcfSMimi Zohar  *
1420973c9f4fSDavid Howells  * If successful, 0 is returned.
1421017679c4SDavid Howells  */
1422017679c4SDavid Howells long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1423017679c4SDavid Howells {
14249156235bSDavid Howells 	struct key *key, *instkey;
1425017679c4SDavid Howells 	key_ref_t key_ref;
1426017679c4SDavid Howells 	long ret;
1427017679c4SDavid Howells 
14285593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1429*2e12256bSDavid Howells 				  KEY_NEED_SETSEC);
1430017679c4SDavid Howells 	if (IS_ERR(key_ref)) {
14319156235bSDavid Howells 		/* setting the timeout on a key under construction is permitted
14329156235bSDavid Howells 		 * if we have the authorisation token handy */
14339156235bSDavid Howells 		if (PTR_ERR(key_ref) == -EACCES) {
14349156235bSDavid Howells 			instkey = key_get_instantiation_authkey(id);
14359156235bSDavid Howells 			if (!IS_ERR(instkey)) {
14369156235bSDavid Howells 				key_put(instkey);
14379156235bSDavid Howells 				key_ref = lookup_user_key(id,
14389156235bSDavid Howells 							  KEY_LOOKUP_PARTIAL,
14399156235bSDavid Howells 							  0);
14409156235bSDavid Howells 				if (!IS_ERR(key_ref))
14419156235bSDavid Howells 					goto okay;
14429156235bSDavid Howells 			}
14439156235bSDavid Howells 		}
14449156235bSDavid Howells 
1445017679c4SDavid Howells 		ret = PTR_ERR(key_ref);
1446017679c4SDavid Howells 		goto error;
1447017679c4SDavid Howells 	}
1448017679c4SDavid Howells 
14499156235bSDavid Howells okay:
1450017679c4SDavid Howells 	key = key_ref_to_ptr(key_ref);
14511d6d167cSMimi Zohar 	ret = 0;
1452d3600bcfSMimi Zohar 	if (test_bit(KEY_FLAG_KEEP, &key->flags))
1453d3600bcfSMimi Zohar 		ret = -EPERM;
14541d6d167cSMimi Zohar 	else
145559e6b9c1SBryan Schumaker 		key_set_timeout(key, timeout);
1456017679c4SDavid Howells 	key_put(key);
1457017679c4SDavid Howells 
1458017679c4SDavid Howells error:
1459017679c4SDavid Howells 	return ret;
1460a8b17ed0SDavid Howells }
1461017679c4SDavid Howells 
1462017679c4SDavid Howells /*
1463973c9f4fSDavid Howells  * Assume (or clear) the authority to instantiate the specified key.
1464973c9f4fSDavid Howells  *
1465973c9f4fSDavid Howells  * This sets the authoritative token currently in force for key instantiation.
1466973c9f4fSDavid Howells  * This must be done for a key to be instantiated.  It has the effect of making
1467973c9f4fSDavid Howells  * available all the keys from the caller of the request_key() that created a
1468973c9f4fSDavid Howells  * key to request_key() calls made by the caller of this function.
1469973c9f4fSDavid Howells  *
1470973c9f4fSDavid Howells  * The caller must have the instantiation key in their process keyrings with a
1471973c9f4fSDavid Howells  * Search permission grant available to the caller.
1472973c9f4fSDavid Howells  *
1473973c9f4fSDavid Howells  * If the ID given is 0, then the setting will be cleared and 0 returned.
1474973c9f4fSDavid Howells  *
1475973c9f4fSDavid Howells  * If the ID given has a matching an authorisation key, then that key will be
1476973c9f4fSDavid Howells  * set and its ID will be returned.  The authorisation key can be read to get
1477973c9f4fSDavid Howells  * the callout information passed to request_key().
1478b5f545c8SDavid Howells  */
1479b5f545c8SDavid Howells long keyctl_assume_authority(key_serial_t id)
1480b5f545c8SDavid Howells {
1481b5f545c8SDavid Howells 	struct key *authkey;
1482b5f545c8SDavid Howells 	long ret;
1483b5f545c8SDavid Howells 
1484b5f545c8SDavid Howells 	/* special key IDs aren't permitted */
1485b5f545c8SDavid Howells 	ret = -EINVAL;
1486b5f545c8SDavid Howells 	if (id < 0)
1487b5f545c8SDavid Howells 		goto error;
1488b5f545c8SDavid Howells 
1489b5f545c8SDavid Howells 	/* we divest ourselves of authority if given an ID of 0 */
1490b5f545c8SDavid Howells 	if (id == 0) {
1491d84f4f99SDavid Howells 		ret = keyctl_change_reqkey_auth(NULL);
1492b5f545c8SDavid Howells 		goto error;
1493b5f545c8SDavid Howells 	}
1494b5f545c8SDavid Howells 
1495b5f545c8SDavid Howells 	/* attempt to assume the authority temporarily granted to us whilst we
1496b5f545c8SDavid Howells 	 * instantiate the specified key
1497b5f545c8SDavid Howells 	 * - the authorisation key must be in the current task's keyrings
1498b5f545c8SDavid Howells 	 *   somewhere
1499b5f545c8SDavid Howells 	 */
1500b5f545c8SDavid Howells 	authkey = key_get_instantiation_authkey(id);
1501b5f545c8SDavid Howells 	if (IS_ERR(authkey)) {
1502b5f545c8SDavid Howells 		ret = PTR_ERR(authkey);
1503b5f545c8SDavid Howells 		goto error;
1504b5f545c8SDavid Howells 	}
1505b5f545c8SDavid Howells 
1506d84f4f99SDavid Howells 	ret = keyctl_change_reqkey_auth(authkey);
1507884bee02SEric Biggers 	if (ret == 0)
1508d84f4f99SDavid Howells 		ret = authkey->serial;
1509884bee02SEric Biggers 	key_put(authkey);
1510b5f545c8SDavid Howells error:
1511b5f545c8SDavid Howells 	return ret;
1512a8b17ed0SDavid Howells }
1513b5f545c8SDavid Howells 
151470a5bb72SDavid Howells /*
1515973c9f4fSDavid Howells  * Get a key's the LSM security label.
1516973c9f4fSDavid Howells  *
1517973c9f4fSDavid Howells  * The key must grant the caller View permission for this to work.
1518973c9f4fSDavid Howells  *
1519973c9f4fSDavid Howells  * If there's a buffer, then up to buflen bytes of data will be placed into it.
1520973c9f4fSDavid Howells  *
1521973c9f4fSDavid Howells  * If successful, the amount of information available will be returned,
1522973c9f4fSDavid Howells  * irrespective of how much was copied (including the terminal NUL).
152370a5bb72SDavid Howells  */
152470a5bb72SDavid Howells long keyctl_get_security(key_serial_t keyid,
152570a5bb72SDavid Howells 			 char __user *buffer,
152670a5bb72SDavid Howells 			 size_t buflen)
152770a5bb72SDavid Howells {
152870a5bb72SDavid Howells 	struct key *key, *instkey;
152970a5bb72SDavid Howells 	key_ref_t key_ref;
153070a5bb72SDavid Howells 	char *context;
153170a5bb72SDavid Howells 	long ret;
153270a5bb72SDavid Howells 
1533f5895943SDavid Howells 	key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
153470a5bb72SDavid Howells 	if (IS_ERR(key_ref)) {
153570a5bb72SDavid Howells 		if (PTR_ERR(key_ref) != -EACCES)
153670a5bb72SDavid Howells 			return PTR_ERR(key_ref);
153770a5bb72SDavid Howells 
153870a5bb72SDavid Howells 		/* viewing a key under construction is also permitted if we
153970a5bb72SDavid Howells 		 * have the authorisation token handy */
154070a5bb72SDavid Howells 		instkey = key_get_instantiation_authkey(keyid);
154170a5bb72SDavid Howells 		if (IS_ERR(instkey))
1542fa1cc7b5SRoel Kluin 			return PTR_ERR(instkey);
154370a5bb72SDavid Howells 		key_put(instkey);
154470a5bb72SDavid Howells 
15455593122eSDavid Howells 		key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
154670a5bb72SDavid Howells 		if (IS_ERR(key_ref))
154770a5bb72SDavid Howells 			return PTR_ERR(key_ref);
154870a5bb72SDavid Howells 	}
154970a5bb72SDavid Howells 
155070a5bb72SDavid Howells 	key = key_ref_to_ptr(key_ref);
155170a5bb72SDavid Howells 	ret = security_key_getsecurity(key, &context);
155270a5bb72SDavid Howells 	if (ret == 0) {
155370a5bb72SDavid Howells 		/* if no information was returned, give userspace an empty
155470a5bb72SDavid Howells 		 * string */
155570a5bb72SDavid Howells 		ret = 1;
155670a5bb72SDavid Howells 		if (buffer && buflen > 0 &&
155770a5bb72SDavid Howells 		    copy_to_user(buffer, "", 1) != 0)
155870a5bb72SDavid Howells 			ret = -EFAULT;
155970a5bb72SDavid Howells 	} else if (ret > 0) {
156070a5bb72SDavid Howells 		/* return as much data as there's room for */
156170a5bb72SDavid Howells 		if (buffer && buflen > 0) {
156270a5bb72SDavid Howells 			if (buflen > ret)
156370a5bb72SDavid Howells 				buflen = ret;
156470a5bb72SDavid Howells 
156570a5bb72SDavid Howells 			if (copy_to_user(buffer, context, buflen) != 0)
156670a5bb72SDavid Howells 				ret = -EFAULT;
156770a5bb72SDavid Howells 		}
156870a5bb72SDavid Howells 
156970a5bb72SDavid Howells 		kfree(context);
157070a5bb72SDavid Howells 	}
157170a5bb72SDavid Howells 
157270a5bb72SDavid Howells 	key_ref_put(key_ref);
157370a5bb72SDavid Howells 	return ret;
157470a5bb72SDavid Howells }
157570a5bb72SDavid Howells 
1576ee18d64cSDavid Howells /*
1577973c9f4fSDavid Howells  * Attempt to install the calling process's session keyring on the process's
1578973c9f4fSDavid Howells  * parent process.
1579973c9f4fSDavid Howells  *
1580*2e12256bSDavid Howells  * The keyring must exist and must grant the caller JOIN permission, and the
1581973c9f4fSDavid Howells  * parent process must be single-threaded and must have the same effective
1582973c9f4fSDavid Howells  * ownership as this process and mustn't be SUID/SGID.
1583973c9f4fSDavid Howells  *
1584973c9f4fSDavid Howells  * The keyring will be emplaced on the parent when it next resumes userspace.
1585973c9f4fSDavid Howells  *
1586973c9f4fSDavid Howells  * If successful, 0 will be returned.
1587ee18d64cSDavid Howells  */
1588ee18d64cSDavid Howells long keyctl_session_to_parent(void)
1589ee18d64cSDavid Howells {
1590ee18d64cSDavid Howells 	struct task_struct *me, *parent;
1591ee18d64cSDavid Howells 	const struct cred *mycred, *pcred;
159267d12145SAl Viro 	struct callback_head *newwork, *oldwork;
1593ee18d64cSDavid Howells 	key_ref_t keyring_r;
1594413cd3d9SOleg Nesterov 	struct cred *cred;
1595ee18d64cSDavid Howells 	int ret;
1596ee18d64cSDavid Howells 
1597*2e12256bSDavid Howells 	keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_JOIN);
1598ee18d64cSDavid Howells 	if (IS_ERR(keyring_r))
1599ee18d64cSDavid Howells 		return PTR_ERR(keyring_r);
1600ee18d64cSDavid Howells 
1601413cd3d9SOleg Nesterov 	ret = -ENOMEM;
1602413cd3d9SOleg Nesterov 
1603ee18d64cSDavid Howells 	/* our parent is going to need a new cred struct, a new tgcred struct
1604ee18d64cSDavid Howells 	 * and new security data, so we allocate them here to prevent ENOMEM in
1605ee18d64cSDavid Howells 	 * our parent */
1606ee18d64cSDavid Howells 	cred = cred_alloc_blank();
1607ee18d64cSDavid Howells 	if (!cred)
160867d12145SAl Viro 		goto error_keyring;
160967d12145SAl Viro 	newwork = &cred->rcu;
1610ee18d64cSDavid Howells 
16113a50597dSDavid Howells 	cred->session_keyring = key_ref_to_ptr(keyring_r);
16123a50597dSDavid Howells 	keyring_r = NULL;
161367d12145SAl Viro 	init_task_work(newwork, key_change_session_keyring);
1614ee18d64cSDavid Howells 
1615ee18d64cSDavid Howells 	me = current;
16169d1ac65aSDavid Howells 	rcu_read_lock();
1617ee18d64cSDavid Howells 	write_lock_irq(&tasklist_lock);
1618ee18d64cSDavid Howells 
1619ee18d64cSDavid Howells 	ret = -EPERM;
1620413cd3d9SOleg Nesterov 	oldwork = NULL;
16217936d16dSDavid Howells 	parent = rcu_dereference_protected(me->real_parent,
16227936d16dSDavid Howells 					   lockdep_is_held(&tasklist_lock));
1623ee18d64cSDavid Howells 
1624ee18d64cSDavid Howells 	/* the parent mustn't be init and mustn't be a kernel thread */
1625ee18d64cSDavid Howells 	if (parent->pid <= 1 || !parent->mm)
1626413cd3d9SOleg Nesterov 		goto unlock;
1627ee18d64cSDavid Howells 
1628ee18d64cSDavid Howells 	/* the parent must be single threaded */
1629dd98acf7SOleg Nesterov 	if (!thread_group_empty(parent))
1630413cd3d9SOleg Nesterov 		goto unlock;
1631ee18d64cSDavid Howells 
1632ee18d64cSDavid Howells 	/* the parent and the child must have different session keyrings or
1633ee18d64cSDavid Howells 	 * there's no point */
1634ee18d64cSDavid Howells 	mycred = current_cred();
1635ee18d64cSDavid Howells 	pcred = __task_cred(parent);
1636ee18d64cSDavid Howells 	if (mycred == pcred ||
16373a50597dSDavid Howells 	    mycred->session_keyring == pcred->session_keyring) {
1638413cd3d9SOleg Nesterov 		ret = 0;
1639413cd3d9SOleg Nesterov 		goto unlock;
1640413cd3d9SOleg Nesterov 	}
1641ee18d64cSDavid Howells 
1642ee18d64cSDavid Howells 	/* the parent must have the same effective ownership and mustn't be
1643ee18d64cSDavid Howells 	 * SUID/SGID */
16449a56c2dbSEric W. Biederman 	if (!uid_eq(pcred->uid,	 mycred->euid) ||
16459a56c2dbSEric W. Biederman 	    !uid_eq(pcred->euid, mycred->euid) ||
16469a56c2dbSEric W. Biederman 	    !uid_eq(pcred->suid, mycred->euid) ||
16479a56c2dbSEric W. Biederman 	    !gid_eq(pcred->gid,	 mycred->egid) ||
16489a56c2dbSEric W. Biederman 	    !gid_eq(pcred->egid, mycred->egid) ||
16499a56c2dbSEric W. Biederman 	    !gid_eq(pcred->sgid, mycred->egid))
1650413cd3d9SOleg Nesterov 		goto unlock;
1651ee18d64cSDavid Howells 
1652ee18d64cSDavid Howells 	/* the keyrings must have the same UID */
16533a50597dSDavid Howells 	if ((pcred->session_keyring &&
16542a74dbb9SLinus Torvalds 	     !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
16552a74dbb9SLinus Torvalds 	    !uid_eq(mycred->session_keyring->uid, mycred->euid))
1656413cd3d9SOleg Nesterov 		goto unlock;
1657ee18d64cSDavid Howells 
1658413cd3d9SOleg Nesterov 	/* cancel an already pending keyring replacement */
1659413cd3d9SOleg Nesterov 	oldwork = task_work_cancel(parent, key_change_session_keyring);
1660ee18d64cSDavid Howells 
1661ee18d64cSDavid Howells 	/* the replacement session keyring is applied just prior to userspace
1662ee18d64cSDavid Howells 	 * restarting */
166367d12145SAl Viro 	ret = task_work_add(parent, newwork, true);
1664413cd3d9SOleg Nesterov 	if (!ret)
1665413cd3d9SOleg Nesterov 		newwork = NULL;
1666413cd3d9SOleg Nesterov unlock:
1667ee18d64cSDavid Howells 	write_unlock_irq(&tasklist_lock);
16689d1ac65aSDavid Howells 	rcu_read_unlock();
166967d12145SAl Viro 	if (oldwork)
167067d12145SAl Viro 		put_cred(container_of(oldwork, struct cred, rcu));
167167d12145SAl Viro 	if (newwork)
167267d12145SAl Viro 		put_cred(cred);
1673ee18d64cSDavid Howells 	return ret;
1674ee18d64cSDavid Howells 
1675ee18d64cSDavid Howells error_keyring:
1676ee18d64cSDavid Howells 	key_ref_put(keyring_r);
1677ee18d64cSDavid Howells 	return ret;
1678ee18d64cSDavid Howells }
1679ee18d64cSDavid Howells 
1680b5f545c8SDavid Howells /*
16816563c91fSMat Martineau  * Apply a restriction to a given keyring.
16826563c91fSMat Martineau  *
16836563c91fSMat Martineau  * The caller must have Setattr permission to change keyring restrictions.
16846563c91fSMat Martineau  *
16856563c91fSMat Martineau  * The requested type name may be a NULL pointer to reject all attempts
168618026d86SEric Biggers  * to link to the keyring.  In this case, _restriction must also be NULL.
168718026d86SEric Biggers  * Otherwise, both _type and _restriction must be non-NULL.
16886563c91fSMat Martineau  *
16896563c91fSMat Martineau  * Returns 0 if successful.
16906563c91fSMat Martineau  */
16916563c91fSMat Martineau long keyctl_restrict_keyring(key_serial_t id, const char __user *_type,
16926563c91fSMat Martineau 			     const char __user *_restriction)
16936563c91fSMat Martineau {
16946563c91fSMat Martineau 	key_ref_t key_ref;
16956563c91fSMat Martineau 	char type[32];
16966563c91fSMat Martineau 	char *restriction = NULL;
16976563c91fSMat Martineau 	long ret;
16986563c91fSMat Martineau 
1699*2e12256bSDavid Howells 	key_ref = lookup_user_key(id, 0, KEY_NEED_SETSEC);
17006563c91fSMat Martineau 	if (IS_ERR(key_ref))
17016563c91fSMat Martineau 		return PTR_ERR(key_ref);
17026563c91fSMat Martineau 
170318026d86SEric Biggers 	ret = -EINVAL;
17046563c91fSMat Martineau 	if (_type) {
170518026d86SEric Biggers 		if (!_restriction)
170618026d86SEric Biggers 			goto error;
170718026d86SEric Biggers 
17086563c91fSMat Martineau 		ret = key_get_type_from_user(type, _type, sizeof(type));
17096563c91fSMat Martineau 		if (ret < 0)
17106563c91fSMat Martineau 			goto error;
17116563c91fSMat Martineau 
17126563c91fSMat Martineau 		restriction = strndup_user(_restriction, PAGE_SIZE);
17136563c91fSMat Martineau 		if (IS_ERR(restriction)) {
17146563c91fSMat Martineau 			ret = PTR_ERR(restriction);
17156563c91fSMat Martineau 			goto error;
17166563c91fSMat Martineau 		}
171718026d86SEric Biggers 	} else {
171818026d86SEric Biggers 		if (_restriction)
171918026d86SEric Biggers 			goto error;
17206563c91fSMat Martineau 	}
17216563c91fSMat Martineau 
172218026d86SEric Biggers 	ret = keyring_restrict(key_ref, _type ? type : NULL, restriction);
17236563c91fSMat Martineau 	kfree(restriction);
17246563c91fSMat Martineau error:
17256563c91fSMat Martineau 	key_ref_put(key_ref);
17266563c91fSMat Martineau 	return ret;
17276563c91fSMat Martineau }
17286563c91fSMat Martineau 
17296563c91fSMat Martineau /*
173045e0f30cSDavid Howells  * Get keyrings subsystem capabilities.
173145e0f30cSDavid Howells  */
173245e0f30cSDavid Howells long keyctl_capabilities(unsigned char __user *_buffer, size_t buflen)
173345e0f30cSDavid Howells {
173445e0f30cSDavid Howells 	size_t size = buflen;
173545e0f30cSDavid Howells 
173645e0f30cSDavid Howells 	if (size > 0) {
173745e0f30cSDavid Howells 		if (size > sizeof(keyrings_capabilities))
173845e0f30cSDavid Howells 			size = sizeof(keyrings_capabilities);
173945e0f30cSDavid Howells 		if (copy_to_user(_buffer, keyrings_capabilities, size) != 0)
174045e0f30cSDavid Howells 			return -EFAULT;
174145e0f30cSDavid Howells 		if (size < buflen &&
174245e0f30cSDavid Howells 		    clear_user(_buffer + size, buflen - size) != 0)
174345e0f30cSDavid Howells 			return -EFAULT;
174445e0f30cSDavid Howells 	}
174545e0f30cSDavid Howells 
174645e0f30cSDavid Howells 	return sizeof(keyrings_capabilities);
174745e0f30cSDavid Howells }
174845e0f30cSDavid Howells 
174945e0f30cSDavid Howells /*
1750973c9f4fSDavid Howells  * The key control system call
17511da177e4SLinus Torvalds  */
1752938bb9f5SHeiko Carstens SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1753938bb9f5SHeiko Carstens 		unsigned long, arg4, unsigned long, arg5)
17541da177e4SLinus Torvalds {
17551da177e4SLinus Torvalds 	switch (option) {
17561da177e4SLinus Torvalds 	case KEYCTL_GET_KEYRING_ID:
17571da177e4SLinus Torvalds 		return keyctl_get_keyring_ID((key_serial_t) arg2,
17581da177e4SLinus Torvalds 					     (int) arg3);
17591da177e4SLinus Torvalds 
17601da177e4SLinus Torvalds 	case KEYCTL_JOIN_SESSION_KEYRING:
17611da177e4SLinus Torvalds 		return keyctl_join_session_keyring((const char __user *) arg2);
17621da177e4SLinus Torvalds 
17631da177e4SLinus Torvalds 	case KEYCTL_UPDATE:
17641da177e4SLinus Torvalds 		return keyctl_update_key((key_serial_t) arg2,
17651da177e4SLinus Torvalds 					 (const void __user *) arg3,
17661da177e4SLinus Torvalds 					 (size_t) arg4);
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds 	case KEYCTL_REVOKE:
17691da177e4SLinus Torvalds 		return keyctl_revoke_key((key_serial_t) arg2);
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds 	case KEYCTL_DESCRIBE:
17721da177e4SLinus Torvalds 		return keyctl_describe_key((key_serial_t) arg2,
17731da177e4SLinus Torvalds 					   (char __user *) arg3,
17741da177e4SLinus Torvalds 					   (unsigned) arg4);
17751da177e4SLinus Torvalds 
17761da177e4SLinus Torvalds 	case KEYCTL_CLEAR:
17771da177e4SLinus Torvalds 		return keyctl_keyring_clear((key_serial_t) arg2);
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds 	case KEYCTL_LINK:
17801da177e4SLinus Torvalds 		return keyctl_keyring_link((key_serial_t) arg2,
17811da177e4SLinus Torvalds 					   (key_serial_t) arg3);
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds 	case KEYCTL_UNLINK:
17841da177e4SLinus Torvalds 		return keyctl_keyring_unlink((key_serial_t) arg2,
17851da177e4SLinus Torvalds 					     (key_serial_t) arg3);
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds 	case KEYCTL_SEARCH:
17881da177e4SLinus Torvalds 		return keyctl_keyring_search((key_serial_t) arg2,
17891da177e4SLinus Torvalds 					     (const char __user *) arg3,
17901da177e4SLinus Torvalds 					     (const char __user *) arg4,
17911da177e4SLinus Torvalds 					     (key_serial_t) arg5);
17921da177e4SLinus Torvalds 
17931da177e4SLinus Torvalds 	case KEYCTL_READ:
17941da177e4SLinus Torvalds 		return keyctl_read_key((key_serial_t) arg2,
17951da177e4SLinus Torvalds 				       (char __user *) arg3,
17961da177e4SLinus Torvalds 				       (size_t) arg4);
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds 	case KEYCTL_CHOWN:
17991da177e4SLinus Torvalds 		return keyctl_chown_key((key_serial_t) arg2,
18001da177e4SLinus Torvalds 					(uid_t) arg3,
18011da177e4SLinus Torvalds 					(gid_t) arg4);
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds 	case KEYCTL_SETPERM:
18041da177e4SLinus Torvalds 		return keyctl_setperm_key((key_serial_t) arg2,
1805*2e12256bSDavid Howells 					  (unsigned int)arg3);
18061da177e4SLinus Torvalds 
18071da177e4SLinus Torvalds 	case KEYCTL_INSTANTIATE:
18081da177e4SLinus Torvalds 		return keyctl_instantiate_key((key_serial_t) arg2,
18091da177e4SLinus Torvalds 					      (const void __user *) arg3,
18101da177e4SLinus Torvalds 					      (size_t) arg4,
18111da177e4SLinus Torvalds 					      (key_serial_t) arg5);
18121da177e4SLinus Torvalds 
18131da177e4SLinus Torvalds 	case KEYCTL_NEGATE:
18141da177e4SLinus Torvalds 		return keyctl_negate_key((key_serial_t) arg2,
18151da177e4SLinus Torvalds 					 (unsigned) arg3,
18161da177e4SLinus Torvalds 					 (key_serial_t) arg4);
18171da177e4SLinus Torvalds 
18183e30148cSDavid Howells 	case KEYCTL_SET_REQKEY_KEYRING:
18193e30148cSDavid Howells 		return keyctl_set_reqkey_keyring(arg2);
18203e30148cSDavid Howells 
1821017679c4SDavid Howells 	case KEYCTL_SET_TIMEOUT:
1822017679c4SDavid Howells 		return keyctl_set_timeout((key_serial_t) arg2,
1823017679c4SDavid Howells 					  (unsigned) arg3);
1824017679c4SDavid Howells 
1825b5f545c8SDavid Howells 	case KEYCTL_ASSUME_AUTHORITY:
1826b5f545c8SDavid Howells 		return keyctl_assume_authority((key_serial_t) arg2);
1827b5f545c8SDavid Howells 
182870a5bb72SDavid Howells 	case KEYCTL_GET_SECURITY:
182970a5bb72SDavid Howells 		return keyctl_get_security((key_serial_t) arg2,
183090bd49abSJames Morris 					   (char __user *) arg3,
183170a5bb72SDavid Howells 					   (size_t) arg4);
183270a5bb72SDavid Howells 
1833ee18d64cSDavid Howells 	case KEYCTL_SESSION_TO_PARENT:
1834ee18d64cSDavid Howells 		return keyctl_session_to_parent();
1835ee18d64cSDavid Howells 
1836fdd1b945SDavid Howells 	case KEYCTL_REJECT:
1837fdd1b945SDavid Howells 		return keyctl_reject_key((key_serial_t) arg2,
1838fdd1b945SDavid Howells 					 (unsigned) arg3,
1839fdd1b945SDavid Howells 					 (unsigned) arg4,
1840fdd1b945SDavid Howells 					 (key_serial_t) arg5);
1841fdd1b945SDavid Howells 
1842ee009e4aSDavid Howells 	case KEYCTL_INSTANTIATE_IOV:
1843ee009e4aSDavid Howells 		return keyctl_instantiate_key_iov(
1844ee009e4aSDavid Howells 			(key_serial_t) arg2,
1845ee009e4aSDavid Howells 			(const struct iovec __user *) arg3,
1846ee009e4aSDavid Howells 			(unsigned) arg4,
1847ee009e4aSDavid Howells 			(key_serial_t) arg5);
1848ee009e4aSDavid Howells 
1849fd75815fSDavid Howells 	case KEYCTL_INVALIDATE:
1850fd75815fSDavid Howells 		return keyctl_invalidate_key((key_serial_t) arg2);
1851fd75815fSDavid Howells 
1852f36f8c75SDavid Howells 	case KEYCTL_GET_PERSISTENT:
1853f36f8c75SDavid Howells 		return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3);
1854f36f8c75SDavid Howells 
1855ddbb4114SMat Martineau 	case KEYCTL_DH_COMPUTE:
1856ddbb4114SMat Martineau 		return keyctl_dh_compute((struct keyctl_dh_params __user *) arg2,
18574693fc73SStephan Mueller 					 (char __user *) arg3, (size_t) arg4,
1858f1c316a3SStephan Mueller 					 (struct keyctl_kdf_params __user *) arg5);
1859ddbb4114SMat Martineau 
18606563c91fSMat Martineau 	case KEYCTL_RESTRICT_KEYRING:
18616563c91fSMat Martineau 		return keyctl_restrict_keyring((key_serial_t) arg2,
18626563c91fSMat Martineau 					       (const char __user *) arg3,
18636563c91fSMat Martineau 					       (const char __user *) arg4);
18641da177e4SLinus Torvalds 
186500d60fd3SDavid Howells 	case KEYCTL_PKEY_QUERY:
186600d60fd3SDavid Howells 		if (arg3 != 0)
186700d60fd3SDavid Howells 			return -EINVAL;
186800d60fd3SDavid Howells 		return keyctl_pkey_query((key_serial_t)arg2,
186900d60fd3SDavid Howells 					 (const char __user *)arg4,
1870468e91ceSBen Dooks 					 (struct keyctl_pkey_query __user *)arg5);
187100d60fd3SDavid Howells 
187200d60fd3SDavid Howells 	case KEYCTL_PKEY_ENCRYPT:
187300d60fd3SDavid Howells 	case KEYCTL_PKEY_DECRYPT:
187400d60fd3SDavid Howells 	case KEYCTL_PKEY_SIGN:
187500d60fd3SDavid Howells 		return keyctl_pkey_e_d_s(
187600d60fd3SDavid Howells 			option,
187700d60fd3SDavid Howells 			(const struct keyctl_pkey_params __user *)arg2,
187800d60fd3SDavid Howells 			(const char __user *)arg3,
187900d60fd3SDavid Howells 			(const void __user *)arg4,
188000d60fd3SDavid Howells 			(void __user *)arg5);
188100d60fd3SDavid Howells 
188200d60fd3SDavid Howells 	case KEYCTL_PKEY_VERIFY:
188300d60fd3SDavid Howells 		return keyctl_pkey_verify(
188400d60fd3SDavid Howells 			(const struct keyctl_pkey_params __user *)arg2,
188500d60fd3SDavid Howells 			(const char __user *)arg3,
188600d60fd3SDavid Howells 			(const void __user *)arg4,
188700d60fd3SDavid Howells 			(const void __user *)arg5);
188800d60fd3SDavid Howells 
1889ed0ac5c7SDavid Howells 	case KEYCTL_MOVE:
1890ed0ac5c7SDavid Howells 		return keyctl_keyring_move((key_serial_t)arg2,
1891ed0ac5c7SDavid Howells 					   (key_serial_t)arg3,
1892ed0ac5c7SDavid Howells 					   (key_serial_t)arg4,
1893ed0ac5c7SDavid Howells 					   (unsigned int)arg5);
1894ed0ac5c7SDavid Howells 
189545e0f30cSDavid Howells 	case KEYCTL_CAPABILITIES:
189645e0f30cSDavid Howells 		return keyctl_capabilities((unsigned char __user *)arg2, (size_t)arg3);
189745e0f30cSDavid Howells 
18981da177e4SLinus Torvalds 	default:
18991da177e4SLinus Torvalds 		return -EOPNOTSUPP;
19001da177e4SLinus Torvalds 	}
19011da177e4SLinus Torvalds }
1902