xref: /openbmc/linux/security/keys/keyctl.c (revision 1260f801)
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>
191da177e4SLinus Torvalds #include <linux/err.h>
201da177e4SLinus Torvalds #include <asm/uaccess.h>
211da177e4SLinus Torvalds #include "internal.h"
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds /*****************************************************************************/
241da177e4SLinus Torvalds /*
251da177e4SLinus Torvalds  * extract the description of a new key from userspace and either add it as a
261da177e4SLinus Torvalds  * new key to the specified keyring or update a matching key in that keyring
271da177e4SLinus Torvalds  * - the keyring must be writable
281da177e4SLinus Torvalds  * - returns the new key's serial number
291da177e4SLinus Torvalds  * - implements add_key()
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds asmlinkage long sys_add_key(const char __user *_type,
321da177e4SLinus Torvalds 			    const char __user *_description,
331da177e4SLinus Torvalds 			    const void __user *_payload,
341da177e4SLinus Torvalds 			    size_t plen,
351da177e4SLinus Torvalds 			    key_serial_t ringid)
361da177e4SLinus Torvalds {
371da177e4SLinus Torvalds 	struct key *keyring, *key;
381da177e4SLinus Torvalds 	char type[32], *description;
391da177e4SLinus Torvalds 	void *payload;
401da177e4SLinus Torvalds 	long dlen, ret;
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds 	ret = -EINVAL;
431da177e4SLinus Torvalds 	if (plen > 32767)
441da177e4SLinus Torvalds 		goto error;
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds 	/* draw all the data into kernel space */
471da177e4SLinus Torvalds 	ret = strncpy_from_user(type, _type, sizeof(type) - 1);
481da177e4SLinus Torvalds 	if (ret < 0)
491da177e4SLinus Torvalds 		goto error;
501da177e4SLinus Torvalds 	type[31] = '\0';
511da177e4SLinus Torvalds 
523e30148cSDavid Howells 	ret = -EPERM;
533e30148cSDavid Howells 	if (type[0] == '.')
543e30148cSDavid Howells 		goto error;
553e30148cSDavid Howells 
561da177e4SLinus Torvalds 	ret = -EFAULT;
571da177e4SLinus Torvalds 	dlen = strnlen_user(_description, PAGE_SIZE - 1);
581da177e4SLinus Torvalds 	if (dlen <= 0)
591da177e4SLinus Torvalds 		goto error;
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds 	ret = -EINVAL;
621da177e4SLinus Torvalds 	if (dlen > PAGE_SIZE - 1)
631da177e4SLinus Torvalds 		goto error;
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	ret = -ENOMEM;
661da177e4SLinus Torvalds 	description = kmalloc(dlen + 1, GFP_KERNEL);
671da177e4SLinus Torvalds 	if (!description)
681da177e4SLinus Torvalds 		goto error;
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds 	ret = -EFAULT;
711da177e4SLinus Torvalds 	if (copy_from_user(description, _description, dlen + 1) != 0)
721da177e4SLinus Torvalds 		goto error2;
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
751da177e4SLinus Torvalds 	payload = NULL;
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	if (_payload) {
781da177e4SLinus Torvalds 		ret = -ENOMEM;
791da177e4SLinus Torvalds 		payload = kmalloc(plen, GFP_KERNEL);
801da177e4SLinus Torvalds 		if (!payload)
811da177e4SLinus Torvalds 			goto error2;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 		ret = -EFAULT;
841da177e4SLinus Torvalds 		if (copy_from_user(payload, _payload, plen) != 0)
851da177e4SLinus Torvalds 			goto error3;
861da177e4SLinus Torvalds 	}
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	/* find the target keyring (which must be writable) */
893e30148cSDavid Howells 	keyring = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
901da177e4SLinus Torvalds 	if (IS_ERR(keyring)) {
911da177e4SLinus Torvalds 		ret = PTR_ERR(keyring);
921da177e4SLinus Torvalds 		goto error3;
931da177e4SLinus Torvalds 	}
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	/* create or update the requested key and add it to the target
961da177e4SLinus Torvalds 	 * keyring */
971da177e4SLinus Torvalds 	key = key_create_or_update(keyring, type, description,
981da177e4SLinus Torvalds 				   payload, plen, 0);
991da177e4SLinus Torvalds 	if (!IS_ERR(key)) {
1001da177e4SLinus Torvalds 		ret = key->serial;
1011da177e4SLinus Torvalds 		key_put(key);
1021da177e4SLinus Torvalds 	}
1031da177e4SLinus Torvalds 	else {
1041da177e4SLinus Torvalds 		ret = PTR_ERR(key);
1051da177e4SLinus Torvalds 	}
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 	key_put(keyring);
1081da177e4SLinus Torvalds  error3:
1091da177e4SLinus Torvalds 	kfree(payload);
1101da177e4SLinus Torvalds  error2:
1111da177e4SLinus Torvalds 	kfree(description);
1121da177e4SLinus Torvalds  error:
1131da177e4SLinus Torvalds 	return ret;
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds } /* end sys_add_key() */
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds /*****************************************************************************/
1181da177e4SLinus Torvalds /*
1191da177e4SLinus Torvalds  * search the process keyrings for a matching key
1201da177e4SLinus Torvalds  * - nested keyrings may also be searched if they have Search permission
1211da177e4SLinus Torvalds  * - if a key is found, it will be attached to the destination keyring if
1221da177e4SLinus Torvalds  *   there's one specified
1231da177e4SLinus Torvalds  * - /sbin/request-key will be invoked if _callout_info is non-NULL
1241da177e4SLinus Torvalds  *   - the _callout_info string will be passed to /sbin/request-key
1251da177e4SLinus Torvalds  *   - if the _callout_info string is empty, it will be rendered as "-"
1261da177e4SLinus Torvalds  * - implements request_key()
1271da177e4SLinus Torvalds  */
1281da177e4SLinus Torvalds asmlinkage long sys_request_key(const char __user *_type,
1291da177e4SLinus Torvalds 				const char __user *_description,
1301da177e4SLinus Torvalds 				const char __user *_callout_info,
1311da177e4SLinus Torvalds 				key_serial_t destringid)
1321da177e4SLinus Torvalds {
1331da177e4SLinus Torvalds 	struct key_type *ktype;
1341da177e4SLinus Torvalds 	struct key *key, *dest;
1351da177e4SLinus Torvalds 	char type[32], *description, *callout_info;
1361da177e4SLinus Torvalds 	long dlen, ret;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	/* pull the type into kernel space */
1391da177e4SLinus Torvalds 	ret = strncpy_from_user(type, _type, sizeof(type) - 1);
1401da177e4SLinus Torvalds 	if (ret < 0)
1411da177e4SLinus Torvalds 		goto error;
1421da177e4SLinus Torvalds 	type[31] = '\0';
1431da177e4SLinus Torvalds 
144*1260f801SDavid Howells 	ret = -EPERM;
145*1260f801SDavid Howells 	if (type[0] == '.')
146*1260f801SDavid Howells 		goto error;
147*1260f801SDavid Howells 
1481da177e4SLinus Torvalds 	/* pull the description into kernel space */
1491da177e4SLinus Torvalds 	ret = -EFAULT;
1501da177e4SLinus Torvalds 	dlen = strnlen_user(_description, PAGE_SIZE - 1);
1511da177e4SLinus Torvalds 	if (dlen <= 0)
1521da177e4SLinus Torvalds 		goto error;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	ret = -EINVAL;
1551da177e4SLinus Torvalds 	if (dlen > PAGE_SIZE - 1)
1561da177e4SLinus Torvalds 		goto error;
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	ret = -ENOMEM;
1591da177e4SLinus Torvalds 	description = kmalloc(dlen + 1, GFP_KERNEL);
1601da177e4SLinus Torvalds 	if (!description)
1611da177e4SLinus Torvalds 		goto error;
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	ret = -EFAULT;
1641da177e4SLinus Torvalds 	if (copy_from_user(description, _description, dlen + 1) != 0)
1651da177e4SLinus Torvalds 		goto error2;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	/* pull the callout info into kernel space */
1681da177e4SLinus Torvalds 	callout_info = NULL;
1691da177e4SLinus Torvalds 	if (_callout_info) {
1701da177e4SLinus Torvalds 		ret = -EFAULT;
1711da177e4SLinus Torvalds 		dlen = strnlen_user(_callout_info, PAGE_SIZE - 1);
1721da177e4SLinus Torvalds 		if (dlen <= 0)
1731da177e4SLinus Torvalds 			goto error2;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 		ret = -EINVAL;
1761da177e4SLinus Torvalds 		if (dlen > PAGE_SIZE - 1)
1771da177e4SLinus Torvalds 			goto error2;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 		ret = -ENOMEM;
1801da177e4SLinus Torvalds 		callout_info = kmalloc(dlen + 1, GFP_KERNEL);
1811da177e4SLinus Torvalds 		if (!callout_info)
1821da177e4SLinus Torvalds 			goto error2;
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 		ret = -EFAULT;
1851da177e4SLinus Torvalds 		if (copy_from_user(callout_info, _callout_info, dlen + 1) != 0)
1861da177e4SLinus Torvalds 			goto error3;
1871da177e4SLinus Torvalds 	}
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	/* get the destination keyring if specified */
1901da177e4SLinus Torvalds 	dest = NULL;
1911da177e4SLinus Torvalds 	if (destringid) {
1923e30148cSDavid Howells 		dest = lookup_user_key(NULL, destringid, 1, 0, KEY_WRITE);
1931da177e4SLinus Torvalds 		if (IS_ERR(dest)) {
1941da177e4SLinus Torvalds 			ret = PTR_ERR(dest);
1951da177e4SLinus Torvalds 			goto error3;
1961da177e4SLinus Torvalds 		}
1971da177e4SLinus Torvalds 	}
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	/* find the key type */
2001da177e4SLinus Torvalds 	ktype = key_type_lookup(type);
2011da177e4SLinus Torvalds 	if (IS_ERR(ktype)) {
2021da177e4SLinus Torvalds 		ret = PTR_ERR(ktype);
2031da177e4SLinus Torvalds 		goto error4;
2041da177e4SLinus Torvalds 	}
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	/* do the search */
2073e30148cSDavid Howells 	key = request_key_and_link(ktype, description, callout_info, dest);
2081da177e4SLinus Torvalds 	if (IS_ERR(key)) {
2091da177e4SLinus Torvalds 		ret = PTR_ERR(key);
2101da177e4SLinus Torvalds 		goto error5;
2111da177e4SLinus Torvalds 	}
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	ret = key->serial;
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds  	key_put(key);
2161da177e4SLinus Torvalds  error5:
2171da177e4SLinus Torvalds 	key_type_put(ktype);
2181da177e4SLinus Torvalds  error4:
2191da177e4SLinus Torvalds 	key_put(dest);
2201da177e4SLinus Torvalds  error3:
2211da177e4SLinus Torvalds 	kfree(callout_info);
2221da177e4SLinus Torvalds  error2:
2231da177e4SLinus Torvalds 	kfree(description);
2241da177e4SLinus Torvalds  error:
2251da177e4SLinus Torvalds 	return ret;
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds } /* end sys_request_key() */
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds /*****************************************************************************/
2301da177e4SLinus Torvalds /*
2311da177e4SLinus Torvalds  * get the ID of the specified process keyring
2321da177e4SLinus Torvalds  * - the keyring must have search permission to be found
2331da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_GET_KEYRING_ID)
2341da177e4SLinus Torvalds  */
2351da177e4SLinus Torvalds long keyctl_get_keyring_ID(key_serial_t id, int create)
2361da177e4SLinus Torvalds {
2371da177e4SLinus Torvalds 	struct key *key;
2381da177e4SLinus Torvalds 	long ret;
2391da177e4SLinus Torvalds 
2403e30148cSDavid Howells 	key = lookup_user_key(NULL, id, create, 0, KEY_SEARCH);
2411da177e4SLinus Torvalds 	if (IS_ERR(key)) {
2421da177e4SLinus Torvalds 		ret = PTR_ERR(key);
2431da177e4SLinus Torvalds 		goto error;
2441da177e4SLinus Torvalds 	}
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	ret = key->serial;
2471da177e4SLinus Torvalds 	key_put(key);
2481da177e4SLinus Torvalds  error:
2491da177e4SLinus Torvalds 	return ret;
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds } /* end keyctl_get_keyring_ID() */
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds /*****************************************************************************/
2541da177e4SLinus Torvalds /*
2551da177e4SLinus Torvalds  * join the session keyring
2561da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING)
2571da177e4SLinus Torvalds  */
2581da177e4SLinus Torvalds long keyctl_join_session_keyring(const char __user *_name)
2591da177e4SLinus Torvalds {
2601da177e4SLinus Torvalds 	char *name;
2611da177e4SLinus Torvalds 	long nlen, ret;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	/* fetch the name from userspace */
2641da177e4SLinus Torvalds 	name = NULL;
2651da177e4SLinus Torvalds 	if (_name) {
2661da177e4SLinus Torvalds 		ret = -EFAULT;
2671da177e4SLinus Torvalds 		nlen = strnlen_user(_name, PAGE_SIZE - 1);
2681da177e4SLinus Torvalds 		if (nlen <= 0)
2691da177e4SLinus Torvalds 			goto error;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 		ret = -EINVAL;
2721da177e4SLinus Torvalds 		if (nlen > PAGE_SIZE - 1)
2731da177e4SLinus Torvalds 			goto error;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 		ret = -ENOMEM;
2761da177e4SLinus Torvalds 		name = kmalloc(nlen + 1, GFP_KERNEL);
2771da177e4SLinus Torvalds 		if (!name)
2781da177e4SLinus Torvalds 			goto error;
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 		ret = -EFAULT;
2811da177e4SLinus Torvalds 		if (copy_from_user(name, _name, nlen + 1) != 0)
2821da177e4SLinus Torvalds 			goto error2;
2831da177e4SLinus Torvalds 	}
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	/* join the session */
2861da177e4SLinus Torvalds 	ret = join_session_keyring(name);
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds  error2:
2891da177e4SLinus Torvalds 	kfree(name);
2901da177e4SLinus Torvalds  error:
2911da177e4SLinus Torvalds 	return ret;
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds } /* end keyctl_join_session_keyring() */
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds /*****************************************************************************/
2961da177e4SLinus Torvalds /*
2971da177e4SLinus Torvalds  * update a key's data payload
2981da177e4SLinus Torvalds  * - the key must be writable
2991da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_UPDATE)
3001da177e4SLinus Torvalds  */
3011da177e4SLinus Torvalds long keyctl_update_key(key_serial_t id,
3021da177e4SLinus Torvalds 		       const void __user *_payload,
3031da177e4SLinus Torvalds 		       size_t plen)
3041da177e4SLinus Torvalds {
3051da177e4SLinus Torvalds 	struct key *key;
3061da177e4SLinus Torvalds 	void *payload;
3071da177e4SLinus Torvalds 	long ret;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	ret = -EINVAL;
3101da177e4SLinus Torvalds 	if (plen > PAGE_SIZE)
3111da177e4SLinus Torvalds 		goto error;
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
3141da177e4SLinus Torvalds 	payload = NULL;
3151da177e4SLinus Torvalds 	if (_payload) {
3161da177e4SLinus Torvalds 		ret = -ENOMEM;
3171da177e4SLinus Torvalds 		payload = kmalloc(plen, GFP_KERNEL);
3181da177e4SLinus Torvalds 		if (!payload)
3191da177e4SLinus Torvalds 			goto error;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 		ret = -EFAULT;
3221da177e4SLinus Torvalds 		if (copy_from_user(payload, _payload, plen) != 0)
3231da177e4SLinus Torvalds 			goto error2;
3241da177e4SLinus Torvalds 	}
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	/* find the target key (which must be writable) */
3273e30148cSDavid Howells 	key = lookup_user_key(NULL, id, 0, 0, KEY_WRITE);
3281da177e4SLinus Torvalds 	if (IS_ERR(key)) {
3291da177e4SLinus Torvalds 		ret = PTR_ERR(key);
3301da177e4SLinus Torvalds 		goto error2;
3311da177e4SLinus Torvalds 	}
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	/* update the key */
3341da177e4SLinus Torvalds 	ret = key_update(key, payload, plen);
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	key_put(key);
3371da177e4SLinus Torvalds  error2:
3381da177e4SLinus Torvalds 	kfree(payload);
3391da177e4SLinus Torvalds  error:
3401da177e4SLinus Torvalds 	return ret;
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds } /* end keyctl_update_key() */
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds /*****************************************************************************/
3451da177e4SLinus Torvalds /*
3461da177e4SLinus Torvalds  * revoke a key
3471da177e4SLinus Torvalds  * - the key must be writable
3481da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_REVOKE)
3491da177e4SLinus Torvalds  */
3501da177e4SLinus Torvalds long keyctl_revoke_key(key_serial_t id)
3511da177e4SLinus Torvalds {
3521da177e4SLinus Torvalds 	struct key *key;
3531da177e4SLinus Torvalds 	long ret;
3541da177e4SLinus Torvalds 
3553e30148cSDavid Howells 	key = lookup_user_key(NULL, id, 0, 0, KEY_WRITE);
3561da177e4SLinus Torvalds 	if (IS_ERR(key)) {
3571da177e4SLinus Torvalds 		ret = PTR_ERR(key);
3581da177e4SLinus Torvalds 		goto error;
3591da177e4SLinus Torvalds 	}
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	key_revoke(key);
3621da177e4SLinus Torvalds 	ret = 0;
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	key_put(key);
3651da177e4SLinus Torvalds  error:
366*1260f801SDavid Howells 	return ret;
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds } /* end keyctl_revoke_key() */
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds /*****************************************************************************/
3711da177e4SLinus Torvalds /*
3721da177e4SLinus Torvalds  * clear the specified process keyring
3731da177e4SLinus Torvalds  * - the keyring must be writable
3741da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_CLEAR)
3751da177e4SLinus Torvalds  */
3761da177e4SLinus Torvalds long keyctl_keyring_clear(key_serial_t ringid)
3771da177e4SLinus Torvalds {
3781da177e4SLinus Torvalds 	struct key *keyring;
3791da177e4SLinus Torvalds 	long ret;
3801da177e4SLinus Torvalds 
3813e30148cSDavid Howells 	keyring = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
3821da177e4SLinus Torvalds 	if (IS_ERR(keyring)) {
3831da177e4SLinus Torvalds 		ret = PTR_ERR(keyring);
3841da177e4SLinus Torvalds 		goto error;
3851da177e4SLinus Torvalds 	}
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	ret = keyring_clear(keyring);
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	key_put(keyring);
3901da177e4SLinus Torvalds  error:
3911da177e4SLinus Torvalds 	return ret;
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds } /* end keyctl_keyring_clear() */
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds /*****************************************************************************/
3961da177e4SLinus Torvalds /*
3971da177e4SLinus Torvalds  * link a key into a keyring
3981da177e4SLinus Torvalds  * - the keyring must be writable
3991da177e4SLinus Torvalds  * - the key must be linkable
4001da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_LINK)
4011da177e4SLinus Torvalds  */
4021da177e4SLinus Torvalds long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
4031da177e4SLinus Torvalds {
4041da177e4SLinus Torvalds 	struct key *keyring, *key;
4051da177e4SLinus Torvalds 	long ret;
4061da177e4SLinus Torvalds 
4073e30148cSDavid Howells 	keyring = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
4081da177e4SLinus Torvalds 	if (IS_ERR(keyring)) {
4091da177e4SLinus Torvalds 		ret = PTR_ERR(keyring);
4101da177e4SLinus Torvalds 		goto error;
4111da177e4SLinus Torvalds 	}
4121da177e4SLinus Torvalds 
4133e30148cSDavid Howells 	key = lookup_user_key(NULL, id, 1, 0, KEY_LINK);
4141da177e4SLinus Torvalds 	if (IS_ERR(key)) {
4151da177e4SLinus Torvalds 		ret = PTR_ERR(key);
4161da177e4SLinus Torvalds 		goto error2;
4171da177e4SLinus Torvalds 	}
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	ret = key_link(keyring, key);
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	key_put(key);
4221da177e4SLinus Torvalds  error2:
4231da177e4SLinus Torvalds 	key_put(keyring);
4241da177e4SLinus Torvalds  error:
4251da177e4SLinus Torvalds 	return ret;
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds } /* end keyctl_keyring_link() */
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds /*****************************************************************************/
4301da177e4SLinus Torvalds /*
4311da177e4SLinus Torvalds  * unlink the first attachment of a key from a keyring
4321da177e4SLinus Torvalds  * - the keyring must be writable
4331da177e4SLinus Torvalds  * - we don't need any permissions on the key
4341da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_UNLINK)
4351da177e4SLinus Torvalds  */
4361da177e4SLinus Torvalds long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
4371da177e4SLinus Torvalds {
4381da177e4SLinus Torvalds 	struct key *keyring, *key;
4391da177e4SLinus Torvalds 	long ret;
4401da177e4SLinus Torvalds 
4413e30148cSDavid Howells 	keyring = lookup_user_key(NULL, ringid, 0, 0, KEY_WRITE);
4421da177e4SLinus Torvalds 	if (IS_ERR(keyring)) {
4431da177e4SLinus Torvalds 		ret = PTR_ERR(keyring);
4441da177e4SLinus Torvalds 		goto error;
4451da177e4SLinus Torvalds 	}
4461da177e4SLinus Torvalds 
4473e30148cSDavid Howells 	key = lookup_user_key(NULL, id, 0, 0, 0);
4481da177e4SLinus Torvalds 	if (IS_ERR(key)) {
4491da177e4SLinus Torvalds 		ret = PTR_ERR(key);
4501da177e4SLinus Torvalds 		goto error2;
4511da177e4SLinus Torvalds 	}
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	ret = key_unlink(keyring, key);
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	key_put(key);
4561da177e4SLinus Torvalds  error2:
4571da177e4SLinus Torvalds 	key_put(keyring);
4581da177e4SLinus Torvalds  error:
4591da177e4SLinus Torvalds 	return ret;
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds } /* end keyctl_keyring_unlink() */
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds /*****************************************************************************/
4641da177e4SLinus Torvalds /*
4651da177e4SLinus Torvalds  * describe a user key
4661da177e4SLinus Torvalds  * - the key must have view permission
4671da177e4SLinus Torvalds  * - if there's a buffer, we place up to buflen bytes of data into it
4681da177e4SLinus Torvalds  * - unless there's an error, we return the amount of description available,
4691da177e4SLinus Torvalds  *   irrespective of how much we may have copied
4701da177e4SLinus Torvalds  * - the description is formatted thus:
4711da177e4SLinus Torvalds  *	type;uid;gid;perm;description<NUL>
4721da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_DESCRIBE)
4731da177e4SLinus Torvalds  */
4741da177e4SLinus Torvalds long keyctl_describe_key(key_serial_t keyid,
4751da177e4SLinus Torvalds 			 char __user *buffer,
4761da177e4SLinus Torvalds 			 size_t buflen)
4771da177e4SLinus Torvalds {
4783e30148cSDavid Howells 	struct key *key, *instkey;
4791da177e4SLinus Torvalds 	char *tmpbuf;
4801da177e4SLinus Torvalds 	long ret;
4811da177e4SLinus Torvalds 
4823e30148cSDavid Howells 	key = lookup_user_key(NULL, keyid, 0, 1, KEY_VIEW);
4831da177e4SLinus Torvalds 	if (IS_ERR(key)) {
4843e30148cSDavid Howells 		/* viewing a key under construction is permitted if we have the
4853e30148cSDavid Howells 		 * authorisation token handy */
4863e30148cSDavid Howells 		if (PTR_ERR(key) == -EACCES) {
4873e30148cSDavid Howells 			instkey = key_get_instantiation_authkey(keyid);
4883e30148cSDavid Howells 			if (!IS_ERR(instkey)) {
4893e30148cSDavid Howells 				key_put(instkey);
4903e30148cSDavid Howells 				key = lookup_user_key(NULL, keyid, 0, 1, 0);
4913e30148cSDavid Howells 				if (!IS_ERR(key))
4923e30148cSDavid Howells 					goto okay;
4933e30148cSDavid Howells 			}
4943e30148cSDavid Howells 		}
4953e30148cSDavid Howells 
4961da177e4SLinus Torvalds 		ret = PTR_ERR(key);
4971da177e4SLinus Torvalds 		goto error;
4981da177e4SLinus Torvalds 	}
4991da177e4SLinus Torvalds 
5003e30148cSDavid Howells okay:
5011da177e4SLinus Torvalds 	/* calculate how much description we're going to return */
5021da177e4SLinus Torvalds 	ret = -ENOMEM;
5031da177e4SLinus Torvalds 	tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5041da177e4SLinus Torvalds 	if (!tmpbuf)
5051da177e4SLinus Torvalds 		goto error2;
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	ret = snprintf(tmpbuf, PAGE_SIZE - 1,
5081da177e4SLinus Torvalds 		       "%s;%d;%d;%06x;%s",
5091da177e4SLinus Torvalds 		       key->type->name,
5101da177e4SLinus Torvalds 		       key->uid,
5111da177e4SLinus Torvalds 		       key->gid,
5121da177e4SLinus Torvalds 		       key->perm,
5131da177e4SLinus Torvalds 		       key->description ? key->description :""
5141da177e4SLinus Torvalds 		       );
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	/* include a NUL char at the end of the data */
5171da177e4SLinus Torvalds 	if (ret > PAGE_SIZE - 1)
5181da177e4SLinus Torvalds 		ret = PAGE_SIZE - 1;
5191da177e4SLinus Torvalds 	tmpbuf[ret] = 0;
5201da177e4SLinus Torvalds 	ret++;
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	/* consider returning the data */
5231da177e4SLinus Torvalds 	if (buffer && buflen > 0) {
5241da177e4SLinus Torvalds 		if (buflen > ret)
5251da177e4SLinus Torvalds 			buflen = ret;
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 		if (copy_to_user(buffer, tmpbuf, buflen) != 0)
5281da177e4SLinus Torvalds 			ret = -EFAULT;
5291da177e4SLinus Torvalds 	}
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	kfree(tmpbuf);
5321da177e4SLinus Torvalds  error2:
5331da177e4SLinus Torvalds 	key_put(key);
5341da177e4SLinus Torvalds  error:
5351da177e4SLinus Torvalds 	return ret;
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds } /* end keyctl_describe_key() */
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds /*****************************************************************************/
5401da177e4SLinus Torvalds /*
5411da177e4SLinus Torvalds  * search the specified keyring for a matching key
5421da177e4SLinus Torvalds  * - the start keyring must be searchable
5431da177e4SLinus Torvalds  * - nested keyrings may also be searched if they are searchable
5441da177e4SLinus Torvalds  * - only keys with search permission may be found
5451da177e4SLinus Torvalds  * - if a key is found, it will be attached to the destination keyring if
5461da177e4SLinus Torvalds  *   there's one specified
5471da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_SEARCH)
5481da177e4SLinus Torvalds  */
5491da177e4SLinus Torvalds long keyctl_keyring_search(key_serial_t ringid,
5501da177e4SLinus Torvalds 			   const char __user *_type,
5511da177e4SLinus Torvalds 			   const char __user *_description,
5521da177e4SLinus Torvalds 			   key_serial_t destringid)
5531da177e4SLinus Torvalds {
5541da177e4SLinus Torvalds 	struct key_type *ktype;
5551da177e4SLinus Torvalds 	struct key *keyring, *key, *dest;
5561da177e4SLinus Torvalds 	char type[32], *description;
5571da177e4SLinus Torvalds 	long dlen, ret;
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	/* pull the type and description into kernel space */
5601da177e4SLinus Torvalds 	ret = strncpy_from_user(type, _type, sizeof(type) - 1);
5611da177e4SLinus Torvalds 	if (ret < 0)
5621da177e4SLinus Torvalds 		goto error;
5631da177e4SLinus Torvalds 	type[31] = '\0';
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	ret = -EFAULT;
5661da177e4SLinus Torvalds 	dlen = strnlen_user(_description, PAGE_SIZE - 1);
5671da177e4SLinus Torvalds 	if (dlen <= 0)
5681da177e4SLinus Torvalds 		goto error;
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	ret = -EINVAL;
5711da177e4SLinus Torvalds 	if (dlen > PAGE_SIZE - 1)
5721da177e4SLinus Torvalds 		goto error;
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	ret = -ENOMEM;
5751da177e4SLinus Torvalds 	description = kmalloc(dlen + 1, GFP_KERNEL);
5761da177e4SLinus Torvalds 	if (!description)
5771da177e4SLinus Torvalds 		goto error;
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	ret = -EFAULT;
5801da177e4SLinus Torvalds 	if (copy_from_user(description, _description, dlen + 1) != 0)
5811da177e4SLinus Torvalds 		goto error2;
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	/* get the keyring at which to begin the search */
5843e30148cSDavid Howells 	keyring = lookup_user_key(NULL, ringid, 0, 0, KEY_SEARCH);
5851da177e4SLinus Torvalds 	if (IS_ERR(keyring)) {
5861da177e4SLinus Torvalds 		ret = PTR_ERR(keyring);
5871da177e4SLinus Torvalds 		goto error2;
5881da177e4SLinus Torvalds 	}
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	/* get the destination keyring if specified */
5911da177e4SLinus Torvalds 	dest = NULL;
5921da177e4SLinus Torvalds 	if (destringid) {
5933e30148cSDavid Howells 		dest = lookup_user_key(NULL, destringid, 1, 0, KEY_WRITE);
5941da177e4SLinus Torvalds 		if (IS_ERR(dest)) {
5951da177e4SLinus Torvalds 			ret = PTR_ERR(dest);
5961da177e4SLinus Torvalds 			goto error3;
5971da177e4SLinus Torvalds 		}
5981da177e4SLinus Torvalds 	}
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 	/* find the key type */
6011da177e4SLinus Torvalds 	ktype = key_type_lookup(type);
6021da177e4SLinus Torvalds 	if (IS_ERR(ktype)) {
6031da177e4SLinus Torvalds 		ret = PTR_ERR(ktype);
6041da177e4SLinus Torvalds 		goto error4;
6051da177e4SLinus Torvalds 	}
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	/* do the search */
6081da177e4SLinus Torvalds 	key = keyring_search(keyring, ktype, description);
6091da177e4SLinus Torvalds 	if (IS_ERR(key)) {
6101da177e4SLinus Torvalds 		ret = PTR_ERR(key);
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 		/* treat lack or presence of a negative key the same */
6131da177e4SLinus Torvalds 		if (ret == -EAGAIN)
6141da177e4SLinus Torvalds 			ret = -ENOKEY;
6151da177e4SLinus Torvalds 		goto error5;
6161da177e4SLinus Torvalds 	}
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 	/* link the resulting key to the destination keyring if we can */
6191da177e4SLinus Torvalds 	if (dest) {
6201da177e4SLinus Torvalds 		ret = -EACCES;
6211da177e4SLinus Torvalds 		if (!key_permission(key, KEY_LINK))
6221da177e4SLinus Torvalds 			goto error6;
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 		ret = key_link(dest, key);
6251da177e4SLinus Torvalds 		if (ret < 0)
6261da177e4SLinus Torvalds 			goto error6;
6271da177e4SLinus Torvalds 	}
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	ret = key->serial;
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds  error6:
6321da177e4SLinus Torvalds 	key_put(key);
6331da177e4SLinus Torvalds  error5:
6341da177e4SLinus Torvalds 	key_type_put(ktype);
6351da177e4SLinus Torvalds  error4:
6361da177e4SLinus Torvalds 	key_put(dest);
6371da177e4SLinus Torvalds  error3:
6381da177e4SLinus Torvalds 	key_put(keyring);
6391da177e4SLinus Torvalds  error2:
6401da177e4SLinus Torvalds 	kfree(description);
6411da177e4SLinus Torvalds  error:
6421da177e4SLinus Torvalds 	return ret;
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds } /* end keyctl_keyring_search() */
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds /*****************************************************************************/
6471da177e4SLinus Torvalds /*
6481da177e4SLinus Torvalds  * see if the key we're looking at is the target key
6491da177e4SLinus Torvalds  */
6501da177e4SLinus Torvalds static int keyctl_read_key_same(const struct key *key, const void *target)
6511da177e4SLinus Torvalds {
6521da177e4SLinus Torvalds 	return key == target;
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds } /* end keyctl_read_key_same() */
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds /*****************************************************************************/
6571da177e4SLinus Torvalds /*
6581da177e4SLinus Torvalds  * read a user key's payload
6591da177e4SLinus Torvalds  * - the keyring must be readable or the key must be searchable from the
6601da177e4SLinus Torvalds  *   process's keyrings
6611da177e4SLinus Torvalds  * - if there's a buffer, we place up to buflen bytes of data into it
6621da177e4SLinus Torvalds  * - unless there's an error, we return the amount of data in the key,
6631da177e4SLinus Torvalds  *   irrespective of how much we may have copied
6641da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_READ)
6651da177e4SLinus Torvalds  */
6661da177e4SLinus Torvalds long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
6671da177e4SLinus Torvalds {
6681da177e4SLinus Torvalds 	struct key *key, *skey;
6691da177e4SLinus Torvalds 	long ret;
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 	/* find the key first */
6723e30148cSDavid Howells 	key = lookup_user_key(NULL, keyid, 0, 0, 0);
6731da177e4SLinus Torvalds 	if (!IS_ERR(key)) {
6741da177e4SLinus Torvalds 		/* see if we can read it directly */
6751da177e4SLinus Torvalds 		if (key_permission(key, KEY_READ))
6761da177e4SLinus Torvalds 			goto can_read_key;
6771da177e4SLinus Torvalds 
6783e30148cSDavid Howells 		/* we can't; see if it's searchable from this process's
6793e30148cSDavid Howells 		 * keyrings
6803e30148cSDavid Howells 		 * - we automatically take account of the fact that it may be
6813e30148cSDavid Howells 		 *   dangling off an instantiation key
6823e30148cSDavid Howells 		 */
6833e30148cSDavid Howells 		skey = search_process_keyrings(key->type, key,
6843e30148cSDavid Howells 					       keyctl_read_key_same, current);
6851da177e4SLinus Torvalds 		if (!IS_ERR(skey))
6861da177e4SLinus Torvalds 			goto can_read_key2;
6871da177e4SLinus Torvalds 
6883e30148cSDavid Howells 		ret = PTR_ERR(skey);
689*1260f801SDavid Howells 		if (ret == -EAGAIN)
690*1260f801SDavid Howells 			ret = -EACCES;
6911da177e4SLinus Torvalds 		goto error2;
6921da177e4SLinus Torvalds 	}
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 	ret = -ENOKEY;
6951da177e4SLinus Torvalds 	goto error;
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	/* the key is probably readable - now try to read it */
6981da177e4SLinus Torvalds  can_read_key2:
6991da177e4SLinus Torvalds 	key_put(skey);
7001da177e4SLinus Torvalds  can_read_key:
7011da177e4SLinus Torvalds 	ret = key_validate(key);
7021da177e4SLinus Torvalds 	if (ret == 0) {
7031da177e4SLinus Torvalds 		ret = -EOPNOTSUPP;
7041da177e4SLinus Torvalds 		if (key->type->read) {
7051da177e4SLinus Torvalds 			/* read the data with the semaphore held (since we
7061da177e4SLinus Torvalds 			 * might sleep) */
7071da177e4SLinus Torvalds 			down_read(&key->sem);
7081da177e4SLinus Torvalds 			ret = key->type->read(key, buffer, buflen);
7091da177e4SLinus Torvalds 			up_read(&key->sem);
7101da177e4SLinus Torvalds 		}
7111da177e4SLinus Torvalds 	}
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds  error2:
7141da177e4SLinus Torvalds 	key_put(key);
7151da177e4SLinus Torvalds  error:
7161da177e4SLinus Torvalds 	return ret;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds } /* end keyctl_read_key() */
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds /*****************************************************************************/
7211da177e4SLinus Torvalds /*
7221da177e4SLinus Torvalds  * change the ownership of a key
7231da177e4SLinus Torvalds  * - the keyring owned by the changer
7241da177e4SLinus Torvalds  * - if the uid or gid is -1, then that parameter is not changed
7251da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_CHOWN)
7261da177e4SLinus Torvalds  */
7271da177e4SLinus Torvalds long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
7281da177e4SLinus Torvalds {
7291da177e4SLinus Torvalds 	struct key *key;
7301da177e4SLinus Torvalds 	long ret;
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	ret = 0;
7331da177e4SLinus Torvalds 	if (uid == (uid_t) -1 && gid == (gid_t) -1)
7341da177e4SLinus Torvalds 		goto error;
7351da177e4SLinus Torvalds 
7363e30148cSDavid Howells 	key = lookup_user_key(NULL, id, 1, 1, 0);
7371da177e4SLinus Torvalds 	if (IS_ERR(key)) {
7381da177e4SLinus Torvalds 		ret = PTR_ERR(key);
7391da177e4SLinus Torvalds 		goto error;
7401da177e4SLinus Torvalds 	}
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	/* make the changes with the locks held to prevent chown/chown races */
7431da177e4SLinus Torvalds 	ret = -EACCES;
7441da177e4SLinus Torvalds 	down_write(&key->sem);
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN)) {
7471da177e4SLinus Torvalds 		/* only the sysadmin can chown a key to some other UID */
7481da177e4SLinus Torvalds 		if (uid != (uid_t) -1 && key->uid != uid)
7491da177e4SLinus Torvalds 			goto no_access;
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 		/* only the sysadmin can set the key's GID to a group other
7521da177e4SLinus Torvalds 		 * than one of those that the current process subscribes to */
7531da177e4SLinus Torvalds 		if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
7541da177e4SLinus Torvalds 			goto no_access;
7551da177e4SLinus Torvalds 	}
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	/* change the UID (have to update the quotas) */
7581da177e4SLinus Torvalds 	if (uid != (uid_t) -1 && uid != key->uid) {
7591da177e4SLinus Torvalds 		/* don't support UID changing yet */
7601da177e4SLinus Torvalds 		ret = -EOPNOTSUPP;
7611da177e4SLinus Torvalds 		goto no_access;
7621da177e4SLinus Torvalds 	}
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	/* change the GID */
7651da177e4SLinus Torvalds 	if (gid != (gid_t) -1)
7661da177e4SLinus Torvalds 		key->gid = gid;
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 	ret = 0;
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds  no_access:
7711da177e4SLinus Torvalds 	up_write(&key->sem);
7721da177e4SLinus Torvalds 	key_put(key);
7731da177e4SLinus Torvalds  error:
7741da177e4SLinus Torvalds 	return ret;
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds } /* end keyctl_chown_key() */
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds /*****************************************************************************/
7791da177e4SLinus Torvalds /*
7801da177e4SLinus Torvalds  * change the permission mask on a key
7811da177e4SLinus Torvalds  * - the keyring owned by the changer
7821da177e4SLinus Torvalds  * - implements keyctl(KEYCTL_SETPERM)
7831da177e4SLinus Torvalds  */
7841da177e4SLinus Torvalds long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
7851da177e4SLinus Torvalds {
7861da177e4SLinus Torvalds 	struct key *key;
7871da177e4SLinus Torvalds 	long ret;
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	ret = -EINVAL;
7901da177e4SLinus Torvalds 	if (perm & ~(KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
7911da177e4SLinus Torvalds 		goto error;
7921da177e4SLinus Torvalds 
7933e30148cSDavid Howells 	key = lookup_user_key(NULL, id, 1, 1, 0);
7941da177e4SLinus Torvalds 	if (IS_ERR(key)) {
7951da177e4SLinus Torvalds 		ret = PTR_ERR(key);
7961da177e4SLinus Torvalds 		goto error;
7971da177e4SLinus Torvalds 	}
7981da177e4SLinus Torvalds 
79976d8aeabSDavid Howells 	/* make the changes with the locks held to prevent chown/chmod races */
8001da177e4SLinus Torvalds 	ret = -EACCES;
8011da177e4SLinus Torvalds 	down_write(&key->sem);
8021da177e4SLinus Torvalds 
80376d8aeabSDavid Howells 	/* if we're not the sysadmin, we can only change a key that we own */
80476d8aeabSDavid Howells 	if (capable(CAP_SYS_ADMIN) || key->uid == current->fsuid) {
8051da177e4SLinus Torvalds 		key->perm = perm;
8061da177e4SLinus Torvalds 		ret = 0;
80776d8aeabSDavid Howells 	}
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	up_write(&key->sem);
8101da177e4SLinus Torvalds 	key_put(key);
8111da177e4SLinus Torvalds error:
8121da177e4SLinus Torvalds 	return ret;
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds } /* end keyctl_setperm_key() */
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds /*****************************************************************************/
8171da177e4SLinus Torvalds /*
8181da177e4SLinus Torvalds  * instantiate the key with the specified payload, and, if one is given, link
8191da177e4SLinus Torvalds  * the key into the keyring
8201da177e4SLinus Torvalds  */
8211da177e4SLinus Torvalds long keyctl_instantiate_key(key_serial_t id,
8221da177e4SLinus Torvalds 			    const void __user *_payload,
8231da177e4SLinus Torvalds 			    size_t plen,
8241da177e4SLinus Torvalds 			    key_serial_t ringid)
8251da177e4SLinus Torvalds {
8263e30148cSDavid Howells 	struct request_key_auth *rka;
8273e30148cSDavid Howells 	struct key *instkey, *keyring;
8281da177e4SLinus Torvalds 	void *payload;
8291da177e4SLinus Torvalds 	long ret;
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 	ret = -EINVAL;
8321da177e4SLinus Torvalds 	if (plen > 32767)
8331da177e4SLinus Torvalds 		goto error;
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	/* pull the payload in if one was supplied */
8361da177e4SLinus Torvalds 	payload = NULL;
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	if (_payload) {
8391da177e4SLinus Torvalds 		ret = -ENOMEM;
8401da177e4SLinus Torvalds 		payload = kmalloc(plen, GFP_KERNEL);
8411da177e4SLinus Torvalds 		if (!payload)
8421da177e4SLinus Torvalds 			goto error;
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 		ret = -EFAULT;
8451da177e4SLinus Torvalds 		if (copy_from_user(payload, _payload, plen) != 0)
8461da177e4SLinus Torvalds 			goto error2;
8471da177e4SLinus Torvalds 	}
8481da177e4SLinus Torvalds 
8493e30148cSDavid Howells 	/* find the instantiation authorisation key */
8503e30148cSDavid Howells 	instkey = key_get_instantiation_authkey(id);
8513e30148cSDavid Howells 	if (IS_ERR(instkey)) {
8523e30148cSDavid Howells 		ret = PTR_ERR(instkey);
8531da177e4SLinus Torvalds 		goto error2;
8541da177e4SLinus Torvalds 	}
8551da177e4SLinus Torvalds 
8563e30148cSDavid Howells 	rka = instkey->payload.data;
8573e30148cSDavid Howells 
8583e30148cSDavid Howells 	/* find the destination keyring amongst those belonging to the
8593e30148cSDavid Howells 	 * requesting task */
8601da177e4SLinus Torvalds 	keyring = NULL;
8611da177e4SLinus Torvalds 	if (ringid) {
8623e30148cSDavid Howells 		keyring = lookup_user_key(rka->context, ringid, 1, 0,
8633e30148cSDavid Howells 					  KEY_WRITE);
8641da177e4SLinus Torvalds 		if (IS_ERR(keyring)) {
8651da177e4SLinus Torvalds 			ret = PTR_ERR(keyring);
8661da177e4SLinus Torvalds 			goto error3;
8671da177e4SLinus Torvalds 		}
8681da177e4SLinus Torvalds 	}
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	/* instantiate the key and link it into a keyring */
8713e30148cSDavid Howells 	ret = key_instantiate_and_link(rka->target_key, payload, plen,
8723e30148cSDavid Howells 				       keyring, instkey);
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	key_put(keyring);
8751da177e4SLinus Torvalds  error3:
8763e30148cSDavid Howells 	key_put(instkey);
8771da177e4SLinus Torvalds  error2:
8781da177e4SLinus Torvalds 	kfree(payload);
8791da177e4SLinus Torvalds  error:
8801da177e4SLinus Torvalds 	return ret;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds } /* end keyctl_instantiate_key() */
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds /*****************************************************************************/
8851da177e4SLinus Torvalds /*
8861da177e4SLinus Torvalds  * negatively instantiate the key with the given timeout (in seconds), and, if
8871da177e4SLinus Torvalds  * one is given, link the key into the keyring
8881da177e4SLinus Torvalds  */
8891da177e4SLinus Torvalds long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
8901da177e4SLinus Torvalds {
8913e30148cSDavid Howells 	struct request_key_auth *rka;
8923e30148cSDavid Howells 	struct key *instkey, *keyring;
8931da177e4SLinus Torvalds 	long ret;
8941da177e4SLinus Torvalds 
8953e30148cSDavid Howells 	/* find the instantiation authorisation key */
8963e30148cSDavid Howells 	instkey = key_get_instantiation_authkey(id);
8973e30148cSDavid Howells 	if (IS_ERR(instkey)) {
8983e30148cSDavid Howells 		ret = PTR_ERR(instkey);
8991da177e4SLinus Torvalds 		goto error;
9001da177e4SLinus Torvalds 	}
9011da177e4SLinus Torvalds 
9023e30148cSDavid Howells 	rka = instkey->payload.data;
9033e30148cSDavid Howells 
9041da177e4SLinus Torvalds 	/* find the destination keyring if present (which must also be
9051da177e4SLinus Torvalds 	 * writable) */
9061da177e4SLinus Torvalds 	keyring = NULL;
9071da177e4SLinus Torvalds 	if (ringid) {
9083e30148cSDavid Howells 		keyring = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
9091da177e4SLinus Torvalds 		if (IS_ERR(keyring)) {
9101da177e4SLinus Torvalds 			ret = PTR_ERR(keyring);
9111da177e4SLinus Torvalds 			goto error2;
9121da177e4SLinus Torvalds 		}
9131da177e4SLinus Torvalds 	}
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	/* instantiate the key and link it into a keyring */
9163e30148cSDavid Howells 	ret = key_negate_and_link(rka->target_key, timeout, keyring, instkey);
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	key_put(keyring);
9191da177e4SLinus Torvalds  error2:
9203e30148cSDavid Howells 	key_put(instkey);
9211da177e4SLinus Torvalds  error:
9221da177e4SLinus Torvalds 	return ret;
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds } /* end keyctl_negate_key() */
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds /*****************************************************************************/
9271da177e4SLinus Torvalds /*
9283e30148cSDavid Howells  * set the default keyring in which request_key() will cache keys
9293e30148cSDavid Howells  * - return the old setting
9303e30148cSDavid Howells  */
9313e30148cSDavid Howells long keyctl_set_reqkey_keyring(int reqkey_defl)
9323e30148cSDavid Howells {
9333e30148cSDavid Howells 	int ret;
9343e30148cSDavid Howells 
9353e30148cSDavid Howells 	switch (reqkey_defl) {
9363e30148cSDavid Howells 	case KEY_REQKEY_DEFL_THREAD_KEYRING:
9373e30148cSDavid Howells 		ret = install_thread_keyring(current);
9383e30148cSDavid Howells 		if (ret < 0)
9393e30148cSDavid Howells 			return ret;
9403e30148cSDavid Howells 		goto set;
9413e30148cSDavid Howells 
9423e30148cSDavid Howells 	case KEY_REQKEY_DEFL_PROCESS_KEYRING:
9433e30148cSDavid Howells 		ret = install_process_keyring(current);
9443e30148cSDavid Howells 		if (ret < 0)
9453e30148cSDavid Howells 			return ret;
9463e30148cSDavid Howells 
9473e30148cSDavid Howells 	case KEY_REQKEY_DEFL_DEFAULT:
9483e30148cSDavid Howells 	case KEY_REQKEY_DEFL_SESSION_KEYRING:
9493e30148cSDavid Howells 	case KEY_REQKEY_DEFL_USER_KEYRING:
9503e30148cSDavid Howells 	case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
9513e30148cSDavid Howells 	set:
9523e30148cSDavid Howells 		current->jit_keyring = reqkey_defl;
9533e30148cSDavid Howells 
9543e30148cSDavid Howells 	case KEY_REQKEY_DEFL_NO_CHANGE:
9553e30148cSDavid Howells 		return current->jit_keyring;
9563e30148cSDavid Howells 
9573e30148cSDavid Howells 	case KEY_REQKEY_DEFL_GROUP_KEYRING:
9583e30148cSDavid Howells 	default:
9593e30148cSDavid Howells 		return -EINVAL;
9603e30148cSDavid Howells 	}
9613e30148cSDavid Howells 
9623e30148cSDavid Howells } /* end keyctl_set_reqkey_keyring() */
9633e30148cSDavid Howells 
9643e30148cSDavid Howells /*****************************************************************************/
9653e30148cSDavid Howells /*
9661da177e4SLinus Torvalds  * the key control system call
9671da177e4SLinus Torvalds  */
9681da177e4SLinus Torvalds asmlinkage long sys_keyctl(int option, unsigned long arg2, unsigned long arg3,
9691da177e4SLinus Torvalds 			   unsigned long arg4, unsigned long arg5)
9701da177e4SLinus Torvalds {
9711da177e4SLinus Torvalds 	switch (option) {
9721da177e4SLinus Torvalds 	case KEYCTL_GET_KEYRING_ID:
9731da177e4SLinus Torvalds 		return keyctl_get_keyring_ID((key_serial_t) arg2,
9741da177e4SLinus Torvalds 					     (int) arg3);
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds 	case KEYCTL_JOIN_SESSION_KEYRING:
9771da177e4SLinus Torvalds 		return keyctl_join_session_keyring((const char __user *) arg2);
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 	case KEYCTL_UPDATE:
9801da177e4SLinus Torvalds 		return keyctl_update_key((key_serial_t) arg2,
9811da177e4SLinus Torvalds 					 (const void __user *) arg3,
9821da177e4SLinus Torvalds 					 (size_t) arg4);
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 	case KEYCTL_REVOKE:
9851da177e4SLinus Torvalds 		return keyctl_revoke_key((key_serial_t) arg2);
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds 	case KEYCTL_DESCRIBE:
9881da177e4SLinus Torvalds 		return keyctl_describe_key((key_serial_t) arg2,
9891da177e4SLinus Torvalds 					   (char __user *) arg3,
9901da177e4SLinus Torvalds 					   (unsigned) arg4);
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 	case KEYCTL_CLEAR:
9931da177e4SLinus Torvalds 		return keyctl_keyring_clear((key_serial_t) arg2);
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	case KEYCTL_LINK:
9961da177e4SLinus Torvalds 		return keyctl_keyring_link((key_serial_t) arg2,
9971da177e4SLinus Torvalds 					   (key_serial_t) arg3);
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 	case KEYCTL_UNLINK:
10001da177e4SLinus Torvalds 		return keyctl_keyring_unlink((key_serial_t) arg2,
10011da177e4SLinus Torvalds 					     (key_serial_t) arg3);
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds 	case KEYCTL_SEARCH:
10041da177e4SLinus Torvalds 		return keyctl_keyring_search((key_serial_t) arg2,
10051da177e4SLinus Torvalds 					     (const char __user *) arg3,
10061da177e4SLinus Torvalds 					     (const char __user *) arg4,
10071da177e4SLinus Torvalds 					     (key_serial_t) arg5);
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 	case KEYCTL_READ:
10101da177e4SLinus Torvalds 		return keyctl_read_key((key_serial_t) arg2,
10111da177e4SLinus Torvalds 				       (char __user *) arg3,
10121da177e4SLinus Torvalds 				       (size_t) arg4);
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds 	case KEYCTL_CHOWN:
10151da177e4SLinus Torvalds 		return keyctl_chown_key((key_serial_t) arg2,
10161da177e4SLinus Torvalds 					(uid_t) arg3,
10171da177e4SLinus Torvalds 					(gid_t) arg4);
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds 	case KEYCTL_SETPERM:
10201da177e4SLinus Torvalds 		return keyctl_setperm_key((key_serial_t) arg2,
10211da177e4SLinus Torvalds 					  (key_perm_t) arg3);
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 	case KEYCTL_INSTANTIATE:
10241da177e4SLinus Torvalds 		return keyctl_instantiate_key((key_serial_t) arg2,
10251da177e4SLinus Torvalds 					      (const void __user *) arg3,
10261da177e4SLinus Torvalds 					      (size_t) arg4,
10271da177e4SLinus Torvalds 					      (key_serial_t) arg5);
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	case KEYCTL_NEGATE:
10301da177e4SLinus Torvalds 		return keyctl_negate_key((key_serial_t) arg2,
10311da177e4SLinus Torvalds 					 (unsigned) arg3,
10321da177e4SLinus Torvalds 					 (key_serial_t) arg4);
10331da177e4SLinus Torvalds 
10343e30148cSDavid Howells 	case KEYCTL_SET_REQKEY_KEYRING:
10353e30148cSDavid Howells 		return keyctl_set_reqkey_keyring(arg2);
10363e30148cSDavid Howells 
10371da177e4SLinus Torvalds 	default:
10381da177e4SLinus Torvalds 		return -EOPNOTSUPP;
10391da177e4SLinus Torvalds 	}
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds } /* end sys_keyctl() */
1042