xref: /openbmc/linux/security/keys/keyctl.c (revision 6b79ccb5)
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) */
106664cceb0SDavid Howells 	keyring_ref = lookup_user_key(NULL, 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,
115*6b79ccb5SArun Raghavan 				       payload, plen, KEY_PERM_UNDEF,
116*6b79ccb5SArun 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) {
188664cceb0SDavid Howells 		dest_ref = lookup_user_key(NULL, 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 
238664cceb0SDavid Howells 	key_ref = lookup_user_key(NULL, 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) */
311664cceb0SDavid Howells 	key_ref = lookup_user_key(NULL, 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 
339664cceb0SDavid Howells 	key_ref = lookup_user_key(NULL, 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 
365664cceb0SDavid Howells 	keyring_ref = lookup_user_key(NULL, 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 
391664cceb0SDavid Howells 	keyring_ref = lookup_user_key(NULL, 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 
397664cceb0SDavid Howells 	key_ref = lookup_user_key(NULL, 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 
425664cceb0SDavid Howells 	keyring_ref = lookup_user_key(NULL, 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 
431664cceb0SDavid Howells 	key_ref = lookup_user_key(NULL, 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 
467664cceb0SDavid Howells 	key_ref = lookup_user_key(NULL, 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);
475664cceb0SDavid Howells 				key_ref = lookup_user_key(NULL, 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 */
560664cceb0SDavid Howells 	keyring_ref = lookup_user_key(NULL, 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) {
569664cceb0SDavid Howells 		dest_ref = lookup_user_key(NULL, 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 */
639664cceb0SDavid Howells 	key_ref = lookup_user_key(NULL, 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 
70229db9190SDavid Howells 	key_ref = lookup_user_key(NULL, 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)) {
7345801649dSFredrik Tolf 			spin_lock(&newowner->lock);
7355801649dSFredrik Tolf 			if (newowner->qnkeys + 1 >= KEYQUOTA_MAX_KEYS ||
7365801649dSFredrik Tolf 			    newowner->qnbytes + key->quotalen >=
7375801649dSFredrik Tolf 			    KEYQUOTA_MAX_BYTES)
7385801649dSFredrik Tolf 				goto quota_overrun;
7395801649dSFredrik Tolf 
7405801649dSFredrik Tolf 			newowner->qnkeys++;
7415801649dSFredrik Tolf 			newowner->qnbytes += key->quotalen;
7425801649dSFredrik Tolf 			spin_unlock(&newowner->lock);
7435801649dSFredrik Tolf 
7445801649dSFredrik Tolf 			spin_lock(&key->user->lock);
7455801649dSFredrik Tolf 			key->user->qnkeys--;
7465801649dSFredrik Tolf 			key->user->qnbytes -= key->quotalen;
7475801649dSFredrik Tolf 			spin_unlock(&key->user->lock);
7485801649dSFredrik Tolf 		}
7495801649dSFredrik Tolf 
7505801649dSFredrik Tolf 		atomic_dec(&key->user->nkeys);
7515801649dSFredrik Tolf 		atomic_inc(&newowner->nkeys);
7525801649dSFredrik Tolf 
7535801649dSFredrik Tolf 		if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
7545801649dSFredrik Tolf 			atomic_dec(&key->user->nikeys);
7555801649dSFredrik Tolf 			atomic_inc(&newowner->nikeys);
7565801649dSFredrik Tolf 		}
7575801649dSFredrik Tolf 
7585801649dSFredrik Tolf 		zapowner = key->user;
7595801649dSFredrik Tolf 		key->user = newowner;
7605801649dSFredrik Tolf 		key->uid = uid;
7611da177e4SLinus Torvalds 	}
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	/* change the GID */
7641da177e4SLinus Torvalds 	if (gid != (gid_t) -1)
7651da177e4SLinus Torvalds 		key->gid = gid;
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	ret = 0;
7681da177e4SLinus Torvalds 
7695801649dSFredrik Tolf error_put:
7701da177e4SLinus Torvalds 	up_write(&key->sem);
7711da177e4SLinus Torvalds 	key_put(key);
7725801649dSFredrik Tolf 	if (zapowner)
7735801649dSFredrik Tolf 		key_user_put(zapowner);
7741da177e4SLinus Torvalds error:
7751da177e4SLinus Torvalds 	return ret;
7761da177e4SLinus Torvalds 
7775801649dSFredrik Tolf quota_overrun:
7785801649dSFredrik Tolf 	spin_unlock(&newowner->lock);
7795801649dSFredrik Tolf 	zapowner = newowner;
7805801649dSFredrik Tolf 	ret = -EDQUOT;
7815801649dSFredrik Tolf 	goto error_put;
7825801649dSFredrik Tolf 
7831da177e4SLinus Torvalds } /* end keyctl_chown_key() */
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds /*****************************************************************************/
7861da177e4SLinus Torvalds /*
7871da177e4SLinus Torvalds  * change the permission mask on a key
7881da177e4SLinus Torvalds  * - the keyring owned by the changer
7891da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_SETPERM)
7901da177e4SLinus Torvalds  */
7911da177e4SLinus Torvalds long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
7921da177e4SLinus Torvalds {
7931da177e4SLinus Torvalds 	struct key *key;
794664cceb0SDavid Howells 	key_ref_t key_ref;
7951da177e4SLinus Torvalds 	long ret;
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 	ret = -EINVAL;
798664cceb0SDavid Howells 	if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
7991da177e4SLinus Torvalds 		goto error;
8001da177e4SLinus Torvalds 
80129db9190SDavid Howells 	key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
802664cceb0SDavid Howells 	if (IS_ERR(key_ref)) {
803664cceb0SDavid Howells 		ret = PTR_ERR(key_ref);
8041da177e4SLinus Torvalds 		goto error;
8051da177e4SLinus Torvalds 	}
8061da177e4SLinus Torvalds 
807664cceb0SDavid Howells 	key = key_ref_to_ptr(key_ref);
808664cceb0SDavid Howells 
80976d8aeabSDavid Howells 	/* make the changes with the locks held to prevent chown/chmod races */
8101da177e4SLinus Torvalds 	ret = -EACCES;
8111da177e4SLinus Torvalds 	down_write(&key->sem);
8121da177e4SLinus Torvalds 
81376d8aeabSDavid Howells 	/* if we're not the sysadmin, we can only change a key that we own */
81476d8aeabSDavid Howells 	if (capable(CAP_SYS_ADMIN) || key->uid == current->fsuid) {
8151da177e4SLinus Torvalds 		key->perm = perm;
8161da177e4SLinus Torvalds 		ret = 0;
81776d8aeabSDavid Howells 	}
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	up_write(&key->sem);
8201da177e4SLinus Torvalds 	key_put(key);
8211da177e4SLinus Torvalds error:
8221da177e4SLinus Torvalds 	return ret;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds } /* end keyctl_setperm_key() */
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds /*****************************************************************************/
8271da177e4SLinus Torvalds /*
8281da177e4SLinus Torvalds  * instantiate the key with the specified payload, and, if one is given, link
8291da177e4SLinus Torvalds  * the key into the keyring
8301da177e4SLinus Torvalds  */
8311da177e4SLinus Torvalds long keyctl_instantiate_key(key_serial_t id,
8321da177e4SLinus Torvalds 			    const void __user *_payload,
8331da177e4SLinus Torvalds 			    size_t plen,
8341da177e4SLinus Torvalds 			    key_serial_t ringid)
8351da177e4SLinus Torvalds {
8363e30148cSDavid Howells 	struct request_key_auth *rka;
837664cceb0SDavid Howells 	struct key *instkey;
838664cceb0SDavid Howells 	key_ref_t keyring_ref;
8391da177e4SLinus Torvalds 	void *payload;
8401da177e4SLinus Torvalds 	long ret;
84138bbca6bSDavid Howells 	bool vm = false;
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	ret = -EINVAL;
84438bbca6bSDavid Howells 	if (plen > 1024 * 1024 - 1)
8451da177e4SLinus Torvalds 		goto error;
8461da177e4SLinus Torvalds 
847b5f545c8SDavid Howells 	/* the appropriate instantiation authorisation key must have been
848b5f545c8SDavid Howells 	 * assumed before calling this */
849b5f545c8SDavid Howells 	ret = -EPERM;
850b5f545c8SDavid Howells 	instkey = current->request_key_auth;
851b5f545c8SDavid Howells 	if (!instkey)
852b5f545c8SDavid Howells 		goto error;
853b5f545c8SDavid Howells 
854b5f545c8SDavid Howells 	rka = instkey->payload.data;
855b5f545c8SDavid Howells 	if (rka->target_key->serial != id)
856b5f545c8SDavid Howells 		goto error;
857b5f545c8SDavid Howells 
8581da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
8591da177e4SLinus Torvalds 	payload = NULL;
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 	if (_payload) {
8621da177e4SLinus Torvalds 		ret = -ENOMEM;
8631da177e4SLinus Torvalds 		payload = kmalloc(plen, GFP_KERNEL);
86438bbca6bSDavid Howells 		if (!payload) {
86538bbca6bSDavid Howells 			if (plen <= PAGE_SIZE)
86638bbca6bSDavid Howells 				goto error;
86738bbca6bSDavid Howells 			vm = true;
86838bbca6bSDavid Howells 			payload = vmalloc(plen);
8691da177e4SLinus Torvalds 			if (!payload)
8701da177e4SLinus Torvalds 				goto error;
87138bbca6bSDavid Howells 		}
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 		ret = -EFAULT;
8741da177e4SLinus Torvalds 		if (copy_from_user(payload, _payload, plen) != 0)
8751da177e4SLinus Torvalds 			goto error2;
8761da177e4SLinus Torvalds 	}
8771da177e4SLinus Torvalds 
8783e30148cSDavid Howells 	/* find the destination keyring amongst those belonging to the
8793e30148cSDavid Howells 	 * requesting task */
880664cceb0SDavid Howells 	keyring_ref = NULL;
8811da177e4SLinus Torvalds 	if (ringid) {
882664cceb0SDavid Howells 		keyring_ref = lookup_user_key(rka->context, ringid, 1, 0,
8833e30148cSDavid Howells 					      KEY_WRITE);
884664cceb0SDavid Howells 		if (IS_ERR(keyring_ref)) {
885664cceb0SDavid Howells 			ret = PTR_ERR(keyring_ref);
886b5f545c8SDavid Howells 			goto error2;
8871da177e4SLinus Torvalds 		}
8881da177e4SLinus Torvalds 	}
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	/* instantiate the key and link it into a keyring */
8913e30148cSDavid Howells 	ret = key_instantiate_and_link(rka->target_key, payload, plen,
892664cceb0SDavid Howells 				       key_ref_to_ptr(keyring_ref), instkey);
8931da177e4SLinus Torvalds 
894664cceb0SDavid Howells 	key_ref_put(keyring_ref);
895b5f545c8SDavid Howells 
896b5f545c8SDavid Howells 	/* discard the assumed authority if it's just been disabled by
897b5f545c8SDavid Howells 	 * instantiation of the key */
898b5f545c8SDavid Howells 	if (ret == 0) {
899b5f545c8SDavid Howells 		key_put(current->request_key_auth);
900b5f545c8SDavid Howells 		current->request_key_auth = NULL;
901b5f545c8SDavid Howells 	}
902b5f545c8SDavid Howells 
9031da177e4SLinus Torvalds error2:
90438bbca6bSDavid Howells 	if (!vm)
9051da177e4SLinus Torvalds 		kfree(payload);
90638bbca6bSDavid Howells 	else
90738bbca6bSDavid Howells 		vfree(payload);
9081da177e4SLinus Torvalds error:
9091da177e4SLinus Torvalds 	return ret;
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds } /* end keyctl_instantiate_key() */
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds /*****************************************************************************/
9141da177e4SLinus Torvalds /*
9151da177e4SLinus Torvalds  * negatively instantiate the key with the given timeout (in seconds), and, if
9161da177e4SLinus Torvalds  * one is given, link the key into the keyring
9171da177e4SLinus Torvalds  */
9181da177e4SLinus Torvalds long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
9191da177e4SLinus Torvalds {
9203e30148cSDavid Howells 	struct request_key_auth *rka;
921664cceb0SDavid Howells 	struct key *instkey;
922664cceb0SDavid Howells 	key_ref_t keyring_ref;
9231da177e4SLinus Torvalds 	long ret;
9241da177e4SLinus Torvalds 
925b5f545c8SDavid Howells 	/* the appropriate instantiation authorisation key must have been
926b5f545c8SDavid Howells 	 * assumed before calling this */
927b5f545c8SDavid Howells 	ret = -EPERM;
928b5f545c8SDavid Howells 	instkey = current->request_key_auth;
929b5f545c8SDavid Howells 	if (!instkey)
9301da177e4SLinus Torvalds 		goto error;
9311da177e4SLinus Torvalds 
9323e30148cSDavid Howells 	rka = instkey->payload.data;
933b5f545c8SDavid Howells 	if (rka->target_key->serial != id)
934b5f545c8SDavid Howells 		goto error;
9353e30148cSDavid Howells 
9361da177e4SLinus Torvalds 	/* find the destination keyring if present (which must also be
9371da177e4SLinus Torvalds 	 * writable) */
938664cceb0SDavid Howells 	keyring_ref = NULL;
9391da177e4SLinus Torvalds 	if (ringid) {
940664cceb0SDavid Howells 		keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
941664cceb0SDavid Howells 		if (IS_ERR(keyring_ref)) {
942664cceb0SDavid Howells 			ret = PTR_ERR(keyring_ref);
943b5f545c8SDavid Howells 			goto error;
9441da177e4SLinus Torvalds 		}
9451da177e4SLinus Torvalds 	}
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	/* instantiate the key and link it into a keyring */
948664cceb0SDavid Howells 	ret = key_negate_and_link(rka->target_key, timeout,
949664cceb0SDavid Howells 				  key_ref_to_ptr(keyring_ref), instkey);
9501da177e4SLinus Torvalds 
951664cceb0SDavid Howells 	key_ref_put(keyring_ref);
952b5f545c8SDavid Howells 
953b5f545c8SDavid Howells 	/* discard the assumed authority if it's just been disabled by
954b5f545c8SDavid Howells 	 * instantiation of the key */
955b5f545c8SDavid Howells 	if (ret == 0) {
956b5f545c8SDavid Howells 		key_put(current->request_key_auth);
957b5f545c8SDavid Howells 		current->request_key_auth = NULL;
958b5f545c8SDavid Howells 	}
959b5f545c8SDavid Howells 
9601da177e4SLinus Torvalds error:
9611da177e4SLinus Torvalds 	return ret;
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds } /* end keyctl_negate_key() */
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds /*****************************************************************************/
9661da177e4SLinus Torvalds /*
9673e30148cSDavid Howells  * set the default keyring in which request_key() will cache keys
9683e30148cSDavid Howells  * - return the old setting
9693e30148cSDavid Howells  */
9703e30148cSDavid Howells long keyctl_set_reqkey_keyring(int reqkey_defl)
9713e30148cSDavid Howells {
9723e30148cSDavid Howells 	int ret;
9733e30148cSDavid Howells 
9743e30148cSDavid Howells 	switch (reqkey_defl) {
9753e30148cSDavid Howells 	case KEY_REQKEY_DEFL_THREAD_KEYRING:
9763e30148cSDavid Howells 		ret = install_thread_keyring(current);
9773e30148cSDavid Howells 		if (ret < 0)
9783e30148cSDavid Howells 			return ret;
9793e30148cSDavid Howells 		goto set;
9803e30148cSDavid Howells 
9813e30148cSDavid Howells 	case KEY_REQKEY_DEFL_PROCESS_KEYRING:
9823e30148cSDavid Howells 		ret = install_process_keyring(current);
9833e30148cSDavid Howells 		if (ret < 0)
9843e30148cSDavid Howells 			return ret;
9853e30148cSDavid Howells 
9863e30148cSDavid Howells 	case KEY_REQKEY_DEFL_DEFAULT:
9873e30148cSDavid Howells 	case KEY_REQKEY_DEFL_SESSION_KEYRING:
9883e30148cSDavid Howells 	case KEY_REQKEY_DEFL_USER_KEYRING:
9893e30148cSDavid Howells 	case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
9903e30148cSDavid Howells 	set:
9913e30148cSDavid Howells 		current->jit_keyring = reqkey_defl;
9923e30148cSDavid Howells 
9933e30148cSDavid Howells 	case KEY_REQKEY_DEFL_NO_CHANGE:
9943e30148cSDavid Howells 		return current->jit_keyring;
9953e30148cSDavid Howells 
9963e30148cSDavid Howells 	case KEY_REQKEY_DEFL_GROUP_KEYRING:
9973e30148cSDavid Howells 	default:
9983e30148cSDavid Howells 		return -EINVAL;
9993e30148cSDavid Howells 	}
10003e30148cSDavid Howells 
10013e30148cSDavid Howells } /* end keyctl_set_reqkey_keyring() */
10023e30148cSDavid Howells 
10033e30148cSDavid Howells /*****************************************************************************/
10043e30148cSDavid Howells /*
1005017679c4SDavid Howells  * set or clear the timeout for a key
1006017679c4SDavid Howells  */
1007017679c4SDavid Howells long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1008017679c4SDavid Howells {
1009017679c4SDavid Howells 	struct timespec now;
1010017679c4SDavid Howells 	struct key *key;
1011017679c4SDavid Howells 	key_ref_t key_ref;
1012017679c4SDavid Howells 	time_t expiry;
1013017679c4SDavid Howells 	long ret;
1014017679c4SDavid Howells 
1015017679c4SDavid Howells 	key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
1016017679c4SDavid Howells 	if (IS_ERR(key_ref)) {
1017017679c4SDavid Howells 		ret = PTR_ERR(key_ref);
1018017679c4SDavid Howells 		goto error;
1019017679c4SDavid Howells 	}
1020017679c4SDavid Howells 
1021017679c4SDavid Howells 	key = key_ref_to_ptr(key_ref);
1022017679c4SDavid Howells 
1023017679c4SDavid Howells 	/* make the changes with the locks held to prevent races */
1024017679c4SDavid Howells 	down_write(&key->sem);
1025017679c4SDavid Howells 
1026017679c4SDavid Howells 	expiry = 0;
1027017679c4SDavid Howells 	if (timeout > 0) {
1028017679c4SDavid Howells 		now = current_kernel_time();
1029017679c4SDavid Howells 		expiry = now.tv_sec + timeout;
1030017679c4SDavid Howells 	}
1031017679c4SDavid Howells 
1032017679c4SDavid Howells 	key->expiry = expiry;
1033017679c4SDavid Howells 
1034017679c4SDavid Howells 	up_write(&key->sem);
1035017679c4SDavid Howells 	key_put(key);
1036017679c4SDavid Howells 
1037017679c4SDavid Howells 	ret = 0;
1038017679c4SDavid Howells error:
1039017679c4SDavid Howells 	return ret;
1040017679c4SDavid Howells 
1041017679c4SDavid Howells } /* end keyctl_set_timeout() */
1042017679c4SDavid Howells 
1043017679c4SDavid Howells /*****************************************************************************/
1044017679c4SDavid Howells /*
1045b5f545c8SDavid Howells  * assume the authority to instantiate the specified key
1046b5f545c8SDavid Howells  */
1047b5f545c8SDavid Howells long keyctl_assume_authority(key_serial_t id)
1048b5f545c8SDavid Howells {
1049b5f545c8SDavid Howells 	struct key *authkey;
1050b5f545c8SDavid Howells 	long ret;
1051b5f545c8SDavid Howells 
1052b5f545c8SDavid Howells 	/* special key IDs aren't permitted */
1053b5f545c8SDavid Howells 	ret = -EINVAL;
1054b5f545c8SDavid Howells 	if (id < 0)
1055b5f545c8SDavid Howells 		goto error;
1056b5f545c8SDavid Howells 
1057b5f545c8SDavid Howells 	/* we divest ourselves of authority if given an ID of 0 */
1058b5f545c8SDavid Howells 	if (id == 0) {
1059b5f545c8SDavid Howells 		key_put(current->request_key_auth);
1060b5f545c8SDavid Howells 		current->request_key_auth = NULL;
1061b5f545c8SDavid Howells 		ret = 0;
1062b5f545c8SDavid Howells 		goto error;
1063b5f545c8SDavid Howells 	}
1064b5f545c8SDavid Howells 
1065b5f545c8SDavid Howells 	/* attempt to assume the authority temporarily granted to us whilst we
1066b5f545c8SDavid Howells 	 * instantiate the specified key
1067b5f545c8SDavid Howells 	 * - the authorisation key must be in the current task's keyrings
1068b5f545c8SDavid Howells 	 *   somewhere
1069b5f545c8SDavid Howells 	 */
1070b5f545c8SDavid Howells 	authkey = key_get_instantiation_authkey(id);
1071b5f545c8SDavid Howells 	if (IS_ERR(authkey)) {
1072b5f545c8SDavid Howells 		ret = PTR_ERR(authkey);
1073b5f545c8SDavid Howells 		goto error;
1074b5f545c8SDavid Howells 	}
1075b5f545c8SDavid Howells 
1076b5f545c8SDavid Howells 	key_put(current->request_key_auth);
1077b5f545c8SDavid Howells 	current->request_key_auth = authkey;
1078b5f545c8SDavid Howells 	ret = authkey->serial;
1079b5f545c8SDavid Howells 
1080b5f545c8SDavid Howells error:
1081b5f545c8SDavid Howells 	return ret;
1082b5f545c8SDavid Howells 
1083b5f545c8SDavid Howells } /* end keyctl_assume_authority() */
1084b5f545c8SDavid Howells 
108570a5bb72SDavid Howells /*
108670a5bb72SDavid Howells  * get the security label of a key
108770a5bb72SDavid Howells  * - the key must grant us view permission
108870a5bb72SDavid Howells  * - if there's a buffer, we place up to buflen bytes of data into it
108970a5bb72SDavid Howells  * - unless there's an error, we return the amount of information available,
109070a5bb72SDavid Howells  *   irrespective of how much we may have copied (including the terminal NUL)
109170a5bb72SDavid Howells  * - implements keyctl(KEYCTL_GET_SECURITY)
109270a5bb72SDavid Howells  */
109370a5bb72SDavid Howells long keyctl_get_security(key_serial_t keyid,
109470a5bb72SDavid Howells 			 char __user *buffer,
109570a5bb72SDavid Howells 			 size_t buflen)
109670a5bb72SDavid Howells {
109770a5bb72SDavid Howells 	struct key *key, *instkey;
109870a5bb72SDavid Howells 	key_ref_t key_ref;
109970a5bb72SDavid Howells 	char *context;
110070a5bb72SDavid Howells 	long ret;
110170a5bb72SDavid Howells 
110270a5bb72SDavid Howells 	key_ref = lookup_user_key(NULL, keyid, 0, 1, KEY_VIEW);
110370a5bb72SDavid Howells 	if (IS_ERR(key_ref)) {
110470a5bb72SDavid Howells 		if (PTR_ERR(key_ref) != -EACCES)
110570a5bb72SDavid Howells 			return PTR_ERR(key_ref);
110670a5bb72SDavid Howells 
110770a5bb72SDavid Howells 		/* viewing a key under construction is also permitted if we
110870a5bb72SDavid Howells 		 * have the authorisation token handy */
110970a5bb72SDavid Howells 		instkey = key_get_instantiation_authkey(keyid);
111070a5bb72SDavid Howells 		if (IS_ERR(instkey))
111170a5bb72SDavid Howells 			return PTR_ERR(key_ref);
111270a5bb72SDavid Howells 		key_put(instkey);
111370a5bb72SDavid Howells 
111470a5bb72SDavid Howells 		key_ref = lookup_user_key(NULL, keyid, 0, 1, 0);
111570a5bb72SDavid Howells 		if (IS_ERR(key_ref))
111670a5bb72SDavid Howells 			return PTR_ERR(key_ref);
111770a5bb72SDavid Howells 	}
111870a5bb72SDavid Howells 
111970a5bb72SDavid Howells 	key = key_ref_to_ptr(key_ref);
112070a5bb72SDavid Howells 	ret = security_key_getsecurity(key, &context);
112170a5bb72SDavid Howells 	if (ret == 0) {
112270a5bb72SDavid Howells 		/* if no information was returned, give userspace an empty
112370a5bb72SDavid Howells 		 * string */
112470a5bb72SDavid Howells 		ret = 1;
112570a5bb72SDavid Howells 		if (buffer && buflen > 0 &&
112670a5bb72SDavid Howells 		    copy_to_user(buffer, "", 1) != 0)
112770a5bb72SDavid Howells 			ret = -EFAULT;
112870a5bb72SDavid Howells 	} else if (ret > 0) {
112970a5bb72SDavid Howells 		/* return as much data as there's room for */
113070a5bb72SDavid Howells 		if (buffer && buflen > 0) {
113170a5bb72SDavid Howells 			if (buflen > ret)
113270a5bb72SDavid Howells 				buflen = ret;
113370a5bb72SDavid Howells 
113470a5bb72SDavid Howells 			if (copy_to_user(buffer, context, buflen) != 0)
113570a5bb72SDavid Howells 				ret = -EFAULT;
113670a5bb72SDavid Howells 		}
113770a5bb72SDavid Howells 
113870a5bb72SDavid Howells 		kfree(context);
113970a5bb72SDavid Howells 	}
114070a5bb72SDavid Howells 
114170a5bb72SDavid Howells 	key_ref_put(key_ref);
114270a5bb72SDavid Howells 	return ret;
114370a5bb72SDavid Howells }
114470a5bb72SDavid Howells 
1145b5f545c8SDavid Howells /*****************************************************************************/
1146b5f545c8SDavid Howells /*
11471da177e4SLinus Torvalds  * the key control system call
11481da177e4SLinus Torvalds  */
11491da177e4SLinus Torvalds asmlinkage long sys_keyctl(int option, unsigned long arg2, unsigned long arg3,
11501da177e4SLinus Torvalds 			   unsigned long arg4, unsigned long arg5)
11511da177e4SLinus Torvalds {
11521da177e4SLinus Torvalds 	switch (option) {
11531da177e4SLinus Torvalds 	case KEYCTL_GET_KEYRING_ID:
11541da177e4SLinus Torvalds 		return keyctl_get_keyring_ID((key_serial_t) arg2,
11551da177e4SLinus Torvalds 					     (int) arg3);
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds 	case KEYCTL_JOIN_SESSION_KEYRING:
11581da177e4SLinus Torvalds 		return keyctl_join_session_keyring((const char __user *) arg2);
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 	case KEYCTL_UPDATE:
11611da177e4SLinus Torvalds 		return keyctl_update_key((key_serial_t) arg2,
11621da177e4SLinus Torvalds 					 (const void __user *) arg3,
11631da177e4SLinus Torvalds 					 (size_t) arg4);
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 	case KEYCTL_REVOKE:
11661da177e4SLinus Torvalds 		return keyctl_revoke_key((key_serial_t) arg2);
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds 	case KEYCTL_DESCRIBE:
11691da177e4SLinus Torvalds 		return keyctl_describe_key((key_serial_t) arg2,
11701da177e4SLinus Torvalds 					   (char __user *) arg3,
11711da177e4SLinus Torvalds 					   (unsigned) arg4);
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 	case KEYCTL_CLEAR:
11741da177e4SLinus Torvalds 		return keyctl_keyring_clear((key_serial_t) arg2);
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 	case KEYCTL_LINK:
11771da177e4SLinus Torvalds 		return keyctl_keyring_link((key_serial_t) arg2,
11781da177e4SLinus Torvalds 					   (key_serial_t) arg3);
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds 	case KEYCTL_UNLINK:
11811da177e4SLinus Torvalds 		return keyctl_keyring_unlink((key_serial_t) arg2,
11821da177e4SLinus Torvalds 					     (key_serial_t) arg3);
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 	case KEYCTL_SEARCH:
11851da177e4SLinus Torvalds 		return keyctl_keyring_search((key_serial_t) arg2,
11861da177e4SLinus Torvalds 					     (const char __user *) arg3,
11871da177e4SLinus Torvalds 					     (const char __user *) arg4,
11881da177e4SLinus Torvalds 					     (key_serial_t) arg5);
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds 	case KEYCTL_READ:
11911da177e4SLinus Torvalds 		return keyctl_read_key((key_serial_t) arg2,
11921da177e4SLinus Torvalds 				       (char __user *) arg3,
11931da177e4SLinus Torvalds 				       (size_t) arg4);
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds 	case KEYCTL_CHOWN:
11961da177e4SLinus Torvalds 		return keyctl_chown_key((key_serial_t) arg2,
11971da177e4SLinus Torvalds 					(uid_t) arg3,
11981da177e4SLinus Torvalds 					(gid_t) arg4);
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds 	case KEYCTL_SETPERM:
12011da177e4SLinus Torvalds 		return keyctl_setperm_key((key_serial_t) arg2,
12021da177e4SLinus Torvalds 					  (key_perm_t) arg3);
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds 	case KEYCTL_INSTANTIATE:
12051da177e4SLinus Torvalds 		return keyctl_instantiate_key((key_serial_t) arg2,
12061da177e4SLinus Torvalds 					      (const void __user *) arg3,
12071da177e4SLinus Torvalds 					      (size_t) arg4,
12081da177e4SLinus Torvalds 					      (key_serial_t) arg5);
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	case KEYCTL_NEGATE:
12111da177e4SLinus Torvalds 		return keyctl_negate_key((key_serial_t) arg2,
12121da177e4SLinus Torvalds 					 (unsigned) arg3,
12131da177e4SLinus Torvalds 					 (key_serial_t) arg4);
12141da177e4SLinus Torvalds 
12153e30148cSDavid Howells 	case KEYCTL_SET_REQKEY_KEYRING:
12163e30148cSDavid Howells 		return keyctl_set_reqkey_keyring(arg2);
12173e30148cSDavid Howells 
1218017679c4SDavid Howells 	case KEYCTL_SET_TIMEOUT:
1219017679c4SDavid Howells 		return keyctl_set_timeout((key_serial_t) arg2,
1220017679c4SDavid Howells 					  (unsigned) arg3);
1221017679c4SDavid Howells 
1222b5f545c8SDavid Howells 	case KEYCTL_ASSUME_AUTHORITY:
1223b5f545c8SDavid Howells 		return keyctl_assume_authority((key_serial_t) arg2);
1224b5f545c8SDavid Howells 
122570a5bb72SDavid Howells 	case KEYCTL_GET_SECURITY:
122670a5bb72SDavid Howells 		return keyctl_get_security((key_serial_t) arg2,
122770a5bb72SDavid Howells 					   (char *) arg3,
122870a5bb72SDavid Howells 					   (size_t) arg4);
122970a5bb72SDavid Howells 
12301da177e4SLinus Torvalds 	default:
12311da177e4SLinus Torvalds 		return -EOPNOTSUPP;
12321da177e4SLinus Torvalds 	}
12331da177e4SLinus Torvalds 
12341da177e4SLinus Torvalds } /* end sys_keyctl() */
1235