xref: /openbmc/linux/security/keys/keyctl.c (revision fcf63409)
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>
171da177e4SLinus Torvalds #include <linux/keyctl.h>
181da177e4SLinus Torvalds #include <linux/fs.h>
19c59ede7bSRandy.Dunlap #include <linux/capability.h>
200cb409d9SDavi Arnaut #include <linux/string.h>
211da177e4SLinus Torvalds #include <linux/err.h>
2238bbca6bSDavid Howells #include <linux/vmalloc.h>
2370a5bb72SDavid Howells #include <linux/security.h>
241da177e4SLinus Torvalds #include <asm/uaccess.h>
251da177e4SLinus Torvalds #include "internal.h"
261da177e4SLinus Torvalds 
270cb409d9SDavi Arnaut static int key_get_type_from_user(char *type,
280cb409d9SDavi Arnaut 				  const char __user *_type,
290cb409d9SDavi Arnaut 				  unsigned len)
300cb409d9SDavi Arnaut {
310cb409d9SDavi Arnaut 	int ret;
320cb409d9SDavi Arnaut 
330cb409d9SDavi Arnaut 	ret = strncpy_from_user(type, _type, len);
340cb409d9SDavi Arnaut 	if (ret < 0)
354303ef19SDan Carpenter 		return ret;
360cb409d9SDavi Arnaut 	if (ret == 0 || ret >= len)
370cb409d9SDavi Arnaut 		return -EINVAL;
380cb409d9SDavi Arnaut 	if (type[0] == '.')
390cb409d9SDavi Arnaut 		return -EPERM;
400cb409d9SDavi Arnaut 	type[len - 1] = '\0';
410cb409d9SDavi Arnaut 	return 0;
420cb409d9SDavi Arnaut }
430cb409d9SDavi Arnaut 
441da177e4SLinus Torvalds /*
45973c9f4fSDavid Howells  * Extract the description of a new key from userspace and either add it as a
46973c9f4fSDavid Howells  * new key to the specified keyring or update a matching key in that keyring.
47973c9f4fSDavid Howells  *
48973c9f4fSDavid Howells  * The keyring must be writable so that we can attach the key to it.
49973c9f4fSDavid Howells  *
50973c9f4fSDavid Howells  * If successful, the new key's serial number is returned, otherwise an error
51973c9f4fSDavid Howells  * code is returned.
521da177e4SLinus Torvalds  */
531e7bfb21SHeiko Carstens SYSCALL_DEFINE5(add_key, const char __user *, _type,
541e7bfb21SHeiko Carstens 		const char __user *, _description,
551e7bfb21SHeiko Carstens 		const void __user *, _payload,
561e7bfb21SHeiko Carstens 		size_t, plen,
571e7bfb21SHeiko Carstens 		key_serial_t, ringid)
581da177e4SLinus Torvalds {
59664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref;
601da177e4SLinus Torvalds 	char type[32], *description;
611da177e4SLinus Torvalds 	void *payload;
620cb409d9SDavi Arnaut 	long ret;
6338bbca6bSDavid Howells 	bool vm;
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	ret = -EINVAL;
6638bbca6bSDavid Howells 	if (plen > 1024 * 1024 - 1)
671da177e4SLinus Torvalds 		goto error;
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 	/* draw all the data into kernel space */
700cb409d9SDavi Arnaut 	ret = key_get_type_from_user(type, _type, sizeof(type));
711da177e4SLinus Torvalds 	if (ret < 0)
721da177e4SLinus Torvalds 		goto error;
731da177e4SLinus Torvalds 
740cb409d9SDavi Arnaut 	description = strndup_user(_description, PAGE_SIZE);
750cb409d9SDavi Arnaut 	if (IS_ERR(description)) {
760cb409d9SDavi Arnaut 		ret = PTR_ERR(description);
773e30148cSDavid Howells 		goto error;
780cb409d9SDavi Arnaut 	}
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
811da177e4SLinus Torvalds 	payload = NULL;
821da177e4SLinus Torvalds 
8338bbca6bSDavid Howells 	vm = false;
841da177e4SLinus Torvalds 	if (_payload) {
851da177e4SLinus Torvalds 		ret = -ENOMEM;
861da177e4SLinus Torvalds 		payload = kmalloc(plen, GFP_KERNEL);
8738bbca6bSDavid Howells 		if (!payload) {
8838bbca6bSDavid Howells 			if (plen <= PAGE_SIZE)
8938bbca6bSDavid Howells 				goto error2;
9038bbca6bSDavid Howells 			vm = true;
9138bbca6bSDavid Howells 			payload = vmalloc(plen);
921da177e4SLinus Torvalds 			if (!payload)
931da177e4SLinus Torvalds 				goto error2;
9438bbca6bSDavid Howells 		}
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 		ret = -EFAULT;
971da177e4SLinus Torvalds 		if (copy_from_user(payload, _payload, plen) != 0)
981da177e4SLinus Torvalds 			goto error3;
991da177e4SLinus Torvalds 	}
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	/* find the target keyring (which must be writable) */
1025593122eSDavid Howells 	keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
103664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
104664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
1051da177e4SLinus Torvalds 		goto error3;
1061da177e4SLinus Torvalds 	}
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 	/* create or update the requested key and add it to the target
1091da177e4SLinus Torvalds 	 * keyring */
110664cceb0SDavid Howells 	key_ref = key_create_or_update(keyring_ref, type, description,
1116b79ccb5SArun Raghavan 				       payload, plen, KEY_PERM_UNDEF,
1126b79ccb5SArun Raghavan 				       KEY_ALLOC_IN_QUOTA);
113664cceb0SDavid Howells 	if (!IS_ERR(key_ref)) {
114664cceb0SDavid Howells 		ret = key_ref_to_ptr(key_ref)->serial;
115664cceb0SDavid Howells 		key_ref_put(key_ref);
1161da177e4SLinus Torvalds 	}
1171da177e4SLinus Torvalds 	else {
118664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
1191da177e4SLinus Torvalds 	}
1201da177e4SLinus Torvalds 
121664cceb0SDavid Howells 	key_ref_put(keyring_ref);
1221da177e4SLinus Torvalds  error3:
12338bbca6bSDavid Howells 	if (!vm)
1241da177e4SLinus Torvalds 		kfree(payload);
12538bbca6bSDavid Howells 	else
12638bbca6bSDavid Howells 		vfree(payload);
1271da177e4SLinus Torvalds  error2:
1281da177e4SLinus Torvalds 	kfree(description);
1291da177e4SLinus Torvalds  error:
1301da177e4SLinus Torvalds 	return ret;
131a8b17ed0SDavid Howells }
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds /*
134973c9f4fSDavid Howells  * Search the process keyrings and keyring trees linked from those for a
135973c9f4fSDavid Howells  * matching key.  Keyrings must have appropriate Search permission to be
136973c9f4fSDavid Howells  * searched.
137973c9f4fSDavid Howells  *
138973c9f4fSDavid Howells  * If a key is found, it will be attached to the destination keyring if there's
139973c9f4fSDavid Howells  * one specified and the serial number of the key will be returned.
140973c9f4fSDavid Howells  *
141973c9f4fSDavid Howells  * If no key is found, /sbin/request-key will be invoked if _callout_info is
142973c9f4fSDavid Howells  * non-NULL in an attempt to create a key.  The _callout_info string will be
143973c9f4fSDavid Howells  * passed to /sbin/request-key to aid with completing the request.  If the
144973c9f4fSDavid Howells  * _callout_info string is "" then it will be changed to "-".
1451da177e4SLinus Torvalds  */
1461e7bfb21SHeiko Carstens SYSCALL_DEFINE4(request_key, const char __user *, _type,
1471e7bfb21SHeiko Carstens 		const char __user *, _description,
1481e7bfb21SHeiko Carstens 		const char __user *, _callout_info,
1491e7bfb21SHeiko Carstens 		key_serial_t, destringid)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	struct key_type *ktype;
152664cceb0SDavid Howells 	struct key *key;
153664cceb0SDavid Howells 	key_ref_t dest_ref;
1544a38e122SDavid Howells 	size_t callout_len;
1551da177e4SLinus Torvalds 	char type[32], *description, *callout_info;
1560cb409d9SDavi Arnaut 	long ret;
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	/* pull the type into kernel space */
1590cb409d9SDavi Arnaut 	ret = key_get_type_from_user(type, _type, sizeof(type));
1601da177e4SLinus Torvalds 	if (ret < 0)
1611da177e4SLinus Torvalds 		goto error;
1621260f801SDavid Howells 
1631da177e4SLinus Torvalds 	/* pull the description into kernel space */
1640cb409d9SDavi Arnaut 	description = strndup_user(_description, PAGE_SIZE);
1650cb409d9SDavi Arnaut 	if (IS_ERR(description)) {
1660cb409d9SDavi Arnaut 		ret = PTR_ERR(description);
1671da177e4SLinus Torvalds 		goto error;
1680cb409d9SDavi Arnaut 	}
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 	/* pull the callout info into kernel space */
1711da177e4SLinus Torvalds 	callout_info = NULL;
1724a38e122SDavid Howells 	callout_len = 0;
1731da177e4SLinus Torvalds 	if (_callout_info) {
1740cb409d9SDavi Arnaut 		callout_info = strndup_user(_callout_info, PAGE_SIZE);
1750cb409d9SDavi Arnaut 		if (IS_ERR(callout_info)) {
1760cb409d9SDavi Arnaut 			ret = PTR_ERR(callout_info);
1771da177e4SLinus Torvalds 			goto error2;
1780cb409d9SDavi Arnaut 		}
1794a38e122SDavid Howells 		callout_len = strlen(callout_info);
1801da177e4SLinus Torvalds 	}
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	/* get the destination keyring if specified */
183664cceb0SDavid Howells 	dest_ref = NULL;
1841da177e4SLinus Torvalds 	if (destringid) {
1855593122eSDavid Howells 		dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
1865593122eSDavid Howells 					   KEY_WRITE);
187664cceb0SDavid Howells 		if (IS_ERR(dest_ref)) {
188664cceb0SDavid Howells 			ret = PTR_ERR(dest_ref);
1891da177e4SLinus Torvalds 			goto error3;
1901da177e4SLinus Torvalds 		}
1911da177e4SLinus Torvalds 	}
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	/* find the key type */
1941da177e4SLinus Torvalds 	ktype = key_type_lookup(type);
1951da177e4SLinus Torvalds 	if (IS_ERR(ktype)) {
1961da177e4SLinus Torvalds 		ret = PTR_ERR(ktype);
1971da177e4SLinus Torvalds 		goto error4;
1981da177e4SLinus Torvalds 	}
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	/* do the search */
2014a38e122SDavid Howells 	key = request_key_and_link(ktype, description, callout_info,
2024a38e122SDavid Howells 				   callout_len, NULL, key_ref_to_ptr(dest_ref),
2037e047ef5SDavid Howells 				   KEY_ALLOC_IN_QUOTA);
2041da177e4SLinus Torvalds 	if (IS_ERR(key)) {
2051da177e4SLinus Torvalds 		ret = PTR_ERR(key);
2061da177e4SLinus Torvalds 		goto error5;
2071da177e4SLinus Torvalds 	}
2081da177e4SLinus Torvalds 
2094aab1e89SDavid Howells 	/* wait for the key to finish being constructed */
2104aab1e89SDavid Howells 	ret = wait_for_key_construction(key, 1);
2114aab1e89SDavid Howells 	if (ret < 0)
2124aab1e89SDavid Howells 		goto error6;
2134aab1e89SDavid Howells 
2141da177e4SLinus Torvalds 	ret = key->serial;
2151da177e4SLinus Torvalds 
2164aab1e89SDavid Howells error6:
2171da177e4SLinus Torvalds  	key_put(key);
2181da177e4SLinus Torvalds error5:
2191da177e4SLinus Torvalds 	key_type_put(ktype);
2201da177e4SLinus Torvalds error4:
221664cceb0SDavid Howells 	key_ref_put(dest_ref);
2221da177e4SLinus Torvalds error3:
2231da177e4SLinus Torvalds 	kfree(callout_info);
2241da177e4SLinus Torvalds error2:
2251da177e4SLinus Torvalds 	kfree(description);
2261da177e4SLinus Torvalds error:
2271da177e4SLinus Torvalds 	return ret;
228a8b17ed0SDavid Howells }
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds /*
231973c9f4fSDavid Howells  * Get the ID of the specified process keyring.
232973c9f4fSDavid Howells  *
233973c9f4fSDavid Howells  * The requested keyring must have search permission to be found.
234973c9f4fSDavid Howells  *
235973c9f4fSDavid Howells  * If successful, the ID of the requested keyring will be returned.
2361da177e4SLinus Torvalds  */
2371da177e4SLinus Torvalds long keyctl_get_keyring_ID(key_serial_t id, int create)
2381da177e4SLinus Torvalds {
239664cceb0SDavid Howells 	key_ref_t key_ref;
2405593122eSDavid Howells 	unsigned long lflags;
2411da177e4SLinus Torvalds 	long ret;
2421da177e4SLinus Torvalds 
2435593122eSDavid Howells 	lflags = create ? KEY_LOOKUP_CREATE : 0;
2445593122eSDavid Howells 	key_ref = lookup_user_key(id, lflags, KEY_SEARCH);
245664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
246664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
2471da177e4SLinus Torvalds 		goto error;
2481da177e4SLinus Torvalds 	}
2491da177e4SLinus Torvalds 
250664cceb0SDavid Howells 	ret = key_ref_to_ptr(key_ref)->serial;
251664cceb0SDavid Howells 	key_ref_put(key_ref);
2521da177e4SLinus Torvalds error:
2531da177e4SLinus Torvalds 	return ret;
254973c9f4fSDavid Howells }
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds /*
257973c9f4fSDavid Howells  * Join a (named) session keyring.
258973c9f4fSDavid Howells  *
259973c9f4fSDavid Howells  * Create and join an anonymous session keyring or join a named session
260973c9f4fSDavid Howells  * keyring, creating it if necessary.  A named session keyring must have Search
261973c9f4fSDavid Howells  * permission for it to be joined.  Session keyrings without this permit will
262973c9f4fSDavid Howells  * be skipped over.
263973c9f4fSDavid Howells  *
264973c9f4fSDavid Howells  * If successful, the ID of the joined session keyring will be returned.
2651da177e4SLinus Torvalds  */
2661da177e4SLinus Torvalds long keyctl_join_session_keyring(const char __user *_name)
2671da177e4SLinus Torvalds {
2681da177e4SLinus Torvalds 	char *name;
2690cb409d9SDavi Arnaut 	long ret;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	/* fetch the name from userspace */
2721da177e4SLinus Torvalds 	name = NULL;
2731da177e4SLinus Torvalds 	if (_name) {
2740cb409d9SDavi Arnaut 		name = strndup_user(_name, PAGE_SIZE);
2750cb409d9SDavi Arnaut 		if (IS_ERR(name)) {
2760cb409d9SDavi Arnaut 			ret = PTR_ERR(name);
2771da177e4SLinus Torvalds 			goto error;
2780cb409d9SDavi Arnaut 		}
2791da177e4SLinus Torvalds 	}
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	/* join the session */
2821da177e4SLinus Torvalds 	ret = join_session_keyring(name);
2830d54ee1cSVegard Nossum 	kfree(name);
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds error:
2861da177e4SLinus Torvalds 	return ret;
287a8b17ed0SDavid Howells }
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds /*
290973c9f4fSDavid Howells  * Update a key's data payload from the given data.
291973c9f4fSDavid Howells  *
292973c9f4fSDavid Howells  * The key must grant the caller Write permission and the key type must support
293973c9f4fSDavid Howells  * updating for this to work.  A negative key can be positively instantiated
294973c9f4fSDavid Howells  * with this call.
295973c9f4fSDavid Howells  *
296973c9f4fSDavid Howells  * If successful, 0 will be returned.  If the key type does not support
297973c9f4fSDavid Howells  * updating, then -EOPNOTSUPP will be returned.
2981da177e4SLinus Torvalds  */
2991da177e4SLinus Torvalds long keyctl_update_key(key_serial_t id,
3001da177e4SLinus Torvalds 		       const void __user *_payload,
3011da177e4SLinus Torvalds 		       size_t plen)
3021da177e4SLinus Torvalds {
303664cceb0SDavid Howells 	key_ref_t key_ref;
3041da177e4SLinus Torvalds 	void *payload;
3051da177e4SLinus Torvalds 	long ret;
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	ret = -EINVAL;
3081da177e4SLinus Torvalds 	if (plen > PAGE_SIZE)
3091da177e4SLinus Torvalds 		goto error;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
3121da177e4SLinus Torvalds 	payload = NULL;
3131da177e4SLinus Torvalds 	if (_payload) {
3141da177e4SLinus Torvalds 		ret = -ENOMEM;
3151da177e4SLinus Torvalds 		payload = kmalloc(plen, GFP_KERNEL);
3161da177e4SLinus Torvalds 		if (!payload)
3171da177e4SLinus Torvalds 			goto error;
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 		ret = -EFAULT;
3201da177e4SLinus Torvalds 		if (copy_from_user(payload, _payload, plen) != 0)
3211da177e4SLinus Torvalds 			goto error2;
3221da177e4SLinus Torvalds 	}
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	/* find the target key (which must be writable) */
3255593122eSDavid Howells 	key_ref = lookup_user_key(id, 0, KEY_WRITE);
326664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
327664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
3281da177e4SLinus Torvalds 		goto error2;
3291da177e4SLinus Torvalds 	}
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	/* update the key */
332664cceb0SDavid Howells 	ret = key_update(key_ref, payload, plen);
3331da177e4SLinus Torvalds 
334664cceb0SDavid Howells 	key_ref_put(key_ref);
3351da177e4SLinus Torvalds error2:
3361da177e4SLinus Torvalds 	kfree(payload);
3371da177e4SLinus Torvalds error:
3381da177e4SLinus Torvalds 	return ret;
339a8b17ed0SDavid Howells }
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds /*
342973c9f4fSDavid Howells  * Revoke a key.
343973c9f4fSDavid Howells  *
344973c9f4fSDavid Howells  * The key must be grant the caller Write or Setattr permission for this to
345973c9f4fSDavid Howells  * work.  The key type should give up its quota claim when revoked.  The key
346973c9f4fSDavid Howells  * and any links to the key will be automatically garbage collected after a
347973c9f4fSDavid Howells  * certain amount of time (/proc/sys/kernel/keys/gc_delay).
348973c9f4fSDavid Howells  *
349973c9f4fSDavid Howells  * If successful, 0 is returned.
3501da177e4SLinus Torvalds  */
3511da177e4SLinus Torvalds long keyctl_revoke_key(key_serial_t id)
3521da177e4SLinus Torvalds {
353664cceb0SDavid Howells 	key_ref_t key_ref;
3541da177e4SLinus Torvalds 	long ret;
3551da177e4SLinus Torvalds 
3565593122eSDavid Howells 	key_ref = lookup_user_key(id, 0, KEY_WRITE);
357664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
358664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
3590c2c9a3fSDavid Howells 		if (ret != -EACCES)
3601da177e4SLinus Torvalds 			goto error;
3610c2c9a3fSDavid Howells 		key_ref = lookup_user_key(id, 0, KEY_SETATTR);
3620c2c9a3fSDavid Howells 		if (IS_ERR(key_ref)) {
3630c2c9a3fSDavid Howells 			ret = PTR_ERR(key_ref);
3640c2c9a3fSDavid Howells 			goto error;
3650c2c9a3fSDavid Howells 		}
3661da177e4SLinus Torvalds 	}
3671da177e4SLinus Torvalds 
368664cceb0SDavid Howells 	key_revoke(key_ref_to_ptr(key_ref));
3691da177e4SLinus Torvalds 	ret = 0;
3701da177e4SLinus Torvalds 
371664cceb0SDavid Howells 	key_ref_put(key_ref);
3721da177e4SLinus Torvalds error:
3731260f801SDavid Howells 	return ret;
374a8b17ed0SDavid Howells }
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds /*
377973c9f4fSDavid Howells  * Clear the specified keyring, creating an empty process keyring if one of the
378973c9f4fSDavid Howells  * special keyring IDs is used.
379973c9f4fSDavid Howells  *
380973c9f4fSDavid Howells  * The keyring must grant the caller Write permission for this to work.  If
381973c9f4fSDavid Howells  * successful, 0 will be returned.
3821da177e4SLinus Torvalds  */
3831da177e4SLinus Torvalds long keyctl_keyring_clear(key_serial_t ringid)
3841da177e4SLinus Torvalds {
385664cceb0SDavid Howells 	key_ref_t keyring_ref;
3861da177e4SLinus Torvalds 	long ret;
3871da177e4SLinus Torvalds 
3885593122eSDavid Howells 	keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
389664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
390664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
3911da177e4SLinus Torvalds 		goto error;
3921da177e4SLinus Torvalds 	}
3931da177e4SLinus Torvalds 
394664cceb0SDavid Howells 	ret = keyring_clear(key_ref_to_ptr(keyring_ref));
3951da177e4SLinus Torvalds 
396664cceb0SDavid Howells 	key_ref_put(keyring_ref);
3971da177e4SLinus Torvalds error:
3981da177e4SLinus Torvalds 	return ret;
399a8b17ed0SDavid Howells }
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds /*
402973c9f4fSDavid Howells  * Create a link from a keyring to a key if there's no matching key in the
403973c9f4fSDavid Howells  * keyring, otherwise replace the link to the matching key with a link to the
404973c9f4fSDavid Howells  * new key.
405973c9f4fSDavid Howells  *
406973c9f4fSDavid Howells  * The key must grant the caller Link permission and the the keyring must grant
407973c9f4fSDavid Howells  * the caller Write permission.  Furthermore, if an additional link is created,
408973c9f4fSDavid Howells  * the keyring's quota will be extended.
409973c9f4fSDavid Howells  *
410973c9f4fSDavid Howells  * If successful, 0 will be returned.
4111da177e4SLinus Torvalds  */
4121da177e4SLinus Torvalds long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
4131da177e4SLinus Torvalds {
414664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref;
4151da177e4SLinus Torvalds 	long ret;
4161da177e4SLinus Torvalds 
4175593122eSDavid Howells 	keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
418664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
419664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
4201da177e4SLinus Torvalds 		goto error;
4211da177e4SLinus Torvalds 	}
4221da177e4SLinus Torvalds 
4235593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_LINK);
424664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
425664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
4261da177e4SLinus Torvalds 		goto error2;
4271da177e4SLinus Torvalds 	}
4281da177e4SLinus Torvalds 
429664cceb0SDavid Howells 	ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
4301da177e4SLinus Torvalds 
431664cceb0SDavid Howells 	key_ref_put(key_ref);
4321da177e4SLinus Torvalds error2:
433664cceb0SDavid Howells 	key_ref_put(keyring_ref);
4341da177e4SLinus Torvalds error:
4351da177e4SLinus Torvalds 	return ret;
436a8b17ed0SDavid Howells }
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds /*
439973c9f4fSDavid Howells  * Unlink a key from a keyring.
440973c9f4fSDavid Howells  *
441973c9f4fSDavid Howells  * The keyring must grant the caller Write permission for this to work; the key
442973c9f4fSDavid Howells  * itself need not grant the caller anything.  If the last link to a key is
443973c9f4fSDavid Howells  * removed then that key will be scheduled for destruction.
444973c9f4fSDavid Howells  *
445973c9f4fSDavid Howells  * If successful, 0 will be returned.
4461da177e4SLinus Torvalds  */
4471da177e4SLinus Torvalds long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
4481da177e4SLinus Torvalds {
449664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref;
4501da177e4SLinus Torvalds 	long ret;
4511da177e4SLinus Torvalds 
4525593122eSDavid Howells 	keyring_ref = lookup_user_key(ringid, 0, KEY_WRITE);
453664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
454664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
4551da177e4SLinus Torvalds 		goto error;
4561da177e4SLinus Torvalds 	}
4571da177e4SLinus Torvalds 
4585593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
459664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
460664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
4611da177e4SLinus Torvalds 		goto error2;
4621da177e4SLinus Torvalds 	}
4631da177e4SLinus Torvalds 
464664cceb0SDavid Howells 	ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
4651da177e4SLinus Torvalds 
466664cceb0SDavid Howells 	key_ref_put(key_ref);
4671da177e4SLinus Torvalds error2:
468664cceb0SDavid Howells 	key_ref_put(keyring_ref);
4691da177e4SLinus Torvalds error:
4701da177e4SLinus Torvalds 	return ret;
471a8b17ed0SDavid Howells }
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds /*
474973c9f4fSDavid Howells  * Return a description of a key to userspace.
475973c9f4fSDavid Howells  *
476973c9f4fSDavid Howells  * The key must grant the caller View permission for this to work.
477973c9f4fSDavid Howells  *
478973c9f4fSDavid Howells  * If there's a buffer, we place up to buflen bytes of data into it formatted
479973c9f4fSDavid Howells  * in the following way:
480973c9f4fSDavid Howells  *
4811da177e4SLinus Torvalds  *	type;uid;gid;perm;description<NUL>
482973c9f4fSDavid Howells  *
483973c9f4fSDavid Howells  * If successful, we return the amount of description available, irrespective
484973c9f4fSDavid Howells  * of how much we may have copied into the buffer.
4851da177e4SLinus Torvalds  */
4861da177e4SLinus Torvalds long keyctl_describe_key(key_serial_t keyid,
4871da177e4SLinus Torvalds 			 char __user *buffer,
4881da177e4SLinus Torvalds 			 size_t buflen)
4891da177e4SLinus Torvalds {
4903e30148cSDavid Howells 	struct key *key, *instkey;
491664cceb0SDavid Howells 	key_ref_t key_ref;
4921da177e4SLinus Torvalds 	char *tmpbuf;
4931da177e4SLinus Torvalds 	long ret;
4941da177e4SLinus Torvalds 
4955593122eSDavid Howells 	key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
496664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
4973e30148cSDavid Howells 		/* viewing a key under construction is permitted if we have the
4983e30148cSDavid Howells 		 * authorisation token handy */
499664cceb0SDavid Howells 		if (PTR_ERR(key_ref) == -EACCES) {
5003e30148cSDavid Howells 			instkey = key_get_instantiation_authkey(keyid);
5013e30148cSDavid Howells 			if (!IS_ERR(instkey)) {
5023e30148cSDavid Howells 				key_put(instkey);
5038bbf4976SDavid Howells 				key_ref = lookup_user_key(keyid,
5045593122eSDavid Howells 							  KEY_LOOKUP_PARTIAL,
5055593122eSDavid Howells 							  0);
506664cceb0SDavid Howells 				if (!IS_ERR(key_ref))
5073e30148cSDavid Howells 					goto okay;
5083e30148cSDavid Howells 			}
5093e30148cSDavid Howells 		}
5103e30148cSDavid Howells 
511664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
5121da177e4SLinus Torvalds 		goto error;
5131da177e4SLinus Torvalds 	}
5141da177e4SLinus Torvalds 
5153e30148cSDavid Howells okay:
5161da177e4SLinus Torvalds 	/* calculate how much description we're going to return */
5171da177e4SLinus Torvalds 	ret = -ENOMEM;
5181da177e4SLinus Torvalds 	tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5191da177e4SLinus Torvalds 	if (!tmpbuf)
5201da177e4SLinus Torvalds 		goto error2;
5211da177e4SLinus Torvalds 
522664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
523664cceb0SDavid Howells 
5241da177e4SLinus Torvalds 	ret = snprintf(tmpbuf, PAGE_SIZE - 1,
525664cceb0SDavid Howells 		       "%s;%d;%d;%08x;%s",
52694fd8405SDavid Howells 		       key->type->name,
52794fd8405SDavid Howells 		       key->uid,
52894fd8405SDavid Howells 		       key->gid,
52994fd8405SDavid Howells 		       key->perm,
53094fd8405SDavid Howells 		       key->description ?: "");
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	/* include a NUL char at the end of the data */
5331da177e4SLinus Torvalds 	if (ret > PAGE_SIZE - 1)
5341da177e4SLinus Torvalds 		ret = PAGE_SIZE - 1;
5351da177e4SLinus Torvalds 	tmpbuf[ret] = 0;
5361da177e4SLinus Torvalds 	ret++;
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	/* consider returning the data */
5391da177e4SLinus Torvalds 	if (buffer && buflen > 0) {
5401da177e4SLinus Torvalds 		if (buflen > ret)
5411da177e4SLinus Torvalds 			buflen = ret;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 		if (copy_to_user(buffer, tmpbuf, buflen) != 0)
5441da177e4SLinus Torvalds 			ret = -EFAULT;
5451da177e4SLinus Torvalds 	}
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	kfree(tmpbuf);
5481da177e4SLinus Torvalds error2:
549664cceb0SDavid Howells 	key_ref_put(key_ref);
5501da177e4SLinus Torvalds error:
5511da177e4SLinus Torvalds 	return ret;
552a8b17ed0SDavid Howells }
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds /*
555973c9f4fSDavid Howells  * Search the specified keyring and any keyrings it links to for a matching
556973c9f4fSDavid Howells  * key.  Only keyrings that grant the caller Search permission will be searched
557973c9f4fSDavid Howells  * (this includes the starting keyring).  Only keys with Search permission can
558973c9f4fSDavid Howells  * be found.
559973c9f4fSDavid Howells  *
560973c9f4fSDavid Howells  * If successful, the found key will be linked to the destination keyring if
561973c9f4fSDavid Howells  * supplied and the key has Link permission, and the found key ID will be
562973c9f4fSDavid Howells  * returned.
5631da177e4SLinus Torvalds  */
5641da177e4SLinus Torvalds long keyctl_keyring_search(key_serial_t ringid,
5651da177e4SLinus Torvalds 			   const char __user *_type,
5661da177e4SLinus Torvalds 			   const char __user *_description,
5671da177e4SLinus Torvalds 			   key_serial_t destringid)
5681da177e4SLinus Torvalds {
5691da177e4SLinus Torvalds 	struct key_type *ktype;
570664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref, dest_ref;
5711da177e4SLinus Torvalds 	char type[32], *description;
5720cb409d9SDavi Arnaut 	long ret;
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	/* pull the type and description into kernel space */
5750cb409d9SDavi Arnaut 	ret = key_get_type_from_user(type, _type, sizeof(type));
5761da177e4SLinus Torvalds 	if (ret < 0)
5771da177e4SLinus Torvalds 		goto error;
5781da177e4SLinus Torvalds 
5790cb409d9SDavi Arnaut 	description = strndup_user(_description, PAGE_SIZE);
5800cb409d9SDavi Arnaut 	if (IS_ERR(description)) {
5810cb409d9SDavi Arnaut 		ret = PTR_ERR(description);
5821da177e4SLinus Torvalds 		goto error;
5830cb409d9SDavi Arnaut 	}
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	/* get the keyring at which to begin the search */
5865593122eSDavid Howells 	keyring_ref = lookup_user_key(ringid, 0, KEY_SEARCH);
587664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
588664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
5891da177e4SLinus Torvalds 		goto error2;
5901da177e4SLinus Torvalds 	}
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	/* get the destination keyring if specified */
593664cceb0SDavid Howells 	dest_ref = NULL;
5941da177e4SLinus Torvalds 	if (destringid) {
5955593122eSDavid Howells 		dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
5965593122eSDavid Howells 					   KEY_WRITE);
597664cceb0SDavid Howells 		if (IS_ERR(dest_ref)) {
598664cceb0SDavid Howells 			ret = PTR_ERR(dest_ref);
5991da177e4SLinus Torvalds 			goto error3;
6001da177e4SLinus Torvalds 		}
6011da177e4SLinus Torvalds 	}
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	/* find the key type */
6041da177e4SLinus Torvalds 	ktype = key_type_lookup(type);
6051da177e4SLinus Torvalds 	if (IS_ERR(ktype)) {
6061da177e4SLinus Torvalds 		ret = PTR_ERR(ktype);
6071da177e4SLinus Torvalds 		goto error4;
6081da177e4SLinus Torvalds 	}
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	/* do the search */
611664cceb0SDavid Howells 	key_ref = keyring_search(keyring_ref, ktype, description);
612664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
613664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 		/* treat lack or presence of a negative key the same */
6161da177e4SLinus Torvalds 		if (ret == -EAGAIN)
6171da177e4SLinus Torvalds 			ret = -ENOKEY;
6181da177e4SLinus Torvalds 		goto error5;
6191da177e4SLinus Torvalds 	}
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	/* link the resulting key to the destination keyring if we can */
622664cceb0SDavid Howells 	if (dest_ref) {
62329db9190SDavid Howells 		ret = key_permission(key_ref, KEY_LINK);
62429db9190SDavid Howells 		if (ret < 0)
6251da177e4SLinus Torvalds 			goto error6;
6261da177e4SLinus Torvalds 
627664cceb0SDavid Howells 		ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
6281da177e4SLinus Torvalds 		if (ret < 0)
6291da177e4SLinus Torvalds 			goto error6;
6301da177e4SLinus Torvalds 	}
6311da177e4SLinus Torvalds 
632664cceb0SDavid Howells 	ret = key_ref_to_ptr(key_ref)->serial;
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds error6:
635664cceb0SDavid Howells 	key_ref_put(key_ref);
6361da177e4SLinus Torvalds error5:
6371da177e4SLinus Torvalds 	key_type_put(ktype);
6381da177e4SLinus Torvalds error4:
639664cceb0SDavid Howells 	key_ref_put(dest_ref);
6401da177e4SLinus Torvalds error3:
641664cceb0SDavid Howells 	key_ref_put(keyring_ref);
6421da177e4SLinus Torvalds error2:
6431da177e4SLinus Torvalds 	kfree(description);
6441da177e4SLinus Torvalds error:
6451da177e4SLinus Torvalds 	return ret;
646a8b17ed0SDavid Howells }
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds /*
649973c9f4fSDavid Howells  * Read a key's payload.
650973c9f4fSDavid Howells  *
651973c9f4fSDavid Howells  * The key must either grant the caller Read permission, or it must grant the
652973c9f4fSDavid Howells  * caller Search permission when searched for from the process keyrings.
653973c9f4fSDavid Howells  *
654973c9f4fSDavid Howells  * If successful, we place up to buflen bytes of data into the buffer, if one
655973c9f4fSDavid Howells  * is provided, and return the amount of data that is available in the key,
656973c9f4fSDavid Howells  * irrespective of how much we copied into the buffer.
6571da177e4SLinus Torvalds  */
6581da177e4SLinus Torvalds long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
6591da177e4SLinus Torvalds {
660664cceb0SDavid Howells 	struct key *key;
661664cceb0SDavid Howells 	key_ref_t key_ref;
6621da177e4SLinus Torvalds 	long ret;
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	/* find the key first */
6655593122eSDavid Howells 	key_ref = lookup_user_key(keyid, 0, 0);
666664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
667664cceb0SDavid Howells 		ret = -ENOKEY;
668664cceb0SDavid Howells 		goto error;
669664cceb0SDavid Howells 	}
670664cceb0SDavid Howells 
671664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
672664cceb0SDavid Howells 
6731da177e4SLinus Torvalds 	/* see if we can read it directly */
67429db9190SDavid Howells 	ret = key_permission(key_ref, KEY_READ);
67529db9190SDavid Howells 	if (ret == 0)
6761da177e4SLinus Torvalds 		goto can_read_key;
67729db9190SDavid Howells 	if (ret != -EACCES)
67829db9190SDavid Howells 		goto error;
6791da177e4SLinus Torvalds 
680664cceb0SDavid Howells 	/* we can't; see if it's searchable from this process's keyrings
6813e30148cSDavid Howells 	 * - we automatically take account of the fact that it may be
6823e30148cSDavid Howells 	 *   dangling off an instantiation key
6833e30148cSDavid Howells 	 */
684664cceb0SDavid Howells 	if (!is_key_possessed(key_ref)) {
6851260f801SDavid Howells 		ret = -EACCES;
6861da177e4SLinus Torvalds 		goto error2;
6871da177e4SLinus Torvalds 	}
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	/* the key is probably readable - now try to read it */
6901da177e4SLinus Torvalds can_read_key:
6911da177e4SLinus Torvalds 	ret = key_validate(key);
6921da177e4SLinus Torvalds 	if (ret == 0) {
6931da177e4SLinus Torvalds 		ret = -EOPNOTSUPP;
6941da177e4SLinus Torvalds 		if (key->type->read) {
6951da177e4SLinus Torvalds 			/* read the data with the semaphore held (since we
6961da177e4SLinus Torvalds 			 * might sleep) */
6971da177e4SLinus Torvalds 			down_read(&key->sem);
6981da177e4SLinus Torvalds 			ret = key->type->read(key, buffer, buflen);
6991da177e4SLinus Torvalds 			up_read(&key->sem);
7001da177e4SLinus Torvalds 		}
7011da177e4SLinus Torvalds 	}
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds error2:
7041da177e4SLinus Torvalds 	key_put(key);
7051da177e4SLinus Torvalds error:
7061da177e4SLinus Torvalds 	return ret;
707a8b17ed0SDavid Howells }
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds /*
710973c9f4fSDavid Howells  * Change the ownership of a key
711973c9f4fSDavid Howells  *
712973c9f4fSDavid Howells  * The key must grant the caller Setattr permission for this to work, though
713973c9f4fSDavid Howells  * the key need not be fully instantiated yet.  For the UID to be changed, or
714973c9f4fSDavid Howells  * for the GID to be changed to a group the caller is not a member of, the
715973c9f4fSDavid Howells  * caller must have sysadmin capability.  If either uid or gid is -1 then that
716973c9f4fSDavid Howells  * attribute is not changed.
717973c9f4fSDavid Howells  *
718973c9f4fSDavid Howells  * If the UID is to be changed, the new user must have sufficient quota to
719973c9f4fSDavid Howells  * accept the key.  The quota deduction will be removed from the old user to
720973c9f4fSDavid Howells  * the new user should the attribute be changed.
721973c9f4fSDavid Howells  *
722973c9f4fSDavid Howells  * If successful, 0 will be returned.
7231da177e4SLinus Torvalds  */
7241da177e4SLinus Torvalds long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
7251da177e4SLinus Torvalds {
7265801649dSFredrik Tolf 	struct key_user *newowner, *zapowner = NULL;
7271da177e4SLinus Torvalds 	struct key *key;
728664cceb0SDavid Howells 	key_ref_t key_ref;
7291da177e4SLinus Torvalds 	long ret;
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	ret = 0;
7321da177e4SLinus Torvalds 	if (uid == (uid_t) -1 && gid == (gid_t) -1)
7331da177e4SLinus Torvalds 		goto error;
7341da177e4SLinus Torvalds 
7355593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
7365593122eSDavid Howells 				  KEY_SETATTR);
737664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
738664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
7391da177e4SLinus Torvalds 		goto error;
7401da177e4SLinus Torvalds 	}
7411da177e4SLinus Torvalds 
742664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
743664cceb0SDavid Howells 
7441da177e4SLinus Torvalds 	/* make the changes with the locks held to prevent chown/chown races */
7451da177e4SLinus Torvalds 	ret = -EACCES;
7461da177e4SLinus Torvalds 	down_write(&key->sem);
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN)) {
7491da177e4SLinus Torvalds 		/* only the sysadmin can chown a key to some other UID */
7501da177e4SLinus Torvalds 		if (uid != (uid_t) -1 && key->uid != uid)
7515801649dSFredrik Tolf 			goto error_put;
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 		/* only the sysadmin can set the key's GID to a group other
7541da177e4SLinus Torvalds 		 * than one of those that the current process subscribes to */
7551da177e4SLinus Torvalds 		if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
7565801649dSFredrik Tolf 			goto error_put;
7571da177e4SLinus Torvalds 	}
7581da177e4SLinus Torvalds 
7595801649dSFredrik Tolf 	/* change the UID */
7601da177e4SLinus Torvalds 	if (uid != (uid_t) -1 && uid != key->uid) {
7615801649dSFredrik Tolf 		ret = -ENOMEM;
7621d1e9756SSerge E. Hallyn 		newowner = key_user_lookup(uid, current_user_ns());
7635801649dSFredrik Tolf 		if (!newowner)
7645801649dSFredrik Tolf 			goto error_put;
7655801649dSFredrik Tolf 
7665801649dSFredrik Tolf 		/* transfer the quota burden to the new user */
7675801649dSFredrik Tolf 		if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
7680b77f5bfSDavid Howells 			unsigned maxkeys = (uid == 0) ?
7690b77f5bfSDavid Howells 				key_quota_root_maxkeys : key_quota_maxkeys;
7700b77f5bfSDavid Howells 			unsigned maxbytes = (uid == 0) ?
7710b77f5bfSDavid Howells 				key_quota_root_maxbytes : key_quota_maxbytes;
7720b77f5bfSDavid Howells 
7735801649dSFredrik Tolf 			spin_lock(&newowner->lock);
7740b77f5bfSDavid Howells 			if (newowner->qnkeys + 1 >= maxkeys ||
7750b77f5bfSDavid Howells 			    newowner->qnbytes + key->quotalen >= maxbytes ||
7760b77f5bfSDavid Howells 			    newowner->qnbytes + key->quotalen <
7770b77f5bfSDavid Howells 			    newowner->qnbytes)
7785801649dSFredrik Tolf 				goto quota_overrun;
7795801649dSFredrik Tolf 
7805801649dSFredrik Tolf 			newowner->qnkeys++;
7815801649dSFredrik Tolf 			newowner->qnbytes += key->quotalen;
7825801649dSFredrik Tolf 			spin_unlock(&newowner->lock);
7835801649dSFredrik Tolf 
7845801649dSFredrik Tolf 			spin_lock(&key->user->lock);
7855801649dSFredrik Tolf 			key->user->qnkeys--;
7865801649dSFredrik Tolf 			key->user->qnbytes -= key->quotalen;
7875801649dSFredrik Tolf 			spin_unlock(&key->user->lock);
7885801649dSFredrik Tolf 		}
7895801649dSFredrik Tolf 
7905801649dSFredrik Tolf 		atomic_dec(&key->user->nkeys);
7915801649dSFredrik Tolf 		atomic_inc(&newowner->nkeys);
7925801649dSFredrik Tolf 
7935801649dSFredrik Tolf 		if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
7945801649dSFredrik Tolf 			atomic_dec(&key->user->nikeys);
7955801649dSFredrik Tolf 			atomic_inc(&newowner->nikeys);
7965801649dSFredrik Tolf 		}
7975801649dSFredrik Tolf 
7985801649dSFredrik Tolf 		zapowner = key->user;
7995801649dSFredrik Tolf 		key->user = newowner;
8005801649dSFredrik Tolf 		key->uid = uid;
8011da177e4SLinus Torvalds 	}
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	/* change the GID */
8041da177e4SLinus Torvalds 	if (gid != (gid_t) -1)
8051da177e4SLinus Torvalds 		key->gid = gid;
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds 	ret = 0;
8081da177e4SLinus Torvalds 
8095801649dSFredrik Tolf error_put:
8101da177e4SLinus Torvalds 	up_write(&key->sem);
8111da177e4SLinus Torvalds 	key_put(key);
8125801649dSFredrik Tolf 	if (zapowner)
8135801649dSFredrik Tolf 		key_user_put(zapowner);
8141da177e4SLinus Torvalds error:
8151da177e4SLinus Torvalds 	return ret;
8161da177e4SLinus Torvalds 
8175801649dSFredrik Tolf quota_overrun:
8185801649dSFredrik Tolf 	spin_unlock(&newowner->lock);
8195801649dSFredrik Tolf 	zapowner = newowner;
8205801649dSFredrik Tolf 	ret = -EDQUOT;
8215801649dSFredrik Tolf 	goto error_put;
822a8b17ed0SDavid Howells }
8235801649dSFredrik Tolf 
8241da177e4SLinus Torvalds /*
825973c9f4fSDavid Howells  * Change the permission mask on a key.
826973c9f4fSDavid Howells  *
827973c9f4fSDavid Howells  * The key must grant the caller Setattr permission for this to work, though
828973c9f4fSDavid Howells  * the key need not be fully instantiated yet.  If the caller does not have
829973c9f4fSDavid Howells  * sysadmin capability, it may only change the permission on keys that it owns.
8301da177e4SLinus Torvalds  */
8311da177e4SLinus Torvalds long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
8321da177e4SLinus Torvalds {
8331da177e4SLinus Torvalds 	struct key *key;
834664cceb0SDavid Howells 	key_ref_t key_ref;
8351da177e4SLinus Torvalds 	long ret;
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 	ret = -EINVAL;
838664cceb0SDavid Howells 	if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
8391da177e4SLinus Torvalds 		goto error;
8401da177e4SLinus Torvalds 
8415593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
8425593122eSDavid Howells 				  KEY_SETATTR);
843664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
844664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
8451da177e4SLinus Torvalds 		goto error;
8461da177e4SLinus Torvalds 	}
8471da177e4SLinus Torvalds 
848664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
849664cceb0SDavid Howells 
85076d8aeabSDavid Howells 	/* make the changes with the locks held to prevent chown/chmod races */
8511da177e4SLinus Torvalds 	ret = -EACCES;
8521da177e4SLinus Torvalds 	down_write(&key->sem);
8531da177e4SLinus Torvalds 
85476d8aeabSDavid Howells 	/* if we're not the sysadmin, we can only change a key that we own */
85547d804bfSDavid Howells 	if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) {
8561da177e4SLinus Torvalds 		key->perm = perm;
8571da177e4SLinus Torvalds 		ret = 0;
85876d8aeabSDavid Howells 	}
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds 	up_write(&key->sem);
8611da177e4SLinus Torvalds 	key_put(key);
8621da177e4SLinus Torvalds error:
8631da177e4SLinus Torvalds 	return ret;
864a8b17ed0SDavid Howells }
8651da177e4SLinus Torvalds 
8668bbf4976SDavid Howells /*
867973c9f4fSDavid Howells  * Get the destination keyring for instantiation and check that the caller has
868973c9f4fSDavid Howells  * Write permission on it.
8698bbf4976SDavid Howells  */
8708bbf4976SDavid Howells static long get_instantiation_keyring(key_serial_t ringid,
8718bbf4976SDavid Howells 				      struct request_key_auth *rka,
8728bbf4976SDavid Howells 				      struct key **_dest_keyring)
8738bbf4976SDavid Howells {
8748bbf4976SDavid Howells 	key_ref_t dkref;
8758bbf4976SDavid Howells 
8768bbf4976SDavid Howells 	*_dest_keyring = NULL;
877eca1bf5bSDavid Howells 
878eca1bf5bSDavid Howells 	/* just return a NULL pointer if we weren't asked to make a link */
879eca1bf5bSDavid Howells 	if (ringid == 0)
8808bbf4976SDavid Howells 		return 0;
8818bbf4976SDavid Howells 
8828bbf4976SDavid Howells 	/* if a specific keyring is nominated by ID, then use that */
8838bbf4976SDavid Howells 	if (ringid > 0) {
8845593122eSDavid Howells 		dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
8858bbf4976SDavid Howells 		if (IS_ERR(dkref))
8868bbf4976SDavid Howells 			return PTR_ERR(dkref);
8878bbf4976SDavid Howells 		*_dest_keyring = key_ref_to_ptr(dkref);
8888bbf4976SDavid Howells 		return 0;
8898bbf4976SDavid Howells 	}
8908bbf4976SDavid Howells 
8918bbf4976SDavid Howells 	if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
8928bbf4976SDavid Howells 		return -EINVAL;
8938bbf4976SDavid Howells 
8948bbf4976SDavid Howells 	/* otherwise specify the destination keyring recorded in the
8958bbf4976SDavid Howells 	 * authorisation key (any KEY_SPEC_*_KEYRING) */
8968bbf4976SDavid Howells 	if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
89721279cfaSDavid Howells 		*_dest_keyring = key_get(rka->dest_keyring);
8988bbf4976SDavid Howells 		return 0;
8998bbf4976SDavid Howells 	}
9008bbf4976SDavid Howells 
9018bbf4976SDavid Howells 	return -ENOKEY;
9028bbf4976SDavid Howells }
9038bbf4976SDavid Howells 
904d84f4f99SDavid Howells /*
905973c9f4fSDavid Howells  * Change the request_key authorisation key on the current process.
906d84f4f99SDavid Howells  */
907d84f4f99SDavid Howells static int keyctl_change_reqkey_auth(struct key *key)
908d84f4f99SDavid Howells {
909d84f4f99SDavid Howells 	struct cred *new;
910d84f4f99SDavid Howells 
911d84f4f99SDavid Howells 	new = prepare_creds();
912d84f4f99SDavid Howells 	if (!new)
913d84f4f99SDavid Howells 		return -ENOMEM;
914d84f4f99SDavid Howells 
915d84f4f99SDavid Howells 	key_put(new->request_key_auth);
916d84f4f99SDavid Howells 	new->request_key_auth = key_get(key);
917d84f4f99SDavid Howells 
918d84f4f99SDavid Howells 	return commit_creds(new);
919d84f4f99SDavid Howells }
920d84f4f99SDavid Howells 
9211da177e4SLinus Torvalds /*
922ee009e4aSDavid Howells  * Copy the iovec data from userspace
923ee009e4aSDavid Howells  */
924ee009e4aSDavid Howells static long copy_from_user_iovec(void *buffer, const struct iovec *iov,
925ee009e4aSDavid Howells 				 unsigned ioc)
926ee009e4aSDavid Howells {
927ee009e4aSDavid Howells 	for (; ioc > 0; ioc--) {
928ee009e4aSDavid Howells 		if (copy_from_user(buffer, iov->iov_base, iov->iov_len) != 0)
929ee009e4aSDavid Howells 			return -EFAULT;
930ee009e4aSDavid Howells 		buffer += iov->iov_len;
931ee009e4aSDavid Howells 		iov++;
932ee009e4aSDavid Howells 	}
933ee009e4aSDavid Howells 	return 0;
934ee009e4aSDavid Howells }
935ee009e4aSDavid Howells 
936ee009e4aSDavid Howells /*
937973c9f4fSDavid Howells  * Instantiate a key with the specified payload and link the key into the
938973c9f4fSDavid Howells  * destination keyring if one is given.
939973c9f4fSDavid Howells  *
940973c9f4fSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
941973c9f4fSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
942973c9f4fSDavid Howells  *
943973c9f4fSDavid Howells  * If successful, 0 will be returned.
9441da177e4SLinus Torvalds  */
945ee009e4aSDavid Howells long keyctl_instantiate_key_common(key_serial_t id,
946ee009e4aSDavid Howells 				   const struct iovec *payload_iov,
947ee009e4aSDavid Howells 				   unsigned ioc,
9481da177e4SLinus Torvalds 				   size_t plen,
9491da177e4SLinus Torvalds 				   key_serial_t ringid)
9501da177e4SLinus Torvalds {
951d84f4f99SDavid Howells 	const struct cred *cred = current_cred();
9523e30148cSDavid Howells 	struct request_key_auth *rka;
9538bbf4976SDavid Howells 	struct key *instkey, *dest_keyring;
9541da177e4SLinus Torvalds 	void *payload;
9551da177e4SLinus Torvalds 	long ret;
95638bbca6bSDavid Howells 	bool vm = false;
9571da177e4SLinus Torvalds 
958d84f4f99SDavid Howells 	kenter("%d,,%zu,%d", id, plen, ringid);
959d84f4f99SDavid Howells 
9601da177e4SLinus Torvalds 	ret = -EINVAL;
96138bbca6bSDavid Howells 	if (plen > 1024 * 1024 - 1)
9621da177e4SLinus Torvalds 		goto error;
9631da177e4SLinus Torvalds 
964b5f545c8SDavid Howells 	/* the appropriate instantiation authorisation key must have been
965b5f545c8SDavid Howells 	 * assumed before calling this */
966b5f545c8SDavid Howells 	ret = -EPERM;
967d84f4f99SDavid Howells 	instkey = cred->request_key_auth;
968b5f545c8SDavid Howells 	if (!instkey)
969b5f545c8SDavid Howells 		goto error;
970b5f545c8SDavid Howells 
971b5f545c8SDavid Howells 	rka = instkey->payload.data;
972b5f545c8SDavid Howells 	if (rka->target_key->serial != id)
973b5f545c8SDavid Howells 		goto error;
974b5f545c8SDavid Howells 
9751da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
9761da177e4SLinus Torvalds 	payload = NULL;
9771da177e4SLinus Torvalds 
978ee009e4aSDavid Howells 	if (payload_iov) {
9791da177e4SLinus Torvalds 		ret = -ENOMEM;
9801da177e4SLinus Torvalds 		payload = kmalloc(plen, GFP_KERNEL);
98138bbca6bSDavid Howells 		if (!payload) {
98238bbca6bSDavid Howells 			if (plen <= PAGE_SIZE)
98338bbca6bSDavid Howells 				goto error;
98438bbca6bSDavid Howells 			vm = true;
98538bbca6bSDavid Howells 			payload = vmalloc(plen);
9861da177e4SLinus Torvalds 			if (!payload)
9871da177e4SLinus Torvalds 				goto error;
98838bbca6bSDavid Howells 		}
9891da177e4SLinus Torvalds 
990ee009e4aSDavid Howells 		ret = copy_from_user_iovec(payload, payload_iov, ioc);
991ee009e4aSDavid Howells 		if (ret < 0)
9921da177e4SLinus Torvalds 			goto error2;
9931da177e4SLinus Torvalds 	}
9941da177e4SLinus Torvalds 
9953e30148cSDavid Howells 	/* find the destination keyring amongst those belonging to the
9963e30148cSDavid Howells 	 * requesting task */
9978bbf4976SDavid Howells 	ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
9988bbf4976SDavid Howells 	if (ret < 0)
999b5f545c8SDavid Howells 		goto error2;
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 	/* instantiate the key and link it into a keyring */
10023e30148cSDavid Howells 	ret = key_instantiate_and_link(rka->target_key, payload, plen,
10038bbf4976SDavid Howells 				       dest_keyring, instkey);
10041da177e4SLinus Torvalds 
10058bbf4976SDavid Howells 	key_put(dest_keyring);
1006b5f545c8SDavid Howells 
1007b5f545c8SDavid Howells 	/* discard the assumed authority if it's just been disabled by
1008b5f545c8SDavid Howells 	 * instantiation of the key */
1009d84f4f99SDavid Howells 	if (ret == 0)
1010d84f4f99SDavid Howells 		keyctl_change_reqkey_auth(NULL);
1011b5f545c8SDavid Howells 
10121da177e4SLinus Torvalds error2:
101338bbca6bSDavid Howells 	if (!vm)
10141da177e4SLinus Torvalds 		kfree(payload);
101538bbca6bSDavid Howells 	else
101638bbca6bSDavid Howells 		vfree(payload);
10171da177e4SLinus Torvalds error:
10181da177e4SLinus Torvalds 	return ret;
1019a8b17ed0SDavid Howells }
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds /*
1022ee009e4aSDavid Howells  * Instantiate a key with the specified payload and link the key into the
1023ee009e4aSDavid Howells  * destination keyring if one is given.
1024ee009e4aSDavid Howells  *
1025ee009e4aSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1026ee009e4aSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1027ee009e4aSDavid Howells  *
1028ee009e4aSDavid Howells  * If successful, 0 will be returned.
1029ee009e4aSDavid Howells  */
1030ee009e4aSDavid Howells long keyctl_instantiate_key(key_serial_t id,
1031ee009e4aSDavid Howells 			    const void __user *_payload,
1032ee009e4aSDavid Howells 			    size_t plen,
1033ee009e4aSDavid Howells 			    key_serial_t ringid)
1034ee009e4aSDavid Howells {
1035ee009e4aSDavid Howells 	if (_payload && plen) {
1036ee009e4aSDavid Howells 		struct iovec iov[1] = {
1037ee009e4aSDavid Howells 			[0].iov_base = (void __user *)_payload,
1038ee009e4aSDavid Howells 			[0].iov_len  = plen
1039ee009e4aSDavid Howells 		};
1040ee009e4aSDavid Howells 
1041ee009e4aSDavid Howells 		return keyctl_instantiate_key_common(id, iov, 1, plen, ringid);
1042ee009e4aSDavid Howells 	}
1043ee009e4aSDavid Howells 
1044ee009e4aSDavid Howells 	return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1045ee009e4aSDavid Howells }
1046ee009e4aSDavid Howells 
1047ee009e4aSDavid Howells /*
1048ee009e4aSDavid Howells  * Instantiate a key with the specified multipart payload and link the key into
1049ee009e4aSDavid Howells  * the destination keyring if one is given.
1050ee009e4aSDavid Howells  *
1051ee009e4aSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1052ee009e4aSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1053ee009e4aSDavid Howells  *
1054ee009e4aSDavid Howells  * If successful, 0 will be returned.
1055ee009e4aSDavid Howells  */
1056ee009e4aSDavid Howells long keyctl_instantiate_key_iov(key_serial_t id,
1057ee009e4aSDavid Howells 				const struct iovec __user *_payload_iov,
1058ee009e4aSDavid Howells 				unsigned ioc,
1059ee009e4aSDavid Howells 				key_serial_t ringid)
1060ee009e4aSDavid Howells {
1061ee009e4aSDavid Howells 	struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1062ee009e4aSDavid Howells 	long ret;
1063ee009e4aSDavid Howells 
1064ee009e4aSDavid Howells 	if (_payload_iov == 0 || ioc == 0)
1065ee009e4aSDavid Howells 		goto no_payload;
1066ee009e4aSDavid Howells 
1067ee009e4aSDavid Howells 	ret = rw_copy_check_uvector(WRITE, _payload_iov, ioc,
1068*fcf63409SChristopher Yeoh 				    ARRAY_SIZE(iovstack), iovstack, &iov, 1);
1069ee009e4aSDavid Howells 	if (ret < 0)
1070ee009e4aSDavid Howells 		return ret;
1071ee009e4aSDavid Howells 	if (ret == 0)
1072ee009e4aSDavid Howells 		goto no_payload_free;
1073ee009e4aSDavid Howells 
1074ee009e4aSDavid Howells 	ret = keyctl_instantiate_key_common(id, iov, ioc, ret, ringid);
1075ee009e4aSDavid Howells 
1076ee009e4aSDavid Howells 	if (iov != iovstack)
1077ee009e4aSDavid Howells 		kfree(iov);
1078ee009e4aSDavid Howells 	return ret;
1079ee009e4aSDavid Howells 
1080ee009e4aSDavid Howells no_payload_free:
1081ee009e4aSDavid Howells 	if (iov != iovstack)
1082ee009e4aSDavid Howells 		kfree(iov);
1083ee009e4aSDavid Howells no_payload:
1084ee009e4aSDavid Howells 	return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1085ee009e4aSDavid Howells }
1086ee009e4aSDavid Howells 
1087ee009e4aSDavid Howells /*
1088973c9f4fSDavid Howells  * Negatively instantiate the key with the given timeout (in seconds) and link
1089973c9f4fSDavid Howells  * the key into the destination keyring if one is given.
1090973c9f4fSDavid Howells  *
1091973c9f4fSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1092973c9f4fSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1093973c9f4fSDavid Howells  *
1094973c9f4fSDavid Howells  * The key and any links to the key will be automatically garbage collected
1095973c9f4fSDavid Howells  * after the timeout expires.
1096973c9f4fSDavid Howells  *
1097973c9f4fSDavid Howells  * Negative keys are used to rate limit repeated request_key() calls by causing
1098973c9f4fSDavid Howells  * them to return -ENOKEY until the negative key expires.
1099973c9f4fSDavid Howells  *
1100973c9f4fSDavid Howells  * If successful, 0 will be returned.
11011da177e4SLinus Torvalds  */
11021da177e4SLinus Torvalds long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
11031da177e4SLinus Torvalds {
1104fdd1b945SDavid Howells 	return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1105fdd1b945SDavid Howells }
1106fdd1b945SDavid Howells 
1107fdd1b945SDavid Howells /*
1108fdd1b945SDavid Howells  * Negatively instantiate the key with the given timeout (in seconds) and error
1109fdd1b945SDavid Howells  * code and link the key into the destination keyring if one is given.
1110fdd1b945SDavid Howells  *
1111fdd1b945SDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1112fdd1b945SDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1113fdd1b945SDavid Howells  *
1114fdd1b945SDavid Howells  * The key and any links to the key will be automatically garbage collected
1115fdd1b945SDavid Howells  * after the timeout expires.
1116fdd1b945SDavid Howells  *
1117fdd1b945SDavid Howells  * Negative keys are used to rate limit repeated request_key() calls by causing
1118fdd1b945SDavid Howells  * them to return the specified error code until the negative key expires.
1119fdd1b945SDavid Howells  *
1120fdd1b945SDavid Howells  * If successful, 0 will be returned.
1121fdd1b945SDavid Howells  */
1122fdd1b945SDavid Howells long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1123fdd1b945SDavid Howells 		       key_serial_t ringid)
1124fdd1b945SDavid Howells {
1125d84f4f99SDavid Howells 	const struct cred *cred = current_cred();
11263e30148cSDavid Howells 	struct request_key_auth *rka;
11278bbf4976SDavid Howells 	struct key *instkey, *dest_keyring;
11281da177e4SLinus Torvalds 	long ret;
11291da177e4SLinus Torvalds 
1130fdd1b945SDavid Howells 	kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1131fdd1b945SDavid Howells 
1132fdd1b945SDavid Howells 	/* must be a valid error code and mustn't be a kernel special */
1133fdd1b945SDavid Howells 	if (error <= 0 ||
1134fdd1b945SDavid Howells 	    error >= MAX_ERRNO ||
1135fdd1b945SDavid Howells 	    error == ERESTARTSYS ||
1136fdd1b945SDavid Howells 	    error == ERESTARTNOINTR ||
1137fdd1b945SDavid Howells 	    error == ERESTARTNOHAND ||
1138fdd1b945SDavid Howells 	    error == ERESTART_RESTARTBLOCK)
1139fdd1b945SDavid Howells 		return -EINVAL;
1140d84f4f99SDavid Howells 
1141b5f545c8SDavid Howells 	/* the appropriate instantiation authorisation key must have been
1142b5f545c8SDavid Howells 	 * assumed before calling this */
1143b5f545c8SDavid Howells 	ret = -EPERM;
1144d84f4f99SDavid Howells 	instkey = cred->request_key_auth;
1145b5f545c8SDavid Howells 	if (!instkey)
11461da177e4SLinus Torvalds 		goto error;
11471da177e4SLinus Torvalds 
11483e30148cSDavid Howells 	rka = instkey->payload.data;
1149b5f545c8SDavid Howells 	if (rka->target_key->serial != id)
1150b5f545c8SDavid Howells 		goto error;
11513e30148cSDavid Howells 
11521da177e4SLinus Torvalds 	/* find the destination keyring if present (which must also be
11531da177e4SLinus Torvalds 	 * writable) */
11548bbf4976SDavid Howells 	ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
11558bbf4976SDavid Howells 	if (ret < 0)
1156b5f545c8SDavid Howells 		goto error;
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 	/* instantiate the key and link it into a keyring */
1159fdd1b945SDavid Howells 	ret = key_reject_and_link(rka->target_key, timeout, error,
11608bbf4976SDavid Howells 				  dest_keyring, instkey);
11611da177e4SLinus Torvalds 
11628bbf4976SDavid Howells 	key_put(dest_keyring);
1163b5f545c8SDavid Howells 
1164b5f545c8SDavid Howells 	/* discard the assumed authority if it's just been disabled by
1165b5f545c8SDavid Howells 	 * instantiation of the key */
1166d84f4f99SDavid Howells 	if (ret == 0)
1167d84f4f99SDavid Howells 		keyctl_change_reqkey_auth(NULL);
1168b5f545c8SDavid Howells 
11691da177e4SLinus Torvalds error:
11701da177e4SLinus Torvalds 	return ret;
1171a8b17ed0SDavid Howells }
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds /*
1174973c9f4fSDavid Howells  * Read or set the default keyring in which request_key() will cache keys and
1175973c9f4fSDavid Howells  * return the old setting.
1176973c9f4fSDavid Howells  *
1177973c9f4fSDavid Howells  * If a process keyring is specified then this will be created if it doesn't
1178973c9f4fSDavid Howells  * yet exist.  The old setting will be returned if successful.
11793e30148cSDavid Howells  */
11803e30148cSDavid Howells long keyctl_set_reqkey_keyring(int reqkey_defl)
11813e30148cSDavid Howells {
1182d84f4f99SDavid Howells 	struct cred *new;
1183d84f4f99SDavid Howells 	int ret, old_setting;
1184d84f4f99SDavid Howells 
1185d84f4f99SDavid Howells 	old_setting = current_cred_xxx(jit_keyring);
1186d84f4f99SDavid Howells 
1187d84f4f99SDavid Howells 	if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1188d84f4f99SDavid Howells 		return old_setting;
1189d84f4f99SDavid Howells 
1190d84f4f99SDavid Howells 	new = prepare_creds();
1191d84f4f99SDavid Howells 	if (!new)
1192d84f4f99SDavid Howells 		return -ENOMEM;
11933e30148cSDavid Howells 
11943e30148cSDavid Howells 	switch (reqkey_defl) {
11953e30148cSDavid Howells 	case KEY_REQKEY_DEFL_THREAD_KEYRING:
1196d84f4f99SDavid Howells 		ret = install_thread_keyring_to_cred(new);
11973e30148cSDavid Howells 		if (ret < 0)
1198d84f4f99SDavid Howells 			goto error;
11993e30148cSDavid Howells 		goto set;
12003e30148cSDavid Howells 
12013e30148cSDavid Howells 	case KEY_REQKEY_DEFL_PROCESS_KEYRING:
1202d84f4f99SDavid Howells 		ret = install_process_keyring_to_cred(new);
1203d84f4f99SDavid Howells 		if (ret < 0) {
1204d84f4f99SDavid Howells 			if (ret != -EEXIST)
1205d84f4f99SDavid Howells 				goto error;
1206d84f4f99SDavid Howells 			ret = 0;
1207d84f4f99SDavid Howells 		}
1208d84f4f99SDavid Howells 		goto set;
12093e30148cSDavid Howells 
12103e30148cSDavid Howells 	case KEY_REQKEY_DEFL_DEFAULT:
12113e30148cSDavid Howells 	case KEY_REQKEY_DEFL_SESSION_KEYRING:
12123e30148cSDavid Howells 	case KEY_REQKEY_DEFL_USER_KEYRING:
12133e30148cSDavid Howells 	case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1214d84f4f99SDavid Howells 	case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1215d84f4f99SDavid Howells 		goto set;
12163e30148cSDavid Howells 
12173e30148cSDavid Howells 	case KEY_REQKEY_DEFL_NO_CHANGE:
12183e30148cSDavid Howells 	case KEY_REQKEY_DEFL_GROUP_KEYRING:
12193e30148cSDavid Howells 	default:
1220d84f4f99SDavid Howells 		ret = -EINVAL;
1221d84f4f99SDavid Howells 		goto error;
12223e30148cSDavid Howells 	}
12233e30148cSDavid Howells 
1224d84f4f99SDavid Howells set:
1225d84f4f99SDavid Howells 	new->jit_keyring = reqkey_defl;
1226d84f4f99SDavid Howells 	commit_creds(new);
1227d84f4f99SDavid Howells 	return old_setting;
1228d84f4f99SDavid Howells error:
1229d84f4f99SDavid Howells 	abort_creds(new);
12304303ef19SDan Carpenter 	return ret;
1231a8b17ed0SDavid Howells }
1232d84f4f99SDavid Howells 
12333e30148cSDavid Howells /*
1234973c9f4fSDavid Howells  * Set or clear the timeout on a key.
1235973c9f4fSDavid Howells  *
1236973c9f4fSDavid Howells  * Either the key must grant the caller Setattr permission or else the caller
1237973c9f4fSDavid Howells  * must hold an instantiation authorisation token for the key.
1238973c9f4fSDavid Howells  *
1239973c9f4fSDavid Howells  * The timeout is either 0 to clear the timeout, or a number of seconds from
1240973c9f4fSDavid Howells  * the current time.  The key and any links to the key will be automatically
1241973c9f4fSDavid Howells  * garbage collected after the timeout expires.
1242973c9f4fSDavid Howells  *
1243973c9f4fSDavid Howells  * If successful, 0 is returned.
1244017679c4SDavid Howells  */
1245017679c4SDavid Howells long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1246017679c4SDavid Howells {
1247017679c4SDavid Howells 	struct timespec now;
12489156235bSDavid Howells 	struct key *key, *instkey;
1249017679c4SDavid Howells 	key_ref_t key_ref;
1250017679c4SDavid Howells 	time_t expiry;
1251017679c4SDavid Howells 	long ret;
1252017679c4SDavid Howells 
12535593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
12545593122eSDavid Howells 				  KEY_SETATTR);
1255017679c4SDavid Howells 	if (IS_ERR(key_ref)) {
12569156235bSDavid Howells 		/* setting the timeout on a key under construction is permitted
12579156235bSDavid Howells 		 * if we have the authorisation token handy */
12589156235bSDavid Howells 		if (PTR_ERR(key_ref) == -EACCES) {
12599156235bSDavid Howells 			instkey = key_get_instantiation_authkey(id);
12609156235bSDavid Howells 			if (!IS_ERR(instkey)) {
12619156235bSDavid Howells 				key_put(instkey);
12629156235bSDavid Howells 				key_ref = lookup_user_key(id,
12639156235bSDavid Howells 							  KEY_LOOKUP_PARTIAL,
12649156235bSDavid Howells 							  0);
12659156235bSDavid Howells 				if (!IS_ERR(key_ref))
12669156235bSDavid Howells 					goto okay;
12679156235bSDavid Howells 			}
12689156235bSDavid Howells 		}
12699156235bSDavid Howells 
1270017679c4SDavid Howells 		ret = PTR_ERR(key_ref);
1271017679c4SDavid Howells 		goto error;
1272017679c4SDavid Howells 	}
1273017679c4SDavid Howells 
12749156235bSDavid Howells okay:
1275017679c4SDavid Howells 	key = key_ref_to_ptr(key_ref);
1276017679c4SDavid Howells 
1277017679c4SDavid Howells 	/* make the changes with the locks held to prevent races */
1278017679c4SDavid Howells 	down_write(&key->sem);
1279017679c4SDavid Howells 
1280017679c4SDavid Howells 	expiry = 0;
1281017679c4SDavid Howells 	if (timeout > 0) {
1282017679c4SDavid Howells 		now = current_kernel_time();
1283017679c4SDavid Howells 		expiry = now.tv_sec + timeout;
1284017679c4SDavid Howells 	}
1285017679c4SDavid Howells 
1286017679c4SDavid Howells 	key->expiry = expiry;
1287c08ef808SDavid Howells 	key_schedule_gc(key->expiry + key_gc_delay);
1288017679c4SDavid Howells 
1289017679c4SDavid Howells 	up_write(&key->sem);
1290017679c4SDavid Howells 	key_put(key);
1291017679c4SDavid Howells 
1292017679c4SDavid Howells 	ret = 0;
1293017679c4SDavid Howells error:
1294017679c4SDavid Howells 	return ret;
1295a8b17ed0SDavid Howells }
1296017679c4SDavid Howells 
1297017679c4SDavid Howells /*
1298973c9f4fSDavid Howells  * Assume (or clear) the authority to instantiate the specified key.
1299973c9f4fSDavid Howells  *
1300973c9f4fSDavid Howells  * This sets the authoritative token currently in force for key instantiation.
1301973c9f4fSDavid Howells  * This must be done for a key to be instantiated.  It has the effect of making
1302973c9f4fSDavid Howells  * available all the keys from the caller of the request_key() that created a
1303973c9f4fSDavid Howells  * key to request_key() calls made by the caller of this function.
1304973c9f4fSDavid Howells  *
1305973c9f4fSDavid Howells  * The caller must have the instantiation key in their process keyrings with a
1306973c9f4fSDavid Howells  * Search permission grant available to the caller.
1307973c9f4fSDavid Howells  *
1308973c9f4fSDavid Howells  * If the ID given is 0, then the setting will be cleared and 0 returned.
1309973c9f4fSDavid Howells  *
1310973c9f4fSDavid Howells  * If the ID given has a matching an authorisation key, then that key will be
1311973c9f4fSDavid Howells  * set and its ID will be returned.  The authorisation key can be read to get
1312973c9f4fSDavid Howells  * the callout information passed to request_key().
1313b5f545c8SDavid Howells  */
1314b5f545c8SDavid Howells long keyctl_assume_authority(key_serial_t id)
1315b5f545c8SDavid Howells {
1316b5f545c8SDavid Howells 	struct key *authkey;
1317b5f545c8SDavid Howells 	long ret;
1318b5f545c8SDavid Howells 
1319b5f545c8SDavid Howells 	/* special key IDs aren't permitted */
1320b5f545c8SDavid Howells 	ret = -EINVAL;
1321b5f545c8SDavid Howells 	if (id < 0)
1322b5f545c8SDavid Howells 		goto error;
1323b5f545c8SDavid Howells 
1324b5f545c8SDavid Howells 	/* we divest ourselves of authority if given an ID of 0 */
1325b5f545c8SDavid Howells 	if (id == 0) {
1326d84f4f99SDavid Howells 		ret = keyctl_change_reqkey_auth(NULL);
1327b5f545c8SDavid Howells 		goto error;
1328b5f545c8SDavid Howells 	}
1329b5f545c8SDavid Howells 
1330b5f545c8SDavid Howells 	/* attempt to assume the authority temporarily granted to us whilst we
1331b5f545c8SDavid Howells 	 * instantiate the specified key
1332b5f545c8SDavid Howells 	 * - the authorisation key must be in the current task's keyrings
1333b5f545c8SDavid Howells 	 *   somewhere
1334b5f545c8SDavid Howells 	 */
1335b5f545c8SDavid Howells 	authkey = key_get_instantiation_authkey(id);
1336b5f545c8SDavid Howells 	if (IS_ERR(authkey)) {
1337b5f545c8SDavid Howells 		ret = PTR_ERR(authkey);
1338b5f545c8SDavid Howells 		goto error;
1339b5f545c8SDavid Howells 	}
1340b5f545c8SDavid Howells 
1341d84f4f99SDavid Howells 	ret = keyctl_change_reqkey_auth(authkey);
1342d84f4f99SDavid Howells 	if (ret < 0)
1343d84f4f99SDavid Howells 		goto error;
1344d84f4f99SDavid Howells 	key_put(authkey);
1345b5f545c8SDavid Howells 
1346d84f4f99SDavid Howells 	ret = authkey->serial;
1347b5f545c8SDavid Howells error:
1348b5f545c8SDavid Howells 	return ret;
1349a8b17ed0SDavid Howells }
1350b5f545c8SDavid Howells 
135170a5bb72SDavid Howells /*
1352973c9f4fSDavid Howells  * Get a key's the LSM security label.
1353973c9f4fSDavid Howells  *
1354973c9f4fSDavid Howells  * The key must grant the caller View permission for this to work.
1355973c9f4fSDavid Howells  *
1356973c9f4fSDavid Howells  * If there's a buffer, then up to buflen bytes of data will be placed into it.
1357973c9f4fSDavid Howells  *
1358973c9f4fSDavid Howells  * If successful, the amount of information available will be returned,
1359973c9f4fSDavid Howells  * irrespective of how much was copied (including the terminal NUL).
136070a5bb72SDavid Howells  */
136170a5bb72SDavid Howells long keyctl_get_security(key_serial_t keyid,
136270a5bb72SDavid Howells 			 char __user *buffer,
136370a5bb72SDavid Howells 			 size_t buflen)
136470a5bb72SDavid Howells {
136570a5bb72SDavid Howells 	struct key *key, *instkey;
136670a5bb72SDavid Howells 	key_ref_t key_ref;
136770a5bb72SDavid Howells 	char *context;
136870a5bb72SDavid Howells 	long ret;
136970a5bb72SDavid Howells 
13705593122eSDavid Howells 	key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
137170a5bb72SDavid Howells 	if (IS_ERR(key_ref)) {
137270a5bb72SDavid Howells 		if (PTR_ERR(key_ref) != -EACCES)
137370a5bb72SDavid Howells 			return PTR_ERR(key_ref);
137470a5bb72SDavid Howells 
137570a5bb72SDavid Howells 		/* viewing a key under construction is also permitted if we
137670a5bb72SDavid Howells 		 * have the authorisation token handy */
137770a5bb72SDavid Howells 		instkey = key_get_instantiation_authkey(keyid);
137870a5bb72SDavid Howells 		if (IS_ERR(instkey))
1379fa1cc7b5SRoel Kluin 			return PTR_ERR(instkey);
138070a5bb72SDavid Howells 		key_put(instkey);
138170a5bb72SDavid Howells 
13825593122eSDavid Howells 		key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
138370a5bb72SDavid Howells 		if (IS_ERR(key_ref))
138470a5bb72SDavid Howells 			return PTR_ERR(key_ref);
138570a5bb72SDavid Howells 	}
138670a5bb72SDavid Howells 
138770a5bb72SDavid Howells 	key = key_ref_to_ptr(key_ref);
138870a5bb72SDavid Howells 	ret = security_key_getsecurity(key, &context);
138970a5bb72SDavid Howells 	if (ret == 0) {
139070a5bb72SDavid Howells 		/* if no information was returned, give userspace an empty
139170a5bb72SDavid Howells 		 * string */
139270a5bb72SDavid Howells 		ret = 1;
139370a5bb72SDavid Howells 		if (buffer && buflen > 0 &&
139470a5bb72SDavid Howells 		    copy_to_user(buffer, "", 1) != 0)
139570a5bb72SDavid Howells 			ret = -EFAULT;
139670a5bb72SDavid Howells 	} else if (ret > 0) {
139770a5bb72SDavid Howells 		/* return as much data as there's room for */
139870a5bb72SDavid Howells 		if (buffer && buflen > 0) {
139970a5bb72SDavid Howells 			if (buflen > ret)
140070a5bb72SDavid Howells 				buflen = ret;
140170a5bb72SDavid Howells 
140270a5bb72SDavid Howells 			if (copy_to_user(buffer, context, buflen) != 0)
140370a5bb72SDavid Howells 				ret = -EFAULT;
140470a5bb72SDavid Howells 		}
140570a5bb72SDavid Howells 
140670a5bb72SDavid Howells 		kfree(context);
140770a5bb72SDavid Howells 	}
140870a5bb72SDavid Howells 
140970a5bb72SDavid Howells 	key_ref_put(key_ref);
141070a5bb72SDavid Howells 	return ret;
141170a5bb72SDavid Howells }
141270a5bb72SDavid Howells 
1413ee18d64cSDavid Howells /*
1414973c9f4fSDavid Howells  * Attempt to install the calling process's session keyring on the process's
1415973c9f4fSDavid Howells  * parent process.
1416973c9f4fSDavid Howells  *
1417973c9f4fSDavid Howells  * The keyring must exist and must grant the caller LINK permission, and the
1418973c9f4fSDavid Howells  * parent process must be single-threaded and must have the same effective
1419973c9f4fSDavid Howells  * ownership as this process and mustn't be SUID/SGID.
1420973c9f4fSDavid Howells  *
1421973c9f4fSDavid Howells  * The keyring will be emplaced on the parent when it next resumes userspace.
1422973c9f4fSDavid Howells  *
1423973c9f4fSDavid Howells  * If successful, 0 will be returned.
1424ee18d64cSDavid Howells  */
1425ee18d64cSDavid Howells long keyctl_session_to_parent(void)
1426ee18d64cSDavid Howells {
1427a00ae4d2SGeert Uytterhoeven #ifdef TIF_NOTIFY_RESUME
1428ee18d64cSDavid Howells 	struct task_struct *me, *parent;
1429ee18d64cSDavid Howells 	const struct cred *mycred, *pcred;
1430ee18d64cSDavid Howells 	struct cred *cred, *oldcred;
1431ee18d64cSDavid Howells 	key_ref_t keyring_r;
1432ee18d64cSDavid Howells 	int ret;
1433ee18d64cSDavid Howells 
1434ee18d64cSDavid Howells 	keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_LINK);
1435ee18d64cSDavid Howells 	if (IS_ERR(keyring_r))
1436ee18d64cSDavid Howells 		return PTR_ERR(keyring_r);
1437ee18d64cSDavid Howells 
1438ee18d64cSDavid Howells 	/* our parent is going to need a new cred struct, a new tgcred struct
1439ee18d64cSDavid Howells 	 * and new security data, so we allocate them here to prevent ENOMEM in
1440ee18d64cSDavid Howells 	 * our parent */
1441ee18d64cSDavid Howells 	ret = -ENOMEM;
1442ee18d64cSDavid Howells 	cred = cred_alloc_blank();
1443ee18d64cSDavid Howells 	if (!cred)
1444ee18d64cSDavid Howells 		goto error_keyring;
1445ee18d64cSDavid Howells 
1446ee18d64cSDavid Howells 	cred->tgcred->session_keyring = key_ref_to_ptr(keyring_r);
1447ee18d64cSDavid Howells 	keyring_r = NULL;
1448ee18d64cSDavid Howells 
1449ee18d64cSDavid Howells 	me = current;
14509d1ac65aSDavid Howells 	rcu_read_lock();
1451ee18d64cSDavid Howells 	write_lock_irq(&tasklist_lock);
1452ee18d64cSDavid Howells 
1453ee18d64cSDavid Howells 	parent = me->real_parent;
1454ee18d64cSDavid Howells 	ret = -EPERM;
1455ee18d64cSDavid Howells 
1456ee18d64cSDavid Howells 	/* the parent mustn't be init and mustn't be a kernel thread */
1457ee18d64cSDavid Howells 	if (parent->pid <= 1 || !parent->mm)
1458ee18d64cSDavid Howells 		goto not_permitted;
1459ee18d64cSDavid Howells 
1460ee18d64cSDavid Howells 	/* the parent must be single threaded */
1461dd98acf7SOleg Nesterov 	if (!thread_group_empty(parent))
1462ee18d64cSDavid Howells 		goto not_permitted;
1463ee18d64cSDavid Howells 
1464ee18d64cSDavid Howells 	/* the parent and the child must have different session keyrings or
1465ee18d64cSDavid Howells 	 * there's no point */
1466ee18d64cSDavid Howells 	mycred = current_cred();
1467ee18d64cSDavid Howells 	pcred = __task_cred(parent);
1468ee18d64cSDavid Howells 	if (mycred == pcred ||
1469ee18d64cSDavid Howells 	    mycred->tgcred->session_keyring == pcred->tgcred->session_keyring)
1470ee18d64cSDavid Howells 		goto already_same;
1471ee18d64cSDavid Howells 
1472ee18d64cSDavid Howells 	/* the parent must have the same effective ownership and mustn't be
1473ee18d64cSDavid Howells 	 * SUID/SGID */
1474ee18d64cSDavid Howells 	if (pcred->uid	!= mycred->euid	||
1475ee18d64cSDavid Howells 	    pcred->euid	!= mycred->euid	||
1476ee18d64cSDavid Howells 	    pcred->suid	!= mycred->euid	||
1477ee18d64cSDavid Howells 	    pcred->gid	!= mycred->egid	||
1478ee18d64cSDavid Howells 	    pcred->egid	!= mycred->egid	||
1479ee18d64cSDavid Howells 	    pcred->sgid	!= mycred->egid)
1480ee18d64cSDavid Howells 		goto not_permitted;
1481ee18d64cSDavid Howells 
1482ee18d64cSDavid Howells 	/* the keyrings must have the same UID */
14833d96406cSDavid Howells 	if ((pcred->tgcred->session_keyring &&
14843d96406cSDavid Howells 	     pcred->tgcred->session_keyring->uid != mycred->euid) ||
1485ee18d64cSDavid Howells 	    mycred->tgcred->session_keyring->uid != mycred->euid)
1486ee18d64cSDavid Howells 		goto not_permitted;
1487ee18d64cSDavid Howells 
1488ee18d64cSDavid Howells 	/* if there's an already pending keyring replacement, then we replace
1489ee18d64cSDavid Howells 	 * that */
1490ee18d64cSDavid Howells 	oldcred = parent->replacement_session_keyring;
1491ee18d64cSDavid Howells 
1492ee18d64cSDavid Howells 	/* the replacement session keyring is applied just prior to userspace
1493ee18d64cSDavid Howells 	 * restarting */
1494ee18d64cSDavid Howells 	parent->replacement_session_keyring = cred;
1495ee18d64cSDavid Howells 	cred = NULL;
1496ee18d64cSDavid Howells 	set_ti_thread_flag(task_thread_info(parent), TIF_NOTIFY_RESUME);
1497ee18d64cSDavid Howells 
1498ee18d64cSDavid Howells 	write_unlock_irq(&tasklist_lock);
14999d1ac65aSDavid Howells 	rcu_read_unlock();
1500ee18d64cSDavid Howells 	if (oldcred)
1501ee18d64cSDavid Howells 		put_cred(oldcred);
1502ee18d64cSDavid Howells 	return 0;
1503ee18d64cSDavid Howells 
1504ee18d64cSDavid Howells already_same:
1505ee18d64cSDavid Howells 	ret = 0;
1506ee18d64cSDavid Howells not_permitted:
15075c84342aSMarc Dionne 	write_unlock_irq(&tasklist_lock);
15089d1ac65aSDavid Howells 	rcu_read_unlock();
1509ee18d64cSDavid Howells 	put_cred(cred);
1510ee18d64cSDavid Howells 	return ret;
1511ee18d64cSDavid Howells 
1512ee18d64cSDavid Howells error_keyring:
1513ee18d64cSDavid Howells 	key_ref_put(keyring_r);
1514ee18d64cSDavid Howells 	return ret;
1515a00ae4d2SGeert Uytterhoeven 
1516a00ae4d2SGeert Uytterhoeven #else /* !TIF_NOTIFY_RESUME */
1517a00ae4d2SGeert Uytterhoeven 	/*
1518a00ae4d2SGeert Uytterhoeven 	 * To be removed when TIF_NOTIFY_RESUME has been implemented on
1519a00ae4d2SGeert Uytterhoeven 	 * m68k/xtensa
1520a00ae4d2SGeert Uytterhoeven 	 */
1521a00ae4d2SGeert Uytterhoeven #warning TIF_NOTIFY_RESUME not implemented
1522a00ae4d2SGeert Uytterhoeven 	return -EOPNOTSUPP;
1523a00ae4d2SGeert Uytterhoeven #endif /* !TIF_NOTIFY_RESUME */
1524ee18d64cSDavid Howells }
1525ee18d64cSDavid Howells 
1526b5f545c8SDavid Howells /*
1527973c9f4fSDavid Howells  * The key control system call
15281da177e4SLinus Torvalds  */
1529938bb9f5SHeiko Carstens SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1530938bb9f5SHeiko Carstens 		unsigned long, arg4, unsigned long, arg5)
15311da177e4SLinus Torvalds {
15321da177e4SLinus Torvalds 	switch (option) {
15331da177e4SLinus Torvalds 	case KEYCTL_GET_KEYRING_ID:
15341da177e4SLinus Torvalds 		return keyctl_get_keyring_ID((key_serial_t) arg2,
15351da177e4SLinus Torvalds 					     (int) arg3);
15361da177e4SLinus Torvalds 
15371da177e4SLinus Torvalds 	case KEYCTL_JOIN_SESSION_KEYRING:
15381da177e4SLinus Torvalds 		return keyctl_join_session_keyring((const char __user *) arg2);
15391da177e4SLinus Torvalds 
15401da177e4SLinus Torvalds 	case KEYCTL_UPDATE:
15411da177e4SLinus Torvalds 		return keyctl_update_key((key_serial_t) arg2,
15421da177e4SLinus Torvalds 					 (const void __user *) arg3,
15431da177e4SLinus Torvalds 					 (size_t) arg4);
15441da177e4SLinus Torvalds 
15451da177e4SLinus Torvalds 	case KEYCTL_REVOKE:
15461da177e4SLinus Torvalds 		return keyctl_revoke_key((key_serial_t) arg2);
15471da177e4SLinus Torvalds 
15481da177e4SLinus Torvalds 	case KEYCTL_DESCRIBE:
15491da177e4SLinus Torvalds 		return keyctl_describe_key((key_serial_t) arg2,
15501da177e4SLinus Torvalds 					   (char __user *) arg3,
15511da177e4SLinus Torvalds 					   (unsigned) arg4);
15521da177e4SLinus Torvalds 
15531da177e4SLinus Torvalds 	case KEYCTL_CLEAR:
15541da177e4SLinus Torvalds 		return keyctl_keyring_clear((key_serial_t) arg2);
15551da177e4SLinus Torvalds 
15561da177e4SLinus Torvalds 	case KEYCTL_LINK:
15571da177e4SLinus Torvalds 		return keyctl_keyring_link((key_serial_t) arg2,
15581da177e4SLinus Torvalds 					   (key_serial_t) arg3);
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds 	case KEYCTL_UNLINK:
15611da177e4SLinus Torvalds 		return keyctl_keyring_unlink((key_serial_t) arg2,
15621da177e4SLinus Torvalds 					     (key_serial_t) arg3);
15631da177e4SLinus Torvalds 
15641da177e4SLinus Torvalds 	case KEYCTL_SEARCH:
15651da177e4SLinus Torvalds 		return keyctl_keyring_search((key_serial_t) arg2,
15661da177e4SLinus Torvalds 					     (const char __user *) arg3,
15671da177e4SLinus Torvalds 					     (const char __user *) arg4,
15681da177e4SLinus Torvalds 					     (key_serial_t) arg5);
15691da177e4SLinus Torvalds 
15701da177e4SLinus Torvalds 	case KEYCTL_READ:
15711da177e4SLinus Torvalds 		return keyctl_read_key((key_serial_t) arg2,
15721da177e4SLinus Torvalds 				       (char __user *) arg3,
15731da177e4SLinus Torvalds 				       (size_t) arg4);
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds 	case KEYCTL_CHOWN:
15761da177e4SLinus Torvalds 		return keyctl_chown_key((key_serial_t) arg2,
15771da177e4SLinus Torvalds 					(uid_t) arg3,
15781da177e4SLinus Torvalds 					(gid_t) arg4);
15791da177e4SLinus Torvalds 
15801da177e4SLinus Torvalds 	case KEYCTL_SETPERM:
15811da177e4SLinus Torvalds 		return keyctl_setperm_key((key_serial_t) arg2,
15821da177e4SLinus Torvalds 					  (key_perm_t) arg3);
15831da177e4SLinus Torvalds 
15841da177e4SLinus Torvalds 	case KEYCTL_INSTANTIATE:
15851da177e4SLinus Torvalds 		return keyctl_instantiate_key((key_serial_t) arg2,
15861da177e4SLinus Torvalds 					      (const void __user *) arg3,
15871da177e4SLinus Torvalds 					      (size_t) arg4,
15881da177e4SLinus Torvalds 					      (key_serial_t) arg5);
15891da177e4SLinus Torvalds 
15901da177e4SLinus Torvalds 	case KEYCTL_NEGATE:
15911da177e4SLinus Torvalds 		return keyctl_negate_key((key_serial_t) arg2,
15921da177e4SLinus Torvalds 					 (unsigned) arg3,
15931da177e4SLinus Torvalds 					 (key_serial_t) arg4);
15941da177e4SLinus Torvalds 
15953e30148cSDavid Howells 	case KEYCTL_SET_REQKEY_KEYRING:
15963e30148cSDavid Howells 		return keyctl_set_reqkey_keyring(arg2);
15973e30148cSDavid Howells 
1598017679c4SDavid Howells 	case KEYCTL_SET_TIMEOUT:
1599017679c4SDavid Howells 		return keyctl_set_timeout((key_serial_t) arg2,
1600017679c4SDavid Howells 					  (unsigned) arg3);
1601017679c4SDavid Howells 
1602b5f545c8SDavid Howells 	case KEYCTL_ASSUME_AUTHORITY:
1603b5f545c8SDavid Howells 		return keyctl_assume_authority((key_serial_t) arg2);
1604b5f545c8SDavid Howells 
160570a5bb72SDavid Howells 	case KEYCTL_GET_SECURITY:
160670a5bb72SDavid Howells 		return keyctl_get_security((key_serial_t) arg2,
160790bd49abSJames Morris 					   (char __user *) arg3,
160870a5bb72SDavid Howells 					   (size_t) arg4);
160970a5bb72SDavid Howells 
1610ee18d64cSDavid Howells 	case KEYCTL_SESSION_TO_PARENT:
1611ee18d64cSDavid Howells 		return keyctl_session_to_parent();
1612ee18d64cSDavid Howells 
1613fdd1b945SDavid Howells 	case KEYCTL_REJECT:
1614fdd1b945SDavid Howells 		return keyctl_reject_key((key_serial_t) arg2,
1615fdd1b945SDavid Howells 					 (unsigned) arg3,
1616fdd1b945SDavid Howells 					 (unsigned) arg4,
1617fdd1b945SDavid Howells 					 (key_serial_t) arg5);
1618fdd1b945SDavid Howells 
1619ee009e4aSDavid Howells 	case KEYCTL_INSTANTIATE_IOV:
1620ee009e4aSDavid Howells 		return keyctl_instantiate_key_iov(
1621ee009e4aSDavid Howells 			(key_serial_t) arg2,
1622ee009e4aSDavid Howells 			(const struct iovec __user *) arg3,
1623ee009e4aSDavid Howells 			(unsigned) arg4,
1624ee009e4aSDavid Howells 			(key_serial_t) arg5);
1625ee009e4aSDavid Howells 
16261da177e4SLinus Torvalds 	default:
16271da177e4SLinus Torvalds 		return -EOPNOTSUPP;
16281da177e4SLinus Torvalds 	}
1629a8b17ed0SDavid Howells }
1630