11da177e4SLinus Torvalds /* keyctl.c: userspace keyctl operations 21da177e4SLinus Torvalds * 3*3e30148cSDavid 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 52*3e30148cSDavid Howells if (!type[0]) 53*3e30148cSDavid Howells goto error; 54*3e30148cSDavid Howells 55*3e30148cSDavid Howells ret = -EPERM; 56*3e30148cSDavid Howells if (type[0] == '.') 57*3e30148cSDavid Howells goto error; 58*3e30148cSDavid Howells 591da177e4SLinus Torvalds ret = -EFAULT; 601da177e4SLinus Torvalds dlen = strnlen_user(_description, PAGE_SIZE - 1); 611da177e4SLinus Torvalds if (dlen <= 0) 621da177e4SLinus Torvalds goto error; 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds ret = -EINVAL; 651da177e4SLinus Torvalds if (dlen > PAGE_SIZE - 1) 661da177e4SLinus Torvalds goto error; 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds ret = -ENOMEM; 691da177e4SLinus Torvalds description = kmalloc(dlen + 1, GFP_KERNEL); 701da177e4SLinus Torvalds if (!description) 711da177e4SLinus Torvalds goto error; 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds ret = -EFAULT; 741da177e4SLinus Torvalds if (copy_from_user(description, _description, dlen + 1) != 0) 751da177e4SLinus Torvalds goto error2; 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 781da177e4SLinus Torvalds payload = NULL; 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds if (_payload) { 811da177e4SLinus Torvalds ret = -ENOMEM; 821da177e4SLinus Torvalds payload = kmalloc(plen, GFP_KERNEL); 831da177e4SLinus Torvalds if (!payload) 841da177e4SLinus Torvalds goto error2; 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds ret = -EFAULT; 871da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 881da177e4SLinus Torvalds goto error3; 891da177e4SLinus Torvalds } 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds /* find the target keyring (which must be writable) */ 92*3e30148cSDavid Howells keyring = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE); 931da177e4SLinus Torvalds if (IS_ERR(keyring)) { 941da177e4SLinus Torvalds ret = PTR_ERR(keyring); 951da177e4SLinus Torvalds goto error3; 961da177e4SLinus Torvalds } 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds /* create or update the requested key and add it to the target 991da177e4SLinus Torvalds * keyring */ 1001da177e4SLinus Torvalds key = key_create_or_update(keyring, type, description, 1011da177e4SLinus Torvalds payload, plen, 0); 1021da177e4SLinus Torvalds if (!IS_ERR(key)) { 1031da177e4SLinus Torvalds ret = key->serial; 1041da177e4SLinus Torvalds key_put(key); 1051da177e4SLinus Torvalds } 1061da177e4SLinus Torvalds else { 1071da177e4SLinus Torvalds ret = PTR_ERR(key); 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds key_put(keyring); 1111da177e4SLinus Torvalds error3: 1121da177e4SLinus Torvalds kfree(payload); 1131da177e4SLinus Torvalds error2: 1141da177e4SLinus Torvalds kfree(description); 1151da177e4SLinus Torvalds error: 1161da177e4SLinus Torvalds return ret; 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds } /* end sys_add_key() */ 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds /*****************************************************************************/ 1211da177e4SLinus Torvalds /* 1221da177e4SLinus Torvalds * search the process keyrings for a matching key 1231da177e4SLinus Torvalds * - nested keyrings may also be searched if they have Search permission 1241da177e4SLinus Torvalds * - if a key is found, it will be attached to the destination keyring if 1251da177e4SLinus Torvalds * there's one specified 1261da177e4SLinus Torvalds * - /sbin/request-key will be invoked if _callout_info is non-NULL 1271da177e4SLinus Torvalds * - the _callout_info string will be passed to /sbin/request-key 1281da177e4SLinus Torvalds * - if the _callout_info string is empty, it will be rendered as "-" 1291da177e4SLinus Torvalds * - implements request_key() 1301da177e4SLinus Torvalds */ 1311da177e4SLinus Torvalds asmlinkage long sys_request_key(const char __user *_type, 1321da177e4SLinus Torvalds const char __user *_description, 1331da177e4SLinus Torvalds const char __user *_callout_info, 1341da177e4SLinus Torvalds key_serial_t destringid) 1351da177e4SLinus Torvalds { 1361da177e4SLinus Torvalds struct key_type *ktype; 1371da177e4SLinus Torvalds struct key *key, *dest; 1381da177e4SLinus Torvalds char type[32], *description, *callout_info; 1391da177e4SLinus Torvalds long dlen, ret; 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds /* pull the type into kernel space */ 1421da177e4SLinus Torvalds ret = strncpy_from_user(type, _type, sizeof(type) - 1); 1431da177e4SLinus Torvalds if (ret < 0) 1441da177e4SLinus Torvalds goto error; 1451da177e4SLinus Torvalds type[31] = '\0'; 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds /* pull the description into kernel space */ 1481da177e4SLinus Torvalds ret = -EFAULT; 1491da177e4SLinus Torvalds dlen = strnlen_user(_description, PAGE_SIZE - 1); 1501da177e4SLinus Torvalds if (dlen <= 0) 1511da177e4SLinus Torvalds goto error; 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds ret = -EINVAL; 1541da177e4SLinus Torvalds if (dlen > PAGE_SIZE - 1) 1551da177e4SLinus Torvalds goto error; 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds ret = -ENOMEM; 1581da177e4SLinus Torvalds description = kmalloc(dlen + 1, GFP_KERNEL); 1591da177e4SLinus Torvalds if (!description) 1601da177e4SLinus Torvalds goto error; 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds ret = -EFAULT; 1631da177e4SLinus Torvalds if (copy_from_user(description, _description, dlen + 1) != 0) 1641da177e4SLinus Torvalds goto error2; 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds /* pull the callout info into kernel space */ 1671da177e4SLinus Torvalds callout_info = NULL; 1681da177e4SLinus Torvalds if (_callout_info) { 1691da177e4SLinus Torvalds ret = -EFAULT; 1701da177e4SLinus Torvalds dlen = strnlen_user(_callout_info, PAGE_SIZE - 1); 1711da177e4SLinus Torvalds if (dlen <= 0) 1721da177e4SLinus Torvalds goto error2; 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds ret = -EINVAL; 1751da177e4SLinus Torvalds if (dlen > PAGE_SIZE - 1) 1761da177e4SLinus Torvalds goto error2; 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds ret = -ENOMEM; 1791da177e4SLinus Torvalds callout_info = kmalloc(dlen + 1, GFP_KERNEL); 1801da177e4SLinus Torvalds if (!callout_info) 1811da177e4SLinus Torvalds goto error2; 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds ret = -EFAULT; 1841da177e4SLinus Torvalds if (copy_from_user(callout_info, _callout_info, dlen + 1) != 0) 1851da177e4SLinus Torvalds goto error3; 1861da177e4SLinus Torvalds } 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds /* get the destination keyring if specified */ 1891da177e4SLinus Torvalds dest = NULL; 1901da177e4SLinus Torvalds if (destringid) { 191*3e30148cSDavid Howells dest = lookup_user_key(NULL, destringid, 1, 0, KEY_WRITE); 1921da177e4SLinus Torvalds if (IS_ERR(dest)) { 1931da177e4SLinus Torvalds ret = PTR_ERR(dest); 1941da177e4SLinus Torvalds goto error3; 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds } 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds /* find the key type */ 1991da177e4SLinus Torvalds ktype = key_type_lookup(type); 2001da177e4SLinus Torvalds if (IS_ERR(ktype)) { 2011da177e4SLinus Torvalds ret = PTR_ERR(ktype); 2021da177e4SLinus Torvalds goto error4; 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds /* do the search */ 206*3e30148cSDavid Howells key = request_key_and_link(ktype, description, callout_info, dest); 2071da177e4SLinus Torvalds if (IS_ERR(key)) { 2081da177e4SLinus Torvalds ret = PTR_ERR(key); 2091da177e4SLinus Torvalds goto error5; 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds ret = key->serial; 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds key_put(key); 2151da177e4SLinus Torvalds error5: 2161da177e4SLinus Torvalds key_type_put(ktype); 2171da177e4SLinus Torvalds error4: 2181da177e4SLinus Torvalds key_put(dest); 2191da177e4SLinus Torvalds error3: 2201da177e4SLinus Torvalds kfree(callout_info); 2211da177e4SLinus Torvalds error2: 2221da177e4SLinus Torvalds kfree(description); 2231da177e4SLinus Torvalds error: 2241da177e4SLinus Torvalds return ret; 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds } /* end sys_request_key() */ 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds /*****************************************************************************/ 2291da177e4SLinus Torvalds /* 2301da177e4SLinus Torvalds * get the ID of the specified process keyring 2311da177e4SLinus Torvalds * - the keyring must have search permission to be found 2321da177e4SLinus Torvalds * - implements keyctl(KEYCTL_GET_KEYRING_ID) 2331da177e4SLinus Torvalds */ 2341da177e4SLinus Torvalds long keyctl_get_keyring_ID(key_serial_t id, int create) 2351da177e4SLinus Torvalds { 2361da177e4SLinus Torvalds struct key *key; 2371da177e4SLinus Torvalds long ret; 2381da177e4SLinus Torvalds 239*3e30148cSDavid Howells key = lookup_user_key(NULL, id, create, 0, KEY_SEARCH); 2401da177e4SLinus Torvalds if (IS_ERR(key)) { 2411da177e4SLinus Torvalds ret = PTR_ERR(key); 2421da177e4SLinus Torvalds goto error; 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds ret = key->serial; 2461da177e4SLinus Torvalds key_put(key); 2471da177e4SLinus Torvalds error: 2481da177e4SLinus Torvalds return ret; 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds } /* end keyctl_get_keyring_ID() */ 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds /*****************************************************************************/ 2531da177e4SLinus Torvalds /* 2541da177e4SLinus Torvalds * join the session keyring 2551da177e4SLinus Torvalds * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING) 2561da177e4SLinus Torvalds */ 2571da177e4SLinus Torvalds long keyctl_join_session_keyring(const char __user *_name) 2581da177e4SLinus Torvalds { 2591da177e4SLinus Torvalds char *name; 2601da177e4SLinus Torvalds long nlen, ret; 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds /* fetch the name from userspace */ 2631da177e4SLinus Torvalds name = NULL; 2641da177e4SLinus Torvalds if (_name) { 2651da177e4SLinus Torvalds ret = -EFAULT; 2661da177e4SLinus Torvalds nlen = strnlen_user(_name, PAGE_SIZE - 1); 2671da177e4SLinus Torvalds if (nlen <= 0) 2681da177e4SLinus Torvalds goto error; 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds ret = -EINVAL; 2711da177e4SLinus Torvalds if (nlen > PAGE_SIZE - 1) 2721da177e4SLinus Torvalds goto error; 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds ret = -ENOMEM; 2751da177e4SLinus Torvalds name = kmalloc(nlen + 1, GFP_KERNEL); 2761da177e4SLinus Torvalds if (!name) 2771da177e4SLinus Torvalds goto error; 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds ret = -EFAULT; 2801da177e4SLinus Torvalds if (copy_from_user(name, _name, nlen + 1) != 0) 2811da177e4SLinus Torvalds goto error2; 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds /* join the session */ 2851da177e4SLinus Torvalds ret = join_session_keyring(name); 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds error2: 2881da177e4SLinus Torvalds kfree(name); 2891da177e4SLinus Torvalds error: 2901da177e4SLinus Torvalds return ret; 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds } /* end keyctl_join_session_keyring() */ 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds /*****************************************************************************/ 2951da177e4SLinus Torvalds /* 2961da177e4SLinus Torvalds * update a key's data payload 2971da177e4SLinus Torvalds * - the key must be writable 2981da177e4SLinus Torvalds * - implements keyctl(KEYCTL_UPDATE) 2991da177e4SLinus Torvalds */ 3001da177e4SLinus Torvalds long keyctl_update_key(key_serial_t id, 3011da177e4SLinus Torvalds const void __user *_payload, 3021da177e4SLinus Torvalds size_t plen) 3031da177e4SLinus Torvalds { 3041da177e4SLinus Torvalds struct key *key; 3051da177e4SLinus Torvalds void *payload; 3061da177e4SLinus Torvalds long ret; 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds ret = -EINVAL; 3091da177e4SLinus Torvalds if (plen > PAGE_SIZE) 3101da177e4SLinus Torvalds goto error; 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 3131da177e4SLinus Torvalds payload = NULL; 3141da177e4SLinus Torvalds if (_payload) { 3151da177e4SLinus Torvalds ret = -ENOMEM; 3161da177e4SLinus Torvalds payload = kmalloc(plen, GFP_KERNEL); 3171da177e4SLinus Torvalds if (!payload) 3181da177e4SLinus Torvalds goto error; 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds ret = -EFAULT; 3211da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 3221da177e4SLinus Torvalds goto error2; 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds /* find the target key (which must be writable) */ 326*3e30148cSDavid Howells key = lookup_user_key(NULL, id, 0, 0, KEY_WRITE); 3271da177e4SLinus Torvalds if (IS_ERR(key)) { 3281da177e4SLinus Torvalds ret = PTR_ERR(key); 3291da177e4SLinus Torvalds goto error2; 3301da177e4SLinus Torvalds } 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds /* update the key */ 3331da177e4SLinus Torvalds ret = key_update(key, payload, plen); 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds key_put(key); 3361da177e4SLinus Torvalds error2: 3371da177e4SLinus Torvalds kfree(payload); 3381da177e4SLinus Torvalds error: 3391da177e4SLinus Torvalds return ret; 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds } /* end keyctl_update_key() */ 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds /*****************************************************************************/ 3441da177e4SLinus Torvalds /* 3451da177e4SLinus Torvalds * revoke a key 3461da177e4SLinus Torvalds * - the key must be writable 3471da177e4SLinus Torvalds * - implements keyctl(KEYCTL_REVOKE) 3481da177e4SLinus Torvalds */ 3491da177e4SLinus Torvalds long keyctl_revoke_key(key_serial_t id) 3501da177e4SLinus Torvalds { 3511da177e4SLinus Torvalds struct key *key; 3521da177e4SLinus Torvalds long ret; 3531da177e4SLinus Torvalds 354*3e30148cSDavid Howells key = lookup_user_key(NULL, id, 0, 0, KEY_WRITE); 3551da177e4SLinus Torvalds if (IS_ERR(key)) { 3561da177e4SLinus Torvalds ret = PTR_ERR(key); 3571da177e4SLinus Torvalds goto error; 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds key_revoke(key); 3611da177e4SLinus Torvalds ret = 0; 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds key_put(key); 3641da177e4SLinus Torvalds error: 3651da177e4SLinus Torvalds return 0; 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds } /* end keyctl_revoke_key() */ 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds /*****************************************************************************/ 3701da177e4SLinus Torvalds /* 3711da177e4SLinus Torvalds * clear the specified process keyring 3721da177e4SLinus Torvalds * - the keyring must be writable 3731da177e4SLinus Torvalds * - implements keyctl(KEYCTL_CLEAR) 3741da177e4SLinus Torvalds */ 3751da177e4SLinus Torvalds long keyctl_keyring_clear(key_serial_t ringid) 3761da177e4SLinus Torvalds { 3771da177e4SLinus Torvalds struct key *keyring; 3781da177e4SLinus Torvalds long ret; 3791da177e4SLinus Torvalds 380*3e30148cSDavid Howells keyring = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE); 3811da177e4SLinus Torvalds if (IS_ERR(keyring)) { 3821da177e4SLinus Torvalds ret = PTR_ERR(keyring); 3831da177e4SLinus Torvalds goto error; 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds ret = keyring_clear(keyring); 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds key_put(keyring); 3891da177e4SLinus Torvalds error: 3901da177e4SLinus Torvalds return ret; 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds } /* end keyctl_keyring_clear() */ 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds /*****************************************************************************/ 3951da177e4SLinus Torvalds /* 3961da177e4SLinus Torvalds * link a key into a keyring 3971da177e4SLinus Torvalds * - the keyring must be writable 3981da177e4SLinus Torvalds * - the key must be linkable 3991da177e4SLinus Torvalds * - implements keyctl(KEYCTL_LINK) 4001da177e4SLinus Torvalds */ 4011da177e4SLinus Torvalds long keyctl_keyring_link(key_serial_t id, key_serial_t ringid) 4021da177e4SLinus Torvalds { 4031da177e4SLinus Torvalds struct key *keyring, *key; 4041da177e4SLinus Torvalds long ret; 4051da177e4SLinus Torvalds 406*3e30148cSDavid Howells keyring = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE); 4071da177e4SLinus Torvalds if (IS_ERR(keyring)) { 4081da177e4SLinus Torvalds ret = PTR_ERR(keyring); 4091da177e4SLinus Torvalds goto error; 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds 412*3e30148cSDavid Howells key = lookup_user_key(NULL, id, 1, 0, KEY_LINK); 4131da177e4SLinus Torvalds if (IS_ERR(key)) { 4141da177e4SLinus Torvalds ret = PTR_ERR(key); 4151da177e4SLinus Torvalds goto error2; 4161da177e4SLinus Torvalds } 4171da177e4SLinus Torvalds 4181da177e4SLinus Torvalds ret = key_link(keyring, key); 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds key_put(key); 4211da177e4SLinus Torvalds error2: 4221da177e4SLinus Torvalds key_put(keyring); 4231da177e4SLinus Torvalds error: 4241da177e4SLinus Torvalds return ret; 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds } /* end keyctl_keyring_link() */ 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds /*****************************************************************************/ 4291da177e4SLinus Torvalds /* 4301da177e4SLinus Torvalds * unlink the first attachment of a key from a keyring 4311da177e4SLinus Torvalds * - the keyring must be writable 4321da177e4SLinus Torvalds * - we don't need any permissions on the key 4331da177e4SLinus Torvalds * - implements keyctl(KEYCTL_UNLINK) 4341da177e4SLinus Torvalds */ 4351da177e4SLinus Torvalds long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid) 4361da177e4SLinus Torvalds { 4371da177e4SLinus Torvalds struct key *keyring, *key; 4381da177e4SLinus Torvalds long ret; 4391da177e4SLinus Torvalds 440*3e30148cSDavid Howells keyring = lookup_user_key(NULL, ringid, 0, 0, KEY_WRITE); 4411da177e4SLinus Torvalds if (IS_ERR(keyring)) { 4421da177e4SLinus Torvalds ret = PTR_ERR(keyring); 4431da177e4SLinus Torvalds goto error; 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds 446*3e30148cSDavid Howells key = lookup_user_key(NULL, id, 0, 0, 0); 4471da177e4SLinus Torvalds if (IS_ERR(key)) { 4481da177e4SLinus Torvalds ret = PTR_ERR(key); 4491da177e4SLinus Torvalds goto error2; 4501da177e4SLinus Torvalds } 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds ret = key_unlink(keyring, key); 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds key_put(key); 4551da177e4SLinus Torvalds error2: 4561da177e4SLinus Torvalds key_put(keyring); 4571da177e4SLinus Torvalds error: 4581da177e4SLinus Torvalds return ret; 4591da177e4SLinus Torvalds 4601da177e4SLinus Torvalds } /* end keyctl_keyring_unlink() */ 4611da177e4SLinus Torvalds 4621da177e4SLinus Torvalds /*****************************************************************************/ 4631da177e4SLinus Torvalds /* 4641da177e4SLinus Torvalds * describe a user key 4651da177e4SLinus Torvalds * - the key must have view permission 4661da177e4SLinus Torvalds * - if there's a buffer, we place up to buflen bytes of data into it 4671da177e4SLinus Torvalds * - unless there's an error, we return the amount of description available, 4681da177e4SLinus Torvalds * irrespective of how much we may have copied 4691da177e4SLinus Torvalds * - the description is formatted thus: 4701da177e4SLinus Torvalds * type;uid;gid;perm;description<NUL> 4711da177e4SLinus Torvalds * - implements keyctl(KEYCTL_DESCRIBE) 4721da177e4SLinus Torvalds */ 4731da177e4SLinus Torvalds long keyctl_describe_key(key_serial_t keyid, 4741da177e4SLinus Torvalds char __user *buffer, 4751da177e4SLinus Torvalds size_t buflen) 4761da177e4SLinus Torvalds { 477*3e30148cSDavid Howells struct key *key, *instkey; 4781da177e4SLinus Torvalds char *tmpbuf; 4791da177e4SLinus Torvalds long ret; 4801da177e4SLinus Torvalds 481*3e30148cSDavid Howells key = lookup_user_key(NULL, keyid, 0, 1, KEY_VIEW); 4821da177e4SLinus Torvalds if (IS_ERR(key)) { 483*3e30148cSDavid Howells /* viewing a key under construction is permitted if we have the 484*3e30148cSDavid Howells * authorisation token handy */ 485*3e30148cSDavid Howells if (PTR_ERR(key) == -EACCES) { 486*3e30148cSDavid Howells instkey = key_get_instantiation_authkey(keyid); 487*3e30148cSDavid Howells if (!IS_ERR(instkey)) { 488*3e30148cSDavid Howells key_put(instkey); 489*3e30148cSDavid Howells key = lookup_user_key(NULL, keyid, 0, 1, 0); 490*3e30148cSDavid Howells if (!IS_ERR(key)) 491*3e30148cSDavid Howells goto okay; 492*3e30148cSDavid Howells } 493*3e30148cSDavid Howells } 494*3e30148cSDavid Howells 4951da177e4SLinus Torvalds ret = PTR_ERR(key); 4961da177e4SLinus Torvalds goto error; 4971da177e4SLinus Torvalds } 4981da177e4SLinus Torvalds 499*3e30148cSDavid Howells okay: 5001da177e4SLinus Torvalds /* calculate how much description we're going to return */ 5011da177e4SLinus Torvalds ret = -ENOMEM; 5021da177e4SLinus Torvalds tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); 5031da177e4SLinus Torvalds if (!tmpbuf) 5041da177e4SLinus Torvalds goto error2; 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds ret = snprintf(tmpbuf, PAGE_SIZE - 1, 5071da177e4SLinus Torvalds "%s;%d;%d;%06x;%s", 5081da177e4SLinus Torvalds key->type->name, 5091da177e4SLinus Torvalds key->uid, 5101da177e4SLinus Torvalds key->gid, 5111da177e4SLinus Torvalds key->perm, 5121da177e4SLinus Torvalds key->description ? key->description :"" 5131da177e4SLinus Torvalds ); 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds /* include a NUL char at the end of the data */ 5161da177e4SLinus Torvalds if (ret > PAGE_SIZE - 1) 5171da177e4SLinus Torvalds ret = PAGE_SIZE - 1; 5181da177e4SLinus Torvalds tmpbuf[ret] = 0; 5191da177e4SLinus Torvalds ret++; 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds /* consider returning the data */ 5221da177e4SLinus Torvalds if (buffer && buflen > 0) { 5231da177e4SLinus Torvalds if (buflen > ret) 5241da177e4SLinus Torvalds buflen = ret; 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds if (copy_to_user(buffer, tmpbuf, buflen) != 0) 5271da177e4SLinus Torvalds ret = -EFAULT; 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds kfree(tmpbuf); 5311da177e4SLinus Torvalds error2: 5321da177e4SLinus Torvalds key_put(key); 5331da177e4SLinus Torvalds error: 5341da177e4SLinus Torvalds return ret; 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds } /* end keyctl_describe_key() */ 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds /*****************************************************************************/ 5391da177e4SLinus Torvalds /* 5401da177e4SLinus Torvalds * search the specified keyring for a matching key 5411da177e4SLinus Torvalds * - the start keyring must be searchable 5421da177e4SLinus Torvalds * - nested keyrings may also be searched if they are searchable 5431da177e4SLinus Torvalds * - only keys with search permission may be found 5441da177e4SLinus Torvalds * - if a key is found, it will be attached to the destination keyring if 5451da177e4SLinus Torvalds * there's one specified 5461da177e4SLinus Torvalds * - implements keyctl(KEYCTL_SEARCH) 5471da177e4SLinus Torvalds */ 5481da177e4SLinus Torvalds long keyctl_keyring_search(key_serial_t ringid, 5491da177e4SLinus Torvalds const char __user *_type, 5501da177e4SLinus Torvalds const char __user *_description, 5511da177e4SLinus Torvalds key_serial_t destringid) 5521da177e4SLinus Torvalds { 5531da177e4SLinus Torvalds struct key_type *ktype; 5541da177e4SLinus Torvalds struct key *keyring, *key, *dest; 5551da177e4SLinus Torvalds char type[32], *description; 5561da177e4SLinus Torvalds long dlen, ret; 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds /* pull the type and description into kernel space */ 5591da177e4SLinus Torvalds ret = strncpy_from_user(type, _type, sizeof(type) - 1); 5601da177e4SLinus Torvalds if (ret < 0) 5611da177e4SLinus Torvalds goto error; 5621da177e4SLinus Torvalds type[31] = '\0'; 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds ret = -EFAULT; 5651da177e4SLinus Torvalds dlen = strnlen_user(_description, PAGE_SIZE - 1); 5661da177e4SLinus Torvalds if (dlen <= 0) 5671da177e4SLinus Torvalds goto error; 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds ret = -EINVAL; 5701da177e4SLinus Torvalds if (dlen > PAGE_SIZE - 1) 5711da177e4SLinus Torvalds goto error; 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds ret = -ENOMEM; 5741da177e4SLinus Torvalds description = kmalloc(dlen + 1, GFP_KERNEL); 5751da177e4SLinus Torvalds if (!description) 5761da177e4SLinus Torvalds goto error; 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds ret = -EFAULT; 5791da177e4SLinus Torvalds if (copy_from_user(description, _description, dlen + 1) != 0) 5801da177e4SLinus Torvalds goto error2; 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds /* get the keyring at which to begin the search */ 583*3e30148cSDavid Howells keyring = lookup_user_key(NULL, ringid, 0, 0, KEY_SEARCH); 5841da177e4SLinus Torvalds if (IS_ERR(keyring)) { 5851da177e4SLinus Torvalds ret = PTR_ERR(keyring); 5861da177e4SLinus Torvalds goto error2; 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds /* get the destination keyring if specified */ 5901da177e4SLinus Torvalds dest = NULL; 5911da177e4SLinus Torvalds if (destringid) { 592*3e30148cSDavid Howells dest = lookup_user_key(NULL, destringid, 1, 0, KEY_WRITE); 5931da177e4SLinus Torvalds if (IS_ERR(dest)) { 5941da177e4SLinus Torvalds ret = PTR_ERR(dest); 5951da177e4SLinus Torvalds goto error3; 5961da177e4SLinus Torvalds } 5971da177e4SLinus Torvalds } 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds /* find the key type */ 6001da177e4SLinus Torvalds ktype = key_type_lookup(type); 6011da177e4SLinus Torvalds if (IS_ERR(ktype)) { 6021da177e4SLinus Torvalds ret = PTR_ERR(ktype); 6031da177e4SLinus Torvalds goto error4; 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds 6061da177e4SLinus Torvalds /* do the search */ 6071da177e4SLinus Torvalds key = keyring_search(keyring, ktype, description); 6081da177e4SLinus Torvalds if (IS_ERR(key)) { 6091da177e4SLinus Torvalds ret = PTR_ERR(key); 6101da177e4SLinus Torvalds 6111da177e4SLinus Torvalds /* treat lack or presence of a negative key the same */ 6121da177e4SLinus Torvalds if (ret == -EAGAIN) 6131da177e4SLinus Torvalds ret = -ENOKEY; 6141da177e4SLinus Torvalds goto error5; 6151da177e4SLinus Torvalds } 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds /* link the resulting key to the destination keyring if we can */ 6181da177e4SLinus Torvalds if (dest) { 6191da177e4SLinus Torvalds ret = -EACCES; 6201da177e4SLinus Torvalds if (!key_permission(key, KEY_LINK)) 6211da177e4SLinus Torvalds goto error6; 6221da177e4SLinus Torvalds 6231da177e4SLinus Torvalds ret = key_link(dest, key); 6241da177e4SLinus Torvalds if (ret < 0) 6251da177e4SLinus Torvalds goto error6; 6261da177e4SLinus Torvalds } 6271da177e4SLinus Torvalds 6281da177e4SLinus Torvalds ret = key->serial; 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds error6: 6311da177e4SLinus Torvalds key_put(key); 6321da177e4SLinus Torvalds error5: 6331da177e4SLinus Torvalds key_type_put(ktype); 6341da177e4SLinus Torvalds error4: 6351da177e4SLinus Torvalds key_put(dest); 6361da177e4SLinus Torvalds error3: 6371da177e4SLinus Torvalds key_put(keyring); 6381da177e4SLinus Torvalds error2: 6391da177e4SLinus Torvalds kfree(description); 6401da177e4SLinus Torvalds error: 6411da177e4SLinus Torvalds return ret; 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds } /* end keyctl_keyring_search() */ 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds /*****************************************************************************/ 6461da177e4SLinus Torvalds /* 6471da177e4SLinus Torvalds * see if the key we're looking at is the target key 6481da177e4SLinus Torvalds */ 6491da177e4SLinus Torvalds static int keyctl_read_key_same(const struct key *key, const void *target) 6501da177e4SLinus Torvalds { 6511da177e4SLinus Torvalds return key == target; 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds } /* end keyctl_read_key_same() */ 6541da177e4SLinus Torvalds 6551da177e4SLinus Torvalds /*****************************************************************************/ 6561da177e4SLinus Torvalds /* 6571da177e4SLinus Torvalds * read a user key's payload 6581da177e4SLinus Torvalds * - the keyring must be readable or the key must be searchable from the 6591da177e4SLinus Torvalds * process's keyrings 6601da177e4SLinus Torvalds * - if there's a buffer, we place up to buflen bytes of data into it 6611da177e4SLinus Torvalds * - unless there's an error, we return the amount of data in the key, 6621da177e4SLinus Torvalds * irrespective of how much we may have copied 6631da177e4SLinus Torvalds * - implements keyctl(KEYCTL_READ) 6641da177e4SLinus Torvalds */ 6651da177e4SLinus Torvalds long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen) 6661da177e4SLinus Torvalds { 6671da177e4SLinus Torvalds struct key *key, *skey; 6681da177e4SLinus Torvalds long ret; 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds /* find the key first */ 671*3e30148cSDavid Howells key = lookup_user_key(NULL, keyid, 0, 0, 0); 6721da177e4SLinus Torvalds if (!IS_ERR(key)) { 6731da177e4SLinus Torvalds /* see if we can read it directly */ 6741da177e4SLinus Torvalds if (key_permission(key, KEY_READ)) 6751da177e4SLinus Torvalds goto can_read_key; 6761da177e4SLinus Torvalds 677*3e30148cSDavid Howells /* we can't; see if it's searchable from this process's 678*3e30148cSDavid Howells * keyrings 679*3e30148cSDavid Howells * - we automatically take account of the fact that it may be 680*3e30148cSDavid Howells * dangling off an instantiation key 681*3e30148cSDavid Howells */ 682*3e30148cSDavid Howells skey = search_process_keyrings(key->type, key, 683*3e30148cSDavid Howells keyctl_read_key_same, current); 6841da177e4SLinus Torvalds if (!IS_ERR(skey)) 6851da177e4SLinus Torvalds goto can_read_key2; 6861da177e4SLinus Torvalds 687*3e30148cSDavid Howells ret = PTR_ERR(skey); 6881da177e4SLinus Torvalds goto error2; 6891da177e4SLinus Torvalds } 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds ret = -ENOKEY; 6921da177e4SLinus Torvalds goto error; 6931da177e4SLinus Torvalds 6941da177e4SLinus Torvalds /* the key is probably readable - now try to read it */ 6951da177e4SLinus Torvalds can_read_key2: 6961da177e4SLinus Torvalds key_put(skey); 6971da177e4SLinus Torvalds can_read_key: 6981da177e4SLinus Torvalds ret = key_validate(key); 6991da177e4SLinus Torvalds if (ret == 0) { 7001da177e4SLinus Torvalds ret = -EOPNOTSUPP; 7011da177e4SLinus Torvalds if (key->type->read) { 7021da177e4SLinus Torvalds /* read the data with the semaphore held (since we 7031da177e4SLinus Torvalds * might sleep) */ 7041da177e4SLinus Torvalds down_read(&key->sem); 7051da177e4SLinus Torvalds ret = key->type->read(key, buffer, buflen); 7061da177e4SLinus Torvalds up_read(&key->sem); 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds error2: 7111da177e4SLinus Torvalds key_put(key); 7121da177e4SLinus Torvalds error: 7131da177e4SLinus Torvalds return ret; 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds } /* end keyctl_read_key() */ 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds /*****************************************************************************/ 7181da177e4SLinus Torvalds /* 7191da177e4SLinus Torvalds * change the ownership of a key 7201da177e4SLinus Torvalds * - the keyring owned by the changer 7211da177e4SLinus Torvalds * - if the uid or gid is -1, then that parameter is not changed 7221da177e4SLinus Torvalds * - implements keyctl(KEYCTL_CHOWN) 7231da177e4SLinus Torvalds */ 7241da177e4SLinus Torvalds long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid) 7251da177e4SLinus Torvalds { 7261da177e4SLinus Torvalds struct key *key; 7271da177e4SLinus Torvalds long ret; 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds ret = 0; 7301da177e4SLinus Torvalds if (uid == (uid_t) -1 && gid == (gid_t) -1) 7311da177e4SLinus Torvalds goto error; 7321da177e4SLinus Torvalds 733*3e30148cSDavid Howells key = lookup_user_key(NULL, id, 1, 1, 0); 7341da177e4SLinus Torvalds if (IS_ERR(key)) { 7351da177e4SLinus Torvalds ret = PTR_ERR(key); 7361da177e4SLinus Torvalds goto error; 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds 7391da177e4SLinus Torvalds /* make the changes with the locks held to prevent chown/chown races */ 7401da177e4SLinus Torvalds ret = -EACCES; 7411da177e4SLinus Torvalds down_write(&key->sem); 7421da177e4SLinus Torvalds 7431da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) { 7441da177e4SLinus Torvalds /* only the sysadmin can chown a key to some other UID */ 7451da177e4SLinus Torvalds if (uid != (uid_t) -1 && key->uid != uid) 7461da177e4SLinus Torvalds goto no_access; 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds /* only the sysadmin can set the key's GID to a group other 7491da177e4SLinus Torvalds * than one of those that the current process subscribes to */ 7501da177e4SLinus Torvalds if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid)) 7511da177e4SLinus Torvalds goto no_access; 7521da177e4SLinus Torvalds } 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds /* change the UID (have to update the quotas) */ 7551da177e4SLinus Torvalds if (uid != (uid_t) -1 && uid != key->uid) { 7561da177e4SLinus Torvalds /* don't support UID changing yet */ 7571da177e4SLinus Torvalds ret = -EOPNOTSUPP; 7581da177e4SLinus Torvalds goto no_access; 7591da177e4SLinus Torvalds } 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds /* change the GID */ 7621da177e4SLinus Torvalds if (gid != (gid_t) -1) 7631da177e4SLinus Torvalds key->gid = gid; 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds ret = 0; 7661da177e4SLinus Torvalds 7671da177e4SLinus Torvalds no_access: 7681da177e4SLinus Torvalds up_write(&key->sem); 7691da177e4SLinus Torvalds key_put(key); 7701da177e4SLinus Torvalds error: 7711da177e4SLinus Torvalds return ret; 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds } /* end keyctl_chown_key() */ 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds /*****************************************************************************/ 7761da177e4SLinus Torvalds /* 7771da177e4SLinus Torvalds * change the permission mask on a key 7781da177e4SLinus Torvalds * - the keyring owned by the changer 7791da177e4SLinus Torvalds * - implements keyctl(KEYCTL_SETPERM) 7801da177e4SLinus Torvalds */ 7811da177e4SLinus Torvalds long keyctl_setperm_key(key_serial_t id, key_perm_t perm) 7821da177e4SLinus Torvalds { 7831da177e4SLinus Torvalds struct key *key; 7841da177e4SLinus Torvalds long ret; 7851da177e4SLinus Torvalds 7861da177e4SLinus Torvalds ret = -EINVAL; 7871da177e4SLinus Torvalds if (perm & ~(KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL)) 7881da177e4SLinus Torvalds goto error; 7891da177e4SLinus Torvalds 790*3e30148cSDavid Howells key = lookup_user_key(NULL, id, 1, 1, 0); 7911da177e4SLinus Torvalds if (IS_ERR(key)) { 7921da177e4SLinus Torvalds ret = PTR_ERR(key); 7931da177e4SLinus Torvalds goto error; 7941da177e4SLinus Torvalds } 7951da177e4SLinus Torvalds 79676d8aeabSDavid Howells /* make the changes with the locks held to prevent chown/chmod races */ 7971da177e4SLinus Torvalds ret = -EACCES; 7981da177e4SLinus Torvalds down_write(&key->sem); 7991da177e4SLinus Torvalds 80076d8aeabSDavid Howells /* if we're not the sysadmin, we can only change a key that we own */ 80176d8aeabSDavid Howells if (capable(CAP_SYS_ADMIN) || key->uid == current->fsuid) { 8021da177e4SLinus Torvalds key->perm = perm; 8031da177e4SLinus Torvalds ret = 0; 80476d8aeabSDavid Howells } 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds up_write(&key->sem); 8071da177e4SLinus Torvalds key_put(key); 8081da177e4SLinus Torvalds error: 8091da177e4SLinus Torvalds return ret; 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds } /* end keyctl_setperm_key() */ 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds /*****************************************************************************/ 8141da177e4SLinus Torvalds /* 8151da177e4SLinus Torvalds * instantiate the key with the specified payload, and, if one is given, link 8161da177e4SLinus Torvalds * the key into the keyring 8171da177e4SLinus Torvalds */ 8181da177e4SLinus Torvalds long keyctl_instantiate_key(key_serial_t id, 8191da177e4SLinus Torvalds const void __user *_payload, 8201da177e4SLinus Torvalds size_t plen, 8211da177e4SLinus Torvalds key_serial_t ringid) 8221da177e4SLinus Torvalds { 823*3e30148cSDavid Howells struct request_key_auth *rka; 824*3e30148cSDavid Howells struct key *instkey, *keyring; 8251da177e4SLinus Torvalds void *payload; 8261da177e4SLinus Torvalds long ret; 8271da177e4SLinus Torvalds 8281da177e4SLinus Torvalds ret = -EINVAL; 8291da177e4SLinus Torvalds if (plen > 32767) 8301da177e4SLinus Torvalds goto error; 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 8331da177e4SLinus Torvalds payload = NULL; 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds if (_payload) { 8361da177e4SLinus Torvalds ret = -ENOMEM; 8371da177e4SLinus Torvalds payload = kmalloc(plen, GFP_KERNEL); 8381da177e4SLinus Torvalds if (!payload) 8391da177e4SLinus Torvalds goto error; 8401da177e4SLinus Torvalds 8411da177e4SLinus Torvalds ret = -EFAULT; 8421da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 8431da177e4SLinus Torvalds goto error2; 8441da177e4SLinus Torvalds } 8451da177e4SLinus Torvalds 846*3e30148cSDavid Howells /* find the instantiation authorisation key */ 847*3e30148cSDavid Howells instkey = key_get_instantiation_authkey(id); 848*3e30148cSDavid Howells if (IS_ERR(instkey)) { 849*3e30148cSDavid Howells ret = PTR_ERR(instkey); 8501da177e4SLinus Torvalds goto error2; 8511da177e4SLinus Torvalds } 8521da177e4SLinus Torvalds 853*3e30148cSDavid Howells rka = instkey->payload.data; 854*3e30148cSDavid Howells 855*3e30148cSDavid Howells /* find the destination keyring amongst those belonging to the 856*3e30148cSDavid Howells * requesting task */ 8571da177e4SLinus Torvalds keyring = NULL; 8581da177e4SLinus Torvalds if (ringid) { 859*3e30148cSDavid Howells keyring = lookup_user_key(rka->context, ringid, 1, 0, 860*3e30148cSDavid Howells KEY_WRITE); 8611da177e4SLinus Torvalds if (IS_ERR(keyring)) { 8621da177e4SLinus Torvalds ret = PTR_ERR(keyring); 8631da177e4SLinus Torvalds goto error3; 8641da177e4SLinus Torvalds } 8651da177e4SLinus Torvalds } 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds /* instantiate the key and link it into a keyring */ 868*3e30148cSDavid Howells ret = key_instantiate_and_link(rka->target_key, payload, plen, 869*3e30148cSDavid Howells keyring, instkey); 8701da177e4SLinus Torvalds 8711da177e4SLinus Torvalds key_put(keyring); 8721da177e4SLinus Torvalds error3: 873*3e30148cSDavid Howells key_put(instkey); 8741da177e4SLinus Torvalds error2: 8751da177e4SLinus Torvalds kfree(payload); 8761da177e4SLinus Torvalds error: 8771da177e4SLinus Torvalds return ret; 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds } /* end keyctl_instantiate_key() */ 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds /*****************************************************************************/ 8821da177e4SLinus Torvalds /* 8831da177e4SLinus Torvalds * negatively instantiate the key with the given timeout (in seconds), and, if 8841da177e4SLinus Torvalds * one is given, link the key into the keyring 8851da177e4SLinus Torvalds */ 8861da177e4SLinus Torvalds long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid) 8871da177e4SLinus Torvalds { 888*3e30148cSDavid Howells struct request_key_auth *rka; 889*3e30148cSDavid Howells struct key *instkey, *keyring; 8901da177e4SLinus Torvalds long ret; 8911da177e4SLinus Torvalds 892*3e30148cSDavid Howells /* find the instantiation authorisation key */ 893*3e30148cSDavid Howells instkey = key_get_instantiation_authkey(id); 894*3e30148cSDavid Howells if (IS_ERR(instkey)) { 895*3e30148cSDavid Howells ret = PTR_ERR(instkey); 8961da177e4SLinus Torvalds goto error; 8971da177e4SLinus Torvalds } 8981da177e4SLinus Torvalds 899*3e30148cSDavid Howells rka = instkey->payload.data; 900*3e30148cSDavid Howells 9011da177e4SLinus Torvalds /* find the destination keyring if present (which must also be 9021da177e4SLinus Torvalds * writable) */ 9031da177e4SLinus Torvalds keyring = NULL; 9041da177e4SLinus Torvalds if (ringid) { 905*3e30148cSDavid Howells keyring = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE); 9061da177e4SLinus Torvalds if (IS_ERR(keyring)) { 9071da177e4SLinus Torvalds ret = PTR_ERR(keyring); 9081da177e4SLinus Torvalds goto error2; 9091da177e4SLinus Torvalds } 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds /* instantiate the key and link it into a keyring */ 913*3e30148cSDavid Howells ret = key_negate_and_link(rka->target_key, timeout, keyring, instkey); 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds key_put(keyring); 9161da177e4SLinus Torvalds error2: 917*3e30148cSDavid Howells key_put(instkey); 9181da177e4SLinus Torvalds error: 9191da177e4SLinus Torvalds return ret; 9201da177e4SLinus Torvalds 9211da177e4SLinus Torvalds } /* end keyctl_negate_key() */ 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds /*****************************************************************************/ 9241da177e4SLinus Torvalds /* 925*3e30148cSDavid Howells * set the default keyring in which request_key() will cache keys 926*3e30148cSDavid Howells * - return the old setting 927*3e30148cSDavid Howells */ 928*3e30148cSDavid Howells long keyctl_set_reqkey_keyring(int reqkey_defl) 929*3e30148cSDavid Howells { 930*3e30148cSDavid Howells int ret; 931*3e30148cSDavid Howells 932*3e30148cSDavid Howells switch (reqkey_defl) { 933*3e30148cSDavid Howells case KEY_REQKEY_DEFL_THREAD_KEYRING: 934*3e30148cSDavid Howells ret = install_thread_keyring(current); 935*3e30148cSDavid Howells if (ret < 0) 936*3e30148cSDavid Howells return ret; 937*3e30148cSDavid Howells goto set; 938*3e30148cSDavid Howells 939*3e30148cSDavid Howells case KEY_REQKEY_DEFL_PROCESS_KEYRING: 940*3e30148cSDavid Howells ret = install_process_keyring(current); 941*3e30148cSDavid Howells if (ret < 0) 942*3e30148cSDavid Howells return ret; 943*3e30148cSDavid Howells 944*3e30148cSDavid Howells case KEY_REQKEY_DEFL_DEFAULT: 945*3e30148cSDavid Howells case KEY_REQKEY_DEFL_SESSION_KEYRING: 946*3e30148cSDavid Howells case KEY_REQKEY_DEFL_USER_KEYRING: 947*3e30148cSDavid Howells case KEY_REQKEY_DEFL_USER_SESSION_KEYRING: 948*3e30148cSDavid Howells set: 949*3e30148cSDavid Howells current->jit_keyring = reqkey_defl; 950*3e30148cSDavid Howells 951*3e30148cSDavid Howells case KEY_REQKEY_DEFL_NO_CHANGE: 952*3e30148cSDavid Howells return current->jit_keyring; 953*3e30148cSDavid Howells 954*3e30148cSDavid Howells case KEY_REQKEY_DEFL_GROUP_KEYRING: 955*3e30148cSDavid Howells default: 956*3e30148cSDavid Howells return -EINVAL; 957*3e30148cSDavid Howells } 958*3e30148cSDavid Howells 959*3e30148cSDavid Howells } /* end keyctl_set_reqkey_keyring() */ 960*3e30148cSDavid Howells 961*3e30148cSDavid Howells /*****************************************************************************/ 962*3e30148cSDavid Howells /* 9631da177e4SLinus Torvalds * the key control system call 9641da177e4SLinus Torvalds */ 9651da177e4SLinus Torvalds asmlinkage long sys_keyctl(int option, unsigned long arg2, unsigned long arg3, 9661da177e4SLinus Torvalds unsigned long arg4, unsigned long arg5) 9671da177e4SLinus Torvalds { 9681da177e4SLinus Torvalds switch (option) { 9691da177e4SLinus Torvalds case KEYCTL_GET_KEYRING_ID: 9701da177e4SLinus Torvalds return keyctl_get_keyring_ID((key_serial_t) arg2, 9711da177e4SLinus Torvalds (int) arg3); 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds case KEYCTL_JOIN_SESSION_KEYRING: 9741da177e4SLinus Torvalds return keyctl_join_session_keyring((const char __user *) arg2); 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds case KEYCTL_UPDATE: 9771da177e4SLinus Torvalds return keyctl_update_key((key_serial_t) arg2, 9781da177e4SLinus Torvalds (const void __user *) arg3, 9791da177e4SLinus Torvalds (size_t) arg4); 9801da177e4SLinus Torvalds 9811da177e4SLinus Torvalds case KEYCTL_REVOKE: 9821da177e4SLinus Torvalds return keyctl_revoke_key((key_serial_t) arg2); 9831da177e4SLinus Torvalds 9841da177e4SLinus Torvalds case KEYCTL_DESCRIBE: 9851da177e4SLinus Torvalds return keyctl_describe_key((key_serial_t) arg2, 9861da177e4SLinus Torvalds (char __user *) arg3, 9871da177e4SLinus Torvalds (unsigned) arg4); 9881da177e4SLinus Torvalds 9891da177e4SLinus Torvalds case KEYCTL_CLEAR: 9901da177e4SLinus Torvalds return keyctl_keyring_clear((key_serial_t) arg2); 9911da177e4SLinus Torvalds 9921da177e4SLinus Torvalds case KEYCTL_LINK: 9931da177e4SLinus Torvalds return keyctl_keyring_link((key_serial_t) arg2, 9941da177e4SLinus Torvalds (key_serial_t) arg3); 9951da177e4SLinus Torvalds 9961da177e4SLinus Torvalds case KEYCTL_UNLINK: 9971da177e4SLinus Torvalds return keyctl_keyring_unlink((key_serial_t) arg2, 9981da177e4SLinus Torvalds (key_serial_t) arg3); 9991da177e4SLinus Torvalds 10001da177e4SLinus Torvalds case KEYCTL_SEARCH: 10011da177e4SLinus Torvalds return keyctl_keyring_search((key_serial_t) arg2, 10021da177e4SLinus Torvalds (const char __user *) arg3, 10031da177e4SLinus Torvalds (const char __user *) arg4, 10041da177e4SLinus Torvalds (key_serial_t) arg5); 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds case KEYCTL_READ: 10071da177e4SLinus Torvalds return keyctl_read_key((key_serial_t) arg2, 10081da177e4SLinus Torvalds (char __user *) arg3, 10091da177e4SLinus Torvalds (size_t) arg4); 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds case KEYCTL_CHOWN: 10121da177e4SLinus Torvalds return keyctl_chown_key((key_serial_t) arg2, 10131da177e4SLinus Torvalds (uid_t) arg3, 10141da177e4SLinus Torvalds (gid_t) arg4); 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds case KEYCTL_SETPERM: 10171da177e4SLinus Torvalds return keyctl_setperm_key((key_serial_t) arg2, 10181da177e4SLinus Torvalds (key_perm_t) arg3); 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds case KEYCTL_INSTANTIATE: 10211da177e4SLinus Torvalds return keyctl_instantiate_key((key_serial_t) arg2, 10221da177e4SLinus Torvalds (const void __user *) arg3, 10231da177e4SLinus Torvalds (size_t) arg4, 10241da177e4SLinus Torvalds (key_serial_t) arg5); 10251da177e4SLinus Torvalds 10261da177e4SLinus Torvalds case KEYCTL_NEGATE: 10271da177e4SLinus Torvalds return keyctl_negate_key((key_serial_t) arg2, 10281da177e4SLinus Torvalds (unsigned) arg3, 10291da177e4SLinus Torvalds (key_serial_t) arg4); 10301da177e4SLinus Torvalds 1031*3e30148cSDavid Howells case KEYCTL_SET_REQKEY_KEYRING: 1032*3e30148cSDavid Howells return keyctl_set_reqkey_keyring(arg2); 1033*3e30148cSDavid Howells 10341da177e4SLinus Torvalds default: 10351da177e4SLinus Torvalds return -EOPNOTSUPP; 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds } /* end sys_keyctl() */ 1039