11da177e4SLinus Torvalds /* keyctl.c: userspace keyctl 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 350cb409d9SDavi Arnaut if (ret < 0) 360cb409d9SDavi Arnaut return -EFAULT; 370cb409d9SDavi Arnaut 380cb409d9SDavi Arnaut if (ret == 0 || ret >= len) 390cb409d9SDavi Arnaut return -EINVAL; 400cb409d9SDavi Arnaut 410cb409d9SDavi Arnaut if (type[0] == '.') 420cb409d9SDavi Arnaut return -EPERM; 430cb409d9SDavi Arnaut 440cb409d9SDavi Arnaut type[len - 1] = '\0'; 450cb409d9SDavi Arnaut 460cb409d9SDavi Arnaut return 0; 470cb409d9SDavi Arnaut } 480cb409d9SDavi Arnaut 491da177e4SLinus Torvalds /*****************************************************************************/ 501da177e4SLinus Torvalds /* 511da177e4SLinus Torvalds * extract the description of a new key from userspace and either add it as a 521da177e4SLinus Torvalds * new key to the specified keyring or update a matching key in that keyring 531da177e4SLinus Torvalds * - the keyring must be writable 541da177e4SLinus Torvalds * - returns the new key's serial number 551da177e4SLinus Torvalds * - implements add_key() 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 780cb409d9SDavi Arnaut description = strndup_user(_description, PAGE_SIZE); 790cb409d9SDavi Arnaut if (IS_ERR(description)) { 800cb409d9SDavi Arnaut ret = PTR_ERR(description); 813e30148cSDavid Howells goto error; 820cb409d9SDavi Arnaut } 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 851da177e4SLinus Torvalds payload = NULL; 861da177e4SLinus Torvalds 8738bbca6bSDavid Howells vm = false; 881da177e4SLinus Torvalds if (_payload) { 891da177e4SLinus Torvalds ret = -ENOMEM; 901da177e4SLinus Torvalds payload = kmalloc(plen, GFP_KERNEL); 9138bbca6bSDavid Howells if (!payload) { 9238bbca6bSDavid Howells if (plen <= PAGE_SIZE) 9338bbca6bSDavid Howells goto error2; 9438bbca6bSDavid Howells vm = true; 9538bbca6bSDavid Howells payload = vmalloc(plen); 961da177e4SLinus Torvalds if (!payload) 971da177e4SLinus Torvalds goto error2; 9838bbca6bSDavid Howells } 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds ret = -EFAULT; 1011da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 1021da177e4SLinus Torvalds goto error3; 1031da177e4SLinus Torvalds } 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds /* find the target keyring (which must be writable) */ 1068bbf4976SDavid Howells keyring_ref = lookup_user_key(ringid, 1, 0, KEY_WRITE); 107664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 108664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 1091da177e4SLinus Torvalds goto error3; 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds /* create or update the requested key and add it to the target 1131da177e4SLinus Torvalds * keyring */ 114664cceb0SDavid Howells key_ref = key_create_or_update(keyring_ref, type, description, 1156b79ccb5SArun Raghavan payload, plen, KEY_PERM_UNDEF, 1166b79ccb5SArun Raghavan KEY_ALLOC_IN_QUOTA); 117664cceb0SDavid Howells if (!IS_ERR(key_ref)) { 118664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 119664cceb0SDavid Howells key_ref_put(key_ref); 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds else { 122664cceb0SDavid Howells ret = PTR_ERR(key_ref); 1231da177e4SLinus Torvalds } 1241da177e4SLinus Torvalds 125664cceb0SDavid Howells key_ref_put(keyring_ref); 1261da177e4SLinus Torvalds error3: 12738bbca6bSDavid Howells if (!vm) 1281da177e4SLinus Torvalds kfree(payload); 12938bbca6bSDavid Howells else 13038bbca6bSDavid Howells vfree(payload); 1311da177e4SLinus Torvalds error2: 1321da177e4SLinus Torvalds kfree(description); 1331da177e4SLinus Torvalds error: 1341da177e4SLinus Torvalds return ret; 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds } /* end sys_add_key() */ 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds /*****************************************************************************/ 1391da177e4SLinus Torvalds /* 1401da177e4SLinus Torvalds * search the process keyrings for a matching key 1411da177e4SLinus Torvalds * - nested keyrings may also be searched if they have Search permission 1421da177e4SLinus Torvalds * - if a key is found, it will be attached to the destination keyring if 1431da177e4SLinus Torvalds * there's one specified 1441da177e4SLinus Torvalds * - /sbin/request-key will be invoked if _callout_info is non-NULL 1451da177e4SLinus Torvalds * - the _callout_info string will be passed to /sbin/request-key 1461da177e4SLinus Torvalds * - if the _callout_info string is empty, it will be rendered as "-" 1471da177e4SLinus Torvalds * - implements request_key() 1481da177e4SLinus Torvalds */ 1491e7bfb21SHeiko Carstens SYSCALL_DEFINE4(request_key, const char __user *, _type, 1501e7bfb21SHeiko Carstens const char __user *, _description, 1511e7bfb21SHeiko Carstens const char __user *, _callout_info, 1521e7bfb21SHeiko Carstens key_serial_t, destringid) 1531da177e4SLinus Torvalds { 1541da177e4SLinus Torvalds struct key_type *ktype; 155664cceb0SDavid Howells struct key *key; 156664cceb0SDavid Howells key_ref_t dest_ref; 1574a38e122SDavid Howells size_t callout_len; 1581da177e4SLinus Torvalds char type[32], *description, *callout_info; 1590cb409d9SDavi Arnaut long ret; 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds /* pull the type into kernel space */ 1620cb409d9SDavi Arnaut ret = key_get_type_from_user(type, _type, sizeof(type)); 1631da177e4SLinus Torvalds if (ret < 0) 1641da177e4SLinus Torvalds goto error; 1651260f801SDavid Howells 1661da177e4SLinus Torvalds /* pull the description into kernel space */ 1670cb409d9SDavi Arnaut description = strndup_user(_description, PAGE_SIZE); 1680cb409d9SDavi Arnaut if (IS_ERR(description)) { 1690cb409d9SDavi Arnaut ret = PTR_ERR(description); 1701da177e4SLinus Torvalds goto error; 1710cb409d9SDavi Arnaut } 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds /* pull the callout info into kernel space */ 1741da177e4SLinus Torvalds callout_info = NULL; 1754a38e122SDavid Howells callout_len = 0; 1761da177e4SLinus Torvalds if (_callout_info) { 1770cb409d9SDavi Arnaut callout_info = strndup_user(_callout_info, PAGE_SIZE); 1780cb409d9SDavi Arnaut if (IS_ERR(callout_info)) { 1790cb409d9SDavi Arnaut ret = PTR_ERR(callout_info); 1801da177e4SLinus Torvalds goto error2; 1810cb409d9SDavi Arnaut } 1824a38e122SDavid Howells callout_len = strlen(callout_info); 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds /* get the destination keyring if specified */ 186664cceb0SDavid Howells dest_ref = NULL; 1871da177e4SLinus Torvalds if (destringid) { 1888bbf4976SDavid Howells dest_ref = lookup_user_key(destringid, 1, 0, KEY_WRITE); 189664cceb0SDavid Howells if (IS_ERR(dest_ref)) { 190664cceb0SDavid Howells ret = PTR_ERR(dest_ref); 1911da177e4SLinus Torvalds goto error3; 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds /* find the key type */ 1961da177e4SLinus Torvalds ktype = key_type_lookup(type); 1971da177e4SLinus Torvalds if (IS_ERR(ktype)) { 1981da177e4SLinus Torvalds ret = PTR_ERR(ktype); 1991da177e4SLinus Torvalds goto error4; 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds /* do the search */ 2034a38e122SDavid Howells key = request_key_and_link(ktype, description, callout_info, 2044a38e122SDavid Howells callout_len, NULL, key_ref_to_ptr(dest_ref), 2057e047ef5SDavid Howells KEY_ALLOC_IN_QUOTA); 2061da177e4SLinus Torvalds if (IS_ERR(key)) { 2071da177e4SLinus Torvalds ret = PTR_ERR(key); 2081da177e4SLinus Torvalds goto error5; 2091da177e4SLinus Torvalds } 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds ret = key->serial; 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds key_put(key); 2141da177e4SLinus Torvalds error5: 2151da177e4SLinus Torvalds key_type_put(ktype); 2161da177e4SLinus Torvalds error4: 217664cceb0SDavid Howells key_ref_put(dest_ref); 2181da177e4SLinus Torvalds error3: 2191da177e4SLinus Torvalds kfree(callout_info); 2201da177e4SLinus Torvalds error2: 2211da177e4SLinus Torvalds kfree(description); 2221da177e4SLinus Torvalds error: 2231da177e4SLinus Torvalds return ret; 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds } /* end sys_request_key() */ 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds /*****************************************************************************/ 2281da177e4SLinus Torvalds /* 2291da177e4SLinus Torvalds * get the ID of the specified process keyring 2301da177e4SLinus Torvalds * - the keyring must have search permission to be found 2311da177e4SLinus Torvalds * - implements keyctl(KEYCTL_GET_KEYRING_ID) 2321da177e4SLinus Torvalds */ 2331da177e4SLinus Torvalds long keyctl_get_keyring_ID(key_serial_t id, int create) 2341da177e4SLinus Torvalds { 235664cceb0SDavid Howells key_ref_t key_ref; 2361da177e4SLinus Torvalds long ret; 2371da177e4SLinus Torvalds 2388bbf4976SDavid Howells key_ref = lookup_user_key(id, create, 0, 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; 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds } /* end keyctl_get_keyring_ID() */ 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds /*****************************************************************************/ 2521da177e4SLinus Torvalds /* 2531da177e4SLinus Torvalds * join the session keyring 2541da177e4SLinus Torvalds * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING) 2551da177e4SLinus Torvalds */ 2561da177e4SLinus Torvalds long keyctl_join_session_keyring(const char __user *_name) 2571da177e4SLinus Torvalds { 2581da177e4SLinus Torvalds char *name; 2590cb409d9SDavi Arnaut long ret; 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds /* fetch the name from userspace */ 2621da177e4SLinus Torvalds name = NULL; 2631da177e4SLinus Torvalds if (_name) { 2640cb409d9SDavi Arnaut name = strndup_user(_name, PAGE_SIZE); 2650cb409d9SDavi Arnaut if (IS_ERR(name)) { 2660cb409d9SDavi Arnaut ret = PTR_ERR(name); 2671da177e4SLinus Torvalds goto error; 2680cb409d9SDavi Arnaut } 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds /* join the session */ 2721da177e4SLinus Torvalds ret = join_session_keyring(name); 2730d54ee1cSVegard Nossum kfree(name); 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds error: 2761da177e4SLinus Torvalds return ret; 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds } /* end keyctl_join_session_keyring() */ 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds /*****************************************************************************/ 2811da177e4SLinus Torvalds /* 2821da177e4SLinus Torvalds * update a key's data payload 2831da177e4SLinus Torvalds * - the key must be writable 2841da177e4SLinus Torvalds * - implements keyctl(KEYCTL_UPDATE) 2851da177e4SLinus Torvalds */ 2861da177e4SLinus Torvalds long keyctl_update_key(key_serial_t id, 2871da177e4SLinus Torvalds const void __user *_payload, 2881da177e4SLinus Torvalds size_t plen) 2891da177e4SLinus Torvalds { 290664cceb0SDavid Howells key_ref_t key_ref; 2911da177e4SLinus Torvalds void *payload; 2921da177e4SLinus Torvalds long ret; 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds ret = -EINVAL; 2951da177e4SLinus Torvalds if (plen > PAGE_SIZE) 2961da177e4SLinus Torvalds goto error; 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 2991da177e4SLinus Torvalds payload = NULL; 3001da177e4SLinus Torvalds if (_payload) { 3011da177e4SLinus Torvalds ret = -ENOMEM; 3021da177e4SLinus Torvalds payload = kmalloc(plen, GFP_KERNEL); 3031da177e4SLinus Torvalds if (!payload) 3041da177e4SLinus Torvalds goto error; 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds ret = -EFAULT; 3071da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 3081da177e4SLinus Torvalds goto error2; 3091da177e4SLinus Torvalds } 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds /* find the target key (which must be writable) */ 3128bbf4976SDavid Howells key_ref = lookup_user_key(id, 0, 0, KEY_WRITE); 313664cceb0SDavid Howells if (IS_ERR(key_ref)) { 314664cceb0SDavid Howells ret = PTR_ERR(key_ref); 3151da177e4SLinus Torvalds goto error2; 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds /* update the key */ 319664cceb0SDavid Howells ret = key_update(key_ref, payload, plen); 3201da177e4SLinus Torvalds 321664cceb0SDavid Howells key_ref_put(key_ref); 3221da177e4SLinus Torvalds error2: 3231da177e4SLinus Torvalds kfree(payload); 3241da177e4SLinus Torvalds error: 3251da177e4SLinus Torvalds return ret; 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds } /* end keyctl_update_key() */ 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds /*****************************************************************************/ 3301da177e4SLinus Torvalds /* 3311da177e4SLinus Torvalds * revoke a key 3321da177e4SLinus Torvalds * - the key must be writable 3331da177e4SLinus Torvalds * - implements keyctl(KEYCTL_REVOKE) 3341da177e4SLinus Torvalds */ 3351da177e4SLinus Torvalds long keyctl_revoke_key(key_serial_t id) 3361da177e4SLinus Torvalds { 337664cceb0SDavid Howells key_ref_t key_ref; 3381da177e4SLinus Torvalds long ret; 3391da177e4SLinus Torvalds 3408bbf4976SDavid Howells key_ref = lookup_user_key(id, 0, 0, KEY_WRITE); 341664cceb0SDavid Howells if (IS_ERR(key_ref)) { 342664cceb0SDavid Howells ret = PTR_ERR(key_ref); 3431da177e4SLinus Torvalds goto error; 3441da177e4SLinus Torvalds } 3451da177e4SLinus Torvalds 346664cceb0SDavid Howells key_revoke(key_ref_to_ptr(key_ref)); 3471da177e4SLinus Torvalds ret = 0; 3481da177e4SLinus Torvalds 349664cceb0SDavid Howells key_ref_put(key_ref); 3501da177e4SLinus Torvalds error: 3511260f801SDavid Howells return ret; 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds } /* end keyctl_revoke_key() */ 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds /*****************************************************************************/ 3561da177e4SLinus Torvalds /* 3571da177e4SLinus Torvalds * clear the specified process keyring 3581da177e4SLinus Torvalds * - the keyring must be writable 3591da177e4SLinus Torvalds * - implements keyctl(KEYCTL_CLEAR) 3601da177e4SLinus Torvalds */ 3611da177e4SLinus Torvalds long keyctl_keyring_clear(key_serial_t ringid) 3621da177e4SLinus Torvalds { 363664cceb0SDavid Howells key_ref_t keyring_ref; 3641da177e4SLinus Torvalds long ret; 3651da177e4SLinus Torvalds 3668bbf4976SDavid Howells keyring_ref = lookup_user_key(ringid, 1, 0, KEY_WRITE); 367664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 368664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 3691da177e4SLinus Torvalds goto error; 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds 372664cceb0SDavid Howells ret = keyring_clear(key_ref_to_ptr(keyring_ref)); 3731da177e4SLinus Torvalds 374664cceb0SDavid Howells key_ref_put(keyring_ref); 3751da177e4SLinus Torvalds error: 3761da177e4SLinus Torvalds return ret; 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds } /* end keyctl_keyring_clear() */ 3791da177e4SLinus Torvalds 3801da177e4SLinus Torvalds /*****************************************************************************/ 3811da177e4SLinus Torvalds /* 3821da177e4SLinus Torvalds * link a key into a keyring 3831da177e4SLinus Torvalds * - the keyring must be writable 3841da177e4SLinus Torvalds * - the key must be linkable 3851da177e4SLinus Torvalds * - implements keyctl(KEYCTL_LINK) 3861da177e4SLinus Torvalds */ 3871da177e4SLinus Torvalds long keyctl_keyring_link(key_serial_t id, key_serial_t ringid) 3881da177e4SLinus Torvalds { 389664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 3901da177e4SLinus Torvalds long ret; 3911da177e4SLinus Torvalds 3928bbf4976SDavid Howells keyring_ref = lookup_user_key(ringid, 1, 0, KEY_WRITE); 393664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 394664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 3951da177e4SLinus Torvalds goto error; 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds 3988bbf4976SDavid Howells key_ref = lookup_user_key(id, 1, 0, KEY_LINK); 399664cceb0SDavid Howells if (IS_ERR(key_ref)) { 400664cceb0SDavid Howells ret = PTR_ERR(key_ref); 4011da177e4SLinus Torvalds goto error2; 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds 404664cceb0SDavid Howells ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref)); 4051da177e4SLinus Torvalds 406664cceb0SDavid Howells key_ref_put(key_ref); 4071da177e4SLinus Torvalds error2: 408664cceb0SDavid Howells key_ref_put(keyring_ref); 4091da177e4SLinus Torvalds error: 4101da177e4SLinus Torvalds return ret; 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds } /* end keyctl_keyring_link() */ 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds /*****************************************************************************/ 4151da177e4SLinus Torvalds /* 4161da177e4SLinus Torvalds * unlink the first attachment of a key from a keyring 4171da177e4SLinus Torvalds * - the keyring must be writable 4181da177e4SLinus Torvalds * - we don't need any permissions on the key 4191da177e4SLinus Torvalds * - implements keyctl(KEYCTL_UNLINK) 4201da177e4SLinus Torvalds */ 4211da177e4SLinus Torvalds long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid) 4221da177e4SLinus Torvalds { 423664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 4241da177e4SLinus Torvalds long ret; 4251da177e4SLinus Torvalds 4268bbf4976SDavid Howells keyring_ref = lookup_user_key(ringid, 0, 0, KEY_WRITE); 427664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 428664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 4291da177e4SLinus Torvalds goto error; 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds 4328bbf4976SDavid Howells key_ref = lookup_user_key(id, 0, 0, 0); 433664cceb0SDavid Howells if (IS_ERR(key_ref)) { 434664cceb0SDavid Howells ret = PTR_ERR(key_ref); 4351da177e4SLinus Torvalds goto error2; 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds 438664cceb0SDavid Howells ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref)); 4391da177e4SLinus Torvalds 440664cceb0SDavid Howells key_ref_put(key_ref); 4411da177e4SLinus Torvalds error2: 442664cceb0SDavid Howells key_ref_put(keyring_ref); 4431da177e4SLinus Torvalds error: 4441da177e4SLinus Torvalds return ret; 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds } /* end keyctl_keyring_unlink() */ 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds /*****************************************************************************/ 4491da177e4SLinus Torvalds /* 4501da177e4SLinus Torvalds * describe a user key 4511da177e4SLinus Torvalds * - the key must have view permission 4521da177e4SLinus Torvalds * - if there's a buffer, we place up to buflen bytes of data into it 4531da177e4SLinus Torvalds * - unless there's an error, we return the amount of description available, 4541da177e4SLinus Torvalds * irrespective of how much we may have copied 4551da177e4SLinus Torvalds * - the description is formatted thus: 4561da177e4SLinus Torvalds * type;uid;gid;perm;description<NUL> 4571da177e4SLinus Torvalds * - implements keyctl(KEYCTL_DESCRIBE) 4581da177e4SLinus Torvalds */ 4591da177e4SLinus Torvalds long keyctl_describe_key(key_serial_t keyid, 4601da177e4SLinus Torvalds char __user *buffer, 4611da177e4SLinus Torvalds size_t buflen) 4621da177e4SLinus Torvalds { 4633e30148cSDavid Howells struct key *key, *instkey; 464664cceb0SDavid Howells key_ref_t key_ref; 4651da177e4SLinus Torvalds char *tmpbuf; 4661da177e4SLinus Torvalds long ret; 4671da177e4SLinus Torvalds 4688bbf4976SDavid Howells key_ref = lookup_user_key(keyid, 0, 1, KEY_VIEW); 469664cceb0SDavid Howells if (IS_ERR(key_ref)) { 4703e30148cSDavid Howells /* viewing a key under construction is permitted if we have the 4713e30148cSDavid Howells * authorisation token handy */ 472664cceb0SDavid Howells if (PTR_ERR(key_ref) == -EACCES) { 4733e30148cSDavid Howells instkey = key_get_instantiation_authkey(keyid); 4743e30148cSDavid Howells if (!IS_ERR(instkey)) { 4753e30148cSDavid Howells key_put(instkey); 4768bbf4976SDavid Howells key_ref = lookup_user_key(keyid, 477664cceb0SDavid Howells 0, 1, 0); 478664cceb0SDavid Howells if (!IS_ERR(key_ref)) 4793e30148cSDavid Howells goto okay; 4803e30148cSDavid Howells } 4813e30148cSDavid Howells } 4823e30148cSDavid Howells 483664cceb0SDavid Howells ret = PTR_ERR(key_ref); 4841da177e4SLinus Torvalds goto error; 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds 4873e30148cSDavid Howells okay: 4881da177e4SLinus Torvalds /* calculate how much description we're going to return */ 4891da177e4SLinus Torvalds ret = -ENOMEM; 4901da177e4SLinus Torvalds tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); 4911da177e4SLinus Torvalds if (!tmpbuf) 4921da177e4SLinus Torvalds goto error2; 4931da177e4SLinus Torvalds 494664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 495664cceb0SDavid Howells 4961da177e4SLinus Torvalds ret = snprintf(tmpbuf, PAGE_SIZE - 1, 497664cceb0SDavid Howells "%s;%d;%d;%08x;%s", 498664cceb0SDavid Howells key_ref_to_ptr(key_ref)->type->name, 499664cceb0SDavid Howells key_ref_to_ptr(key_ref)->uid, 500664cceb0SDavid Howells key_ref_to_ptr(key_ref)->gid, 501664cceb0SDavid Howells key_ref_to_ptr(key_ref)->perm, 502664cceb0SDavid Howells key_ref_to_ptr(key_ref)->description ? 503664cceb0SDavid Howells key_ref_to_ptr(key_ref)->description : "" 5041da177e4SLinus Torvalds ); 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds /* include a NUL char at the end of the data */ 5071da177e4SLinus Torvalds if (ret > PAGE_SIZE - 1) 5081da177e4SLinus Torvalds ret = PAGE_SIZE - 1; 5091da177e4SLinus Torvalds tmpbuf[ret] = 0; 5101da177e4SLinus Torvalds ret++; 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds /* consider returning the data */ 5131da177e4SLinus Torvalds if (buffer && buflen > 0) { 5141da177e4SLinus Torvalds if (buflen > ret) 5151da177e4SLinus Torvalds buflen = ret; 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds if (copy_to_user(buffer, tmpbuf, buflen) != 0) 5181da177e4SLinus Torvalds ret = -EFAULT; 5191da177e4SLinus Torvalds } 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds kfree(tmpbuf); 5221da177e4SLinus Torvalds error2: 523664cceb0SDavid Howells key_ref_put(key_ref); 5241da177e4SLinus Torvalds error: 5251da177e4SLinus Torvalds return ret; 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds } /* end keyctl_describe_key() */ 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds /*****************************************************************************/ 5301da177e4SLinus Torvalds /* 5311da177e4SLinus Torvalds * search the specified keyring for a matching key 5321da177e4SLinus Torvalds * - the start keyring must be searchable 5331da177e4SLinus Torvalds * - nested keyrings may also be searched if they are searchable 5341da177e4SLinus Torvalds * - only keys with search permission may be found 5351da177e4SLinus Torvalds * - if a key is found, it will be attached to the destination keyring if 5361da177e4SLinus Torvalds * there's one specified 5371da177e4SLinus Torvalds * - implements keyctl(KEYCTL_SEARCH) 5381da177e4SLinus Torvalds */ 5391da177e4SLinus Torvalds long keyctl_keyring_search(key_serial_t ringid, 5401da177e4SLinus Torvalds const char __user *_type, 5411da177e4SLinus Torvalds const char __user *_description, 5421da177e4SLinus Torvalds key_serial_t destringid) 5431da177e4SLinus Torvalds { 5441da177e4SLinus Torvalds struct key_type *ktype; 545664cceb0SDavid Howells key_ref_t keyring_ref, key_ref, dest_ref; 5461da177e4SLinus Torvalds char type[32], *description; 5470cb409d9SDavi Arnaut long ret; 5481da177e4SLinus Torvalds 5491da177e4SLinus Torvalds /* pull the type and description into kernel space */ 5500cb409d9SDavi Arnaut ret = key_get_type_from_user(type, _type, sizeof(type)); 5511da177e4SLinus Torvalds if (ret < 0) 5521da177e4SLinus Torvalds goto error; 5531da177e4SLinus Torvalds 5540cb409d9SDavi Arnaut description = strndup_user(_description, PAGE_SIZE); 5550cb409d9SDavi Arnaut if (IS_ERR(description)) { 5560cb409d9SDavi Arnaut ret = PTR_ERR(description); 5571da177e4SLinus Torvalds goto error; 5580cb409d9SDavi Arnaut } 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds /* get the keyring at which to begin the search */ 5618bbf4976SDavid Howells keyring_ref = lookup_user_key(ringid, 0, 0, KEY_SEARCH); 562664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 563664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 5641da177e4SLinus Torvalds goto error2; 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds /* get the destination keyring if specified */ 568664cceb0SDavid Howells dest_ref = NULL; 5691da177e4SLinus Torvalds if (destringid) { 5708bbf4976SDavid Howells dest_ref = lookup_user_key(destringid, 1, 0, KEY_WRITE); 571664cceb0SDavid Howells if (IS_ERR(dest_ref)) { 572664cceb0SDavid Howells ret = PTR_ERR(dest_ref); 5731da177e4SLinus Torvalds goto error3; 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds } 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds /* find the key type */ 5781da177e4SLinus Torvalds ktype = key_type_lookup(type); 5791da177e4SLinus Torvalds if (IS_ERR(ktype)) { 5801da177e4SLinus Torvalds ret = PTR_ERR(ktype); 5811da177e4SLinus Torvalds goto error4; 5821da177e4SLinus Torvalds } 5831da177e4SLinus Torvalds 5841da177e4SLinus Torvalds /* do the search */ 585664cceb0SDavid Howells key_ref = keyring_search(keyring_ref, ktype, description); 586664cceb0SDavid Howells if (IS_ERR(key_ref)) { 587664cceb0SDavid Howells ret = PTR_ERR(key_ref); 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds /* treat lack or presence of a negative key the same */ 5901da177e4SLinus Torvalds if (ret == -EAGAIN) 5911da177e4SLinus Torvalds ret = -ENOKEY; 5921da177e4SLinus Torvalds goto error5; 5931da177e4SLinus Torvalds } 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds /* link the resulting key to the destination keyring if we can */ 596664cceb0SDavid Howells if (dest_ref) { 59729db9190SDavid Howells ret = key_permission(key_ref, KEY_LINK); 59829db9190SDavid Howells if (ret < 0) 5991da177e4SLinus Torvalds goto error6; 6001da177e4SLinus Torvalds 601664cceb0SDavid Howells ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref)); 6021da177e4SLinus Torvalds if (ret < 0) 6031da177e4SLinus Torvalds goto error6; 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds 606664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds error6: 609664cceb0SDavid Howells key_ref_put(key_ref); 6101da177e4SLinus Torvalds error5: 6111da177e4SLinus Torvalds key_type_put(ktype); 6121da177e4SLinus Torvalds error4: 613664cceb0SDavid Howells key_ref_put(dest_ref); 6141da177e4SLinus Torvalds error3: 615664cceb0SDavid Howells key_ref_put(keyring_ref); 6161da177e4SLinus Torvalds error2: 6171da177e4SLinus Torvalds kfree(description); 6181da177e4SLinus Torvalds error: 6191da177e4SLinus Torvalds return ret; 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds } /* end keyctl_keyring_search() */ 6221da177e4SLinus Torvalds 6231da177e4SLinus Torvalds /*****************************************************************************/ 6241da177e4SLinus Torvalds /* 6251da177e4SLinus Torvalds * read a user key's payload 6261da177e4SLinus Torvalds * - the keyring must be readable or the key must be searchable from the 6271da177e4SLinus Torvalds * process's keyrings 6281da177e4SLinus Torvalds * - if there's a buffer, we place up to buflen bytes of data into it 6291da177e4SLinus Torvalds * - unless there's an error, we return the amount of data in the key, 6301da177e4SLinus Torvalds * irrespective of how much we may have copied 6311da177e4SLinus Torvalds * - implements keyctl(KEYCTL_READ) 6321da177e4SLinus Torvalds */ 6331da177e4SLinus Torvalds long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen) 6341da177e4SLinus Torvalds { 635664cceb0SDavid Howells struct key *key; 636664cceb0SDavid Howells key_ref_t key_ref; 6371da177e4SLinus Torvalds long ret; 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds /* find the key first */ 6408bbf4976SDavid Howells key_ref = lookup_user_key(keyid, 0, 0, 0); 641664cceb0SDavid Howells if (IS_ERR(key_ref)) { 642664cceb0SDavid Howells ret = -ENOKEY; 643664cceb0SDavid Howells goto error; 644664cceb0SDavid Howells } 645664cceb0SDavid Howells 646664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 647664cceb0SDavid Howells 6481da177e4SLinus Torvalds /* see if we can read it directly */ 64929db9190SDavid Howells ret = key_permission(key_ref, KEY_READ); 65029db9190SDavid Howells if (ret == 0) 6511da177e4SLinus Torvalds goto can_read_key; 65229db9190SDavid Howells if (ret != -EACCES) 65329db9190SDavid Howells goto error; 6541da177e4SLinus Torvalds 655664cceb0SDavid Howells /* we can't; see if it's searchable from this process's keyrings 6563e30148cSDavid Howells * - we automatically take account of the fact that it may be 6573e30148cSDavid Howells * dangling off an instantiation key 6583e30148cSDavid Howells */ 659664cceb0SDavid Howells if (!is_key_possessed(key_ref)) { 6601260f801SDavid Howells ret = -EACCES; 6611da177e4SLinus Torvalds goto error2; 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds /* the key is probably readable - now try to read it */ 6651da177e4SLinus Torvalds can_read_key: 6661da177e4SLinus Torvalds ret = key_validate(key); 6671da177e4SLinus Torvalds if (ret == 0) { 6681da177e4SLinus Torvalds ret = -EOPNOTSUPP; 6691da177e4SLinus Torvalds if (key->type->read) { 6701da177e4SLinus Torvalds /* read the data with the semaphore held (since we 6711da177e4SLinus Torvalds * might sleep) */ 6721da177e4SLinus Torvalds down_read(&key->sem); 6731da177e4SLinus Torvalds ret = key->type->read(key, buffer, buflen); 6741da177e4SLinus Torvalds up_read(&key->sem); 6751da177e4SLinus Torvalds } 6761da177e4SLinus Torvalds } 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds error2: 6791da177e4SLinus Torvalds key_put(key); 6801da177e4SLinus Torvalds error: 6811da177e4SLinus Torvalds return ret; 6821da177e4SLinus Torvalds 6831da177e4SLinus Torvalds } /* end keyctl_read_key() */ 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds /*****************************************************************************/ 6861da177e4SLinus Torvalds /* 6871da177e4SLinus Torvalds * change the ownership of a key 6881da177e4SLinus Torvalds * - the keyring owned by the changer 6891da177e4SLinus Torvalds * - if the uid or gid is -1, then that parameter is not changed 6901da177e4SLinus Torvalds * - implements keyctl(KEYCTL_CHOWN) 6911da177e4SLinus Torvalds */ 6921da177e4SLinus Torvalds long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid) 6931da177e4SLinus Torvalds { 6945801649dSFredrik Tolf struct key_user *newowner, *zapowner = NULL; 6951da177e4SLinus Torvalds struct key *key; 696664cceb0SDavid Howells key_ref_t key_ref; 6971da177e4SLinus Torvalds long ret; 6981da177e4SLinus Torvalds 6991da177e4SLinus Torvalds ret = 0; 7001da177e4SLinus Torvalds if (uid == (uid_t) -1 && gid == (gid_t) -1) 7011da177e4SLinus Torvalds goto error; 7021da177e4SLinus Torvalds 7038bbf4976SDavid Howells key_ref = lookup_user_key(id, 1, 1, KEY_SETATTR); 704664cceb0SDavid Howells if (IS_ERR(key_ref)) { 705664cceb0SDavid Howells ret = PTR_ERR(key_ref); 7061da177e4SLinus Torvalds goto error; 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds 709664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 710664cceb0SDavid Howells 7111da177e4SLinus Torvalds /* make the changes with the locks held to prevent chown/chown races */ 7121da177e4SLinus Torvalds ret = -EACCES; 7131da177e4SLinus Torvalds down_write(&key->sem); 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) { 7161da177e4SLinus Torvalds /* only the sysadmin can chown a key to some other UID */ 7171da177e4SLinus Torvalds if (uid != (uid_t) -1 && key->uid != uid) 7185801649dSFredrik Tolf goto error_put; 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds /* only the sysadmin can set the key's GID to a group other 7211da177e4SLinus Torvalds * than one of those that the current process subscribes to */ 7221da177e4SLinus Torvalds if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid)) 7235801649dSFredrik Tolf goto error_put; 7241da177e4SLinus Torvalds } 7251da177e4SLinus Torvalds 7265801649dSFredrik Tolf /* change the UID */ 7271da177e4SLinus Torvalds if (uid != (uid_t) -1 && uid != key->uid) { 7285801649dSFredrik Tolf ret = -ENOMEM; 729*1d1e9756SSerge E. Hallyn newowner = key_user_lookup(uid, current_user_ns()); 7305801649dSFredrik Tolf if (!newowner) 7315801649dSFredrik Tolf goto error_put; 7325801649dSFredrik Tolf 7335801649dSFredrik Tolf /* transfer the quota burden to the new user */ 7345801649dSFredrik Tolf if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { 7350b77f5bfSDavid Howells unsigned maxkeys = (uid == 0) ? 7360b77f5bfSDavid Howells key_quota_root_maxkeys : key_quota_maxkeys; 7370b77f5bfSDavid Howells unsigned maxbytes = (uid == 0) ? 7380b77f5bfSDavid Howells key_quota_root_maxbytes : key_quota_maxbytes; 7390b77f5bfSDavid Howells 7405801649dSFredrik Tolf spin_lock(&newowner->lock); 7410b77f5bfSDavid Howells if (newowner->qnkeys + 1 >= maxkeys || 7420b77f5bfSDavid Howells newowner->qnbytes + key->quotalen >= maxbytes || 7430b77f5bfSDavid Howells newowner->qnbytes + key->quotalen < 7440b77f5bfSDavid Howells newowner->qnbytes) 7455801649dSFredrik Tolf goto quota_overrun; 7465801649dSFredrik Tolf 7475801649dSFredrik Tolf newowner->qnkeys++; 7485801649dSFredrik Tolf newowner->qnbytes += key->quotalen; 7495801649dSFredrik Tolf spin_unlock(&newowner->lock); 7505801649dSFredrik Tolf 7515801649dSFredrik Tolf spin_lock(&key->user->lock); 7525801649dSFredrik Tolf key->user->qnkeys--; 7535801649dSFredrik Tolf key->user->qnbytes -= key->quotalen; 7545801649dSFredrik Tolf spin_unlock(&key->user->lock); 7555801649dSFredrik Tolf } 7565801649dSFredrik Tolf 7575801649dSFredrik Tolf atomic_dec(&key->user->nkeys); 7585801649dSFredrik Tolf atomic_inc(&newowner->nkeys); 7595801649dSFredrik Tolf 7605801649dSFredrik Tolf if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) { 7615801649dSFredrik Tolf atomic_dec(&key->user->nikeys); 7625801649dSFredrik Tolf atomic_inc(&newowner->nikeys); 7635801649dSFredrik Tolf } 7645801649dSFredrik Tolf 7655801649dSFredrik Tolf zapowner = key->user; 7665801649dSFredrik Tolf key->user = newowner; 7675801649dSFredrik Tolf key->uid = uid; 7681da177e4SLinus Torvalds } 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds /* change the GID */ 7711da177e4SLinus Torvalds if (gid != (gid_t) -1) 7721da177e4SLinus Torvalds key->gid = gid; 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds ret = 0; 7751da177e4SLinus Torvalds 7765801649dSFredrik Tolf error_put: 7771da177e4SLinus Torvalds up_write(&key->sem); 7781da177e4SLinus Torvalds key_put(key); 7795801649dSFredrik Tolf if (zapowner) 7805801649dSFredrik Tolf key_user_put(zapowner); 7811da177e4SLinus Torvalds error: 7821da177e4SLinus Torvalds return ret; 7831da177e4SLinus Torvalds 7845801649dSFredrik Tolf quota_overrun: 7855801649dSFredrik Tolf spin_unlock(&newowner->lock); 7865801649dSFredrik Tolf zapowner = newowner; 7875801649dSFredrik Tolf ret = -EDQUOT; 7885801649dSFredrik Tolf goto error_put; 7895801649dSFredrik Tolf 7901da177e4SLinus Torvalds } /* end keyctl_chown_key() */ 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds /*****************************************************************************/ 7931da177e4SLinus Torvalds /* 7941da177e4SLinus Torvalds * change the permission mask on a key 7951da177e4SLinus Torvalds * - the keyring owned by the changer 7961da177e4SLinus Torvalds * - implements keyctl(KEYCTL_SETPERM) 7971da177e4SLinus Torvalds */ 7981da177e4SLinus Torvalds long keyctl_setperm_key(key_serial_t id, key_perm_t perm) 7991da177e4SLinus Torvalds { 8001da177e4SLinus Torvalds struct key *key; 801664cceb0SDavid Howells key_ref_t key_ref; 8021da177e4SLinus Torvalds long ret; 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds ret = -EINVAL; 805664cceb0SDavid Howells if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL)) 8061da177e4SLinus Torvalds goto error; 8071da177e4SLinus Torvalds 8088bbf4976SDavid Howells key_ref = lookup_user_key(id, 1, 1, KEY_SETATTR); 809664cceb0SDavid Howells if (IS_ERR(key_ref)) { 810664cceb0SDavid Howells ret = PTR_ERR(key_ref); 8111da177e4SLinus Torvalds goto error; 8121da177e4SLinus Torvalds } 8131da177e4SLinus Torvalds 814664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 815664cceb0SDavid Howells 81676d8aeabSDavid Howells /* make the changes with the locks held to prevent chown/chmod races */ 8171da177e4SLinus Torvalds ret = -EACCES; 8181da177e4SLinus Torvalds down_write(&key->sem); 8191da177e4SLinus Torvalds 82076d8aeabSDavid Howells /* if we're not the sysadmin, we can only change a key that we own */ 82147d804bfSDavid Howells if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) { 8221da177e4SLinus Torvalds key->perm = perm; 8231da177e4SLinus Torvalds ret = 0; 82476d8aeabSDavid Howells } 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds up_write(&key->sem); 8271da177e4SLinus Torvalds key_put(key); 8281da177e4SLinus Torvalds error: 8291da177e4SLinus Torvalds return ret; 8301da177e4SLinus Torvalds 8311da177e4SLinus Torvalds } /* end keyctl_setperm_key() */ 8321da177e4SLinus Torvalds 8338bbf4976SDavid Howells /* 8348bbf4976SDavid Howells * get the destination keyring for instantiation 8358bbf4976SDavid Howells */ 8368bbf4976SDavid Howells static long get_instantiation_keyring(key_serial_t ringid, 8378bbf4976SDavid Howells struct request_key_auth *rka, 8388bbf4976SDavid Howells struct key **_dest_keyring) 8398bbf4976SDavid Howells { 8408bbf4976SDavid Howells key_ref_t dkref; 8418bbf4976SDavid Howells 8428bbf4976SDavid Howells *_dest_keyring = NULL; 843eca1bf5bSDavid Howells 844eca1bf5bSDavid Howells /* just return a NULL pointer if we weren't asked to make a link */ 845eca1bf5bSDavid Howells if (ringid == 0) 8468bbf4976SDavid Howells return 0; 8478bbf4976SDavid Howells 8488bbf4976SDavid Howells /* if a specific keyring is nominated by ID, then use that */ 8498bbf4976SDavid Howells if (ringid > 0) { 8508bbf4976SDavid Howells dkref = lookup_user_key(ringid, 1, 0, KEY_WRITE); 8518bbf4976SDavid Howells if (IS_ERR(dkref)) 8528bbf4976SDavid Howells return PTR_ERR(dkref); 8538bbf4976SDavid Howells *_dest_keyring = key_ref_to_ptr(dkref); 8548bbf4976SDavid Howells return 0; 8558bbf4976SDavid Howells } 8568bbf4976SDavid Howells 8578bbf4976SDavid Howells if (ringid == KEY_SPEC_REQKEY_AUTH_KEY) 8588bbf4976SDavid Howells return -EINVAL; 8598bbf4976SDavid Howells 8608bbf4976SDavid Howells /* otherwise specify the destination keyring recorded in the 8618bbf4976SDavid Howells * authorisation key (any KEY_SPEC_*_KEYRING) */ 8628bbf4976SDavid Howells if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) { 8638bbf4976SDavid Howells *_dest_keyring = rka->dest_keyring; 8648bbf4976SDavid Howells return 0; 8658bbf4976SDavid Howells } 8668bbf4976SDavid Howells 8678bbf4976SDavid Howells return -ENOKEY; 8688bbf4976SDavid Howells } 8698bbf4976SDavid Howells 870d84f4f99SDavid Howells /* 871d84f4f99SDavid Howells * change the request_key authorisation key on the current process 872d84f4f99SDavid Howells */ 873d84f4f99SDavid Howells static int keyctl_change_reqkey_auth(struct key *key) 874d84f4f99SDavid Howells { 875d84f4f99SDavid Howells struct cred *new; 876d84f4f99SDavid Howells 877d84f4f99SDavid Howells new = prepare_creds(); 878d84f4f99SDavid Howells if (!new) 879d84f4f99SDavid Howells return -ENOMEM; 880d84f4f99SDavid Howells 881d84f4f99SDavid Howells key_put(new->request_key_auth); 882d84f4f99SDavid Howells new->request_key_auth = key_get(key); 883d84f4f99SDavid Howells 884d84f4f99SDavid Howells return commit_creds(new); 885d84f4f99SDavid Howells } 886d84f4f99SDavid Howells 8871da177e4SLinus Torvalds /*****************************************************************************/ 8881da177e4SLinus Torvalds /* 8891da177e4SLinus Torvalds * instantiate the key with the specified payload, and, if one is given, link 8901da177e4SLinus Torvalds * the key into the keyring 8911da177e4SLinus Torvalds */ 8921da177e4SLinus Torvalds long keyctl_instantiate_key(key_serial_t id, 8931da177e4SLinus Torvalds const void __user *_payload, 8941da177e4SLinus Torvalds size_t plen, 8951da177e4SLinus Torvalds key_serial_t ringid) 8961da177e4SLinus Torvalds { 897d84f4f99SDavid Howells const struct cred *cred = current_cred(); 8983e30148cSDavid Howells struct request_key_auth *rka; 8998bbf4976SDavid Howells struct key *instkey, *dest_keyring; 9001da177e4SLinus Torvalds void *payload; 9011da177e4SLinus Torvalds long ret; 90238bbca6bSDavid Howells bool vm = false; 9031da177e4SLinus Torvalds 904d84f4f99SDavid Howells kenter("%d,,%zu,%d", id, plen, ringid); 905d84f4f99SDavid Howells 9061da177e4SLinus Torvalds ret = -EINVAL; 90738bbca6bSDavid Howells if (plen > 1024 * 1024 - 1) 9081da177e4SLinus Torvalds goto error; 9091da177e4SLinus Torvalds 910b5f545c8SDavid Howells /* the appropriate instantiation authorisation key must have been 911b5f545c8SDavid Howells * assumed before calling this */ 912b5f545c8SDavid Howells ret = -EPERM; 913d84f4f99SDavid Howells instkey = cred->request_key_auth; 914b5f545c8SDavid Howells if (!instkey) 915b5f545c8SDavid Howells goto error; 916b5f545c8SDavid Howells 917b5f545c8SDavid Howells rka = instkey->payload.data; 918b5f545c8SDavid Howells if (rka->target_key->serial != id) 919b5f545c8SDavid Howells goto error; 920b5f545c8SDavid Howells 9211da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 9221da177e4SLinus Torvalds payload = NULL; 9231da177e4SLinus Torvalds 9241da177e4SLinus Torvalds if (_payload) { 9251da177e4SLinus Torvalds ret = -ENOMEM; 9261da177e4SLinus Torvalds payload = kmalloc(plen, GFP_KERNEL); 92738bbca6bSDavid Howells if (!payload) { 92838bbca6bSDavid Howells if (plen <= PAGE_SIZE) 92938bbca6bSDavid Howells goto error; 93038bbca6bSDavid Howells vm = true; 93138bbca6bSDavid Howells payload = vmalloc(plen); 9321da177e4SLinus Torvalds if (!payload) 9331da177e4SLinus Torvalds goto error; 93438bbca6bSDavid Howells } 9351da177e4SLinus Torvalds 9361da177e4SLinus Torvalds ret = -EFAULT; 9371da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 9381da177e4SLinus Torvalds goto error2; 9391da177e4SLinus Torvalds } 9401da177e4SLinus Torvalds 9413e30148cSDavid Howells /* find the destination keyring amongst those belonging to the 9423e30148cSDavid Howells * requesting task */ 9438bbf4976SDavid Howells ret = get_instantiation_keyring(ringid, rka, &dest_keyring); 9448bbf4976SDavid Howells if (ret < 0) 945b5f545c8SDavid Howells goto error2; 9461da177e4SLinus Torvalds 9471da177e4SLinus Torvalds /* instantiate the key and link it into a keyring */ 9483e30148cSDavid Howells ret = key_instantiate_and_link(rka->target_key, payload, plen, 9498bbf4976SDavid Howells dest_keyring, instkey); 9501da177e4SLinus Torvalds 9518bbf4976SDavid Howells key_put(dest_keyring); 952b5f545c8SDavid Howells 953b5f545c8SDavid Howells /* discard the assumed authority if it's just been disabled by 954b5f545c8SDavid Howells * instantiation of the key */ 955d84f4f99SDavid Howells if (ret == 0) 956d84f4f99SDavid Howells keyctl_change_reqkey_auth(NULL); 957b5f545c8SDavid Howells 9581da177e4SLinus Torvalds error2: 95938bbca6bSDavid Howells if (!vm) 9601da177e4SLinus Torvalds kfree(payload); 96138bbca6bSDavid Howells else 96238bbca6bSDavid Howells vfree(payload); 9631da177e4SLinus Torvalds error: 9641da177e4SLinus Torvalds return ret; 9651da177e4SLinus Torvalds 9661da177e4SLinus Torvalds } /* end keyctl_instantiate_key() */ 9671da177e4SLinus Torvalds 9681da177e4SLinus Torvalds /*****************************************************************************/ 9691da177e4SLinus Torvalds /* 9701da177e4SLinus Torvalds * negatively instantiate the key with the given timeout (in seconds), and, if 9711da177e4SLinus Torvalds * one is given, link the key into the keyring 9721da177e4SLinus Torvalds */ 9731da177e4SLinus Torvalds long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid) 9741da177e4SLinus Torvalds { 975d84f4f99SDavid Howells const struct cred *cred = current_cred(); 9763e30148cSDavid Howells struct request_key_auth *rka; 9778bbf4976SDavid Howells struct key *instkey, *dest_keyring; 9781da177e4SLinus Torvalds long ret; 9791da177e4SLinus Torvalds 980d84f4f99SDavid Howells kenter("%d,%u,%d", id, timeout, ringid); 981d84f4f99SDavid Howells 982b5f545c8SDavid Howells /* the appropriate instantiation authorisation key must have been 983b5f545c8SDavid Howells * assumed before calling this */ 984b5f545c8SDavid Howells ret = -EPERM; 985d84f4f99SDavid Howells instkey = cred->request_key_auth; 986b5f545c8SDavid Howells if (!instkey) 9871da177e4SLinus Torvalds goto error; 9881da177e4SLinus Torvalds 9893e30148cSDavid Howells rka = instkey->payload.data; 990b5f545c8SDavid Howells if (rka->target_key->serial != id) 991b5f545c8SDavid Howells goto error; 9923e30148cSDavid Howells 9931da177e4SLinus Torvalds /* find the destination keyring if present (which must also be 9941da177e4SLinus Torvalds * writable) */ 9958bbf4976SDavid Howells ret = get_instantiation_keyring(ringid, rka, &dest_keyring); 9968bbf4976SDavid Howells if (ret < 0) 997b5f545c8SDavid Howells goto error; 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvalds /* instantiate the key and link it into a keyring */ 1000664cceb0SDavid Howells ret = key_negate_and_link(rka->target_key, timeout, 10018bbf4976SDavid Howells dest_keyring, instkey); 10021da177e4SLinus Torvalds 10038bbf4976SDavid Howells key_put(dest_keyring); 1004b5f545c8SDavid Howells 1005b5f545c8SDavid Howells /* discard the assumed authority if it's just been disabled by 1006b5f545c8SDavid Howells * instantiation of the key */ 1007d84f4f99SDavid Howells if (ret == 0) 1008d84f4f99SDavid Howells keyctl_change_reqkey_auth(NULL); 1009b5f545c8SDavid Howells 10101da177e4SLinus Torvalds error: 10111da177e4SLinus Torvalds return ret; 10121da177e4SLinus Torvalds 10131da177e4SLinus Torvalds } /* end keyctl_negate_key() */ 10141da177e4SLinus Torvalds 10151da177e4SLinus Torvalds /*****************************************************************************/ 10161da177e4SLinus Torvalds /* 10173e30148cSDavid Howells * set the default keyring in which request_key() will cache keys 10183e30148cSDavid Howells * - return the old setting 10193e30148cSDavid Howells */ 10203e30148cSDavid Howells long keyctl_set_reqkey_keyring(int reqkey_defl) 10213e30148cSDavid Howells { 1022d84f4f99SDavid Howells struct cred *new; 1023d84f4f99SDavid Howells int ret, old_setting; 1024d84f4f99SDavid Howells 1025d84f4f99SDavid Howells old_setting = current_cred_xxx(jit_keyring); 1026d84f4f99SDavid Howells 1027d84f4f99SDavid Howells if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE) 1028d84f4f99SDavid Howells return old_setting; 1029d84f4f99SDavid Howells 1030d84f4f99SDavid Howells new = prepare_creds(); 1031d84f4f99SDavid Howells if (!new) 1032d84f4f99SDavid Howells return -ENOMEM; 10333e30148cSDavid Howells 10343e30148cSDavid Howells switch (reqkey_defl) { 10353e30148cSDavid Howells case KEY_REQKEY_DEFL_THREAD_KEYRING: 1036d84f4f99SDavid Howells ret = install_thread_keyring_to_cred(new); 10373e30148cSDavid Howells if (ret < 0) 1038d84f4f99SDavid Howells goto error; 10393e30148cSDavid Howells goto set; 10403e30148cSDavid Howells 10413e30148cSDavid Howells case KEY_REQKEY_DEFL_PROCESS_KEYRING: 1042d84f4f99SDavid Howells ret = install_process_keyring_to_cred(new); 1043d84f4f99SDavid Howells if (ret < 0) { 1044d84f4f99SDavid Howells if (ret != -EEXIST) 1045d84f4f99SDavid Howells goto error; 1046d84f4f99SDavid Howells ret = 0; 1047d84f4f99SDavid Howells } 1048d84f4f99SDavid Howells goto set; 10493e30148cSDavid Howells 10503e30148cSDavid Howells case KEY_REQKEY_DEFL_DEFAULT: 10513e30148cSDavid Howells case KEY_REQKEY_DEFL_SESSION_KEYRING: 10523e30148cSDavid Howells case KEY_REQKEY_DEFL_USER_KEYRING: 10533e30148cSDavid Howells case KEY_REQKEY_DEFL_USER_SESSION_KEYRING: 1054d84f4f99SDavid Howells case KEY_REQKEY_DEFL_REQUESTOR_KEYRING: 1055d84f4f99SDavid Howells goto set; 10563e30148cSDavid Howells 10573e30148cSDavid Howells case KEY_REQKEY_DEFL_NO_CHANGE: 10583e30148cSDavid Howells case KEY_REQKEY_DEFL_GROUP_KEYRING: 10593e30148cSDavid Howells default: 1060d84f4f99SDavid Howells ret = -EINVAL; 1061d84f4f99SDavid Howells goto error; 10623e30148cSDavid Howells } 10633e30148cSDavid Howells 1064d84f4f99SDavid Howells set: 1065d84f4f99SDavid Howells new->jit_keyring = reqkey_defl; 1066d84f4f99SDavid Howells commit_creds(new); 1067d84f4f99SDavid Howells return old_setting; 1068d84f4f99SDavid Howells error: 1069d84f4f99SDavid Howells abort_creds(new); 1070d84f4f99SDavid Howells return -EINVAL; 1071d84f4f99SDavid Howells 10723e30148cSDavid Howells } /* end keyctl_set_reqkey_keyring() */ 10733e30148cSDavid Howells 10743e30148cSDavid Howells /*****************************************************************************/ 10753e30148cSDavid Howells /* 1076017679c4SDavid Howells * set or clear the timeout for a key 1077017679c4SDavid Howells */ 1078017679c4SDavid Howells long keyctl_set_timeout(key_serial_t id, unsigned timeout) 1079017679c4SDavid Howells { 1080017679c4SDavid Howells struct timespec now; 1081017679c4SDavid Howells struct key *key; 1082017679c4SDavid Howells key_ref_t key_ref; 1083017679c4SDavid Howells time_t expiry; 1084017679c4SDavid Howells long ret; 1085017679c4SDavid Howells 10868bbf4976SDavid Howells key_ref = lookup_user_key(id, 1, 1, KEY_SETATTR); 1087017679c4SDavid Howells if (IS_ERR(key_ref)) { 1088017679c4SDavid Howells ret = PTR_ERR(key_ref); 1089017679c4SDavid Howells goto error; 1090017679c4SDavid Howells } 1091017679c4SDavid Howells 1092017679c4SDavid Howells key = key_ref_to_ptr(key_ref); 1093017679c4SDavid Howells 1094017679c4SDavid Howells /* make the changes with the locks held to prevent races */ 1095017679c4SDavid Howells down_write(&key->sem); 1096017679c4SDavid Howells 1097017679c4SDavid Howells expiry = 0; 1098017679c4SDavid Howells if (timeout > 0) { 1099017679c4SDavid Howells now = current_kernel_time(); 1100017679c4SDavid Howells expiry = now.tv_sec + timeout; 1101017679c4SDavid Howells } 1102017679c4SDavid Howells 1103017679c4SDavid Howells key->expiry = expiry; 1104017679c4SDavid Howells 1105017679c4SDavid Howells up_write(&key->sem); 1106017679c4SDavid Howells key_put(key); 1107017679c4SDavid Howells 1108017679c4SDavid Howells ret = 0; 1109017679c4SDavid Howells error: 1110017679c4SDavid Howells return ret; 1111017679c4SDavid Howells 1112017679c4SDavid Howells } /* end keyctl_set_timeout() */ 1113017679c4SDavid Howells 1114017679c4SDavid Howells /*****************************************************************************/ 1115017679c4SDavid Howells /* 1116b5f545c8SDavid Howells * assume the authority to instantiate the specified key 1117b5f545c8SDavid Howells */ 1118b5f545c8SDavid Howells long keyctl_assume_authority(key_serial_t id) 1119b5f545c8SDavid Howells { 1120b5f545c8SDavid Howells struct key *authkey; 1121b5f545c8SDavid Howells long ret; 1122b5f545c8SDavid Howells 1123b5f545c8SDavid Howells /* special key IDs aren't permitted */ 1124b5f545c8SDavid Howells ret = -EINVAL; 1125b5f545c8SDavid Howells if (id < 0) 1126b5f545c8SDavid Howells goto error; 1127b5f545c8SDavid Howells 1128b5f545c8SDavid Howells /* we divest ourselves of authority if given an ID of 0 */ 1129b5f545c8SDavid Howells if (id == 0) { 1130d84f4f99SDavid Howells ret = keyctl_change_reqkey_auth(NULL); 1131b5f545c8SDavid Howells goto error; 1132b5f545c8SDavid Howells } 1133b5f545c8SDavid Howells 1134b5f545c8SDavid Howells /* attempt to assume the authority temporarily granted to us whilst we 1135b5f545c8SDavid Howells * instantiate the specified key 1136b5f545c8SDavid Howells * - the authorisation key must be in the current task's keyrings 1137b5f545c8SDavid Howells * somewhere 1138b5f545c8SDavid Howells */ 1139b5f545c8SDavid Howells authkey = key_get_instantiation_authkey(id); 1140b5f545c8SDavid Howells if (IS_ERR(authkey)) { 1141b5f545c8SDavid Howells ret = PTR_ERR(authkey); 1142b5f545c8SDavid Howells goto error; 1143b5f545c8SDavid Howells } 1144b5f545c8SDavid Howells 1145d84f4f99SDavid Howells ret = keyctl_change_reqkey_auth(authkey); 1146d84f4f99SDavid Howells if (ret < 0) 1147d84f4f99SDavid Howells goto error; 1148d84f4f99SDavid Howells key_put(authkey); 1149b5f545c8SDavid Howells 1150d84f4f99SDavid Howells ret = authkey->serial; 1151b5f545c8SDavid Howells error: 1152b5f545c8SDavid Howells return ret; 1153b5f545c8SDavid Howells 1154b5f545c8SDavid Howells } /* end keyctl_assume_authority() */ 1155b5f545c8SDavid Howells 115670a5bb72SDavid Howells /* 115770a5bb72SDavid Howells * get the security label of a key 115870a5bb72SDavid Howells * - the key must grant us view permission 115970a5bb72SDavid Howells * - if there's a buffer, we place up to buflen bytes of data into it 116070a5bb72SDavid Howells * - unless there's an error, we return the amount of information available, 116170a5bb72SDavid Howells * irrespective of how much we may have copied (including the terminal NUL) 116270a5bb72SDavid Howells * - implements keyctl(KEYCTL_GET_SECURITY) 116370a5bb72SDavid Howells */ 116470a5bb72SDavid Howells long keyctl_get_security(key_serial_t keyid, 116570a5bb72SDavid Howells char __user *buffer, 116670a5bb72SDavid Howells size_t buflen) 116770a5bb72SDavid Howells { 116870a5bb72SDavid Howells struct key *key, *instkey; 116970a5bb72SDavid Howells key_ref_t key_ref; 117070a5bb72SDavid Howells char *context; 117170a5bb72SDavid Howells long ret; 117270a5bb72SDavid Howells 11738bbf4976SDavid Howells key_ref = lookup_user_key(keyid, 0, 1, KEY_VIEW); 117470a5bb72SDavid Howells if (IS_ERR(key_ref)) { 117570a5bb72SDavid Howells if (PTR_ERR(key_ref) != -EACCES) 117670a5bb72SDavid Howells return PTR_ERR(key_ref); 117770a5bb72SDavid Howells 117870a5bb72SDavid Howells /* viewing a key under construction is also permitted if we 117970a5bb72SDavid Howells * have the authorisation token handy */ 118070a5bb72SDavid Howells instkey = key_get_instantiation_authkey(keyid); 118170a5bb72SDavid Howells if (IS_ERR(instkey)) 118270a5bb72SDavid Howells return PTR_ERR(key_ref); 118370a5bb72SDavid Howells key_put(instkey); 118470a5bb72SDavid Howells 11858bbf4976SDavid Howells key_ref = lookup_user_key(keyid, 0, 1, 0); 118670a5bb72SDavid Howells if (IS_ERR(key_ref)) 118770a5bb72SDavid Howells return PTR_ERR(key_ref); 118870a5bb72SDavid Howells } 118970a5bb72SDavid Howells 119070a5bb72SDavid Howells key = key_ref_to_ptr(key_ref); 119170a5bb72SDavid Howells ret = security_key_getsecurity(key, &context); 119270a5bb72SDavid Howells if (ret == 0) { 119370a5bb72SDavid Howells /* if no information was returned, give userspace an empty 119470a5bb72SDavid Howells * string */ 119570a5bb72SDavid Howells ret = 1; 119670a5bb72SDavid Howells if (buffer && buflen > 0 && 119770a5bb72SDavid Howells copy_to_user(buffer, "", 1) != 0) 119870a5bb72SDavid Howells ret = -EFAULT; 119970a5bb72SDavid Howells } else if (ret > 0) { 120070a5bb72SDavid Howells /* return as much data as there's room for */ 120170a5bb72SDavid Howells if (buffer && buflen > 0) { 120270a5bb72SDavid Howells if (buflen > ret) 120370a5bb72SDavid Howells buflen = ret; 120470a5bb72SDavid Howells 120570a5bb72SDavid Howells if (copy_to_user(buffer, context, buflen) != 0) 120670a5bb72SDavid Howells ret = -EFAULT; 120770a5bb72SDavid Howells } 120870a5bb72SDavid Howells 120970a5bb72SDavid Howells kfree(context); 121070a5bb72SDavid Howells } 121170a5bb72SDavid Howells 121270a5bb72SDavid Howells key_ref_put(key_ref); 121370a5bb72SDavid Howells return ret; 121470a5bb72SDavid Howells } 121570a5bb72SDavid Howells 1216b5f545c8SDavid Howells /*****************************************************************************/ 1217b5f545c8SDavid Howells /* 12181da177e4SLinus Torvalds * the key control system call 12191da177e4SLinus Torvalds */ 1220938bb9f5SHeiko Carstens SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3, 1221938bb9f5SHeiko Carstens unsigned long, arg4, unsigned long, arg5) 12221da177e4SLinus Torvalds { 12231da177e4SLinus Torvalds switch (option) { 12241da177e4SLinus Torvalds case KEYCTL_GET_KEYRING_ID: 12251da177e4SLinus Torvalds return keyctl_get_keyring_ID((key_serial_t) arg2, 12261da177e4SLinus Torvalds (int) arg3); 12271da177e4SLinus Torvalds 12281da177e4SLinus Torvalds case KEYCTL_JOIN_SESSION_KEYRING: 12291da177e4SLinus Torvalds return keyctl_join_session_keyring((const char __user *) arg2); 12301da177e4SLinus Torvalds 12311da177e4SLinus Torvalds case KEYCTL_UPDATE: 12321da177e4SLinus Torvalds return keyctl_update_key((key_serial_t) arg2, 12331da177e4SLinus Torvalds (const void __user *) arg3, 12341da177e4SLinus Torvalds (size_t) arg4); 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds case KEYCTL_REVOKE: 12371da177e4SLinus Torvalds return keyctl_revoke_key((key_serial_t) arg2); 12381da177e4SLinus Torvalds 12391da177e4SLinus Torvalds case KEYCTL_DESCRIBE: 12401da177e4SLinus Torvalds return keyctl_describe_key((key_serial_t) arg2, 12411da177e4SLinus Torvalds (char __user *) arg3, 12421da177e4SLinus Torvalds (unsigned) arg4); 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds case KEYCTL_CLEAR: 12451da177e4SLinus Torvalds return keyctl_keyring_clear((key_serial_t) arg2); 12461da177e4SLinus Torvalds 12471da177e4SLinus Torvalds case KEYCTL_LINK: 12481da177e4SLinus Torvalds return keyctl_keyring_link((key_serial_t) arg2, 12491da177e4SLinus Torvalds (key_serial_t) arg3); 12501da177e4SLinus Torvalds 12511da177e4SLinus Torvalds case KEYCTL_UNLINK: 12521da177e4SLinus Torvalds return keyctl_keyring_unlink((key_serial_t) arg2, 12531da177e4SLinus Torvalds (key_serial_t) arg3); 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds case KEYCTL_SEARCH: 12561da177e4SLinus Torvalds return keyctl_keyring_search((key_serial_t) arg2, 12571da177e4SLinus Torvalds (const char __user *) arg3, 12581da177e4SLinus Torvalds (const char __user *) arg4, 12591da177e4SLinus Torvalds (key_serial_t) arg5); 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds case KEYCTL_READ: 12621da177e4SLinus Torvalds return keyctl_read_key((key_serial_t) arg2, 12631da177e4SLinus Torvalds (char __user *) arg3, 12641da177e4SLinus Torvalds (size_t) arg4); 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds case KEYCTL_CHOWN: 12671da177e4SLinus Torvalds return keyctl_chown_key((key_serial_t) arg2, 12681da177e4SLinus Torvalds (uid_t) arg3, 12691da177e4SLinus Torvalds (gid_t) arg4); 12701da177e4SLinus Torvalds 12711da177e4SLinus Torvalds case KEYCTL_SETPERM: 12721da177e4SLinus Torvalds return keyctl_setperm_key((key_serial_t) arg2, 12731da177e4SLinus Torvalds (key_perm_t) arg3); 12741da177e4SLinus Torvalds 12751da177e4SLinus Torvalds case KEYCTL_INSTANTIATE: 12761da177e4SLinus Torvalds return keyctl_instantiate_key((key_serial_t) arg2, 12771da177e4SLinus Torvalds (const void __user *) arg3, 12781da177e4SLinus Torvalds (size_t) arg4, 12791da177e4SLinus Torvalds (key_serial_t) arg5); 12801da177e4SLinus Torvalds 12811da177e4SLinus Torvalds case KEYCTL_NEGATE: 12821da177e4SLinus Torvalds return keyctl_negate_key((key_serial_t) arg2, 12831da177e4SLinus Torvalds (unsigned) arg3, 12841da177e4SLinus Torvalds (key_serial_t) arg4); 12851da177e4SLinus Torvalds 12863e30148cSDavid Howells case KEYCTL_SET_REQKEY_KEYRING: 12873e30148cSDavid Howells return keyctl_set_reqkey_keyring(arg2); 12883e30148cSDavid Howells 1289017679c4SDavid Howells case KEYCTL_SET_TIMEOUT: 1290017679c4SDavid Howells return keyctl_set_timeout((key_serial_t) arg2, 1291017679c4SDavid Howells (unsigned) arg3); 1292017679c4SDavid Howells 1293b5f545c8SDavid Howells case KEYCTL_ASSUME_AUTHORITY: 1294b5f545c8SDavid Howells return keyctl_assume_authority((key_serial_t) arg2); 1295b5f545c8SDavid Howells 129670a5bb72SDavid Howells case KEYCTL_GET_SECURITY: 129770a5bb72SDavid Howells return keyctl_get_security((key_serial_t) arg2, 129890bd49abSJames Morris (char __user *) arg3, 129970a5bb72SDavid Howells (size_t) arg4); 130070a5bb72SDavid Howells 13011da177e4SLinus Torvalds default: 13021da177e4SLinus Torvalds return -EOPNOTSUPP; 13031da177e4SLinus Torvalds } 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds } /* end sys_keyctl() */ 1306