xref: /openbmc/linux/security/keys/keyctl.c (revision 5593122e)
11da177e4SLinus Torvalds /* keyctl.c: userspace keyctl 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 
350cb409d9SDavi Arnaut 	if (ret < 0)
360cb409d9SDavi Arnaut 		return -EFAULT;
370cb409d9SDavi Arnaut 
380cb409d9SDavi Arnaut 	if (ret == 0 || ret >= len)
390cb409d9SDavi Arnaut 		return -EINVAL;
400cb409d9SDavi Arnaut 
410cb409d9SDavi Arnaut 	if (type[0] == '.')
420cb409d9SDavi Arnaut 		return -EPERM;
430cb409d9SDavi Arnaut 
440cb409d9SDavi Arnaut 	type[len - 1] = '\0';
450cb409d9SDavi Arnaut 
460cb409d9SDavi Arnaut 	return 0;
470cb409d9SDavi Arnaut }
480cb409d9SDavi Arnaut 
491da177e4SLinus Torvalds /*****************************************************************************/
501da177e4SLinus Torvalds /*
511da177e4SLinus Torvalds  * extract the description of a new key from userspace and either add it as a
521da177e4SLinus Torvalds  * new key to the specified keyring or update a matching key in that keyring
531da177e4SLinus Torvalds  * - the keyring must be writable
541da177e4SLinus Torvalds  * - returns the new key's serial number
551da177e4SLinus Torvalds  * - implements add_key()
561da177e4SLinus Torvalds  */
571e7bfb21SHeiko Carstens SYSCALL_DEFINE5(add_key, const char __user *, _type,
581e7bfb21SHeiko Carstens 		const char __user *, _description,
591e7bfb21SHeiko Carstens 		const void __user *, _payload,
601e7bfb21SHeiko Carstens 		size_t, plen,
611e7bfb21SHeiko Carstens 		key_serial_t, ringid)
621da177e4SLinus Torvalds {
63664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref;
641da177e4SLinus Torvalds 	char type[32], *description;
651da177e4SLinus Torvalds 	void *payload;
660cb409d9SDavi Arnaut 	long ret;
6738bbca6bSDavid Howells 	bool vm;
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 	ret = -EINVAL;
7038bbca6bSDavid Howells 	if (plen > 1024 * 1024 - 1)
711da177e4SLinus Torvalds 		goto error;
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 	/* draw all the data into kernel space */
740cb409d9SDavi Arnaut 	ret = key_get_type_from_user(type, _type, sizeof(type));
751da177e4SLinus Torvalds 	if (ret < 0)
761da177e4SLinus Torvalds 		goto error;
771da177e4SLinus Torvalds 
780cb409d9SDavi Arnaut 	description = strndup_user(_description, PAGE_SIZE);
790cb409d9SDavi Arnaut 	if (IS_ERR(description)) {
800cb409d9SDavi Arnaut 		ret = PTR_ERR(description);
813e30148cSDavid Howells 		goto error;
820cb409d9SDavi Arnaut 	}
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
851da177e4SLinus Torvalds 	payload = NULL;
861da177e4SLinus Torvalds 
8738bbca6bSDavid Howells 	vm = false;
881da177e4SLinus Torvalds 	if (_payload) {
891da177e4SLinus Torvalds 		ret = -ENOMEM;
901da177e4SLinus Torvalds 		payload = kmalloc(plen, GFP_KERNEL);
9138bbca6bSDavid Howells 		if (!payload) {
9238bbca6bSDavid Howells 			if (plen <= PAGE_SIZE)
9338bbca6bSDavid Howells 				goto error2;
9438bbca6bSDavid Howells 			vm = true;
9538bbca6bSDavid Howells 			payload = vmalloc(plen);
961da177e4SLinus Torvalds 			if (!payload)
971da177e4SLinus Torvalds 				goto error2;
9838bbca6bSDavid Howells 		}
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 		ret = -EFAULT;
1011da177e4SLinus Torvalds 		if (copy_from_user(payload, _payload, plen) != 0)
1021da177e4SLinus Torvalds 			goto error3;
1031da177e4SLinus Torvalds 	}
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	/* find the target keyring (which must be writable) */
106*5593122eSDavid Howells 	keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
107664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
108664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
1091da177e4SLinus Torvalds 		goto error3;
1101da177e4SLinus Torvalds 	}
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 	/* create or update the requested key and add it to the target
1131da177e4SLinus Torvalds 	 * keyring */
114664cceb0SDavid Howells 	key_ref = key_create_or_update(keyring_ref, type, description,
1156b79ccb5SArun Raghavan 				       payload, plen, KEY_PERM_UNDEF,
1166b79ccb5SArun Raghavan 				       KEY_ALLOC_IN_QUOTA);
117664cceb0SDavid Howells 	if (!IS_ERR(key_ref)) {
118664cceb0SDavid Howells 		ret = key_ref_to_ptr(key_ref)->serial;
119664cceb0SDavid Howells 		key_ref_put(key_ref);
1201da177e4SLinus Torvalds 	}
1211da177e4SLinus Torvalds 	else {
122664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
1231da177e4SLinus Torvalds 	}
1241da177e4SLinus Torvalds 
125664cceb0SDavid Howells 	key_ref_put(keyring_ref);
1261da177e4SLinus Torvalds  error3:
12738bbca6bSDavid Howells 	if (!vm)
1281da177e4SLinus Torvalds 		kfree(payload);
12938bbca6bSDavid Howells 	else
13038bbca6bSDavid Howells 		vfree(payload);
1311da177e4SLinus Torvalds  error2:
1321da177e4SLinus Torvalds 	kfree(description);
1331da177e4SLinus Torvalds  error:
1341da177e4SLinus Torvalds 	return ret;
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds } /* end sys_add_key() */
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds /*****************************************************************************/
1391da177e4SLinus Torvalds /*
1401da177e4SLinus Torvalds  * search the process keyrings for a matching key
1411da177e4SLinus Torvalds  * - nested keyrings may also be searched if they have Search permission
1421da177e4SLinus Torvalds  * - if a key is found, it will be attached to the destination keyring if
1431da177e4SLinus Torvalds  *   there's one specified
1441da177e4SLinus Torvalds  * - /sbin/request-key will be invoked if _callout_info is non-NULL
1451da177e4SLinus Torvalds  *   - the _callout_info string will be passed to /sbin/request-key
1461da177e4SLinus Torvalds  *   - if the _callout_info string is empty, it will be rendered as "-"
1471da177e4SLinus Torvalds  * - implements request_key()
1481da177e4SLinus Torvalds  */
1491e7bfb21SHeiko Carstens SYSCALL_DEFINE4(request_key, const char __user *, _type,
1501e7bfb21SHeiko Carstens 		const char __user *, _description,
1511e7bfb21SHeiko Carstens 		const char __user *, _callout_info,
1521e7bfb21SHeiko Carstens 		key_serial_t, destringid)
1531da177e4SLinus Torvalds {
1541da177e4SLinus Torvalds 	struct key_type *ktype;
155664cceb0SDavid Howells 	struct key *key;
156664cceb0SDavid Howells 	key_ref_t dest_ref;
1574a38e122SDavid Howells 	size_t callout_len;
1581da177e4SLinus Torvalds 	char type[32], *description, *callout_info;
1590cb409d9SDavi Arnaut 	long ret;
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	/* pull the type into kernel space */
1620cb409d9SDavi Arnaut 	ret = key_get_type_from_user(type, _type, sizeof(type));
1631da177e4SLinus Torvalds 	if (ret < 0)
1641da177e4SLinus Torvalds 		goto error;
1651260f801SDavid Howells 
1661da177e4SLinus Torvalds 	/* pull the description into kernel space */
1670cb409d9SDavi Arnaut 	description = strndup_user(_description, PAGE_SIZE);
1680cb409d9SDavi Arnaut 	if (IS_ERR(description)) {
1690cb409d9SDavi Arnaut 		ret = PTR_ERR(description);
1701da177e4SLinus Torvalds 		goto error;
1710cb409d9SDavi Arnaut 	}
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	/* pull the callout info into kernel space */
1741da177e4SLinus Torvalds 	callout_info = NULL;
1754a38e122SDavid Howells 	callout_len = 0;
1761da177e4SLinus Torvalds 	if (_callout_info) {
1770cb409d9SDavi Arnaut 		callout_info = strndup_user(_callout_info, PAGE_SIZE);
1780cb409d9SDavi Arnaut 		if (IS_ERR(callout_info)) {
1790cb409d9SDavi Arnaut 			ret = PTR_ERR(callout_info);
1801da177e4SLinus Torvalds 			goto error2;
1810cb409d9SDavi Arnaut 		}
1824a38e122SDavid Howells 		callout_len = strlen(callout_info);
1831da177e4SLinus Torvalds 	}
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	/* get the destination keyring if specified */
186664cceb0SDavid Howells 	dest_ref = NULL;
1871da177e4SLinus Torvalds 	if (destringid) {
188*5593122eSDavid Howells 		dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
189*5593122eSDavid Howells 					   KEY_WRITE);
190664cceb0SDavid Howells 		if (IS_ERR(dest_ref)) {
191664cceb0SDavid Howells 			ret = PTR_ERR(dest_ref);
1921da177e4SLinus Torvalds 			goto error3;
1931da177e4SLinus Torvalds 		}
1941da177e4SLinus Torvalds 	}
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	/* find the key type */
1971da177e4SLinus Torvalds 	ktype = key_type_lookup(type);
1981da177e4SLinus Torvalds 	if (IS_ERR(ktype)) {
1991da177e4SLinus Torvalds 		ret = PTR_ERR(ktype);
2001da177e4SLinus Torvalds 		goto error4;
2011da177e4SLinus Torvalds 	}
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 	/* do the search */
2044a38e122SDavid Howells 	key = request_key_and_link(ktype, description, callout_info,
2054a38e122SDavid Howells 				   callout_len, NULL, key_ref_to_ptr(dest_ref),
2067e047ef5SDavid Howells 				   KEY_ALLOC_IN_QUOTA);
2071da177e4SLinus Torvalds 	if (IS_ERR(key)) {
2081da177e4SLinus Torvalds 		ret = PTR_ERR(key);
2091da177e4SLinus Torvalds 		goto error5;
2101da177e4SLinus Torvalds 	}
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	ret = key->serial;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds  	key_put(key);
2151da177e4SLinus Torvalds  error5:
2161da177e4SLinus Torvalds 	key_type_put(ktype);
2171da177e4SLinus Torvalds  error4:
218664cceb0SDavid Howells 	key_ref_put(dest_ref);
2191da177e4SLinus Torvalds  error3:
2201da177e4SLinus Torvalds 	kfree(callout_info);
2211da177e4SLinus Torvalds  error2:
2221da177e4SLinus Torvalds 	kfree(description);
2231da177e4SLinus Torvalds  error:
2241da177e4SLinus Torvalds 	return ret;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds } /* end sys_request_key() */
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds /*****************************************************************************/
2291da177e4SLinus Torvalds /*
2301da177e4SLinus Torvalds  * get the ID of the specified process keyring
2311da177e4SLinus Torvalds  * - the keyring must have search permission to be found
2321da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_GET_KEYRING_ID)
2331da177e4SLinus Torvalds  */
2341da177e4SLinus Torvalds long keyctl_get_keyring_ID(key_serial_t id, int create)
2351da177e4SLinus Torvalds {
236664cceb0SDavid Howells 	key_ref_t key_ref;
237*5593122eSDavid Howells 	unsigned long lflags;
2381da177e4SLinus Torvalds 	long ret;
2391da177e4SLinus Torvalds 
240*5593122eSDavid Howells 	lflags = create ? KEY_LOOKUP_CREATE : 0;
241*5593122eSDavid Howells 	key_ref = lookup_user_key(id, lflags, KEY_SEARCH);
242664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
243664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
2441da177e4SLinus Torvalds 		goto error;
2451da177e4SLinus Torvalds 	}
2461da177e4SLinus Torvalds 
247664cceb0SDavid Howells 	ret = key_ref_to_ptr(key_ref)->serial;
248664cceb0SDavid Howells 	key_ref_put(key_ref);
2491da177e4SLinus Torvalds  error:
2501da177e4SLinus Torvalds 	return ret;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds } /* end keyctl_get_keyring_ID() */
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds /*****************************************************************************/
2551da177e4SLinus Torvalds /*
2561da177e4SLinus Torvalds  * join the session keyring
2571da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING)
2581da177e4SLinus Torvalds  */
2591da177e4SLinus Torvalds long keyctl_join_session_keyring(const char __user *_name)
2601da177e4SLinus Torvalds {
2611da177e4SLinus Torvalds 	char *name;
2620cb409d9SDavi Arnaut 	long ret;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	/* fetch the name from userspace */
2651da177e4SLinus Torvalds 	name = NULL;
2661da177e4SLinus Torvalds 	if (_name) {
2670cb409d9SDavi Arnaut 		name = strndup_user(_name, PAGE_SIZE);
2680cb409d9SDavi Arnaut 		if (IS_ERR(name)) {
2690cb409d9SDavi Arnaut 			ret = PTR_ERR(name);
2701da177e4SLinus Torvalds 			goto error;
2710cb409d9SDavi Arnaut 		}
2721da177e4SLinus Torvalds 	}
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	/* join the session */
2751da177e4SLinus Torvalds 	ret = join_session_keyring(name);
2760d54ee1cSVegard Nossum 	kfree(name);
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds  error:
2791da177e4SLinus Torvalds 	return ret;
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds } /* end keyctl_join_session_keyring() */
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds /*****************************************************************************/
2841da177e4SLinus Torvalds /*
2851da177e4SLinus Torvalds  * update a key's data payload
2861da177e4SLinus Torvalds  * - the key must be writable
2871da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_UPDATE)
2881da177e4SLinus Torvalds  */
2891da177e4SLinus Torvalds long keyctl_update_key(key_serial_t id,
2901da177e4SLinus Torvalds 		       const void __user *_payload,
2911da177e4SLinus Torvalds 		       size_t plen)
2921da177e4SLinus Torvalds {
293664cceb0SDavid Howells 	key_ref_t key_ref;
2941da177e4SLinus Torvalds 	void *payload;
2951da177e4SLinus Torvalds 	long ret;
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	ret = -EINVAL;
2981da177e4SLinus Torvalds 	if (plen > PAGE_SIZE)
2991da177e4SLinus Torvalds 		goto error;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
3021da177e4SLinus Torvalds 	payload = NULL;
3031da177e4SLinus Torvalds 	if (_payload) {
3041da177e4SLinus Torvalds 		ret = -ENOMEM;
3051da177e4SLinus Torvalds 		payload = kmalloc(plen, GFP_KERNEL);
3061da177e4SLinus Torvalds 		if (!payload)
3071da177e4SLinus Torvalds 			goto error;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 		ret = -EFAULT;
3101da177e4SLinus Torvalds 		if (copy_from_user(payload, _payload, plen) != 0)
3111da177e4SLinus Torvalds 			goto error2;
3121da177e4SLinus Torvalds 	}
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	/* find the target key (which must be writable) */
315*5593122eSDavid Howells 	key_ref = lookup_user_key(id, 0, KEY_WRITE);
316664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
317664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
3181da177e4SLinus Torvalds 		goto error2;
3191da177e4SLinus Torvalds 	}
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	/* update the key */
322664cceb0SDavid Howells 	ret = key_update(key_ref, payload, plen);
3231da177e4SLinus Torvalds 
324664cceb0SDavid Howells 	key_ref_put(key_ref);
3251da177e4SLinus Torvalds  error2:
3261da177e4SLinus Torvalds 	kfree(payload);
3271da177e4SLinus Torvalds  error:
3281da177e4SLinus Torvalds 	return ret;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds } /* end keyctl_update_key() */
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds /*****************************************************************************/
3331da177e4SLinus Torvalds /*
3341da177e4SLinus Torvalds  * revoke a key
3351da177e4SLinus Torvalds  * - the key must be writable
3361da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_REVOKE)
3371da177e4SLinus Torvalds  */
3381da177e4SLinus Torvalds long keyctl_revoke_key(key_serial_t id)
3391da177e4SLinus Torvalds {
340664cceb0SDavid Howells 	key_ref_t key_ref;
3411da177e4SLinus Torvalds 	long ret;
3421da177e4SLinus Torvalds 
343*5593122eSDavid Howells 	key_ref = lookup_user_key(id, 0, KEY_WRITE);
344664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
345664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
3461da177e4SLinus Torvalds 		goto error;
3471da177e4SLinus Torvalds 	}
3481da177e4SLinus Torvalds 
349664cceb0SDavid Howells 	key_revoke(key_ref_to_ptr(key_ref));
3501da177e4SLinus Torvalds 	ret = 0;
3511da177e4SLinus Torvalds 
352664cceb0SDavid Howells 	key_ref_put(key_ref);
3531da177e4SLinus Torvalds  error:
3541260f801SDavid Howells 	return ret;
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds } /* end keyctl_revoke_key() */
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds /*****************************************************************************/
3591da177e4SLinus Torvalds /*
3601da177e4SLinus Torvalds  * clear the specified process keyring
3611da177e4SLinus Torvalds  * - the keyring must be writable
3621da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_CLEAR)
3631da177e4SLinus Torvalds  */
3641da177e4SLinus Torvalds long keyctl_keyring_clear(key_serial_t ringid)
3651da177e4SLinus Torvalds {
366664cceb0SDavid Howells 	key_ref_t keyring_ref;
3671da177e4SLinus Torvalds 	long ret;
3681da177e4SLinus Torvalds 
369*5593122eSDavid Howells 	keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
370664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
371664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
3721da177e4SLinus Torvalds 		goto error;
3731da177e4SLinus Torvalds 	}
3741da177e4SLinus Torvalds 
375664cceb0SDavid Howells 	ret = keyring_clear(key_ref_to_ptr(keyring_ref));
3761da177e4SLinus Torvalds 
377664cceb0SDavid Howells 	key_ref_put(keyring_ref);
3781da177e4SLinus Torvalds  error:
3791da177e4SLinus Torvalds 	return ret;
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds } /* end keyctl_keyring_clear() */
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds /*****************************************************************************/
3841da177e4SLinus Torvalds /*
3851da177e4SLinus Torvalds  * link a key into a keyring
3861da177e4SLinus Torvalds  * - the keyring must be writable
3871da177e4SLinus Torvalds  * - the key must be linkable
3881da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_LINK)
3891da177e4SLinus Torvalds  */
3901da177e4SLinus Torvalds long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
3911da177e4SLinus Torvalds {
392664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref;
3931da177e4SLinus Torvalds 	long ret;
3941da177e4SLinus Torvalds 
395*5593122eSDavid Howells 	keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
396664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
397664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
3981da177e4SLinus Torvalds 		goto error;
3991da177e4SLinus Torvalds 	}
4001da177e4SLinus Torvalds 
401*5593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_LINK);
402664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
403664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
4041da177e4SLinus Torvalds 		goto error2;
4051da177e4SLinus Torvalds 	}
4061da177e4SLinus Torvalds 
407664cceb0SDavid Howells 	ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
4081da177e4SLinus Torvalds 
409664cceb0SDavid Howells 	key_ref_put(key_ref);
4101da177e4SLinus Torvalds  error2:
411664cceb0SDavid Howells 	key_ref_put(keyring_ref);
4121da177e4SLinus Torvalds  error:
4131da177e4SLinus Torvalds 	return ret;
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds } /* end keyctl_keyring_link() */
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds /*****************************************************************************/
4181da177e4SLinus Torvalds /*
4191da177e4SLinus Torvalds  * unlink the first attachment of a key from a keyring
4201da177e4SLinus Torvalds  * - the keyring must be writable
4211da177e4SLinus Torvalds  * - we don't need any permissions on the key
4221da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_UNLINK)
4231da177e4SLinus Torvalds  */
4241da177e4SLinus Torvalds long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
4251da177e4SLinus Torvalds {
426664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref;
4271da177e4SLinus Torvalds 	long ret;
4281da177e4SLinus Torvalds 
429*5593122eSDavid Howells 	keyring_ref = lookup_user_key(ringid, 0, KEY_WRITE);
430664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
431664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
4321da177e4SLinus Torvalds 		goto error;
4331da177e4SLinus Torvalds 	}
4341da177e4SLinus Torvalds 
435*5593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
436664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
437664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
4381da177e4SLinus Torvalds 		goto error2;
4391da177e4SLinus Torvalds 	}
4401da177e4SLinus Torvalds 
441664cceb0SDavid Howells 	ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
4421da177e4SLinus Torvalds 
443664cceb0SDavid Howells 	key_ref_put(key_ref);
4441da177e4SLinus Torvalds  error2:
445664cceb0SDavid Howells 	key_ref_put(keyring_ref);
4461da177e4SLinus Torvalds  error:
4471da177e4SLinus Torvalds 	return ret;
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds } /* end keyctl_keyring_unlink() */
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds /*****************************************************************************/
4521da177e4SLinus Torvalds /*
4531da177e4SLinus Torvalds  * describe a user key
4541da177e4SLinus Torvalds  * - the key must have view permission
4551da177e4SLinus Torvalds  * - if there's a buffer, we place up to buflen bytes of data into it
4561da177e4SLinus Torvalds  * - unless there's an error, we return the amount of description available,
4571da177e4SLinus Torvalds  *   irrespective of how much we may have copied
4581da177e4SLinus Torvalds  * - the description is formatted thus:
4591da177e4SLinus Torvalds  *	type;uid;gid;perm;description<NUL>
4601da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_DESCRIBE)
4611da177e4SLinus Torvalds  */
4621da177e4SLinus Torvalds long keyctl_describe_key(key_serial_t keyid,
4631da177e4SLinus Torvalds 			 char __user *buffer,
4641da177e4SLinus Torvalds 			 size_t buflen)
4651da177e4SLinus Torvalds {
4663e30148cSDavid Howells 	struct key *key, *instkey;
467664cceb0SDavid Howells 	key_ref_t key_ref;
4681da177e4SLinus Torvalds 	char *tmpbuf;
4691da177e4SLinus Torvalds 	long ret;
4701da177e4SLinus Torvalds 
471*5593122eSDavid Howells 	key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
472664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
4733e30148cSDavid Howells 		/* viewing a key under construction is permitted if we have the
4743e30148cSDavid Howells 		 * authorisation token handy */
475664cceb0SDavid Howells 		if (PTR_ERR(key_ref) == -EACCES) {
4763e30148cSDavid Howells 			instkey = key_get_instantiation_authkey(keyid);
4773e30148cSDavid Howells 			if (!IS_ERR(instkey)) {
4783e30148cSDavid Howells 				key_put(instkey);
4798bbf4976SDavid Howells 				key_ref = lookup_user_key(keyid,
480*5593122eSDavid Howells 							  KEY_LOOKUP_PARTIAL,
481*5593122eSDavid Howells 							  0);
482664cceb0SDavid Howells 				if (!IS_ERR(key_ref))
4833e30148cSDavid Howells 					goto okay;
4843e30148cSDavid Howells 			}
4853e30148cSDavid Howells 		}
4863e30148cSDavid Howells 
487664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
4881da177e4SLinus Torvalds 		goto error;
4891da177e4SLinus Torvalds 	}
4901da177e4SLinus Torvalds 
4913e30148cSDavid Howells okay:
4921da177e4SLinus Torvalds 	/* calculate how much description we're going to return */
4931da177e4SLinus Torvalds 	ret = -ENOMEM;
4941da177e4SLinus Torvalds 	tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4951da177e4SLinus Torvalds 	if (!tmpbuf)
4961da177e4SLinus Torvalds 		goto error2;
4971da177e4SLinus Torvalds 
498664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
499664cceb0SDavid Howells 
5001da177e4SLinus Torvalds 	ret = snprintf(tmpbuf, PAGE_SIZE - 1,
501664cceb0SDavid Howells 		       "%s;%d;%d;%08x;%s",
502664cceb0SDavid Howells 		       key_ref_to_ptr(key_ref)->type->name,
503664cceb0SDavid Howells 		       key_ref_to_ptr(key_ref)->uid,
504664cceb0SDavid Howells 		       key_ref_to_ptr(key_ref)->gid,
505664cceb0SDavid Howells 		       key_ref_to_ptr(key_ref)->perm,
506664cceb0SDavid Howells 		       key_ref_to_ptr(key_ref)->description ?
507664cceb0SDavid Howells 		       key_ref_to_ptr(key_ref)->description : ""
5081da177e4SLinus Torvalds 		       );
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	/* include a NUL char at the end of the data */
5111da177e4SLinus Torvalds 	if (ret > PAGE_SIZE - 1)
5121da177e4SLinus Torvalds 		ret = PAGE_SIZE - 1;
5131da177e4SLinus Torvalds 	tmpbuf[ret] = 0;
5141da177e4SLinus Torvalds 	ret++;
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	/* consider returning the data */
5171da177e4SLinus Torvalds 	if (buffer && buflen > 0) {
5181da177e4SLinus Torvalds 		if (buflen > ret)
5191da177e4SLinus Torvalds 			buflen = ret;
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 		if (copy_to_user(buffer, tmpbuf, buflen) != 0)
5221da177e4SLinus Torvalds 			ret = -EFAULT;
5231da177e4SLinus Torvalds 	}
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 	kfree(tmpbuf);
5261da177e4SLinus Torvalds  error2:
527664cceb0SDavid Howells 	key_ref_put(key_ref);
5281da177e4SLinus Torvalds  error:
5291da177e4SLinus Torvalds 	return ret;
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds } /* end keyctl_describe_key() */
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds /*****************************************************************************/
5341da177e4SLinus Torvalds /*
5351da177e4SLinus Torvalds  * search the specified keyring for a matching key
5361da177e4SLinus Torvalds  * - the start keyring must be searchable
5371da177e4SLinus Torvalds  * - nested keyrings may also be searched if they are searchable
5381da177e4SLinus Torvalds  * - only keys with search permission may be found
5391da177e4SLinus Torvalds  * - if a key is found, it will be attached to the destination keyring if
5401da177e4SLinus Torvalds  *   there's one specified
5411da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_SEARCH)
5421da177e4SLinus Torvalds  */
5431da177e4SLinus Torvalds long keyctl_keyring_search(key_serial_t ringid,
5441da177e4SLinus Torvalds 			   const char __user *_type,
5451da177e4SLinus Torvalds 			   const char __user *_description,
5461da177e4SLinus Torvalds 			   key_serial_t destringid)
5471da177e4SLinus Torvalds {
5481da177e4SLinus Torvalds 	struct key_type *ktype;
549664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref, dest_ref;
5501da177e4SLinus Torvalds 	char type[32], *description;
5510cb409d9SDavi Arnaut 	long ret;
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	/* pull the type and description into kernel space */
5540cb409d9SDavi Arnaut 	ret = key_get_type_from_user(type, _type, sizeof(type));
5551da177e4SLinus Torvalds 	if (ret < 0)
5561da177e4SLinus Torvalds 		goto error;
5571da177e4SLinus Torvalds 
5580cb409d9SDavi Arnaut 	description = strndup_user(_description, PAGE_SIZE);
5590cb409d9SDavi Arnaut 	if (IS_ERR(description)) {
5600cb409d9SDavi Arnaut 		ret = PTR_ERR(description);
5611da177e4SLinus Torvalds 		goto error;
5620cb409d9SDavi Arnaut 	}
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	/* get the keyring at which to begin the search */
565*5593122eSDavid Howells 	keyring_ref = lookup_user_key(ringid, 0, KEY_SEARCH);
566664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
567664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
5681da177e4SLinus Torvalds 		goto error2;
5691da177e4SLinus Torvalds 	}
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	/* get the destination keyring if specified */
572664cceb0SDavid Howells 	dest_ref = NULL;
5731da177e4SLinus Torvalds 	if (destringid) {
574*5593122eSDavid Howells 		dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
575*5593122eSDavid Howells 					   KEY_WRITE);
576664cceb0SDavid Howells 		if (IS_ERR(dest_ref)) {
577664cceb0SDavid Howells 			ret = PTR_ERR(dest_ref);
5781da177e4SLinus Torvalds 			goto error3;
5791da177e4SLinus Torvalds 		}
5801da177e4SLinus Torvalds 	}
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	/* find the key type */
5831da177e4SLinus Torvalds 	ktype = key_type_lookup(type);
5841da177e4SLinus Torvalds 	if (IS_ERR(ktype)) {
5851da177e4SLinus Torvalds 		ret = PTR_ERR(ktype);
5861da177e4SLinus Torvalds 		goto error4;
5871da177e4SLinus Torvalds 	}
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	/* do the search */
590664cceb0SDavid Howells 	key_ref = keyring_search(keyring_ref, ktype, description);
591664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
592664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 		/* treat lack or presence of a negative key the same */
5951da177e4SLinus Torvalds 		if (ret == -EAGAIN)
5961da177e4SLinus Torvalds 			ret = -ENOKEY;
5971da177e4SLinus Torvalds 		goto error5;
5981da177e4SLinus Torvalds 	}
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 	/* link the resulting key to the destination keyring if we can */
601664cceb0SDavid Howells 	if (dest_ref) {
60229db9190SDavid Howells 		ret = key_permission(key_ref, KEY_LINK);
60329db9190SDavid Howells 		if (ret < 0)
6041da177e4SLinus Torvalds 			goto error6;
6051da177e4SLinus Torvalds 
606664cceb0SDavid Howells 		ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
6071da177e4SLinus Torvalds 		if (ret < 0)
6081da177e4SLinus Torvalds 			goto error6;
6091da177e4SLinus Torvalds 	}
6101da177e4SLinus Torvalds 
611664cceb0SDavid Howells 	ret = key_ref_to_ptr(key_ref)->serial;
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds  error6:
614664cceb0SDavid Howells 	key_ref_put(key_ref);
6151da177e4SLinus Torvalds  error5:
6161da177e4SLinus Torvalds 	key_type_put(ktype);
6171da177e4SLinus Torvalds  error4:
618664cceb0SDavid Howells 	key_ref_put(dest_ref);
6191da177e4SLinus Torvalds  error3:
620664cceb0SDavid Howells 	key_ref_put(keyring_ref);
6211da177e4SLinus Torvalds  error2:
6221da177e4SLinus Torvalds 	kfree(description);
6231da177e4SLinus Torvalds  error:
6241da177e4SLinus Torvalds 	return ret;
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds } /* end keyctl_keyring_search() */
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds /*****************************************************************************/
6291da177e4SLinus Torvalds /*
6301da177e4SLinus Torvalds  * read a user key's payload
6311da177e4SLinus Torvalds  * - the keyring must be readable or the key must be searchable from the
6321da177e4SLinus Torvalds  *   process's keyrings
6331da177e4SLinus Torvalds  * - if there's a buffer, we place up to buflen bytes of data into it
6341da177e4SLinus Torvalds  * - unless there's an error, we return the amount of data in the key,
6351da177e4SLinus Torvalds  *   irrespective of how much we may have copied
6361da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_READ)
6371da177e4SLinus Torvalds  */
6381da177e4SLinus Torvalds long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
6391da177e4SLinus Torvalds {
640664cceb0SDavid Howells 	struct key *key;
641664cceb0SDavid Howells 	key_ref_t key_ref;
6421da177e4SLinus Torvalds 	long ret;
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	/* find the key first */
645*5593122eSDavid Howells 	key_ref = lookup_user_key(keyid, 0, 0);
646664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
647664cceb0SDavid Howells 		ret = -ENOKEY;
648664cceb0SDavid Howells 		goto error;
649664cceb0SDavid Howells 	}
650664cceb0SDavid Howells 
651664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
652664cceb0SDavid Howells 
6531da177e4SLinus Torvalds 	/* see if we can read it directly */
65429db9190SDavid Howells 	ret = key_permission(key_ref, KEY_READ);
65529db9190SDavid Howells 	if (ret == 0)
6561da177e4SLinus Torvalds 		goto can_read_key;
65729db9190SDavid Howells 	if (ret != -EACCES)
65829db9190SDavid Howells 		goto error;
6591da177e4SLinus Torvalds 
660664cceb0SDavid Howells 	/* we can't; see if it's searchable from this process's keyrings
6613e30148cSDavid Howells 	 * - we automatically take account of the fact that it may be
6623e30148cSDavid Howells 	 *   dangling off an instantiation key
6633e30148cSDavid Howells 	 */
664664cceb0SDavid Howells 	if (!is_key_possessed(key_ref)) {
6651260f801SDavid Howells 		ret = -EACCES;
6661da177e4SLinus Torvalds 		goto error2;
6671da177e4SLinus Torvalds 	}
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	/* the key is probably readable - now try to read it */
6701da177e4SLinus Torvalds  can_read_key:
6711da177e4SLinus Torvalds 	ret = key_validate(key);
6721da177e4SLinus Torvalds 	if (ret == 0) {
6731da177e4SLinus Torvalds 		ret = -EOPNOTSUPP;
6741da177e4SLinus Torvalds 		if (key->type->read) {
6751da177e4SLinus Torvalds 			/* read the data with the semaphore held (since we
6761da177e4SLinus Torvalds 			 * might sleep) */
6771da177e4SLinus Torvalds 			down_read(&key->sem);
6781da177e4SLinus Torvalds 			ret = key->type->read(key, buffer, buflen);
6791da177e4SLinus Torvalds 			up_read(&key->sem);
6801da177e4SLinus Torvalds 		}
6811da177e4SLinus Torvalds 	}
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds  error2:
6841da177e4SLinus Torvalds 	key_put(key);
6851da177e4SLinus Torvalds  error:
6861da177e4SLinus Torvalds 	return ret;
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds } /* end keyctl_read_key() */
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds /*****************************************************************************/
6911da177e4SLinus Torvalds /*
6921da177e4SLinus Torvalds  * change the ownership of a key
6931da177e4SLinus Torvalds  * - the keyring owned by the changer
6941da177e4SLinus Torvalds  * - if the uid or gid is -1, then that parameter is not changed
6951da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_CHOWN)
6961da177e4SLinus Torvalds  */
6971da177e4SLinus Torvalds long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
6981da177e4SLinus Torvalds {
6995801649dSFredrik Tolf 	struct key_user *newowner, *zapowner = NULL;
7001da177e4SLinus Torvalds 	struct key *key;
701664cceb0SDavid Howells 	key_ref_t key_ref;
7021da177e4SLinus Torvalds 	long ret;
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	ret = 0;
7051da177e4SLinus Torvalds 	if (uid == (uid_t) -1 && gid == (gid_t) -1)
7061da177e4SLinus Torvalds 		goto error;
7071da177e4SLinus Torvalds 
708*5593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
709*5593122eSDavid Howells 				  KEY_SETATTR);
710664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
711664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
7121da177e4SLinus Torvalds 		goto error;
7131da177e4SLinus Torvalds 	}
7141da177e4SLinus Torvalds 
715664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
716664cceb0SDavid Howells 
7171da177e4SLinus Torvalds 	/* make the changes with the locks held to prevent chown/chown races */
7181da177e4SLinus Torvalds 	ret = -EACCES;
7191da177e4SLinus Torvalds 	down_write(&key->sem);
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN)) {
7221da177e4SLinus Torvalds 		/* only the sysadmin can chown a key to some other UID */
7231da177e4SLinus Torvalds 		if (uid != (uid_t) -1 && key->uid != uid)
7245801649dSFredrik Tolf 			goto error_put;
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 		/* only the sysadmin can set the key's GID to a group other
7271da177e4SLinus Torvalds 		 * than one of those that the current process subscribes to */
7281da177e4SLinus Torvalds 		if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
7295801649dSFredrik Tolf 			goto error_put;
7301da177e4SLinus Torvalds 	}
7311da177e4SLinus Torvalds 
7325801649dSFredrik Tolf 	/* change the UID */
7331da177e4SLinus Torvalds 	if (uid != (uid_t) -1 && uid != key->uid) {
7345801649dSFredrik Tolf 		ret = -ENOMEM;
7351d1e9756SSerge E. Hallyn 		newowner = key_user_lookup(uid, current_user_ns());
7365801649dSFredrik Tolf 		if (!newowner)
7375801649dSFredrik Tolf 			goto error_put;
7385801649dSFredrik Tolf 
7395801649dSFredrik Tolf 		/* transfer the quota burden to the new user */
7405801649dSFredrik Tolf 		if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
7410b77f5bfSDavid Howells 			unsigned maxkeys = (uid == 0) ?
7420b77f5bfSDavid Howells 				key_quota_root_maxkeys : key_quota_maxkeys;
7430b77f5bfSDavid Howells 			unsigned maxbytes = (uid == 0) ?
7440b77f5bfSDavid Howells 				key_quota_root_maxbytes : key_quota_maxbytes;
7450b77f5bfSDavid Howells 
7465801649dSFredrik Tolf 			spin_lock(&newowner->lock);
7470b77f5bfSDavid Howells 			if (newowner->qnkeys + 1 >= maxkeys ||
7480b77f5bfSDavid Howells 			    newowner->qnbytes + key->quotalen >= maxbytes ||
7490b77f5bfSDavid Howells 			    newowner->qnbytes + key->quotalen <
7500b77f5bfSDavid Howells 			    newowner->qnbytes)
7515801649dSFredrik Tolf 				goto quota_overrun;
7525801649dSFredrik Tolf 
7535801649dSFredrik Tolf 			newowner->qnkeys++;
7545801649dSFredrik Tolf 			newowner->qnbytes += key->quotalen;
7555801649dSFredrik Tolf 			spin_unlock(&newowner->lock);
7565801649dSFredrik Tolf 
7575801649dSFredrik Tolf 			spin_lock(&key->user->lock);
7585801649dSFredrik Tolf 			key->user->qnkeys--;
7595801649dSFredrik Tolf 			key->user->qnbytes -= key->quotalen;
7605801649dSFredrik Tolf 			spin_unlock(&key->user->lock);
7615801649dSFredrik Tolf 		}
7625801649dSFredrik Tolf 
7635801649dSFredrik Tolf 		atomic_dec(&key->user->nkeys);
7645801649dSFredrik Tolf 		atomic_inc(&newowner->nkeys);
7655801649dSFredrik Tolf 
7665801649dSFredrik Tolf 		if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
7675801649dSFredrik Tolf 			atomic_dec(&key->user->nikeys);
7685801649dSFredrik Tolf 			atomic_inc(&newowner->nikeys);
7695801649dSFredrik Tolf 		}
7705801649dSFredrik Tolf 
7715801649dSFredrik Tolf 		zapowner = key->user;
7725801649dSFredrik Tolf 		key->user = newowner;
7735801649dSFredrik Tolf 		key->uid = uid;
7741da177e4SLinus Torvalds 	}
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	/* change the GID */
7771da177e4SLinus Torvalds 	if (gid != (gid_t) -1)
7781da177e4SLinus Torvalds 		key->gid = gid;
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 	ret = 0;
7811da177e4SLinus Torvalds 
7825801649dSFredrik Tolf error_put:
7831da177e4SLinus Torvalds 	up_write(&key->sem);
7841da177e4SLinus Torvalds 	key_put(key);
7855801649dSFredrik Tolf 	if (zapowner)
7865801649dSFredrik Tolf 		key_user_put(zapowner);
7871da177e4SLinus Torvalds error:
7881da177e4SLinus Torvalds 	return ret;
7891da177e4SLinus Torvalds 
7905801649dSFredrik Tolf quota_overrun:
7915801649dSFredrik Tolf 	spin_unlock(&newowner->lock);
7925801649dSFredrik Tolf 	zapowner = newowner;
7935801649dSFredrik Tolf 	ret = -EDQUOT;
7945801649dSFredrik Tolf 	goto error_put;
7955801649dSFredrik Tolf 
7961da177e4SLinus Torvalds } /* end keyctl_chown_key() */
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds /*****************************************************************************/
7991da177e4SLinus Torvalds /*
8001da177e4SLinus Torvalds  * change the permission mask on a key
8011da177e4SLinus Torvalds  * - the keyring owned by the changer
8021da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_SETPERM)
8031da177e4SLinus Torvalds  */
8041da177e4SLinus Torvalds long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
8051da177e4SLinus Torvalds {
8061da177e4SLinus Torvalds 	struct key *key;
807664cceb0SDavid Howells 	key_ref_t key_ref;
8081da177e4SLinus Torvalds 	long ret;
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	ret = -EINVAL;
811664cceb0SDavid Howells 	if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
8121da177e4SLinus Torvalds 		goto error;
8131da177e4SLinus Torvalds 
814*5593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
815*5593122eSDavid Howells 				  KEY_SETATTR);
816664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
817664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
8181da177e4SLinus Torvalds 		goto error;
8191da177e4SLinus Torvalds 	}
8201da177e4SLinus Torvalds 
821664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
822664cceb0SDavid Howells 
82376d8aeabSDavid Howells 	/* make the changes with the locks held to prevent chown/chmod races */
8241da177e4SLinus Torvalds 	ret = -EACCES;
8251da177e4SLinus Torvalds 	down_write(&key->sem);
8261da177e4SLinus Torvalds 
82776d8aeabSDavid Howells 	/* if we're not the sysadmin, we can only change a key that we own */
82847d804bfSDavid Howells 	if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) {
8291da177e4SLinus Torvalds 		key->perm = perm;
8301da177e4SLinus Torvalds 		ret = 0;
83176d8aeabSDavid Howells 	}
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	up_write(&key->sem);
8341da177e4SLinus Torvalds 	key_put(key);
8351da177e4SLinus Torvalds error:
8361da177e4SLinus Torvalds 	return ret;
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds } /* end keyctl_setperm_key() */
8391da177e4SLinus Torvalds 
8408bbf4976SDavid Howells /*
8418bbf4976SDavid Howells  * get the destination keyring for instantiation
8428bbf4976SDavid Howells  */
8438bbf4976SDavid Howells static long get_instantiation_keyring(key_serial_t ringid,
8448bbf4976SDavid Howells 				      struct request_key_auth *rka,
8458bbf4976SDavid Howells 				      struct key **_dest_keyring)
8468bbf4976SDavid Howells {
8478bbf4976SDavid Howells 	key_ref_t dkref;
8488bbf4976SDavid Howells 
8498bbf4976SDavid Howells 	*_dest_keyring = NULL;
850eca1bf5bSDavid Howells 
851eca1bf5bSDavid Howells 	/* just return a NULL pointer if we weren't asked to make a link */
852eca1bf5bSDavid Howells 	if (ringid == 0)
8538bbf4976SDavid Howells 		return 0;
8548bbf4976SDavid Howells 
8558bbf4976SDavid Howells 	/* if a specific keyring is nominated by ID, then use that */
8568bbf4976SDavid Howells 	if (ringid > 0) {
857*5593122eSDavid Howells 		dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
8588bbf4976SDavid Howells 		if (IS_ERR(dkref))
8598bbf4976SDavid Howells 			return PTR_ERR(dkref);
8608bbf4976SDavid Howells 		*_dest_keyring = key_ref_to_ptr(dkref);
8618bbf4976SDavid Howells 		return 0;
8628bbf4976SDavid Howells 	}
8638bbf4976SDavid Howells 
8648bbf4976SDavid Howells 	if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
8658bbf4976SDavid Howells 		return -EINVAL;
8668bbf4976SDavid Howells 
8678bbf4976SDavid Howells 	/* otherwise specify the destination keyring recorded in the
8688bbf4976SDavid Howells 	 * authorisation key (any KEY_SPEC_*_KEYRING) */
8698bbf4976SDavid Howells 	if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
8708bbf4976SDavid Howells 		*_dest_keyring = rka->dest_keyring;
8718bbf4976SDavid Howells 		return 0;
8728bbf4976SDavid Howells 	}
8738bbf4976SDavid Howells 
8748bbf4976SDavid Howells 	return -ENOKEY;
8758bbf4976SDavid Howells }
8768bbf4976SDavid Howells 
877d84f4f99SDavid Howells /*
878d84f4f99SDavid Howells  * change the request_key authorisation key on the current process
879d84f4f99SDavid Howells  */
880d84f4f99SDavid Howells static int keyctl_change_reqkey_auth(struct key *key)
881d84f4f99SDavid Howells {
882d84f4f99SDavid Howells 	struct cred *new;
883d84f4f99SDavid Howells 
884d84f4f99SDavid Howells 	new = prepare_creds();
885d84f4f99SDavid Howells 	if (!new)
886d84f4f99SDavid Howells 		return -ENOMEM;
887d84f4f99SDavid Howells 
888d84f4f99SDavid Howells 	key_put(new->request_key_auth);
889d84f4f99SDavid Howells 	new->request_key_auth = key_get(key);
890d84f4f99SDavid Howells 
891d84f4f99SDavid Howells 	return commit_creds(new);
892d84f4f99SDavid Howells }
893d84f4f99SDavid Howells 
8941da177e4SLinus Torvalds /*****************************************************************************/
8951da177e4SLinus Torvalds /*
8961da177e4SLinus Torvalds  * instantiate the key with the specified payload, and, if one is given, link
8971da177e4SLinus Torvalds  * the key into the keyring
8981da177e4SLinus Torvalds  */
8991da177e4SLinus Torvalds long keyctl_instantiate_key(key_serial_t id,
9001da177e4SLinus Torvalds 			    const void __user *_payload,
9011da177e4SLinus Torvalds 			    size_t plen,
9021da177e4SLinus Torvalds 			    key_serial_t ringid)
9031da177e4SLinus Torvalds {
904d84f4f99SDavid Howells 	const struct cred *cred = current_cred();
9053e30148cSDavid Howells 	struct request_key_auth *rka;
9068bbf4976SDavid Howells 	struct key *instkey, *dest_keyring;
9071da177e4SLinus Torvalds 	void *payload;
9081da177e4SLinus Torvalds 	long ret;
90938bbca6bSDavid Howells 	bool vm = false;
9101da177e4SLinus Torvalds 
911d84f4f99SDavid Howells 	kenter("%d,,%zu,%d", id, plen, ringid);
912d84f4f99SDavid Howells 
9131da177e4SLinus Torvalds 	ret = -EINVAL;
91438bbca6bSDavid Howells 	if (plen > 1024 * 1024 - 1)
9151da177e4SLinus Torvalds 		goto error;
9161da177e4SLinus Torvalds 
917b5f545c8SDavid Howells 	/* the appropriate instantiation authorisation key must have been
918b5f545c8SDavid Howells 	 * assumed before calling this */
919b5f545c8SDavid Howells 	ret = -EPERM;
920d84f4f99SDavid Howells 	instkey = cred->request_key_auth;
921b5f545c8SDavid Howells 	if (!instkey)
922b5f545c8SDavid Howells 		goto error;
923b5f545c8SDavid Howells 
924b5f545c8SDavid Howells 	rka = instkey->payload.data;
925b5f545c8SDavid Howells 	if (rka->target_key->serial != id)
926b5f545c8SDavid Howells 		goto error;
927b5f545c8SDavid Howells 
9281da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
9291da177e4SLinus Torvalds 	payload = NULL;
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	if (_payload) {
9321da177e4SLinus Torvalds 		ret = -ENOMEM;
9331da177e4SLinus Torvalds 		payload = kmalloc(plen, GFP_KERNEL);
93438bbca6bSDavid Howells 		if (!payload) {
93538bbca6bSDavid Howells 			if (plen <= PAGE_SIZE)
93638bbca6bSDavid Howells 				goto error;
93738bbca6bSDavid Howells 			vm = true;
93838bbca6bSDavid Howells 			payload = vmalloc(plen);
9391da177e4SLinus Torvalds 			if (!payload)
9401da177e4SLinus Torvalds 				goto error;
94138bbca6bSDavid Howells 		}
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 		ret = -EFAULT;
9441da177e4SLinus Torvalds 		if (copy_from_user(payload, _payload, plen) != 0)
9451da177e4SLinus Torvalds 			goto error2;
9461da177e4SLinus Torvalds 	}
9471da177e4SLinus Torvalds 
9483e30148cSDavid Howells 	/* find the destination keyring amongst those belonging to the
9493e30148cSDavid Howells 	 * requesting task */
9508bbf4976SDavid Howells 	ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
9518bbf4976SDavid Howells 	if (ret < 0)
952b5f545c8SDavid Howells 		goto error2;
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 	/* instantiate the key and link it into a keyring */
9553e30148cSDavid Howells 	ret = key_instantiate_and_link(rka->target_key, payload, plen,
9568bbf4976SDavid Howells 				       dest_keyring, instkey);
9571da177e4SLinus Torvalds 
9588bbf4976SDavid Howells 	key_put(dest_keyring);
959b5f545c8SDavid Howells 
960b5f545c8SDavid Howells 	/* discard the assumed authority if it's just been disabled by
961b5f545c8SDavid Howells 	 * instantiation of the key */
962d84f4f99SDavid Howells 	if (ret == 0)
963d84f4f99SDavid Howells 		keyctl_change_reqkey_auth(NULL);
964b5f545c8SDavid Howells 
9651da177e4SLinus Torvalds error2:
96638bbca6bSDavid Howells 	if (!vm)
9671da177e4SLinus Torvalds 		kfree(payload);
96838bbca6bSDavid Howells 	else
96938bbca6bSDavid Howells 		vfree(payload);
9701da177e4SLinus Torvalds error:
9711da177e4SLinus Torvalds 	return ret;
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds } /* end keyctl_instantiate_key() */
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds /*****************************************************************************/
9761da177e4SLinus Torvalds /*
9771da177e4SLinus Torvalds  * negatively instantiate the key with the given timeout (in seconds), and, if
9781da177e4SLinus Torvalds  * one is given, link the key into the keyring
9791da177e4SLinus Torvalds  */
9801da177e4SLinus Torvalds long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
9811da177e4SLinus Torvalds {
982d84f4f99SDavid Howells 	const struct cred *cred = current_cred();
9833e30148cSDavid Howells 	struct request_key_auth *rka;
9848bbf4976SDavid Howells 	struct key *instkey, *dest_keyring;
9851da177e4SLinus Torvalds 	long ret;
9861da177e4SLinus Torvalds 
987d84f4f99SDavid Howells 	kenter("%d,%u,%d", id, timeout, ringid);
988d84f4f99SDavid Howells 
989b5f545c8SDavid Howells 	/* the appropriate instantiation authorisation key must have been
990b5f545c8SDavid Howells 	 * assumed before calling this */
991b5f545c8SDavid Howells 	ret = -EPERM;
992d84f4f99SDavid Howells 	instkey = cred->request_key_auth;
993b5f545c8SDavid Howells 	if (!instkey)
9941da177e4SLinus Torvalds 		goto error;
9951da177e4SLinus Torvalds 
9963e30148cSDavid Howells 	rka = instkey->payload.data;
997b5f545c8SDavid Howells 	if (rka->target_key->serial != id)
998b5f545c8SDavid Howells 		goto error;
9993e30148cSDavid Howells 
10001da177e4SLinus Torvalds 	/* find the destination keyring if present (which must also be
10011da177e4SLinus Torvalds 	 * writable) */
10028bbf4976SDavid Howells 	ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
10038bbf4976SDavid Howells 	if (ret < 0)
1004b5f545c8SDavid Howells 		goto error;
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds 	/* instantiate the key and link it into a keyring */
1007664cceb0SDavid Howells 	ret = key_negate_and_link(rka->target_key, timeout,
10088bbf4976SDavid Howells 				  dest_keyring, instkey);
10091da177e4SLinus Torvalds 
10108bbf4976SDavid Howells 	key_put(dest_keyring);
1011b5f545c8SDavid Howells 
1012b5f545c8SDavid Howells 	/* discard the assumed authority if it's just been disabled by
1013b5f545c8SDavid Howells 	 * instantiation of the key */
1014d84f4f99SDavid Howells 	if (ret == 0)
1015d84f4f99SDavid Howells 		keyctl_change_reqkey_auth(NULL);
1016b5f545c8SDavid Howells 
10171da177e4SLinus Torvalds error:
10181da177e4SLinus Torvalds 	return ret;
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds } /* end keyctl_negate_key() */
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds /*****************************************************************************/
10231da177e4SLinus Torvalds /*
10243e30148cSDavid Howells  * set the default keyring in which request_key() will cache keys
10253e30148cSDavid Howells  * - return the old setting
10263e30148cSDavid Howells  */
10273e30148cSDavid Howells long keyctl_set_reqkey_keyring(int reqkey_defl)
10283e30148cSDavid Howells {
1029d84f4f99SDavid Howells 	struct cred *new;
1030d84f4f99SDavid Howells 	int ret, old_setting;
1031d84f4f99SDavid Howells 
1032d84f4f99SDavid Howells 	old_setting = current_cred_xxx(jit_keyring);
1033d84f4f99SDavid Howells 
1034d84f4f99SDavid Howells 	if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1035d84f4f99SDavid Howells 		return old_setting;
1036d84f4f99SDavid Howells 
1037d84f4f99SDavid Howells 	new = prepare_creds();
1038d84f4f99SDavid Howells 	if (!new)
1039d84f4f99SDavid Howells 		return -ENOMEM;
10403e30148cSDavid Howells 
10413e30148cSDavid Howells 	switch (reqkey_defl) {
10423e30148cSDavid Howells 	case KEY_REQKEY_DEFL_THREAD_KEYRING:
1043d84f4f99SDavid Howells 		ret = install_thread_keyring_to_cred(new);
10443e30148cSDavid Howells 		if (ret < 0)
1045d84f4f99SDavid Howells 			goto error;
10463e30148cSDavid Howells 		goto set;
10473e30148cSDavid Howells 
10483e30148cSDavid Howells 	case KEY_REQKEY_DEFL_PROCESS_KEYRING:
1049d84f4f99SDavid Howells 		ret = install_process_keyring_to_cred(new);
1050d84f4f99SDavid Howells 		if (ret < 0) {
1051d84f4f99SDavid Howells 			if (ret != -EEXIST)
1052d84f4f99SDavid Howells 				goto error;
1053d84f4f99SDavid Howells 			ret = 0;
1054d84f4f99SDavid Howells 		}
1055d84f4f99SDavid Howells 		goto set;
10563e30148cSDavid Howells 
10573e30148cSDavid Howells 	case KEY_REQKEY_DEFL_DEFAULT:
10583e30148cSDavid Howells 	case KEY_REQKEY_DEFL_SESSION_KEYRING:
10593e30148cSDavid Howells 	case KEY_REQKEY_DEFL_USER_KEYRING:
10603e30148cSDavid Howells 	case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1061d84f4f99SDavid Howells 	case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1062d84f4f99SDavid Howells 		goto set;
10633e30148cSDavid Howells 
10643e30148cSDavid Howells 	case KEY_REQKEY_DEFL_NO_CHANGE:
10653e30148cSDavid Howells 	case KEY_REQKEY_DEFL_GROUP_KEYRING:
10663e30148cSDavid Howells 	default:
1067d84f4f99SDavid Howells 		ret = -EINVAL;
1068d84f4f99SDavid Howells 		goto error;
10693e30148cSDavid Howells 	}
10703e30148cSDavid Howells 
1071d84f4f99SDavid Howells set:
1072d84f4f99SDavid Howells 	new->jit_keyring = reqkey_defl;
1073d84f4f99SDavid Howells 	commit_creds(new);
1074d84f4f99SDavid Howells 	return old_setting;
1075d84f4f99SDavid Howells error:
1076d84f4f99SDavid Howells 	abort_creds(new);
1077d84f4f99SDavid Howells 	return -EINVAL;
1078d84f4f99SDavid Howells 
10793e30148cSDavid Howells } /* end keyctl_set_reqkey_keyring() */
10803e30148cSDavid Howells 
10813e30148cSDavid Howells /*****************************************************************************/
10823e30148cSDavid Howells /*
1083017679c4SDavid Howells  * set or clear the timeout for a key
1084017679c4SDavid Howells  */
1085017679c4SDavid Howells long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1086017679c4SDavid Howells {
1087017679c4SDavid Howells 	struct timespec now;
1088017679c4SDavid Howells 	struct key *key;
1089017679c4SDavid Howells 	key_ref_t key_ref;
1090017679c4SDavid Howells 	time_t expiry;
1091017679c4SDavid Howells 	long ret;
1092017679c4SDavid Howells 
1093*5593122eSDavid Howells 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1094*5593122eSDavid Howells 				  KEY_SETATTR);
1095017679c4SDavid Howells 	if (IS_ERR(key_ref)) {
1096017679c4SDavid Howells 		ret = PTR_ERR(key_ref);
1097017679c4SDavid Howells 		goto error;
1098017679c4SDavid Howells 	}
1099017679c4SDavid Howells 
1100017679c4SDavid Howells 	key = key_ref_to_ptr(key_ref);
1101017679c4SDavid Howells 
1102017679c4SDavid Howells 	/* make the changes with the locks held to prevent races */
1103017679c4SDavid Howells 	down_write(&key->sem);
1104017679c4SDavid Howells 
1105017679c4SDavid Howells 	expiry = 0;
1106017679c4SDavid Howells 	if (timeout > 0) {
1107017679c4SDavid Howells 		now = current_kernel_time();
1108017679c4SDavid Howells 		expiry = now.tv_sec + timeout;
1109017679c4SDavid Howells 	}
1110017679c4SDavid Howells 
1111017679c4SDavid Howells 	key->expiry = expiry;
1112017679c4SDavid Howells 
1113017679c4SDavid Howells 	up_write(&key->sem);
1114017679c4SDavid Howells 	key_put(key);
1115017679c4SDavid Howells 
1116017679c4SDavid Howells 	ret = 0;
1117017679c4SDavid Howells error:
1118017679c4SDavid Howells 	return ret;
1119017679c4SDavid Howells 
1120017679c4SDavid Howells } /* end keyctl_set_timeout() */
1121017679c4SDavid Howells 
1122017679c4SDavid Howells /*****************************************************************************/
1123017679c4SDavid Howells /*
1124b5f545c8SDavid Howells  * assume the authority to instantiate the specified key
1125b5f545c8SDavid Howells  */
1126b5f545c8SDavid Howells long keyctl_assume_authority(key_serial_t id)
1127b5f545c8SDavid Howells {
1128b5f545c8SDavid Howells 	struct key *authkey;
1129b5f545c8SDavid Howells 	long ret;
1130b5f545c8SDavid Howells 
1131b5f545c8SDavid Howells 	/* special key IDs aren't permitted */
1132b5f545c8SDavid Howells 	ret = -EINVAL;
1133b5f545c8SDavid Howells 	if (id < 0)
1134b5f545c8SDavid Howells 		goto error;
1135b5f545c8SDavid Howells 
1136b5f545c8SDavid Howells 	/* we divest ourselves of authority if given an ID of 0 */
1137b5f545c8SDavid Howells 	if (id == 0) {
1138d84f4f99SDavid Howells 		ret = keyctl_change_reqkey_auth(NULL);
1139b5f545c8SDavid Howells 		goto error;
1140b5f545c8SDavid Howells 	}
1141b5f545c8SDavid Howells 
1142b5f545c8SDavid Howells 	/* attempt to assume the authority temporarily granted to us whilst we
1143b5f545c8SDavid Howells 	 * instantiate the specified key
1144b5f545c8SDavid Howells 	 * - the authorisation key must be in the current task's keyrings
1145b5f545c8SDavid Howells 	 *   somewhere
1146b5f545c8SDavid Howells 	 */
1147b5f545c8SDavid Howells 	authkey = key_get_instantiation_authkey(id);
1148b5f545c8SDavid Howells 	if (IS_ERR(authkey)) {
1149b5f545c8SDavid Howells 		ret = PTR_ERR(authkey);
1150b5f545c8SDavid Howells 		goto error;
1151b5f545c8SDavid Howells 	}
1152b5f545c8SDavid Howells 
1153d84f4f99SDavid Howells 	ret = keyctl_change_reqkey_auth(authkey);
1154d84f4f99SDavid Howells 	if (ret < 0)
1155d84f4f99SDavid Howells 		goto error;
1156d84f4f99SDavid Howells 	key_put(authkey);
1157b5f545c8SDavid Howells 
1158d84f4f99SDavid Howells 	ret = authkey->serial;
1159b5f545c8SDavid Howells error:
1160b5f545c8SDavid Howells 	return ret;
1161b5f545c8SDavid Howells 
1162b5f545c8SDavid Howells } /* end keyctl_assume_authority() */
1163b5f545c8SDavid Howells 
116470a5bb72SDavid Howells /*
116570a5bb72SDavid Howells  * get the security label of a key
116670a5bb72SDavid Howells  * - the key must grant us view permission
116770a5bb72SDavid Howells  * - if there's a buffer, we place up to buflen bytes of data into it
116870a5bb72SDavid Howells  * - unless there's an error, we return the amount of information available,
116970a5bb72SDavid Howells  *   irrespective of how much we may have copied (including the terminal NUL)
117070a5bb72SDavid Howells  * - implements keyctl(KEYCTL_GET_SECURITY)
117170a5bb72SDavid Howells  */
117270a5bb72SDavid Howells long keyctl_get_security(key_serial_t keyid,
117370a5bb72SDavid Howells 			 char __user *buffer,
117470a5bb72SDavid Howells 			 size_t buflen)
117570a5bb72SDavid Howells {
117670a5bb72SDavid Howells 	struct key *key, *instkey;
117770a5bb72SDavid Howells 	key_ref_t key_ref;
117870a5bb72SDavid Howells 	char *context;
117970a5bb72SDavid Howells 	long ret;
118070a5bb72SDavid Howells 
1181*5593122eSDavid Howells 	key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
118270a5bb72SDavid Howells 	if (IS_ERR(key_ref)) {
118370a5bb72SDavid Howells 		if (PTR_ERR(key_ref) != -EACCES)
118470a5bb72SDavid Howells 			return PTR_ERR(key_ref);
118570a5bb72SDavid Howells 
118670a5bb72SDavid Howells 		/* viewing a key under construction is also permitted if we
118770a5bb72SDavid Howells 		 * have the authorisation token handy */
118870a5bb72SDavid Howells 		instkey = key_get_instantiation_authkey(keyid);
118970a5bb72SDavid Howells 		if (IS_ERR(instkey))
119070a5bb72SDavid Howells 			return PTR_ERR(key_ref);
119170a5bb72SDavid Howells 		key_put(instkey);
119270a5bb72SDavid Howells 
1193*5593122eSDavid Howells 		key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
119470a5bb72SDavid Howells 		if (IS_ERR(key_ref))
119570a5bb72SDavid Howells 			return PTR_ERR(key_ref);
119670a5bb72SDavid Howells 	}
119770a5bb72SDavid Howells 
119870a5bb72SDavid Howells 	key = key_ref_to_ptr(key_ref);
119970a5bb72SDavid Howells 	ret = security_key_getsecurity(key, &context);
120070a5bb72SDavid Howells 	if (ret == 0) {
120170a5bb72SDavid Howells 		/* if no information was returned, give userspace an empty
120270a5bb72SDavid Howells 		 * string */
120370a5bb72SDavid Howells 		ret = 1;
120470a5bb72SDavid Howells 		if (buffer && buflen > 0 &&
120570a5bb72SDavid Howells 		    copy_to_user(buffer, "", 1) != 0)
120670a5bb72SDavid Howells 			ret = -EFAULT;
120770a5bb72SDavid Howells 	} else if (ret > 0) {
120870a5bb72SDavid Howells 		/* return as much data as there's room for */
120970a5bb72SDavid Howells 		if (buffer && buflen > 0) {
121070a5bb72SDavid Howells 			if (buflen > ret)
121170a5bb72SDavid Howells 				buflen = ret;
121270a5bb72SDavid Howells 
121370a5bb72SDavid Howells 			if (copy_to_user(buffer, context, buflen) != 0)
121470a5bb72SDavid Howells 				ret = -EFAULT;
121570a5bb72SDavid Howells 		}
121670a5bb72SDavid Howells 
121770a5bb72SDavid Howells 		kfree(context);
121870a5bb72SDavid Howells 	}
121970a5bb72SDavid Howells 
122070a5bb72SDavid Howells 	key_ref_put(key_ref);
122170a5bb72SDavid Howells 	return ret;
122270a5bb72SDavid Howells }
122370a5bb72SDavid Howells 
1224b5f545c8SDavid Howells /*****************************************************************************/
1225b5f545c8SDavid Howells /*
12261da177e4SLinus Torvalds  * the key control system call
12271da177e4SLinus Torvalds  */
1228938bb9f5SHeiko Carstens SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1229938bb9f5SHeiko Carstens 		unsigned long, arg4, unsigned long, arg5)
12301da177e4SLinus Torvalds {
12311da177e4SLinus Torvalds 	switch (option) {
12321da177e4SLinus Torvalds 	case KEYCTL_GET_KEYRING_ID:
12331da177e4SLinus Torvalds 		return keyctl_get_keyring_ID((key_serial_t) arg2,
12341da177e4SLinus Torvalds 					     (int) arg3);
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 	case KEYCTL_JOIN_SESSION_KEYRING:
12371da177e4SLinus Torvalds 		return keyctl_join_session_keyring((const char __user *) arg2);
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 	case KEYCTL_UPDATE:
12401da177e4SLinus Torvalds 		return keyctl_update_key((key_serial_t) arg2,
12411da177e4SLinus Torvalds 					 (const void __user *) arg3,
12421da177e4SLinus Torvalds 					 (size_t) arg4);
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds 	case KEYCTL_REVOKE:
12451da177e4SLinus Torvalds 		return keyctl_revoke_key((key_serial_t) arg2);
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 	case KEYCTL_DESCRIBE:
12481da177e4SLinus Torvalds 		return keyctl_describe_key((key_serial_t) arg2,
12491da177e4SLinus Torvalds 					   (char __user *) arg3,
12501da177e4SLinus Torvalds 					   (unsigned) arg4);
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds 	case KEYCTL_CLEAR:
12531da177e4SLinus Torvalds 		return keyctl_keyring_clear((key_serial_t) arg2);
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds 	case KEYCTL_LINK:
12561da177e4SLinus Torvalds 		return keyctl_keyring_link((key_serial_t) arg2,
12571da177e4SLinus Torvalds 					   (key_serial_t) arg3);
12581da177e4SLinus Torvalds 
12591da177e4SLinus Torvalds 	case KEYCTL_UNLINK:
12601da177e4SLinus Torvalds 		return keyctl_keyring_unlink((key_serial_t) arg2,
12611da177e4SLinus Torvalds 					     (key_serial_t) arg3);
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds 	case KEYCTL_SEARCH:
12641da177e4SLinus Torvalds 		return keyctl_keyring_search((key_serial_t) arg2,
12651da177e4SLinus Torvalds 					     (const char __user *) arg3,
12661da177e4SLinus Torvalds 					     (const char __user *) arg4,
12671da177e4SLinus Torvalds 					     (key_serial_t) arg5);
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds 	case KEYCTL_READ:
12701da177e4SLinus Torvalds 		return keyctl_read_key((key_serial_t) arg2,
12711da177e4SLinus Torvalds 				       (char __user *) arg3,
12721da177e4SLinus Torvalds 				       (size_t) arg4);
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 	case KEYCTL_CHOWN:
12751da177e4SLinus Torvalds 		return keyctl_chown_key((key_serial_t) arg2,
12761da177e4SLinus Torvalds 					(uid_t) arg3,
12771da177e4SLinus Torvalds 					(gid_t) arg4);
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 	case KEYCTL_SETPERM:
12801da177e4SLinus Torvalds 		return keyctl_setperm_key((key_serial_t) arg2,
12811da177e4SLinus Torvalds 					  (key_perm_t) arg3);
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	case KEYCTL_INSTANTIATE:
12841da177e4SLinus Torvalds 		return keyctl_instantiate_key((key_serial_t) arg2,
12851da177e4SLinus Torvalds 					      (const void __user *) arg3,
12861da177e4SLinus Torvalds 					      (size_t) arg4,
12871da177e4SLinus Torvalds 					      (key_serial_t) arg5);
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds 	case KEYCTL_NEGATE:
12901da177e4SLinus Torvalds 		return keyctl_negate_key((key_serial_t) arg2,
12911da177e4SLinus Torvalds 					 (unsigned) arg3,
12921da177e4SLinus Torvalds 					 (key_serial_t) arg4);
12931da177e4SLinus Torvalds 
12943e30148cSDavid Howells 	case KEYCTL_SET_REQKEY_KEYRING:
12953e30148cSDavid Howells 		return keyctl_set_reqkey_keyring(arg2);
12963e30148cSDavid Howells 
1297017679c4SDavid Howells 	case KEYCTL_SET_TIMEOUT:
1298017679c4SDavid Howells 		return keyctl_set_timeout((key_serial_t) arg2,
1299017679c4SDavid Howells 					  (unsigned) arg3);
1300017679c4SDavid Howells 
1301b5f545c8SDavid Howells 	case KEYCTL_ASSUME_AUTHORITY:
1302b5f545c8SDavid Howells 		return keyctl_assume_authority((key_serial_t) arg2);
1303b5f545c8SDavid Howells 
130470a5bb72SDavid Howells 	case KEYCTL_GET_SECURITY:
130570a5bb72SDavid Howells 		return keyctl_get_security((key_serial_t) arg2,
130690bd49abSJames Morris 					   (char __user *) arg3,
130770a5bb72SDavid Howells 					   (size_t) arg4);
130870a5bb72SDavid Howells 
13091da177e4SLinus Torvalds 	default:
13101da177e4SLinus Torvalds 		return -EOPNOTSUPP;
13111da177e4SLinus Torvalds 	}
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds } /* end sys_keyctl() */
1314