1*973c9f4fSDavid 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> 171da177e4SLinus Torvalds #include <linux/keyctl.h> 181da177e4SLinus Torvalds #include <linux/fs.h> 19c59ede7bSRandy.Dunlap #include <linux/capability.h> 200cb409d9SDavi Arnaut #include <linux/string.h> 211da177e4SLinus Torvalds #include <linux/err.h> 2238bbca6bSDavid Howells #include <linux/vmalloc.h> 2370a5bb72SDavid Howells #include <linux/security.h> 241da177e4SLinus Torvalds #include <asm/uaccess.h> 251da177e4SLinus Torvalds #include "internal.h" 261da177e4SLinus Torvalds 270cb409d9SDavi Arnaut static int key_get_type_from_user(char *type, 280cb409d9SDavi Arnaut const char __user *_type, 290cb409d9SDavi Arnaut unsigned len) 300cb409d9SDavi Arnaut { 310cb409d9SDavi Arnaut int ret; 320cb409d9SDavi Arnaut 330cb409d9SDavi Arnaut ret = strncpy_from_user(type, _type, len); 340cb409d9SDavi Arnaut if (ret < 0) 354303ef19SDan Carpenter return ret; 360cb409d9SDavi Arnaut if (ret == 0 || ret >= len) 370cb409d9SDavi Arnaut return -EINVAL; 380cb409d9SDavi Arnaut if (type[0] == '.') 390cb409d9SDavi Arnaut return -EPERM; 400cb409d9SDavi Arnaut type[len - 1] = '\0'; 410cb409d9SDavi Arnaut return 0; 420cb409d9SDavi Arnaut } 430cb409d9SDavi Arnaut 441da177e4SLinus Torvalds /* 45*973c9f4fSDavid Howells * Extract the description of a new key from userspace and either add it as a 46*973c9f4fSDavid Howells * new key to the specified keyring or update a matching key in that keyring. 47*973c9f4fSDavid Howells * 48*973c9f4fSDavid Howells * The keyring must be writable so that we can attach the key to it. 49*973c9f4fSDavid Howells * 50*973c9f4fSDavid Howells * If successful, the new key's serial number is returned, otherwise an error 51*973c9f4fSDavid Howells * code is returned. 521da177e4SLinus Torvalds */ 531e7bfb21SHeiko Carstens SYSCALL_DEFINE5(add_key, const char __user *, _type, 541e7bfb21SHeiko Carstens const char __user *, _description, 551e7bfb21SHeiko Carstens const void __user *, _payload, 561e7bfb21SHeiko Carstens size_t, plen, 571e7bfb21SHeiko Carstens key_serial_t, ringid) 581da177e4SLinus Torvalds { 59664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 601da177e4SLinus Torvalds char type[32], *description; 611da177e4SLinus Torvalds void *payload; 620cb409d9SDavi Arnaut long ret; 6338bbca6bSDavid Howells bool vm; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds ret = -EINVAL; 6638bbca6bSDavid Howells if (plen > 1024 * 1024 - 1) 671da177e4SLinus Torvalds goto error; 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds /* draw all the data into kernel space */ 700cb409d9SDavi Arnaut ret = key_get_type_from_user(type, _type, sizeof(type)); 711da177e4SLinus Torvalds if (ret < 0) 721da177e4SLinus Torvalds goto error; 731da177e4SLinus Torvalds 740cb409d9SDavi Arnaut description = strndup_user(_description, PAGE_SIZE); 750cb409d9SDavi Arnaut if (IS_ERR(description)) { 760cb409d9SDavi Arnaut ret = PTR_ERR(description); 773e30148cSDavid Howells goto error; 780cb409d9SDavi Arnaut } 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 811da177e4SLinus Torvalds payload = NULL; 821da177e4SLinus Torvalds 8338bbca6bSDavid Howells vm = false; 841da177e4SLinus Torvalds if (_payload) { 851da177e4SLinus Torvalds ret = -ENOMEM; 861da177e4SLinus Torvalds payload = kmalloc(plen, GFP_KERNEL); 8738bbca6bSDavid Howells if (!payload) { 8838bbca6bSDavid Howells if (plen <= PAGE_SIZE) 8938bbca6bSDavid Howells goto error2; 9038bbca6bSDavid Howells vm = true; 9138bbca6bSDavid Howells payload = vmalloc(plen); 921da177e4SLinus Torvalds if (!payload) 931da177e4SLinus Torvalds goto error2; 9438bbca6bSDavid Howells } 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds ret = -EFAULT; 971da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 981da177e4SLinus Torvalds goto error3; 991da177e4SLinus Torvalds } 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds /* find the target keyring (which must be writable) */ 1025593122eSDavid Howells keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); 103664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 104664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 1051da177e4SLinus Torvalds goto error3; 1061da177e4SLinus Torvalds } 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds /* create or update the requested key and add it to the target 1091da177e4SLinus Torvalds * keyring */ 110664cceb0SDavid Howells key_ref = key_create_or_update(keyring_ref, type, description, 1116b79ccb5SArun Raghavan payload, plen, KEY_PERM_UNDEF, 1126b79ccb5SArun Raghavan KEY_ALLOC_IN_QUOTA); 113664cceb0SDavid Howells if (!IS_ERR(key_ref)) { 114664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 115664cceb0SDavid Howells key_ref_put(key_ref); 1161da177e4SLinus Torvalds } 1171da177e4SLinus Torvalds else { 118664cceb0SDavid Howells ret = PTR_ERR(key_ref); 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds 121664cceb0SDavid Howells key_ref_put(keyring_ref); 1221da177e4SLinus Torvalds error3: 12338bbca6bSDavid Howells if (!vm) 1241da177e4SLinus Torvalds kfree(payload); 12538bbca6bSDavid Howells else 12638bbca6bSDavid Howells vfree(payload); 1271da177e4SLinus Torvalds error2: 1281da177e4SLinus Torvalds kfree(description); 1291da177e4SLinus Torvalds error: 1301da177e4SLinus Torvalds return ret; 131a8b17ed0SDavid Howells } 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds /* 134*973c9f4fSDavid Howells * Search the process keyrings and keyring trees linked from those for a 135*973c9f4fSDavid Howells * matching key. Keyrings must have appropriate Search permission to be 136*973c9f4fSDavid Howells * searched. 137*973c9f4fSDavid Howells * 138*973c9f4fSDavid Howells * If a key is found, it will be attached to the destination keyring if there's 139*973c9f4fSDavid Howells * one specified and the serial number of the key will be returned. 140*973c9f4fSDavid Howells * 141*973c9f4fSDavid Howells * If no key is found, /sbin/request-key will be invoked if _callout_info is 142*973c9f4fSDavid Howells * non-NULL in an attempt to create a key. The _callout_info string will be 143*973c9f4fSDavid Howells * passed to /sbin/request-key to aid with completing the request. If the 144*973c9f4fSDavid Howells * _callout_info string is "" then it will be changed to "-". 1451da177e4SLinus Torvalds */ 1461e7bfb21SHeiko Carstens SYSCALL_DEFINE4(request_key, const char __user *, _type, 1471e7bfb21SHeiko Carstens const char __user *, _description, 1481e7bfb21SHeiko Carstens const char __user *, _callout_info, 1491e7bfb21SHeiko Carstens key_serial_t, destringid) 1501da177e4SLinus Torvalds { 1511da177e4SLinus Torvalds struct key_type *ktype; 152664cceb0SDavid Howells struct key *key; 153664cceb0SDavid Howells key_ref_t dest_ref; 1544a38e122SDavid Howells size_t callout_len; 1551da177e4SLinus Torvalds char type[32], *description, *callout_info; 1560cb409d9SDavi Arnaut long ret; 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds /* pull the type into kernel space */ 1590cb409d9SDavi Arnaut ret = key_get_type_from_user(type, _type, sizeof(type)); 1601da177e4SLinus Torvalds if (ret < 0) 1611da177e4SLinus Torvalds goto error; 1621260f801SDavid Howells 1631da177e4SLinus Torvalds /* pull the description into kernel space */ 1640cb409d9SDavi Arnaut description = strndup_user(_description, PAGE_SIZE); 1650cb409d9SDavi Arnaut if (IS_ERR(description)) { 1660cb409d9SDavi Arnaut ret = PTR_ERR(description); 1671da177e4SLinus Torvalds goto error; 1680cb409d9SDavi Arnaut } 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds /* pull the callout info into kernel space */ 1711da177e4SLinus Torvalds callout_info = NULL; 1724a38e122SDavid Howells callout_len = 0; 1731da177e4SLinus Torvalds if (_callout_info) { 1740cb409d9SDavi Arnaut callout_info = strndup_user(_callout_info, PAGE_SIZE); 1750cb409d9SDavi Arnaut if (IS_ERR(callout_info)) { 1760cb409d9SDavi Arnaut ret = PTR_ERR(callout_info); 1771da177e4SLinus Torvalds goto error2; 1780cb409d9SDavi Arnaut } 1794a38e122SDavid Howells callout_len = strlen(callout_info); 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds /* get the destination keyring if specified */ 183664cceb0SDavid Howells dest_ref = NULL; 1841da177e4SLinus Torvalds if (destringid) { 1855593122eSDavid Howells dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE, 1865593122eSDavid Howells KEY_WRITE); 187664cceb0SDavid Howells if (IS_ERR(dest_ref)) { 188664cceb0SDavid Howells ret = PTR_ERR(dest_ref); 1891da177e4SLinus Torvalds goto error3; 1901da177e4SLinus Torvalds } 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds /* find the key type */ 1941da177e4SLinus Torvalds ktype = key_type_lookup(type); 1951da177e4SLinus Torvalds if (IS_ERR(ktype)) { 1961da177e4SLinus Torvalds ret = PTR_ERR(ktype); 1971da177e4SLinus Torvalds goto error4; 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds /* do the search */ 2014a38e122SDavid Howells key = request_key_and_link(ktype, description, callout_info, 2024a38e122SDavid Howells callout_len, NULL, key_ref_to_ptr(dest_ref), 2037e047ef5SDavid Howells KEY_ALLOC_IN_QUOTA); 2041da177e4SLinus Torvalds if (IS_ERR(key)) { 2051da177e4SLinus Torvalds ret = PTR_ERR(key); 2061da177e4SLinus Torvalds goto error5; 2071da177e4SLinus Torvalds } 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds ret = key->serial; 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds key_put(key); 2121da177e4SLinus Torvalds error5: 2131da177e4SLinus Torvalds key_type_put(ktype); 2141da177e4SLinus Torvalds error4: 215664cceb0SDavid Howells key_ref_put(dest_ref); 2161da177e4SLinus Torvalds error3: 2171da177e4SLinus Torvalds kfree(callout_info); 2181da177e4SLinus Torvalds error2: 2191da177e4SLinus Torvalds kfree(description); 2201da177e4SLinus Torvalds error: 2211da177e4SLinus Torvalds return ret; 222a8b17ed0SDavid Howells } 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds /* 225*973c9f4fSDavid Howells * Get the ID of the specified process keyring. 226*973c9f4fSDavid Howells * 227*973c9f4fSDavid Howells * The requested keyring must have search permission to be found. 228*973c9f4fSDavid Howells * 229*973c9f4fSDavid Howells * If successful, the ID of the requested keyring will be returned. 2301da177e4SLinus Torvalds */ 2311da177e4SLinus Torvalds long keyctl_get_keyring_ID(key_serial_t id, int create) 2321da177e4SLinus Torvalds { 233664cceb0SDavid Howells key_ref_t key_ref; 2345593122eSDavid Howells unsigned long lflags; 2351da177e4SLinus Torvalds long ret; 2361da177e4SLinus Torvalds 2375593122eSDavid Howells lflags = create ? KEY_LOOKUP_CREATE : 0; 2385593122eSDavid Howells key_ref = lookup_user_key(id, lflags, KEY_SEARCH); 239664cceb0SDavid Howells if (IS_ERR(key_ref)) { 240664cceb0SDavid Howells ret = PTR_ERR(key_ref); 2411da177e4SLinus Torvalds goto error; 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds 244664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 245664cceb0SDavid Howells key_ref_put(key_ref); 2461da177e4SLinus Torvalds error: 2471da177e4SLinus Torvalds return ret; 248*973c9f4fSDavid Howells } 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds /* 251*973c9f4fSDavid Howells * Join a (named) session keyring. 252*973c9f4fSDavid Howells * 253*973c9f4fSDavid Howells * Create and join an anonymous session keyring or join a named session 254*973c9f4fSDavid Howells * keyring, creating it if necessary. A named session keyring must have Search 255*973c9f4fSDavid Howells * permission for it to be joined. Session keyrings without this permit will 256*973c9f4fSDavid Howells * be skipped over. 257*973c9f4fSDavid Howells * 258*973c9f4fSDavid Howells * If successful, the ID of the joined session keyring will be returned. 2591da177e4SLinus Torvalds */ 2601da177e4SLinus Torvalds long keyctl_join_session_keyring(const char __user *_name) 2611da177e4SLinus Torvalds { 2621da177e4SLinus Torvalds char *name; 2630cb409d9SDavi Arnaut long ret; 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds /* fetch the name from userspace */ 2661da177e4SLinus Torvalds name = NULL; 2671da177e4SLinus Torvalds if (_name) { 2680cb409d9SDavi Arnaut name = strndup_user(_name, PAGE_SIZE); 2690cb409d9SDavi Arnaut if (IS_ERR(name)) { 2700cb409d9SDavi Arnaut ret = PTR_ERR(name); 2711da177e4SLinus Torvalds goto error; 2720cb409d9SDavi Arnaut } 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds /* join the session */ 2761da177e4SLinus Torvalds ret = join_session_keyring(name); 2770d54ee1cSVegard Nossum kfree(name); 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds error: 2801da177e4SLinus Torvalds return ret; 281a8b17ed0SDavid Howells } 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds /* 284*973c9f4fSDavid Howells * Update a key's data payload from the given data. 285*973c9f4fSDavid Howells * 286*973c9f4fSDavid Howells * The key must grant the caller Write permission and the key type must support 287*973c9f4fSDavid Howells * updating for this to work. A negative key can be positively instantiated 288*973c9f4fSDavid Howells * with this call. 289*973c9f4fSDavid Howells * 290*973c9f4fSDavid Howells * If successful, 0 will be returned. If the key type does not support 291*973c9f4fSDavid Howells * updating, then -EOPNOTSUPP will be returned. 2921da177e4SLinus Torvalds */ 2931da177e4SLinus Torvalds long keyctl_update_key(key_serial_t id, 2941da177e4SLinus Torvalds const void __user *_payload, 2951da177e4SLinus Torvalds size_t plen) 2961da177e4SLinus Torvalds { 297664cceb0SDavid Howells key_ref_t key_ref; 2981da177e4SLinus Torvalds void *payload; 2991da177e4SLinus Torvalds long ret; 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds ret = -EINVAL; 3021da177e4SLinus Torvalds if (plen > PAGE_SIZE) 3031da177e4SLinus Torvalds goto error; 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 3061da177e4SLinus Torvalds payload = NULL; 3071da177e4SLinus Torvalds if (_payload) { 3081da177e4SLinus Torvalds ret = -ENOMEM; 3091da177e4SLinus Torvalds payload = kmalloc(plen, GFP_KERNEL); 3101da177e4SLinus Torvalds if (!payload) 3111da177e4SLinus Torvalds goto error; 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds ret = -EFAULT; 3141da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 3151da177e4SLinus Torvalds goto error2; 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds /* find the target key (which must be writable) */ 3195593122eSDavid Howells key_ref = lookup_user_key(id, 0, KEY_WRITE); 320664cceb0SDavid Howells if (IS_ERR(key_ref)) { 321664cceb0SDavid Howells ret = PTR_ERR(key_ref); 3221da177e4SLinus Torvalds goto error2; 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds /* update the key */ 326664cceb0SDavid Howells ret = key_update(key_ref, payload, plen); 3271da177e4SLinus Torvalds 328664cceb0SDavid Howells key_ref_put(key_ref); 3291da177e4SLinus Torvalds error2: 3301da177e4SLinus Torvalds kfree(payload); 3311da177e4SLinus Torvalds error: 3321da177e4SLinus Torvalds return ret; 333a8b17ed0SDavid Howells } 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds /* 336*973c9f4fSDavid Howells * Revoke a key. 337*973c9f4fSDavid Howells * 338*973c9f4fSDavid Howells * The key must be grant the caller Write or Setattr permission for this to 339*973c9f4fSDavid Howells * work. The key type should give up its quota claim when revoked. The key 340*973c9f4fSDavid Howells * and any links to the key will be automatically garbage collected after a 341*973c9f4fSDavid Howells * certain amount of time (/proc/sys/kernel/keys/gc_delay). 342*973c9f4fSDavid Howells * 343*973c9f4fSDavid Howells * If successful, 0 is returned. 3441da177e4SLinus Torvalds */ 3451da177e4SLinus Torvalds long keyctl_revoke_key(key_serial_t id) 3461da177e4SLinus Torvalds { 347664cceb0SDavid Howells key_ref_t key_ref; 3481da177e4SLinus Torvalds long ret; 3491da177e4SLinus Torvalds 3505593122eSDavid Howells key_ref = lookup_user_key(id, 0, KEY_WRITE); 351664cceb0SDavid Howells if (IS_ERR(key_ref)) { 352664cceb0SDavid Howells ret = PTR_ERR(key_ref); 3530c2c9a3fSDavid Howells if (ret != -EACCES) 3541da177e4SLinus Torvalds goto error; 3550c2c9a3fSDavid Howells key_ref = lookup_user_key(id, 0, KEY_SETATTR); 3560c2c9a3fSDavid Howells if (IS_ERR(key_ref)) { 3570c2c9a3fSDavid Howells ret = PTR_ERR(key_ref); 3580c2c9a3fSDavid Howells goto error; 3590c2c9a3fSDavid Howells } 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds 362664cceb0SDavid Howells key_revoke(key_ref_to_ptr(key_ref)); 3631da177e4SLinus Torvalds ret = 0; 3641da177e4SLinus Torvalds 365664cceb0SDavid Howells key_ref_put(key_ref); 3661da177e4SLinus Torvalds error: 3671260f801SDavid Howells return ret; 368a8b17ed0SDavid Howells } 3691da177e4SLinus Torvalds 3701da177e4SLinus Torvalds /* 371*973c9f4fSDavid Howells * Clear the specified keyring, creating an empty process keyring if one of the 372*973c9f4fSDavid Howells * special keyring IDs is used. 373*973c9f4fSDavid Howells * 374*973c9f4fSDavid Howells * The keyring must grant the caller Write permission for this to work. If 375*973c9f4fSDavid Howells * successful, 0 will be returned. 3761da177e4SLinus Torvalds */ 3771da177e4SLinus Torvalds long keyctl_keyring_clear(key_serial_t ringid) 3781da177e4SLinus Torvalds { 379664cceb0SDavid Howells key_ref_t keyring_ref; 3801da177e4SLinus Torvalds long ret; 3811da177e4SLinus Torvalds 3825593122eSDavid Howells keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); 383664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 384664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 3851da177e4SLinus Torvalds goto error; 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds 388664cceb0SDavid Howells ret = keyring_clear(key_ref_to_ptr(keyring_ref)); 3891da177e4SLinus Torvalds 390664cceb0SDavid Howells key_ref_put(keyring_ref); 3911da177e4SLinus Torvalds error: 3921da177e4SLinus Torvalds return ret; 393a8b17ed0SDavid Howells } 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds /* 396*973c9f4fSDavid Howells * Create a link from a keyring to a key if there's no matching key in the 397*973c9f4fSDavid Howells * keyring, otherwise replace the link to the matching key with a link to the 398*973c9f4fSDavid Howells * new key. 399*973c9f4fSDavid Howells * 400*973c9f4fSDavid Howells * The key must grant the caller Link permission and the the keyring must grant 401*973c9f4fSDavid Howells * the caller Write permission. Furthermore, if an additional link is created, 402*973c9f4fSDavid Howells * the keyring's quota will be extended. 403*973c9f4fSDavid Howells * 404*973c9f4fSDavid Howells * If successful, 0 will be returned. 4051da177e4SLinus Torvalds */ 4061da177e4SLinus Torvalds long keyctl_keyring_link(key_serial_t id, key_serial_t ringid) 4071da177e4SLinus Torvalds { 408664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 4091da177e4SLinus Torvalds long ret; 4101da177e4SLinus Torvalds 4115593122eSDavid Howells keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); 412664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 413664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 4141da177e4SLinus Torvalds goto error; 4151da177e4SLinus Torvalds } 4161da177e4SLinus Torvalds 4175593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_LINK); 418664cceb0SDavid Howells if (IS_ERR(key_ref)) { 419664cceb0SDavid Howells ret = PTR_ERR(key_ref); 4201da177e4SLinus Torvalds goto error2; 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds 423664cceb0SDavid Howells ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref)); 4241da177e4SLinus Torvalds 425664cceb0SDavid Howells key_ref_put(key_ref); 4261da177e4SLinus Torvalds error2: 427664cceb0SDavid Howells key_ref_put(keyring_ref); 4281da177e4SLinus Torvalds error: 4291da177e4SLinus Torvalds return ret; 430a8b17ed0SDavid Howells } 4311da177e4SLinus Torvalds 4321da177e4SLinus Torvalds /* 433*973c9f4fSDavid Howells * Unlink a key from a keyring. 434*973c9f4fSDavid Howells * 435*973c9f4fSDavid Howells * The keyring must grant the caller Write permission for this to work; the key 436*973c9f4fSDavid Howells * itself need not grant the caller anything. If the last link to a key is 437*973c9f4fSDavid Howells * removed then that key will be scheduled for destruction. 438*973c9f4fSDavid Howells * 439*973c9f4fSDavid Howells * If successful, 0 will be returned. 4401da177e4SLinus Torvalds */ 4411da177e4SLinus Torvalds long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid) 4421da177e4SLinus Torvalds { 443664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 4441da177e4SLinus Torvalds long ret; 4451da177e4SLinus Torvalds 4465593122eSDavid Howells keyring_ref = lookup_user_key(ringid, 0, KEY_WRITE); 447664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 448664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 4491da177e4SLinus Torvalds goto error; 4501da177e4SLinus Torvalds } 4511da177e4SLinus Torvalds 4525593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0); 453664cceb0SDavid Howells if (IS_ERR(key_ref)) { 454664cceb0SDavid Howells ret = PTR_ERR(key_ref); 4551da177e4SLinus Torvalds goto error2; 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 458664cceb0SDavid Howells ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref)); 4591da177e4SLinus Torvalds 460664cceb0SDavid Howells key_ref_put(key_ref); 4611da177e4SLinus Torvalds error2: 462664cceb0SDavid Howells key_ref_put(keyring_ref); 4631da177e4SLinus Torvalds error: 4641da177e4SLinus Torvalds return ret; 465a8b17ed0SDavid Howells } 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds /* 468*973c9f4fSDavid Howells * Return a description of a key to userspace. 469*973c9f4fSDavid Howells * 470*973c9f4fSDavid Howells * The key must grant the caller View permission for this to work. 471*973c9f4fSDavid Howells * 472*973c9f4fSDavid Howells * If there's a buffer, we place up to buflen bytes of data into it formatted 473*973c9f4fSDavid Howells * in the following way: 474*973c9f4fSDavid Howells * 4751da177e4SLinus Torvalds * type;uid;gid;perm;description<NUL> 476*973c9f4fSDavid Howells * 477*973c9f4fSDavid Howells * If successful, we return the amount of description available, irrespective 478*973c9f4fSDavid Howells * of how much we may have copied into the buffer. 4791da177e4SLinus Torvalds */ 4801da177e4SLinus Torvalds long keyctl_describe_key(key_serial_t keyid, 4811da177e4SLinus Torvalds char __user *buffer, 4821da177e4SLinus Torvalds size_t buflen) 4831da177e4SLinus Torvalds { 4843e30148cSDavid Howells struct key *key, *instkey; 485664cceb0SDavid Howells key_ref_t key_ref; 4861da177e4SLinus Torvalds char *tmpbuf; 4871da177e4SLinus Torvalds long ret; 4881da177e4SLinus Torvalds 4895593122eSDavid Howells key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW); 490664cceb0SDavid Howells if (IS_ERR(key_ref)) { 4913e30148cSDavid Howells /* viewing a key under construction is permitted if we have the 4923e30148cSDavid Howells * authorisation token handy */ 493664cceb0SDavid Howells if (PTR_ERR(key_ref) == -EACCES) { 4943e30148cSDavid Howells instkey = key_get_instantiation_authkey(keyid); 4953e30148cSDavid Howells if (!IS_ERR(instkey)) { 4963e30148cSDavid Howells key_put(instkey); 4978bbf4976SDavid Howells key_ref = lookup_user_key(keyid, 4985593122eSDavid Howells KEY_LOOKUP_PARTIAL, 4995593122eSDavid Howells 0); 500664cceb0SDavid Howells if (!IS_ERR(key_ref)) 5013e30148cSDavid Howells goto okay; 5023e30148cSDavid Howells } 5033e30148cSDavid Howells } 5043e30148cSDavid Howells 505664cceb0SDavid Howells ret = PTR_ERR(key_ref); 5061da177e4SLinus Torvalds goto error; 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds 5093e30148cSDavid Howells okay: 5101da177e4SLinus Torvalds /* calculate how much description we're going to return */ 5111da177e4SLinus Torvalds ret = -ENOMEM; 5121da177e4SLinus Torvalds tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); 5131da177e4SLinus Torvalds if (!tmpbuf) 5141da177e4SLinus Torvalds goto error2; 5151da177e4SLinus Torvalds 516664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 517664cceb0SDavid Howells 5181da177e4SLinus Torvalds ret = snprintf(tmpbuf, PAGE_SIZE - 1, 519664cceb0SDavid Howells "%s;%d;%d;%08x;%s", 52094fd8405SDavid Howells key->type->name, 52194fd8405SDavid Howells key->uid, 52294fd8405SDavid Howells key->gid, 52394fd8405SDavid Howells key->perm, 52494fd8405SDavid Howells key->description ?: ""); 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds /* include a NUL char at the end of the data */ 5271da177e4SLinus Torvalds if (ret > PAGE_SIZE - 1) 5281da177e4SLinus Torvalds ret = PAGE_SIZE - 1; 5291da177e4SLinus Torvalds tmpbuf[ret] = 0; 5301da177e4SLinus Torvalds ret++; 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds /* consider returning the data */ 5331da177e4SLinus Torvalds if (buffer && buflen > 0) { 5341da177e4SLinus Torvalds if (buflen > ret) 5351da177e4SLinus Torvalds buflen = ret; 5361da177e4SLinus Torvalds 5371da177e4SLinus Torvalds if (copy_to_user(buffer, tmpbuf, buflen) != 0) 5381da177e4SLinus Torvalds ret = -EFAULT; 5391da177e4SLinus Torvalds } 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds kfree(tmpbuf); 5421da177e4SLinus Torvalds error2: 543664cceb0SDavid Howells key_ref_put(key_ref); 5441da177e4SLinus Torvalds error: 5451da177e4SLinus Torvalds return ret; 546a8b17ed0SDavid Howells } 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds /* 549*973c9f4fSDavid Howells * Search the specified keyring and any keyrings it links to for a matching 550*973c9f4fSDavid Howells * key. Only keyrings that grant the caller Search permission will be searched 551*973c9f4fSDavid Howells * (this includes the starting keyring). Only keys with Search permission can 552*973c9f4fSDavid Howells * be found. 553*973c9f4fSDavid Howells * 554*973c9f4fSDavid Howells * If successful, the found key will be linked to the destination keyring if 555*973c9f4fSDavid Howells * supplied and the key has Link permission, and the found key ID will be 556*973c9f4fSDavid Howells * returned. 5571da177e4SLinus Torvalds */ 5581da177e4SLinus Torvalds long keyctl_keyring_search(key_serial_t ringid, 5591da177e4SLinus Torvalds const char __user *_type, 5601da177e4SLinus Torvalds const char __user *_description, 5611da177e4SLinus Torvalds key_serial_t destringid) 5621da177e4SLinus Torvalds { 5631da177e4SLinus Torvalds struct key_type *ktype; 564664cceb0SDavid Howells key_ref_t keyring_ref, key_ref, dest_ref; 5651da177e4SLinus Torvalds char type[32], *description; 5660cb409d9SDavi Arnaut long ret; 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds /* pull the type and description into kernel space */ 5690cb409d9SDavi Arnaut ret = key_get_type_from_user(type, _type, sizeof(type)); 5701da177e4SLinus Torvalds if (ret < 0) 5711da177e4SLinus Torvalds goto error; 5721da177e4SLinus Torvalds 5730cb409d9SDavi Arnaut description = strndup_user(_description, PAGE_SIZE); 5740cb409d9SDavi Arnaut if (IS_ERR(description)) { 5750cb409d9SDavi Arnaut ret = PTR_ERR(description); 5761da177e4SLinus Torvalds goto error; 5770cb409d9SDavi Arnaut } 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds /* get the keyring at which to begin the search */ 5805593122eSDavid Howells keyring_ref = lookup_user_key(ringid, 0, KEY_SEARCH); 581664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 582664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 5831da177e4SLinus Torvalds goto error2; 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds /* get the destination keyring if specified */ 587664cceb0SDavid Howells dest_ref = NULL; 5881da177e4SLinus Torvalds if (destringid) { 5895593122eSDavid Howells dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE, 5905593122eSDavid Howells KEY_WRITE); 591664cceb0SDavid Howells if (IS_ERR(dest_ref)) { 592664cceb0SDavid Howells ret = PTR_ERR(dest_ref); 5931da177e4SLinus Torvalds goto error3; 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds } 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds /* find the key type */ 5981da177e4SLinus Torvalds ktype = key_type_lookup(type); 5991da177e4SLinus Torvalds if (IS_ERR(ktype)) { 6001da177e4SLinus Torvalds ret = PTR_ERR(ktype); 6011da177e4SLinus Torvalds goto error4; 6021da177e4SLinus Torvalds } 6031da177e4SLinus Torvalds 6041da177e4SLinus Torvalds /* do the search */ 605664cceb0SDavid Howells key_ref = keyring_search(keyring_ref, ktype, description); 606664cceb0SDavid Howells if (IS_ERR(key_ref)) { 607664cceb0SDavid Howells ret = PTR_ERR(key_ref); 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds /* treat lack or presence of a negative key the same */ 6101da177e4SLinus Torvalds if (ret == -EAGAIN) 6111da177e4SLinus Torvalds ret = -ENOKEY; 6121da177e4SLinus Torvalds goto error5; 6131da177e4SLinus Torvalds } 6141da177e4SLinus Torvalds 6151da177e4SLinus Torvalds /* link the resulting key to the destination keyring if we can */ 616664cceb0SDavid Howells if (dest_ref) { 61729db9190SDavid Howells ret = key_permission(key_ref, KEY_LINK); 61829db9190SDavid Howells if (ret < 0) 6191da177e4SLinus Torvalds goto error6; 6201da177e4SLinus Torvalds 621664cceb0SDavid Howells ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref)); 6221da177e4SLinus Torvalds if (ret < 0) 6231da177e4SLinus Torvalds goto error6; 6241da177e4SLinus Torvalds } 6251da177e4SLinus Torvalds 626664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 6271da177e4SLinus Torvalds 6281da177e4SLinus Torvalds error6: 629664cceb0SDavid Howells key_ref_put(key_ref); 6301da177e4SLinus Torvalds error5: 6311da177e4SLinus Torvalds key_type_put(ktype); 6321da177e4SLinus Torvalds error4: 633664cceb0SDavid Howells key_ref_put(dest_ref); 6341da177e4SLinus Torvalds error3: 635664cceb0SDavid Howells key_ref_put(keyring_ref); 6361da177e4SLinus Torvalds error2: 6371da177e4SLinus Torvalds kfree(description); 6381da177e4SLinus Torvalds error: 6391da177e4SLinus Torvalds return ret; 640a8b17ed0SDavid Howells } 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds /* 643*973c9f4fSDavid Howells * Read a key's payload. 644*973c9f4fSDavid Howells * 645*973c9f4fSDavid Howells * The key must either grant the caller Read permission, or it must grant the 646*973c9f4fSDavid Howells * caller Search permission when searched for from the process keyrings. 647*973c9f4fSDavid Howells * 648*973c9f4fSDavid Howells * If successful, we place up to buflen bytes of data into the buffer, if one 649*973c9f4fSDavid Howells * is provided, and return the amount of data that is available in the key, 650*973c9f4fSDavid Howells * irrespective of how much we copied into the buffer. 6511da177e4SLinus Torvalds */ 6521da177e4SLinus Torvalds long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen) 6531da177e4SLinus Torvalds { 654664cceb0SDavid Howells struct key *key; 655664cceb0SDavid Howells key_ref_t key_ref; 6561da177e4SLinus Torvalds long ret; 6571da177e4SLinus Torvalds 6581da177e4SLinus Torvalds /* find the key first */ 6595593122eSDavid Howells key_ref = lookup_user_key(keyid, 0, 0); 660664cceb0SDavid Howells if (IS_ERR(key_ref)) { 661664cceb0SDavid Howells ret = -ENOKEY; 662664cceb0SDavid Howells goto error; 663664cceb0SDavid Howells } 664664cceb0SDavid Howells 665664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 666664cceb0SDavid Howells 6671da177e4SLinus Torvalds /* see if we can read it directly */ 66829db9190SDavid Howells ret = key_permission(key_ref, KEY_READ); 66929db9190SDavid Howells if (ret == 0) 6701da177e4SLinus Torvalds goto can_read_key; 67129db9190SDavid Howells if (ret != -EACCES) 67229db9190SDavid Howells goto error; 6731da177e4SLinus Torvalds 674664cceb0SDavid Howells /* we can't; see if it's searchable from this process's keyrings 6753e30148cSDavid Howells * - we automatically take account of the fact that it may be 6763e30148cSDavid Howells * dangling off an instantiation key 6773e30148cSDavid Howells */ 678664cceb0SDavid Howells if (!is_key_possessed(key_ref)) { 6791260f801SDavid Howells ret = -EACCES; 6801da177e4SLinus Torvalds goto error2; 6811da177e4SLinus Torvalds } 6821da177e4SLinus Torvalds 6831da177e4SLinus Torvalds /* the key is probably readable - now try to read it */ 6841da177e4SLinus Torvalds can_read_key: 6851da177e4SLinus Torvalds ret = key_validate(key); 6861da177e4SLinus Torvalds if (ret == 0) { 6871da177e4SLinus Torvalds ret = -EOPNOTSUPP; 6881da177e4SLinus Torvalds if (key->type->read) { 6891da177e4SLinus Torvalds /* read the data with the semaphore held (since we 6901da177e4SLinus Torvalds * might sleep) */ 6911da177e4SLinus Torvalds down_read(&key->sem); 6921da177e4SLinus Torvalds ret = key->type->read(key, buffer, buflen); 6931da177e4SLinus Torvalds up_read(&key->sem); 6941da177e4SLinus Torvalds } 6951da177e4SLinus Torvalds } 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds error2: 6981da177e4SLinus Torvalds key_put(key); 6991da177e4SLinus Torvalds error: 7001da177e4SLinus Torvalds return ret; 701a8b17ed0SDavid Howells } 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds /* 704*973c9f4fSDavid Howells * Change the ownership of a key 705*973c9f4fSDavid Howells * 706*973c9f4fSDavid Howells * The key must grant the caller Setattr permission for this to work, though 707*973c9f4fSDavid Howells * the key need not be fully instantiated yet. For the UID to be changed, or 708*973c9f4fSDavid Howells * for the GID to be changed to a group the caller is not a member of, the 709*973c9f4fSDavid Howells * caller must have sysadmin capability. If either uid or gid is -1 then that 710*973c9f4fSDavid Howells * attribute is not changed. 711*973c9f4fSDavid Howells * 712*973c9f4fSDavid Howells * If the UID is to be changed, the new user must have sufficient quota to 713*973c9f4fSDavid Howells * accept the key. The quota deduction will be removed from the old user to 714*973c9f4fSDavid Howells * the new user should the attribute be changed. 715*973c9f4fSDavid Howells * 716*973c9f4fSDavid Howells * If successful, 0 will be returned. 7171da177e4SLinus Torvalds */ 7181da177e4SLinus Torvalds long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid) 7191da177e4SLinus Torvalds { 7205801649dSFredrik Tolf struct key_user *newowner, *zapowner = NULL; 7211da177e4SLinus Torvalds struct key *key; 722664cceb0SDavid Howells key_ref_t key_ref; 7231da177e4SLinus Torvalds long ret; 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds ret = 0; 7261da177e4SLinus Torvalds if (uid == (uid_t) -1 && gid == (gid_t) -1) 7271da177e4SLinus Torvalds goto error; 7281da177e4SLinus Torvalds 7295593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 7305593122eSDavid Howells KEY_SETATTR); 731664cceb0SDavid Howells if (IS_ERR(key_ref)) { 732664cceb0SDavid Howells ret = PTR_ERR(key_ref); 7331da177e4SLinus Torvalds goto error; 7341da177e4SLinus Torvalds } 7351da177e4SLinus Torvalds 736664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 737664cceb0SDavid Howells 7381da177e4SLinus Torvalds /* make the changes with the locks held to prevent chown/chown races */ 7391da177e4SLinus Torvalds ret = -EACCES; 7401da177e4SLinus Torvalds down_write(&key->sem); 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) { 7431da177e4SLinus Torvalds /* only the sysadmin can chown a key to some other UID */ 7441da177e4SLinus Torvalds if (uid != (uid_t) -1 && key->uid != uid) 7455801649dSFredrik Tolf goto error_put; 7461da177e4SLinus Torvalds 7471da177e4SLinus Torvalds /* only the sysadmin can set the key's GID to a group other 7481da177e4SLinus Torvalds * than one of those that the current process subscribes to */ 7491da177e4SLinus Torvalds if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid)) 7505801649dSFredrik Tolf goto error_put; 7511da177e4SLinus Torvalds } 7521da177e4SLinus Torvalds 7535801649dSFredrik Tolf /* change the UID */ 7541da177e4SLinus Torvalds if (uid != (uid_t) -1 && uid != key->uid) { 7555801649dSFredrik Tolf ret = -ENOMEM; 7561d1e9756SSerge E. Hallyn newowner = key_user_lookup(uid, current_user_ns()); 7575801649dSFredrik Tolf if (!newowner) 7585801649dSFredrik Tolf goto error_put; 7595801649dSFredrik Tolf 7605801649dSFredrik Tolf /* transfer the quota burden to the new user */ 7615801649dSFredrik Tolf if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { 7620b77f5bfSDavid Howells unsigned maxkeys = (uid == 0) ? 7630b77f5bfSDavid Howells key_quota_root_maxkeys : key_quota_maxkeys; 7640b77f5bfSDavid Howells unsigned maxbytes = (uid == 0) ? 7650b77f5bfSDavid Howells key_quota_root_maxbytes : key_quota_maxbytes; 7660b77f5bfSDavid Howells 7675801649dSFredrik Tolf spin_lock(&newowner->lock); 7680b77f5bfSDavid Howells if (newowner->qnkeys + 1 >= maxkeys || 7690b77f5bfSDavid Howells newowner->qnbytes + key->quotalen >= maxbytes || 7700b77f5bfSDavid Howells newowner->qnbytes + key->quotalen < 7710b77f5bfSDavid Howells newowner->qnbytes) 7725801649dSFredrik Tolf goto quota_overrun; 7735801649dSFredrik Tolf 7745801649dSFredrik Tolf newowner->qnkeys++; 7755801649dSFredrik Tolf newowner->qnbytes += key->quotalen; 7765801649dSFredrik Tolf spin_unlock(&newowner->lock); 7775801649dSFredrik Tolf 7785801649dSFredrik Tolf spin_lock(&key->user->lock); 7795801649dSFredrik Tolf key->user->qnkeys--; 7805801649dSFredrik Tolf key->user->qnbytes -= key->quotalen; 7815801649dSFredrik Tolf spin_unlock(&key->user->lock); 7825801649dSFredrik Tolf } 7835801649dSFredrik Tolf 7845801649dSFredrik Tolf atomic_dec(&key->user->nkeys); 7855801649dSFredrik Tolf atomic_inc(&newowner->nkeys); 7865801649dSFredrik Tolf 7875801649dSFredrik Tolf if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) { 7885801649dSFredrik Tolf atomic_dec(&key->user->nikeys); 7895801649dSFredrik Tolf atomic_inc(&newowner->nikeys); 7905801649dSFredrik Tolf } 7915801649dSFredrik Tolf 7925801649dSFredrik Tolf zapowner = key->user; 7935801649dSFredrik Tolf key->user = newowner; 7945801649dSFredrik Tolf key->uid = uid; 7951da177e4SLinus Torvalds } 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds /* change the GID */ 7981da177e4SLinus Torvalds if (gid != (gid_t) -1) 7991da177e4SLinus Torvalds key->gid = gid; 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds ret = 0; 8021da177e4SLinus Torvalds 8035801649dSFredrik Tolf error_put: 8041da177e4SLinus Torvalds up_write(&key->sem); 8051da177e4SLinus Torvalds key_put(key); 8065801649dSFredrik Tolf if (zapowner) 8075801649dSFredrik Tolf key_user_put(zapowner); 8081da177e4SLinus Torvalds error: 8091da177e4SLinus Torvalds return ret; 8101da177e4SLinus Torvalds 8115801649dSFredrik Tolf quota_overrun: 8125801649dSFredrik Tolf spin_unlock(&newowner->lock); 8135801649dSFredrik Tolf zapowner = newowner; 8145801649dSFredrik Tolf ret = -EDQUOT; 8155801649dSFredrik Tolf goto error_put; 816a8b17ed0SDavid Howells } 8175801649dSFredrik Tolf 8181da177e4SLinus Torvalds /* 819*973c9f4fSDavid Howells * Change the permission mask on a key. 820*973c9f4fSDavid Howells * 821*973c9f4fSDavid Howells * The key must grant the caller Setattr permission for this to work, though 822*973c9f4fSDavid Howells * the key need not be fully instantiated yet. If the caller does not have 823*973c9f4fSDavid Howells * sysadmin capability, it may only change the permission on keys that it owns. 8241da177e4SLinus Torvalds */ 8251da177e4SLinus Torvalds long keyctl_setperm_key(key_serial_t id, key_perm_t perm) 8261da177e4SLinus Torvalds { 8271da177e4SLinus Torvalds struct key *key; 828664cceb0SDavid Howells key_ref_t key_ref; 8291da177e4SLinus Torvalds long ret; 8301da177e4SLinus Torvalds 8311da177e4SLinus Torvalds ret = -EINVAL; 832664cceb0SDavid Howells if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL)) 8331da177e4SLinus Torvalds goto error; 8341da177e4SLinus Torvalds 8355593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 8365593122eSDavid Howells KEY_SETATTR); 837664cceb0SDavid Howells if (IS_ERR(key_ref)) { 838664cceb0SDavid Howells ret = PTR_ERR(key_ref); 8391da177e4SLinus Torvalds goto error; 8401da177e4SLinus Torvalds } 8411da177e4SLinus Torvalds 842664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 843664cceb0SDavid Howells 84476d8aeabSDavid Howells /* make the changes with the locks held to prevent chown/chmod races */ 8451da177e4SLinus Torvalds ret = -EACCES; 8461da177e4SLinus Torvalds down_write(&key->sem); 8471da177e4SLinus Torvalds 84876d8aeabSDavid Howells /* if we're not the sysadmin, we can only change a key that we own */ 84947d804bfSDavid Howells if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) { 8501da177e4SLinus Torvalds key->perm = perm; 8511da177e4SLinus Torvalds ret = 0; 85276d8aeabSDavid Howells } 8531da177e4SLinus Torvalds 8541da177e4SLinus Torvalds up_write(&key->sem); 8551da177e4SLinus Torvalds key_put(key); 8561da177e4SLinus Torvalds error: 8571da177e4SLinus Torvalds return ret; 858a8b17ed0SDavid Howells } 8591da177e4SLinus Torvalds 8608bbf4976SDavid Howells /* 861*973c9f4fSDavid Howells * Get the destination keyring for instantiation and check that the caller has 862*973c9f4fSDavid Howells * Write permission on it. 8638bbf4976SDavid Howells */ 8648bbf4976SDavid Howells static long get_instantiation_keyring(key_serial_t ringid, 8658bbf4976SDavid Howells struct request_key_auth *rka, 8668bbf4976SDavid Howells struct key **_dest_keyring) 8678bbf4976SDavid Howells { 8688bbf4976SDavid Howells key_ref_t dkref; 8698bbf4976SDavid Howells 8708bbf4976SDavid Howells *_dest_keyring = NULL; 871eca1bf5bSDavid Howells 872eca1bf5bSDavid Howells /* just return a NULL pointer if we weren't asked to make a link */ 873eca1bf5bSDavid Howells if (ringid == 0) 8748bbf4976SDavid Howells return 0; 8758bbf4976SDavid Howells 8768bbf4976SDavid Howells /* if a specific keyring is nominated by ID, then use that */ 8778bbf4976SDavid Howells if (ringid > 0) { 8785593122eSDavid Howells dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); 8798bbf4976SDavid Howells if (IS_ERR(dkref)) 8808bbf4976SDavid Howells return PTR_ERR(dkref); 8818bbf4976SDavid Howells *_dest_keyring = key_ref_to_ptr(dkref); 8828bbf4976SDavid Howells return 0; 8838bbf4976SDavid Howells } 8848bbf4976SDavid Howells 8858bbf4976SDavid Howells if (ringid == KEY_SPEC_REQKEY_AUTH_KEY) 8868bbf4976SDavid Howells return -EINVAL; 8878bbf4976SDavid Howells 8888bbf4976SDavid Howells /* otherwise specify the destination keyring recorded in the 8898bbf4976SDavid Howells * authorisation key (any KEY_SPEC_*_KEYRING) */ 8908bbf4976SDavid Howells if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) { 89121279cfaSDavid Howells *_dest_keyring = key_get(rka->dest_keyring); 8928bbf4976SDavid Howells return 0; 8938bbf4976SDavid Howells } 8948bbf4976SDavid Howells 8958bbf4976SDavid Howells return -ENOKEY; 8968bbf4976SDavid Howells } 8978bbf4976SDavid Howells 898d84f4f99SDavid Howells /* 899*973c9f4fSDavid Howells * Change the request_key authorisation key on the current process. 900d84f4f99SDavid Howells */ 901d84f4f99SDavid Howells static int keyctl_change_reqkey_auth(struct key *key) 902d84f4f99SDavid Howells { 903d84f4f99SDavid Howells struct cred *new; 904d84f4f99SDavid Howells 905d84f4f99SDavid Howells new = prepare_creds(); 906d84f4f99SDavid Howells if (!new) 907d84f4f99SDavid Howells return -ENOMEM; 908d84f4f99SDavid Howells 909d84f4f99SDavid Howells key_put(new->request_key_auth); 910d84f4f99SDavid Howells new->request_key_auth = key_get(key); 911d84f4f99SDavid Howells 912d84f4f99SDavid Howells return commit_creds(new); 913d84f4f99SDavid Howells } 914d84f4f99SDavid Howells 9151da177e4SLinus Torvalds /* 916*973c9f4fSDavid Howells * Instantiate a key with the specified payload and link the key into the 917*973c9f4fSDavid Howells * destination keyring if one is given. 918*973c9f4fSDavid Howells * 919*973c9f4fSDavid Howells * The caller must have the appropriate instantiation permit set for this to 920*973c9f4fSDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 921*973c9f4fSDavid Howells * 922*973c9f4fSDavid Howells * If successful, 0 will be returned. 9231da177e4SLinus Torvalds */ 9241da177e4SLinus Torvalds long keyctl_instantiate_key(key_serial_t id, 9251da177e4SLinus Torvalds const void __user *_payload, 9261da177e4SLinus Torvalds size_t plen, 9271da177e4SLinus Torvalds key_serial_t ringid) 9281da177e4SLinus Torvalds { 929d84f4f99SDavid Howells const struct cred *cred = current_cred(); 9303e30148cSDavid Howells struct request_key_auth *rka; 9318bbf4976SDavid Howells struct key *instkey, *dest_keyring; 9321da177e4SLinus Torvalds void *payload; 9331da177e4SLinus Torvalds long ret; 93438bbca6bSDavid Howells bool vm = false; 9351da177e4SLinus Torvalds 936d84f4f99SDavid Howells kenter("%d,,%zu,%d", id, plen, ringid); 937d84f4f99SDavid Howells 9381da177e4SLinus Torvalds ret = -EINVAL; 93938bbca6bSDavid Howells if (plen > 1024 * 1024 - 1) 9401da177e4SLinus Torvalds goto error; 9411da177e4SLinus Torvalds 942b5f545c8SDavid Howells /* the appropriate instantiation authorisation key must have been 943b5f545c8SDavid Howells * assumed before calling this */ 944b5f545c8SDavid Howells ret = -EPERM; 945d84f4f99SDavid Howells instkey = cred->request_key_auth; 946b5f545c8SDavid Howells if (!instkey) 947b5f545c8SDavid Howells goto error; 948b5f545c8SDavid Howells 949b5f545c8SDavid Howells rka = instkey->payload.data; 950b5f545c8SDavid Howells if (rka->target_key->serial != id) 951b5f545c8SDavid Howells goto error; 952b5f545c8SDavid Howells 9531da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 9541da177e4SLinus Torvalds payload = NULL; 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds if (_payload) { 9571da177e4SLinus Torvalds ret = -ENOMEM; 9581da177e4SLinus Torvalds payload = kmalloc(plen, GFP_KERNEL); 95938bbca6bSDavid Howells if (!payload) { 96038bbca6bSDavid Howells if (plen <= PAGE_SIZE) 96138bbca6bSDavid Howells goto error; 96238bbca6bSDavid Howells vm = true; 96338bbca6bSDavid Howells payload = vmalloc(plen); 9641da177e4SLinus Torvalds if (!payload) 9651da177e4SLinus Torvalds goto error; 96638bbca6bSDavid Howells } 9671da177e4SLinus Torvalds 9681da177e4SLinus Torvalds ret = -EFAULT; 9691da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 9701da177e4SLinus Torvalds goto error2; 9711da177e4SLinus Torvalds } 9721da177e4SLinus Torvalds 9733e30148cSDavid Howells /* find the destination keyring amongst those belonging to the 9743e30148cSDavid Howells * requesting task */ 9758bbf4976SDavid Howells ret = get_instantiation_keyring(ringid, rka, &dest_keyring); 9768bbf4976SDavid Howells if (ret < 0) 977b5f545c8SDavid Howells goto error2; 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds /* instantiate the key and link it into a keyring */ 9803e30148cSDavid Howells ret = key_instantiate_and_link(rka->target_key, payload, plen, 9818bbf4976SDavid Howells dest_keyring, instkey); 9821da177e4SLinus Torvalds 9838bbf4976SDavid Howells key_put(dest_keyring); 984b5f545c8SDavid Howells 985b5f545c8SDavid Howells /* discard the assumed authority if it's just been disabled by 986b5f545c8SDavid Howells * instantiation of the key */ 987d84f4f99SDavid Howells if (ret == 0) 988d84f4f99SDavid Howells keyctl_change_reqkey_auth(NULL); 989b5f545c8SDavid Howells 9901da177e4SLinus Torvalds error2: 99138bbca6bSDavid Howells if (!vm) 9921da177e4SLinus Torvalds kfree(payload); 99338bbca6bSDavid Howells else 99438bbca6bSDavid Howells vfree(payload); 9951da177e4SLinus Torvalds error: 9961da177e4SLinus Torvalds return ret; 997a8b17ed0SDavid Howells } 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvalds /* 1000*973c9f4fSDavid Howells * Negatively instantiate the key with the given timeout (in seconds) and link 1001*973c9f4fSDavid Howells * the key into the destination keyring if one is given. 1002*973c9f4fSDavid Howells * 1003*973c9f4fSDavid Howells * The caller must have the appropriate instantiation permit set for this to 1004*973c9f4fSDavid Howells * work (see keyctl_assume_authority). No other permissions are required. 1005*973c9f4fSDavid Howells * 1006*973c9f4fSDavid Howells * The key and any links to the key will be automatically garbage collected 1007*973c9f4fSDavid Howells * after the timeout expires. 1008*973c9f4fSDavid Howells * 1009*973c9f4fSDavid Howells * Negative keys are used to rate limit repeated request_key() calls by causing 1010*973c9f4fSDavid Howells * them to return -ENOKEY until the negative key expires. 1011*973c9f4fSDavid Howells * 1012*973c9f4fSDavid Howells * If successful, 0 will be returned. 10131da177e4SLinus Torvalds */ 10141da177e4SLinus Torvalds long keyctl_negate_key(key_serial_t id, unsigned timeout, 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 long ret; 10201da177e4SLinus Torvalds 1021d84f4f99SDavid Howells kenter("%d,%u,%d", id, timeout, ringid); 1022d84f4f99SDavid Howells 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) 10281da177e4SLinus Torvalds goto error; 10291da177e4SLinus Torvalds 10303e30148cSDavid Howells rka = instkey->payload.data; 1031b5f545c8SDavid Howells if (rka->target_key->serial != id) 1032b5f545c8SDavid Howells goto error; 10333e30148cSDavid Howells 10341da177e4SLinus Torvalds /* find the destination keyring if present (which must also be 10351da177e4SLinus Torvalds * writable) */ 10368bbf4976SDavid Howells ret = get_instantiation_keyring(ringid, rka, &dest_keyring); 10378bbf4976SDavid Howells if (ret < 0) 1038b5f545c8SDavid Howells goto error; 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds /* instantiate the key and link it into a keyring */ 1041664cceb0SDavid Howells ret = key_negate_and_link(rka->target_key, timeout, 10428bbf4976SDavid Howells dest_keyring, instkey); 10431da177e4SLinus Torvalds 10448bbf4976SDavid Howells key_put(dest_keyring); 1045b5f545c8SDavid Howells 1046b5f545c8SDavid Howells /* discard the assumed authority if it's just been disabled by 1047b5f545c8SDavid Howells * instantiation of the key */ 1048d84f4f99SDavid Howells if (ret == 0) 1049d84f4f99SDavid Howells keyctl_change_reqkey_auth(NULL); 1050b5f545c8SDavid Howells 10511da177e4SLinus Torvalds error: 10521da177e4SLinus Torvalds return ret; 1053a8b17ed0SDavid Howells } 10541da177e4SLinus Torvalds 10551da177e4SLinus Torvalds /* 1056*973c9f4fSDavid Howells * Read or set the default keyring in which request_key() will cache keys and 1057*973c9f4fSDavid Howells * return the old setting. 1058*973c9f4fSDavid Howells * 1059*973c9f4fSDavid Howells * If a process keyring is specified then this will be created if it doesn't 1060*973c9f4fSDavid Howells * yet exist. The old setting will be returned if successful. 10613e30148cSDavid Howells */ 10623e30148cSDavid Howells long keyctl_set_reqkey_keyring(int reqkey_defl) 10633e30148cSDavid Howells { 1064d84f4f99SDavid Howells struct cred *new; 1065d84f4f99SDavid Howells int ret, old_setting; 1066d84f4f99SDavid Howells 1067d84f4f99SDavid Howells old_setting = current_cred_xxx(jit_keyring); 1068d84f4f99SDavid Howells 1069d84f4f99SDavid Howells if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE) 1070d84f4f99SDavid Howells return old_setting; 1071d84f4f99SDavid Howells 1072d84f4f99SDavid Howells new = prepare_creds(); 1073d84f4f99SDavid Howells if (!new) 1074d84f4f99SDavid Howells return -ENOMEM; 10753e30148cSDavid Howells 10763e30148cSDavid Howells switch (reqkey_defl) { 10773e30148cSDavid Howells case KEY_REQKEY_DEFL_THREAD_KEYRING: 1078d84f4f99SDavid Howells ret = install_thread_keyring_to_cred(new); 10793e30148cSDavid Howells if (ret < 0) 1080d84f4f99SDavid Howells goto error; 10813e30148cSDavid Howells goto set; 10823e30148cSDavid Howells 10833e30148cSDavid Howells case KEY_REQKEY_DEFL_PROCESS_KEYRING: 1084d84f4f99SDavid Howells ret = install_process_keyring_to_cred(new); 1085d84f4f99SDavid Howells if (ret < 0) { 1086d84f4f99SDavid Howells if (ret != -EEXIST) 1087d84f4f99SDavid Howells goto error; 1088d84f4f99SDavid Howells ret = 0; 1089d84f4f99SDavid Howells } 1090d84f4f99SDavid Howells goto set; 10913e30148cSDavid Howells 10923e30148cSDavid Howells case KEY_REQKEY_DEFL_DEFAULT: 10933e30148cSDavid Howells case KEY_REQKEY_DEFL_SESSION_KEYRING: 10943e30148cSDavid Howells case KEY_REQKEY_DEFL_USER_KEYRING: 10953e30148cSDavid Howells case KEY_REQKEY_DEFL_USER_SESSION_KEYRING: 1096d84f4f99SDavid Howells case KEY_REQKEY_DEFL_REQUESTOR_KEYRING: 1097d84f4f99SDavid Howells goto set; 10983e30148cSDavid Howells 10993e30148cSDavid Howells case KEY_REQKEY_DEFL_NO_CHANGE: 11003e30148cSDavid Howells case KEY_REQKEY_DEFL_GROUP_KEYRING: 11013e30148cSDavid Howells default: 1102d84f4f99SDavid Howells ret = -EINVAL; 1103d84f4f99SDavid Howells goto error; 11043e30148cSDavid Howells } 11053e30148cSDavid Howells 1106d84f4f99SDavid Howells set: 1107d84f4f99SDavid Howells new->jit_keyring = reqkey_defl; 1108d84f4f99SDavid Howells commit_creds(new); 1109d84f4f99SDavid Howells return old_setting; 1110d84f4f99SDavid Howells error: 1111d84f4f99SDavid Howells abort_creds(new); 11124303ef19SDan Carpenter return ret; 1113a8b17ed0SDavid Howells } 1114d84f4f99SDavid Howells 11153e30148cSDavid Howells /* 1116*973c9f4fSDavid Howells * Set or clear the timeout on a key. 1117*973c9f4fSDavid Howells * 1118*973c9f4fSDavid Howells * Either the key must grant the caller Setattr permission or else the caller 1119*973c9f4fSDavid Howells * must hold an instantiation authorisation token for the key. 1120*973c9f4fSDavid Howells * 1121*973c9f4fSDavid Howells * The timeout is either 0 to clear the timeout, or a number of seconds from 1122*973c9f4fSDavid Howells * the current time. The key and any links to the key will be automatically 1123*973c9f4fSDavid Howells * garbage collected after the timeout expires. 1124*973c9f4fSDavid Howells * 1125*973c9f4fSDavid Howells * If successful, 0 is returned. 1126017679c4SDavid Howells */ 1127017679c4SDavid Howells long keyctl_set_timeout(key_serial_t id, unsigned timeout) 1128017679c4SDavid Howells { 1129017679c4SDavid Howells struct timespec now; 11309156235bSDavid Howells struct key *key, *instkey; 1131017679c4SDavid Howells key_ref_t key_ref; 1132017679c4SDavid Howells time_t expiry; 1133017679c4SDavid Howells long ret; 1134017679c4SDavid Howells 11355593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 11365593122eSDavid Howells KEY_SETATTR); 1137017679c4SDavid Howells if (IS_ERR(key_ref)) { 11389156235bSDavid Howells /* setting the timeout on a key under construction is permitted 11399156235bSDavid Howells * if we have the authorisation token handy */ 11409156235bSDavid Howells if (PTR_ERR(key_ref) == -EACCES) { 11419156235bSDavid Howells instkey = key_get_instantiation_authkey(id); 11429156235bSDavid Howells if (!IS_ERR(instkey)) { 11439156235bSDavid Howells key_put(instkey); 11449156235bSDavid Howells key_ref = lookup_user_key(id, 11459156235bSDavid Howells KEY_LOOKUP_PARTIAL, 11469156235bSDavid Howells 0); 11479156235bSDavid Howells if (!IS_ERR(key_ref)) 11489156235bSDavid Howells goto okay; 11499156235bSDavid Howells } 11509156235bSDavid Howells } 11519156235bSDavid Howells 1152017679c4SDavid Howells ret = PTR_ERR(key_ref); 1153017679c4SDavid Howells goto error; 1154017679c4SDavid Howells } 1155017679c4SDavid Howells 11569156235bSDavid Howells okay: 1157017679c4SDavid Howells key = key_ref_to_ptr(key_ref); 1158017679c4SDavid Howells 1159017679c4SDavid Howells /* make the changes with the locks held to prevent races */ 1160017679c4SDavid Howells down_write(&key->sem); 1161017679c4SDavid Howells 1162017679c4SDavid Howells expiry = 0; 1163017679c4SDavid Howells if (timeout > 0) { 1164017679c4SDavid Howells now = current_kernel_time(); 1165017679c4SDavid Howells expiry = now.tv_sec + timeout; 1166017679c4SDavid Howells } 1167017679c4SDavid Howells 1168017679c4SDavid Howells key->expiry = expiry; 1169c08ef808SDavid Howells key_schedule_gc(key->expiry + key_gc_delay); 1170017679c4SDavid Howells 1171017679c4SDavid Howells up_write(&key->sem); 1172017679c4SDavid Howells key_put(key); 1173017679c4SDavid Howells 1174017679c4SDavid Howells ret = 0; 1175017679c4SDavid Howells error: 1176017679c4SDavid Howells return ret; 1177a8b17ed0SDavid Howells } 1178017679c4SDavid Howells 1179017679c4SDavid Howells /* 1180*973c9f4fSDavid Howells * Assume (or clear) the authority to instantiate the specified key. 1181*973c9f4fSDavid Howells * 1182*973c9f4fSDavid Howells * This sets the authoritative token currently in force for key instantiation. 1183*973c9f4fSDavid Howells * This must be done for a key to be instantiated. It has the effect of making 1184*973c9f4fSDavid Howells * available all the keys from the caller of the request_key() that created a 1185*973c9f4fSDavid Howells * key to request_key() calls made by the caller of this function. 1186*973c9f4fSDavid Howells * 1187*973c9f4fSDavid Howells * The caller must have the instantiation key in their process keyrings with a 1188*973c9f4fSDavid Howells * Search permission grant available to the caller. 1189*973c9f4fSDavid Howells * 1190*973c9f4fSDavid Howells * If the ID given is 0, then the setting will be cleared and 0 returned. 1191*973c9f4fSDavid Howells * 1192*973c9f4fSDavid Howells * If the ID given has a matching an authorisation key, then that key will be 1193*973c9f4fSDavid Howells * set and its ID will be returned. The authorisation key can be read to get 1194*973c9f4fSDavid Howells * the callout information passed to request_key(). 1195b5f545c8SDavid Howells */ 1196b5f545c8SDavid Howells long keyctl_assume_authority(key_serial_t id) 1197b5f545c8SDavid Howells { 1198b5f545c8SDavid Howells struct key *authkey; 1199b5f545c8SDavid Howells long ret; 1200b5f545c8SDavid Howells 1201b5f545c8SDavid Howells /* special key IDs aren't permitted */ 1202b5f545c8SDavid Howells ret = -EINVAL; 1203b5f545c8SDavid Howells if (id < 0) 1204b5f545c8SDavid Howells goto error; 1205b5f545c8SDavid Howells 1206b5f545c8SDavid Howells /* we divest ourselves of authority if given an ID of 0 */ 1207b5f545c8SDavid Howells if (id == 0) { 1208d84f4f99SDavid Howells ret = keyctl_change_reqkey_auth(NULL); 1209b5f545c8SDavid Howells goto error; 1210b5f545c8SDavid Howells } 1211b5f545c8SDavid Howells 1212b5f545c8SDavid Howells /* attempt to assume the authority temporarily granted to us whilst we 1213b5f545c8SDavid Howells * instantiate the specified key 1214b5f545c8SDavid Howells * - the authorisation key must be in the current task's keyrings 1215b5f545c8SDavid Howells * somewhere 1216b5f545c8SDavid Howells */ 1217b5f545c8SDavid Howells authkey = key_get_instantiation_authkey(id); 1218b5f545c8SDavid Howells if (IS_ERR(authkey)) { 1219b5f545c8SDavid Howells ret = PTR_ERR(authkey); 1220b5f545c8SDavid Howells goto error; 1221b5f545c8SDavid Howells } 1222b5f545c8SDavid Howells 1223d84f4f99SDavid Howells ret = keyctl_change_reqkey_auth(authkey); 1224d84f4f99SDavid Howells if (ret < 0) 1225d84f4f99SDavid Howells goto error; 1226d84f4f99SDavid Howells key_put(authkey); 1227b5f545c8SDavid Howells 1228d84f4f99SDavid Howells ret = authkey->serial; 1229b5f545c8SDavid Howells error: 1230b5f545c8SDavid Howells return ret; 1231a8b17ed0SDavid Howells } 1232b5f545c8SDavid Howells 123370a5bb72SDavid Howells /* 1234*973c9f4fSDavid Howells * Get a key's the LSM security label. 1235*973c9f4fSDavid Howells * 1236*973c9f4fSDavid Howells * The key must grant the caller View permission for this to work. 1237*973c9f4fSDavid Howells * 1238*973c9f4fSDavid Howells * If there's a buffer, then up to buflen bytes of data will be placed into it. 1239*973c9f4fSDavid Howells * 1240*973c9f4fSDavid Howells * If successful, the amount of information available will be returned, 1241*973c9f4fSDavid Howells * irrespective of how much was copied (including the terminal NUL). 124270a5bb72SDavid Howells */ 124370a5bb72SDavid Howells long keyctl_get_security(key_serial_t keyid, 124470a5bb72SDavid Howells char __user *buffer, 124570a5bb72SDavid Howells size_t buflen) 124670a5bb72SDavid Howells { 124770a5bb72SDavid Howells struct key *key, *instkey; 124870a5bb72SDavid Howells key_ref_t key_ref; 124970a5bb72SDavid Howells char *context; 125070a5bb72SDavid Howells long ret; 125170a5bb72SDavid Howells 12525593122eSDavid Howells key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW); 125370a5bb72SDavid Howells if (IS_ERR(key_ref)) { 125470a5bb72SDavid Howells if (PTR_ERR(key_ref) != -EACCES) 125570a5bb72SDavid Howells return PTR_ERR(key_ref); 125670a5bb72SDavid Howells 125770a5bb72SDavid Howells /* viewing a key under construction is also permitted if we 125870a5bb72SDavid Howells * have the authorisation token handy */ 125970a5bb72SDavid Howells instkey = key_get_instantiation_authkey(keyid); 126070a5bb72SDavid Howells if (IS_ERR(instkey)) 1261fa1cc7b5SRoel Kluin return PTR_ERR(instkey); 126270a5bb72SDavid Howells key_put(instkey); 126370a5bb72SDavid Howells 12645593122eSDavid Howells key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0); 126570a5bb72SDavid Howells if (IS_ERR(key_ref)) 126670a5bb72SDavid Howells return PTR_ERR(key_ref); 126770a5bb72SDavid Howells } 126870a5bb72SDavid Howells 126970a5bb72SDavid Howells key = key_ref_to_ptr(key_ref); 127070a5bb72SDavid Howells ret = security_key_getsecurity(key, &context); 127170a5bb72SDavid Howells if (ret == 0) { 127270a5bb72SDavid Howells /* if no information was returned, give userspace an empty 127370a5bb72SDavid Howells * string */ 127470a5bb72SDavid Howells ret = 1; 127570a5bb72SDavid Howells if (buffer && buflen > 0 && 127670a5bb72SDavid Howells copy_to_user(buffer, "", 1) != 0) 127770a5bb72SDavid Howells ret = -EFAULT; 127870a5bb72SDavid Howells } else if (ret > 0) { 127970a5bb72SDavid Howells /* return as much data as there's room for */ 128070a5bb72SDavid Howells if (buffer && buflen > 0) { 128170a5bb72SDavid Howells if (buflen > ret) 128270a5bb72SDavid Howells buflen = ret; 128370a5bb72SDavid Howells 128470a5bb72SDavid Howells if (copy_to_user(buffer, context, buflen) != 0) 128570a5bb72SDavid Howells ret = -EFAULT; 128670a5bb72SDavid Howells } 128770a5bb72SDavid Howells 128870a5bb72SDavid Howells kfree(context); 128970a5bb72SDavid Howells } 129070a5bb72SDavid Howells 129170a5bb72SDavid Howells key_ref_put(key_ref); 129270a5bb72SDavid Howells return ret; 129370a5bb72SDavid Howells } 129470a5bb72SDavid Howells 1295ee18d64cSDavid Howells /* 1296*973c9f4fSDavid Howells * Attempt to install the calling process's session keyring on the process's 1297*973c9f4fSDavid Howells * parent process. 1298*973c9f4fSDavid Howells * 1299*973c9f4fSDavid Howells * The keyring must exist and must grant the caller LINK permission, and the 1300*973c9f4fSDavid Howells * parent process must be single-threaded and must have the same effective 1301*973c9f4fSDavid Howells * ownership as this process and mustn't be SUID/SGID. 1302*973c9f4fSDavid Howells * 1303*973c9f4fSDavid Howells * The keyring will be emplaced on the parent when it next resumes userspace. 1304*973c9f4fSDavid Howells * 1305*973c9f4fSDavid Howells * If successful, 0 will be returned. 1306ee18d64cSDavid Howells */ 1307ee18d64cSDavid Howells long keyctl_session_to_parent(void) 1308ee18d64cSDavid Howells { 1309a00ae4d2SGeert Uytterhoeven #ifdef TIF_NOTIFY_RESUME 1310ee18d64cSDavid Howells struct task_struct *me, *parent; 1311ee18d64cSDavid Howells const struct cred *mycred, *pcred; 1312ee18d64cSDavid Howells struct cred *cred, *oldcred; 1313ee18d64cSDavid Howells key_ref_t keyring_r; 1314ee18d64cSDavid Howells int ret; 1315ee18d64cSDavid Howells 1316ee18d64cSDavid Howells keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_LINK); 1317ee18d64cSDavid Howells if (IS_ERR(keyring_r)) 1318ee18d64cSDavid Howells return PTR_ERR(keyring_r); 1319ee18d64cSDavid Howells 1320ee18d64cSDavid Howells /* our parent is going to need a new cred struct, a new tgcred struct 1321ee18d64cSDavid Howells * and new security data, so we allocate them here to prevent ENOMEM in 1322ee18d64cSDavid Howells * our parent */ 1323ee18d64cSDavid Howells ret = -ENOMEM; 1324ee18d64cSDavid Howells cred = cred_alloc_blank(); 1325ee18d64cSDavid Howells if (!cred) 1326ee18d64cSDavid Howells goto error_keyring; 1327ee18d64cSDavid Howells 1328ee18d64cSDavid Howells cred->tgcred->session_keyring = key_ref_to_ptr(keyring_r); 1329ee18d64cSDavid Howells keyring_r = NULL; 1330ee18d64cSDavid Howells 1331ee18d64cSDavid Howells me = current; 13329d1ac65aSDavid Howells rcu_read_lock(); 1333ee18d64cSDavid Howells write_lock_irq(&tasklist_lock); 1334ee18d64cSDavid Howells 1335ee18d64cSDavid Howells parent = me->real_parent; 1336ee18d64cSDavid Howells ret = -EPERM; 1337ee18d64cSDavid Howells 1338ee18d64cSDavid Howells /* the parent mustn't be init and mustn't be a kernel thread */ 1339ee18d64cSDavid Howells if (parent->pid <= 1 || !parent->mm) 1340ee18d64cSDavid Howells goto not_permitted; 1341ee18d64cSDavid Howells 1342ee18d64cSDavid Howells /* the parent must be single threaded */ 1343dd98acf7SOleg Nesterov if (!thread_group_empty(parent)) 1344ee18d64cSDavid Howells goto not_permitted; 1345ee18d64cSDavid Howells 1346ee18d64cSDavid Howells /* the parent and the child must have different session keyrings or 1347ee18d64cSDavid Howells * there's no point */ 1348ee18d64cSDavid Howells mycred = current_cred(); 1349ee18d64cSDavid Howells pcred = __task_cred(parent); 1350ee18d64cSDavid Howells if (mycred == pcred || 1351ee18d64cSDavid Howells mycred->tgcred->session_keyring == pcred->tgcred->session_keyring) 1352ee18d64cSDavid Howells goto already_same; 1353ee18d64cSDavid Howells 1354ee18d64cSDavid Howells /* the parent must have the same effective ownership and mustn't be 1355ee18d64cSDavid Howells * SUID/SGID */ 1356ee18d64cSDavid Howells if (pcred->uid != mycred->euid || 1357ee18d64cSDavid Howells pcred->euid != mycred->euid || 1358ee18d64cSDavid Howells pcred->suid != mycred->euid || 1359ee18d64cSDavid Howells pcred->gid != mycred->egid || 1360ee18d64cSDavid Howells pcred->egid != mycred->egid || 1361ee18d64cSDavid Howells pcred->sgid != mycred->egid) 1362ee18d64cSDavid Howells goto not_permitted; 1363ee18d64cSDavid Howells 1364ee18d64cSDavid Howells /* the keyrings must have the same UID */ 13653d96406cSDavid Howells if ((pcred->tgcred->session_keyring && 13663d96406cSDavid Howells pcred->tgcred->session_keyring->uid != mycred->euid) || 1367ee18d64cSDavid Howells mycred->tgcred->session_keyring->uid != mycred->euid) 1368ee18d64cSDavid Howells goto not_permitted; 1369ee18d64cSDavid Howells 1370ee18d64cSDavid Howells /* if there's an already pending keyring replacement, then we replace 1371ee18d64cSDavid Howells * that */ 1372ee18d64cSDavid Howells oldcred = parent->replacement_session_keyring; 1373ee18d64cSDavid Howells 1374ee18d64cSDavid Howells /* the replacement session keyring is applied just prior to userspace 1375ee18d64cSDavid Howells * restarting */ 1376ee18d64cSDavid Howells parent->replacement_session_keyring = cred; 1377ee18d64cSDavid Howells cred = NULL; 1378ee18d64cSDavid Howells set_ti_thread_flag(task_thread_info(parent), TIF_NOTIFY_RESUME); 1379ee18d64cSDavid Howells 1380ee18d64cSDavid Howells write_unlock_irq(&tasklist_lock); 13819d1ac65aSDavid Howells rcu_read_unlock(); 1382ee18d64cSDavid Howells if (oldcred) 1383ee18d64cSDavid Howells put_cred(oldcred); 1384ee18d64cSDavid Howells return 0; 1385ee18d64cSDavid Howells 1386ee18d64cSDavid Howells already_same: 1387ee18d64cSDavid Howells ret = 0; 1388ee18d64cSDavid Howells not_permitted: 13895c84342aSMarc Dionne write_unlock_irq(&tasklist_lock); 13909d1ac65aSDavid Howells rcu_read_unlock(); 1391ee18d64cSDavid Howells put_cred(cred); 1392ee18d64cSDavid Howells return ret; 1393ee18d64cSDavid Howells 1394ee18d64cSDavid Howells error_keyring: 1395ee18d64cSDavid Howells key_ref_put(keyring_r); 1396ee18d64cSDavid Howells return ret; 1397a00ae4d2SGeert Uytterhoeven 1398a00ae4d2SGeert Uytterhoeven #else /* !TIF_NOTIFY_RESUME */ 1399a00ae4d2SGeert Uytterhoeven /* 1400a00ae4d2SGeert Uytterhoeven * To be removed when TIF_NOTIFY_RESUME has been implemented on 1401a00ae4d2SGeert Uytterhoeven * m68k/xtensa 1402a00ae4d2SGeert Uytterhoeven */ 1403a00ae4d2SGeert Uytterhoeven #warning TIF_NOTIFY_RESUME not implemented 1404a00ae4d2SGeert Uytterhoeven return -EOPNOTSUPP; 1405a00ae4d2SGeert Uytterhoeven #endif /* !TIF_NOTIFY_RESUME */ 1406ee18d64cSDavid Howells } 1407ee18d64cSDavid Howells 1408b5f545c8SDavid Howells /* 1409*973c9f4fSDavid Howells * The key control system call 14101da177e4SLinus Torvalds */ 1411938bb9f5SHeiko Carstens SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3, 1412938bb9f5SHeiko Carstens unsigned long, arg4, unsigned long, arg5) 14131da177e4SLinus Torvalds { 14141da177e4SLinus Torvalds switch (option) { 14151da177e4SLinus Torvalds case KEYCTL_GET_KEYRING_ID: 14161da177e4SLinus Torvalds return keyctl_get_keyring_ID((key_serial_t) arg2, 14171da177e4SLinus Torvalds (int) arg3); 14181da177e4SLinus Torvalds 14191da177e4SLinus Torvalds case KEYCTL_JOIN_SESSION_KEYRING: 14201da177e4SLinus Torvalds return keyctl_join_session_keyring((const char __user *) arg2); 14211da177e4SLinus Torvalds 14221da177e4SLinus Torvalds case KEYCTL_UPDATE: 14231da177e4SLinus Torvalds return keyctl_update_key((key_serial_t) arg2, 14241da177e4SLinus Torvalds (const void __user *) arg3, 14251da177e4SLinus Torvalds (size_t) arg4); 14261da177e4SLinus Torvalds 14271da177e4SLinus Torvalds case KEYCTL_REVOKE: 14281da177e4SLinus Torvalds return keyctl_revoke_key((key_serial_t) arg2); 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds case KEYCTL_DESCRIBE: 14311da177e4SLinus Torvalds return keyctl_describe_key((key_serial_t) arg2, 14321da177e4SLinus Torvalds (char __user *) arg3, 14331da177e4SLinus Torvalds (unsigned) arg4); 14341da177e4SLinus Torvalds 14351da177e4SLinus Torvalds case KEYCTL_CLEAR: 14361da177e4SLinus Torvalds return keyctl_keyring_clear((key_serial_t) arg2); 14371da177e4SLinus Torvalds 14381da177e4SLinus Torvalds case KEYCTL_LINK: 14391da177e4SLinus Torvalds return keyctl_keyring_link((key_serial_t) arg2, 14401da177e4SLinus Torvalds (key_serial_t) arg3); 14411da177e4SLinus Torvalds 14421da177e4SLinus Torvalds case KEYCTL_UNLINK: 14431da177e4SLinus Torvalds return keyctl_keyring_unlink((key_serial_t) arg2, 14441da177e4SLinus Torvalds (key_serial_t) arg3); 14451da177e4SLinus Torvalds 14461da177e4SLinus Torvalds case KEYCTL_SEARCH: 14471da177e4SLinus Torvalds return keyctl_keyring_search((key_serial_t) arg2, 14481da177e4SLinus Torvalds (const char __user *) arg3, 14491da177e4SLinus Torvalds (const char __user *) arg4, 14501da177e4SLinus Torvalds (key_serial_t) arg5); 14511da177e4SLinus Torvalds 14521da177e4SLinus Torvalds case KEYCTL_READ: 14531da177e4SLinus Torvalds return keyctl_read_key((key_serial_t) arg2, 14541da177e4SLinus Torvalds (char __user *) arg3, 14551da177e4SLinus Torvalds (size_t) arg4); 14561da177e4SLinus Torvalds 14571da177e4SLinus Torvalds case KEYCTL_CHOWN: 14581da177e4SLinus Torvalds return keyctl_chown_key((key_serial_t) arg2, 14591da177e4SLinus Torvalds (uid_t) arg3, 14601da177e4SLinus Torvalds (gid_t) arg4); 14611da177e4SLinus Torvalds 14621da177e4SLinus Torvalds case KEYCTL_SETPERM: 14631da177e4SLinus Torvalds return keyctl_setperm_key((key_serial_t) arg2, 14641da177e4SLinus Torvalds (key_perm_t) arg3); 14651da177e4SLinus Torvalds 14661da177e4SLinus Torvalds case KEYCTL_INSTANTIATE: 14671da177e4SLinus Torvalds return keyctl_instantiate_key((key_serial_t) arg2, 14681da177e4SLinus Torvalds (const void __user *) arg3, 14691da177e4SLinus Torvalds (size_t) arg4, 14701da177e4SLinus Torvalds (key_serial_t) arg5); 14711da177e4SLinus Torvalds 14721da177e4SLinus Torvalds case KEYCTL_NEGATE: 14731da177e4SLinus Torvalds return keyctl_negate_key((key_serial_t) arg2, 14741da177e4SLinus Torvalds (unsigned) arg3, 14751da177e4SLinus Torvalds (key_serial_t) arg4); 14761da177e4SLinus Torvalds 14773e30148cSDavid Howells case KEYCTL_SET_REQKEY_KEYRING: 14783e30148cSDavid Howells return keyctl_set_reqkey_keyring(arg2); 14793e30148cSDavid Howells 1480017679c4SDavid Howells case KEYCTL_SET_TIMEOUT: 1481017679c4SDavid Howells return keyctl_set_timeout((key_serial_t) arg2, 1482017679c4SDavid Howells (unsigned) arg3); 1483017679c4SDavid Howells 1484b5f545c8SDavid Howells case KEYCTL_ASSUME_AUTHORITY: 1485b5f545c8SDavid Howells return keyctl_assume_authority((key_serial_t) arg2); 1486b5f545c8SDavid Howells 148770a5bb72SDavid Howells case KEYCTL_GET_SECURITY: 148870a5bb72SDavid Howells return keyctl_get_security((key_serial_t) arg2, 148990bd49abSJames Morris (char __user *) arg3, 149070a5bb72SDavid Howells (size_t) arg4); 149170a5bb72SDavid Howells 1492ee18d64cSDavid Howells case KEYCTL_SESSION_TO_PARENT: 1493ee18d64cSDavid Howells return keyctl_session_to_parent(); 1494ee18d64cSDavid Howells 14951da177e4SLinus Torvalds default: 14961da177e4SLinus Torvalds return -EOPNOTSUPP; 14971da177e4SLinus Torvalds } 1498a8b17ed0SDavid Howells } 1499