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/module.h> 131da177e4SLinus Torvalds #include <linux/init.h> 141da177e4SLinus Torvalds #include <linux/sched.h> 151da177e4SLinus Torvalds #include <linux/slab.h> 161da177e4SLinus Torvalds #include <linux/syscalls.h> 1759e6b9c1SBryan Schumaker #include <linux/key.h> 181da177e4SLinus Torvalds #include <linux/keyctl.h> 191da177e4SLinus Torvalds #include <linux/fs.h> 20c59ede7bSRandy.Dunlap #include <linux/capability.h> 210cb409d9SDavi Arnaut #include <linux/string.h> 221da177e4SLinus Torvalds #include <linux/err.h> 2338bbca6bSDavid Howells #include <linux/vmalloc.h> 2470a5bb72SDavid Howells #include <linux/security.h> 25a27bb332SKent Overstreet #include <linux/uio.h> 261da177e4SLinus Torvalds #include <asm/uaccess.h> 271da177e4SLinus Torvalds #include "internal.h" 281da177e4SLinus Torvalds 29aa9d4437SDavid Howells #define KEY_MAX_DESC_SIZE 4096 30aa9d4437SDavid Howells 310cb409d9SDavi Arnaut static int key_get_type_from_user(char *type, 320cb409d9SDavi Arnaut const char __user *_type, 330cb409d9SDavi Arnaut unsigned len) 340cb409d9SDavi Arnaut { 350cb409d9SDavi Arnaut int ret; 360cb409d9SDavi Arnaut 370cb409d9SDavi Arnaut ret = strncpy_from_user(type, _type, len); 380cb409d9SDavi Arnaut if (ret < 0) 394303ef19SDan Carpenter return ret; 400cb409d9SDavi Arnaut if (ret == 0 || ret >= len) 410cb409d9SDavi Arnaut return -EINVAL; 4254e2c2c1SDavid Howells if (type[0] == '.') 4354e2c2c1SDavid Howells return -EPERM; 440cb409d9SDavi Arnaut type[len - 1] = '\0'; 450cb409d9SDavi Arnaut return 0; 460cb409d9SDavi Arnaut } 470cb409d9SDavi Arnaut 481da177e4SLinus Torvalds /* 49973c9f4fSDavid Howells * Extract the description of a new key from userspace and either add it as a 50973c9f4fSDavid Howells * new key to the specified keyring or update a matching key in that keyring. 51973c9f4fSDavid Howells * 52cf7f601cSDavid Howells * If the description is NULL or an empty string, the key type is asked to 53cf7f601cSDavid Howells * generate one from the payload. 54cf7f601cSDavid Howells * 55973c9f4fSDavid Howells * The keyring must be writable so that we can attach the key to it. 56973c9f4fSDavid Howells * 57973c9f4fSDavid Howells * If successful, the new key's serial number is returned, otherwise an error 58973c9f4fSDavid Howells * code is returned. 591da177e4SLinus Torvalds */ 601e7bfb21SHeiko Carstens SYSCALL_DEFINE5(add_key, const char __user *, _type, 611e7bfb21SHeiko Carstens const char __user *, _description, 621e7bfb21SHeiko Carstens const void __user *, _payload, 631e7bfb21SHeiko Carstens size_t, plen, 641e7bfb21SHeiko Carstens key_serial_t, ringid) 651da177e4SLinus Torvalds { 66664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 671da177e4SLinus Torvalds char type[32], *description; 681da177e4SLinus Torvalds void *payload; 690cb409d9SDavi Arnaut long ret; 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds ret = -EINVAL; 7238bbca6bSDavid Howells if (plen > 1024 * 1024 - 1) 731da177e4SLinus Torvalds goto error; 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds /* draw all the data into kernel space */ 760cb409d9SDavi Arnaut ret = key_get_type_from_user(type, _type, sizeof(type)); 771da177e4SLinus Torvalds if (ret < 0) 781da177e4SLinus Torvalds goto error; 791da177e4SLinus Torvalds 80cf7f601cSDavid Howells description = NULL; 81cf7f601cSDavid Howells if (_description) { 82aa9d4437SDavid Howells description = strndup_user(_description, KEY_MAX_DESC_SIZE); 830cb409d9SDavi Arnaut if (IS_ERR(description)) { 840cb409d9SDavi Arnaut ret = PTR_ERR(description); 853e30148cSDavid Howells goto error; 860cb409d9SDavi Arnaut } 87cf7f601cSDavid Howells if (!*description) { 88cf7f601cSDavid Howells kfree(description); 89cf7f601cSDavid Howells description = NULL; 90a4e3b8d7SMimi Zohar } else if ((description[0] == '.') && 91a4e3b8d7SMimi Zohar (strncmp(type, "keyring", 7) == 0)) { 92a4e3b8d7SMimi Zohar ret = -EPERM; 93a4e3b8d7SMimi Zohar goto error2; 94cf7f601cSDavid Howells } 95cf7f601cSDavid Howells } 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 981da177e4SLinus Torvalds payload = NULL; 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds if (_payload) { 1011da177e4SLinus Torvalds ret = -ENOMEM; 1024f1c28d2SAndrew Morton payload = kmalloc(plen, GFP_KERNEL | __GFP_NOWARN); 10338bbca6bSDavid Howells if (!payload) { 10438bbca6bSDavid Howells if (plen <= PAGE_SIZE) 10538bbca6bSDavid Howells goto error2; 10638bbca6bSDavid Howells payload = vmalloc(plen); 1071da177e4SLinus Torvalds if (!payload) 1081da177e4SLinus Torvalds goto error2; 10938bbca6bSDavid Howells } 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds ret = -EFAULT; 1121da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 1131da177e4SLinus Torvalds goto error3; 1141da177e4SLinus Torvalds } 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds /* find the target keyring (which must be writable) */ 117f5895943SDavid Howells keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); 118664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 119664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 1201da177e4SLinus Torvalds goto error3; 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds /* create or update the requested key and add it to the target 1241da177e4SLinus Torvalds * keyring */ 125664cceb0SDavid Howells key_ref = key_create_or_update(keyring_ref, type, description, 1266b79ccb5SArun Raghavan payload, plen, KEY_PERM_UNDEF, 1276b79ccb5SArun Raghavan KEY_ALLOC_IN_QUOTA); 128664cceb0SDavid Howells if (!IS_ERR(key_ref)) { 129664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 130664cceb0SDavid Howells key_ref_put(key_ref); 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds else { 133664cceb0SDavid Howells ret = PTR_ERR(key_ref); 1341da177e4SLinus Torvalds } 1351da177e4SLinus Torvalds 136664cceb0SDavid Howells key_ref_put(keyring_ref); 1371da177e4SLinus Torvalds error3: 138d0e0eba0SGeliang Tang kvfree(payload); 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 274973c9f4fSDavid Howells * be skipped over. 275973c9f4fSDavid Howells * 276973c9f4fSDavid Howells * If successful, the ID of the joined session keyring will be returned. 2771da177e4SLinus Torvalds */ 2781da177e4SLinus Torvalds long keyctl_join_session_keyring(const char __user *_name) 2791da177e4SLinus Torvalds { 2801da177e4SLinus Torvalds char *name; 2810cb409d9SDavi Arnaut long ret; 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds /* fetch the name from userspace */ 2841da177e4SLinus Torvalds name = NULL; 2851da177e4SLinus Torvalds if (_name) { 286aa9d4437SDavid Howells name = strndup_user(_name, KEY_MAX_DESC_SIZE); 2870cb409d9SDavi Arnaut if (IS_ERR(name)) { 2880cb409d9SDavi Arnaut ret = PTR_ERR(name); 2891da177e4SLinus Torvalds goto error; 2900cb409d9SDavi Arnaut } 2911da177e4SLinus Torvalds } 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds /* join the session */ 2941da177e4SLinus Torvalds ret = join_session_keyring(name); 2950d54ee1cSVegard Nossum kfree(name); 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds error: 2981da177e4SLinus Torvalds return ret; 299a8b17ed0SDavid Howells } 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds /* 302973c9f4fSDavid Howells * Update a key's data payload from the given data. 303973c9f4fSDavid Howells * 304973c9f4fSDavid Howells * The key must grant the caller Write permission and the key type must support 305973c9f4fSDavid Howells * updating for this to work. A negative key can be positively instantiated 306973c9f4fSDavid Howells * with this call. 307973c9f4fSDavid Howells * 308973c9f4fSDavid Howells * If successful, 0 will be returned. If the key type does not support 309973c9f4fSDavid Howells * updating, then -EOPNOTSUPP will be returned. 3101da177e4SLinus Torvalds */ 3111da177e4SLinus Torvalds long keyctl_update_key(key_serial_t id, 3121da177e4SLinus Torvalds const void __user *_payload, 3131da177e4SLinus Torvalds size_t plen) 3141da177e4SLinus Torvalds { 315664cceb0SDavid Howells key_ref_t key_ref; 3161da177e4SLinus Torvalds void *payload; 3171da177e4SLinus Torvalds long ret; 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds ret = -EINVAL; 3201da177e4SLinus Torvalds if (plen > PAGE_SIZE) 3211da177e4SLinus Torvalds goto error; 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 3241da177e4SLinus Torvalds payload = NULL; 3251da177e4SLinus Torvalds if (_payload) { 3261da177e4SLinus Torvalds ret = -ENOMEM; 3271da177e4SLinus Torvalds payload = kmalloc(plen, GFP_KERNEL); 3281da177e4SLinus Torvalds if (!payload) 3291da177e4SLinus Torvalds goto error; 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds ret = -EFAULT; 3321da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 3331da177e4SLinus Torvalds goto error2; 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds /* find the target key (which must be writable) */ 337f5895943SDavid Howells key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE); 338664cceb0SDavid Howells if (IS_ERR(key_ref)) { 339664cceb0SDavid Howells ret = PTR_ERR(key_ref); 3401da177e4SLinus Torvalds goto error2; 3411da177e4SLinus Torvalds } 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds /* update the key */ 344664cceb0SDavid Howells ret = key_update(key_ref, payload, plen); 3451da177e4SLinus Torvalds 346664cceb0SDavid Howells key_ref_put(key_ref); 3471da177e4SLinus Torvalds error2: 3481da177e4SLinus Torvalds kfree(payload); 3491da177e4SLinus Torvalds error: 3501da177e4SLinus Torvalds return ret; 351a8b17ed0SDavid Howells } 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds /* 354973c9f4fSDavid Howells * Revoke a key. 355973c9f4fSDavid Howells * 356973c9f4fSDavid Howells * The key must be grant the caller Write or Setattr permission for this to 357973c9f4fSDavid Howells * work. The key type should give up its quota claim when revoked. The key 358973c9f4fSDavid Howells * and any links to the key will be automatically garbage collected after a 359973c9f4fSDavid Howells * certain amount of time (/proc/sys/kernel/keys/gc_delay). 360973c9f4fSDavid Howells * 361973c9f4fSDavid Howells * If successful, 0 is returned. 3621da177e4SLinus Torvalds */ 3631da177e4SLinus Torvalds long keyctl_revoke_key(key_serial_t id) 3641da177e4SLinus Torvalds { 365664cceb0SDavid Howells key_ref_t key_ref; 3661da177e4SLinus Torvalds long ret; 3671da177e4SLinus Torvalds 368f5895943SDavid Howells key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE); 369664cceb0SDavid Howells if (IS_ERR(key_ref)) { 370664cceb0SDavid Howells ret = PTR_ERR(key_ref); 3710c2c9a3fSDavid Howells if (ret != -EACCES) 3721da177e4SLinus Torvalds goto error; 373f5895943SDavid Howells key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR); 3740c2c9a3fSDavid Howells if (IS_ERR(key_ref)) { 3750c2c9a3fSDavid Howells ret = PTR_ERR(key_ref); 3760c2c9a3fSDavid Howells goto error; 3770c2c9a3fSDavid Howells } 3781da177e4SLinus Torvalds } 3791da177e4SLinus Torvalds 380664cceb0SDavid Howells key_revoke(key_ref_to_ptr(key_ref)); 3811da177e4SLinus Torvalds ret = 0; 3821da177e4SLinus Torvalds 383664cceb0SDavid Howells key_ref_put(key_ref); 3841da177e4SLinus Torvalds error: 3851260f801SDavid Howells return ret; 386a8b17ed0SDavid Howells } 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds /* 389fd75815fSDavid Howells * Invalidate a key. 390fd75815fSDavid Howells * 391fd75815fSDavid Howells * The key must be grant the caller Invalidate permission for this to work. 392fd75815fSDavid Howells * The key and any links to the key will be automatically garbage collected 393fd75815fSDavid Howells * immediately. 394fd75815fSDavid Howells * 395fd75815fSDavid Howells * If successful, 0 is returned. 396fd75815fSDavid Howells */ 397fd75815fSDavid Howells long keyctl_invalidate_key(key_serial_t id) 398fd75815fSDavid Howells { 399fd75815fSDavid Howells key_ref_t key_ref; 400fd75815fSDavid Howells long ret; 401fd75815fSDavid Howells 402fd75815fSDavid Howells kenter("%d", id); 403fd75815fSDavid Howells 404f5895943SDavid Howells key_ref = lookup_user_key(id, 0, KEY_NEED_SEARCH); 405fd75815fSDavid Howells if (IS_ERR(key_ref)) { 406fd75815fSDavid Howells ret = PTR_ERR(key_ref); 4070c7774abSDavid Howells 4080c7774abSDavid Howells /* Root is permitted to invalidate certain special keys */ 4090c7774abSDavid Howells if (capable(CAP_SYS_ADMIN)) { 4100c7774abSDavid Howells key_ref = lookup_user_key(id, 0, 0); 4110c7774abSDavid Howells if (IS_ERR(key_ref)) 4120c7774abSDavid Howells goto error; 4130c7774abSDavid Howells if (test_bit(KEY_FLAG_ROOT_CAN_INVAL, 4140c7774abSDavid Howells &key_ref_to_ptr(key_ref)->flags)) 4150c7774abSDavid Howells goto invalidate; 4160c7774abSDavid Howells goto error_put; 4170c7774abSDavid Howells } 4180c7774abSDavid Howells 419fd75815fSDavid Howells goto error; 420fd75815fSDavid Howells } 421fd75815fSDavid Howells 4220c7774abSDavid Howells invalidate: 423fd75815fSDavid Howells key_invalidate(key_ref_to_ptr(key_ref)); 424fd75815fSDavid Howells ret = 0; 4250c7774abSDavid Howells error_put: 426fd75815fSDavid Howells key_ref_put(key_ref); 427fd75815fSDavid Howells error: 428fd75815fSDavid Howells kleave(" = %ld", ret); 429fd75815fSDavid Howells return ret; 430fd75815fSDavid Howells } 431fd75815fSDavid Howells 432fd75815fSDavid Howells /* 433973c9f4fSDavid Howells * Clear the specified keyring, creating an empty process keyring if one of the 434973c9f4fSDavid Howells * special keyring IDs is used. 435973c9f4fSDavid Howells * 436973c9f4fSDavid Howells * The keyring must grant the caller Write permission for this to work. If 437973c9f4fSDavid Howells * successful, 0 will be returned. 4381da177e4SLinus Torvalds */ 4391da177e4SLinus Torvalds long keyctl_keyring_clear(key_serial_t ringid) 4401da177e4SLinus Torvalds { 441664cceb0SDavid Howells key_ref_t keyring_ref; 4421da177e4SLinus Torvalds long ret; 4431da177e4SLinus Torvalds 444f5895943SDavid Howells keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); 445664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 446664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 447700920ebSDavid Howells 448700920ebSDavid Howells /* Root is permitted to invalidate certain special keyrings */ 449700920ebSDavid Howells if (capable(CAP_SYS_ADMIN)) { 450700920ebSDavid Howells keyring_ref = lookup_user_key(ringid, 0, 0); 451700920ebSDavid Howells if (IS_ERR(keyring_ref)) 452700920ebSDavid Howells goto error; 453700920ebSDavid Howells if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR, 454700920ebSDavid Howells &key_ref_to_ptr(keyring_ref)->flags)) 455700920ebSDavid Howells goto clear; 456700920ebSDavid Howells goto error_put; 457700920ebSDavid Howells } 458700920ebSDavid Howells 4591da177e4SLinus Torvalds goto error; 4601da177e4SLinus Torvalds } 4611da177e4SLinus Torvalds 462700920ebSDavid Howells clear: 463664cceb0SDavid Howells ret = keyring_clear(key_ref_to_ptr(keyring_ref)); 464700920ebSDavid Howells error_put: 465664cceb0SDavid Howells key_ref_put(keyring_ref); 4661da177e4SLinus Torvalds error: 4671da177e4SLinus Torvalds return ret; 468a8b17ed0SDavid Howells } 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds /* 471973c9f4fSDavid Howells * Create a link from a keyring to a key if there's no matching key in the 472973c9f4fSDavid Howells * keyring, otherwise replace the link to the matching key with a link to the 473973c9f4fSDavid Howells * new key. 474973c9f4fSDavid Howells * 475973c9f4fSDavid Howells * The key must grant the caller Link permission and the the keyring must grant 476973c9f4fSDavid Howells * the caller Write permission. Furthermore, if an additional link is created, 477973c9f4fSDavid Howells * the keyring's quota will be extended. 478973c9f4fSDavid Howells * 479973c9f4fSDavid Howells * If successful, 0 will be returned. 4801da177e4SLinus Torvalds */ 4811da177e4SLinus Torvalds long keyctl_keyring_link(key_serial_t id, key_serial_t ringid) 4821da177e4SLinus Torvalds { 483664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 4841da177e4SLinus Torvalds long ret; 4851da177e4SLinus Torvalds 486f5895943SDavid Howells keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); 487664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 488664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 4891da177e4SLinus Torvalds goto error; 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds 492f5895943SDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK); 493664cceb0SDavid Howells if (IS_ERR(key_ref)) { 494664cceb0SDavid Howells ret = PTR_ERR(key_ref); 4951da177e4SLinus Torvalds goto error2; 4961da177e4SLinus Torvalds } 4971da177e4SLinus Torvalds 498664cceb0SDavid Howells ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref)); 4991da177e4SLinus Torvalds 500664cceb0SDavid Howells key_ref_put(key_ref); 5011da177e4SLinus Torvalds error2: 502664cceb0SDavid Howells key_ref_put(keyring_ref); 5031da177e4SLinus Torvalds error: 5041da177e4SLinus Torvalds return ret; 505a8b17ed0SDavid Howells } 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds /* 508973c9f4fSDavid Howells * Unlink a key from a keyring. 509973c9f4fSDavid Howells * 510973c9f4fSDavid Howells * The keyring must grant the caller Write permission for this to work; the key 511973c9f4fSDavid Howells * itself need not grant the caller anything. If the last link to a key is 512973c9f4fSDavid Howells * removed then that key will be scheduled for destruction. 513973c9f4fSDavid Howells * 514973c9f4fSDavid Howells * If successful, 0 will be returned. 5151da177e4SLinus Torvalds */ 5161da177e4SLinus Torvalds long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid) 5171da177e4SLinus Torvalds { 518664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 5191da177e4SLinus Torvalds long ret; 5201da177e4SLinus Torvalds 521f5895943SDavid Howells keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE); 522664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 523664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 5241da177e4SLinus Torvalds goto error; 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds 5275593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0); 528664cceb0SDavid Howells if (IS_ERR(key_ref)) { 529664cceb0SDavid Howells ret = PTR_ERR(key_ref); 5301da177e4SLinus Torvalds goto error2; 5311da177e4SLinus Torvalds } 5321da177e4SLinus Torvalds 533664cceb0SDavid Howells ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref)); 5341da177e4SLinus Torvalds 535664cceb0SDavid Howells key_ref_put(key_ref); 5361da177e4SLinus Torvalds error2: 537664cceb0SDavid Howells key_ref_put(keyring_ref); 5381da177e4SLinus Torvalds error: 5391da177e4SLinus Torvalds return ret; 540a8b17ed0SDavid Howells } 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds /* 543973c9f4fSDavid Howells * Return a description of a key to userspace. 544973c9f4fSDavid Howells * 545973c9f4fSDavid Howells * The key must grant the caller View permission for this to work. 546973c9f4fSDavid Howells * 547973c9f4fSDavid Howells * If there's a buffer, we place up to buflen bytes of data into it formatted 548973c9f4fSDavid Howells * in the following way: 549973c9f4fSDavid Howells * 5501da177e4SLinus Torvalds * type;uid;gid;perm;description<NUL> 551973c9f4fSDavid Howells * 552973c9f4fSDavid Howells * If successful, we return the amount of description available, irrespective 553973c9f4fSDavid Howells * of how much we may have copied into the buffer. 5541da177e4SLinus Torvalds */ 5551da177e4SLinus Torvalds long keyctl_describe_key(key_serial_t keyid, 5561da177e4SLinus Torvalds char __user *buffer, 5571da177e4SLinus Torvalds size_t buflen) 5581da177e4SLinus Torvalds { 5593e30148cSDavid Howells struct key *key, *instkey; 560664cceb0SDavid Howells key_ref_t key_ref; 561aa9d4437SDavid Howells char *infobuf; 5621da177e4SLinus Torvalds long ret; 563aa9d4437SDavid Howells int desclen, infolen; 5641da177e4SLinus Torvalds 565f5895943SDavid Howells key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW); 566664cceb0SDavid Howells if (IS_ERR(key_ref)) { 5673e30148cSDavid Howells /* viewing a key under construction is permitted if we have the 5683e30148cSDavid Howells * authorisation token handy */ 569664cceb0SDavid Howells if (PTR_ERR(key_ref) == -EACCES) { 5703e30148cSDavid Howells instkey = key_get_instantiation_authkey(keyid); 5713e30148cSDavid Howells if (!IS_ERR(instkey)) { 5723e30148cSDavid Howells key_put(instkey); 5738bbf4976SDavid Howells key_ref = lookup_user_key(keyid, 5745593122eSDavid Howells KEY_LOOKUP_PARTIAL, 5755593122eSDavid Howells 0); 576664cceb0SDavid Howells if (!IS_ERR(key_ref)) 5773e30148cSDavid Howells goto okay; 5783e30148cSDavid Howells } 5793e30148cSDavid Howells } 5803e30148cSDavid Howells 581664cceb0SDavid Howells ret = PTR_ERR(key_ref); 5821da177e4SLinus Torvalds goto error; 5831da177e4SLinus Torvalds } 5841da177e4SLinus Torvalds 5853e30148cSDavid Howells okay: 586664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 587aa9d4437SDavid Howells desclen = strlen(key->description); 588664cceb0SDavid Howells 589aa9d4437SDavid Howells /* calculate how much information we're going to return */ 590aa9d4437SDavid Howells ret = -ENOMEM; 591aa9d4437SDavid Howells infobuf = kasprintf(GFP_KERNEL, 592aa9d4437SDavid Howells "%s;%d;%d;%08x;", 59394fd8405SDavid Howells key->type->name, 5949a56c2dbSEric W. Biederman from_kuid_munged(current_user_ns(), key->uid), 5959a56c2dbSEric W. Biederman from_kgid_munged(current_user_ns(), key->gid), 596aa9d4437SDavid Howells key->perm); 597aa9d4437SDavid Howells if (!infobuf) 598aa9d4437SDavid Howells goto error2; 599aa9d4437SDavid Howells infolen = strlen(infobuf); 600aa9d4437SDavid Howells ret = infolen + desclen + 1; 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds /* consider returning the data */ 603aa9d4437SDavid Howells if (buffer && buflen >= ret) { 604aa9d4437SDavid Howells if (copy_to_user(buffer, infobuf, infolen) != 0 || 605aa9d4437SDavid Howells copy_to_user(buffer + infolen, key->description, 606aa9d4437SDavid Howells desclen + 1) != 0) 6071da177e4SLinus Torvalds ret = -EFAULT; 6081da177e4SLinus Torvalds } 6091da177e4SLinus Torvalds 610aa9d4437SDavid Howells kfree(infobuf); 6111da177e4SLinus Torvalds error2: 612664cceb0SDavid Howells key_ref_put(key_ref); 6131da177e4SLinus Torvalds error: 6141da177e4SLinus Torvalds return ret; 615a8b17ed0SDavid Howells } 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds /* 618973c9f4fSDavid Howells * Search the specified keyring and any keyrings it links to for a matching 619973c9f4fSDavid Howells * key. Only keyrings that grant the caller Search permission will be searched 620973c9f4fSDavid Howells * (this includes the starting keyring). Only keys with Search permission can 621973c9f4fSDavid Howells * be found. 622973c9f4fSDavid Howells * 623973c9f4fSDavid Howells * If successful, the found key will be linked to the destination keyring if 624973c9f4fSDavid Howells * supplied and the key has Link permission, and the found key ID will be 625973c9f4fSDavid Howells * returned. 6261da177e4SLinus Torvalds */ 6271da177e4SLinus Torvalds long keyctl_keyring_search(key_serial_t ringid, 6281da177e4SLinus Torvalds const char __user *_type, 6291da177e4SLinus Torvalds const char __user *_description, 6301da177e4SLinus Torvalds key_serial_t destringid) 6311da177e4SLinus Torvalds { 6321da177e4SLinus Torvalds struct key_type *ktype; 633664cceb0SDavid Howells key_ref_t keyring_ref, key_ref, dest_ref; 6341da177e4SLinus Torvalds char type[32], *description; 6350cb409d9SDavi Arnaut long ret; 6361da177e4SLinus Torvalds 6371da177e4SLinus Torvalds /* pull the type and description into kernel space */ 6380cb409d9SDavi Arnaut ret = key_get_type_from_user(type, _type, sizeof(type)); 6391da177e4SLinus Torvalds if (ret < 0) 6401da177e4SLinus Torvalds goto error; 6411da177e4SLinus Torvalds 642aa9d4437SDavid Howells description = strndup_user(_description, KEY_MAX_DESC_SIZE); 6430cb409d9SDavi Arnaut if (IS_ERR(description)) { 6440cb409d9SDavi Arnaut ret = PTR_ERR(description); 6451da177e4SLinus Torvalds goto error; 6460cb409d9SDavi Arnaut } 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds /* get the keyring at which to begin the search */ 649f5895943SDavid Howells keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH); 650664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 651664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 6521da177e4SLinus Torvalds goto error2; 6531da177e4SLinus Torvalds } 6541da177e4SLinus Torvalds 6551da177e4SLinus Torvalds /* get the destination keyring if specified */ 656664cceb0SDavid Howells dest_ref = NULL; 6571da177e4SLinus Torvalds if (destringid) { 6585593122eSDavid Howells dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE, 659f5895943SDavid Howells KEY_NEED_WRITE); 660664cceb0SDavid Howells if (IS_ERR(dest_ref)) { 661664cceb0SDavid Howells ret = PTR_ERR(dest_ref); 6621da177e4SLinus Torvalds goto error3; 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds } 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds /* find the key type */ 6671da177e4SLinus Torvalds ktype = key_type_lookup(type); 6681da177e4SLinus Torvalds if (IS_ERR(ktype)) { 6691da177e4SLinus Torvalds ret = PTR_ERR(ktype); 6701da177e4SLinus Torvalds goto error4; 6711da177e4SLinus Torvalds } 6721da177e4SLinus Torvalds 6731da177e4SLinus Torvalds /* do the search */ 674664cceb0SDavid Howells key_ref = keyring_search(keyring_ref, ktype, description); 675664cceb0SDavid Howells if (IS_ERR(key_ref)) { 676664cceb0SDavid Howells ret = PTR_ERR(key_ref); 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds /* treat lack or presence of a negative key the same */ 6791da177e4SLinus Torvalds if (ret == -EAGAIN) 6801da177e4SLinus Torvalds ret = -ENOKEY; 6811da177e4SLinus Torvalds goto error5; 6821da177e4SLinus Torvalds } 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds /* link the resulting key to the destination keyring if we can */ 685664cceb0SDavid Howells if (dest_ref) { 686f5895943SDavid Howells ret = key_permission(key_ref, KEY_NEED_LINK); 68729db9190SDavid Howells if (ret < 0) 6881da177e4SLinus Torvalds goto error6; 6891da177e4SLinus Torvalds 690664cceb0SDavid Howells ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref)); 6911da177e4SLinus Torvalds if (ret < 0) 6921da177e4SLinus Torvalds goto error6; 6931da177e4SLinus Torvalds } 6941da177e4SLinus Torvalds 695664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds error6: 698664cceb0SDavid Howells key_ref_put(key_ref); 6991da177e4SLinus Torvalds error5: 7001da177e4SLinus Torvalds key_type_put(ktype); 7011da177e4SLinus Torvalds error4: 702664cceb0SDavid Howells key_ref_put(dest_ref); 7031da177e4SLinus Torvalds error3: 704664cceb0SDavid Howells key_ref_put(keyring_ref); 7051da177e4SLinus Torvalds error2: 7061da177e4SLinus Torvalds kfree(description); 7071da177e4SLinus Torvalds error: 7081da177e4SLinus Torvalds return ret; 709a8b17ed0SDavid Howells } 7101da177e4SLinus Torvalds 7111da177e4SLinus Torvalds /* 712973c9f4fSDavid Howells * Read a key's payload. 713973c9f4fSDavid Howells * 714973c9f4fSDavid Howells * The key must either grant the caller Read permission, or it must grant the 715973c9f4fSDavid Howells * caller Search permission when searched for from the process keyrings. 716973c9f4fSDavid Howells * 717973c9f4fSDavid Howells * If successful, we place up to buflen bytes of data into the buffer, if one 718973c9f4fSDavid Howells * is provided, and return the amount of data that is available in the key, 719973c9f4fSDavid Howells * irrespective of how much we copied into the buffer. 7201da177e4SLinus Torvalds */ 7211da177e4SLinus Torvalds long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen) 7221da177e4SLinus Torvalds { 723664cceb0SDavid Howells struct key *key; 724664cceb0SDavid Howells key_ref_t key_ref; 7251da177e4SLinus Torvalds long ret; 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds /* find the key first */ 7285593122eSDavid Howells key_ref = lookup_user_key(keyid, 0, 0); 729664cceb0SDavid Howells if (IS_ERR(key_ref)) { 730664cceb0SDavid Howells ret = -ENOKEY; 731664cceb0SDavid Howells goto error; 732664cceb0SDavid Howells } 733664cceb0SDavid Howells 734664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 735664cceb0SDavid Howells 7361da177e4SLinus Torvalds /* see if we can read it directly */ 737f5895943SDavid Howells ret = key_permission(key_ref, KEY_NEED_READ); 73829db9190SDavid Howells if (ret == 0) 7391da177e4SLinus Torvalds goto can_read_key; 74029db9190SDavid Howells if (ret != -EACCES) 74129db9190SDavid Howells goto error; 7421da177e4SLinus Torvalds 743664cceb0SDavid Howells /* we can't; see if it's searchable from this process's keyrings 7443e30148cSDavid Howells * - we automatically take account of the fact that it may be 7453e30148cSDavid Howells * dangling off an instantiation key 7463e30148cSDavid Howells */ 747664cceb0SDavid Howells if (!is_key_possessed(key_ref)) { 7481260f801SDavid Howells ret = -EACCES; 7491da177e4SLinus Torvalds goto error2; 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds /* the key is probably readable - now try to read it */ 7531da177e4SLinus Torvalds can_read_key: 7541da177e4SLinus Torvalds ret = -EOPNOTSUPP; 7551da177e4SLinus Torvalds if (key->type->read) { 756*b4a1b4f5SDavid Howells /* Read the data with the semaphore held (since we might sleep) 757*b4a1b4f5SDavid Howells * to protect against the key being updated or revoked. 758*b4a1b4f5SDavid Howells */ 7591da177e4SLinus Torvalds down_read(&key->sem); 760*b4a1b4f5SDavid Howells ret = key_validate(key); 761*b4a1b4f5SDavid Howells if (ret == 0) 7621da177e4SLinus Torvalds ret = key->type->read(key, buffer, buflen); 7631da177e4SLinus Torvalds up_read(&key->sem); 7641da177e4SLinus Torvalds } 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds error2: 7671da177e4SLinus Torvalds key_put(key); 7681da177e4SLinus Torvalds error: 7691da177e4SLinus Torvalds return ret; 770a8b17ed0SDavid Howells } 7711da177e4SLinus Torvalds 7721da177e4SLinus Torvalds /* 773973c9f4fSDavid Howells * Change the ownership of a key 774973c9f4fSDavid Howells * 775973c9f4fSDavid Howells * The key must grant the caller Setattr permission for this to work, though 776973c9f4fSDavid Howells * the key need not be fully instantiated yet. For the UID to be changed, or 777973c9f4fSDavid Howells * for the GID to be changed to a group the caller is not a member of, the 778973c9f4fSDavid Howells * caller must have sysadmin capability. If either uid or gid is -1 then that 779973c9f4fSDavid Howells * attribute is not changed. 780973c9f4fSDavid Howells * 781973c9f4fSDavid Howells * If the UID is to be changed, the new user must have sufficient quota to 782973c9f4fSDavid Howells * accept the key. The quota deduction will be removed from the old user to 783973c9f4fSDavid Howells * the new user should the attribute be changed. 784973c9f4fSDavid Howells * 785973c9f4fSDavid Howells * If successful, 0 will be returned. 7861da177e4SLinus Torvalds */ 7879a56c2dbSEric W. Biederman long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group) 7881da177e4SLinus Torvalds { 7895801649dSFredrik Tolf struct key_user *newowner, *zapowner = NULL; 7901da177e4SLinus Torvalds struct key *key; 791664cceb0SDavid Howells key_ref_t key_ref; 7921da177e4SLinus Torvalds long ret; 7939a56c2dbSEric W. Biederman kuid_t uid; 7949a56c2dbSEric W. Biederman kgid_t gid; 7959a56c2dbSEric W. Biederman 7969a56c2dbSEric W. Biederman uid = make_kuid(current_user_ns(), user); 7979a56c2dbSEric W. Biederman gid = make_kgid(current_user_ns(), group); 7989a56c2dbSEric W. Biederman ret = -EINVAL; 7999a56c2dbSEric W. Biederman if ((user != (uid_t) -1) && !uid_valid(uid)) 8009a56c2dbSEric W. Biederman goto error; 8019a56c2dbSEric W. Biederman if ((group != (gid_t) -1) && !gid_valid(gid)) 8029a56c2dbSEric W. Biederman goto error; 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds ret = 0; 8059a56c2dbSEric W. Biederman if (user == (uid_t) -1 && group == (gid_t) -1) 8061da177e4SLinus Torvalds goto error; 8071da177e4SLinus Torvalds 8085593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 809f5895943SDavid Howells KEY_NEED_SETATTR); 810664cceb0SDavid Howells if (IS_ERR(key_ref)) { 811664cceb0SDavid Howells ret = PTR_ERR(key_ref); 8121da177e4SLinus Torvalds goto error; 8131da177e4SLinus Torvalds } 8141da177e4SLinus Torvalds 815664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 816664cceb0SDavid Howells 8171da177e4SLinus Torvalds /* make the changes with the locks held to prevent chown/chown races */ 8181da177e4SLinus Torvalds ret = -EACCES; 8191da177e4SLinus Torvalds down_write(&key->sem); 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) { 8221da177e4SLinus Torvalds /* only the sysadmin can chown a key to some other UID */ 8239a56c2dbSEric W. Biederman if (user != (uid_t) -1 && !uid_eq(key->uid, uid)) 8245801649dSFredrik Tolf goto error_put; 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds /* only the sysadmin can set the key's GID to a group other 8271da177e4SLinus Torvalds * than one of those that the current process subscribes to */ 8289a56c2dbSEric W. Biederman if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid)) 8295801649dSFredrik Tolf goto error_put; 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds 8325801649dSFredrik Tolf /* change the UID */ 8339a56c2dbSEric W. Biederman if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) { 8345801649dSFredrik Tolf ret = -ENOMEM; 8359a56c2dbSEric W. Biederman newowner = key_user_lookup(uid); 8365801649dSFredrik Tolf if (!newowner) 8375801649dSFredrik Tolf goto error_put; 8385801649dSFredrik Tolf 8395801649dSFredrik Tolf /* transfer the quota burden to the new user */ 8405801649dSFredrik Tolf if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { 8419a56c2dbSEric W. Biederman unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ? 8420b77f5bfSDavid Howells key_quota_root_maxkeys : key_quota_maxkeys; 8439a56c2dbSEric W. Biederman unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ? 8440b77f5bfSDavid Howells key_quota_root_maxbytes : key_quota_maxbytes; 8450b77f5bfSDavid Howells 8465801649dSFredrik Tolf spin_lock(&newowner->lock); 8470b77f5bfSDavid Howells if (newowner->qnkeys + 1 >= maxkeys || 8480b77f5bfSDavid Howells newowner->qnbytes + key->quotalen >= maxbytes || 8490b77f5bfSDavid Howells newowner->qnbytes + key->quotalen < 8500b77f5bfSDavid Howells newowner->qnbytes) 8515801649dSFredrik Tolf goto quota_overrun; 8525801649dSFredrik Tolf 8535801649dSFredrik Tolf newowner->qnkeys++; 8545801649dSFredrik Tolf newowner->qnbytes += key->quotalen; 8555801649dSFredrik Tolf spin_unlock(&newowner->lock); 8565801649dSFredrik Tolf 8575801649dSFredrik Tolf spin_lock(&key->user->lock); 8585801649dSFredrik Tolf key->user->qnkeys--; 8595801649dSFredrik Tolf key->user->qnbytes -= key->quotalen; 8605801649dSFredrik Tolf spin_unlock(&key->user->lock); 8615801649dSFredrik Tolf } 8625801649dSFredrik Tolf 8635801649dSFredrik Tolf atomic_dec(&key->user->nkeys); 8645801649dSFredrik Tolf atomic_inc(&newowner->nkeys); 8655801649dSFredrik Tolf 8665801649dSFredrik Tolf if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) { 8675801649dSFredrik Tolf atomic_dec(&key->user->nikeys); 8685801649dSFredrik Tolf atomic_inc(&newowner->nikeys); 8695801649dSFredrik Tolf } 8705801649dSFredrik Tolf 8715801649dSFredrik Tolf zapowner = key->user; 8725801649dSFredrik Tolf key->user = newowner; 8735801649dSFredrik Tolf key->uid = uid; 8741da177e4SLinus Torvalds } 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds /* change the GID */ 8779a56c2dbSEric W. Biederman if (group != (gid_t) -1) 8781da177e4SLinus Torvalds key->gid = gid; 8791da177e4SLinus Torvalds 8801da177e4SLinus Torvalds ret = 0; 8811da177e4SLinus Torvalds 8825801649dSFredrik Tolf error_put: 8831da177e4SLinus Torvalds up_write(&key->sem); 8841da177e4SLinus Torvalds key_put(key); 8855801649dSFredrik Tolf if (zapowner) 8865801649dSFredrik Tolf key_user_put(zapowner); 8871da177e4SLinus Torvalds error: 8881da177e4SLinus Torvalds return ret; 8891da177e4SLinus Torvalds 8905801649dSFredrik Tolf quota_overrun: 8915801649dSFredrik Tolf spin_unlock(&newowner->lock); 8925801649dSFredrik Tolf zapowner = newowner; 8935801649dSFredrik Tolf ret = -EDQUOT; 8945801649dSFredrik Tolf goto error_put; 895a8b17ed0SDavid Howells } 8965801649dSFredrik Tolf 8971da177e4SLinus Torvalds /* 898973c9f4fSDavid Howells * Change the permission mask on a key. 899973c9f4fSDavid Howells * 900973c9f4fSDavid Howells * The key must grant the caller Setattr permission for this to work, though 901973c9f4fSDavid Howells * the key need not be fully instantiated yet. If the caller does not have 902973c9f4fSDavid Howells * sysadmin capability, it may only change the permission on keys that it owns. 9031da177e4SLinus Torvalds */ 9041da177e4SLinus Torvalds long keyctl_setperm_key(key_serial_t id, key_perm_t perm) 9051da177e4SLinus Torvalds { 9061da177e4SLinus Torvalds struct key *key; 907664cceb0SDavid Howells key_ref_t key_ref; 9081da177e4SLinus Torvalds long ret; 9091da177e4SLinus Torvalds 9101da177e4SLinus Torvalds ret = -EINVAL; 911664cceb0SDavid Howells if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL)) 9121da177e4SLinus Torvalds goto error; 9131da177e4SLinus Torvalds 9145593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 915f5895943SDavid Howells KEY_NEED_SETATTR); 916664cceb0SDavid Howells if (IS_ERR(key_ref)) { 917664cceb0SDavid Howells ret = PTR_ERR(key_ref); 9181da177e4SLinus Torvalds goto error; 9191da177e4SLinus Torvalds } 9201da177e4SLinus Torvalds 921664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 922664cceb0SDavid Howells 92376d8aeabSDavid Howells /* make the changes with the locks held to prevent chown/chmod races */ 9241da177e4SLinus Torvalds ret = -EACCES; 9251da177e4SLinus Torvalds down_write(&key->sem); 9261da177e4SLinus Torvalds 92776d8aeabSDavid Howells /* if we're not the sysadmin, we can only change a key that we own */ 9289a56c2dbSEric W. Biederman if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) { 9291da177e4SLinus Torvalds key->perm = perm; 9301da177e4SLinus Torvalds ret = 0; 93176d8aeabSDavid Howells } 9321da177e4SLinus Torvalds 9331da177e4SLinus Torvalds up_write(&key->sem); 9341da177e4SLinus Torvalds key_put(key); 9351da177e4SLinus Torvalds error: 9361da177e4SLinus Torvalds return ret; 937a8b17ed0SDavid Howells } 9381da177e4SLinus Torvalds 9398bbf4976SDavid Howells /* 940973c9f4fSDavid Howells * Get the destination keyring for instantiation and check that the caller has 941973c9f4fSDavid Howells * Write permission on it. 9428bbf4976SDavid Howells */ 9438bbf4976SDavid Howells static long get_instantiation_keyring(key_serial_t ringid, 9448bbf4976SDavid Howells struct request_key_auth *rka, 9458bbf4976SDavid Howells struct key **_dest_keyring) 9468bbf4976SDavid Howells { 9478bbf4976SDavid Howells key_ref_t dkref; 9488bbf4976SDavid Howells 9498bbf4976SDavid Howells *_dest_keyring = NULL; 950eca1bf5bSDavid Howells 951eca1bf5bSDavid Howells /* just return a NULL pointer if we weren't asked to make a link */ 952eca1bf5bSDavid Howells if (ringid == 0) 9538bbf4976SDavid Howells return 0; 9548bbf4976SDavid Howells 9558bbf4976SDavid Howells /* if a specific keyring is nominated by ID, then use that */ 9568bbf4976SDavid Howells if (ringid > 0) { 957f5895943SDavid Howells dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); 9588bbf4976SDavid Howells if (IS_ERR(dkref)) 9598bbf4976SDavid Howells return PTR_ERR(dkref); 9608bbf4976SDavid Howells *_dest_keyring = key_ref_to_ptr(dkref); 9618bbf4976SDavid Howells return 0; 9628bbf4976SDavid Howells } 9638bbf4976SDavid Howells 9648bbf4976SDavid Howells if (ringid == KEY_SPEC_REQKEY_AUTH_KEY) 9658bbf4976SDavid Howells return -EINVAL; 9668bbf4976SDavid Howells 9678bbf4976SDavid Howells /* otherwise specify the destination keyring recorded in the 9688bbf4976SDavid Howells * authorisation key (any KEY_SPEC_*_KEYRING) */ 9698bbf4976SDavid Howells if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) { 97021279cfaSDavid Howells *_dest_keyring = key_get(rka->dest_keyring); 9718bbf4976SDavid Howells return 0; 9728bbf4976SDavid Howells } 9738bbf4976SDavid Howells 9748bbf4976SDavid Howells return -ENOKEY; 9758bbf4976SDavid Howells } 9768bbf4976SDavid Howells 977d84f4f99SDavid Howells /* 978973c9f4fSDavid Howells * Change the request_key authorisation key on the current process. 979d84f4f99SDavid Howells */ 980d84f4f99SDavid Howells static int keyctl_change_reqkey_auth(struct key *key) 981d84f4f99SDavid Howells { 982d84f4f99SDavid Howells struct cred *new; 983d84f4f99SDavid Howells 984d84f4f99SDavid Howells new = prepare_creds(); 985d84f4f99SDavid Howells if (!new) 986d84f4f99SDavid Howells return -ENOMEM; 987d84f4f99SDavid Howells 988d84f4f99SDavid Howells key_put(new->request_key_auth); 989d84f4f99SDavid Howells new->request_key_auth = key_get(key); 990d84f4f99SDavid Howells 991d84f4f99SDavid Howells return commit_creds(new); 992d84f4f99SDavid Howells } 993d84f4f99SDavid Howells 9941da177e4SLinus Torvalds /* 995973c9f4fSDavid Howells * Instantiate a key with the specified payload and link the key into the 996973c9f4fSDavid Howells * destination keyring if one is given. 997973c9f4fSDavid Howells * 998973c9f4fSDavid Howells * The caller must have the appropriate instantiation permit set for this to 999973c9f4fSDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1000973c9f4fSDavid Howells * 1001973c9f4fSDavid Howells * If successful, 0 will be returned. 10021da177e4SLinus Torvalds */ 1003ee009e4aSDavid Howells long keyctl_instantiate_key_common(key_serial_t id, 1004b353a1f7SAl Viro struct iov_iter *from, 10051da177e4SLinus Torvalds key_serial_t ringid) 10061da177e4SLinus Torvalds { 1007d84f4f99SDavid Howells const struct cred *cred = current_cred(); 10083e30148cSDavid Howells struct request_key_auth *rka; 10098bbf4976SDavid Howells struct key *instkey, *dest_keyring; 1010b353a1f7SAl Viro size_t plen = from ? iov_iter_count(from) : 0; 10111da177e4SLinus Torvalds void *payload; 10121da177e4SLinus Torvalds long ret; 10131da177e4SLinus Torvalds 1014d84f4f99SDavid Howells kenter("%d,,%zu,%d", id, plen, ringid); 1015d84f4f99SDavid Howells 1016b353a1f7SAl Viro if (!plen) 1017b353a1f7SAl Viro from = NULL; 1018b353a1f7SAl Viro 10191da177e4SLinus Torvalds ret = -EINVAL; 102038bbca6bSDavid Howells if (plen > 1024 * 1024 - 1) 10211da177e4SLinus Torvalds goto error; 10221da177e4SLinus Torvalds 1023b5f545c8SDavid Howells /* the appropriate instantiation authorisation key must have been 1024b5f545c8SDavid Howells * assumed before calling this */ 1025b5f545c8SDavid Howells ret = -EPERM; 1026d84f4f99SDavid Howells instkey = cred->request_key_auth; 1027b5f545c8SDavid Howells if (!instkey) 1028b5f545c8SDavid Howells goto error; 1029b5f545c8SDavid Howells 1030146aa8b1SDavid Howells rka = instkey->payload.data[0]; 1031b5f545c8SDavid Howells if (rka->target_key->serial != id) 1032b5f545c8SDavid Howells goto error; 1033b5f545c8SDavid Howells 10341da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 10351da177e4SLinus Torvalds payload = NULL; 10361da177e4SLinus Torvalds 1037b353a1f7SAl Viro if (from) { 10381da177e4SLinus Torvalds ret = -ENOMEM; 10391da177e4SLinus Torvalds payload = kmalloc(plen, GFP_KERNEL); 104038bbca6bSDavid Howells if (!payload) { 104138bbca6bSDavid Howells if (plen <= PAGE_SIZE) 104238bbca6bSDavid Howells goto error; 104338bbca6bSDavid Howells payload = vmalloc(plen); 10441da177e4SLinus Torvalds if (!payload) 10451da177e4SLinus Torvalds goto error; 104638bbca6bSDavid Howells } 10471da177e4SLinus Torvalds 1048b353a1f7SAl Viro ret = -EFAULT; 1049b353a1f7SAl Viro if (copy_from_iter(payload, plen, from) != plen) 10501da177e4SLinus Torvalds goto error2; 10511da177e4SLinus Torvalds } 10521da177e4SLinus Torvalds 10533e30148cSDavid Howells /* find the destination keyring amongst those belonging to the 10543e30148cSDavid Howells * requesting task */ 10558bbf4976SDavid Howells ret = get_instantiation_keyring(ringid, rka, &dest_keyring); 10568bbf4976SDavid Howells if (ret < 0) 1057b5f545c8SDavid Howells goto error2; 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds /* instantiate the key and link it into a keyring */ 10603e30148cSDavid Howells ret = key_instantiate_and_link(rka->target_key, payload, plen, 10618bbf4976SDavid Howells dest_keyring, instkey); 10621da177e4SLinus Torvalds 10638bbf4976SDavid Howells key_put(dest_keyring); 1064b5f545c8SDavid Howells 1065b5f545c8SDavid Howells /* discard the assumed authority if it's just been disabled by 1066b5f545c8SDavid Howells * instantiation of the key */ 1067d84f4f99SDavid Howells if (ret == 0) 1068d84f4f99SDavid Howells keyctl_change_reqkey_auth(NULL); 1069b5f545c8SDavid Howells 10701da177e4SLinus Torvalds error2: 1071b353a1f7SAl Viro kvfree(payload); 10721da177e4SLinus Torvalds error: 10731da177e4SLinus Torvalds return ret; 1074a8b17ed0SDavid Howells } 10751da177e4SLinus Torvalds 10761da177e4SLinus Torvalds /* 1077ee009e4aSDavid Howells * Instantiate a key with the specified payload and link the key into the 1078ee009e4aSDavid Howells * destination keyring if one is given. 1079ee009e4aSDavid Howells * 1080ee009e4aSDavid Howells * The caller must have the appropriate instantiation permit set for this to 1081ee009e4aSDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1082ee009e4aSDavid Howells * 1083ee009e4aSDavid Howells * If successful, 0 will be returned. 1084ee009e4aSDavid Howells */ 1085ee009e4aSDavid Howells long keyctl_instantiate_key(key_serial_t id, 1086ee009e4aSDavid Howells const void __user *_payload, 1087ee009e4aSDavid Howells size_t plen, 1088ee009e4aSDavid Howells key_serial_t ringid) 1089ee009e4aSDavid Howells { 1090ee009e4aSDavid Howells if (_payload && plen) { 1091b353a1f7SAl Viro struct iovec iov; 1092b353a1f7SAl Viro struct iov_iter from; 1093b353a1f7SAl Viro int ret; 1094ee009e4aSDavid Howells 1095b353a1f7SAl Viro ret = import_single_range(WRITE, (void __user *)_payload, plen, 1096b353a1f7SAl Viro &iov, &from); 1097b353a1f7SAl Viro if (unlikely(ret)) 1098b353a1f7SAl Viro return ret; 1099b353a1f7SAl Viro 1100b353a1f7SAl Viro return keyctl_instantiate_key_common(id, &from, ringid); 1101ee009e4aSDavid Howells } 1102ee009e4aSDavid Howells 1103b353a1f7SAl Viro return keyctl_instantiate_key_common(id, NULL, ringid); 1104ee009e4aSDavid Howells } 1105ee009e4aSDavid Howells 1106ee009e4aSDavid Howells /* 1107ee009e4aSDavid Howells * Instantiate a key with the specified multipart payload and link the key into 1108ee009e4aSDavid Howells * the destination keyring if one is given. 1109ee009e4aSDavid Howells * 1110ee009e4aSDavid Howells * The caller must have the appropriate instantiation permit set for this to 1111ee009e4aSDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1112ee009e4aSDavid Howells * 1113ee009e4aSDavid Howells * If successful, 0 will be returned. 1114ee009e4aSDavid Howells */ 1115ee009e4aSDavid Howells long keyctl_instantiate_key_iov(key_serial_t id, 1116ee009e4aSDavid Howells const struct iovec __user *_payload_iov, 1117ee009e4aSDavid Howells unsigned ioc, 1118ee009e4aSDavid Howells key_serial_t ringid) 1119ee009e4aSDavid Howells { 1120ee009e4aSDavid Howells struct iovec iovstack[UIO_FASTIOV], *iov = iovstack; 1121b353a1f7SAl Viro struct iov_iter from; 1122ee009e4aSDavid Howells long ret; 1123ee009e4aSDavid Howells 1124b353a1f7SAl Viro if (!_payload_iov) 1125b353a1f7SAl Viro ioc = 0; 1126ee009e4aSDavid Howells 1127b353a1f7SAl Viro ret = import_iovec(WRITE, _payload_iov, ioc, 1128b353a1f7SAl Viro ARRAY_SIZE(iovstack), &iov, &from); 1129ee009e4aSDavid Howells if (ret < 0) 1130b353a1f7SAl Viro return ret; 1131b353a1f7SAl Viro ret = keyctl_instantiate_key_common(id, &from, ringid); 1132ee009e4aSDavid Howells kfree(iov); 1133ee009e4aSDavid Howells return ret; 1134ee009e4aSDavid Howells } 1135ee009e4aSDavid Howells 1136ee009e4aSDavid Howells /* 1137973c9f4fSDavid Howells * Negatively instantiate the key with the given timeout (in seconds) and link 1138973c9f4fSDavid Howells * the key into the destination keyring if one is given. 1139973c9f4fSDavid Howells * 1140973c9f4fSDavid Howells * The caller must have the appropriate instantiation permit set for this to 1141973c9f4fSDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1142973c9f4fSDavid Howells * 1143973c9f4fSDavid Howells * The key and any links to the key will be automatically garbage collected 1144973c9f4fSDavid Howells * after the timeout expires. 1145973c9f4fSDavid Howells * 1146973c9f4fSDavid Howells * Negative keys are used to rate limit repeated request_key() calls by causing 1147973c9f4fSDavid Howells * them to return -ENOKEY until the negative key expires. 1148973c9f4fSDavid Howells * 1149973c9f4fSDavid Howells * If successful, 0 will be returned. 11501da177e4SLinus Torvalds */ 11511da177e4SLinus Torvalds long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid) 11521da177e4SLinus Torvalds { 1153fdd1b945SDavid Howells return keyctl_reject_key(id, timeout, ENOKEY, ringid); 1154fdd1b945SDavid Howells } 1155fdd1b945SDavid Howells 1156fdd1b945SDavid Howells /* 1157fdd1b945SDavid Howells * Negatively instantiate the key with the given timeout (in seconds) and error 1158fdd1b945SDavid Howells * code and link the key into the destination keyring if one is given. 1159fdd1b945SDavid Howells * 1160fdd1b945SDavid Howells * The caller must have the appropriate instantiation permit set for this to 1161fdd1b945SDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1162fdd1b945SDavid Howells * 1163fdd1b945SDavid Howells * The key and any links to the key will be automatically garbage collected 1164fdd1b945SDavid Howells * after the timeout expires. 1165fdd1b945SDavid Howells * 1166fdd1b945SDavid Howells * Negative keys are used to rate limit repeated request_key() calls by causing 1167fdd1b945SDavid Howells * them to return the specified error code until the negative key expires. 1168fdd1b945SDavid Howells * 1169fdd1b945SDavid Howells * If successful, 0 will be returned. 1170fdd1b945SDavid Howells */ 1171fdd1b945SDavid Howells long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error, 1172fdd1b945SDavid Howells key_serial_t ringid) 1173fdd1b945SDavid Howells { 1174d84f4f99SDavid Howells const struct cred *cred = current_cred(); 11753e30148cSDavid Howells struct request_key_auth *rka; 11768bbf4976SDavid Howells struct key *instkey, *dest_keyring; 11771da177e4SLinus Torvalds long ret; 11781da177e4SLinus Torvalds 1179fdd1b945SDavid Howells kenter("%d,%u,%u,%d", id, timeout, error, ringid); 1180fdd1b945SDavid Howells 1181fdd1b945SDavid Howells /* must be a valid error code and mustn't be a kernel special */ 1182fdd1b945SDavid Howells if (error <= 0 || 1183fdd1b945SDavid Howells error >= MAX_ERRNO || 1184fdd1b945SDavid Howells error == ERESTARTSYS || 1185fdd1b945SDavid Howells error == ERESTARTNOINTR || 1186fdd1b945SDavid Howells error == ERESTARTNOHAND || 1187fdd1b945SDavid Howells error == ERESTART_RESTARTBLOCK) 1188fdd1b945SDavid Howells return -EINVAL; 1189d84f4f99SDavid Howells 1190b5f545c8SDavid Howells /* the appropriate instantiation authorisation key must have been 1191b5f545c8SDavid Howells * assumed before calling this */ 1192b5f545c8SDavid Howells ret = -EPERM; 1193d84f4f99SDavid Howells instkey = cred->request_key_auth; 1194b5f545c8SDavid Howells if (!instkey) 11951da177e4SLinus Torvalds goto error; 11961da177e4SLinus Torvalds 1197146aa8b1SDavid Howells rka = instkey->payload.data[0]; 1198b5f545c8SDavid Howells if (rka->target_key->serial != id) 1199b5f545c8SDavid Howells goto error; 12003e30148cSDavid Howells 12011da177e4SLinus Torvalds /* find the destination keyring if present (which must also be 12021da177e4SLinus Torvalds * writable) */ 12038bbf4976SDavid Howells ret = get_instantiation_keyring(ringid, rka, &dest_keyring); 12048bbf4976SDavid Howells if (ret < 0) 1205b5f545c8SDavid Howells goto error; 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds /* instantiate the key and link it into a keyring */ 1208fdd1b945SDavid Howells ret = key_reject_and_link(rka->target_key, timeout, error, 12098bbf4976SDavid Howells dest_keyring, instkey); 12101da177e4SLinus Torvalds 12118bbf4976SDavid Howells key_put(dest_keyring); 1212b5f545c8SDavid Howells 1213b5f545c8SDavid Howells /* discard the assumed authority if it's just been disabled by 1214b5f545c8SDavid Howells * instantiation of the key */ 1215d84f4f99SDavid Howells if (ret == 0) 1216d84f4f99SDavid Howells keyctl_change_reqkey_auth(NULL); 1217b5f545c8SDavid Howells 12181da177e4SLinus Torvalds error: 12191da177e4SLinus Torvalds return ret; 1220a8b17ed0SDavid Howells } 12211da177e4SLinus Torvalds 12221da177e4SLinus Torvalds /* 1223973c9f4fSDavid Howells * Read or set the default keyring in which request_key() will cache keys and 1224973c9f4fSDavid Howells * return the old setting. 1225973c9f4fSDavid Howells * 1226973c9f4fSDavid Howells * If a process keyring is specified then this will be created if it doesn't 1227973c9f4fSDavid Howells * yet exist. The old setting will be returned if successful. 12283e30148cSDavid Howells */ 12293e30148cSDavid Howells long keyctl_set_reqkey_keyring(int reqkey_defl) 12303e30148cSDavid Howells { 1231d84f4f99SDavid Howells struct cred *new; 1232d84f4f99SDavid Howells int ret, old_setting; 1233d84f4f99SDavid Howells 1234d84f4f99SDavid Howells old_setting = current_cred_xxx(jit_keyring); 1235d84f4f99SDavid Howells 1236d84f4f99SDavid Howells if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE) 1237d84f4f99SDavid Howells return old_setting; 1238d84f4f99SDavid Howells 1239d84f4f99SDavid Howells new = prepare_creds(); 1240d84f4f99SDavid Howells if (!new) 1241d84f4f99SDavid Howells return -ENOMEM; 12423e30148cSDavid Howells 12433e30148cSDavid Howells switch (reqkey_defl) { 12443e30148cSDavid Howells case KEY_REQKEY_DEFL_THREAD_KEYRING: 1245d84f4f99SDavid Howells ret = install_thread_keyring_to_cred(new); 12463e30148cSDavid Howells if (ret < 0) 1247d84f4f99SDavid Howells goto error; 12483e30148cSDavid Howells goto set; 12493e30148cSDavid Howells 12503e30148cSDavid Howells case KEY_REQKEY_DEFL_PROCESS_KEYRING: 1251d84f4f99SDavid Howells ret = install_process_keyring_to_cred(new); 1252d84f4f99SDavid Howells if (ret < 0) { 1253d84f4f99SDavid Howells if (ret != -EEXIST) 1254d84f4f99SDavid Howells goto error; 1255d84f4f99SDavid Howells ret = 0; 1256d84f4f99SDavid Howells } 1257d84f4f99SDavid Howells goto set; 12583e30148cSDavid Howells 12593e30148cSDavid Howells case KEY_REQKEY_DEFL_DEFAULT: 12603e30148cSDavid Howells case KEY_REQKEY_DEFL_SESSION_KEYRING: 12613e30148cSDavid Howells case KEY_REQKEY_DEFL_USER_KEYRING: 12623e30148cSDavid Howells case KEY_REQKEY_DEFL_USER_SESSION_KEYRING: 1263d84f4f99SDavid Howells case KEY_REQKEY_DEFL_REQUESTOR_KEYRING: 1264d84f4f99SDavid Howells goto set; 12653e30148cSDavid Howells 12663e30148cSDavid Howells case KEY_REQKEY_DEFL_NO_CHANGE: 12673e30148cSDavid Howells case KEY_REQKEY_DEFL_GROUP_KEYRING: 12683e30148cSDavid Howells default: 1269d84f4f99SDavid Howells ret = -EINVAL; 1270d84f4f99SDavid Howells goto error; 12713e30148cSDavid Howells } 12723e30148cSDavid Howells 1273d84f4f99SDavid Howells set: 1274d84f4f99SDavid Howells new->jit_keyring = reqkey_defl; 1275d84f4f99SDavid Howells commit_creds(new); 1276d84f4f99SDavid Howells return old_setting; 1277d84f4f99SDavid Howells error: 1278d84f4f99SDavid Howells abort_creds(new); 12794303ef19SDan Carpenter return ret; 1280a8b17ed0SDavid Howells } 1281d84f4f99SDavid Howells 12823e30148cSDavid Howells /* 1283973c9f4fSDavid Howells * Set or clear the timeout on a key. 1284973c9f4fSDavid Howells * 1285973c9f4fSDavid Howells * Either the key must grant the caller Setattr permission or else the caller 1286973c9f4fSDavid Howells * must hold an instantiation authorisation token for the key. 1287973c9f4fSDavid Howells * 1288973c9f4fSDavid Howells * The timeout is either 0 to clear the timeout, or a number of seconds from 1289973c9f4fSDavid Howells * the current time. The key and any links to the key will be automatically 1290973c9f4fSDavid Howells * garbage collected after the timeout expires. 1291973c9f4fSDavid Howells * 1292973c9f4fSDavid Howells * If successful, 0 is returned. 1293017679c4SDavid Howells */ 1294017679c4SDavid Howells long keyctl_set_timeout(key_serial_t id, unsigned timeout) 1295017679c4SDavid Howells { 12969156235bSDavid Howells struct key *key, *instkey; 1297017679c4SDavid Howells key_ref_t key_ref; 1298017679c4SDavid Howells long ret; 1299017679c4SDavid Howells 13005593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 1301f5895943SDavid Howells KEY_NEED_SETATTR); 1302017679c4SDavid Howells if (IS_ERR(key_ref)) { 13039156235bSDavid Howells /* setting the timeout on a key under construction is permitted 13049156235bSDavid Howells * if we have the authorisation token handy */ 13059156235bSDavid Howells if (PTR_ERR(key_ref) == -EACCES) { 13069156235bSDavid Howells instkey = key_get_instantiation_authkey(id); 13079156235bSDavid Howells if (!IS_ERR(instkey)) { 13089156235bSDavid Howells key_put(instkey); 13099156235bSDavid Howells key_ref = lookup_user_key(id, 13109156235bSDavid Howells KEY_LOOKUP_PARTIAL, 13119156235bSDavid Howells 0); 13129156235bSDavid Howells if (!IS_ERR(key_ref)) 13139156235bSDavid Howells goto okay; 13149156235bSDavid Howells } 13159156235bSDavid Howells } 13169156235bSDavid Howells 1317017679c4SDavid Howells ret = PTR_ERR(key_ref); 1318017679c4SDavid Howells goto error; 1319017679c4SDavid Howells } 1320017679c4SDavid Howells 13219156235bSDavid Howells okay: 1322017679c4SDavid Howells key = key_ref_to_ptr(key_ref); 132359e6b9c1SBryan Schumaker key_set_timeout(key, timeout); 1324017679c4SDavid Howells key_put(key); 1325017679c4SDavid Howells 1326017679c4SDavid Howells ret = 0; 1327017679c4SDavid Howells error: 1328017679c4SDavid Howells return ret; 1329a8b17ed0SDavid Howells } 1330017679c4SDavid Howells 1331017679c4SDavid Howells /* 1332973c9f4fSDavid Howells * Assume (or clear) the authority to instantiate the specified key. 1333973c9f4fSDavid Howells * 1334973c9f4fSDavid Howells * This sets the authoritative token currently in force for key instantiation. 1335973c9f4fSDavid Howells * This must be done for a key to be instantiated. It has the effect of making 1336973c9f4fSDavid Howells * available all the keys from the caller of the request_key() that created a 1337973c9f4fSDavid Howells * key to request_key() calls made by the caller of this function. 1338973c9f4fSDavid Howells * 1339973c9f4fSDavid Howells * The caller must have the instantiation key in their process keyrings with a 1340973c9f4fSDavid Howells * Search permission grant available to the caller. 1341973c9f4fSDavid Howells * 1342973c9f4fSDavid Howells * If the ID given is 0, then the setting will be cleared and 0 returned. 1343973c9f4fSDavid Howells * 1344973c9f4fSDavid Howells * If the ID given has a matching an authorisation key, then that key will be 1345973c9f4fSDavid Howells * set and its ID will be returned. The authorisation key can be read to get 1346973c9f4fSDavid Howells * the callout information passed to request_key(). 1347b5f545c8SDavid Howells */ 1348b5f545c8SDavid Howells long keyctl_assume_authority(key_serial_t id) 1349b5f545c8SDavid Howells { 1350b5f545c8SDavid Howells struct key *authkey; 1351b5f545c8SDavid Howells long ret; 1352b5f545c8SDavid Howells 1353b5f545c8SDavid Howells /* special key IDs aren't permitted */ 1354b5f545c8SDavid Howells ret = -EINVAL; 1355b5f545c8SDavid Howells if (id < 0) 1356b5f545c8SDavid Howells goto error; 1357b5f545c8SDavid Howells 1358b5f545c8SDavid Howells /* we divest ourselves of authority if given an ID of 0 */ 1359b5f545c8SDavid Howells if (id == 0) { 1360d84f4f99SDavid Howells ret = keyctl_change_reqkey_auth(NULL); 1361b5f545c8SDavid Howells goto error; 1362b5f545c8SDavid Howells } 1363b5f545c8SDavid Howells 1364b5f545c8SDavid Howells /* attempt to assume the authority temporarily granted to us whilst we 1365b5f545c8SDavid Howells * instantiate the specified key 1366b5f545c8SDavid Howells * - the authorisation key must be in the current task's keyrings 1367b5f545c8SDavid Howells * somewhere 1368b5f545c8SDavid Howells */ 1369b5f545c8SDavid Howells authkey = key_get_instantiation_authkey(id); 1370b5f545c8SDavid Howells if (IS_ERR(authkey)) { 1371b5f545c8SDavid Howells ret = PTR_ERR(authkey); 1372b5f545c8SDavid Howells goto error; 1373b5f545c8SDavid Howells } 1374b5f545c8SDavid Howells 1375d84f4f99SDavid Howells ret = keyctl_change_reqkey_auth(authkey); 1376d84f4f99SDavid Howells if (ret < 0) 1377d84f4f99SDavid Howells goto error; 1378d84f4f99SDavid Howells key_put(authkey); 1379b5f545c8SDavid Howells 1380d84f4f99SDavid Howells ret = authkey->serial; 1381b5f545c8SDavid Howells error: 1382b5f545c8SDavid Howells return ret; 1383a8b17ed0SDavid Howells } 1384b5f545c8SDavid Howells 138570a5bb72SDavid Howells /* 1386973c9f4fSDavid Howells * Get a key's the LSM security label. 1387973c9f4fSDavid Howells * 1388973c9f4fSDavid Howells * The key must grant the caller View permission for this to work. 1389973c9f4fSDavid Howells * 1390973c9f4fSDavid Howells * If there's a buffer, then up to buflen bytes of data will be placed into it. 1391973c9f4fSDavid Howells * 1392973c9f4fSDavid Howells * If successful, the amount of information available will be returned, 1393973c9f4fSDavid Howells * irrespective of how much was copied (including the terminal NUL). 139470a5bb72SDavid Howells */ 139570a5bb72SDavid Howells long keyctl_get_security(key_serial_t keyid, 139670a5bb72SDavid Howells char __user *buffer, 139770a5bb72SDavid Howells size_t buflen) 139870a5bb72SDavid Howells { 139970a5bb72SDavid Howells struct key *key, *instkey; 140070a5bb72SDavid Howells key_ref_t key_ref; 140170a5bb72SDavid Howells char *context; 140270a5bb72SDavid Howells long ret; 140370a5bb72SDavid Howells 1404f5895943SDavid Howells key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW); 140570a5bb72SDavid Howells if (IS_ERR(key_ref)) { 140670a5bb72SDavid Howells if (PTR_ERR(key_ref) != -EACCES) 140770a5bb72SDavid Howells return PTR_ERR(key_ref); 140870a5bb72SDavid Howells 140970a5bb72SDavid Howells /* viewing a key under construction is also permitted if we 141070a5bb72SDavid Howells * have the authorisation token handy */ 141170a5bb72SDavid Howells instkey = key_get_instantiation_authkey(keyid); 141270a5bb72SDavid Howells if (IS_ERR(instkey)) 1413fa1cc7b5SRoel Kluin return PTR_ERR(instkey); 141470a5bb72SDavid Howells key_put(instkey); 141570a5bb72SDavid Howells 14165593122eSDavid Howells key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0); 141770a5bb72SDavid Howells if (IS_ERR(key_ref)) 141870a5bb72SDavid Howells return PTR_ERR(key_ref); 141970a5bb72SDavid Howells } 142070a5bb72SDavid Howells 142170a5bb72SDavid Howells key = key_ref_to_ptr(key_ref); 142270a5bb72SDavid Howells ret = security_key_getsecurity(key, &context); 142370a5bb72SDavid Howells if (ret == 0) { 142470a5bb72SDavid Howells /* if no information was returned, give userspace an empty 142570a5bb72SDavid Howells * string */ 142670a5bb72SDavid Howells ret = 1; 142770a5bb72SDavid Howells if (buffer && buflen > 0 && 142870a5bb72SDavid Howells copy_to_user(buffer, "", 1) != 0) 142970a5bb72SDavid Howells ret = -EFAULT; 143070a5bb72SDavid Howells } else if (ret > 0) { 143170a5bb72SDavid Howells /* return as much data as there's room for */ 143270a5bb72SDavid Howells if (buffer && buflen > 0) { 143370a5bb72SDavid Howells if (buflen > ret) 143470a5bb72SDavid Howells buflen = ret; 143570a5bb72SDavid Howells 143670a5bb72SDavid Howells if (copy_to_user(buffer, context, buflen) != 0) 143770a5bb72SDavid Howells ret = -EFAULT; 143870a5bb72SDavid Howells } 143970a5bb72SDavid Howells 144070a5bb72SDavid Howells kfree(context); 144170a5bb72SDavid Howells } 144270a5bb72SDavid Howells 144370a5bb72SDavid Howells key_ref_put(key_ref); 144470a5bb72SDavid Howells return ret; 144570a5bb72SDavid Howells } 144670a5bb72SDavid Howells 1447ee18d64cSDavid Howells /* 1448973c9f4fSDavid Howells * Attempt to install the calling process's session keyring on the process's 1449973c9f4fSDavid Howells * parent process. 1450973c9f4fSDavid Howells * 1451973c9f4fSDavid Howells * The keyring must exist and must grant the caller LINK permission, and the 1452973c9f4fSDavid Howells * parent process must be single-threaded and must have the same effective 1453973c9f4fSDavid Howells * ownership as this process and mustn't be SUID/SGID. 1454973c9f4fSDavid Howells * 1455973c9f4fSDavid Howells * The keyring will be emplaced on the parent when it next resumes userspace. 1456973c9f4fSDavid Howells * 1457973c9f4fSDavid Howells * If successful, 0 will be returned. 1458ee18d64cSDavid Howells */ 1459ee18d64cSDavid Howells long keyctl_session_to_parent(void) 1460ee18d64cSDavid Howells { 1461ee18d64cSDavid Howells struct task_struct *me, *parent; 1462ee18d64cSDavid Howells const struct cred *mycred, *pcred; 146367d12145SAl Viro struct callback_head *newwork, *oldwork; 1464ee18d64cSDavid Howells key_ref_t keyring_r; 1465413cd3d9SOleg Nesterov struct cred *cred; 1466ee18d64cSDavid Howells int ret; 1467ee18d64cSDavid Howells 1468f5895943SDavid Howells keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK); 1469ee18d64cSDavid Howells if (IS_ERR(keyring_r)) 1470ee18d64cSDavid Howells return PTR_ERR(keyring_r); 1471ee18d64cSDavid Howells 1472413cd3d9SOleg Nesterov ret = -ENOMEM; 1473413cd3d9SOleg Nesterov 1474ee18d64cSDavid Howells /* our parent is going to need a new cred struct, a new tgcred struct 1475ee18d64cSDavid Howells * and new security data, so we allocate them here to prevent ENOMEM in 1476ee18d64cSDavid Howells * our parent */ 1477ee18d64cSDavid Howells cred = cred_alloc_blank(); 1478ee18d64cSDavid Howells if (!cred) 147967d12145SAl Viro goto error_keyring; 148067d12145SAl Viro newwork = &cred->rcu; 1481ee18d64cSDavid Howells 14823a50597dSDavid Howells cred->session_keyring = key_ref_to_ptr(keyring_r); 14833a50597dSDavid Howells keyring_r = NULL; 148467d12145SAl Viro init_task_work(newwork, key_change_session_keyring); 1485ee18d64cSDavid Howells 1486ee18d64cSDavid Howells me = current; 14879d1ac65aSDavid Howells rcu_read_lock(); 1488ee18d64cSDavid Howells write_lock_irq(&tasklist_lock); 1489ee18d64cSDavid Howells 1490ee18d64cSDavid Howells ret = -EPERM; 1491413cd3d9SOleg Nesterov oldwork = NULL; 1492413cd3d9SOleg Nesterov parent = me->real_parent; 1493ee18d64cSDavid Howells 1494ee18d64cSDavid Howells /* the parent mustn't be init and mustn't be a kernel thread */ 1495ee18d64cSDavid Howells if (parent->pid <= 1 || !parent->mm) 1496413cd3d9SOleg Nesterov goto unlock; 1497ee18d64cSDavid Howells 1498ee18d64cSDavid Howells /* the parent must be single threaded */ 1499dd98acf7SOleg Nesterov if (!thread_group_empty(parent)) 1500413cd3d9SOleg Nesterov goto unlock; 1501ee18d64cSDavid Howells 1502ee18d64cSDavid Howells /* the parent and the child must have different session keyrings or 1503ee18d64cSDavid Howells * there's no point */ 1504ee18d64cSDavid Howells mycred = current_cred(); 1505ee18d64cSDavid Howells pcred = __task_cred(parent); 1506ee18d64cSDavid Howells if (mycred == pcred || 15073a50597dSDavid Howells mycred->session_keyring == pcred->session_keyring) { 1508413cd3d9SOleg Nesterov ret = 0; 1509413cd3d9SOleg Nesterov goto unlock; 1510413cd3d9SOleg Nesterov } 1511ee18d64cSDavid Howells 1512ee18d64cSDavid Howells /* the parent must have the same effective ownership and mustn't be 1513ee18d64cSDavid Howells * SUID/SGID */ 15149a56c2dbSEric W. Biederman if (!uid_eq(pcred->uid, mycred->euid) || 15159a56c2dbSEric W. Biederman !uid_eq(pcred->euid, mycred->euid) || 15169a56c2dbSEric W. Biederman !uid_eq(pcred->suid, mycred->euid) || 15179a56c2dbSEric W. Biederman !gid_eq(pcred->gid, mycred->egid) || 15189a56c2dbSEric W. Biederman !gid_eq(pcred->egid, mycred->egid) || 15199a56c2dbSEric W. Biederman !gid_eq(pcred->sgid, mycred->egid)) 1520413cd3d9SOleg Nesterov goto unlock; 1521ee18d64cSDavid Howells 1522ee18d64cSDavid Howells /* the keyrings must have the same UID */ 15233a50597dSDavid Howells if ((pcred->session_keyring && 15242a74dbb9SLinus Torvalds !uid_eq(pcred->session_keyring->uid, mycred->euid)) || 15252a74dbb9SLinus Torvalds !uid_eq(mycred->session_keyring->uid, mycred->euid)) 1526413cd3d9SOleg Nesterov goto unlock; 1527ee18d64cSDavid Howells 1528413cd3d9SOleg Nesterov /* cancel an already pending keyring replacement */ 1529413cd3d9SOleg Nesterov oldwork = task_work_cancel(parent, key_change_session_keyring); 1530ee18d64cSDavid Howells 1531ee18d64cSDavid Howells /* the replacement session keyring is applied just prior to userspace 1532ee18d64cSDavid Howells * restarting */ 153367d12145SAl Viro ret = task_work_add(parent, newwork, true); 1534413cd3d9SOleg Nesterov if (!ret) 1535413cd3d9SOleg Nesterov newwork = NULL; 1536413cd3d9SOleg Nesterov unlock: 1537ee18d64cSDavid Howells write_unlock_irq(&tasklist_lock); 15389d1ac65aSDavid Howells rcu_read_unlock(); 153967d12145SAl Viro if (oldwork) 154067d12145SAl Viro put_cred(container_of(oldwork, struct cred, rcu)); 154167d12145SAl Viro if (newwork) 154267d12145SAl Viro put_cred(cred); 1543ee18d64cSDavid Howells return ret; 1544ee18d64cSDavid Howells 1545ee18d64cSDavid Howells error_keyring: 1546ee18d64cSDavid Howells key_ref_put(keyring_r); 1547ee18d64cSDavid Howells return ret; 1548ee18d64cSDavid Howells } 1549ee18d64cSDavid Howells 1550b5f545c8SDavid Howells /* 1551973c9f4fSDavid Howells * The key control system call 15521da177e4SLinus Torvalds */ 1553938bb9f5SHeiko Carstens SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3, 1554938bb9f5SHeiko Carstens unsigned long, arg4, unsigned long, arg5) 15551da177e4SLinus Torvalds { 15561da177e4SLinus Torvalds switch (option) { 15571da177e4SLinus Torvalds case KEYCTL_GET_KEYRING_ID: 15581da177e4SLinus Torvalds return keyctl_get_keyring_ID((key_serial_t) arg2, 15591da177e4SLinus Torvalds (int) arg3); 15601da177e4SLinus Torvalds 15611da177e4SLinus Torvalds case KEYCTL_JOIN_SESSION_KEYRING: 15621da177e4SLinus Torvalds return keyctl_join_session_keyring((const char __user *) arg2); 15631da177e4SLinus Torvalds 15641da177e4SLinus Torvalds case KEYCTL_UPDATE: 15651da177e4SLinus Torvalds return keyctl_update_key((key_serial_t) arg2, 15661da177e4SLinus Torvalds (const void __user *) arg3, 15671da177e4SLinus Torvalds (size_t) arg4); 15681da177e4SLinus Torvalds 15691da177e4SLinus Torvalds case KEYCTL_REVOKE: 15701da177e4SLinus Torvalds return keyctl_revoke_key((key_serial_t) arg2); 15711da177e4SLinus Torvalds 15721da177e4SLinus Torvalds case KEYCTL_DESCRIBE: 15731da177e4SLinus Torvalds return keyctl_describe_key((key_serial_t) arg2, 15741da177e4SLinus Torvalds (char __user *) arg3, 15751da177e4SLinus Torvalds (unsigned) arg4); 15761da177e4SLinus Torvalds 15771da177e4SLinus Torvalds case KEYCTL_CLEAR: 15781da177e4SLinus Torvalds return keyctl_keyring_clear((key_serial_t) arg2); 15791da177e4SLinus Torvalds 15801da177e4SLinus Torvalds case KEYCTL_LINK: 15811da177e4SLinus Torvalds return keyctl_keyring_link((key_serial_t) arg2, 15821da177e4SLinus Torvalds (key_serial_t) arg3); 15831da177e4SLinus Torvalds 15841da177e4SLinus Torvalds case KEYCTL_UNLINK: 15851da177e4SLinus Torvalds return keyctl_keyring_unlink((key_serial_t) arg2, 15861da177e4SLinus Torvalds (key_serial_t) arg3); 15871da177e4SLinus Torvalds 15881da177e4SLinus Torvalds case KEYCTL_SEARCH: 15891da177e4SLinus Torvalds return keyctl_keyring_search((key_serial_t) arg2, 15901da177e4SLinus Torvalds (const char __user *) arg3, 15911da177e4SLinus Torvalds (const char __user *) arg4, 15921da177e4SLinus Torvalds (key_serial_t) arg5); 15931da177e4SLinus Torvalds 15941da177e4SLinus Torvalds case KEYCTL_READ: 15951da177e4SLinus Torvalds return keyctl_read_key((key_serial_t) arg2, 15961da177e4SLinus Torvalds (char __user *) arg3, 15971da177e4SLinus Torvalds (size_t) arg4); 15981da177e4SLinus Torvalds 15991da177e4SLinus Torvalds case KEYCTL_CHOWN: 16001da177e4SLinus Torvalds return keyctl_chown_key((key_serial_t) arg2, 16011da177e4SLinus Torvalds (uid_t) arg3, 16021da177e4SLinus Torvalds (gid_t) arg4); 16031da177e4SLinus Torvalds 16041da177e4SLinus Torvalds case KEYCTL_SETPERM: 16051da177e4SLinus Torvalds return keyctl_setperm_key((key_serial_t) arg2, 16061da177e4SLinus Torvalds (key_perm_t) arg3); 16071da177e4SLinus Torvalds 16081da177e4SLinus Torvalds case KEYCTL_INSTANTIATE: 16091da177e4SLinus Torvalds return keyctl_instantiate_key((key_serial_t) arg2, 16101da177e4SLinus Torvalds (const void __user *) arg3, 16111da177e4SLinus Torvalds (size_t) arg4, 16121da177e4SLinus Torvalds (key_serial_t) arg5); 16131da177e4SLinus Torvalds 16141da177e4SLinus Torvalds case KEYCTL_NEGATE: 16151da177e4SLinus Torvalds return keyctl_negate_key((key_serial_t) arg2, 16161da177e4SLinus Torvalds (unsigned) arg3, 16171da177e4SLinus Torvalds (key_serial_t) arg4); 16181da177e4SLinus Torvalds 16193e30148cSDavid Howells case KEYCTL_SET_REQKEY_KEYRING: 16203e30148cSDavid Howells return keyctl_set_reqkey_keyring(arg2); 16213e30148cSDavid Howells 1622017679c4SDavid Howells case KEYCTL_SET_TIMEOUT: 1623017679c4SDavid Howells return keyctl_set_timeout((key_serial_t) arg2, 1624017679c4SDavid Howells (unsigned) arg3); 1625017679c4SDavid Howells 1626b5f545c8SDavid Howells case KEYCTL_ASSUME_AUTHORITY: 1627b5f545c8SDavid Howells return keyctl_assume_authority((key_serial_t) arg2); 1628b5f545c8SDavid Howells 162970a5bb72SDavid Howells case KEYCTL_GET_SECURITY: 163070a5bb72SDavid Howells return keyctl_get_security((key_serial_t) arg2, 163190bd49abSJames Morris (char __user *) arg3, 163270a5bb72SDavid Howells (size_t) arg4); 163370a5bb72SDavid Howells 1634ee18d64cSDavid Howells case KEYCTL_SESSION_TO_PARENT: 1635ee18d64cSDavid Howells return keyctl_session_to_parent(); 1636ee18d64cSDavid Howells 1637fdd1b945SDavid Howells case KEYCTL_REJECT: 1638fdd1b945SDavid Howells return keyctl_reject_key((key_serial_t) arg2, 1639fdd1b945SDavid Howells (unsigned) arg3, 1640fdd1b945SDavid Howells (unsigned) arg4, 1641fdd1b945SDavid Howells (key_serial_t) arg5); 1642fdd1b945SDavid Howells 1643ee009e4aSDavid Howells case KEYCTL_INSTANTIATE_IOV: 1644ee009e4aSDavid Howells return keyctl_instantiate_key_iov( 1645ee009e4aSDavid Howells (key_serial_t) arg2, 1646ee009e4aSDavid Howells (const struct iovec __user *) arg3, 1647ee009e4aSDavid Howells (unsigned) arg4, 1648ee009e4aSDavid Howells (key_serial_t) arg5); 1649ee009e4aSDavid Howells 1650fd75815fSDavid Howells case KEYCTL_INVALIDATE: 1651fd75815fSDavid Howells return keyctl_invalidate_key((key_serial_t) arg2); 1652fd75815fSDavid Howells 1653f36f8c75SDavid Howells case KEYCTL_GET_PERSISTENT: 1654f36f8c75SDavid Howells return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3); 1655f36f8c75SDavid Howells 16561da177e4SLinus Torvalds default: 16571da177e4SLinus Torvalds return -EOPNOTSUPP; 16581da177e4SLinus Torvalds } 1659a8b17ed0SDavid Howells } 1660