xref: /openbmc/linux/security/keys/keyctl.c (revision b206f281)
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 
33*b206f281SDavid 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 	       ),
43*b206f281SDavid Howells 	[1] = (KEYCTL_CAPS1_NS_KEYRING_NAME),
4445e0f30cSDavid Howells };
4545e0f30cSDavid Howells 
460cb409d9SDavi Arnaut static int key_get_type_from_user(char *type,
470cb409d9SDavi Arnaut 				  const char __user *_type,
480cb409d9SDavi Arnaut 				  unsigned len)
490cb409d9SDavi Arnaut {
500cb409d9SDavi Arnaut 	int ret;
510cb409d9SDavi Arnaut 
520cb409d9SDavi Arnaut 	ret = strncpy_from_user(type, _type, len);
530cb409d9SDavi Arnaut 	if (ret < 0)
544303ef19SDan Carpenter 		return ret;
550cb409d9SDavi Arnaut 	if (ret == 0 || ret >= len)
560cb409d9SDavi Arnaut 		return -EINVAL;
5754e2c2c1SDavid Howells 	if (type[0] == '.')
5854e2c2c1SDavid Howells 		return -EPERM;
590cb409d9SDavi Arnaut 	type[len - 1] = '\0';
600cb409d9SDavi Arnaut 	return 0;
610cb409d9SDavi Arnaut }
620cb409d9SDavi Arnaut 
631da177e4SLinus Torvalds /*
64973c9f4fSDavid Howells  * Extract the description of a new key from userspace and either add it as a
65973c9f4fSDavid Howells  * new key to the specified keyring or update a matching key in that keyring.
66973c9f4fSDavid Howells  *
67cf7f601cSDavid Howells  * If the description is NULL or an empty string, the key type is asked to
68cf7f601cSDavid Howells  * generate one from the payload.
69cf7f601cSDavid Howells  *
70973c9f4fSDavid Howells  * The keyring must be writable so that we can attach the key to it.
71973c9f4fSDavid Howells  *
72973c9f4fSDavid Howells  * If successful, the new key's serial number is returned, otherwise an error
73973c9f4fSDavid Howells  * code is returned.
741da177e4SLinus Torvalds  */
751e7bfb21SHeiko Carstens SYSCALL_DEFINE5(add_key, const char __user *, _type,
761e7bfb21SHeiko Carstens 		const char __user *, _description,
771e7bfb21SHeiko Carstens 		const void __user *, _payload,
781e7bfb21SHeiko Carstens 		size_t, plen,
791e7bfb21SHeiko Carstens 		key_serial_t, ringid)
801da177e4SLinus Torvalds {
81664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref;
821da177e4SLinus Torvalds 	char type[32], *description;
831da177e4SLinus Torvalds 	void *payload;
840cb409d9SDavi Arnaut 	long ret;
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds 	ret = -EINVAL;
8738bbca6bSDavid Howells 	if (plen > 1024 * 1024 - 1)
881da177e4SLinus Torvalds 		goto error;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	/* draw all the data into kernel space */
910cb409d9SDavi Arnaut 	ret = key_get_type_from_user(type, _type, sizeof(type));
921da177e4SLinus Torvalds 	if (ret < 0)
931da177e4SLinus Torvalds 		goto error;
941da177e4SLinus Torvalds 
95cf7f601cSDavid Howells 	description = NULL;
96cf7f601cSDavid Howells 	if (_description) {
97aa9d4437SDavid Howells 		description = strndup_user(_description, KEY_MAX_DESC_SIZE);
980cb409d9SDavi Arnaut 		if (IS_ERR(description)) {
990cb409d9SDavi Arnaut 			ret = PTR_ERR(description);
1003e30148cSDavid Howells 			goto error;
1010cb409d9SDavi Arnaut 		}
102cf7f601cSDavid Howells 		if (!*description) {
103cf7f601cSDavid Howells 			kfree(description);
104cf7f601cSDavid Howells 			description = NULL;
105a4e3b8d7SMimi Zohar 		} else if ((description[0] == '.') &&
106a4e3b8d7SMimi Zohar 			   (strncmp(type, "keyring", 7) == 0)) {
107a4e3b8d7SMimi Zohar 			ret = -EPERM;
108a4e3b8d7SMimi Zohar 			goto error2;
109cf7f601cSDavid Howells 		}
110cf7f601cSDavid Howells 	}
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
1131da177e4SLinus Torvalds 	payload = NULL;
1141da177e4SLinus Torvalds 
1155649645dSEric Biggers 	if (plen) {
1161da177e4SLinus Torvalds 		ret = -ENOMEM;
117752ade68SMichal Hocko 		payload = kvmalloc(plen, GFP_KERNEL);
1181da177e4SLinus Torvalds 		if (!payload)
1191da177e4SLinus Torvalds 			goto error2;
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 		ret = -EFAULT;
1221da177e4SLinus Torvalds 		if (copy_from_user(payload, _payload, plen) != 0)
1231da177e4SLinus Torvalds 			goto error3;
1241da177e4SLinus Torvalds 	}
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	/* find the target keyring (which must be writable) */
127f5895943SDavid Howells 	keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
128664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
129664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
1301da177e4SLinus Torvalds 		goto error3;
1311da177e4SLinus Torvalds 	}
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	/* create or update the requested key and add it to the target
1341da177e4SLinus Torvalds 	 * keyring */
135664cceb0SDavid Howells 	key_ref = key_create_or_update(keyring_ref, type, description,
1366b79ccb5SArun Raghavan 				       payload, plen, KEY_PERM_UNDEF,
1376b79ccb5SArun Raghavan 				       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 */
2264a38e122SDavid Howells 	key = request_key_and_link(ktype, description, callout_info,
2274a38e122SDavid Howells 				   callout_len, NULL, key_ref_to_ptr(dest_ref),
2287e047ef5SDavid Howells 				   KEY_ALLOC_IN_QUOTA);
2291da177e4SLinus Torvalds 	if (IS_ERR(key)) {
2301da177e4SLinus Torvalds 		ret = PTR_ERR(key);
2311da177e4SLinus Torvalds 		goto error5;
2321da177e4SLinus Torvalds 	}
2331da177e4SLinus Torvalds 
2344aab1e89SDavid Howells 	/* wait for the key to finish being constructed */
2354aab1e89SDavid Howells 	ret = wait_for_key_construction(key, 1);
2364aab1e89SDavid Howells 	if (ret < 0)
2374aab1e89SDavid Howells 		goto error6;
2384aab1e89SDavid Howells 
2391da177e4SLinus Torvalds 	ret = key->serial;
2401da177e4SLinus Torvalds 
2414aab1e89SDavid Howells error6:
2421da177e4SLinus Torvalds  	key_put(key);
2431da177e4SLinus Torvalds error5:
2441da177e4SLinus Torvalds 	key_type_put(ktype);
2451da177e4SLinus Torvalds error4:
246664cceb0SDavid Howells 	key_ref_put(dest_ref);
2471da177e4SLinus Torvalds error3:
2481da177e4SLinus Torvalds 	kfree(callout_info);
2491da177e4SLinus Torvalds error2:
2501da177e4SLinus Torvalds 	kfree(description);
2511da177e4SLinus Torvalds error:
2521da177e4SLinus Torvalds 	return ret;
253a8b17ed0SDavid Howells }
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds /*
256973c9f4fSDavid Howells  * Get the ID of the specified process keyring.
257973c9f4fSDavid Howells  *
258973c9f4fSDavid Howells  * The requested keyring must have search permission to be found.
259973c9f4fSDavid Howells  *
260973c9f4fSDavid Howells  * If successful, the ID of the requested keyring will be returned.
2611da177e4SLinus Torvalds  */
2621da177e4SLinus Torvalds long keyctl_get_keyring_ID(key_serial_t id, int create)
2631da177e4SLinus Torvalds {
264664cceb0SDavid Howells 	key_ref_t key_ref;
2655593122eSDavid Howells 	unsigned long lflags;
2661da177e4SLinus Torvalds 	long ret;
2671da177e4SLinus Torvalds 
2685593122eSDavid Howells 	lflags = create ? KEY_LOOKUP_CREATE : 0;
269f5895943SDavid Howells 	key_ref = lookup_user_key(id, lflags, KEY_NEED_SEARCH);
270664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
271664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
2721da177e4SLinus Torvalds 		goto error;
2731da177e4SLinus Torvalds 	}
2741da177e4SLinus Torvalds 
275664cceb0SDavid Howells 	ret = key_ref_to_ptr(key_ref)->serial;
276664cceb0SDavid Howells 	key_ref_put(key_ref);
2771da177e4SLinus Torvalds error:
2781da177e4SLinus Torvalds 	return ret;
279973c9f4fSDavid Howells }
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds /*
282973c9f4fSDavid Howells  * Join a (named) session keyring.
283973c9f4fSDavid Howells  *
284973c9f4fSDavid Howells  * Create and join an anonymous session keyring or join a named session
285973c9f4fSDavid Howells  * keyring, creating it if necessary.  A named session keyring must have Search
286973c9f4fSDavid Howells  * permission for it to be joined.  Session keyrings without this permit will
287ee8f844eSDavid Howells  * be skipped over.  It is not permitted for userspace to create or join
288ee8f844eSDavid Howells  * keyrings whose name begin with a dot.
289973c9f4fSDavid Howells  *
290973c9f4fSDavid Howells  * If successful, the ID of the joined session keyring will be returned.
2911da177e4SLinus Torvalds  */
2921da177e4SLinus Torvalds long keyctl_join_session_keyring(const char __user *_name)
2931da177e4SLinus Torvalds {
2941da177e4SLinus Torvalds 	char *name;
2950cb409d9SDavi Arnaut 	long ret;
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	/* fetch the name from userspace */
2981da177e4SLinus Torvalds 	name = NULL;
2991da177e4SLinus Torvalds 	if (_name) {
300aa9d4437SDavid Howells 		name = strndup_user(_name, KEY_MAX_DESC_SIZE);
3010cb409d9SDavi Arnaut 		if (IS_ERR(name)) {
3020cb409d9SDavi Arnaut 			ret = PTR_ERR(name);
3031da177e4SLinus Torvalds 			goto error;
3040cb409d9SDavi Arnaut 		}
305ee8f844eSDavid Howells 
306ee8f844eSDavid Howells 		ret = -EPERM;
307ee8f844eSDavid Howells 		if (name[0] == '.')
308ee8f844eSDavid Howells 			goto error_name;
3091da177e4SLinus Torvalds 	}
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	/* join the session */
3121da177e4SLinus Torvalds 	ret = join_session_keyring(name);
313ee8f844eSDavid Howells error_name:
3140d54ee1cSVegard Nossum 	kfree(name);
3151da177e4SLinus Torvalds error:
3161da177e4SLinus Torvalds 	return ret;
317a8b17ed0SDavid Howells }
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds /*
320973c9f4fSDavid Howells  * Update a key's data payload from the given data.
321973c9f4fSDavid Howells  *
322973c9f4fSDavid Howells  * The key must grant the caller Write permission and the key type must support
323973c9f4fSDavid Howells  * updating for this to work.  A negative key can be positively instantiated
324973c9f4fSDavid Howells  * with this call.
325973c9f4fSDavid Howells  *
326973c9f4fSDavid Howells  * If successful, 0 will be returned.  If the key type does not support
327973c9f4fSDavid Howells  * updating, then -EOPNOTSUPP will be returned.
3281da177e4SLinus Torvalds  */
3291da177e4SLinus Torvalds long keyctl_update_key(key_serial_t id,
3301da177e4SLinus Torvalds 		       const void __user *_payload,
3311da177e4SLinus Torvalds 		       size_t plen)
3321da177e4SLinus Torvalds {
333664cceb0SDavid Howells 	key_ref_t key_ref;
3341da177e4SLinus Torvalds 	void *payload;
3351da177e4SLinus Torvalds 	long ret;
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	ret = -EINVAL;
3381da177e4SLinus Torvalds 	if (plen > PAGE_SIZE)
3391da177e4SLinus Torvalds 		goto error;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
3421da177e4SLinus Torvalds 	payload = NULL;
3435649645dSEric Biggers 	if (plen) {
3441da177e4SLinus Torvalds 		ret = -ENOMEM;
3451da177e4SLinus Torvalds 		payload = kmalloc(plen, GFP_KERNEL);
3461da177e4SLinus Torvalds 		if (!payload)
3471da177e4SLinus Torvalds 			goto error;
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 		ret = -EFAULT;
3501da177e4SLinus Torvalds 		if (copy_from_user(payload, _payload, plen) != 0)
3511da177e4SLinus Torvalds 			goto error2;
3521da177e4SLinus Torvalds 	}
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 	/* find the target key (which must be writable) */
355f5895943SDavid Howells 	key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
356664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
357664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
3581da177e4SLinus Torvalds 		goto error2;
3591da177e4SLinus Torvalds 	}
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	/* update the key */
362664cceb0SDavid Howells 	ret = key_update(key_ref, payload, plen);
3631da177e4SLinus Torvalds 
364664cceb0SDavid Howells 	key_ref_put(key_ref);
3651da177e4SLinus Torvalds error2:
36657070c85SEric Biggers 	kzfree(payload);
3671da177e4SLinus Torvalds error:
3681da177e4SLinus Torvalds 	return ret;
369a8b17ed0SDavid Howells }
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds /*
372973c9f4fSDavid Howells  * Revoke a key.
373973c9f4fSDavid Howells  *
374973c9f4fSDavid Howells  * The key must be grant the caller Write or Setattr permission for this to
375973c9f4fSDavid Howells  * work.  The key type should give up its quota claim when revoked.  The key
376973c9f4fSDavid Howells  * and any links to the key will be automatically garbage collected after a
377973c9f4fSDavid Howells  * certain amount of time (/proc/sys/kernel/keys/gc_delay).
378973c9f4fSDavid Howells  *
379d3600bcfSMimi Zohar  * Keys with KEY_FLAG_KEEP set should not be revoked.
380d3600bcfSMimi Zohar  *
381973c9f4fSDavid Howells  * If successful, 0 is returned.
3821da177e4SLinus Torvalds  */
3831da177e4SLinus Torvalds long keyctl_revoke_key(key_serial_t id)
3841da177e4SLinus Torvalds {
385664cceb0SDavid Howells 	key_ref_t key_ref;
386d3600bcfSMimi Zohar 	struct key *key;
3871da177e4SLinus Torvalds 	long ret;
3881da177e4SLinus Torvalds 
389f5895943SDavid Howells 	key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
390664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
391664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
3920c2c9a3fSDavid Howells 		if (ret != -EACCES)
3931da177e4SLinus Torvalds 			goto error;
394f5895943SDavid Howells 		key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
3950c2c9a3fSDavid Howells 		if (IS_ERR(key_ref)) {
3960c2c9a3fSDavid Howells 			ret = PTR_ERR(key_ref);
3970c2c9a3fSDavid Howells 			goto error;
3980c2c9a3fSDavid Howells 		}
3991da177e4SLinus Torvalds 	}
4001da177e4SLinus Torvalds 
401d3600bcfSMimi Zohar 	key = key_ref_to_ptr(key_ref);
4021da177e4SLinus Torvalds 	ret = 0;
4031d6d167cSMimi Zohar 	if (test_bit(KEY_FLAG_KEEP, &key->flags))
4041d6d167cSMimi Zohar 		ret = -EPERM;
4051d6d167cSMimi Zohar 	else
4061d6d167cSMimi Zohar 		key_revoke(key);
4071da177e4SLinus Torvalds 
408664cceb0SDavid Howells 	key_ref_put(key_ref);
4091da177e4SLinus Torvalds error:
4101260f801SDavid Howells 	return ret;
411a8b17ed0SDavid Howells }
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds /*
414fd75815fSDavid Howells  * Invalidate a key.
415fd75815fSDavid Howells  *
416fd75815fSDavid Howells  * The key must be grant the caller Invalidate permission for this to work.
417fd75815fSDavid Howells  * The key and any links to the key will be automatically garbage collected
418fd75815fSDavid Howells  * immediately.
419fd75815fSDavid Howells  *
420d3600bcfSMimi Zohar  * Keys with KEY_FLAG_KEEP set should not be invalidated.
421d3600bcfSMimi Zohar  *
422fd75815fSDavid Howells  * If successful, 0 is returned.
423fd75815fSDavid Howells  */
424fd75815fSDavid Howells long keyctl_invalidate_key(key_serial_t id)
425fd75815fSDavid Howells {
426fd75815fSDavid Howells 	key_ref_t key_ref;
427d3600bcfSMimi Zohar 	struct key *key;
428fd75815fSDavid Howells 	long ret;
429fd75815fSDavid Howells 
430fd75815fSDavid Howells 	kenter("%d", id);
431fd75815fSDavid Howells 
432f5895943SDavid Howells 	key_ref = lookup_user_key(id, 0, KEY_NEED_SEARCH);
433fd75815fSDavid Howells 	if (IS_ERR(key_ref)) {
434fd75815fSDavid Howells 		ret = PTR_ERR(key_ref);
4350c7774abSDavid Howells 
4360c7774abSDavid Howells 		/* Root is permitted to invalidate certain special keys */
4370c7774abSDavid Howells 		if (capable(CAP_SYS_ADMIN)) {
4380c7774abSDavid Howells 			key_ref = lookup_user_key(id, 0, 0);
4390c7774abSDavid Howells 			if (IS_ERR(key_ref))
4400c7774abSDavid Howells 				goto error;
4410c7774abSDavid Howells 			if (test_bit(KEY_FLAG_ROOT_CAN_INVAL,
4420c7774abSDavid Howells 				     &key_ref_to_ptr(key_ref)->flags))
4430c7774abSDavid Howells 				goto invalidate;
4440c7774abSDavid Howells 			goto error_put;
4450c7774abSDavid Howells 		}
4460c7774abSDavid Howells 
447fd75815fSDavid Howells 		goto error;
448fd75815fSDavid Howells 	}
449fd75815fSDavid Howells 
4500c7774abSDavid Howells invalidate:
451d3600bcfSMimi Zohar 	key = key_ref_to_ptr(key_ref);
452fd75815fSDavid Howells 	ret = 0;
453d3600bcfSMimi Zohar 	if (test_bit(KEY_FLAG_KEEP, &key->flags))
454d3600bcfSMimi Zohar 		ret = -EPERM;
4551d6d167cSMimi Zohar 	else
456d3600bcfSMimi Zohar 		key_invalidate(key);
4570c7774abSDavid Howells error_put:
458fd75815fSDavid Howells 	key_ref_put(key_ref);
459fd75815fSDavid Howells error:
460fd75815fSDavid Howells 	kleave(" = %ld", ret);
461fd75815fSDavid Howells 	return ret;
462fd75815fSDavid Howells }
463fd75815fSDavid Howells 
464fd75815fSDavid Howells /*
465973c9f4fSDavid Howells  * Clear the specified keyring, creating an empty process keyring if one of the
466973c9f4fSDavid Howells  * special keyring IDs is used.
467973c9f4fSDavid Howells  *
468d3600bcfSMimi Zohar  * The keyring must grant the caller Write permission and not have
469d3600bcfSMimi Zohar  * KEY_FLAG_KEEP set for this to work.  If successful, 0 will be returned.
4701da177e4SLinus Torvalds  */
4711da177e4SLinus Torvalds long keyctl_keyring_clear(key_serial_t ringid)
4721da177e4SLinus Torvalds {
473664cceb0SDavid Howells 	key_ref_t keyring_ref;
474d3600bcfSMimi Zohar 	struct key *keyring;
4751da177e4SLinus Torvalds 	long ret;
4761da177e4SLinus Torvalds 
477f5895943SDavid Howells 	keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
478664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
479664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
480700920ebSDavid Howells 
481700920ebSDavid Howells 		/* Root is permitted to invalidate certain special keyrings */
482700920ebSDavid Howells 		if (capable(CAP_SYS_ADMIN)) {
483700920ebSDavid Howells 			keyring_ref = lookup_user_key(ringid, 0, 0);
484700920ebSDavid Howells 			if (IS_ERR(keyring_ref))
485700920ebSDavid Howells 				goto error;
486700920ebSDavid Howells 			if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
487700920ebSDavid Howells 				     &key_ref_to_ptr(keyring_ref)->flags))
488700920ebSDavid Howells 				goto clear;
489700920ebSDavid Howells 			goto error_put;
490700920ebSDavid Howells 		}
491700920ebSDavid Howells 
4921da177e4SLinus Torvalds 		goto error;
4931da177e4SLinus Torvalds 	}
4941da177e4SLinus Torvalds 
495700920ebSDavid Howells clear:
496d3600bcfSMimi Zohar 	keyring = key_ref_to_ptr(keyring_ref);
497d3600bcfSMimi Zohar 	if (test_bit(KEY_FLAG_KEEP, &keyring->flags))
498d3600bcfSMimi Zohar 		ret = -EPERM;
499d3600bcfSMimi Zohar 	else
500d3600bcfSMimi Zohar 		ret = keyring_clear(keyring);
501700920ebSDavid Howells error_put:
502664cceb0SDavid Howells 	key_ref_put(keyring_ref);
5031da177e4SLinus Torvalds error:
5041da177e4SLinus Torvalds 	return ret;
505a8b17ed0SDavid Howells }
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds /*
508973c9f4fSDavid Howells  * Create a link from a keyring to a key if there's no matching key in the
509973c9f4fSDavid Howells  * keyring, otherwise replace the link to the matching key with a link to the
510973c9f4fSDavid Howells  * new key.
511973c9f4fSDavid Howells  *
512973c9f4fSDavid Howells  * The key must grant the caller Link permission and the the keyring must grant
513973c9f4fSDavid Howells  * the caller Write permission.  Furthermore, if an additional link is created,
514973c9f4fSDavid Howells  * the keyring's quota will be extended.
515973c9f4fSDavid Howells  *
516973c9f4fSDavid Howells  * If successful, 0 will be returned.
5171da177e4SLinus Torvalds  */
5181da177e4SLinus Torvalds long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
5191da177e4SLinus Torvalds {
520664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref;
5211da177e4SLinus Torvalds 	long ret;
5221da177e4SLinus Torvalds 
523f5895943SDavid Howells 	keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
524664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
525664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
5261da177e4SLinus Torvalds 		goto error;
5271da177e4SLinus Torvalds 	}
5281da177e4SLinus Torvalds 
529f5895943SDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
530664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
531664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
5321da177e4SLinus Torvalds 		goto error2;
5331da177e4SLinus Torvalds 	}
5341da177e4SLinus Torvalds 
535664cceb0SDavid Howells 	ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
5361da177e4SLinus Torvalds 
537664cceb0SDavid Howells 	key_ref_put(key_ref);
5381da177e4SLinus Torvalds error2:
539664cceb0SDavid Howells 	key_ref_put(keyring_ref);
5401da177e4SLinus Torvalds error:
5411da177e4SLinus Torvalds 	return ret;
542a8b17ed0SDavid Howells }
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds /*
545973c9f4fSDavid Howells  * Unlink a key from a keyring.
546973c9f4fSDavid Howells  *
547973c9f4fSDavid Howells  * The keyring must grant the caller Write permission for this to work; the key
548973c9f4fSDavid Howells  * itself need not grant the caller anything.  If the last link to a key is
549973c9f4fSDavid Howells  * removed then that key will be scheduled for destruction.
550973c9f4fSDavid Howells  *
551d3600bcfSMimi Zohar  * Keys or keyrings with KEY_FLAG_KEEP set should not be unlinked.
552d3600bcfSMimi Zohar  *
553973c9f4fSDavid Howells  * If successful, 0 will be returned.
5541da177e4SLinus Torvalds  */
5551da177e4SLinus Torvalds long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
5561da177e4SLinus Torvalds {
557664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref;
558d3600bcfSMimi Zohar 	struct key *keyring, *key;
5591da177e4SLinus Torvalds 	long ret;
5601da177e4SLinus Torvalds 
561f5895943SDavid Howells 	keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE);
562664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
563664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
5641da177e4SLinus Torvalds 		goto error;
5651da177e4SLinus Torvalds 	}
5661da177e4SLinus Torvalds 
5675593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
568664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
569664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
5701da177e4SLinus Torvalds 		goto error2;
5711da177e4SLinus Torvalds 	}
5721da177e4SLinus Torvalds 
573d3600bcfSMimi Zohar 	keyring = key_ref_to_ptr(keyring_ref);
574d3600bcfSMimi Zohar 	key = key_ref_to_ptr(key_ref);
575d3600bcfSMimi Zohar 	if (test_bit(KEY_FLAG_KEEP, &keyring->flags) &&
576d3600bcfSMimi Zohar 	    test_bit(KEY_FLAG_KEEP, &key->flags))
577d3600bcfSMimi Zohar 		ret = -EPERM;
578d3600bcfSMimi Zohar 	else
579d3600bcfSMimi Zohar 		ret = key_unlink(keyring, key);
5801da177e4SLinus Torvalds 
581664cceb0SDavid Howells 	key_ref_put(key_ref);
5821da177e4SLinus Torvalds error2:
583664cceb0SDavid Howells 	key_ref_put(keyring_ref);
5841da177e4SLinus Torvalds error:
5851da177e4SLinus Torvalds 	return ret;
586a8b17ed0SDavid Howells }
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds /*
589ed0ac5c7SDavid Howells  * Move a link to a key from one keyring to another, displacing any matching
590ed0ac5c7SDavid Howells  * key from the destination keyring.
591ed0ac5c7SDavid Howells  *
592ed0ac5c7SDavid Howells  * The key must grant the caller Link permission and both keyrings must grant
593ed0ac5c7SDavid Howells  * the caller Write permission.  There must also be a link in the from keyring
594ed0ac5c7SDavid Howells  * to the key.  If both keyrings are the same, nothing is done.
595ed0ac5c7SDavid Howells  *
596ed0ac5c7SDavid Howells  * If successful, 0 will be returned.
597ed0ac5c7SDavid Howells  */
598ed0ac5c7SDavid Howells long keyctl_keyring_move(key_serial_t id, key_serial_t from_ringid,
599ed0ac5c7SDavid Howells 			 key_serial_t to_ringid, unsigned int flags)
600ed0ac5c7SDavid Howells {
601ed0ac5c7SDavid Howells 	key_ref_t key_ref, from_ref, to_ref;
602ed0ac5c7SDavid Howells 	long ret;
603ed0ac5c7SDavid Howells 
604ed0ac5c7SDavid Howells 	if (flags & ~KEYCTL_MOVE_EXCL)
605ed0ac5c7SDavid Howells 		return -EINVAL;
606ed0ac5c7SDavid Howells 
607ed0ac5c7SDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
608ed0ac5c7SDavid Howells 	if (IS_ERR(key_ref))
609ed0ac5c7SDavid Howells 		return PTR_ERR(key_ref);
610ed0ac5c7SDavid Howells 
611ed0ac5c7SDavid Howells 	from_ref = lookup_user_key(from_ringid, 0, KEY_NEED_WRITE);
612ed0ac5c7SDavid Howells 	if (IS_ERR(from_ref)) {
613ed0ac5c7SDavid Howells 		ret = PTR_ERR(from_ref);
614ed0ac5c7SDavid Howells 		goto error2;
615ed0ac5c7SDavid Howells 	}
616ed0ac5c7SDavid Howells 
617ed0ac5c7SDavid Howells 	to_ref = lookup_user_key(to_ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
618ed0ac5c7SDavid Howells 	if (IS_ERR(to_ref)) {
619ed0ac5c7SDavid Howells 		ret = PTR_ERR(to_ref);
620ed0ac5c7SDavid Howells 		goto error3;
621ed0ac5c7SDavid Howells 	}
622ed0ac5c7SDavid Howells 
623ed0ac5c7SDavid Howells 	ret = key_move(key_ref_to_ptr(key_ref), key_ref_to_ptr(from_ref),
624ed0ac5c7SDavid Howells 		       key_ref_to_ptr(to_ref), flags);
625ed0ac5c7SDavid Howells 
626ed0ac5c7SDavid Howells 	key_ref_put(to_ref);
627ed0ac5c7SDavid Howells error3:
628ed0ac5c7SDavid Howells 	key_ref_put(from_ref);
629ed0ac5c7SDavid Howells error2:
630ed0ac5c7SDavid Howells 	key_ref_put(key_ref);
631ed0ac5c7SDavid Howells 	return ret;
632ed0ac5c7SDavid Howells }
633ed0ac5c7SDavid Howells 
634ed0ac5c7SDavid Howells /*
635973c9f4fSDavid Howells  * Return a description of a key to userspace.
636973c9f4fSDavid Howells  *
637973c9f4fSDavid Howells  * The key must grant the caller View permission for this to work.
638973c9f4fSDavid Howells  *
639973c9f4fSDavid Howells  * If there's a buffer, we place up to buflen bytes of data into it formatted
640973c9f4fSDavid Howells  * in the following way:
641973c9f4fSDavid Howells  *
6421da177e4SLinus Torvalds  *	type;uid;gid;perm;description<NUL>
643973c9f4fSDavid Howells  *
644973c9f4fSDavid Howells  * If successful, we return the amount of description available, irrespective
645973c9f4fSDavid Howells  * of how much we may have copied into the buffer.
6461da177e4SLinus Torvalds  */
6471da177e4SLinus Torvalds long keyctl_describe_key(key_serial_t keyid,
6481da177e4SLinus Torvalds 			 char __user *buffer,
6491da177e4SLinus Torvalds 			 size_t buflen)
6501da177e4SLinus Torvalds {
6513e30148cSDavid Howells 	struct key *key, *instkey;
652664cceb0SDavid Howells 	key_ref_t key_ref;
653aa9d4437SDavid Howells 	char *infobuf;
6541da177e4SLinus Torvalds 	long ret;
655aa9d4437SDavid Howells 	int desclen, infolen;
6561da177e4SLinus Torvalds 
657f5895943SDavid Howells 	key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
658664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
6593e30148cSDavid Howells 		/* viewing a key under construction is permitted if we have the
6603e30148cSDavid Howells 		 * authorisation token handy */
661664cceb0SDavid Howells 		if (PTR_ERR(key_ref) == -EACCES) {
6623e30148cSDavid Howells 			instkey = key_get_instantiation_authkey(keyid);
6633e30148cSDavid Howells 			if (!IS_ERR(instkey)) {
6643e30148cSDavid Howells 				key_put(instkey);
6658bbf4976SDavid Howells 				key_ref = lookup_user_key(keyid,
6665593122eSDavid Howells 							  KEY_LOOKUP_PARTIAL,
6675593122eSDavid Howells 							  0);
668664cceb0SDavid Howells 				if (!IS_ERR(key_ref))
6693e30148cSDavid Howells 					goto okay;
6703e30148cSDavid Howells 			}
6713e30148cSDavid Howells 		}
6723e30148cSDavid Howells 
673664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
6741da177e4SLinus Torvalds 		goto error;
6751da177e4SLinus Torvalds 	}
6761da177e4SLinus Torvalds 
6773e30148cSDavid Howells okay:
678664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
679aa9d4437SDavid Howells 	desclen = strlen(key->description);
680664cceb0SDavid Howells 
681aa9d4437SDavid Howells 	/* calculate how much information we're going to return */
682aa9d4437SDavid Howells 	ret = -ENOMEM;
683aa9d4437SDavid Howells 	infobuf = kasprintf(GFP_KERNEL,
684aa9d4437SDavid Howells 			    "%s;%d;%d;%08x;",
68594fd8405SDavid Howells 			    key->type->name,
6869a56c2dbSEric W. Biederman 			    from_kuid_munged(current_user_ns(), key->uid),
6879a56c2dbSEric W. Biederman 			    from_kgid_munged(current_user_ns(), key->gid),
688aa9d4437SDavid Howells 			    key->perm);
689aa9d4437SDavid Howells 	if (!infobuf)
690aa9d4437SDavid Howells 		goto error2;
691aa9d4437SDavid Howells 	infolen = strlen(infobuf);
692aa9d4437SDavid Howells 	ret = infolen + desclen + 1;
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 	/* consider returning the data */
695aa9d4437SDavid Howells 	if (buffer && buflen >= ret) {
696aa9d4437SDavid Howells 		if (copy_to_user(buffer, infobuf, infolen) != 0 ||
697aa9d4437SDavid Howells 		    copy_to_user(buffer + infolen, key->description,
698aa9d4437SDavid Howells 				 desclen + 1) != 0)
6991da177e4SLinus Torvalds 			ret = -EFAULT;
7001da177e4SLinus Torvalds 	}
7011da177e4SLinus Torvalds 
702aa9d4437SDavid Howells 	kfree(infobuf);
7031da177e4SLinus Torvalds error2:
704664cceb0SDavid Howells 	key_ref_put(key_ref);
7051da177e4SLinus Torvalds error:
7061da177e4SLinus Torvalds 	return ret;
707a8b17ed0SDavid Howells }
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds /*
710973c9f4fSDavid Howells  * Search the specified keyring and any keyrings it links to for a matching
711973c9f4fSDavid Howells  * key.  Only keyrings that grant the caller Search permission will be searched
712973c9f4fSDavid Howells  * (this includes the starting keyring).  Only keys with Search permission can
713973c9f4fSDavid Howells  * be found.
714973c9f4fSDavid Howells  *
715973c9f4fSDavid Howells  * If successful, the found key will be linked to the destination keyring if
716973c9f4fSDavid Howells  * supplied and the key has Link permission, and the found key ID will be
717973c9f4fSDavid Howells  * returned.
7181da177e4SLinus Torvalds  */
7191da177e4SLinus Torvalds long keyctl_keyring_search(key_serial_t ringid,
7201da177e4SLinus Torvalds 			   const char __user *_type,
7211da177e4SLinus Torvalds 			   const char __user *_description,
7221da177e4SLinus Torvalds 			   key_serial_t destringid)
7231da177e4SLinus Torvalds {
7241da177e4SLinus Torvalds 	struct key_type *ktype;
725664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref, dest_ref;
7261da177e4SLinus Torvalds 	char type[32], *description;
7270cb409d9SDavi Arnaut 	long ret;
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	/* pull the type and description into kernel space */
7300cb409d9SDavi Arnaut 	ret = key_get_type_from_user(type, _type, sizeof(type));
7311da177e4SLinus Torvalds 	if (ret < 0)
7321da177e4SLinus Torvalds 		goto error;
7331da177e4SLinus Torvalds 
734aa9d4437SDavid Howells 	description = strndup_user(_description, KEY_MAX_DESC_SIZE);
7350cb409d9SDavi Arnaut 	if (IS_ERR(description)) {
7360cb409d9SDavi Arnaut 		ret = PTR_ERR(description);
7371da177e4SLinus Torvalds 		goto error;
7380cb409d9SDavi Arnaut 	}
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	/* get the keyring at which to begin the search */
741f5895943SDavid Howells 	keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH);
742664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
743664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
7441da177e4SLinus Torvalds 		goto error2;
7451da177e4SLinus Torvalds 	}
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	/* get the destination keyring if specified */
748664cceb0SDavid Howells 	dest_ref = NULL;
7491da177e4SLinus Torvalds 	if (destringid) {
7505593122eSDavid Howells 		dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
751f5895943SDavid Howells 					   KEY_NEED_WRITE);
752664cceb0SDavid Howells 		if (IS_ERR(dest_ref)) {
753664cceb0SDavid Howells 			ret = PTR_ERR(dest_ref);
7541da177e4SLinus Torvalds 			goto error3;
7551da177e4SLinus Torvalds 		}
7561da177e4SLinus Torvalds 	}
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 	/* find the key type */
7591da177e4SLinus Torvalds 	ktype = key_type_lookup(type);
7601da177e4SLinus Torvalds 	if (IS_ERR(ktype)) {
7611da177e4SLinus Torvalds 		ret = PTR_ERR(ktype);
7621da177e4SLinus Torvalds 		goto error4;
7631da177e4SLinus Torvalds 	}
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	/* do the search */
766dcf49dbcSDavid Howells 	key_ref = keyring_search(keyring_ref, ktype, description, true);
767664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
768664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 		/* treat lack or presence of a negative key the same */
7711da177e4SLinus Torvalds 		if (ret == -EAGAIN)
7721da177e4SLinus Torvalds 			ret = -ENOKEY;
7731da177e4SLinus Torvalds 		goto error5;
7741da177e4SLinus Torvalds 	}
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	/* link the resulting key to the destination keyring if we can */
777664cceb0SDavid Howells 	if (dest_ref) {
778f5895943SDavid Howells 		ret = key_permission(key_ref, KEY_NEED_LINK);
77929db9190SDavid Howells 		if (ret < 0)
7801da177e4SLinus Torvalds 			goto error6;
7811da177e4SLinus Torvalds 
782664cceb0SDavid Howells 		ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
7831da177e4SLinus Torvalds 		if (ret < 0)
7841da177e4SLinus Torvalds 			goto error6;
7851da177e4SLinus Torvalds 	}
7861da177e4SLinus Torvalds 
787664cceb0SDavid Howells 	ret = key_ref_to_ptr(key_ref)->serial;
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds error6:
790664cceb0SDavid Howells 	key_ref_put(key_ref);
7911da177e4SLinus Torvalds error5:
7921da177e4SLinus Torvalds 	key_type_put(ktype);
7931da177e4SLinus Torvalds error4:
794664cceb0SDavid Howells 	key_ref_put(dest_ref);
7951da177e4SLinus Torvalds error3:
796664cceb0SDavid Howells 	key_ref_put(keyring_ref);
7971da177e4SLinus Torvalds error2:
7981da177e4SLinus Torvalds 	kfree(description);
7991da177e4SLinus Torvalds error:
8001da177e4SLinus Torvalds 	return ret;
801a8b17ed0SDavid Howells }
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds /*
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,
905f5895943SDavid Howells 				  KEY_NEED_SETATTR);
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  */
10001da177e4SLinus Torvalds long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
10011da177e4SLinus Torvalds {
10021da177e4SLinus Torvalds 	struct key *key;
1003664cceb0SDavid Howells 	key_ref_t key_ref;
10041da177e4SLinus Torvalds 	long ret;
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds 	ret = -EINVAL;
1007664cceb0SDavid Howells 	if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
10081da177e4SLinus Torvalds 		goto error;
10091da177e4SLinus Torvalds 
10105593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1011f5895943SDavid Howells 				  KEY_NEED_SETATTR);
1012664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
1013664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
10141da177e4SLinus Torvalds 		goto error;
10151da177e4SLinus Torvalds 	}
10161da177e4SLinus Torvalds 
1017664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
1018664cceb0SDavid Howells 
101976d8aeabSDavid Howells 	/* make the changes with the locks held to prevent chown/chmod races */
10201da177e4SLinus Torvalds 	ret = -EACCES;
10211da177e4SLinus Torvalds 	down_write(&key->sem);
10221da177e4SLinus Torvalds 
102376d8aeabSDavid Howells 	/* if we're not the sysadmin, we can only change a key that we own */
10249a56c2dbSEric W. Biederman 	if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
10251da177e4SLinus Torvalds 		key->perm = perm;
10261da177e4SLinus Torvalds 		ret = 0;
102776d8aeabSDavid Howells 	}
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	up_write(&key->sem);
10301da177e4SLinus Torvalds 	key_put(key);
10311da177e4SLinus Torvalds error:
10321da177e4SLinus Torvalds 	return ret;
1033a8b17ed0SDavid Howells }
10341da177e4SLinus Torvalds 
10358bbf4976SDavid Howells /*
1036973c9f4fSDavid Howells  * Get the destination keyring for instantiation and check that the caller has
1037973c9f4fSDavid Howells  * Write permission on it.
10388bbf4976SDavid Howells  */
10398bbf4976SDavid Howells static long get_instantiation_keyring(key_serial_t ringid,
10408bbf4976SDavid Howells 				      struct request_key_auth *rka,
10418bbf4976SDavid Howells 				      struct key **_dest_keyring)
10428bbf4976SDavid Howells {
10438bbf4976SDavid Howells 	key_ref_t dkref;
10448bbf4976SDavid Howells 
10458bbf4976SDavid Howells 	*_dest_keyring = NULL;
1046eca1bf5bSDavid Howells 
1047eca1bf5bSDavid Howells 	/* just return a NULL pointer if we weren't asked to make a link */
1048eca1bf5bSDavid Howells 	if (ringid == 0)
10498bbf4976SDavid Howells 		return 0;
10508bbf4976SDavid Howells 
10518bbf4976SDavid Howells 	/* if a specific keyring is nominated by ID, then use that */
10528bbf4976SDavid Howells 	if (ringid > 0) {
1053f5895943SDavid Howells 		dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
10548bbf4976SDavid Howells 		if (IS_ERR(dkref))
10558bbf4976SDavid Howells 			return PTR_ERR(dkref);
10568bbf4976SDavid Howells 		*_dest_keyring = key_ref_to_ptr(dkref);
10578bbf4976SDavid Howells 		return 0;
10588bbf4976SDavid Howells 	}
10598bbf4976SDavid Howells 
10608bbf4976SDavid Howells 	if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
10618bbf4976SDavid Howells 		return -EINVAL;
10628bbf4976SDavid Howells 
10638bbf4976SDavid Howells 	/* otherwise specify the destination keyring recorded in the
10648bbf4976SDavid Howells 	 * authorisation key (any KEY_SPEC_*_KEYRING) */
10658bbf4976SDavid Howells 	if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
106621279cfaSDavid Howells 		*_dest_keyring = key_get(rka->dest_keyring);
10678bbf4976SDavid Howells 		return 0;
10688bbf4976SDavid Howells 	}
10698bbf4976SDavid Howells 
10708bbf4976SDavid Howells 	return -ENOKEY;
10718bbf4976SDavid Howells }
10728bbf4976SDavid Howells 
1073d84f4f99SDavid Howells /*
1074973c9f4fSDavid Howells  * Change the request_key authorisation key on the current process.
1075d84f4f99SDavid Howells  */
1076d84f4f99SDavid Howells static int keyctl_change_reqkey_auth(struct key *key)
1077d84f4f99SDavid Howells {
1078d84f4f99SDavid Howells 	struct cred *new;
1079d84f4f99SDavid Howells 
1080d84f4f99SDavid Howells 	new = prepare_creds();
1081d84f4f99SDavid Howells 	if (!new)
1082d84f4f99SDavid Howells 		return -ENOMEM;
1083d84f4f99SDavid Howells 
1084d84f4f99SDavid Howells 	key_put(new->request_key_auth);
1085d84f4f99SDavid Howells 	new->request_key_auth = key_get(key);
1086d84f4f99SDavid Howells 
1087d84f4f99SDavid Howells 	return commit_creds(new);
1088d84f4f99SDavid Howells }
1089d84f4f99SDavid Howells 
10901da177e4SLinus Torvalds /*
1091973c9f4fSDavid Howells  * Instantiate a key with the specified payload and link the key into the
1092973c9f4fSDavid Howells  * destination keyring if one is given.
1093973c9f4fSDavid Howells  *
1094973c9f4fSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1095973c9f4fSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1096973c9f4fSDavid Howells  *
1097973c9f4fSDavid Howells  * If successful, 0 will be returned.
10981da177e4SLinus Torvalds  */
1099ee009e4aSDavid Howells long keyctl_instantiate_key_common(key_serial_t id,
1100b353a1f7SAl Viro 				   struct iov_iter *from,
11011da177e4SLinus Torvalds 				   key_serial_t ringid)
11021da177e4SLinus Torvalds {
1103d84f4f99SDavid Howells 	const struct cred *cred = current_cred();
11043e30148cSDavid Howells 	struct request_key_auth *rka;
11058bbf4976SDavid Howells 	struct key *instkey, *dest_keyring;
1106b353a1f7SAl Viro 	size_t plen = from ? iov_iter_count(from) : 0;
11071da177e4SLinus Torvalds 	void *payload;
11081da177e4SLinus Torvalds 	long ret;
11091da177e4SLinus Torvalds 
1110d84f4f99SDavid Howells 	kenter("%d,,%zu,%d", id, plen, ringid);
1111d84f4f99SDavid Howells 
1112b353a1f7SAl Viro 	if (!plen)
1113b353a1f7SAl Viro 		from = NULL;
1114b353a1f7SAl Viro 
11151da177e4SLinus Torvalds 	ret = -EINVAL;
111638bbca6bSDavid Howells 	if (plen > 1024 * 1024 - 1)
11171da177e4SLinus Torvalds 		goto error;
11181da177e4SLinus Torvalds 
1119b5f545c8SDavid Howells 	/* the appropriate instantiation authorisation key must have been
1120b5f545c8SDavid Howells 	 * assumed before calling this */
1121b5f545c8SDavid Howells 	ret = -EPERM;
1122d84f4f99SDavid Howells 	instkey = cred->request_key_auth;
1123b5f545c8SDavid Howells 	if (!instkey)
1124b5f545c8SDavid Howells 		goto error;
1125b5f545c8SDavid Howells 
1126146aa8b1SDavid Howells 	rka = instkey->payload.data[0];
1127b5f545c8SDavid Howells 	if (rka->target_key->serial != id)
1128b5f545c8SDavid Howells 		goto error;
1129b5f545c8SDavid Howells 
11301da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
11311da177e4SLinus Torvalds 	payload = NULL;
11321da177e4SLinus Torvalds 
1133b353a1f7SAl Viro 	if (from) {
11341da177e4SLinus Torvalds 		ret = -ENOMEM;
1135752ade68SMichal Hocko 		payload = kvmalloc(plen, GFP_KERNEL);
11361da177e4SLinus Torvalds 		if (!payload)
11371da177e4SLinus Torvalds 			goto error;
11381da177e4SLinus Torvalds 
1139b353a1f7SAl Viro 		ret = -EFAULT;
1140cbbd26b8SAl Viro 		if (!copy_from_iter_full(payload, plen, from))
11411da177e4SLinus Torvalds 			goto error2;
11421da177e4SLinus Torvalds 	}
11431da177e4SLinus Torvalds 
11443e30148cSDavid Howells 	/* find the destination keyring amongst those belonging to the
11453e30148cSDavid Howells 	 * requesting task */
11468bbf4976SDavid Howells 	ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
11478bbf4976SDavid Howells 	if (ret < 0)
1148b5f545c8SDavid Howells 		goto error2;
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds 	/* instantiate the key and link it into a keyring */
11513e30148cSDavid Howells 	ret = key_instantiate_and_link(rka->target_key, payload, plen,
11528bbf4976SDavid Howells 				       dest_keyring, instkey);
11531da177e4SLinus Torvalds 
11548bbf4976SDavid Howells 	key_put(dest_keyring);
1155b5f545c8SDavid Howells 
1156b5f545c8SDavid Howells 	/* discard the assumed authority if it's just been disabled by
1157b5f545c8SDavid Howells 	 * instantiation of the key */
1158d84f4f99SDavid Howells 	if (ret == 0)
1159d84f4f99SDavid Howells 		keyctl_change_reqkey_auth(NULL);
1160b5f545c8SDavid Howells 
11611da177e4SLinus Torvalds error2:
116257070c85SEric Biggers 	if (payload) {
116357070c85SEric Biggers 		memzero_explicit(payload, plen);
1164b353a1f7SAl Viro 		kvfree(payload);
116557070c85SEric Biggers 	}
11661da177e4SLinus Torvalds error:
11671da177e4SLinus Torvalds 	return ret;
1168a8b17ed0SDavid Howells }
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds /*
1171ee009e4aSDavid Howells  * Instantiate a key with the specified payload and link the key into the
1172ee009e4aSDavid Howells  * destination keyring if one is given.
1173ee009e4aSDavid Howells  *
1174ee009e4aSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1175ee009e4aSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1176ee009e4aSDavid Howells  *
1177ee009e4aSDavid Howells  * If successful, 0 will be returned.
1178ee009e4aSDavid Howells  */
1179ee009e4aSDavid Howells long keyctl_instantiate_key(key_serial_t id,
1180ee009e4aSDavid Howells 			    const void __user *_payload,
1181ee009e4aSDavid Howells 			    size_t plen,
1182ee009e4aSDavid Howells 			    key_serial_t ringid)
1183ee009e4aSDavid Howells {
1184ee009e4aSDavid Howells 	if (_payload && plen) {
1185b353a1f7SAl Viro 		struct iovec iov;
1186b353a1f7SAl Viro 		struct iov_iter from;
1187b353a1f7SAl Viro 		int ret;
1188ee009e4aSDavid Howells 
1189b353a1f7SAl Viro 		ret = import_single_range(WRITE, (void __user *)_payload, plen,
1190b353a1f7SAl Viro 					  &iov, &from);
1191b353a1f7SAl Viro 		if (unlikely(ret))
1192b353a1f7SAl Viro 			return ret;
1193b353a1f7SAl Viro 
1194b353a1f7SAl Viro 		return keyctl_instantiate_key_common(id, &from, ringid);
1195ee009e4aSDavid Howells 	}
1196ee009e4aSDavid Howells 
1197b353a1f7SAl Viro 	return keyctl_instantiate_key_common(id, NULL, ringid);
1198ee009e4aSDavid Howells }
1199ee009e4aSDavid Howells 
1200ee009e4aSDavid Howells /*
1201ee009e4aSDavid Howells  * Instantiate a key with the specified multipart payload and link the key into
1202ee009e4aSDavid Howells  * the destination keyring if one is given.
1203ee009e4aSDavid Howells  *
1204ee009e4aSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1205ee009e4aSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1206ee009e4aSDavid Howells  *
1207ee009e4aSDavid Howells  * If successful, 0 will be returned.
1208ee009e4aSDavid Howells  */
1209ee009e4aSDavid Howells long keyctl_instantiate_key_iov(key_serial_t id,
1210ee009e4aSDavid Howells 				const struct iovec __user *_payload_iov,
1211ee009e4aSDavid Howells 				unsigned ioc,
1212ee009e4aSDavid Howells 				key_serial_t ringid)
1213ee009e4aSDavid Howells {
1214ee009e4aSDavid Howells 	struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1215b353a1f7SAl Viro 	struct iov_iter from;
1216ee009e4aSDavid Howells 	long ret;
1217ee009e4aSDavid Howells 
1218b353a1f7SAl Viro 	if (!_payload_iov)
1219b353a1f7SAl Viro 		ioc = 0;
1220ee009e4aSDavid Howells 
1221b353a1f7SAl Viro 	ret = import_iovec(WRITE, _payload_iov, ioc,
1222b353a1f7SAl Viro 				    ARRAY_SIZE(iovstack), &iov, &from);
1223ee009e4aSDavid Howells 	if (ret < 0)
1224b353a1f7SAl Viro 		return ret;
1225b353a1f7SAl Viro 	ret = keyctl_instantiate_key_common(id, &from, ringid);
1226ee009e4aSDavid Howells 	kfree(iov);
1227ee009e4aSDavid Howells 	return ret;
1228ee009e4aSDavid Howells }
1229ee009e4aSDavid Howells 
1230ee009e4aSDavid Howells /*
1231973c9f4fSDavid Howells  * Negatively instantiate the key with the given timeout (in seconds) and link
1232973c9f4fSDavid Howells  * the key into the destination keyring if one is given.
1233973c9f4fSDavid Howells  *
1234973c9f4fSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1235973c9f4fSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1236973c9f4fSDavid Howells  *
1237973c9f4fSDavid Howells  * The key and any links to the key will be automatically garbage collected
1238973c9f4fSDavid Howells  * after the timeout expires.
1239973c9f4fSDavid Howells  *
1240973c9f4fSDavid Howells  * Negative keys are used to rate limit repeated request_key() calls by causing
1241973c9f4fSDavid Howells  * them to return -ENOKEY until the negative key expires.
1242973c9f4fSDavid Howells  *
1243973c9f4fSDavid Howells  * If successful, 0 will be returned.
12441da177e4SLinus Torvalds  */
12451da177e4SLinus Torvalds long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
12461da177e4SLinus Torvalds {
1247fdd1b945SDavid Howells 	return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1248fdd1b945SDavid Howells }
1249fdd1b945SDavid Howells 
1250fdd1b945SDavid Howells /*
1251fdd1b945SDavid Howells  * Negatively instantiate the key with the given timeout (in seconds) and error
1252fdd1b945SDavid Howells  * code and link the key into the destination keyring if one is given.
1253fdd1b945SDavid Howells  *
1254fdd1b945SDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1255fdd1b945SDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1256fdd1b945SDavid Howells  *
1257fdd1b945SDavid Howells  * The key and any links to the key will be automatically garbage collected
1258fdd1b945SDavid Howells  * after the timeout expires.
1259fdd1b945SDavid Howells  *
1260fdd1b945SDavid Howells  * Negative keys are used to rate limit repeated request_key() calls by causing
1261fdd1b945SDavid Howells  * them to return the specified error code until the negative key expires.
1262fdd1b945SDavid Howells  *
1263fdd1b945SDavid Howells  * If successful, 0 will be returned.
1264fdd1b945SDavid Howells  */
1265fdd1b945SDavid Howells long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1266fdd1b945SDavid Howells 		       key_serial_t ringid)
1267fdd1b945SDavid Howells {
1268d84f4f99SDavid Howells 	const struct cred *cred = current_cred();
12693e30148cSDavid Howells 	struct request_key_auth *rka;
12708bbf4976SDavid Howells 	struct key *instkey, *dest_keyring;
12711da177e4SLinus Torvalds 	long ret;
12721da177e4SLinus Torvalds 
1273fdd1b945SDavid Howells 	kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1274fdd1b945SDavid Howells 
1275fdd1b945SDavid Howells 	/* must be a valid error code and mustn't be a kernel special */
1276fdd1b945SDavid Howells 	if (error <= 0 ||
1277fdd1b945SDavid Howells 	    error >= MAX_ERRNO ||
1278fdd1b945SDavid Howells 	    error == ERESTARTSYS ||
1279fdd1b945SDavid Howells 	    error == ERESTARTNOINTR ||
1280fdd1b945SDavid Howells 	    error == ERESTARTNOHAND ||
1281fdd1b945SDavid Howells 	    error == ERESTART_RESTARTBLOCK)
1282fdd1b945SDavid Howells 		return -EINVAL;
1283d84f4f99SDavid Howells 
1284b5f545c8SDavid Howells 	/* the appropriate instantiation authorisation key must have been
1285b5f545c8SDavid Howells 	 * assumed before calling this */
1286b5f545c8SDavid Howells 	ret = -EPERM;
1287d84f4f99SDavid Howells 	instkey = cred->request_key_auth;
1288b5f545c8SDavid Howells 	if (!instkey)
12891da177e4SLinus Torvalds 		goto error;
12901da177e4SLinus Torvalds 
1291146aa8b1SDavid Howells 	rka = instkey->payload.data[0];
1292b5f545c8SDavid Howells 	if (rka->target_key->serial != id)
1293b5f545c8SDavid Howells 		goto error;
12943e30148cSDavid Howells 
12951da177e4SLinus Torvalds 	/* find the destination keyring if present (which must also be
12961da177e4SLinus Torvalds 	 * writable) */
12978bbf4976SDavid Howells 	ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
12988bbf4976SDavid Howells 	if (ret < 0)
1299b5f545c8SDavid Howells 		goto error;
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 	/* instantiate the key and link it into a keyring */
1302fdd1b945SDavid Howells 	ret = key_reject_and_link(rka->target_key, timeout, error,
13038bbf4976SDavid Howells 				  dest_keyring, instkey);
13041da177e4SLinus Torvalds 
13058bbf4976SDavid Howells 	key_put(dest_keyring);
1306b5f545c8SDavid Howells 
1307b5f545c8SDavid Howells 	/* discard the assumed authority if it's just been disabled by
1308b5f545c8SDavid Howells 	 * instantiation of the key */
1309d84f4f99SDavid Howells 	if (ret == 0)
1310d84f4f99SDavid Howells 		keyctl_change_reqkey_auth(NULL);
1311b5f545c8SDavid Howells 
13121da177e4SLinus Torvalds error:
13131da177e4SLinus Torvalds 	return ret;
1314a8b17ed0SDavid Howells }
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds /*
1317973c9f4fSDavid Howells  * Read or set the default keyring in which request_key() will cache keys and
1318973c9f4fSDavid Howells  * return the old setting.
1319973c9f4fSDavid Howells  *
1320c9f838d1SEric Biggers  * If a thread or process keyring is specified then it will be created if it
1321c9f838d1SEric Biggers  * doesn't yet exist.  The old setting will be returned if successful.
13223e30148cSDavid Howells  */
13233e30148cSDavid Howells long keyctl_set_reqkey_keyring(int reqkey_defl)
13243e30148cSDavid Howells {
1325d84f4f99SDavid Howells 	struct cred *new;
1326d84f4f99SDavid Howells 	int ret, old_setting;
1327d84f4f99SDavid Howells 
1328d84f4f99SDavid Howells 	old_setting = current_cred_xxx(jit_keyring);
1329d84f4f99SDavid Howells 
1330d84f4f99SDavid Howells 	if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1331d84f4f99SDavid Howells 		return old_setting;
1332d84f4f99SDavid Howells 
1333d84f4f99SDavid Howells 	new = prepare_creds();
1334d84f4f99SDavid Howells 	if (!new)
1335d84f4f99SDavid Howells 		return -ENOMEM;
13363e30148cSDavid Howells 
13373e30148cSDavid Howells 	switch (reqkey_defl) {
13383e30148cSDavid Howells 	case KEY_REQKEY_DEFL_THREAD_KEYRING:
1339d84f4f99SDavid Howells 		ret = install_thread_keyring_to_cred(new);
13403e30148cSDavid Howells 		if (ret < 0)
1341d84f4f99SDavid Howells 			goto error;
13423e30148cSDavid Howells 		goto set;
13433e30148cSDavid Howells 
13443e30148cSDavid Howells 	case KEY_REQKEY_DEFL_PROCESS_KEYRING:
1345d84f4f99SDavid Howells 		ret = install_process_keyring_to_cred(new);
1346c9f838d1SEric Biggers 		if (ret < 0)
1347d84f4f99SDavid Howells 			goto error;
1348d84f4f99SDavid Howells 		goto set;
13493e30148cSDavid Howells 
13503e30148cSDavid Howells 	case KEY_REQKEY_DEFL_DEFAULT:
13513e30148cSDavid Howells 	case KEY_REQKEY_DEFL_SESSION_KEYRING:
13523e30148cSDavid Howells 	case KEY_REQKEY_DEFL_USER_KEYRING:
13533e30148cSDavid Howells 	case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1354d84f4f99SDavid Howells 	case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1355d84f4f99SDavid Howells 		goto set;
13563e30148cSDavid Howells 
13573e30148cSDavid Howells 	case KEY_REQKEY_DEFL_NO_CHANGE:
13583e30148cSDavid Howells 	case KEY_REQKEY_DEFL_GROUP_KEYRING:
13593e30148cSDavid Howells 	default:
1360d84f4f99SDavid Howells 		ret = -EINVAL;
1361d84f4f99SDavid Howells 		goto error;
13623e30148cSDavid Howells 	}
13633e30148cSDavid Howells 
1364d84f4f99SDavid Howells set:
1365d84f4f99SDavid Howells 	new->jit_keyring = reqkey_defl;
1366d84f4f99SDavid Howells 	commit_creds(new);
1367d84f4f99SDavid Howells 	return old_setting;
1368d84f4f99SDavid Howells error:
1369d84f4f99SDavid Howells 	abort_creds(new);
13704303ef19SDan Carpenter 	return ret;
1371a8b17ed0SDavid Howells }
1372d84f4f99SDavid Howells 
13733e30148cSDavid Howells /*
1374973c9f4fSDavid Howells  * Set or clear the timeout on a key.
1375973c9f4fSDavid Howells  *
1376973c9f4fSDavid Howells  * Either the key must grant the caller Setattr permission or else the caller
1377973c9f4fSDavid Howells  * must hold an instantiation authorisation token for the key.
1378973c9f4fSDavid Howells  *
1379973c9f4fSDavid Howells  * The timeout is either 0 to clear the timeout, or a number of seconds from
1380973c9f4fSDavid Howells  * the current time.  The key and any links to the key will be automatically
1381973c9f4fSDavid Howells  * garbage collected after the timeout expires.
1382973c9f4fSDavid Howells  *
1383d3600bcfSMimi Zohar  * Keys with KEY_FLAG_KEEP set should not be timed out.
1384d3600bcfSMimi Zohar  *
1385973c9f4fSDavid Howells  * If successful, 0 is returned.
1386017679c4SDavid Howells  */
1387017679c4SDavid Howells long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1388017679c4SDavid Howells {
13899156235bSDavid Howells 	struct key *key, *instkey;
1390017679c4SDavid Howells 	key_ref_t key_ref;
1391017679c4SDavid Howells 	long ret;
1392017679c4SDavid Howells 
13935593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1394f5895943SDavid Howells 				  KEY_NEED_SETATTR);
1395017679c4SDavid Howells 	if (IS_ERR(key_ref)) {
13969156235bSDavid Howells 		/* setting the timeout on a key under construction is permitted
13979156235bSDavid Howells 		 * if we have the authorisation token handy */
13989156235bSDavid Howells 		if (PTR_ERR(key_ref) == -EACCES) {
13999156235bSDavid Howells 			instkey = key_get_instantiation_authkey(id);
14009156235bSDavid Howells 			if (!IS_ERR(instkey)) {
14019156235bSDavid Howells 				key_put(instkey);
14029156235bSDavid Howells 				key_ref = lookup_user_key(id,
14039156235bSDavid Howells 							  KEY_LOOKUP_PARTIAL,
14049156235bSDavid Howells 							  0);
14059156235bSDavid Howells 				if (!IS_ERR(key_ref))
14069156235bSDavid Howells 					goto okay;
14079156235bSDavid Howells 			}
14089156235bSDavid Howells 		}
14099156235bSDavid Howells 
1410017679c4SDavid Howells 		ret = PTR_ERR(key_ref);
1411017679c4SDavid Howells 		goto error;
1412017679c4SDavid Howells 	}
1413017679c4SDavid Howells 
14149156235bSDavid Howells okay:
1415017679c4SDavid Howells 	key = key_ref_to_ptr(key_ref);
14161d6d167cSMimi Zohar 	ret = 0;
1417d3600bcfSMimi Zohar 	if (test_bit(KEY_FLAG_KEEP, &key->flags))
1418d3600bcfSMimi Zohar 		ret = -EPERM;
14191d6d167cSMimi Zohar 	else
142059e6b9c1SBryan Schumaker 		key_set_timeout(key, timeout);
1421017679c4SDavid Howells 	key_put(key);
1422017679c4SDavid Howells 
1423017679c4SDavid Howells error:
1424017679c4SDavid Howells 	return ret;
1425a8b17ed0SDavid Howells }
1426017679c4SDavid Howells 
1427017679c4SDavid Howells /*
1428973c9f4fSDavid Howells  * Assume (or clear) the authority to instantiate the specified key.
1429973c9f4fSDavid Howells  *
1430973c9f4fSDavid Howells  * This sets the authoritative token currently in force for key instantiation.
1431973c9f4fSDavid Howells  * This must be done for a key to be instantiated.  It has the effect of making
1432973c9f4fSDavid Howells  * available all the keys from the caller of the request_key() that created a
1433973c9f4fSDavid Howells  * key to request_key() calls made by the caller of this function.
1434973c9f4fSDavid Howells  *
1435973c9f4fSDavid Howells  * The caller must have the instantiation key in their process keyrings with a
1436973c9f4fSDavid Howells  * Search permission grant available to the caller.
1437973c9f4fSDavid Howells  *
1438973c9f4fSDavid Howells  * If the ID given is 0, then the setting will be cleared and 0 returned.
1439973c9f4fSDavid Howells  *
1440973c9f4fSDavid Howells  * If the ID given has a matching an authorisation key, then that key will be
1441973c9f4fSDavid Howells  * set and its ID will be returned.  The authorisation key can be read to get
1442973c9f4fSDavid Howells  * the callout information passed to request_key().
1443b5f545c8SDavid Howells  */
1444b5f545c8SDavid Howells long keyctl_assume_authority(key_serial_t id)
1445b5f545c8SDavid Howells {
1446b5f545c8SDavid Howells 	struct key *authkey;
1447b5f545c8SDavid Howells 	long ret;
1448b5f545c8SDavid Howells 
1449b5f545c8SDavid Howells 	/* special key IDs aren't permitted */
1450b5f545c8SDavid Howells 	ret = -EINVAL;
1451b5f545c8SDavid Howells 	if (id < 0)
1452b5f545c8SDavid Howells 		goto error;
1453b5f545c8SDavid Howells 
1454b5f545c8SDavid Howells 	/* we divest ourselves of authority if given an ID of 0 */
1455b5f545c8SDavid Howells 	if (id == 0) {
1456d84f4f99SDavid Howells 		ret = keyctl_change_reqkey_auth(NULL);
1457b5f545c8SDavid Howells 		goto error;
1458b5f545c8SDavid Howells 	}
1459b5f545c8SDavid Howells 
1460b5f545c8SDavid Howells 	/* attempt to assume the authority temporarily granted to us whilst we
1461b5f545c8SDavid Howells 	 * instantiate the specified key
1462b5f545c8SDavid Howells 	 * - the authorisation key must be in the current task's keyrings
1463b5f545c8SDavid Howells 	 *   somewhere
1464b5f545c8SDavid Howells 	 */
1465b5f545c8SDavid Howells 	authkey = key_get_instantiation_authkey(id);
1466b5f545c8SDavid Howells 	if (IS_ERR(authkey)) {
1467b5f545c8SDavid Howells 		ret = PTR_ERR(authkey);
1468b5f545c8SDavid Howells 		goto error;
1469b5f545c8SDavid Howells 	}
1470b5f545c8SDavid Howells 
1471d84f4f99SDavid Howells 	ret = keyctl_change_reqkey_auth(authkey);
1472884bee02SEric Biggers 	if (ret == 0)
1473d84f4f99SDavid Howells 		ret = authkey->serial;
1474884bee02SEric Biggers 	key_put(authkey);
1475b5f545c8SDavid Howells error:
1476b5f545c8SDavid Howells 	return ret;
1477a8b17ed0SDavid Howells }
1478b5f545c8SDavid Howells 
147970a5bb72SDavid Howells /*
1480973c9f4fSDavid Howells  * Get a key's the LSM security label.
1481973c9f4fSDavid Howells  *
1482973c9f4fSDavid Howells  * The key must grant the caller View permission for this to work.
1483973c9f4fSDavid Howells  *
1484973c9f4fSDavid Howells  * If there's a buffer, then up to buflen bytes of data will be placed into it.
1485973c9f4fSDavid Howells  *
1486973c9f4fSDavid Howells  * If successful, the amount of information available will be returned,
1487973c9f4fSDavid Howells  * irrespective of how much was copied (including the terminal NUL).
148870a5bb72SDavid Howells  */
148970a5bb72SDavid Howells long keyctl_get_security(key_serial_t keyid,
149070a5bb72SDavid Howells 			 char __user *buffer,
149170a5bb72SDavid Howells 			 size_t buflen)
149270a5bb72SDavid Howells {
149370a5bb72SDavid Howells 	struct key *key, *instkey;
149470a5bb72SDavid Howells 	key_ref_t key_ref;
149570a5bb72SDavid Howells 	char *context;
149670a5bb72SDavid Howells 	long ret;
149770a5bb72SDavid Howells 
1498f5895943SDavid Howells 	key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
149970a5bb72SDavid Howells 	if (IS_ERR(key_ref)) {
150070a5bb72SDavid Howells 		if (PTR_ERR(key_ref) != -EACCES)
150170a5bb72SDavid Howells 			return PTR_ERR(key_ref);
150270a5bb72SDavid Howells 
150370a5bb72SDavid Howells 		/* viewing a key under construction is also permitted if we
150470a5bb72SDavid Howells 		 * have the authorisation token handy */
150570a5bb72SDavid Howells 		instkey = key_get_instantiation_authkey(keyid);
150670a5bb72SDavid Howells 		if (IS_ERR(instkey))
1507fa1cc7b5SRoel Kluin 			return PTR_ERR(instkey);
150870a5bb72SDavid Howells 		key_put(instkey);
150970a5bb72SDavid Howells 
15105593122eSDavid Howells 		key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
151170a5bb72SDavid Howells 		if (IS_ERR(key_ref))
151270a5bb72SDavid Howells 			return PTR_ERR(key_ref);
151370a5bb72SDavid Howells 	}
151470a5bb72SDavid Howells 
151570a5bb72SDavid Howells 	key = key_ref_to_ptr(key_ref);
151670a5bb72SDavid Howells 	ret = security_key_getsecurity(key, &context);
151770a5bb72SDavid Howells 	if (ret == 0) {
151870a5bb72SDavid Howells 		/* if no information was returned, give userspace an empty
151970a5bb72SDavid Howells 		 * string */
152070a5bb72SDavid Howells 		ret = 1;
152170a5bb72SDavid Howells 		if (buffer && buflen > 0 &&
152270a5bb72SDavid Howells 		    copy_to_user(buffer, "", 1) != 0)
152370a5bb72SDavid Howells 			ret = -EFAULT;
152470a5bb72SDavid Howells 	} else if (ret > 0) {
152570a5bb72SDavid Howells 		/* return as much data as there's room for */
152670a5bb72SDavid Howells 		if (buffer && buflen > 0) {
152770a5bb72SDavid Howells 			if (buflen > ret)
152870a5bb72SDavid Howells 				buflen = ret;
152970a5bb72SDavid Howells 
153070a5bb72SDavid Howells 			if (copy_to_user(buffer, context, buflen) != 0)
153170a5bb72SDavid Howells 				ret = -EFAULT;
153270a5bb72SDavid Howells 		}
153370a5bb72SDavid Howells 
153470a5bb72SDavid Howells 		kfree(context);
153570a5bb72SDavid Howells 	}
153670a5bb72SDavid Howells 
153770a5bb72SDavid Howells 	key_ref_put(key_ref);
153870a5bb72SDavid Howells 	return ret;
153970a5bb72SDavid Howells }
154070a5bb72SDavid Howells 
1541ee18d64cSDavid Howells /*
1542973c9f4fSDavid Howells  * Attempt to install the calling process's session keyring on the process's
1543973c9f4fSDavid Howells  * parent process.
1544973c9f4fSDavid Howells  *
1545973c9f4fSDavid Howells  * The keyring must exist and must grant the caller LINK permission, and the
1546973c9f4fSDavid Howells  * parent process must be single-threaded and must have the same effective
1547973c9f4fSDavid Howells  * ownership as this process and mustn't be SUID/SGID.
1548973c9f4fSDavid Howells  *
1549973c9f4fSDavid Howells  * The keyring will be emplaced on the parent when it next resumes userspace.
1550973c9f4fSDavid Howells  *
1551973c9f4fSDavid Howells  * If successful, 0 will be returned.
1552ee18d64cSDavid Howells  */
1553ee18d64cSDavid Howells long keyctl_session_to_parent(void)
1554ee18d64cSDavid Howells {
1555ee18d64cSDavid Howells 	struct task_struct *me, *parent;
1556ee18d64cSDavid Howells 	const struct cred *mycred, *pcred;
155767d12145SAl Viro 	struct callback_head *newwork, *oldwork;
1558ee18d64cSDavid Howells 	key_ref_t keyring_r;
1559413cd3d9SOleg Nesterov 	struct cred *cred;
1560ee18d64cSDavid Howells 	int ret;
1561ee18d64cSDavid Howells 
1562f5895943SDavid Howells 	keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK);
1563ee18d64cSDavid Howells 	if (IS_ERR(keyring_r))
1564ee18d64cSDavid Howells 		return PTR_ERR(keyring_r);
1565ee18d64cSDavid Howells 
1566413cd3d9SOleg Nesterov 	ret = -ENOMEM;
1567413cd3d9SOleg Nesterov 
1568ee18d64cSDavid Howells 	/* our parent is going to need a new cred struct, a new tgcred struct
1569ee18d64cSDavid Howells 	 * and new security data, so we allocate them here to prevent ENOMEM in
1570ee18d64cSDavid Howells 	 * our parent */
1571ee18d64cSDavid Howells 	cred = cred_alloc_blank();
1572ee18d64cSDavid Howells 	if (!cred)
157367d12145SAl Viro 		goto error_keyring;
157467d12145SAl Viro 	newwork = &cred->rcu;
1575ee18d64cSDavid Howells 
15763a50597dSDavid Howells 	cred->session_keyring = key_ref_to_ptr(keyring_r);
15773a50597dSDavid Howells 	keyring_r = NULL;
157867d12145SAl Viro 	init_task_work(newwork, key_change_session_keyring);
1579ee18d64cSDavid Howells 
1580ee18d64cSDavid Howells 	me = current;
15819d1ac65aSDavid Howells 	rcu_read_lock();
1582ee18d64cSDavid Howells 	write_lock_irq(&tasklist_lock);
1583ee18d64cSDavid Howells 
1584ee18d64cSDavid Howells 	ret = -EPERM;
1585413cd3d9SOleg Nesterov 	oldwork = NULL;
15867936d16dSDavid Howells 	parent = rcu_dereference_protected(me->real_parent,
15877936d16dSDavid Howells 					   lockdep_is_held(&tasklist_lock));
1588ee18d64cSDavid Howells 
1589ee18d64cSDavid Howells 	/* the parent mustn't be init and mustn't be a kernel thread */
1590ee18d64cSDavid Howells 	if (parent->pid <= 1 || !parent->mm)
1591413cd3d9SOleg Nesterov 		goto unlock;
1592ee18d64cSDavid Howells 
1593ee18d64cSDavid Howells 	/* the parent must be single threaded */
1594dd98acf7SOleg Nesterov 	if (!thread_group_empty(parent))
1595413cd3d9SOleg Nesterov 		goto unlock;
1596ee18d64cSDavid Howells 
1597ee18d64cSDavid Howells 	/* the parent and the child must have different session keyrings or
1598ee18d64cSDavid Howells 	 * there's no point */
1599ee18d64cSDavid Howells 	mycred = current_cred();
1600ee18d64cSDavid Howells 	pcred = __task_cred(parent);
1601ee18d64cSDavid Howells 	if (mycred == pcred ||
16023a50597dSDavid Howells 	    mycred->session_keyring == pcred->session_keyring) {
1603413cd3d9SOleg Nesterov 		ret = 0;
1604413cd3d9SOleg Nesterov 		goto unlock;
1605413cd3d9SOleg Nesterov 	}
1606ee18d64cSDavid Howells 
1607ee18d64cSDavid Howells 	/* the parent must have the same effective ownership and mustn't be
1608ee18d64cSDavid Howells 	 * SUID/SGID */
16099a56c2dbSEric W. Biederman 	if (!uid_eq(pcred->uid,	 mycred->euid) ||
16109a56c2dbSEric W. Biederman 	    !uid_eq(pcred->euid, mycred->euid) ||
16119a56c2dbSEric W. Biederman 	    !uid_eq(pcred->suid, mycred->euid) ||
16129a56c2dbSEric W. Biederman 	    !gid_eq(pcred->gid,	 mycred->egid) ||
16139a56c2dbSEric W. Biederman 	    !gid_eq(pcred->egid, mycred->egid) ||
16149a56c2dbSEric W. Biederman 	    !gid_eq(pcred->sgid, mycred->egid))
1615413cd3d9SOleg Nesterov 		goto unlock;
1616ee18d64cSDavid Howells 
1617ee18d64cSDavid Howells 	/* the keyrings must have the same UID */
16183a50597dSDavid Howells 	if ((pcred->session_keyring &&
16192a74dbb9SLinus Torvalds 	     !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
16202a74dbb9SLinus Torvalds 	    !uid_eq(mycred->session_keyring->uid, mycred->euid))
1621413cd3d9SOleg Nesterov 		goto unlock;
1622ee18d64cSDavid Howells 
1623413cd3d9SOleg Nesterov 	/* cancel an already pending keyring replacement */
1624413cd3d9SOleg Nesterov 	oldwork = task_work_cancel(parent, key_change_session_keyring);
1625ee18d64cSDavid Howells 
1626ee18d64cSDavid Howells 	/* the replacement session keyring is applied just prior to userspace
1627ee18d64cSDavid Howells 	 * restarting */
162867d12145SAl Viro 	ret = task_work_add(parent, newwork, true);
1629413cd3d9SOleg Nesterov 	if (!ret)
1630413cd3d9SOleg Nesterov 		newwork = NULL;
1631413cd3d9SOleg Nesterov unlock:
1632ee18d64cSDavid Howells 	write_unlock_irq(&tasklist_lock);
16339d1ac65aSDavid Howells 	rcu_read_unlock();
163467d12145SAl Viro 	if (oldwork)
163567d12145SAl Viro 		put_cred(container_of(oldwork, struct cred, rcu));
163667d12145SAl Viro 	if (newwork)
163767d12145SAl Viro 		put_cred(cred);
1638ee18d64cSDavid Howells 	return ret;
1639ee18d64cSDavid Howells 
1640ee18d64cSDavid Howells error_keyring:
1641ee18d64cSDavid Howells 	key_ref_put(keyring_r);
1642ee18d64cSDavid Howells 	return ret;
1643ee18d64cSDavid Howells }
1644ee18d64cSDavid Howells 
1645b5f545c8SDavid Howells /*
16466563c91fSMat Martineau  * Apply a restriction to a given keyring.
16476563c91fSMat Martineau  *
16486563c91fSMat Martineau  * The caller must have Setattr permission to change keyring restrictions.
16496563c91fSMat Martineau  *
16506563c91fSMat Martineau  * The requested type name may be a NULL pointer to reject all attempts
165118026d86SEric Biggers  * to link to the keyring.  In this case, _restriction must also be NULL.
165218026d86SEric Biggers  * Otherwise, both _type and _restriction must be non-NULL.
16536563c91fSMat Martineau  *
16546563c91fSMat Martineau  * Returns 0 if successful.
16556563c91fSMat Martineau  */
16566563c91fSMat Martineau long keyctl_restrict_keyring(key_serial_t id, const char __user *_type,
16576563c91fSMat Martineau 			     const char __user *_restriction)
16586563c91fSMat Martineau {
16596563c91fSMat Martineau 	key_ref_t key_ref;
16606563c91fSMat Martineau 	char type[32];
16616563c91fSMat Martineau 	char *restriction = NULL;
16626563c91fSMat Martineau 	long ret;
16636563c91fSMat Martineau 
16646563c91fSMat Martineau 	key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
16656563c91fSMat Martineau 	if (IS_ERR(key_ref))
16666563c91fSMat Martineau 		return PTR_ERR(key_ref);
16676563c91fSMat Martineau 
166818026d86SEric Biggers 	ret = -EINVAL;
16696563c91fSMat Martineau 	if (_type) {
167018026d86SEric Biggers 		if (!_restriction)
167118026d86SEric Biggers 			goto error;
167218026d86SEric Biggers 
16736563c91fSMat Martineau 		ret = key_get_type_from_user(type, _type, sizeof(type));
16746563c91fSMat Martineau 		if (ret < 0)
16756563c91fSMat Martineau 			goto error;
16766563c91fSMat Martineau 
16776563c91fSMat Martineau 		restriction = strndup_user(_restriction, PAGE_SIZE);
16786563c91fSMat Martineau 		if (IS_ERR(restriction)) {
16796563c91fSMat Martineau 			ret = PTR_ERR(restriction);
16806563c91fSMat Martineau 			goto error;
16816563c91fSMat Martineau 		}
168218026d86SEric Biggers 	} else {
168318026d86SEric Biggers 		if (_restriction)
168418026d86SEric Biggers 			goto error;
16856563c91fSMat Martineau 	}
16866563c91fSMat Martineau 
168718026d86SEric Biggers 	ret = keyring_restrict(key_ref, _type ? type : NULL, restriction);
16886563c91fSMat Martineau 	kfree(restriction);
16896563c91fSMat Martineau error:
16906563c91fSMat Martineau 	key_ref_put(key_ref);
16916563c91fSMat Martineau 	return ret;
16926563c91fSMat Martineau }
16936563c91fSMat Martineau 
16946563c91fSMat Martineau /*
169545e0f30cSDavid Howells  * Get keyrings subsystem capabilities.
169645e0f30cSDavid Howells  */
169745e0f30cSDavid Howells long keyctl_capabilities(unsigned char __user *_buffer, size_t buflen)
169845e0f30cSDavid Howells {
169945e0f30cSDavid Howells 	size_t size = buflen;
170045e0f30cSDavid Howells 
170145e0f30cSDavid Howells 	if (size > 0) {
170245e0f30cSDavid Howells 		if (size > sizeof(keyrings_capabilities))
170345e0f30cSDavid Howells 			size = sizeof(keyrings_capabilities);
170445e0f30cSDavid Howells 		if (copy_to_user(_buffer, keyrings_capabilities, size) != 0)
170545e0f30cSDavid Howells 			return -EFAULT;
170645e0f30cSDavid Howells 		if (size < buflen &&
170745e0f30cSDavid Howells 		    clear_user(_buffer + size, buflen - size) != 0)
170845e0f30cSDavid Howells 			return -EFAULT;
170945e0f30cSDavid Howells 	}
171045e0f30cSDavid Howells 
171145e0f30cSDavid Howells 	return sizeof(keyrings_capabilities);
171245e0f30cSDavid Howells }
171345e0f30cSDavid Howells 
171445e0f30cSDavid Howells /*
1715973c9f4fSDavid Howells  * The key control system call
17161da177e4SLinus Torvalds  */
1717938bb9f5SHeiko Carstens SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1718938bb9f5SHeiko Carstens 		unsigned long, arg4, unsigned long, arg5)
17191da177e4SLinus Torvalds {
17201da177e4SLinus Torvalds 	switch (option) {
17211da177e4SLinus Torvalds 	case KEYCTL_GET_KEYRING_ID:
17221da177e4SLinus Torvalds 		return keyctl_get_keyring_ID((key_serial_t) arg2,
17231da177e4SLinus Torvalds 					     (int) arg3);
17241da177e4SLinus Torvalds 
17251da177e4SLinus Torvalds 	case KEYCTL_JOIN_SESSION_KEYRING:
17261da177e4SLinus Torvalds 		return keyctl_join_session_keyring((const char __user *) arg2);
17271da177e4SLinus Torvalds 
17281da177e4SLinus Torvalds 	case KEYCTL_UPDATE:
17291da177e4SLinus Torvalds 		return keyctl_update_key((key_serial_t) arg2,
17301da177e4SLinus Torvalds 					 (const void __user *) arg3,
17311da177e4SLinus Torvalds 					 (size_t) arg4);
17321da177e4SLinus Torvalds 
17331da177e4SLinus Torvalds 	case KEYCTL_REVOKE:
17341da177e4SLinus Torvalds 		return keyctl_revoke_key((key_serial_t) arg2);
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds 	case KEYCTL_DESCRIBE:
17371da177e4SLinus Torvalds 		return keyctl_describe_key((key_serial_t) arg2,
17381da177e4SLinus Torvalds 					   (char __user *) arg3,
17391da177e4SLinus Torvalds 					   (unsigned) arg4);
17401da177e4SLinus Torvalds 
17411da177e4SLinus Torvalds 	case KEYCTL_CLEAR:
17421da177e4SLinus Torvalds 		return keyctl_keyring_clear((key_serial_t) arg2);
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds 	case KEYCTL_LINK:
17451da177e4SLinus Torvalds 		return keyctl_keyring_link((key_serial_t) arg2,
17461da177e4SLinus Torvalds 					   (key_serial_t) arg3);
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds 	case KEYCTL_UNLINK:
17491da177e4SLinus Torvalds 		return keyctl_keyring_unlink((key_serial_t) arg2,
17501da177e4SLinus Torvalds 					     (key_serial_t) arg3);
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds 	case KEYCTL_SEARCH:
17531da177e4SLinus Torvalds 		return keyctl_keyring_search((key_serial_t) arg2,
17541da177e4SLinus Torvalds 					     (const char __user *) arg3,
17551da177e4SLinus Torvalds 					     (const char __user *) arg4,
17561da177e4SLinus Torvalds 					     (key_serial_t) arg5);
17571da177e4SLinus Torvalds 
17581da177e4SLinus Torvalds 	case KEYCTL_READ:
17591da177e4SLinus Torvalds 		return keyctl_read_key((key_serial_t) arg2,
17601da177e4SLinus Torvalds 				       (char __user *) arg3,
17611da177e4SLinus Torvalds 				       (size_t) arg4);
17621da177e4SLinus Torvalds 
17631da177e4SLinus Torvalds 	case KEYCTL_CHOWN:
17641da177e4SLinus Torvalds 		return keyctl_chown_key((key_serial_t) arg2,
17651da177e4SLinus Torvalds 					(uid_t) arg3,
17661da177e4SLinus Torvalds 					(gid_t) arg4);
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds 	case KEYCTL_SETPERM:
17691da177e4SLinus Torvalds 		return keyctl_setperm_key((key_serial_t) arg2,
17701da177e4SLinus Torvalds 					  (key_perm_t) arg3);
17711da177e4SLinus Torvalds 
17721da177e4SLinus Torvalds 	case KEYCTL_INSTANTIATE:
17731da177e4SLinus Torvalds 		return keyctl_instantiate_key((key_serial_t) arg2,
17741da177e4SLinus Torvalds 					      (const void __user *) arg3,
17751da177e4SLinus Torvalds 					      (size_t) arg4,
17761da177e4SLinus Torvalds 					      (key_serial_t) arg5);
17771da177e4SLinus Torvalds 
17781da177e4SLinus Torvalds 	case KEYCTL_NEGATE:
17791da177e4SLinus Torvalds 		return keyctl_negate_key((key_serial_t) arg2,
17801da177e4SLinus Torvalds 					 (unsigned) arg3,
17811da177e4SLinus Torvalds 					 (key_serial_t) arg4);
17821da177e4SLinus Torvalds 
17833e30148cSDavid Howells 	case KEYCTL_SET_REQKEY_KEYRING:
17843e30148cSDavid Howells 		return keyctl_set_reqkey_keyring(arg2);
17853e30148cSDavid Howells 
1786017679c4SDavid Howells 	case KEYCTL_SET_TIMEOUT:
1787017679c4SDavid Howells 		return keyctl_set_timeout((key_serial_t) arg2,
1788017679c4SDavid Howells 					  (unsigned) arg3);
1789017679c4SDavid Howells 
1790b5f545c8SDavid Howells 	case KEYCTL_ASSUME_AUTHORITY:
1791b5f545c8SDavid Howells 		return keyctl_assume_authority((key_serial_t) arg2);
1792b5f545c8SDavid Howells 
179370a5bb72SDavid Howells 	case KEYCTL_GET_SECURITY:
179470a5bb72SDavid Howells 		return keyctl_get_security((key_serial_t) arg2,
179590bd49abSJames Morris 					   (char __user *) arg3,
179670a5bb72SDavid Howells 					   (size_t) arg4);
179770a5bb72SDavid Howells 
1798ee18d64cSDavid Howells 	case KEYCTL_SESSION_TO_PARENT:
1799ee18d64cSDavid Howells 		return keyctl_session_to_parent();
1800ee18d64cSDavid Howells 
1801fdd1b945SDavid Howells 	case KEYCTL_REJECT:
1802fdd1b945SDavid Howells 		return keyctl_reject_key((key_serial_t) arg2,
1803fdd1b945SDavid Howells 					 (unsigned) arg3,
1804fdd1b945SDavid Howells 					 (unsigned) arg4,
1805fdd1b945SDavid Howells 					 (key_serial_t) arg5);
1806fdd1b945SDavid Howells 
1807ee009e4aSDavid Howells 	case KEYCTL_INSTANTIATE_IOV:
1808ee009e4aSDavid Howells 		return keyctl_instantiate_key_iov(
1809ee009e4aSDavid Howells 			(key_serial_t) arg2,
1810ee009e4aSDavid Howells 			(const struct iovec __user *) arg3,
1811ee009e4aSDavid Howells 			(unsigned) arg4,
1812ee009e4aSDavid Howells 			(key_serial_t) arg5);
1813ee009e4aSDavid Howells 
1814fd75815fSDavid Howells 	case KEYCTL_INVALIDATE:
1815fd75815fSDavid Howells 		return keyctl_invalidate_key((key_serial_t) arg2);
1816fd75815fSDavid Howells 
1817f36f8c75SDavid Howells 	case KEYCTL_GET_PERSISTENT:
1818f36f8c75SDavid Howells 		return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3);
1819f36f8c75SDavid Howells 
1820ddbb4114SMat Martineau 	case KEYCTL_DH_COMPUTE:
1821ddbb4114SMat Martineau 		return keyctl_dh_compute((struct keyctl_dh_params __user *) arg2,
18224693fc73SStephan Mueller 					 (char __user *) arg3, (size_t) arg4,
1823f1c316a3SStephan Mueller 					 (struct keyctl_kdf_params __user *) arg5);
1824ddbb4114SMat Martineau 
18256563c91fSMat Martineau 	case KEYCTL_RESTRICT_KEYRING:
18266563c91fSMat Martineau 		return keyctl_restrict_keyring((key_serial_t) arg2,
18276563c91fSMat Martineau 					       (const char __user *) arg3,
18286563c91fSMat Martineau 					       (const char __user *) arg4);
18291da177e4SLinus Torvalds 
183000d60fd3SDavid Howells 	case KEYCTL_PKEY_QUERY:
183100d60fd3SDavid Howells 		if (arg3 != 0)
183200d60fd3SDavid Howells 			return -EINVAL;
183300d60fd3SDavid Howells 		return keyctl_pkey_query((key_serial_t)arg2,
183400d60fd3SDavid Howells 					 (const char __user *)arg4,
1835468e91ceSBen Dooks 					 (struct keyctl_pkey_query __user *)arg5);
183600d60fd3SDavid Howells 
183700d60fd3SDavid Howells 	case KEYCTL_PKEY_ENCRYPT:
183800d60fd3SDavid Howells 	case KEYCTL_PKEY_DECRYPT:
183900d60fd3SDavid Howells 	case KEYCTL_PKEY_SIGN:
184000d60fd3SDavid Howells 		return keyctl_pkey_e_d_s(
184100d60fd3SDavid Howells 			option,
184200d60fd3SDavid Howells 			(const struct keyctl_pkey_params __user *)arg2,
184300d60fd3SDavid Howells 			(const char __user *)arg3,
184400d60fd3SDavid Howells 			(const void __user *)arg4,
184500d60fd3SDavid Howells 			(void __user *)arg5);
184600d60fd3SDavid Howells 
184700d60fd3SDavid Howells 	case KEYCTL_PKEY_VERIFY:
184800d60fd3SDavid Howells 		return keyctl_pkey_verify(
184900d60fd3SDavid Howells 			(const struct keyctl_pkey_params __user *)arg2,
185000d60fd3SDavid Howells 			(const char __user *)arg3,
185100d60fd3SDavid Howells 			(const void __user *)arg4,
185200d60fd3SDavid Howells 			(const void __user *)arg5);
185300d60fd3SDavid Howells 
1854ed0ac5c7SDavid Howells 	case KEYCTL_MOVE:
1855ed0ac5c7SDavid Howells 		return keyctl_keyring_move((key_serial_t)arg2,
1856ed0ac5c7SDavid Howells 					   (key_serial_t)arg3,
1857ed0ac5c7SDavid Howells 					   (key_serial_t)arg4,
1858ed0ac5c7SDavid Howells 					   (unsigned int)arg5);
1859ed0ac5c7SDavid Howells 
186045e0f30cSDavid Howells 	case KEYCTL_CAPABILITIES:
186145e0f30cSDavid Howells 		return keyctl_capabilities((unsigned char __user *)arg2, (size_t)arg3);
186245e0f30cSDavid Howells 
18631da177e4SLinus Torvalds 	default:
18641da177e4SLinus Torvalds 		return -EOPNOTSUPP;
18651da177e4SLinus Torvalds 	}
18661da177e4SLinus Torvalds }
1867