12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2973c9f4fSDavid Howells /* Userspace key control operations 31da177e4SLinus Torvalds * 43e30148cSDavid Howells * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved. 51da177e4SLinus Torvalds * Written by David Howells (dhowells@redhat.com) 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds #include <linux/init.h> 91da177e4SLinus Torvalds #include <linux/sched.h> 1029930025SIngo Molnar #include <linux/sched/task.h> 111da177e4SLinus Torvalds #include <linux/slab.h> 121da177e4SLinus Torvalds #include <linux/syscalls.h> 1359e6b9c1SBryan Schumaker #include <linux/key.h> 141da177e4SLinus Torvalds #include <linux/keyctl.h> 151da177e4SLinus Torvalds #include <linux/fs.h> 16c59ede7bSRandy.Dunlap #include <linux/capability.h> 175b825c3aSIngo Molnar #include <linux/cred.h> 180cb409d9SDavi Arnaut #include <linux/string.h> 191da177e4SLinus Torvalds #include <linux/err.h> 2038bbca6bSDavid Howells #include <linux/vmalloc.h> 2170a5bb72SDavid Howells #include <linux/security.h> 22a27bb332SKent Overstreet #include <linux/uio.h> 237c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 24822ad64dSDavid Howells #include <keys/request_key_auth-type.h> 251da177e4SLinus Torvalds #include "internal.h" 261da177e4SLinus Torvalds 27aa9d4437SDavid Howells #define KEY_MAX_DESC_SIZE 4096 28aa9d4437SDavid Howells 29b206f281SDavid Howells static const unsigned char keyrings_capabilities[2] = { 3045e0f30cSDavid Howells [0] = (KEYCTL_CAPS0_CAPABILITIES | 3145e0f30cSDavid Howells (IS_ENABLED(CONFIG_PERSISTENT_KEYRINGS) ? KEYCTL_CAPS0_PERSISTENT_KEYRINGS : 0) | 3245e0f30cSDavid Howells (IS_ENABLED(CONFIG_KEY_DH_OPERATIONS) ? KEYCTL_CAPS0_DIFFIE_HELLMAN : 0) | 3345e0f30cSDavid Howells (IS_ENABLED(CONFIG_ASYMMETRIC_KEY_TYPE) ? KEYCTL_CAPS0_PUBLIC_KEY : 0) | 3445e0f30cSDavid Howells (IS_ENABLED(CONFIG_BIG_KEYS) ? KEYCTL_CAPS0_BIG_KEY : 0) | 3545e0f30cSDavid Howells KEYCTL_CAPS0_INVALIDATE | 3645e0f30cSDavid Howells KEYCTL_CAPS0_RESTRICT_KEYRING | 3745e0f30cSDavid Howells KEYCTL_CAPS0_MOVE 3845e0f30cSDavid Howells ), 393b6e4de0SDavid Howells [1] = (KEYCTL_CAPS1_NS_KEYRING_NAME | 40028db3e2SLinus Torvalds KEYCTL_CAPS1_NS_KEY_TAG), 4145e0f30cSDavid Howells }; 4245e0f30cSDavid Howells 430cb409d9SDavi Arnaut static int key_get_type_from_user(char *type, 440cb409d9SDavi Arnaut const char __user *_type, 450cb409d9SDavi Arnaut unsigned len) 460cb409d9SDavi Arnaut { 470cb409d9SDavi Arnaut int ret; 480cb409d9SDavi Arnaut 490cb409d9SDavi Arnaut ret = strncpy_from_user(type, _type, len); 500cb409d9SDavi Arnaut if (ret < 0) 514303ef19SDan Carpenter return ret; 520cb409d9SDavi Arnaut if (ret == 0 || ret >= len) 530cb409d9SDavi Arnaut return -EINVAL; 5454e2c2c1SDavid Howells if (type[0] == '.') 5554e2c2c1SDavid Howells return -EPERM; 560cb409d9SDavi Arnaut type[len - 1] = '\0'; 570cb409d9SDavi Arnaut return 0; 580cb409d9SDavi Arnaut } 590cb409d9SDavi Arnaut 601da177e4SLinus Torvalds /* 61973c9f4fSDavid Howells * Extract the description of a new key from userspace and either add it as a 62973c9f4fSDavid Howells * new key to the specified keyring or update a matching key in that keyring. 63973c9f4fSDavid Howells * 64cf7f601cSDavid Howells * If the description is NULL or an empty string, the key type is asked to 65cf7f601cSDavid Howells * generate one from the payload. 66cf7f601cSDavid Howells * 67973c9f4fSDavid Howells * The keyring must be writable so that we can attach the key to it. 68973c9f4fSDavid Howells * 69973c9f4fSDavid Howells * If successful, the new key's serial number is returned, otherwise an error 70973c9f4fSDavid Howells * code is returned. 711da177e4SLinus Torvalds */ 721e7bfb21SHeiko Carstens SYSCALL_DEFINE5(add_key, const char __user *, _type, 731e7bfb21SHeiko Carstens const char __user *, _description, 741e7bfb21SHeiko Carstens const void __user *, _payload, 751e7bfb21SHeiko Carstens size_t, plen, 761e7bfb21SHeiko Carstens key_serial_t, ringid) 771da177e4SLinus Torvalds { 78664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 791da177e4SLinus Torvalds char type[32], *description; 801da177e4SLinus Torvalds void *payload; 810cb409d9SDavi Arnaut long ret; 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds ret = -EINVAL; 8438bbca6bSDavid Howells if (plen > 1024 * 1024 - 1) 851da177e4SLinus Torvalds goto error; 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds /* draw all the data into kernel space */ 880cb409d9SDavi Arnaut ret = key_get_type_from_user(type, _type, sizeof(type)); 891da177e4SLinus Torvalds if (ret < 0) 901da177e4SLinus Torvalds goto error; 911da177e4SLinus Torvalds 92cf7f601cSDavid Howells description = NULL; 93cf7f601cSDavid Howells if (_description) { 94aa9d4437SDavid Howells description = strndup_user(_description, KEY_MAX_DESC_SIZE); 950cb409d9SDavi Arnaut if (IS_ERR(description)) { 960cb409d9SDavi Arnaut ret = PTR_ERR(description); 973e30148cSDavid Howells goto error; 980cb409d9SDavi Arnaut } 99cf7f601cSDavid Howells if (!*description) { 100cf7f601cSDavid Howells kfree(description); 101cf7f601cSDavid Howells description = NULL; 102a4e3b8d7SMimi Zohar } else if ((description[0] == '.') && 103a4e3b8d7SMimi Zohar (strncmp(type, "keyring", 7) == 0)) { 104a4e3b8d7SMimi Zohar ret = -EPERM; 105a4e3b8d7SMimi Zohar goto error2; 106cf7f601cSDavid Howells } 107cf7f601cSDavid Howells } 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 1101da177e4SLinus Torvalds payload = NULL; 1111da177e4SLinus Torvalds 1125649645dSEric Biggers if (plen) { 1131da177e4SLinus Torvalds ret = -ENOMEM; 114752ade68SMichal Hocko payload = kvmalloc(plen, GFP_KERNEL); 1151da177e4SLinus Torvalds if (!payload) 1161da177e4SLinus Torvalds goto error2; 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds ret = -EFAULT; 1191da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 1201da177e4SLinus Torvalds goto error3; 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds /* find the target keyring (which must be writable) */ 124f5895943SDavid Howells keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); 125664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 126664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 1271da177e4SLinus Torvalds goto error3; 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds /* create or update the requested key and add it to the target 1311da177e4SLinus Torvalds * keyring */ 132664cceb0SDavid Howells key_ref = key_create_or_update(keyring_ref, type, description, 133028db3e2SLinus Torvalds payload, plen, KEY_PERM_UNDEF, 134028db3e2SLinus Torvalds KEY_ALLOC_IN_QUOTA); 135664cceb0SDavid Howells if (!IS_ERR(key_ref)) { 136664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 137664cceb0SDavid Howells key_ref_put(key_ref); 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds else { 140664cceb0SDavid Howells ret = PTR_ERR(key_ref); 1411da177e4SLinus Torvalds } 1421da177e4SLinus Torvalds 143664cceb0SDavid Howells key_ref_put(keyring_ref); 1441da177e4SLinus Torvalds error3: 145*d4eaa283SWaiman Long kvfree_sensitive(payload, plen); 1461da177e4SLinus Torvalds error2: 1471da177e4SLinus Torvalds kfree(description); 1481da177e4SLinus Torvalds error: 1491da177e4SLinus Torvalds return ret; 150a8b17ed0SDavid Howells } 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds /* 153973c9f4fSDavid Howells * Search the process keyrings and keyring trees linked from those for a 154973c9f4fSDavid Howells * matching key. Keyrings must have appropriate Search permission to be 155973c9f4fSDavid Howells * searched. 156973c9f4fSDavid Howells * 157973c9f4fSDavid Howells * If a key is found, it will be attached to the destination keyring if there's 158973c9f4fSDavid Howells * one specified and the serial number of the key will be returned. 159973c9f4fSDavid Howells * 160973c9f4fSDavid Howells * If no key is found, /sbin/request-key will be invoked if _callout_info is 161973c9f4fSDavid Howells * non-NULL in an attempt to create a key. The _callout_info string will be 162973c9f4fSDavid Howells * passed to /sbin/request-key to aid with completing the request. If the 163973c9f4fSDavid Howells * _callout_info string is "" then it will be changed to "-". 1641da177e4SLinus Torvalds */ 1651e7bfb21SHeiko Carstens SYSCALL_DEFINE4(request_key, const char __user *, _type, 1661e7bfb21SHeiko Carstens const char __user *, _description, 1671e7bfb21SHeiko Carstens const char __user *, _callout_info, 1681e7bfb21SHeiko Carstens key_serial_t, destringid) 1691da177e4SLinus Torvalds { 1701da177e4SLinus Torvalds struct key_type *ktype; 171664cceb0SDavid Howells struct key *key; 172664cceb0SDavid Howells key_ref_t dest_ref; 1734a38e122SDavid Howells size_t callout_len; 1741da177e4SLinus Torvalds char type[32], *description, *callout_info; 1750cb409d9SDavi Arnaut long ret; 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds /* pull the type into kernel space */ 1780cb409d9SDavi Arnaut ret = key_get_type_from_user(type, _type, sizeof(type)); 1791da177e4SLinus Torvalds if (ret < 0) 1801da177e4SLinus Torvalds goto error; 1811260f801SDavid Howells 1821da177e4SLinus Torvalds /* pull the description into kernel space */ 183aa9d4437SDavid Howells description = strndup_user(_description, KEY_MAX_DESC_SIZE); 1840cb409d9SDavi Arnaut if (IS_ERR(description)) { 1850cb409d9SDavi Arnaut ret = PTR_ERR(description); 1861da177e4SLinus Torvalds goto error; 1870cb409d9SDavi Arnaut } 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds /* pull the callout info into kernel space */ 1901da177e4SLinus Torvalds callout_info = NULL; 1914a38e122SDavid Howells callout_len = 0; 1921da177e4SLinus Torvalds if (_callout_info) { 1930cb409d9SDavi Arnaut callout_info = strndup_user(_callout_info, PAGE_SIZE); 1940cb409d9SDavi Arnaut if (IS_ERR(callout_info)) { 1950cb409d9SDavi Arnaut ret = PTR_ERR(callout_info); 1961da177e4SLinus Torvalds goto error2; 1970cb409d9SDavi Arnaut } 1984a38e122SDavid Howells callout_len = strlen(callout_info); 1991da177e4SLinus Torvalds } 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds /* get the destination keyring if specified */ 202664cceb0SDavid Howells dest_ref = NULL; 2031da177e4SLinus Torvalds if (destringid) { 2045593122eSDavid Howells dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE, 205f5895943SDavid Howells KEY_NEED_WRITE); 206664cceb0SDavid Howells if (IS_ERR(dest_ref)) { 207664cceb0SDavid Howells ret = PTR_ERR(dest_ref); 2081da177e4SLinus Torvalds goto error3; 2091da177e4SLinus Torvalds } 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds /* find the key type */ 2131da177e4SLinus Torvalds ktype = key_type_lookup(type); 2141da177e4SLinus Torvalds if (IS_ERR(ktype)) { 2151da177e4SLinus Torvalds ret = PTR_ERR(ktype); 2161da177e4SLinus Torvalds goto error4; 2171da177e4SLinus Torvalds } 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds /* do the search */ 220a58946c1SDavid Howells key = request_key_and_link(ktype, description, NULL, callout_info, 221028db3e2SLinus Torvalds callout_len, NULL, key_ref_to_ptr(dest_ref), 2227e047ef5SDavid Howells KEY_ALLOC_IN_QUOTA); 2231da177e4SLinus Torvalds if (IS_ERR(key)) { 2241da177e4SLinus Torvalds ret = PTR_ERR(key); 2251da177e4SLinus Torvalds goto error5; 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds 2284aab1e89SDavid Howells /* wait for the key to finish being constructed */ 2294aab1e89SDavid Howells ret = wait_for_key_construction(key, 1); 2304aab1e89SDavid Howells if (ret < 0) 2314aab1e89SDavid Howells goto error6; 2324aab1e89SDavid Howells 2331da177e4SLinus Torvalds ret = key->serial; 2341da177e4SLinus Torvalds 2354aab1e89SDavid Howells error6: 2361da177e4SLinus Torvalds key_put(key); 2371da177e4SLinus Torvalds error5: 2381da177e4SLinus Torvalds key_type_put(ktype); 2391da177e4SLinus Torvalds error4: 240664cceb0SDavid Howells key_ref_put(dest_ref); 2411da177e4SLinus Torvalds error3: 2421da177e4SLinus Torvalds kfree(callout_info); 2431da177e4SLinus Torvalds error2: 2441da177e4SLinus Torvalds kfree(description); 2451da177e4SLinus Torvalds error: 2461da177e4SLinus Torvalds return ret; 247a8b17ed0SDavid Howells } 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds /* 250973c9f4fSDavid Howells * Get the ID of the specified process keyring. 251973c9f4fSDavid Howells * 252973c9f4fSDavid Howells * The requested keyring must have search permission to be found. 253973c9f4fSDavid Howells * 254973c9f4fSDavid Howells * If successful, the ID of the requested keyring will be returned. 2551da177e4SLinus Torvalds */ 2561da177e4SLinus Torvalds long keyctl_get_keyring_ID(key_serial_t id, int create) 2571da177e4SLinus Torvalds { 258664cceb0SDavid Howells key_ref_t key_ref; 2595593122eSDavid Howells unsigned long lflags; 2601da177e4SLinus Torvalds long ret; 2611da177e4SLinus Torvalds 2625593122eSDavid Howells lflags = create ? KEY_LOOKUP_CREATE : 0; 263f5895943SDavid Howells key_ref = lookup_user_key(id, lflags, KEY_NEED_SEARCH); 264664cceb0SDavid Howells if (IS_ERR(key_ref)) { 265664cceb0SDavid Howells ret = PTR_ERR(key_ref); 2661da177e4SLinus Torvalds goto error; 2671da177e4SLinus Torvalds } 2681da177e4SLinus Torvalds 269664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 270664cceb0SDavid Howells key_ref_put(key_ref); 2711da177e4SLinus Torvalds error: 2721da177e4SLinus Torvalds return ret; 273973c9f4fSDavid Howells } 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds /* 276973c9f4fSDavid Howells * Join a (named) session keyring. 277973c9f4fSDavid Howells * 278973c9f4fSDavid Howells * Create and join an anonymous session keyring or join a named session 279973c9f4fSDavid Howells * keyring, creating it if necessary. A named session keyring must have Search 280973c9f4fSDavid Howells * permission for it to be joined. Session keyrings without this permit will 281ee8f844eSDavid Howells * be skipped over. It is not permitted for userspace to create or join 282ee8f844eSDavid Howells * keyrings whose name begin with a dot. 283973c9f4fSDavid Howells * 284973c9f4fSDavid Howells * If successful, the ID of the joined session keyring will be returned. 2851da177e4SLinus Torvalds */ 2861da177e4SLinus Torvalds long keyctl_join_session_keyring(const char __user *_name) 2871da177e4SLinus Torvalds { 2881da177e4SLinus Torvalds char *name; 2890cb409d9SDavi Arnaut long ret; 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds /* fetch the name from userspace */ 2921da177e4SLinus Torvalds name = NULL; 2931da177e4SLinus Torvalds if (_name) { 294aa9d4437SDavid Howells name = strndup_user(_name, KEY_MAX_DESC_SIZE); 2950cb409d9SDavi Arnaut if (IS_ERR(name)) { 2960cb409d9SDavi Arnaut ret = PTR_ERR(name); 2971da177e4SLinus Torvalds goto error; 2980cb409d9SDavi Arnaut } 299ee8f844eSDavid Howells 300ee8f844eSDavid Howells ret = -EPERM; 301ee8f844eSDavid Howells if (name[0] == '.') 302ee8f844eSDavid Howells goto error_name; 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds /* join the session */ 3061da177e4SLinus Torvalds ret = join_session_keyring(name); 307ee8f844eSDavid Howells error_name: 3080d54ee1cSVegard Nossum kfree(name); 3091da177e4SLinus Torvalds error: 3101da177e4SLinus Torvalds return ret; 311a8b17ed0SDavid Howells } 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds /* 314973c9f4fSDavid Howells * Update a key's data payload from the given data. 315973c9f4fSDavid Howells * 316973c9f4fSDavid Howells * The key must grant the caller Write permission and the key type must support 317973c9f4fSDavid Howells * updating for this to work. A negative key can be positively instantiated 318973c9f4fSDavid Howells * with this call. 319973c9f4fSDavid Howells * 320973c9f4fSDavid Howells * If successful, 0 will be returned. If the key type does not support 321973c9f4fSDavid Howells * updating, then -EOPNOTSUPP will be returned. 3221da177e4SLinus Torvalds */ 3231da177e4SLinus Torvalds long keyctl_update_key(key_serial_t id, 3241da177e4SLinus Torvalds const void __user *_payload, 3251da177e4SLinus Torvalds size_t plen) 3261da177e4SLinus Torvalds { 327664cceb0SDavid Howells key_ref_t key_ref; 3281da177e4SLinus Torvalds void *payload; 3291da177e4SLinus Torvalds long ret; 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds ret = -EINVAL; 3321da177e4SLinus Torvalds if (plen > PAGE_SIZE) 3331da177e4SLinus Torvalds goto error; 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 3361da177e4SLinus Torvalds payload = NULL; 3375649645dSEric Biggers if (plen) { 3381da177e4SLinus Torvalds ret = -ENOMEM; 3394f088249SWaiman Long payload = kvmalloc(plen, GFP_KERNEL); 3401da177e4SLinus Torvalds if (!payload) 3411da177e4SLinus Torvalds goto error; 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds ret = -EFAULT; 3441da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 3451da177e4SLinus Torvalds goto error2; 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds /* find the target key (which must be writable) */ 349f5895943SDavid Howells key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE); 350664cceb0SDavid Howells if (IS_ERR(key_ref)) { 351664cceb0SDavid Howells ret = PTR_ERR(key_ref); 3521da177e4SLinus Torvalds goto error2; 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds /* update the key */ 356664cceb0SDavid Howells ret = key_update(key_ref, payload, plen); 3571da177e4SLinus Torvalds 358664cceb0SDavid Howells key_ref_put(key_ref); 3591da177e4SLinus Torvalds error2: 360*d4eaa283SWaiman Long kvfree_sensitive(payload, plen); 3611da177e4SLinus Torvalds error: 3621da177e4SLinus Torvalds return ret; 363a8b17ed0SDavid Howells } 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds /* 366973c9f4fSDavid Howells * Revoke a key. 367973c9f4fSDavid Howells * 368973c9f4fSDavid Howells * The key must be grant the caller Write or Setattr permission for this to 369973c9f4fSDavid Howells * work. The key type should give up its quota claim when revoked. The key 370973c9f4fSDavid Howells * and any links to the key will be automatically garbage collected after a 371973c9f4fSDavid Howells * certain amount of time (/proc/sys/kernel/keys/gc_delay). 372973c9f4fSDavid Howells * 373d3600bcfSMimi Zohar * Keys with KEY_FLAG_KEEP set should not be revoked. 374d3600bcfSMimi Zohar * 375973c9f4fSDavid Howells * If successful, 0 is returned. 3761da177e4SLinus Torvalds */ 3771da177e4SLinus Torvalds long keyctl_revoke_key(key_serial_t id) 3781da177e4SLinus Torvalds { 379664cceb0SDavid Howells key_ref_t key_ref; 380d3600bcfSMimi Zohar struct key *key; 3811da177e4SLinus Torvalds long ret; 3821da177e4SLinus Torvalds 383028db3e2SLinus Torvalds key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE); 384028db3e2SLinus Torvalds if (IS_ERR(key_ref)) { 385028db3e2SLinus Torvalds ret = PTR_ERR(key_ref); 386028db3e2SLinus Torvalds if (ret != -EACCES) 387028db3e2SLinus Torvalds goto error; 388028db3e2SLinus Torvalds key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR); 3890c2c9a3fSDavid Howells if (IS_ERR(key_ref)) { 3900c2c9a3fSDavid Howells ret = PTR_ERR(key_ref); 3910c2c9a3fSDavid Howells goto error; 3920c2c9a3fSDavid Howells } 393028db3e2SLinus Torvalds } 3941da177e4SLinus Torvalds 395d3600bcfSMimi Zohar key = key_ref_to_ptr(key_ref); 3961da177e4SLinus Torvalds ret = 0; 3971d6d167cSMimi Zohar if (test_bit(KEY_FLAG_KEEP, &key->flags)) 3981d6d167cSMimi Zohar ret = -EPERM; 3991d6d167cSMimi Zohar else 4001d6d167cSMimi Zohar key_revoke(key); 4011da177e4SLinus Torvalds 402664cceb0SDavid Howells key_ref_put(key_ref); 4031da177e4SLinus Torvalds error: 4041260f801SDavid Howells return ret; 405a8b17ed0SDavid Howells } 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds /* 408fd75815fSDavid Howells * Invalidate a key. 409fd75815fSDavid Howells * 410fd75815fSDavid Howells * The key must be grant the caller Invalidate permission for this to work. 411fd75815fSDavid Howells * The key and any links to the key will be automatically garbage collected 412fd75815fSDavid Howells * immediately. 413fd75815fSDavid Howells * 414d3600bcfSMimi Zohar * Keys with KEY_FLAG_KEEP set should not be invalidated. 415d3600bcfSMimi Zohar * 416fd75815fSDavid Howells * If successful, 0 is returned. 417fd75815fSDavid Howells */ 418fd75815fSDavid Howells long keyctl_invalidate_key(key_serial_t id) 419fd75815fSDavid Howells { 420fd75815fSDavid Howells key_ref_t key_ref; 421d3600bcfSMimi Zohar struct key *key; 422fd75815fSDavid Howells long ret; 423fd75815fSDavid Howells 424fd75815fSDavid Howells kenter("%d", id); 425fd75815fSDavid Howells 426028db3e2SLinus Torvalds key_ref = lookup_user_key(id, 0, KEY_NEED_SEARCH); 427fd75815fSDavid Howells if (IS_ERR(key_ref)) { 428fd75815fSDavid Howells ret = PTR_ERR(key_ref); 4290c7774abSDavid Howells 4300c7774abSDavid Howells /* Root is permitted to invalidate certain special keys */ 4310c7774abSDavid Howells if (capable(CAP_SYS_ADMIN)) { 4320c7774abSDavid Howells key_ref = lookup_user_key(id, 0, 0); 4330c7774abSDavid Howells if (IS_ERR(key_ref)) 4340c7774abSDavid Howells goto error; 4350c7774abSDavid Howells if (test_bit(KEY_FLAG_ROOT_CAN_INVAL, 4360c7774abSDavid Howells &key_ref_to_ptr(key_ref)->flags)) 4370c7774abSDavid Howells goto invalidate; 4380c7774abSDavid Howells goto error_put; 4390c7774abSDavid Howells } 4400c7774abSDavid Howells 441fd75815fSDavid Howells goto error; 442fd75815fSDavid Howells } 443fd75815fSDavid Howells 4440c7774abSDavid Howells invalidate: 445d3600bcfSMimi Zohar key = key_ref_to_ptr(key_ref); 446fd75815fSDavid Howells ret = 0; 447d3600bcfSMimi Zohar if (test_bit(KEY_FLAG_KEEP, &key->flags)) 448d3600bcfSMimi Zohar ret = -EPERM; 4491d6d167cSMimi Zohar else 450d3600bcfSMimi Zohar key_invalidate(key); 4510c7774abSDavid Howells error_put: 452fd75815fSDavid Howells key_ref_put(key_ref); 453fd75815fSDavid Howells error: 454fd75815fSDavid Howells kleave(" = %ld", ret); 455fd75815fSDavid Howells return ret; 456fd75815fSDavid Howells } 457fd75815fSDavid Howells 458fd75815fSDavid Howells /* 459973c9f4fSDavid Howells * Clear the specified keyring, creating an empty process keyring if one of the 460973c9f4fSDavid Howells * special keyring IDs is used. 461973c9f4fSDavid Howells * 462d3600bcfSMimi Zohar * The keyring must grant the caller Write permission and not have 463d3600bcfSMimi Zohar * KEY_FLAG_KEEP set for this to work. If successful, 0 will be returned. 4641da177e4SLinus Torvalds */ 4651da177e4SLinus Torvalds long keyctl_keyring_clear(key_serial_t ringid) 4661da177e4SLinus Torvalds { 467664cceb0SDavid Howells key_ref_t keyring_ref; 468d3600bcfSMimi Zohar struct key *keyring; 4691da177e4SLinus Torvalds long ret; 4701da177e4SLinus Torvalds 471028db3e2SLinus Torvalds keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); 472664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 473664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 474700920ebSDavid Howells 475700920ebSDavid Howells /* Root is permitted to invalidate certain special keyrings */ 476700920ebSDavid Howells if (capable(CAP_SYS_ADMIN)) { 477700920ebSDavid Howells keyring_ref = lookup_user_key(ringid, 0, 0); 478700920ebSDavid Howells if (IS_ERR(keyring_ref)) 479700920ebSDavid Howells goto error; 480700920ebSDavid Howells if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR, 481700920ebSDavid Howells &key_ref_to_ptr(keyring_ref)->flags)) 482700920ebSDavid Howells goto clear; 483700920ebSDavid Howells goto error_put; 484700920ebSDavid Howells } 485700920ebSDavid Howells 4861da177e4SLinus Torvalds goto error; 4871da177e4SLinus Torvalds } 4881da177e4SLinus Torvalds 489700920ebSDavid Howells clear: 490d3600bcfSMimi Zohar keyring = key_ref_to_ptr(keyring_ref); 491d3600bcfSMimi Zohar if (test_bit(KEY_FLAG_KEEP, &keyring->flags)) 492d3600bcfSMimi Zohar ret = -EPERM; 493d3600bcfSMimi Zohar else 494d3600bcfSMimi Zohar ret = keyring_clear(keyring); 495700920ebSDavid Howells error_put: 496664cceb0SDavid Howells key_ref_put(keyring_ref); 4971da177e4SLinus Torvalds error: 4981da177e4SLinus Torvalds return ret; 499a8b17ed0SDavid Howells } 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds /* 502973c9f4fSDavid Howells * Create a link from a keyring to a key if there's no matching key in the 503973c9f4fSDavid Howells * keyring, otherwise replace the link to the matching key with a link to the 504973c9f4fSDavid Howells * new key. 505973c9f4fSDavid Howells * 506973c9f4fSDavid Howells * The key must grant the caller Link permission and the the keyring must grant 507973c9f4fSDavid Howells * the caller Write permission. Furthermore, if an additional link is created, 508973c9f4fSDavid Howells * the keyring's quota will be extended. 509973c9f4fSDavid Howells * 510973c9f4fSDavid Howells * If successful, 0 will be returned. 5111da177e4SLinus Torvalds */ 5121da177e4SLinus Torvalds long keyctl_keyring_link(key_serial_t id, key_serial_t ringid) 5131da177e4SLinus Torvalds { 514664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 5151da177e4SLinus Torvalds long ret; 5161da177e4SLinus Torvalds 517f5895943SDavid Howells keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); 518664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 519664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 5201da177e4SLinus Torvalds goto error; 5211da177e4SLinus Torvalds } 5221da177e4SLinus Torvalds 523f5895943SDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK); 524664cceb0SDavid Howells if (IS_ERR(key_ref)) { 525664cceb0SDavid Howells ret = PTR_ERR(key_ref); 5261da177e4SLinus Torvalds goto error2; 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds 529664cceb0SDavid Howells ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref)); 5301da177e4SLinus Torvalds 531664cceb0SDavid Howells key_ref_put(key_ref); 5321da177e4SLinus Torvalds error2: 533664cceb0SDavid Howells key_ref_put(keyring_ref); 5341da177e4SLinus Torvalds error: 5351da177e4SLinus Torvalds return ret; 536a8b17ed0SDavid Howells } 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds /* 539973c9f4fSDavid Howells * Unlink a key from a keyring. 540973c9f4fSDavid Howells * 541973c9f4fSDavid Howells * The keyring must grant the caller Write permission for this to work; the key 542973c9f4fSDavid Howells * itself need not grant the caller anything. If the last link to a key is 543973c9f4fSDavid Howells * removed then that key will be scheduled for destruction. 544973c9f4fSDavid Howells * 545d3600bcfSMimi Zohar * Keys or keyrings with KEY_FLAG_KEEP set should not be unlinked. 546d3600bcfSMimi Zohar * 547973c9f4fSDavid Howells * If successful, 0 will be returned. 5481da177e4SLinus Torvalds */ 5491da177e4SLinus Torvalds long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid) 5501da177e4SLinus Torvalds { 551664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 552d3600bcfSMimi Zohar struct key *keyring, *key; 5531da177e4SLinus Torvalds long ret; 5541da177e4SLinus Torvalds 555f5895943SDavid Howells keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE); 556664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 557664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 5581da177e4SLinus Torvalds goto error; 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds 5615593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0); 562664cceb0SDavid Howells if (IS_ERR(key_ref)) { 563664cceb0SDavid Howells ret = PTR_ERR(key_ref); 5641da177e4SLinus Torvalds goto error2; 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds 567d3600bcfSMimi Zohar keyring = key_ref_to_ptr(keyring_ref); 568d3600bcfSMimi Zohar key = key_ref_to_ptr(key_ref); 569d3600bcfSMimi Zohar if (test_bit(KEY_FLAG_KEEP, &keyring->flags) && 570d3600bcfSMimi Zohar test_bit(KEY_FLAG_KEEP, &key->flags)) 571d3600bcfSMimi Zohar ret = -EPERM; 572d3600bcfSMimi Zohar else 573d3600bcfSMimi Zohar ret = key_unlink(keyring, key); 5741da177e4SLinus Torvalds 575664cceb0SDavid Howells key_ref_put(key_ref); 5761da177e4SLinus Torvalds error2: 577664cceb0SDavid Howells key_ref_put(keyring_ref); 5781da177e4SLinus Torvalds error: 5791da177e4SLinus Torvalds return ret; 580a8b17ed0SDavid Howells } 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds /* 583ed0ac5c7SDavid Howells * Move a link to a key from one keyring to another, displacing any matching 584ed0ac5c7SDavid Howells * key from the destination keyring. 585ed0ac5c7SDavid Howells * 586ed0ac5c7SDavid Howells * The key must grant the caller Link permission and both keyrings must grant 587ed0ac5c7SDavid Howells * the caller Write permission. There must also be a link in the from keyring 588ed0ac5c7SDavid Howells * to the key. If both keyrings are the same, nothing is done. 589ed0ac5c7SDavid Howells * 590ed0ac5c7SDavid Howells * If successful, 0 will be returned. 591ed0ac5c7SDavid Howells */ 592ed0ac5c7SDavid Howells long keyctl_keyring_move(key_serial_t id, key_serial_t from_ringid, 593ed0ac5c7SDavid Howells key_serial_t to_ringid, unsigned int flags) 594ed0ac5c7SDavid Howells { 595ed0ac5c7SDavid Howells key_ref_t key_ref, from_ref, to_ref; 596ed0ac5c7SDavid Howells long ret; 597ed0ac5c7SDavid Howells 598ed0ac5c7SDavid Howells if (flags & ~KEYCTL_MOVE_EXCL) 599ed0ac5c7SDavid Howells return -EINVAL; 600ed0ac5c7SDavid Howells 601ed0ac5c7SDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK); 602ed0ac5c7SDavid Howells if (IS_ERR(key_ref)) 603ed0ac5c7SDavid Howells return PTR_ERR(key_ref); 604ed0ac5c7SDavid Howells 605ed0ac5c7SDavid Howells from_ref = lookup_user_key(from_ringid, 0, KEY_NEED_WRITE); 606ed0ac5c7SDavid Howells if (IS_ERR(from_ref)) { 607ed0ac5c7SDavid Howells ret = PTR_ERR(from_ref); 608ed0ac5c7SDavid Howells goto error2; 609ed0ac5c7SDavid Howells } 610ed0ac5c7SDavid Howells 611ed0ac5c7SDavid Howells to_ref = lookup_user_key(to_ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); 612ed0ac5c7SDavid Howells if (IS_ERR(to_ref)) { 613ed0ac5c7SDavid Howells ret = PTR_ERR(to_ref); 614ed0ac5c7SDavid Howells goto error3; 615ed0ac5c7SDavid Howells } 616ed0ac5c7SDavid Howells 617ed0ac5c7SDavid Howells ret = key_move(key_ref_to_ptr(key_ref), key_ref_to_ptr(from_ref), 618ed0ac5c7SDavid Howells key_ref_to_ptr(to_ref), flags); 619ed0ac5c7SDavid Howells 620ed0ac5c7SDavid Howells key_ref_put(to_ref); 621ed0ac5c7SDavid Howells error3: 622ed0ac5c7SDavid Howells key_ref_put(from_ref); 623ed0ac5c7SDavid Howells error2: 624ed0ac5c7SDavid Howells key_ref_put(key_ref); 625ed0ac5c7SDavid Howells return ret; 626ed0ac5c7SDavid Howells } 627ed0ac5c7SDavid Howells 628ed0ac5c7SDavid Howells /* 629973c9f4fSDavid Howells * Return a description of a key to userspace. 630973c9f4fSDavid Howells * 631973c9f4fSDavid Howells * The key must grant the caller View permission for this to work. 632973c9f4fSDavid Howells * 633973c9f4fSDavid Howells * If there's a buffer, we place up to buflen bytes of data into it formatted 634973c9f4fSDavid Howells * in the following way: 635973c9f4fSDavid Howells * 6361da177e4SLinus Torvalds * type;uid;gid;perm;description<NUL> 637973c9f4fSDavid Howells * 638973c9f4fSDavid Howells * If successful, we return the amount of description available, irrespective 639973c9f4fSDavid Howells * of how much we may have copied into the buffer. 6401da177e4SLinus Torvalds */ 6411da177e4SLinus Torvalds long keyctl_describe_key(key_serial_t keyid, 6421da177e4SLinus Torvalds char __user *buffer, 6431da177e4SLinus Torvalds size_t buflen) 6441da177e4SLinus Torvalds { 6453e30148cSDavid Howells struct key *key, *instkey; 646664cceb0SDavid Howells key_ref_t key_ref; 647aa9d4437SDavid Howells char *infobuf; 6481da177e4SLinus Torvalds long ret; 649aa9d4437SDavid Howells int desclen, infolen; 6501da177e4SLinus Torvalds 651f5895943SDavid Howells key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW); 652664cceb0SDavid Howells if (IS_ERR(key_ref)) { 6533e30148cSDavid Howells /* viewing a key under construction is permitted if we have the 6543e30148cSDavid Howells * authorisation token handy */ 655664cceb0SDavid Howells if (PTR_ERR(key_ref) == -EACCES) { 6563e30148cSDavid Howells instkey = key_get_instantiation_authkey(keyid); 6573e30148cSDavid Howells if (!IS_ERR(instkey)) { 6583e30148cSDavid Howells key_put(instkey); 6598bbf4976SDavid Howells key_ref = lookup_user_key(keyid, 6605593122eSDavid Howells KEY_LOOKUP_PARTIAL, 6615593122eSDavid Howells 0); 662664cceb0SDavid Howells if (!IS_ERR(key_ref)) 6633e30148cSDavid Howells goto okay; 6643e30148cSDavid Howells } 6653e30148cSDavid Howells } 6663e30148cSDavid Howells 667664cceb0SDavid Howells ret = PTR_ERR(key_ref); 6681da177e4SLinus Torvalds goto error; 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds 6713e30148cSDavid Howells okay: 672664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 673aa9d4437SDavid Howells desclen = strlen(key->description); 674664cceb0SDavid Howells 675aa9d4437SDavid Howells /* calculate how much information we're going to return */ 676aa9d4437SDavid Howells ret = -ENOMEM; 677aa9d4437SDavid Howells infobuf = kasprintf(GFP_KERNEL, 678aa9d4437SDavid Howells "%s;%d;%d;%08x;", 67994fd8405SDavid Howells key->type->name, 6809a56c2dbSEric W. Biederman from_kuid_munged(current_user_ns(), key->uid), 6819a56c2dbSEric W. Biederman from_kgid_munged(current_user_ns(), key->gid), 682028db3e2SLinus Torvalds key->perm); 683aa9d4437SDavid Howells if (!infobuf) 684aa9d4437SDavid Howells goto error2; 685aa9d4437SDavid Howells infolen = strlen(infobuf); 686aa9d4437SDavid Howells ret = infolen + desclen + 1; 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds /* consider returning the data */ 689aa9d4437SDavid Howells if (buffer && buflen >= ret) { 690aa9d4437SDavid Howells if (copy_to_user(buffer, infobuf, infolen) != 0 || 691aa9d4437SDavid Howells copy_to_user(buffer + infolen, key->description, 692aa9d4437SDavid Howells desclen + 1) != 0) 6931da177e4SLinus Torvalds ret = -EFAULT; 6941da177e4SLinus Torvalds } 6951da177e4SLinus Torvalds 696aa9d4437SDavid Howells kfree(infobuf); 6971da177e4SLinus Torvalds error2: 698664cceb0SDavid Howells key_ref_put(key_ref); 6991da177e4SLinus Torvalds error: 7001da177e4SLinus Torvalds return ret; 701a8b17ed0SDavid Howells } 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds /* 704973c9f4fSDavid Howells * Search the specified keyring and any keyrings it links to for a matching 705973c9f4fSDavid Howells * key. Only keyrings that grant the caller Search permission will be searched 706973c9f4fSDavid Howells * (this includes the starting keyring). Only keys with Search permission can 707973c9f4fSDavid Howells * be found. 708973c9f4fSDavid Howells * 709973c9f4fSDavid Howells * If successful, the found key will be linked to the destination keyring if 710973c9f4fSDavid Howells * supplied and the key has Link permission, and the found key ID will be 711973c9f4fSDavid Howells * returned. 7121da177e4SLinus Torvalds */ 7131da177e4SLinus Torvalds long keyctl_keyring_search(key_serial_t ringid, 7141da177e4SLinus Torvalds const char __user *_type, 7151da177e4SLinus Torvalds const char __user *_description, 7161da177e4SLinus Torvalds key_serial_t destringid) 7171da177e4SLinus Torvalds { 7181da177e4SLinus Torvalds struct key_type *ktype; 719664cceb0SDavid Howells key_ref_t keyring_ref, key_ref, dest_ref; 7201da177e4SLinus Torvalds char type[32], *description; 7210cb409d9SDavi Arnaut long ret; 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds /* pull the type and description into kernel space */ 7240cb409d9SDavi Arnaut ret = key_get_type_from_user(type, _type, sizeof(type)); 7251da177e4SLinus Torvalds if (ret < 0) 7261da177e4SLinus Torvalds goto error; 7271da177e4SLinus Torvalds 728aa9d4437SDavid Howells description = strndup_user(_description, KEY_MAX_DESC_SIZE); 7290cb409d9SDavi Arnaut if (IS_ERR(description)) { 7300cb409d9SDavi Arnaut ret = PTR_ERR(description); 7311da177e4SLinus Torvalds goto error; 7320cb409d9SDavi Arnaut } 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds /* get the keyring at which to begin the search */ 735f5895943SDavid Howells keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH); 736664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 737664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 7381da177e4SLinus Torvalds goto error2; 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds /* get the destination keyring if specified */ 742664cceb0SDavid Howells dest_ref = NULL; 7431da177e4SLinus Torvalds if (destringid) { 7445593122eSDavid Howells dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE, 745f5895943SDavid Howells KEY_NEED_WRITE); 746664cceb0SDavid Howells if (IS_ERR(dest_ref)) { 747664cceb0SDavid Howells ret = PTR_ERR(dest_ref); 7481da177e4SLinus Torvalds goto error3; 7491da177e4SLinus Torvalds } 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds /* find the key type */ 7531da177e4SLinus Torvalds ktype = key_type_lookup(type); 7541da177e4SLinus Torvalds if (IS_ERR(ktype)) { 7551da177e4SLinus Torvalds ret = PTR_ERR(ktype); 7561da177e4SLinus Torvalds goto error4; 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds /* do the search */ 760dcf49dbcSDavid Howells key_ref = keyring_search(keyring_ref, ktype, description, true); 761664cceb0SDavid Howells if (IS_ERR(key_ref)) { 762664cceb0SDavid Howells ret = PTR_ERR(key_ref); 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds /* treat lack or presence of a negative key the same */ 7651da177e4SLinus Torvalds if (ret == -EAGAIN) 7661da177e4SLinus Torvalds ret = -ENOKEY; 7671da177e4SLinus Torvalds goto error5; 7681da177e4SLinus Torvalds } 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds /* link the resulting key to the destination keyring if we can */ 771664cceb0SDavid Howells if (dest_ref) { 772f5895943SDavid Howells ret = key_permission(key_ref, KEY_NEED_LINK); 77329db9190SDavid Howells if (ret < 0) 7741da177e4SLinus Torvalds goto error6; 7751da177e4SLinus Torvalds 776664cceb0SDavid Howells ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref)); 7771da177e4SLinus Torvalds if (ret < 0) 7781da177e4SLinus Torvalds goto error6; 7791da177e4SLinus Torvalds } 7801da177e4SLinus Torvalds 781664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds error6: 784664cceb0SDavid Howells key_ref_put(key_ref); 7851da177e4SLinus Torvalds error5: 7861da177e4SLinus Torvalds key_type_put(ktype); 7871da177e4SLinus Torvalds error4: 788664cceb0SDavid Howells key_ref_put(dest_ref); 7891da177e4SLinus Torvalds error3: 790664cceb0SDavid Howells key_ref_put(keyring_ref); 7911da177e4SLinus Torvalds error2: 7921da177e4SLinus Torvalds kfree(description); 7931da177e4SLinus Torvalds error: 7941da177e4SLinus Torvalds return ret; 795a8b17ed0SDavid Howells } 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds /* 798d3ec10aaSWaiman Long * Call the read method 799d3ec10aaSWaiman Long */ 800d3ec10aaSWaiman Long static long __keyctl_read_key(struct key *key, char *buffer, size_t buflen) 801d3ec10aaSWaiman Long { 802d3ec10aaSWaiman Long long ret; 803d3ec10aaSWaiman Long 804d3ec10aaSWaiman Long down_read(&key->sem); 805d3ec10aaSWaiman Long ret = key_validate(key); 806d3ec10aaSWaiman Long if (ret == 0) 807d3ec10aaSWaiman Long ret = key->type->read(key, buffer, buflen); 808d3ec10aaSWaiman Long up_read(&key->sem); 809d3ec10aaSWaiman Long return ret; 810d3ec10aaSWaiman Long } 811d3ec10aaSWaiman Long 812d3ec10aaSWaiman Long /* 813973c9f4fSDavid Howells * Read a key's payload. 814973c9f4fSDavid Howells * 815973c9f4fSDavid Howells * The key must either grant the caller Read permission, or it must grant the 816973c9f4fSDavid Howells * caller Search permission when searched for from the process keyrings. 817973c9f4fSDavid Howells * 818973c9f4fSDavid Howells * If successful, we place up to buflen bytes of data into the buffer, if one 819973c9f4fSDavid Howells * is provided, and return the amount of data that is available in the key, 820973c9f4fSDavid Howells * irrespective of how much we copied into the buffer. 8211da177e4SLinus Torvalds */ 8221da177e4SLinus Torvalds long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen) 8231da177e4SLinus Torvalds { 824664cceb0SDavid Howells struct key *key; 825664cceb0SDavid Howells key_ref_t key_ref; 8261da177e4SLinus Torvalds long ret; 8274f088249SWaiman Long char *key_data = NULL; 8284f088249SWaiman Long size_t key_data_len; 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds /* find the key first */ 8315593122eSDavid Howells key_ref = lookup_user_key(keyid, 0, 0); 832664cceb0SDavid Howells if (IS_ERR(key_ref)) { 833664cceb0SDavid Howells ret = -ENOKEY; 834d3ec10aaSWaiman Long goto out; 835664cceb0SDavid Howells } 836664cceb0SDavid Howells 837664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 838664cceb0SDavid Howells 839363b02daSDavid Howells ret = key_read_state(key); 840363b02daSDavid Howells if (ret < 0) 841d3ec10aaSWaiman Long goto key_put_out; /* Negatively instantiated */ 84237863c43SEric Biggers 8431da177e4SLinus Torvalds /* see if we can read it directly */ 844f5895943SDavid Howells ret = key_permission(key_ref, KEY_NEED_READ); 84529db9190SDavid Howells if (ret == 0) 8461da177e4SLinus Torvalds goto can_read_key; 84729db9190SDavid Howells if (ret != -EACCES) 848d3ec10aaSWaiman Long goto key_put_out; 8491da177e4SLinus Torvalds 850664cceb0SDavid Howells /* we can't; see if it's searchable from this process's keyrings 8513e30148cSDavid Howells * - we automatically take account of the fact that it may be 8523e30148cSDavid Howells * dangling off an instantiation key 8533e30148cSDavid Howells */ 854664cceb0SDavid Howells if (!is_key_possessed(key_ref)) { 8551260f801SDavid Howells ret = -EACCES; 856d3ec10aaSWaiman Long goto key_put_out; 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds /* the key is probably readable - now try to read it */ 8601da177e4SLinus Torvalds can_read_key: 861d3ec10aaSWaiman Long if (!key->type->read) { 8621da177e4SLinus Torvalds ret = -EOPNOTSUPP; 863d3ec10aaSWaiman Long goto key_put_out; 8641da177e4SLinus Torvalds } 8651da177e4SLinus Torvalds 866d3ec10aaSWaiman Long if (!buffer || !buflen) { 867d3ec10aaSWaiman Long /* Get the key length from the read method */ 868d3ec10aaSWaiman Long ret = __keyctl_read_key(key, NULL, 0); 869d3ec10aaSWaiman Long goto key_put_out; 870d3ec10aaSWaiman Long } 871d3ec10aaSWaiman Long 872d3ec10aaSWaiman Long /* 873d3ec10aaSWaiman Long * Read the data with the semaphore held (since we might sleep) 874d3ec10aaSWaiman Long * to protect against the key being updated or revoked. 875d3ec10aaSWaiman Long * 876d3ec10aaSWaiman Long * Allocating a temporary buffer to hold the keys before 877d3ec10aaSWaiman Long * transferring them to user buffer to avoid potential 878d3ec10aaSWaiman Long * deadlock involving page fault and mmap_sem. 8794f088249SWaiman Long * 8804f088249SWaiman Long * key_data_len = (buflen <= PAGE_SIZE) 8814f088249SWaiman Long * ? buflen : actual length of key data 8824f088249SWaiman Long * 8834f088249SWaiman Long * This prevents allocating arbitrary large buffer which can 8844f088249SWaiman Long * be much larger than the actual key length. In the latter case, 8854f088249SWaiman Long * at least 2 passes of this loop is required. 886d3ec10aaSWaiman Long */ 8874f088249SWaiman Long key_data_len = (buflen <= PAGE_SIZE) ? buflen : 0; 8884f088249SWaiman Long for (;;) { 8894f088249SWaiman Long if (key_data_len) { 8904f088249SWaiman Long key_data = kvmalloc(key_data_len, GFP_KERNEL); 891d3ec10aaSWaiman Long if (!key_data) { 892d3ec10aaSWaiman Long ret = -ENOMEM; 893d3ec10aaSWaiman Long goto key_put_out; 894d3ec10aaSWaiman Long } 8954f088249SWaiman Long } 8964f088249SWaiman Long 8974f088249SWaiman Long ret = __keyctl_read_key(key, key_data, key_data_len); 898d3ec10aaSWaiman Long 899d3ec10aaSWaiman Long /* 900d3ec10aaSWaiman Long * Read methods will just return the required length without 901d3ec10aaSWaiman Long * any copying if the provided length isn't large enough. 902d3ec10aaSWaiman Long */ 9034f088249SWaiman Long if (ret <= 0 || ret > buflen) 9044f088249SWaiman Long break; 9054f088249SWaiman Long 9064f088249SWaiman Long /* 9074f088249SWaiman Long * The key may change (unlikely) in between 2 consecutive 9084f088249SWaiman Long * __keyctl_read_key() calls. In this case, we reallocate 9094f088249SWaiman Long * a larger buffer and redo the key read when 9104f088249SWaiman Long * key_data_len < ret <= buflen. 9114f088249SWaiman Long */ 9124f088249SWaiman Long if (ret > key_data_len) { 9134f088249SWaiman Long if (unlikely(key_data)) 914*d4eaa283SWaiman Long kvfree_sensitive(key_data, key_data_len); 9154f088249SWaiman Long key_data_len = ret; 9164f088249SWaiman Long continue; /* Allocate buffer */ 9174f088249SWaiman Long } 9184f088249SWaiman Long 919d3ec10aaSWaiman Long if (copy_to_user(buffer, key_data, ret)) 920d3ec10aaSWaiman Long ret = -EFAULT; 9214f088249SWaiman Long break; 922d3ec10aaSWaiman Long } 923*d4eaa283SWaiman Long kvfree_sensitive(key_data, key_data_len); 924d3ec10aaSWaiman Long 925d3ec10aaSWaiman Long key_put_out: 9261da177e4SLinus Torvalds key_put(key); 927d3ec10aaSWaiman Long out: 9281da177e4SLinus Torvalds return ret; 929a8b17ed0SDavid Howells } 9301da177e4SLinus Torvalds 9311da177e4SLinus Torvalds /* 932973c9f4fSDavid Howells * Change the ownership of a key 933973c9f4fSDavid Howells * 934973c9f4fSDavid Howells * The key must grant the caller Setattr permission for this to work, though 935973c9f4fSDavid Howells * the key need not be fully instantiated yet. For the UID to be changed, or 936973c9f4fSDavid Howells * for the GID to be changed to a group the caller is not a member of, the 937973c9f4fSDavid Howells * caller must have sysadmin capability. If either uid or gid is -1 then that 938973c9f4fSDavid Howells * attribute is not changed. 939973c9f4fSDavid Howells * 940973c9f4fSDavid Howells * If the UID is to be changed, the new user must have sufficient quota to 941973c9f4fSDavid Howells * accept the key. The quota deduction will be removed from the old user to 942973c9f4fSDavid Howells * the new user should the attribute be changed. 943973c9f4fSDavid Howells * 944973c9f4fSDavid Howells * If successful, 0 will be returned. 9451da177e4SLinus Torvalds */ 9469a56c2dbSEric W. Biederman long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group) 9471da177e4SLinus Torvalds { 9485801649dSFredrik Tolf struct key_user *newowner, *zapowner = NULL; 9491da177e4SLinus Torvalds struct key *key; 950664cceb0SDavid Howells key_ref_t key_ref; 9511da177e4SLinus Torvalds long ret; 9529a56c2dbSEric W. Biederman kuid_t uid; 9539a56c2dbSEric W. Biederman kgid_t gid; 9549a56c2dbSEric W. Biederman 9559a56c2dbSEric W. Biederman uid = make_kuid(current_user_ns(), user); 9569a56c2dbSEric W. Biederman gid = make_kgid(current_user_ns(), group); 9579a56c2dbSEric W. Biederman ret = -EINVAL; 9589a56c2dbSEric W. Biederman if ((user != (uid_t) -1) && !uid_valid(uid)) 9599a56c2dbSEric W. Biederman goto error; 9609a56c2dbSEric W. Biederman if ((group != (gid_t) -1) && !gid_valid(gid)) 9619a56c2dbSEric W. Biederman goto error; 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds ret = 0; 9649a56c2dbSEric W. Biederman if (user == (uid_t) -1 && group == (gid_t) -1) 9651da177e4SLinus Torvalds goto error; 9661da177e4SLinus Torvalds 9675593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 968028db3e2SLinus Torvalds KEY_NEED_SETATTR); 969664cceb0SDavid Howells if (IS_ERR(key_ref)) { 970664cceb0SDavid Howells ret = PTR_ERR(key_ref); 9711da177e4SLinus Torvalds goto error; 9721da177e4SLinus Torvalds } 9731da177e4SLinus Torvalds 974664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 975664cceb0SDavid Howells 9761da177e4SLinus Torvalds /* make the changes with the locks held to prevent chown/chown races */ 9771da177e4SLinus Torvalds ret = -EACCES; 9781da177e4SLinus Torvalds down_write(&key->sem); 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) { 9811da177e4SLinus Torvalds /* only the sysadmin can chown a key to some other UID */ 9829a56c2dbSEric W. Biederman if (user != (uid_t) -1 && !uid_eq(key->uid, uid)) 9835801649dSFredrik Tolf goto error_put; 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds /* only the sysadmin can set the key's GID to a group other 9861da177e4SLinus Torvalds * than one of those that the current process subscribes to */ 9879a56c2dbSEric W. Biederman if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid)) 9885801649dSFredrik Tolf goto error_put; 9891da177e4SLinus Torvalds } 9901da177e4SLinus Torvalds 9915801649dSFredrik Tolf /* change the UID */ 9929a56c2dbSEric W. Biederman if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) { 9935801649dSFredrik Tolf ret = -ENOMEM; 9949a56c2dbSEric W. Biederman newowner = key_user_lookup(uid); 9955801649dSFredrik Tolf if (!newowner) 9965801649dSFredrik Tolf goto error_put; 9975801649dSFredrik Tolf 9985801649dSFredrik Tolf /* transfer the quota burden to the new user */ 9995801649dSFredrik Tolf if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { 10009a56c2dbSEric W. Biederman unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ? 10010b77f5bfSDavid Howells key_quota_root_maxkeys : key_quota_maxkeys; 10029a56c2dbSEric W. Biederman unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ? 10030b77f5bfSDavid Howells key_quota_root_maxbytes : key_quota_maxbytes; 10040b77f5bfSDavid Howells 10055801649dSFredrik Tolf spin_lock(&newowner->lock); 10062e356101SYang Xu if (newowner->qnkeys + 1 > maxkeys || 10072e356101SYang Xu newowner->qnbytes + key->quotalen > maxbytes || 10080b77f5bfSDavid Howells newowner->qnbytes + key->quotalen < 10090b77f5bfSDavid Howells newowner->qnbytes) 10105801649dSFredrik Tolf goto quota_overrun; 10115801649dSFredrik Tolf 10125801649dSFredrik Tolf newowner->qnkeys++; 10135801649dSFredrik Tolf newowner->qnbytes += key->quotalen; 10145801649dSFredrik Tolf spin_unlock(&newowner->lock); 10155801649dSFredrik Tolf 10165801649dSFredrik Tolf spin_lock(&key->user->lock); 10175801649dSFredrik Tolf key->user->qnkeys--; 10185801649dSFredrik Tolf key->user->qnbytes -= key->quotalen; 10195801649dSFredrik Tolf spin_unlock(&key->user->lock); 10205801649dSFredrik Tolf } 10215801649dSFredrik Tolf 10225801649dSFredrik Tolf atomic_dec(&key->user->nkeys); 10235801649dSFredrik Tolf atomic_inc(&newowner->nkeys); 10245801649dSFredrik Tolf 1025363b02daSDavid Howells if (key->state != KEY_IS_UNINSTANTIATED) { 10265801649dSFredrik Tolf atomic_dec(&key->user->nikeys); 10275801649dSFredrik Tolf atomic_inc(&newowner->nikeys); 10285801649dSFredrik Tolf } 10295801649dSFredrik Tolf 10305801649dSFredrik Tolf zapowner = key->user; 10315801649dSFredrik Tolf key->user = newowner; 10325801649dSFredrik Tolf key->uid = uid; 10331da177e4SLinus Torvalds } 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds /* change the GID */ 10369a56c2dbSEric W. Biederman if (group != (gid_t) -1) 10371da177e4SLinus Torvalds key->gid = gid; 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds ret = 0; 10401da177e4SLinus Torvalds 10415801649dSFredrik Tolf error_put: 10421da177e4SLinus Torvalds up_write(&key->sem); 10431da177e4SLinus Torvalds key_put(key); 10445801649dSFredrik Tolf if (zapowner) 10455801649dSFredrik Tolf key_user_put(zapowner); 10461da177e4SLinus Torvalds error: 10471da177e4SLinus Torvalds return ret; 10481da177e4SLinus Torvalds 10495801649dSFredrik Tolf quota_overrun: 10505801649dSFredrik Tolf spin_unlock(&newowner->lock); 10515801649dSFredrik Tolf zapowner = newowner; 10525801649dSFredrik Tolf ret = -EDQUOT; 10535801649dSFredrik Tolf goto error_put; 1054a8b17ed0SDavid Howells } 10555801649dSFredrik Tolf 10561da177e4SLinus Torvalds /* 1057973c9f4fSDavid Howells * Change the permission mask on a key. 1058973c9f4fSDavid Howells * 1059973c9f4fSDavid Howells * The key must grant the caller Setattr permission for this to work, though 1060973c9f4fSDavid Howells * the key need not be fully instantiated yet. If the caller does not have 1061973c9f4fSDavid Howells * sysadmin capability, it may only change the permission on keys that it owns. 10621da177e4SLinus Torvalds */ 1063028db3e2SLinus Torvalds long keyctl_setperm_key(key_serial_t id, key_perm_t perm) 10641da177e4SLinus Torvalds { 10651da177e4SLinus Torvalds struct key *key; 1066664cceb0SDavid Howells key_ref_t key_ref; 10671da177e4SLinus Torvalds long ret; 10681da177e4SLinus Torvalds 1069028db3e2SLinus Torvalds ret = -EINVAL; 1070664cceb0SDavid Howells if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL)) 1071028db3e2SLinus Torvalds goto error; 10721da177e4SLinus Torvalds 10735593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 1074028db3e2SLinus Torvalds KEY_NEED_SETATTR); 1075664cceb0SDavid Howells if (IS_ERR(key_ref)) { 1076664cceb0SDavid Howells ret = PTR_ERR(key_ref); 10771da177e4SLinus Torvalds goto error; 10781da177e4SLinus Torvalds } 10791da177e4SLinus Torvalds 1080664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 1081664cceb0SDavid Howells 10822e12256bSDavid Howells /* make the changes with the locks held to prevent chown/chmod races */ 1083028db3e2SLinus Torvalds ret = -EACCES; 10842e12256bSDavid Howells down_write(&key->sem); 1085028db3e2SLinus Torvalds 1086028db3e2SLinus Torvalds /* if we're not the sysadmin, we can only change a key that we own */ 1087028db3e2SLinus Torvalds if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) { 1088028db3e2SLinus Torvalds key->perm = perm; 1089028db3e2SLinus Torvalds ret = 0; 1090028db3e2SLinus Torvalds } 1091028db3e2SLinus Torvalds 10921da177e4SLinus Torvalds up_write(&key->sem); 10931da177e4SLinus Torvalds key_put(key); 10941da177e4SLinus Torvalds error: 10951da177e4SLinus Torvalds return ret; 1096a8b17ed0SDavid Howells } 10971da177e4SLinus Torvalds 10988bbf4976SDavid Howells /* 1099973c9f4fSDavid Howells * Get the destination keyring for instantiation and check that the caller has 1100973c9f4fSDavid Howells * Write permission on it. 11018bbf4976SDavid Howells */ 11028bbf4976SDavid Howells static long get_instantiation_keyring(key_serial_t ringid, 11038bbf4976SDavid Howells struct request_key_auth *rka, 11048bbf4976SDavid Howells struct key **_dest_keyring) 11058bbf4976SDavid Howells { 11068bbf4976SDavid Howells key_ref_t dkref; 11078bbf4976SDavid Howells 11088bbf4976SDavid Howells *_dest_keyring = NULL; 1109eca1bf5bSDavid Howells 1110eca1bf5bSDavid Howells /* just return a NULL pointer if we weren't asked to make a link */ 1111eca1bf5bSDavid Howells if (ringid == 0) 11128bbf4976SDavid Howells return 0; 11138bbf4976SDavid Howells 11148bbf4976SDavid Howells /* if a specific keyring is nominated by ID, then use that */ 11158bbf4976SDavid Howells if (ringid > 0) { 1116f5895943SDavid Howells dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); 11178bbf4976SDavid Howells if (IS_ERR(dkref)) 11188bbf4976SDavid Howells return PTR_ERR(dkref); 11198bbf4976SDavid Howells *_dest_keyring = key_ref_to_ptr(dkref); 11208bbf4976SDavid Howells return 0; 11218bbf4976SDavid Howells } 11228bbf4976SDavid Howells 11238bbf4976SDavid Howells if (ringid == KEY_SPEC_REQKEY_AUTH_KEY) 11248bbf4976SDavid Howells return -EINVAL; 11258bbf4976SDavid Howells 11268bbf4976SDavid Howells /* otherwise specify the destination keyring recorded in the 11278bbf4976SDavid Howells * authorisation key (any KEY_SPEC_*_KEYRING) */ 11288bbf4976SDavid Howells if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) { 112921279cfaSDavid Howells *_dest_keyring = key_get(rka->dest_keyring); 11308bbf4976SDavid Howells return 0; 11318bbf4976SDavid Howells } 11328bbf4976SDavid Howells 11338bbf4976SDavid Howells return -ENOKEY; 11348bbf4976SDavid Howells } 11358bbf4976SDavid Howells 1136d84f4f99SDavid Howells /* 1137973c9f4fSDavid Howells * Change the request_key authorisation key on the current process. 1138d84f4f99SDavid Howells */ 1139d84f4f99SDavid Howells static int keyctl_change_reqkey_auth(struct key *key) 1140d84f4f99SDavid Howells { 1141d84f4f99SDavid Howells struct cred *new; 1142d84f4f99SDavid Howells 1143d84f4f99SDavid Howells new = prepare_creds(); 1144d84f4f99SDavid Howells if (!new) 1145d84f4f99SDavid Howells return -ENOMEM; 1146d84f4f99SDavid Howells 1147d84f4f99SDavid Howells key_put(new->request_key_auth); 1148d84f4f99SDavid Howells new->request_key_auth = key_get(key); 1149d84f4f99SDavid Howells 1150d84f4f99SDavid Howells return commit_creds(new); 1151d84f4f99SDavid Howells } 1152d84f4f99SDavid Howells 11531da177e4SLinus Torvalds /* 1154973c9f4fSDavid Howells * Instantiate a key with the specified payload and link the key into the 1155973c9f4fSDavid Howells * destination keyring if one is given. 1156973c9f4fSDavid Howells * 1157973c9f4fSDavid Howells * The caller must have the appropriate instantiation permit set for this to 1158973c9f4fSDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1159973c9f4fSDavid Howells * 1160973c9f4fSDavid Howells * If successful, 0 will be returned. 11611da177e4SLinus Torvalds */ 1162ee009e4aSDavid Howells long keyctl_instantiate_key_common(key_serial_t id, 1163b353a1f7SAl Viro struct iov_iter *from, 11641da177e4SLinus Torvalds key_serial_t ringid) 11651da177e4SLinus Torvalds { 1166d84f4f99SDavid Howells const struct cred *cred = current_cred(); 11673e30148cSDavid Howells struct request_key_auth *rka; 11688bbf4976SDavid Howells struct key *instkey, *dest_keyring; 1169b353a1f7SAl Viro size_t plen = from ? iov_iter_count(from) : 0; 11701da177e4SLinus Torvalds void *payload; 11711da177e4SLinus Torvalds long ret; 11721da177e4SLinus Torvalds 1173d84f4f99SDavid Howells kenter("%d,,%zu,%d", id, plen, ringid); 1174d84f4f99SDavid Howells 1175b353a1f7SAl Viro if (!plen) 1176b353a1f7SAl Viro from = NULL; 1177b353a1f7SAl Viro 11781da177e4SLinus Torvalds ret = -EINVAL; 117938bbca6bSDavid Howells if (plen > 1024 * 1024 - 1) 11801da177e4SLinus Torvalds goto error; 11811da177e4SLinus Torvalds 1182b5f545c8SDavid Howells /* the appropriate instantiation authorisation key must have been 1183b5f545c8SDavid Howells * assumed before calling this */ 1184b5f545c8SDavid Howells ret = -EPERM; 1185d84f4f99SDavid Howells instkey = cred->request_key_auth; 1186b5f545c8SDavid Howells if (!instkey) 1187b5f545c8SDavid Howells goto error; 1188b5f545c8SDavid Howells 1189146aa8b1SDavid Howells rka = instkey->payload.data[0]; 1190b5f545c8SDavid Howells if (rka->target_key->serial != id) 1191b5f545c8SDavid Howells goto error; 1192b5f545c8SDavid Howells 11931da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 11941da177e4SLinus Torvalds payload = NULL; 11951da177e4SLinus Torvalds 1196b353a1f7SAl Viro if (from) { 11971da177e4SLinus Torvalds ret = -ENOMEM; 1198752ade68SMichal Hocko payload = kvmalloc(plen, GFP_KERNEL); 11991da177e4SLinus Torvalds if (!payload) 12001da177e4SLinus Torvalds goto error; 12011da177e4SLinus Torvalds 1202b353a1f7SAl Viro ret = -EFAULT; 1203cbbd26b8SAl Viro if (!copy_from_iter_full(payload, plen, from)) 12041da177e4SLinus Torvalds goto error2; 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds 12073e30148cSDavid Howells /* find the destination keyring amongst those belonging to the 12083e30148cSDavid Howells * requesting task */ 12098bbf4976SDavid Howells ret = get_instantiation_keyring(ringid, rka, &dest_keyring); 12108bbf4976SDavid Howells if (ret < 0) 1211b5f545c8SDavid Howells goto error2; 12121da177e4SLinus Torvalds 12131da177e4SLinus Torvalds /* instantiate the key and link it into a keyring */ 12143e30148cSDavid Howells ret = key_instantiate_and_link(rka->target_key, payload, plen, 12158bbf4976SDavid Howells dest_keyring, instkey); 12161da177e4SLinus Torvalds 12178bbf4976SDavid Howells key_put(dest_keyring); 1218b5f545c8SDavid Howells 1219b5f545c8SDavid Howells /* discard the assumed authority if it's just been disabled by 1220b5f545c8SDavid Howells * instantiation of the key */ 1221d84f4f99SDavid Howells if (ret == 0) 1222d84f4f99SDavid Howells keyctl_change_reqkey_auth(NULL); 1223b5f545c8SDavid Howells 12241da177e4SLinus Torvalds error2: 1225*d4eaa283SWaiman Long kvfree_sensitive(payload, plen); 12261da177e4SLinus Torvalds error: 12271da177e4SLinus Torvalds return ret; 1228a8b17ed0SDavid Howells } 12291da177e4SLinus Torvalds 12301da177e4SLinus Torvalds /* 1231ee009e4aSDavid Howells * Instantiate a key with the specified payload and link the key into the 1232ee009e4aSDavid Howells * destination keyring if one is given. 1233ee009e4aSDavid Howells * 1234ee009e4aSDavid Howells * The caller must have the appropriate instantiation permit set for this to 1235ee009e4aSDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1236ee009e4aSDavid Howells * 1237ee009e4aSDavid Howells * If successful, 0 will be returned. 1238ee009e4aSDavid Howells */ 1239ee009e4aSDavid Howells long keyctl_instantiate_key(key_serial_t id, 1240ee009e4aSDavid Howells const void __user *_payload, 1241ee009e4aSDavid Howells size_t plen, 1242ee009e4aSDavid Howells key_serial_t ringid) 1243ee009e4aSDavid Howells { 1244ee009e4aSDavid Howells if (_payload && plen) { 1245b353a1f7SAl Viro struct iovec iov; 1246b353a1f7SAl Viro struct iov_iter from; 1247b353a1f7SAl Viro int ret; 1248ee009e4aSDavid Howells 1249b353a1f7SAl Viro ret = import_single_range(WRITE, (void __user *)_payload, plen, 1250b353a1f7SAl Viro &iov, &from); 1251b353a1f7SAl Viro if (unlikely(ret)) 1252b353a1f7SAl Viro return ret; 1253b353a1f7SAl Viro 1254b353a1f7SAl Viro return keyctl_instantiate_key_common(id, &from, ringid); 1255ee009e4aSDavid Howells } 1256ee009e4aSDavid Howells 1257b353a1f7SAl Viro return keyctl_instantiate_key_common(id, NULL, ringid); 1258ee009e4aSDavid Howells } 1259ee009e4aSDavid Howells 1260ee009e4aSDavid Howells /* 1261ee009e4aSDavid Howells * Instantiate a key with the specified multipart payload and link the key into 1262ee009e4aSDavid Howells * the destination keyring if one is given. 1263ee009e4aSDavid Howells * 1264ee009e4aSDavid Howells * The caller must have the appropriate instantiation permit set for this to 1265ee009e4aSDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1266ee009e4aSDavid Howells * 1267ee009e4aSDavid Howells * If successful, 0 will be returned. 1268ee009e4aSDavid Howells */ 1269ee009e4aSDavid Howells long keyctl_instantiate_key_iov(key_serial_t id, 1270ee009e4aSDavid Howells const struct iovec __user *_payload_iov, 1271ee009e4aSDavid Howells unsigned ioc, 1272ee009e4aSDavid Howells key_serial_t ringid) 1273ee009e4aSDavid Howells { 1274ee009e4aSDavid Howells struct iovec iovstack[UIO_FASTIOV], *iov = iovstack; 1275b353a1f7SAl Viro struct iov_iter from; 1276ee009e4aSDavid Howells long ret; 1277ee009e4aSDavid Howells 1278b353a1f7SAl Viro if (!_payload_iov) 1279b353a1f7SAl Viro ioc = 0; 1280ee009e4aSDavid Howells 1281b353a1f7SAl Viro ret = import_iovec(WRITE, _payload_iov, ioc, 1282b353a1f7SAl Viro ARRAY_SIZE(iovstack), &iov, &from); 1283ee009e4aSDavid Howells if (ret < 0) 1284b353a1f7SAl Viro return ret; 1285b353a1f7SAl Viro ret = keyctl_instantiate_key_common(id, &from, ringid); 1286ee009e4aSDavid Howells kfree(iov); 1287ee009e4aSDavid Howells return ret; 1288ee009e4aSDavid Howells } 1289ee009e4aSDavid Howells 1290ee009e4aSDavid Howells /* 1291973c9f4fSDavid Howells * Negatively instantiate the key with the given timeout (in seconds) and link 1292973c9f4fSDavid Howells * the key into the destination keyring if one is given. 1293973c9f4fSDavid Howells * 1294973c9f4fSDavid Howells * The caller must have the appropriate instantiation permit set for this to 1295973c9f4fSDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1296973c9f4fSDavid Howells * 1297973c9f4fSDavid Howells * The key and any links to the key will be automatically garbage collected 1298973c9f4fSDavid Howells * after the timeout expires. 1299973c9f4fSDavid Howells * 1300973c9f4fSDavid Howells * Negative keys are used to rate limit repeated request_key() calls by causing 1301973c9f4fSDavid Howells * them to return -ENOKEY until the negative key expires. 1302973c9f4fSDavid Howells * 1303973c9f4fSDavid Howells * If successful, 0 will be returned. 13041da177e4SLinus Torvalds */ 13051da177e4SLinus Torvalds long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid) 13061da177e4SLinus Torvalds { 1307fdd1b945SDavid Howells return keyctl_reject_key(id, timeout, ENOKEY, ringid); 1308fdd1b945SDavid Howells } 1309fdd1b945SDavid Howells 1310fdd1b945SDavid Howells /* 1311fdd1b945SDavid Howells * Negatively instantiate the key with the given timeout (in seconds) and error 1312fdd1b945SDavid Howells * code and link the key into the destination keyring if one is given. 1313fdd1b945SDavid Howells * 1314fdd1b945SDavid Howells * The caller must have the appropriate instantiation permit set for this to 1315fdd1b945SDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1316fdd1b945SDavid Howells * 1317fdd1b945SDavid Howells * The key and any links to the key will be automatically garbage collected 1318fdd1b945SDavid Howells * after the timeout expires. 1319fdd1b945SDavid Howells * 1320fdd1b945SDavid Howells * Negative keys are used to rate limit repeated request_key() calls by causing 1321fdd1b945SDavid Howells * them to return the specified error code until the negative key expires. 1322fdd1b945SDavid Howells * 1323fdd1b945SDavid Howells * If successful, 0 will be returned. 1324fdd1b945SDavid Howells */ 1325fdd1b945SDavid Howells long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error, 1326fdd1b945SDavid Howells key_serial_t ringid) 1327fdd1b945SDavid Howells { 1328d84f4f99SDavid Howells const struct cred *cred = current_cred(); 13293e30148cSDavid Howells struct request_key_auth *rka; 13308bbf4976SDavid Howells struct key *instkey, *dest_keyring; 13311da177e4SLinus Torvalds long ret; 13321da177e4SLinus Torvalds 1333fdd1b945SDavid Howells kenter("%d,%u,%u,%d", id, timeout, error, ringid); 1334fdd1b945SDavid Howells 1335fdd1b945SDavid Howells /* must be a valid error code and mustn't be a kernel special */ 1336fdd1b945SDavid Howells if (error <= 0 || 1337fdd1b945SDavid Howells error >= MAX_ERRNO || 1338fdd1b945SDavid Howells error == ERESTARTSYS || 1339fdd1b945SDavid Howells error == ERESTARTNOINTR || 1340fdd1b945SDavid Howells error == ERESTARTNOHAND || 1341fdd1b945SDavid Howells error == ERESTART_RESTARTBLOCK) 1342fdd1b945SDavid Howells return -EINVAL; 1343d84f4f99SDavid Howells 1344b5f545c8SDavid Howells /* the appropriate instantiation authorisation key must have been 1345b5f545c8SDavid Howells * assumed before calling this */ 1346b5f545c8SDavid Howells ret = -EPERM; 1347d84f4f99SDavid Howells instkey = cred->request_key_auth; 1348b5f545c8SDavid Howells if (!instkey) 13491da177e4SLinus Torvalds goto error; 13501da177e4SLinus Torvalds 1351146aa8b1SDavid Howells rka = instkey->payload.data[0]; 1352b5f545c8SDavid Howells if (rka->target_key->serial != id) 1353b5f545c8SDavid Howells goto error; 13543e30148cSDavid Howells 13551da177e4SLinus Torvalds /* find the destination keyring if present (which must also be 13561da177e4SLinus Torvalds * writable) */ 13578bbf4976SDavid Howells ret = get_instantiation_keyring(ringid, rka, &dest_keyring); 13588bbf4976SDavid Howells if (ret < 0) 1359b5f545c8SDavid Howells goto error; 13601da177e4SLinus Torvalds 13611da177e4SLinus Torvalds /* instantiate the key and link it into a keyring */ 1362fdd1b945SDavid Howells ret = key_reject_and_link(rka->target_key, timeout, error, 13638bbf4976SDavid Howells dest_keyring, instkey); 13641da177e4SLinus Torvalds 13658bbf4976SDavid Howells key_put(dest_keyring); 1366b5f545c8SDavid Howells 1367b5f545c8SDavid Howells /* discard the assumed authority if it's just been disabled by 1368b5f545c8SDavid Howells * instantiation of the key */ 1369d84f4f99SDavid Howells if (ret == 0) 1370d84f4f99SDavid Howells keyctl_change_reqkey_auth(NULL); 1371b5f545c8SDavid Howells 13721da177e4SLinus Torvalds error: 13731da177e4SLinus Torvalds return ret; 1374a8b17ed0SDavid Howells } 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds /* 1377973c9f4fSDavid Howells * Read or set the default keyring in which request_key() will cache keys and 1378973c9f4fSDavid Howells * return the old setting. 1379973c9f4fSDavid Howells * 1380c9f838d1SEric Biggers * If a thread or process keyring is specified then it will be created if it 1381c9f838d1SEric Biggers * doesn't yet exist. The old setting will be returned if successful. 13823e30148cSDavid Howells */ 13833e30148cSDavid Howells long keyctl_set_reqkey_keyring(int reqkey_defl) 13843e30148cSDavid Howells { 1385d84f4f99SDavid Howells struct cred *new; 1386d84f4f99SDavid Howells int ret, old_setting; 1387d84f4f99SDavid Howells 1388d84f4f99SDavid Howells old_setting = current_cred_xxx(jit_keyring); 1389d84f4f99SDavid Howells 1390d84f4f99SDavid Howells if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE) 1391d84f4f99SDavid Howells return old_setting; 1392d84f4f99SDavid Howells 1393d84f4f99SDavid Howells new = prepare_creds(); 1394d84f4f99SDavid Howells if (!new) 1395d84f4f99SDavid Howells return -ENOMEM; 13963e30148cSDavid Howells 13973e30148cSDavid Howells switch (reqkey_defl) { 13983e30148cSDavid Howells case KEY_REQKEY_DEFL_THREAD_KEYRING: 1399d84f4f99SDavid Howells ret = install_thread_keyring_to_cred(new); 14003e30148cSDavid Howells if (ret < 0) 1401d84f4f99SDavid Howells goto error; 14023e30148cSDavid Howells goto set; 14033e30148cSDavid Howells 14043e30148cSDavid Howells case KEY_REQKEY_DEFL_PROCESS_KEYRING: 1405d84f4f99SDavid Howells ret = install_process_keyring_to_cred(new); 1406c9f838d1SEric Biggers if (ret < 0) 1407d84f4f99SDavid Howells goto error; 1408d84f4f99SDavid Howells goto set; 14093e30148cSDavid Howells 14103e30148cSDavid Howells case KEY_REQKEY_DEFL_DEFAULT: 14113e30148cSDavid Howells case KEY_REQKEY_DEFL_SESSION_KEYRING: 14123e30148cSDavid Howells case KEY_REQKEY_DEFL_USER_KEYRING: 14133e30148cSDavid Howells case KEY_REQKEY_DEFL_USER_SESSION_KEYRING: 1414d84f4f99SDavid Howells case KEY_REQKEY_DEFL_REQUESTOR_KEYRING: 1415d84f4f99SDavid Howells goto set; 14163e30148cSDavid Howells 14173e30148cSDavid Howells case KEY_REQKEY_DEFL_NO_CHANGE: 14183e30148cSDavid Howells case KEY_REQKEY_DEFL_GROUP_KEYRING: 14193e30148cSDavid Howells default: 1420d84f4f99SDavid Howells ret = -EINVAL; 1421d84f4f99SDavid Howells goto error; 14223e30148cSDavid Howells } 14233e30148cSDavid Howells 1424d84f4f99SDavid Howells set: 1425d84f4f99SDavid Howells new->jit_keyring = reqkey_defl; 1426d84f4f99SDavid Howells commit_creds(new); 1427d84f4f99SDavid Howells return old_setting; 1428d84f4f99SDavid Howells error: 1429d84f4f99SDavid Howells abort_creds(new); 14304303ef19SDan Carpenter return ret; 1431a8b17ed0SDavid Howells } 1432d84f4f99SDavid Howells 14333e30148cSDavid Howells /* 1434973c9f4fSDavid Howells * Set or clear the timeout on a key. 1435973c9f4fSDavid Howells * 1436973c9f4fSDavid Howells * Either the key must grant the caller Setattr permission or else the caller 1437973c9f4fSDavid Howells * must hold an instantiation authorisation token for the key. 1438973c9f4fSDavid Howells * 1439973c9f4fSDavid Howells * The timeout is either 0 to clear the timeout, or a number of seconds from 1440973c9f4fSDavid Howells * the current time. The key and any links to the key will be automatically 1441973c9f4fSDavid Howells * garbage collected after the timeout expires. 1442973c9f4fSDavid Howells * 1443d3600bcfSMimi Zohar * Keys with KEY_FLAG_KEEP set should not be timed out. 1444d3600bcfSMimi Zohar * 1445973c9f4fSDavid Howells * If successful, 0 is returned. 1446017679c4SDavid Howells */ 1447017679c4SDavid Howells long keyctl_set_timeout(key_serial_t id, unsigned timeout) 1448017679c4SDavid Howells { 14499156235bSDavid Howells struct key *key, *instkey; 1450017679c4SDavid Howells key_ref_t key_ref; 1451017679c4SDavid Howells long ret; 1452017679c4SDavid Howells 14535593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 1454028db3e2SLinus Torvalds KEY_NEED_SETATTR); 1455017679c4SDavid Howells if (IS_ERR(key_ref)) { 14569156235bSDavid Howells /* setting the timeout on a key under construction is permitted 14579156235bSDavid Howells * if we have the authorisation token handy */ 14589156235bSDavid Howells if (PTR_ERR(key_ref) == -EACCES) { 14599156235bSDavid Howells instkey = key_get_instantiation_authkey(id); 14609156235bSDavid Howells if (!IS_ERR(instkey)) { 14619156235bSDavid Howells key_put(instkey); 14629156235bSDavid Howells key_ref = lookup_user_key(id, 14639156235bSDavid Howells KEY_LOOKUP_PARTIAL, 14649156235bSDavid Howells 0); 14659156235bSDavid Howells if (!IS_ERR(key_ref)) 14669156235bSDavid Howells goto okay; 14679156235bSDavid Howells } 14689156235bSDavid Howells } 14699156235bSDavid Howells 1470017679c4SDavid Howells ret = PTR_ERR(key_ref); 1471017679c4SDavid Howells goto error; 1472017679c4SDavid Howells } 1473017679c4SDavid Howells 14749156235bSDavid Howells okay: 1475017679c4SDavid Howells key = key_ref_to_ptr(key_ref); 14761d6d167cSMimi Zohar ret = 0; 1477d3600bcfSMimi Zohar if (test_bit(KEY_FLAG_KEEP, &key->flags)) 1478d3600bcfSMimi Zohar ret = -EPERM; 14791d6d167cSMimi Zohar else 148059e6b9c1SBryan Schumaker key_set_timeout(key, timeout); 1481017679c4SDavid Howells key_put(key); 1482017679c4SDavid Howells 1483017679c4SDavid Howells error: 1484017679c4SDavid Howells return ret; 1485a8b17ed0SDavid Howells } 1486017679c4SDavid Howells 1487017679c4SDavid Howells /* 1488973c9f4fSDavid Howells * Assume (or clear) the authority to instantiate the specified key. 1489973c9f4fSDavid Howells * 1490973c9f4fSDavid Howells * This sets the authoritative token currently in force for key instantiation. 1491973c9f4fSDavid Howells * This must be done for a key to be instantiated. It has the effect of making 1492973c9f4fSDavid Howells * available all the keys from the caller of the request_key() that created a 1493973c9f4fSDavid Howells * key to request_key() calls made by the caller of this function. 1494973c9f4fSDavid Howells * 1495973c9f4fSDavid Howells * The caller must have the instantiation key in their process keyrings with a 1496973c9f4fSDavid Howells * Search permission grant available to the caller. 1497973c9f4fSDavid Howells * 1498973c9f4fSDavid Howells * If the ID given is 0, then the setting will be cleared and 0 returned. 1499973c9f4fSDavid Howells * 1500973c9f4fSDavid Howells * If the ID given has a matching an authorisation key, then that key will be 1501973c9f4fSDavid Howells * set and its ID will be returned. The authorisation key can be read to get 1502973c9f4fSDavid Howells * the callout information passed to request_key(). 1503b5f545c8SDavid Howells */ 1504b5f545c8SDavid Howells long keyctl_assume_authority(key_serial_t id) 1505b5f545c8SDavid Howells { 1506b5f545c8SDavid Howells struct key *authkey; 1507b5f545c8SDavid Howells long ret; 1508b5f545c8SDavid Howells 1509b5f545c8SDavid Howells /* special key IDs aren't permitted */ 1510b5f545c8SDavid Howells ret = -EINVAL; 1511b5f545c8SDavid Howells if (id < 0) 1512b5f545c8SDavid Howells goto error; 1513b5f545c8SDavid Howells 1514b5f545c8SDavid Howells /* we divest ourselves of authority if given an ID of 0 */ 1515b5f545c8SDavid Howells if (id == 0) { 1516d84f4f99SDavid Howells ret = keyctl_change_reqkey_auth(NULL); 1517b5f545c8SDavid Howells goto error; 1518b5f545c8SDavid Howells } 1519b5f545c8SDavid Howells 1520b5f545c8SDavid Howells /* attempt to assume the authority temporarily granted to us whilst we 1521b5f545c8SDavid Howells * instantiate the specified key 1522b5f545c8SDavid Howells * - the authorisation key must be in the current task's keyrings 1523b5f545c8SDavid Howells * somewhere 1524b5f545c8SDavid Howells */ 1525b5f545c8SDavid Howells authkey = key_get_instantiation_authkey(id); 1526b5f545c8SDavid Howells if (IS_ERR(authkey)) { 1527b5f545c8SDavid Howells ret = PTR_ERR(authkey); 1528b5f545c8SDavid Howells goto error; 1529b5f545c8SDavid Howells } 1530b5f545c8SDavid Howells 1531d84f4f99SDavid Howells ret = keyctl_change_reqkey_auth(authkey); 1532884bee02SEric Biggers if (ret == 0) 1533d84f4f99SDavid Howells ret = authkey->serial; 1534884bee02SEric Biggers key_put(authkey); 1535b5f545c8SDavid Howells error: 1536b5f545c8SDavid Howells return ret; 1537a8b17ed0SDavid Howells } 1538b5f545c8SDavid Howells 153970a5bb72SDavid Howells /* 1540973c9f4fSDavid Howells * Get a key's the LSM security label. 1541973c9f4fSDavid Howells * 1542973c9f4fSDavid Howells * The key must grant the caller View permission for this to work. 1543973c9f4fSDavid Howells * 1544973c9f4fSDavid Howells * If there's a buffer, then up to buflen bytes of data will be placed into it. 1545973c9f4fSDavid Howells * 1546973c9f4fSDavid Howells * If successful, the amount of information available will be returned, 1547973c9f4fSDavid Howells * irrespective of how much was copied (including the terminal NUL). 154870a5bb72SDavid Howells */ 154970a5bb72SDavid Howells long keyctl_get_security(key_serial_t keyid, 155070a5bb72SDavid Howells char __user *buffer, 155170a5bb72SDavid Howells size_t buflen) 155270a5bb72SDavid Howells { 155370a5bb72SDavid Howells struct key *key, *instkey; 155470a5bb72SDavid Howells key_ref_t key_ref; 155570a5bb72SDavid Howells char *context; 155670a5bb72SDavid Howells long ret; 155770a5bb72SDavid Howells 1558f5895943SDavid Howells key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW); 155970a5bb72SDavid Howells if (IS_ERR(key_ref)) { 156070a5bb72SDavid Howells if (PTR_ERR(key_ref) != -EACCES) 156170a5bb72SDavid Howells return PTR_ERR(key_ref); 156270a5bb72SDavid Howells 156370a5bb72SDavid Howells /* viewing a key under construction is also permitted if we 156470a5bb72SDavid Howells * have the authorisation token handy */ 156570a5bb72SDavid Howells instkey = key_get_instantiation_authkey(keyid); 156670a5bb72SDavid Howells if (IS_ERR(instkey)) 1567fa1cc7b5SRoel Kluin return PTR_ERR(instkey); 156870a5bb72SDavid Howells key_put(instkey); 156970a5bb72SDavid Howells 15705593122eSDavid Howells key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0); 157170a5bb72SDavid Howells if (IS_ERR(key_ref)) 157270a5bb72SDavid Howells return PTR_ERR(key_ref); 157370a5bb72SDavid Howells } 157470a5bb72SDavid Howells 157570a5bb72SDavid Howells key = key_ref_to_ptr(key_ref); 157670a5bb72SDavid Howells ret = security_key_getsecurity(key, &context); 157770a5bb72SDavid Howells if (ret == 0) { 157870a5bb72SDavid Howells /* if no information was returned, give userspace an empty 157970a5bb72SDavid Howells * string */ 158070a5bb72SDavid Howells ret = 1; 158170a5bb72SDavid Howells if (buffer && buflen > 0 && 158270a5bb72SDavid Howells copy_to_user(buffer, "", 1) != 0) 158370a5bb72SDavid Howells ret = -EFAULT; 158470a5bb72SDavid Howells } else if (ret > 0) { 158570a5bb72SDavid Howells /* return as much data as there's room for */ 158670a5bb72SDavid Howells if (buffer && buflen > 0) { 158770a5bb72SDavid Howells if (buflen > ret) 158870a5bb72SDavid Howells buflen = ret; 158970a5bb72SDavid Howells 159070a5bb72SDavid Howells if (copy_to_user(buffer, context, buflen) != 0) 159170a5bb72SDavid Howells ret = -EFAULT; 159270a5bb72SDavid Howells } 159370a5bb72SDavid Howells 159470a5bb72SDavid Howells kfree(context); 159570a5bb72SDavid Howells } 159670a5bb72SDavid Howells 159770a5bb72SDavid Howells key_ref_put(key_ref); 159870a5bb72SDavid Howells return ret; 159970a5bb72SDavid Howells } 160070a5bb72SDavid Howells 1601ee18d64cSDavid Howells /* 1602973c9f4fSDavid Howells * Attempt to install the calling process's session keyring on the process's 1603973c9f4fSDavid Howells * parent process. 1604973c9f4fSDavid Howells * 1605028db3e2SLinus Torvalds * The keyring must exist and must grant the caller LINK permission, and the 1606973c9f4fSDavid Howells * parent process must be single-threaded and must have the same effective 1607973c9f4fSDavid Howells * ownership as this process and mustn't be SUID/SGID. 1608973c9f4fSDavid Howells * 1609973c9f4fSDavid Howells * The keyring will be emplaced on the parent when it next resumes userspace. 1610973c9f4fSDavid Howells * 1611973c9f4fSDavid Howells * If successful, 0 will be returned. 1612ee18d64cSDavid Howells */ 1613ee18d64cSDavid Howells long keyctl_session_to_parent(void) 1614ee18d64cSDavid Howells { 1615ee18d64cSDavid Howells struct task_struct *me, *parent; 1616ee18d64cSDavid Howells const struct cred *mycred, *pcred; 161767d12145SAl Viro struct callback_head *newwork, *oldwork; 1618ee18d64cSDavid Howells key_ref_t keyring_r; 1619413cd3d9SOleg Nesterov struct cred *cred; 1620ee18d64cSDavid Howells int ret; 1621ee18d64cSDavid Howells 1622028db3e2SLinus Torvalds keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK); 1623ee18d64cSDavid Howells if (IS_ERR(keyring_r)) 1624ee18d64cSDavid Howells return PTR_ERR(keyring_r); 1625ee18d64cSDavid Howells 1626413cd3d9SOleg Nesterov ret = -ENOMEM; 1627413cd3d9SOleg Nesterov 1628ee18d64cSDavid Howells /* our parent is going to need a new cred struct, a new tgcred struct 1629ee18d64cSDavid Howells * and new security data, so we allocate them here to prevent ENOMEM in 1630ee18d64cSDavid Howells * our parent */ 1631ee18d64cSDavid Howells cred = cred_alloc_blank(); 1632ee18d64cSDavid Howells if (!cred) 163367d12145SAl Viro goto error_keyring; 163467d12145SAl Viro newwork = &cred->rcu; 1635ee18d64cSDavid Howells 16363a50597dSDavid Howells cred->session_keyring = key_ref_to_ptr(keyring_r); 16373a50597dSDavid Howells keyring_r = NULL; 163867d12145SAl Viro init_task_work(newwork, key_change_session_keyring); 1639ee18d64cSDavid Howells 1640ee18d64cSDavid Howells me = current; 16419d1ac65aSDavid Howells rcu_read_lock(); 1642ee18d64cSDavid Howells write_lock_irq(&tasklist_lock); 1643ee18d64cSDavid Howells 1644ee18d64cSDavid Howells ret = -EPERM; 1645413cd3d9SOleg Nesterov oldwork = NULL; 16467936d16dSDavid Howells parent = rcu_dereference_protected(me->real_parent, 16477936d16dSDavid Howells lockdep_is_held(&tasklist_lock)); 1648ee18d64cSDavid Howells 1649ee18d64cSDavid Howells /* the parent mustn't be init and mustn't be a kernel thread */ 1650ee18d64cSDavid Howells if (parent->pid <= 1 || !parent->mm) 1651413cd3d9SOleg Nesterov goto unlock; 1652ee18d64cSDavid Howells 1653ee18d64cSDavid Howells /* the parent must be single threaded */ 1654dd98acf7SOleg Nesterov if (!thread_group_empty(parent)) 1655413cd3d9SOleg Nesterov goto unlock; 1656ee18d64cSDavid Howells 1657ee18d64cSDavid Howells /* the parent and the child must have different session keyrings or 1658ee18d64cSDavid Howells * there's no point */ 1659ee18d64cSDavid Howells mycred = current_cred(); 1660ee18d64cSDavid Howells pcred = __task_cred(parent); 1661ee18d64cSDavid Howells if (mycred == pcred || 16623a50597dSDavid Howells mycred->session_keyring == pcred->session_keyring) { 1663413cd3d9SOleg Nesterov ret = 0; 1664413cd3d9SOleg Nesterov goto unlock; 1665413cd3d9SOleg Nesterov } 1666ee18d64cSDavid Howells 1667ee18d64cSDavid Howells /* the parent must have the same effective ownership and mustn't be 1668ee18d64cSDavid Howells * SUID/SGID */ 16699a56c2dbSEric W. Biederman if (!uid_eq(pcred->uid, mycred->euid) || 16709a56c2dbSEric W. Biederman !uid_eq(pcred->euid, mycred->euid) || 16719a56c2dbSEric W. Biederman !uid_eq(pcred->suid, mycred->euid) || 16729a56c2dbSEric W. Biederman !gid_eq(pcred->gid, mycred->egid) || 16739a56c2dbSEric W. Biederman !gid_eq(pcred->egid, mycred->egid) || 16749a56c2dbSEric W. Biederman !gid_eq(pcred->sgid, mycred->egid)) 1675413cd3d9SOleg Nesterov goto unlock; 1676ee18d64cSDavid Howells 1677ee18d64cSDavid Howells /* the keyrings must have the same UID */ 16783a50597dSDavid Howells if ((pcred->session_keyring && 16792a74dbb9SLinus Torvalds !uid_eq(pcred->session_keyring->uid, mycred->euid)) || 16802a74dbb9SLinus Torvalds !uid_eq(mycred->session_keyring->uid, mycred->euid)) 1681413cd3d9SOleg Nesterov goto unlock; 1682ee18d64cSDavid Howells 1683413cd3d9SOleg Nesterov /* cancel an already pending keyring replacement */ 1684413cd3d9SOleg Nesterov oldwork = task_work_cancel(parent, key_change_session_keyring); 1685ee18d64cSDavid Howells 1686ee18d64cSDavid Howells /* the replacement session keyring is applied just prior to userspace 1687ee18d64cSDavid Howells * restarting */ 168867d12145SAl Viro ret = task_work_add(parent, newwork, true); 1689413cd3d9SOleg Nesterov if (!ret) 1690413cd3d9SOleg Nesterov newwork = NULL; 1691413cd3d9SOleg Nesterov unlock: 1692ee18d64cSDavid Howells write_unlock_irq(&tasklist_lock); 16939d1ac65aSDavid Howells rcu_read_unlock(); 169467d12145SAl Viro if (oldwork) 169567d12145SAl Viro put_cred(container_of(oldwork, struct cred, rcu)); 169667d12145SAl Viro if (newwork) 169767d12145SAl Viro put_cred(cred); 1698ee18d64cSDavid Howells return ret; 1699ee18d64cSDavid Howells 1700ee18d64cSDavid Howells error_keyring: 1701ee18d64cSDavid Howells key_ref_put(keyring_r); 1702ee18d64cSDavid Howells return ret; 1703ee18d64cSDavid Howells } 1704ee18d64cSDavid Howells 1705b5f545c8SDavid Howells /* 17066563c91fSMat Martineau * Apply a restriction to a given keyring. 17076563c91fSMat Martineau * 17086563c91fSMat Martineau * The caller must have Setattr permission to change keyring restrictions. 17096563c91fSMat Martineau * 17106563c91fSMat Martineau * The requested type name may be a NULL pointer to reject all attempts 171118026d86SEric Biggers * to link to the keyring. In this case, _restriction must also be NULL. 171218026d86SEric Biggers * Otherwise, both _type and _restriction must be non-NULL. 17136563c91fSMat Martineau * 17146563c91fSMat Martineau * Returns 0 if successful. 17156563c91fSMat Martineau */ 17166563c91fSMat Martineau long keyctl_restrict_keyring(key_serial_t id, const char __user *_type, 17176563c91fSMat Martineau const char __user *_restriction) 17186563c91fSMat Martineau { 17196563c91fSMat Martineau key_ref_t key_ref; 17206563c91fSMat Martineau char type[32]; 17216563c91fSMat Martineau char *restriction = NULL; 17226563c91fSMat Martineau long ret; 17236563c91fSMat Martineau 1724028db3e2SLinus Torvalds key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR); 17256563c91fSMat Martineau if (IS_ERR(key_ref)) 17266563c91fSMat Martineau return PTR_ERR(key_ref); 17276563c91fSMat Martineau 172818026d86SEric Biggers ret = -EINVAL; 17296563c91fSMat Martineau if (_type) { 173018026d86SEric Biggers if (!_restriction) 173118026d86SEric Biggers goto error; 173218026d86SEric Biggers 17336563c91fSMat Martineau ret = key_get_type_from_user(type, _type, sizeof(type)); 17346563c91fSMat Martineau if (ret < 0) 17356563c91fSMat Martineau goto error; 17366563c91fSMat Martineau 17376563c91fSMat Martineau restriction = strndup_user(_restriction, PAGE_SIZE); 17386563c91fSMat Martineau if (IS_ERR(restriction)) { 17396563c91fSMat Martineau ret = PTR_ERR(restriction); 17406563c91fSMat Martineau goto error; 17416563c91fSMat Martineau } 174218026d86SEric Biggers } else { 174318026d86SEric Biggers if (_restriction) 174418026d86SEric Biggers goto error; 17456563c91fSMat Martineau } 17466563c91fSMat Martineau 174718026d86SEric Biggers ret = keyring_restrict(key_ref, _type ? type : NULL, restriction); 17486563c91fSMat Martineau kfree(restriction); 17496563c91fSMat Martineau error: 17506563c91fSMat Martineau key_ref_put(key_ref); 17516563c91fSMat Martineau return ret; 17526563c91fSMat Martineau } 17536563c91fSMat Martineau 17546563c91fSMat Martineau /* 175545e0f30cSDavid Howells * Get keyrings subsystem capabilities. 175645e0f30cSDavid Howells */ 175745e0f30cSDavid Howells long keyctl_capabilities(unsigned char __user *_buffer, size_t buflen) 175845e0f30cSDavid Howells { 175945e0f30cSDavid Howells size_t size = buflen; 176045e0f30cSDavid Howells 176145e0f30cSDavid Howells if (size > 0) { 176245e0f30cSDavid Howells if (size > sizeof(keyrings_capabilities)) 176345e0f30cSDavid Howells size = sizeof(keyrings_capabilities); 176445e0f30cSDavid Howells if (copy_to_user(_buffer, keyrings_capabilities, size) != 0) 176545e0f30cSDavid Howells return -EFAULT; 176645e0f30cSDavid Howells if (size < buflen && 176745e0f30cSDavid Howells clear_user(_buffer + size, buflen - size) != 0) 176845e0f30cSDavid Howells return -EFAULT; 176945e0f30cSDavid Howells } 177045e0f30cSDavid Howells 177145e0f30cSDavid Howells return sizeof(keyrings_capabilities); 177245e0f30cSDavid Howells } 177345e0f30cSDavid Howells 177445e0f30cSDavid Howells /* 1775973c9f4fSDavid Howells * The key control system call 17761da177e4SLinus Torvalds */ 1777938bb9f5SHeiko Carstens SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3, 1778938bb9f5SHeiko Carstens unsigned long, arg4, unsigned long, arg5) 17791da177e4SLinus Torvalds { 17801da177e4SLinus Torvalds switch (option) { 17811da177e4SLinus Torvalds case KEYCTL_GET_KEYRING_ID: 17821da177e4SLinus Torvalds return keyctl_get_keyring_ID((key_serial_t) arg2, 17831da177e4SLinus Torvalds (int) arg3); 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds case KEYCTL_JOIN_SESSION_KEYRING: 17861da177e4SLinus Torvalds return keyctl_join_session_keyring((const char __user *) arg2); 17871da177e4SLinus Torvalds 17881da177e4SLinus Torvalds case KEYCTL_UPDATE: 17891da177e4SLinus Torvalds return keyctl_update_key((key_serial_t) arg2, 17901da177e4SLinus Torvalds (const void __user *) arg3, 17911da177e4SLinus Torvalds (size_t) arg4); 17921da177e4SLinus Torvalds 17931da177e4SLinus Torvalds case KEYCTL_REVOKE: 17941da177e4SLinus Torvalds return keyctl_revoke_key((key_serial_t) arg2); 17951da177e4SLinus Torvalds 17961da177e4SLinus Torvalds case KEYCTL_DESCRIBE: 17971da177e4SLinus Torvalds return keyctl_describe_key((key_serial_t) arg2, 17981da177e4SLinus Torvalds (char __user *) arg3, 17991da177e4SLinus Torvalds (unsigned) arg4); 18001da177e4SLinus Torvalds 18011da177e4SLinus Torvalds case KEYCTL_CLEAR: 18021da177e4SLinus Torvalds return keyctl_keyring_clear((key_serial_t) arg2); 18031da177e4SLinus Torvalds 18041da177e4SLinus Torvalds case KEYCTL_LINK: 18051da177e4SLinus Torvalds return keyctl_keyring_link((key_serial_t) arg2, 18061da177e4SLinus Torvalds (key_serial_t) arg3); 18071da177e4SLinus Torvalds 18081da177e4SLinus Torvalds case KEYCTL_UNLINK: 18091da177e4SLinus Torvalds return keyctl_keyring_unlink((key_serial_t) arg2, 18101da177e4SLinus Torvalds (key_serial_t) arg3); 18111da177e4SLinus Torvalds 18121da177e4SLinus Torvalds case KEYCTL_SEARCH: 18131da177e4SLinus Torvalds return keyctl_keyring_search((key_serial_t) arg2, 18141da177e4SLinus Torvalds (const char __user *) arg3, 18151da177e4SLinus Torvalds (const char __user *) arg4, 18161da177e4SLinus Torvalds (key_serial_t) arg5); 18171da177e4SLinus Torvalds 18181da177e4SLinus Torvalds case KEYCTL_READ: 18191da177e4SLinus Torvalds return keyctl_read_key((key_serial_t) arg2, 18201da177e4SLinus Torvalds (char __user *) arg3, 18211da177e4SLinus Torvalds (size_t) arg4); 18221da177e4SLinus Torvalds 18231da177e4SLinus Torvalds case KEYCTL_CHOWN: 18241da177e4SLinus Torvalds return keyctl_chown_key((key_serial_t) arg2, 18251da177e4SLinus Torvalds (uid_t) arg3, 18261da177e4SLinus Torvalds (gid_t) arg4); 18271da177e4SLinus Torvalds 18281da177e4SLinus Torvalds case KEYCTL_SETPERM: 18291da177e4SLinus Torvalds return keyctl_setperm_key((key_serial_t) arg2, 1830028db3e2SLinus Torvalds (key_perm_t) arg3); 18311da177e4SLinus Torvalds 18321da177e4SLinus Torvalds case KEYCTL_INSTANTIATE: 18331da177e4SLinus Torvalds return keyctl_instantiate_key((key_serial_t) arg2, 18341da177e4SLinus Torvalds (const void __user *) arg3, 18351da177e4SLinus Torvalds (size_t) arg4, 18361da177e4SLinus Torvalds (key_serial_t) arg5); 18371da177e4SLinus Torvalds 18381da177e4SLinus Torvalds case KEYCTL_NEGATE: 18391da177e4SLinus Torvalds return keyctl_negate_key((key_serial_t) arg2, 18401da177e4SLinus Torvalds (unsigned) arg3, 18411da177e4SLinus Torvalds (key_serial_t) arg4); 18421da177e4SLinus Torvalds 18433e30148cSDavid Howells case KEYCTL_SET_REQKEY_KEYRING: 18443e30148cSDavid Howells return keyctl_set_reqkey_keyring(arg2); 18453e30148cSDavid Howells 1846017679c4SDavid Howells case KEYCTL_SET_TIMEOUT: 1847017679c4SDavid Howells return keyctl_set_timeout((key_serial_t) arg2, 1848017679c4SDavid Howells (unsigned) arg3); 1849017679c4SDavid Howells 1850b5f545c8SDavid Howells case KEYCTL_ASSUME_AUTHORITY: 1851b5f545c8SDavid Howells return keyctl_assume_authority((key_serial_t) arg2); 1852b5f545c8SDavid Howells 185370a5bb72SDavid Howells case KEYCTL_GET_SECURITY: 185470a5bb72SDavid Howells return keyctl_get_security((key_serial_t) arg2, 185590bd49abSJames Morris (char __user *) arg3, 185670a5bb72SDavid Howells (size_t) arg4); 185770a5bb72SDavid Howells 1858ee18d64cSDavid Howells case KEYCTL_SESSION_TO_PARENT: 1859ee18d64cSDavid Howells return keyctl_session_to_parent(); 1860ee18d64cSDavid Howells 1861fdd1b945SDavid Howells case KEYCTL_REJECT: 1862fdd1b945SDavid Howells return keyctl_reject_key((key_serial_t) arg2, 1863fdd1b945SDavid Howells (unsigned) arg3, 1864fdd1b945SDavid Howells (unsigned) arg4, 1865fdd1b945SDavid Howells (key_serial_t) arg5); 1866fdd1b945SDavid Howells 1867ee009e4aSDavid Howells case KEYCTL_INSTANTIATE_IOV: 1868ee009e4aSDavid Howells return keyctl_instantiate_key_iov( 1869ee009e4aSDavid Howells (key_serial_t) arg2, 1870ee009e4aSDavid Howells (const struct iovec __user *) arg3, 1871ee009e4aSDavid Howells (unsigned) arg4, 1872ee009e4aSDavid Howells (key_serial_t) arg5); 1873ee009e4aSDavid Howells 1874fd75815fSDavid Howells case KEYCTL_INVALIDATE: 1875fd75815fSDavid Howells return keyctl_invalidate_key((key_serial_t) arg2); 1876fd75815fSDavid Howells 1877f36f8c75SDavid Howells case KEYCTL_GET_PERSISTENT: 1878f36f8c75SDavid Howells return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3); 1879f36f8c75SDavid Howells 1880ddbb4114SMat Martineau case KEYCTL_DH_COMPUTE: 1881ddbb4114SMat Martineau return keyctl_dh_compute((struct keyctl_dh_params __user *) arg2, 18824693fc73SStephan Mueller (char __user *) arg3, (size_t) arg4, 1883f1c316a3SStephan Mueller (struct keyctl_kdf_params __user *) arg5); 1884ddbb4114SMat Martineau 18856563c91fSMat Martineau case KEYCTL_RESTRICT_KEYRING: 18866563c91fSMat Martineau return keyctl_restrict_keyring((key_serial_t) arg2, 18876563c91fSMat Martineau (const char __user *) arg3, 18886563c91fSMat Martineau (const char __user *) arg4); 18891da177e4SLinus Torvalds 189000d60fd3SDavid Howells case KEYCTL_PKEY_QUERY: 189100d60fd3SDavid Howells if (arg3 != 0) 189200d60fd3SDavid Howells return -EINVAL; 189300d60fd3SDavid Howells return keyctl_pkey_query((key_serial_t)arg2, 189400d60fd3SDavid Howells (const char __user *)arg4, 1895468e91ceSBen Dooks (struct keyctl_pkey_query __user *)arg5); 189600d60fd3SDavid Howells 189700d60fd3SDavid Howells case KEYCTL_PKEY_ENCRYPT: 189800d60fd3SDavid Howells case KEYCTL_PKEY_DECRYPT: 189900d60fd3SDavid Howells case KEYCTL_PKEY_SIGN: 190000d60fd3SDavid Howells return keyctl_pkey_e_d_s( 190100d60fd3SDavid Howells option, 190200d60fd3SDavid Howells (const struct keyctl_pkey_params __user *)arg2, 190300d60fd3SDavid Howells (const char __user *)arg3, 190400d60fd3SDavid Howells (const void __user *)arg4, 190500d60fd3SDavid Howells (void __user *)arg5); 190600d60fd3SDavid Howells 190700d60fd3SDavid Howells case KEYCTL_PKEY_VERIFY: 190800d60fd3SDavid Howells return keyctl_pkey_verify( 190900d60fd3SDavid Howells (const struct keyctl_pkey_params __user *)arg2, 191000d60fd3SDavid Howells (const char __user *)arg3, 191100d60fd3SDavid Howells (const void __user *)arg4, 191200d60fd3SDavid Howells (const void __user *)arg5); 191300d60fd3SDavid Howells 1914ed0ac5c7SDavid Howells case KEYCTL_MOVE: 1915ed0ac5c7SDavid Howells return keyctl_keyring_move((key_serial_t)arg2, 1916ed0ac5c7SDavid Howells (key_serial_t)arg3, 1917ed0ac5c7SDavid Howells (key_serial_t)arg4, 1918ed0ac5c7SDavid Howells (unsigned int)arg5); 1919ed0ac5c7SDavid Howells 192045e0f30cSDavid Howells case KEYCTL_CAPABILITIES: 192145e0f30cSDavid Howells return keyctl_capabilities((unsigned char __user *)arg2, (size_t)arg3); 192245e0f30cSDavid Howells 19231da177e4SLinus Torvalds default: 19241da177e4SLinus Torvalds return -EOPNOTSUPP; 19251da177e4SLinus Torvalds } 19261da177e4SLinus Torvalds } 1927