xref: /openbmc/linux/security/keys/keyctl.c (revision 5b825c3a)
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/module.h>
131da177e4SLinus Torvalds #include <linux/init.h>
141da177e4SLinus Torvalds #include <linux/sched.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>
21*5b825c3aSIngo 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>
281da177e4SLinus Torvalds #include "internal.h"
291da177e4SLinus Torvalds 
30aa9d4437SDavid Howells #define KEY_MAX_DESC_SIZE 4096
31aa9d4437SDavid Howells 
320cb409d9SDavi Arnaut static int key_get_type_from_user(char *type,
330cb409d9SDavi Arnaut 				  const char __user *_type,
340cb409d9SDavi Arnaut 				  unsigned len)
350cb409d9SDavi Arnaut {
360cb409d9SDavi Arnaut 	int ret;
370cb409d9SDavi Arnaut 
380cb409d9SDavi Arnaut 	ret = strncpy_from_user(type, _type, len);
390cb409d9SDavi Arnaut 	if (ret < 0)
404303ef19SDan Carpenter 		return ret;
410cb409d9SDavi Arnaut 	if (ret == 0 || ret >= len)
420cb409d9SDavi Arnaut 		return -EINVAL;
4354e2c2c1SDavid Howells 	if (type[0] == '.')
4454e2c2c1SDavid Howells 		return -EPERM;
450cb409d9SDavi Arnaut 	type[len - 1] = '\0';
460cb409d9SDavi Arnaut 	return 0;
470cb409d9SDavi Arnaut }
480cb409d9SDavi Arnaut 
491da177e4SLinus Torvalds /*
50973c9f4fSDavid Howells  * Extract the description of a new key from userspace and either add it as a
51973c9f4fSDavid Howells  * new key to the specified keyring or update a matching key in that keyring.
52973c9f4fSDavid Howells  *
53cf7f601cSDavid Howells  * If the description is NULL or an empty string, the key type is asked to
54cf7f601cSDavid Howells  * generate one from the payload.
55cf7f601cSDavid Howells  *
56973c9f4fSDavid Howells  * The keyring must be writable so that we can attach the key to it.
57973c9f4fSDavid Howells  *
58973c9f4fSDavid Howells  * If successful, the new key's serial number is returned, otherwise an error
59973c9f4fSDavid Howells  * code is returned.
601da177e4SLinus Torvalds  */
611e7bfb21SHeiko Carstens SYSCALL_DEFINE5(add_key, const char __user *, _type,
621e7bfb21SHeiko Carstens 		const char __user *, _description,
631e7bfb21SHeiko Carstens 		const void __user *, _payload,
641e7bfb21SHeiko Carstens 		size_t, plen,
651e7bfb21SHeiko Carstens 		key_serial_t, ringid)
661da177e4SLinus Torvalds {
67664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref;
681da177e4SLinus Torvalds 	char type[32], *description;
691da177e4SLinus Torvalds 	void *payload;
700cb409d9SDavi Arnaut 	long ret;
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 	ret = -EINVAL;
7338bbca6bSDavid Howells 	if (plen > 1024 * 1024 - 1)
741da177e4SLinus Torvalds 		goto error;
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 	/* draw all the data into kernel space */
770cb409d9SDavi Arnaut 	ret = key_get_type_from_user(type, _type, sizeof(type));
781da177e4SLinus Torvalds 	if (ret < 0)
791da177e4SLinus Torvalds 		goto error;
801da177e4SLinus Torvalds 
81cf7f601cSDavid Howells 	description = NULL;
82cf7f601cSDavid Howells 	if (_description) {
83aa9d4437SDavid Howells 		description = strndup_user(_description, KEY_MAX_DESC_SIZE);
840cb409d9SDavi Arnaut 		if (IS_ERR(description)) {
850cb409d9SDavi Arnaut 			ret = PTR_ERR(description);
863e30148cSDavid Howells 			goto error;
870cb409d9SDavi Arnaut 		}
88cf7f601cSDavid Howells 		if (!*description) {
89cf7f601cSDavid Howells 			kfree(description);
90cf7f601cSDavid Howells 			description = NULL;
91a4e3b8d7SMimi Zohar 		} else if ((description[0] == '.') &&
92a4e3b8d7SMimi Zohar 			   (strncmp(type, "keyring", 7) == 0)) {
93a4e3b8d7SMimi Zohar 			ret = -EPERM;
94a4e3b8d7SMimi Zohar 			goto error2;
95cf7f601cSDavid Howells 		}
96cf7f601cSDavid Howells 	}
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
991da177e4SLinus Torvalds 	payload = NULL;
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	if (_payload) {
1021da177e4SLinus Torvalds 		ret = -ENOMEM;
1034f1c28d2SAndrew Morton 		payload = kmalloc(plen, GFP_KERNEL | __GFP_NOWARN);
10438bbca6bSDavid Howells 		if (!payload) {
10538bbca6bSDavid Howells 			if (plen <= PAGE_SIZE)
10638bbca6bSDavid Howells 				goto error2;
10738bbca6bSDavid Howells 			payload = vmalloc(plen);
1081da177e4SLinus Torvalds 			if (!payload)
1091da177e4SLinus Torvalds 				goto error2;
11038bbca6bSDavid Howells 		}
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 		ret = -EFAULT;
1131da177e4SLinus Torvalds 		if (copy_from_user(payload, _payload, plen) != 0)
1141da177e4SLinus Torvalds 			goto error3;
1151da177e4SLinus Torvalds 	}
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	/* find the target keyring (which must be writable) */
118f5895943SDavid Howells 	keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
119664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
120664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
1211da177e4SLinus Torvalds 		goto error3;
1221da177e4SLinus Torvalds 	}
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	/* create or update the requested key and add it to the target
1251da177e4SLinus Torvalds 	 * keyring */
126664cceb0SDavid Howells 	key_ref = key_create_or_update(keyring_ref, type, description,
1276b79ccb5SArun Raghavan 				       payload, plen, KEY_PERM_UNDEF,
1286b79ccb5SArun Raghavan 				       KEY_ALLOC_IN_QUOTA);
129664cceb0SDavid Howells 	if (!IS_ERR(key_ref)) {
130664cceb0SDavid Howells 		ret = key_ref_to_ptr(key_ref)->serial;
131664cceb0SDavid Howells 		key_ref_put(key_ref);
1321da177e4SLinus Torvalds 	}
1331da177e4SLinus Torvalds 	else {
134664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
1351da177e4SLinus Torvalds 	}
1361da177e4SLinus Torvalds 
137664cceb0SDavid Howells 	key_ref_put(keyring_ref);
1381da177e4SLinus Torvalds  error3:
139d0e0eba0SGeliang Tang 	kvfree(payload);
1401da177e4SLinus Torvalds  error2:
1411da177e4SLinus Torvalds 	kfree(description);
1421da177e4SLinus Torvalds  error:
1431da177e4SLinus Torvalds 	return ret;
144a8b17ed0SDavid Howells }
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds /*
147973c9f4fSDavid Howells  * Search the process keyrings and keyring trees linked from those for a
148973c9f4fSDavid Howells  * matching key.  Keyrings must have appropriate Search permission to be
149973c9f4fSDavid Howells  * searched.
150973c9f4fSDavid Howells  *
151973c9f4fSDavid Howells  * If a key is found, it will be attached to the destination keyring if there's
152973c9f4fSDavid Howells  * one specified and the serial number of the key will be returned.
153973c9f4fSDavid Howells  *
154973c9f4fSDavid Howells  * If no key is found, /sbin/request-key will be invoked if _callout_info is
155973c9f4fSDavid Howells  * non-NULL in an attempt to create a key.  The _callout_info string will be
156973c9f4fSDavid Howells  * passed to /sbin/request-key to aid with completing the request.  If the
157973c9f4fSDavid Howells  * _callout_info string is "" then it will be changed to "-".
1581da177e4SLinus Torvalds  */
1591e7bfb21SHeiko Carstens SYSCALL_DEFINE4(request_key, const char __user *, _type,
1601e7bfb21SHeiko Carstens 		const char __user *, _description,
1611e7bfb21SHeiko Carstens 		const char __user *, _callout_info,
1621e7bfb21SHeiko Carstens 		key_serial_t, destringid)
1631da177e4SLinus Torvalds {
1641da177e4SLinus Torvalds 	struct key_type *ktype;
165664cceb0SDavid Howells 	struct key *key;
166664cceb0SDavid Howells 	key_ref_t dest_ref;
1674a38e122SDavid Howells 	size_t callout_len;
1681da177e4SLinus Torvalds 	char type[32], *description, *callout_info;
1690cb409d9SDavi Arnaut 	long ret;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	/* pull the type into kernel space */
1720cb409d9SDavi Arnaut 	ret = key_get_type_from_user(type, _type, sizeof(type));
1731da177e4SLinus Torvalds 	if (ret < 0)
1741da177e4SLinus Torvalds 		goto error;
1751260f801SDavid Howells 
1761da177e4SLinus Torvalds 	/* pull the description into kernel space */
177aa9d4437SDavid Howells 	description = strndup_user(_description, KEY_MAX_DESC_SIZE);
1780cb409d9SDavi Arnaut 	if (IS_ERR(description)) {
1790cb409d9SDavi Arnaut 		ret = PTR_ERR(description);
1801da177e4SLinus Torvalds 		goto error;
1810cb409d9SDavi Arnaut 	}
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	/* pull the callout info into kernel space */
1841da177e4SLinus Torvalds 	callout_info = NULL;
1854a38e122SDavid Howells 	callout_len = 0;
1861da177e4SLinus Torvalds 	if (_callout_info) {
1870cb409d9SDavi Arnaut 		callout_info = strndup_user(_callout_info, PAGE_SIZE);
1880cb409d9SDavi Arnaut 		if (IS_ERR(callout_info)) {
1890cb409d9SDavi Arnaut 			ret = PTR_ERR(callout_info);
1901da177e4SLinus Torvalds 			goto error2;
1910cb409d9SDavi Arnaut 		}
1924a38e122SDavid Howells 		callout_len = strlen(callout_info);
1931da177e4SLinus Torvalds 	}
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	/* get the destination keyring if specified */
196664cceb0SDavid Howells 	dest_ref = NULL;
1971da177e4SLinus Torvalds 	if (destringid) {
1985593122eSDavid Howells 		dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
199f5895943SDavid Howells 					   KEY_NEED_WRITE);
200664cceb0SDavid Howells 		if (IS_ERR(dest_ref)) {
201664cceb0SDavid Howells 			ret = PTR_ERR(dest_ref);
2021da177e4SLinus Torvalds 			goto error3;
2031da177e4SLinus Torvalds 		}
2041da177e4SLinus Torvalds 	}
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	/* find the key type */
2071da177e4SLinus Torvalds 	ktype = key_type_lookup(type);
2081da177e4SLinus Torvalds 	if (IS_ERR(ktype)) {
2091da177e4SLinus Torvalds 		ret = PTR_ERR(ktype);
2101da177e4SLinus Torvalds 		goto error4;
2111da177e4SLinus Torvalds 	}
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	/* do the search */
2144a38e122SDavid Howells 	key = request_key_and_link(ktype, description, callout_info,
2154a38e122SDavid Howells 				   callout_len, NULL, key_ref_to_ptr(dest_ref),
2167e047ef5SDavid Howells 				   KEY_ALLOC_IN_QUOTA);
2171da177e4SLinus Torvalds 	if (IS_ERR(key)) {
2181da177e4SLinus Torvalds 		ret = PTR_ERR(key);
2191da177e4SLinus Torvalds 		goto error5;
2201da177e4SLinus Torvalds 	}
2211da177e4SLinus Torvalds 
2224aab1e89SDavid Howells 	/* wait for the key to finish being constructed */
2234aab1e89SDavid Howells 	ret = wait_for_key_construction(key, 1);
2244aab1e89SDavid Howells 	if (ret < 0)
2254aab1e89SDavid Howells 		goto error6;
2264aab1e89SDavid Howells 
2271da177e4SLinus Torvalds 	ret = key->serial;
2281da177e4SLinus Torvalds 
2294aab1e89SDavid Howells error6:
2301da177e4SLinus Torvalds  	key_put(key);
2311da177e4SLinus Torvalds error5:
2321da177e4SLinus Torvalds 	key_type_put(ktype);
2331da177e4SLinus Torvalds error4:
234664cceb0SDavid Howells 	key_ref_put(dest_ref);
2351da177e4SLinus Torvalds error3:
2361da177e4SLinus Torvalds 	kfree(callout_info);
2371da177e4SLinus Torvalds error2:
2381da177e4SLinus Torvalds 	kfree(description);
2391da177e4SLinus Torvalds error:
2401da177e4SLinus Torvalds 	return ret;
241a8b17ed0SDavid Howells }
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds /*
244973c9f4fSDavid Howells  * Get the ID of the specified process keyring.
245973c9f4fSDavid Howells  *
246973c9f4fSDavid Howells  * The requested keyring must have search permission to be found.
247973c9f4fSDavid Howells  *
248973c9f4fSDavid Howells  * If successful, the ID of the requested keyring will be returned.
2491da177e4SLinus Torvalds  */
2501da177e4SLinus Torvalds long keyctl_get_keyring_ID(key_serial_t id, int create)
2511da177e4SLinus Torvalds {
252664cceb0SDavid Howells 	key_ref_t key_ref;
2535593122eSDavid Howells 	unsigned long lflags;
2541da177e4SLinus Torvalds 	long ret;
2551da177e4SLinus Torvalds 
2565593122eSDavid Howells 	lflags = create ? KEY_LOOKUP_CREATE : 0;
257f5895943SDavid Howells 	key_ref = lookup_user_key(id, lflags, KEY_NEED_SEARCH);
258664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
259664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
2601da177e4SLinus Torvalds 		goto error;
2611da177e4SLinus Torvalds 	}
2621da177e4SLinus Torvalds 
263664cceb0SDavid Howells 	ret = key_ref_to_ptr(key_ref)->serial;
264664cceb0SDavid Howells 	key_ref_put(key_ref);
2651da177e4SLinus Torvalds error:
2661da177e4SLinus Torvalds 	return ret;
267973c9f4fSDavid Howells }
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds /*
270973c9f4fSDavid Howells  * Join a (named) session keyring.
271973c9f4fSDavid Howells  *
272973c9f4fSDavid Howells  * Create and join an anonymous session keyring or join a named session
273973c9f4fSDavid Howells  * keyring, creating it if necessary.  A named session keyring must have Search
274973c9f4fSDavid Howells  * permission for it to be joined.  Session keyrings without this permit will
275973c9f4fSDavid Howells  * be skipped over.
276973c9f4fSDavid Howells  *
277973c9f4fSDavid Howells  * If successful, the ID of the joined session keyring will be returned.
2781da177e4SLinus Torvalds  */
2791da177e4SLinus Torvalds long keyctl_join_session_keyring(const char __user *_name)
2801da177e4SLinus Torvalds {
2811da177e4SLinus Torvalds 	char *name;
2820cb409d9SDavi Arnaut 	long ret;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	/* fetch the name from userspace */
2851da177e4SLinus Torvalds 	name = NULL;
2861da177e4SLinus Torvalds 	if (_name) {
287aa9d4437SDavid Howells 		name = strndup_user(_name, KEY_MAX_DESC_SIZE);
2880cb409d9SDavi Arnaut 		if (IS_ERR(name)) {
2890cb409d9SDavi Arnaut 			ret = PTR_ERR(name);
2901da177e4SLinus Torvalds 			goto error;
2910cb409d9SDavi Arnaut 		}
2921da177e4SLinus Torvalds 	}
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	/* join the session */
2951da177e4SLinus Torvalds 	ret = join_session_keyring(name);
2960d54ee1cSVegard Nossum 	kfree(name);
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds error:
2991da177e4SLinus Torvalds 	return ret;
300a8b17ed0SDavid Howells }
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds /*
303973c9f4fSDavid Howells  * Update a key's data payload from the given data.
304973c9f4fSDavid Howells  *
305973c9f4fSDavid Howells  * The key must grant the caller Write permission and the key type must support
306973c9f4fSDavid Howells  * updating for this to work.  A negative key can be positively instantiated
307973c9f4fSDavid Howells  * with this call.
308973c9f4fSDavid Howells  *
309973c9f4fSDavid Howells  * If successful, 0 will be returned.  If the key type does not support
310973c9f4fSDavid Howells  * updating, then -EOPNOTSUPP will be returned.
3111da177e4SLinus Torvalds  */
3121da177e4SLinus Torvalds long keyctl_update_key(key_serial_t id,
3131da177e4SLinus Torvalds 		       const void __user *_payload,
3141da177e4SLinus Torvalds 		       size_t plen)
3151da177e4SLinus Torvalds {
316664cceb0SDavid Howells 	key_ref_t key_ref;
3171da177e4SLinus Torvalds 	void *payload;
3181da177e4SLinus Torvalds 	long ret;
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	ret = -EINVAL;
3211da177e4SLinus Torvalds 	if (plen > PAGE_SIZE)
3221da177e4SLinus Torvalds 		goto error;
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
3251da177e4SLinus Torvalds 	payload = NULL;
3261da177e4SLinus Torvalds 	if (_payload) {
3271da177e4SLinus Torvalds 		ret = -ENOMEM;
3281da177e4SLinus Torvalds 		payload = kmalloc(plen, GFP_KERNEL);
3291da177e4SLinus Torvalds 		if (!payload)
3301da177e4SLinus Torvalds 			goto error;
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 		ret = -EFAULT;
3331da177e4SLinus Torvalds 		if (copy_from_user(payload, _payload, plen) != 0)
3341da177e4SLinus Torvalds 			goto error2;
3351da177e4SLinus Torvalds 	}
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	/* find the target key (which must be writable) */
338f5895943SDavid Howells 	key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
339664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
340664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
3411da177e4SLinus Torvalds 		goto error2;
3421da177e4SLinus Torvalds 	}
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	/* update the key */
345664cceb0SDavid Howells 	ret = key_update(key_ref, payload, plen);
3461da177e4SLinus Torvalds 
347664cceb0SDavid Howells 	key_ref_put(key_ref);
3481da177e4SLinus Torvalds error2:
3491da177e4SLinus Torvalds 	kfree(payload);
3501da177e4SLinus Torvalds error:
3511da177e4SLinus Torvalds 	return ret;
352a8b17ed0SDavid Howells }
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds /*
355973c9f4fSDavid Howells  * Revoke a key.
356973c9f4fSDavid Howells  *
357973c9f4fSDavid Howells  * The key must be grant the caller Write or Setattr permission for this to
358973c9f4fSDavid Howells  * work.  The key type should give up its quota claim when revoked.  The key
359973c9f4fSDavid Howells  * and any links to the key will be automatically garbage collected after a
360973c9f4fSDavid Howells  * certain amount of time (/proc/sys/kernel/keys/gc_delay).
361973c9f4fSDavid Howells  *
362d3600bcfSMimi Zohar  * Keys with KEY_FLAG_KEEP set should not be revoked.
363d3600bcfSMimi Zohar  *
364973c9f4fSDavid Howells  * If successful, 0 is returned.
3651da177e4SLinus Torvalds  */
3661da177e4SLinus Torvalds long keyctl_revoke_key(key_serial_t id)
3671da177e4SLinus Torvalds {
368664cceb0SDavid Howells 	key_ref_t key_ref;
369d3600bcfSMimi Zohar 	struct key *key;
3701da177e4SLinus Torvalds 	long ret;
3711da177e4SLinus Torvalds 
372f5895943SDavid Howells 	key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
373664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
374664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
3750c2c9a3fSDavid Howells 		if (ret != -EACCES)
3761da177e4SLinus Torvalds 			goto error;
377f5895943SDavid Howells 		key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
3780c2c9a3fSDavid Howells 		if (IS_ERR(key_ref)) {
3790c2c9a3fSDavid Howells 			ret = PTR_ERR(key_ref);
3800c2c9a3fSDavid Howells 			goto error;
3810c2c9a3fSDavid Howells 		}
3821da177e4SLinus Torvalds 	}
3831da177e4SLinus Torvalds 
384d3600bcfSMimi Zohar 	key = key_ref_to_ptr(key_ref);
3851da177e4SLinus Torvalds 	ret = 0;
3861d6d167cSMimi Zohar 	if (test_bit(KEY_FLAG_KEEP, &key->flags))
3871d6d167cSMimi Zohar 		ret = -EPERM;
3881d6d167cSMimi Zohar 	else
3891d6d167cSMimi Zohar 		key_revoke(key);
3901da177e4SLinus Torvalds 
391664cceb0SDavid Howells 	key_ref_put(key_ref);
3921da177e4SLinus Torvalds error:
3931260f801SDavid Howells 	return ret;
394a8b17ed0SDavid Howells }
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds /*
397fd75815fSDavid Howells  * Invalidate a key.
398fd75815fSDavid Howells  *
399fd75815fSDavid Howells  * The key must be grant the caller Invalidate permission for this to work.
400fd75815fSDavid Howells  * The key and any links to the key will be automatically garbage collected
401fd75815fSDavid Howells  * immediately.
402fd75815fSDavid Howells  *
403d3600bcfSMimi Zohar  * Keys with KEY_FLAG_KEEP set should not be invalidated.
404d3600bcfSMimi Zohar  *
405fd75815fSDavid Howells  * If successful, 0 is returned.
406fd75815fSDavid Howells  */
407fd75815fSDavid Howells long keyctl_invalidate_key(key_serial_t id)
408fd75815fSDavid Howells {
409fd75815fSDavid Howells 	key_ref_t key_ref;
410d3600bcfSMimi Zohar 	struct key *key;
411fd75815fSDavid Howells 	long ret;
412fd75815fSDavid Howells 
413fd75815fSDavid Howells 	kenter("%d", id);
414fd75815fSDavid Howells 
415f5895943SDavid Howells 	key_ref = lookup_user_key(id, 0, KEY_NEED_SEARCH);
416fd75815fSDavid Howells 	if (IS_ERR(key_ref)) {
417fd75815fSDavid Howells 		ret = PTR_ERR(key_ref);
4180c7774abSDavid Howells 
4190c7774abSDavid Howells 		/* Root is permitted to invalidate certain special keys */
4200c7774abSDavid Howells 		if (capable(CAP_SYS_ADMIN)) {
4210c7774abSDavid Howells 			key_ref = lookup_user_key(id, 0, 0);
4220c7774abSDavid Howells 			if (IS_ERR(key_ref))
4230c7774abSDavid Howells 				goto error;
4240c7774abSDavid Howells 			if (test_bit(KEY_FLAG_ROOT_CAN_INVAL,
4250c7774abSDavid Howells 				     &key_ref_to_ptr(key_ref)->flags))
4260c7774abSDavid Howells 				goto invalidate;
4270c7774abSDavid Howells 			goto error_put;
4280c7774abSDavid Howells 		}
4290c7774abSDavid Howells 
430fd75815fSDavid Howells 		goto error;
431fd75815fSDavid Howells 	}
432fd75815fSDavid Howells 
4330c7774abSDavid Howells invalidate:
434d3600bcfSMimi Zohar 	key = key_ref_to_ptr(key_ref);
435fd75815fSDavid Howells 	ret = 0;
436d3600bcfSMimi Zohar 	if (test_bit(KEY_FLAG_KEEP, &key->flags))
437d3600bcfSMimi Zohar 		ret = -EPERM;
4381d6d167cSMimi Zohar 	else
439d3600bcfSMimi Zohar 		key_invalidate(key);
4400c7774abSDavid Howells error_put:
441fd75815fSDavid Howells 	key_ref_put(key_ref);
442fd75815fSDavid Howells error:
443fd75815fSDavid Howells 	kleave(" = %ld", ret);
444fd75815fSDavid Howells 	return ret;
445fd75815fSDavid Howells }
446fd75815fSDavid Howells 
447fd75815fSDavid Howells /*
448973c9f4fSDavid Howells  * Clear the specified keyring, creating an empty process keyring if one of the
449973c9f4fSDavid Howells  * special keyring IDs is used.
450973c9f4fSDavid Howells  *
451d3600bcfSMimi Zohar  * The keyring must grant the caller Write permission and not have
452d3600bcfSMimi Zohar  * KEY_FLAG_KEEP set for this to work.  If successful, 0 will be returned.
4531da177e4SLinus Torvalds  */
4541da177e4SLinus Torvalds long keyctl_keyring_clear(key_serial_t ringid)
4551da177e4SLinus Torvalds {
456664cceb0SDavid Howells 	key_ref_t keyring_ref;
457d3600bcfSMimi Zohar 	struct key *keyring;
4581da177e4SLinus Torvalds 	long ret;
4591da177e4SLinus Torvalds 
460f5895943SDavid Howells 	keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
461664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
462664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
463700920ebSDavid Howells 
464700920ebSDavid Howells 		/* Root is permitted to invalidate certain special keyrings */
465700920ebSDavid Howells 		if (capable(CAP_SYS_ADMIN)) {
466700920ebSDavid Howells 			keyring_ref = lookup_user_key(ringid, 0, 0);
467700920ebSDavid Howells 			if (IS_ERR(keyring_ref))
468700920ebSDavid Howells 				goto error;
469700920ebSDavid Howells 			if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
470700920ebSDavid Howells 				     &key_ref_to_ptr(keyring_ref)->flags))
471700920ebSDavid Howells 				goto clear;
472700920ebSDavid Howells 			goto error_put;
473700920ebSDavid Howells 		}
474700920ebSDavid Howells 
4751da177e4SLinus Torvalds 		goto error;
4761da177e4SLinus Torvalds 	}
4771da177e4SLinus Torvalds 
478700920ebSDavid Howells clear:
479d3600bcfSMimi Zohar 	keyring = key_ref_to_ptr(keyring_ref);
480d3600bcfSMimi Zohar 	if (test_bit(KEY_FLAG_KEEP, &keyring->flags))
481d3600bcfSMimi Zohar 		ret = -EPERM;
482d3600bcfSMimi Zohar 	else
483d3600bcfSMimi Zohar 		ret = keyring_clear(keyring);
484700920ebSDavid Howells error_put:
485664cceb0SDavid Howells 	key_ref_put(keyring_ref);
4861da177e4SLinus Torvalds error:
4871da177e4SLinus Torvalds 	return ret;
488a8b17ed0SDavid Howells }
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds /*
491973c9f4fSDavid Howells  * Create a link from a keyring to a key if there's no matching key in the
492973c9f4fSDavid Howells  * keyring, otherwise replace the link to the matching key with a link to the
493973c9f4fSDavid Howells  * new key.
494973c9f4fSDavid Howells  *
495973c9f4fSDavid Howells  * The key must grant the caller Link permission and the the keyring must grant
496973c9f4fSDavid Howells  * the caller Write permission.  Furthermore, if an additional link is created,
497973c9f4fSDavid Howells  * the keyring's quota will be extended.
498973c9f4fSDavid Howells  *
499973c9f4fSDavid Howells  * If successful, 0 will be returned.
5001da177e4SLinus Torvalds  */
5011da177e4SLinus Torvalds long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
5021da177e4SLinus Torvalds {
503664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref;
5041da177e4SLinus Torvalds 	long ret;
5051da177e4SLinus Torvalds 
506f5895943SDavid Howells 	keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
507664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
508664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
5091da177e4SLinus Torvalds 		goto error;
5101da177e4SLinus Torvalds 	}
5111da177e4SLinus Torvalds 
512f5895943SDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
513664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
514664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
5151da177e4SLinus Torvalds 		goto error2;
5161da177e4SLinus Torvalds 	}
5171da177e4SLinus Torvalds 
518664cceb0SDavid Howells 	ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
5191da177e4SLinus Torvalds 
520664cceb0SDavid Howells 	key_ref_put(key_ref);
5211da177e4SLinus Torvalds error2:
522664cceb0SDavid Howells 	key_ref_put(keyring_ref);
5231da177e4SLinus Torvalds error:
5241da177e4SLinus Torvalds 	return ret;
525a8b17ed0SDavid Howells }
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds /*
528973c9f4fSDavid Howells  * Unlink a key from a keyring.
529973c9f4fSDavid Howells  *
530973c9f4fSDavid Howells  * The keyring must grant the caller Write permission for this to work; the key
531973c9f4fSDavid Howells  * itself need not grant the caller anything.  If the last link to a key is
532973c9f4fSDavid Howells  * removed then that key will be scheduled for destruction.
533973c9f4fSDavid Howells  *
534d3600bcfSMimi Zohar  * Keys or keyrings with KEY_FLAG_KEEP set should not be unlinked.
535d3600bcfSMimi Zohar  *
536973c9f4fSDavid Howells  * If successful, 0 will be returned.
5371da177e4SLinus Torvalds  */
5381da177e4SLinus Torvalds long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
5391da177e4SLinus Torvalds {
540664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref;
541d3600bcfSMimi Zohar 	struct key *keyring, *key;
5421da177e4SLinus Torvalds 	long ret;
5431da177e4SLinus Torvalds 
544f5895943SDavid Howells 	keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE);
545664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
546664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
5471da177e4SLinus Torvalds 		goto error;
5481da177e4SLinus Torvalds 	}
5491da177e4SLinus Torvalds 
5505593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
551664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
552664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
5531da177e4SLinus Torvalds 		goto error2;
5541da177e4SLinus Torvalds 	}
5551da177e4SLinus Torvalds 
556d3600bcfSMimi Zohar 	keyring = key_ref_to_ptr(keyring_ref);
557d3600bcfSMimi Zohar 	key = key_ref_to_ptr(key_ref);
558d3600bcfSMimi Zohar 	if (test_bit(KEY_FLAG_KEEP, &keyring->flags) &&
559d3600bcfSMimi Zohar 	    test_bit(KEY_FLAG_KEEP, &key->flags))
560d3600bcfSMimi Zohar 		ret = -EPERM;
561d3600bcfSMimi Zohar 	else
562d3600bcfSMimi Zohar 		ret = key_unlink(keyring, key);
5631da177e4SLinus Torvalds 
564664cceb0SDavid Howells 	key_ref_put(key_ref);
5651da177e4SLinus Torvalds error2:
566664cceb0SDavid Howells 	key_ref_put(keyring_ref);
5671da177e4SLinus Torvalds error:
5681da177e4SLinus Torvalds 	return ret;
569a8b17ed0SDavid Howells }
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds /*
572973c9f4fSDavid Howells  * Return a description of a key to userspace.
573973c9f4fSDavid Howells  *
574973c9f4fSDavid Howells  * The key must grant the caller View permission for this to work.
575973c9f4fSDavid Howells  *
576973c9f4fSDavid Howells  * If there's a buffer, we place up to buflen bytes of data into it formatted
577973c9f4fSDavid Howells  * in the following way:
578973c9f4fSDavid Howells  *
5791da177e4SLinus Torvalds  *	type;uid;gid;perm;description<NUL>
580973c9f4fSDavid Howells  *
581973c9f4fSDavid Howells  * If successful, we return the amount of description available, irrespective
582973c9f4fSDavid Howells  * of how much we may have copied into the buffer.
5831da177e4SLinus Torvalds  */
5841da177e4SLinus Torvalds long keyctl_describe_key(key_serial_t keyid,
5851da177e4SLinus Torvalds 			 char __user *buffer,
5861da177e4SLinus Torvalds 			 size_t buflen)
5871da177e4SLinus Torvalds {
5883e30148cSDavid Howells 	struct key *key, *instkey;
589664cceb0SDavid Howells 	key_ref_t key_ref;
590aa9d4437SDavid Howells 	char *infobuf;
5911da177e4SLinus Torvalds 	long ret;
592aa9d4437SDavid Howells 	int desclen, infolen;
5931da177e4SLinus Torvalds 
594f5895943SDavid Howells 	key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
595664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
5963e30148cSDavid Howells 		/* viewing a key under construction is permitted if we have the
5973e30148cSDavid Howells 		 * authorisation token handy */
598664cceb0SDavid Howells 		if (PTR_ERR(key_ref) == -EACCES) {
5993e30148cSDavid Howells 			instkey = key_get_instantiation_authkey(keyid);
6003e30148cSDavid Howells 			if (!IS_ERR(instkey)) {
6013e30148cSDavid Howells 				key_put(instkey);
6028bbf4976SDavid Howells 				key_ref = lookup_user_key(keyid,
6035593122eSDavid Howells 							  KEY_LOOKUP_PARTIAL,
6045593122eSDavid Howells 							  0);
605664cceb0SDavid Howells 				if (!IS_ERR(key_ref))
6063e30148cSDavid Howells 					goto okay;
6073e30148cSDavid Howells 			}
6083e30148cSDavid Howells 		}
6093e30148cSDavid Howells 
610664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
6111da177e4SLinus Torvalds 		goto error;
6121da177e4SLinus Torvalds 	}
6131da177e4SLinus Torvalds 
6143e30148cSDavid Howells okay:
615664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
616aa9d4437SDavid Howells 	desclen = strlen(key->description);
617664cceb0SDavid Howells 
618aa9d4437SDavid Howells 	/* calculate how much information we're going to return */
619aa9d4437SDavid Howells 	ret = -ENOMEM;
620aa9d4437SDavid Howells 	infobuf = kasprintf(GFP_KERNEL,
621aa9d4437SDavid Howells 			    "%s;%d;%d;%08x;",
62294fd8405SDavid Howells 			    key->type->name,
6239a56c2dbSEric W. Biederman 			    from_kuid_munged(current_user_ns(), key->uid),
6249a56c2dbSEric W. Biederman 			    from_kgid_munged(current_user_ns(), key->gid),
625aa9d4437SDavid Howells 			    key->perm);
626aa9d4437SDavid Howells 	if (!infobuf)
627aa9d4437SDavid Howells 		goto error2;
628aa9d4437SDavid Howells 	infolen = strlen(infobuf);
629aa9d4437SDavid Howells 	ret = infolen + desclen + 1;
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	/* consider returning the data */
632aa9d4437SDavid Howells 	if (buffer && buflen >= ret) {
633aa9d4437SDavid Howells 		if (copy_to_user(buffer, infobuf, infolen) != 0 ||
634aa9d4437SDavid Howells 		    copy_to_user(buffer + infolen, key->description,
635aa9d4437SDavid Howells 				 desclen + 1) != 0)
6361da177e4SLinus Torvalds 			ret = -EFAULT;
6371da177e4SLinus Torvalds 	}
6381da177e4SLinus Torvalds 
639aa9d4437SDavid Howells 	kfree(infobuf);
6401da177e4SLinus Torvalds error2:
641664cceb0SDavid Howells 	key_ref_put(key_ref);
6421da177e4SLinus Torvalds error:
6431da177e4SLinus Torvalds 	return ret;
644a8b17ed0SDavid Howells }
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds /*
647973c9f4fSDavid Howells  * Search the specified keyring and any keyrings it links to for a matching
648973c9f4fSDavid Howells  * key.  Only keyrings that grant the caller Search permission will be searched
649973c9f4fSDavid Howells  * (this includes the starting keyring).  Only keys with Search permission can
650973c9f4fSDavid Howells  * be found.
651973c9f4fSDavid Howells  *
652973c9f4fSDavid Howells  * If successful, the found key will be linked to the destination keyring if
653973c9f4fSDavid Howells  * supplied and the key has Link permission, and the found key ID will be
654973c9f4fSDavid Howells  * returned.
6551da177e4SLinus Torvalds  */
6561da177e4SLinus Torvalds long keyctl_keyring_search(key_serial_t ringid,
6571da177e4SLinus Torvalds 			   const char __user *_type,
6581da177e4SLinus Torvalds 			   const char __user *_description,
6591da177e4SLinus Torvalds 			   key_serial_t destringid)
6601da177e4SLinus Torvalds {
6611da177e4SLinus Torvalds 	struct key_type *ktype;
662664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref, dest_ref;
6631da177e4SLinus Torvalds 	char type[32], *description;
6640cb409d9SDavi Arnaut 	long ret;
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	/* pull the type and description into kernel space */
6670cb409d9SDavi Arnaut 	ret = key_get_type_from_user(type, _type, sizeof(type));
6681da177e4SLinus Torvalds 	if (ret < 0)
6691da177e4SLinus Torvalds 		goto error;
6701da177e4SLinus Torvalds 
671aa9d4437SDavid Howells 	description = strndup_user(_description, KEY_MAX_DESC_SIZE);
6720cb409d9SDavi Arnaut 	if (IS_ERR(description)) {
6730cb409d9SDavi Arnaut 		ret = PTR_ERR(description);
6741da177e4SLinus Torvalds 		goto error;
6750cb409d9SDavi Arnaut 	}
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 	/* get the keyring at which to begin the search */
678f5895943SDavid Howells 	keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH);
679664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
680664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
6811da177e4SLinus Torvalds 		goto error2;
6821da177e4SLinus Torvalds 	}
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 	/* get the destination keyring if specified */
685664cceb0SDavid Howells 	dest_ref = NULL;
6861da177e4SLinus Torvalds 	if (destringid) {
6875593122eSDavid Howells 		dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
688f5895943SDavid Howells 					   KEY_NEED_WRITE);
689664cceb0SDavid Howells 		if (IS_ERR(dest_ref)) {
690664cceb0SDavid Howells 			ret = PTR_ERR(dest_ref);
6911da177e4SLinus Torvalds 			goto error3;
6921da177e4SLinus Torvalds 		}
6931da177e4SLinus Torvalds 	}
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	/* find the key type */
6961da177e4SLinus Torvalds 	ktype = key_type_lookup(type);
6971da177e4SLinus Torvalds 	if (IS_ERR(ktype)) {
6981da177e4SLinus Torvalds 		ret = PTR_ERR(ktype);
6991da177e4SLinus Torvalds 		goto error4;
7001da177e4SLinus Torvalds 	}
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 	/* do the search */
703664cceb0SDavid Howells 	key_ref = keyring_search(keyring_ref, ktype, description);
704664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
705664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 		/* treat lack or presence of a negative key the same */
7081da177e4SLinus Torvalds 		if (ret == -EAGAIN)
7091da177e4SLinus Torvalds 			ret = -ENOKEY;
7101da177e4SLinus Torvalds 		goto error5;
7111da177e4SLinus Torvalds 	}
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	/* link the resulting key to the destination keyring if we can */
714664cceb0SDavid Howells 	if (dest_ref) {
715f5895943SDavid Howells 		ret = key_permission(key_ref, KEY_NEED_LINK);
71629db9190SDavid Howells 		if (ret < 0)
7171da177e4SLinus Torvalds 			goto error6;
7181da177e4SLinus Torvalds 
719664cceb0SDavid Howells 		ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
7201da177e4SLinus Torvalds 		if (ret < 0)
7211da177e4SLinus Torvalds 			goto error6;
7221da177e4SLinus Torvalds 	}
7231da177e4SLinus Torvalds 
724664cceb0SDavid Howells 	ret = key_ref_to_ptr(key_ref)->serial;
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds error6:
727664cceb0SDavid Howells 	key_ref_put(key_ref);
7281da177e4SLinus Torvalds error5:
7291da177e4SLinus Torvalds 	key_type_put(ktype);
7301da177e4SLinus Torvalds error4:
731664cceb0SDavid Howells 	key_ref_put(dest_ref);
7321da177e4SLinus Torvalds error3:
733664cceb0SDavid Howells 	key_ref_put(keyring_ref);
7341da177e4SLinus Torvalds error2:
7351da177e4SLinus Torvalds 	kfree(description);
7361da177e4SLinus Torvalds error:
7371da177e4SLinus Torvalds 	return ret;
738a8b17ed0SDavid Howells }
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds /*
741973c9f4fSDavid Howells  * Read a key's payload.
742973c9f4fSDavid Howells  *
743973c9f4fSDavid Howells  * The key must either grant the caller Read permission, or it must grant the
744973c9f4fSDavid Howells  * caller Search permission when searched for from the process keyrings.
745973c9f4fSDavid Howells  *
746973c9f4fSDavid Howells  * If successful, we place up to buflen bytes of data into the buffer, if one
747973c9f4fSDavid Howells  * is provided, and return the amount of data that is available in the key,
748973c9f4fSDavid Howells  * irrespective of how much we copied into the buffer.
7491da177e4SLinus Torvalds  */
7501da177e4SLinus Torvalds long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
7511da177e4SLinus Torvalds {
752664cceb0SDavid Howells 	struct key *key;
753664cceb0SDavid Howells 	key_ref_t key_ref;
7541da177e4SLinus Torvalds 	long ret;
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 	/* find the key first */
7575593122eSDavid Howells 	key_ref = lookup_user_key(keyid, 0, 0);
758664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
759664cceb0SDavid Howells 		ret = -ENOKEY;
760664cceb0SDavid Howells 		goto error;
761664cceb0SDavid Howells 	}
762664cceb0SDavid Howells 
763664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
764664cceb0SDavid Howells 
7651da177e4SLinus Torvalds 	/* see if we can read it directly */
766f5895943SDavid Howells 	ret = key_permission(key_ref, KEY_NEED_READ);
76729db9190SDavid Howells 	if (ret == 0)
7681da177e4SLinus Torvalds 		goto can_read_key;
76929db9190SDavid Howells 	if (ret != -EACCES)
77029db9190SDavid Howells 		goto error;
7711da177e4SLinus Torvalds 
772664cceb0SDavid Howells 	/* we can't; see if it's searchable from this process's keyrings
7733e30148cSDavid Howells 	 * - we automatically take account of the fact that it may be
7743e30148cSDavid Howells 	 *   dangling off an instantiation key
7753e30148cSDavid Howells 	 */
776664cceb0SDavid Howells 	if (!is_key_possessed(key_ref)) {
7771260f801SDavid Howells 		ret = -EACCES;
7781da177e4SLinus Torvalds 		goto error2;
7791da177e4SLinus Torvalds 	}
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	/* the key is probably readable - now try to read it */
7821da177e4SLinus Torvalds can_read_key:
7831da177e4SLinus Torvalds 	ret = -EOPNOTSUPP;
7841da177e4SLinus Torvalds 	if (key->type->read) {
785b4a1b4f5SDavid Howells 		/* Read the data with the semaphore held (since we might sleep)
786b4a1b4f5SDavid Howells 		 * to protect against the key being updated or revoked.
787b4a1b4f5SDavid Howells 		 */
7881da177e4SLinus Torvalds 		down_read(&key->sem);
789b4a1b4f5SDavid Howells 		ret = key_validate(key);
790b4a1b4f5SDavid Howells 		if (ret == 0)
7911da177e4SLinus Torvalds 			ret = key->type->read(key, buffer, buflen);
7921da177e4SLinus Torvalds 		up_read(&key->sem);
7931da177e4SLinus Torvalds 	}
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds error2:
7961da177e4SLinus Torvalds 	key_put(key);
7971da177e4SLinus Torvalds error:
7981da177e4SLinus Torvalds 	return ret;
799a8b17ed0SDavid Howells }
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds /*
802973c9f4fSDavid Howells  * Change the ownership of a key
803973c9f4fSDavid Howells  *
804973c9f4fSDavid Howells  * The key must grant the caller Setattr permission for this to work, though
805973c9f4fSDavid Howells  * the key need not be fully instantiated yet.  For the UID to be changed, or
806973c9f4fSDavid Howells  * for the GID to be changed to a group the caller is not a member of, the
807973c9f4fSDavid Howells  * caller must have sysadmin capability.  If either uid or gid is -1 then that
808973c9f4fSDavid Howells  * attribute is not changed.
809973c9f4fSDavid Howells  *
810973c9f4fSDavid Howells  * If the UID is to be changed, the new user must have sufficient quota to
811973c9f4fSDavid Howells  * accept the key.  The quota deduction will be removed from the old user to
812973c9f4fSDavid Howells  * the new user should the attribute be changed.
813973c9f4fSDavid Howells  *
814973c9f4fSDavid Howells  * If successful, 0 will be returned.
8151da177e4SLinus Torvalds  */
8169a56c2dbSEric W. Biederman long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
8171da177e4SLinus Torvalds {
8185801649dSFredrik Tolf 	struct key_user *newowner, *zapowner = NULL;
8191da177e4SLinus Torvalds 	struct key *key;
820664cceb0SDavid Howells 	key_ref_t key_ref;
8211da177e4SLinus Torvalds 	long ret;
8229a56c2dbSEric W. Biederman 	kuid_t uid;
8239a56c2dbSEric W. Biederman 	kgid_t gid;
8249a56c2dbSEric W. Biederman 
8259a56c2dbSEric W. Biederman 	uid = make_kuid(current_user_ns(), user);
8269a56c2dbSEric W. Biederman 	gid = make_kgid(current_user_ns(), group);
8279a56c2dbSEric W. Biederman 	ret = -EINVAL;
8289a56c2dbSEric W. Biederman 	if ((user != (uid_t) -1) && !uid_valid(uid))
8299a56c2dbSEric W. Biederman 		goto error;
8309a56c2dbSEric W. Biederman 	if ((group != (gid_t) -1) && !gid_valid(gid))
8319a56c2dbSEric W. Biederman 		goto error;
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	ret = 0;
8349a56c2dbSEric W. Biederman 	if (user == (uid_t) -1 && group == (gid_t) -1)
8351da177e4SLinus Torvalds 		goto error;
8361da177e4SLinus Torvalds 
8375593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
838f5895943SDavid Howells 				  KEY_NEED_SETATTR);
839664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
840664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
8411da177e4SLinus Torvalds 		goto error;
8421da177e4SLinus Torvalds 	}
8431da177e4SLinus Torvalds 
844664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
845664cceb0SDavid Howells 
8461da177e4SLinus Torvalds 	/* make the changes with the locks held to prevent chown/chown races */
8471da177e4SLinus Torvalds 	ret = -EACCES;
8481da177e4SLinus Torvalds 	down_write(&key->sem);
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN)) {
8511da177e4SLinus Torvalds 		/* only the sysadmin can chown a key to some other UID */
8529a56c2dbSEric W. Biederman 		if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
8535801649dSFredrik Tolf 			goto error_put;
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 		/* only the sysadmin can set the key's GID to a group other
8561da177e4SLinus Torvalds 		 * than one of those that the current process subscribes to */
8579a56c2dbSEric W. Biederman 		if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
8585801649dSFredrik Tolf 			goto error_put;
8591da177e4SLinus Torvalds 	}
8601da177e4SLinus Torvalds 
8615801649dSFredrik Tolf 	/* change the UID */
8629a56c2dbSEric W. Biederman 	if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) {
8635801649dSFredrik Tolf 		ret = -ENOMEM;
8649a56c2dbSEric W. Biederman 		newowner = key_user_lookup(uid);
8655801649dSFredrik Tolf 		if (!newowner)
8665801649dSFredrik Tolf 			goto error_put;
8675801649dSFredrik Tolf 
8685801649dSFredrik Tolf 		/* transfer the quota burden to the new user */
8695801649dSFredrik Tolf 		if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
8709a56c2dbSEric W. Biederman 			unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
8710b77f5bfSDavid Howells 				key_quota_root_maxkeys : key_quota_maxkeys;
8729a56c2dbSEric W. Biederman 			unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
8730b77f5bfSDavid Howells 				key_quota_root_maxbytes : key_quota_maxbytes;
8740b77f5bfSDavid Howells 
8755801649dSFredrik Tolf 			spin_lock(&newowner->lock);
8760b77f5bfSDavid Howells 			if (newowner->qnkeys + 1 >= maxkeys ||
8770b77f5bfSDavid Howells 			    newowner->qnbytes + key->quotalen >= maxbytes ||
8780b77f5bfSDavid Howells 			    newowner->qnbytes + key->quotalen <
8790b77f5bfSDavid Howells 			    newowner->qnbytes)
8805801649dSFredrik Tolf 				goto quota_overrun;
8815801649dSFredrik Tolf 
8825801649dSFredrik Tolf 			newowner->qnkeys++;
8835801649dSFredrik Tolf 			newowner->qnbytes += key->quotalen;
8845801649dSFredrik Tolf 			spin_unlock(&newowner->lock);
8855801649dSFredrik Tolf 
8865801649dSFredrik Tolf 			spin_lock(&key->user->lock);
8875801649dSFredrik Tolf 			key->user->qnkeys--;
8885801649dSFredrik Tolf 			key->user->qnbytes -= key->quotalen;
8895801649dSFredrik Tolf 			spin_unlock(&key->user->lock);
8905801649dSFredrik Tolf 		}
8915801649dSFredrik Tolf 
8925801649dSFredrik Tolf 		atomic_dec(&key->user->nkeys);
8935801649dSFredrik Tolf 		atomic_inc(&newowner->nkeys);
8945801649dSFredrik Tolf 
8955801649dSFredrik Tolf 		if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
8965801649dSFredrik Tolf 			atomic_dec(&key->user->nikeys);
8975801649dSFredrik Tolf 			atomic_inc(&newowner->nikeys);
8985801649dSFredrik Tolf 		}
8995801649dSFredrik Tolf 
9005801649dSFredrik Tolf 		zapowner = key->user;
9015801649dSFredrik Tolf 		key->user = newowner;
9025801649dSFredrik Tolf 		key->uid = uid;
9031da177e4SLinus Torvalds 	}
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	/* change the GID */
9069a56c2dbSEric W. Biederman 	if (group != (gid_t) -1)
9071da177e4SLinus Torvalds 		key->gid = gid;
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 	ret = 0;
9101da177e4SLinus Torvalds 
9115801649dSFredrik Tolf error_put:
9121da177e4SLinus Torvalds 	up_write(&key->sem);
9131da177e4SLinus Torvalds 	key_put(key);
9145801649dSFredrik Tolf 	if (zapowner)
9155801649dSFredrik Tolf 		key_user_put(zapowner);
9161da177e4SLinus Torvalds error:
9171da177e4SLinus Torvalds 	return ret;
9181da177e4SLinus Torvalds 
9195801649dSFredrik Tolf quota_overrun:
9205801649dSFredrik Tolf 	spin_unlock(&newowner->lock);
9215801649dSFredrik Tolf 	zapowner = newowner;
9225801649dSFredrik Tolf 	ret = -EDQUOT;
9235801649dSFredrik Tolf 	goto error_put;
924a8b17ed0SDavid Howells }
9255801649dSFredrik Tolf 
9261da177e4SLinus Torvalds /*
927973c9f4fSDavid Howells  * Change the permission mask on a key.
928973c9f4fSDavid Howells  *
929973c9f4fSDavid Howells  * The key must grant the caller Setattr permission for this to work, though
930973c9f4fSDavid Howells  * the key need not be fully instantiated yet.  If the caller does not have
931973c9f4fSDavid Howells  * sysadmin capability, it may only change the permission on keys that it owns.
9321da177e4SLinus Torvalds  */
9331da177e4SLinus Torvalds long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
9341da177e4SLinus Torvalds {
9351da177e4SLinus Torvalds 	struct key *key;
936664cceb0SDavid Howells 	key_ref_t key_ref;
9371da177e4SLinus Torvalds 	long ret;
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	ret = -EINVAL;
940664cceb0SDavid Howells 	if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
9411da177e4SLinus Torvalds 		goto error;
9421da177e4SLinus Torvalds 
9435593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
944f5895943SDavid Howells 				  KEY_NEED_SETATTR);
945664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
946664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
9471da177e4SLinus Torvalds 		goto error;
9481da177e4SLinus Torvalds 	}
9491da177e4SLinus Torvalds 
950664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
951664cceb0SDavid Howells 
95276d8aeabSDavid Howells 	/* make the changes with the locks held to prevent chown/chmod races */
9531da177e4SLinus Torvalds 	ret = -EACCES;
9541da177e4SLinus Torvalds 	down_write(&key->sem);
9551da177e4SLinus Torvalds 
95676d8aeabSDavid Howells 	/* if we're not the sysadmin, we can only change a key that we own */
9579a56c2dbSEric W. Biederman 	if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
9581da177e4SLinus Torvalds 		key->perm = perm;
9591da177e4SLinus Torvalds 		ret = 0;
96076d8aeabSDavid Howells 	}
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 	up_write(&key->sem);
9631da177e4SLinus Torvalds 	key_put(key);
9641da177e4SLinus Torvalds error:
9651da177e4SLinus Torvalds 	return ret;
966a8b17ed0SDavid Howells }
9671da177e4SLinus Torvalds 
9688bbf4976SDavid Howells /*
969973c9f4fSDavid Howells  * Get the destination keyring for instantiation and check that the caller has
970973c9f4fSDavid Howells  * Write permission on it.
9718bbf4976SDavid Howells  */
9728bbf4976SDavid Howells static long get_instantiation_keyring(key_serial_t ringid,
9738bbf4976SDavid Howells 				      struct request_key_auth *rka,
9748bbf4976SDavid Howells 				      struct key **_dest_keyring)
9758bbf4976SDavid Howells {
9768bbf4976SDavid Howells 	key_ref_t dkref;
9778bbf4976SDavid Howells 
9788bbf4976SDavid Howells 	*_dest_keyring = NULL;
979eca1bf5bSDavid Howells 
980eca1bf5bSDavid Howells 	/* just return a NULL pointer if we weren't asked to make a link */
981eca1bf5bSDavid Howells 	if (ringid == 0)
9828bbf4976SDavid Howells 		return 0;
9838bbf4976SDavid Howells 
9848bbf4976SDavid Howells 	/* if a specific keyring is nominated by ID, then use that */
9858bbf4976SDavid Howells 	if (ringid > 0) {
986f5895943SDavid Howells 		dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
9878bbf4976SDavid Howells 		if (IS_ERR(dkref))
9888bbf4976SDavid Howells 			return PTR_ERR(dkref);
9898bbf4976SDavid Howells 		*_dest_keyring = key_ref_to_ptr(dkref);
9908bbf4976SDavid Howells 		return 0;
9918bbf4976SDavid Howells 	}
9928bbf4976SDavid Howells 
9938bbf4976SDavid Howells 	if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
9948bbf4976SDavid Howells 		return -EINVAL;
9958bbf4976SDavid Howells 
9968bbf4976SDavid Howells 	/* otherwise specify the destination keyring recorded in the
9978bbf4976SDavid Howells 	 * authorisation key (any KEY_SPEC_*_KEYRING) */
9988bbf4976SDavid Howells 	if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
99921279cfaSDavid Howells 		*_dest_keyring = key_get(rka->dest_keyring);
10008bbf4976SDavid Howells 		return 0;
10018bbf4976SDavid Howells 	}
10028bbf4976SDavid Howells 
10038bbf4976SDavid Howells 	return -ENOKEY;
10048bbf4976SDavid Howells }
10058bbf4976SDavid Howells 
1006d84f4f99SDavid Howells /*
1007973c9f4fSDavid Howells  * Change the request_key authorisation key on the current process.
1008d84f4f99SDavid Howells  */
1009d84f4f99SDavid Howells static int keyctl_change_reqkey_auth(struct key *key)
1010d84f4f99SDavid Howells {
1011d84f4f99SDavid Howells 	struct cred *new;
1012d84f4f99SDavid Howells 
1013d84f4f99SDavid Howells 	new = prepare_creds();
1014d84f4f99SDavid Howells 	if (!new)
1015d84f4f99SDavid Howells 		return -ENOMEM;
1016d84f4f99SDavid Howells 
1017d84f4f99SDavid Howells 	key_put(new->request_key_auth);
1018d84f4f99SDavid Howells 	new->request_key_auth = key_get(key);
1019d84f4f99SDavid Howells 
1020d84f4f99SDavid Howells 	return commit_creds(new);
1021d84f4f99SDavid Howells }
1022d84f4f99SDavid Howells 
10231da177e4SLinus Torvalds /*
1024973c9f4fSDavid Howells  * Instantiate a key with the specified payload and link the key into the
1025973c9f4fSDavid Howells  * destination keyring if one is given.
1026973c9f4fSDavid Howells  *
1027973c9f4fSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1028973c9f4fSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1029973c9f4fSDavid Howells  *
1030973c9f4fSDavid Howells  * If successful, 0 will be returned.
10311da177e4SLinus Torvalds  */
1032ee009e4aSDavid Howells long keyctl_instantiate_key_common(key_serial_t id,
1033b353a1f7SAl Viro 				   struct iov_iter *from,
10341da177e4SLinus Torvalds 				   key_serial_t ringid)
10351da177e4SLinus Torvalds {
1036d84f4f99SDavid Howells 	const struct cred *cred = current_cred();
10373e30148cSDavid Howells 	struct request_key_auth *rka;
10388bbf4976SDavid Howells 	struct key *instkey, *dest_keyring;
1039b353a1f7SAl Viro 	size_t plen = from ? iov_iter_count(from) : 0;
10401da177e4SLinus Torvalds 	void *payload;
10411da177e4SLinus Torvalds 	long ret;
10421da177e4SLinus Torvalds 
1043d84f4f99SDavid Howells 	kenter("%d,,%zu,%d", id, plen, ringid);
1044d84f4f99SDavid Howells 
1045b353a1f7SAl Viro 	if (!plen)
1046b353a1f7SAl Viro 		from = NULL;
1047b353a1f7SAl Viro 
10481da177e4SLinus Torvalds 	ret = -EINVAL;
104938bbca6bSDavid Howells 	if (plen > 1024 * 1024 - 1)
10501da177e4SLinus Torvalds 		goto error;
10511da177e4SLinus Torvalds 
1052b5f545c8SDavid Howells 	/* the appropriate instantiation authorisation key must have been
1053b5f545c8SDavid Howells 	 * assumed before calling this */
1054b5f545c8SDavid Howells 	ret = -EPERM;
1055d84f4f99SDavid Howells 	instkey = cred->request_key_auth;
1056b5f545c8SDavid Howells 	if (!instkey)
1057b5f545c8SDavid Howells 		goto error;
1058b5f545c8SDavid Howells 
1059146aa8b1SDavid Howells 	rka = instkey->payload.data[0];
1060b5f545c8SDavid Howells 	if (rka->target_key->serial != id)
1061b5f545c8SDavid Howells 		goto error;
1062b5f545c8SDavid Howells 
10631da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
10641da177e4SLinus Torvalds 	payload = NULL;
10651da177e4SLinus Torvalds 
1066b353a1f7SAl Viro 	if (from) {
10671da177e4SLinus Torvalds 		ret = -ENOMEM;
10681da177e4SLinus Torvalds 		payload = kmalloc(plen, GFP_KERNEL);
106938bbca6bSDavid Howells 		if (!payload) {
107038bbca6bSDavid Howells 			if (plen <= PAGE_SIZE)
107138bbca6bSDavid Howells 				goto error;
107238bbca6bSDavid Howells 			payload = vmalloc(plen);
10731da177e4SLinus Torvalds 			if (!payload)
10741da177e4SLinus Torvalds 				goto error;
107538bbca6bSDavid Howells 		}
10761da177e4SLinus Torvalds 
1077b353a1f7SAl Viro 		ret = -EFAULT;
1078cbbd26b8SAl Viro 		if (!copy_from_iter_full(payload, plen, from))
10791da177e4SLinus Torvalds 			goto error2;
10801da177e4SLinus Torvalds 	}
10811da177e4SLinus Torvalds 
10823e30148cSDavid Howells 	/* find the destination keyring amongst those belonging to the
10833e30148cSDavid Howells 	 * requesting task */
10848bbf4976SDavid Howells 	ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
10858bbf4976SDavid Howells 	if (ret < 0)
1086b5f545c8SDavid Howells 		goto error2;
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds 	/* instantiate the key and link it into a keyring */
10893e30148cSDavid Howells 	ret = key_instantiate_and_link(rka->target_key, payload, plen,
10908bbf4976SDavid Howells 				       dest_keyring, instkey);
10911da177e4SLinus Torvalds 
10928bbf4976SDavid Howells 	key_put(dest_keyring);
1093b5f545c8SDavid Howells 
1094b5f545c8SDavid Howells 	/* discard the assumed authority if it's just been disabled by
1095b5f545c8SDavid Howells 	 * instantiation of the key */
1096d84f4f99SDavid Howells 	if (ret == 0)
1097d84f4f99SDavid Howells 		keyctl_change_reqkey_auth(NULL);
1098b5f545c8SDavid Howells 
10991da177e4SLinus Torvalds error2:
1100b353a1f7SAl Viro 	kvfree(payload);
11011da177e4SLinus Torvalds error:
11021da177e4SLinus Torvalds 	return ret;
1103a8b17ed0SDavid Howells }
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds /*
1106ee009e4aSDavid Howells  * Instantiate a key with the specified payload and link the key into the
1107ee009e4aSDavid Howells  * destination keyring if one is given.
1108ee009e4aSDavid Howells  *
1109ee009e4aSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1110ee009e4aSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1111ee009e4aSDavid Howells  *
1112ee009e4aSDavid Howells  * If successful, 0 will be returned.
1113ee009e4aSDavid Howells  */
1114ee009e4aSDavid Howells long keyctl_instantiate_key(key_serial_t id,
1115ee009e4aSDavid Howells 			    const void __user *_payload,
1116ee009e4aSDavid Howells 			    size_t plen,
1117ee009e4aSDavid Howells 			    key_serial_t ringid)
1118ee009e4aSDavid Howells {
1119ee009e4aSDavid Howells 	if (_payload && plen) {
1120b353a1f7SAl Viro 		struct iovec iov;
1121b353a1f7SAl Viro 		struct iov_iter from;
1122b353a1f7SAl Viro 		int ret;
1123ee009e4aSDavid Howells 
1124b353a1f7SAl Viro 		ret = import_single_range(WRITE, (void __user *)_payload, plen,
1125b353a1f7SAl Viro 					  &iov, &from);
1126b353a1f7SAl Viro 		if (unlikely(ret))
1127b353a1f7SAl Viro 			return ret;
1128b353a1f7SAl Viro 
1129b353a1f7SAl Viro 		return keyctl_instantiate_key_common(id, &from, ringid);
1130ee009e4aSDavid Howells 	}
1131ee009e4aSDavid Howells 
1132b353a1f7SAl Viro 	return keyctl_instantiate_key_common(id, NULL, ringid);
1133ee009e4aSDavid Howells }
1134ee009e4aSDavid Howells 
1135ee009e4aSDavid Howells /*
1136ee009e4aSDavid Howells  * Instantiate a key with the specified multipart payload and link the key into
1137ee009e4aSDavid Howells  * the destination keyring if one is given.
1138ee009e4aSDavid Howells  *
1139ee009e4aSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1140ee009e4aSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1141ee009e4aSDavid Howells  *
1142ee009e4aSDavid Howells  * If successful, 0 will be returned.
1143ee009e4aSDavid Howells  */
1144ee009e4aSDavid Howells long keyctl_instantiate_key_iov(key_serial_t id,
1145ee009e4aSDavid Howells 				const struct iovec __user *_payload_iov,
1146ee009e4aSDavid Howells 				unsigned ioc,
1147ee009e4aSDavid Howells 				key_serial_t ringid)
1148ee009e4aSDavid Howells {
1149ee009e4aSDavid Howells 	struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1150b353a1f7SAl Viro 	struct iov_iter from;
1151ee009e4aSDavid Howells 	long ret;
1152ee009e4aSDavid Howells 
1153b353a1f7SAl Viro 	if (!_payload_iov)
1154b353a1f7SAl Viro 		ioc = 0;
1155ee009e4aSDavid Howells 
1156b353a1f7SAl Viro 	ret = import_iovec(WRITE, _payload_iov, ioc,
1157b353a1f7SAl Viro 				    ARRAY_SIZE(iovstack), &iov, &from);
1158ee009e4aSDavid Howells 	if (ret < 0)
1159b353a1f7SAl Viro 		return ret;
1160b353a1f7SAl Viro 	ret = keyctl_instantiate_key_common(id, &from, ringid);
1161ee009e4aSDavid Howells 	kfree(iov);
1162ee009e4aSDavid Howells 	return ret;
1163ee009e4aSDavid Howells }
1164ee009e4aSDavid Howells 
1165ee009e4aSDavid Howells /*
1166973c9f4fSDavid Howells  * Negatively instantiate the key with the given timeout (in seconds) and link
1167973c9f4fSDavid Howells  * the key into the destination keyring if one is given.
1168973c9f4fSDavid Howells  *
1169973c9f4fSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1170973c9f4fSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1171973c9f4fSDavid Howells  *
1172973c9f4fSDavid Howells  * The key and any links to the key will be automatically garbage collected
1173973c9f4fSDavid Howells  * after the timeout expires.
1174973c9f4fSDavid Howells  *
1175973c9f4fSDavid Howells  * Negative keys are used to rate limit repeated request_key() calls by causing
1176973c9f4fSDavid Howells  * them to return -ENOKEY until the negative key expires.
1177973c9f4fSDavid Howells  *
1178973c9f4fSDavid Howells  * If successful, 0 will be returned.
11791da177e4SLinus Torvalds  */
11801da177e4SLinus Torvalds long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
11811da177e4SLinus Torvalds {
1182fdd1b945SDavid Howells 	return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1183fdd1b945SDavid Howells }
1184fdd1b945SDavid Howells 
1185fdd1b945SDavid Howells /*
1186fdd1b945SDavid Howells  * Negatively instantiate the key with the given timeout (in seconds) and error
1187fdd1b945SDavid Howells  * code and link the key into the destination keyring if one is given.
1188fdd1b945SDavid Howells  *
1189fdd1b945SDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1190fdd1b945SDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1191fdd1b945SDavid Howells  *
1192fdd1b945SDavid Howells  * The key and any links to the key will be automatically garbage collected
1193fdd1b945SDavid Howells  * after the timeout expires.
1194fdd1b945SDavid Howells  *
1195fdd1b945SDavid Howells  * Negative keys are used to rate limit repeated request_key() calls by causing
1196fdd1b945SDavid Howells  * them to return the specified error code until the negative key expires.
1197fdd1b945SDavid Howells  *
1198fdd1b945SDavid Howells  * If successful, 0 will be returned.
1199fdd1b945SDavid Howells  */
1200fdd1b945SDavid Howells long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1201fdd1b945SDavid Howells 		       key_serial_t ringid)
1202fdd1b945SDavid Howells {
1203d84f4f99SDavid Howells 	const struct cred *cred = current_cred();
12043e30148cSDavid Howells 	struct request_key_auth *rka;
12058bbf4976SDavid Howells 	struct key *instkey, *dest_keyring;
12061da177e4SLinus Torvalds 	long ret;
12071da177e4SLinus Torvalds 
1208fdd1b945SDavid Howells 	kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1209fdd1b945SDavid Howells 
1210fdd1b945SDavid Howells 	/* must be a valid error code and mustn't be a kernel special */
1211fdd1b945SDavid Howells 	if (error <= 0 ||
1212fdd1b945SDavid Howells 	    error >= MAX_ERRNO ||
1213fdd1b945SDavid Howells 	    error == ERESTARTSYS ||
1214fdd1b945SDavid Howells 	    error == ERESTARTNOINTR ||
1215fdd1b945SDavid Howells 	    error == ERESTARTNOHAND ||
1216fdd1b945SDavid Howells 	    error == ERESTART_RESTARTBLOCK)
1217fdd1b945SDavid Howells 		return -EINVAL;
1218d84f4f99SDavid Howells 
1219b5f545c8SDavid Howells 	/* the appropriate instantiation authorisation key must have been
1220b5f545c8SDavid Howells 	 * assumed before calling this */
1221b5f545c8SDavid Howells 	ret = -EPERM;
1222d84f4f99SDavid Howells 	instkey = cred->request_key_auth;
1223b5f545c8SDavid Howells 	if (!instkey)
12241da177e4SLinus Torvalds 		goto error;
12251da177e4SLinus Torvalds 
1226146aa8b1SDavid Howells 	rka = instkey->payload.data[0];
1227b5f545c8SDavid Howells 	if (rka->target_key->serial != id)
1228b5f545c8SDavid Howells 		goto error;
12293e30148cSDavid Howells 
12301da177e4SLinus Torvalds 	/* find the destination keyring if present (which must also be
12311da177e4SLinus Torvalds 	 * writable) */
12328bbf4976SDavid Howells 	ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
12338bbf4976SDavid Howells 	if (ret < 0)
1234b5f545c8SDavid Howells 		goto error;
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 	/* instantiate the key and link it into a keyring */
1237fdd1b945SDavid Howells 	ret = key_reject_and_link(rka->target_key, timeout, error,
12388bbf4976SDavid Howells 				  dest_keyring, instkey);
12391da177e4SLinus Torvalds 
12408bbf4976SDavid Howells 	key_put(dest_keyring);
1241b5f545c8SDavid Howells 
1242b5f545c8SDavid Howells 	/* discard the assumed authority if it's just been disabled by
1243b5f545c8SDavid Howells 	 * instantiation of the key */
1244d84f4f99SDavid Howells 	if (ret == 0)
1245d84f4f99SDavid Howells 		keyctl_change_reqkey_auth(NULL);
1246b5f545c8SDavid Howells 
12471da177e4SLinus Torvalds error:
12481da177e4SLinus Torvalds 	return ret;
1249a8b17ed0SDavid Howells }
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds /*
1252973c9f4fSDavid Howells  * Read or set the default keyring in which request_key() will cache keys and
1253973c9f4fSDavid Howells  * return the old setting.
1254973c9f4fSDavid Howells  *
1255973c9f4fSDavid Howells  * If a process keyring is specified then this will be created if it doesn't
1256973c9f4fSDavid Howells  * yet exist.  The old setting will be returned if successful.
12573e30148cSDavid Howells  */
12583e30148cSDavid Howells long keyctl_set_reqkey_keyring(int reqkey_defl)
12593e30148cSDavid Howells {
1260d84f4f99SDavid Howells 	struct cred *new;
1261d84f4f99SDavid Howells 	int ret, old_setting;
1262d84f4f99SDavid Howells 
1263d84f4f99SDavid Howells 	old_setting = current_cred_xxx(jit_keyring);
1264d84f4f99SDavid Howells 
1265d84f4f99SDavid Howells 	if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1266d84f4f99SDavid Howells 		return old_setting;
1267d84f4f99SDavid Howells 
1268d84f4f99SDavid Howells 	new = prepare_creds();
1269d84f4f99SDavid Howells 	if (!new)
1270d84f4f99SDavid Howells 		return -ENOMEM;
12713e30148cSDavid Howells 
12723e30148cSDavid Howells 	switch (reqkey_defl) {
12733e30148cSDavid Howells 	case KEY_REQKEY_DEFL_THREAD_KEYRING:
1274d84f4f99SDavid Howells 		ret = install_thread_keyring_to_cred(new);
12753e30148cSDavid Howells 		if (ret < 0)
1276d84f4f99SDavid Howells 			goto error;
12773e30148cSDavid Howells 		goto set;
12783e30148cSDavid Howells 
12793e30148cSDavid Howells 	case KEY_REQKEY_DEFL_PROCESS_KEYRING:
1280d84f4f99SDavid Howells 		ret = install_process_keyring_to_cred(new);
1281d84f4f99SDavid Howells 		if (ret < 0) {
1282d84f4f99SDavid Howells 			if (ret != -EEXIST)
1283d84f4f99SDavid Howells 				goto error;
1284d84f4f99SDavid Howells 			ret = 0;
1285d84f4f99SDavid Howells 		}
1286d84f4f99SDavid Howells 		goto set;
12873e30148cSDavid Howells 
12883e30148cSDavid Howells 	case KEY_REQKEY_DEFL_DEFAULT:
12893e30148cSDavid Howells 	case KEY_REQKEY_DEFL_SESSION_KEYRING:
12903e30148cSDavid Howells 	case KEY_REQKEY_DEFL_USER_KEYRING:
12913e30148cSDavid Howells 	case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1292d84f4f99SDavid Howells 	case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1293d84f4f99SDavid Howells 		goto set;
12943e30148cSDavid Howells 
12953e30148cSDavid Howells 	case KEY_REQKEY_DEFL_NO_CHANGE:
12963e30148cSDavid Howells 	case KEY_REQKEY_DEFL_GROUP_KEYRING:
12973e30148cSDavid Howells 	default:
1298d84f4f99SDavid Howells 		ret = -EINVAL;
1299d84f4f99SDavid Howells 		goto error;
13003e30148cSDavid Howells 	}
13013e30148cSDavid Howells 
1302d84f4f99SDavid Howells set:
1303d84f4f99SDavid Howells 	new->jit_keyring = reqkey_defl;
1304d84f4f99SDavid Howells 	commit_creds(new);
1305d84f4f99SDavid Howells 	return old_setting;
1306d84f4f99SDavid Howells error:
1307d84f4f99SDavid Howells 	abort_creds(new);
13084303ef19SDan Carpenter 	return ret;
1309a8b17ed0SDavid Howells }
1310d84f4f99SDavid Howells 
13113e30148cSDavid Howells /*
1312973c9f4fSDavid Howells  * Set or clear the timeout on a key.
1313973c9f4fSDavid Howells  *
1314973c9f4fSDavid Howells  * Either the key must grant the caller Setattr permission or else the caller
1315973c9f4fSDavid Howells  * must hold an instantiation authorisation token for the key.
1316973c9f4fSDavid Howells  *
1317973c9f4fSDavid Howells  * The timeout is either 0 to clear the timeout, or a number of seconds from
1318973c9f4fSDavid Howells  * the current time.  The key and any links to the key will be automatically
1319973c9f4fSDavid Howells  * garbage collected after the timeout expires.
1320973c9f4fSDavid Howells  *
1321d3600bcfSMimi Zohar  * Keys with KEY_FLAG_KEEP set should not be timed out.
1322d3600bcfSMimi Zohar  *
1323973c9f4fSDavid Howells  * If successful, 0 is returned.
1324017679c4SDavid Howells  */
1325017679c4SDavid Howells long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1326017679c4SDavid Howells {
13279156235bSDavid Howells 	struct key *key, *instkey;
1328017679c4SDavid Howells 	key_ref_t key_ref;
1329017679c4SDavid Howells 	long ret;
1330017679c4SDavid Howells 
13315593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1332f5895943SDavid Howells 				  KEY_NEED_SETATTR);
1333017679c4SDavid Howells 	if (IS_ERR(key_ref)) {
13349156235bSDavid Howells 		/* setting the timeout on a key under construction is permitted
13359156235bSDavid Howells 		 * if we have the authorisation token handy */
13369156235bSDavid Howells 		if (PTR_ERR(key_ref) == -EACCES) {
13379156235bSDavid Howells 			instkey = key_get_instantiation_authkey(id);
13389156235bSDavid Howells 			if (!IS_ERR(instkey)) {
13399156235bSDavid Howells 				key_put(instkey);
13409156235bSDavid Howells 				key_ref = lookup_user_key(id,
13419156235bSDavid Howells 							  KEY_LOOKUP_PARTIAL,
13429156235bSDavid Howells 							  0);
13439156235bSDavid Howells 				if (!IS_ERR(key_ref))
13449156235bSDavid Howells 					goto okay;
13459156235bSDavid Howells 			}
13469156235bSDavid Howells 		}
13479156235bSDavid Howells 
1348017679c4SDavid Howells 		ret = PTR_ERR(key_ref);
1349017679c4SDavid Howells 		goto error;
1350017679c4SDavid Howells 	}
1351017679c4SDavid Howells 
13529156235bSDavid Howells okay:
1353017679c4SDavid Howells 	key = key_ref_to_ptr(key_ref);
13541d6d167cSMimi Zohar 	ret = 0;
1355d3600bcfSMimi Zohar 	if (test_bit(KEY_FLAG_KEEP, &key->flags))
1356d3600bcfSMimi Zohar 		ret = -EPERM;
13571d6d167cSMimi Zohar 	else
135859e6b9c1SBryan Schumaker 		key_set_timeout(key, timeout);
1359017679c4SDavid Howells 	key_put(key);
1360017679c4SDavid Howells 
1361017679c4SDavid Howells error:
1362017679c4SDavid Howells 	return ret;
1363a8b17ed0SDavid Howells }
1364017679c4SDavid Howells 
1365017679c4SDavid Howells /*
1366973c9f4fSDavid Howells  * Assume (or clear) the authority to instantiate the specified key.
1367973c9f4fSDavid Howells  *
1368973c9f4fSDavid Howells  * This sets the authoritative token currently in force for key instantiation.
1369973c9f4fSDavid Howells  * This must be done for a key to be instantiated.  It has the effect of making
1370973c9f4fSDavid Howells  * available all the keys from the caller of the request_key() that created a
1371973c9f4fSDavid Howells  * key to request_key() calls made by the caller of this function.
1372973c9f4fSDavid Howells  *
1373973c9f4fSDavid Howells  * The caller must have the instantiation key in their process keyrings with a
1374973c9f4fSDavid Howells  * Search permission grant available to the caller.
1375973c9f4fSDavid Howells  *
1376973c9f4fSDavid Howells  * If the ID given is 0, then the setting will be cleared and 0 returned.
1377973c9f4fSDavid Howells  *
1378973c9f4fSDavid Howells  * If the ID given has a matching an authorisation key, then that key will be
1379973c9f4fSDavid Howells  * set and its ID will be returned.  The authorisation key can be read to get
1380973c9f4fSDavid Howells  * the callout information passed to request_key().
1381b5f545c8SDavid Howells  */
1382b5f545c8SDavid Howells long keyctl_assume_authority(key_serial_t id)
1383b5f545c8SDavid Howells {
1384b5f545c8SDavid Howells 	struct key *authkey;
1385b5f545c8SDavid Howells 	long ret;
1386b5f545c8SDavid Howells 
1387b5f545c8SDavid Howells 	/* special key IDs aren't permitted */
1388b5f545c8SDavid Howells 	ret = -EINVAL;
1389b5f545c8SDavid Howells 	if (id < 0)
1390b5f545c8SDavid Howells 		goto error;
1391b5f545c8SDavid Howells 
1392b5f545c8SDavid Howells 	/* we divest ourselves of authority if given an ID of 0 */
1393b5f545c8SDavid Howells 	if (id == 0) {
1394d84f4f99SDavid Howells 		ret = keyctl_change_reqkey_auth(NULL);
1395b5f545c8SDavid Howells 		goto error;
1396b5f545c8SDavid Howells 	}
1397b5f545c8SDavid Howells 
1398b5f545c8SDavid Howells 	/* attempt to assume the authority temporarily granted to us whilst we
1399b5f545c8SDavid Howells 	 * instantiate the specified key
1400b5f545c8SDavid Howells 	 * - the authorisation key must be in the current task's keyrings
1401b5f545c8SDavid Howells 	 *   somewhere
1402b5f545c8SDavid Howells 	 */
1403b5f545c8SDavid Howells 	authkey = key_get_instantiation_authkey(id);
1404b5f545c8SDavid Howells 	if (IS_ERR(authkey)) {
1405b5f545c8SDavid Howells 		ret = PTR_ERR(authkey);
1406b5f545c8SDavid Howells 		goto error;
1407b5f545c8SDavid Howells 	}
1408b5f545c8SDavid Howells 
1409d84f4f99SDavid Howells 	ret = keyctl_change_reqkey_auth(authkey);
1410d84f4f99SDavid Howells 	if (ret < 0)
1411d84f4f99SDavid Howells 		goto error;
1412d84f4f99SDavid Howells 	key_put(authkey);
1413b5f545c8SDavid Howells 
1414d84f4f99SDavid Howells 	ret = authkey->serial;
1415b5f545c8SDavid Howells error:
1416b5f545c8SDavid Howells 	return ret;
1417a8b17ed0SDavid Howells }
1418b5f545c8SDavid Howells 
141970a5bb72SDavid Howells /*
1420973c9f4fSDavid Howells  * Get a key's the LSM security label.
1421973c9f4fSDavid Howells  *
1422973c9f4fSDavid Howells  * The key must grant the caller View permission for this to work.
1423973c9f4fSDavid Howells  *
1424973c9f4fSDavid Howells  * If there's a buffer, then up to buflen bytes of data will be placed into it.
1425973c9f4fSDavid Howells  *
1426973c9f4fSDavid Howells  * If successful, the amount of information available will be returned,
1427973c9f4fSDavid Howells  * irrespective of how much was copied (including the terminal NUL).
142870a5bb72SDavid Howells  */
142970a5bb72SDavid Howells long keyctl_get_security(key_serial_t keyid,
143070a5bb72SDavid Howells 			 char __user *buffer,
143170a5bb72SDavid Howells 			 size_t buflen)
143270a5bb72SDavid Howells {
143370a5bb72SDavid Howells 	struct key *key, *instkey;
143470a5bb72SDavid Howells 	key_ref_t key_ref;
143570a5bb72SDavid Howells 	char *context;
143670a5bb72SDavid Howells 	long ret;
143770a5bb72SDavid Howells 
1438f5895943SDavid Howells 	key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
143970a5bb72SDavid Howells 	if (IS_ERR(key_ref)) {
144070a5bb72SDavid Howells 		if (PTR_ERR(key_ref) != -EACCES)
144170a5bb72SDavid Howells 			return PTR_ERR(key_ref);
144270a5bb72SDavid Howells 
144370a5bb72SDavid Howells 		/* viewing a key under construction is also permitted if we
144470a5bb72SDavid Howells 		 * have the authorisation token handy */
144570a5bb72SDavid Howells 		instkey = key_get_instantiation_authkey(keyid);
144670a5bb72SDavid Howells 		if (IS_ERR(instkey))
1447fa1cc7b5SRoel Kluin 			return PTR_ERR(instkey);
144870a5bb72SDavid Howells 		key_put(instkey);
144970a5bb72SDavid Howells 
14505593122eSDavid Howells 		key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
145170a5bb72SDavid Howells 		if (IS_ERR(key_ref))
145270a5bb72SDavid Howells 			return PTR_ERR(key_ref);
145370a5bb72SDavid Howells 	}
145470a5bb72SDavid Howells 
145570a5bb72SDavid Howells 	key = key_ref_to_ptr(key_ref);
145670a5bb72SDavid Howells 	ret = security_key_getsecurity(key, &context);
145770a5bb72SDavid Howells 	if (ret == 0) {
145870a5bb72SDavid Howells 		/* if no information was returned, give userspace an empty
145970a5bb72SDavid Howells 		 * string */
146070a5bb72SDavid Howells 		ret = 1;
146170a5bb72SDavid Howells 		if (buffer && buflen > 0 &&
146270a5bb72SDavid Howells 		    copy_to_user(buffer, "", 1) != 0)
146370a5bb72SDavid Howells 			ret = -EFAULT;
146470a5bb72SDavid Howells 	} else if (ret > 0) {
146570a5bb72SDavid Howells 		/* return as much data as there's room for */
146670a5bb72SDavid Howells 		if (buffer && buflen > 0) {
146770a5bb72SDavid Howells 			if (buflen > ret)
146870a5bb72SDavid Howells 				buflen = ret;
146970a5bb72SDavid Howells 
147070a5bb72SDavid Howells 			if (copy_to_user(buffer, context, buflen) != 0)
147170a5bb72SDavid Howells 				ret = -EFAULT;
147270a5bb72SDavid Howells 		}
147370a5bb72SDavid Howells 
147470a5bb72SDavid Howells 		kfree(context);
147570a5bb72SDavid Howells 	}
147670a5bb72SDavid Howells 
147770a5bb72SDavid Howells 	key_ref_put(key_ref);
147870a5bb72SDavid Howells 	return ret;
147970a5bb72SDavid Howells }
148070a5bb72SDavid Howells 
1481ee18d64cSDavid Howells /*
1482973c9f4fSDavid Howells  * Attempt to install the calling process's session keyring on the process's
1483973c9f4fSDavid Howells  * parent process.
1484973c9f4fSDavid Howells  *
1485973c9f4fSDavid Howells  * The keyring must exist and must grant the caller LINK permission, and the
1486973c9f4fSDavid Howells  * parent process must be single-threaded and must have the same effective
1487973c9f4fSDavid Howells  * ownership as this process and mustn't be SUID/SGID.
1488973c9f4fSDavid Howells  *
1489973c9f4fSDavid Howells  * The keyring will be emplaced on the parent when it next resumes userspace.
1490973c9f4fSDavid Howells  *
1491973c9f4fSDavid Howells  * If successful, 0 will be returned.
1492ee18d64cSDavid Howells  */
1493ee18d64cSDavid Howells long keyctl_session_to_parent(void)
1494ee18d64cSDavid Howells {
1495ee18d64cSDavid Howells 	struct task_struct *me, *parent;
1496ee18d64cSDavid Howells 	const struct cred *mycred, *pcred;
149767d12145SAl Viro 	struct callback_head *newwork, *oldwork;
1498ee18d64cSDavid Howells 	key_ref_t keyring_r;
1499413cd3d9SOleg Nesterov 	struct cred *cred;
1500ee18d64cSDavid Howells 	int ret;
1501ee18d64cSDavid Howells 
1502f5895943SDavid Howells 	keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK);
1503ee18d64cSDavid Howells 	if (IS_ERR(keyring_r))
1504ee18d64cSDavid Howells 		return PTR_ERR(keyring_r);
1505ee18d64cSDavid Howells 
1506413cd3d9SOleg Nesterov 	ret = -ENOMEM;
1507413cd3d9SOleg Nesterov 
1508ee18d64cSDavid Howells 	/* our parent is going to need a new cred struct, a new tgcred struct
1509ee18d64cSDavid Howells 	 * and new security data, so we allocate them here to prevent ENOMEM in
1510ee18d64cSDavid Howells 	 * our parent */
1511ee18d64cSDavid Howells 	cred = cred_alloc_blank();
1512ee18d64cSDavid Howells 	if (!cred)
151367d12145SAl Viro 		goto error_keyring;
151467d12145SAl Viro 	newwork = &cred->rcu;
1515ee18d64cSDavid Howells 
15163a50597dSDavid Howells 	cred->session_keyring = key_ref_to_ptr(keyring_r);
15173a50597dSDavid Howells 	keyring_r = NULL;
151867d12145SAl Viro 	init_task_work(newwork, key_change_session_keyring);
1519ee18d64cSDavid Howells 
1520ee18d64cSDavid Howells 	me = current;
15219d1ac65aSDavid Howells 	rcu_read_lock();
1522ee18d64cSDavid Howells 	write_lock_irq(&tasklist_lock);
1523ee18d64cSDavid Howells 
1524ee18d64cSDavid Howells 	ret = -EPERM;
1525413cd3d9SOleg Nesterov 	oldwork = NULL;
1526413cd3d9SOleg Nesterov 	parent = me->real_parent;
1527ee18d64cSDavid Howells 
1528ee18d64cSDavid Howells 	/* the parent mustn't be init and mustn't be a kernel thread */
1529ee18d64cSDavid Howells 	if (parent->pid <= 1 || !parent->mm)
1530413cd3d9SOleg Nesterov 		goto unlock;
1531ee18d64cSDavid Howells 
1532ee18d64cSDavid Howells 	/* the parent must be single threaded */
1533dd98acf7SOleg Nesterov 	if (!thread_group_empty(parent))
1534413cd3d9SOleg Nesterov 		goto unlock;
1535ee18d64cSDavid Howells 
1536ee18d64cSDavid Howells 	/* the parent and the child must have different session keyrings or
1537ee18d64cSDavid Howells 	 * there's no point */
1538ee18d64cSDavid Howells 	mycred = current_cred();
1539ee18d64cSDavid Howells 	pcred = __task_cred(parent);
1540ee18d64cSDavid Howells 	if (mycred == pcred ||
15413a50597dSDavid Howells 	    mycred->session_keyring == pcred->session_keyring) {
1542413cd3d9SOleg Nesterov 		ret = 0;
1543413cd3d9SOleg Nesterov 		goto unlock;
1544413cd3d9SOleg Nesterov 	}
1545ee18d64cSDavid Howells 
1546ee18d64cSDavid Howells 	/* the parent must have the same effective ownership and mustn't be
1547ee18d64cSDavid Howells 	 * SUID/SGID */
15489a56c2dbSEric W. Biederman 	if (!uid_eq(pcred->uid,	 mycred->euid) ||
15499a56c2dbSEric W. Biederman 	    !uid_eq(pcred->euid, mycred->euid) ||
15509a56c2dbSEric W. Biederman 	    !uid_eq(pcred->suid, mycred->euid) ||
15519a56c2dbSEric W. Biederman 	    !gid_eq(pcred->gid,	 mycred->egid) ||
15529a56c2dbSEric W. Biederman 	    !gid_eq(pcred->egid, mycred->egid) ||
15539a56c2dbSEric W. Biederman 	    !gid_eq(pcred->sgid, mycred->egid))
1554413cd3d9SOleg Nesterov 		goto unlock;
1555ee18d64cSDavid Howells 
1556ee18d64cSDavid Howells 	/* the keyrings must have the same UID */
15573a50597dSDavid Howells 	if ((pcred->session_keyring &&
15582a74dbb9SLinus Torvalds 	     !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
15592a74dbb9SLinus Torvalds 	    !uid_eq(mycred->session_keyring->uid, mycred->euid))
1560413cd3d9SOleg Nesterov 		goto unlock;
1561ee18d64cSDavid Howells 
1562413cd3d9SOleg Nesterov 	/* cancel an already pending keyring replacement */
1563413cd3d9SOleg Nesterov 	oldwork = task_work_cancel(parent, key_change_session_keyring);
1564ee18d64cSDavid Howells 
1565ee18d64cSDavid Howells 	/* the replacement session keyring is applied just prior to userspace
1566ee18d64cSDavid Howells 	 * restarting */
156767d12145SAl Viro 	ret = task_work_add(parent, newwork, true);
1568413cd3d9SOleg Nesterov 	if (!ret)
1569413cd3d9SOleg Nesterov 		newwork = NULL;
1570413cd3d9SOleg Nesterov unlock:
1571ee18d64cSDavid Howells 	write_unlock_irq(&tasklist_lock);
15729d1ac65aSDavid Howells 	rcu_read_unlock();
157367d12145SAl Viro 	if (oldwork)
157467d12145SAl Viro 		put_cred(container_of(oldwork, struct cred, rcu));
157567d12145SAl Viro 	if (newwork)
157667d12145SAl Viro 		put_cred(cred);
1577ee18d64cSDavid Howells 	return ret;
1578ee18d64cSDavid Howells 
1579ee18d64cSDavid Howells error_keyring:
1580ee18d64cSDavid Howells 	key_ref_put(keyring_r);
1581ee18d64cSDavid Howells 	return ret;
1582ee18d64cSDavid Howells }
1583ee18d64cSDavid Howells 
1584b5f545c8SDavid Howells /*
1585973c9f4fSDavid Howells  * The key control system call
15861da177e4SLinus Torvalds  */
1587938bb9f5SHeiko Carstens SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1588938bb9f5SHeiko Carstens 		unsigned long, arg4, unsigned long, arg5)
15891da177e4SLinus Torvalds {
15901da177e4SLinus Torvalds 	switch (option) {
15911da177e4SLinus Torvalds 	case KEYCTL_GET_KEYRING_ID:
15921da177e4SLinus Torvalds 		return keyctl_get_keyring_ID((key_serial_t) arg2,
15931da177e4SLinus Torvalds 					     (int) arg3);
15941da177e4SLinus Torvalds 
15951da177e4SLinus Torvalds 	case KEYCTL_JOIN_SESSION_KEYRING:
15961da177e4SLinus Torvalds 		return keyctl_join_session_keyring((const char __user *) arg2);
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds 	case KEYCTL_UPDATE:
15991da177e4SLinus Torvalds 		return keyctl_update_key((key_serial_t) arg2,
16001da177e4SLinus Torvalds 					 (const void __user *) arg3,
16011da177e4SLinus Torvalds 					 (size_t) arg4);
16021da177e4SLinus Torvalds 
16031da177e4SLinus Torvalds 	case KEYCTL_REVOKE:
16041da177e4SLinus Torvalds 		return keyctl_revoke_key((key_serial_t) arg2);
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds 	case KEYCTL_DESCRIBE:
16071da177e4SLinus Torvalds 		return keyctl_describe_key((key_serial_t) arg2,
16081da177e4SLinus Torvalds 					   (char __user *) arg3,
16091da177e4SLinus Torvalds 					   (unsigned) arg4);
16101da177e4SLinus Torvalds 
16111da177e4SLinus Torvalds 	case KEYCTL_CLEAR:
16121da177e4SLinus Torvalds 		return keyctl_keyring_clear((key_serial_t) arg2);
16131da177e4SLinus Torvalds 
16141da177e4SLinus Torvalds 	case KEYCTL_LINK:
16151da177e4SLinus Torvalds 		return keyctl_keyring_link((key_serial_t) arg2,
16161da177e4SLinus Torvalds 					   (key_serial_t) arg3);
16171da177e4SLinus Torvalds 
16181da177e4SLinus Torvalds 	case KEYCTL_UNLINK:
16191da177e4SLinus Torvalds 		return keyctl_keyring_unlink((key_serial_t) arg2,
16201da177e4SLinus Torvalds 					     (key_serial_t) arg3);
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds 	case KEYCTL_SEARCH:
16231da177e4SLinus Torvalds 		return keyctl_keyring_search((key_serial_t) arg2,
16241da177e4SLinus Torvalds 					     (const char __user *) arg3,
16251da177e4SLinus Torvalds 					     (const char __user *) arg4,
16261da177e4SLinus Torvalds 					     (key_serial_t) arg5);
16271da177e4SLinus Torvalds 
16281da177e4SLinus Torvalds 	case KEYCTL_READ:
16291da177e4SLinus Torvalds 		return keyctl_read_key((key_serial_t) arg2,
16301da177e4SLinus Torvalds 				       (char __user *) arg3,
16311da177e4SLinus Torvalds 				       (size_t) arg4);
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds 	case KEYCTL_CHOWN:
16341da177e4SLinus Torvalds 		return keyctl_chown_key((key_serial_t) arg2,
16351da177e4SLinus Torvalds 					(uid_t) arg3,
16361da177e4SLinus Torvalds 					(gid_t) arg4);
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds 	case KEYCTL_SETPERM:
16391da177e4SLinus Torvalds 		return keyctl_setperm_key((key_serial_t) arg2,
16401da177e4SLinus Torvalds 					  (key_perm_t) arg3);
16411da177e4SLinus Torvalds 
16421da177e4SLinus Torvalds 	case KEYCTL_INSTANTIATE:
16431da177e4SLinus Torvalds 		return keyctl_instantiate_key((key_serial_t) arg2,
16441da177e4SLinus Torvalds 					      (const void __user *) arg3,
16451da177e4SLinus Torvalds 					      (size_t) arg4,
16461da177e4SLinus Torvalds 					      (key_serial_t) arg5);
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 	case KEYCTL_NEGATE:
16491da177e4SLinus Torvalds 		return keyctl_negate_key((key_serial_t) arg2,
16501da177e4SLinus Torvalds 					 (unsigned) arg3,
16511da177e4SLinus Torvalds 					 (key_serial_t) arg4);
16521da177e4SLinus Torvalds 
16533e30148cSDavid Howells 	case KEYCTL_SET_REQKEY_KEYRING:
16543e30148cSDavid Howells 		return keyctl_set_reqkey_keyring(arg2);
16553e30148cSDavid Howells 
1656017679c4SDavid Howells 	case KEYCTL_SET_TIMEOUT:
1657017679c4SDavid Howells 		return keyctl_set_timeout((key_serial_t) arg2,
1658017679c4SDavid Howells 					  (unsigned) arg3);
1659017679c4SDavid Howells 
1660b5f545c8SDavid Howells 	case KEYCTL_ASSUME_AUTHORITY:
1661b5f545c8SDavid Howells 		return keyctl_assume_authority((key_serial_t) arg2);
1662b5f545c8SDavid Howells 
166370a5bb72SDavid Howells 	case KEYCTL_GET_SECURITY:
166470a5bb72SDavid Howells 		return keyctl_get_security((key_serial_t) arg2,
166590bd49abSJames Morris 					   (char __user *) arg3,
166670a5bb72SDavid Howells 					   (size_t) arg4);
166770a5bb72SDavid Howells 
1668ee18d64cSDavid Howells 	case KEYCTL_SESSION_TO_PARENT:
1669ee18d64cSDavid Howells 		return keyctl_session_to_parent();
1670ee18d64cSDavid Howells 
1671fdd1b945SDavid Howells 	case KEYCTL_REJECT:
1672fdd1b945SDavid Howells 		return keyctl_reject_key((key_serial_t) arg2,
1673fdd1b945SDavid Howells 					 (unsigned) arg3,
1674fdd1b945SDavid Howells 					 (unsigned) arg4,
1675fdd1b945SDavid Howells 					 (key_serial_t) arg5);
1676fdd1b945SDavid Howells 
1677ee009e4aSDavid Howells 	case KEYCTL_INSTANTIATE_IOV:
1678ee009e4aSDavid Howells 		return keyctl_instantiate_key_iov(
1679ee009e4aSDavid Howells 			(key_serial_t) arg2,
1680ee009e4aSDavid Howells 			(const struct iovec __user *) arg3,
1681ee009e4aSDavid Howells 			(unsigned) arg4,
1682ee009e4aSDavid Howells 			(key_serial_t) arg5);
1683ee009e4aSDavid Howells 
1684fd75815fSDavid Howells 	case KEYCTL_INVALIDATE:
1685fd75815fSDavid Howells 		return keyctl_invalidate_key((key_serial_t) arg2);
1686fd75815fSDavid Howells 
1687f36f8c75SDavid Howells 	case KEYCTL_GET_PERSISTENT:
1688f36f8c75SDavid Howells 		return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3);
1689f36f8c75SDavid Howells 
1690ddbb4114SMat Martineau 	case KEYCTL_DH_COMPUTE:
1691ddbb4114SMat Martineau 		return keyctl_dh_compute((struct keyctl_dh_params __user *) arg2,
16924693fc73SStephan Mueller 					 (char __user *) arg3, (size_t) arg4,
16934693fc73SStephan Mueller 					 (void __user *) arg5);
1694ddbb4114SMat Martineau 
16951da177e4SLinus Torvalds 	default:
16961da177e4SLinus Torvalds 		return -EOPNOTSUPP;
16971da177e4SLinus Torvalds 	}
1698a8b17ed0SDavid Howells }
1699