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) */ 1065593122eSDavid Howells keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, 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) { 1885593122eSDavid Howells dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE, 1895593122eSDavid Howells KEY_WRITE); 190664cceb0SDavid Howells if (IS_ERR(dest_ref)) { 191664cceb0SDavid Howells ret = PTR_ERR(dest_ref); 1921da177e4SLinus Torvalds goto error3; 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds /* find the key type */ 1971da177e4SLinus Torvalds ktype = key_type_lookup(type); 1981da177e4SLinus Torvalds if (IS_ERR(ktype)) { 1991da177e4SLinus Torvalds ret = PTR_ERR(ktype); 2001da177e4SLinus Torvalds goto error4; 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds /* do the search */ 2044a38e122SDavid Howells key = request_key_and_link(ktype, description, callout_info, 2054a38e122SDavid Howells callout_len, NULL, key_ref_to_ptr(dest_ref), 2067e047ef5SDavid Howells KEY_ALLOC_IN_QUOTA); 2071da177e4SLinus Torvalds if (IS_ERR(key)) { 2081da177e4SLinus Torvalds ret = PTR_ERR(key); 2091da177e4SLinus Torvalds goto error5; 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds ret = key->serial; 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds key_put(key); 2151da177e4SLinus Torvalds error5: 2161da177e4SLinus Torvalds key_type_put(ktype); 2171da177e4SLinus Torvalds error4: 218664cceb0SDavid Howells key_ref_put(dest_ref); 2191da177e4SLinus Torvalds error3: 2201da177e4SLinus Torvalds kfree(callout_info); 2211da177e4SLinus Torvalds error2: 2221da177e4SLinus Torvalds kfree(description); 2231da177e4SLinus Torvalds error: 2241da177e4SLinus Torvalds return ret; 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds } /* end sys_request_key() */ 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds /*****************************************************************************/ 2291da177e4SLinus Torvalds /* 2301da177e4SLinus Torvalds * get the ID of the specified process keyring 2311da177e4SLinus Torvalds * - the keyring must have search permission to be found 2321da177e4SLinus Torvalds * - implements keyctl(KEYCTL_GET_KEYRING_ID) 2331da177e4SLinus Torvalds */ 2341da177e4SLinus Torvalds long keyctl_get_keyring_ID(key_serial_t id, int create) 2351da177e4SLinus Torvalds { 236664cceb0SDavid Howells key_ref_t key_ref; 2375593122eSDavid Howells unsigned long lflags; 2381da177e4SLinus Torvalds long ret; 2391da177e4SLinus Torvalds 2405593122eSDavid Howells lflags = create ? KEY_LOOKUP_CREATE : 0; 2415593122eSDavid Howells key_ref = lookup_user_key(id, lflags, KEY_SEARCH); 242664cceb0SDavid Howells if (IS_ERR(key_ref)) { 243664cceb0SDavid Howells ret = PTR_ERR(key_ref); 2441da177e4SLinus Torvalds goto error; 2451da177e4SLinus Torvalds } 2461da177e4SLinus Torvalds 247664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 248664cceb0SDavid Howells key_ref_put(key_ref); 2491da177e4SLinus Torvalds error: 2501da177e4SLinus Torvalds return ret; 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds } /* end keyctl_get_keyring_ID() */ 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds /*****************************************************************************/ 2551da177e4SLinus Torvalds /* 2561da177e4SLinus Torvalds * join the session keyring 2571da177e4SLinus Torvalds * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING) 2581da177e4SLinus Torvalds */ 2591da177e4SLinus Torvalds long keyctl_join_session_keyring(const char __user *_name) 2601da177e4SLinus Torvalds { 2611da177e4SLinus Torvalds char *name; 2620cb409d9SDavi Arnaut long ret; 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds /* fetch the name from userspace */ 2651da177e4SLinus Torvalds name = NULL; 2661da177e4SLinus Torvalds if (_name) { 2670cb409d9SDavi Arnaut name = strndup_user(_name, PAGE_SIZE); 2680cb409d9SDavi Arnaut if (IS_ERR(name)) { 2690cb409d9SDavi Arnaut ret = PTR_ERR(name); 2701da177e4SLinus Torvalds goto error; 2710cb409d9SDavi Arnaut } 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds /* join the session */ 2751da177e4SLinus Torvalds ret = join_session_keyring(name); 2760d54ee1cSVegard Nossum kfree(name); 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds error: 2791da177e4SLinus Torvalds return ret; 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds } /* end keyctl_join_session_keyring() */ 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds /*****************************************************************************/ 2841da177e4SLinus Torvalds /* 2851da177e4SLinus Torvalds * update a key's data payload 2861da177e4SLinus Torvalds * - the key must be writable 2871da177e4SLinus Torvalds * - implements keyctl(KEYCTL_UPDATE) 2881da177e4SLinus Torvalds */ 2891da177e4SLinus Torvalds long keyctl_update_key(key_serial_t id, 2901da177e4SLinus Torvalds const void __user *_payload, 2911da177e4SLinus Torvalds size_t plen) 2921da177e4SLinus Torvalds { 293664cceb0SDavid Howells key_ref_t key_ref; 2941da177e4SLinus Torvalds void *payload; 2951da177e4SLinus Torvalds long ret; 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds ret = -EINVAL; 2981da177e4SLinus Torvalds if (plen > PAGE_SIZE) 2991da177e4SLinus Torvalds goto error; 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 3021da177e4SLinus Torvalds payload = NULL; 3031da177e4SLinus Torvalds if (_payload) { 3041da177e4SLinus Torvalds ret = -ENOMEM; 3051da177e4SLinus Torvalds payload = kmalloc(plen, GFP_KERNEL); 3061da177e4SLinus Torvalds if (!payload) 3071da177e4SLinus Torvalds goto error; 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds ret = -EFAULT; 3101da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 3111da177e4SLinus Torvalds goto error2; 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds /* find the target key (which must be writable) */ 3155593122eSDavid Howells key_ref = lookup_user_key(id, 0, KEY_WRITE); 316664cceb0SDavid Howells if (IS_ERR(key_ref)) { 317664cceb0SDavid Howells ret = PTR_ERR(key_ref); 3181da177e4SLinus Torvalds goto error2; 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds /* update the key */ 322664cceb0SDavid Howells ret = key_update(key_ref, payload, plen); 3231da177e4SLinus Torvalds 324664cceb0SDavid Howells key_ref_put(key_ref); 3251da177e4SLinus Torvalds error2: 3261da177e4SLinus Torvalds kfree(payload); 3271da177e4SLinus Torvalds error: 3281da177e4SLinus Torvalds return ret; 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds } /* end keyctl_update_key() */ 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds /*****************************************************************************/ 3331da177e4SLinus Torvalds /* 3341da177e4SLinus Torvalds * revoke a key 3351da177e4SLinus Torvalds * - the key must be writable 3361da177e4SLinus Torvalds * - implements keyctl(KEYCTL_REVOKE) 3371da177e4SLinus Torvalds */ 3381da177e4SLinus Torvalds long keyctl_revoke_key(key_serial_t id) 3391da177e4SLinus Torvalds { 340664cceb0SDavid Howells key_ref_t key_ref; 3411da177e4SLinus Torvalds long ret; 3421da177e4SLinus Torvalds 3435593122eSDavid Howells key_ref = lookup_user_key(id, 0, KEY_WRITE); 344664cceb0SDavid Howells if (IS_ERR(key_ref)) { 345664cceb0SDavid Howells ret = PTR_ERR(key_ref); 3460c2c9a3fSDavid Howells if (ret != -EACCES) 3471da177e4SLinus Torvalds goto error; 3480c2c9a3fSDavid Howells key_ref = lookup_user_key(id, 0, KEY_SETATTR); 3490c2c9a3fSDavid Howells if (IS_ERR(key_ref)) { 3500c2c9a3fSDavid Howells ret = PTR_ERR(key_ref); 3510c2c9a3fSDavid Howells goto error; 3520c2c9a3fSDavid Howells } 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds 355664cceb0SDavid Howells key_revoke(key_ref_to_ptr(key_ref)); 3561da177e4SLinus Torvalds ret = 0; 3571da177e4SLinus Torvalds 358664cceb0SDavid Howells key_ref_put(key_ref); 3591da177e4SLinus Torvalds error: 3601260f801SDavid Howells return ret; 3611da177e4SLinus Torvalds 3621da177e4SLinus Torvalds } /* end keyctl_revoke_key() */ 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds /*****************************************************************************/ 3651da177e4SLinus Torvalds /* 3661da177e4SLinus Torvalds * clear the specified process keyring 3671da177e4SLinus Torvalds * - the keyring must be writable 3681da177e4SLinus Torvalds * - implements keyctl(KEYCTL_CLEAR) 3691da177e4SLinus Torvalds */ 3701da177e4SLinus Torvalds long keyctl_keyring_clear(key_serial_t ringid) 3711da177e4SLinus Torvalds { 372664cceb0SDavid Howells key_ref_t keyring_ref; 3731da177e4SLinus Torvalds long ret; 3741da177e4SLinus Torvalds 3755593122eSDavid Howells keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); 376664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 377664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 3781da177e4SLinus Torvalds goto error; 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds 381664cceb0SDavid Howells ret = keyring_clear(key_ref_to_ptr(keyring_ref)); 3821da177e4SLinus Torvalds 383664cceb0SDavid Howells key_ref_put(keyring_ref); 3841da177e4SLinus Torvalds error: 3851da177e4SLinus Torvalds return ret; 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds } /* end keyctl_keyring_clear() */ 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds /*****************************************************************************/ 3901da177e4SLinus Torvalds /* 3911da177e4SLinus Torvalds * link a key into a keyring 3921da177e4SLinus Torvalds * - the keyring must be writable 3931da177e4SLinus Torvalds * - the key must be linkable 3941da177e4SLinus Torvalds * - implements keyctl(KEYCTL_LINK) 3951da177e4SLinus Torvalds */ 3961da177e4SLinus Torvalds long keyctl_keyring_link(key_serial_t id, key_serial_t ringid) 3971da177e4SLinus Torvalds { 398664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 3991da177e4SLinus Torvalds long ret; 4001da177e4SLinus Torvalds 4015593122eSDavid Howells keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); 402664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 403664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 4041da177e4SLinus Torvalds goto error; 4051da177e4SLinus Torvalds } 4061da177e4SLinus Torvalds 4075593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_LINK); 408664cceb0SDavid Howells if (IS_ERR(key_ref)) { 409664cceb0SDavid Howells ret = PTR_ERR(key_ref); 4101da177e4SLinus Torvalds goto error2; 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds 413664cceb0SDavid Howells ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref)); 4141da177e4SLinus Torvalds 415664cceb0SDavid Howells key_ref_put(key_ref); 4161da177e4SLinus Torvalds error2: 417664cceb0SDavid Howells key_ref_put(keyring_ref); 4181da177e4SLinus Torvalds error: 4191da177e4SLinus Torvalds return ret; 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds } /* end keyctl_keyring_link() */ 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds /*****************************************************************************/ 4241da177e4SLinus Torvalds /* 4251da177e4SLinus Torvalds * unlink the first attachment of a key from a keyring 4261da177e4SLinus Torvalds * - the keyring must be writable 4271da177e4SLinus Torvalds * - we don't need any permissions on the key 4281da177e4SLinus Torvalds * - implements keyctl(KEYCTL_UNLINK) 4291da177e4SLinus Torvalds */ 4301da177e4SLinus Torvalds long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid) 4311da177e4SLinus Torvalds { 432664cceb0SDavid Howells key_ref_t keyring_ref, key_ref; 4331da177e4SLinus Torvalds long ret; 4341da177e4SLinus Torvalds 4355593122eSDavid Howells keyring_ref = lookup_user_key(ringid, 0, KEY_WRITE); 436664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 437664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 4381da177e4SLinus Torvalds goto error; 4391da177e4SLinus Torvalds } 4401da177e4SLinus Torvalds 4415593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0); 442664cceb0SDavid Howells if (IS_ERR(key_ref)) { 443664cceb0SDavid Howells ret = PTR_ERR(key_ref); 4441da177e4SLinus Torvalds goto error2; 4451da177e4SLinus Torvalds } 4461da177e4SLinus Torvalds 447664cceb0SDavid Howells ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref)); 4481da177e4SLinus Torvalds 449664cceb0SDavid Howells key_ref_put(key_ref); 4501da177e4SLinus Torvalds error2: 451664cceb0SDavid Howells key_ref_put(keyring_ref); 4521da177e4SLinus Torvalds error: 4531da177e4SLinus Torvalds return ret; 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds } /* end keyctl_keyring_unlink() */ 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds /*****************************************************************************/ 4581da177e4SLinus Torvalds /* 4591da177e4SLinus Torvalds * describe a user key 4601da177e4SLinus Torvalds * - the key must have view permission 4611da177e4SLinus Torvalds * - if there's a buffer, we place up to buflen bytes of data into it 4621da177e4SLinus Torvalds * - unless there's an error, we return the amount of description available, 4631da177e4SLinus Torvalds * irrespective of how much we may have copied 4641da177e4SLinus Torvalds * - the description is formatted thus: 4651da177e4SLinus Torvalds * type;uid;gid;perm;description<NUL> 4661da177e4SLinus Torvalds * - implements keyctl(KEYCTL_DESCRIBE) 4671da177e4SLinus Torvalds */ 4681da177e4SLinus Torvalds long keyctl_describe_key(key_serial_t keyid, 4691da177e4SLinus Torvalds char __user *buffer, 4701da177e4SLinus Torvalds size_t buflen) 4711da177e4SLinus Torvalds { 4723e30148cSDavid Howells struct key *key, *instkey; 473664cceb0SDavid Howells key_ref_t key_ref; 4741da177e4SLinus Torvalds char *tmpbuf; 4751da177e4SLinus Torvalds long ret; 4761da177e4SLinus Torvalds 4775593122eSDavid Howells key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW); 478664cceb0SDavid Howells if (IS_ERR(key_ref)) { 4793e30148cSDavid Howells /* viewing a key under construction is permitted if we have the 4803e30148cSDavid Howells * authorisation token handy */ 481664cceb0SDavid Howells if (PTR_ERR(key_ref) == -EACCES) { 4823e30148cSDavid Howells instkey = key_get_instantiation_authkey(keyid); 4833e30148cSDavid Howells if (!IS_ERR(instkey)) { 4843e30148cSDavid Howells key_put(instkey); 4858bbf4976SDavid Howells key_ref = lookup_user_key(keyid, 4865593122eSDavid Howells KEY_LOOKUP_PARTIAL, 4875593122eSDavid Howells 0); 488664cceb0SDavid Howells if (!IS_ERR(key_ref)) 4893e30148cSDavid Howells goto okay; 4903e30148cSDavid Howells } 4913e30148cSDavid Howells } 4923e30148cSDavid Howells 493664cceb0SDavid Howells ret = PTR_ERR(key_ref); 4941da177e4SLinus Torvalds goto error; 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds 4973e30148cSDavid Howells okay: 4981da177e4SLinus Torvalds /* calculate how much description we're going to return */ 4991da177e4SLinus Torvalds ret = -ENOMEM; 5001da177e4SLinus Torvalds tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); 5011da177e4SLinus Torvalds if (!tmpbuf) 5021da177e4SLinus Torvalds goto error2; 5031da177e4SLinus Torvalds 504664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 505664cceb0SDavid Howells 5061da177e4SLinus Torvalds ret = snprintf(tmpbuf, PAGE_SIZE - 1, 507664cceb0SDavid Howells "%s;%d;%d;%08x;%s", 508664cceb0SDavid Howells key_ref_to_ptr(key_ref)->type->name, 509664cceb0SDavid Howells key_ref_to_ptr(key_ref)->uid, 510664cceb0SDavid Howells key_ref_to_ptr(key_ref)->gid, 511664cceb0SDavid Howells key_ref_to_ptr(key_ref)->perm, 512664cceb0SDavid Howells key_ref_to_ptr(key_ref)->description ? 513664cceb0SDavid Howells key_ref_to_ptr(key_ref)->description : "" 5141da177e4SLinus Torvalds ); 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds /* include a NUL char at the end of the data */ 5171da177e4SLinus Torvalds if (ret > PAGE_SIZE - 1) 5181da177e4SLinus Torvalds ret = PAGE_SIZE - 1; 5191da177e4SLinus Torvalds tmpbuf[ret] = 0; 5201da177e4SLinus Torvalds ret++; 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds /* consider returning the data */ 5231da177e4SLinus Torvalds if (buffer && buflen > 0) { 5241da177e4SLinus Torvalds if (buflen > ret) 5251da177e4SLinus Torvalds buflen = ret; 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds if (copy_to_user(buffer, tmpbuf, buflen) != 0) 5281da177e4SLinus Torvalds ret = -EFAULT; 5291da177e4SLinus Torvalds } 5301da177e4SLinus Torvalds 5311da177e4SLinus Torvalds kfree(tmpbuf); 5321da177e4SLinus Torvalds error2: 533664cceb0SDavid Howells key_ref_put(key_ref); 5341da177e4SLinus Torvalds error: 5351da177e4SLinus Torvalds return ret; 5361da177e4SLinus Torvalds 5371da177e4SLinus Torvalds } /* end keyctl_describe_key() */ 5381da177e4SLinus Torvalds 5391da177e4SLinus Torvalds /*****************************************************************************/ 5401da177e4SLinus Torvalds /* 5411da177e4SLinus Torvalds * search the specified keyring for a matching key 5421da177e4SLinus Torvalds * - the start keyring must be searchable 5431da177e4SLinus Torvalds * - nested keyrings may also be searched if they are searchable 5441da177e4SLinus Torvalds * - only keys with search permission may be found 5451da177e4SLinus Torvalds * - if a key is found, it will be attached to the destination keyring if 5461da177e4SLinus Torvalds * there's one specified 5471da177e4SLinus Torvalds * - implements keyctl(KEYCTL_SEARCH) 5481da177e4SLinus Torvalds */ 5491da177e4SLinus Torvalds long keyctl_keyring_search(key_serial_t ringid, 5501da177e4SLinus Torvalds const char __user *_type, 5511da177e4SLinus Torvalds const char __user *_description, 5521da177e4SLinus Torvalds key_serial_t destringid) 5531da177e4SLinus Torvalds { 5541da177e4SLinus Torvalds struct key_type *ktype; 555664cceb0SDavid Howells key_ref_t keyring_ref, key_ref, dest_ref; 5561da177e4SLinus Torvalds char type[32], *description; 5570cb409d9SDavi Arnaut long ret; 5581da177e4SLinus Torvalds 5591da177e4SLinus Torvalds /* pull the type and description into kernel space */ 5600cb409d9SDavi Arnaut ret = key_get_type_from_user(type, _type, sizeof(type)); 5611da177e4SLinus Torvalds if (ret < 0) 5621da177e4SLinus Torvalds goto error; 5631da177e4SLinus Torvalds 5640cb409d9SDavi Arnaut description = strndup_user(_description, PAGE_SIZE); 5650cb409d9SDavi Arnaut if (IS_ERR(description)) { 5660cb409d9SDavi Arnaut ret = PTR_ERR(description); 5671da177e4SLinus Torvalds goto error; 5680cb409d9SDavi Arnaut } 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds /* get the keyring at which to begin the search */ 5715593122eSDavid Howells keyring_ref = lookup_user_key(ringid, 0, KEY_SEARCH); 572664cceb0SDavid Howells if (IS_ERR(keyring_ref)) { 573664cceb0SDavid Howells ret = PTR_ERR(keyring_ref); 5741da177e4SLinus Torvalds goto error2; 5751da177e4SLinus Torvalds } 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds /* get the destination keyring if specified */ 578664cceb0SDavid Howells dest_ref = NULL; 5791da177e4SLinus Torvalds if (destringid) { 5805593122eSDavid Howells dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE, 5815593122eSDavid Howells KEY_WRITE); 582664cceb0SDavid Howells if (IS_ERR(dest_ref)) { 583664cceb0SDavid Howells ret = PTR_ERR(dest_ref); 5841da177e4SLinus Torvalds goto error3; 5851da177e4SLinus Torvalds } 5861da177e4SLinus Torvalds } 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds /* find the key type */ 5891da177e4SLinus Torvalds ktype = key_type_lookup(type); 5901da177e4SLinus Torvalds if (IS_ERR(ktype)) { 5911da177e4SLinus Torvalds ret = PTR_ERR(ktype); 5921da177e4SLinus Torvalds goto error4; 5931da177e4SLinus Torvalds } 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds /* do the search */ 596664cceb0SDavid Howells key_ref = keyring_search(keyring_ref, ktype, description); 597664cceb0SDavid Howells if (IS_ERR(key_ref)) { 598664cceb0SDavid Howells ret = PTR_ERR(key_ref); 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds /* treat lack or presence of a negative key the same */ 6011da177e4SLinus Torvalds if (ret == -EAGAIN) 6021da177e4SLinus Torvalds ret = -ENOKEY; 6031da177e4SLinus Torvalds goto error5; 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds 6061da177e4SLinus Torvalds /* link the resulting key to the destination keyring if we can */ 607664cceb0SDavid Howells if (dest_ref) { 60829db9190SDavid Howells ret = key_permission(key_ref, KEY_LINK); 60929db9190SDavid Howells if (ret < 0) 6101da177e4SLinus Torvalds goto error6; 6111da177e4SLinus Torvalds 612664cceb0SDavid Howells ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref)); 6131da177e4SLinus Torvalds if (ret < 0) 6141da177e4SLinus Torvalds goto error6; 6151da177e4SLinus Torvalds } 6161da177e4SLinus Torvalds 617664cceb0SDavid Howells ret = key_ref_to_ptr(key_ref)->serial; 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds error6: 620664cceb0SDavid Howells key_ref_put(key_ref); 6211da177e4SLinus Torvalds error5: 6221da177e4SLinus Torvalds key_type_put(ktype); 6231da177e4SLinus Torvalds error4: 624664cceb0SDavid Howells key_ref_put(dest_ref); 6251da177e4SLinus Torvalds error3: 626664cceb0SDavid Howells key_ref_put(keyring_ref); 6271da177e4SLinus Torvalds error2: 6281da177e4SLinus Torvalds kfree(description); 6291da177e4SLinus Torvalds error: 6301da177e4SLinus Torvalds return ret; 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds } /* end keyctl_keyring_search() */ 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds /*****************************************************************************/ 6351da177e4SLinus Torvalds /* 6361da177e4SLinus Torvalds * read a user key's payload 6371da177e4SLinus Torvalds * - the keyring must be readable or the key must be searchable from the 6381da177e4SLinus Torvalds * process's keyrings 6391da177e4SLinus Torvalds * - if there's a buffer, we place up to buflen bytes of data into it 6401da177e4SLinus Torvalds * - unless there's an error, we return the amount of data in the key, 6411da177e4SLinus Torvalds * irrespective of how much we may have copied 6421da177e4SLinus Torvalds * - implements keyctl(KEYCTL_READ) 6431da177e4SLinus Torvalds */ 6441da177e4SLinus Torvalds long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen) 6451da177e4SLinus Torvalds { 646664cceb0SDavid Howells struct key *key; 647664cceb0SDavid Howells key_ref_t key_ref; 6481da177e4SLinus Torvalds long ret; 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds /* find the key first */ 6515593122eSDavid Howells key_ref = lookup_user_key(keyid, 0, 0); 652664cceb0SDavid Howells if (IS_ERR(key_ref)) { 653664cceb0SDavid Howells ret = -ENOKEY; 654664cceb0SDavid Howells goto error; 655664cceb0SDavid Howells } 656664cceb0SDavid Howells 657664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 658664cceb0SDavid Howells 6591da177e4SLinus Torvalds /* see if we can read it directly */ 66029db9190SDavid Howells ret = key_permission(key_ref, KEY_READ); 66129db9190SDavid Howells if (ret == 0) 6621da177e4SLinus Torvalds goto can_read_key; 66329db9190SDavid Howells if (ret != -EACCES) 66429db9190SDavid Howells goto error; 6651da177e4SLinus Torvalds 666664cceb0SDavid Howells /* we can't; see if it's searchable from this process's keyrings 6673e30148cSDavid Howells * - we automatically take account of the fact that it may be 6683e30148cSDavid Howells * dangling off an instantiation key 6693e30148cSDavid Howells */ 670664cceb0SDavid Howells if (!is_key_possessed(key_ref)) { 6711260f801SDavid Howells ret = -EACCES; 6721da177e4SLinus Torvalds goto error2; 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds /* the key is probably readable - now try to read it */ 6761da177e4SLinus Torvalds can_read_key: 6771da177e4SLinus Torvalds ret = key_validate(key); 6781da177e4SLinus Torvalds if (ret == 0) { 6791da177e4SLinus Torvalds ret = -EOPNOTSUPP; 6801da177e4SLinus Torvalds if (key->type->read) { 6811da177e4SLinus Torvalds /* read the data with the semaphore held (since we 6821da177e4SLinus Torvalds * might sleep) */ 6831da177e4SLinus Torvalds down_read(&key->sem); 6841da177e4SLinus Torvalds ret = key->type->read(key, buffer, buflen); 6851da177e4SLinus Torvalds up_read(&key->sem); 6861da177e4SLinus Torvalds } 6871da177e4SLinus Torvalds } 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds error2: 6901da177e4SLinus Torvalds key_put(key); 6911da177e4SLinus Torvalds error: 6921da177e4SLinus Torvalds return ret; 6931da177e4SLinus Torvalds 6941da177e4SLinus Torvalds } /* end keyctl_read_key() */ 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds /*****************************************************************************/ 6971da177e4SLinus Torvalds /* 6981da177e4SLinus Torvalds * change the ownership of a key 6991da177e4SLinus Torvalds * - the keyring owned by the changer 7001da177e4SLinus Torvalds * - if the uid or gid is -1, then that parameter is not changed 7011da177e4SLinus Torvalds * - implements keyctl(KEYCTL_CHOWN) 7021da177e4SLinus Torvalds */ 7031da177e4SLinus Torvalds long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid) 7041da177e4SLinus Torvalds { 7055801649dSFredrik Tolf struct key_user *newowner, *zapowner = NULL; 7061da177e4SLinus Torvalds struct key *key; 707664cceb0SDavid Howells key_ref_t key_ref; 7081da177e4SLinus Torvalds long ret; 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds ret = 0; 7111da177e4SLinus Torvalds if (uid == (uid_t) -1 && gid == (gid_t) -1) 7121da177e4SLinus Torvalds goto error; 7131da177e4SLinus Torvalds 7145593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 7155593122eSDavid Howells KEY_SETATTR); 716664cceb0SDavid Howells if (IS_ERR(key_ref)) { 717664cceb0SDavid Howells ret = PTR_ERR(key_ref); 7181da177e4SLinus Torvalds goto error; 7191da177e4SLinus Torvalds } 7201da177e4SLinus Torvalds 721664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 722664cceb0SDavid Howells 7231da177e4SLinus Torvalds /* make the changes with the locks held to prevent chown/chown races */ 7241da177e4SLinus Torvalds ret = -EACCES; 7251da177e4SLinus Torvalds down_write(&key->sem); 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) { 7281da177e4SLinus Torvalds /* only the sysadmin can chown a key to some other UID */ 7291da177e4SLinus Torvalds if (uid != (uid_t) -1 && key->uid != uid) 7305801649dSFredrik Tolf goto error_put; 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds /* only the sysadmin can set the key's GID to a group other 7331da177e4SLinus Torvalds * than one of those that the current process subscribes to */ 7341da177e4SLinus Torvalds if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid)) 7355801649dSFredrik Tolf goto error_put; 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds 7385801649dSFredrik Tolf /* change the UID */ 7391da177e4SLinus Torvalds if (uid != (uid_t) -1 && uid != key->uid) { 7405801649dSFredrik Tolf ret = -ENOMEM; 7411d1e9756SSerge E. Hallyn newowner = key_user_lookup(uid, current_user_ns()); 7425801649dSFredrik Tolf if (!newowner) 7435801649dSFredrik Tolf goto error_put; 7445801649dSFredrik Tolf 7455801649dSFredrik Tolf /* transfer the quota burden to the new user */ 7465801649dSFredrik Tolf if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { 7470b77f5bfSDavid Howells unsigned maxkeys = (uid == 0) ? 7480b77f5bfSDavid Howells key_quota_root_maxkeys : key_quota_maxkeys; 7490b77f5bfSDavid Howells unsigned maxbytes = (uid == 0) ? 7500b77f5bfSDavid Howells key_quota_root_maxbytes : key_quota_maxbytes; 7510b77f5bfSDavid Howells 7525801649dSFredrik Tolf spin_lock(&newowner->lock); 7530b77f5bfSDavid Howells if (newowner->qnkeys + 1 >= maxkeys || 7540b77f5bfSDavid Howells newowner->qnbytes + key->quotalen >= maxbytes || 7550b77f5bfSDavid Howells newowner->qnbytes + key->quotalen < 7560b77f5bfSDavid Howells newowner->qnbytes) 7575801649dSFredrik Tolf goto quota_overrun; 7585801649dSFredrik Tolf 7595801649dSFredrik Tolf newowner->qnkeys++; 7605801649dSFredrik Tolf newowner->qnbytes += key->quotalen; 7615801649dSFredrik Tolf spin_unlock(&newowner->lock); 7625801649dSFredrik Tolf 7635801649dSFredrik Tolf spin_lock(&key->user->lock); 7645801649dSFredrik Tolf key->user->qnkeys--; 7655801649dSFredrik Tolf key->user->qnbytes -= key->quotalen; 7665801649dSFredrik Tolf spin_unlock(&key->user->lock); 7675801649dSFredrik Tolf } 7685801649dSFredrik Tolf 7695801649dSFredrik Tolf atomic_dec(&key->user->nkeys); 7705801649dSFredrik Tolf atomic_inc(&newowner->nkeys); 7715801649dSFredrik Tolf 7725801649dSFredrik Tolf if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) { 7735801649dSFredrik Tolf atomic_dec(&key->user->nikeys); 7745801649dSFredrik Tolf atomic_inc(&newowner->nikeys); 7755801649dSFredrik Tolf } 7765801649dSFredrik Tolf 7775801649dSFredrik Tolf zapowner = key->user; 7785801649dSFredrik Tolf key->user = newowner; 7795801649dSFredrik Tolf key->uid = uid; 7801da177e4SLinus Torvalds } 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds /* change the GID */ 7831da177e4SLinus Torvalds if (gid != (gid_t) -1) 7841da177e4SLinus Torvalds key->gid = gid; 7851da177e4SLinus Torvalds 7861da177e4SLinus Torvalds ret = 0; 7871da177e4SLinus Torvalds 7885801649dSFredrik Tolf error_put: 7891da177e4SLinus Torvalds up_write(&key->sem); 7901da177e4SLinus Torvalds key_put(key); 7915801649dSFredrik Tolf if (zapowner) 7925801649dSFredrik Tolf key_user_put(zapowner); 7931da177e4SLinus Torvalds error: 7941da177e4SLinus Torvalds return ret; 7951da177e4SLinus Torvalds 7965801649dSFredrik Tolf quota_overrun: 7975801649dSFredrik Tolf spin_unlock(&newowner->lock); 7985801649dSFredrik Tolf zapowner = newowner; 7995801649dSFredrik Tolf ret = -EDQUOT; 8005801649dSFredrik Tolf goto error_put; 8015801649dSFredrik Tolf 8021da177e4SLinus Torvalds } /* end keyctl_chown_key() */ 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds /*****************************************************************************/ 8051da177e4SLinus Torvalds /* 8061da177e4SLinus Torvalds * change the permission mask on a key 8071da177e4SLinus Torvalds * - the keyring owned by the changer 8081da177e4SLinus Torvalds * - implements keyctl(KEYCTL_SETPERM) 8091da177e4SLinus Torvalds */ 8101da177e4SLinus Torvalds long keyctl_setperm_key(key_serial_t id, key_perm_t perm) 8111da177e4SLinus Torvalds { 8121da177e4SLinus Torvalds struct key *key; 813664cceb0SDavid Howells key_ref_t key_ref; 8141da177e4SLinus Torvalds long ret; 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds ret = -EINVAL; 817664cceb0SDavid Howells if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL)) 8181da177e4SLinus Torvalds goto error; 8191da177e4SLinus Torvalds 8205593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 8215593122eSDavid Howells KEY_SETATTR); 822664cceb0SDavid Howells if (IS_ERR(key_ref)) { 823664cceb0SDavid Howells ret = PTR_ERR(key_ref); 8241da177e4SLinus Torvalds goto error; 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds 827664cceb0SDavid Howells key = key_ref_to_ptr(key_ref); 828664cceb0SDavid Howells 82976d8aeabSDavid Howells /* make the changes with the locks held to prevent chown/chmod races */ 8301da177e4SLinus Torvalds ret = -EACCES; 8311da177e4SLinus Torvalds down_write(&key->sem); 8321da177e4SLinus Torvalds 83376d8aeabSDavid Howells /* if we're not the sysadmin, we can only change a key that we own */ 83447d804bfSDavid Howells if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) { 8351da177e4SLinus Torvalds key->perm = perm; 8361da177e4SLinus Torvalds ret = 0; 83776d8aeabSDavid Howells } 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds up_write(&key->sem); 8401da177e4SLinus Torvalds key_put(key); 8411da177e4SLinus Torvalds error: 8421da177e4SLinus Torvalds return ret; 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds } /* end keyctl_setperm_key() */ 8451da177e4SLinus Torvalds 8468bbf4976SDavid Howells /* 8478bbf4976SDavid Howells * get the destination keyring for instantiation 8488bbf4976SDavid Howells */ 8498bbf4976SDavid Howells static long get_instantiation_keyring(key_serial_t ringid, 8508bbf4976SDavid Howells struct request_key_auth *rka, 8518bbf4976SDavid Howells struct key **_dest_keyring) 8528bbf4976SDavid Howells { 8538bbf4976SDavid Howells key_ref_t dkref; 8548bbf4976SDavid Howells 8558bbf4976SDavid Howells *_dest_keyring = NULL; 856eca1bf5bSDavid Howells 857eca1bf5bSDavid Howells /* just return a NULL pointer if we weren't asked to make a link */ 858eca1bf5bSDavid Howells if (ringid == 0) 8598bbf4976SDavid Howells return 0; 8608bbf4976SDavid Howells 8618bbf4976SDavid Howells /* if a specific keyring is nominated by ID, then use that */ 8628bbf4976SDavid Howells if (ringid > 0) { 8635593122eSDavid Howells dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); 8648bbf4976SDavid Howells if (IS_ERR(dkref)) 8658bbf4976SDavid Howells return PTR_ERR(dkref); 8668bbf4976SDavid Howells *_dest_keyring = key_ref_to_ptr(dkref); 8678bbf4976SDavid Howells return 0; 8688bbf4976SDavid Howells } 8698bbf4976SDavid Howells 8708bbf4976SDavid Howells if (ringid == KEY_SPEC_REQKEY_AUTH_KEY) 8718bbf4976SDavid Howells return -EINVAL; 8728bbf4976SDavid Howells 8738bbf4976SDavid Howells /* otherwise specify the destination keyring recorded in the 8748bbf4976SDavid Howells * authorisation key (any KEY_SPEC_*_KEYRING) */ 8758bbf4976SDavid Howells if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) { 8768bbf4976SDavid Howells *_dest_keyring = rka->dest_keyring; 8778bbf4976SDavid Howells return 0; 8788bbf4976SDavid Howells } 8798bbf4976SDavid Howells 8808bbf4976SDavid Howells return -ENOKEY; 8818bbf4976SDavid Howells } 8828bbf4976SDavid Howells 883d84f4f99SDavid Howells /* 884d84f4f99SDavid Howells * change the request_key authorisation key on the current process 885d84f4f99SDavid Howells */ 886d84f4f99SDavid Howells static int keyctl_change_reqkey_auth(struct key *key) 887d84f4f99SDavid Howells { 888d84f4f99SDavid Howells struct cred *new; 889d84f4f99SDavid Howells 890d84f4f99SDavid Howells new = prepare_creds(); 891d84f4f99SDavid Howells if (!new) 892d84f4f99SDavid Howells return -ENOMEM; 893d84f4f99SDavid Howells 894d84f4f99SDavid Howells key_put(new->request_key_auth); 895d84f4f99SDavid Howells new->request_key_auth = key_get(key); 896d84f4f99SDavid Howells 897d84f4f99SDavid Howells return commit_creds(new); 898d84f4f99SDavid Howells } 899d84f4f99SDavid Howells 9001da177e4SLinus Torvalds /*****************************************************************************/ 9011da177e4SLinus Torvalds /* 9021da177e4SLinus Torvalds * instantiate the key with the specified payload, and, if one is given, link 9031da177e4SLinus Torvalds * the key into the keyring 9041da177e4SLinus Torvalds */ 9051da177e4SLinus Torvalds long keyctl_instantiate_key(key_serial_t id, 9061da177e4SLinus Torvalds const void __user *_payload, 9071da177e4SLinus Torvalds size_t plen, 9081da177e4SLinus Torvalds key_serial_t ringid) 9091da177e4SLinus Torvalds { 910d84f4f99SDavid Howells const struct cred *cred = current_cred(); 9113e30148cSDavid Howells struct request_key_auth *rka; 9128bbf4976SDavid Howells struct key *instkey, *dest_keyring; 9131da177e4SLinus Torvalds void *payload; 9141da177e4SLinus Torvalds long ret; 91538bbca6bSDavid Howells bool vm = false; 9161da177e4SLinus Torvalds 917d84f4f99SDavid Howells kenter("%d,,%zu,%d", id, plen, ringid); 918d84f4f99SDavid Howells 9191da177e4SLinus Torvalds ret = -EINVAL; 92038bbca6bSDavid Howells if (plen > 1024 * 1024 - 1) 9211da177e4SLinus Torvalds goto error; 9221da177e4SLinus Torvalds 923b5f545c8SDavid Howells /* the appropriate instantiation authorisation key must have been 924b5f545c8SDavid Howells * assumed before calling this */ 925b5f545c8SDavid Howells ret = -EPERM; 926d84f4f99SDavid Howells instkey = cred->request_key_auth; 927b5f545c8SDavid Howells if (!instkey) 928b5f545c8SDavid Howells goto error; 929b5f545c8SDavid Howells 930b5f545c8SDavid Howells rka = instkey->payload.data; 931b5f545c8SDavid Howells if (rka->target_key->serial != id) 932b5f545c8SDavid Howells goto error; 933b5f545c8SDavid Howells 9341da177e4SLinus Torvalds /* pull the payload in if one was supplied */ 9351da177e4SLinus Torvalds payload = NULL; 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds if (_payload) { 9381da177e4SLinus Torvalds ret = -ENOMEM; 9391da177e4SLinus Torvalds payload = kmalloc(plen, GFP_KERNEL); 94038bbca6bSDavid Howells if (!payload) { 94138bbca6bSDavid Howells if (plen <= PAGE_SIZE) 94238bbca6bSDavid Howells goto error; 94338bbca6bSDavid Howells vm = true; 94438bbca6bSDavid Howells payload = vmalloc(plen); 9451da177e4SLinus Torvalds if (!payload) 9461da177e4SLinus Torvalds goto error; 94738bbca6bSDavid Howells } 9481da177e4SLinus Torvalds 9491da177e4SLinus Torvalds ret = -EFAULT; 9501da177e4SLinus Torvalds if (copy_from_user(payload, _payload, plen) != 0) 9511da177e4SLinus Torvalds goto error2; 9521da177e4SLinus Torvalds } 9531da177e4SLinus Torvalds 9543e30148cSDavid Howells /* find the destination keyring amongst those belonging to the 9553e30148cSDavid Howells * requesting task */ 9568bbf4976SDavid Howells ret = get_instantiation_keyring(ringid, rka, &dest_keyring); 9578bbf4976SDavid Howells if (ret < 0) 958b5f545c8SDavid Howells goto error2; 9591da177e4SLinus Torvalds 9601da177e4SLinus Torvalds /* instantiate the key and link it into a keyring */ 9613e30148cSDavid Howells ret = key_instantiate_and_link(rka->target_key, payload, plen, 9628bbf4976SDavid Howells dest_keyring, instkey); 9631da177e4SLinus Torvalds 9648bbf4976SDavid Howells key_put(dest_keyring); 965b5f545c8SDavid Howells 966b5f545c8SDavid Howells /* discard the assumed authority if it's just been disabled by 967b5f545c8SDavid Howells * instantiation of the key */ 968d84f4f99SDavid Howells if (ret == 0) 969d84f4f99SDavid Howells keyctl_change_reqkey_auth(NULL); 970b5f545c8SDavid Howells 9711da177e4SLinus Torvalds error2: 97238bbca6bSDavid Howells if (!vm) 9731da177e4SLinus Torvalds kfree(payload); 97438bbca6bSDavid Howells else 97538bbca6bSDavid Howells vfree(payload); 9761da177e4SLinus Torvalds error: 9771da177e4SLinus Torvalds return ret; 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds } /* end keyctl_instantiate_key() */ 9801da177e4SLinus Torvalds 9811da177e4SLinus Torvalds /*****************************************************************************/ 9821da177e4SLinus Torvalds /* 9831da177e4SLinus Torvalds * negatively instantiate the key with the given timeout (in seconds), and, if 9841da177e4SLinus Torvalds * one is given, link the key into the keyring 9851da177e4SLinus Torvalds */ 9861da177e4SLinus Torvalds long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid) 9871da177e4SLinus Torvalds { 988d84f4f99SDavid Howells const struct cred *cred = current_cred(); 9893e30148cSDavid Howells struct request_key_auth *rka; 9908bbf4976SDavid Howells struct key *instkey, *dest_keyring; 9911da177e4SLinus Torvalds long ret; 9921da177e4SLinus Torvalds 993d84f4f99SDavid Howells kenter("%d,%u,%d", id, timeout, ringid); 994d84f4f99SDavid Howells 995b5f545c8SDavid Howells /* the appropriate instantiation authorisation key must have been 996b5f545c8SDavid Howells * assumed before calling this */ 997b5f545c8SDavid Howells ret = -EPERM; 998d84f4f99SDavid Howells instkey = cred->request_key_auth; 999b5f545c8SDavid Howells if (!instkey) 10001da177e4SLinus Torvalds goto error; 10011da177e4SLinus Torvalds 10023e30148cSDavid Howells rka = instkey->payload.data; 1003b5f545c8SDavid Howells if (rka->target_key->serial != id) 1004b5f545c8SDavid Howells goto error; 10053e30148cSDavid Howells 10061da177e4SLinus Torvalds /* find the destination keyring if present (which must also be 10071da177e4SLinus Torvalds * writable) */ 10088bbf4976SDavid Howells ret = get_instantiation_keyring(ringid, rka, &dest_keyring); 10098bbf4976SDavid Howells if (ret < 0) 1010b5f545c8SDavid Howells goto error; 10111da177e4SLinus Torvalds 10121da177e4SLinus Torvalds /* instantiate the key and link it into a keyring */ 1013664cceb0SDavid Howells ret = key_negate_and_link(rka->target_key, timeout, 10148bbf4976SDavid Howells dest_keyring, instkey); 10151da177e4SLinus Torvalds 10168bbf4976SDavid Howells key_put(dest_keyring); 1017b5f545c8SDavid Howells 1018b5f545c8SDavid Howells /* discard the assumed authority if it's just been disabled by 1019b5f545c8SDavid Howells * instantiation of the key */ 1020d84f4f99SDavid Howells if (ret == 0) 1021d84f4f99SDavid Howells keyctl_change_reqkey_auth(NULL); 1022b5f545c8SDavid Howells 10231da177e4SLinus Torvalds error: 10241da177e4SLinus Torvalds return ret; 10251da177e4SLinus Torvalds 10261da177e4SLinus Torvalds } /* end keyctl_negate_key() */ 10271da177e4SLinus Torvalds 10281da177e4SLinus Torvalds /*****************************************************************************/ 10291da177e4SLinus Torvalds /* 10303e30148cSDavid Howells * set the default keyring in which request_key() will cache keys 10313e30148cSDavid Howells * - return the old setting 10323e30148cSDavid Howells */ 10333e30148cSDavid Howells long keyctl_set_reqkey_keyring(int reqkey_defl) 10343e30148cSDavid Howells { 1035d84f4f99SDavid Howells struct cred *new; 1036d84f4f99SDavid Howells int ret, old_setting; 1037d84f4f99SDavid Howells 1038d84f4f99SDavid Howells old_setting = current_cred_xxx(jit_keyring); 1039d84f4f99SDavid Howells 1040d84f4f99SDavid Howells if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE) 1041d84f4f99SDavid Howells return old_setting; 1042d84f4f99SDavid Howells 1043d84f4f99SDavid Howells new = prepare_creds(); 1044d84f4f99SDavid Howells if (!new) 1045d84f4f99SDavid Howells return -ENOMEM; 10463e30148cSDavid Howells 10473e30148cSDavid Howells switch (reqkey_defl) { 10483e30148cSDavid Howells case KEY_REQKEY_DEFL_THREAD_KEYRING: 1049d84f4f99SDavid Howells ret = install_thread_keyring_to_cred(new); 10503e30148cSDavid Howells if (ret < 0) 1051d84f4f99SDavid Howells goto error; 10523e30148cSDavid Howells goto set; 10533e30148cSDavid Howells 10543e30148cSDavid Howells case KEY_REQKEY_DEFL_PROCESS_KEYRING: 1055d84f4f99SDavid Howells ret = install_process_keyring_to_cred(new); 1056d84f4f99SDavid Howells if (ret < 0) { 1057d84f4f99SDavid Howells if (ret != -EEXIST) 1058d84f4f99SDavid Howells goto error; 1059d84f4f99SDavid Howells ret = 0; 1060d84f4f99SDavid Howells } 1061d84f4f99SDavid Howells goto set; 10623e30148cSDavid Howells 10633e30148cSDavid Howells case KEY_REQKEY_DEFL_DEFAULT: 10643e30148cSDavid Howells case KEY_REQKEY_DEFL_SESSION_KEYRING: 10653e30148cSDavid Howells case KEY_REQKEY_DEFL_USER_KEYRING: 10663e30148cSDavid Howells case KEY_REQKEY_DEFL_USER_SESSION_KEYRING: 1067d84f4f99SDavid Howells case KEY_REQKEY_DEFL_REQUESTOR_KEYRING: 1068d84f4f99SDavid Howells goto set; 10693e30148cSDavid Howells 10703e30148cSDavid Howells case KEY_REQKEY_DEFL_NO_CHANGE: 10713e30148cSDavid Howells case KEY_REQKEY_DEFL_GROUP_KEYRING: 10723e30148cSDavid Howells default: 1073d84f4f99SDavid Howells ret = -EINVAL; 1074d84f4f99SDavid Howells goto error; 10753e30148cSDavid Howells } 10763e30148cSDavid Howells 1077d84f4f99SDavid Howells set: 1078d84f4f99SDavid Howells new->jit_keyring = reqkey_defl; 1079d84f4f99SDavid Howells commit_creds(new); 1080d84f4f99SDavid Howells return old_setting; 1081d84f4f99SDavid Howells error: 1082d84f4f99SDavid Howells abort_creds(new); 1083d84f4f99SDavid Howells return -EINVAL; 1084d84f4f99SDavid Howells 10853e30148cSDavid Howells } /* end keyctl_set_reqkey_keyring() */ 10863e30148cSDavid Howells 10873e30148cSDavid Howells /*****************************************************************************/ 10883e30148cSDavid Howells /* 1089017679c4SDavid Howells * set or clear the timeout for a key 1090017679c4SDavid Howells */ 1091017679c4SDavid Howells long keyctl_set_timeout(key_serial_t id, unsigned timeout) 1092017679c4SDavid Howells { 1093017679c4SDavid Howells struct timespec now; 1094017679c4SDavid Howells struct key *key; 1095017679c4SDavid Howells key_ref_t key_ref; 1096017679c4SDavid Howells time_t expiry; 1097017679c4SDavid Howells long ret; 1098017679c4SDavid Howells 10995593122eSDavid Howells key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 11005593122eSDavid Howells KEY_SETATTR); 1101017679c4SDavid Howells if (IS_ERR(key_ref)) { 1102017679c4SDavid Howells ret = PTR_ERR(key_ref); 1103017679c4SDavid Howells goto error; 1104017679c4SDavid Howells } 1105017679c4SDavid Howells 1106017679c4SDavid Howells key = key_ref_to_ptr(key_ref); 1107017679c4SDavid Howells 1108017679c4SDavid Howells /* make the changes with the locks held to prevent races */ 1109017679c4SDavid Howells down_write(&key->sem); 1110017679c4SDavid Howells 1111017679c4SDavid Howells expiry = 0; 1112017679c4SDavid Howells if (timeout > 0) { 1113017679c4SDavid Howells now = current_kernel_time(); 1114017679c4SDavid Howells expiry = now.tv_sec + timeout; 1115017679c4SDavid Howells } 1116017679c4SDavid Howells 1117017679c4SDavid Howells key->expiry = expiry; 11185d135440SDavid Howells key_schedule_gc(key->expiry); 1119017679c4SDavid Howells 1120017679c4SDavid Howells up_write(&key->sem); 1121017679c4SDavid Howells key_put(key); 1122017679c4SDavid Howells 1123017679c4SDavid Howells ret = 0; 1124017679c4SDavid Howells error: 1125017679c4SDavid Howells return ret; 1126017679c4SDavid Howells 1127017679c4SDavid Howells } /* end keyctl_set_timeout() */ 1128017679c4SDavid Howells 1129017679c4SDavid Howells /*****************************************************************************/ 1130017679c4SDavid Howells /* 1131b5f545c8SDavid Howells * assume the authority to instantiate the specified key 1132b5f545c8SDavid Howells */ 1133b5f545c8SDavid Howells long keyctl_assume_authority(key_serial_t id) 1134b5f545c8SDavid Howells { 1135b5f545c8SDavid Howells struct key *authkey; 1136b5f545c8SDavid Howells long ret; 1137b5f545c8SDavid Howells 1138b5f545c8SDavid Howells /* special key IDs aren't permitted */ 1139b5f545c8SDavid Howells ret = -EINVAL; 1140b5f545c8SDavid Howells if (id < 0) 1141b5f545c8SDavid Howells goto error; 1142b5f545c8SDavid Howells 1143b5f545c8SDavid Howells /* we divest ourselves of authority if given an ID of 0 */ 1144b5f545c8SDavid Howells if (id == 0) { 1145d84f4f99SDavid Howells ret = keyctl_change_reqkey_auth(NULL); 1146b5f545c8SDavid Howells goto error; 1147b5f545c8SDavid Howells } 1148b5f545c8SDavid Howells 1149b5f545c8SDavid Howells /* attempt to assume the authority temporarily granted to us whilst we 1150b5f545c8SDavid Howells * instantiate the specified key 1151b5f545c8SDavid Howells * - the authorisation key must be in the current task's keyrings 1152b5f545c8SDavid Howells * somewhere 1153b5f545c8SDavid Howells */ 1154b5f545c8SDavid Howells authkey = key_get_instantiation_authkey(id); 1155b5f545c8SDavid Howells if (IS_ERR(authkey)) { 1156b5f545c8SDavid Howells ret = PTR_ERR(authkey); 1157b5f545c8SDavid Howells goto error; 1158b5f545c8SDavid Howells } 1159b5f545c8SDavid Howells 1160d84f4f99SDavid Howells ret = keyctl_change_reqkey_auth(authkey); 1161d84f4f99SDavid Howells if (ret < 0) 1162d84f4f99SDavid Howells goto error; 1163d84f4f99SDavid Howells key_put(authkey); 1164b5f545c8SDavid Howells 1165d84f4f99SDavid Howells ret = authkey->serial; 1166b5f545c8SDavid Howells error: 1167b5f545c8SDavid Howells return ret; 1168b5f545c8SDavid Howells 1169b5f545c8SDavid Howells } /* end keyctl_assume_authority() */ 1170b5f545c8SDavid Howells 117170a5bb72SDavid Howells /* 117270a5bb72SDavid Howells * get the security label of a key 117370a5bb72SDavid Howells * - the key must grant us view permission 117470a5bb72SDavid Howells * - if there's a buffer, we place up to buflen bytes of data into it 117570a5bb72SDavid Howells * - unless there's an error, we return the amount of information available, 117670a5bb72SDavid Howells * irrespective of how much we may have copied (including the terminal NUL) 117770a5bb72SDavid Howells * - implements keyctl(KEYCTL_GET_SECURITY) 117870a5bb72SDavid Howells */ 117970a5bb72SDavid Howells long keyctl_get_security(key_serial_t keyid, 118070a5bb72SDavid Howells char __user *buffer, 118170a5bb72SDavid Howells size_t buflen) 118270a5bb72SDavid Howells { 118370a5bb72SDavid Howells struct key *key, *instkey; 118470a5bb72SDavid Howells key_ref_t key_ref; 118570a5bb72SDavid Howells char *context; 118670a5bb72SDavid Howells long ret; 118770a5bb72SDavid Howells 11885593122eSDavid Howells key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW); 118970a5bb72SDavid Howells if (IS_ERR(key_ref)) { 119070a5bb72SDavid Howells if (PTR_ERR(key_ref) != -EACCES) 119170a5bb72SDavid Howells return PTR_ERR(key_ref); 119270a5bb72SDavid Howells 119370a5bb72SDavid Howells /* viewing a key under construction is also permitted if we 119470a5bb72SDavid Howells * have the authorisation token handy */ 119570a5bb72SDavid Howells instkey = key_get_instantiation_authkey(keyid); 119670a5bb72SDavid Howells if (IS_ERR(instkey)) 119770a5bb72SDavid Howells return PTR_ERR(key_ref); 119870a5bb72SDavid Howells key_put(instkey); 119970a5bb72SDavid Howells 12005593122eSDavid Howells key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0); 120170a5bb72SDavid Howells if (IS_ERR(key_ref)) 120270a5bb72SDavid Howells return PTR_ERR(key_ref); 120370a5bb72SDavid Howells } 120470a5bb72SDavid Howells 120570a5bb72SDavid Howells key = key_ref_to_ptr(key_ref); 120670a5bb72SDavid Howells ret = security_key_getsecurity(key, &context); 120770a5bb72SDavid Howells if (ret == 0) { 120870a5bb72SDavid Howells /* if no information was returned, give userspace an empty 120970a5bb72SDavid Howells * string */ 121070a5bb72SDavid Howells ret = 1; 121170a5bb72SDavid Howells if (buffer && buflen > 0 && 121270a5bb72SDavid Howells copy_to_user(buffer, "", 1) != 0) 121370a5bb72SDavid Howells ret = -EFAULT; 121470a5bb72SDavid Howells } else if (ret > 0) { 121570a5bb72SDavid Howells /* return as much data as there's room for */ 121670a5bb72SDavid Howells if (buffer && buflen > 0) { 121770a5bb72SDavid Howells if (buflen > ret) 121870a5bb72SDavid Howells buflen = ret; 121970a5bb72SDavid Howells 122070a5bb72SDavid Howells if (copy_to_user(buffer, context, buflen) != 0) 122170a5bb72SDavid Howells ret = -EFAULT; 122270a5bb72SDavid Howells } 122370a5bb72SDavid Howells 122470a5bb72SDavid Howells kfree(context); 122570a5bb72SDavid Howells } 122670a5bb72SDavid Howells 122770a5bb72SDavid Howells key_ref_put(key_ref); 122870a5bb72SDavid Howells return ret; 122970a5bb72SDavid Howells } 123070a5bb72SDavid Howells 1231ee18d64cSDavid Howells /* 1232ee18d64cSDavid Howells * attempt to install the calling process's session keyring on the process's 1233ee18d64cSDavid Howells * parent process 1234ee18d64cSDavid Howells * - the keyring must exist and must grant us LINK permission 1235ee18d64cSDavid Howells * - implements keyctl(KEYCTL_SESSION_TO_PARENT) 1236ee18d64cSDavid Howells */ 1237ee18d64cSDavid Howells long keyctl_session_to_parent(void) 1238ee18d64cSDavid Howells { 1239ee18d64cSDavid Howells struct task_struct *me, *parent; 1240ee18d64cSDavid Howells const struct cred *mycred, *pcred; 1241ee18d64cSDavid Howells struct cred *cred, *oldcred; 1242ee18d64cSDavid Howells key_ref_t keyring_r; 1243ee18d64cSDavid Howells int ret; 1244ee18d64cSDavid Howells 1245ee18d64cSDavid Howells keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_LINK); 1246ee18d64cSDavid Howells if (IS_ERR(keyring_r)) 1247ee18d64cSDavid Howells return PTR_ERR(keyring_r); 1248ee18d64cSDavid Howells 1249ee18d64cSDavid Howells /* our parent is going to need a new cred struct, a new tgcred struct 1250ee18d64cSDavid Howells * and new security data, so we allocate them here to prevent ENOMEM in 1251ee18d64cSDavid Howells * our parent */ 1252ee18d64cSDavid Howells ret = -ENOMEM; 1253ee18d64cSDavid Howells cred = cred_alloc_blank(); 1254ee18d64cSDavid Howells if (!cred) 1255ee18d64cSDavid Howells goto error_keyring; 1256ee18d64cSDavid Howells 1257ee18d64cSDavid Howells cred->tgcred->session_keyring = key_ref_to_ptr(keyring_r); 1258ee18d64cSDavid Howells keyring_r = NULL; 1259ee18d64cSDavid Howells 1260ee18d64cSDavid Howells me = current; 1261ee18d64cSDavid Howells write_lock_irq(&tasklist_lock); 1262ee18d64cSDavid Howells 1263ee18d64cSDavid Howells parent = me->real_parent; 1264ee18d64cSDavid Howells ret = -EPERM; 1265ee18d64cSDavid Howells 1266ee18d64cSDavid Howells /* the parent mustn't be init and mustn't be a kernel thread */ 1267ee18d64cSDavid Howells if (parent->pid <= 1 || !parent->mm) 1268ee18d64cSDavid Howells goto not_permitted; 1269ee18d64cSDavid Howells 1270ee18d64cSDavid Howells /* the parent must be single threaded */ 1271ee18d64cSDavid Howells if (atomic_read(&parent->signal->count) != 1) 1272ee18d64cSDavid Howells goto not_permitted; 1273ee18d64cSDavid Howells 1274ee18d64cSDavid Howells /* the parent and the child must have different session keyrings or 1275ee18d64cSDavid Howells * there's no point */ 1276ee18d64cSDavid Howells mycred = current_cred(); 1277ee18d64cSDavid Howells pcred = __task_cred(parent); 1278ee18d64cSDavid Howells if (mycred == pcred || 1279ee18d64cSDavid Howells mycred->tgcred->session_keyring == pcred->tgcred->session_keyring) 1280ee18d64cSDavid Howells goto already_same; 1281ee18d64cSDavid Howells 1282ee18d64cSDavid Howells /* the parent must have the same effective ownership and mustn't be 1283ee18d64cSDavid Howells * SUID/SGID */ 1284ee18d64cSDavid Howells if (pcred-> uid != mycred->euid || 1285ee18d64cSDavid Howells pcred->euid != mycred->euid || 1286ee18d64cSDavid Howells pcred->suid != mycred->euid || 1287ee18d64cSDavid Howells pcred-> gid != mycred->egid || 1288ee18d64cSDavid Howells pcred->egid != mycred->egid || 1289ee18d64cSDavid Howells pcred->sgid != mycred->egid) 1290ee18d64cSDavid Howells goto not_permitted; 1291ee18d64cSDavid Howells 1292ee18d64cSDavid Howells /* the keyrings must have the same UID */ 1293ee18d64cSDavid Howells if (pcred ->tgcred->session_keyring->uid != mycred->euid || 1294ee18d64cSDavid Howells mycred->tgcred->session_keyring->uid != mycred->euid) 1295ee18d64cSDavid Howells goto not_permitted; 1296ee18d64cSDavid Howells 1297ee18d64cSDavid Howells /* the LSM must permit the replacement of the parent's keyring with the 1298ee18d64cSDavid Howells * keyring from this process */ 1299ee18d64cSDavid Howells ret = security_key_session_to_parent(mycred, pcred, 1300ee18d64cSDavid Howells key_ref_to_ptr(keyring_r)); 1301ee18d64cSDavid Howells if (ret < 0) 1302ee18d64cSDavid Howells goto not_permitted; 1303ee18d64cSDavid Howells 1304ee18d64cSDavid Howells /* if there's an already pending keyring replacement, then we replace 1305ee18d64cSDavid Howells * that */ 1306ee18d64cSDavid Howells oldcred = parent->replacement_session_keyring; 1307ee18d64cSDavid Howells 1308ee18d64cSDavid Howells /* the replacement session keyring is applied just prior to userspace 1309ee18d64cSDavid Howells * restarting */ 1310ee18d64cSDavid Howells parent->replacement_session_keyring = cred; 1311ee18d64cSDavid Howells cred = NULL; 1312ee18d64cSDavid Howells set_ti_thread_flag(task_thread_info(parent), TIF_NOTIFY_RESUME); 1313ee18d64cSDavid Howells 1314ee18d64cSDavid Howells write_unlock_irq(&tasklist_lock); 1315ee18d64cSDavid Howells if (oldcred) 1316ee18d64cSDavid Howells put_cred(oldcred); 1317ee18d64cSDavid Howells return 0; 1318ee18d64cSDavid Howells 1319ee18d64cSDavid Howells already_same: 1320ee18d64cSDavid Howells ret = 0; 1321ee18d64cSDavid Howells not_permitted: 1322*5c84342aSMarc Dionne write_unlock_irq(&tasklist_lock); 1323ee18d64cSDavid Howells put_cred(cred); 1324ee18d64cSDavid Howells return ret; 1325ee18d64cSDavid Howells 1326ee18d64cSDavid Howells error_keyring: 1327ee18d64cSDavid Howells key_ref_put(keyring_r); 1328ee18d64cSDavid Howells return ret; 1329ee18d64cSDavid Howells } 1330ee18d64cSDavid Howells 1331b5f545c8SDavid Howells /*****************************************************************************/ 1332b5f545c8SDavid Howells /* 13331da177e4SLinus Torvalds * the key control system call 13341da177e4SLinus Torvalds */ 1335938bb9f5SHeiko Carstens SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3, 1336938bb9f5SHeiko Carstens unsigned long, arg4, unsigned long, arg5) 13371da177e4SLinus Torvalds { 13381da177e4SLinus Torvalds switch (option) { 13391da177e4SLinus Torvalds case KEYCTL_GET_KEYRING_ID: 13401da177e4SLinus Torvalds return keyctl_get_keyring_ID((key_serial_t) arg2, 13411da177e4SLinus Torvalds (int) arg3); 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds case KEYCTL_JOIN_SESSION_KEYRING: 13441da177e4SLinus Torvalds return keyctl_join_session_keyring((const char __user *) arg2); 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds case KEYCTL_UPDATE: 13471da177e4SLinus Torvalds return keyctl_update_key((key_serial_t) arg2, 13481da177e4SLinus Torvalds (const void __user *) arg3, 13491da177e4SLinus Torvalds (size_t) arg4); 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds case KEYCTL_REVOKE: 13521da177e4SLinus Torvalds return keyctl_revoke_key((key_serial_t) arg2); 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds case KEYCTL_DESCRIBE: 13551da177e4SLinus Torvalds return keyctl_describe_key((key_serial_t) arg2, 13561da177e4SLinus Torvalds (char __user *) arg3, 13571da177e4SLinus Torvalds (unsigned) arg4); 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds case KEYCTL_CLEAR: 13601da177e4SLinus Torvalds return keyctl_keyring_clear((key_serial_t) arg2); 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds case KEYCTL_LINK: 13631da177e4SLinus Torvalds return keyctl_keyring_link((key_serial_t) arg2, 13641da177e4SLinus Torvalds (key_serial_t) arg3); 13651da177e4SLinus Torvalds 13661da177e4SLinus Torvalds case KEYCTL_UNLINK: 13671da177e4SLinus Torvalds return keyctl_keyring_unlink((key_serial_t) arg2, 13681da177e4SLinus Torvalds (key_serial_t) arg3); 13691da177e4SLinus Torvalds 13701da177e4SLinus Torvalds case KEYCTL_SEARCH: 13711da177e4SLinus Torvalds return keyctl_keyring_search((key_serial_t) arg2, 13721da177e4SLinus Torvalds (const char __user *) arg3, 13731da177e4SLinus Torvalds (const char __user *) arg4, 13741da177e4SLinus Torvalds (key_serial_t) arg5); 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds case KEYCTL_READ: 13771da177e4SLinus Torvalds return keyctl_read_key((key_serial_t) arg2, 13781da177e4SLinus Torvalds (char __user *) arg3, 13791da177e4SLinus Torvalds (size_t) arg4); 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds case KEYCTL_CHOWN: 13821da177e4SLinus Torvalds return keyctl_chown_key((key_serial_t) arg2, 13831da177e4SLinus Torvalds (uid_t) arg3, 13841da177e4SLinus Torvalds (gid_t) arg4); 13851da177e4SLinus Torvalds 13861da177e4SLinus Torvalds case KEYCTL_SETPERM: 13871da177e4SLinus Torvalds return keyctl_setperm_key((key_serial_t) arg2, 13881da177e4SLinus Torvalds (key_perm_t) arg3); 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds case KEYCTL_INSTANTIATE: 13911da177e4SLinus Torvalds return keyctl_instantiate_key((key_serial_t) arg2, 13921da177e4SLinus Torvalds (const void __user *) arg3, 13931da177e4SLinus Torvalds (size_t) arg4, 13941da177e4SLinus Torvalds (key_serial_t) arg5); 13951da177e4SLinus Torvalds 13961da177e4SLinus Torvalds case KEYCTL_NEGATE: 13971da177e4SLinus Torvalds return keyctl_negate_key((key_serial_t) arg2, 13981da177e4SLinus Torvalds (unsigned) arg3, 13991da177e4SLinus Torvalds (key_serial_t) arg4); 14001da177e4SLinus Torvalds 14013e30148cSDavid Howells case KEYCTL_SET_REQKEY_KEYRING: 14023e30148cSDavid Howells return keyctl_set_reqkey_keyring(arg2); 14033e30148cSDavid Howells 1404017679c4SDavid Howells case KEYCTL_SET_TIMEOUT: 1405017679c4SDavid Howells return keyctl_set_timeout((key_serial_t) arg2, 1406017679c4SDavid Howells (unsigned) arg3); 1407017679c4SDavid Howells 1408b5f545c8SDavid Howells case KEYCTL_ASSUME_AUTHORITY: 1409b5f545c8SDavid Howells return keyctl_assume_authority((key_serial_t) arg2); 1410b5f545c8SDavid Howells 141170a5bb72SDavid Howells case KEYCTL_GET_SECURITY: 141270a5bb72SDavid Howells return keyctl_get_security((key_serial_t) arg2, 141390bd49abSJames Morris (char __user *) arg3, 141470a5bb72SDavid Howells (size_t) arg4); 141570a5bb72SDavid Howells 1416ee18d64cSDavid Howells case KEYCTL_SESSION_TO_PARENT: 1417ee18d64cSDavid Howells return keyctl_session_to_parent(); 1418ee18d64cSDavid Howells 14191da177e4SLinus Torvalds default: 14201da177e4SLinus Torvalds return -EOPNOTSUPP; 14211da177e4SLinus Torvalds } 14221da177e4SLinus Torvalds 14231da177e4SLinus Torvalds } /* end sys_keyctl() */ 1424