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> 22*38bbca6bSDavid Howells #include <linux/vmalloc.h> 231da177e4SLinus Torvalds #include <asm/uaccess.h> 241da177e4SLinus Torvalds #include "internal.h" 251da177e4SLinus Torvalds 260cb409d9SDavi Arnaut static int key_get_type_from_user(char *type, 270cb409d9SDavi Arnaut const char __user *_type, 280cb409d9SDavi Arnaut unsigned len) 290cb409d9SDavi Arnaut { 300cb409d9SDavi Arnaut int ret; 310cb409d9SDavi Arnaut 320cb409d9SDavi Arnaut ret = strncpy_from_user(type, _type, len); 330cb409d9SDavi Arnaut 340cb409d9SDavi Arnaut if (ret < 0) 350cb409d9SDavi Arnaut return -EFAULT; 360cb409d9SDavi Arnaut 370cb409d9SDavi Arnaut if (ret == 0 || ret >= len) 380cb409d9SDavi Arnaut return -EINVAL; 390cb409d9SDavi Arnaut 400cb409d9SDavi Arnaut if (type[0] == '.') 410cb409d9SDavi Arnaut return -EPERM; 420cb409d9SDavi Arnaut 430cb409d9SDavi Arnaut type[len - 1] = '\0'; 440cb409d9SDavi Arnaut 450cb409d9SDavi Arnaut return 0; 460cb409d9SDavi Arnaut } 470cb409d9SDavi Arnaut 481da177e4SLinus Torvalds /*****************************************************************************/ 491da177e4SLinus Torvalds /* 501da177e4SLinus Torvalds * extract the description of a new key from userspace and either add it as a 511da177e4SLinus Torvalds * new key to the specified keyring or update a matching key in that keyring 521da177e4SLinus Torvalds * - the keyring must be writable 531da177e4SLinus Torvalds * - returns the new key's serial number 541da177e4SLinus Torvalds * - implements add_key() 551da177e4SLinus Torvalds */ 561da177e4SLinus Torvalds asmlinkage long sys_add_key(const char __user *_type, 571da177e4SLinus Torvalds const char __user *_description, 581da177e4SLinus Torvalds const void __user *_payload, 591da177e4SLinus Torvalds size_t plen, 601da177e4SLinus Torvalds key_serial_t ringid) 611da177e4SLinus Torvalds { 62664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 631da177e4SLinus Torvalds char type[32], *description; 641da177e4SLinus Torvalds void *payload; 650cb409d9SDavi Arnaut long ret; 66*38bbca6bSDavid Howells bool vm; 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds ret = -EINVAL; 69*38bbca6bSDavid Howells if (plen > 1024 * 1024 - 1) 701da177e4SLinus Torvalds goto error; 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds /* draw all the data into kernel space */ 730cb409d9SDavi Arnaut ret = key_get_type_from_user(type, _type, sizeof(type)); 741da177e4SLinus Torvalds if (ret < 0) 751da177e4SLinus Torvalds goto error; 761da177e4SLinus Torvalds 770cb409d9SDavi Arnaut description = strndup_user(_description, PAGE_SIZE); 780cb409d9SDavi Arnaut if (IS_ERR(description)) { 790cb409d9SDavi Arnaut ret = PTR_ERR(description); 803e30148cSDavid Howells goto error; 810cb409d9SDavi Arnaut } 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 841da177e4SLinus Torvalds payload = NULL; 851da177e4SLinus Torvalds 86*38bbca6bSDavid Howells vm = false; 871da177e4SLinus Torvalds if (_payload) { 881da177e4SLinus Torvalds ret = -ENOMEM; 891da177e4SLinus Torvalds payload = kmalloc(plen, GFP_KERNEL); 90*38bbca6bSDavid Howells if (!payload) { 91*38bbca6bSDavid Howells if (plen <= PAGE_SIZE) 92*38bbca6bSDavid Howells goto error2; 93*38bbca6bSDavid Howells vm = true; 94*38bbca6bSDavid Howells payload = vmalloc(plen); 951da177e4SLinus Torvalds if (!payload) 961da177e4SLinus Torvalds goto error2; 97*38bbca6bSDavid Howells } 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds ret = -EFAULT; 1001da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 1011da177e4SLinus Torvalds goto error3; 1021da177e4SLinus Torvalds } 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds /* find the target keyring (which must be writable) */ 105664cceb0SDavid Howells keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE); 106664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 107664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 1081da177e4SLinus Torvalds goto error3; 1091da177e4SLinus Torvalds } 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds /* create or update the requested key and add it to the target 1121da177e4SLinus Torvalds * keyring */ 113664cceb0SDavid Howells key_ref = key_create_or_update(keyring_ref, type, description, 1147e047ef5SDavid Howells payload, plen, KEY_ALLOC_IN_QUOTA); 115664cceb0SDavid Howells if (!IS_ERR(key_ref)) { 116664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 117664cceb0SDavid Howells key_ref_put(key_ref); 1181da177e4SLinus Torvalds } 1191da177e4SLinus Torvalds else { 120664cceb0SDavid Howells ret = PTR_ERR(key_ref); 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds 123664cceb0SDavid Howells key_ref_put(keyring_ref); 1241da177e4SLinus Torvalds error3: 125*38bbca6bSDavid Howells if (!vm) 1261da177e4SLinus Torvalds kfree(payload); 127*38bbca6bSDavid Howells else 128*38bbca6bSDavid Howells vfree(payload); 1291da177e4SLinus Torvalds error2: 1301da177e4SLinus Torvalds kfree(description); 1311da177e4SLinus Torvalds error: 1321da177e4SLinus Torvalds return ret; 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds } /* end sys_add_key() */ 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds /*****************************************************************************/ 1371da177e4SLinus Torvalds /* 1381da177e4SLinus Torvalds * search the process keyrings for a matching key 1391da177e4SLinus Torvalds * - nested keyrings may also be searched if they have Search permission 1401da177e4SLinus Torvalds * - if a key is found, it will be attached to the destination keyring if 1411da177e4SLinus Torvalds * there's one specified 1421da177e4SLinus Torvalds * - /sbin/request-key will be invoked if _callout_info is non-NULL 1431da177e4SLinus Torvalds * - the _callout_info string will be passed to /sbin/request-key 1441da177e4SLinus Torvalds * - if the _callout_info string is empty, it will be rendered as "-" 1451da177e4SLinus Torvalds * - implements request_key() 1461da177e4SLinus Torvalds */ 1471da177e4SLinus Torvalds asmlinkage long sys_request_key(const char __user *_type, 1481da177e4SLinus Torvalds const char __user *_description, 1491da177e4SLinus Torvalds const char __user *_callout_info, 1501da177e4SLinus Torvalds key_serial_t destringid) 1511da177e4SLinus Torvalds { 1521da177e4SLinus Torvalds struct key_type *ktype; 153664cceb0SDavid Howells struct key *key; 154664cceb0SDavid Howells key_ref_t dest_ref; 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; 1721da177e4SLinus Torvalds if (_callout_info) { 1730cb409d9SDavi Arnaut callout_info = strndup_user(_callout_info, PAGE_SIZE); 1740cb409d9SDavi Arnaut if (IS_ERR(callout_info)) { 1750cb409d9SDavi Arnaut ret = PTR_ERR(callout_info); 1761da177e4SLinus Torvalds goto error2; 1770cb409d9SDavi Arnaut } 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds /* get the destination keyring if specified */ 181664cceb0SDavid Howells dest_ref = NULL; 1821da177e4SLinus Torvalds if (destringid) { 183664cceb0SDavid Howells dest_ref = lookup_user_key(NULL, destringid, 1, 0, KEY_WRITE); 184664cceb0SDavid Howells if (IS_ERR(dest_ref)) { 185664cceb0SDavid Howells ret = PTR_ERR(dest_ref); 1861da177e4SLinus Torvalds goto error3; 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds /* find the key type */ 1911da177e4SLinus Torvalds ktype = key_type_lookup(type); 1921da177e4SLinus Torvalds if (IS_ERR(ktype)) { 1931da177e4SLinus Torvalds ret = PTR_ERR(ktype); 1941da177e4SLinus Torvalds goto error4; 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds /* do the search */ 1984e54f085SDavid Howells key = request_key_and_link(ktype, description, callout_info, NULL, 1997e047ef5SDavid Howells key_ref_to_ptr(dest_ref), 2007e047ef5SDavid Howells KEY_ALLOC_IN_QUOTA); 2011da177e4SLinus Torvalds if (IS_ERR(key)) { 2021da177e4SLinus Torvalds ret = PTR_ERR(key); 2031da177e4SLinus Torvalds goto error5; 2041da177e4SLinus Torvalds } 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds ret = key->serial; 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds key_put(key); 2091da177e4SLinus Torvalds error5: 2101da177e4SLinus Torvalds key_type_put(ktype); 2111da177e4SLinus Torvalds error4: 212664cceb0SDavid Howells key_ref_put(dest_ref); 2131da177e4SLinus Torvalds error3: 2141da177e4SLinus Torvalds kfree(callout_info); 2151da177e4SLinus Torvalds error2: 2161da177e4SLinus Torvalds kfree(description); 2171da177e4SLinus Torvalds error: 2181da177e4SLinus Torvalds return ret; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds } /* end sys_request_key() */ 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds /*****************************************************************************/ 2231da177e4SLinus Torvalds /* 2241da177e4SLinus Torvalds * get the ID of the specified process keyring 2251da177e4SLinus Torvalds * - the keyring must have search permission to be found 2261da177e4SLinus Torvalds * - implements keyctl(KEYCTL_GET_KEYRING_ID) 2271da177e4SLinus Torvalds */ 2281da177e4SLinus Torvalds long keyctl_get_keyring_ID(key_serial_t id, int create) 2291da177e4SLinus Torvalds { 230664cceb0SDavid Howells key_ref_t key_ref; 2311da177e4SLinus Torvalds long ret; 2321da177e4SLinus Torvalds 233664cceb0SDavid Howells key_ref = lookup_user_key(NULL, id, create, 0, KEY_SEARCH); 234664cceb0SDavid Howells if (IS_ERR(key_ref)) { 235664cceb0SDavid Howells ret = PTR_ERR(key_ref); 2361da177e4SLinus Torvalds goto error; 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds 239664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 240664cceb0SDavid Howells key_ref_put(key_ref); 2411da177e4SLinus Torvalds error: 2421da177e4SLinus Torvalds return ret; 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds } /* end keyctl_get_keyring_ID() */ 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds /*****************************************************************************/ 2471da177e4SLinus Torvalds /* 2481da177e4SLinus Torvalds * join the session keyring 2491da177e4SLinus Torvalds * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING) 2501da177e4SLinus Torvalds */ 2511da177e4SLinus Torvalds long keyctl_join_session_keyring(const char __user *_name) 2521da177e4SLinus Torvalds { 2531da177e4SLinus Torvalds char *name; 2540cb409d9SDavi Arnaut long ret; 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds /* fetch the name from userspace */ 2571da177e4SLinus Torvalds name = NULL; 2581da177e4SLinus Torvalds if (_name) { 2590cb409d9SDavi Arnaut name = strndup_user(_name, PAGE_SIZE); 2600cb409d9SDavi Arnaut if (IS_ERR(name)) { 2610cb409d9SDavi Arnaut ret = PTR_ERR(name); 2621da177e4SLinus Torvalds goto error; 2630cb409d9SDavi Arnaut } 2641da177e4SLinus Torvalds } 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds /* join the session */ 2671da177e4SLinus Torvalds ret = join_session_keyring(name); 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds error: 2701da177e4SLinus Torvalds return ret; 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds } /* end keyctl_join_session_keyring() */ 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds /*****************************************************************************/ 2751da177e4SLinus Torvalds /* 2761da177e4SLinus Torvalds * update a key's data payload 2771da177e4SLinus Torvalds * - the key must be writable 2781da177e4SLinus Torvalds * - implements keyctl(KEYCTL_UPDATE) 2791da177e4SLinus Torvalds */ 2801da177e4SLinus Torvalds long keyctl_update_key(key_serial_t id, 2811da177e4SLinus Torvalds const void __user *_payload, 2821da177e4SLinus Torvalds size_t plen) 2831da177e4SLinus Torvalds { 284664cceb0SDavid Howells key_ref_t key_ref; 2851da177e4SLinus Torvalds void *payload; 2861da177e4SLinus Torvalds long ret; 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds ret = -EINVAL; 2891da177e4SLinus Torvalds if (plen > PAGE_SIZE) 2901da177e4SLinus Torvalds goto error; 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 2931da177e4SLinus Torvalds payload = NULL; 2941da177e4SLinus Torvalds if (_payload) { 2951da177e4SLinus Torvalds ret = -ENOMEM; 2961da177e4SLinus Torvalds payload = kmalloc(plen, GFP_KERNEL); 2971da177e4SLinus Torvalds if (!payload) 2981da177e4SLinus Torvalds goto error; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds ret = -EFAULT; 3011da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 3021da177e4SLinus Torvalds goto error2; 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds /* find the target key (which must be writable) */ 306664cceb0SDavid Howells key_ref = lookup_user_key(NULL, id, 0, 0, KEY_WRITE); 307664cceb0SDavid Howells if (IS_ERR(key_ref)) { 308664cceb0SDavid Howells ret = PTR_ERR(key_ref); 3091da177e4SLinus Torvalds goto error2; 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds /* update the key */ 313664cceb0SDavid Howells ret = key_update(key_ref, payload, plen); 3141da177e4SLinus Torvalds 315664cceb0SDavid Howells key_ref_put(key_ref); 3161da177e4SLinus Torvalds error2: 3171da177e4SLinus Torvalds kfree(payload); 3181da177e4SLinus Torvalds error: 3191da177e4SLinus Torvalds return ret; 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds } /* end keyctl_update_key() */ 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds /*****************************************************************************/ 3241da177e4SLinus Torvalds /* 3251da177e4SLinus Torvalds * revoke a key 3261da177e4SLinus Torvalds * - the key must be writable 3271da177e4SLinus Torvalds * - implements keyctl(KEYCTL_REVOKE) 3281da177e4SLinus Torvalds */ 3291da177e4SLinus Torvalds long keyctl_revoke_key(key_serial_t id) 3301da177e4SLinus Torvalds { 331664cceb0SDavid Howells key_ref_t key_ref; 3321da177e4SLinus Torvalds long ret; 3331da177e4SLinus Torvalds 334664cceb0SDavid Howells key_ref = lookup_user_key(NULL, id, 0, 0, KEY_WRITE); 335664cceb0SDavid Howells if (IS_ERR(key_ref)) { 336664cceb0SDavid Howells ret = PTR_ERR(key_ref); 3371da177e4SLinus Torvalds goto error; 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds 340664cceb0SDavid Howells key_revoke(key_ref_to_ptr(key_ref)); 3411da177e4SLinus Torvalds ret = 0; 3421da177e4SLinus Torvalds 343664cceb0SDavid Howells key_ref_put(key_ref); 3441da177e4SLinus Torvalds error: 3451260f801SDavid Howells return ret; 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds } /* end keyctl_revoke_key() */ 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds /*****************************************************************************/ 3501da177e4SLinus Torvalds /* 3511da177e4SLinus Torvalds * clear the specified process keyring 3521da177e4SLinus Torvalds * - the keyring must be writable 3531da177e4SLinus Torvalds * - implements keyctl(KEYCTL_CLEAR) 3541da177e4SLinus Torvalds */ 3551da177e4SLinus Torvalds long keyctl_keyring_clear(key_serial_t ringid) 3561da177e4SLinus Torvalds { 357664cceb0SDavid Howells key_ref_t keyring_ref; 3581da177e4SLinus Torvalds long ret; 3591da177e4SLinus Torvalds 360664cceb0SDavid Howells keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE); 361664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 362664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 3631da177e4SLinus Torvalds goto error; 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds 366664cceb0SDavid Howells ret = keyring_clear(key_ref_to_ptr(keyring_ref)); 3671da177e4SLinus Torvalds 368664cceb0SDavid Howells key_ref_put(keyring_ref); 3691da177e4SLinus Torvalds error: 3701da177e4SLinus Torvalds return ret; 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds } /* end keyctl_keyring_clear() */ 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds /*****************************************************************************/ 3751da177e4SLinus Torvalds /* 3761da177e4SLinus Torvalds * link a key into a keyring 3771da177e4SLinus Torvalds * - the keyring must be writable 3781da177e4SLinus Torvalds * - the key must be linkable 3791da177e4SLinus Torvalds * - implements keyctl(KEYCTL_LINK) 3801da177e4SLinus Torvalds */ 3811da177e4SLinus Torvalds long keyctl_keyring_link(key_serial_t id, key_serial_t ringid) 3821da177e4SLinus Torvalds { 383664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 3841da177e4SLinus Torvalds long ret; 3851da177e4SLinus Torvalds 386664cceb0SDavid Howells keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE); 387664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 388664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 3891da177e4SLinus Torvalds goto error; 3901da177e4SLinus Torvalds } 3911da177e4SLinus Torvalds 392664cceb0SDavid Howells key_ref = lookup_user_key(NULL, id, 1, 0, KEY_LINK); 393664cceb0SDavid Howells if (IS_ERR(key_ref)) { 394664cceb0SDavid Howells ret = PTR_ERR(key_ref); 3951da177e4SLinus Torvalds goto error2; 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds 398664cceb0SDavid Howells ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref)); 3991da177e4SLinus Torvalds 400664cceb0SDavid Howells key_ref_put(key_ref); 4011da177e4SLinus Torvalds error2: 402664cceb0SDavid Howells key_ref_put(keyring_ref); 4031da177e4SLinus Torvalds error: 4041da177e4SLinus Torvalds return ret; 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds } /* end keyctl_keyring_link() */ 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds /*****************************************************************************/ 4091da177e4SLinus Torvalds /* 4101da177e4SLinus Torvalds * unlink the first attachment of a key from a keyring 4111da177e4SLinus Torvalds * - the keyring must be writable 4121da177e4SLinus Torvalds * - we don't need any permissions on the key 4131da177e4SLinus Torvalds * - implements keyctl(KEYCTL_UNLINK) 4141da177e4SLinus Torvalds */ 4151da177e4SLinus Torvalds long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid) 4161da177e4SLinus Torvalds { 417664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 4181da177e4SLinus Torvalds long ret; 4191da177e4SLinus Torvalds 420664cceb0SDavid Howells keyring_ref = lookup_user_key(NULL, ringid, 0, 0, KEY_WRITE); 421664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 422664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 4231da177e4SLinus Torvalds goto error; 4241da177e4SLinus Torvalds } 4251da177e4SLinus Torvalds 426664cceb0SDavid Howells key_ref = lookup_user_key(NULL, id, 0, 0, 0); 427664cceb0SDavid Howells if (IS_ERR(key_ref)) { 428664cceb0SDavid Howells ret = PTR_ERR(key_ref); 4291da177e4SLinus Torvalds goto error2; 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds 432664cceb0SDavid Howells ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref)); 4331da177e4SLinus Torvalds 434664cceb0SDavid Howells key_ref_put(key_ref); 4351da177e4SLinus Torvalds error2: 436664cceb0SDavid Howells key_ref_put(keyring_ref); 4371da177e4SLinus Torvalds error: 4381da177e4SLinus Torvalds return ret; 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds } /* end keyctl_keyring_unlink() */ 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds /*****************************************************************************/ 4431da177e4SLinus Torvalds /* 4441da177e4SLinus Torvalds * describe a user key 4451da177e4SLinus Torvalds * - the key must have view permission 4461da177e4SLinus Torvalds * - if there's a buffer, we place up to buflen bytes of data into it 4471da177e4SLinus Torvalds * - unless there's an error, we return the amount of description available, 4481da177e4SLinus Torvalds * irrespective of how much we may have copied 4491da177e4SLinus Torvalds * - the description is formatted thus: 4501da177e4SLinus Torvalds * type;uid;gid;perm;description<NUL> 4511da177e4SLinus Torvalds * - implements keyctl(KEYCTL_DESCRIBE) 4521da177e4SLinus Torvalds */ 4531da177e4SLinus Torvalds long keyctl_describe_key(key_serial_t keyid, 4541da177e4SLinus Torvalds char __user *buffer, 4551da177e4SLinus Torvalds size_t buflen) 4561da177e4SLinus Torvalds { 4573e30148cSDavid Howells struct key *key, *instkey; 458664cceb0SDavid Howells key_ref_t key_ref; 4591da177e4SLinus Torvalds char *tmpbuf; 4601da177e4SLinus Torvalds long ret; 4611da177e4SLinus Torvalds 462664cceb0SDavid Howells key_ref = lookup_user_key(NULL, keyid, 0, 1, KEY_VIEW); 463664cceb0SDavid Howells if (IS_ERR(key_ref)) { 4643e30148cSDavid Howells /* viewing a key under construction is permitted if we have the 4653e30148cSDavid Howells * authorisation token handy */ 466664cceb0SDavid Howells if (PTR_ERR(key_ref) == -EACCES) { 4673e30148cSDavid Howells instkey = key_get_instantiation_authkey(keyid); 4683e30148cSDavid Howells if (!IS_ERR(instkey)) { 4693e30148cSDavid Howells key_put(instkey); 470664cceb0SDavid Howells key_ref = lookup_user_key(NULL, keyid, 471664cceb0SDavid Howells 0, 1, 0); 472664cceb0SDavid Howells if (!IS_ERR(key_ref)) 4733e30148cSDavid Howells goto okay; 4743e30148cSDavid Howells } 4753e30148cSDavid Howells } 4763e30148cSDavid Howells 477664cceb0SDavid Howells ret = PTR_ERR(key_ref); 4781da177e4SLinus Torvalds goto error; 4791da177e4SLinus Torvalds } 4801da177e4SLinus Torvalds 4813e30148cSDavid Howells okay: 4821da177e4SLinus Torvalds /* calculate how much description we're going to return */ 4831da177e4SLinus Torvalds ret = -ENOMEM; 4841da177e4SLinus Torvalds tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); 4851da177e4SLinus Torvalds if (!tmpbuf) 4861da177e4SLinus Torvalds goto error2; 4871da177e4SLinus Torvalds 488664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 489664cceb0SDavid Howells 4901da177e4SLinus Torvalds ret = snprintf(tmpbuf, PAGE_SIZE - 1, 491664cceb0SDavid Howells "%s;%d;%d;%08x;%s", 492664cceb0SDavid Howells key_ref_to_ptr(key_ref)->type->name, 493664cceb0SDavid Howells key_ref_to_ptr(key_ref)->uid, 494664cceb0SDavid Howells key_ref_to_ptr(key_ref)->gid, 495664cceb0SDavid Howells key_ref_to_ptr(key_ref)->perm, 496664cceb0SDavid Howells key_ref_to_ptr(key_ref)->description ? 497664cceb0SDavid Howells key_ref_to_ptr(key_ref)->description : "" 4981da177e4SLinus Torvalds ); 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds /* include a NUL char at the end of the data */ 5011da177e4SLinus Torvalds if (ret > PAGE_SIZE - 1) 5021da177e4SLinus Torvalds ret = PAGE_SIZE - 1; 5031da177e4SLinus Torvalds tmpbuf[ret] = 0; 5041da177e4SLinus Torvalds ret++; 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds /* consider returning the data */ 5071da177e4SLinus Torvalds if (buffer && buflen > 0) { 5081da177e4SLinus Torvalds if (buflen > ret) 5091da177e4SLinus Torvalds buflen = ret; 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds if (copy_to_user(buffer, tmpbuf, buflen) != 0) 5121da177e4SLinus Torvalds ret = -EFAULT; 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds kfree(tmpbuf); 5161da177e4SLinus Torvalds error2: 517664cceb0SDavid Howells key_ref_put(key_ref); 5181da177e4SLinus Torvalds error: 5191da177e4SLinus Torvalds return ret; 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds } /* end keyctl_describe_key() */ 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds /*****************************************************************************/ 5241da177e4SLinus Torvalds /* 5251da177e4SLinus Torvalds * search the specified keyring for a matching key 5261da177e4SLinus Torvalds * - the start keyring must be searchable 5271da177e4SLinus Torvalds * - nested keyrings may also be searched if they are searchable 5281da177e4SLinus Torvalds * - only keys with search permission may be found 5291da177e4SLinus Torvalds * - if a key is found, it will be attached to the destination keyring if 5301da177e4SLinus Torvalds * there's one specified 5311da177e4SLinus Torvalds * - implements keyctl(KEYCTL_SEARCH) 5321da177e4SLinus Torvalds */ 5331da177e4SLinus Torvalds long keyctl_keyring_search(key_serial_t ringid, 5341da177e4SLinus Torvalds const char __user *_type, 5351da177e4SLinus Torvalds const char __user *_description, 5361da177e4SLinus Torvalds key_serial_t destringid) 5371da177e4SLinus Torvalds { 5381da177e4SLinus Torvalds struct key_type *ktype; 539664cceb0SDavid Howells key_ref_t keyring_ref, key_ref, dest_ref; 5401da177e4SLinus Torvalds char type[32], *description; 5410cb409d9SDavi Arnaut long ret; 5421da177e4SLinus Torvalds 5431da177e4SLinus Torvalds /* pull the type and description into kernel space */ 5440cb409d9SDavi Arnaut ret = key_get_type_from_user(type, _type, sizeof(type)); 5451da177e4SLinus Torvalds if (ret < 0) 5461da177e4SLinus Torvalds goto error; 5471da177e4SLinus Torvalds 5480cb409d9SDavi Arnaut description = strndup_user(_description, PAGE_SIZE); 5490cb409d9SDavi Arnaut if (IS_ERR(description)) { 5500cb409d9SDavi Arnaut ret = PTR_ERR(description); 5511da177e4SLinus Torvalds goto error; 5520cb409d9SDavi Arnaut } 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds /* get the keyring at which to begin the search */ 555664cceb0SDavid Howells keyring_ref = lookup_user_key(NULL, ringid, 0, 0, KEY_SEARCH); 556664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 557664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 5581da177e4SLinus Torvalds goto error2; 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds /* get the destination keyring if specified */ 562664cceb0SDavid Howells dest_ref = NULL; 5631da177e4SLinus Torvalds if (destringid) { 564664cceb0SDavid Howells dest_ref = lookup_user_key(NULL, destringid, 1, 0, KEY_WRITE); 565664cceb0SDavid Howells if (IS_ERR(dest_ref)) { 566664cceb0SDavid Howells ret = PTR_ERR(dest_ref); 5671da177e4SLinus Torvalds goto error3; 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds } 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds /* find the key type */ 5721da177e4SLinus Torvalds ktype = key_type_lookup(type); 5731da177e4SLinus Torvalds if (IS_ERR(ktype)) { 5741da177e4SLinus Torvalds ret = PTR_ERR(ktype); 5751da177e4SLinus Torvalds goto error4; 5761da177e4SLinus Torvalds } 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds /* do the search */ 579664cceb0SDavid Howells key_ref = keyring_search(keyring_ref, ktype, description); 580664cceb0SDavid Howells if (IS_ERR(key_ref)) { 581664cceb0SDavid Howells ret = PTR_ERR(key_ref); 5821da177e4SLinus Torvalds 5831da177e4SLinus Torvalds /* treat lack or presence of a negative key the same */ 5841da177e4SLinus Torvalds if (ret == -EAGAIN) 5851da177e4SLinus Torvalds ret = -ENOKEY; 5861da177e4SLinus Torvalds goto error5; 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds /* link the resulting key to the destination keyring if we can */ 590664cceb0SDavid Howells if (dest_ref) { 59129db9190SDavid Howells ret = key_permission(key_ref, KEY_LINK); 59229db9190SDavid Howells if (ret < 0) 5931da177e4SLinus Torvalds goto error6; 5941da177e4SLinus Torvalds 595664cceb0SDavid Howells ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref)); 5961da177e4SLinus Torvalds if (ret < 0) 5971da177e4SLinus Torvalds goto error6; 5981da177e4SLinus Torvalds } 5991da177e4SLinus Torvalds 600664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds error6: 603664cceb0SDavid Howells key_ref_put(key_ref); 6041da177e4SLinus Torvalds error5: 6051da177e4SLinus Torvalds key_type_put(ktype); 6061da177e4SLinus Torvalds error4: 607664cceb0SDavid Howells key_ref_put(dest_ref); 6081da177e4SLinus Torvalds error3: 609664cceb0SDavid Howells key_ref_put(keyring_ref); 6101da177e4SLinus Torvalds error2: 6111da177e4SLinus Torvalds kfree(description); 6121da177e4SLinus Torvalds error: 6131da177e4SLinus Torvalds return ret; 6141da177e4SLinus Torvalds 6151da177e4SLinus Torvalds } /* end keyctl_keyring_search() */ 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds /*****************************************************************************/ 6181da177e4SLinus Torvalds /* 6191da177e4SLinus Torvalds * read a user key's payload 6201da177e4SLinus Torvalds * - the keyring must be readable or the key must be searchable from the 6211da177e4SLinus Torvalds * process's keyrings 6221da177e4SLinus Torvalds * - if there's a buffer, we place up to buflen bytes of data into it 6231da177e4SLinus Torvalds * - unless there's an error, we return the amount of data in the key, 6241da177e4SLinus Torvalds * irrespective of how much we may have copied 6251da177e4SLinus Torvalds * - implements keyctl(KEYCTL_READ) 6261da177e4SLinus Torvalds */ 6271da177e4SLinus Torvalds long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen) 6281da177e4SLinus Torvalds { 629664cceb0SDavid Howells struct key *key; 630664cceb0SDavid Howells key_ref_t key_ref; 6311da177e4SLinus Torvalds long ret; 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds /* find the key first */ 634664cceb0SDavid Howells key_ref = lookup_user_key(NULL, keyid, 0, 0, 0); 635664cceb0SDavid Howells if (IS_ERR(key_ref)) { 636664cceb0SDavid Howells ret = -ENOKEY; 637664cceb0SDavid Howells goto error; 638664cceb0SDavid Howells } 639664cceb0SDavid Howells 640664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 641664cceb0SDavid Howells 6421da177e4SLinus Torvalds /* see if we can read it directly */ 64329db9190SDavid Howells ret = key_permission(key_ref, KEY_READ); 64429db9190SDavid Howells if (ret == 0) 6451da177e4SLinus Torvalds goto can_read_key; 64629db9190SDavid Howells if (ret != -EACCES) 64729db9190SDavid Howells goto error; 6481da177e4SLinus Torvalds 649664cceb0SDavid Howells /* we can't; see if it's searchable from this process's keyrings 6503e30148cSDavid Howells * - we automatically take account of the fact that it may be 6513e30148cSDavid Howells * dangling off an instantiation key 6523e30148cSDavid Howells */ 653664cceb0SDavid Howells if (!is_key_possessed(key_ref)) { 6541260f801SDavid Howells ret = -EACCES; 6551da177e4SLinus Torvalds goto error2; 6561da177e4SLinus Torvalds } 6571da177e4SLinus Torvalds 6581da177e4SLinus Torvalds /* the key is probably readable - now try to read it */ 6591da177e4SLinus Torvalds can_read_key: 6601da177e4SLinus Torvalds ret = key_validate(key); 6611da177e4SLinus Torvalds if (ret == 0) { 6621da177e4SLinus Torvalds ret = -EOPNOTSUPP; 6631da177e4SLinus Torvalds if (key->type->read) { 6641da177e4SLinus Torvalds /* read the data with the semaphore held (since we 6651da177e4SLinus Torvalds * might sleep) */ 6661da177e4SLinus Torvalds down_read(&key->sem); 6671da177e4SLinus Torvalds ret = key->type->read(key, buffer, buflen); 6681da177e4SLinus Torvalds up_read(&key->sem); 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds } 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds error2: 6731da177e4SLinus Torvalds key_put(key); 6741da177e4SLinus Torvalds error: 6751da177e4SLinus Torvalds return ret; 6761da177e4SLinus Torvalds 6771da177e4SLinus Torvalds } /* end keyctl_read_key() */ 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds /*****************************************************************************/ 6801da177e4SLinus Torvalds /* 6811da177e4SLinus Torvalds * change the ownership of a key 6821da177e4SLinus Torvalds * - the keyring owned by the changer 6831da177e4SLinus Torvalds * - if the uid or gid is -1, then that parameter is not changed 6841da177e4SLinus Torvalds * - implements keyctl(KEYCTL_CHOWN) 6851da177e4SLinus Torvalds */ 6861da177e4SLinus Torvalds long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid) 6871da177e4SLinus Torvalds { 6885801649dSFredrik Tolf struct key_user *newowner, *zapowner = NULL; 6891da177e4SLinus Torvalds struct key *key; 690664cceb0SDavid Howells key_ref_t key_ref; 6911da177e4SLinus Torvalds long ret; 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds ret = 0; 6941da177e4SLinus Torvalds if (uid == (uid_t) -1 && gid == (gid_t) -1) 6951da177e4SLinus Torvalds goto error; 6961da177e4SLinus Torvalds 69729db9190SDavid Howells key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR); 698664cceb0SDavid Howells if (IS_ERR(key_ref)) { 699664cceb0SDavid Howells ret = PTR_ERR(key_ref); 7001da177e4SLinus Torvalds goto error; 7011da177e4SLinus Torvalds } 7021da177e4SLinus Torvalds 703664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 704664cceb0SDavid Howells 7051da177e4SLinus Torvalds /* make the changes with the locks held to prevent chown/chown races */ 7061da177e4SLinus Torvalds ret = -EACCES; 7071da177e4SLinus Torvalds down_write(&key->sem); 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) { 7101da177e4SLinus Torvalds /* only the sysadmin can chown a key to some other UID */ 7111da177e4SLinus Torvalds if (uid != (uid_t) -1 && key->uid != uid) 7125801649dSFredrik Tolf goto error_put; 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds /* only the sysadmin can set the key's GID to a group other 7151da177e4SLinus Torvalds * than one of those that the current process subscribes to */ 7161da177e4SLinus Torvalds if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid)) 7175801649dSFredrik Tolf goto error_put; 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds 7205801649dSFredrik Tolf /* change the UID */ 7211da177e4SLinus Torvalds if (uid != (uid_t) -1 && uid != key->uid) { 7225801649dSFredrik Tolf ret = -ENOMEM; 7235801649dSFredrik Tolf newowner = key_user_lookup(uid); 7245801649dSFredrik Tolf if (!newowner) 7255801649dSFredrik Tolf goto error_put; 7265801649dSFredrik Tolf 7275801649dSFredrik Tolf /* transfer the quota burden to the new user */ 7285801649dSFredrik Tolf if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { 7295801649dSFredrik Tolf spin_lock(&newowner->lock); 7305801649dSFredrik Tolf if (newowner->qnkeys + 1 >= KEYQUOTA_MAX_KEYS || 7315801649dSFredrik Tolf newowner->qnbytes + key->quotalen >= 7325801649dSFredrik Tolf KEYQUOTA_MAX_BYTES) 7335801649dSFredrik Tolf goto quota_overrun; 7345801649dSFredrik Tolf 7355801649dSFredrik Tolf newowner->qnkeys++; 7365801649dSFredrik Tolf newowner->qnbytes += key->quotalen; 7375801649dSFredrik Tolf spin_unlock(&newowner->lock); 7385801649dSFredrik Tolf 7395801649dSFredrik Tolf spin_lock(&key->user->lock); 7405801649dSFredrik Tolf key->user->qnkeys--; 7415801649dSFredrik Tolf key->user->qnbytes -= key->quotalen; 7425801649dSFredrik Tolf spin_unlock(&key->user->lock); 7435801649dSFredrik Tolf } 7445801649dSFredrik Tolf 7455801649dSFredrik Tolf atomic_dec(&key->user->nkeys); 7465801649dSFredrik Tolf atomic_inc(&newowner->nkeys); 7475801649dSFredrik Tolf 7485801649dSFredrik Tolf if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) { 7495801649dSFredrik Tolf atomic_dec(&key->user->nikeys); 7505801649dSFredrik Tolf atomic_inc(&newowner->nikeys); 7515801649dSFredrik Tolf } 7525801649dSFredrik Tolf 7535801649dSFredrik Tolf zapowner = key->user; 7545801649dSFredrik Tolf key->user = newowner; 7555801649dSFredrik Tolf key->uid = uid; 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds /* change the GID */ 7591da177e4SLinus Torvalds if (gid != (gid_t) -1) 7601da177e4SLinus Torvalds key->gid = gid; 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds ret = 0; 7631da177e4SLinus Torvalds 7645801649dSFredrik Tolf error_put: 7651da177e4SLinus Torvalds up_write(&key->sem); 7661da177e4SLinus Torvalds key_put(key); 7675801649dSFredrik Tolf if (zapowner) 7685801649dSFredrik Tolf key_user_put(zapowner); 7691da177e4SLinus Torvalds error: 7701da177e4SLinus Torvalds return ret; 7711da177e4SLinus Torvalds 7725801649dSFredrik Tolf quota_overrun: 7735801649dSFredrik Tolf spin_unlock(&newowner->lock); 7745801649dSFredrik Tolf zapowner = newowner; 7755801649dSFredrik Tolf ret = -EDQUOT; 7765801649dSFredrik Tolf goto error_put; 7775801649dSFredrik Tolf 7781da177e4SLinus Torvalds } /* end keyctl_chown_key() */ 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds /*****************************************************************************/ 7811da177e4SLinus Torvalds /* 7821da177e4SLinus Torvalds * change the permission mask on a key 7831da177e4SLinus Torvalds * - the keyring owned by the changer 7841da177e4SLinus Torvalds * - implements keyctl(KEYCTL_SETPERM) 7851da177e4SLinus Torvalds */ 7861da177e4SLinus Torvalds long keyctl_setperm_key(key_serial_t id, key_perm_t perm) 7871da177e4SLinus Torvalds { 7881da177e4SLinus Torvalds struct key *key; 789664cceb0SDavid Howells key_ref_t key_ref; 7901da177e4SLinus Torvalds long ret; 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds ret = -EINVAL; 793664cceb0SDavid Howells if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL)) 7941da177e4SLinus Torvalds goto error; 7951da177e4SLinus Torvalds 79629db9190SDavid Howells key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR); 797664cceb0SDavid Howells if (IS_ERR(key_ref)) { 798664cceb0SDavid Howells ret = PTR_ERR(key_ref); 7991da177e4SLinus Torvalds goto error; 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds 802664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 803664cceb0SDavid Howells 80476d8aeabSDavid Howells /* make the changes with the locks held to prevent chown/chmod races */ 8051da177e4SLinus Torvalds ret = -EACCES; 8061da177e4SLinus Torvalds down_write(&key->sem); 8071da177e4SLinus Torvalds 80876d8aeabSDavid Howells /* if we're not the sysadmin, we can only change a key that we own */ 80976d8aeabSDavid Howells if (capable(CAP_SYS_ADMIN) || key->uid == current->fsuid) { 8101da177e4SLinus Torvalds key->perm = perm; 8111da177e4SLinus Torvalds ret = 0; 81276d8aeabSDavid Howells } 8131da177e4SLinus Torvalds 8141da177e4SLinus Torvalds up_write(&key->sem); 8151da177e4SLinus Torvalds key_put(key); 8161da177e4SLinus Torvalds error: 8171da177e4SLinus Torvalds return ret; 8181da177e4SLinus Torvalds 8191da177e4SLinus Torvalds } /* end keyctl_setperm_key() */ 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds /*****************************************************************************/ 8221da177e4SLinus Torvalds /* 8231da177e4SLinus Torvalds * instantiate the key with the specified payload, and, if one is given, link 8241da177e4SLinus Torvalds * the key into the keyring 8251da177e4SLinus Torvalds */ 8261da177e4SLinus Torvalds long keyctl_instantiate_key(key_serial_t id, 8271da177e4SLinus Torvalds const void __user *_payload, 8281da177e4SLinus Torvalds size_t plen, 8291da177e4SLinus Torvalds key_serial_t ringid) 8301da177e4SLinus Torvalds { 8313e30148cSDavid Howells struct request_key_auth *rka; 832664cceb0SDavid Howells struct key *instkey; 833664cceb0SDavid Howells key_ref_t keyring_ref; 8341da177e4SLinus Torvalds void *payload; 8351da177e4SLinus Torvalds long ret; 836*38bbca6bSDavid Howells bool vm = false; 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds ret = -EINVAL; 839*38bbca6bSDavid Howells if (plen > 1024 * 1024 - 1) 8401da177e4SLinus Torvalds goto error; 8411da177e4SLinus Torvalds 842b5f545c8SDavid Howells /* the appropriate instantiation authorisation key must have been 843b5f545c8SDavid Howells * assumed before calling this */ 844b5f545c8SDavid Howells ret = -EPERM; 845b5f545c8SDavid Howells instkey = current->request_key_auth; 846b5f545c8SDavid Howells if (!instkey) 847b5f545c8SDavid Howells goto error; 848b5f545c8SDavid Howells 849b5f545c8SDavid Howells rka = instkey->payload.data; 850b5f545c8SDavid Howells if (rka->target_key->serial != id) 851b5f545c8SDavid Howells goto error; 852b5f545c8SDavid Howells 8531da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 8541da177e4SLinus Torvalds payload = NULL; 8551da177e4SLinus Torvalds 8561da177e4SLinus Torvalds if (_payload) { 8571da177e4SLinus Torvalds ret = -ENOMEM; 8581da177e4SLinus Torvalds payload = kmalloc(plen, GFP_KERNEL); 859*38bbca6bSDavid Howells if (!payload) { 860*38bbca6bSDavid Howells if (plen <= PAGE_SIZE) 861*38bbca6bSDavid Howells goto error; 862*38bbca6bSDavid Howells vm = true; 863*38bbca6bSDavid Howells payload = vmalloc(plen); 8641da177e4SLinus Torvalds if (!payload) 8651da177e4SLinus Torvalds goto error; 866*38bbca6bSDavid Howells } 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds ret = -EFAULT; 8691da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 8701da177e4SLinus Torvalds goto error2; 8711da177e4SLinus Torvalds } 8721da177e4SLinus Torvalds 8733e30148cSDavid Howells /* find the destination keyring amongst those belonging to the 8743e30148cSDavid Howells * requesting task */ 875664cceb0SDavid Howells keyring_ref = NULL; 8761da177e4SLinus Torvalds if (ringid) { 877664cceb0SDavid Howells keyring_ref = lookup_user_key(rka->context, ringid, 1, 0, 8783e30148cSDavid Howells KEY_WRITE); 879664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 880664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 881b5f545c8SDavid Howells goto error2; 8821da177e4SLinus Torvalds } 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds /* instantiate the key and link it into a keyring */ 8863e30148cSDavid Howells ret = key_instantiate_and_link(rka->target_key, payload, plen, 887664cceb0SDavid Howells key_ref_to_ptr(keyring_ref), instkey); 8881da177e4SLinus Torvalds 889664cceb0SDavid Howells key_ref_put(keyring_ref); 890b5f545c8SDavid Howells 891b5f545c8SDavid Howells /* discard the assumed authority if it's just been disabled by 892b5f545c8SDavid Howells * instantiation of the key */ 893b5f545c8SDavid Howells if (ret == 0) { 894b5f545c8SDavid Howells key_put(current->request_key_auth); 895b5f545c8SDavid Howells current->request_key_auth = NULL; 896b5f545c8SDavid Howells } 897b5f545c8SDavid Howells 8981da177e4SLinus Torvalds error2: 899*38bbca6bSDavid Howells if (!vm) 9001da177e4SLinus Torvalds kfree(payload); 901*38bbca6bSDavid Howells else 902*38bbca6bSDavid Howells vfree(payload); 9031da177e4SLinus Torvalds error: 9041da177e4SLinus Torvalds return ret; 9051da177e4SLinus Torvalds 9061da177e4SLinus Torvalds } /* end keyctl_instantiate_key() */ 9071da177e4SLinus Torvalds 9081da177e4SLinus Torvalds /*****************************************************************************/ 9091da177e4SLinus Torvalds /* 9101da177e4SLinus Torvalds * negatively instantiate the key with the given timeout (in seconds), and, if 9111da177e4SLinus Torvalds * one is given, link the key into the keyring 9121da177e4SLinus Torvalds */ 9131da177e4SLinus Torvalds long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid) 9141da177e4SLinus Torvalds { 9153e30148cSDavid Howells struct request_key_auth *rka; 916664cceb0SDavid Howells struct key *instkey; 917664cceb0SDavid Howells key_ref_t keyring_ref; 9181da177e4SLinus Torvalds long ret; 9191da177e4SLinus Torvalds 920b5f545c8SDavid Howells /* the appropriate instantiation authorisation key must have been 921b5f545c8SDavid Howells * assumed before calling this */ 922b5f545c8SDavid Howells ret = -EPERM; 923b5f545c8SDavid Howells instkey = current->request_key_auth; 924b5f545c8SDavid Howells if (!instkey) 9251da177e4SLinus Torvalds goto error; 9261da177e4SLinus Torvalds 9273e30148cSDavid Howells rka = instkey->payload.data; 928b5f545c8SDavid Howells if (rka->target_key->serial != id) 929b5f545c8SDavid Howells goto error; 9303e30148cSDavid Howells 9311da177e4SLinus Torvalds /* find the destination keyring if present (which must also be 9321da177e4SLinus Torvalds * writable) */ 933664cceb0SDavid Howells keyring_ref = NULL; 9341da177e4SLinus Torvalds if (ringid) { 935664cceb0SDavid Howells keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE); 936664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 937664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 938b5f545c8SDavid Howells goto error; 9391da177e4SLinus Torvalds } 9401da177e4SLinus Torvalds } 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds /* instantiate the key and link it into a keyring */ 943664cceb0SDavid Howells ret = key_negate_and_link(rka->target_key, timeout, 944664cceb0SDavid Howells key_ref_to_ptr(keyring_ref), instkey); 9451da177e4SLinus Torvalds 946664cceb0SDavid Howells key_ref_put(keyring_ref); 947b5f545c8SDavid Howells 948b5f545c8SDavid Howells /* discard the assumed authority if it's just been disabled by 949b5f545c8SDavid Howells * instantiation of the key */ 950b5f545c8SDavid Howells if (ret == 0) { 951b5f545c8SDavid Howells key_put(current->request_key_auth); 952b5f545c8SDavid Howells current->request_key_auth = NULL; 953b5f545c8SDavid Howells } 954b5f545c8SDavid Howells 9551da177e4SLinus Torvalds error: 9561da177e4SLinus Torvalds return ret; 9571da177e4SLinus Torvalds 9581da177e4SLinus Torvalds } /* end keyctl_negate_key() */ 9591da177e4SLinus Torvalds 9601da177e4SLinus Torvalds /*****************************************************************************/ 9611da177e4SLinus Torvalds /* 9623e30148cSDavid Howells * set the default keyring in which request_key() will cache keys 9633e30148cSDavid Howells * - return the old setting 9643e30148cSDavid Howells */ 9653e30148cSDavid Howells long keyctl_set_reqkey_keyring(int reqkey_defl) 9663e30148cSDavid Howells { 9673e30148cSDavid Howells int ret; 9683e30148cSDavid Howells 9693e30148cSDavid Howells switch (reqkey_defl) { 9703e30148cSDavid Howells case KEY_REQKEY_DEFL_THREAD_KEYRING: 9713e30148cSDavid Howells ret = install_thread_keyring(current); 9723e30148cSDavid Howells if (ret < 0) 9733e30148cSDavid Howells return ret; 9743e30148cSDavid Howells goto set; 9753e30148cSDavid Howells 9763e30148cSDavid Howells case KEY_REQKEY_DEFL_PROCESS_KEYRING: 9773e30148cSDavid Howells ret = install_process_keyring(current); 9783e30148cSDavid Howells if (ret < 0) 9793e30148cSDavid Howells return ret; 9803e30148cSDavid Howells 9813e30148cSDavid Howells case KEY_REQKEY_DEFL_DEFAULT: 9823e30148cSDavid Howells case KEY_REQKEY_DEFL_SESSION_KEYRING: 9833e30148cSDavid Howells case KEY_REQKEY_DEFL_USER_KEYRING: 9843e30148cSDavid Howells case KEY_REQKEY_DEFL_USER_SESSION_KEYRING: 9853e30148cSDavid Howells set: 9863e30148cSDavid Howells current->jit_keyring = reqkey_defl; 9873e30148cSDavid Howells 9883e30148cSDavid Howells case KEY_REQKEY_DEFL_NO_CHANGE: 9893e30148cSDavid Howells return current->jit_keyring; 9903e30148cSDavid Howells 9913e30148cSDavid Howells case KEY_REQKEY_DEFL_GROUP_KEYRING: 9923e30148cSDavid Howells default: 9933e30148cSDavid Howells return -EINVAL; 9943e30148cSDavid Howells } 9953e30148cSDavid Howells 9963e30148cSDavid Howells } /* end keyctl_set_reqkey_keyring() */ 9973e30148cSDavid Howells 9983e30148cSDavid Howells /*****************************************************************************/ 9993e30148cSDavid Howells /* 1000017679c4SDavid Howells * set or clear the timeout for a key 1001017679c4SDavid Howells */ 1002017679c4SDavid Howells long keyctl_set_timeout(key_serial_t id, unsigned timeout) 1003017679c4SDavid Howells { 1004017679c4SDavid Howells struct timespec now; 1005017679c4SDavid Howells struct key *key; 1006017679c4SDavid Howells key_ref_t key_ref; 1007017679c4SDavid Howells time_t expiry; 1008017679c4SDavid Howells long ret; 1009017679c4SDavid Howells 1010017679c4SDavid Howells key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR); 1011017679c4SDavid Howells if (IS_ERR(key_ref)) { 1012017679c4SDavid Howells ret = PTR_ERR(key_ref); 1013017679c4SDavid Howells goto error; 1014017679c4SDavid Howells } 1015017679c4SDavid Howells 1016017679c4SDavid Howells key = key_ref_to_ptr(key_ref); 1017017679c4SDavid Howells 1018017679c4SDavid Howells /* make the changes with the locks held to prevent races */ 1019017679c4SDavid Howells down_write(&key->sem); 1020017679c4SDavid Howells 1021017679c4SDavid Howells expiry = 0; 1022017679c4SDavid Howells if (timeout > 0) { 1023017679c4SDavid Howells now = current_kernel_time(); 1024017679c4SDavid Howells expiry = now.tv_sec + timeout; 1025017679c4SDavid Howells } 1026017679c4SDavid Howells 1027017679c4SDavid Howells key->expiry = expiry; 1028017679c4SDavid Howells 1029017679c4SDavid Howells up_write(&key->sem); 1030017679c4SDavid Howells key_put(key); 1031017679c4SDavid Howells 1032017679c4SDavid Howells ret = 0; 1033017679c4SDavid Howells error: 1034017679c4SDavid Howells return ret; 1035017679c4SDavid Howells 1036017679c4SDavid Howells } /* end keyctl_set_timeout() */ 1037017679c4SDavid Howells 1038017679c4SDavid Howells /*****************************************************************************/ 1039017679c4SDavid Howells /* 1040b5f545c8SDavid Howells * assume the authority to instantiate the specified key 1041b5f545c8SDavid Howells */ 1042b5f545c8SDavid Howells long keyctl_assume_authority(key_serial_t id) 1043b5f545c8SDavid Howells { 1044b5f545c8SDavid Howells struct key *authkey; 1045b5f545c8SDavid Howells long ret; 1046b5f545c8SDavid Howells 1047b5f545c8SDavid Howells /* special key IDs aren't permitted */ 1048b5f545c8SDavid Howells ret = -EINVAL; 1049b5f545c8SDavid Howells if (id < 0) 1050b5f545c8SDavid Howells goto error; 1051b5f545c8SDavid Howells 1052b5f545c8SDavid Howells /* we divest ourselves of authority if given an ID of 0 */ 1053b5f545c8SDavid Howells if (id == 0) { 1054b5f545c8SDavid Howells key_put(current->request_key_auth); 1055b5f545c8SDavid Howells current->request_key_auth = NULL; 1056b5f545c8SDavid Howells ret = 0; 1057b5f545c8SDavid Howells goto error; 1058b5f545c8SDavid Howells } 1059b5f545c8SDavid Howells 1060b5f545c8SDavid Howells /* attempt to assume the authority temporarily granted to us whilst we 1061b5f545c8SDavid Howells * instantiate the specified key 1062b5f545c8SDavid Howells * - the authorisation key must be in the current task's keyrings 1063b5f545c8SDavid Howells * somewhere 1064b5f545c8SDavid Howells */ 1065b5f545c8SDavid Howells authkey = key_get_instantiation_authkey(id); 1066b5f545c8SDavid Howells if (IS_ERR(authkey)) { 1067b5f545c8SDavid Howells ret = PTR_ERR(authkey); 1068b5f545c8SDavid Howells goto error; 1069b5f545c8SDavid Howells } 1070b5f545c8SDavid Howells 1071b5f545c8SDavid Howells key_put(current->request_key_auth); 1072b5f545c8SDavid Howells current->request_key_auth = authkey; 1073b5f545c8SDavid Howells ret = authkey->serial; 1074b5f545c8SDavid Howells 1075b5f545c8SDavid Howells error: 1076b5f545c8SDavid Howells return ret; 1077b5f545c8SDavid Howells 1078b5f545c8SDavid Howells } /* end keyctl_assume_authority() */ 1079b5f545c8SDavid Howells 1080b5f545c8SDavid Howells /*****************************************************************************/ 1081b5f545c8SDavid Howells /* 10821da177e4SLinus Torvalds * the key control system call 10831da177e4SLinus Torvalds */ 10841da177e4SLinus Torvalds asmlinkage long sys_keyctl(int option, unsigned long arg2, unsigned long arg3, 10851da177e4SLinus Torvalds unsigned long arg4, unsigned long arg5) 10861da177e4SLinus Torvalds { 10871da177e4SLinus Torvalds switch (option) { 10881da177e4SLinus Torvalds case KEYCTL_GET_KEYRING_ID: 10891da177e4SLinus Torvalds return keyctl_get_keyring_ID((key_serial_t) arg2, 10901da177e4SLinus Torvalds (int) arg3); 10911da177e4SLinus Torvalds 10921da177e4SLinus Torvalds case KEYCTL_JOIN_SESSION_KEYRING: 10931da177e4SLinus Torvalds return keyctl_join_session_keyring((const char __user *) arg2); 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds case KEYCTL_UPDATE: 10961da177e4SLinus Torvalds return keyctl_update_key((key_serial_t) arg2, 10971da177e4SLinus Torvalds (const void __user *) arg3, 10981da177e4SLinus Torvalds (size_t) arg4); 10991da177e4SLinus Torvalds 11001da177e4SLinus Torvalds case KEYCTL_REVOKE: 11011da177e4SLinus Torvalds return keyctl_revoke_key((key_serial_t) arg2); 11021da177e4SLinus Torvalds 11031da177e4SLinus Torvalds case KEYCTL_DESCRIBE: 11041da177e4SLinus Torvalds return keyctl_describe_key((key_serial_t) arg2, 11051da177e4SLinus Torvalds (char __user *) arg3, 11061da177e4SLinus Torvalds (unsigned) arg4); 11071da177e4SLinus Torvalds 11081da177e4SLinus Torvalds case KEYCTL_CLEAR: 11091da177e4SLinus Torvalds return keyctl_keyring_clear((key_serial_t) arg2); 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds case KEYCTL_LINK: 11121da177e4SLinus Torvalds return keyctl_keyring_link((key_serial_t) arg2, 11131da177e4SLinus Torvalds (key_serial_t) arg3); 11141da177e4SLinus Torvalds 11151da177e4SLinus Torvalds case KEYCTL_UNLINK: 11161da177e4SLinus Torvalds return keyctl_keyring_unlink((key_serial_t) arg2, 11171da177e4SLinus Torvalds (key_serial_t) arg3); 11181da177e4SLinus Torvalds 11191da177e4SLinus Torvalds case KEYCTL_SEARCH: 11201da177e4SLinus Torvalds return keyctl_keyring_search((key_serial_t) arg2, 11211da177e4SLinus Torvalds (const char __user *) arg3, 11221da177e4SLinus Torvalds (const char __user *) arg4, 11231da177e4SLinus Torvalds (key_serial_t) arg5); 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds case KEYCTL_READ: 11261da177e4SLinus Torvalds return keyctl_read_key((key_serial_t) arg2, 11271da177e4SLinus Torvalds (char __user *) arg3, 11281da177e4SLinus Torvalds (size_t) arg4); 11291da177e4SLinus Torvalds 11301da177e4SLinus Torvalds case KEYCTL_CHOWN: 11311da177e4SLinus Torvalds return keyctl_chown_key((key_serial_t) arg2, 11321da177e4SLinus Torvalds (uid_t) arg3, 11331da177e4SLinus Torvalds (gid_t) arg4); 11341da177e4SLinus Torvalds 11351da177e4SLinus Torvalds case KEYCTL_SETPERM: 11361da177e4SLinus Torvalds return keyctl_setperm_key((key_serial_t) arg2, 11371da177e4SLinus Torvalds (key_perm_t) arg3); 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds case KEYCTL_INSTANTIATE: 11401da177e4SLinus Torvalds return keyctl_instantiate_key((key_serial_t) arg2, 11411da177e4SLinus Torvalds (const void __user *) arg3, 11421da177e4SLinus Torvalds (size_t) arg4, 11431da177e4SLinus Torvalds (key_serial_t) arg5); 11441da177e4SLinus Torvalds 11451da177e4SLinus Torvalds case KEYCTL_NEGATE: 11461da177e4SLinus Torvalds return keyctl_negate_key((key_serial_t) arg2, 11471da177e4SLinus Torvalds (unsigned) arg3, 11481da177e4SLinus Torvalds (key_serial_t) arg4); 11491da177e4SLinus Torvalds 11503e30148cSDavid Howells case KEYCTL_SET_REQKEY_KEYRING: 11513e30148cSDavid Howells return keyctl_set_reqkey_keyring(arg2); 11523e30148cSDavid Howells 1153017679c4SDavid Howells case KEYCTL_SET_TIMEOUT: 1154017679c4SDavid Howells return keyctl_set_timeout((key_serial_t) arg2, 1155017679c4SDavid Howells (unsigned) arg3); 1156017679c4SDavid Howells 1157b5f545c8SDavid Howells case KEYCTL_ASSUME_AUTHORITY: 1158b5f545c8SDavid Howells return keyctl_assume_authority((key_serial_t) arg2); 1159b5f545c8SDavid Howells 11601da177e4SLinus Torvalds default: 11611da177e4SLinus Torvalds return -EOPNOTSUPP; 11621da177e4SLinus Torvalds } 11631da177e4SLinus Torvalds 11641da177e4SLinus Torvalds } /* end sys_keyctl() */ 1165