xref: /openbmc/linux/security/keys/keyctl.c (revision eca1bf5b)
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  */
571da177e4SLinus Torvalds asmlinkage long sys_add_key(const char __user *_type,
581da177e4SLinus Torvalds 			    const char __user *_description,
591da177e4SLinus Torvalds 			    const void __user *_payload,
601da177e4SLinus Torvalds 			    size_t plen,
611da177e4SLinus Torvalds 			    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) */
1068bbf4976SDavid Howells 	keyring_ref = lookup_user_key(ringid, 1, 0, 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  */
1491da177e4SLinus Torvalds asmlinkage long sys_request_key(const char __user *_type,
1501da177e4SLinus Torvalds 				const char __user *_description,
1511da177e4SLinus Torvalds 				const char __user *_callout_info,
1521da177e4SLinus Torvalds 				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) {
1888bbf4976SDavid Howells 		dest_ref = lookup_user_key(destringid, 1, 0, KEY_WRITE);
189664cceb0SDavid Howells 		if (IS_ERR(dest_ref)) {
190664cceb0SDavid Howells 			ret = PTR_ERR(dest_ref);
1911da177e4SLinus Torvalds 			goto error3;
1921da177e4SLinus Torvalds 		}
1931da177e4SLinus Torvalds 	}
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	/* find the key type */
1961da177e4SLinus Torvalds 	ktype = key_type_lookup(type);
1971da177e4SLinus Torvalds 	if (IS_ERR(ktype)) {
1981da177e4SLinus Torvalds 		ret = PTR_ERR(ktype);
1991da177e4SLinus Torvalds 		goto error4;
2001da177e4SLinus Torvalds 	}
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	/* do the search */
2034a38e122SDavid Howells 	key = request_key_and_link(ktype, description, callout_info,
2044a38e122SDavid Howells 				   callout_len, NULL, key_ref_to_ptr(dest_ref),
2057e047ef5SDavid Howells 				   KEY_ALLOC_IN_QUOTA);
2061da177e4SLinus Torvalds 	if (IS_ERR(key)) {
2071da177e4SLinus Torvalds 		ret = PTR_ERR(key);
2081da177e4SLinus Torvalds 		goto error5;
2091da177e4SLinus Torvalds 	}
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	ret = key->serial;
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds  	key_put(key);
2141da177e4SLinus Torvalds  error5:
2151da177e4SLinus Torvalds 	key_type_put(ktype);
2161da177e4SLinus Torvalds  error4:
217664cceb0SDavid Howells 	key_ref_put(dest_ref);
2181da177e4SLinus Torvalds  error3:
2191da177e4SLinus Torvalds 	kfree(callout_info);
2201da177e4SLinus Torvalds  error2:
2211da177e4SLinus Torvalds 	kfree(description);
2221da177e4SLinus Torvalds  error:
2231da177e4SLinus Torvalds 	return ret;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds } /* end sys_request_key() */
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds /*****************************************************************************/
2281da177e4SLinus Torvalds /*
2291da177e4SLinus Torvalds  * get the ID of the specified process keyring
2301da177e4SLinus Torvalds  * - the keyring must have search permission to be found
2311da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_GET_KEYRING_ID)
2321da177e4SLinus Torvalds  */
2331da177e4SLinus Torvalds long keyctl_get_keyring_ID(key_serial_t id, int create)
2341da177e4SLinus Torvalds {
235664cceb0SDavid Howells 	key_ref_t key_ref;
2361da177e4SLinus Torvalds 	long ret;
2371da177e4SLinus Torvalds 
2388bbf4976SDavid Howells 	key_ref = lookup_user_key(id, create, 0, KEY_SEARCH);
239664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
240664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
2411da177e4SLinus Torvalds 		goto error;
2421da177e4SLinus Torvalds 	}
2431da177e4SLinus Torvalds 
244664cceb0SDavid Howells 	ret = key_ref_to_ptr(key_ref)->serial;
245664cceb0SDavid Howells 	key_ref_put(key_ref);
2461da177e4SLinus Torvalds  error:
2471da177e4SLinus Torvalds 	return ret;
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds } /* end keyctl_get_keyring_ID() */
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds /*****************************************************************************/
2521da177e4SLinus Torvalds /*
2531da177e4SLinus Torvalds  * join the session keyring
2541da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING)
2551da177e4SLinus Torvalds  */
2561da177e4SLinus Torvalds long keyctl_join_session_keyring(const char __user *_name)
2571da177e4SLinus Torvalds {
2581da177e4SLinus Torvalds 	char *name;
2590cb409d9SDavi Arnaut 	long ret;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	/* fetch the name from userspace */
2621da177e4SLinus Torvalds 	name = NULL;
2631da177e4SLinus Torvalds 	if (_name) {
2640cb409d9SDavi Arnaut 		name = strndup_user(_name, PAGE_SIZE);
2650cb409d9SDavi Arnaut 		if (IS_ERR(name)) {
2660cb409d9SDavi Arnaut 			ret = PTR_ERR(name);
2671da177e4SLinus Torvalds 			goto error;
2680cb409d9SDavi Arnaut 		}
2691da177e4SLinus Torvalds 	}
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	/* join the session */
2721da177e4SLinus Torvalds 	ret = join_session_keyring(name);
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds  error:
2751da177e4SLinus Torvalds 	return ret;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds } /* end keyctl_join_session_keyring() */
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds /*****************************************************************************/
2801da177e4SLinus Torvalds /*
2811da177e4SLinus Torvalds  * update a key's data payload
2821da177e4SLinus Torvalds  * - the key must be writable
2831da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_UPDATE)
2841da177e4SLinus Torvalds  */
2851da177e4SLinus Torvalds long keyctl_update_key(key_serial_t id,
2861da177e4SLinus Torvalds 		       const void __user *_payload,
2871da177e4SLinus Torvalds 		       size_t plen)
2881da177e4SLinus Torvalds {
289664cceb0SDavid Howells 	key_ref_t key_ref;
2901da177e4SLinus Torvalds 	void *payload;
2911da177e4SLinus Torvalds 	long ret;
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	ret = -EINVAL;
2941da177e4SLinus Torvalds 	if (plen > PAGE_SIZE)
2951da177e4SLinus Torvalds 		goto error;
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
2981da177e4SLinus Torvalds 	payload = NULL;
2991da177e4SLinus Torvalds 	if (_payload) {
3001da177e4SLinus Torvalds 		ret = -ENOMEM;
3011da177e4SLinus Torvalds 		payload = kmalloc(plen, GFP_KERNEL);
3021da177e4SLinus Torvalds 		if (!payload)
3031da177e4SLinus Torvalds 			goto error;
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 		ret = -EFAULT;
3061da177e4SLinus Torvalds 		if (copy_from_user(payload, _payload, plen) != 0)
3071da177e4SLinus Torvalds 			goto error2;
3081da177e4SLinus Torvalds 	}
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	/* find the target key (which must be writable) */
3118bbf4976SDavid Howells 	key_ref = lookup_user_key(id, 0, 0, KEY_WRITE);
312664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
313664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
3141da177e4SLinus Torvalds 		goto error2;
3151da177e4SLinus Torvalds 	}
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	/* update the key */
318664cceb0SDavid Howells 	ret = key_update(key_ref, payload, plen);
3191da177e4SLinus Torvalds 
320664cceb0SDavid Howells 	key_ref_put(key_ref);
3211da177e4SLinus Torvalds  error2:
3221da177e4SLinus Torvalds 	kfree(payload);
3231da177e4SLinus Torvalds  error:
3241da177e4SLinus Torvalds 	return ret;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds } /* end keyctl_update_key() */
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds /*****************************************************************************/
3291da177e4SLinus Torvalds /*
3301da177e4SLinus Torvalds  * revoke a key
3311da177e4SLinus Torvalds  * - the key must be writable
3321da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_REVOKE)
3331da177e4SLinus Torvalds  */
3341da177e4SLinus Torvalds long keyctl_revoke_key(key_serial_t id)
3351da177e4SLinus Torvalds {
336664cceb0SDavid Howells 	key_ref_t key_ref;
3371da177e4SLinus Torvalds 	long ret;
3381da177e4SLinus Torvalds 
3398bbf4976SDavid Howells 	key_ref = lookup_user_key(id, 0, 0, KEY_WRITE);
340664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
341664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
3421da177e4SLinus Torvalds 		goto error;
3431da177e4SLinus Torvalds 	}
3441da177e4SLinus Torvalds 
345664cceb0SDavid Howells 	key_revoke(key_ref_to_ptr(key_ref));
3461da177e4SLinus Torvalds 	ret = 0;
3471da177e4SLinus Torvalds 
348664cceb0SDavid Howells 	key_ref_put(key_ref);
3491da177e4SLinus Torvalds  error:
3501260f801SDavid Howells 	return ret;
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds } /* end keyctl_revoke_key() */
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds /*****************************************************************************/
3551da177e4SLinus Torvalds /*
3561da177e4SLinus Torvalds  * clear the specified process keyring
3571da177e4SLinus Torvalds  * - the keyring must be writable
3581da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_CLEAR)
3591da177e4SLinus Torvalds  */
3601da177e4SLinus Torvalds long keyctl_keyring_clear(key_serial_t ringid)
3611da177e4SLinus Torvalds {
362664cceb0SDavid Howells 	key_ref_t keyring_ref;
3631da177e4SLinus Torvalds 	long ret;
3641da177e4SLinus Torvalds 
3658bbf4976SDavid Howells 	keyring_ref = lookup_user_key(ringid, 1, 0, KEY_WRITE);
366664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
367664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
3681da177e4SLinus Torvalds 		goto error;
3691da177e4SLinus Torvalds 	}
3701da177e4SLinus Torvalds 
371664cceb0SDavid Howells 	ret = keyring_clear(key_ref_to_ptr(keyring_ref));
3721da177e4SLinus Torvalds 
373664cceb0SDavid Howells 	key_ref_put(keyring_ref);
3741da177e4SLinus Torvalds  error:
3751da177e4SLinus Torvalds 	return ret;
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds } /* end keyctl_keyring_clear() */
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds /*****************************************************************************/
3801da177e4SLinus Torvalds /*
3811da177e4SLinus Torvalds  * link a key into a keyring
3821da177e4SLinus Torvalds  * - the keyring must be writable
3831da177e4SLinus Torvalds  * - the key must be linkable
3841da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_LINK)
3851da177e4SLinus Torvalds  */
3861da177e4SLinus Torvalds long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
3871da177e4SLinus Torvalds {
388664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref;
3891da177e4SLinus Torvalds 	long ret;
3901da177e4SLinus Torvalds 
3918bbf4976SDavid Howells 	keyring_ref = lookup_user_key(ringid, 1, 0, KEY_WRITE);
392664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
393664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
3941da177e4SLinus Torvalds 		goto error;
3951da177e4SLinus Torvalds 	}
3961da177e4SLinus Torvalds 
3978bbf4976SDavid Howells 	key_ref = lookup_user_key(id, 1, 0, KEY_LINK);
398664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
399664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
4001da177e4SLinus Torvalds 		goto error2;
4011da177e4SLinus Torvalds 	}
4021da177e4SLinus Torvalds 
403664cceb0SDavid Howells 	ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
4041da177e4SLinus Torvalds 
405664cceb0SDavid Howells 	key_ref_put(key_ref);
4061da177e4SLinus Torvalds  error2:
407664cceb0SDavid Howells 	key_ref_put(keyring_ref);
4081da177e4SLinus Torvalds  error:
4091da177e4SLinus Torvalds 	return ret;
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds } /* end keyctl_keyring_link() */
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds /*****************************************************************************/
4141da177e4SLinus Torvalds /*
4151da177e4SLinus Torvalds  * unlink the first attachment of a key from a keyring
4161da177e4SLinus Torvalds  * - the keyring must be writable
4171da177e4SLinus Torvalds  * - we don't need any permissions on the key
4181da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_UNLINK)
4191da177e4SLinus Torvalds  */
4201da177e4SLinus Torvalds long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
4211da177e4SLinus Torvalds {
422664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref;
4231da177e4SLinus Torvalds 	long ret;
4241da177e4SLinus Torvalds 
4258bbf4976SDavid Howells 	keyring_ref = lookup_user_key(ringid, 0, 0, KEY_WRITE);
426664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
427664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
4281da177e4SLinus Torvalds 		goto error;
4291da177e4SLinus Torvalds 	}
4301da177e4SLinus Torvalds 
4318bbf4976SDavid Howells 	key_ref = lookup_user_key(id, 0, 0, 0);
432664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
433664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
4341da177e4SLinus Torvalds 		goto error2;
4351da177e4SLinus Torvalds 	}
4361da177e4SLinus Torvalds 
437664cceb0SDavid Howells 	ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
4381da177e4SLinus Torvalds 
439664cceb0SDavid Howells 	key_ref_put(key_ref);
4401da177e4SLinus Torvalds  error2:
441664cceb0SDavid Howells 	key_ref_put(keyring_ref);
4421da177e4SLinus Torvalds  error:
4431da177e4SLinus Torvalds 	return ret;
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds } /* end keyctl_keyring_unlink() */
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds /*****************************************************************************/
4481da177e4SLinus Torvalds /*
4491da177e4SLinus Torvalds  * describe a user key
4501da177e4SLinus Torvalds  * - the key must have view permission
4511da177e4SLinus Torvalds  * - if there's a buffer, we place up to buflen bytes of data into it
4521da177e4SLinus Torvalds  * - unless there's an error, we return the amount of description available,
4531da177e4SLinus Torvalds  *   irrespective of how much we may have copied
4541da177e4SLinus Torvalds  * - the description is formatted thus:
4551da177e4SLinus Torvalds  *	type;uid;gid;perm;description<NUL>
4561da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_DESCRIBE)
4571da177e4SLinus Torvalds  */
4581da177e4SLinus Torvalds long keyctl_describe_key(key_serial_t keyid,
4591da177e4SLinus Torvalds 			 char __user *buffer,
4601da177e4SLinus Torvalds 			 size_t buflen)
4611da177e4SLinus Torvalds {
4623e30148cSDavid Howells 	struct key *key, *instkey;
463664cceb0SDavid Howells 	key_ref_t key_ref;
4641da177e4SLinus Torvalds 	char *tmpbuf;
4651da177e4SLinus Torvalds 	long ret;
4661da177e4SLinus Torvalds 
4678bbf4976SDavid Howells 	key_ref = lookup_user_key(keyid, 0, 1, KEY_VIEW);
468664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
4693e30148cSDavid Howells 		/* viewing a key under construction is permitted if we have the
4703e30148cSDavid Howells 		 * authorisation token handy */
471664cceb0SDavid Howells 		if (PTR_ERR(key_ref) == -EACCES) {
4723e30148cSDavid Howells 			instkey = key_get_instantiation_authkey(keyid);
4733e30148cSDavid Howells 			if (!IS_ERR(instkey)) {
4743e30148cSDavid Howells 				key_put(instkey);
4758bbf4976SDavid Howells 				key_ref = lookup_user_key(keyid,
476664cceb0SDavid Howells 							  0, 1, 0);
477664cceb0SDavid Howells 				if (!IS_ERR(key_ref))
4783e30148cSDavid Howells 					goto okay;
4793e30148cSDavid Howells 			}
4803e30148cSDavid Howells 		}
4813e30148cSDavid Howells 
482664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
4831da177e4SLinus Torvalds 		goto error;
4841da177e4SLinus Torvalds 	}
4851da177e4SLinus Torvalds 
4863e30148cSDavid Howells okay:
4871da177e4SLinus Torvalds 	/* calculate how much description we're going to return */
4881da177e4SLinus Torvalds 	ret = -ENOMEM;
4891da177e4SLinus Torvalds 	tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4901da177e4SLinus Torvalds 	if (!tmpbuf)
4911da177e4SLinus Torvalds 		goto error2;
4921da177e4SLinus Torvalds 
493664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
494664cceb0SDavid Howells 
4951da177e4SLinus Torvalds 	ret = snprintf(tmpbuf, PAGE_SIZE - 1,
496664cceb0SDavid Howells 		       "%s;%d;%d;%08x;%s",
497664cceb0SDavid Howells 		       key_ref_to_ptr(key_ref)->type->name,
498664cceb0SDavid Howells 		       key_ref_to_ptr(key_ref)->uid,
499664cceb0SDavid Howells 		       key_ref_to_ptr(key_ref)->gid,
500664cceb0SDavid Howells 		       key_ref_to_ptr(key_ref)->perm,
501664cceb0SDavid Howells 		       key_ref_to_ptr(key_ref)->description ?
502664cceb0SDavid Howells 		       key_ref_to_ptr(key_ref)->description : ""
5031da177e4SLinus Torvalds 		       );
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	/* include a NUL char at the end of the data */
5061da177e4SLinus Torvalds 	if (ret > PAGE_SIZE - 1)
5071da177e4SLinus Torvalds 		ret = PAGE_SIZE - 1;
5081da177e4SLinus Torvalds 	tmpbuf[ret] = 0;
5091da177e4SLinus Torvalds 	ret++;
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	/* consider returning the data */
5121da177e4SLinus Torvalds 	if (buffer && buflen > 0) {
5131da177e4SLinus Torvalds 		if (buflen > ret)
5141da177e4SLinus Torvalds 			buflen = ret;
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 		if (copy_to_user(buffer, tmpbuf, buflen) != 0)
5171da177e4SLinus Torvalds 			ret = -EFAULT;
5181da177e4SLinus Torvalds 	}
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	kfree(tmpbuf);
5211da177e4SLinus Torvalds  error2:
522664cceb0SDavid Howells 	key_ref_put(key_ref);
5231da177e4SLinus Torvalds  error:
5241da177e4SLinus Torvalds 	return ret;
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds } /* end keyctl_describe_key() */
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds /*****************************************************************************/
5291da177e4SLinus Torvalds /*
5301da177e4SLinus Torvalds  * search the specified keyring for a matching key
5311da177e4SLinus Torvalds  * - the start keyring must be searchable
5321da177e4SLinus Torvalds  * - nested keyrings may also be searched if they are searchable
5331da177e4SLinus Torvalds  * - only keys with search permission may be found
5341da177e4SLinus Torvalds  * - if a key is found, it will be attached to the destination keyring if
5351da177e4SLinus Torvalds  *   there's one specified
5361da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_SEARCH)
5371da177e4SLinus Torvalds  */
5381da177e4SLinus Torvalds long keyctl_keyring_search(key_serial_t ringid,
5391da177e4SLinus Torvalds 			   const char __user *_type,
5401da177e4SLinus Torvalds 			   const char __user *_description,
5411da177e4SLinus Torvalds 			   key_serial_t destringid)
5421da177e4SLinus Torvalds {
5431da177e4SLinus Torvalds 	struct key_type *ktype;
544664cceb0SDavid Howells 	key_ref_t keyring_ref, key_ref, dest_ref;
5451da177e4SLinus Torvalds 	char type[32], *description;
5460cb409d9SDavi Arnaut 	long ret;
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 	/* pull the type and description into kernel space */
5490cb409d9SDavi Arnaut 	ret = key_get_type_from_user(type, _type, sizeof(type));
5501da177e4SLinus Torvalds 	if (ret < 0)
5511da177e4SLinus Torvalds 		goto error;
5521da177e4SLinus Torvalds 
5530cb409d9SDavi Arnaut 	description = strndup_user(_description, PAGE_SIZE);
5540cb409d9SDavi Arnaut 	if (IS_ERR(description)) {
5550cb409d9SDavi Arnaut 		ret = PTR_ERR(description);
5561da177e4SLinus Torvalds 		goto error;
5570cb409d9SDavi Arnaut 	}
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	/* get the keyring at which to begin the search */
5608bbf4976SDavid Howells 	keyring_ref = lookup_user_key(ringid, 0, 0, KEY_SEARCH);
561664cceb0SDavid Howells 	if (IS_ERR(keyring_ref)) {
562664cceb0SDavid Howells 		ret = PTR_ERR(keyring_ref);
5631da177e4SLinus Torvalds 		goto error2;
5641da177e4SLinus Torvalds 	}
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	/* get the destination keyring if specified */
567664cceb0SDavid Howells 	dest_ref = NULL;
5681da177e4SLinus Torvalds 	if (destringid) {
5698bbf4976SDavid Howells 		dest_ref = lookup_user_key(destringid, 1, 0, KEY_WRITE);
570664cceb0SDavid Howells 		if (IS_ERR(dest_ref)) {
571664cceb0SDavid Howells 			ret = PTR_ERR(dest_ref);
5721da177e4SLinus Torvalds 			goto error3;
5731da177e4SLinus Torvalds 		}
5741da177e4SLinus Torvalds 	}
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 	/* find the key type */
5771da177e4SLinus Torvalds 	ktype = key_type_lookup(type);
5781da177e4SLinus Torvalds 	if (IS_ERR(ktype)) {
5791da177e4SLinus Torvalds 		ret = PTR_ERR(ktype);
5801da177e4SLinus Torvalds 		goto error4;
5811da177e4SLinus Torvalds 	}
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	/* do the search */
584664cceb0SDavid Howells 	key_ref = keyring_search(keyring_ref, ktype, description);
585664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
586664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 		/* treat lack or presence of a negative key the same */
5891da177e4SLinus Torvalds 		if (ret == -EAGAIN)
5901da177e4SLinus Torvalds 			ret = -ENOKEY;
5911da177e4SLinus Torvalds 		goto error5;
5921da177e4SLinus Torvalds 	}
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	/* link the resulting key to the destination keyring if we can */
595664cceb0SDavid Howells 	if (dest_ref) {
59629db9190SDavid Howells 		ret = key_permission(key_ref, KEY_LINK);
59729db9190SDavid Howells 		if (ret < 0)
5981da177e4SLinus Torvalds 			goto error6;
5991da177e4SLinus Torvalds 
600664cceb0SDavid Howells 		ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
6011da177e4SLinus Torvalds 		if (ret < 0)
6021da177e4SLinus Torvalds 			goto error6;
6031da177e4SLinus Torvalds 	}
6041da177e4SLinus Torvalds 
605664cceb0SDavid Howells 	ret = key_ref_to_ptr(key_ref)->serial;
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds  error6:
608664cceb0SDavid Howells 	key_ref_put(key_ref);
6091da177e4SLinus Torvalds  error5:
6101da177e4SLinus Torvalds 	key_type_put(ktype);
6111da177e4SLinus Torvalds  error4:
612664cceb0SDavid Howells 	key_ref_put(dest_ref);
6131da177e4SLinus Torvalds  error3:
614664cceb0SDavid Howells 	key_ref_put(keyring_ref);
6151da177e4SLinus Torvalds  error2:
6161da177e4SLinus Torvalds 	kfree(description);
6171da177e4SLinus Torvalds  error:
6181da177e4SLinus Torvalds 	return ret;
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds } /* end keyctl_keyring_search() */
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds /*****************************************************************************/
6231da177e4SLinus Torvalds /*
6241da177e4SLinus Torvalds  * read a user key's payload
6251da177e4SLinus Torvalds  * - the keyring must be readable or the key must be searchable from the
6261da177e4SLinus Torvalds  *   process's keyrings
6271da177e4SLinus Torvalds  * - if there's a buffer, we place up to buflen bytes of data into it
6281da177e4SLinus Torvalds  * - unless there's an error, we return the amount of data in the key,
6291da177e4SLinus Torvalds  *   irrespective of how much we may have copied
6301da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_READ)
6311da177e4SLinus Torvalds  */
6321da177e4SLinus Torvalds long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
6331da177e4SLinus Torvalds {
634664cceb0SDavid Howells 	struct key *key;
635664cceb0SDavid Howells 	key_ref_t key_ref;
6361da177e4SLinus Torvalds 	long ret;
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	/* find the key first */
6398bbf4976SDavid Howells 	key_ref = lookup_user_key(keyid, 0, 0, 0);
640664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
641664cceb0SDavid Howells 		ret = -ENOKEY;
642664cceb0SDavid Howells 		goto error;
643664cceb0SDavid Howells 	}
644664cceb0SDavid Howells 
645664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
646664cceb0SDavid Howells 
6471da177e4SLinus Torvalds 	/* see if we can read it directly */
64829db9190SDavid Howells 	ret = key_permission(key_ref, KEY_READ);
64929db9190SDavid Howells 	if (ret == 0)
6501da177e4SLinus Torvalds 		goto can_read_key;
65129db9190SDavid Howells 	if (ret != -EACCES)
65229db9190SDavid Howells 		goto error;
6531da177e4SLinus Torvalds 
654664cceb0SDavid Howells 	/* we can't; see if it's searchable from this process's keyrings
6553e30148cSDavid Howells 	 * - we automatically take account of the fact that it may be
6563e30148cSDavid Howells 	 *   dangling off an instantiation key
6573e30148cSDavid Howells 	 */
658664cceb0SDavid Howells 	if (!is_key_possessed(key_ref)) {
6591260f801SDavid Howells 		ret = -EACCES;
6601da177e4SLinus Torvalds 		goto error2;
6611da177e4SLinus Torvalds 	}
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 	/* the key is probably readable - now try to read it */
6641da177e4SLinus Torvalds  can_read_key:
6651da177e4SLinus Torvalds 	ret = key_validate(key);
6661da177e4SLinus Torvalds 	if (ret == 0) {
6671da177e4SLinus Torvalds 		ret = -EOPNOTSUPP;
6681da177e4SLinus Torvalds 		if (key->type->read) {
6691da177e4SLinus Torvalds 			/* read the data with the semaphore held (since we
6701da177e4SLinus Torvalds 			 * might sleep) */
6711da177e4SLinus Torvalds 			down_read(&key->sem);
6721da177e4SLinus Torvalds 			ret = key->type->read(key, buffer, buflen);
6731da177e4SLinus Torvalds 			up_read(&key->sem);
6741da177e4SLinus Torvalds 		}
6751da177e4SLinus Torvalds 	}
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds  error2:
6781da177e4SLinus Torvalds 	key_put(key);
6791da177e4SLinus Torvalds  error:
6801da177e4SLinus Torvalds 	return ret;
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds } /* end keyctl_read_key() */
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds /*****************************************************************************/
6851da177e4SLinus Torvalds /*
6861da177e4SLinus Torvalds  * change the ownership of a key
6871da177e4SLinus Torvalds  * - the keyring owned by the changer
6881da177e4SLinus Torvalds  * - if the uid or gid is -1, then that parameter is not changed
6891da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_CHOWN)
6901da177e4SLinus Torvalds  */
6911da177e4SLinus Torvalds long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
6921da177e4SLinus Torvalds {
6935801649dSFredrik Tolf 	struct key_user *newowner, *zapowner = NULL;
6941da177e4SLinus Torvalds 	struct key *key;
695664cceb0SDavid Howells 	key_ref_t key_ref;
6961da177e4SLinus Torvalds 	long ret;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	ret = 0;
6991da177e4SLinus Torvalds 	if (uid == (uid_t) -1 && gid == (gid_t) -1)
7001da177e4SLinus Torvalds 		goto error;
7011da177e4SLinus Torvalds 
7028bbf4976SDavid Howells 	key_ref = lookup_user_key(id, 1, 1, KEY_SETATTR);
703664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
704664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
7051da177e4SLinus Torvalds 		goto error;
7061da177e4SLinus Torvalds 	}
7071da177e4SLinus Torvalds 
708664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
709664cceb0SDavid Howells 
7101da177e4SLinus Torvalds 	/* make the changes with the locks held to prevent chown/chown races */
7111da177e4SLinus Torvalds 	ret = -EACCES;
7121da177e4SLinus Torvalds 	down_write(&key->sem);
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN)) {
7151da177e4SLinus Torvalds 		/* only the sysadmin can chown a key to some other UID */
7161da177e4SLinus Torvalds 		if (uid != (uid_t) -1 && key->uid != uid)
7175801649dSFredrik Tolf 			goto error_put;
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 		/* only the sysadmin can set the key's GID to a group other
7201da177e4SLinus Torvalds 		 * than one of those that the current process subscribes to */
7211da177e4SLinus Torvalds 		if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
7225801649dSFredrik Tolf 			goto error_put;
7231da177e4SLinus Torvalds 	}
7241da177e4SLinus Torvalds 
7255801649dSFredrik Tolf 	/* change the UID */
7261da177e4SLinus Torvalds 	if (uid != (uid_t) -1 && uid != key->uid) {
7275801649dSFredrik Tolf 		ret = -ENOMEM;
7285801649dSFredrik Tolf 		newowner = key_user_lookup(uid);
7295801649dSFredrik Tolf 		if (!newowner)
7305801649dSFredrik Tolf 			goto error_put;
7315801649dSFredrik Tolf 
7325801649dSFredrik Tolf 		/* transfer the quota burden to the new user */
7335801649dSFredrik Tolf 		if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
7340b77f5bfSDavid Howells 			unsigned maxkeys = (uid == 0) ?
7350b77f5bfSDavid Howells 				key_quota_root_maxkeys : key_quota_maxkeys;
7360b77f5bfSDavid Howells 			unsigned maxbytes = (uid == 0) ?
7370b77f5bfSDavid Howells 				key_quota_root_maxbytes : key_quota_maxbytes;
7380b77f5bfSDavid Howells 
7395801649dSFredrik Tolf 			spin_lock(&newowner->lock);
7400b77f5bfSDavid Howells 			if (newowner->qnkeys + 1 >= maxkeys ||
7410b77f5bfSDavid Howells 			    newowner->qnbytes + key->quotalen >= maxbytes ||
7420b77f5bfSDavid Howells 			    newowner->qnbytes + key->quotalen <
7430b77f5bfSDavid Howells 			    newowner->qnbytes)
7445801649dSFredrik Tolf 				goto quota_overrun;
7455801649dSFredrik Tolf 
7465801649dSFredrik Tolf 			newowner->qnkeys++;
7475801649dSFredrik Tolf 			newowner->qnbytes += key->quotalen;
7485801649dSFredrik Tolf 			spin_unlock(&newowner->lock);
7495801649dSFredrik Tolf 
7505801649dSFredrik Tolf 			spin_lock(&key->user->lock);
7515801649dSFredrik Tolf 			key->user->qnkeys--;
7525801649dSFredrik Tolf 			key->user->qnbytes -= key->quotalen;
7535801649dSFredrik Tolf 			spin_unlock(&key->user->lock);
7545801649dSFredrik Tolf 		}
7555801649dSFredrik Tolf 
7565801649dSFredrik Tolf 		atomic_dec(&key->user->nkeys);
7575801649dSFredrik Tolf 		atomic_inc(&newowner->nkeys);
7585801649dSFredrik Tolf 
7595801649dSFredrik Tolf 		if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
7605801649dSFredrik Tolf 			atomic_dec(&key->user->nikeys);
7615801649dSFredrik Tolf 			atomic_inc(&newowner->nikeys);
7625801649dSFredrik Tolf 		}
7635801649dSFredrik Tolf 
7645801649dSFredrik Tolf 		zapowner = key->user;
7655801649dSFredrik Tolf 		key->user = newowner;
7665801649dSFredrik Tolf 		key->uid = uid;
7671da177e4SLinus Torvalds 	}
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	/* change the GID */
7701da177e4SLinus Torvalds 	if (gid != (gid_t) -1)
7711da177e4SLinus Torvalds 		key->gid = gid;
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	ret = 0;
7741da177e4SLinus Torvalds 
7755801649dSFredrik Tolf error_put:
7761da177e4SLinus Torvalds 	up_write(&key->sem);
7771da177e4SLinus Torvalds 	key_put(key);
7785801649dSFredrik Tolf 	if (zapowner)
7795801649dSFredrik Tolf 		key_user_put(zapowner);
7801da177e4SLinus Torvalds error:
7811da177e4SLinus Torvalds 	return ret;
7821da177e4SLinus Torvalds 
7835801649dSFredrik Tolf quota_overrun:
7845801649dSFredrik Tolf 	spin_unlock(&newowner->lock);
7855801649dSFredrik Tolf 	zapowner = newowner;
7865801649dSFredrik Tolf 	ret = -EDQUOT;
7875801649dSFredrik Tolf 	goto error_put;
7885801649dSFredrik Tolf 
7891da177e4SLinus Torvalds } /* end keyctl_chown_key() */
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds /*****************************************************************************/
7921da177e4SLinus Torvalds /*
7931da177e4SLinus Torvalds  * change the permission mask on a key
7941da177e4SLinus Torvalds  * - the keyring owned by the changer
7951da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_SETPERM)
7961da177e4SLinus Torvalds  */
7971da177e4SLinus Torvalds long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
7981da177e4SLinus Torvalds {
7991da177e4SLinus Torvalds 	struct key *key;
800664cceb0SDavid Howells 	key_ref_t key_ref;
8011da177e4SLinus Torvalds 	long ret;
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	ret = -EINVAL;
804664cceb0SDavid Howells 	if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
8051da177e4SLinus Torvalds 		goto error;
8061da177e4SLinus Torvalds 
8078bbf4976SDavid Howells 	key_ref = lookup_user_key(id, 1, 1, KEY_SETATTR);
808664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
809664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
8101da177e4SLinus Torvalds 		goto error;
8111da177e4SLinus Torvalds 	}
8121da177e4SLinus Torvalds 
813664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
814664cceb0SDavid Howells 
81576d8aeabSDavid Howells 	/* make the changes with the locks held to prevent chown/chmod races */
8161da177e4SLinus Torvalds 	ret = -EACCES;
8171da177e4SLinus Torvalds 	down_write(&key->sem);
8181da177e4SLinus Torvalds 
81976d8aeabSDavid Howells 	/* if we're not the sysadmin, we can only change a key that we own */
82047d804bfSDavid Howells 	if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) {
8211da177e4SLinus Torvalds 		key->perm = perm;
8221da177e4SLinus Torvalds 		ret = 0;
82376d8aeabSDavid Howells 	}
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	up_write(&key->sem);
8261da177e4SLinus Torvalds 	key_put(key);
8271da177e4SLinus Torvalds error:
8281da177e4SLinus Torvalds 	return ret;
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds } /* end keyctl_setperm_key() */
8311da177e4SLinus Torvalds 
8328bbf4976SDavid Howells /*
8338bbf4976SDavid Howells  * get the destination keyring for instantiation
8348bbf4976SDavid Howells  */
8358bbf4976SDavid Howells static long get_instantiation_keyring(key_serial_t ringid,
8368bbf4976SDavid Howells 				      struct request_key_auth *rka,
8378bbf4976SDavid Howells 				      struct key **_dest_keyring)
8388bbf4976SDavid Howells {
8398bbf4976SDavid Howells 	key_ref_t dkref;
8408bbf4976SDavid Howells 
8418bbf4976SDavid Howells 	*_dest_keyring = NULL;
842*eca1bf5bSDavid Howells 
843*eca1bf5bSDavid Howells 	/* just return a NULL pointer if we weren't asked to make a link */
844*eca1bf5bSDavid Howells 	if (ringid == 0)
8458bbf4976SDavid Howells 		return 0;
8468bbf4976SDavid Howells 
8478bbf4976SDavid Howells 	/* if a specific keyring is nominated by ID, then use that */
8488bbf4976SDavid Howells 	if (ringid > 0) {
8498bbf4976SDavid Howells 		dkref = lookup_user_key(ringid, 1, 0, KEY_WRITE);
8508bbf4976SDavid Howells 		if (IS_ERR(dkref))
8518bbf4976SDavid Howells 			return PTR_ERR(dkref);
8528bbf4976SDavid Howells 		*_dest_keyring = key_ref_to_ptr(dkref);
8538bbf4976SDavid Howells 		return 0;
8548bbf4976SDavid Howells 	}
8558bbf4976SDavid Howells 
8568bbf4976SDavid Howells 	if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
8578bbf4976SDavid Howells 		return -EINVAL;
8588bbf4976SDavid Howells 
8598bbf4976SDavid Howells 	/* otherwise specify the destination keyring recorded in the
8608bbf4976SDavid Howells 	 * authorisation key (any KEY_SPEC_*_KEYRING) */
8618bbf4976SDavid Howells 	if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
8628bbf4976SDavid Howells 		*_dest_keyring = rka->dest_keyring;
8638bbf4976SDavid Howells 		return 0;
8648bbf4976SDavid Howells 	}
8658bbf4976SDavid Howells 
8668bbf4976SDavid Howells 	return -ENOKEY;
8678bbf4976SDavid Howells }
8688bbf4976SDavid Howells 
869d84f4f99SDavid Howells /*
870d84f4f99SDavid Howells  * change the request_key authorisation key on the current process
871d84f4f99SDavid Howells  */
872d84f4f99SDavid Howells static int keyctl_change_reqkey_auth(struct key *key)
873d84f4f99SDavid Howells {
874d84f4f99SDavid Howells 	struct cred *new;
875d84f4f99SDavid Howells 
876d84f4f99SDavid Howells 	new = prepare_creds();
877d84f4f99SDavid Howells 	if (!new)
878d84f4f99SDavid Howells 		return -ENOMEM;
879d84f4f99SDavid Howells 
880d84f4f99SDavid Howells 	key_put(new->request_key_auth);
881d84f4f99SDavid Howells 	new->request_key_auth = key_get(key);
882d84f4f99SDavid Howells 
883d84f4f99SDavid Howells 	return commit_creds(new);
884d84f4f99SDavid Howells }
885d84f4f99SDavid Howells 
8861da177e4SLinus Torvalds /*****************************************************************************/
8871da177e4SLinus Torvalds /*
8881da177e4SLinus Torvalds  * instantiate the key with the specified payload, and, if one is given, link
8891da177e4SLinus Torvalds  * the key into the keyring
8901da177e4SLinus Torvalds  */
8911da177e4SLinus Torvalds long keyctl_instantiate_key(key_serial_t id,
8921da177e4SLinus Torvalds 			    const void __user *_payload,
8931da177e4SLinus Torvalds 			    size_t plen,
8941da177e4SLinus Torvalds 			    key_serial_t ringid)
8951da177e4SLinus Torvalds {
896d84f4f99SDavid Howells 	const struct cred *cred = current_cred();
8973e30148cSDavid Howells 	struct request_key_auth *rka;
8988bbf4976SDavid Howells 	struct key *instkey, *dest_keyring;
8991da177e4SLinus Torvalds 	void *payload;
9001da177e4SLinus Torvalds 	long ret;
90138bbca6bSDavid Howells 	bool vm = false;
9021da177e4SLinus Torvalds 
903d84f4f99SDavid Howells 	kenter("%d,,%zu,%d", id, plen, ringid);
904d84f4f99SDavid Howells 
9051da177e4SLinus Torvalds 	ret = -EINVAL;
90638bbca6bSDavid Howells 	if (plen > 1024 * 1024 - 1)
9071da177e4SLinus Torvalds 		goto error;
9081da177e4SLinus Torvalds 
909b5f545c8SDavid Howells 	/* the appropriate instantiation authorisation key must have been
910b5f545c8SDavid Howells 	 * assumed before calling this */
911b5f545c8SDavid Howells 	ret = -EPERM;
912d84f4f99SDavid Howells 	instkey = cred->request_key_auth;
913b5f545c8SDavid Howells 	if (!instkey)
914b5f545c8SDavid Howells 		goto error;
915b5f545c8SDavid Howells 
916b5f545c8SDavid Howells 	rka = instkey->payload.data;
917b5f545c8SDavid Howells 	if (rka->target_key->serial != id)
918b5f545c8SDavid Howells 		goto error;
919b5f545c8SDavid Howells 
9201da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
9211da177e4SLinus Torvalds 	payload = NULL;
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds 	if (_payload) {
9241da177e4SLinus Torvalds 		ret = -ENOMEM;
9251da177e4SLinus Torvalds 		payload = kmalloc(plen, GFP_KERNEL);
92638bbca6bSDavid Howells 		if (!payload) {
92738bbca6bSDavid Howells 			if (plen <= PAGE_SIZE)
92838bbca6bSDavid Howells 				goto error;
92938bbca6bSDavid Howells 			vm = true;
93038bbca6bSDavid Howells 			payload = vmalloc(plen);
9311da177e4SLinus Torvalds 			if (!payload)
9321da177e4SLinus Torvalds 				goto error;
93338bbca6bSDavid Howells 		}
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 		ret = -EFAULT;
9361da177e4SLinus Torvalds 		if (copy_from_user(payload, _payload, plen) != 0)
9371da177e4SLinus Torvalds 			goto error2;
9381da177e4SLinus Torvalds 	}
9391da177e4SLinus Torvalds 
9403e30148cSDavid Howells 	/* find the destination keyring amongst those belonging to the
9413e30148cSDavid Howells 	 * requesting task */
9428bbf4976SDavid Howells 	ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
9438bbf4976SDavid Howells 	if (ret < 0)
944b5f545c8SDavid Howells 		goto error2;
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 	/* instantiate the key and link it into a keyring */
9473e30148cSDavid Howells 	ret = key_instantiate_and_link(rka->target_key, payload, plen,
9488bbf4976SDavid Howells 				       dest_keyring, instkey);
9491da177e4SLinus Torvalds 
9508bbf4976SDavid Howells 	key_put(dest_keyring);
951b5f545c8SDavid Howells 
952b5f545c8SDavid Howells 	/* discard the assumed authority if it's just been disabled by
953b5f545c8SDavid Howells 	 * instantiation of the key */
954d84f4f99SDavid Howells 	if (ret == 0)
955d84f4f99SDavid Howells 		keyctl_change_reqkey_auth(NULL);
956b5f545c8SDavid Howells 
9571da177e4SLinus Torvalds error2:
95838bbca6bSDavid Howells 	if (!vm)
9591da177e4SLinus Torvalds 		kfree(payload);
96038bbca6bSDavid Howells 	else
96138bbca6bSDavid Howells 		vfree(payload);
9621da177e4SLinus Torvalds error:
9631da177e4SLinus Torvalds 	return ret;
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds } /* end keyctl_instantiate_key() */
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds /*****************************************************************************/
9681da177e4SLinus Torvalds /*
9691da177e4SLinus Torvalds  * negatively instantiate the key with the given timeout (in seconds), and, if
9701da177e4SLinus Torvalds  * one is given, link the key into the keyring
9711da177e4SLinus Torvalds  */
9721da177e4SLinus Torvalds long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
9731da177e4SLinus Torvalds {
974d84f4f99SDavid Howells 	const struct cred *cred = current_cred();
9753e30148cSDavid Howells 	struct request_key_auth *rka;
9768bbf4976SDavid Howells 	struct key *instkey, *dest_keyring;
9771da177e4SLinus Torvalds 	long ret;
9781da177e4SLinus Torvalds 
979d84f4f99SDavid Howells 	kenter("%d,%u,%d", id, timeout, ringid);
980d84f4f99SDavid Howells 
981b5f545c8SDavid Howells 	/* the appropriate instantiation authorisation key must have been
982b5f545c8SDavid Howells 	 * assumed before calling this */
983b5f545c8SDavid Howells 	ret = -EPERM;
984d84f4f99SDavid Howells 	instkey = cred->request_key_auth;
985b5f545c8SDavid Howells 	if (!instkey)
9861da177e4SLinus Torvalds 		goto error;
9871da177e4SLinus Torvalds 
9883e30148cSDavid Howells 	rka = instkey->payload.data;
989b5f545c8SDavid Howells 	if (rka->target_key->serial != id)
990b5f545c8SDavid Howells 		goto error;
9913e30148cSDavid Howells 
9921da177e4SLinus Torvalds 	/* find the destination keyring if present (which must also be
9931da177e4SLinus Torvalds 	 * writable) */
9948bbf4976SDavid Howells 	ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
9958bbf4976SDavid Howells 	if (ret < 0)
996b5f545c8SDavid Howells 		goto error;
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 	/* instantiate the key and link it into a keyring */
999664cceb0SDavid Howells 	ret = key_negate_and_link(rka->target_key, timeout,
10008bbf4976SDavid Howells 				  dest_keyring, instkey);
10011da177e4SLinus Torvalds 
10028bbf4976SDavid Howells 	key_put(dest_keyring);
1003b5f545c8SDavid Howells 
1004b5f545c8SDavid Howells 	/* discard the assumed authority if it's just been disabled by
1005b5f545c8SDavid Howells 	 * instantiation of the key */
1006d84f4f99SDavid Howells 	if (ret == 0)
1007d84f4f99SDavid Howells 		keyctl_change_reqkey_auth(NULL);
1008b5f545c8SDavid Howells 
10091da177e4SLinus Torvalds error:
10101da177e4SLinus Torvalds 	return ret;
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds } /* end keyctl_negate_key() */
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds /*****************************************************************************/
10151da177e4SLinus Torvalds /*
10163e30148cSDavid Howells  * set the default keyring in which request_key() will cache keys
10173e30148cSDavid Howells  * - return the old setting
10183e30148cSDavid Howells  */
10193e30148cSDavid Howells long keyctl_set_reqkey_keyring(int reqkey_defl)
10203e30148cSDavid Howells {
1021d84f4f99SDavid Howells 	struct cred *new;
1022d84f4f99SDavid Howells 	int ret, old_setting;
1023d84f4f99SDavid Howells 
1024d84f4f99SDavid Howells 	old_setting = current_cred_xxx(jit_keyring);
1025d84f4f99SDavid Howells 
1026d84f4f99SDavid Howells 	if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1027d84f4f99SDavid Howells 		return old_setting;
1028d84f4f99SDavid Howells 
1029d84f4f99SDavid Howells 	new = prepare_creds();
1030d84f4f99SDavid Howells 	if (!new)
1031d84f4f99SDavid Howells 		return -ENOMEM;
10323e30148cSDavid Howells 
10333e30148cSDavid Howells 	switch (reqkey_defl) {
10343e30148cSDavid Howells 	case KEY_REQKEY_DEFL_THREAD_KEYRING:
1035d84f4f99SDavid Howells 		ret = install_thread_keyring_to_cred(new);
10363e30148cSDavid Howells 		if (ret < 0)
1037d84f4f99SDavid Howells 			goto error;
10383e30148cSDavid Howells 		goto set;
10393e30148cSDavid Howells 
10403e30148cSDavid Howells 	case KEY_REQKEY_DEFL_PROCESS_KEYRING:
1041d84f4f99SDavid Howells 		ret = install_process_keyring_to_cred(new);
1042d84f4f99SDavid Howells 		if (ret < 0) {
1043d84f4f99SDavid Howells 			if (ret != -EEXIST)
1044d84f4f99SDavid Howells 				goto error;
1045d84f4f99SDavid Howells 			ret = 0;
1046d84f4f99SDavid Howells 		}
1047d84f4f99SDavid Howells 		goto set;
10483e30148cSDavid Howells 
10493e30148cSDavid Howells 	case KEY_REQKEY_DEFL_DEFAULT:
10503e30148cSDavid Howells 	case KEY_REQKEY_DEFL_SESSION_KEYRING:
10513e30148cSDavid Howells 	case KEY_REQKEY_DEFL_USER_KEYRING:
10523e30148cSDavid Howells 	case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1053d84f4f99SDavid Howells 	case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1054d84f4f99SDavid Howells 		goto set;
10553e30148cSDavid Howells 
10563e30148cSDavid Howells 	case KEY_REQKEY_DEFL_NO_CHANGE:
10573e30148cSDavid Howells 	case KEY_REQKEY_DEFL_GROUP_KEYRING:
10583e30148cSDavid Howells 	default:
1059d84f4f99SDavid Howells 		ret = -EINVAL;
1060d84f4f99SDavid Howells 		goto error;
10613e30148cSDavid Howells 	}
10623e30148cSDavid Howells 
1063d84f4f99SDavid Howells set:
1064d84f4f99SDavid Howells 	new->jit_keyring = reqkey_defl;
1065d84f4f99SDavid Howells 	commit_creds(new);
1066d84f4f99SDavid Howells 	return old_setting;
1067d84f4f99SDavid Howells error:
1068d84f4f99SDavid Howells 	abort_creds(new);
1069d84f4f99SDavid Howells 	return -EINVAL;
1070d84f4f99SDavid Howells 
10713e30148cSDavid Howells } /* end keyctl_set_reqkey_keyring() */
10723e30148cSDavid Howells 
10733e30148cSDavid Howells /*****************************************************************************/
10743e30148cSDavid Howells /*
1075017679c4SDavid Howells  * set or clear the timeout for a key
1076017679c4SDavid Howells  */
1077017679c4SDavid Howells long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1078017679c4SDavid Howells {
1079017679c4SDavid Howells 	struct timespec now;
1080017679c4SDavid Howells 	struct key *key;
1081017679c4SDavid Howells 	key_ref_t key_ref;
1082017679c4SDavid Howells 	time_t expiry;
1083017679c4SDavid Howells 	long ret;
1084017679c4SDavid Howells 
10858bbf4976SDavid Howells 	key_ref = lookup_user_key(id, 1, 1, KEY_SETATTR);
1086017679c4SDavid Howells 	if (IS_ERR(key_ref)) {
1087017679c4SDavid Howells 		ret = PTR_ERR(key_ref);
1088017679c4SDavid Howells 		goto error;
1089017679c4SDavid Howells 	}
1090017679c4SDavid Howells 
1091017679c4SDavid Howells 	key = key_ref_to_ptr(key_ref);
1092017679c4SDavid Howells 
1093017679c4SDavid Howells 	/* make the changes with the locks held to prevent races */
1094017679c4SDavid Howells 	down_write(&key->sem);
1095017679c4SDavid Howells 
1096017679c4SDavid Howells 	expiry = 0;
1097017679c4SDavid Howells 	if (timeout > 0) {
1098017679c4SDavid Howells 		now = current_kernel_time();
1099017679c4SDavid Howells 		expiry = now.tv_sec + timeout;
1100017679c4SDavid Howells 	}
1101017679c4SDavid Howells 
1102017679c4SDavid Howells 	key->expiry = expiry;
1103017679c4SDavid Howells 
1104017679c4SDavid Howells 	up_write(&key->sem);
1105017679c4SDavid Howells 	key_put(key);
1106017679c4SDavid Howells 
1107017679c4SDavid Howells 	ret = 0;
1108017679c4SDavid Howells error:
1109017679c4SDavid Howells 	return ret;
1110017679c4SDavid Howells 
1111017679c4SDavid Howells } /* end keyctl_set_timeout() */
1112017679c4SDavid Howells 
1113017679c4SDavid Howells /*****************************************************************************/
1114017679c4SDavid Howells /*
1115b5f545c8SDavid Howells  * assume the authority to instantiate the specified key
1116b5f545c8SDavid Howells  */
1117b5f545c8SDavid Howells long keyctl_assume_authority(key_serial_t id)
1118b5f545c8SDavid Howells {
1119b5f545c8SDavid Howells 	struct key *authkey;
1120b5f545c8SDavid Howells 	long ret;
1121b5f545c8SDavid Howells 
1122b5f545c8SDavid Howells 	/* special key IDs aren't permitted */
1123b5f545c8SDavid Howells 	ret = -EINVAL;
1124b5f545c8SDavid Howells 	if (id < 0)
1125b5f545c8SDavid Howells 		goto error;
1126b5f545c8SDavid Howells 
1127b5f545c8SDavid Howells 	/* we divest ourselves of authority if given an ID of 0 */
1128b5f545c8SDavid Howells 	if (id == 0) {
1129d84f4f99SDavid Howells 		ret = keyctl_change_reqkey_auth(NULL);
1130b5f545c8SDavid Howells 		goto error;
1131b5f545c8SDavid Howells 	}
1132b5f545c8SDavid Howells 
1133b5f545c8SDavid Howells 	/* attempt to assume the authority temporarily granted to us whilst we
1134b5f545c8SDavid Howells 	 * instantiate the specified key
1135b5f545c8SDavid Howells 	 * - the authorisation key must be in the current task's keyrings
1136b5f545c8SDavid Howells 	 *   somewhere
1137b5f545c8SDavid Howells 	 */
1138b5f545c8SDavid Howells 	authkey = key_get_instantiation_authkey(id);
1139b5f545c8SDavid Howells 	if (IS_ERR(authkey)) {
1140b5f545c8SDavid Howells 		ret = PTR_ERR(authkey);
1141b5f545c8SDavid Howells 		goto error;
1142b5f545c8SDavid Howells 	}
1143b5f545c8SDavid Howells 
1144d84f4f99SDavid Howells 	ret = keyctl_change_reqkey_auth(authkey);
1145d84f4f99SDavid Howells 	if (ret < 0)
1146d84f4f99SDavid Howells 		goto error;
1147d84f4f99SDavid Howells 	key_put(authkey);
1148b5f545c8SDavid Howells 
1149d84f4f99SDavid Howells 	ret = authkey->serial;
1150b5f545c8SDavid Howells error:
1151b5f545c8SDavid Howells 	return ret;
1152b5f545c8SDavid Howells 
1153b5f545c8SDavid Howells } /* end keyctl_assume_authority() */
1154b5f545c8SDavid Howells 
115570a5bb72SDavid Howells /*
115670a5bb72SDavid Howells  * get the security label of a key
115770a5bb72SDavid Howells  * - the key must grant us view permission
115870a5bb72SDavid Howells  * - if there's a buffer, we place up to buflen bytes of data into it
115970a5bb72SDavid Howells  * - unless there's an error, we return the amount of information available,
116070a5bb72SDavid Howells  *   irrespective of how much we may have copied (including the terminal NUL)
116170a5bb72SDavid Howells  * - implements keyctl(KEYCTL_GET_SECURITY)
116270a5bb72SDavid Howells  */
116370a5bb72SDavid Howells long keyctl_get_security(key_serial_t keyid,
116470a5bb72SDavid Howells 			 char __user *buffer,
116570a5bb72SDavid Howells 			 size_t buflen)
116670a5bb72SDavid Howells {
116770a5bb72SDavid Howells 	struct key *key, *instkey;
116870a5bb72SDavid Howells 	key_ref_t key_ref;
116970a5bb72SDavid Howells 	char *context;
117070a5bb72SDavid Howells 	long ret;
117170a5bb72SDavid Howells 
11728bbf4976SDavid Howells 	key_ref = lookup_user_key(keyid, 0, 1, KEY_VIEW);
117370a5bb72SDavid Howells 	if (IS_ERR(key_ref)) {
117470a5bb72SDavid Howells 		if (PTR_ERR(key_ref) != -EACCES)
117570a5bb72SDavid Howells 			return PTR_ERR(key_ref);
117670a5bb72SDavid Howells 
117770a5bb72SDavid Howells 		/* viewing a key under construction is also permitted if we
117870a5bb72SDavid Howells 		 * have the authorisation token handy */
117970a5bb72SDavid Howells 		instkey = key_get_instantiation_authkey(keyid);
118070a5bb72SDavid Howells 		if (IS_ERR(instkey))
118170a5bb72SDavid Howells 			return PTR_ERR(key_ref);
118270a5bb72SDavid Howells 		key_put(instkey);
118370a5bb72SDavid Howells 
11848bbf4976SDavid Howells 		key_ref = lookup_user_key(keyid, 0, 1, 0);
118570a5bb72SDavid Howells 		if (IS_ERR(key_ref))
118670a5bb72SDavid Howells 			return PTR_ERR(key_ref);
118770a5bb72SDavid Howells 	}
118870a5bb72SDavid Howells 
118970a5bb72SDavid Howells 	key = key_ref_to_ptr(key_ref);
119070a5bb72SDavid Howells 	ret = security_key_getsecurity(key, &context);
119170a5bb72SDavid Howells 	if (ret == 0) {
119270a5bb72SDavid Howells 		/* if no information was returned, give userspace an empty
119370a5bb72SDavid Howells 		 * string */
119470a5bb72SDavid Howells 		ret = 1;
119570a5bb72SDavid Howells 		if (buffer && buflen > 0 &&
119670a5bb72SDavid Howells 		    copy_to_user(buffer, "", 1) != 0)
119770a5bb72SDavid Howells 			ret = -EFAULT;
119870a5bb72SDavid Howells 	} else if (ret > 0) {
119970a5bb72SDavid Howells 		/* return as much data as there's room for */
120070a5bb72SDavid Howells 		if (buffer && buflen > 0) {
120170a5bb72SDavid Howells 			if (buflen > ret)
120270a5bb72SDavid Howells 				buflen = ret;
120370a5bb72SDavid Howells 
120470a5bb72SDavid Howells 			if (copy_to_user(buffer, context, buflen) != 0)
120570a5bb72SDavid Howells 				ret = -EFAULT;
120670a5bb72SDavid Howells 		}
120770a5bb72SDavid Howells 
120870a5bb72SDavid Howells 		kfree(context);
120970a5bb72SDavid Howells 	}
121070a5bb72SDavid Howells 
121170a5bb72SDavid Howells 	key_ref_put(key_ref);
121270a5bb72SDavid Howells 	return ret;
121370a5bb72SDavid Howells }
121470a5bb72SDavid Howells 
1215b5f545c8SDavid Howells /*****************************************************************************/
1216b5f545c8SDavid Howells /*
12171da177e4SLinus Torvalds  * the key control system call
12181da177e4SLinus Torvalds  */
12191da177e4SLinus Torvalds asmlinkage long sys_keyctl(int option, unsigned long arg2, unsigned long arg3,
12201da177e4SLinus Torvalds 			   unsigned long arg4, unsigned long arg5)
12211da177e4SLinus Torvalds {
12221da177e4SLinus Torvalds 	switch (option) {
12231da177e4SLinus Torvalds 	case KEYCTL_GET_KEYRING_ID:
12241da177e4SLinus Torvalds 		return keyctl_get_keyring_ID((key_serial_t) arg2,
12251da177e4SLinus Torvalds 					     (int) arg3);
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 	case KEYCTL_JOIN_SESSION_KEYRING:
12281da177e4SLinus Torvalds 		return keyctl_join_session_keyring((const char __user *) arg2);
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 	case KEYCTL_UPDATE:
12311da177e4SLinus Torvalds 		return keyctl_update_key((key_serial_t) arg2,
12321da177e4SLinus Torvalds 					 (const void __user *) arg3,
12331da177e4SLinus Torvalds 					 (size_t) arg4);
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds 	case KEYCTL_REVOKE:
12361da177e4SLinus Torvalds 		return keyctl_revoke_key((key_serial_t) arg2);
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 	case KEYCTL_DESCRIBE:
12391da177e4SLinus Torvalds 		return keyctl_describe_key((key_serial_t) arg2,
12401da177e4SLinus Torvalds 					   (char __user *) arg3,
12411da177e4SLinus Torvalds 					   (unsigned) arg4);
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 	case KEYCTL_CLEAR:
12441da177e4SLinus Torvalds 		return keyctl_keyring_clear((key_serial_t) arg2);
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 	case KEYCTL_LINK:
12471da177e4SLinus Torvalds 		return keyctl_keyring_link((key_serial_t) arg2,
12481da177e4SLinus Torvalds 					   (key_serial_t) arg3);
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds 	case KEYCTL_UNLINK:
12511da177e4SLinus Torvalds 		return keyctl_keyring_unlink((key_serial_t) arg2,
12521da177e4SLinus Torvalds 					     (key_serial_t) arg3);
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds 	case KEYCTL_SEARCH:
12551da177e4SLinus Torvalds 		return keyctl_keyring_search((key_serial_t) arg2,
12561da177e4SLinus Torvalds 					     (const char __user *) arg3,
12571da177e4SLinus Torvalds 					     (const char __user *) arg4,
12581da177e4SLinus Torvalds 					     (key_serial_t) arg5);
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds 	case KEYCTL_READ:
12611da177e4SLinus Torvalds 		return keyctl_read_key((key_serial_t) arg2,
12621da177e4SLinus Torvalds 				       (char __user *) arg3,
12631da177e4SLinus Torvalds 				       (size_t) arg4);
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 	case KEYCTL_CHOWN:
12661da177e4SLinus Torvalds 		return keyctl_chown_key((key_serial_t) arg2,
12671da177e4SLinus Torvalds 					(uid_t) arg3,
12681da177e4SLinus Torvalds 					(gid_t) arg4);
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds 	case KEYCTL_SETPERM:
12711da177e4SLinus Torvalds 		return keyctl_setperm_key((key_serial_t) arg2,
12721da177e4SLinus Torvalds 					  (key_perm_t) arg3);
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 	case KEYCTL_INSTANTIATE:
12751da177e4SLinus Torvalds 		return keyctl_instantiate_key((key_serial_t) arg2,
12761da177e4SLinus Torvalds 					      (const void __user *) arg3,
12771da177e4SLinus Torvalds 					      (size_t) arg4,
12781da177e4SLinus Torvalds 					      (key_serial_t) arg5);
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds 	case KEYCTL_NEGATE:
12811da177e4SLinus Torvalds 		return keyctl_negate_key((key_serial_t) arg2,
12821da177e4SLinus Torvalds 					 (unsigned) arg3,
12831da177e4SLinus Torvalds 					 (key_serial_t) arg4);
12841da177e4SLinus Torvalds 
12853e30148cSDavid Howells 	case KEYCTL_SET_REQKEY_KEYRING:
12863e30148cSDavid Howells 		return keyctl_set_reqkey_keyring(arg2);
12873e30148cSDavid Howells 
1288017679c4SDavid Howells 	case KEYCTL_SET_TIMEOUT:
1289017679c4SDavid Howells 		return keyctl_set_timeout((key_serial_t) arg2,
1290017679c4SDavid Howells 					  (unsigned) arg3);
1291017679c4SDavid Howells 
1292b5f545c8SDavid Howells 	case KEYCTL_ASSUME_AUTHORITY:
1293b5f545c8SDavid Howells 		return keyctl_assume_authority((key_serial_t) arg2);
1294b5f545c8SDavid Howells 
129570a5bb72SDavid Howells 	case KEYCTL_GET_SECURITY:
129670a5bb72SDavid Howells 		return keyctl_get_security((key_serial_t) arg2,
129770a5bb72SDavid Howells 					   (char *) arg3,
129870a5bb72SDavid Howells 					   (size_t) arg4);
129970a5bb72SDavid Howells 
13001da177e4SLinus Torvalds 	default:
13011da177e4SLinus Torvalds 		return -EOPNOTSUPP;
13021da177e4SLinus Torvalds 	}
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds } /* end sys_keyctl() */
1305