xref: /openbmc/linux/security/keys/keyctl.c (revision 700920eb)
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);
391*700920ebSDavid Howells 
392*700920ebSDavid Howells 		/* Root is permitted to invalidate certain special keyrings */
393*700920ebSDavid Howells 		if (capable(CAP_SYS_ADMIN)) {
394*700920ebSDavid Howells 			keyring_ref = lookup_user_key(ringid, 0, 0);
395*700920ebSDavid Howells 			if (IS_ERR(keyring_ref))
396*700920ebSDavid Howells 				goto error;
397*700920ebSDavid Howells 			if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
398*700920ebSDavid Howells 				     &key_ref_to_ptr(keyring_ref)->flags))
399*700920ebSDavid Howells 				goto clear;
400*700920ebSDavid Howells 			goto error_put;
401*700920ebSDavid Howells 		}
402*700920ebSDavid Howells 
4031da177e4SLinus Torvalds 		goto error;
4041da177e4SLinus Torvalds 	}
4051da177e4SLinus Torvalds 
406*700920ebSDavid Howells clear:
407664cceb0SDavid Howells 	ret = keyring_clear(key_ref_to_ptr(keyring_ref));
408*700920ebSDavid Howells error_put:
409664cceb0SDavid Howells 	key_ref_put(keyring_ref);
4101da177e4SLinus Torvalds error:
4111da177e4SLinus Torvalds 	return ret;
412a8b17ed0SDavid Howells }
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds /*
415973c9f4fSDavid Howells  * Create a link from a keyring to a key if there's no matching key in the
416973c9f4fSDavid Howells  * keyring, otherwise replace the link to the matching key with a link to the
417973c9f4fSDavid Howells  * new key.
418973c9f4fSDavid Howells  *
419973c9f4fSDavid Howells  * The key must grant the caller Link permission and the the keyring must grant
420973c9f4fSDavid Howells  * the caller Write permission.  Furthermore, if an additional link is created,
421973c9f4fSDavid Howells  * the keyring's quota will be extended.
422973c9f4fSDavid Howells  *
423973c9f4fSDavid Howells  * If successful, 0 will be returned.
4241da177e4SLinus Torvalds  */
4251da177e4SLinus Torvalds long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
4261da177e4SLinus Torvalds {
427664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref;
4281da177e4SLinus Torvalds 	long ret;
4291da177e4SLinus Torvalds 
4305593122eSDavid Howells 	keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
431664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
432664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
4331da177e4SLinus Torvalds 		goto error;
4341da177e4SLinus Torvalds 	}
4351da177e4SLinus Torvalds 
4365593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_LINK);
437664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
438664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
4391da177e4SLinus Torvalds 		goto error2;
4401da177e4SLinus Torvalds 	}
4411da177e4SLinus Torvalds 
442664cceb0SDavid Howells 	ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
4431da177e4SLinus Torvalds 
444664cceb0SDavid Howells 	key_ref_put(key_ref);
4451da177e4SLinus Torvalds error2:
446664cceb0SDavid Howells 	key_ref_put(keyring_ref);
4471da177e4SLinus Torvalds error:
4481da177e4SLinus Torvalds 	return ret;
449a8b17ed0SDavid Howells }
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds /*
452973c9f4fSDavid Howells  * Unlink a key from a keyring.
453973c9f4fSDavid Howells  *
454973c9f4fSDavid Howells  * The keyring must grant the caller Write permission for this to work; the key
455973c9f4fSDavid Howells  * itself need not grant the caller anything.  If the last link to a key is
456973c9f4fSDavid Howells  * removed then that key will be scheduled for destruction.
457973c9f4fSDavid Howells  *
458973c9f4fSDavid Howells  * If successful, 0 will be returned.
4591da177e4SLinus Torvalds  */
4601da177e4SLinus Torvalds long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
4611da177e4SLinus Torvalds {
462664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref;
4631da177e4SLinus Torvalds 	long ret;
4641da177e4SLinus Torvalds 
4655593122eSDavid Howells 	keyring_ref = lookup_user_key(ringid, 0, KEY_WRITE);
466664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
467664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
4681da177e4SLinus Torvalds 		goto error;
4691da177e4SLinus Torvalds 	}
4701da177e4SLinus Torvalds 
4715593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
472664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
473664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
4741da177e4SLinus Torvalds 		goto error2;
4751da177e4SLinus Torvalds 	}
4761da177e4SLinus Torvalds 
477664cceb0SDavid Howells 	ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
4781da177e4SLinus Torvalds 
479664cceb0SDavid Howells 	key_ref_put(key_ref);
4801da177e4SLinus Torvalds error2:
481664cceb0SDavid Howells 	key_ref_put(keyring_ref);
4821da177e4SLinus Torvalds error:
4831da177e4SLinus Torvalds 	return ret;
484a8b17ed0SDavid Howells }
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds /*
487973c9f4fSDavid Howells  * Return a description of a key to userspace.
488973c9f4fSDavid Howells  *
489973c9f4fSDavid Howells  * The key must grant the caller View permission for this to work.
490973c9f4fSDavid Howells  *
491973c9f4fSDavid Howells  * If there's a buffer, we place up to buflen bytes of data into it formatted
492973c9f4fSDavid Howells  * in the following way:
493973c9f4fSDavid Howells  *
4941da177e4SLinus Torvalds  *	type;uid;gid;perm;description<NUL>
495973c9f4fSDavid Howells  *
496973c9f4fSDavid Howells  * If successful, we return the amount of description available, irrespective
497973c9f4fSDavid Howells  * of how much we may have copied into the buffer.
4981da177e4SLinus Torvalds  */
4991da177e4SLinus Torvalds long keyctl_describe_key(key_serial_t keyid,
5001da177e4SLinus Torvalds 			 char __user *buffer,
5011da177e4SLinus Torvalds 			 size_t buflen)
5021da177e4SLinus Torvalds {
5033e30148cSDavid Howells 	struct key *key, *instkey;
504664cceb0SDavid Howells 	key_ref_t key_ref;
5051da177e4SLinus Torvalds 	char *tmpbuf;
5061da177e4SLinus Torvalds 	long ret;
5071da177e4SLinus Torvalds 
5085593122eSDavid Howells 	key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
509664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
5103e30148cSDavid Howells 		/* viewing a key under construction is permitted if we have the
5113e30148cSDavid Howells 		 * authorisation token handy */
512664cceb0SDavid Howells 		if (PTR_ERR(key_ref) == -EACCES) {
5133e30148cSDavid Howells 			instkey = key_get_instantiation_authkey(keyid);
5143e30148cSDavid Howells 			if (!IS_ERR(instkey)) {
5153e30148cSDavid Howells 				key_put(instkey);
5168bbf4976SDavid Howells 				key_ref = lookup_user_key(keyid,
5175593122eSDavid Howells 							  KEY_LOOKUP_PARTIAL,
5185593122eSDavid Howells 							  0);
519664cceb0SDavid Howells 				if (!IS_ERR(key_ref))
5203e30148cSDavid Howells 					goto okay;
5213e30148cSDavid Howells 			}
5223e30148cSDavid Howells 		}
5233e30148cSDavid Howells 
524664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
5251da177e4SLinus Torvalds 		goto error;
5261da177e4SLinus Torvalds 	}
5271da177e4SLinus Torvalds 
5283e30148cSDavid Howells okay:
5291da177e4SLinus Torvalds 	/* calculate how much description we're going to return */
5301da177e4SLinus Torvalds 	ret = -ENOMEM;
5311da177e4SLinus Torvalds 	tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5321da177e4SLinus Torvalds 	if (!tmpbuf)
5331da177e4SLinus Torvalds 		goto error2;
5341da177e4SLinus Torvalds 
535664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
536664cceb0SDavid Howells 
5371da177e4SLinus Torvalds 	ret = snprintf(tmpbuf, PAGE_SIZE - 1,
538664cceb0SDavid Howells 		       "%s;%d;%d;%08x;%s",
53994fd8405SDavid Howells 		       key->type->name,
54094fd8405SDavid Howells 		       key->uid,
54194fd8405SDavid Howells 		       key->gid,
54294fd8405SDavid Howells 		       key->perm,
54394fd8405SDavid Howells 		       key->description ?: "");
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	/* include a NUL char at the end of the data */
5461da177e4SLinus Torvalds 	if (ret > PAGE_SIZE - 1)
5471da177e4SLinus Torvalds 		ret = PAGE_SIZE - 1;
5481da177e4SLinus Torvalds 	tmpbuf[ret] = 0;
5491da177e4SLinus Torvalds 	ret++;
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	/* consider returning the data */
5521da177e4SLinus Torvalds 	if (buffer && buflen > 0) {
5531da177e4SLinus Torvalds 		if (buflen > ret)
5541da177e4SLinus Torvalds 			buflen = ret;
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 		if (copy_to_user(buffer, tmpbuf, buflen) != 0)
5571da177e4SLinus Torvalds 			ret = -EFAULT;
5581da177e4SLinus Torvalds 	}
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	kfree(tmpbuf);
5611da177e4SLinus Torvalds error2:
562664cceb0SDavid Howells 	key_ref_put(key_ref);
5631da177e4SLinus Torvalds error:
5641da177e4SLinus Torvalds 	return ret;
565a8b17ed0SDavid Howells }
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds /*
568973c9f4fSDavid Howells  * Search the specified keyring and any keyrings it links to for a matching
569973c9f4fSDavid Howells  * key.  Only keyrings that grant the caller Search permission will be searched
570973c9f4fSDavid Howells  * (this includes the starting keyring).  Only keys with Search permission can
571973c9f4fSDavid Howells  * be found.
572973c9f4fSDavid Howells  *
573973c9f4fSDavid Howells  * If successful, the found key will be linked to the destination keyring if
574973c9f4fSDavid Howells  * supplied and the key has Link permission, and the found key ID will be
575973c9f4fSDavid Howells  * returned.
5761da177e4SLinus Torvalds  */
5771da177e4SLinus Torvalds long keyctl_keyring_search(key_serial_t ringid,
5781da177e4SLinus Torvalds 			   const char __user *_type,
5791da177e4SLinus Torvalds 			   const char __user *_description,
5801da177e4SLinus Torvalds 			   key_serial_t destringid)
5811da177e4SLinus Torvalds {
5821da177e4SLinus Torvalds 	struct key_type *ktype;
583664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref, dest_ref;
5841da177e4SLinus Torvalds 	char type[32], *description;
5850cb409d9SDavi Arnaut 	long ret;
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 	/* pull the type and description into kernel space */
5880cb409d9SDavi Arnaut 	ret = key_get_type_from_user(type, _type, sizeof(type));
5891da177e4SLinus Torvalds 	if (ret < 0)
5901da177e4SLinus Torvalds 		goto error;
5911da177e4SLinus Torvalds 
5920cb409d9SDavi Arnaut 	description = strndup_user(_description, PAGE_SIZE);
5930cb409d9SDavi Arnaut 	if (IS_ERR(description)) {
5940cb409d9SDavi Arnaut 		ret = PTR_ERR(description);
5951da177e4SLinus Torvalds 		goto error;
5960cb409d9SDavi Arnaut 	}
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	/* get the keyring at which to begin the search */
5995593122eSDavid Howells 	keyring_ref = lookup_user_key(ringid, 0, KEY_SEARCH);
600664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
601664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
6021da177e4SLinus Torvalds 		goto error2;
6031da177e4SLinus Torvalds 	}
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	/* get the destination keyring if specified */
606664cceb0SDavid Howells 	dest_ref = NULL;
6071da177e4SLinus Torvalds 	if (destringid) {
6085593122eSDavid Howells 		dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
6095593122eSDavid Howells 					   KEY_WRITE);
610664cceb0SDavid Howells 		if (IS_ERR(dest_ref)) {
611664cceb0SDavid Howells 			ret = PTR_ERR(dest_ref);
6121da177e4SLinus Torvalds 			goto error3;
6131da177e4SLinus Torvalds 		}
6141da177e4SLinus Torvalds 	}
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	/* find the key type */
6171da177e4SLinus Torvalds 	ktype = key_type_lookup(type);
6181da177e4SLinus Torvalds 	if (IS_ERR(ktype)) {
6191da177e4SLinus Torvalds 		ret = PTR_ERR(ktype);
6201da177e4SLinus Torvalds 		goto error4;
6211da177e4SLinus Torvalds 	}
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	/* do the search */
624664cceb0SDavid Howells 	key_ref = keyring_search(keyring_ref, ktype, description);
625664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
626664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 		/* treat lack or presence of a negative key the same */
6291da177e4SLinus Torvalds 		if (ret == -EAGAIN)
6301da177e4SLinus Torvalds 			ret = -ENOKEY;
6311da177e4SLinus Torvalds 		goto error5;
6321da177e4SLinus Torvalds 	}
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	/* link the resulting key to the destination keyring if we can */
635664cceb0SDavid Howells 	if (dest_ref) {
63629db9190SDavid Howells 		ret = key_permission(key_ref, KEY_LINK);
63729db9190SDavid Howells 		if (ret < 0)
6381da177e4SLinus Torvalds 			goto error6;
6391da177e4SLinus Torvalds 
640664cceb0SDavid Howells 		ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
6411da177e4SLinus Torvalds 		if (ret < 0)
6421da177e4SLinus Torvalds 			goto error6;
6431da177e4SLinus Torvalds 	}
6441da177e4SLinus Torvalds 
645664cceb0SDavid Howells 	ret = key_ref_to_ptr(key_ref)->serial;
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds error6:
648664cceb0SDavid Howells 	key_ref_put(key_ref);
6491da177e4SLinus Torvalds error5:
6501da177e4SLinus Torvalds 	key_type_put(ktype);
6511da177e4SLinus Torvalds error4:
652664cceb0SDavid Howells 	key_ref_put(dest_ref);
6531da177e4SLinus Torvalds error3:
654664cceb0SDavid Howells 	key_ref_put(keyring_ref);
6551da177e4SLinus Torvalds error2:
6561da177e4SLinus Torvalds 	kfree(description);
6571da177e4SLinus Torvalds error:
6581da177e4SLinus Torvalds 	return ret;
659a8b17ed0SDavid Howells }
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds /*
662973c9f4fSDavid Howells  * Read a key's payload.
663973c9f4fSDavid Howells  *
664973c9f4fSDavid Howells  * The key must either grant the caller Read permission, or it must grant the
665973c9f4fSDavid Howells  * caller Search permission when searched for from the process keyrings.
666973c9f4fSDavid Howells  *
667973c9f4fSDavid Howells  * If successful, we place up to buflen bytes of data into the buffer, if one
668973c9f4fSDavid Howells  * is provided, and return the amount of data that is available in the key,
669973c9f4fSDavid Howells  * irrespective of how much we copied into the buffer.
6701da177e4SLinus Torvalds  */
6711da177e4SLinus Torvalds long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
6721da177e4SLinus Torvalds {
673664cceb0SDavid Howells 	struct key *key;
674664cceb0SDavid Howells 	key_ref_t key_ref;
6751da177e4SLinus Torvalds 	long ret;
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 	/* find the key first */
6785593122eSDavid Howells 	key_ref = lookup_user_key(keyid, 0, 0);
679664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
680664cceb0SDavid Howells 		ret = -ENOKEY;
681664cceb0SDavid Howells 		goto error;
682664cceb0SDavid Howells 	}
683664cceb0SDavid Howells 
684664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
685664cceb0SDavid Howells 
6861da177e4SLinus Torvalds 	/* see if we can read it directly */
68729db9190SDavid Howells 	ret = key_permission(key_ref, KEY_READ);
68829db9190SDavid Howells 	if (ret == 0)
6891da177e4SLinus Torvalds 		goto can_read_key;
69029db9190SDavid Howells 	if (ret != -EACCES)
69129db9190SDavid Howells 		goto error;
6921da177e4SLinus Torvalds 
693664cceb0SDavid Howells 	/* we can't; see if it's searchable from this process's keyrings
6943e30148cSDavid Howells 	 * - we automatically take account of the fact that it may be
6953e30148cSDavid Howells 	 *   dangling off an instantiation key
6963e30148cSDavid Howells 	 */
697664cceb0SDavid Howells 	if (!is_key_possessed(key_ref)) {
6981260f801SDavid Howells 		ret = -EACCES;
6991da177e4SLinus Torvalds 		goto error2;
7001da177e4SLinus Torvalds 	}
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 	/* the key is probably readable - now try to read it */
7031da177e4SLinus Torvalds can_read_key:
7041da177e4SLinus Torvalds 	ret = key_validate(key);
7051da177e4SLinus Torvalds 	if (ret == 0) {
7061da177e4SLinus Torvalds 		ret = -EOPNOTSUPP;
7071da177e4SLinus Torvalds 		if (key->type->read) {
7081da177e4SLinus Torvalds 			/* read the data with the semaphore held (since we
7091da177e4SLinus Torvalds 			 * might sleep) */
7101da177e4SLinus Torvalds 			down_read(&key->sem);
7111da177e4SLinus Torvalds 			ret = key->type->read(key, buffer, buflen);
7121da177e4SLinus Torvalds 			up_read(&key->sem);
7131da177e4SLinus Torvalds 		}
7141da177e4SLinus Torvalds 	}
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds error2:
7171da177e4SLinus Torvalds 	key_put(key);
7181da177e4SLinus Torvalds error:
7191da177e4SLinus Torvalds 	return ret;
720a8b17ed0SDavid Howells }
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds /*
723973c9f4fSDavid Howells  * Change the ownership of a key
724973c9f4fSDavid Howells  *
725973c9f4fSDavid Howells  * The key must grant the caller Setattr permission for this to work, though
726973c9f4fSDavid Howells  * the key need not be fully instantiated yet.  For the UID to be changed, or
727973c9f4fSDavid Howells  * for the GID to be changed to a group the caller is not a member of, the
728973c9f4fSDavid Howells  * caller must have sysadmin capability.  If either uid or gid is -1 then that
729973c9f4fSDavid Howells  * attribute is not changed.
730973c9f4fSDavid Howells  *
731973c9f4fSDavid Howells  * If the UID is to be changed, the new user must have sufficient quota to
732973c9f4fSDavid Howells  * accept the key.  The quota deduction will be removed from the old user to
733973c9f4fSDavid Howells  * the new user should the attribute be changed.
734973c9f4fSDavid Howells  *
735973c9f4fSDavid Howells  * If successful, 0 will be returned.
7361da177e4SLinus Torvalds  */
7371da177e4SLinus Torvalds long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
7381da177e4SLinus Torvalds {
7395801649dSFredrik Tolf 	struct key_user *newowner, *zapowner = NULL;
7401da177e4SLinus Torvalds 	struct key *key;
741664cceb0SDavid Howells 	key_ref_t key_ref;
7421da177e4SLinus Torvalds 	long ret;
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	ret = 0;
7451da177e4SLinus Torvalds 	if (uid == (uid_t) -1 && gid == (gid_t) -1)
7461da177e4SLinus Torvalds 		goto error;
7471da177e4SLinus Torvalds 
7485593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
7495593122eSDavid Howells 				  KEY_SETATTR);
750664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
751664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
7521da177e4SLinus Torvalds 		goto error;
7531da177e4SLinus Torvalds 	}
7541da177e4SLinus Torvalds 
755664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
756664cceb0SDavid Howells 
7571da177e4SLinus Torvalds 	/* make the changes with the locks held to prevent chown/chown races */
7581da177e4SLinus Torvalds 	ret = -EACCES;
7591da177e4SLinus Torvalds 	down_write(&key->sem);
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN)) {
7621da177e4SLinus Torvalds 		/* only the sysadmin can chown a key to some other UID */
7631da177e4SLinus Torvalds 		if (uid != (uid_t) -1 && key->uid != uid)
7645801649dSFredrik Tolf 			goto error_put;
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 		/* only the sysadmin can set the key's GID to a group other
7671da177e4SLinus Torvalds 		 * than one of those that the current process subscribes to */
7681da177e4SLinus Torvalds 		if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
7695801649dSFredrik Tolf 			goto error_put;
7701da177e4SLinus Torvalds 	}
7711da177e4SLinus Torvalds 
7725801649dSFredrik Tolf 	/* change the UID */
7731da177e4SLinus Torvalds 	if (uid != (uid_t) -1 && uid != key->uid) {
7745801649dSFredrik Tolf 		ret = -ENOMEM;
7751d1e9756SSerge E. Hallyn 		newowner = key_user_lookup(uid, current_user_ns());
7765801649dSFredrik Tolf 		if (!newowner)
7775801649dSFredrik Tolf 			goto error_put;
7785801649dSFredrik Tolf 
7795801649dSFredrik Tolf 		/* transfer the quota burden to the new user */
7805801649dSFredrik Tolf 		if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
7810b77f5bfSDavid Howells 			unsigned maxkeys = (uid == 0) ?
7820b77f5bfSDavid Howells 				key_quota_root_maxkeys : key_quota_maxkeys;
7830b77f5bfSDavid Howells 			unsigned maxbytes = (uid == 0) ?
7840b77f5bfSDavid Howells 				key_quota_root_maxbytes : key_quota_maxbytes;
7850b77f5bfSDavid Howells 
7865801649dSFredrik Tolf 			spin_lock(&newowner->lock);
7870b77f5bfSDavid Howells 			if (newowner->qnkeys + 1 >= maxkeys ||
7880b77f5bfSDavid Howells 			    newowner->qnbytes + key->quotalen >= maxbytes ||
7890b77f5bfSDavid Howells 			    newowner->qnbytes + key->quotalen <
7900b77f5bfSDavid Howells 			    newowner->qnbytes)
7915801649dSFredrik Tolf 				goto quota_overrun;
7925801649dSFredrik Tolf 
7935801649dSFredrik Tolf 			newowner->qnkeys++;
7945801649dSFredrik Tolf 			newowner->qnbytes += key->quotalen;
7955801649dSFredrik Tolf 			spin_unlock(&newowner->lock);
7965801649dSFredrik Tolf 
7975801649dSFredrik Tolf 			spin_lock(&key->user->lock);
7985801649dSFredrik Tolf 			key->user->qnkeys--;
7995801649dSFredrik Tolf 			key->user->qnbytes -= key->quotalen;
8005801649dSFredrik Tolf 			spin_unlock(&key->user->lock);
8015801649dSFredrik Tolf 		}
8025801649dSFredrik Tolf 
8035801649dSFredrik Tolf 		atomic_dec(&key->user->nkeys);
8045801649dSFredrik Tolf 		atomic_inc(&newowner->nkeys);
8055801649dSFredrik Tolf 
8065801649dSFredrik Tolf 		if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
8075801649dSFredrik Tolf 			atomic_dec(&key->user->nikeys);
8085801649dSFredrik Tolf 			atomic_inc(&newowner->nikeys);
8095801649dSFredrik Tolf 		}
8105801649dSFredrik Tolf 
8115801649dSFredrik Tolf 		zapowner = key->user;
8125801649dSFredrik Tolf 		key->user = newowner;
8135801649dSFredrik Tolf 		key->uid = uid;
8141da177e4SLinus Torvalds 	}
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 	/* change the GID */
8171da177e4SLinus Torvalds 	if (gid != (gid_t) -1)
8181da177e4SLinus Torvalds 		key->gid = gid;
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 	ret = 0;
8211da177e4SLinus Torvalds 
8225801649dSFredrik Tolf error_put:
8231da177e4SLinus Torvalds 	up_write(&key->sem);
8241da177e4SLinus Torvalds 	key_put(key);
8255801649dSFredrik Tolf 	if (zapowner)
8265801649dSFredrik Tolf 		key_user_put(zapowner);
8271da177e4SLinus Torvalds error:
8281da177e4SLinus Torvalds 	return ret;
8291da177e4SLinus Torvalds 
8305801649dSFredrik Tolf quota_overrun:
8315801649dSFredrik Tolf 	spin_unlock(&newowner->lock);
8325801649dSFredrik Tolf 	zapowner = newowner;
8335801649dSFredrik Tolf 	ret = -EDQUOT;
8345801649dSFredrik Tolf 	goto error_put;
835a8b17ed0SDavid Howells }
8365801649dSFredrik Tolf 
8371da177e4SLinus Torvalds /*
838973c9f4fSDavid Howells  * Change the permission mask on a key.
839973c9f4fSDavid Howells  *
840973c9f4fSDavid Howells  * The key must grant the caller Setattr permission for this to work, though
841973c9f4fSDavid Howells  * the key need not be fully instantiated yet.  If the caller does not have
842973c9f4fSDavid Howells  * sysadmin capability, it may only change the permission on keys that it owns.
8431da177e4SLinus Torvalds  */
8441da177e4SLinus Torvalds long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
8451da177e4SLinus Torvalds {
8461da177e4SLinus Torvalds 	struct key *key;
847664cceb0SDavid Howells 	key_ref_t key_ref;
8481da177e4SLinus Torvalds 	long ret;
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 	ret = -EINVAL;
851664cceb0SDavid Howells 	if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
8521da177e4SLinus Torvalds 		goto error;
8531da177e4SLinus Torvalds 
8545593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
8555593122eSDavid Howells 				  KEY_SETATTR);
856664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
857664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
8581da177e4SLinus Torvalds 		goto error;
8591da177e4SLinus Torvalds 	}
8601da177e4SLinus Torvalds 
861664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
862664cceb0SDavid Howells 
86376d8aeabSDavid Howells 	/* make the changes with the locks held to prevent chown/chmod races */
8641da177e4SLinus Torvalds 	ret = -EACCES;
8651da177e4SLinus Torvalds 	down_write(&key->sem);
8661da177e4SLinus Torvalds 
86776d8aeabSDavid Howells 	/* if we're not the sysadmin, we can only change a key that we own */
86847d804bfSDavid Howells 	if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) {
8691da177e4SLinus Torvalds 		key->perm = perm;
8701da177e4SLinus Torvalds 		ret = 0;
87176d8aeabSDavid Howells 	}
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	up_write(&key->sem);
8741da177e4SLinus Torvalds 	key_put(key);
8751da177e4SLinus Torvalds error:
8761da177e4SLinus Torvalds 	return ret;
877a8b17ed0SDavid Howells }
8781da177e4SLinus Torvalds 
8798bbf4976SDavid Howells /*
880973c9f4fSDavid Howells  * Get the destination keyring for instantiation and check that the caller has
881973c9f4fSDavid Howells  * Write permission on it.
8828bbf4976SDavid Howells  */
8838bbf4976SDavid Howells static long get_instantiation_keyring(key_serial_t ringid,
8848bbf4976SDavid Howells 				      struct request_key_auth *rka,
8858bbf4976SDavid Howells 				      struct key **_dest_keyring)
8868bbf4976SDavid Howells {
8878bbf4976SDavid Howells 	key_ref_t dkref;
8888bbf4976SDavid Howells 
8898bbf4976SDavid Howells 	*_dest_keyring = NULL;
890eca1bf5bSDavid Howells 
891eca1bf5bSDavid Howells 	/* just return a NULL pointer if we weren't asked to make a link */
892eca1bf5bSDavid Howells 	if (ringid == 0)
8938bbf4976SDavid Howells 		return 0;
8948bbf4976SDavid Howells 
8958bbf4976SDavid Howells 	/* if a specific keyring is nominated by ID, then use that */
8968bbf4976SDavid Howells 	if (ringid > 0) {
8975593122eSDavid Howells 		dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
8988bbf4976SDavid Howells 		if (IS_ERR(dkref))
8998bbf4976SDavid Howells 			return PTR_ERR(dkref);
9008bbf4976SDavid Howells 		*_dest_keyring = key_ref_to_ptr(dkref);
9018bbf4976SDavid Howells 		return 0;
9028bbf4976SDavid Howells 	}
9038bbf4976SDavid Howells 
9048bbf4976SDavid Howells 	if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
9058bbf4976SDavid Howells 		return -EINVAL;
9068bbf4976SDavid Howells 
9078bbf4976SDavid Howells 	/* otherwise specify the destination keyring recorded in the
9088bbf4976SDavid Howells 	 * authorisation key (any KEY_SPEC_*_KEYRING) */
9098bbf4976SDavid Howells 	if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
91021279cfaSDavid Howells 		*_dest_keyring = key_get(rka->dest_keyring);
9118bbf4976SDavid Howells 		return 0;
9128bbf4976SDavid Howells 	}
9138bbf4976SDavid Howells 
9148bbf4976SDavid Howells 	return -ENOKEY;
9158bbf4976SDavid Howells }
9168bbf4976SDavid Howells 
917d84f4f99SDavid Howells /*
918973c9f4fSDavid Howells  * Change the request_key authorisation key on the current process.
919d84f4f99SDavid Howells  */
920d84f4f99SDavid Howells static int keyctl_change_reqkey_auth(struct key *key)
921d84f4f99SDavid Howells {
922d84f4f99SDavid Howells 	struct cred *new;
923d84f4f99SDavid Howells 
924d84f4f99SDavid Howells 	new = prepare_creds();
925d84f4f99SDavid Howells 	if (!new)
926d84f4f99SDavid Howells 		return -ENOMEM;
927d84f4f99SDavid Howells 
928d84f4f99SDavid Howells 	key_put(new->request_key_auth);
929d84f4f99SDavid Howells 	new->request_key_auth = key_get(key);
930d84f4f99SDavid Howells 
931d84f4f99SDavid Howells 	return commit_creds(new);
932d84f4f99SDavid Howells }
933d84f4f99SDavid Howells 
9341da177e4SLinus Torvalds /*
935ee009e4aSDavid Howells  * Copy the iovec data from userspace
936ee009e4aSDavid Howells  */
937ee009e4aSDavid Howells static long copy_from_user_iovec(void *buffer, const struct iovec *iov,
938ee009e4aSDavid Howells 				 unsigned ioc)
939ee009e4aSDavid Howells {
940ee009e4aSDavid Howells 	for (; ioc > 0; ioc--) {
941ee009e4aSDavid Howells 		if (copy_from_user(buffer, iov->iov_base, iov->iov_len) != 0)
942ee009e4aSDavid Howells 			return -EFAULT;
943ee009e4aSDavid Howells 		buffer += iov->iov_len;
944ee009e4aSDavid Howells 		iov++;
945ee009e4aSDavid Howells 	}
946ee009e4aSDavid Howells 	return 0;
947ee009e4aSDavid Howells }
948ee009e4aSDavid Howells 
949ee009e4aSDavid Howells /*
950973c9f4fSDavid Howells  * Instantiate a key with the specified payload and link the key into the
951973c9f4fSDavid Howells  * destination keyring if one is given.
952973c9f4fSDavid Howells  *
953973c9f4fSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
954973c9f4fSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
955973c9f4fSDavid Howells  *
956973c9f4fSDavid Howells  * If successful, 0 will be returned.
9571da177e4SLinus Torvalds  */
958ee009e4aSDavid Howells long keyctl_instantiate_key_common(key_serial_t id,
959ee009e4aSDavid Howells 				   const struct iovec *payload_iov,
960ee009e4aSDavid Howells 				   unsigned ioc,
9611da177e4SLinus Torvalds 				   size_t plen,
9621da177e4SLinus Torvalds 				   key_serial_t ringid)
9631da177e4SLinus Torvalds {
964d84f4f99SDavid Howells 	const struct cred *cred = current_cred();
9653e30148cSDavid Howells 	struct request_key_auth *rka;
9668bbf4976SDavid Howells 	struct key *instkey, *dest_keyring;
9671da177e4SLinus Torvalds 	void *payload;
9681da177e4SLinus Torvalds 	long ret;
96938bbca6bSDavid Howells 	bool vm = false;
9701da177e4SLinus Torvalds 
971d84f4f99SDavid Howells 	kenter("%d,,%zu,%d", id, plen, ringid);
972d84f4f99SDavid Howells 
9731da177e4SLinus Torvalds 	ret = -EINVAL;
97438bbca6bSDavid Howells 	if (plen > 1024 * 1024 - 1)
9751da177e4SLinus Torvalds 		goto error;
9761da177e4SLinus Torvalds 
977b5f545c8SDavid Howells 	/* the appropriate instantiation authorisation key must have been
978b5f545c8SDavid Howells 	 * assumed before calling this */
979b5f545c8SDavid Howells 	ret = -EPERM;
980d84f4f99SDavid Howells 	instkey = cred->request_key_auth;
981b5f545c8SDavid Howells 	if (!instkey)
982b5f545c8SDavid Howells 		goto error;
983b5f545c8SDavid Howells 
984b5f545c8SDavid Howells 	rka = instkey->payload.data;
985b5f545c8SDavid Howells 	if (rka->target_key->serial != id)
986b5f545c8SDavid Howells 		goto error;
987b5f545c8SDavid Howells 
9881da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
9891da177e4SLinus Torvalds 	payload = NULL;
9901da177e4SLinus Torvalds 
991ee009e4aSDavid Howells 	if (payload_iov) {
9921da177e4SLinus Torvalds 		ret = -ENOMEM;
9931da177e4SLinus Torvalds 		payload = kmalloc(plen, GFP_KERNEL);
99438bbca6bSDavid Howells 		if (!payload) {
99538bbca6bSDavid Howells 			if (plen <= PAGE_SIZE)
99638bbca6bSDavid Howells 				goto error;
99738bbca6bSDavid Howells 			vm = true;
99838bbca6bSDavid Howells 			payload = vmalloc(plen);
9991da177e4SLinus Torvalds 			if (!payload)
10001da177e4SLinus Torvalds 				goto error;
100138bbca6bSDavid Howells 		}
10021da177e4SLinus Torvalds 
1003ee009e4aSDavid Howells 		ret = copy_from_user_iovec(payload, payload_iov, ioc);
1004ee009e4aSDavid Howells 		if (ret < 0)
10051da177e4SLinus Torvalds 			goto error2;
10061da177e4SLinus Torvalds 	}
10071da177e4SLinus Torvalds 
10083e30148cSDavid Howells 	/* find the destination keyring amongst those belonging to the
10093e30148cSDavid Howells 	 * requesting task */
10108bbf4976SDavid Howells 	ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
10118bbf4976SDavid Howells 	if (ret < 0)
1012b5f545c8SDavid Howells 		goto error2;
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds 	/* instantiate the key and link it into a keyring */
10153e30148cSDavid Howells 	ret = key_instantiate_and_link(rka->target_key, payload, plen,
10168bbf4976SDavid Howells 				       dest_keyring, instkey);
10171da177e4SLinus Torvalds 
10188bbf4976SDavid Howells 	key_put(dest_keyring);
1019b5f545c8SDavid Howells 
1020b5f545c8SDavid Howells 	/* discard the assumed authority if it's just been disabled by
1021b5f545c8SDavid Howells 	 * instantiation of the key */
1022d84f4f99SDavid Howells 	if (ret == 0)
1023d84f4f99SDavid Howells 		keyctl_change_reqkey_auth(NULL);
1024b5f545c8SDavid Howells 
10251da177e4SLinus Torvalds error2:
102638bbca6bSDavid Howells 	if (!vm)
10271da177e4SLinus Torvalds 		kfree(payload);
102838bbca6bSDavid Howells 	else
102938bbca6bSDavid Howells 		vfree(payload);
10301da177e4SLinus Torvalds error:
10311da177e4SLinus Torvalds 	return ret;
1032a8b17ed0SDavid Howells }
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds /*
1035ee009e4aSDavid Howells  * Instantiate a key with the specified payload and link the key into the
1036ee009e4aSDavid Howells  * destination keyring if one is given.
1037ee009e4aSDavid Howells  *
1038ee009e4aSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1039ee009e4aSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1040ee009e4aSDavid Howells  *
1041ee009e4aSDavid Howells  * If successful, 0 will be returned.
1042ee009e4aSDavid Howells  */
1043ee009e4aSDavid Howells long keyctl_instantiate_key(key_serial_t id,
1044ee009e4aSDavid Howells 			    const void __user *_payload,
1045ee009e4aSDavid Howells 			    size_t plen,
1046ee009e4aSDavid Howells 			    key_serial_t ringid)
1047ee009e4aSDavid Howells {
1048ee009e4aSDavid Howells 	if (_payload && plen) {
1049ee009e4aSDavid Howells 		struct iovec iov[1] = {
1050ee009e4aSDavid Howells 			[0].iov_base = (void __user *)_payload,
1051ee009e4aSDavid Howells 			[0].iov_len  = plen
1052ee009e4aSDavid Howells 		};
1053ee009e4aSDavid Howells 
1054ee009e4aSDavid Howells 		return keyctl_instantiate_key_common(id, iov, 1, plen, ringid);
1055ee009e4aSDavid Howells 	}
1056ee009e4aSDavid Howells 
1057ee009e4aSDavid Howells 	return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1058ee009e4aSDavid Howells }
1059ee009e4aSDavid Howells 
1060ee009e4aSDavid Howells /*
1061ee009e4aSDavid Howells  * Instantiate a key with the specified multipart payload and link the key into
1062ee009e4aSDavid Howells  * the destination keyring if one is given.
1063ee009e4aSDavid Howells  *
1064ee009e4aSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1065ee009e4aSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1066ee009e4aSDavid Howells  *
1067ee009e4aSDavid Howells  * If successful, 0 will be returned.
1068ee009e4aSDavid Howells  */
1069ee009e4aSDavid Howells long keyctl_instantiate_key_iov(key_serial_t id,
1070ee009e4aSDavid Howells 				const struct iovec __user *_payload_iov,
1071ee009e4aSDavid Howells 				unsigned ioc,
1072ee009e4aSDavid Howells 				key_serial_t ringid)
1073ee009e4aSDavid Howells {
1074ee009e4aSDavid Howells 	struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1075ee009e4aSDavid Howells 	long ret;
1076ee009e4aSDavid Howells 
1077ee009e4aSDavid Howells 	if (_payload_iov == 0 || ioc == 0)
1078ee009e4aSDavid Howells 		goto no_payload;
1079ee009e4aSDavid Howells 
1080ee009e4aSDavid Howells 	ret = rw_copy_check_uvector(WRITE, _payload_iov, ioc,
1081fcf63409SChristopher Yeoh 				    ARRAY_SIZE(iovstack), iovstack, &iov, 1);
1082ee009e4aSDavid Howells 	if (ret < 0)
1083ee009e4aSDavid Howells 		return ret;
1084ee009e4aSDavid Howells 	if (ret == 0)
1085ee009e4aSDavid Howells 		goto no_payload_free;
1086ee009e4aSDavid Howells 
1087ee009e4aSDavid Howells 	ret = keyctl_instantiate_key_common(id, iov, ioc, ret, ringid);
1088ee009e4aSDavid Howells 
1089ee009e4aSDavid Howells 	if (iov != iovstack)
1090ee009e4aSDavid Howells 		kfree(iov);
1091ee009e4aSDavid Howells 	return ret;
1092ee009e4aSDavid Howells 
1093ee009e4aSDavid Howells no_payload_free:
1094ee009e4aSDavid Howells 	if (iov != iovstack)
1095ee009e4aSDavid Howells 		kfree(iov);
1096ee009e4aSDavid Howells no_payload:
1097ee009e4aSDavid Howells 	return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1098ee009e4aSDavid Howells }
1099ee009e4aSDavid Howells 
1100ee009e4aSDavid Howells /*
1101973c9f4fSDavid Howells  * Negatively instantiate the key with the given timeout (in seconds) and link
1102973c9f4fSDavid Howells  * the key into the destination keyring if one is given.
1103973c9f4fSDavid Howells  *
1104973c9f4fSDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1105973c9f4fSDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1106973c9f4fSDavid Howells  *
1107973c9f4fSDavid Howells  * The key and any links to the key will be automatically garbage collected
1108973c9f4fSDavid Howells  * after the timeout expires.
1109973c9f4fSDavid Howells  *
1110973c9f4fSDavid Howells  * Negative keys are used to rate limit repeated request_key() calls by causing
1111973c9f4fSDavid Howells  * them to return -ENOKEY until the negative key expires.
1112973c9f4fSDavid Howells  *
1113973c9f4fSDavid Howells  * If successful, 0 will be returned.
11141da177e4SLinus Torvalds  */
11151da177e4SLinus Torvalds long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
11161da177e4SLinus Torvalds {
1117fdd1b945SDavid Howells 	return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1118fdd1b945SDavid Howells }
1119fdd1b945SDavid Howells 
1120fdd1b945SDavid Howells /*
1121fdd1b945SDavid Howells  * Negatively instantiate the key with the given timeout (in seconds) and error
1122fdd1b945SDavid Howells  * code and link the key into the destination keyring if one is given.
1123fdd1b945SDavid Howells  *
1124fdd1b945SDavid Howells  * The caller must have the appropriate instantiation permit set for this to
1125fdd1b945SDavid Howells  * work (see keyctl_assume_authority).  No other permissions are required.
1126fdd1b945SDavid Howells  *
1127fdd1b945SDavid Howells  * The key and any links to the key will be automatically garbage collected
1128fdd1b945SDavid Howells  * after the timeout expires.
1129fdd1b945SDavid Howells  *
1130fdd1b945SDavid Howells  * Negative keys are used to rate limit repeated request_key() calls by causing
1131fdd1b945SDavid Howells  * them to return the specified error code until the negative key expires.
1132fdd1b945SDavid Howells  *
1133fdd1b945SDavid Howells  * If successful, 0 will be returned.
1134fdd1b945SDavid Howells  */
1135fdd1b945SDavid Howells long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1136fdd1b945SDavid Howells 		       key_serial_t ringid)
1137fdd1b945SDavid Howells {
1138d84f4f99SDavid Howells 	const struct cred *cred = current_cred();
11393e30148cSDavid Howells 	struct request_key_auth *rka;
11408bbf4976SDavid Howells 	struct key *instkey, *dest_keyring;
11411da177e4SLinus Torvalds 	long ret;
11421da177e4SLinus Torvalds 
1143fdd1b945SDavid Howells 	kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1144fdd1b945SDavid Howells 
1145fdd1b945SDavid Howells 	/* must be a valid error code and mustn't be a kernel special */
1146fdd1b945SDavid Howells 	if (error <= 0 ||
1147fdd1b945SDavid Howells 	    error >= MAX_ERRNO ||
1148fdd1b945SDavid Howells 	    error == ERESTARTSYS ||
1149fdd1b945SDavid Howells 	    error == ERESTARTNOINTR ||
1150fdd1b945SDavid Howells 	    error == ERESTARTNOHAND ||
1151fdd1b945SDavid Howells 	    error == ERESTART_RESTARTBLOCK)
1152fdd1b945SDavid Howells 		return -EINVAL;
1153d84f4f99SDavid Howells 
1154b5f545c8SDavid Howells 	/* the appropriate instantiation authorisation key must have been
1155b5f545c8SDavid Howells 	 * assumed before calling this */
1156b5f545c8SDavid Howells 	ret = -EPERM;
1157d84f4f99SDavid Howells 	instkey = cred->request_key_auth;
1158b5f545c8SDavid Howells 	if (!instkey)
11591da177e4SLinus Torvalds 		goto error;
11601da177e4SLinus Torvalds 
11613e30148cSDavid Howells 	rka = instkey->payload.data;
1162b5f545c8SDavid Howells 	if (rka->target_key->serial != id)
1163b5f545c8SDavid Howells 		goto error;
11643e30148cSDavid Howells 
11651da177e4SLinus Torvalds 	/* find the destination keyring if present (which must also be
11661da177e4SLinus Torvalds 	 * writable) */
11678bbf4976SDavid Howells 	ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
11688bbf4976SDavid Howells 	if (ret < 0)
1169b5f545c8SDavid Howells 		goto error;
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 	/* instantiate the key and link it into a keyring */
1172fdd1b945SDavid Howells 	ret = key_reject_and_link(rka->target_key, timeout, error,
11738bbf4976SDavid Howells 				  dest_keyring, instkey);
11741da177e4SLinus Torvalds 
11758bbf4976SDavid Howells 	key_put(dest_keyring);
1176b5f545c8SDavid Howells 
1177b5f545c8SDavid Howells 	/* discard the assumed authority if it's just been disabled by
1178b5f545c8SDavid Howells 	 * instantiation of the key */
1179d84f4f99SDavid Howells 	if (ret == 0)
1180d84f4f99SDavid Howells 		keyctl_change_reqkey_auth(NULL);
1181b5f545c8SDavid Howells 
11821da177e4SLinus Torvalds error:
11831da177e4SLinus Torvalds 	return ret;
1184a8b17ed0SDavid Howells }
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds /*
1187973c9f4fSDavid Howells  * Read or set the default keyring in which request_key() will cache keys and
1188973c9f4fSDavid Howells  * return the old setting.
1189973c9f4fSDavid Howells  *
1190973c9f4fSDavid Howells  * If a process keyring is specified then this will be created if it doesn't
1191973c9f4fSDavid Howells  * yet exist.  The old setting will be returned if successful.
11923e30148cSDavid Howells  */
11933e30148cSDavid Howells long keyctl_set_reqkey_keyring(int reqkey_defl)
11943e30148cSDavid Howells {
1195d84f4f99SDavid Howells 	struct cred *new;
1196d84f4f99SDavid Howells 	int ret, old_setting;
1197d84f4f99SDavid Howells 
1198d84f4f99SDavid Howells 	old_setting = current_cred_xxx(jit_keyring);
1199d84f4f99SDavid Howells 
1200d84f4f99SDavid Howells 	if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1201d84f4f99SDavid Howells 		return old_setting;
1202d84f4f99SDavid Howells 
1203d84f4f99SDavid Howells 	new = prepare_creds();
1204d84f4f99SDavid Howells 	if (!new)
1205d84f4f99SDavid Howells 		return -ENOMEM;
12063e30148cSDavid Howells 
12073e30148cSDavid Howells 	switch (reqkey_defl) {
12083e30148cSDavid Howells 	case KEY_REQKEY_DEFL_THREAD_KEYRING:
1209d84f4f99SDavid Howells 		ret = install_thread_keyring_to_cred(new);
12103e30148cSDavid Howells 		if (ret < 0)
1211d84f4f99SDavid Howells 			goto error;
12123e30148cSDavid Howells 		goto set;
12133e30148cSDavid Howells 
12143e30148cSDavid Howells 	case KEY_REQKEY_DEFL_PROCESS_KEYRING:
1215d84f4f99SDavid Howells 		ret = install_process_keyring_to_cred(new);
1216d84f4f99SDavid Howells 		if (ret < 0) {
1217d84f4f99SDavid Howells 			if (ret != -EEXIST)
1218d84f4f99SDavid Howells 				goto error;
1219d84f4f99SDavid Howells 			ret = 0;
1220d84f4f99SDavid Howells 		}
1221d84f4f99SDavid Howells 		goto set;
12223e30148cSDavid Howells 
12233e30148cSDavid Howells 	case KEY_REQKEY_DEFL_DEFAULT:
12243e30148cSDavid Howells 	case KEY_REQKEY_DEFL_SESSION_KEYRING:
12253e30148cSDavid Howells 	case KEY_REQKEY_DEFL_USER_KEYRING:
12263e30148cSDavid Howells 	case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1227d84f4f99SDavid Howells 	case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1228d84f4f99SDavid Howells 		goto set;
12293e30148cSDavid Howells 
12303e30148cSDavid Howells 	case KEY_REQKEY_DEFL_NO_CHANGE:
12313e30148cSDavid Howells 	case KEY_REQKEY_DEFL_GROUP_KEYRING:
12323e30148cSDavid Howells 	default:
1233d84f4f99SDavid Howells 		ret = -EINVAL;
1234d84f4f99SDavid Howells 		goto error;
12353e30148cSDavid Howells 	}
12363e30148cSDavid Howells 
1237d84f4f99SDavid Howells set:
1238d84f4f99SDavid Howells 	new->jit_keyring = reqkey_defl;
1239d84f4f99SDavid Howells 	commit_creds(new);
1240d84f4f99SDavid Howells 	return old_setting;
1241d84f4f99SDavid Howells error:
1242d84f4f99SDavid Howells 	abort_creds(new);
12434303ef19SDan Carpenter 	return ret;
1244a8b17ed0SDavid Howells }
1245d84f4f99SDavid Howells 
12463e30148cSDavid Howells /*
1247973c9f4fSDavid Howells  * Set or clear the timeout on a key.
1248973c9f4fSDavid Howells  *
1249973c9f4fSDavid Howells  * Either the key must grant the caller Setattr permission or else the caller
1250973c9f4fSDavid Howells  * must hold an instantiation authorisation token for the key.
1251973c9f4fSDavid Howells  *
1252973c9f4fSDavid Howells  * The timeout is either 0 to clear the timeout, or a number of seconds from
1253973c9f4fSDavid Howells  * the current time.  The key and any links to the key will be automatically
1254973c9f4fSDavid Howells  * garbage collected after the timeout expires.
1255973c9f4fSDavid Howells  *
1256973c9f4fSDavid Howells  * If successful, 0 is returned.
1257017679c4SDavid Howells  */
1258017679c4SDavid Howells long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1259017679c4SDavid Howells {
1260017679c4SDavid Howells 	struct timespec now;
12619156235bSDavid Howells 	struct key *key, *instkey;
1262017679c4SDavid Howells 	key_ref_t key_ref;
1263017679c4SDavid Howells 	time_t expiry;
1264017679c4SDavid Howells 	long ret;
1265017679c4SDavid Howells 
12665593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
12675593122eSDavid Howells 				  KEY_SETATTR);
1268017679c4SDavid Howells 	if (IS_ERR(key_ref)) {
12699156235bSDavid Howells 		/* setting the timeout on a key under construction is permitted
12709156235bSDavid Howells 		 * if we have the authorisation token handy */
12719156235bSDavid Howells 		if (PTR_ERR(key_ref) == -EACCES) {
12729156235bSDavid Howells 			instkey = key_get_instantiation_authkey(id);
12739156235bSDavid Howells 			if (!IS_ERR(instkey)) {
12749156235bSDavid Howells 				key_put(instkey);
12759156235bSDavid Howells 				key_ref = lookup_user_key(id,
12769156235bSDavid Howells 							  KEY_LOOKUP_PARTIAL,
12779156235bSDavid Howells 							  0);
12789156235bSDavid Howells 				if (!IS_ERR(key_ref))
12799156235bSDavid Howells 					goto okay;
12809156235bSDavid Howells 			}
12819156235bSDavid Howells 		}
12829156235bSDavid Howells 
1283017679c4SDavid Howells 		ret = PTR_ERR(key_ref);
1284017679c4SDavid Howells 		goto error;
1285017679c4SDavid Howells 	}
1286017679c4SDavid Howells 
12879156235bSDavid Howells okay:
1288017679c4SDavid Howells 	key = key_ref_to_ptr(key_ref);
1289017679c4SDavid Howells 
1290017679c4SDavid Howells 	/* make the changes with the locks held to prevent races */
1291017679c4SDavid Howells 	down_write(&key->sem);
1292017679c4SDavid Howells 
1293017679c4SDavid Howells 	expiry = 0;
1294017679c4SDavid Howells 	if (timeout > 0) {
1295017679c4SDavid Howells 		now = current_kernel_time();
1296017679c4SDavid Howells 		expiry = now.tv_sec + timeout;
1297017679c4SDavid Howells 	}
1298017679c4SDavid Howells 
1299017679c4SDavid Howells 	key->expiry = expiry;
1300c08ef808SDavid Howells 	key_schedule_gc(key->expiry + key_gc_delay);
1301017679c4SDavid Howells 
1302017679c4SDavid Howells 	up_write(&key->sem);
1303017679c4SDavid Howells 	key_put(key);
1304017679c4SDavid Howells 
1305017679c4SDavid Howells 	ret = 0;
1306017679c4SDavid Howells error:
1307017679c4SDavid Howells 	return ret;
1308a8b17ed0SDavid Howells }
1309017679c4SDavid Howells 
1310017679c4SDavid Howells /*
1311973c9f4fSDavid Howells  * Assume (or clear) the authority to instantiate the specified key.
1312973c9f4fSDavid Howells  *
1313973c9f4fSDavid Howells  * This sets the authoritative token currently in force for key instantiation.
1314973c9f4fSDavid Howells  * This must be done for a key to be instantiated.  It has the effect of making
1315973c9f4fSDavid Howells  * available all the keys from the caller of the request_key() that created a
1316973c9f4fSDavid Howells  * key to request_key() calls made by the caller of this function.
1317973c9f4fSDavid Howells  *
1318973c9f4fSDavid Howells  * The caller must have the instantiation key in their process keyrings with a
1319973c9f4fSDavid Howells  * Search permission grant available to the caller.
1320973c9f4fSDavid Howells  *
1321973c9f4fSDavid Howells  * If the ID given is 0, then the setting will be cleared and 0 returned.
1322973c9f4fSDavid Howells  *
1323973c9f4fSDavid Howells  * If the ID given has a matching an authorisation key, then that key will be
1324973c9f4fSDavid Howells  * set and its ID will be returned.  The authorisation key can be read to get
1325973c9f4fSDavid Howells  * the callout information passed to request_key().
1326b5f545c8SDavid Howells  */
1327b5f545c8SDavid Howells long keyctl_assume_authority(key_serial_t id)
1328b5f545c8SDavid Howells {
1329b5f545c8SDavid Howells 	struct key *authkey;
1330b5f545c8SDavid Howells 	long ret;
1331b5f545c8SDavid Howells 
1332b5f545c8SDavid Howells 	/* special key IDs aren't permitted */
1333b5f545c8SDavid Howells 	ret = -EINVAL;
1334b5f545c8SDavid Howells 	if (id < 0)
1335b5f545c8SDavid Howells 		goto error;
1336b5f545c8SDavid Howells 
1337b5f545c8SDavid Howells 	/* we divest ourselves of authority if given an ID of 0 */
1338b5f545c8SDavid Howells 	if (id == 0) {
1339d84f4f99SDavid Howells 		ret = keyctl_change_reqkey_auth(NULL);
1340b5f545c8SDavid Howells 		goto error;
1341b5f545c8SDavid Howells 	}
1342b5f545c8SDavid Howells 
1343b5f545c8SDavid Howells 	/* attempt to assume the authority temporarily granted to us whilst we
1344b5f545c8SDavid Howells 	 * instantiate the specified key
1345b5f545c8SDavid Howells 	 * - the authorisation key must be in the current task's keyrings
1346b5f545c8SDavid Howells 	 *   somewhere
1347b5f545c8SDavid Howells 	 */
1348b5f545c8SDavid Howells 	authkey = key_get_instantiation_authkey(id);
1349b5f545c8SDavid Howells 	if (IS_ERR(authkey)) {
1350b5f545c8SDavid Howells 		ret = PTR_ERR(authkey);
1351b5f545c8SDavid Howells 		goto error;
1352b5f545c8SDavid Howells 	}
1353b5f545c8SDavid Howells 
1354d84f4f99SDavid Howells 	ret = keyctl_change_reqkey_auth(authkey);
1355d84f4f99SDavid Howells 	if (ret < 0)
1356d84f4f99SDavid Howells 		goto error;
1357d84f4f99SDavid Howells 	key_put(authkey);
1358b5f545c8SDavid Howells 
1359d84f4f99SDavid Howells 	ret = authkey->serial;
1360b5f545c8SDavid Howells error:
1361b5f545c8SDavid Howells 	return ret;
1362a8b17ed0SDavid Howells }
1363b5f545c8SDavid Howells 
136470a5bb72SDavid Howells /*
1365973c9f4fSDavid Howells  * Get a key's the LSM security label.
1366973c9f4fSDavid Howells  *
1367973c9f4fSDavid Howells  * The key must grant the caller View permission for this to work.
1368973c9f4fSDavid Howells  *
1369973c9f4fSDavid Howells  * If there's a buffer, then up to buflen bytes of data will be placed into it.
1370973c9f4fSDavid Howells  *
1371973c9f4fSDavid Howells  * If successful, the amount of information available will be returned,
1372973c9f4fSDavid Howells  * irrespective of how much was copied (including the terminal NUL).
137370a5bb72SDavid Howells  */
137470a5bb72SDavid Howells long keyctl_get_security(key_serial_t keyid,
137570a5bb72SDavid Howells 			 char __user *buffer,
137670a5bb72SDavid Howells 			 size_t buflen)
137770a5bb72SDavid Howells {
137870a5bb72SDavid Howells 	struct key *key, *instkey;
137970a5bb72SDavid Howells 	key_ref_t key_ref;
138070a5bb72SDavid Howells 	char *context;
138170a5bb72SDavid Howells 	long ret;
138270a5bb72SDavid Howells 
13835593122eSDavid Howells 	key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
138470a5bb72SDavid Howells 	if (IS_ERR(key_ref)) {
138570a5bb72SDavid Howells 		if (PTR_ERR(key_ref) != -EACCES)
138670a5bb72SDavid Howells 			return PTR_ERR(key_ref);
138770a5bb72SDavid Howells 
138870a5bb72SDavid Howells 		/* viewing a key under construction is also permitted if we
138970a5bb72SDavid Howells 		 * have the authorisation token handy */
139070a5bb72SDavid Howells 		instkey = key_get_instantiation_authkey(keyid);
139170a5bb72SDavid Howells 		if (IS_ERR(instkey))
1392fa1cc7b5SRoel Kluin 			return PTR_ERR(instkey);
139370a5bb72SDavid Howells 		key_put(instkey);
139470a5bb72SDavid Howells 
13955593122eSDavid Howells 		key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
139670a5bb72SDavid Howells 		if (IS_ERR(key_ref))
139770a5bb72SDavid Howells 			return PTR_ERR(key_ref);
139870a5bb72SDavid Howells 	}
139970a5bb72SDavid Howells 
140070a5bb72SDavid Howells 	key = key_ref_to_ptr(key_ref);
140170a5bb72SDavid Howells 	ret = security_key_getsecurity(key, &context);
140270a5bb72SDavid Howells 	if (ret == 0) {
140370a5bb72SDavid Howells 		/* if no information was returned, give userspace an empty
140470a5bb72SDavid Howells 		 * string */
140570a5bb72SDavid Howells 		ret = 1;
140670a5bb72SDavid Howells 		if (buffer && buflen > 0 &&
140770a5bb72SDavid Howells 		    copy_to_user(buffer, "", 1) != 0)
140870a5bb72SDavid Howells 			ret = -EFAULT;
140970a5bb72SDavid Howells 	} else if (ret > 0) {
141070a5bb72SDavid Howells 		/* return as much data as there's room for */
141170a5bb72SDavid Howells 		if (buffer && buflen > 0) {
141270a5bb72SDavid Howells 			if (buflen > ret)
141370a5bb72SDavid Howells 				buflen = ret;
141470a5bb72SDavid Howells 
141570a5bb72SDavid Howells 			if (copy_to_user(buffer, context, buflen) != 0)
141670a5bb72SDavid Howells 				ret = -EFAULT;
141770a5bb72SDavid Howells 		}
141870a5bb72SDavid Howells 
141970a5bb72SDavid Howells 		kfree(context);
142070a5bb72SDavid Howells 	}
142170a5bb72SDavid Howells 
142270a5bb72SDavid Howells 	key_ref_put(key_ref);
142370a5bb72SDavid Howells 	return ret;
142470a5bb72SDavid Howells }
142570a5bb72SDavid Howells 
1426ee18d64cSDavid Howells /*
1427973c9f4fSDavid Howells  * Attempt to install the calling process's session keyring on the process's
1428973c9f4fSDavid Howells  * parent process.
1429973c9f4fSDavid Howells  *
1430973c9f4fSDavid Howells  * The keyring must exist and must grant the caller LINK permission, and the
1431973c9f4fSDavid Howells  * parent process must be single-threaded and must have the same effective
1432973c9f4fSDavid Howells  * ownership as this process and mustn't be SUID/SGID.
1433973c9f4fSDavid Howells  *
1434973c9f4fSDavid Howells  * The keyring will be emplaced on the parent when it next resumes userspace.
1435973c9f4fSDavid Howells  *
1436973c9f4fSDavid Howells  * If successful, 0 will be returned.
1437ee18d64cSDavid Howells  */
1438ee18d64cSDavid Howells long keyctl_session_to_parent(void)
1439ee18d64cSDavid Howells {
1440a00ae4d2SGeert Uytterhoeven #ifdef TIF_NOTIFY_RESUME
1441ee18d64cSDavid Howells 	struct task_struct *me, *parent;
1442ee18d64cSDavid Howells 	const struct cred *mycred, *pcred;
1443ee18d64cSDavid Howells 	struct cred *cred, *oldcred;
1444ee18d64cSDavid Howells 	key_ref_t keyring_r;
1445ee18d64cSDavid Howells 	int ret;
1446ee18d64cSDavid Howells 
1447ee18d64cSDavid Howells 	keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_LINK);
1448ee18d64cSDavid Howells 	if (IS_ERR(keyring_r))
1449ee18d64cSDavid Howells 		return PTR_ERR(keyring_r);
1450ee18d64cSDavid Howells 
1451ee18d64cSDavid Howells 	/* our parent is going to need a new cred struct, a new tgcred struct
1452ee18d64cSDavid Howells 	 * and new security data, so we allocate them here to prevent ENOMEM in
1453ee18d64cSDavid Howells 	 * our parent */
1454ee18d64cSDavid Howells 	ret = -ENOMEM;
1455ee18d64cSDavid Howells 	cred = cred_alloc_blank();
1456ee18d64cSDavid Howells 	if (!cred)
1457ee18d64cSDavid Howells 		goto error_keyring;
1458ee18d64cSDavid Howells 
1459ee18d64cSDavid Howells 	cred->tgcred->session_keyring = key_ref_to_ptr(keyring_r);
1460ee18d64cSDavid Howells 	keyring_r = NULL;
1461ee18d64cSDavid Howells 
1462ee18d64cSDavid Howells 	me = current;
14639d1ac65aSDavid Howells 	rcu_read_lock();
1464ee18d64cSDavid Howells 	write_lock_irq(&tasklist_lock);
1465ee18d64cSDavid Howells 
1466ee18d64cSDavid Howells 	parent = me->real_parent;
1467ee18d64cSDavid Howells 	ret = -EPERM;
1468ee18d64cSDavid Howells 
1469ee18d64cSDavid Howells 	/* the parent mustn't be init and mustn't be a kernel thread */
1470ee18d64cSDavid Howells 	if (parent->pid <= 1 || !parent->mm)
1471ee18d64cSDavid Howells 		goto not_permitted;
1472ee18d64cSDavid Howells 
1473ee18d64cSDavid Howells 	/* the parent must be single threaded */
1474dd98acf7SOleg Nesterov 	if (!thread_group_empty(parent))
1475ee18d64cSDavid Howells 		goto not_permitted;
1476ee18d64cSDavid Howells 
1477ee18d64cSDavid Howells 	/* the parent and the child must have different session keyrings or
1478ee18d64cSDavid Howells 	 * there's no point */
1479ee18d64cSDavid Howells 	mycred = current_cred();
1480ee18d64cSDavid Howells 	pcred = __task_cred(parent);
1481ee18d64cSDavid Howells 	if (mycred == pcred ||
1482ee18d64cSDavid Howells 	    mycred->tgcred->session_keyring == pcred->tgcred->session_keyring)
1483ee18d64cSDavid Howells 		goto already_same;
1484ee18d64cSDavid Howells 
1485ee18d64cSDavid Howells 	/* the parent must have the same effective ownership and mustn't be
1486ee18d64cSDavid Howells 	 * SUID/SGID */
1487ee18d64cSDavid Howells 	if (pcred->uid	!= mycred->euid	||
1488ee18d64cSDavid Howells 	    pcred->euid	!= mycred->euid	||
1489ee18d64cSDavid Howells 	    pcred->suid	!= mycred->euid	||
1490ee18d64cSDavid Howells 	    pcred->gid	!= mycred->egid	||
1491ee18d64cSDavid Howells 	    pcred->egid	!= mycred->egid	||
1492ee18d64cSDavid Howells 	    pcred->sgid	!= mycred->egid)
1493ee18d64cSDavid Howells 		goto not_permitted;
1494ee18d64cSDavid Howells 
1495ee18d64cSDavid Howells 	/* the keyrings must have the same UID */
14963d96406cSDavid Howells 	if ((pcred->tgcred->session_keyring &&
14973d96406cSDavid Howells 	     pcred->tgcred->session_keyring->uid != mycred->euid) ||
1498ee18d64cSDavid Howells 	    mycred->tgcred->session_keyring->uid != mycred->euid)
1499ee18d64cSDavid Howells 		goto not_permitted;
1500ee18d64cSDavid Howells 
1501ee18d64cSDavid Howells 	/* if there's an already pending keyring replacement, then we replace
1502ee18d64cSDavid Howells 	 * that */
1503ee18d64cSDavid Howells 	oldcred = parent->replacement_session_keyring;
1504ee18d64cSDavid Howells 
1505ee18d64cSDavid Howells 	/* the replacement session keyring is applied just prior to userspace
1506ee18d64cSDavid Howells 	 * restarting */
1507ee18d64cSDavid Howells 	parent->replacement_session_keyring = cred;
1508ee18d64cSDavid Howells 	cred = NULL;
1509ee18d64cSDavid Howells 	set_ti_thread_flag(task_thread_info(parent), TIF_NOTIFY_RESUME);
1510ee18d64cSDavid Howells 
1511ee18d64cSDavid Howells 	write_unlock_irq(&tasklist_lock);
15129d1ac65aSDavid Howells 	rcu_read_unlock();
1513ee18d64cSDavid Howells 	if (oldcred)
1514ee18d64cSDavid Howells 		put_cred(oldcred);
1515ee18d64cSDavid Howells 	return 0;
1516ee18d64cSDavid Howells 
1517ee18d64cSDavid Howells already_same:
1518ee18d64cSDavid Howells 	ret = 0;
1519ee18d64cSDavid Howells not_permitted:
15205c84342aSMarc Dionne 	write_unlock_irq(&tasklist_lock);
15219d1ac65aSDavid Howells 	rcu_read_unlock();
1522ee18d64cSDavid Howells 	put_cred(cred);
1523ee18d64cSDavid Howells 	return ret;
1524ee18d64cSDavid Howells 
1525ee18d64cSDavid Howells error_keyring:
1526ee18d64cSDavid Howells 	key_ref_put(keyring_r);
1527ee18d64cSDavid Howells 	return ret;
1528a00ae4d2SGeert Uytterhoeven 
1529a00ae4d2SGeert Uytterhoeven #else /* !TIF_NOTIFY_RESUME */
1530a00ae4d2SGeert Uytterhoeven 	/*
1531a00ae4d2SGeert Uytterhoeven 	 * To be removed when TIF_NOTIFY_RESUME has been implemented on
1532a00ae4d2SGeert Uytterhoeven 	 * m68k/xtensa
1533a00ae4d2SGeert Uytterhoeven 	 */
1534a00ae4d2SGeert Uytterhoeven #warning TIF_NOTIFY_RESUME not implemented
1535a00ae4d2SGeert Uytterhoeven 	return -EOPNOTSUPP;
1536a00ae4d2SGeert Uytterhoeven #endif /* !TIF_NOTIFY_RESUME */
1537ee18d64cSDavid Howells }
1538ee18d64cSDavid Howells 
1539b5f545c8SDavid Howells /*
1540973c9f4fSDavid Howells  * The key control system call
15411da177e4SLinus Torvalds  */
1542938bb9f5SHeiko Carstens SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1543938bb9f5SHeiko Carstens 		unsigned long, arg4, unsigned long, arg5)
15441da177e4SLinus Torvalds {
15451da177e4SLinus Torvalds 	switch (option) {
15461da177e4SLinus Torvalds 	case KEYCTL_GET_KEYRING_ID:
15471da177e4SLinus Torvalds 		return keyctl_get_keyring_ID((key_serial_t) arg2,
15481da177e4SLinus Torvalds 					     (int) arg3);
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds 	case KEYCTL_JOIN_SESSION_KEYRING:
15511da177e4SLinus Torvalds 		return keyctl_join_session_keyring((const char __user *) arg2);
15521da177e4SLinus Torvalds 
15531da177e4SLinus Torvalds 	case KEYCTL_UPDATE:
15541da177e4SLinus Torvalds 		return keyctl_update_key((key_serial_t) arg2,
15551da177e4SLinus Torvalds 					 (const void __user *) arg3,
15561da177e4SLinus Torvalds 					 (size_t) arg4);
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds 	case KEYCTL_REVOKE:
15591da177e4SLinus Torvalds 		return keyctl_revoke_key((key_serial_t) arg2);
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds 	case KEYCTL_DESCRIBE:
15621da177e4SLinus Torvalds 		return keyctl_describe_key((key_serial_t) arg2,
15631da177e4SLinus Torvalds 					   (char __user *) arg3,
15641da177e4SLinus Torvalds 					   (unsigned) arg4);
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds 	case KEYCTL_CLEAR:
15671da177e4SLinus Torvalds 		return keyctl_keyring_clear((key_serial_t) arg2);
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds 	case KEYCTL_LINK:
15701da177e4SLinus Torvalds 		return keyctl_keyring_link((key_serial_t) arg2,
15711da177e4SLinus Torvalds 					   (key_serial_t) arg3);
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds 	case KEYCTL_UNLINK:
15741da177e4SLinus Torvalds 		return keyctl_keyring_unlink((key_serial_t) arg2,
15751da177e4SLinus Torvalds 					     (key_serial_t) arg3);
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds 	case KEYCTL_SEARCH:
15781da177e4SLinus Torvalds 		return keyctl_keyring_search((key_serial_t) arg2,
15791da177e4SLinus Torvalds 					     (const char __user *) arg3,
15801da177e4SLinus Torvalds 					     (const char __user *) arg4,
15811da177e4SLinus Torvalds 					     (key_serial_t) arg5);
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds 	case KEYCTL_READ:
15841da177e4SLinus Torvalds 		return keyctl_read_key((key_serial_t) arg2,
15851da177e4SLinus Torvalds 				       (char __user *) arg3,
15861da177e4SLinus Torvalds 				       (size_t) arg4);
15871da177e4SLinus Torvalds 
15881da177e4SLinus Torvalds 	case KEYCTL_CHOWN:
15891da177e4SLinus Torvalds 		return keyctl_chown_key((key_serial_t) arg2,
15901da177e4SLinus Torvalds 					(uid_t) arg3,
15911da177e4SLinus Torvalds 					(gid_t) arg4);
15921da177e4SLinus Torvalds 
15931da177e4SLinus Torvalds 	case KEYCTL_SETPERM:
15941da177e4SLinus Torvalds 		return keyctl_setperm_key((key_serial_t) arg2,
15951da177e4SLinus Torvalds 					  (key_perm_t) arg3);
15961da177e4SLinus Torvalds 
15971da177e4SLinus Torvalds 	case KEYCTL_INSTANTIATE:
15981da177e4SLinus Torvalds 		return keyctl_instantiate_key((key_serial_t) arg2,
15991da177e4SLinus Torvalds 					      (const void __user *) arg3,
16001da177e4SLinus Torvalds 					      (size_t) arg4,
16011da177e4SLinus Torvalds 					      (key_serial_t) arg5);
16021da177e4SLinus Torvalds 
16031da177e4SLinus Torvalds 	case KEYCTL_NEGATE:
16041da177e4SLinus Torvalds 		return keyctl_negate_key((key_serial_t) arg2,
16051da177e4SLinus Torvalds 					 (unsigned) arg3,
16061da177e4SLinus Torvalds 					 (key_serial_t) arg4);
16071da177e4SLinus Torvalds 
16083e30148cSDavid Howells 	case KEYCTL_SET_REQKEY_KEYRING:
16093e30148cSDavid Howells 		return keyctl_set_reqkey_keyring(arg2);
16103e30148cSDavid Howells 
1611017679c4SDavid Howells 	case KEYCTL_SET_TIMEOUT:
1612017679c4SDavid Howells 		return keyctl_set_timeout((key_serial_t) arg2,
1613017679c4SDavid Howells 					  (unsigned) arg3);
1614017679c4SDavid Howells 
1615b5f545c8SDavid Howells 	case KEYCTL_ASSUME_AUTHORITY:
1616b5f545c8SDavid Howells 		return keyctl_assume_authority((key_serial_t) arg2);
1617b5f545c8SDavid Howells 
161870a5bb72SDavid Howells 	case KEYCTL_GET_SECURITY:
161970a5bb72SDavid Howells 		return keyctl_get_security((key_serial_t) arg2,
162090bd49abSJames Morris 					   (char __user *) arg3,
162170a5bb72SDavid Howells 					   (size_t) arg4);
162270a5bb72SDavid Howells 
1623ee18d64cSDavid Howells 	case KEYCTL_SESSION_TO_PARENT:
1624ee18d64cSDavid Howells 		return keyctl_session_to_parent();
1625ee18d64cSDavid Howells 
1626fdd1b945SDavid Howells 	case KEYCTL_REJECT:
1627fdd1b945SDavid Howells 		return keyctl_reject_key((key_serial_t) arg2,
1628fdd1b945SDavid Howells 					 (unsigned) arg3,
1629fdd1b945SDavid Howells 					 (unsigned) arg4,
1630fdd1b945SDavid Howells 					 (key_serial_t) arg5);
1631fdd1b945SDavid Howells 
1632ee009e4aSDavid Howells 	case KEYCTL_INSTANTIATE_IOV:
1633ee009e4aSDavid Howells 		return keyctl_instantiate_key_iov(
1634ee009e4aSDavid Howells 			(key_serial_t) arg2,
1635ee009e4aSDavid Howells 			(const struct iovec __user *) arg3,
1636ee009e4aSDavid Howells 			(unsigned) arg4,
1637ee009e4aSDavid Howells 			(key_serial_t) arg5);
1638ee009e4aSDavid Howells 
16391da177e4SLinus Torvalds 	default:
16401da177e4SLinus Torvalds 		return -EOPNOTSUPP;
16411da177e4SLinus Torvalds 	}
1642a8b17ed0SDavid Howells }
1643