1973c9f4fSDavid Howells /* Userspace key control operations 21da177e4SLinus Torvalds * 33e30148cSDavid Howells * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved. 41da177e4SLinus Torvalds * Written by David Howells (dhowells@redhat.com) 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 71da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 81da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 91da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds #include <linux/init.h> 131da177e4SLinus Torvalds #include <linux/sched.h> 1429930025SIngo Molnar #include <linux/sched/task.h> 151da177e4SLinus Torvalds #include <linux/slab.h> 161da177e4SLinus Torvalds #include <linux/syscalls.h> 1759e6b9c1SBryan Schumaker #include <linux/key.h> 181da177e4SLinus Torvalds #include <linux/keyctl.h> 191da177e4SLinus Torvalds #include <linux/fs.h> 20c59ede7bSRandy.Dunlap #include <linux/capability.h> 215b825c3aSIngo Molnar #include <linux/cred.h> 220cb409d9SDavi Arnaut #include <linux/string.h> 231da177e4SLinus Torvalds #include <linux/err.h> 2438bbca6bSDavid Howells #include <linux/vmalloc.h> 2570a5bb72SDavid Howells #include <linux/security.h> 26a27bb332SKent Overstreet #include <linux/uio.h> 277c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 28822ad64dSDavid Howells #include <keys/request_key_auth-type.h> 291da177e4SLinus Torvalds #include "internal.h" 301da177e4SLinus Torvalds 31aa9d4437SDavid Howells #define KEY_MAX_DESC_SIZE 4096 32aa9d4437SDavid Howells 33b206f281SDavid Howells static const unsigned char keyrings_capabilities[2] = { 3445e0f30cSDavid Howells [0] = (KEYCTL_CAPS0_CAPABILITIES | 3545e0f30cSDavid Howells (IS_ENABLED(CONFIG_PERSISTENT_KEYRINGS) ? KEYCTL_CAPS0_PERSISTENT_KEYRINGS : 0) | 3645e0f30cSDavid Howells (IS_ENABLED(CONFIG_KEY_DH_OPERATIONS) ? KEYCTL_CAPS0_DIFFIE_HELLMAN : 0) | 3745e0f30cSDavid Howells (IS_ENABLED(CONFIG_ASYMMETRIC_KEY_TYPE) ? KEYCTL_CAPS0_PUBLIC_KEY : 0) | 3845e0f30cSDavid Howells (IS_ENABLED(CONFIG_BIG_KEYS) ? KEYCTL_CAPS0_BIG_KEY : 0) | 3945e0f30cSDavid Howells KEYCTL_CAPS0_INVALIDATE | 4045e0f30cSDavid Howells KEYCTL_CAPS0_RESTRICT_KEYRING | 4145e0f30cSDavid Howells KEYCTL_CAPS0_MOVE 4245e0f30cSDavid Howells ), 43*3b6e4de0SDavid Howells [1] = (KEYCTL_CAPS1_NS_KEYRING_NAME | 44*3b6e4de0SDavid Howells KEYCTL_CAPS1_NS_KEY_TAG), 4545e0f30cSDavid Howells }; 4645e0f30cSDavid Howells 470cb409d9SDavi Arnaut static int key_get_type_from_user(char *type, 480cb409d9SDavi Arnaut const char __user *_type, 490cb409d9SDavi Arnaut unsigned len) 500cb409d9SDavi Arnaut { 510cb409d9SDavi Arnaut int ret; 520cb409d9SDavi Arnaut 530cb409d9SDavi Arnaut ret = strncpy_from_user(type, _type, len); 540cb409d9SDavi Arnaut if (ret < 0) 554303ef19SDan Carpenter return ret; 560cb409d9SDavi Arnaut if (ret == 0 || ret >= len) 570cb409d9SDavi Arnaut return -EINVAL; 5854e2c2c1SDavid Howells if (type[0] == '.') 5954e2c2c1SDavid Howells return -EPERM; 600cb409d9SDavi Arnaut type[len - 1] = '\0'; 610cb409d9SDavi Arnaut return 0; 620cb409d9SDavi Arnaut } 630cb409d9SDavi Arnaut 641da177e4SLinus Torvalds /* 65973c9f4fSDavid Howells * Extract the description of a new key from userspace and either add it as a 66973c9f4fSDavid Howells * new key to the specified keyring or update a matching key in that keyring. 67973c9f4fSDavid Howells * 68cf7f601cSDavid Howells * If the description is NULL or an empty string, the key type is asked to 69cf7f601cSDavid Howells * generate one from the payload. 70cf7f601cSDavid Howells * 71973c9f4fSDavid Howells * The keyring must be writable so that we can attach the key to it. 72973c9f4fSDavid Howells * 73973c9f4fSDavid Howells * If successful, the new key's serial number is returned, otherwise an error 74973c9f4fSDavid Howells * code is returned. 751da177e4SLinus Torvalds */ 761e7bfb21SHeiko Carstens SYSCALL_DEFINE5(add_key, const char __user *, _type, 771e7bfb21SHeiko Carstens const char __user *, _description, 781e7bfb21SHeiko Carstens const void __user *, _payload, 791e7bfb21SHeiko Carstens size_t, plen, 801e7bfb21SHeiko Carstens key_serial_t, ringid) 811da177e4SLinus Torvalds { 82664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 831da177e4SLinus Torvalds char type[32], *description; 841da177e4SLinus Torvalds void *payload; 850cb409d9SDavi Arnaut long ret; 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds ret = -EINVAL; 8838bbca6bSDavid Howells if (plen > 1024 * 1024 - 1) 891da177e4SLinus Torvalds goto error; 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds /* draw all the data into kernel space */ 920cb409d9SDavi Arnaut ret = key_get_type_from_user(type, _type, sizeof(type)); 931da177e4SLinus Torvalds if (ret < 0) 941da177e4SLinus Torvalds goto error; 951da177e4SLinus Torvalds 96cf7f601cSDavid Howells description = NULL; 97cf7f601cSDavid Howells if (_description) { 98aa9d4437SDavid Howells description = strndup_user(_description, KEY_MAX_DESC_SIZE); 990cb409d9SDavi Arnaut if (IS_ERR(description)) { 1000cb409d9SDavi Arnaut ret = PTR_ERR(description); 1013e30148cSDavid Howells goto error; 1020cb409d9SDavi Arnaut } 103cf7f601cSDavid Howells if (!*description) { 104cf7f601cSDavid Howells kfree(description); 105cf7f601cSDavid Howells description = NULL; 106a4e3b8d7SMimi Zohar } else if ((description[0] == '.') && 107a4e3b8d7SMimi Zohar (strncmp(type, "keyring", 7) == 0)) { 108a4e3b8d7SMimi Zohar ret = -EPERM; 109a4e3b8d7SMimi Zohar goto error2; 110cf7f601cSDavid Howells } 111cf7f601cSDavid Howells } 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 1141da177e4SLinus Torvalds payload = NULL; 1151da177e4SLinus Torvalds 1165649645dSEric Biggers if (plen) { 1171da177e4SLinus Torvalds ret = -ENOMEM; 118752ade68SMichal Hocko payload = kvmalloc(plen, GFP_KERNEL); 1191da177e4SLinus Torvalds if (!payload) 1201da177e4SLinus Torvalds goto error2; 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds ret = -EFAULT; 1231da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 1241da177e4SLinus Torvalds goto error3; 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds /* find the target keyring (which must be writable) */ 128f5895943SDavid Howells keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); 129664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 130664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 1311da177e4SLinus Torvalds goto error3; 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds /* create or update the requested key and add it to the target 1351da177e4SLinus Torvalds * keyring */ 136664cceb0SDavid Howells key_ref = key_create_or_update(keyring_ref, type, description, 1376b79ccb5SArun Raghavan payload, plen, KEY_PERM_UNDEF, 1386b79ccb5SArun Raghavan KEY_ALLOC_IN_QUOTA); 139664cceb0SDavid Howells if (!IS_ERR(key_ref)) { 140664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 141664cceb0SDavid Howells key_ref_put(key_ref); 1421da177e4SLinus Torvalds } 1431da177e4SLinus Torvalds else { 144664cceb0SDavid Howells ret = PTR_ERR(key_ref); 1451da177e4SLinus Torvalds } 1461da177e4SLinus Torvalds 147664cceb0SDavid Howells key_ref_put(keyring_ref); 1481da177e4SLinus Torvalds error3: 14957070c85SEric Biggers if (payload) { 15057070c85SEric Biggers memzero_explicit(payload, plen); 151d0e0eba0SGeliang Tang kvfree(payload); 15257070c85SEric Biggers } 1531da177e4SLinus Torvalds error2: 1541da177e4SLinus Torvalds kfree(description); 1551da177e4SLinus Torvalds error: 1561da177e4SLinus Torvalds return ret; 157a8b17ed0SDavid Howells } 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds /* 160973c9f4fSDavid Howells * Search the process keyrings and keyring trees linked from those for a 161973c9f4fSDavid Howells * matching key. Keyrings must have appropriate Search permission to be 162973c9f4fSDavid Howells * searched. 163973c9f4fSDavid Howells * 164973c9f4fSDavid Howells * If a key is found, it will be attached to the destination keyring if there's 165973c9f4fSDavid Howells * one specified and the serial number of the key will be returned. 166973c9f4fSDavid Howells * 167973c9f4fSDavid Howells * If no key is found, /sbin/request-key will be invoked if _callout_info is 168973c9f4fSDavid Howells * non-NULL in an attempt to create a key. The _callout_info string will be 169973c9f4fSDavid Howells * passed to /sbin/request-key to aid with completing the request. If the 170973c9f4fSDavid Howells * _callout_info string is "" then it will be changed to "-". 1711da177e4SLinus Torvalds */ 1721e7bfb21SHeiko Carstens SYSCALL_DEFINE4(request_key, const char __user *, _type, 1731e7bfb21SHeiko Carstens const char __user *, _description, 1741e7bfb21SHeiko Carstens const char __user *, _callout_info, 1751e7bfb21SHeiko Carstens key_serial_t, destringid) 1761da177e4SLinus Torvalds { 1771da177e4SLinus Torvalds struct key_type *ktype; 178664cceb0SDavid Howells struct key *key; 179664cceb0SDavid Howells key_ref_t dest_ref; 1804a38e122SDavid Howells size_t callout_len; 1811da177e4SLinus Torvalds char type[32], *description, *callout_info; 1820cb409d9SDavi Arnaut long ret; 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds /* pull the type into kernel space */ 1850cb409d9SDavi Arnaut ret = key_get_type_from_user(type, _type, sizeof(type)); 1861da177e4SLinus Torvalds if (ret < 0) 1871da177e4SLinus Torvalds goto error; 1881260f801SDavid Howells 1891da177e4SLinus Torvalds /* pull the description into kernel space */ 190aa9d4437SDavid Howells description = strndup_user(_description, KEY_MAX_DESC_SIZE); 1910cb409d9SDavi Arnaut if (IS_ERR(description)) { 1920cb409d9SDavi Arnaut ret = PTR_ERR(description); 1931da177e4SLinus Torvalds goto error; 1940cb409d9SDavi Arnaut } 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds /* pull the callout info into kernel space */ 1971da177e4SLinus Torvalds callout_info = NULL; 1984a38e122SDavid Howells callout_len = 0; 1991da177e4SLinus Torvalds if (_callout_info) { 2000cb409d9SDavi Arnaut callout_info = strndup_user(_callout_info, PAGE_SIZE); 2010cb409d9SDavi Arnaut if (IS_ERR(callout_info)) { 2020cb409d9SDavi Arnaut ret = PTR_ERR(callout_info); 2031da177e4SLinus Torvalds goto error2; 2040cb409d9SDavi Arnaut } 2054a38e122SDavid Howells callout_len = strlen(callout_info); 2061da177e4SLinus Torvalds } 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds /* get the destination keyring if specified */ 209664cceb0SDavid Howells dest_ref = NULL; 2101da177e4SLinus Torvalds if (destringid) { 2115593122eSDavid Howells dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE, 212f5895943SDavid Howells KEY_NEED_WRITE); 213664cceb0SDavid Howells if (IS_ERR(dest_ref)) { 214664cceb0SDavid Howells ret = PTR_ERR(dest_ref); 2151da177e4SLinus Torvalds goto error3; 2161da177e4SLinus Torvalds } 2171da177e4SLinus Torvalds } 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds /* find the key type */ 2201da177e4SLinus Torvalds ktype = key_type_lookup(type); 2211da177e4SLinus Torvalds if (IS_ERR(ktype)) { 2221da177e4SLinus Torvalds ret = PTR_ERR(ktype); 2231da177e4SLinus Torvalds goto error4; 2241da177e4SLinus Torvalds } 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds /* do the search */ 2274a38e122SDavid Howells key = request_key_and_link(ktype, description, callout_info, 2284a38e122SDavid Howells callout_len, NULL, key_ref_to_ptr(dest_ref), 2297e047ef5SDavid Howells KEY_ALLOC_IN_QUOTA); 2301da177e4SLinus Torvalds if (IS_ERR(key)) { 2311da177e4SLinus Torvalds ret = PTR_ERR(key); 2321da177e4SLinus Torvalds goto error5; 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds 2354aab1e89SDavid Howells /* wait for the key to finish being constructed */ 2364aab1e89SDavid Howells ret = wait_for_key_construction(key, 1); 2374aab1e89SDavid Howells if (ret < 0) 2384aab1e89SDavid Howells goto error6; 2394aab1e89SDavid Howells 2401da177e4SLinus Torvalds ret = key->serial; 2411da177e4SLinus Torvalds 2424aab1e89SDavid Howells error6: 2431da177e4SLinus Torvalds key_put(key); 2441da177e4SLinus Torvalds error5: 2451da177e4SLinus Torvalds key_type_put(ktype); 2461da177e4SLinus Torvalds error4: 247664cceb0SDavid Howells key_ref_put(dest_ref); 2481da177e4SLinus Torvalds error3: 2491da177e4SLinus Torvalds kfree(callout_info); 2501da177e4SLinus Torvalds error2: 2511da177e4SLinus Torvalds kfree(description); 2521da177e4SLinus Torvalds error: 2531da177e4SLinus Torvalds return ret; 254a8b17ed0SDavid Howells } 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds /* 257973c9f4fSDavid Howells * Get the ID of the specified process keyring. 258973c9f4fSDavid Howells * 259973c9f4fSDavid Howells * The requested keyring must have search permission to be found. 260973c9f4fSDavid Howells * 261973c9f4fSDavid Howells * If successful, the ID of the requested keyring will be returned. 2621da177e4SLinus Torvalds */ 2631da177e4SLinus Torvalds long keyctl_get_keyring_ID(key_serial_t id, int create) 2641da177e4SLinus Torvalds { 265664cceb0SDavid Howells key_ref_t key_ref; 2665593122eSDavid Howells unsigned long lflags; 2671da177e4SLinus Torvalds long ret; 2681da177e4SLinus Torvalds 2695593122eSDavid Howells lflags = create ? KEY_LOOKUP_CREATE : 0; 270f5895943SDavid Howells key_ref = lookup_user_key(id, lflags, KEY_NEED_SEARCH); 271664cceb0SDavid Howells if (IS_ERR(key_ref)) { 272664cceb0SDavid Howells ret = PTR_ERR(key_ref); 2731da177e4SLinus Torvalds goto error; 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds 276664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 277664cceb0SDavid Howells key_ref_put(key_ref); 2781da177e4SLinus Torvalds error: 2791da177e4SLinus Torvalds return ret; 280973c9f4fSDavid Howells } 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds /* 283973c9f4fSDavid Howells * Join a (named) session keyring. 284973c9f4fSDavid Howells * 285973c9f4fSDavid Howells * Create and join an anonymous session keyring or join a named session 286973c9f4fSDavid Howells * keyring, creating it if necessary. A named session keyring must have Search 287973c9f4fSDavid Howells * permission for it to be joined. Session keyrings without this permit will 288ee8f844eSDavid Howells * be skipped over. It is not permitted for userspace to create or join 289ee8f844eSDavid Howells * keyrings whose name begin with a dot. 290973c9f4fSDavid Howells * 291973c9f4fSDavid Howells * If successful, the ID of the joined session keyring will be returned. 2921da177e4SLinus Torvalds */ 2931da177e4SLinus Torvalds long keyctl_join_session_keyring(const char __user *_name) 2941da177e4SLinus Torvalds { 2951da177e4SLinus Torvalds char *name; 2960cb409d9SDavi Arnaut long ret; 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds /* fetch the name from userspace */ 2991da177e4SLinus Torvalds name = NULL; 3001da177e4SLinus Torvalds if (_name) { 301aa9d4437SDavid Howells name = strndup_user(_name, KEY_MAX_DESC_SIZE); 3020cb409d9SDavi Arnaut if (IS_ERR(name)) { 3030cb409d9SDavi Arnaut ret = PTR_ERR(name); 3041da177e4SLinus Torvalds goto error; 3050cb409d9SDavi Arnaut } 306ee8f844eSDavid Howells 307ee8f844eSDavid Howells ret = -EPERM; 308ee8f844eSDavid Howells if (name[0] == '.') 309ee8f844eSDavid Howells goto error_name; 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds /* join the session */ 3131da177e4SLinus Torvalds ret = join_session_keyring(name); 314ee8f844eSDavid Howells error_name: 3150d54ee1cSVegard Nossum kfree(name); 3161da177e4SLinus Torvalds error: 3171da177e4SLinus Torvalds return ret; 318a8b17ed0SDavid Howells } 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds /* 321973c9f4fSDavid Howells * Update a key's data payload from the given data. 322973c9f4fSDavid Howells * 323973c9f4fSDavid Howells * The key must grant the caller Write permission and the key type must support 324973c9f4fSDavid Howells * updating for this to work. A negative key can be positively instantiated 325973c9f4fSDavid Howells * with this call. 326973c9f4fSDavid Howells * 327973c9f4fSDavid Howells * If successful, 0 will be returned. If the key type does not support 328973c9f4fSDavid Howells * updating, then -EOPNOTSUPP will be returned. 3291da177e4SLinus Torvalds */ 3301da177e4SLinus Torvalds long keyctl_update_key(key_serial_t id, 3311da177e4SLinus Torvalds const void __user *_payload, 3321da177e4SLinus Torvalds size_t plen) 3331da177e4SLinus Torvalds { 334664cceb0SDavid Howells key_ref_t key_ref; 3351da177e4SLinus Torvalds void *payload; 3361da177e4SLinus Torvalds long ret; 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds ret = -EINVAL; 3391da177e4SLinus Torvalds if (plen > PAGE_SIZE) 3401da177e4SLinus Torvalds goto error; 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 3431da177e4SLinus Torvalds payload = NULL; 3445649645dSEric Biggers if (plen) { 3451da177e4SLinus Torvalds ret = -ENOMEM; 3461da177e4SLinus Torvalds payload = kmalloc(plen, GFP_KERNEL); 3471da177e4SLinus Torvalds if (!payload) 3481da177e4SLinus Torvalds goto error; 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds ret = -EFAULT; 3511da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 3521da177e4SLinus Torvalds goto error2; 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds /* find the target key (which must be writable) */ 356f5895943SDavid Howells key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE); 357664cceb0SDavid Howells if (IS_ERR(key_ref)) { 358664cceb0SDavid Howells ret = PTR_ERR(key_ref); 3591da177e4SLinus Torvalds goto error2; 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds 3621da177e4SLinus Torvalds /* update the key */ 363664cceb0SDavid Howells ret = key_update(key_ref, payload, plen); 3641da177e4SLinus Torvalds 365664cceb0SDavid Howells key_ref_put(key_ref); 3661da177e4SLinus Torvalds error2: 36757070c85SEric Biggers kzfree(payload); 3681da177e4SLinus Torvalds error: 3691da177e4SLinus Torvalds return ret; 370a8b17ed0SDavid Howells } 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds /* 373973c9f4fSDavid Howells * Revoke a key. 374973c9f4fSDavid Howells * 375973c9f4fSDavid Howells * The key must be grant the caller Write or Setattr permission for this to 376973c9f4fSDavid Howells * work. The key type should give up its quota claim when revoked. The key 377973c9f4fSDavid Howells * and any links to the key will be automatically garbage collected after a 378973c9f4fSDavid Howells * certain amount of time (/proc/sys/kernel/keys/gc_delay). 379973c9f4fSDavid Howells * 380d3600bcfSMimi Zohar * Keys with KEY_FLAG_KEEP set should not be revoked. 381d3600bcfSMimi Zohar * 382973c9f4fSDavid Howells * If successful, 0 is returned. 3831da177e4SLinus Torvalds */ 3841da177e4SLinus Torvalds long keyctl_revoke_key(key_serial_t id) 3851da177e4SLinus Torvalds { 386664cceb0SDavid Howells key_ref_t key_ref; 387d3600bcfSMimi Zohar struct key *key; 3881da177e4SLinus Torvalds long ret; 3891da177e4SLinus Torvalds 390f5895943SDavid Howells key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE); 391664cceb0SDavid Howells if (IS_ERR(key_ref)) { 392664cceb0SDavid Howells ret = PTR_ERR(key_ref); 3930c2c9a3fSDavid Howells if (ret != -EACCES) 3941da177e4SLinus Torvalds goto error; 395f5895943SDavid Howells key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR); 3960c2c9a3fSDavid Howells if (IS_ERR(key_ref)) { 3970c2c9a3fSDavid Howells ret = PTR_ERR(key_ref); 3980c2c9a3fSDavid Howells goto error; 3990c2c9a3fSDavid Howells } 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds 402d3600bcfSMimi Zohar key = key_ref_to_ptr(key_ref); 4031da177e4SLinus Torvalds ret = 0; 4041d6d167cSMimi Zohar if (test_bit(KEY_FLAG_KEEP, &key->flags)) 4051d6d167cSMimi Zohar ret = -EPERM; 4061d6d167cSMimi Zohar else 4071d6d167cSMimi Zohar key_revoke(key); 4081da177e4SLinus Torvalds 409664cceb0SDavid Howells key_ref_put(key_ref); 4101da177e4SLinus Torvalds error: 4111260f801SDavid Howells return ret; 412a8b17ed0SDavid Howells } 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds /* 415fd75815fSDavid Howells * Invalidate a key. 416fd75815fSDavid Howells * 417fd75815fSDavid Howells * The key must be grant the caller Invalidate permission for this to work. 418fd75815fSDavid Howells * The key and any links to the key will be automatically garbage collected 419fd75815fSDavid Howells * immediately. 420fd75815fSDavid Howells * 421d3600bcfSMimi Zohar * Keys with KEY_FLAG_KEEP set should not be invalidated. 422d3600bcfSMimi Zohar * 423fd75815fSDavid Howells * If successful, 0 is returned. 424fd75815fSDavid Howells */ 425fd75815fSDavid Howells long keyctl_invalidate_key(key_serial_t id) 426fd75815fSDavid Howells { 427fd75815fSDavid Howells key_ref_t key_ref; 428d3600bcfSMimi Zohar struct key *key; 429fd75815fSDavid Howells long ret; 430fd75815fSDavid Howells 431fd75815fSDavid Howells kenter("%d", id); 432fd75815fSDavid Howells 433f5895943SDavid Howells key_ref = lookup_user_key(id, 0, KEY_NEED_SEARCH); 434fd75815fSDavid Howells if (IS_ERR(key_ref)) { 435fd75815fSDavid Howells ret = PTR_ERR(key_ref); 4360c7774abSDavid Howells 4370c7774abSDavid Howells /* Root is permitted to invalidate certain special keys */ 4380c7774abSDavid Howells if (capable(CAP_SYS_ADMIN)) { 4390c7774abSDavid Howells key_ref = lookup_user_key(id, 0, 0); 4400c7774abSDavid Howells if (IS_ERR(key_ref)) 4410c7774abSDavid Howells goto error; 4420c7774abSDavid Howells if (test_bit(KEY_FLAG_ROOT_CAN_INVAL, 4430c7774abSDavid Howells &key_ref_to_ptr(key_ref)->flags)) 4440c7774abSDavid Howells goto invalidate; 4450c7774abSDavid Howells goto error_put; 4460c7774abSDavid Howells } 4470c7774abSDavid Howells 448fd75815fSDavid Howells goto error; 449fd75815fSDavid Howells } 450fd75815fSDavid Howells 4510c7774abSDavid Howells invalidate: 452d3600bcfSMimi Zohar key = key_ref_to_ptr(key_ref); 453fd75815fSDavid Howells ret = 0; 454d3600bcfSMimi Zohar if (test_bit(KEY_FLAG_KEEP, &key->flags)) 455d3600bcfSMimi Zohar ret = -EPERM; 4561d6d167cSMimi Zohar else 457d3600bcfSMimi Zohar key_invalidate(key); 4580c7774abSDavid Howells error_put: 459fd75815fSDavid Howells key_ref_put(key_ref); 460fd75815fSDavid Howells error: 461fd75815fSDavid Howells kleave(" = %ld", ret); 462fd75815fSDavid Howells return ret; 463fd75815fSDavid Howells } 464fd75815fSDavid Howells 465fd75815fSDavid Howells /* 466973c9f4fSDavid Howells * Clear the specified keyring, creating an empty process keyring if one of the 467973c9f4fSDavid Howells * special keyring IDs is used. 468973c9f4fSDavid Howells * 469d3600bcfSMimi Zohar * The keyring must grant the caller Write permission and not have 470d3600bcfSMimi Zohar * KEY_FLAG_KEEP set for this to work. If successful, 0 will be returned. 4711da177e4SLinus Torvalds */ 4721da177e4SLinus Torvalds long keyctl_keyring_clear(key_serial_t ringid) 4731da177e4SLinus Torvalds { 474664cceb0SDavid Howells key_ref_t keyring_ref; 475d3600bcfSMimi Zohar struct key *keyring; 4761da177e4SLinus Torvalds long ret; 4771da177e4SLinus Torvalds 478f5895943SDavid Howells keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); 479664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 480664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 481700920ebSDavid Howells 482700920ebSDavid Howells /* Root is permitted to invalidate certain special keyrings */ 483700920ebSDavid Howells if (capable(CAP_SYS_ADMIN)) { 484700920ebSDavid Howells keyring_ref = lookup_user_key(ringid, 0, 0); 485700920ebSDavid Howells if (IS_ERR(keyring_ref)) 486700920ebSDavid Howells goto error; 487700920ebSDavid Howells if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR, 488700920ebSDavid Howells &key_ref_to_ptr(keyring_ref)->flags)) 489700920ebSDavid Howells goto clear; 490700920ebSDavid Howells goto error_put; 491700920ebSDavid Howells } 492700920ebSDavid Howells 4931da177e4SLinus Torvalds goto error; 4941da177e4SLinus Torvalds } 4951da177e4SLinus Torvalds 496700920ebSDavid Howells clear: 497d3600bcfSMimi Zohar keyring = key_ref_to_ptr(keyring_ref); 498d3600bcfSMimi Zohar if (test_bit(KEY_FLAG_KEEP, &keyring->flags)) 499d3600bcfSMimi Zohar ret = -EPERM; 500d3600bcfSMimi Zohar else 501d3600bcfSMimi Zohar ret = keyring_clear(keyring); 502700920ebSDavid Howells error_put: 503664cceb0SDavid Howells key_ref_put(keyring_ref); 5041da177e4SLinus Torvalds error: 5051da177e4SLinus Torvalds return ret; 506a8b17ed0SDavid Howells } 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds /* 509973c9f4fSDavid Howells * Create a link from a keyring to a key if there's no matching key in the 510973c9f4fSDavid Howells * keyring, otherwise replace the link to the matching key with a link to the 511973c9f4fSDavid Howells * new key. 512973c9f4fSDavid Howells * 513973c9f4fSDavid Howells * The key must grant the caller Link permission and the the keyring must grant 514973c9f4fSDavid Howells * the caller Write permission. Furthermore, if an additional link is created, 515973c9f4fSDavid Howells * the keyring's quota will be extended. 516973c9f4fSDavid Howells * 517973c9f4fSDavid Howells * If successful, 0 will be returned. 5181da177e4SLinus Torvalds */ 5191da177e4SLinus Torvalds long keyctl_keyring_link(key_serial_t id, key_serial_t ringid) 5201da177e4SLinus Torvalds { 521664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 5221da177e4SLinus Torvalds long ret; 5231da177e4SLinus Torvalds 524f5895943SDavid Howells keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); 525664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 526664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 5271da177e4SLinus Torvalds goto error; 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 530f5895943SDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK); 531664cceb0SDavid Howells if (IS_ERR(key_ref)) { 532664cceb0SDavid Howells ret = PTR_ERR(key_ref); 5331da177e4SLinus Torvalds goto error2; 5341da177e4SLinus Torvalds } 5351da177e4SLinus Torvalds 536664cceb0SDavid Howells ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref)); 5371da177e4SLinus Torvalds 538664cceb0SDavid Howells key_ref_put(key_ref); 5391da177e4SLinus Torvalds error2: 540664cceb0SDavid Howells key_ref_put(keyring_ref); 5411da177e4SLinus Torvalds error: 5421da177e4SLinus Torvalds return ret; 543a8b17ed0SDavid Howells } 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds /* 546973c9f4fSDavid Howells * Unlink a key from a keyring. 547973c9f4fSDavid Howells * 548973c9f4fSDavid Howells * The keyring must grant the caller Write permission for this to work; the key 549973c9f4fSDavid Howells * itself need not grant the caller anything. If the last link to a key is 550973c9f4fSDavid Howells * removed then that key will be scheduled for destruction. 551973c9f4fSDavid Howells * 552d3600bcfSMimi Zohar * Keys or keyrings with KEY_FLAG_KEEP set should not be unlinked. 553d3600bcfSMimi Zohar * 554973c9f4fSDavid Howells * If successful, 0 will be returned. 5551da177e4SLinus Torvalds */ 5561da177e4SLinus Torvalds long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid) 5571da177e4SLinus Torvalds { 558664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 559d3600bcfSMimi Zohar struct key *keyring, *key; 5601da177e4SLinus Torvalds long ret; 5611da177e4SLinus Torvalds 562f5895943SDavid Howells keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE); 563664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 564664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 5651da177e4SLinus Torvalds goto error; 5661da177e4SLinus Torvalds } 5671da177e4SLinus Torvalds 5685593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0); 569664cceb0SDavid Howells if (IS_ERR(key_ref)) { 570664cceb0SDavid Howells ret = PTR_ERR(key_ref); 5711da177e4SLinus Torvalds goto error2; 5721da177e4SLinus Torvalds } 5731da177e4SLinus Torvalds 574d3600bcfSMimi Zohar keyring = key_ref_to_ptr(keyring_ref); 575d3600bcfSMimi Zohar key = key_ref_to_ptr(key_ref); 576d3600bcfSMimi Zohar if (test_bit(KEY_FLAG_KEEP, &keyring->flags) && 577d3600bcfSMimi Zohar test_bit(KEY_FLAG_KEEP, &key->flags)) 578d3600bcfSMimi Zohar ret = -EPERM; 579d3600bcfSMimi Zohar else 580d3600bcfSMimi Zohar ret = key_unlink(keyring, key); 5811da177e4SLinus Torvalds 582664cceb0SDavid Howells key_ref_put(key_ref); 5831da177e4SLinus Torvalds error2: 584664cceb0SDavid Howells key_ref_put(keyring_ref); 5851da177e4SLinus Torvalds error: 5861da177e4SLinus Torvalds return ret; 587a8b17ed0SDavid Howells } 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds /* 590ed0ac5c7SDavid Howells * Move a link to a key from one keyring to another, displacing any matching 591ed0ac5c7SDavid Howells * key from the destination keyring. 592ed0ac5c7SDavid Howells * 593ed0ac5c7SDavid Howells * The key must grant the caller Link permission and both keyrings must grant 594ed0ac5c7SDavid Howells * the caller Write permission. There must also be a link in the from keyring 595ed0ac5c7SDavid Howells * to the key. If both keyrings are the same, nothing is done. 596ed0ac5c7SDavid Howells * 597ed0ac5c7SDavid Howells * If successful, 0 will be returned. 598ed0ac5c7SDavid Howells */ 599ed0ac5c7SDavid Howells long keyctl_keyring_move(key_serial_t id, key_serial_t from_ringid, 600ed0ac5c7SDavid Howells key_serial_t to_ringid, unsigned int flags) 601ed0ac5c7SDavid Howells { 602ed0ac5c7SDavid Howells key_ref_t key_ref, from_ref, to_ref; 603ed0ac5c7SDavid Howells long ret; 604ed0ac5c7SDavid Howells 605ed0ac5c7SDavid Howells if (flags & ~KEYCTL_MOVE_EXCL) 606ed0ac5c7SDavid Howells return -EINVAL; 607ed0ac5c7SDavid Howells 608ed0ac5c7SDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK); 609ed0ac5c7SDavid Howells if (IS_ERR(key_ref)) 610ed0ac5c7SDavid Howells return PTR_ERR(key_ref); 611ed0ac5c7SDavid Howells 612ed0ac5c7SDavid Howells from_ref = lookup_user_key(from_ringid, 0, KEY_NEED_WRITE); 613ed0ac5c7SDavid Howells if (IS_ERR(from_ref)) { 614ed0ac5c7SDavid Howells ret = PTR_ERR(from_ref); 615ed0ac5c7SDavid Howells goto error2; 616ed0ac5c7SDavid Howells } 617ed0ac5c7SDavid Howells 618ed0ac5c7SDavid Howells to_ref = lookup_user_key(to_ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); 619ed0ac5c7SDavid Howells if (IS_ERR(to_ref)) { 620ed0ac5c7SDavid Howells ret = PTR_ERR(to_ref); 621ed0ac5c7SDavid Howells goto error3; 622ed0ac5c7SDavid Howells } 623ed0ac5c7SDavid Howells 624ed0ac5c7SDavid Howells ret = key_move(key_ref_to_ptr(key_ref), key_ref_to_ptr(from_ref), 625ed0ac5c7SDavid Howells key_ref_to_ptr(to_ref), flags); 626ed0ac5c7SDavid Howells 627ed0ac5c7SDavid Howells key_ref_put(to_ref); 628ed0ac5c7SDavid Howells error3: 629ed0ac5c7SDavid Howells key_ref_put(from_ref); 630ed0ac5c7SDavid Howells error2: 631ed0ac5c7SDavid Howells key_ref_put(key_ref); 632ed0ac5c7SDavid Howells return ret; 633ed0ac5c7SDavid Howells } 634ed0ac5c7SDavid Howells 635ed0ac5c7SDavid Howells /* 636973c9f4fSDavid Howells * Return a description of a key to userspace. 637973c9f4fSDavid Howells * 638973c9f4fSDavid Howells * The key must grant the caller View permission for this to work. 639973c9f4fSDavid Howells * 640973c9f4fSDavid Howells * If there's a buffer, we place up to buflen bytes of data into it formatted 641973c9f4fSDavid Howells * in the following way: 642973c9f4fSDavid Howells * 6431da177e4SLinus Torvalds * type;uid;gid;perm;description<NUL> 644973c9f4fSDavid Howells * 645973c9f4fSDavid Howells * If successful, we return the amount of description available, irrespective 646973c9f4fSDavid Howells * of how much we may have copied into the buffer. 6471da177e4SLinus Torvalds */ 6481da177e4SLinus Torvalds long keyctl_describe_key(key_serial_t keyid, 6491da177e4SLinus Torvalds char __user *buffer, 6501da177e4SLinus Torvalds size_t buflen) 6511da177e4SLinus Torvalds { 6523e30148cSDavid Howells struct key *key, *instkey; 653664cceb0SDavid Howells key_ref_t key_ref; 654aa9d4437SDavid Howells char *infobuf; 6551da177e4SLinus Torvalds long ret; 656aa9d4437SDavid Howells int desclen, infolen; 6571da177e4SLinus Torvalds 658f5895943SDavid Howells key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW); 659664cceb0SDavid Howells if (IS_ERR(key_ref)) { 6603e30148cSDavid Howells /* viewing a key under construction is permitted if we have the 6613e30148cSDavid Howells * authorisation token handy */ 662664cceb0SDavid Howells if (PTR_ERR(key_ref) == -EACCES) { 6633e30148cSDavid Howells instkey = key_get_instantiation_authkey(keyid); 6643e30148cSDavid Howells if (!IS_ERR(instkey)) { 6653e30148cSDavid Howells key_put(instkey); 6668bbf4976SDavid Howells key_ref = lookup_user_key(keyid, 6675593122eSDavid Howells KEY_LOOKUP_PARTIAL, 6685593122eSDavid Howells 0); 669664cceb0SDavid Howells if (!IS_ERR(key_ref)) 6703e30148cSDavid Howells goto okay; 6713e30148cSDavid Howells } 6723e30148cSDavid Howells } 6733e30148cSDavid Howells 674664cceb0SDavid Howells ret = PTR_ERR(key_ref); 6751da177e4SLinus Torvalds goto error; 6761da177e4SLinus Torvalds } 6771da177e4SLinus Torvalds 6783e30148cSDavid Howells okay: 679664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 680aa9d4437SDavid Howells desclen = strlen(key->description); 681664cceb0SDavid Howells 682aa9d4437SDavid Howells /* calculate how much information we're going to return */ 683aa9d4437SDavid Howells ret = -ENOMEM; 684aa9d4437SDavid Howells infobuf = kasprintf(GFP_KERNEL, 685aa9d4437SDavid Howells "%s;%d;%d;%08x;", 68694fd8405SDavid Howells key->type->name, 6879a56c2dbSEric W. Biederman from_kuid_munged(current_user_ns(), key->uid), 6889a56c2dbSEric W. Biederman from_kgid_munged(current_user_ns(), key->gid), 689aa9d4437SDavid Howells key->perm); 690aa9d4437SDavid Howells if (!infobuf) 691aa9d4437SDavid Howells goto error2; 692aa9d4437SDavid Howells infolen = strlen(infobuf); 693aa9d4437SDavid Howells ret = infolen + desclen + 1; 6941da177e4SLinus Torvalds 6951da177e4SLinus Torvalds /* consider returning the data */ 696aa9d4437SDavid Howells if (buffer && buflen >= ret) { 697aa9d4437SDavid Howells if (copy_to_user(buffer, infobuf, infolen) != 0 || 698aa9d4437SDavid Howells copy_to_user(buffer + infolen, key->description, 699aa9d4437SDavid Howells desclen + 1) != 0) 7001da177e4SLinus Torvalds ret = -EFAULT; 7011da177e4SLinus Torvalds } 7021da177e4SLinus Torvalds 703aa9d4437SDavid Howells kfree(infobuf); 7041da177e4SLinus Torvalds error2: 705664cceb0SDavid Howells key_ref_put(key_ref); 7061da177e4SLinus Torvalds error: 7071da177e4SLinus Torvalds return ret; 708a8b17ed0SDavid Howells } 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds /* 711973c9f4fSDavid Howells * Search the specified keyring and any keyrings it links to for a matching 712973c9f4fSDavid Howells * key. Only keyrings that grant the caller Search permission will be searched 713973c9f4fSDavid Howells * (this includes the starting keyring). Only keys with Search permission can 714973c9f4fSDavid Howells * be found. 715973c9f4fSDavid Howells * 716973c9f4fSDavid Howells * If successful, the found key will be linked to the destination keyring if 717973c9f4fSDavid Howells * supplied and the key has Link permission, and the found key ID will be 718973c9f4fSDavid Howells * returned. 7191da177e4SLinus Torvalds */ 7201da177e4SLinus Torvalds long keyctl_keyring_search(key_serial_t ringid, 7211da177e4SLinus Torvalds const char __user *_type, 7221da177e4SLinus Torvalds const char __user *_description, 7231da177e4SLinus Torvalds key_serial_t destringid) 7241da177e4SLinus Torvalds { 7251da177e4SLinus Torvalds struct key_type *ktype; 726664cceb0SDavid Howells key_ref_t keyring_ref, key_ref, dest_ref; 7271da177e4SLinus Torvalds char type[32], *description; 7280cb409d9SDavi Arnaut long ret; 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds /* pull the type and description into kernel space */ 7310cb409d9SDavi Arnaut ret = key_get_type_from_user(type, _type, sizeof(type)); 7321da177e4SLinus Torvalds if (ret < 0) 7331da177e4SLinus Torvalds goto error; 7341da177e4SLinus Torvalds 735aa9d4437SDavid Howells description = strndup_user(_description, KEY_MAX_DESC_SIZE); 7360cb409d9SDavi Arnaut if (IS_ERR(description)) { 7370cb409d9SDavi Arnaut ret = PTR_ERR(description); 7381da177e4SLinus Torvalds goto error; 7390cb409d9SDavi Arnaut } 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds /* get the keyring at which to begin the search */ 742f5895943SDavid Howells keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH); 743664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 744664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 7451da177e4SLinus Torvalds goto error2; 7461da177e4SLinus Torvalds } 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds /* get the destination keyring if specified */ 749664cceb0SDavid Howells dest_ref = NULL; 7501da177e4SLinus Torvalds if (destringid) { 7515593122eSDavid Howells dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE, 752f5895943SDavid Howells KEY_NEED_WRITE); 753664cceb0SDavid Howells if (IS_ERR(dest_ref)) { 754664cceb0SDavid Howells ret = PTR_ERR(dest_ref); 7551da177e4SLinus Torvalds goto error3; 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds /* find the key type */ 7601da177e4SLinus Torvalds ktype = key_type_lookup(type); 7611da177e4SLinus Torvalds if (IS_ERR(ktype)) { 7621da177e4SLinus Torvalds ret = PTR_ERR(ktype); 7631da177e4SLinus Torvalds goto error4; 7641da177e4SLinus Torvalds } 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds /* do the search */ 767dcf49dbcSDavid Howells key_ref = keyring_search(keyring_ref, ktype, description, true); 768664cceb0SDavid Howells if (IS_ERR(key_ref)) { 769664cceb0SDavid Howells ret = PTR_ERR(key_ref); 7701da177e4SLinus Torvalds 7711da177e4SLinus Torvalds /* treat lack or presence of a negative key the same */ 7721da177e4SLinus Torvalds if (ret == -EAGAIN) 7731da177e4SLinus Torvalds ret = -ENOKEY; 7741da177e4SLinus Torvalds goto error5; 7751da177e4SLinus Torvalds } 7761da177e4SLinus Torvalds 7771da177e4SLinus Torvalds /* link the resulting key to the destination keyring if we can */ 778664cceb0SDavid Howells if (dest_ref) { 779f5895943SDavid Howells ret = key_permission(key_ref, KEY_NEED_LINK); 78029db9190SDavid Howells if (ret < 0) 7811da177e4SLinus Torvalds goto error6; 7821da177e4SLinus Torvalds 783664cceb0SDavid Howells ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref)); 7841da177e4SLinus Torvalds if (ret < 0) 7851da177e4SLinus Torvalds goto error6; 7861da177e4SLinus Torvalds } 7871da177e4SLinus Torvalds 788664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 7891da177e4SLinus Torvalds 7901da177e4SLinus Torvalds error6: 791664cceb0SDavid Howells key_ref_put(key_ref); 7921da177e4SLinus Torvalds error5: 7931da177e4SLinus Torvalds key_type_put(ktype); 7941da177e4SLinus Torvalds error4: 795664cceb0SDavid Howells key_ref_put(dest_ref); 7961da177e4SLinus Torvalds error3: 797664cceb0SDavid Howells key_ref_put(keyring_ref); 7981da177e4SLinus Torvalds error2: 7991da177e4SLinus Torvalds kfree(description); 8001da177e4SLinus Torvalds error: 8011da177e4SLinus Torvalds return ret; 802a8b17ed0SDavid Howells } 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds /* 805973c9f4fSDavid Howells * Read a key's payload. 806973c9f4fSDavid Howells * 807973c9f4fSDavid Howells * The key must either grant the caller Read permission, or it must grant the 808973c9f4fSDavid Howells * caller Search permission when searched for from the process keyrings. 809973c9f4fSDavid Howells * 810973c9f4fSDavid Howells * If successful, we place up to buflen bytes of data into the buffer, if one 811973c9f4fSDavid Howells * is provided, and return the amount of data that is available in the key, 812973c9f4fSDavid Howells * irrespective of how much we copied into the buffer. 8131da177e4SLinus Torvalds */ 8141da177e4SLinus Torvalds long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen) 8151da177e4SLinus Torvalds { 816664cceb0SDavid Howells struct key *key; 817664cceb0SDavid Howells key_ref_t key_ref; 8181da177e4SLinus Torvalds long ret; 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds /* find the key first */ 8215593122eSDavid Howells key_ref = lookup_user_key(keyid, 0, 0); 822664cceb0SDavid Howells if (IS_ERR(key_ref)) { 823664cceb0SDavid Howells ret = -ENOKEY; 824664cceb0SDavid Howells goto error; 825664cceb0SDavid Howells } 826664cceb0SDavid Howells 827664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 828664cceb0SDavid Howells 829363b02daSDavid Howells ret = key_read_state(key); 830363b02daSDavid Howells if (ret < 0) 831363b02daSDavid Howells goto error2; /* Negatively instantiated */ 83237863c43SEric Biggers 8331da177e4SLinus Torvalds /* see if we can read it directly */ 834f5895943SDavid Howells ret = key_permission(key_ref, KEY_NEED_READ); 83529db9190SDavid Howells if (ret == 0) 8361da177e4SLinus Torvalds goto can_read_key; 83729db9190SDavid Howells if (ret != -EACCES) 8387fc0786dSEric Biggers goto error2; 8391da177e4SLinus Torvalds 840664cceb0SDavid Howells /* we can't; see if it's searchable from this process's keyrings 8413e30148cSDavid Howells * - we automatically take account of the fact that it may be 8423e30148cSDavid Howells * dangling off an instantiation key 8433e30148cSDavid Howells */ 844664cceb0SDavid Howells if (!is_key_possessed(key_ref)) { 8451260f801SDavid Howells ret = -EACCES; 8461da177e4SLinus Torvalds goto error2; 8471da177e4SLinus Torvalds } 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds /* the key is probably readable - now try to read it */ 8501da177e4SLinus Torvalds can_read_key: 8511da177e4SLinus Torvalds ret = -EOPNOTSUPP; 8521da177e4SLinus Torvalds if (key->type->read) { 853b4a1b4f5SDavid Howells /* Read the data with the semaphore held (since we might sleep) 854b4a1b4f5SDavid Howells * to protect against the key being updated or revoked. 855b4a1b4f5SDavid Howells */ 8561da177e4SLinus Torvalds down_read(&key->sem); 857b4a1b4f5SDavid Howells ret = key_validate(key); 858b4a1b4f5SDavid Howells if (ret == 0) 8591da177e4SLinus Torvalds ret = key->type->read(key, buffer, buflen); 8601da177e4SLinus Torvalds up_read(&key->sem); 8611da177e4SLinus Torvalds } 8621da177e4SLinus Torvalds 8631da177e4SLinus Torvalds error2: 8641da177e4SLinus Torvalds key_put(key); 8651da177e4SLinus Torvalds error: 8661da177e4SLinus Torvalds return ret; 867a8b17ed0SDavid Howells } 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds /* 870973c9f4fSDavid Howells * Change the ownership of a key 871973c9f4fSDavid Howells * 872973c9f4fSDavid Howells * The key must grant the caller Setattr permission for this to work, though 873973c9f4fSDavid Howells * the key need not be fully instantiated yet. For the UID to be changed, or 874973c9f4fSDavid Howells * for the GID to be changed to a group the caller is not a member of, the 875973c9f4fSDavid Howells * caller must have sysadmin capability. If either uid or gid is -1 then that 876973c9f4fSDavid Howells * attribute is not changed. 877973c9f4fSDavid Howells * 878973c9f4fSDavid Howells * If the UID is to be changed, the new user must have sufficient quota to 879973c9f4fSDavid Howells * accept the key. The quota deduction will be removed from the old user to 880973c9f4fSDavid Howells * the new user should the attribute be changed. 881973c9f4fSDavid Howells * 882973c9f4fSDavid Howells * If successful, 0 will be returned. 8831da177e4SLinus Torvalds */ 8849a56c2dbSEric W. Biederman long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group) 8851da177e4SLinus Torvalds { 8865801649dSFredrik Tolf struct key_user *newowner, *zapowner = NULL; 8871da177e4SLinus Torvalds struct key *key; 888664cceb0SDavid Howells key_ref_t key_ref; 8891da177e4SLinus Torvalds long ret; 8909a56c2dbSEric W. Biederman kuid_t uid; 8919a56c2dbSEric W. Biederman kgid_t gid; 8929a56c2dbSEric W. Biederman 8939a56c2dbSEric W. Biederman uid = make_kuid(current_user_ns(), user); 8949a56c2dbSEric W. Biederman gid = make_kgid(current_user_ns(), group); 8959a56c2dbSEric W. Biederman ret = -EINVAL; 8969a56c2dbSEric W. Biederman if ((user != (uid_t) -1) && !uid_valid(uid)) 8979a56c2dbSEric W. Biederman goto error; 8989a56c2dbSEric W. Biederman if ((group != (gid_t) -1) && !gid_valid(gid)) 8999a56c2dbSEric W. Biederman goto error; 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds ret = 0; 9029a56c2dbSEric W. Biederman if (user == (uid_t) -1 && group == (gid_t) -1) 9031da177e4SLinus Torvalds goto error; 9041da177e4SLinus Torvalds 9055593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 906f5895943SDavid Howells KEY_NEED_SETATTR); 907664cceb0SDavid Howells if (IS_ERR(key_ref)) { 908664cceb0SDavid Howells ret = PTR_ERR(key_ref); 9091da177e4SLinus Torvalds goto error; 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds 912664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 913664cceb0SDavid Howells 9141da177e4SLinus Torvalds /* make the changes with the locks held to prevent chown/chown races */ 9151da177e4SLinus Torvalds ret = -EACCES; 9161da177e4SLinus Torvalds down_write(&key->sem); 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) { 9191da177e4SLinus Torvalds /* only the sysadmin can chown a key to some other UID */ 9209a56c2dbSEric W. Biederman if (user != (uid_t) -1 && !uid_eq(key->uid, uid)) 9215801649dSFredrik Tolf goto error_put; 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds /* only the sysadmin can set the key's GID to a group other 9241da177e4SLinus Torvalds * than one of those that the current process subscribes to */ 9259a56c2dbSEric W. Biederman if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid)) 9265801649dSFredrik Tolf goto error_put; 9271da177e4SLinus Torvalds } 9281da177e4SLinus Torvalds 9295801649dSFredrik Tolf /* change the UID */ 9309a56c2dbSEric W. Biederman if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) { 9315801649dSFredrik Tolf ret = -ENOMEM; 9329a56c2dbSEric W. Biederman newowner = key_user_lookup(uid); 9335801649dSFredrik Tolf if (!newowner) 9345801649dSFredrik Tolf goto error_put; 9355801649dSFredrik Tolf 9365801649dSFredrik Tolf /* transfer the quota burden to the new user */ 9375801649dSFredrik Tolf if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { 9389a56c2dbSEric W. Biederman unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ? 9390b77f5bfSDavid Howells key_quota_root_maxkeys : key_quota_maxkeys; 9409a56c2dbSEric W. Biederman unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ? 9410b77f5bfSDavid Howells key_quota_root_maxbytes : key_quota_maxbytes; 9420b77f5bfSDavid Howells 9435801649dSFredrik Tolf spin_lock(&newowner->lock); 9440b77f5bfSDavid Howells if (newowner->qnkeys + 1 >= maxkeys || 9450b77f5bfSDavid Howells newowner->qnbytes + key->quotalen >= maxbytes || 9460b77f5bfSDavid Howells newowner->qnbytes + key->quotalen < 9470b77f5bfSDavid Howells newowner->qnbytes) 9485801649dSFredrik Tolf goto quota_overrun; 9495801649dSFredrik Tolf 9505801649dSFredrik Tolf newowner->qnkeys++; 9515801649dSFredrik Tolf newowner->qnbytes += key->quotalen; 9525801649dSFredrik Tolf spin_unlock(&newowner->lock); 9535801649dSFredrik Tolf 9545801649dSFredrik Tolf spin_lock(&key->user->lock); 9555801649dSFredrik Tolf key->user->qnkeys--; 9565801649dSFredrik Tolf key->user->qnbytes -= key->quotalen; 9575801649dSFredrik Tolf spin_unlock(&key->user->lock); 9585801649dSFredrik Tolf } 9595801649dSFredrik Tolf 9605801649dSFredrik Tolf atomic_dec(&key->user->nkeys); 9615801649dSFredrik Tolf atomic_inc(&newowner->nkeys); 9625801649dSFredrik Tolf 963363b02daSDavid Howells if (key->state != KEY_IS_UNINSTANTIATED) { 9645801649dSFredrik Tolf atomic_dec(&key->user->nikeys); 9655801649dSFredrik Tolf atomic_inc(&newowner->nikeys); 9665801649dSFredrik Tolf } 9675801649dSFredrik Tolf 9685801649dSFredrik Tolf zapowner = key->user; 9695801649dSFredrik Tolf key->user = newowner; 9705801649dSFredrik Tolf key->uid = uid; 9711da177e4SLinus Torvalds } 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds /* change the GID */ 9749a56c2dbSEric W. Biederman if (group != (gid_t) -1) 9751da177e4SLinus Torvalds key->gid = gid; 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds ret = 0; 9781da177e4SLinus Torvalds 9795801649dSFredrik Tolf error_put: 9801da177e4SLinus Torvalds up_write(&key->sem); 9811da177e4SLinus Torvalds key_put(key); 9825801649dSFredrik Tolf if (zapowner) 9835801649dSFredrik Tolf key_user_put(zapowner); 9841da177e4SLinus Torvalds error: 9851da177e4SLinus Torvalds return ret; 9861da177e4SLinus Torvalds 9875801649dSFredrik Tolf quota_overrun: 9885801649dSFredrik Tolf spin_unlock(&newowner->lock); 9895801649dSFredrik Tolf zapowner = newowner; 9905801649dSFredrik Tolf ret = -EDQUOT; 9915801649dSFredrik Tolf goto error_put; 992a8b17ed0SDavid Howells } 9935801649dSFredrik Tolf 9941da177e4SLinus Torvalds /* 995973c9f4fSDavid Howells * Change the permission mask on a key. 996973c9f4fSDavid Howells * 997973c9f4fSDavid Howells * The key must grant the caller Setattr permission for this to work, though 998973c9f4fSDavid Howells * the key need not be fully instantiated yet. If the caller does not have 999973c9f4fSDavid Howells * sysadmin capability, it may only change the permission on keys that it owns. 10001da177e4SLinus Torvalds */ 10011da177e4SLinus Torvalds long keyctl_setperm_key(key_serial_t id, key_perm_t perm) 10021da177e4SLinus Torvalds { 10031da177e4SLinus Torvalds struct key *key; 1004664cceb0SDavid Howells key_ref_t key_ref; 10051da177e4SLinus Torvalds long ret; 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds ret = -EINVAL; 1008664cceb0SDavid Howells if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL)) 10091da177e4SLinus Torvalds goto error; 10101da177e4SLinus Torvalds 10115593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 1012f5895943SDavid Howells KEY_NEED_SETATTR); 1013664cceb0SDavid Howells if (IS_ERR(key_ref)) { 1014664cceb0SDavid Howells ret = PTR_ERR(key_ref); 10151da177e4SLinus Torvalds goto error; 10161da177e4SLinus Torvalds } 10171da177e4SLinus Torvalds 1018664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 1019664cceb0SDavid Howells 102076d8aeabSDavid Howells /* make the changes with the locks held to prevent chown/chmod races */ 10211da177e4SLinus Torvalds ret = -EACCES; 10221da177e4SLinus Torvalds down_write(&key->sem); 10231da177e4SLinus Torvalds 102476d8aeabSDavid Howells /* if we're not the sysadmin, we can only change a key that we own */ 10259a56c2dbSEric W. Biederman if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) { 10261da177e4SLinus Torvalds key->perm = perm; 10271da177e4SLinus Torvalds ret = 0; 102876d8aeabSDavid Howells } 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds up_write(&key->sem); 10311da177e4SLinus Torvalds key_put(key); 10321da177e4SLinus Torvalds error: 10331da177e4SLinus Torvalds return ret; 1034a8b17ed0SDavid Howells } 10351da177e4SLinus Torvalds 10368bbf4976SDavid Howells /* 1037973c9f4fSDavid Howells * Get the destination keyring for instantiation and check that the caller has 1038973c9f4fSDavid Howells * Write permission on it. 10398bbf4976SDavid Howells */ 10408bbf4976SDavid Howells static long get_instantiation_keyring(key_serial_t ringid, 10418bbf4976SDavid Howells struct request_key_auth *rka, 10428bbf4976SDavid Howells struct key **_dest_keyring) 10438bbf4976SDavid Howells { 10448bbf4976SDavid Howells key_ref_t dkref; 10458bbf4976SDavid Howells 10468bbf4976SDavid Howells *_dest_keyring = NULL; 1047eca1bf5bSDavid Howells 1048eca1bf5bSDavid Howells /* just return a NULL pointer if we weren't asked to make a link */ 1049eca1bf5bSDavid Howells if (ringid == 0) 10508bbf4976SDavid Howells return 0; 10518bbf4976SDavid Howells 10528bbf4976SDavid Howells /* if a specific keyring is nominated by ID, then use that */ 10538bbf4976SDavid Howells if (ringid > 0) { 1054f5895943SDavid Howells dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); 10558bbf4976SDavid Howells if (IS_ERR(dkref)) 10568bbf4976SDavid Howells return PTR_ERR(dkref); 10578bbf4976SDavid Howells *_dest_keyring = key_ref_to_ptr(dkref); 10588bbf4976SDavid Howells return 0; 10598bbf4976SDavid Howells } 10608bbf4976SDavid Howells 10618bbf4976SDavid Howells if (ringid == KEY_SPEC_REQKEY_AUTH_KEY) 10628bbf4976SDavid Howells return -EINVAL; 10638bbf4976SDavid Howells 10648bbf4976SDavid Howells /* otherwise specify the destination keyring recorded in the 10658bbf4976SDavid Howells * authorisation key (any KEY_SPEC_*_KEYRING) */ 10668bbf4976SDavid Howells if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) { 106721279cfaSDavid Howells *_dest_keyring = key_get(rka->dest_keyring); 10688bbf4976SDavid Howells return 0; 10698bbf4976SDavid Howells } 10708bbf4976SDavid Howells 10718bbf4976SDavid Howells return -ENOKEY; 10728bbf4976SDavid Howells } 10738bbf4976SDavid Howells 1074d84f4f99SDavid Howells /* 1075973c9f4fSDavid Howells * Change the request_key authorisation key on the current process. 1076d84f4f99SDavid Howells */ 1077d84f4f99SDavid Howells static int keyctl_change_reqkey_auth(struct key *key) 1078d84f4f99SDavid Howells { 1079d84f4f99SDavid Howells struct cred *new; 1080d84f4f99SDavid Howells 1081d84f4f99SDavid Howells new = prepare_creds(); 1082d84f4f99SDavid Howells if (!new) 1083d84f4f99SDavid Howells return -ENOMEM; 1084d84f4f99SDavid Howells 1085d84f4f99SDavid Howells key_put(new->request_key_auth); 1086d84f4f99SDavid Howells new->request_key_auth = key_get(key); 1087d84f4f99SDavid Howells 1088d84f4f99SDavid Howells return commit_creds(new); 1089d84f4f99SDavid Howells } 1090d84f4f99SDavid Howells 10911da177e4SLinus Torvalds /* 1092973c9f4fSDavid Howells * Instantiate a key with the specified payload and link the key into the 1093973c9f4fSDavid Howells * destination keyring if one is given. 1094973c9f4fSDavid Howells * 1095973c9f4fSDavid Howells * The caller must have the appropriate instantiation permit set for this to 1096973c9f4fSDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1097973c9f4fSDavid Howells * 1098973c9f4fSDavid Howells * If successful, 0 will be returned. 10991da177e4SLinus Torvalds */ 1100ee009e4aSDavid Howells long keyctl_instantiate_key_common(key_serial_t id, 1101b353a1f7SAl Viro struct iov_iter *from, 11021da177e4SLinus Torvalds key_serial_t ringid) 11031da177e4SLinus Torvalds { 1104d84f4f99SDavid Howells const struct cred *cred = current_cred(); 11053e30148cSDavid Howells struct request_key_auth *rka; 11068bbf4976SDavid Howells struct key *instkey, *dest_keyring; 1107b353a1f7SAl Viro size_t plen = from ? iov_iter_count(from) : 0; 11081da177e4SLinus Torvalds void *payload; 11091da177e4SLinus Torvalds long ret; 11101da177e4SLinus Torvalds 1111d84f4f99SDavid Howells kenter("%d,,%zu,%d", id, plen, ringid); 1112d84f4f99SDavid Howells 1113b353a1f7SAl Viro if (!plen) 1114b353a1f7SAl Viro from = NULL; 1115b353a1f7SAl Viro 11161da177e4SLinus Torvalds ret = -EINVAL; 111738bbca6bSDavid Howells if (plen > 1024 * 1024 - 1) 11181da177e4SLinus Torvalds goto error; 11191da177e4SLinus Torvalds 1120b5f545c8SDavid Howells /* the appropriate instantiation authorisation key must have been 1121b5f545c8SDavid Howells * assumed before calling this */ 1122b5f545c8SDavid Howells ret = -EPERM; 1123d84f4f99SDavid Howells instkey = cred->request_key_auth; 1124b5f545c8SDavid Howells if (!instkey) 1125b5f545c8SDavid Howells goto error; 1126b5f545c8SDavid Howells 1127146aa8b1SDavid Howells rka = instkey->payload.data[0]; 1128b5f545c8SDavid Howells if (rka->target_key->serial != id) 1129b5f545c8SDavid Howells goto error; 1130b5f545c8SDavid Howells 11311da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 11321da177e4SLinus Torvalds payload = NULL; 11331da177e4SLinus Torvalds 1134b353a1f7SAl Viro if (from) { 11351da177e4SLinus Torvalds ret = -ENOMEM; 1136752ade68SMichal Hocko payload = kvmalloc(plen, GFP_KERNEL); 11371da177e4SLinus Torvalds if (!payload) 11381da177e4SLinus Torvalds goto error; 11391da177e4SLinus Torvalds 1140b353a1f7SAl Viro ret = -EFAULT; 1141cbbd26b8SAl Viro if (!copy_from_iter_full(payload, plen, from)) 11421da177e4SLinus Torvalds goto error2; 11431da177e4SLinus Torvalds } 11441da177e4SLinus Torvalds 11453e30148cSDavid Howells /* find the destination keyring amongst those belonging to the 11463e30148cSDavid Howells * requesting task */ 11478bbf4976SDavid Howells ret = get_instantiation_keyring(ringid, rka, &dest_keyring); 11488bbf4976SDavid Howells if (ret < 0) 1149b5f545c8SDavid Howells goto error2; 11501da177e4SLinus Torvalds 11511da177e4SLinus Torvalds /* instantiate the key and link it into a keyring */ 11523e30148cSDavid Howells ret = key_instantiate_and_link(rka->target_key, payload, plen, 11538bbf4976SDavid Howells dest_keyring, instkey); 11541da177e4SLinus Torvalds 11558bbf4976SDavid Howells key_put(dest_keyring); 1156b5f545c8SDavid Howells 1157b5f545c8SDavid Howells /* discard the assumed authority if it's just been disabled by 1158b5f545c8SDavid Howells * instantiation of the key */ 1159d84f4f99SDavid Howells if (ret == 0) 1160d84f4f99SDavid Howells keyctl_change_reqkey_auth(NULL); 1161b5f545c8SDavid Howells 11621da177e4SLinus Torvalds error2: 116357070c85SEric Biggers if (payload) { 116457070c85SEric Biggers memzero_explicit(payload, plen); 1165b353a1f7SAl Viro kvfree(payload); 116657070c85SEric Biggers } 11671da177e4SLinus Torvalds error: 11681da177e4SLinus Torvalds return ret; 1169a8b17ed0SDavid Howells } 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds /* 1172ee009e4aSDavid Howells * Instantiate a key with the specified payload and link the key into the 1173ee009e4aSDavid Howells * destination keyring if one is given. 1174ee009e4aSDavid Howells * 1175ee009e4aSDavid Howells * The caller must have the appropriate instantiation permit set for this to 1176ee009e4aSDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1177ee009e4aSDavid Howells * 1178ee009e4aSDavid Howells * If successful, 0 will be returned. 1179ee009e4aSDavid Howells */ 1180ee009e4aSDavid Howells long keyctl_instantiate_key(key_serial_t id, 1181ee009e4aSDavid Howells const void __user *_payload, 1182ee009e4aSDavid Howells size_t plen, 1183ee009e4aSDavid Howells key_serial_t ringid) 1184ee009e4aSDavid Howells { 1185ee009e4aSDavid Howells if (_payload && plen) { 1186b353a1f7SAl Viro struct iovec iov; 1187b353a1f7SAl Viro struct iov_iter from; 1188b353a1f7SAl Viro int ret; 1189ee009e4aSDavid Howells 1190b353a1f7SAl Viro ret = import_single_range(WRITE, (void __user *)_payload, plen, 1191b353a1f7SAl Viro &iov, &from); 1192b353a1f7SAl Viro if (unlikely(ret)) 1193b353a1f7SAl Viro return ret; 1194b353a1f7SAl Viro 1195b353a1f7SAl Viro return keyctl_instantiate_key_common(id, &from, ringid); 1196ee009e4aSDavid Howells } 1197ee009e4aSDavid Howells 1198b353a1f7SAl Viro return keyctl_instantiate_key_common(id, NULL, ringid); 1199ee009e4aSDavid Howells } 1200ee009e4aSDavid Howells 1201ee009e4aSDavid Howells /* 1202ee009e4aSDavid Howells * Instantiate a key with the specified multipart payload and link the key into 1203ee009e4aSDavid Howells * the destination keyring if one is given. 1204ee009e4aSDavid Howells * 1205ee009e4aSDavid Howells * The caller must have the appropriate instantiation permit set for this to 1206ee009e4aSDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1207ee009e4aSDavid Howells * 1208ee009e4aSDavid Howells * If successful, 0 will be returned. 1209ee009e4aSDavid Howells */ 1210ee009e4aSDavid Howells long keyctl_instantiate_key_iov(key_serial_t id, 1211ee009e4aSDavid Howells const struct iovec __user *_payload_iov, 1212ee009e4aSDavid Howells unsigned ioc, 1213ee009e4aSDavid Howells key_serial_t ringid) 1214ee009e4aSDavid Howells { 1215ee009e4aSDavid Howells struct iovec iovstack[UIO_FASTIOV], *iov = iovstack; 1216b353a1f7SAl Viro struct iov_iter from; 1217ee009e4aSDavid Howells long ret; 1218ee009e4aSDavid Howells 1219b353a1f7SAl Viro if (!_payload_iov) 1220b353a1f7SAl Viro ioc = 0; 1221ee009e4aSDavid Howells 1222b353a1f7SAl Viro ret = import_iovec(WRITE, _payload_iov, ioc, 1223b353a1f7SAl Viro ARRAY_SIZE(iovstack), &iov, &from); 1224ee009e4aSDavid Howells if (ret < 0) 1225b353a1f7SAl Viro return ret; 1226b353a1f7SAl Viro ret = keyctl_instantiate_key_common(id, &from, ringid); 1227ee009e4aSDavid Howells kfree(iov); 1228ee009e4aSDavid Howells return ret; 1229ee009e4aSDavid Howells } 1230ee009e4aSDavid Howells 1231ee009e4aSDavid Howells /* 1232973c9f4fSDavid Howells * Negatively instantiate the key with the given timeout (in seconds) and link 1233973c9f4fSDavid Howells * the key into the destination keyring if one is given. 1234973c9f4fSDavid Howells * 1235973c9f4fSDavid Howells * The caller must have the appropriate instantiation permit set for this to 1236973c9f4fSDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1237973c9f4fSDavid Howells * 1238973c9f4fSDavid Howells * The key and any links to the key will be automatically garbage collected 1239973c9f4fSDavid Howells * after the timeout expires. 1240973c9f4fSDavid Howells * 1241973c9f4fSDavid Howells * Negative keys are used to rate limit repeated request_key() calls by causing 1242973c9f4fSDavid Howells * them to return -ENOKEY until the negative key expires. 1243973c9f4fSDavid Howells * 1244973c9f4fSDavid Howells * If successful, 0 will be returned. 12451da177e4SLinus Torvalds */ 12461da177e4SLinus Torvalds long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid) 12471da177e4SLinus Torvalds { 1248fdd1b945SDavid Howells return keyctl_reject_key(id, timeout, ENOKEY, ringid); 1249fdd1b945SDavid Howells } 1250fdd1b945SDavid Howells 1251fdd1b945SDavid Howells /* 1252fdd1b945SDavid Howells * Negatively instantiate the key with the given timeout (in seconds) and error 1253fdd1b945SDavid Howells * code and link the key into the destination keyring if one is given. 1254fdd1b945SDavid Howells * 1255fdd1b945SDavid Howells * The caller must have the appropriate instantiation permit set for this to 1256fdd1b945SDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1257fdd1b945SDavid Howells * 1258fdd1b945SDavid Howells * The key and any links to the key will be automatically garbage collected 1259fdd1b945SDavid Howells * after the timeout expires. 1260fdd1b945SDavid Howells * 1261fdd1b945SDavid Howells * Negative keys are used to rate limit repeated request_key() calls by causing 1262fdd1b945SDavid Howells * them to return the specified error code until the negative key expires. 1263fdd1b945SDavid Howells * 1264fdd1b945SDavid Howells * If successful, 0 will be returned. 1265fdd1b945SDavid Howells */ 1266fdd1b945SDavid Howells long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error, 1267fdd1b945SDavid Howells key_serial_t ringid) 1268fdd1b945SDavid Howells { 1269d84f4f99SDavid Howells const struct cred *cred = current_cred(); 12703e30148cSDavid Howells struct request_key_auth *rka; 12718bbf4976SDavid Howells struct key *instkey, *dest_keyring; 12721da177e4SLinus Torvalds long ret; 12731da177e4SLinus Torvalds 1274fdd1b945SDavid Howells kenter("%d,%u,%u,%d", id, timeout, error, ringid); 1275fdd1b945SDavid Howells 1276fdd1b945SDavid Howells /* must be a valid error code and mustn't be a kernel special */ 1277fdd1b945SDavid Howells if (error <= 0 || 1278fdd1b945SDavid Howells error >= MAX_ERRNO || 1279fdd1b945SDavid Howells error == ERESTARTSYS || 1280fdd1b945SDavid Howells error == ERESTARTNOINTR || 1281fdd1b945SDavid Howells error == ERESTARTNOHAND || 1282fdd1b945SDavid Howells error == ERESTART_RESTARTBLOCK) 1283fdd1b945SDavid Howells return -EINVAL; 1284d84f4f99SDavid Howells 1285b5f545c8SDavid Howells /* the appropriate instantiation authorisation key must have been 1286b5f545c8SDavid Howells * assumed before calling this */ 1287b5f545c8SDavid Howells ret = -EPERM; 1288d84f4f99SDavid Howells instkey = cred->request_key_auth; 1289b5f545c8SDavid Howells if (!instkey) 12901da177e4SLinus Torvalds goto error; 12911da177e4SLinus Torvalds 1292146aa8b1SDavid Howells rka = instkey->payload.data[0]; 1293b5f545c8SDavid Howells if (rka->target_key->serial != id) 1294b5f545c8SDavid Howells goto error; 12953e30148cSDavid Howells 12961da177e4SLinus Torvalds /* find the destination keyring if present (which must also be 12971da177e4SLinus Torvalds * writable) */ 12988bbf4976SDavid Howells ret = get_instantiation_keyring(ringid, rka, &dest_keyring); 12998bbf4976SDavid Howells if (ret < 0) 1300b5f545c8SDavid Howells goto error; 13011da177e4SLinus Torvalds 13021da177e4SLinus Torvalds /* instantiate the key and link it into a keyring */ 1303fdd1b945SDavid Howells ret = key_reject_and_link(rka->target_key, timeout, error, 13048bbf4976SDavid Howells dest_keyring, instkey); 13051da177e4SLinus Torvalds 13068bbf4976SDavid Howells key_put(dest_keyring); 1307b5f545c8SDavid Howells 1308b5f545c8SDavid Howells /* discard the assumed authority if it's just been disabled by 1309b5f545c8SDavid Howells * instantiation of the key */ 1310d84f4f99SDavid Howells if (ret == 0) 1311d84f4f99SDavid Howells keyctl_change_reqkey_auth(NULL); 1312b5f545c8SDavid Howells 13131da177e4SLinus Torvalds error: 13141da177e4SLinus Torvalds return ret; 1315a8b17ed0SDavid Howells } 13161da177e4SLinus Torvalds 13171da177e4SLinus Torvalds /* 1318973c9f4fSDavid Howells * Read or set the default keyring in which request_key() will cache keys and 1319973c9f4fSDavid Howells * return the old setting. 1320973c9f4fSDavid Howells * 1321c9f838d1SEric Biggers * If a thread or process keyring is specified then it will be created if it 1322c9f838d1SEric Biggers * doesn't yet exist. The old setting will be returned if successful. 13233e30148cSDavid Howells */ 13243e30148cSDavid Howells long keyctl_set_reqkey_keyring(int reqkey_defl) 13253e30148cSDavid Howells { 1326d84f4f99SDavid Howells struct cred *new; 1327d84f4f99SDavid Howells int ret, old_setting; 1328d84f4f99SDavid Howells 1329d84f4f99SDavid Howells old_setting = current_cred_xxx(jit_keyring); 1330d84f4f99SDavid Howells 1331d84f4f99SDavid Howells if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE) 1332d84f4f99SDavid Howells return old_setting; 1333d84f4f99SDavid Howells 1334d84f4f99SDavid Howells new = prepare_creds(); 1335d84f4f99SDavid Howells if (!new) 1336d84f4f99SDavid Howells return -ENOMEM; 13373e30148cSDavid Howells 13383e30148cSDavid Howells switch (reqkey_defl) { 13393e30148cSDavid Howells case KEY_REQKEY_DEFL_THREAD_KEYRING: 1340d84f4f99SDavid Howells ret = install_thread_keyring_to_cred(new); 13413e30148cSDavid Howells if (ret < 0) 1342d84f4f99SDavid Howells goto error; 13433e30148cSDavid Howells goto set; 13443e30148cSDavid Howells 13453e30148cSDavid Howells case KEY_REQKEY_DEFL_PROCESS_KEYRING: 1346d84f4f99SDavid Howells ret = install_process_keyring_to_cred(new); 1347c9f838d1SEric Biggers if (ret < 0) 1348d84f4f99SDavid Howells goto error; 1349d84f4f99SDavid Howells goto set; 13503e30148cSDavid Howells 13513e30148cSDavid Howells case KEY_REQKEY_DEFL_DEFAULT: 13523e30148cSDavid Howells case KEY_REQKEY_DEFL_SESSION_KEYRING: 13533e30148cSDavid Howells case KEY_REQKEY_DEFL_USER_KEYRING: 13543e30148cSDavid Howells case KEY_REQKEY_DEFL_USER_SESSION_KEYRING: 1355d84f4f99SDavid Howells case KEY_REQKEY_DEFL_REQUESTOR_KEYRING: 1356d84f4f99SDavid Howells goto set; 13573e30148cSDavid Howells 13583e30148cSDavid Howells case KEY_REQKEY_DEFL_NO_CHANGE: 13593e30148cSDavid Howells case KEY_REQKEY_DEFL_GROUP_KEYRING: 13603e30148cSDavid Howells default: 1361d84f4f99SDavid Howells ret = -EINVAL; 1362d84f4f99SDavid Howells goto error; 13633e30148cSDavid Howells } 13643e30148cSDavid Howells 1365d84f4f99SDavid Howells set: 1366d84f4f99SDavid Howells new->jit_keyring = reqkey_defl; 1367d84f4f99SDavid Howells commit_creds(new); 1368d84f4f99SDavid Howells return old_setting; 1369d84f4f99SDavid Howells error: 1370d84f4f99SDavid Howells abort_creds(new); 13714303ef19SDan Carpenter return ret; 1372a8b17ed0SDavid Howells } 1373d84f4f99SDavid Howells 13743e30148cSDavid Howells /* 1375973c9f4fSDavid Howells * Set or clear the timeout on a key. 1376973c9f4fSDavid Howells * 1377973c9f4fSDavid Howells * Either the key must grant the caller Setattr permission or else the caller 1378973c9f4fSDavid Howells * must hold an instantiation authorisation token for the key. 1379973c9f4fSDavid Howells * 1380973c9f4fSDavid Howells * The timeout is either 0 to clear the timeout, or a number of seconds from 1381973c9f4fSDavid Howells * the current time. The key and any links to the key will be automatically 1382973c9f4fSDavid Howells * garbage collected after the timeout expires. 1383973c9f4fSDavid Howells * 1384d3600bcfSMimi Zohar * Keys with KEY_FLAG_KEEP set should not be timed out. 1385d3600bcfSMimi Zohar * 1386973c9f4fSDavid Howells * If successful, 0 is returned. 1387017679c4SDavid Howells */ 1388017679c4SDavid Howells long keyctl_set_timeout(key_serial_t id, unsigned timeout) 1389017679c4SDavid Howells { 13909156235bSDavid Howells struct key *key, *instkey; 1391017679c4SDavid Howells key_ref_t key_ref; 1392017679c4SDavid Howells long ret; 1393017679c4SDavid Howells 13945593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 1395f5895943SDavid Howells KEY_NEED_SETATTR); 1396017679c4SDavid Howells if (IS_ERR(key_ref)) { 13979156235bSDavid Howells /* setting the timeout on a key under construction is permitted 13989156235bSDavid Howells * if we have the authorisation token handy */ 13999156235bSDavid Howells if (PTR_ERR(key_ref) == -EACCES) { 14009156235bSDavid Howells instkey = key_get_instantiation_authkey(id); 14019156235bSDavid Howells if (!IS_ERR(instkey)) { 14029156235bSDavid Howells key_put(instkey); 14039156235bSDavid Howells key_ref = lookup_user_key(id, 14049156235bSDavid Howells KEY_LOOKUP_PARTIAL, 14059156235bSDavid Howells 0); 14069156235bSDavid Howells if (!IS_ERR(key_ref)) 14079156235bSDavid Howells goto okay; 14089156235bSDavid Howells } 14099156235bSDavid Howells } 14109156235bSDavid Howells 1411017679c4SDavid Howells ret = PTR_ERR(key_ref); 1412017679c4SDavid Howells goto error; 1413017679c4SDavid Howells } 1414017679c4SDavid Howells 14159156235bSDavid Howells okay: 1416017679c4SDavid Howells key = key_ref_to_ptr(key_ref); 14171d6d167cSMimi Zohar ret = 0; 1418d3600bcfSMimi Zohar if (test_bit(KEY_FLAG_KEEP, &key->flags)) 1419d3600bcfSMimi Zohar ret = -EPERM; 14201d6d167cSMimi Zohar else 142159e6b9c1SBryan Schumaker key_set_timeout(key, timeout); 1422017679c4SDavid Howells key_put(key); 1423017679c4SDavid Howells 1424017679c4SDavid Howells error: 1425017679c4SDavid Howells return ret; 1426a8b17ed0SDavid Howells } 1427017679c4SDavid Howells 1428017679c4SDavid Howells /* 1429973c9f4fSDavid Howells * Assume (or clear) the authority to instantiate the specified key. 1430973c9f4fSDavid Howells * 1431973c9f4fSDavid Howells * This sets the authoritative token currently in force for key instantiation. 1432973c9f4fSDavid Howells * This must be done for a key to be instantiated. It has the effect of making 1433973c9f4fSDavid Howells * available all the keys from the caller of the request_key() that created a 1434973c9f4fSDavid Howells * key to request_key() calls made by the caller of this function. 1435973c9f4fSDavid Howells * 1436973c9f4fSDavid Howells * The caller must have the instantiation key in their process keyrings with a 1437973c9f4fSDavid Howells * Search permission grant available to the caller. 1438973c9f4fSDavid Howells * 1439973c9f4fSDavid Howells * If the ID given is 0, then the setting will be cleared and 0 returned. 1440973c9f4fSDavid Howells * 1441973c9f4fSDavid Howells * If the ID given has a matching an authorisation key, then that key will be 1442973c9f4fSDavid Howells * set and its ID will be returned. The authorisation key can be read to get 1443973c9f4fSDavid Howells * the callout information passed to request_key(). 1444b5f545c8SDavid Howells */ 1445b5f545c8SDavid Howells long keyctl_assume_authority(key_serial_t id) 1446b5f545c8SDavid Howells { 1447b5f545c8SDavid Howells struct key *authkey; 1448b5f545c8SDavid Howells long ret; 1449b5f545c8SDavid Howells 1450b5f545c8SDavid Howells /* special key IDs aren't permitted */ 1451b5f545c8SDavid Howells ret = -EINVAL; 1452b5f545c8SDavid Howells if (id < 0) 1453b5f545c8SDavid Howells goto error; 1454b5f545c8SDavid Howells 1455b5f545c8SDavid Howells /* we divest ourselves of authority if given an ID of 0 */ 1456b5f545c8SDavid Howells if (id == 0) { 1457d84f4f99SDavid Howells ret = keyctl_change_reqkey_auth(NULL); 1458b5f545c8SDavid Howells goto error; 1459b5f545c8SDavid Howells } 1460b5f545c8SDavid Howells 1461b5f545c8SDavid Howells /* attempt to assume the authority temporarily granted to us whilst we 1462b5f545c8SDavid Howells * instantiate the specified key 1463b5f545c8SDavid Howells * - the authorisation key must be in the current task's keyrings 1464b5f545c8SDavid Howells * somewhere 1465b5f545c8SDavid Howells */ 1466b5f545c8SDavid Howells authkey = key_get_instantiation_authkey(id); 1467b5f545c8SDavid Howells if (IS_ERR(authkey)) { 1468b5f545c8SDavid Howells ret = PTR_ERR(authkey); 1469b5f545c8SDavid Howells goto error; 1470b5f545c8SDavid Howells } 1471b5f545c8SDavid Howells 1472d84f4f99SDavid Howells ret = keyctl_change_reqkey_auth(authkey); 1473884bee02SEric Biggers if (ret == 0) 1474d84f4f99SDavid Howells ret = authkey->serial; 1475884bee02SEric Biggers key_put(authkey); 1476b5f545c8SDavid Howells error: 1477b5f545c8SDavid Howells return ret; 1478a8b17ed0SDavid Howells } 1479b5f545c8SDavid Howells 148070a5bb72SDavid Howells /* 1481973c9f4fSDavid Howells * Get a key's the LSM security label. 1482973c9f4fSDavid Howells * 1483973c9f4fSDavid Howells * The key must grant the caller View permission for this to work. 1484973c9f4fSDavid Howells * 1485973c9f4fSDavid Howells * If there's a buffer, then up to buflen bytes of data will be placed into it. 1486973c9f4fSDavid Howells * 1487973c9f4fSDavid Howells * If successful, the amount of information available will be returned, 1488973c9f4fSDavid Howells * irrespective of how much was copied (including the terminal NUL). 148970a5bb72SDavid Howells */ 149070a5bb72SDavid Howells long keyctl_get_security(key_serial_t keyid, 149170a5bb72SDavid Howells char __user *buffer, 149270a5bb72SDavid Howells size_t buflen) 149370a5bb72SDavid Howells { 149470a5bb72SDavid Howells struct key *key, *instkey; 149570a5bb72SDavid Howells key_ref_t key_ref; 149670a5bb72SDavid Howells char *context; 149770a5bb72SDavid Howells long ret; 149870a5bb72SDavid Howells 1499f5895943SDavid Howells key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW); 150070a5bb72SDavid Howells if (IS_ERR(key_ref)) { 150170a5bb72SDavid Howells if (PTR_ERR(key_ref) != -EACCES) 150270a5bb72SDavid Howells return PTR_ERR(key_ref); 150370a5bb72SDavid Howells 150470a5bb72SDavid Howells /* viewing a key under construction is also permitted if we 150570a5bb72SDavid Howells * have the authorisation token handy */ 150670a5bb72SDavid Howells instkey = key_get_instantiation_authkey(keyid); 150770a5bb72SDavid Howells if (IS_ERR(instkey)) 1508fa1cc7b5SRoel Kluin return PTR_ERR(instkey); 150970a5bb72SDavid Howells key_put(instkey); 151070a5bb72SDavid Howells 15115593122eSDavid Howells key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0); 151270a5bb72SDavid Howells if (IS_ERR(key_ref)) 151370a5bb72SDavid Howells return PTR_ERR(key_ref); 151470a5bb72SDavid Howells } 151570a5bb72SDavid Howells 151670a5bb72SDavid Howells key = key_ref_to_ptr(key_ref); 151770a5bb72SDavid Howells ret = security_key_getsecurity(key, &context); 151870a5bb72SDavid Howells if (ret == 0) { 151970a5bb72SDavid Howells /* if no information was returned, give userspace an empty 152070a5bb72SDavid Howells * string */ 152170a5bb72SDavid Howells ret = 1; 152270a5bb72SDavid Howells if (buffer && buflen > 0 && 152370a5bb72SDavid Howells copy_to_user(buffer, "", 1) != 0) 152470a5bb72SDavid Howells ret = -EFAULT; 152570a5bb72SDavid Howells } else if (ret > 0) { 152670a5bb72SDavid Howells /* return as much data as there's room for */ 152770a5bb72SDavid Howells if (buffer && buflen > 0) { 152870a5bb72SDavid Howells if (buflen > ret) 152970a5bb72SDavid Howells buflen = ret; 153070a5bb72SDavid Howells 153170a5bb72SDavid Howells if (copy_to_user(buffer, context, buflen) != 0) 153270a5bb72SDavid Howells ret = -EFAULT; 153370a5bb72SDavid Howells } 153470a5bb72SDavid Howells 153570a5bb72SDavid Howells kfree(context); 153670a5bb72SDavid Howells } 153770a5bb72SDavid Howells 153870a5bb72SDavid Howells key_ref_put(key_ref); 153970a5bb72SDavid Howells return ret; 154070a5bb72SDavid Howells } 154170a5bb72SDavid Howells 1542ee18d64cSDavid Howells /* 1543973c9f4fSDavid Howells * Attempt to install the calling process's session keyring on the process's 1544973c9f4fSDavid Howells * parent process. 1545973c9f4fSDavid Howells * 1546973c9f4fSDavid Howells * The keyring must exist and must grant the caller LINK permission, and the 1547973c9f4fSDavid Howells * parent process must be single-threaded and must have the same effective 1548973c9f4fSDavid Howells * ownership as this process and mustn't be SUID/SGID. 1549973c9f4fSDavid Howells * 1550973c9f4fSDavid Howells * The keyring will be emplaced on the parent when it next resumes userspace. 1551973c9f4fSDavid Howells * 1552973c9f4fSDavid Howells * If successful, 0 will be returned. 1553ee18d64cSDavid Howells */ 1554ee18d64cSDavid Howells long keyctl_session_to_parent(void) 1555ee18d64cSDavid Howells { 1556ee18d64cSDavid Howells struct task_struct *me, *parent; 1557ee18d64cSDavid Howells const struct cred *mycred, *pcred; 155867d12145SAl Viro struct callback_head *newwork, *oldwork; 1559ee18d64cSDavid Howells key_ref_t keyring_r; 1560413cd3d9SOleg Nesterov struct cred *cred; 1561ee18d64cSDavid Howells int ret; 1562ee18d64cSDavid Howells 1563f5895943SDavid Howells keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK); 1564ee18d64cSDavid Howells if (IS_ERR(keyring_r)) 1565ee18d64cSDavid Howells return PTR_ERR(keyring_r); 1566ee18d64cSDavid Howells 1567413cd3d9SOleg Nesterov ret = -ENOMEM; 1568413cd3d9SOleg Nesterov 1569ee18d64cSDavid Howells /* our parent is going to need a new cred struct, a new tgcred struct 1570ee18d64cSDavid Howells * and new security data, so we allocate them here to prevent ENOMEM in 1571ee18d64cSDavid Howells * our parent */ 1572ee18d64cSDavid Howells cred = cred_alloc_blank(); 1573ee18d64cSDavid Howells if (!cred) 157467d12145SAl Viro goto error_keyring; 157567d12145SAl Viro newwork = &cred->rcu; 1576ee18d64cSDavid Howells 15773a50597dSDavid Howells cred->session_keyring = key_ref_to_ptr(keyring_r); 15783a50597dSDavid Howells keyring_r = NULL; 157967d12145SAl Viro init_task_work(newwork, key_change_session_keyring); 1580ee18d64cSDavid Howells 1581ee18d64cSDavid Howells me = current; 15829d1ac65aSDavid Howells rcu_read_lock(); 1583ee18d64cSDavid Howells write_lock_irq(&tasklist_lock); 1584ee18d64cSDavid Howells 1585ee18d64cSDavid Howells ret = -EPERM; 1586413cd3d9SOleg Nesterov oldwork = NULL; 15877936d16dSDavid Howells parent = rcu_dereference_protected(me->real_parent, 15887936d16dSDavid Howells lockdep_is_held(&tasklist_lock)); 1589ee18d64cSDavid Howells 1590ee18d64cSDavid Howells /* the parent mustn't be init and mustn't be a kernel thread */ 1591ee18d64cSDavid Howells if (parent->pid <= 1 || !parent->mm) 1592413cd3d9SOleg Nesterov goto unlock; 1593ee18d64cSDavid Howells 1594ee18d64cSDavid Howells /* the parent must be single threaded */ 1595dd98acf7SOleg Nesterov if (!thread_group_empty(parent)) 1596413cd3d9SOleg Nesterov goto unlock; 1597ee18d64cSDavid Howells 1598ee18d64cSDavid Howells /* the parent and the child must have different session keyrings or 1599ee18d64cSDavid Howells * there's no point */ 1600ee18d64cSDavid Howells mycred = current_cred(); 1601ee18d64cSDavid Howells pcred = __task_cred(parent); 1602ee18d64cSDavid Howells if (mycred == pcred || 16033a50597dSDavid Howells mycred->session_keyring == pcred->session_keyring) { 1604413cd3d9SOleg Nesterov ret = 0; 1605413cd3d9SOleg Nesterov goto unlock; 1606413cd3d9SOleg Nesterov } 1607ee18d64cSDavid Howells 1608ee18d64cSDavid Howells /* the parent must have the same effective ownership and mustn't be 1609ee18d64cSDavid Howells * SUID/SGID */ 16109a56c2dbSEric W. Biederman if (!uid_eq(pcred->uid, mycred->euid) || 16119a56c2dbSEric W. Biederman !uid_eq(pcred->euid, mycred->euid) || 16129a56c2dbSEric W. Biederman !uid_eq(pcred->suid, mycred->euid) || 16139a56c2dbSEric W. Biederman !gid_eq(pcred->gid, mycred->egid) || 16149a56c2dbSEric W. Biederman !gid_eq(pcred->egid, mycred->egid) || 16159a56c2dbSEric W. Biederman !gid_eq(pcred->sgid, mycred->egid)) 1616413cd3d9SOleg Nesterov goto unlock; 1617ee18d64cSDavid Howells 1618ee18d64cSDavid Howells /* the keyrings must have the same UID */ 16193a50597dSDavid Howells if ((pcred->session_keyring && 16202a74dbb9SLinus Torvalds !uid_eq(pcred->session_keyring->uid, mycred->euid)) || 16212a74dbb9SLinus Torvalds !uid_eq(mycred->session_keyring->uid, mycred->euid)) 1622413cd3d9SOleg Nesterov goto unlock; 1623ee18d64cSDavid Howells 1624413cd3d9SOleg Nesterov /* cancel an already pending keyring replacement */ 1625413cd3d9SOleg Nesterov oldwork = task_work_cancel(parent, key_change_session_keyring); 1626ee18d64cSDavid Howells 1627ee18d64cSDavid Howells /* the replacement session keyring is applied just prior to userspace 1628ee18d64cSDavid Howells * restarting */ 162967d12145SAl Viro ret = task_work_add(parent, newwork, true); 1630413cd3d9SOleg Nesterov if (!ret) 1631413cd3d9SOleg Nesterov newwork = NULL; 1632413cd3d9SOleg Nesterov unlock: 1633ee18d64cSDavid Howells write_unlock_irq(&tasklist_lock); 16349d1ac65aSDavid Howells rcu_read_unlock(); 163567d12145SAl Viro if (oldwork) 163667d12145SAl Viro put_cred(container_of(oldwork, struct cred, rcu)); 163767d12145SAl Viro if (newwork) 163867d12145SAl Viro put_cred(cred); 1639ee18d64cSDavid Howells return ret; 1640ee18d64cSDavid Howells 1641ee18d64cSDavid Howells error_keyring: 1642ee18d64cSDavid Howells key_ref_put(keyring_r); 1643ee18d64cSDavid Howells return ret; 1644ee18d64cSDavid Howells } 1645ee18d64cSDavid Howells 1646b5f545c8SDavid Howells /* 16476563c91fSMat Martineau * Apply a restriction to a given keyring. 16486563c91fSMat Martineau * 16496563c91fSMat Martineau * The caller must have Setattr permission to change keyring restrictions. 16506563c91fSMat Martineau * 16516563c91fSMat Martineau * The requested type name may be a NULL pointer to reject all attempts 165218026d86SEric Biggers * to link to the keyring. In this case, _restriction must also be NULL. 165318026d86SEric Biggers * Otherwise, both _type and _restriction must be non-NULL. 16546563c91fSMat Martineau * 16556563c91fSMat Martineau * Returns 0 if successful. 16566563c91fSMat Martineau */ 16576563c91fSMat Martineau long keyctl_restrict_keyring(key_serial_t id, const char __user *_type, 16586563c91fSMat Martineau const char __user *_restriction) 16596563c91fSMat Martineau { 16606563c91fSMat Martineau key_ref_t key_ref; 16616563c91fSMat Martineau char type[32]; 16626563c91fSMat Martineau char *restriction = NULL; 16636563c91fSMat Martineau long ret; 16646563c91fSMat Martineau 16656563c91fSMat Martineau key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR); 16666563c91fSMat Martineau if (IS_ERR(key_ref)) 16676563c91fSMat Martineau return PTR_ERR(key_ref); 16686563c91fSMat Martineau 166918026d86SEric Biggers ret = -EINVAL; 16706563c91fSMat Martineau if (_type) { 167118026d86SEric Biggers if (!_restriction) 167218026d86SEric Biggers goto error; 167318026d86SEric Biggers 16746563c91fSMat Martineau ret = key_get_type_from_user(type, _type, sizeof(type)); 16756563c91fSMat Martineau if (ret < 0) 16766563c91fSMat Martineau goto error; 16776563c91fSMat Martineau 16786563c91fSMat Martineau restriction = strndup_user(_restriction, PAGE_SIZE); 16796563c91fSMat Martineau if (IS_ERR(restriction)) { 16806563c91fSMat Martineau ret = PTR_ERR(restriction); 16816563c91fSMat Martineau goto error; 16826563c91fSMat Martineau } 168318026d86SEric Biggers } else { 168418026d86SEric Biggers if (_restriction) 168518026d86SEric Biggers goto error; 16866563c91fSMat Martineau } 16876563c91fSMat Martineau 168818026d86SEric Biggers ret = keyring_restrict(key_ref, _type ? type : NULL, restriction); 16896563c91fSMat Martineau kfree(restriction); 16906563c91fSMat Martineau error: 16916563c91fSMat Martineau key_ref_put(key_ref); 16926563c91fSMat Martineau return ret; 16936563c91fSMat Martineau } 16946563c91fSMat Martineau 16956563c91fSMat Martineau /* 169645e0f30cSDavid Howells * Get keyrings subsystem capabilities. 169745e0f30cSDavid Howells */ 169845e0f30cSDavid Howells long keyctl_capabilities(unsigned char __user *_buffer, size_t buflen) 169945e0f30cSDavid Howells { 170045e0f30cSDavid Howells size_t size = buflen; 170145e0f30cSDavid Howells 170245e0f30cSDavid Howells if (size > 0) { 170345e0f30cSDavid Howells if (size > sizeof(keyrings_capabilities)) 170445e0f30cSDavid Howells size = sizeof(keyrings_capabilities); 170545e0f30cSDavid Howells if (copy_to_user(_buffer, keyrings_capabilities, size) != 0) 170645e0f30cSDavid Howells return -EFAULT; 170745e0f30cSDavid Howells if (size < buflen && 170845e0f30cSDavid Howells clear_user(_buffer + size, buflen - size) != 0) 170945e0f30cSDavid Howells return -EFAULT; 171045e0f30cSDavid Howells } 171145e0f30cSDavid Howells 171245e0f30cSDavid Howells return sizeof(keyrings_capabilities); 171345e0f30cSDavid Howells } 171445e0f30cSDavid Howells 171545e0f30cSDavid Howells /* 1716973c9f4fSDavid Howells * The key control system call 17171da177e4SLinus Torvalds */ 1718938bb9f5SHeiko Carstens SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3, 1719938bb9f5SHeiko Carstens unsigned long, arg4, unsigned long, arg5) 17201da177e4SLinus Torvalds { 17211da177e4SLinus Torvalds switch (option) { 17221da177e4SLinus Torvalds case KEYCTL_GET_KEYRING_ID: 17231da177e4SLinus Torvalds return keyctl_get_keyring_ID((key_serial_t) arg2, 17241da177e4SLinus Torvalds (int) arg3); 17251da177e4SLinus Torvalds 17261da177e4SLinus Torvalds case KEYCTL_JOIN_SESSION_KEYRING: 17271da177e4SLinus Torvalds return keyctl_join_session_keyring((const char __user *) arg2); 17281da177e4SLinus Torvalds 17291da177e4SLinus Torvalds case KEYCTL_UPDATE: 17301da177e4SLinus Torvalds return keyctl_update_key((key_serial_t) arg2, 17311da177e4SLinus Torvalds (const void __user *) arg3, 17321da177e4SLinus Torvalds (size_t) arg4); 17331da177e4SLinus Torvalds 17341da177e4SLinus Torvalds case KEYCTL_REVOKE: 17351da177e4SLinus Torvalds return keyctl_revoke_key((key_serial_t) arg2); 17361da177e4SLinus Torvalds 17371da177e4SLinus Torvalds case KEYCTL_DESCRIBE: 17381da177e4SLinus Torvalds return keyctl_describe_key((key_serial_t) arg2, 17391da177e4SLinus Torvalds (char __user *) arg3, 17401da177e4SLinus Torvalds (unsigned) arg4); 17411da177e4SLinus Torvalds 17421da177e4SLinus Torvalds case KEYCTL_CLEAR: 17431da177e4SLinus Torvalds return keyctl_keyring_clear((key_serial_t) arg2); 17441da177e4SLinus Torvalds 17451da177e4SLinus Torvalds case KEYCTL_LINK: 17461da177e4SLinus Torvalds return keyctl_keyring_link((key_serial_t) arg2, 17471da177e4SLinus Torvalds (key_serial_t) arg3); 17481da177e4SLinus Torvalds 17491da177e4SLinus Torvalds case KEYCTL_UNLINK: 17501da177e4SLinus Torvalds return keyctl_keyring_unlink((key_serial_t) arg2, 17511da177e4SLinus Torvalds (key_serial_t) arg3); 17521da177e4SLinus Torvalds 17531da177e4SLinus Torvalds case KEYCTL_SEARCH: 17541da177e4SLinus Torvalds return keyctl_keyring_search((key_serial_t) arg2, 17551da177e4SLinus Torvalds (const char __user *) arg3, 17561da177e4SLinus Torvalds (const char __user *) arg4, 17571da177e4SLinus Torvalds (key_serial_t) arg5); 17581da177e4SLinus Torvalds 17591da177e4SLinus Torvalds case KEYCTL_READ: 17601da177e4SLinus Torvalds return keyctl_read_key((key_serial_t) arg2, 17611da177e4SLinus Torvalds (char __user *) arg3, 17621da177e4SLinus Torvalds (size_t) arg4); 17631da177e4SLinus Torvalds 17641da177e4SLinus Torvalds case KEYCTL_CHOWN: 17651da177e4SLinus Torvalds return keyctl_chown_key((key_serial_t) arg2, 17661da177e4SLinus Torvalds (uid_t) arg3, 17671da177e4SLinus Torvalds (gid_t) arg4); 17681da177e4SLinus Torvalds 17691da177e4SLinus Torvalds case KEYCTL_SETPERM: 17701da177e4SLinus Torvalds return keyctl_setperm_key((key_serial_t) arg2, 17711da177e4SLinus Torvalds (key_perm_t) arg3); 17721da177e4SLinus Torvalds 17731da177e4SLinus Torvalds case KEYCTL_INSTANTIATE: 17741da177e4SLinus Torvalds return keyctl_instantiate_key((key_serial_t) arg2, 17751da177e4SLinus Torvalds (const void __user *) arg3, 17761da177e4SLinus Torvalds (size_t) arg4, 17771da177e4SLinus Torvalds (key_serial_t) arg5); 17781da177e4SLinus Torvalds 17791da177e4SLinus Torvalds case KEYCTL_NEGATE: 17801da177e4SLinus Torvalds return keyctl_negate_key((key_serial_t) arg2, 17811da177e4SLinus Torvalds (unsigned) arg3, 17821da177e4SLinus Torvalds (key_serial_t) arg4); 17831da177e4SLinus Torvalds 17843e30148cSDavid Howells case KEYCTL_SET_REQKEY_KEYRING: 17853e30148cSDavid Howells return keyctl_set_reqkey_keyring(arg2); 17863e30148cSDavid Howells 1787017679c4SDavid Howells case KEYCTL_SET_TIMEOUT: 1788017679c4SDavid Howells return keyctl_set_timeout((key_serial_t) arg2, 1789017679c4SDavid Howells (unsigned) arg3); 1790017679c4SDavid Howells 1791b5f545c8SDavid Howells case KEYCTL_ASSUME_AUTHORITY: 1792b5f545c8SDavid Howells return keyctl_assume_authority((key_serial_t) arg2); 1793b5f545c8SDavid Howells 179470a5bb72SDavid Howells case KEYCTL_GET_SECURITY: 179570a5bb72SDavid Howells return keyctl_get_security((key_serial_t) arg2, 179690bd49abSJames Morris (char __user *) arg3, 179770a5bb72SDavid Howells (size_t) arg4); 179870a5bb72SDavid Howells 1799ee18d64cSDavid Howells case KEYCTL_SESSION_TO_PARENT: 1800ee18d64cSDavid Howells return keyctl_session_to_parent(); 1801ee18d64cSDavid Howells 1802fdd1b945SDavid Howells case KEYCTL_REJECT: 1803fdd1b945SDavid Howells return keyctl_reject_key((key_serial_t) arg2, 1804fdd1b945SDavid Howells (unsigned) arg3, 1805fdd1b945SDavid Howells (unsigned) arg4, 1806fdd1b945SDavid Howells (key_serial_t) arg5); 1807fdd1b945SDavid Howells 1808ee009e4aSDavid Howells case KEYCTL_INSTANTIATE_IOV: 1809ee009e4aSDavid Howells return keyctl_instantiate_key_iov( 1810ee009e4aSDavid Howells (key_serial_t) arg2, 1811ee009e4aSDavid Howells (const struct iovec __user *) arg3, 1812ee009e4aSDavid Howells (unsigned) arg4, 1813ee009e4aSDavid Howells (key_serial_t) arg5); 1814ee009e4aSDavid Howells 1815fd75815fSDavid Howells case KEYCTL_INVALIDATE: 1816fd75815fSDavid Howells return keyctl_invalidate_key((key_serial_t) arg2); 1817fd75815fSDavid Howells 1818f36f8c75SDavid Howells case KEYCTL_GET_PERSISTENT: 1819f36f8c75SDavid Howells return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3); 1820f36f8c75SDavid Howells 1821ddbb4114SMat Martineau case KEYCTL_DH_COMPUTE: 1822ddbb4114SMat Martineau return keyctl_dh_compute((struct keyctl_dh_params __user *) arg2, 18234693fc73SStephan Mueller (char __user *) arg3, (size_t) arg4, 1824f1c316a3SStephan Mueller (struct keyctl_kdf_params __user *) arg5); 1825ddbb4114SMat Martineau 18266563c91fSMat Martineau case KEYCTL_RESTRICT_KEYRING: 18276563c91fSMat Martineau return keyctl_restrict_keyring((key_serial_t) arg2, 18286563c91fSMat Martineau (const char __user *) arg3, 18296563c91fSMat Martineau (const char __user *) arg4); 18301da177e4SLinus Torvalds 183100d60fd3SDavid Howells case KEYCTL_PKEY_QUERY: 183200d60fd3SDavid Howells if (arg3 != 0) 183300d60fd3SDavid Howells return -EINVAL; 183400d60fd3SDavid Howells return keyctl_pkey_query((key_serial_t)arg2, 183500d60fd3SDavid Howells (const char __user *)arg4, 1836468e91ceSBen Dooks (struct keyctl_pkey_query __user *)arg5); 183700d60fd3SDavid Howells 183800d60fd3SDavid Howells case KEYCTL_PKEY_ENCRYPT: 183900d60fd3SDavid Howells case KEYCTL_PKEY_DECRYPT: 184000d60fd3SDavid Howells case KEYCTL_PKEY_SIGN: 184100d60fd3SDavid Howells return keyctl_pkey_e_d_s( 184200d60fd3SDavid Howells option, 184300d60fd3SDavid Howells (const struct keyctl_pkey_params __user *)arg2, 184400d60fd3SDavid Howells (const char __user *)arg3, 184500d60fd3SDavid Howells (const void __user *)arg4, 184600d60fd3SDavid Howells (void __user *)arg5); 184700d60fd3SDavid Howells 184800d60fd3SDavid Howells case KEYCTL_PKEY_VERIFY: 184900d60fd3SDavid Howells return keyctl_pkey_verify( 185000d60fd3SDavid Howells (const struct keyctl_pkey_params __user *)arg2, 185100d60fd3SDavid Howells (const char __user *)arg3, 185200d60fd3SDavid Howells (const void __user *)arg4, 185300d60fd3SDavid Howells (const void __user *)arg5); 185400d60fd3SDavid Howells 1855ed0ac5c7SDavid Howells case KEYCTL_MOVE: 1856ed0ac5c7SDavid Howells return keyctl_keyring_move((key_serial_t)arg2, 1857ed0ac5c7SDavid Howells (key_serial_t)arg3, 1858ed0ac5c7SDavid Howells (key_serial_t)arg4, 1859ed0ac5c7SDavid Howells (unsigned int)arg5); 1860ed0ac5c7SDavid Howells 186145e0f30cSDavid Howells case KEYCTL_CAPABILITIES: 186245e0f30cSDavid Howells return keyctl_capabilities((unsigned char __user *)arg2, (size_t)arg3); 186345e0f30cSDavid Howells 18641da177e4SLinus Torvalds default: 18651da177e4SLinus Torvalds return -EOPNOTSUPP; 18661da177e4SLinus Torvalds } 18671da177e4SLinus Torvalds } 1868