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 330cb409d9SDavi Arnaut static int key_get_type_from_user(char *type, 340cb409d9SDavi Arnaut const char __user *_type, 350cb409d9SDavi Arnaut unsigned len) 360cb409d9SDavi Arnaut { 370cb409d9SDavi Arnaut int ret; 380cb409d9SDavi Arnaut 390cb409d9SDavi Arnaut ret = strncpy_from_user(type, _type, len); 400cb409d9SDavi Arnaut if (ret < 0) 414303ef19SDan Carpenter return ret; 420cb409d9SDavi Arnaut if (ret == 0 || ret >= len) 430cb409d9SDavi Arnaut return -EINVAL; 4454e2c2c1SDavid Howells if (type[0] == '.') 4554e2c2c1SDavid Howells return -EPERM; 460cb409d9SDavi Arnaut type[len - 1] = '\0'; 470cb409d9SDavi Arnaut return 0; 480cb409d9SDavi Arnaut } 490cb409d9SDavi Arnaut 501da177e4SLinus Torvalds /* 51973c9f4fSDavid Howells * Extract the description of a new key from userspace and either add it as a 52973c9f4fSDavid Howells * new key to the specified keyring or update a matching key in that keyring. 53973c9f4fSDavid Howells * 54cf7f601cSDavid Howells * If the description is NULL or an empty string, the key type is asked to 55cf7f601cSDavid Howells * generate one from the payload. 56cf7f601cSDavid Howells * 57973c9f4fSDavid Howells * The keyring must be writable so that we can attach the key to it. 58973c9f4fSDavid Howells * 59973c9f4fSDavid Howells * If successful, the new key's serial number is returned, otherwise an error 60973c9f4fSDavid Howells * code is returned. 611da177e4SLinus Torvalds */ 621e7bfb21SHeiko Carstens SYSCALL_DEFINE5(add_key, const char __user *, _type, 631e7bfb21SHeiko Carstens const char __user *, _description, 641e7bfb21SHeiko Carstens const void __user *, _payload, 651e7bfb21SHeiko Carstens size_t, plen, 661e7bfb21SHeiko Carstens key_serial_t, ringid) 671da177e4SLinus Torvalds { 68664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 691da177e4SLinus Torvalds char type[32], *description; 701da177e4SLinus Torvalds void *payload; 710cb409d9SDavi Arnaut long ret; 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds ret = -EINVAL; 7438bbca6bSDavid Howells if (plen > 1024 * 1024 - 1) 751da177e4SLinus Torvalds goto error; 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds /* draw all the data into kernel space */ 780cb409d9SDavi Arnaut ret = key_get_type_from_user(type, _type, sizeof(type)); 791da177e4SLinus Torvalds if (ret < 0) 801da177e4SLinus Torvalds goto error; 811da177e4SLinus Torvalds 82cf7f601cSDavid Howells description = NULL; 83cf7f601cSDavid Howells if (_description) { 84aa9d4437SDavid Howells description = strndup_user(_description, KEY_MAX_DESC_SIZE); 850cb409d9SDavi Arnaut if (IS_ERR(description)) { 860cb409d9SDavi Arnaut ret = PTR_ERR(description); 873e30148cSDavid Howells goto error; 880cb409d9SDavi Arnaut } 89cf7f601cSDavid Howells if (!*description) { 90cf7f601cSDavid Howells kfree(description); 91cf7f601cSDavid Howells description = NULL; 92a4e3b8d7SMimi Zohar } else if ((description[0] == '.') && 93a4e3b8d7SMimi Zohar (strncmp(type, "keyring", 7) == 0)) { 94a4e3b8d7SMimi Zohar ret = -EPERM; 95a4e3b8d7SMimi Zohar goto error2; 96cf7f601cSDavid Howells } 97cf7f601cSDavid Howells } 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 1001da177e4SLinus Torvalds payload = NULL; 1011da177e4SLinus Torvalds 1025649645dSEric Biggers if (plen) { 1031da177e4SLinus Torvalds ret = -ENOMEM; 104752ade68SMichal Hocko payload = kvmalloc(plen, GFP_KERNEL); 1051da177e4SLinus Torvalds if (!payload) 1061da177e4SLinus Torvalds goto error2; 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds ret = -EFAULT; 1091da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 1101da177e4SLinus Torvalds goto error3; 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds /* find the target keyring (which must be writable) */ 114f5895943SDavid Howells keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); 115664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 116664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 1171da177e4SLinus Torvalds goto error3; 1181da177e4SLinus Torvalds } 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds /* create or update the requested key and add it to the target 1211da177e4SLinus Torvalds * keyring */ 122664cceb0SDavid Howells key_ref = key_create_or_update(keyring_ref, type, description, 1236b79ccb5SArun Raghavan payload, plen, KEY_PERM_UNDEF, 1246b79ccb5SArun Raghavan KEY_ALLOC_IN_QUOTA); 125664cceb0SDavid Howells if (!IS_ERR(key_ref)) { 126664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 127664cceb0SDavid Howells key_ref_put(key_ref); 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds else { 130664cceb0SDavid Howells ret = PTR_ERR(key_ref); 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds 133664cceb0SDavid Howells key_ref_put(keyring_ref); 1341da177e4SLinus Torvalds error3: 13557070c85SEric Biggers if (payload) { 13657070c85SEric Biggers memzero_explicit(payload, plen); 137d0e0eba0SGeliang Tang kvfree(payload); 13857070c85SEric Biggers } 1391da177e4SLinus Torvalds error2: 1401da177e4SLinus Torvalds kfree(description); 1411da177e4SLinus Torvalds error: 1421da177e4SLinus Torvalds return ret; 143a8b17ed0SDavid Howells } 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds /* 146973c9f4fSDavid Howells * Search the process keyrings and keyring trees linked from those for a 147973c9f4fSDavid Howells * matching key. Keyrings must have appropriate Search permission to be 148973c9f4fSDavid Howells * searched. 149973c9f4fSDavid Howells * 150973c9f4fSDavid Howells * If a key is found, it will be attached to the destination keyring if there's 151973c9f4fSDavid Howells * one specified and the serial number of the key will be returned. 152973c9f4fSDavid Howells * 153973c9f4fSDavid Howells * If no key is found, /sbin/request-key will be invoked if _callout_info is 154973c9f4fSDavid Howells * non-NULL in an attempt to create a key. The _callout_info string will be 155973c9f4fSDavid Howells * passed to /sbin/request-key to aid with completing the request. If the 156973c9f4fSDavid Howells * _callout_info string is "" then it will be changed to "-". 1571da177e4SLinus Torvalds */ 1581e7bfb21SHeiko Carstens SYSCALL_DEFINE4(request_key, const char __user *, _type, 1591e7bfb21SHeiko Carstens const char __user *, _description, 1601e7bfb21SHeiko Carstens const char __user *, _callout_info, 1611e7bfb21SHeiko Carstens key_serial_t, destringid) 1621da177e4SLinus Torvalds { 1631da177e4SLinus Torvalds struct key_type *ktype; 164664cceb0SDavid Howells struct key *key; 165664cceb0SDavid Howells key_ref_t dest_ref; 1664a38e122SDavid Howells size_t callout_len; 1671da177e4SLinus Torvalds char type[32], *description, *callout_info; 1680cb409d9SDavi Arnaut long ret; 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds /* pull the type into kernel space */ 1710cb409d9SDavi Arnaut ret = key_get_type_from_user(type, _type, sizeof(type)); 1721da177e4SLinus Torvalds if (ret < 0) 1731da177e4SLinus Torvalds goto error; 1741260f801SDavid Howells 1751da177e4SLinus Torvalds /* pull the description into kernel space */ 176aa9d4437SDavid Howells description = strndup_user(_description, KEY_MAX_DESC_SIZE); 1770cb409d9SDavi Arnaut if (IS_ERR(description)) { 1780cb409d9SDavi Arnaut ret = PTR_ERR(description); 1791da177e4SLinus Torvalds goto error; 1800cb409d9SDavi Arnaut } 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds /* pull the callout info into kernel space */ 1831da177e4SLinus Torvalds callout_info = NULL; 1844a38e122SDavid Howells callout_len = 0; 1851da177e4SLinus Torvalds if (_callout_info) { 1860cb409d9SDavi Arnaut callout_info = strndup_user(_callout_info, PAGE_SIZE); 1870cb409d9SDavi Arnaut if (IS_ERR(callout_info)) { 1880cb409d9SDavi Arnaut ret = PTR_ERR(callout_info); 1891da177e4SLinus Torvalds goto error2; 1900cb409d9SDavi Arnaut } 1914a38e122SDavid Howells callout_len = strlen(callout_info); 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds /* get the destination keyring if specified */ 195664cceb0SDavid Howells dest_ref = NULL; 1961da177e4SLinus Torvalds if (destringid) { 1975593122eSDavid Howells dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE, 198f5895943SDavid Howells KEY_NEED_WRITE); 199664cceb0SDavid Howells if (IS_ERR(dest_ref)) { 200664cceb0SDavid Howells ret = PTR_ERR(dest_ref); 2011da177e4SLinus Torvalds goto error3; 2021da177e4SLinus Torvalds } 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds /* find the key type */ 2061da177e4SLinus Torvalds ktype = key_type_lookup(type); 2071da177e4SLinus Torvalds if (IS_ERR(ktype)) { 2081da177e4SLinus Torvalds ret = PTR_ERR(ktype); 2091da177e4SLinus Torvalds goto error4; 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds /* do the search */ 2134a38e122SDavid Howells key = request_key_and_link(ktype, description, callout_info, 2144a38e122SDavid Howells callout_len, NULL, key_ref_to_ptr(dest_ref), 2157e047ef5SDavid Howells KEY_ALLOC_IN_QUOTA); 2161da177e4SLinus Torvalds if (IS_ERR(key)) { 2171da177e4SLinus Torvalds ret = PTR_ERR(key); 2181da177e4SLinus Torvalds goto error5; 2191da177e4SLinus Torvalds } 2201da177e4SLinus Torvalds 2214aab1e89SDavid Howells /* wait for the key to finish being constructed */ 2224aab1e89SDavid Howells ret = wait_for_key_construction(key, 1); 2234aab1e89SDavid Howells if (ret < 0) 2244aab1e89SDavid Howells goto error6; 2254aab1e89SDavid Howells 2261da177e4SLinus Torvalds ret = key->serial; 2271da177e4SLinus Torvalds 2284aab1e89SDavid Howells error6: 2291da177e4SLinus Torvalds key_put(key); 2301da177e4SLinus Torvalds error5: 2311da177e4SLinus Torvalds key_type_put(ktype); 2321da177e4SLinus Torvalds error4: 233664cceb0SDavid Howells key_ref_put(dest_ref); 2341da177e4SLinus Torvalds error3: 2351da177e4SLinus Torvalds kfree(callout_info); 2361da177e4SLinus Torvalds error2: 2371da177e4SLinus Torvalds kfree(description); 2381da177e4SLinus Torvalds error: 2391da177e4SLinus Torvalds return ret; 240a8b17ed0SDavid Howells } 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds /* 243973c9f4fSDavid Howells * Get the ID of the specified process keyring. 244973c9f4fSDavid Howells * 245973c9f4fSDavid Howells * The requested keyring must have search permission to be found. 246973c9f4fSDavid Howells * 247973c9f4fSDavid Howells * If successful, the ID of the requested keyring will be returned. 2481da177e4SLinus Torvalds */ 2491da177e4SLinus Torvalds long keyctl_get_keyring_ID(key_serial_t id, int create) 2501da177e4SLinus Torvalds { 251664cceb0SDavid Howells key_ref_t key_ref; 2525593122eSDavid Howells unsigned long lflags; 2531da177e4SLinus Torvalds long ret; 2541da177e4SLinus Torvalds 2555593122eSDavid Howells lflags = create ? KEY_LOOKUP_CREATE : 0; 256f5895943SDavid Howells key_ref = lookup_user_key(id, lflags, KEY_NEED_SEARCH); 257664cceb0SDavid Howells if (IS_ERR(key_ref)) { 258664cceb0SDavid Howells ret = PTR_ERR(key_ref); 2591da177e4SLinus Torvalds goto error; 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds 262664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 263664cceb0SDavid Howells key_ref_put(key_ref); 2641da177e4SLinus Torvalds error: 2651da177e4SLinus Torvalds return ret; 266973c9f4fSDavid Howells } 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds /* 269973c9f4fSDavid Howells * Join a (named) session keyring. 270973c9f4fSDavid Howells * 271973c9f4fSDavid Howells * Create and join an anonymous session keyring or join a named session 272973c9f4fSDavid Howells * keyring, creating it if necessary. A named session keyring must have Search 273973c9f4fSDavid Howells * permission for it to be joined. Session keyrings without this permit will 274ee8f844eSDavid Howells * be skipped over. It is not permitted for userspace to create or join 275ee8f844eSDavid Howells * keyrings whose name begin with a dot. 276973c9f4fSDavid Howells * 277973c9f4fSDavid Howells * If successful, the ID of the joined session keyring will be returned. 2781da177e4SLinus Torvalds */ 2791da177e4SLinus Torvalds long keyctl_join_session_keyring(const char __user *_name) 2801da177e4SLinus Torvalds { 2811da177e4SLinus Torvalds char *name; 2820cb409d9SDavi Arnaut long ret; 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds /* fetch the name from userspace */ 2851da177e4SLinus Torvalds name = NULL; 2861da177e4SLinus Torvalds if (_name) { 287aa9d4437SDavid Howells name = strndup_user(_name, KEY_MAX_DESC_SIZE); 2880cb409d9SDavi Arnaut if (IS_ERR(name)) { 2890cb409d9SDavi Arnaut ret = PTR_ERR(name); 2901da177e4SLinus Torvalds goto error; 2910cb409d9SDavi Arnaut } 292ee8f844eSDavid Howells 293ee8f844eSDavid Howells ret = -EPERM; 294ee8f844eSDavid Howells if (name[0] == '.') 295ee8f844eSDavid Howells goto error_name; 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds /* join the session */ 2991da177e4SLinus Torvalds ret = join_session_keyring(name); 300ee8f844eSDavid Howells error_name: 3010d54ee1cSVegard Nossum kfree(name); 3021da177e4SLinus Torvalds error: 3031da177e4SLinus Torvalds return ret; 304a8b17ed0SDavid Howells } 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds /* 307973c9f4fSDavid Howells * Update a key's data payload from the given data. 308973c9f4fSDavid Howells * 309973c9f4fSDavid Howells * The key must grant the caller Write permission and the key type must support 310973c9f4fSDavid Howells * updating for this to work. A negative key can be positively instantiated 311973c9f4fSDavid Howells * with this call. 312973c9f4fSDavid Howells * 313973c9f4fSDavid Howells * If successful, 0 will be returned. If the key type does not support 314973c9f4fSDavid Howells * updating, then -EOPNOTSUPP will be returned. 3151da177e4SLinus Torvalds */ 3161da177e4SLinus Torvalds long keyctl_update_key(key_serial_t id, 3171da177e4SLinus Torvalds const void __user *_payload, 3181da177e4SLinus Torvalds size_t plen) 3191da177e4SLinus Torvalds { 320664cceb0SDavid Howells key_ref_t key_ref; 3211da177e4SLinus Torvalds void *payload; 3221da177e4SLinus Torvalds long ret; 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds ret = -EINVAL; 3251da177e4SLinus Torvalds if (plen > PAGE_SIZE) 3261da177e4SLinus Torvalds goto error; 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 3291da177e4SLinus Torvalds payload = NULL; 3305649645dSEric Biggers if (plen) { 3311da177e4SLinus Torvalds ret = -ENOMEM; 3321da177e4SLinus Torvalds payload = kmalloc(plen, GFP_KERNEL); 3331da177e4SLinus Torvalds if (!payload) 3341da177e4SLinus Torvalds goto error; 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds ret = -EFAULT; 3371da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 3381da177e4SLinus Torvalds goto error2; 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds /* find the target key (which must be writable) */ 342f5895943SDavid Howells key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE); 343664cceb0SDavid Howells if (IS_ERR(key_ref)) { 344664cceb0SDavid Howells ret = PTR_ERR(key_ref); 3451da177e4SLinus Torvalds goto error2; 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds /* update the key */ 349664cceb0SDavid Howells ret = key_update(key_ref, payload, plen); 3501da177e4SLinus Torvalds 351664cceb0SDavid Howells key_ref_put(key_ref); 3521da177e4SLinus Torvalds error2: 35357070c85SEric Biggers kzfree(payload); 3541da177e4SLinus Torvalds error: 3551da177e4SLinus Torvalds return ret; 356a8b17ed0SDavid Howells } 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds /* 359973c9f4fSDavid Howells * Revoke a key. 360973c9f4fSDavid Howells * 361973c9f4fSDavid Howells * The key must be grant the caller Write or Setattr permission for this to 362973c9f4fSDavid Howells * work. The key type should give up its quota claim when revoked. The key 363973c9f4fSDavid Howells * and any links to the key will be automatically garbage collected after a 364973c9f4fSDavid Howells * certain amount of time (/proc/sys/kernel/keys/gc_delay). 365973c9f4fSDavid Howells * 366d3600bcfSMimi Zohar * Keys with KEY_FLAG_KEEP set should not be revoked. 367d3600bcfSMimi Zohar * 368973c9f4fSDavid Howells * If successful, 0 is returned. 3691da177e4SLinus Torvalds */ 3701da177e4SLinus Torvalds long keyctl_revoke_key(key_serial_t id) 3711da177e4SLinus Torvalds { 372664cceb0SDavid Howells key_ref_t key_ref; 373d3600bcfSMimi Zohar struct key *key; 3741da177e4SLinus Torvalds long ret; 3751da177e4SLinus Torvalds 376f5895943SDavid Howells key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE); 377664cceb0SDavid Howells if (IS_ERR(key_ref)) { 378664cceb0SDavid Howells ret = PTR_ERR(key_ref); 3790c2c9a3fSDavid Howells if (ret != -EACCES) 3801da177e4SLinus Torvalds goto error; 381f5895943SDavid Howells key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR); 3820c2c9a3fSDavid Howells if (IS_ERR(key_ref)) { 3830c2c9a3fSDavid Howells ret = PTR_ERR(key_ref); 3840c2c9a3fSDavid Howells goto error; 3850c2c9a3fSDavid Howells } 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds 388d3600bcfSMimi Zohar key = key_ref_to_ptr(key_ref); 3891da177e4SLinus Torvalds ret = 0; 3901d6d167cSMimi Zohar if (test_bit(KEY_FLAG_KEEP, &key->flags)) 3911d6d167cSMimi Zohar ret = -EPERM; 3921d6d167cSMimi Zohar else 3931d6d167cSMimi Zohar key_revoke(key); 3941da177e4SLinus Torvalds 395664cceb0SDavid Howells key_ref_put(key_ref); 3961da177e4SLinus Torvalds error: 3971260f801SDavid Howells return ret; 398a8b17ed0SDavid Howells } 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds /* 401fd75815fSDavid Howells * Invalidate a key. 402fd75815fSDavid Howells * 403fd75815fSDavid Howells * The key must be grant the caller Invalidate permission for this to work. 404fd75815fSDavid Howells * The key and any links to the key will be automatically garbage collected 405fd75815fSDavid Howells * immediately. 406fd75815fSDavid Howells * 407d3600bcfSMimi Zohar * Keys with KEY_FLAG_KEEP set should not be invalidated. 408d3600bcfSMimi Zohar * 409fd75815fSDavid Howells * If successful, 0 is returned. 410fd75815fSDavid Howells */ 411fd75815fSDavid Howells long keyctl_invalidate_key(key_serial_t id) 412fd75815fSDavid Howells { 413fd75815fSDavid Howells key_ref_t key_ref; 414d3600bcfSMimi Zohar struct key *key; 415fd75815fSDavid Howells long ret; 416fd75815fSDavid Howells 417fd75815fSDavid Howells kenter("%d", id); 418fd75815fSDavid Howells 419f5895943SDavid Howells key_ref = lookup_user_key(id, 0, KEY_NEED_SEARCH); 420fd75815fSDavid Howells if (IS_ERR(key_ref)) { 421fd75815fSDavid Howells ret = PTR_ERR(key_ref); 4220c7774abSDavid Howells 4230c7774abSDavid Howells /* Root is permitted to invalidate certain special keys */ 4240c7774abSDavid Howells if (capable(CAP_SYS_ADMIN)) { 4250c7774abSDavid Howells key_ref = lookup_user_key(id, 0, 0); 4260c7774abSDavid Howells if (IS_ERR(key_ref)) 4270c7774abSDavid Howells goto error; 4280c7774abSDavid Howells if (test_bit(KEY_FLAG_ROOT_CAN_INVAL, 4290c7774abSDavid Howells &key_ref_to_ptr(key_ref)->flags)) 4300c7774abSDavid Howells goto invalidate; 4310c7774abSDavid Howells goto error_put; 4320c7774abSDavid Howells } 4330c7774abSDavid Howells 434fd75815fSDavid Howells goto error; 435fd75815fSDavid Howells } 436fd75815fSDavid Howells 4370c7774abSDavid Howells invalidate: 438d3600bcfSMimi Zohar key = key_ref_to_ptr(key_ref); 439fd75815fSDavid Howells ret = 0; 440d3600bcfSMimi Zohar if (test_bit(KEY_FLAG_KEEP, &key->flags)) 441d3600bcfSMimi Zohar ret = -EPERM; 4421d6d167cSMimi Zohar else 443d3600bcfSMimi Zohar key_invalidate(key); 4440c7774abSDavid Howells error_put: 445fd75815fSDavid Howells key_ref_put(key_ref); 446fd75815fSDavid Howells error: 447fd75815fSDavid Howells kleave(" = %ld", ret); 448fd75815fSDavid Howells return ret; 449fd75815fSDavid Howells } 450fd75815fSDavid Howells 451fd75815fSDavid Howells /* 452973c9f4fSDavid Howells * Clear the specified keyring, creating an empty process keyring if one of the 453973c9f4fSDavid Howells * special keyring IDs is used. 454973c9f4fSDavid Howells * 455d3600bcfSMimi Zohar * The keyring must grant the caller Write permission and not have 456d3600bcfSMimi Zohar * KEY_FLAG_KEEP set for this to work. If successful, 0 will be returned. 4571da177e4SLinus Torvalds */ 4581da177e4SLinus Torvalds long keyctl_keyring_clear(key_serial_t ringid) 4591da177e4SLinus Torvalds { 460664cceb0SDavid Howells key_ref_t keyring_ref; 461d3600bcfSMimi Zohar struct key *keyring; 4621da177e4SLinus Torvalds long ret; 4631da177e4SLinus Torvalds 464f5895943SDavid Howells keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); 465664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 466664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 467700920ebSDavid Howells 468700920ebSDavid Howells /* Root is permitted to invalidate certain special keyrings */ 469700920ebSDavid Howells if (capable(CAP_SYS_ADMIN)) { 470700920ebSDavid Howells keyring_ref = lookup_user_key(ringid, 0, 0); 471700920ebSDavid Howells if (IS_ERR(keyring_ref)) 472700920ebSDavid Howells goto error; 473700920ebSDavid Howells if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR, 474700920ebSDavid Howells &key_ref_to_ptr(keyring_ref)->flags)) 475700920ebSDavid Howells goto clear; 476700920ebSDavid Howells goto error_put; 477700920ebSDavid Howells } 478700920ebSDavid Howells 4791da177e4SLinus Torvalds goto error; 4801da177e4SLinus Torvalds } 4811da177e4SLinus Torvalds 482700920ebSDavid Howells clear: 483d3600bcfSMimi Zohar keyring = key_ref_to_ptr(keyring_ref); 484d3600bcfSMimi Zohar if (test_bit(KEY_FLAG_KEEP, &keyring->flags)) 485d3600bcfSMimi Zohar ret = -EPERM; 486d3600bcfSMimi Zohar else 487d3600bcfSMimi Zohar ret = keyring_clear(keyring); 488700920ebSDavid Howells error_put: 489664cceb0SDavid Howells key_ref_put(keyring_ref); 4901da177e4SLinus Torvalds error: 4911da177e4SLinus Torvalds return ret; 492a8b17ed0SDavid Howells } 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds /* 495973c9f4fSDavid Howells * Create a link from a keyring to a key if there's no matching key in the 496973c9f4fSDavid Howells * keyring, otherwise replace the link to the matching key with a link to the 497973c9f4fSDavid Howells * new key. 498973c9f4fSDavid Howells * 499973c9f4fSDavid Howells * The key must grant the caller Link permission and the the keyring must grant 500973c9f4fSDavid Howells * the caller Write permission. Furthermore, if an additional link is created, 501973c9f4fSDavid Howells * the keyring's quota will be extended. 502973c9f4fSDavid Howells * 503973c9f4fSDavid Howells * If successful, 0 will be returned. 5041da177e4SLinus Torvalds */ 5051da177e4SLinus Torvalds long keyctl_keyring_link(key_serial_t id, key_serial_t ringid) 5061da177e4SLinus Torvalds { 507664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 5081da177e4SLinus Torvalds long ret; 5091da177e4SLinus Torvalds 510f5895943SDavid Howells keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); 511664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 512664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 5131da177e4SLinus Torvalds goto error; 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds 516f5895943SDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK); 517664cceb0SDavid Howells if (IS_ERR(key_ref)) { 518664cceb0SDavid Howells ret = PTR_ERR(key_ref); 5191da177e4SLinus Torvalds goto error2; 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds 522664cceb0SDavid Howells ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref)); 5231da177e4SLinus Torvalds 524664cceb0SDavid Howells key_ref_put(key_ref); 5251da177e4SLinus Torvalds error2: 526664cceb0SDavid Howells key_ref_put(keyring_ref); 5271da177e4SLinus Torvalds error: 5281da177e4SLinus Torvalds return ret; 529a8b17ed0SDavid Howells } 5301da177e4SLinus Torvalds 5311da177e4SLinus Torvalds /* 532973c9f4fSDavid Howells * Unlink a key from a keyring. 533973c9f4fSDavid Howells * 534973c9f4fSDavid Howells * The keyring must grant the caller Write permission for this to work; the key 535973c9f4fSDavid Howells * itself need not grant the caller anything. If the last link to a key is 536973c9f4fSDavid Howells * removed then that key will be scheduled for destruction. 537973c9f4fSDavid Howells * 538d3600bcfSMimi Zohar * Keys or keyrings with KEY_FLAG_KEEP set should not be unlinked. 539d3600bcfSMimi Zohar * 540973c9f4fSDavid Howells * If successful, 0 will be returned. 5411da177e4SLinus Torvalds */ 5421da177e4SLinus Torvalds long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid) 5431da177e4SLinus Torvalds { 544664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 545d3600bcfSMimi Zohar struct key *keyring, *key; 5461da177e4SLinus Torvalds long ret; 5471da177e4SLinus Torvalds 548f5895943SDavid Howells keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE); 549664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 550664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 5511da177e4SLinus Torvalds goto error; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 5545593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0); 555664cceb0SDavid Howells if (IS_ERR(key_ref)) { 556664cceb0SDavid Howells ret = PTR_ERR(key_ref); 5571da177e4SLinus Torvalds goto error2; 5581da177e4SLinus Torvalds } 5591da177e4SLinus Torvalds 560d3600bcfSMimi Zohar keyring = key_ref_to_ptr(keyring_ref); 561d3600bcfSMimi Zohar key = key_ref_to_ptr(key_ref); 562d3600bcfSMimi Zohar if (test_bit(KEY_FLAG_KEEP, &keyring->flags) && 563d3600bcfSMimi Zohar test_bit(KEY_FLAG_KEEP, &key->flags)) 564d3600bcfSMimi Zohar ret = -EPERM; 565d3600bcfSMimi Zohar else 566d3600bcfSMimi Zohar ret = key_unlink(keyring, key); 5671da177e4SLinus Torvalds 568664cceb0SDavid Howells key_ref_put(key_ref); 5691da177e4SLinus Torvalds error2: 570664cceb0SDavid Howells key_ref_put(keyring_ref); 5711da177e4SLinus Torvalds error: 5721da177e4SLinus Torvalds return ret; 573a8b17ed0SDavid Howells } 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds /* 576*ed0ac5c7SDavid Howells * Move a link to a key from one keyring to another, displacing any matching 577*ed0ac5c7SDavid Howells * key from the destination keyring. 578*ed0ac5c7SDavid Howells * 579*ed0ac5c7SDavid Howells * The key must grant the caller Link permission and both keyrings must grant 580*ed0ac5c7SDavid Howells * the caller Write permission. There must also be a link in the from keyring 581*ed0ac5c7SDavid Howells * to the key. If both keyrings are the same, nothing is done. 582*ed0ac5c7SDavid Howells * 583*ed0ac5c7SDavid Howells * If successful, 0 will be returned. 584*ed0ac5c7SDavid Howells */ 585*ed0ac5c7SDavid Howells long keyctl_keyring_move(key_serial_t id, key_serial_t from_ringid, 586*ed0ac5c7SDavid Howells key_serial_t to_ringid, unsigned int flags) 587*ed0ac5c7SDavid Howells { 588*ed0ac5c7SDavid Howells key_ref_t key_ref, from_ref, to_ref; 589*ed0ac5c7SDavid Howells long ret; 590*ed0ac5c7SDavid Howells 591*ed0ac5c7SDavid Howells if (flags & ~KEYCTL_MOVE_EXCL) 592*ed0ac5c7SDavid Howells return -EINVAL; 593*ed0ac5c7SDavid Howells 594*ed0ac5c7SDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK); 595*ed0ac5c7SDavid Howells if (IS_ERR(key_ref)) 596*ed0ac5c7SDavid Howells return PTR_ERR(key_ref); 597*ed0ac5c7SDavid Howells 598*ed0ac5c7SDavid Howells from_ref = lookup_user_key(from_ringid, 0, KEY_NEED_WRITE); 599*ed0ac5c7SDavid Howells if (IS_ERR(from_ref)) { 600*ed0ac5c7SDavid Howells ret = PTR_ERR(from_ref); 601*ed0ac5c7SDavid Howells goto error2; 602*ed0ac5c7SDavid Howells } 603*ed0ac5c7SDavid Howells 604*ed0ac5c7SDavid Howells to_ref = lookup_user_key(to_ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); 605*ed0ac5c7SDavid Howells if (IS_ERR(to_ref)) { 606*ed0ac5c7SDavid Howells ret = PTR_ERR(to_ref); 607*ed0ac5c7SDavid Howells goto error3; 608*ed0ac5c7SDavid Howells } 609*ed0ac5c7SDavid Howells 610*ed0ac5c7SDavid Howells ret = key_move(key_ref_to_ptr(key_ref), key_ref_to_ptr(from_ref), 611*ed0ac5c7SDavid Howells key_ref_to_ptr(to_ref), flags); 612*ed0ac5c7SDavid Howells 613*ed0ac5c7SDavid Howells key_ref_put(to_ref); 614*ed0ac5c7SDavid Howells error3: 615*ed0ac5c7SDavid Howells key_ref_put(from_ref); 616*ed0ac5c7SDavid Howells error2: 617*ed0ac5c7SDavid Howells key_ref_put(key_ref); 618*ed0ac5c7SDavid Howells return ret; 619*ed0ac5c7SDavid Howells } 620*ed0ac5c7SDavid Howells 621*ed0ac5c7SDavid Howells /* 622973c9f4fSDavid Howells * Return a description of a key to userspace. 623973c9f4fSDavid Howells * 624973c9f4fSDavid Howells * The key must grant the caller View permission for this to work. 625973c9f4fSDavid Howells * 626973c9f4fSDavid Howells * If there's a buffer, we place up to buflen bytes of data into it formatted 627973c9f4fSDavid Howells * in the following way: 628973c9f4fSDavid Howells * 6291da177e4SLinus Torvalds * type;uid;gid;perm;description<NUL> 630973c9f4fSDavid Howells * 631973c9f4fSDavid Howells * If successful, we return the amount of description available, irrespective 632973c9f4fSDavid Howells * of how much we may have copied into the buffer. 6331da177e4SLinus Torvalds */ 6341da177e4SLinus Torvalds long keyctl_describe_key(key_serial_t keyid, 6351da177e4SLinus Torvalds char __user *buffer, 6361da177e4SLinus Torvalds size_t buflen) 6371da177e4SLinus Torvalds { 6383e30148cSDavid Howells struct key *key, *instkey; 639664cceb0SDavid Howells key_ref_t key_ref; 640aa9d4437SDavid Howells char *infobuf; 6411da177e4SLinus Torvalds long ret; 642aa9d4437SDavid Howells int desclen, infolen; 6431da177e4SLinus Torvalds 644f5895943SDavid Howells key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW); 645664cceb0SDavid Howells if (IS_ERR(key_ref)) { 6463e30148cSDavid Howells /* viewing a key under construction is permitted if we have the 6473e30148cSDavid Howells * authorisation token handy */ 648664cceb0SDavid Howells if (PTR_ERR(key_ref) == -EACCES) { 6493e30148cSDavid Howells instkey = key_get_instantiation_authkey(keyid); 6503e30148cSDavid Howells if (!IS_ERR(instkey)) { 6513e30148cSDavid Howells key_put(instkey); 6528bbf4976SDavid Howells key_ref = lookup_user_key(keyid, 6535593122eSDavid Howells KEY_LOOKUP_PARTIAL, 6545593122eSDavid Howells 0); 655664cceb0SDavid Howells if (!IS_ERR(key_ref)) 6563e30148cSDavid Howells goto okay; 6573e30148cSDavid Howells } 6583e30148cSDavid Howells } 6593e30148cSDavid Howells 660664cceb0SDavid Howells ret = PTR_ERR(key_ref); 6611da177e4SLinus Torvalds goto error; 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds 6643e30148cSDavid Howells okay: 665664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 666aa9d4437SDavid Howells desclen = strlen(key->description); 667664cceb0SDavid Howells 668aa9d4437SDavid Howells /* calculate how much information we're going to return */ 669aa9d4437SDavid Howells ret = -ENOMEM; 670aa9d4437SDavid Howells infobuf = kasprintf(GFP_KERNEL, 671aa9d4437SDavid Howells "%s;%d;%d;%08x;", 67294fd8405SDavid Howells key->type->name, 6739a56c2dbSEric W. Biederman from_kuid_munged(current_user_ns(), key->uid), 6749a56c2dbSEric W. Biederman from_kgid_munged(current_user_ns(), key->gid), 675aa9d4437SDavid Howells key->perm); 676aa9d4437SDavid Howells if (!infobuf) 677aa9d4437SDavid Howells goto error2; 678aa9d4437SDavid Howells infolen = strlen(infobuf); 679aa9d4437SDavid Howells ret = infolen + desclen + 1; 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds /* consider returning the data */ 682aa9d4437SDavid Howells if (buffer && buflen >= ret) { 683aa9d4437SDavid Howells if (copy_to_user(buffer, infobuf, infolen) != 0 || 684aa9d4437SDavid Howells copy_to_user(buffer + infolen, key->description, 685aa9d4437SDavid Howells desclen + 1) != 0) 6861da177e4SLinus Torvalds ret = -EFAULT; 6871da177e4SLinus Torvalds } 6881da177e4SLinus Torvalds 689aa9d4437SDavid Howells kfree(infobuf); 6901da177e4SLinus Torvalds error2: 691664cceb0SDavid Howells key_ref_put(key_ref); 6921da177e4SLinus Torvalds error: 6931da177e4SLinus Torvalds return ret; 694a8b17ed0SDavid Howells } 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds /* 697973c9f4fSDavid Howells * Search the specified keyring and any keyrings it links to for a matching 698973c9f4fSDavid Howells * key. Only keyrings that grant the caller Search permission will be searched 699973c9f4fSDavid Howells * (this includes the starting keyring). Only keys with Search permission can 700973c9f4fSDavid Howells * be found. 701973c9f4fSDavid Howells * 702973c9f4fSDavid Howells * If successful, the found key will be linked to the destination keyring if 703973c9f4fSDavid Howells * supplied and the key has Link permission, and the found key ID will be 704973c9f4fSDavid Howells * returned. 7051da177e4SLinus Torvalds */ 7061da177e4SLinus Torvalds long keyctl_keyring_search(key_serial_t ringid, 7071da177e4SLinus Torvalds const char __user *_type, 7081da177e4SLinus Torvalds const char __user *_description, 7091da177e4SLinus Torvalds key_serial_t destringid) 7101da177e4SLinus Torvalds { 7111da177e4SLinus Torvalds struct key_type *ktype; 712664cceb0SDavid Howells key_ref_t keyring_ref, key_ref, dest_ref; 7131da177e4SLinus Torvalds char type[32], *description; 7140cb409d9SDavi Arnaut long ret; 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds /* pull the type and description into kernel space */ 7170cb409d9SDavi Arnaut ret = key_get_type_from_user(type, _type, sizeof(type)); 7181da177e4SLinus Torvalds if (ret < 0) 7191da177e4SLinus Torvalds goto error; 7201da177e4SLinus Torvalds 721aa9d4437SDavid Howells description = strndup_user(_description, KEY_MAX_DESC_SIZE); 7220cb409d9SDavi Arnaut if (IS_ERR(description)) { 7230cb409d9SDavi Arnaut ret = PTR_ERR(description); 7241da177e4SLinus Torvalds goto error; 7250cb409d9SDavi Arnaut } 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds /* get the keyring at which to begin the search */ 728f5895943SDavid Howells keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH); 729664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 730664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 7311da177e4SLinus Torvalds goto error2; 7321da177e4SLinus Torvalds } 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds /* get the destination keyring if specified */ 735664cceb0SDavid Howells dest_ref = NULL; 7361da177e4SLinus Torvalds if (destringid) { 7375593122eSDavid Howells dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE, 738f5895943SDavid Howells KEY_NEED_WRITE); 739664cceb0SDavid Howells if (IS_ERR(dest_ref)) { 740664cceb0SDavid Howells ret = PTR_ERR(dest_ref); 7411da177e4SLinus Torvalds goto error3; 7421da177e4SLinus Torvalds } 7431da177e4SLinus Torvalds } 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds /* find the key type */ 7461da177e4SLinus Torvalds ktype = key_type_lookup(type); 7471da177e4SLinus Torvalds if (IS_ERR(ktype)) { 7481da177e4SLinus Torvalds ret = PTR_ERR(ktype); 7491da177e4SLinus Torvalds goto error4; 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds /* do the search */ 753664cceb0SDavid Howells key_ref = keyring_search(keyring_ref, ktype, description); 754664cceb0SDavid Howells if (IS_ERR(key_ref)) { 755664cceb0SDavid Howells ret = PTR_ERR(key_ref); 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds /* treat lack or presence of a negative key the same */ 7581da177e4SLinus Torvalds if (ret == -EAGAIN) 7591da177e4SLinus Torvalds ret = -ENOKEY; 7601da177e4SLinus Torvalds goto error5; 7611da177e4SLinus Torvalds } 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds /* link the resulting key to the destination keyring if we can */ 764664cceb0SDavid Howells if (dest_ref) { 765f5895943SDavid Howells ret = key_permission(key_ref, KEY_NEED_LINK); 76629db9190SDavid Howells if (ret < 0) 7671da177e4SLinus Torvalds goto error6; 7681da177e4SLinus Torvalds 769664cceb0SDavid Howells ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref)); 7701da177e4SLinus Torvalds if (ret < 0) 7711da177e4SLinus Torvalds goto error6; 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds 774664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 7751da177e4SLinus Torvalds 7761da177e4SLinus Torvalds error6: 777664cceb0SDavid Howells key_ref_put(key_ref); 7781da177e4SLinus Torvalds error5: 7791da177e4SLinus Torvalds key_type_put(ktype); 7801da177e4SLinus Torvalds error4: 781664cceb0SDavid Howells key_ref_put(dest_ref); 7821da177e4SLinus Torvalds error3: 783664cceb0SDavid Howells key_ref_put(keyring_ref); 7841da177e4SLinus Torvalds error2: 7851da177e4SLinus Torvalds kfree(description); 7861da177e4SLinus Torvalds error: 7871da177e4SLinus Torvalds return ret; 788a8b17ed0SDavid Howells } 7891da177e4SLinus Torvalds 7901da177e4SLinus Torvalds /* 791973c9f4fSDavid Howells * Read a key's payload. 792973c9f4fSDavid Howells * 793973c9f4fSDavid Howells * The key must either grant the caller Read permission, or it must grant the 794973c9f4fSDavid Howells * caller Search permission when searched for from the process keyrings. 795973c9f4fSDavid Howells * 796973c9f4fSDavid Howells * If successful, we place up to buflen bytes of data into the buffer, if one 797973c9f4fSDavid Howells * is provided, and return the amount of data that is available in the key, 798973c9f4fSDavid Howells * irrespective of how much we copied into the buffer. 7991da177e4SLinus Torvalds */ 8001da177e4SLinus Torvalds long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen) 8011da177e4SLinus Torvalds { 802664cceb0SDavid Howells struct key *key; 803664cceb0SDavid Howells key_ref_t key_ref; 8041da177e4SLinus Torvalds long ret; 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds /* find the key first */ 8075593122eSDavid Howells key_ref = lookup_user_key(keyid, 0, 0); 808664cceb0SDavid Howells if (IS_ERR(key_ref)) { 809664cceb0SDavid Howells ret = -ENOKEY; 810664cceb0SDavid Howells goto error; 811664cceb0SDavid Howells } 812664cceb0SDavid Howells 813664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 814664cceb0SDavid Howells 815363b02daSDavid Howells ret = key_read_state(key); 816363b02daSDavid Howells if (ret < 0) 817363b02daSDavid Howells goto error2; /* Negatively instantiated */ 81837863c43SEric Biggers 8191da177e4SLinus Torvalds /* see if we can read it directly */ 820f5895943SDavid Howells ret = key_permission(key_ref, KEY_NEED_READ); 82129db9190SDavid Howells if (ret == 0) 8221da177e4SLinus Torvalds goto can_read_key; 82329db9190SDavid Howells if (ret != -EACCES) 8247fc0786dSEric Biggers goto error2; 8251da177e4SLinus Torvalds 826664cceb0SDavid Howells /* we can't; see if it's searchable from this process's keyrings 8273e30148cSDavid Howells * - we automatically take account of the fact that it may be 8283e30148cSDavid Howells * dangling off an instantiation key 8293e30148cSDavid Howells */ 830664cceb0SDavid Howells if (!is_key_possessed(key_ref)) { 8311260f801SDavid Howells ret = -EACCES; 8321da177e4SLinus Torvalds goto error2; 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds /* the key is probably readable - now try to read it */ 8361da177e4SLinus Torvalds can_read_key: 8371da177e4SLinus Torvalds ret = -EOPNOTSUPP; 8381da177e4SLinus Torvalds if (key->type->read) { 839b4a1b4f5SDavid Howells /* Read the data with the semaphore held (since we might sleep) 840b4a1b4f5SDavid Howells * to protect against the key being updated or revoked. 841b4a1b4f5SDavid Howells */ 8421da177e4SLinus Torvalds down_read(&key->sem); 843b4a1b4f5SDavid Howells ret = key_validate(key); 844b4a1b4f5SDavid Howells if (ret == 0) 8451da177e4SLinus Torvalds ret = key->type->read(key, buffer, buflen); 8461da177e4SLinus Torvalds up_read(&key->sem); 8471da177e4SLinus Torvalds } 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds error2: 8501da177e4SLinus Torvalds key_put(key); 8511da177e4SLinus Torvalds error: 8521da177e4SLinus Torvalds return ret; 853a8b17ed0SDavid Howells } 8541da177e4SLinus Torvalds 8551da177e4SLinus Torvalds /* 856973c9f4fSDavid Howells * Change the ownership of a key 857973c9f4fSDavid Howells * 858973c9f4fSDavid Howells * The key must grant the caller Setattr permission for this to work, though 859973c9f4fSDavid Howells * the key need not be fully instantiated yet. For the UID to be changed, or 860973c9f4fSDavid Howells * for the GID to be changed to a group the caller is not a member of, the 861973c9f4fSDavid Howells * caller must have sysadmin capability. If either uid or gid is -1 then that 862973c9f4fSDavid Howells * attribute is not changed. 863973c9f4fSDavid Howells * 864973c9f4fSDavid Howells * If the UID is to be changed, the new user must have sufficient quota to 865973c9f4fSDavid Howells * accept the key. The quota deduction will be removed from the old user to 866973c9f4fSDavid Howells * the new user should the attribute be changed. 867973c9f4fSDavid Howells * 868973c9f4fSDavid Howells * If successful, 0 will be returned. 8691da177e4SLinus Torvalds */ 8709a56c2dbSEric W. Biederman long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group) 8711da177e4SLinus Torvalds { 8725801649dSFredrik Tolf struct key_user *newowner, *zapowner = NULL; 8731da177e4SLinus Torvalds struct key *key; 874664cceb0SDavid Howells key_ref_t key_ref; 8751da177e4SLinus Torvalds long ret; 8769a56c2dbSEric W. Biederman kuid_t uid; 8779a56c2dbSEric W. Biederman kgid_t gid; 8789a56c2dbSEric W. Biederman 8799a56c2dbSEric W. Biederman uid = make_kuid(current_user_ns(), user); 8809a56c2dbSEric W. Biederman gid = make_kgid(current_user_ns(), group); 8819a56c2dbSEric W. Biederman ret = -EINVAL; 8829a56c2dbSEric W. Biederman if ((user != (uid_t) -1) && !uid_valid(uid)) 8839a56c2dbSEric W. Biederman goto error; 8849a56c2dbSEric W. Biederman if ((group != (gid_t) -1) && !gid_valid(gid)) 8859a56c2dbSEric W. Biederman goto error; 8861da177e4SLinus Torvalds 8871da177e4SLinus Torvalds ret = 0; 8889a56c2dbSEric W. Biederman if (user == (uid_t) -1 && group == (gid_t) -1) 8891da177e4SLinus Torvalds goto error; 8901da177e4SLinus Torvalds 8915593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 892f5895943SDavid Howells KEY_NEED_SETATTR); 893664cceb0SDavid Howells if (IS_ERR(key_ref)) { 894664cceb0SDavid Howells ret = PTR_ERR(key_ref); 8951da177e4SLinus Torvalds goto error; 8961da177e4SLinus Torvalds } 8971da177e4SLinus Torvalds 898664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 899664cceb0SDavid Howells 9001da177e4SLinus Torvalds /* make the changes with the locks held to prevent chown/chown races */ 9011da177e4SLinus Torvalds ret = -EACCES; 9021da177e4SLinus Torvalds down_write(&key->sem); 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) { 9051da177e4SLinus Torvalds /* only the sysadmin can chown a key to some other UID */ 9069a56c2dbSEric W. Biederman if (user != (uid_t) -1 && !uid_eq(key->uid, uid)) 9075801649dSFredrik Tolf goto error_put; 9081da177e4SLinus Torvalds 9091da177e4SLinus Torvalds /* only the sysadmin can set the key's GID to a group other 9101da177e4SLinus Torvalds * than one of those that the current process subscribes to */ 9119a56c2dbSEric W. Biederman if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid)) 9125801649dSFredrik Tolf goto error_put; 9131da177e4SLinus Torvalds } 9141da177e4SLinus Torvalds 9155801649dSFredrik Tolf /* change the UID */ 9169a56c2dbSEric W. Biederman if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) { 9175801649dSFredrik Tolf ret = -ENOMEM; 9189a56c2dbSEric W. Biederman newowner = key_user_lookup(uid); 9195801649dSFredrik Tolf if (!newowner) 9205801649dSFredrik Tolf goto error_put; 9215801649dSFredrik Tolf 9225801649dSFredrik Tolf /* transfer the quota burden to the new user */ 9235801649dSFredrik Tolf if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { 9249a56c2dbSEric W. Biederman unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ? 9250b77f5bfSDavid Howells key_quota_root_maxkeys : key_quota_maxkeys; 9269a56c2dbSEric W. Biederman unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ? 9270b77f5bfSDavid Howells key_quota_root_maxbytes : key_quota_maxbytes; 9280b77f5bfSDavid Howells 9295801649dSFredrik Tolf spin_lock(&newowner->lock); 9300b77f5bfSDavid Howells if (newowner->qnkeys + 1 >= maxkeys || 9310b77f5bfSDavid Howells newowner->qnbytes + key->quotalen >= maxbytes || 9320b77f5bfSDavid Howells newowner->qnbytes + key->quotalen < 9330b77f5bfSDavid Howells newowner->qnbytes) 9345801649dSFredrik Tolf goto quota_overrun; 9355801649dSFredrik Tolf 9365801649dSFredrik Tolf newowner->qnkeys++; 9375801649dSFredrik Tolf newowner->qnbytes += key->quotalen; 9385801649dSFredrik Tolf spin_unlock(&newowner->lock); 9395801649dSFredrik Tolf 9405801649dSFredrik Tolf spin_lock(&key->user->lock); 9415801649dSFredrik Tolf key->user->qnkeys--; 9425801649dSFredrik Tolf key->user->qnbytes -= key->quotalen; 9435801649dSFredrik Tolf spin_unlock(&key->user->lock); 9445801649dSFredrik Tolf } 9455801649dSFredrik Tolf 9465801649dSFredrik Tolf atomic_dec(&key->user->nkeys); 9475801649dSFredrik Tolf atomic_inc(&newowner->nkeys); 9485801649dSFredrik Tolf 949363b02daSDavid Howells if (key->state != KEY_IS_UNINSTANTIATED) { 9505801649dSFredrik Tolf atomic_dec(&key->user->nikeys); 9515801649dSFredrik Tolf atomic_inc(&newowner->nikeys); 9525801649dSFredrik Tolf } 9535801649dSFredrik Tolf 9545801649dSFredrik Tolf zapowner = key->user; 9555801649dSFredrik Tolf key->user = newowner; 9565801649dSFredrik Tolf key->uid = uid; 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds 9591da177e4SLinus Torvalds /* change the GID */ 9609a56c2dbSEric W. Biederman if (group != (gid_t) -1) 9611da177e4SLinus Torvalds key->gid = gid; 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds ret = 0; 9641da177e4SLinus Torvalds 9655801649dSFredrik Tolf error_put: 9661da177e4SLinus Torvalds up_write(&key->sem); 9671da177e4SLinus Torvalds key_put(key); 9685801649dSFredrik Tolf if (zapowner) 9695801649dSFredrik Tolf key_user_put(zapowner); 9701da177e4SLinus Torvalds error: 9711da177e4SLinus Torvalds return ret; 9721da177e4SLinus Torvalds 9735801649dSFredrik Tolf quota_overrun: 9745801649dSFredrik Tolf spin_unlock(&newowner->lock); 9755801649dSFredrik Tolf zapowner = newowner; 9765801649dSFredrik Tolf ret = -EDQUOT; 9775801649dSFredrik Tolf goto error_put; 978a8b17ed0SDavid Howells } 9795801649dSFredrik Tolf 9801da177e4SLinus Torvalds /* 981973c9f4fSDavid Howells * Change the permission mask on a key. 982973c9f4fSDavid Howells * 983973c9f4fSDavid Howells * The key must grant the caller Setattr permission for this to work, though 984973c9f4fSDavid Howells * the key need not be fully instantiated yet. If the caller does not have 985973c9f4fSDavid Howells * sysadmin capability, it may only change the permission on keys that it owns. 9861da177e4SLinus Torvalds */ 9871da177e4SLinus Torvalds long keyctl_setperm_key(key_serial_t id, key_perm_t perm) 9881da177e4SLinus Torvalds { 9891da177e4SLinus Torvalds struct key *key; 990664cceb0SDavid Howells key_ref_t key_ref; 9911da177e4SLinus Torvalds long ret; 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds ret = -EINVAL; 994664cceb0SDavid Howells if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL)) 9951da177e4SLinus Torvalds goto error; 9961da177e4SLinus Torvalds 9975593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 998f5895943SDavid Howells KEY_NEED_SETATTR); 999664cceb0SDavid Howells if (IS_ERR(key_ref)) { 1000664cceb0SDavid Howells ret = PTR_ERR(key_ref); 10011da177e4SLinus Torvalds goto error; 10021da177e4SLinus Torvalds } 10031da177e4SLinus Torvalds 1004664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 1005664cceb0SDavid Howells 100676d8aeabSDavid Howells /* make the changes with the locks held to prevent chown/chmod races */ 10071da177e4SLinus Torvalds ret = -EACCES; 10081da177e4SLinus Torvalds down_write(&key->sem); 10091da177e4SLinus Torvalds 101076d8aeabSDavid Howells /* if we're not the sysadmin, we can only change a key that we own */ 10119a56c2dbSEric W. Biederman if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) { 10121da177e4SLinus Torvalds key->perm = perm; 10131da177e4SLinus Torvalds ret = 0; 101476d8aeabSDavid Howells } 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds up_write(&key->sem); 10171da177e4SLinus Torvalds key_put(key); 10181da177e4SLinus Torvalds error: 10191da177e4SLinus Torvalds return ret; 1020a8b17ed0SDavid Howells } 10211da177e4SLinus Torvalds 10228bbf4976SDavid Howells /* 1023973c9f4fSDavid Howells * Get the destination keyring for instantiation and check that the caller has 1024973c9f4fSDavid Howells * Write permission on it. 10258bbf4976SDavid Howells */ 10268bbf4976SDavid Howells static long get_instantiation_keyring(key_serial_t ringid, 10278bbf4976SDavid Howells struct request_key_auth *rka, 10288bbf4976SDavid Howells struct key **_dest_keyring) 10298bbf4976SDavid Howells { 10308bbf4976SDavid Howells key_ref_t dkref; 10318bbf4976SDavid Howells 10328bbf4976SDavid Howells *_dest_keyring = NULL; 1033eca1bf5bSDavid Howells 1034eca1bf5bSDavid Howells /* just return a NULL pointer if we weren't asked to make a link */ 1035eca1bf5bSDavid Howells if (ringid == 0) 10368bbf4976SDavid Howells return 0; 10378bbf4976SDavid Howells 10388bbf4976SDavid Howells /* if a specific keyring is nominated by ID, then use that */ 10398bbf4976SDavid Howells if (ringid > 0) { 1040f5895943SDavid Howells dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); 10418bbf4976SDavid Howells if (IS_ERR(dkref)) 10428bbf4976SDavid Howells return PTR_ERR(dkref); 10438bbf4976SDavid Howells *_dest_keyring = key_ref_to_ptr(dkref); 10448bbf4976SDavid Howells return 0; 10458bbf4976SDavid Howells } 10468bbf4976SDavid Howells 10478bbf4976SDavid Howells if (ringid == KEY_SPEC_REQKEY_AUTH_KEY) 10488bbf4976SDavid Howells return -EINVAL; 10498bbf4976SDavid Howells 10508bbf4976SDavid Howells /* otherwise specify the destination keyring recorded in the 10518bbf4976SDavid Howells * authorisation key (any KEY_SPEC_*_KEYRING) */ 10528bbf4976SDavid Howells if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) { 105321279cfaSDavid Howells *_dest_keyring = key_get(rka->dest_keyring); 10548bbf4976SDavid Howells return 0; 10558bbf4976SDavid Howells } 10568bbf4976SDavid Howells 10578bbf4976SDavid Howells return -ENOKEY; 10588bbf4976SDavid Howells } 10598bbf4976SDavid Howells 1060d84f4f99SDavid Howells /* 1061973c9f4fSDavid Howells * Change the request_key authorisation key on the current process. 1062d84f4f99SDavid Howells */ 1063d84f4f99SDavid Howells static int keyctl_change_reqkey_auth(struct key *key) 1064d84f4f99SDavid Howells { 1065d84f4f99SDavid Howells struct cred *new; 1066d84f4f99SDavid Howells 1067d84f4f99SDavid Howells new = prepare_creds(); 1068d84f4f99SDavid Howells if (!new) 1069d84f4f99SDavid Howells return -ENOMEM; 1070d84f4f99SDavid Howells 1071d84f4f99SDavid Howells key_put(new->request_key_auth); 1072d84f4f99SDavid Howells new->request_key_auth = key_get(key); 1073d84f4f99SDavid Howells 1074d84f4f99SDavid Howells return commit_creds(new); 1075d84f4f99SDavid Howells } 1076d84f4f99SDavid Howells 10771da177e4SLinus Torvalds /* 1078973c9f4fSDavid Howells * Instantiate a key with the specified payload and link the key into the 1079973c9f4fSDavid Howells * destination keyring if one is given. 1080973c9f4fSDavid Howells * 1081973c9f4fSDavid Howells * The caller must have the appropriate instantiation permit set for this to 1082973c9f4fSDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1083973c9f4fSDavid Howells * 1084973c9f4fSDavid Howells * If successful, 0 will be returned. 10851da177e4SLinus Torvalds */ 1086ee009e4aSDavid Howells long keyctl_instantiate_key_common(key_serial_t id, 1087b353a1f7SAl Viro struct iov_iter *from, 10881da177e4SLinus Torvalds key_serial_t ringid) 10891da177e4SLinus Torvalds { 1090d84f4f99SDavid Howells const struct cred *cred = current_cred(); 10913e30148cSDavid Howells struct request_key_auth *rka; 10928bbf4976SDavid Howells struct key *instkey, *dest_keyring; 1093b353a1f7SAl Viro size_t plen = from ? iov_iter_count(from) : 0; 10941da177e4SLinus Torvalds void *payload; 10951da177e4SLinus Torvalds long ret; 10961da177e4SLinus Torvalds 1097d84f4f99SDavid Howells kenter("%d,,%zu,%d", id, plen, ringid); 1098d84f4f99SDavid Howells 1099b353a1f7SAl Viro if (!plen) 1100b353a1f7SAl Viro from = NULL; 1101b353a1f7SAl Viro 11021da177e4SLinus Torvalds ret = -EINVAL; 110338bbca6bSDavid Howells if (plen > 1024 * 1024 - 1) 11041da177e4SLinus Torvalds goto error; 11051da177e4SLinus Torvalds 1106b5f545c8SDavid Howells /* the appropriate instantiation authorisation key must have been 1107b5f545c8SDavid Howells * assumed before calling this */ 1108b5f545c8SDavid Howells ret = -EPERM; 1109d84f4f99SDavid Howells instkey = cred->request_key_auth; 1110b5f545c8SDavid Howells if (!instkey) 1111b5f545c8SDavid Howells goto error; 1112b5f545c8SDavid Howells 1113146aa8b1SDavid Howells rka = instkey->payload.data[0]; 1114b5f545c8SDavid Howells if (rka->target_key->serial != id) 1115b5f545c8SDavid Howells goto error; 1116b5f545c8SDavid Howells 11171da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 11181da177e4SLinus Torvalds payload = NULL; 11191da177e4SLinus Torvalds 1120b353a1f7SAl Viro if (from) { 11211da177e4SLinus Torvalds ret = -ENOMEM; 1122752ade68SMichal Hocko payload = kvmalloc(plen, GFP_KERNEL); 11231da177e4SLinus Torvalds if (!payload) 11241da177e4SLinus Torvalds goto error; 11251da177e4SLinus Torvalds 1126b353a1f7SAl Viro ret = -EFAULT; 1127cbbd26b8SAl Viro if (!copy_from_iter_full(payload, plen, from)) 11281da177e4SLinus Torvalds goto error2; 11291da177e4SLinus Torvalds } 11301da177e4SLinus Torvalds 11313e30148cSDavid Howells /* find the destination keyring amongst those belonging to the 11323e30148cSDavid Howells * requesting task */ 11338bbf4976SDavid Howells ret = get_instantiation_keyring(ringid, rka, &dest_keyring); 11348bbf4976SDavid Howells if (ret < 0) 1135b5f545c8SDavid Howells goto error2; 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds /* instantiate the key and link it into a keyring */ 11383e30148cSDavid Howells ret = key_instantiate_and_link(rka->target_key, payload, plen, 11398bbf4976SDavid Howells dest_keyring, instkey); 11401da177e4SLinus Torvalds 11418bbf4976SDavid Howells key_put(dest_keyring); 1142b5f545c8SDavid Howells 1143b5f545c8SDavid Howells /* discard the assumed authority if it's just been disabled by 1144b5f545c8SDavid Howells * instantiation of the key */ 1145d84f4f99SDavid Howells if (ret == 0) 1146d84f4f99SDavid Howells keyctl_change_reqkey_auth(NULL); 1147b5f545c8SDavid Howells 11481da177e4SLinus Torvalds error2: 114957070c85SEric Biggers if (payload) { 115057070c85SEric Biggers memzero_explicit(payload, plen); 1151b353a1f7SAl Viro kvfree(payload); 115257070c85SEric Biggers } 11531da177e4SLinus Torvalds error: 11541da177e4SLinus Torvalds return ret; 1155a8b17ed0SDavid Howells } 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds /* 1158ee009e4aSDavid Howells * Instantiate a key with the specified payload and link the key into the 1159ee009e4aSDavid Howells * destination keyring if one is given. 1160ee009e4aSDavid Howells * 1161ee009e4aSDavid Howells * The caller must have the appropriate instantiation permit set for this to 1162ee009e4aSDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1163ee009e4aSDavid Howells * 1164ee009e4aSDavid Howells * If successful, 0 will be returned. 1165ee009e4aSDavid Howells */ 1166ee009e4aSDavid Howells long keyctl_instantiate_key(key_serial_t id, 1167ee009e4aSDavid Howells const void __user *_payload, 1168ee009e4aSDavid Howells size_t plen, 1169ee009e4aSDavid Howells key_serial_t ringid) 1170ee009e4aSDavid Howells { 1171ee009e4aSDavid Howells if (_payload && plen) { 1172b353a1f7SAl Viro struct iovec iov; 1173b353a1f7SAl Viro struct iov_iter from; 1174b353a1f7SAl Viro int ret; 1175ee009e4aSDavid Howells 1176b353a1f7SAl Viro ret = import_single_range(WRITE, (void __user *)_payload, plen, 1177b353a1f7SAl Viro &iov, &from); 1178b353a1f7SAl Viro if (unlikely(ret)) 1179b353a1f7SAl Viro return ret; 1180b353a1f7SAl Viro 1181b353a1f7SAl Viro return keyctl_instantiate_key_common(id, &from, ringid); 1182ee009e4aSDavid Howells } 1183ee009e4aSDavid Howells 1184b353a1f7SAl Viro return keyctl_instantiate_key_common(id, NULL, ringid); 1185ee009e4aSDavid Howells } 1186ee009e4aSDavid Howells 1187ee009e4aSDavid Howells /* 1188ee009e4aSDavid Howells * Instantiate a key with the specified multipart payload and link the key into 1189ee009e4aSDavid Howells * the destination keyring if one is given. 1190ee009e4aSDavid Howells * 1191ee009e4aSDavid Howells * The caller must have the appropriate instantiation permit set for this to 1192ee009e4aSDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1193ee009e4aSDavid Howells * 1194ee009e4aSDavid Howells * If successful, 0 will be returned. 1195ee009e4aSDavid Howells */ 1196ee009e4aSDavid Howells long keyctl_instantiate_key_iov(key_serial_t id, 1197ee009e4aSDavid Howells const struct iovec __user *_payload_iov, 1198ee009e4aSDavid Howells unsigned ioc, 1199ee009e4aSDavid Howells key_serial_t ringid) 1200ee009e4aSDavid Howells { 1201ee009e4aSDavid Howells struct iovec iovstack[UIO_FASTIOV], *iov = iovstack; 1202b353a1f7SAl Viro struct iov_iter from; 1203ee009e4aSDavid Howells long ret; 1204ee009e4aSDavid Howells 1205b353a1f7SAl Viro if (!_payload_iov) 1206b353a1f7SAl Viro ioc = 0; 1207ee009e4aSDavid Howells 1208b353a1f7SAl Viro ret = import_iovec(WRITE, _payload_iov, ioc, 1209b353a1f7SAl Viro ARRAY_SIZE(iovstack), &iov, &from); 1210ee009e4aSDavid Howells if (ret < 0) 1211b353a1f7SAl Viro return ret; 1212b353a1f7SAl Viro ret = keyctl_instantiate_key_common(id, &from, ringid); 1213ee009e4aSDavid Howells kfree(iov); 1214ee009e4aSDavid Howells return ret; 1215ee009e4aSDavid Howells } 1216ee009e4aSDavid Howells 1217ee009e4aSDavid Howells /* 1218973c9f4fSDavid Howells * Negatively instantiate the key with the given timeout (in seconds) and link 1219973c9f4fSDavid Howells * the key into the destination keyring if one is given. 1220973c9f4fSDavid Howells * 1221973c9f4fSDavid Howells * The caller must have the appropriate instantiation permit set for this to 1222973c9f4fSDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1223973c9f4fSDavid Howells * 1224973c9f4fSDavid Howells * The key and any links to the key will be automatically garbage collected 1225973c9f4fSDavid Howells * after the timeout expires. 1226973c9f4fSDavid Howells * 1227973c9f4fSDavid Howells * Negative keys are used to rate limit repeated request_key() calls by causing 1228973c9f4fSDavid Howells * them to return -ENOKEY until the negative key expires. 1229973c9f4fSDavid Howells * 1230973c9f4fSDavid Howells * If successful, 0 will be returned. 12311da177e4SLinus Torvalds */ 12321da177e4SLinus Torvalds long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid) 12331da177e4SLinus Torvalds { 1234fdd1b945SDavid Howells return keyctl_reject_key(id, timeout, ENOKEY, ringid); 1235fdd1b945SDavid Howells } 1236fdd1b945SDavid Howells 1237fdd1b945SDavid Howells /* 1238fdd1b945SDavid Howells * Negatively instantiate the key with the given timeout (in seconds) and error 1239fdd1b945SDavid Howells * code and link the key into the destination keyring if one is given. 1240fdd1b945SDavid Howells * 1241fdd1b945SDavid Howells * The caller must have the appropriate instantiation permit set for this to 1242fdd1b945SDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1243fdd1b945SDavid Howells * 1244fdd1b945SDavid Howells * The key and any links to the key will be automatically garbage collected 1245fdd1b945SDavid Howells * after the timeout expires. 1246fdd1b945SDavid Howells * 1247fdd1b945SDavid Howells * Negative keys are used to rate limit repeated request_key() calls by causing 1248fdd1b945SDavid Howells * them to return the specified error code until the negative key expires. 1249fdd1b945SDavid Howells * 1250fdd1b945SDavid Howells * If successful, 0 will be returned. 1251fdd1b945SDavid Howells */ 1252fdd1b945SDavid Howells long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error, 1253fdd1b945SDavid Howells key_serial_t ringid) 1254fdd1b945SDavid Howells { 1255d84f4f99SDavid Howells const struct cred *cred = current_cred(); 12563e30148cSDavid Howells struct request_key_auth *rka; 12578bbf4976SDavid Howells struct key *instkey, *dest_keyring; 12581da177e4SLinus Torvalds long ret; 12591da177e4SLinus Torvalds 1260fdd1b945SDavid Howells kenter("%d,%u,%u,%d", id, timeout, error, ringid); 1261fdd1b945SDavid Howells 1262fdd1b945SDavid Howells /* must be a valid error code and mustn't be a kernel special */ 1263fdd1b945SDavid Howells if (error <= 0 || 1264fdd1b945SDavid Howells error >= MAX_ERRNO || 1265fdd1b945SDavid Howells error == ERESTARTSYS || 1266fdd1b945SDavid Howells error == ERESTARTNOINTR || 1267fdd1b945SDavid Howells error == ERESTARTNOHAND || 1268fdd1b945SDavid Howells error == ERESTART_RESTARTBLOCK) 1269fdd1b945SDavid Howells return -EINVAL; 1270d84f4f99SDavid Howells 1271b5f545c8SDavid Howells /* the appropriate instantiation authorisation key must have been 1272b5f545c8SDavid Howells * assumed before calling this */ 1273b5f545c8SDavid Howells ret = -EPERM; 1274d84f4f99SDavid Howells instkey = cred->request_key_auth; 1275b5f545c8SDavid Howells if (!instkey) 12761da177e4SLinus Torvalds goto error; 12771da177e4SLinus Torvalds 1278146aa8b1SDavid Howells rka = instkey->payload.data[0]; 1279b5f545c8SDavid Howells if (rka->target_key->serial != id) 1280b5f545c8SDavid Howells goto error; 12813e30148cSDavid Howells 12821da177e4SLinus Torvalds /* find the destination keyring if present (which must also be 12831da177e4SLinus Torvalds * writable) */ 12848bbf4976SDavid Howells ret = get_instantiation_keyring(ringid, rka, &dest_keyring); 12858bbf4976SDavid Howells if (ret < 0) 1286b5f545c8SDavid Howells goto error; 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds /* instantiate the key and link it into a keyring */ 1289fdd1b945SDavid Howells ret = key_reject_and_link(rka->target_key, timeout, error, 12908bbf4976SDavid Howells dest_keyring, instkey); 12911da177e4SLinus Torvalds 12928bbf4976SDavid Howells key_put(dest_keyring); 1293b5f545c8SDavid Howells 1294b5f545c8SDavid Howells /* discard the assumed authority if it's just been disabled by 1295b5f545c8SDavid Howells * instantiation of the key */ 1296d84f4f99SDavid Howells if (ret == 0) 1297d84f4f99SDavid Howells keyctl_change_reqkey_auth(NULL); 1298b5f545c8SDavid Howells 12991da177e4SLinus Torvalds error: 13001da177e4SLinus Torvalds return ret; 1301a8b17ed0SDavid Howells } 13021da177e4SLinus Torvalds 13031da177e4SLinus Torvalds /* 1304973c9f4fSDavid Howells * Read or set the default keyring in which request_key() will cache keys and 1305973c9f4fSDavid Howells * return the old setting. 1306973c9f4fSDavid Howells * 1307c9f838d1SEric Biggers * If a thread or process keyring is specified then it will be created if it 1308c9f838d1SEric Biggers * doesn't yet exist. The old setting will be returned if successful. 13093e30148cSDavid Howells */ 13103e30148cSDavid Howells long keyctl_set_reqkey_keyring(int reqkey_defl) 13113e30148cSDavid Howells { 1312d84f4f99SDavid Howells struct cred *new; 1313d84f4f99SDavid Howells int ret, old_setting; 1314d84f4f99SDavid Howells 1315d84f4f99SDavid Howells old_setting = current_cred_xxx(jit_keyring); 1316d84f4f99SDavid Howells 1317d84f4f99SDavid Howells if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE) 1318d84f4f99SDavid Howells return old_setting; 1319d84f4f99SDavid Howells 1320d84f4f99SDavid Howells new = prepare_creds(); 1321d84f4f99SDavid Howells if (!new) 1322d84f4f99SDavid Howells return -ENOMEM; 13233e30148cSDavid Howells 13243e30148cSDavid Howells switch (reqkey_defl) { 13253e30148cSDavid Howells case KEY_REQKEY_DEFL_THREAD_KEYRING: 1326d84f4f99SDavid Howells ret = install_thread_keyring_to_cred(new); 13273e30148cSDavid Howells if (ret < 0) 1328d84f4f99SDavid Howells goto error; 13293e30148cSDavid Howells goto set; 13303e30148cSDavid Howells 13313e30148cSDavid Howells case KEY_REQKEY_DEFL_PROCESS_KEYRING: 1332d84f4f99SDavid Howells ret = install_process_keyring_to_cred(new); 1333c9f838d1SEric Biggers if (ret < 0) 1334d84f4f99SDavid Howells goto error; 1335d84f4f99SDavid Howells goto set; 13363e30148cSDavid Howells 13373e30148cSDavid Howells case KEY_REQKEY_DEFL_DEFAULT: 13383e30148cSDavid Howells case KEY_REQKEY_DEFL_SESSION_KEYRING: 13393e30148cSDavid Howells case KEY_REQKEY_DEFL_USER_KEYRING: 13403e30148cSDavid Howells case KEY_REQKEY_DEFL_USER_SESSION_KEYRING: 1341d84f4f99SDavid Howells case KEY_REQKEY_DEFL_REQUESTOR_KEYRING: 1342d84f4f99SDavid Howells goto set; 13433e30148cSDavid Howells 13443e30148cSDavid Howells case KEY_REQKEY_DEFL_NO_CHANGE: 13453e30148cSDavid Howells case KEY_REQKEY_DEFL_GROUP_KEYRING: 13463e30148cSDavid Howells default: 1347d84f4f99SDavid Howells ret = -EINVAL; 1348d84f4f99SDavid Howells goto error; 13493e30148cSDavid Howells } 13503e30148cSDavid Howells 1351d84f4f99SDavid Howells set: 1352d84f4f99SDavid Howells new->jit_keyring = reqkey_defl; 1353d84f4f99SDavid Howells commit_creds(new); 1354d84f4f99SDavid Howells return old_setting; 1355d84f4f99SDavid Howells error: 1356d84f4f99SDavid Howells abort_creds(new); 13574303ef19SDan Carpenter return ret; 1358a8b17ed0SDavid Howells } 1359d84f4f99SDavid Howells 13603e30148cSDavid Howells /* 1361973c9f4fSDavid Howells * Set or clear the timeout on a key. 1362973c9f4fSDavid Howells * 1363973c9f4fSDavid Howells * Either the key must grant the caller Setattr permission or else the caller 1364973c9f4fSDavid Howells * must hold an instantiation authorisation token for the key. 1365973c9f4fSDavid Howells * 1366973c9f4fSDavid Howells * The timeout is either 0 to clear the timeout, or a number of seconds from 1367973c9f4fSDavid Howells * the current time. The key and any links to the key will be automatically 1368973c9f4fSDavid Howells * garbage collected after the timeout expires. 1369973c9f4fSDavid Howells * 1370d3600bcfSMimi Zohar * Keys with KEY_FLAG_KEEP set should not be timed out. 1371d3600bcfSMimi Zohar * 1372973c9f4fSDavid Howells * If successful, 0 is returned. 1373017679c4SDavid Howells */ 1374017679c4SDavid Howells long keyctl_set_timeout(key_serial_t id, unsigned timeout) 1375017679c4SDavid Howells { 13769156235bSDavid Howells struct key *key, *instkey; 1377017679c4SDavid Howells key_ref_t key_ref; 1378017679c4SDavid Howells long ret; 1379017679c4SDavid Howells 13805593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 1381f5895943SDavid Howells KEY_NEED_SETATTR); 1382017679c4SDavid Howells if (IS_ERR(key_ref)) { 13839156235bSDavid Howells /* setting the timeout on a key under construction is permitted 13849156235bSDavid Howells * if we have the authorisation token handy */ 13859156235bSDavid Howells if (PTR_ERR(key_ref) == -EACCES) { 13869156235bSDavid Howells instkey = key_get_instantiation_authkey(id); 13879156235bSDavid Howells if (!IS_ERR(instkey)) { 13889156235bSDavid Howells key_put(instkey); 13899156235bSDavid Howells key_ref = lookup_user_key(id, 13909156235bSDavid Howells KEY_LOOKUP_PARTIAL, 13919156235bSDavid Howells 0); 13929156235bSDavid Howells if (!IS_ERR(key_ref)) 13939156235bSDavid Howells goto okay; 13949156235bSDavid Howells } 13959156235bSDavid Howells } 13969156235bSDavid Howells 1397017679c4SDavid Howells ret = PTR_ERR(key_ref); 1398017679c4SDavid Howells goto error; 1399017679c4SDavid Howells } 1400017679c4SDavid Howells 14019156235bSDavid Howells okay: 1402017679c4SDavid Howells key = key_ref_to_ptr(key_ref); 14031d6d167cSMimi Zohar ret = 0; 1404d3600bcfSMimi Zohar if (test_bit(KEY_FLAG_KEEP, &key->flags)) 1405d3600bcfSMimi Zohar ret = -EPERM; 14061d6d167cSMimi Zohar else 140759e6b9c1SBryan Schumaker key_set_timeout(key, timeout); 1408017679c4SDavid Howells key_put(key); 1409017679c4SDavid Howells 1410017679c4SDavid Howells error: 1411017679c4SDavid Howells return ret; 1412a8b17ed0SDavid Howells } 1413017679c4SDavid Howells 1414017679c4SDavid Howells /* 1415973c9f4fSDavid Howells * Assume (or clear) the authority to instantiate the specified key. 1416973c9f4fSDavid Howells * 1417973c9f4fSDavid Howells * This sets the authoritative token currently in force for key instantiation. 1418973c9f4fSDavid Howells * This must be done for a key to be instantiated. It has the effect of making 1419973c9f4fSDavid Howells * available all the keys from the caller of the request_key() that created a 1420973c9f4fSDavid Howells * key to request_key() calls made by the caller of this function. 1421973c9f4fSDavid Howells * 1422973c9f4fSDavid Howells * The caller must have the instantiation key in their process keyrings with a 1423973c9f4fSDavid Howells * Search permission grant available to the caller. 1424973c9f4fSDavid Howells * 1425973c9f4fSDavid Howells * If the ID given is 0, then the setting will be cleared and 0 returned. 1426973c9f4fSDavid Howells * 1427973c9f4fSDavid Howells * If the ID given has a matching an authorisation key, then that key will be 1428973c9f4fSDavid Howells * set and its ID will be returned. The authorisation key can be read to get 1429973c9f4fSDavid Howells * the callout information passed to request_key(). 1430b5f545c8SDavid Howells */ 1431b5f545c8SDavid Howells long keyctl_assume_authority(key_serial_t id) 1432b5f545c8SDavid Howells { 1433b5f545c8SDavid Howells struct key *authkey; 1434b5f545c8SDavid Howells long ret; 1435b5f545c8SDavid Howells 1436b5f545c8SDavid Howells /* special key IDs aren't permitted */ 1437b5f545c8SDavid Howells ret = -EINVAL; 1438b5f545c8SDavid Howells if (id < 0) 1439b5f545c8SDavid Howells goto error; 1440b5f545c8SDavid Howells 1441b5f545c8SDavid Howells /* we divest ourselves of authority if given an ID of 0 */ 1442b5f545c8SDavid Howells if (id == 0) { 1443d84f4f99SDavid Howells ret = keyctl_change_reqkey_auth(NULL); 1444b5f545c8SDavid Howells goto error; 1445b5f545c8SDavid Howells } 1446b5f545c8SDavid Howells 1447b5f545c8SDavid Howells /* attempt to assume the authority temporarily granted to us whilst we 1448b5f545c8SDavid Howells * instantiate the specified key 1449b5f545c8SDavid Howells * - the authorisation key must be in the current task's keyrings 1450b5f545c8SDavid Howells * somewhere 1451b5f545c8SDavid Howells */ 1452b5f545c8SDavid Howells authkey = key_get_instantiation_authkey(id); 1453b5f545c8SDavid Howells if (IS_ERR(authkey)) { 1454b5f545c8SDavid Howells ret = PTR_ERR(authkey); 1455b5f545c8SDavid Howells goto error; 1456b5f545c8SDavid Howells } 1457b5f545c8SDavid Howells 1458d84f4f99SDavid Howells ret = keyctl_change_reqkey_auth(authkey); 1459884bee02SEric Biggers if (ret == 0) 1460d84f4f99SDavid Howells ret = authkey->serial; 1461884bee02SEric Biggers key_put(authkey); 1462b5f545c8SDavid Howells error: 1463b5f545c8SDavid Howells return ret; 1464a8b17ed0SDavid Howells } 1465b5f545c8SDavid Howells 146670a5bb72SDavid Howells /* 1467973c9f4fSDavid Howells * Get a key's the LSM security label. 1468973c9f4fSDavid Howells * 1469973c9f4fSDavid Howells * The key must grant the caller View permission for this to work. 1470973c9f4fSDavid Howells * 1471973c9f4fSDavid Howells * If there's a buffer, then up to buflen bytes of data will be placed into it. 1472973c9f4fSDavid Howells * 1473973c9f4fSDavid Howells * If successful, the amount of information available will be returned, 1474973c9f4fSDavid Howells * irrespective of how much was copied (including the terminal NUL). 147570a5bb72SDavid Howells */ 147670a5bb72SDavid Howells long keyctl_get_security(key_serial_t keyid, 147770a5bb72SDavid Howells char __user *buffer, 147870a5bb72SDavid Howells size_t buflen) 147970a5bb72SDavid Howells { 148070a5bb72SDavid Howells struct key *key, *instkey; 148170a5bb72SDavid Howells key_ref_t key_ref; 148270a5bb72SDavid Howells char *context; 148370a5bb72SDavid Howells long ret; 148470a5bb72SDavid Howells 1485f5895943SDavid Howells key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW); 148670a5bb72SDavid Howells if (IS_ERR(key_ref)) { 148770a5bb72SDavid Howells if (PTR_ERR(key_ref) != -EACCES) 148870a5bb72SDavid Howells return PTR_ERR(key_ref); 148970a5bb72SDavid Howells 149070a5bb72SDavid Howells /* viewing a key under construction is also permitted if we 149170a5bb72SDavid Howells * have the authorisation token handy */ 149270a5bb72SDavid Howells instkey = key_get_instantiation_authkey(keyid); 149370a5bb72SDavid Howells if (IS_ERR(instkey)) 1494fa1cc7b5SRoel Kluin return PTR_ERR(instkey); 149570a5bb72SDavid Howells key_put(instkey); 149670a5bb72SDavid Howells 14975593122eSDavid Howells key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0); 149870a5bb72SDavid Howells if (IS_ERR(key_ref)) 149970a5bb72SDavid Howells return PTR_ERR(key_ref); 150070a5bb72SDavid Howells } 150170a5bb72SDavid Howells 150270a5bb72SDavid Howells key = key_ref_to_ptr(key_ref); 150370a5bb72SDavid Howells ret = security_key_getsecurity(key, &context); 150470a5bb72SDavid Howells if (ret == 0) { 150570a5bb72SDavid Howells /* if no information was returned, give userspace an empty 150670a5bb72SDavid Howells * string */ 150770a5bb72SDavid Howells ret = 1; 150870a5bb72SDavid Howells if (buffer && buflen > 0 && 150970a5bb72SDavid Howells copy_to_user(buffer, "", 1) != 0) 151070a5bb72SDavid Howells ret = -EFAULT; 151170a5bb72SDavid Howells } else if (ret > 0) { 151270a5bb72SDavid Howells /* return as much data as there's room for */ 151370a5bb72SDavid Howells if (buffer && buflen > 0) { 151470a5bb72SDavid Howells if (buflen > ret) 151570a5bb72SDavid Howells buflen = ret; 151670a5bb72SDavid Howells 151770a5bb72SDavid Howells if (copy_to_user(buffer, context, buflen) != 0) 151870a5bb72SDavid Howells ret = -EFAULT; 151970a5bb72SDavid Howells } 152070a5bb72SDavid Howells 152170a5bb72SDavid Howells kfree(context); 152270a5bb72SDavid Howells } 152370a5bb72SDavid Howells 152470a5bb72SDavid Howells key_ref_put(key_ref); 152570a5bb72SDavid Howells return ret; 152670a5bb72SDavid Howells } 152770a5bb72SDavid Howells 1528ee18d64cSDavid Howells /* 1529973c9f4fSDavid Howells * Attempt to install the calling process's session keyring on the process's 1530973c9f4fSDavid Howells * parent process. 1531973c9f4fSDavid Howells * 1532973c9f4fSDavid Howells * The keyring must exist and must grant the caller LINK permission, and the 1533973c9f4fSDavid Howells * parent process must be single-threaded and must have the same effective 1534973c9f4fSDavid Howells * ownership as this process and mustn't be SUID/SGID. 1535973c9f4fSDavid Howells * 1536973c9f4fSDavid Howells * The keyring will be emplaced on the parent when it next resumes userspace. 1537973c9f4fSDavid Howells * 1538973c9f4fSDavid Howells * If successful, 0 will be returned. 1539ee18d64cSDavid Howells */ 1540ee18d64cSDavid Howells long keyctl_session_to_parent(void) 1541ee18d64cSDavid Howells { 1542ee18d64cSDavid Howells struct task_struct *me, *parent; 1543ee18d64cSDavid Howells const struct cred *mycred, *pcred; 154467d12145SAl Viro struct callback_head *newwork, *oldwork; 1545ee18d64cSDavid Howells key_ref_t keyring_r; 1546413cd3d9SOleg Nesterov struct cred *cred; 1547ee18d64cSDavid Howells int ret; 1548ee18d64cSDavid Howells 1549f5895943SDavid Howells keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK); 1550ee18d64cSDavid Howells if (IS_ERR(keyring_r)) 1551ee18d64cSDavid Howells return PTR_ERR(keyring_r); 1552ee18d64cSDavid Howells 1553413cd3d9SOleg Nesterov ret = -ENOMEM; 1554413cd3d9SOleg Nesterov 1555ee18d64cSDavid Howells /* our parent is going to need a new cred struct, a new tgcred struct 1556ee18d64cSDavid Howells * and new security data, so we allocate them here to prevent ENOMEM in 1557ee18d64cSDavid Howells * our parent */ 1558ee18d64cSDavid Howells cred = cred_alloc_blank(); 1559ee18d64cSDavid Howells if (!cred) 156067d12145SAl Viro goto error_keyring; 156167d12145SAl Viro newwork = &cred->rcu; 1562ee18d64cSDavid Howells 15633a50597dSDavid Howells cred->session_keyring = key_ref_to_ptr(keyring_r); 15643a50597dSDavid Howells keyring_r = NULL; 156567d12145SAl Viro init_task_work(newwork, key_change_session_keyring); 1566ee18d64cSDavid Howells 1567ee18d64cSDavid Howells me = current; 15689d1ac65aSDavid Howells rcu_read_lock(); 1569ee18d64cSDavid Howells write_lock_irq(&tasklist_lock); 1570ee18d64cSDavid Howells 1571ee18d64cSDavid Howells ret = -EPERM; 1572413cd3d9SOleg Nesterov oldwork = NULL; 15737936d16dSDavid Howells parent = rcu_dereference_protected(me->real_parent, 15747936d16dSDavid Howells lockdep_is_held(&tasklist_lock)); 1575ee18d64cSDavid Howells 1576ee18d64cSDavid Howells /* the parent mustn't be init and mustn't be a kernel thread */ 1577ee18d64cSDavid Howells if (parent->pid <= 1 || !parent->mm) 1578413cd3d9SOleg Nesterov goto unlock; 1579ee18d64cSDavid Howells 1580ee18d64cSDavid Howells /* the parent must be single threaded */ 1581dd98acf7SOleg Nesterov if (!thread_group_empty(parent)) 1582413cd3d9SOleg Nesterov goto unlock; 1583ee18d64cSDavid Howells 1584ee18d64cSDavid Howells /* the parent and the child must have different session keyrings or 1585ee18d64cSDavid Howells * there's no point */ 1586ee18d64cSDavid Howells mycred = current_cred(); 1587ee18d64cSDavid Howells pcred = __task_cred(parent); 1588ee18d64cSDavid Howells if (mycred == pcred || 15893a50597dSDavid Howells mycred->session_keyring == pcred->session_keyring) { 1590413cd3d9SOleg Nesterov ret = 0; 1591413cd3d9SOleg Nesterov goto unlock; 1592413cd3d9SOleg Nesterov } 1593ee18d64cSDavid Howells 1594ee18d64cSDavid Howells /* the parent must have the same effective ownership and mustn't be 1595ee18d64cSDavid Howells * SUID/SGID */ 15969a56c2dbSEric W. Biederman if (!uid_eq(pcred->uid, mycred->euid) || 15979a56c2dbSEric W. Biederman !uid_eq(pcred->euid, mycred->euid) || 15989a56c2dbSEric W. Biederman !uid_eq(pcred->suid, mycred->euid) || 15999a56c2dbSEric W. Biederman !gid_eq(pcred->gid, mycred->egid) || 16009a56c2dbSEric W. Biederman !gid_eq(pcred->egid, mycred->egid) || 16019a56c2dbSEric W. Biederman !gid_eq(pcred->sgid, mycred->egid)) 1602413cd3d9SOleg Nesterov goto unlock; 1603ee18d64cSDavid Howells 1604ee18d64cSDavid Howells /* the keyrings must have the same UID */ 16053a50597dSDavid Howells if ((pcred->session_keyring && 16062a74dbb9SLinus Torvalds !uid_eq(pcred->session_keyring->uid, mycred->euid)) || 16072a74dbb9SLinus Torvalds !uid_eq(mycred->session_keyring->uid, mycred->euid)) 1608413cd3d9SOleg Nesterov goto unlock; 1609ee18d64cSDavid Howells 1610413cd3d9SOleg Nesterov /* cancel an already pending keyring replacement */ 1611413cd3d9SOleg Nesterov oldwork = task_work_cancel(parent, key_change_session_keyring); 1612ee18d64cSDavid Howells 1613ee18d64cSDavid Howells /* the replacement session keyring is applied just prior to userspace 1614ee18d64cSDavid Howells * restarting */ 161567d12145SAl Viro ret = task_work_add(parent, newwork, true); 1616413cd3d9SOleg Nesterov if (!ret) 1617413cd3d9SOleg Nesterov newwork = NULL; 1618413cd3d9SOleg Nesterov unlock: 1619ee18d64cSDavid Howells write_unlock_irq(&tasklist_lock); 16209d1ac65aSDavid Howells rcu_read_unlock(); 162167d12145SAl Viro if (oldwork) 162267d12145SAl Viro put_cred(container_of(oldwork, struct cred, rcu)); 162367d12145SAl Viro if (newwork) 162467d12145SAl Viro put_cred(cred); 1625ee18d64cSDavid Howells return ret; 1626ee18d64cSDavid Howells 1627ee18d64cSDavid Howells error_keyring: 1628ee18d64cSDavid Howells key_ref_put(keyring_r); 1629ee18d64cSDavid Howells return ret; 1630ee18d64cSDavid Howells } 1631ee18d64cSDavid Howells 1632b5f545c8SDavid Howells /* 16336563c91fSMat Martineau * Apply a restriction to a given keyring. 16346563c91fSMat Martineau * 16356563c91fSMat Martineau * The caller must have Setattr permission to change keyring restrictions. 16366563c91fSMat Martineau * 16376563c91fSMat Martineau * The requested type name may be a NULL pointer to reject all attempts 163818026d86SEric Biggers * to link to the keyring. In this case, _restriction must also be NULL. 163918026d86SEric Biggers * Otherwise, both _type and _restriction must be non-NULL. 16406563c91fSMat Martineau * 16416563c91fSMat Martineau * Returns 0 if successful. 16426563c91fSMat Martineau */ 16436563c91fSMat Martineau long keyctl_restrict_keyring(key_serial_t id, const char __user *_type, 16446563c91fSMat Martineau const char __user *_restriction) 16456563c91fSMat Martineau { 16466563c91fSMat Martineau key_ref_t key_ref; 16476563c91fSMat Martineau char type[32]; 16486563c91fSMat Martineau char *restriction = NULL; 16496563c91fSMat Martineau long ret; 16506563c91fSMat Martineau 16516563c91fSMat Martineau key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR); 16526563c91fSMat Martineau if (IS_ERR(key_ref)) 16536563c91fSMat Martineau return PTR_ERR(key_ref); 16546563c91fSMat Martineau 165518026d86SEric Biggers ret = -EINVAL; 16566563c91fSMat Martineau if (_type) { 165718026d86SEric Biggers if (!_restriction) 165818026d86SEric Biggers goto error; 165918026d86SEric Biggers 16606563c91fSMat Martineau ret = key_get_type_from_user(type, _type, sizeof(type)); 16616563c91fSMat Martineau if (ret < 0) 16626563c91fSMat Martineau goto error; 16636563c91fSMat Martineau 16646563c91fSMat Martineau restriction = strndup_user(_restriction, PAGE_SIZE); 16656563c91fSMat Martineau if (IS_ERR(restriction)) { 16666563c91fSMat Martineau ret = PTR_ERR(restriction); 16676563c91fSMat Martineau goto error; 16686563c91fSMat Martineau } 166918026d86SEric Biggers } else { 167018026d86SEric Biggers if (_restriction) 167118026d86SEric Biggers goto error; 16726563c91fSMat Martineau } 16736563c91fSMat Martineau 167418026d86SEric Biggers ret = keyring_restrict(key_ref, _type ? type : NULL, restriction); 16756563c91fSMat Martineau kfree(restriction); 16766563c91fSMat Martineau error: 16776563c91fSMat Martineau key_ref_put(key_ref); 16786563c91fSMat Martineau return ret; 16796563c91fSMat Martineau } 16806563c91fSMat Martineau 16816563c91fSMat Martineau /* 1682973c9f4fSDavid Howells * The key control system call 16831da177e4SLinus Torvalds */ 1684938bb9f5SHeiko Carstens SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3, 1685938bb9f5SHeiko Carstens unsigned long, arg4, unsigned long, arg5) 16861da177e4SLinus Torvalds { 16871da177e4SLinus Torvalds switch (option) { 16881da177e4SLinus Torvalds case KEYCTL_GET_KEYRING_ID: 16891da177e4SLinus Torvalds return keyctl_get_keyring_ID((key_serial_t) arg2, 16901da177e4SLinus Torvalds (int) arg3); 16911da177e4SLinus Torvalds 16921da177e4SLinus Torvalds case KEYCTL_JOIN_SESSION_KEYRING: 16931da177e4SLinus Torvalds return keyctl_join_session_keyring((const char __user *) arg2); 16941da177e4SLinus Torvalds 16951da177e4SLinus Torvalds case KEYCTL_UPDATE: 16961da177e4SLinus Torvalds return keyctl_update_key((key_serial_t) arg2, 16971da177e4SLinus Torvalds (const void __user *) arg3, 16981da177e4SLinus Torvalds (size_t) arg4); 16991da177e4SLinus Torvalds 17001da177e4SLinus Torvalds case KEYCTL_REVOKE: 17011da177e4SLinus Torvalds return keyctl_revoke_key((key_serial_t) arg2); 17021da177e4SLinus Torvalds 17031da177e4SLinus Torvalds case KEYCTL_DESCRIBE: 17041da177e4SLinus Torvalds return keyctl_describe_key((key_serial_t) arg2, 17051da177e4SLinus Torvalds (char __user *) arg3, 17061da177e4SLinus Torvalds (unsigned) arg4); 17071da177e4SLinus Torvalds 17081da177e4SLinus Torvalds case KEYCTL_CLEAR: 17091da177e4SLinus Torvalds return keyctl_keyring_clear((key_serial_t) arg2); 17101da177e4SLinus Torvalds 17111da177e4SLinus Torvalds case KEYCTL_LINK: 17121da177e4SLinus Torvalds return keyctl_keyring_link((key_serial_t) arg2, 17131da177e4SLinus Torvalds (key_serial_t) arg3); 17141da177e4SLinus Torvalds 17151da177e4SLinus Torvalds case KEYCTL_UNLINK: 17161da177e4SLinus Torvalds return keyctl_keyring_unlink((key_serial_t) arg2, 17171da177e4SLinus Torvalds (key_serial_t) arg3); 17181da177e4SLinus Torvalds 17191da177e4SLinus Torvalds case KEYCTL_SEARCH: 17201da177e4SLinus Torvalds return keyctl_keyring_search((key_serial_t) arg2, 17211da177e4SLinus Torvalds (const char __user *) arg3, 17221da177e4SLinus Torvalds (const char __user *) arg4, 17231da177e4SLinus Torvalds (key_serial_t) arg5); 17241da177e4SLinus Torvalds 17251da177e4SLinus Torvalds case KEYCTL_READ: 17261da177e4SLinus Torvalds return keyctl_read_key((key_serial_t) arg2, 17271da177e4SLinus Torvalds (char __user *) arg3, 17281da177e4SLinus Torvalds (size_t) arg4); 17291da177e4SLinus Torvalds 17301da177e4SLinus Torvalds case KEYCTL_CHOWN: 17311da177e4SLinus Torvalds return keyctl_chown_key((key_serial_t) arg2, 17321da177e4SLinus Torvalds (uid_t) arg3, 17331da177e4SLinus Torvalds (gid_t) arg4); 17341da177e4SLinus Torvalds 17351da177e4SLinus Torvalds case KEYCTL_SETPERM: 17361da177e4SLinus Torvalds return keyctl_setperm_key((key_serial_t) arg2, 17371da177e4SLinus Torvalds (key_perm_t) arg3); 17381da177e4SLinus Torvalds 17391da177e4SLinus Torvalds case KEYCTL_INSTANTIATE: 17401da177e4SLinus Torvalds return keyctl_instantiate_key((key_serial_t) arg2, 17411da177e4SLinus Torvalds (const void __user *) arg3, 17421da177e4SLinus Torvalds (size_t) arg4, 17431da177e4SLinus Torvalds (key_serial_t) arg5); 17441da177e4SLinus Torvalds 17451da177e4SLinus Torvalds case KEYCTL_NEGATE: 17461da177e4SLinus Torvalds return keyctl_negate_key((key_serial_t) arg2, 17471da177e4SLinus Torvalds (unsigned) arg3, 17481da177e4SLinus Torvalds (key_serial_t) arg4); 17491da177e4SLinus Torvalds 17503e30148cSDavid Howells case KEYCTL_SET_REQKEY_KEYRING: 17513e30148cSDavid Howells return keyctl_set_reqkey_keyring(arg2); 17523e30148cSDavid Howells 1753017679c4SDavid Howells case KEYCTL_SET_TIMEOUT: 1754017679c4SDavid Howells return keyctl_set_timeout((key_serial_t) arg2, 1755017679c4SDavid Howells (unsigned) arg3); 1756017679c4SDavid Howells 1757b5f545c8SDavid Howells case KEYCTL_ASSUME_AUTHORITY: 1758b5f545c8SDavid Howells return keyctl_assume_authority((key_serial_t) arg2); 1759b5f545c8SDavid Howells 176070a5bb72SDavid Howells case KEYCTL_GET_SECURITY: 176170a5bb72SDavid Howells return keyctl_get_security((key_serial_t) arg2, 176290bd49abSJames Morris (char __user *) arg3, 176370a5bb72SDavid Howells (size_t) arg4); 176470a5bb72SDavid Howells 1765ee18d64cSDavid Howells case KEYCTL_SESSION_TO_PARENT: 1766ee18d64cSDavid Howells return keyctl_session_to_parent(); 1767ee18d64cSDavid Howells 1768fdd1b945SDavid Howells case KEYCTL_REJECT: 1769fdd1b945SDavid Howells return keyctl_reject_key((key_serial_t) arg2, 1770fdd1b945SDavid Howells (unsigned) arg3, 1771fdd1b945SDavid Howells (unsigned) arg4, 1772fdd1b945SDavid Howells (key_serial_t) arg5); 1773fdd1b945SDavid Howells 1774ee009e4aSDavid Howells case KEYCTL_INSTANTIATE_IOV: 1775ee009e4aSDavid Howells return keyctl_instantiate_key_iov( 1776ee009e4aSDavid Howells (key_serial_t) arg2, 1777ee009e4aSDavid Howells (const struct iovec __user *) arg3, 1778ee009e4aSDavid Howells (unsigned) arg4, 1779ee009e4aSDavid Howells (key_serial_t) arg5); 1780ee009e4aSDavid Howells 1781fd75815fSDavid Howells case KEYCTL_INVALIDATE: 1782fd75815fSDavid Howells return keyctl_invalidate_key((key_serial_t) arg2); 1783fd75815fSDavid Howells 1784f36f8c75SDavid Howells case KEYCTL_GET_PERSISTENT: 1785f36f8c75SDavid Howells return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3); 1786f36f8c75SDavid Howells 1787ddbb4114SMat Martineau case KEYCTL_DH_COMPUTE: 1788ddbb4114SMat Martineau return keyctl_dh_compute((struct keyctl_dh_params __user *) arg2, 17894693fc73SStephan Mueller (char __user *) arg3, (size_t) arg4, 1790f1c316a3SStephan Mueller (struct keyctl_kdf_params __user *) arg5); 1791ddbb4114SMat Martineau 17926563c91fSMat Martineau case KEYCTL_RESTRICT_KEYRING: 17936563c91fSMat Martineau return keyctl_restrict_keyring((key_serial_t) arg2, 17946563c91fSMat Martineau (const char __user *) arg3, 17956563c91fSMat Martineau (const char __user *) arg4); 17961da177e4SLinus Torvalds 179700d60fd3SDavid Howells case KEYCTL_PKEY_QUERY: 179800d60fd3SDavid Howells if (arg3 != 0) 179900d60fd3SDavid Howells return -EINVAL; 180000d60fd3SDavid Howells return keyctl_pkey_query((key_serial_t)arg2, 180100d60fd3SDavid Howells (const char __user *)arg4, 1802468e91ceSBen Dooks (struct keyctl_pkey_query __user *)arg5); 180300d60fd3SDavid Howells 180400d60fd3SDavid Howells case KEYCTL_PKEY_ENCRYPT: 180500d60fd3SDavid Howells case KEYCTL_PKEY_DECRYPT: 180600d60fd3SDavid Howells case KEYCTL_PKEY_SIGN: 180700d60fd3SDavid Howells return keyctl_pkey_e_d_s( 180800d60fd3SDavid Howells option, 180900d60fd3SDavid Howells (const struct keyctl_pkey_params __user *)arg2, 181000d60fd3SDavid Howells (const char __user *)arg3, 181100d60fd3SDavid Howells (const void __user *)arg4, 181200d60fd3SDavid Howells (void __user *)arg5); 181300d60fd3SDavid Howells 181400d60fd3SDavid Howells case KEYCTL_PKEY_VERIFY: 181500d60fd3SDavid Howells return keyctl_pkey_verify( 181600d60fd3SDavid Howells (const struct keyctl_pkey_params __user *)arg2, 181700d60fd3SDavid Howells (const char __user *)arg3, 181800d60fd3SDavid Howells (const void __user *)arg4, 181900d60fd3SDavid Howells (const void __user *)arg5); 182000d60fd3SDavid Howells 1821*ed0ac5c7SDavid Howells case KEYCTL_MOVE: 1822*ed0ac5c7SDavid Howells return keyctl_keyring_move((key_serial_t)arg2, 1823*ed0ac5c7SDavid Howells (key_serial_t)arg3, 1824*ed0ac5c7SDavid Howells (key_serial_t)arg4, 1825*ed0ac5c7SDavid Howells (unsigned int)arg5); 1826*ed0ac5c7SDavid Howells 18271da177e4SLinus Torvalds default: 18281da177e4SLinus Torvalds return -EOPNOTSUPP; 18291da177e4SLinus Torvalds } 18301da177e4SLinus Torvalds } 1831