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> 251da177e4SLinus Torvalds #include <asm/uaccess.h> 261da177e4SLinus Torvalds #include "internal.h" 271da177e4SLinus Torvalds 280cb409d9SDavi Arnaut static int key_get_type_from_user(char *type, 290cb409d9SDavi Arnaut const char __user *_type, 300cb409d9SDavi Arnaut unsigned len) 310cb409d9SDavi Arnaut { 320cb409d9SDavi Arnaut int ret; 330cb409d9SDavi Arnaut 340cb409d9SDavi Arnaut ret = strncpy_from_user(type, _type, len); 350cb409d9SDavi Arnaut if (ret < 0) 364303ef19SDan Carpenter return ret; 370cb409d9SDavi Arnaut if (ret == 0 || ret >= len) 380cb409d9SDavi Arnaut return -EINVAL; 390cb409d9SDavi Arnaut if (type[0] == '.') 400cb409d9SDavi Arnaut return -EPERM; 410cb409d9SDavi Arnaut type[len - 1] = '\0'; 420cb409d9SDavi Arnaut return 0; 430cb409d9SDavi Arnaut } 440cb409d9SDavi Arnaut 451da177e4SLinus Torvalds /* 46973c9f4fSDavid Howells * Extract the description of a new key from userspace and either add it as a 47973c9f4fSDavid Howells * new key to the specified keyring or update a matching key in that keyring. 48973c9f4fSDavid Howells * 49*cf7f601cSDavid Howells * If the description is NULL or an empty string, the key type is asked to 50*cf7f601cSDavid Howells * generate one from the payload. 51*cf7f601cSDavid Howells * 52973c9f4fSDavid Howells * The keyring must be writable so that we can attach the key to it. 53973c9f4fSDavid Howells * 54973c9f4fSDavid Howells * If successful, the new key's serial number is returned, otherwise an error 55973c9f4fSDavid Howells * code is returned. 561da177e4SLinus Torvalds */ 571e7bfb21SHeiko Carstens SYSCALL_DEFINE5(add_key, const char __user *, _type, 581e7bfb21SHeiko Carstens const char __user *, _description, 591e7bfb21SHeiko Carstens const void __user *, _payload, 601e7bfb21SHeiko Carstens size_t, plen, 611e7bfb21SHeiko Carstens key_serial_t, ringid) 621da177e4SLinus Torvalds { 63664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 641da177e4SLinus Torvalds char type[32], *description; 651da177e4SLinus Torvalds void *payload; 660cb409d9SDavi Arnaut long ret; 6738bbca6bSDavid Howells bool vm; 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds ret = -EINVAL; 7038bbca6bSDavid Howells if (plen > 1024 * 1024 - 1) 711da177e4SLinus Torvalds goto error; 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds /* draw all the data into kernel space */ 740cb409d9SDavi Arnaut ret = key_get_type_from_user(type, _type, sizeof(type)); 751da177e4SLinus Torvalds if (ret < 0) 761da177e4SLinus Torvalds goto error; 771da177e4SLinus Torvalds 78*cf7f601cSDavid Howells description = NULL; 79*cf7f601cSDavid Howells if (_description) { 800cb409d9SDavi Arnaut description = strndup_user(_description, PAGE_SIZE); 810cb409d9SDavi Arnaut if (IS_ERR(description)) { 820cb409d9SDavi Arnaut ret = PTR_ERR(description); 833e30148cSDavid Howells goto error; 840cb409d9SDavi Arnaut } 85*cf7f601cSDavid Howells if (!*description) { 86*cf7f601cSDavid Howells kfree(description); 87*cf7f601cSDavid Howells description = NULL; 88*cf7f601cSDavid Howells } 89*cf7f601cSDavid Howells } 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 921da177e4SLinus Torvalds payload = NULL; 931da177e4SLinus Torvalds 9438bbca6bSDavid Howells vm = false; 951da177e4SLinus Torvalds if (_payload) { 961da177e4SLinus Torvalds ret = -ENOMEM; 974f1c28d2SAndrew Morton payload = kmalloc(plen, GFP_KERNEL | __GFP_NOWARN); 9838bbca6bSDavid Howells if (!payload) { 9938bbca6bSDavid Howells if (plen <= PAGE_SIZE) 10038bbca6bSDavid Howells goto error2; 10138bbca6bSDavid Howells vm = true; 10238bbca6bSDavid Howells payload = vmalloc(plen); 1031da177e4SLinus Torvalds if (!payload) 1041da177e4SLinus Torvalds goto error2; 10538bbca6bSDavid Howells } 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds ret = -EFAULT; 1081da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 1091da177e4SLinus Torvalds goto error3; 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds /* find the target keyring (which must be writable) */ 1135593122eSDavid Howells keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); 114664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 115664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 1161da177e4SLinus Torvalds goto error3; 1171da177e4SLinus Torvalds } 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds /* create or update the requested key and add it to the target 1201da177e4SLinus Torvalds * keyring */ 121664cceb0SDavid Howells key_ref = key_create_or_update(keyring_ref, type, description, 1226b79ccb5SArun Raghavan payload, plen, KEY_PERM_UNDEF, 1236b79ccb5SArun Raghavan KEY_ALLOC_IN_QUOTA); 124664cceb0SDavid Howells if (!IS_ERR(key_ref)) { 125664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 126664cceb0SDavid Howells key_ref_put(key_ref); 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds else { 129664cceb0SDavid Howells ret = PTR_ERR(key_ref); 1301da177e4SLinus Torvalds } 1311da177e4SLinus Torvalds 132664cceb0SDavid Howells key_ref_put(keyring_ref); 1331da177e4SLinus Torvalds error3: 13438bbca6bSDavid Howells if (!vm) 1351da177e4SLinus Torvalds kfree(payload); 13638bbca6bSDavid Howells else 13738bbca6bSDavid Howells vfree(payload); 1381da177e4SLinus Torvalds error2: 1391da177e4SLinus Torvalds kfree(description); 1401da177e4SLinus Torvalds error: 1411da177e4SLinus Torvalds return ret; 142a8b17ed0SDavid Howells } 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds /* 145973c9f4fSDavid Howells * Search the process keyrings and keyring trees linked from those for a 146973c9f4fSDavid Howells * matching key. Keyrings must have appropriate Search permission to be 147973c9f4fSDavid Howells * searched. 148973c9f4fSDavid Howells * 149973c9f4fSDavid Howells * If a key is found, it will be attached to the destination keyring if there's 150973c9f4fSDavid Howells * one specified and the serial number of the key will be returned. 151973c9f4fSDavid Howells * 152973c9f4fSDavid Howells * If no key is found, /sbin/request-key will be invoked if _callout_info is 153973c9f4fSDavid Howells * non-NULL in an attempt to create a key. The _callout_info string will be 154973c9f4fSDavid Howells * passed to /sbin/request-key to aid with completing the request. If the 155973c9f4fSDavid Howells * _callout_info string is "" then it will be changed to "-". 1561da177e4SLinus Torvalds */ 1571e7bfb21SHeiko Carstens SYSCALL_DEFINE4(request_key, const char __user *, _type, 1581e7bfb21SHeiko Carstens const char __user *, _description, 1591e7bfb21SHeiko Carstens const char __user *, _callout_info, 1601e7bfb21SHeiko Carstens key_serial_t, destringid) 1611da177e4SLinus Torvalds { 1621da177e4SLinus Torvalds struct key_type *ktype; 163664cceb0SDavid Howells struct key *key; 164664cceb0SDavid Howells key_ref_t dest_ref; 1654a38e122SDavid Howells size_t callout_len; 1661da177e4SLinus Torvalds char type[32], *description, *callout_info; 1670cb409d9SDavi Arnaut long ret; 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds /* pull the type into kernel space */ 1700cb409d9SDavi Arnaut ret = key_get_type_from_user(type, _type, sizeof(type)); 1711da177e4SLinus Torvalds if (ret < 0) 1721da177e4SLinus Torvalds goto error; 1731260f801SDavid Howells 1741da177e4SLinus Torvalds /* pull the description into kernel space */ 1750cb409d9SDavi Arnaut description = strndup_user(_description, PAGE_SIZE); 1760cb409d9SDavi Arnaut if (IS_ERR(description)) { 1770cb409d9SDavi Arnaut ret = PTR_ERR(description); 1781da177e4SLinus Torvalds goto error; 1790cb409d9SDavi Arnaut } 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds /* pull the callout info into kernel space */ 1821da177e4SLinus Torvalds callout_info = NULL; 1834a38e122SDavid Howells callout_len = 0; 1841da177e4SLinus Torvalds if (_callout_info) { 1850cb409d9SDavi Arnaut callout_info = strndup_user(_callout_info, PAGE_SIZE); 1860cb409d9SDavi Arnaut if (IS_ERR(callout_info)) { 1870cb409d9SDavi Arnaut ret = PTR_ERR(callout_info); 1881da177e4SLinus Torvalds goto error2; 1890cb409d9SDavi Arnaut } 1904a38e122SDavid Howells callout_len = strlen(callout_info); 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds /* get the destination keyring if specified */ 194664cceb0SDavid Howells dest_ref = NULL; 1951da177e4SLinus Torvalds if (destringid) { 1965593122eSDavid Howells dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE, 1975593122eSDavid Howells KEY_WRITE); 198664cceb0SDavid Howells if (IS_ERR(dest_ref)) { 199664cceb0SDavid Howells ret = PTR_ERR(dest_ref); 2001da177e4SLinus Torvalds goto error3; 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds } 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds /* find the key type */ 2051da177e4SLinus Torvalds ktype = key_type_lookup(type); 2061da177e4SLinus Torvalds if (IS_ERR(ktype)) { 2071da177e4SLinus Torvalds ret = PTR_ERR(ktype); 2081da177e4SLinus Torvalds goto error4; 2091da177e4SLinus Torvalds } 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds /* do the search */ 2124a38e122SDavid Howells key = request_key_and_link(ktype, description, callout_info, 2134a38e122SDavid Howells callout_len, NULL, key_ref_to_ptr(dest_ref), 2147e047ef5SDavid Howells KEY_ALLOC_IN_QUOTA); 2151da177e4SLinus Torvalds if (IS_ERR(key)) { 2161da177e4SLinus Torvalds ret = PTR_ERR(key); 2171da177e4SLinus Torvalds goto error5; 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds 2204aab1e89SDavid Howells /* wait for the key to finish being constructed */ 2214aab1e89SDavid Howells ret = wait_for_key_construction(key, 1); 2224aab1e89SDavid Howells if (ret < 0) 2234aab1e89SDavid Howells goto error6; 2244aab1e89SDavid Howells 2251da177e4SLinus Torvalds ret = key->serial; 2261da177e4SLinus Torvalds 2274aab1e89SDavid Howells error6: 2281da177e4SLinus Torvalds key_put(key); 2291da177e4SLinus Torvalds error5: 2301da177e4SLinus Torvalds key_type_put(ktype); 2311da177e4SLinus Torvalds error4: 232664cceb0SDavid Howells key_ref_put(dest_ref); 2331da177e4SLinus Torvalds error3: 2341da177e4SLinus Torvalds kfree(callout_info); 2351da177e4SLinus Torvalds error2: 2361da177e4SLinus Torvalds kfree(description); 2371da177e4SLinus Torvalds error: 2381da177e4SLinus Torvalds return ret; 239a8b17ed0SDavid Howells } 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds /* 242973c9f4fSDavid Howells * Get the ID of the specified process keyring. 243973c9f4fSDavid Howells * 244973c9f4fSDavid Howells * The requested keyring must have search permission to be found. 245973c9f4fSDavid Howells * 246973c9f4fSDavid Howells * If successful, the ID of the requested keyring will be returned. 2471da177e4SLinus Torvalds */ 2481da177e4SLinus Torvalds long keyctl_get_keyring_ID(key_serial_t id, int create) 2491da177e4SLinus Torvalds { 250664cceb0SDavid Howells key_ref_t key_ref; 2515593122eSDavid Howells unsigned long lflags; 2521da177e4SLinus Torvalds long ret; 2531da177e4SLinus Torvalds 2545593122eSDavid Howells lflags = create ? KEY_LOOKUP_CREATE : 0; 2555593122eSDavid Howells key_ref = lookup_user_key(id, lflags, KEY_SEARCH); 256664cceb0SDavid Howells if (IS_ERR(key_ref)) { 257664cceb0SDavid Howells ret = PTR_ERR(key_ref); 2581da177e4SLinus Torvalds goto error; 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds 261664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 262664cceb0SDavid Howells key_ref_put(key_ref); 2631da177e4SLinus Torvalds error: 2641da177e4SLinus Torvalds return ret; 265973c9f4fSDavid Howells } 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds /* 268973c9f4fSDavid Howells * Join a (named) session keyring. 269973c9f4fSDavid Howells * 270973c9f4fSDavid Howells * Create and join an anonymous session keyring or join a named session 271973c9f4fSDavid Howells * keyring, creating it if necessary. A named session keyring must have Search 272973c9f4fSDavid Howells * permission for it to be joined. Session keyrings without this permit will 273973c9f4fSDavid Howells * be skipped over. 274973c9f4fSDavid Howells * 275973c9f4fSDavid Howells * If successful, the ID of the joined session keyring will be returned. 2761da177e4SLinus Torvalds */ 2771da177e4SLinus Torvalds long keyctl_join_session_keyring(const char __user *_name) 2781da177e4SLinus Torvalds { 2791da177e4SLinus Torvalds char *name; 2800cb409d9SDavi Arnaut long ret; 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds /* fetch the name from userspace */ 2831da177e4SLinus Torvalds name = NULL; 2841da177e4SLinus Torvalds if (_name) { 2850cb409d9SDavi Arnaut name = strndup_user(_name, PAGE_SIZE); 2860cb409d9SDavi Arnaut if (IS_ERR(name)) { 2870cb409d9SDavi Arnaut ret = PTR_ERR(name); 2881da177e4SLinus Torvalds goto error; 2890cb409d9SDavi Arnaut } 2901da177e4SLinus Torvalds } 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds /* join the session */ 2931da177e4SLinus Torvalds ret = join_session_keyring(name); 2940d54ee1cSVegard Nossum kfree(name); 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds error: 2971da177e4SLinus Torvalds return ret; 298a8b17ed0SDavid Howells } 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds /* 301973c9f4fSDavid Howells * Update a key's data payload from the given data. 302973c9f4fSDavid Howells * 303973c9f4fSDavid Howells * The key must grant the caller Write permission and the key type must support 304973c9f4fSDavid Howells * updating for this to work. A negative key can be positively instantiated 305973c9f4fSDavid Howells * with this call. 306973c9f4fSDavid Howells * 307973c9f4fSDavid Howells * If successful, 0 will be returned. If the key type does not support 308973c9f4fSDavid Howells * updating, then -EOPNOTSUPP will be returned. 3091da177e4SLinus Torvalds */ 3101da177e4SLinus Torvalds long keyctl_update_key(key_serial_t id, 3111da177e4SLinus Torvalds const void __user *_payload, 3121da177e4SLinus Torvalds size_t plen) 3131da177e4SLinus Torvalds { 314664cceb0SDavid Howells key_ref_t key_ref; 3151da177e4SLinus Torvalds void *payload; 3161da177e4SLinus Torvalds long ret; 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds ret = -EINVAL; 3191da177e4SLinus Torvalds if (plen > PAGE_SIZE) 3201da177e4SLinus Torvalds goto error; 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 3231da177e4SLinus Torvalds payload = NULL; 3241da177e4SLinus Torvalds if (_payload) { 3251da177e4SLinus Torvalds ret = -ENOMEM; 3261da177e4SLinus Torvalds payload = kmalloc(plen, GFP_KERNEL); 3271da177e4SLinus Torvalds if (!payload) 3281da177e4SLinus Torvalds goto error; 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds ret = -EFAULT; 3311da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 3321da177e4SLinus Torvalds goto error2; 3331da177e4SLinus Torvalds } 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds /* find the target key (which must be writable) */ 3365593122eSDavid Howells key_ref = lookup_user_key(id, 0, KEY_WRITE); 337664cceb0SDavid Howells if (IS_ERR(key_ref)) { 338664cceb0SDavid Howells ret = PTR_ERR(key_ref); 3391da177e4SLinus Torvalds goto error2; 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds /* update the key */ 343664cceb0SDavid Howells ret = key_update(key_ref, payload, plen); 3441da177e4SLinus Torvalds 345664cceb0SDavid Howells key_ref_put(key_ref); 3461da177e4SLinus Torvalds error2: 3471da177e4SLinus Torvalds kfree(payload); 3481da177e4SLinus Torvalds error: 3491da177e4SLinus Torvalds return ret; 350a8b17ed0SDavid Howells } 3511da177e4SLinus Torvalds 3521da177e4SLinus Torvalds /* 353973c9f4fSDavid Howells * Revoke a key. 354973c9f4fSDavid Howells * 355973c9f4fSDavid Howells * The key must be grant the caller Write or Setattr permission for this to 356973c9f4fSDavid Howells * work. The key type should give up its quota claim when revoked. The key 357973c9f4fSDavid Howells * and any links to the key will be automatically garbage collected after a 358973c9f4fSDavid Howells * certain amount of time (/proc/sys/kernel/keys/gc_delay). 359973c9f4fSDavid Howells * 360973c9f4fSDavid Howells * If successful, 0 is returned. 3611da177e4SLinus Torvalds */ 3621da177e4SLinus Torvalds long keyctl_revoke_key(key_serial_t id) 3631da177e4SLinus Torvalds { 364664cceb0SDavid Howells key_ref_t key_ref; 3651da177e4SLinus Torvalds long ret; 3661da177e4SLinus Torvalds 3675593122eSDavid Howells key_ref = lookup_user_key(id, 0, KEY_WRITE); 368664cceb0SDavid Howells if (IS_ERR(key_ref)) { 369664cceb0SDavid Howells ret = PTR_ERR(key_ref); 3700c2c9a3fSDavid Howells if (ret != -EACCES) 3711da177e4SLinus Torvalds goto error; 3720c2c9a3fSDavid Howells key_ref = lookup_user_key(id, 0, KEY_SETATTR); 3730c2c9a3fSDavid Howells if (IS_ERR(key_ref)) { 3740c2c9a3fSDavid Howells ret = PTR_ERR(key_ref); 3750c2c9a3fSDavid Howells goto error; 3760c2c9a3fSDavid Howells } 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 379664cceb0SDavid Howells key_revoke(key_ref_to_ptr(key_ref)); 3801da177e4SLinus Torvalds ret = 0; 3811da177e4SLinus Torvalds 382664cceb0SDavid Howells key_ref_put(key_ref); 3831da177e4SLinus Torvalds error: 3841260f801SDavid Howells return ret; 385a8b17ed0SDavid Howells } 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds /* 388fd75815fSDavid Howells * Invalidate a key. 389fd75815fSDavid Howells * 390fd75815fSDavid Howells * The key must be grant the caller Invalidate permission for this to work. 391fd75815fSDavid Howells * The key and any links to the key will be automatically garbage collected 392fd75815fSDavid Howells * immediately. 393fd75815fSDavid Howells * 394fd75815fSDavid Howells * If successful, 0 is returned. 395fd75815fSDavid Howells */ 396fd75815fSDavid Howells long keyctl_invalidate_key(key_serial_t id) 397fd75815fSDavid Howells { 398fd75815fSDavid Howells key_ref_t key_ref; 399fd75815fSDavid Howells long ret; 400fd75815fSDavid Howells 401fd75815fSDavid Howells kenter("%d", id); 402fd75815fSDavid Howells 403fd75815fSDavid Howells key_ref = lookup_user_key(id, 0, KEY_SEARCH); 404fd75815fSDavid Howells if (IS_ERR(key_ref)) { 405fd75815fSDavid Howells ret = PTR_ERR(key_ref); 406fd75815fSDavid Howells goto error; 407fd75815fSDavid Howells } 408fd75815fSDavid Howells 409fd75815fSDavid Howells key_invalidate(key_ref_to_ptr(key_ref)); 410fd75815fSDavid Howells ret = 0; 411fd75815fSDavid Howells 412fd75815fSDavid Howells key_ref_put(key_ref); 413fd75815fSDavid Howells error: 414fd75815fSDavid Howells kleave(" = %ld", ret); 415fd75815fSDavid Howells return ret; 416fd75815fSDavid Howells } 417fd75815fSDavid Howells 418fd75815fSDavid Howells /* 419973c9f4fSDavid Howells * Clear the specified keyring, creating an empty process keyring if one of the 420973c9f4fSDavid Howells * special keyring IDs is used. 421973c9f4fSDavid Howells * 422973c9f4fSDavid Howells * The keyring must grant the caller Write permission for this to work. If 423973c9f4fSDavid Howells * successful, 0 will be returned. 4241da177e4SLinus Torvalds */ 4251da177e4SLinus Torvalds long keyctl_keyring_clear(key_serial_t ringid) 4261da177e4SLinus Torvalds { 427664cceb0SDavid Howells key_ref_t keyring_ref; 4281da177e4SLinus Torvalds long ret; 4291da177e4SLinus Torvalds 4305593122eSDavid Howells keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); 431664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 432664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 433700920ebSDavid Howells 434700920ebSDavid Howells /* Root is permitted to invalidate certain special keyrings */ 435700920ebSDavid Howells if (capable(CAP_SYS_ADMIN)) { 436700920ebSDavid Howells keyring_ref = lookup_user_key(ringid, 0, 0); 437700920ebSDavid Howells if (IS_ERR(keyring_ref)) 438700920ebSDavid Howells goto error; 439700920ebSDavid Howells if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR, 440700920ebSDavid Howells &key_ref_to_ptr(keyring_ref)->flags)) 441700920ebSDavid Howells goto clear; 442700920ebSDavid Howells goto error_put; 443700920ebSDavid Howells } 444700920ebSDavid Howells 4451da177e4SLinus Torvalds goto error; 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds 448700920ebSDavid Howells clear: 449664cceb0SDavid Howells ret = keyring_clear(key_ref_to_ptr(keyring_ref)); 450700920ebSDavid Howells error_put: 451664cceb0SDavid Howells key_ref_put(keyring_ref); 4521da177e4SLinus Torvalds error: 4531da177e4SLinus Torvalds return ret; 454a8b17ed0SDavid Howells } 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds /* 457973c9f4fSDavid Howells * Create a link from a keyring to a key if there's no matching key in the 458973c9f4fSDavid Howells * keyring, otherwise replace the link to the matching key with a link to the 459973c9f4fSDavid Howells * new key. 460973c9f4fSDavid Howells * 461973c9f4fSDavid Howells * The key must grant the caller Link permission and the the keyring must grant 462973c9f4fSDavid Howells * the caller Write permission. Furthermore, if an additional link is created, 463973c9f4fSDavid Howells * the keyring's quota will be extended. 464973c9f4fSDavid Howells * 465973c9f4fSDavid Howells * If successful, 0 will be returned. 4661da177e4SLinus Torvalds */ 4671da177e4SLinus Torvalds long keyctl_keyring_link(key_serial_t id, key_serial_t ringid) 4681da177e4SLinus Torvalds { 469664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 4701da177e4SLinus Torvalds long ret; 4711da177e4SLinus Torvalds 4725593122eSDavid Howells keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); 473664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 474664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 4751da177e4SLinus Torvalds goto error; 4761da177e4SLinus Torvalds } 4771da177e4SLinus Torvalds 4785593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_LINK); 479664cceb0SDavid Howells if (IS_ERR(key_ref)) { 480664cceb0SDavid Howells ret = PTR_ERR(key_ref); 4811da177e4SLinus Torvalds goto error2; 4821da177e4SLinus Torvalds } 4831da177e4SLinus Torvalds 484664cceb0SDavid Howells ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref)); 4851da177e4SLinus Torvalds 486664cceb0SDavid Howells key_ref_put(key_ref); 4871da177e4SLinus Torvalds error2: 488664cceb0SDavid Howells key_ref_put(keyring_ref); 4891da177e4SLinus Torvalds error: 4901da177e4SLinus Torvalds return ret; 491a8b17ed0SDavid Howells } 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds /* 494973c9f4fSDavid Howells * Unlink a key from a keyring. 495973c9f4fSDavid Howells * 496973c9f4fSDavid Howells * The keyring must grant the caller Write permission for this to work; the key 497973c9f4fSDavid Howells * itself need not grant the caller anything. If the last link to a key is 498973c9f4fSDavid Howells * removed then that key will be scheduled for destruction. 499973c9f4fSDavid Howells * 500973c9f4fSDavid Howells * If successful, 0 will be returned. 5011da177e4SLinus Torvalds */ 5021da177e4SLinus Torvalds long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid) 5031da177e4SLinus Torvalds { 504664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 5051da177e4SLinus Torvalds long ret; 5061da177e4SLinus Torvalds 5075593122eSDavid Howells keyring_ref = lookup_user_key(ringid, 0, KEY_WRITE); 508664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 509664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 5101da177e4SLinus Torvalds goto error; 5111da177e4SLinus Torvalds } 5121da177e4SLinus Torvalds 5135593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0); 514664cceb0SDavid Howells if (IS_ERR(key_ref)) { 515664cceb0SDavid Howells ret = PTR_ERR(key_ref); 5161da177e4SLinus Torvalds goto error2; 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds 519664cceb0SDavid Howells ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref)); 5201da177e4SLinus Torvalds 521664cceb0SDavid Howells key_ref_put(key_ref); 5221da177e4SLinus Torvalds error2: 523664cceb0SDavid Howells key_ref_put(keyring_ref); 5241da177e4SLinus Torvalds error: 5251da177e4SLinus Torvalds return ret; 526a8b17ed0SDavid Howells } 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds /* 529973c9f4fSDavid Howells * Return a description of a key to userspace. 530973c9f4fSDavid Howells * 531973c9f4fSDavid Howells * The key must grant the caller View permission for this to work. 532973c9f4fSDavid Howells * 533973c9f4fSDavid Howells * If there's a buffer, we place up to buflen bytes of data into it formatted 534973c9f4fSDavid Howells * in the following way: 535973c9f4fSDavid Howells * 5361da177e4SLinus Torvalds * type;uid;gid;perm;description<NUL> 537973c9f4fSDavid Howells * 538973c9f4fSDavid Howells * If successful, we return the amount of description available, irrespective 539973c9f4fSDavid Howells * of how much we may have copied into the buffer. 5401da177e4SLinus Torvalds */ 5411da177e4SLinus Torvalds long keyctl_describe_key(key_serial_t keyid, 5421da177e4SLinus Torvalds char __user *buffer, 5431da177e4SLinus Torvalds size_t buflen) 5441da177e4SLinus Torvalds { 5453e30148cSDavid Howells struct key *key, *instkey; 546664cceb0SDavid Howells key_ref_t key_ref; 5471da177e4SLinus Torvalds char *tmpbuf; 5481da177e4SLinus Torvalds long ret; 5491da177e4SLinus Torvalds 5505593122eSDavid Howells key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW); 551664cceb0SDavid Howells if (IS_ERR(key_ref)) { 5523e30148cSDavid Howells /* viewing a key under construction is permitted if we have the 5533e30148cSDavid Howells * authorisation token handy */ 554664cceb0SDavid Howells if (PTR_ERR(key_ref) == -EACCES) { 5553e30148cSDavid Howells instkey = key_get_instantiation_authkey(keyid); 5563e30148cSDavid Howells if (!IS_ERR(instkey)) { 5573e30148cSDavid Howells key_put(instkey); 5588bbf4976SDavid Howells key_ref = lookup_user_key(keyid, 5595593122eSDavid Howells KEY_LOOKUP_PARTIAL, 5605593122eSDavid Howells 0); 561664cceb0SDavid Howells if (!IS_ERR(key_ref)) 5623e30148cSDavid Howells goto okay; 5633e30148cSDavid Howells } 5643e30148cSDavid Howells } 5653e30148cSDavid Howells 566664cceb0SDavid Howells ret = PTR_ERR(key_ref); 5671da177e4SLinus Torvalds goto error; 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds 5703e30148cSDavid Howells okay: 5711da177e4SLinus Torvalds /* calculate how much description we're going to return */ 5721da177e4SLinus Torvalds ret = -ENOMEM; 5731da177e4SLinus Torvalds tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); 5741da177e4SLinus Torvalds if (!tmpbuf) 5751da177e4SLinus Torvalds goto error2; 5761da177e4SLinus Torvalds 577664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 578664cceb0SDavid Howells 5791da177e4SLinus Torvalds ret = snprintf(tmpbuf, PAGE_SIZE - 1, 580664cceb0SDavid Howells "%s;%d;%d;%08x;%s", 58194fd8405SDavid Howells key->type->name, 5829a56c2dbSEric W. Biederman from_kuid_munged(current_user_ns(), key->uid), 5839a56c2dbSEric W. Biederman from_kgid_munged(current_user_ns(), key->gid), 58494fd8405SDavid Howells key->perm, 58594fd8405SDavid Howells key->description ?: ""); 5861da177e4SLinus Torvalds 5871da177e4SLinus Torvalds /* include a NUL char at the end of the data */ 5881da177e4SLinus Torvalds if (ret > PAGE_SIZE - 1) 5891da177e4SLinus Torvalds ret = PAGE_SIZE - 1; 5901da177e4SLinus Torvalds tmpbuf[ret] = 0; 5911da177e4SLinus Torvalds ret++; 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds /* consider returning the data */ 5941da177e4SLinus Torvalds if (buffer && buflen > 0) { 5951da177e4SLinus Torvalds if (buflen > ret) 5961da177e4SLinus Torvalds buflen = ret; 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds if (copy_to_user(buffer, tmpbuf, buflen) != 0) 5991da177e4SLinus Torvalds ret = -EFAULT; 6001da177e4SLinus Torvalds } 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds kfree(tmpbuf); 6031da177e4SLinus Torvalds error2: 604664cceb0SDavid Howells key_ref_put(key_ref); 6051da177e4SLinus Torvalds error: 6061da177e4SLinus Torvalds return ret; 607a8b17ed0SDavid Howells } 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds /* 610973c9f4fSDavid Howells * Search the specified keyring and any keyrings it links to for a matching 611973c9f4fSDavid Howells * key. Only keyrings that grant the caller Search permission will be searched 612973c9f4fSDavid Howells * (this includes the starting keyring). Only keys with Search permission can 613973c9f4fSDavid Howells * be found. 614973c9f4fSDavid Howells * 615973c9f4fSDavid Howells * If successful, the found key will be linked to the destination keyring if 616973c9f4fSDavid Howells * supplied and the key has Link permission, and the found key ID will be 617973c9f4fSDavid Howells * returned. 6181da177e4SLinus Torvalds */ 6191da177e4SLinus Torvalds long keyctl_keyring_search(key_serial_t ringid, 6201da177e4SLinus Torvalds const char __user *_type, 6211da177e4SLinus Torvalds const char __user *_description, 6221da177e4SLinus Torvalds key_serial_t destringid) 6231da177e4SLinus Torvalds { 6241da177e4SLinus Torvalds struct key_type *ktype; 625664cceb0SDavid Howells key_ref_t keyring_ref, key_ref, dest_ref; 6261da177e4SLinus Torvalds char type[32], *description; 6270cb409d9SDavi Arnaut long ret; 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds /* pull the type and description into kernel space */ 6300cb409d9SDavi Arnaut ret = key_get_type_from_user(type, _type, sizeof(type)); 6311da177e4SLinus Torvalds if (ret < 0) 6321da177e4SLinus Torvalds goto error; 6331da177e4SLinus Torvalds 6340cb409d9SDavi Arnaut description = strndup_user(_description, PAGE_SIZE); 6350cb409d9SDavi Arnaut if (IS_ERR(description)) { 6360cb409d9SDavi Arnaut ret = PTR_ERR(description); 6371da177e4SLinus Torvalds goto error; 6380cb409d9SDavi Arnaut } 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds /* get the keyring at which to begin the search */ 6415593122eSDavid Howells keyring_ref = lookup_user_key(ringid, 0, KEY_SEARCH); 642664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 643664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 6441da177e4SLinus Torvalds goto error2; 6451da177e4SLinus Torvalds } 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds /* get the destination keyring if specified */ 648664cceb0SDavid Howells dest_ref = NULL; 6491da177e4SLinus Torvalds if (destringid) { 6505593122eSDavid Howells dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE, 6515593122eSDavid Howells KEY_WRITE); 652664cceb0SDavid Howells if (IS_ERR(dest_ref)) { 653664cceb0SDavid Howells ret = PTR_ERR(dest_ref); 6541da177e4SLinus Torvalds goto error3; 6551da177e4SLinus Torvalds } 6561da177e4SLinus Torvalds } 6571da177e4SLinus Torvalds 6581da177e4SLinus Torvalds /* find the key type */ 6591da177e4SLinus Torvalds ktype = key_type_lookup(type); 6601da177e4SLinus Torvalds if (IS_ERR(ktype)) { 6611da177e4SLinus Torvalds ret = PTR_ERR(ktype); 6621da177e4SLinus Torvalds goto error4; 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds /* do the search */ 666664cceb0SDavid Howells key_ref = keyring_search(keyring_ref, ktype, description); 667664cceb0SDavid Howells if (IS_ERR(key_ref)) { 668664cceb0SDavid Howells ret = PTR_ERR(key_ref); 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds /* treat lack or presence of a negative key the same */ 6711da177e4SLinus Torvalds if (ret == -EAGAIN) 6721da177e4SLinus Torvalds ret = -ENOKEY; 6731da177e4SLinus Torvalds goto error5; 6741da177e4SLinus Torvalds } 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds /* link the resulting key to the destination keyring if we can */ 677664cceb0SDavid Howells if (dest_ref) { 67829db9190SDavid Howells ret = key_permission(key_ref, KEY_LINK); 67929db9190SDavid Howells if (ret < 0) 6801da177e4SLinus Torvalds goto error6; 6811da177e4SLinus Torvalds 682664cceb0SDavid Howells ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref)); 6831da177e4SLinus Torvalds if (ret < 0) 6841da177e4SLinus Torvalds goto error6; 6851da177e4SLinus Torvalds } 6861da177e4SLinus Torvalds 687664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds error6: 690664cceb0SDavid Howells key_ref_put(key_ref); 6911da177e4SLinus Torvalds error5: 6921da177e4SLinus Torvalds key_type_put(ktype); 6931da177e4SLinus Torvalds error4: 694664cceb0SDavid Howells key_ref_put(dest_ref); 6951da177e4SLinus Torvalds error3: 696664cceb0SDavid Howells key_ref_put(keyring_ref); 6971da177e4SLinus Torvalds error2: 6981da177e4SLinus Torvalds kfree(description); 6991da177e4SLinus Torvalds error: 7001da177e4SLinus Torvalds return ret; 701a8b17ed0SDavid Howells } 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds /* 704973c9f4fSDavid Howells * Read a key's payload. 705973c9f4fSDavid Howells * 706973c9f4fSDavid Howells * The key must either grant the caller Read permission, or it must grant the 707973c9f4fSDavid Howells * caller Search permission when searched for from the process keyrings. 708973c9f4fSDavid Howells * 709973c9f4fSDavid Howells * If successful, we place up to buflen bytes of data into the buffer, if one 710973c9f4fSDavid Howells * is provided, and return the amount of data that is available in the key, 711973c9f4fSDavid Howells * irrespective of how much we copied into the buffer. 7121da177e4SLinus Torvalds */ 7131da177e4SLinus Torvalds long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen) 7141da177e4SLinus Torvalds { 715664cceb0SDavid Howells struct key *key; 716664cceb0SDavid Howells key_ref_t key_ref; 7171da177e4SLinus Torvalds long ret; 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds /* find the key first */ 7205593122eSDavid Howells key_ref = lookup_user_key(keyid, 0, 0); 721664cceb0SDavid Howells if (IS_ERR(key_ref)) { 722664cceb0SDavid Howells ret = -ENOKEY; 723664cceb0SDavid Howells goto error; 724664cceb0SDavid Howells } 725664cceb0SDavid Howells 726664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 727664cceb0SDavid Howells 7281da177e4SLinus Torvalds /* see if we can read it directly */ 72929db9190SDavid Howells ret = key_permission(key_ref, KEY_READ); 73029db9190SDavid Howells if (ret == 0) 7311da177e4SLinus Torvalds goto can_read_key; 73229db9190SDavid Howells if (ret != -EACCES) 73329db9190SDavid Howells goto error; 7341da177e4SLinus Torvalds 735664cceb0SDavid Howells /* we can't; see if it's searchable from this process's keyrings 7363e30148cSDavid Howells * - we automatically take account of the fact that it may be 7373e30148cSDavid Howells * dangling off an instantiation key 7383e30148cSDavid Howells */ 739664cceb0SDavid Howells if (!is_key_possessed(key_ref)) { 7401260f801SDavid Howells ret = -EACCES; 7411da177e4SLinus Torvalds goto error2; 7421da177e4SLinus Torvalds } 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds /* the key is probably readable - now try to read it */ 7451da177e4SLinus Torvalds can_read_key: 7461da177e4SLinus Torvalds ret = key_validate(key); 7471da177e4SLinus Torvalds if (ret == 0) { 7481da177e4SLinus Torvalds ret = -EOPNOTSUPP; 7491da177e4SLinus Torvalds if (key->type->read) { 7501da177e4SLinus Torvalds /* read the data with the semaphore held (since we 7511da177e4SLinus Torvalds * might sleep) */ 7521da177e4SLinus Torvalds down_read(&key->sem); 7531da177e4SLinus Torvalds ret = key->type->read(key, buffer, buflen); 7541da177e4SLinus Torvalds up_read(&key->sem); 7551da177e4SLinus Torvalds } 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds error2: 7591da177e4SLinus Torvalds key_put(key); 7601da177e4SLinus Torvalds error: 7611da177e4SLinus Torvalds return ret; 762a8b17ed0SDavid Howells } 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds /* 765973c9f4fSDavid Howells * Change the ownership of a key 766973c9f4fSDavid Howells * 767973c9f4fSDavid Howells * The key must grant the caller Setattr permission for this to work, though 768973c9f4fSDavid Howells * the key need not be fully instantiated yet. For the UID to be changed, or 769973c9f4fSDavid Howells * for the GID to be changed to a group the caller is not a member of, the 770973c9f4fSDavid Howells * caller must have sysadmin capability. If either uid or gid is -1 then that 771973c9f4fSDavid Howells * attribute is not changed. 772973c9f4fSDavid Howells * 773973c9f4fSDavid Howells * If the UID is to be changed, the new user must have sufficient quota to 774973c9f4fSDavid Howells * accept the key. The quota deduction will be removed from the old user to 775973c9f4fSDavid Howells * the new user should the attribute be changed. 776973c9f4fSDavid Howells * 777973c9f4fSDavid Howells * If successful, 0 will be returned. 7781da177e4SLinus Torvalds */ 7799a56c2dbSEric W. Biederman long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group) 7801da177e4SLinus Torvalds { 7815801649dSFredrik Tolf struct key_user *newowner, *zapowner = NULL; 7821da177e4SLinus Torvalds struct key *key; 783664cceb0SDavid Howells key_ref_t key_ref; 7841da177e4SLinus Torvalds long ret; 7859a56c2dbSEric W. Biederman kuid_t uid; 7869a56c2dbSEric W. Biederman kgid_t gid; 7879a56c2dbSEric W. Biederman 7889a56c2dbSEric W. Biederman uid = make_kuid(current_user_ns(), user); 7899a56c2dbSEric W. Biederman gid = make_kgid(current_user_ns(), group); 7909a56c2dbSEric W. Biederman ret = -EINVAL; 7919a56c2dbSEric W. Biederman if ((user != (uid_t) -1) && !uid_valid(uid)) 7929a56c2dbSEric W. Biederman goto error; 7939a56c2dbSEric W. Biederman if ((group != (gid_t) -1) && !gid_valid(gid)) 7949a56c2dbSEric W. Biederman goto error; 7951da177e4SLinus Torvalds 7961da177e4SLinus Torvalds ret = 0; 7979a56c2dbSEric W. Biederman if (user == (uid_t) -1 && group == (gid_t) -1) 7981da177e4SLinus Torvalds goto error; 7991da177e4SLinus Torvalds 8005593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 8015593122eSDavid Howells KEY_SETATTR); 802664cceb0SDavid Howells if (IS_ERR(key_ref)) { 803664cceb0SDavid Howells ret = PTR_ERR(key_ref); 8041da177e4SLinus Torvalds goto error; 8051da177e4SLinus Torvalds } 8061da177e4SLinus Torvalds 807664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 808664cceb0SDavid Howells 8091da177e4SLinus Torvalds /* make the changes with the locks held to prevent chown/chown races */ 8101da177e4SLinus Torvalds ret = -EACCES; 8111da177e4SLinus Torvalds down_write(&key->sem); 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) { 8141da177e4SLinus Torvalds /* only the sysadmin can chown a key to some other UID */ 8159a56c2dbSEric W. Biederman if (user != (uid_t) -1 && !uid_eq(key->uid, uid)) 8165801649dSFredrik Tolf goto error_put; 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds /* only the sysadmin can set the key's GID to a group other 8191da177e4SLinus Torvalds * than one of those that the current process subscribes to */ 8209a56c2dbSEric W. Biederman if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid)) 8215801649dSFredrik Tolf goto error_put; 8221da177e4SLinus Torvalds } 8231da177e4SLinus Torvalds 8245801649dSFredrik Tolf /* change the UID */ 8259a56c2dbSEric W. Biederman if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) { 8265801649dSFredrik Tolf ret = -ENOMEM; 8279a56c2dbSEric W. Biederman newowner = key_user_lookup(uid); 8285801649dSFredrik Tolf if (!newowner) 8295801649dSFredrik Tolf goto error_put; 8305801649dSFredrik Tolf 8315801649dSFredrik Tolf /* transfer the quota burden to the new user */ 8325801649dSFredrik Tolf if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { 8339a56c2dbSEric W. Biederman unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ? 8340b77f5bfSDavid Howells key_quota_root_maxkeys : key_quota_maxkeys; 8359a56c2dbSEric W. Biederman unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ? 8360b77f5bfSDavid Howells key_quota_root_maxbytes : key_quota_maxbytes; 8370b77f5bfSDavid Howells 8385801649dSFredrik Tolf spin_lock(&newowner->lock); 8390b77f5bfSDavid Howells if (newowner->qnkeys + 1 >= maxkeys || 8400b77f5bfSDavid Howells newowner->qnbytes + key->quotalen >= maxbytes || 8410b77f5bfSDavid Howells newowner->qnbytes + key->quotalen < 8420b77f5bfSDavid Howells newowner->qnbytes) 8435801649dSFredrik Tolf goto quota_overrun; 8445801649dSFredrik Tolf 8455801649dSFredrik Tolf newowner->qnkeys++; 8465801649dSFredrik Tolf newowner->qnbytes += key->quotalen; 8475801649dSFredrik Tolf spin_unlock(&newowner->lock); 8485801649dSFredrik Tolf 8495801649dSFredrik Tolf spin_lock(&key->user->lock); 8505801649dSFredrik Tolf key->user->qnkeys--; 8515801649dSFredrik Tolf key->user->qnbytes -= key->quotalen; 8525801649dSFredrik Tolf spin_unlock(&key->user->lock); 8535801649dSFredrik Tolf } 8545801649dSFredrik Tolf 8555801649dSFredrik Tolf atomic_dec(&key->user->nkeys); 8565801649dSFredrik Tolf atomic_inc(&newowner->nkeys); 8575801649dSFredrik Tolf 8585801649dSFredrik Tolf if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) { 8595801649dSFredrik Tolf atomic_dec(&key->user->nikeys); 8605801649dSFredrik Tolf atomic_inc(&newowner->nikeys); 8615801649dSFredrik Tolf } 8625801649dSFredrik Tolf 8635801649dSFredrik Tolf zapowner = key->user; 8645801649dSFredrik Tolf key->user = newowner; 8655801649dSFredrik Tolf key->uid = uid; 8661da177e4SLinus Torvalds } 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds /* change the GID */ 8699a56c2dbSEric W. Biederman if (group != (gid_t) -1) 8701da177e4SLinus Torvalds key->gid = gid; 8711da177e4SLinus Torvalds 8721da177e4SLinus Torvalds ret = 0; 8731da177e4SLinus Torvalds 8745801649dSFredrik Tolf error_put: 8751da177e4SLinus Torvalds up_write(&key->sem); 8761da177e4SLinus Torvalds key_put(key); 8775801649dSFredrik Tolf if (zapowner) 8785801649dSFredrik Tolf key_user_put(zapowner); 8791da177e4SLinus Torvalds error: 8801da177e4SLinus Torvalds return ret; 8811da177e4SLinus Torvalds 8825801649dSFredrik Tolf quota_overrun: 8835801649dSFredrik Tolf spin_unlock(&newowner->lock); 8845801649dSFredrik Tolf zapowner = newowner; 8855801649dSFredrik Tolf ret = -EDQUOT; 8865801649dSFredrik Tolf goto error_put; 887a8b17ed0SDavid Howells } 8885801649dSFredrik Tolf 8891da177e4SLinus Torvalds /* 890973c9f4fSDavid Howells * Change the permission mask on a key. 891973c9f4fSDavid Howells * 892973c9f4fSDavid Howells * The key must grant the caller Setattr permission for this to work, though 893973c9f4fSDavid Howells * the key need not be fully instantiated yet. If the caller does not have 894973c9f4fSDavid Howells * sysadmin capability, it may only change the permission on keys that it owns. 8951da177e4SLinus Torvalds */ 8961da177e4SLinus Torvalds long keyctl_setperm_key(key_serial_t id, key_perm_t perm) 8971da177e4SLinus Torvalds { 8981da177e4SLinus Torvalds struct key *key; 899664cceb0SDavid Howells key_ref_t key_ref; 9001da177e4SLinus Torvalds long ret; 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds ret = -EINVAL; 903664cceb0SDavid Howells if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL)) 9041da177e4SLinus Torvalds goto error; 9051da177e4SLinus Torvalds 9065593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 9075593122eSDavid Howells KEY_SETATTR); 908664cceb0SDavid Howells if (IS_ERR(key_ref)) { 909664cceb0SDavid Howells ret = PTR_ERR(key_ref); 9101da177e4SLinus Torvalds goto error; 9111da177e4SLinus Torvalds } 9121da177e4SLinus Torvalds 913664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 914664cceb0SDavid Howells 91576d8aeabSDavid Howells /* make the changes with the locks held to prevent chown/chmod races */ 9161da177e4SLinus Torvalds ret = -EACCES; 9171da177e4SLinus Torvalds down_write(&key->sem); 9181da177e4SLinus Torvalds 91976d8aeabSDavid Howells /* if we're not the sysadmin, we can only change a key that we own */ 9209a56c2dbSEric W. Biederman if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) { 9211da177e4SLinus Torvalds key->perm = perm; 9221da177e4SLinus Torvalds ret = 0; 92376d8aeabSDavid Howells } 9241da177e4SLinus Torvalds 9251da177e4SLinus Torvalds up_write(&key->sem); 9261da177e4SLinus Torvalds key_put(key); 9271da177e4SLinus Torvalds error: 9281da177e4SLinus Torvalds return ret; 929a8b17ed0SDavid Howells } 9301da177e4SLinus Torvalds 9318bbf4976SDavid Howells /* 932973c9f4fSDavid Howells * Get the destination keyring for instantiation and check that the caller has 933973c9f4fSDavid Howells * Write permission on it. 9348bbf4976SDavid Howells */ 9358bbf4976SDavid Howells static long get_instantiation_keyring(key_serial_t ringid, 9368bbf4976SDavid Howells struct request_key_auth *rka, 9378bbf4976SDavid Howells struct key **_dest_keyring) 9388bbf4976SDavid Howells { 9398bbf4976SDavid Howells key_ref_t dkref; 9408bbf4976SDavid Howells 9418bbf4976SDavid Howells *_dest_keyring = NULL; 942eca1bf5bSDavid Howells 943eca1bf5bSDavid Howells /* just return a NULL pointer if we weren't asked to make a link */ 944eca1bf5bSDavid Howells if (ringid == 0) 9458bbf4976SDavid Howells return 0; 9468bbf4976SDavid Howells 9478bbf4976SDavid Howells /* if a specific keyring is nominated by ID, then use that */ 9488bbf4976SDavid Howells if (ringid > 0) { 9495593122eSDavid Howells dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); 9508bbf4976SDavid Howells if (IS_ERR(dkref)) 9518bbf4976SDavid Howells return PTR_ERR(dkref); 9528bbf4976SDavid Howells *_dest_keyring = key_ref_to_ptr(dkref); 9538bbf4976SDavid Howells return 0; 9548bbf4976SDavid Howells } 9558bbf4976SDavid Howells 9568bbf4976SDavid Howells if (ringid == KEY_SPEC_REQKEY_AUTH_KEY) 9578bbf4976SDavid Howells return -EINVAL; 9588bbf4976SDavid Howells 9598bbf4976SDavid Howells /* otherwise specify the destination keyring recorded in the 9608bbf4976SDavid Howells * authorisation key (any KEY_SPEC_*_KEYRING) */ 9618bbf4976SDavid Howells if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) { 96221279cfaSDavid Howells *_dest_keyring = key_get(rka->dest_keyring); 9638bbf4976SDavid Howells return 0; 9648bbf4976SDavid Howells } 9658bbf4976SDavid Howells 9668bbf4976SDavid Howells return -ENOKEY; 9678bbf4976SDavid Howells } 9688bbf4976SDavid Howells 969d84f4f99SDavid Howells /* 970973c9f4fSDavid Howells * Change the request_key authorisation key on the current process. 971d84f4f99SDavid Howells */ 972d84f4f99SDavid Howells static int keyctl_change_reqkey_auth(struct key *key) 973d84f4f99SDavid Howells { 974d84f4f99SDavid Howells struct cred *new; 975d84f4f99SDavid Howells 976d84f4f99SDavid Howells new = prepare_creds(); 977d84f4f99SDavid Howells if (!new) 978d84f4f99SDavid Howells return -ENOMEM; 979d84f4f99SDavid Howells 980d84f4f99SDavid Howells key_put(new->request_key_auth); 981d84f4f99SDavid Howells new->request_key_auth = key_get(key); 982d84f4f99SDavid Howells 983d84f4f99SDavid Howells return commit_creds(new); 984d84f4f99SDavid Howells } 985d84f4f99SDavid Howells 9861da177e4SLinus Torvalds /* 987ee009e4aSDavid Howells * Copy the iovec data from userspace 988ee009e4aSDavid Howells */ 989ee009e4aSDavid Howells static long copy_from_user_iovec(void *buffer, const struct iovec *iov, 990ee009e4aSDavid Howells unsigned ioc) 991ee009e4aSDavid Howells { 992ee009e4aSDavid Howells for (; ioc > 0; ioc--) { 993ee009e4aSDavid Howells if (copy_from_user(buffer, iov->iov_base, iov->iov_len) != 0) 994ee009e4aSDavid Howells return -EFAULT; 995ee009e4aSDavid Howells buffer += iov->iov_len; 996ee009e4aSDavid Howells iov++; 997ee009e4aSDavid Howells } 998ee009e4aSDavid Howells return 0; 999ee009e4aSDavid Howells } 1000ee009e4aSDavid Howells 1001ee009e4aSDavid Howells /* 1002973c9f4fSDavid Howells * Instantiate a key with the specified payload and link the key into the 1003973c9f4fSDavid Howells * destination keyring if one is given. 1004973c9f4fSDavid Howells * 1005973c9f4fSDavid Howells * The caller must have the appropriate instantiation permit set for this to 1006973c9f4fSDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1007973c9f4fSDavid Howells * 1008973c9f4fSDavid Howells * If successful, 0 will be returned. 10091da177e4SLinus Torvalds */ 1010ee009e4aSDavid Howells long keyctl_instantiate_key_common(key_serial_t id, 1011ee009e4aSDavid Howells const struct iovec *payload_iov, 1012ee009e4aSDavid Howells unsigned ioc, 10131da177e4SLinus Torvalds size_t plen, 10141da177e4SLinus Torvalds key_serial_t ringid) 10151da177e4SLinus Torvalds { 1016d84f4f99SDavid Howells const struct cred *cred = current_cred(); 10173e30148cSDavid Howells struct request_key_auth *rka; 10188bbf4976SDavid Howells struct key *instkey, *dest_keyring; 10191da177e4SLinus Torvalds void *payload; 10201da177e4SLinus Torvalds long ret; 102138bbca6bSDavid Howells bool vm = false; 10221da177e4SLinus Torvalds 1023d84f4f99SDavid Howells kenter("%d,,%zu,%d", id, plen, ringid); 1024d84f4f99SDavid Howells 10251da177e4SLinus Torvalds ret = -EINVAL; 102638bbca6bSDavid Howells if (plen > 1024 * 1024 - 1) 10271da177e4SLinus Torvalds goto error; 10281da177e4SLinus Torvalds 1029b5f545c8SDavid Howells /* the appropriate instantiation authorisation key must have been 1030b5f545c8SDavid Howells * assumed before calling this */ 1031b5f545c8SDavid Howells ret = -EPERM; 1032d84f4f99SDavid Howells instkey = cred->request_key_auth; 1033b5f545c8SDavid Howells if (!instkey) 1034b5f545c8SDavid Howells goto error; 1035b5f545c8SDavid Howells 1036b5f545c8SDavid Howells rka = instkey->payload.data; 1037b5f545c8SDavid Howells if (rka->target_key->serial != id) 1038b5f545c8SDavid Howells goto error; 1039b5f545c8SDavid Howells 10401da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 10411da177e4SLinus Torvalds payload = NULL; 10421da177e4SLinus Torvalds 1043ee009e4aSDavid Howells if (payload_iov) { 10441da177e4SLinus Torvalds ret = -ENOMEM; 10451da177e4SLinus Torvalds payload = kmalloc(plen, GFP_KERNEL); 104638bbca6bSDavid Howells if (!payload) { 104738bbca6bSDavid Howells if (plen <= PAGE_SIZE) 104838bbca6bSDavid Howells goto error; 104938bbca6bSDavid Howells vm = true; 105038bbca6bSDavid Howells payload = vmalloc(plen); 10511da177e4SLinus Torvalds if (!payload) 10521da177e4SLinus Torvalds goto error; 105338bbca6bSDavid Howells } 10541da177e4SLinus Torvalds 1055ee009e4aSDavid Howells ret = copy_from_user_iovec(payload, payload_iov, ioc); 1056ee009e4aSDavid Howells if (ret < 0) 10571da177e4SLinus Torvalds goto error2; 10581da177e4SLinus Torvalds } 10591da177e4SLinus Torvalds 10603e30148cSDavid Howells /* find the destination keyring amongst those belonging to the 10613e30148cSDavid Howells * requesting task */ 10628bbf4976SDavid Howells ret = get_instantiation_keyring(ringid, rka, &dest_keyring); 10638bbf4976SDavid Howells if (ret < 0) 1064b5f545c8SDavid Howells goto error2; 10651da177e4SLinus Torvalds 10661da177e4SLinus Torvalds /* instantiate the key and link it into a keyring */ 10673e30148cSDavid Howells ret = key_instantiate_and_link(rka->target_key, payload, plen, 10688bbf4976SDavid Howells dest_keyring, instkey); 10691da177e4SLinus Torvalds 10708bbf4976SDavid Howells key_put(dest_keyring); 1071b5f545c8SDavid Howells 1072b5f545c8SDavid Howells /* discard the assumed authority if it's just been disabled by 1073b5f545c8SDavid Howells * instantiation of the key */ 1074d84f4f99SDavid Howells if (ret == 0) 1075d84f4f99SDavid Howells keyctl_change_reqkey_auth(NULL); 1076b5f545c8SDavid Howells 10771da177e4SLinus Torvalds error2: 107838bbca6bSDavid Howells if (!vm) 10791da177e4SLinus Torvalds kfree(payload); 108038bbca6bSDavid Howells else 108138bbca6bSDavid Howells vfree(payload); 10821da177e4SLinus Torvalds error: 10831da177e4SLinus Torvalds return ret; 1084a8b17ed0SDavid Howells } 10851da177e4SLinus Torvalds 10861da177e4SLinus Torvalds /* 1087ee009e4aSDavid Howells * Instantiate a key with the specified payload and link the key into the 1088ee009e4aSDavid Howells * destination keyring if one is given. 1089ee009e4aSDavid Howells * 1090ee009e4aSDavid Howells * The caller must have the appropriate instantiation permit set for this to 1091ee009e4aSDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1092ee009e4aSDavid Howells * 1093ee009e4aSDavid Howells * If successful, 0 will be returned. 1094ee009e4aSDavid Howells */ 1095ee009e4aSDavid Howells long keyctl_instantiate_key(key_serial_t id, 1096ee009e4aSDavid Howells const void __user *_payload, 1097ee009e4aSDavid Howells size_t plen, 1098ee009e4aSDavid Howells key_serial_t ringid) 1099ee009e4aSDavid Howells { 1100ee009e4aSDavid Howells if (_payload && plen) { 1101ee009e4aSDavid Howells struct iovec iov[1] = { 1102ee009e4aSDavid Howells [0].iov_base = (void __user *)_payload, 1103ee009e4aSDavid Howells [0].iov_len = plen 1104ee009e4aSDavid Howells }; 1105ee009e4aSDavid Howells 1106ee009e4aSDavid Howells return keyctl_instantiate_key_common(id, iov, 1, plen, ringid); 1107ee009e4aSDavid Howells } 1108ee009e4aSDavid Howells 1109ee009e4aSDavid Howells return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid); 1110ee009e4aSDavid Howells } 1111ee009e4aSDavid Howells 1112ee009e4aSDavid Howells /* 1113ee009e4aSDavid Howells * Instantiate a key with the specified multipart payload and link the key into 1114ee009e4aSDavid Howells * the destination keyring if one is given. 1115ee009e4aSDavid Howells * 1116ee009e4aSDavid Howells * The caller must have the appropriate instantiation permit set for this to 1117ee009e4aSDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1118ee009e4aSDavid Howells * 1119ee009e4aSDavid Howells * If successful, 0 will be returned. 1120ee009e4aSDavid Howells */ 1121ee009e4aSDavid Howells long keyctl_instantiate_key_iov(key_serial_t id, 1122ee009e4aSDavid Howells const struct iovec __user *_payload_iov, 1123ee009e4aSDavid Howells unsigned ioc, 1124ee009e4aSDavid Howells key_serial_t ringid) 1125ee009e4aSDavid Howells { 1126ee009e4aSDavid Howells struct iovec iovstack[UIO_FASTIOV], *iov = iovstack; 1127ee009e4aSDavid Howells long ret; 1128ee009e4aSDavid Howells 1129423b9788SDavid Howells if (!_payload_iov || !ioc) 1130ee009e4aSDavid Howells goto no_payload; 1131ee009e4aSDavid Howells 1132ee009e4aSDavid Howells ret = rw_copy_check_uvector(WRITE, _payload_iov, ioc, 1133ac34ebb3SChristopher Yeoh ARRAY_SIZE(iovstack), iovstack, &iov); 1134ee009e4aSDavid Howells if (ret < 0) 1135a84a9219SAlan Cox goto err; 1136ee009e4aSDavid Howells if (ret == 0) 1137ee009e4aSDavid Howells goto no_payload_free; 1138ee009e4aSDavid Howells 1139ee009e4aSDavid Howells ret = keyctl_instantiate_key_common(id, iov, ioc, ret, ringid); 1140a84a9219SAlan Cox err: 1141ee009e4aSDavid Howells if (iov != iovstack) 1142ee009e4aSDavid Howells kfree(iov); 1143ee009e4aSDavid Howells return ret; 1144ee009e4aSDavid Howells 1145ee009e4aSDavid Howells no_payload_free: 1146ee009e4aSDavid Howells if (iov != iovstack) 1147ee009e4aSDavid Howells kfree(iov); 1148ee009e4aSDavid Howells no_payload: 1149ee009e4aSDavid Howells return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid); 1150ee009e4aSDavid Howells } 1151ee009e4aSDavid Howells 1152ee009e4aSDavid Howells /* 1153973c9f4fSDavid Howells * Negatively instantiate the key with the given timeout (in seconds) and link 1154973c9f4fSDavid Howells * the key into the destination keyring if one is given. 1155973c9f4fSDavid Howells * 1156973c9f4fSDavid Howells * The caller must have the appropriate instantiation permit set for this to 1157973c9f4fSDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1158973c9f4fSDavid Howells * 1159973c9f4fSDavid Howells * The key and any links to the key will be automatically garbage collected 1160973c9f4fSDavid Howells * after the timeout expires. 1161973c9f4fSDavid Howells * 1162973c9f4fSDavid Howells * Negative keys are used to rate limit repeated request_key() calls by causing 1163973c9f4fSDavid Howells * them to return -ENOKEY until the negative key expires. 1164973c9f4fSDavid Howells * 1165973c9f4fSDavid Howells * If successful, 0 will be returned. 11661da177e4SLinus Torvalds */ 11671da177e4SLinus Torvalds long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid) 11681da177e4SLinus Torvalds { 1169fdd1b945SDavid Howells return keyctl_reject_key(id, timeout, ENOKEY, ringid); 1170fdd1b945SDavid Howells } 1171fdd1b945SDavid Howells 1172fdd1b945SDavid Howells /* 1173fdd1b945SDavid Howells * Negatively instantiate the key with the given timeout (in seconds) and error 1174fdd1b945SDavid Howells * code and link the key into the destination keyring if one is given. 1175fdd1b945SDavid Howells * 1176fdd1b945SDavid Howells * The caller must have the appropriate instantiation permit set for this to 1177fdd1b945SDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1178fdd1b945SDavid Howells * 1179fdd1b945SDavid Howells * The key and any links to the key will be automatically garbage collected 1180fdd1b945SDavid Howells * after the timeout expires. 1181fdd1b945SDavid Howells * 1182fdd1b945SDavid Howells * Negative keys are used to rate limit repeated request_key() calls by causing 1183fdd1b945SDavid Howells * them to return the specified error code until the negative key expires. 1184fdd1b945SDavid Howells * 1185fdd1b945SDavid Howells * If successful, 0 will be returned. 1186fdd1b945SDavid Howells */ 1187fdd1b945SDavid Howells long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error, 1188fdd1b945SDavid Howells key_serial_t ringid) 1189fdd1b945SDavid Howells { 1190d84f4f99SDavid Howells const struct cred *cred = current_cred(); 11913e30148cSDavid Howells struct request_key_auth *rka; 11928bbf4976SDavid Howells struct key *instkey, *dest_keyring; 11931da177e4SLinus Torvalds long ret; 11941da177e4SLinus Torvalds 1195fdd1b945SDavid Howells kenter("%d,%u,%u,%d", id, timeout, error, ringid); 1196fdd1b945SDavid Howells 1197fdd1b945SDavid Howells /* must be a valid error code and mustn't be a kernel special */ 1198fdd1b945SDavid Howells if (error <= 0 || 1199fdd1b945SDavid Howells error >= MAX_ERRNO || 1200fdd1b945SDavid Howells error == ERESTARTSYS || 1201fdd1b945SDavid Howells error == ERESTARTNOINTR || 1202fdd1b945SDavid Howells error == ERESTARTNOHAND || 1203fdd1b945SDavid Howells error == ERESTART_RESTARTBLOCK) 1204fdd1b945SDavid Howells return -EINVAL; 1205d84f4f99SDavid Howells 1206b5f545c8SDavid Howells /* the appropriate instantiation authorisation key must have been 1207b5f545c8SDavid Howells * assumed before calling this */ 1208b5f545c8SDavid Howells ret = -EPERM; 1209d84f4f99SDavid Howells instkey = cred->request_key_auth; 1210b5f545c8SDavid Howells if (!instkey) 12111da177e4SLinus Torvalds goto error; 12121da177e4SLinus Torvalds 12133e30148cSDavid Howells rka = instkey->payload.data; 1214b5f545c8SDavid Howells if (rka->target_key->serial != id) 1215b5f545c8SDavid Howells goto error; 12163e30148cSDavid Howells 12171da177e4SLinus Torvalds /* find the destination keyring if present (which must also be 12181da177e4SLinus Torvalds * writable) */ 12198bbf4976SDavid Howells ret = get_instantiation_keyring(ringid, rka, &dest_keyring); 12208bbf4976SDavid Howells if (ret < 0) 1221b5f545c8SDavid Howells goto error; 12221da177e4SLinus Torvalds 12231da177e4SLinus Torvalds /* instantiate the key and link it into a keyring */ 1224fdd1b945SDavid Howells ret = key_reject_and_link(rka->target_key, timeout, error, 12258bbf4976SDavid Howells dest_keyring, instkey); 12261da177e4SLinus Torvalds 12278bbf4976SDavid Howells key_put(dest_keyring); 1228b5f545c8SDavid Howells 1229b5f545c8SDavid Howells /* discard the assumed authority if it's just been disabled by 1230b5f545c8SDavid Howells * instantiation of the key */ 1231d84f4f99SDavid Howells if (ret == 0) 1232d84f4f99SDavid Howells keyctl_change_reqkey_auth(NULL); 1233b5f545c8SDavid Howells 12341da177e4SLinus Torvalds error: 12351da177e4SLinus Torvalds return ret; 1236a8b17ed0SDavid Howells } 12371da177e4SLinus Torvalds 12381da177e4SLinus Torvalds /* 1239973c9f4fSDavid Howells * Read or set the default keyring in which request_key() will cache keys and 1240973c9f4fSDavid Howells * return the old setting. 1241973c9f4fSDavid Howells * 1242973c9f4fSDavid Howells * If a process keyring is specified then this will be created if it doesn't 1243973c9f4fSDavid Howells * yet exist. The old setting will be returned if successful. 12443e30148cSDavid Howells */ 12453e30148cSDavid Howells long keyctl_set_reqkey_keyring(int reqkey_defl) 12463e30148cSDavid Howells { 1247d84f4f99SDavid Howells struct cred *new; 1248d84f4f99SDavid Howells int ret, old_setting; 1249d84f4f99SDavid Howells 1250d84f4f99SDavid Howells old_setting = current_cred_xxx(jit_keyring); 1251d84f4f99SDavid Howells 1252d84f4f99SDavid Howells if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE) 1253d84f4f99SDavid Howells return old_setting; 1254d84f4f99SDavid Howells 1255d84f4f99SDavid Howells new = prepare_creds(); 1256d84f4f99SDavid Howells if (!new) 1257d84f4f99SDavid Howells return -ENOMEM; 12583e30148cSDavid Howells 12593e30148cSDavid Howells switch (reqkey_defl) { 12603e30148cSDavid Howells case KEY_REQKEY_DEFL_THREAD_KEYRING: 1261d84f4f99SDavid Howells ret = install_thread_keyring_to_cred(new); 12623e30148cSDavid Howells if (ret < 0) 1263d84f4f99SDavid Howells goto error; 12643e30148cSDavid Howells goto set; 12653e30148cSDavid Howells 12663e30148cSDavid Howells case KEY_REQKEY_DEFL_PROCESS_KEYRING: 1267d84f4f99SDavid Howells ret = install_process_keyring_to_cred(new); 1268d84f4f99SDavid Howells if (ret < 0) { 1269d84f4f99SDavid Howells if (ret != -EEXIST) 1270d84f4f99SDavid Howells goto error; 1271d84f4f99SDavid Howells ret = 0; 1272d84f4f99SDavid Howells } 1273d84f4f99SDavid Howells goto set; 12743e30148cSDavid Howells 12753e30148cSDavid Howells case KEY_REQKEY_DEFL_DEFAULT: 12763e30148cSDavid Howells case KEY_REQKEY_DEFL_SESSION_KEYRING: 12773e30148cSDavid Howells case KEY_REQKEY_DEFL_USER_KEYRING: 12783e30148cSDavid Howells case KEY_REQKEY_DEFL_USER_SESSION_KEYRING: 1279d84f4f99SDavid Howells case KEY_REQKEY_DEFL_REQUESTOR_KEYRING: 1280d84f4f99SDavid Howells goto set; 12813e30148cSDavid Howells 12823e30148cSDavid Howells case KEY_REQKEY_DEFL_NO_CHANGE: 12833e30148cSDavid Howells case KEY_REQKEY_DEFL_GROUP_KEYRING: 12843e30148cSDavid Howells default: 1285d84f4f99SDavid Howells ret = -EINVAL; 1286d84f4f99SDavid Howells goto error; 12873e30148cSDavid Howells } 12883e30148cSDavid Howells 1289d84f4f99SDavid Howells set: 1290d84f4f99SDavid Howells new->jit_keyring = reqkey_defl; 1291d84f4f99SDavid Howells commit_creds(new); 1292d84f4f99SDavid Howells return old_setting; 1293d84f4f99SDavid Howells error: 1294d84f4f99SDavid Howells abort_creds(new); 12954303ef19SDan Carpenter return ret; 1296a8b17ed0SDavid Howells } 1297d84f4f99SDavid Howells 12983e30148cSDavid Howells /* 1299973c9f4fSDavid Howells * Set or clear the timeout on a key. 1300973c9f4fSDavid Howells * 1301973c9f4fSDavid Howells * Either the key must grant the caller Setattr permission or else the caller 1302973c9f4fSDavid Howells * must hold an instantiation authorisation token for the key. 1303973c9f4fSDavid Howells * 1304973c9f4fSDavid Howells * The timeout is either 0 to clear the timeout, or a number of seconds from 1305973c9f4fSDavid Howells * the current time. The key and any links to the key will be automatically 1306973c9f4fSDavid Howells * garbage collected after the timeout expires. 1307973c9f4fSDavid Howells * 1308973c9f4fSDavid Howells * If successful, 0 is returned. 1309017679c4SDavid Howells */ 1310017679c4SDavid Howells long keyctl_set_timeout(key_serial_t id, unsigned timeout) 1311017679c4SDavid Howells { 13129156235bSDavid Howells struct key *key, *instkey; 1313017679c4SDavid Howells key_ref_t key_ref; 1314017679c4SDavid Howells long ret; 1315017679c4SDavid Howells 13165593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 13175593122eSDavid Howells KEY_SETATTR); 1318017679c4SDavid Howells if (IS_ERR(key_ref)) { 13199156235bSDavid Howells /* setting the timeout on a key under construction is permitted 13209156235bSDavid Howells * if we have the authorisation token handy */ 13219156235bSDavid Howells if (PTR_ERR(key_ref) == -EACCES) { 13229156235bSDavid Howells instkey = key_get_instantiation_authkey(id); 13239156235bSDavid Howells if (!IS_ERR(instkey)) { 13249156235bSDavid Howells key_put(instkey); 13259156235bSDavid Howells key_ref = lookup_user_key(id, 13269156235bSDavid Howells KEY_LOOKUP_PARTIAL, 13279156235bSDavid Howells 0); 13289156235bSDavid Howells if (!IS_ERR(key_ref)) 13299156235bSDavid Howells goto okay; 13309156235bSDavid Howells } 13319156235bSDavid Howells } 13329156235bSDavid Howells 1333017679c4SDavid Howells ret = PTR_ERR(key_ref); 1334017679c4SDavid Howells goto error; 1335017679c4SDavid Howells } 1336017679c4SDavid Howells 13379156235bSDavid Howells okay: 1338017679c4SDavid Howells key = key_ref_to_ptr(key_ref); 133959e6b9c1SBryan Schumaker key_set_timeout(key, timeout); 1340017679c4SDavid Howells key_put(key); 1341017679c4SDavid Howells 1342017679c4SDavid Howells ret = 0; 1343017679c4SDavid Howells error: 1344017679c4SDavid Howells return ret; 1345a8b17ed0SDavid Howells } 1346017679c4SDavid Howells 1347017679c4SDavid Howells /* 1348973c9f4fSDavid Howells * Assume (or clear) the authority to instantiate the specified key. 1349973c9f4fSDavid Howells * 1350973c9f4fSDavid Howells * This sets the authoritative token currently in force for key instantiation. 1351973c9f4fSDavid Howells * This must be done for a key to be instantiated. It has the effect of making 1352973c9f4fSDavid Howells * available all the keys from the caller of the request_key() that created a 1353973c9f4fSDavid Howells * key to request_key() calls made by the caller of this function. 1354973c9f4fSDavid Howells * 1355973c9f4fSDavid Howells * The caller must have the instantiation key in their process keyrings with a 1356973c9f4fSDavid Howells * Search permission grant available to the caller. 1357973c9f4fSDavid Howells * 1358973c9f4fSDavid Howells * If the ID given is 0, then the setting will be cleared and 0 returned. 1359973c9f4fSDavid Howells * 1360973c9f4fSDavid Howells * If the ID given has a matching an authorisation key, then that key will be 1361973c9f4fSDavid Howells * set and its ID will be returned. The authorisation key can be read to get 1362973c9f4fSDavid Howells * the callout information passed to request_key(). 1363b5f545c8SDavid Howells */ 1364b5f545c8SDavid Howells long keyctl_assume_authority(key_serial_t id) 1365b5f545c8SDavid Howells { 1366b5f545c8SDavid Howells struct key *authkey; 1367b5f545c8SDavid Howells long ret; 1368b5f545c8SDavid Howells 1369b5f545c8SDavid Howells /* special key IDs aren't permitted */ 1370b5f545c8SDavid Howells ret = -EINVAL; 1371b5f545c8SDavid Howells if (id < 0) 1372b5f545c8SDavid Howells goto error; 1373b5f545c8SDavid Howells 1374b5f545c8SDavid Howells /* we divest ourselves of authority if given an ID of 0 */ 1375b5f545c8SDavid Howells if (id == 0) { 1376d84f4f99SDavid Howells ret = keyctl_change_reqkey_auth(NULL); 1377b5f545c8SDavid Howells goto error; 1378b5f545c8SDavid Howells } 1379b5f545c8SDavid Howells 1380b5f545c8SDavid Howells /* attempt to assume the authority temporarily granted to us whilst we 1381b5f545c8SDavid Howells * instantiate the specified key 1382b5f545c8SDavid Howells * - the authorisation key must be in the current task's keyrings 1383b5f545c8SDavid Howells * somewhere 1384b5f545c8SDavid Howells */ 1385b5f545c8SDavid Howells authkey = key_get_instantiation_authkey(id); 1386b5f545c8SDavid Howells if (IS_ERR(authkey)) { 1387b5f545c8SDavid Howells ret = PTR_ERR(authkey); 1388b5f545c8SDavid Howells goto error; 1389b5f545c8SDavid Howells } 1390b5f545c8SDavid Howells 1391d84f4f99SDavid Howells ret = keyctl_change_reqkey_auth(authkey); 1392d84f4f99SDavid Howells if (ret < 0) 1393d84f4f99SDavid Howells goto error; 1394d84f4f99SDavid Howells key_put(authkey); 1395b5f545c8SDavid Howells 1396d84f4f99SDavid Howells ret = authkey->serial; 1397b5f545c8SDavid Howells error: 1398b5f545c8SDavid Howells return ret; 1399a8b17ed0SDavid Howells } 1400b5f545c8SDavid Howells 140170a5bb72SDavid Howells /* 1402973c9f4fSDavid Howells * Get a key's the LSM security label. 1403973c9f4fSDavid Howells * 1404973c9f4fSDavid Howells * The key must grant the caller View permission for this to work. 1405973c9f4fSDavid Howells * 1406973c9f4fSDavid Howells * If there's a buffer, then up to buflen bytes of data will be placed into it. 1407973c9f4fSDavid Howells * 1408973c9f4fSDavid Howells * If successful, the amount of information available will be returned, 1409973c9f4fSDavid Howells * irrespective of how much was copied (including the terminal NUL). 141070a5bb72SDavid Howells */ 141170a5bb72SDavid Howells long keyctl_get_security(key_serial_t keyid, 141270a5bb72SDavid Howells char __user *buffer, 141370a5bb72SDavid Howells size_t buflen) 141470a5bb72SDavid Howells { 141570a5bb72SDavid Howells struct key *key, *instkey; 141670a5bb72SDavid Howells key_ref_t key_ref; 141770a5bb72SDavid Howells char *context; 141870a5bb72SDavid Howells long ret; 141970a5bb72SDavid Howells 14205593122eSDavid Howells key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW); 142170a5bb72SDavid Howells if (IS_ERR(key_ref)) { 142270a5bb72SDavid Howells if (PTR_ERR(key_ref) != -EACCES) 142370a5bb72SDavid Howells return PTR_ERR(key_ref); 142470a5bb72SDavid Howells 142570a5bb72SDavid Howells /* viewing a key under construction is also permitted if we 142670a5bb72SDavid Howells * have the authorisation token handy */ 142770a5bb72SDavid Howells instkey = key_get_instantiation_authkey(keyid); 142870a5bb72SDavid Howells if (IS_ERR(instkey)) 1429fa1cc7b5SRoel Kluin return PTR_ERR(instkey); 143070a5bb72SDavid Howells key_put(instkey); 143170a5bb72SDavid Howells 14325593122eSDavid Howells key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0); 143370a5bb72SDavid Howells if (IS_ERR(key_ref)) 143470a5bb72SDavid Howells return PTR_ERR(key_ref); 143570a5bb72SDavid Howells } 143670a5bb72SDavid Howells 143770a5bb72SDavid Howells key = key_ref_to_ptr(key_ref); 143870a5bb72SDavid Howells ret = security_key_getsecurity(key, &context); 143970a5bb72SDavid Howells if (ret == 0) { 144070a5bb72SDavid Howells /* if no information was returned, give userspace an empty 144170a5bb72SDavid Howells * string */ 144270a5bb72SDavid Howells ret = 1; 144370a5bb72SDavid Howells if (buffer && buflen > 0 && 144470a5bb72SDavid Howells copy_to_user(buffer, "", 1) != 0) 144570a5bb72SDavid Howells ret = -EFAULT; 144670a5bb72SDavid Howells } else if (ret > 0) { 144770a5bb72SDavid Howells /* return as much data as there's room for */ 144870a5bb72SDavid Howells if (buffer && buflen > 0) { 144970a5bb72SDavid Howells if (buflen > ret) 145070a5bb72SDavid Howells buflen = ret; 145170a5bb72SDavid Howells 145270a5bb72SDavid Howells if (copy_to_user(buffer, context, buflen) != 0) 145370a5bb72SDavid Howells ret = -EFAULT; 145470a5bb72SDavid Howells } 145570a5bb72SDavid Howells 145670a5bb72SDavid Howells kfree(context); 145770a5bb72SDavid Howells } 145870a5bb72SDavid Howells 145970a5bb72SDavid Howells key_ref_put(key_ref); 146070a5bb72SDavid Howells return ret; 146170a5bb72SDavid Howells } 146270a5bb72SDavid Howells 1463ee18d64cSDavid Howells /* 1464973c9f4fSDavid Howells * Attempt to install the calling process's session keyring on the process's 1465973c9f4fSDavid Howells * parent process. 1466973c9f4fSDavid Howells * 1467973c9f4fSDavid Howells * The keyring must exist and must grant the caller LINK permission, and the 1468973c9f4fSDavid Howells * parent process must be single-threaded and must have the same effective 1469973c9f4fSDavid Howells * ownership as this process and mustn't be SUID/SGID. 1470973c9f4fSDavid Howells * 1471973c9f4fSDavid Howells * The keyring will be emplaced on the parent when it next resumes userspace. 1472973c9f4fSDavid Howells * 1473973c9f4fSDavid Howells * If successful, 0 will be returned. 1474ee18d64cSDavid Howells */ 1475ee18d64cSDavid Howells long keyctl_session_to_parent(void) 1476ee18d64cSDavid Howells { 1477ee18d64cSDavid Howells struct task_struct *me, *parent; 1478ee18d64cSDavid Howells const struct cred *mycred, *pcred; 147967d12145SAl Viro struct callback_head *newwork, *oldwork; 1480ee18d64cSDavid Howells key_ref_t keyring_r; 1481413cd3d9SOleg Nesterov struct cred *cred; 1482ee18d64cSDavid Howells int ret; 1483ee18d64cSDavid Howells 1484ee18d64cSDavid Howells keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_LINK); 1485ee18d64cSDavid Howells if (IS_ERR(keyring_r)) 1486ee18d64cSDavid Howells return PTR_ERR(keyring_r); 1487ee18d64cSDavid Howells 1488413cd3d9SOleg Nesterov ret = -ENOMEM; 1489413cd3d9SOleg Nesterov 1490ee18d64cSDavid Howells /* our parent is going to need a new cred struct, a new tgcred struct 1491ee18d64cSDavid Howells * and new security data, so we allocate them here to prevent ENOMEM in 1492ee18d64cSDavid Howells * our parent */ 1493ee18d64cSDavid Howells cred = cred_alloc_blank(); 1494ee18d64cSDavid Howells if (!cred) 149567d12145SAl Viro goto error_keyring; 149667d12145SAl Viro newwork = &cred->rcu; 1497ee18d64cSDavid Howells 14983a50597dSDavid Howells cred->session_keyring = key_ref_to_ptr(keyring_r); 14993a50597dSDavid Howells keyring_r = NULL; 150067d12145SAl Viro init_task_work(newwork, key_change_session_keyring); 1501ee18d64cSDavid Howells 1502ee18d64cSDavid Howells me = current; 15039d1ac65aSDavid Howells rcu_read_lock(); 1504ee18d64cSDavid Howells write_lock_irq(&tasklist_lock); 1505ee18d64cSDavid Howells 1506ee18d64cSDavid Howells ret = -EPERM; 1507413cd3d9SOleg Nesterov oldwork = NULL; 1508413cd3d9SOleg Nesterov parent = me->real_parent; 1509ee18d64cSDavid Howells 1510ee18d64cSDavid Howells /* the parent mustn't be init and mustn't be a kernel thread */ 1511ee18d64cSDavid Howells if (parent->pid <= 1 || !parent->mm) 1512413cd3d9SOleg Nesterov goto unlock; 1513ee18d64cSDavid Howells 1514ee18d64cSDavid Howells /* the parent must be single threaded */ 1515dd98acf7SOleg Nesterov if (!thread_group_empty(parent)) 1516413cd3d9SOleg Nesterov goto unlock; 1517ee18d64cSDavid Howells 1518ee18d64cSDavid Howells /* the parent and the child must have different session keyrings or 1519ee18d64cSDavid Howells * there's no point */ 1520ee18d64cSDavid Howells mycred = current_cred(); 1521ee18d64cSDavid Howells pcred = __task_cred(parent); 1522ee18d64cSDavid Howells if (mycred == pcred || 15233a50597dSDavid Howells mycred->session_keyring == pcred->session_keyring) { 1524413cd3d9SOleg Nesterov ret = 0; 1525413cd3d9SOleg Nesterov goto unlock; 1526413cd3d9SOleg Nesterov } 1527ee18d64cSDavid Howells 1528ee18d64cSDavid Howells /* the parent must have the same effective ownership and mustn't be 1529ee18d64cSDavid Howells * SUID/SGID */ 15309a56c2dbSEric W. Biederman if (!uid_eq(pcred->uid, mycred->euid) || 15319a56c2dbSEric W. Biederman !uid_eq(pcred->euid, mycred->euid) || 15329a56c2dbSEric W. Biederman !uid_eq(pcred->suid, mycred->euid) || 15339a56c2dbSEric W. Biederman !gid_eq(pcred->gid, mycred->egid) || 15349a56c2dbSEric W. Biederman !gid_eq(pcred->egid, mycred->egid) || 15359a56c2dbSEric W. Biederman !gid_eq(pcred->sgid, mycred->egid)) 1536413cd3d9SOleg Nesterov goto unlock; 1537ee18d64cSDavid Howells 1538ee18d64cSDavid Howells /* the keyrings must have the same UID */ 15393a50597dSDavid Howells if ((pcred->session_keyring && 15402a74dbb9SLinus Torvalds !uid_eq(pcred->session_keyring->uid, mycred->euid)) || 15412a74dbb9SLinus Torvalds !uid_eq(mycred->session_keyring->uid, mycred->euid)) 1542413cd3d9SOleg Nesterov goto unlock; 1543ee18d64cSDavid Howells 1544413cd3d9SOleg Nesterov /* cancel an already pending keyring replacement */ 1545413cd3d9SOleg Nesterov oldwork = task_work_cancel(parent, key_change_session_keyring); 1546ee18d64cSDavid Howells 1547ee18d64cSDavid Howells /* the replacement session keyring is applied just prior to userspace 1548ee18d64cSDavid Howells * restarting */ 154967d12145SAl Viro ret = task_work_add(parent, newwork, true); 1550413cd3d9SOleg Nesterov if (!ret) 1551413cd3d9SOleg Nesterov newwork = NULL; 1552413cd3d9SOleg Nesterov unlock: 1553ee18d64cSDavid Howells write_unlock_irq(&tasklist_lock); 15549d1ac65aSDavid Howells rcu_read_unlock(); 155567d12145SAl Viro if (oldwork) 155667d12145SAl Viro put_cred(container_of(oldwork, struct cred, rcu)); 155767d12145SAl Viro if (newwork) 155867d12145SAl Viro put_cred(cred); 1559ee18d64cSDavid Howells return ret; 1560ee18d64cSDavid Howells 1561ee18d64cSDavid Howells error_keyring: 1562ee18d64cSDavid Howells key_ref_put(keyring_r); 1563ee18d64cSDavid Howells return ret; 1564ee18d64cSDavid Howells } 1565ee18d64cSDavid Howells 1566b5f545c8SDavid Howells /* 1567973c9f4fSDavid Howells * The key control system call 15681da177e4SLinus Torvalds */ 1569938bb9f5SHeiko Carstens SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3, 1570938bb9f5SHeiko Carstens unsigned long, arg4, unsigned long, arg5) 15711da177e4SLinus Torvalds { 15721da177e4SLinus Torvalds switch (option) { 15731da177e4SLinus Torvalds case KEYCTL_GET_KEYRING_ID: 15741da177e4SLinus Torvalds return keyctl_get_keyring_ID((key_serial_t) arg2, 15751da177e4SLinus Torvalds (int) arg3); 15761da177e4SLinus Torvalds 15771da177e4SLinus Torvalds case KEYCTL_JOIN_SESSION_KEYRING: 15781da177e4SLinus Torvalds return keyctl_join_session_keyring((const char __user *) arg2); 15791da177e4SLinus Torvalds 15801da177e4SLinus Torvalds case KEYCTL_UPDATE: 15811da177e4SLinus Torvalds return keyctl_update_key((key_serial_t) arg2, 15821da177e4SLinus Torvalds (const void __user *) arg3, 15831da177e4SLinus Torvalds (size_t) arg4); 15841da177e4SLinus Torvalds 15851da177e4SLinus Torvalds case KEYCTL_REVOKE: 15861da177e4SLinus Torvalds return keyctl_revoke_key((key_serial_t) arg2); 15871da177e4SLinus Torvalds 15881da177e4SLinus Torvalds case KEYCTL_DESCRIBE: 15891da177e4SLinus Torvalds return keyctl_describe_key((key_serial_t) arg2, 15901da177e4SLinus Torvalds (char __user *) arg3, 15911da177e4SLinus Torvalds (unsigned) arg4); 15921da177e4SLinus Torvalds 15931da177e4SLinus Torvalds case KEYCTL_CLEAR: 15941da177e4SLinus Torvalds return keyctl_keyring_clear((key_serial_t) arg2); 15951da177e4SLinus Torvalds 15961da177e4SLinus Torvalds case KEYCTL_LINK: 15971da177e4SLinus Torvalds return keyctl_keyring_link((key_serial_t) arg2, 15981da177e4SLinus Torvalds (key_serial_t) arg3); 15991da177e4SLinus Torvalds 16001da177e4SLinus Torvalds case KEYCTL_UNLINK: 16011da177e4SLinus Torvalds return keyctl_keyring_unlink((key_serial_t) arg2, 16021da177e4SLinus Torvalds (key_serial_t) arg3); 16031da177e4SLinus Torvalds 16041da177e4SLinus Torvalds case KEYCTL_SEARCH: 16051da177e4SLinus Torvalds return keyctl_keyring_search((key_serial_t) arg2, 16061da177e4SLinus Torvalds (const char __user *) arg3, 16071da177e4SLinus Torvalds (const char __user *) arg4, 16081da177e4SLinus Torvalds (key_serial_t) arg5); 16091da177e4SLinus Torvalds 16101da177e4SLinus Torvalds case KEYCTL_READ: 16111da177e4SLinus Torvalds return keyctl_read_key((key_serial_t) arg2, 16121da177e4SLinus Torvalds (char __user *) arg3, 16131da177e4SLinus Torvalds (size_t) arg4); 16141da177e4SLinus Torvalds 16151da177e4SLinus Torvalds case KEYCTL_CHOWN: 16161da177e4SLinus Torvalds return keyctl_chown_key((key_serial_t) arg2, 16171da177e4SLinus Torvalds (uid_t) arg3, 16181da177e4SLinus Torvalds (gid_t) arg4); 16191da177e4SLinus Torvalds 16201da177e4SLinus Torvalds case KEYCTL_SETPERM: 16211da177e4SLinus Torvalds return keyctl_setperm_key((key_serial_t) arg2, 16221da177e4SLinus Torvalds (key_perm_t) arg3); 16231da177e4SLinus Torvalds 16241da177e4SLinus Torvalds case KEYCTL_INSTANTIATE: 16251da177e4SLinus Torvalds return keyctl_instantiate_key((key_serial_t) arg2, 16261da177e4SLinus Torvalds (const void __user *) arg3, 16271da177e4SLinus Torvalds (size_t) arg4, 16281da177e4SLinus Torvalds (key_serial_t) arg5); 16291da177e4SLinus Torvalds 16301da177e4SLinus Torvalds case KEYCTL_NEGATE: 16311da177e4SLinus Torvalds return keyctl_negate_key((key_serial_t) arg2, 16321da177e4SLinus Torvalds (unsigned) arg3, 16331da177e4SLinus Torvalds (key_serial_t) arg4); 16341da177e4SLinus Torvalds 16353e30148cSDavid Howells case KEYCTL_SET_REQKEY_KEYRING: 16363e30148cSDavid Howells return keyctl_set_reqkey_keyring(arg2); 16373e30148cSDavid Howells 1638017679c4SDavid Howells case KEYCTL_SET_TIMEOUT: 1639017679c4SDavid Howells return keyctl_set_timeout((key_serial_t) arg2, 1640017679c4SDavid Howells (unsigned) arg3); 1641017679c4SDavid Howells 1642b5f545c8SDavid Howells case KEYCTL_ASSUME_AUTHORITY: 1643b5f545c8SDavid Howells return keyctl_assume_authority((key_serial_t) arg2); 1644b5f545c8SDavid Howells 164570a5bb72SDavid Howells case KEYCTL_GET_SECURITY: 164670a5bb72SDavid Howells return keyctl_get_security((key_serial_t) arg2, 164790bd49abSJames Morris (char __user *) arg3, 164870a5bb72SDavid Howells (size_t) arg4); 164970a5bb72SDavid Howells 1650ee18d64cSDavid Howells case KEYCTL_SESSION_TO_PARENT: 1651ee18d64cSDavid Howells return keyctl_session_to_parent(); 1652ee18d64cSDavid Howells 1653fdd1b945SDavid Howells case KEYCTL_REJECT: 1654fdd1b945SDavid Howells return keyctl_reject_key((key_serial_t) arg2, 1655fdd1b945SDavid Howells (unsigned) arg3, 1656fdd1b945SDavid Howells (unsigned) arg4, 1657fdd1b945SDavid Howells (key_serial_t) arg5); 1658fdd1b945SDavid Howells 1659ee009e4aSDavid Howells case KEYCTL_INSTANTIATE_IOV: 1660ee009e4aSDavid Howells return keyctl_instantiate_key_iov( 1661ee009e4aSDavid Howells (key_serial_t) arg2, 1662ee009e4aSDavid Howells (const struct iovec __user *) arg3, 1663ee009e4aSDavid Howells (unsigned) arg4, 1664ee009e4aSDavid Howells (key_serial_t) arg5); 1665ee009e4aSDavid Howells 1666fd75815fSDavid Howells case KEYCTL_INVALIDATE: 1667fd75815fSDavid Howells return keyctl_invalidate_key((key_serial_t) arg2); 1668fd75815fSDavid Howells 16691da177e4SLinus Torvalds default: 16701da177e4SLinus Torvalds return -EOPNOTSUPP; 16711da177e4SLinus Torvalds } 1672a8b17ed0SDavid Howells } 1673