Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* keyctl.c: userspace keyctl operations |
| 2 | * |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 3 | * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/syscalls.h> |
| 17 | #include <linux/keyctl.h> |
| 18 | #include <linux/fs.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 19 | #include <linux/capability.h> |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 20 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/err.h> |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 22 | #include <linux/vmalloc.h> |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 23 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <asm/uaccess.h> |
| 25 | #include "internal.h" |
| 26 | |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 27 | static int key_get_type_from_user(char *type, |
| 28 | const char __user *_type, |
| 29 | unsigned len) |
| 30 | { |
| 31 | int ret; |
| 32 | |
| 33 | ret = strncpy_from_user(type, _type, len); |
| 34 | |
| 35 | if (ret < 0) |
Dan Carpenter | 4303ef1 | 2010-06-11 17:30:05 +0100 | [diff] [blame] | 36 | return ret; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 37 | |
| 38 | if (ret == 0 || ret >= len) |
| 39 | return -EINVAL; |
| 40 | |
| 41 | if (type[0] == '.') |
| 42 | return -EPERM; |
| 43 | |
| 44 | type[len - 1] = '\0'; |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | /* |
| 50 | * extract the description of a new key from userspace and either add it as a |
| 51 | * new key to the specified keyring or update a matching key in that keyring |
| 52 | * - the keyring must be writable |
| 53 | * - returns the new key's serial number |
| 54 | * - implements add_key() |
| 55 | */ |
Heiko Carstens | 1e7bfb2 | 2009-01-14 14:14:29 +0100 | [diff] [blame] | 56 | SYSCALL_DEFINE5(add_key, const char __user *, _type, |
| 57 | const char __user *, _description, |
| 58 | const void __user *, _payload, |
| 59 | size_t, plen, |
| 60 | key_serial_t, ringid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 62 | key_ref_t keyring_ref, key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | char type[32], *description; |
| 64 | void *payload; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 65 | long ret; |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 66 | bool vm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | ret = -EINVAL; |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 69 | if (plen > 1024 * 1024 - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | goto error; |
| 71 | |
| 72 | /* draw all the data into kernel space */ |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 73 | ret = key_get_type_from_user(type, _type, sizeof(type)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | if (ret < 0) |
| 75 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 77 | description = strndup_user(_description, PAGE_SIZE); |
| 78 | if (IS_ERR(description)) { |
| 79 | ret = PTR_ERR(description); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 80 | goto error; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 81 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | /* pull the payload in if one was supplied */ |
| 84 | payload = NULL; |
| 85 | |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 86 | vm = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | if (_payload) { |
| 88 | ret = -ENOMEM; |
| 89 | payload = kmalloc(plen, GFP_KERNEL); |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 90 | if (!payload) { |
| 91 | if (plen <= PAGE_SIZE) |
| 92 | goto error2; |
| 93 | vm = true; |
| 94 | payload = vmalloc(plen); |
| 95 | if (!payload) |
| 96 | goto error2; |
| 97 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
| 99 | ret = -EFAULT; |
| 100 | if (copy_from_user(payload, _payload, plen) != 0) |
| 101 | goto error3; |
| 102 | } |
| 103 | |
| 104 | /* find the target keyring (which must be writable) */ |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 105 | keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 106 | if (IS_ERR(keyring_ref)) { |
| 107 | ret = PTR_ERR(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | goto error3; |
| 109 | } |
| 110 | |
| 111 | /* create or update the requested key and add it to the target |
| 112 | * keyring */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 113 | key_ref = key_create_or_update(keyring_ref, type, description, |
Arun Raghavan | 6b79ccb | 2008-04-29 01:01:28 -0700 | [diff] [blame] | 114 | payload, plen, KEY_PERM_UNDEF, |
| 115 | KEY_ALLOC_IN_QUOTA); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 116 | if (!IS_ERR(key_ref)) { |
| 117 | ret = key_ref_to_ptr(key_ref)->serial; |
| 118 | key_ref_put(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
| 120 | else { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 121 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 124 | key_ref_put(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | error3: |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 126 | if (!vm) |
| 127 | kfree(payload); |
| 128 | else |
| 129 | vfree(payload); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | error2: |
| 131 | kfree(description); |
| 132 | error: |
| 133 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame^] | 134 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | /* |
| 137 | * search the process keyrings for a matching key |
| 138 | * - nested keyrings may also be searched if they have Search permission |
| 139 | * - if a key is found, it will be attached to the destination keyring if |
| 140 | * there's one specified |
| 141 | * - /sbin/request-key will be invoked if _callout_info is non-NULL |
| 142 | * - the _callout_info string will be passed to /sbin/request-key |
| 143 | * - if the _callout_info string is empty, it will be rendered as "-" |
| 144 | * - implements request_key() |
| 145 | */ |
Heiko Carstens | 1e7bfb2 | 2009-01-14 14:14:29 +0100 | [diff] [blame] | 146 | SYSCALL_DEFINE4(request_key, const char __user *, _type, |
| 147 | const char __user *, _description, |
| 148 | const char __user *, _callout_info, |
| 149 | key_serial_t, destringid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
| 151 | struct key_type *ktype; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 152 | struct key *key; |
| 153 | key_ref_t dest_ref; |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 154 | size_t callout_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | char type[32], *description, *callout_info; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 156 | long ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
| 158 | /* pull the type into kernel space */ |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 159 | ret = key_get_type_from_user(type, _type, sizeof(type)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | if (ret < 0) |
| 161 | goto error; |
David Howells | 1260f80 | 2005-08-04 11:50:01 +0100 | [diff] [blame] | 162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | /* pull the description into kernel space */ |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 164 | description = strndup_user(_description, PAGE_SIZE); |
| 165 | if (IS_ERR(description)) { |
| 166 | ret = PTR_ERR(description); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | goto error; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 168 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
| 170 | /* pull the callout info into kernel space */ |
| 171 | callout_info = NULL; |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 172 | callout_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | if (_callout_info) { |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 174 | callout_info = strndup_user(_callout_info, PAGE_SIZE); |
| 175 | if (IS_ERR(callout_info)) { |
| 176 | ret = PTR_ERR(callout_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | goto error2; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 178 | } |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 179 | callout_len = strlen(callout_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* get the destination keyring if specified */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 183 | dest_ref = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | if (destringid) { |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 185 | dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE, |
| 186 | KEY_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 187 | if (IS_ERR(dest_ref)) { |
| 188 | ret = PTR_ERR(dest_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | goto error3; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /* find the key type */ |
| 194 | ktype = key_type_lookup(type); |
| 195 | if (IS_ERR(ktype)) { |
| 196 | ret = PTR_ERR(ktype); |
| 197 | goto error4; |
| 198 | } |
| 199 | |
| 200 | /* do the search */ |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 201 | key = request_key_and_link(ktype, description, callout_info, |
| 202 | callout_len, NULL, key_ref_to_ptr(dest_ref), |
David Howells | 7e047ef | 2006-06-26 00:24:50 -0700 | [diff] [blame] | 203 | KEY_ALLOC_IN_QUOTA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | if (IS_ERR(key)) { |
| 205 | ret = PTR_ERR(key); |
| 206 | goto error5; |
| 207 | } |
| 208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | ret = key->serial; |
| 210 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 211 | key_put(key); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 212 | error5: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | key_type_put(ktype); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 214 | error4: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 215 | key_ref_put(dest_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 216 | error3: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | kfree(callout_info); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 218 | error2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | kfree(description); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 220 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame^] | 222 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | /* |
| 225 | * get the ID of the specified process keyring |
| 226 | * - the keyring must have search permission to be found |
| 227 | * - implements keyctl(KEYCTL_GET_KEYRING_ID) |
| 228 | */ |
| 229 | long keyctl_get_keyring_ID(key_serial_t id, int create) |
| 230 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 231 | key_ref_t key_ref; |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 232 | unsigned long lflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | long ret; |
| 234 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 235 | lflags = create ? KEY_LOOKUP_CREATE : 0; |
| 236 | key_ref = lookup_user_key(id, lflags, KEY_SEARCH); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 237 | if (IS_ERR(key_ref)) { |
| 238 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | goto error; |
| 240 | } |
| 241 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 242 | ret = key_ref_to_ptr(key_ref)->serial; |
| 243 | key_ref_put(key_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 244 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | return ret; |
| 246 | |
| 247 | } /* end keyctl_get_keyring_ID() */ |
| 248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | /* |
| 250 | * join the session keyring |
| 251 | * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING) |
| 252 | */ |
| 253 | long keyctl_join_session_keyring(const char __user *_name) |
| 254 | { |
| 255 | char *name; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 256 | long ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
| 258 | /* fetch the name from userspace */ |
| 259 | name = NULL; |
| 260 | if (_name) { |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 261 | name = strndup_user(_name, PAGE_SIZE); |
| 262 | if (IS_ERR(name)) { |
| 263 | ret = PTR_ERR(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | goto error; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 265 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* join the session */ |
| 269 | ret = join_session_keyring(name); |
Vegard Nossum | 0d54ee1 | 2009-01-17 17:45:45 +0100 | [diff] [blame] | 270 | kfree(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 272 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame^] | 274 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | /* |
| 277 | * update a key's data payload |
| 278 | * - the key must be writable |
| 279 | * - implements keyctl(KEYCTL_UPDATE) |
| 280 | */ |
| 281 | long keyctl_update_key(key_serial_t id, |
| 282 | const void __user *_payload, |
| 283 | size_t plen) |
| 284 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 285 | key_ref_t key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | void *payload; |
| 287 | long ret; |
| 288 | |
| 289 | ret = -EINVAL; |
| 290 | if (plen > PAGE_SIZE) |
| 291 | goto error; |
| 292 | |
| 293 | /* pull the payload in if one was supplied */ |
| 294 | payload = NULL; |
| 295 | if (_payload) { |
| 296 | ret = -ENOMEM; |
| 297 | payload = kmalloc(plen, GFP_KERNEL); |
| 298 | if (!payload) |
| 299 | goto error; |
| 300 | |
| 301 | ret = -EFAULT; |
| 302 | if (copy_from_user(payload, _payload, plen) != 0) |
| 303 | goto error2; |
| 304 | } |
| 305 | |
| 306 | /* find the target key (which must be writable) */ |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 307 | key_ref = lookup_user_key(id, 0, KEY_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 308 | if (IS_ERR(key_ref)) { |
| 309 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | goto error2; |
| 311 | } |
| 312 | |
| 313 | /* update the key */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 314 | ret = key_update(key_ref, payload, plen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 316 | key_ref_put(key_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 317 | error2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | kfree(payload); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 319 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame^] | 321 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | /* |
| 324 | * revoke a key |
| 325 | * - the key must be writable |
| 326 | * - implements keyctl(KEYCTL_REVOKE) |
| 327 | */ |
| 328 | long keyctl_revoke_key(key_serial_t id) |
| 329 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 330 | key_ref_t key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | long ret; |
| 332 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 333 | key_ref = lookup_user_key(id, 0, KEY_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 334 | if (IS_ERR(key_ref)) { |
| 335 | ret = PTR_ERR(key_ref); |
David Howells | 0c2c9a3 | 2009-09-02 09:13:50 +0100 | [diff] [blame] | 336 | if (ret != -EACCES) |
| 337 | goto error; |
| 338 | key_ref = lookup_user_key(id, 0, KEY_SETATTR); |
| 339 | if (IS_ERR(key_ref)) { |
| 340 | ret = PTR_ERR(key_ref); |
| 341 | goto error; |
| 342 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
| 344 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 345 | key_revoke(key_ref_to_ptr(key_ref)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | ret = 0; |
| 347 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 348 | key_ref_put(key_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 349 | error: |
David Howells | 1260f80 | 2005-08-04 11:50:01 +0100 | [diff] [blame] | 350 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame^] | 351 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | /* |
| 354 | * clear the specified process keyring |
| 355 | * - the keyring must be writable |
| 356 | * - implements keyctl(KEYCTL_CLEAR) |
| 357 | */ |
| 358 | long keyctl_keyring_clear(key_serial_t ringid) |
| 359 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 360 | key_ref_t keyring_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | long ret; |
| 362 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 363 | keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 364 | if (IS_ERR(keyring_ref)) { |
| 365 | ret = PTR_ERR(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | goto error; |
| 367 | } |
| 368 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 369 | ret = keyring_clear(key_ref_to_ptr(keyring_ref)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 371 | key_ref_put(keyring_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 372 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame^] | 374 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | /* |
| 377 | * link a key into a keyring |
| 378 | * - the keyring must be writable |
| 379 | * - the key must be linkable |
| 380 | * - implements keyctl(KEYCTL_LINK) |
| 381 | */ |
| 382 | long keyctl_keyring_link(key_serial_t id, key_serial_t ringid) |
| 383 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 384 | key_ref_t keyring_ref, key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | long ret; |
| 386 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 387 | keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 388 | if (IS_ERR(keyring_ref)) { |
| 389 | ret = PTR_ERR(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | goto error; |
| 391 | } |
| 392 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 393 | key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_LINK); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 394 | if (IS_ERR(key_ref)) { |
| 395 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | goto error2; |
| 397 | } |
| 398 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 399 | ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 401 | key_ref_put(key_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 402 | error2: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 403 | key_ref_put(keyring_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 404 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame^] | 406 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | /* |
| 409 | * unlink the first attachment of a key from a keyring |
| 410 | * - the keyring must be writable |
| 411 | * - we don't need any permissions on the key |
| 412 | * - implements keyctl(KEYCTL_UNLINK) |
| 413 | */ |
| 414 | long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid) |
| 415 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 416 | key_ref_t keyring_ref, key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | long ret; |
| 418 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 419 | keyring_ref = lookup_user_key(ringid, 0, KEY_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 420 | if (IS_ERR(keyring_ref)) { |
| 421 | ret = PTR_ERR(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | goto error; |
| 423 | } |
| 424 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 425 | key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 426 | if (IS_ERR(key_ref)) { |
| 427 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | goto error2; |
| 429 | } |
| 430 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 431 | ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 433 | key_ref_put(key_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 434 | error2: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 435 | key_ref_put(keyring_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 436 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame^] | 438 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | /* |
| 441 | * describe a user key |
| 442 | * - the key must have view permission |
| 443 | * - if there's a buffer, we place up to buflen bytes of data into it |
| 444 | * - unless there's an error, we return the amount of description available, |
| 445 | * irrespective of how much we may have copied |
| 446 | * - the description is formatted thus: |
| 447 | * type;uid;gid;perm;description<NUL> |
| 448 | * - implements keyctl(KEYCTL_DESCRIBE) |
| 449 | */ |
| 450 | long keyctl_describe_key(key_serial_t keyid, |
| 451 | char __user *buffer, |
| 452 | size_t buflen) |
| 453 | { |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 454 | struct key *key, *instkey; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 455 | key_ref_t key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | char *tmpbuf; |
| 457 | long ret; |
| 458 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 459 | key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 460 | if (IS_ERR(key_ref)) { |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 461 | /* viewing a key under construction is permitted if we have the |
| 462 | * authorisation token handy */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 463 | if (PTR_ERR(key_ref) == -EACCES) { |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 464 | instkey = key_get_instantiation_authkey(keyid); |
| 465 | if (!IS_ERR(instkey)) { |
| 466 | key_put(instkey); |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 467 | key_ref = lookup_user_key(keyid, |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 468 | KEY_LOOKUP_PARTIAL, |
| 469 | 0); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 470 | if (!IS_ERR(key_ref)) |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 471 | goto okay; |
| 472 | } |
| 473 | } |
| 474 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 475 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | goto error; |
| 477 | } |
| 478 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 479 | okay: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | /* calculate how much description we're going to return */ |
| 481 | ret = -ENOMEM; |
| 482 | tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 483 | if (!tmpbuf) |
| 484 | goto error2; |
| 485 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 486 | key = key_ref_to_ptr(key_ref); |
| 487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | ret = snprintf(tmpbuf, PAGE_SIZE - 1, |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 489 | "%s;%d;%d;%08x;%s", |
David Howells | 94fd840 | 2010-06-28 14:05:04 +0100 | [diff] [blame] | 490 | key->type->name, |
| 491 | key->uid, |
| 492 | key->gid, |
| 493 | key->perm, |
| 494 | key->description ?: ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
| 496 | /* include a NUL char at the end of the data */ |
| 497 | if (ret > PAGE_SIZE - 1) |
| 498 | ret = PAGE_SIZE - 1; |
| 499 | tmpbuf[ret] = 0; |
| 500 | ret++; |
| 501 | |
| 502 | /* consider returning the data */ |
| 503 | if (buffer && buflen > 0) { |
| 504 | if (buflen > ret) |
| 505 | buflen = ret; |
| 506 | |
| 507 | if (copy_to_user(buffer, tmpbuf, buflen) != 0) |
| 508 | ret = -EFAULT; |
| 509 | } |
| 510 | |
| 511 | kfree(tmpbuf); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 512 | error2: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 513 | key_ref_put(key_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 514 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame^] | 516 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | /* |
| 519 | * search the specified keyring for a matching key |
| 520 | * - the start keyring must be searchable |
| 521 | * - nested keyrings may also be searched if they are searchable |
| 522 | * - only keys with search permission may be found |
| 523 | * - if a key is found, it will be attached to the destination keyring if |
| 524 | * there's one specified |
| 525 | * - implements keyctl(KEYCTL_SEARCH) |
| 526 | */ |
| 527 | long keyctl_keyring_search(key_serial_t ringid, |
| 528 | const char __user *_type, |
| 529 | const char __user *_description, |
| 530 | key_serial_t destringid) |
| 531 | { |
| 532 | struct key_type *ktype; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 533 | key_ref_t keyring_ref, key_ref, dest_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | char type[32], *description; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 535 | long ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
| 537 | /* pull the type and description into kernel space */ |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 538 | ret = key_get_type_from_user(type, _type, sizeof(type)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | if (ret < 0) |
| 540 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 542 | description = strndup_user(_description, PAGE_SIZE); |
| 543 | if (IS_ERR(description)) { |
| 544 | ret = PTR_ERR(description); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | goto error; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 546 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
| 548 | /* get the keyring at which to begin the search */ |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 549 | keyring_ref = lookup_user_key(ringid, 0, KEY_SEARCH); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 550 | if (IS_ERR(keyring_ref)) { |
| 551 | ret = PTR_ERR(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | goto error2; |
| 553 | } |
| 554 | |
| 555 | /* get the destination keyring if specified */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 556 | dest_ref = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | if (destringid) { |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 558 | dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE, |
| 559 | KEY_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 560 | if (IS_ERR(dest_ref)) { |
| 561 | ret = PTR_ERR(dest_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | goto error3; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | /* find the key type */ |
| 567 | ktype = key_type_lookup(type); |
| 568 | if (IS_ERR(ktype)) { |
| 569 | ret = PTR_ERR(ktype); |
| 570 | goto error4; |
| 571 | } |
| 572 | |
| 573 | /* do the search */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 574 | key_ref = keyring_search(keyring_ref, ktype, description); |
| 575 | if (IS_ERR(key_ref)) { |
| 576 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | |
| 578 | /* treat lack or presence of a negative key the same */ |
| 579 | if (ret == -EAGAIN) |
| 580 | ret = -ENOKEY; |
| 581 | goto error5; |
| 582 | } |
| 583 | |
| 584 | /* link the resulting key to the destination keyring if we can */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 585 | if (dest_ref) { |
David Howells | 29db919 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 586 | ret = key_permission(key_ref, KEY_LINK); |
| 587 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | goto error6; |
| 589 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 590 | ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | if (ret < 0) |
| 592 | goto error6; |
| 593 | } |
| 594 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 595 | ret = key_ref_to_ptr(key_ref)->serial; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 597 | error6: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 598 | key_ref_put(key_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 599 | error5: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | key_type_put(ktype); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 601 | error4: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 602 | key_ref_put(dest_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 603 | error3: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 604 | key_ref_put(keyring_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 605 | error2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | kfree(description); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 607 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame^] | 609 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | * read a user key's payload |
| 613 | * - the keyring must be readable or the key must be searchable from the |
| 614 | * process's keyrings |
| 615 | * - if there's a buffer, we place up to buflen bytes of data into it |
| 616 | * - unless there's an error, we return the amount of data in the key, |
| 617 | * irrespective of how much we may have copied |
| 618 | * - implements keyctl(KEYCTL_READ) |
| 619 | */ |
| 620 | long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen) |
| 621 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 622 | struct key *key; |
| 623 | key_ref_t key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | long ret; |
| 625 | |
| 626 | /* find the key first */ |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 627 | key_ref = lookup_user_key(keyid, 0, 0); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 628 | if (IS_ERR(key_ref)) { |
| 629 | ret = -ENOKEY; |
| 630 | goto error; |
| 631 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 633 | key = key_ref_to_ptr(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 635 | /* see if we can read it directly */ |
David Howells | 29db919 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 636 | ret = key_permission(key_ref, KEY_READ); |
| 637 | if (ret == 0) |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 638 | goto can_read_key; |
David Howells | 29db919 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 639 | if (ret != -EACCES) |
| 640 | goto error; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 641 | |
| 642 | /* we can't; see if it's searchable from this process's keyrings |
| 643 | * - we automatically take account of the fact that it may be |
| 644 | * dangling off an instantiation key |
| 645 | */ |
| 646 | if (!is_key_possessed(key_ref)) { |
| 647 | ret = -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | goto error2; |
| 649 | } |
| 650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | /* the key is probably readable - now try to read it */ |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 652 | can_read_key: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | ret = key_validate(key); |
| 654 | if (ret == 0) { |
| 655 | ret = -EOPNOTSUPP; |
| 656 | if (key->type->read) { |
| 657 | /* read the data with the semaphore held (since we |
| 658 | * might sleep) */ |
| 659 | down_read(&key->sem); |
| 660 | ret = key->type->read(key, buffer, buflen); |
| 661 | up_read(&key->sem); |
| 662 | } |
| 663 | } |
| 664 | |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 665 | error2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | key_put(key); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 667 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame^] | 669 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | /* |
| 672 | * change the ownership of a key |
| 673 | * - the keyring owned by the changer |
| 674 | * - if the uid or gid is -1, then that parameter is not changed |
| 675 | * - implements keyctl(KEYCTL_CHOWN) |
| 676 | */ |
| 677 | long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid) |
| 678 | { |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 679 | struct key_user *newowner, *zapowner = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | struct key *key; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 681 | key_ref_t key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | long ret; |
| 683 | |
| 684 | ret = 0; |
| 685 | if (uid == (uid_t) -1 && gid == (gid_t) -1) |
| 686 | goto error; |
| 687 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 688 | key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, |
| 689 | KEY_SETATTR); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 690 | if (IS_ERR(key_ref)) { |
| 691 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | goto error; |
| 693 | } |
| 694 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 695 | key = key_ref_to_ptr(key_ref); |
| 696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | /* make the changes with the locks held to prevent chown/chown races */ |
| 698 | ret = -EACCES; |
| 699 | down_write(&key->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
| 701 | if (!capable(CAP_SYS_ADMIN)) { |
| 702 | /* only the sysadmin can chown a key to some other UID */ |
| 703 | if (uid != (uid_t) -1 && key->uid != uid) |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 704 | goto error_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | |
| 706 | /* only the sysadmin can set the key's GID to a group other |
| 707 | * than one of those that the current process subscribes to */ |
| 708 | if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid)) |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 709 | goto error_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 712 | /* change the UID */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | if (uid != (uid_t) -1 && uid != key->uid) { |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 714 | ret = -ENOMEM; |
Serge E. Hallyn | 1d1e975 | 2009-02-26 18:27:38 -0600 | [diff] [blame] | 715 | newowner = key_user_lookup(uid, current_user_ns()); |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 716 | if (!newowner) |
| 717 | goto error_put; |
| 718 | |
| 719 | /* transfer the quota burden to the new user */ |
| 720 | if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { |
David Howells | 0b77f5b | 2008-04-29 01:01:32 -0700 | [diff] [blame] | 721 | unsigned maxkeys = (uid == 0) ? |
| 722 | key_quota_root_maxkeys : key_quota_maxkeys; |
| 723 | unsigned maxbytes = (uid == 0) ? |
| 724 | key_quota_root_maxbytes : key_quota_maxbytes; |
| 725 | |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 726 | spin_lock(&newowner->lock); |
David Howells | 0b77f5b | 2008-04-29 01:01:32 -0700 | [diff] [blame] | 727 | if (newowner->qnkeys + 1 >= maxkeys || |
| 728 | newowner->qnbytes + key->quotalen >= maxbytes || |
| 729 | newowner->qnbytes + key->quotalen < |
| 730 | newowner->qnbytes) |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 731 | goto quota_overrun; |
| 732 | |
| 733 | newowner->qnkeys++; |
| 734 | newowner->qnbytes += key->quotalen; |
| 735 | spin_unlock(&newowner->lock); |
| 736 | |
| 737 | spin_lock(&key->user->lock); |
| 738 | key->user->qnkeys--; |
| 739 | key->user->qnbytes -= key->quotalen; |
| 740 | spin_unlock(&key->user->lock); |
| 741 | } |
| 742 | |
| 743 | atomic_dec(&key->user->nkeys); |
| 744 | atomic_inc(&newowner->nkeys); |
| 745 | |
| 746 | if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) { |
| 747 | atomic_dec(&key->user->nikeys); |
| 748 | atomic_inc(&newowner->nikeys); |
| 749 | } |
| 750 | |
| 751 | zapowner = key->user; |
| 752 | key->user = newowner; |
| 753 | key->uid = uid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | /* change the GID */ |
| 757 | if (gid != (gid_t) -1) |
| 758 | key->gid = gid; |
| 759 | |
| 760 | ret = 0; |
| 761 | |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 762 | error_put: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | up_write(&key->sem); |
| 764 | key_put(key); |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 765 | if (zapowner) |
| 766 | key_user_put(zapowner); |
| 767 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | return ret; |
| 769 | |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 770 | quota_overrun: |
| 771 | spin_unlock(&newowner->lock); |
| 772 | zapowner = newowner; |
| 773 | ret = -EDQUOT; |
| 774 | goto error_put; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame^] | 775 | } |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 776 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | /* |
| 778 | * change the permission mask on a key |
| 779 | * - the keyring owned by the changer |
| 780 | * - implements keyctl(KEYCTL_SETPERM) |
| 781 | */ |
| 782 | long keyctl_setperm_key(key_serial_t id, key_perm_t perm) |
| 783 | { |
| 784 | struct key *key; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 785 | key_ref_t key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | long ret; |
| 787 | |
| 788 | ret = -EINVAL; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 789 | if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | goto error; |
| 791 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 792 | key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, |
| 793 | KEY_SETATTR); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 794 | if (IS_ERR(key_ref)) { |
| 795 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | goto error; |
| 797 | } |
| 798 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 799 | key = key_ref_to_ptr(key_ref); |
| 800 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 801 | /* make the changes with the locks held to prevent chown/chmod races */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | ret = -EACCES; |
| 803 | down_write(&key->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 805 | /* if we're not the sysadmin, we can only change a key that we own */ |
David Howells | 47d804b | 2008-11-14 10:39:11 +1100 | [diff] [blame] | 806 | if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) { |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 807 | key->perm = perm; |
| 808 | ret = 0; |
| 809 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | up_write(&key->sem); |
| 812 | key_put(key); |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 813 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame^] | 815 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 817 | /* |
| 818 | * get the destination keyring for instantiation |
| 819 | */ |
| 820 | static long get_instantiation_keyring(key_serial_t ringid, |
| 821 | struct request_key_auth *rka, |
| 822 | struct key **_dest_keyring) |
| 823 | { |
| 824 | key_ref_t dkref; |
| 825 | |
David Howells | eca1bf5 | 2008-12-29 00:41:51 +0000 | [diff] [blame] | 826 | *_dest_keyring = NULL; |
| 827 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 828 | /* just return a NULL pointer if we weren't asked to make a link */ |
David Howells | eca1bf5 | 2008-12-29 00:41:51 +0000 | [diff] [blame] | 829 | if (ringid == 0) |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 830 | return 0; |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 831 | |
| 832 | /* if a specific keyring is nominated by ID, then use that */ |
| 833 | if (ringid > 0) { |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 834 | dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 835 | if (IS_ERR(dkref)) |
| 836 | return PTR_ERR(dkref); |
| 837 | *_dest_keyring = key_ref_to_ptr(dkref); |
| 838 | return 0; |
| 839 | } |
| 840 | |
| 841 | if (ringid == KEY_SPEC_REQKEY_AUTH_KEY) |
| 842 | return -EINVAL; |
| 843 | |
| 844 | /* otherwise specify the destination keyring recorded in the |
| 845 | * authorisation key (any KEY_SPEC_*_KEYRING) */ |
| 846 | if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) { |
David Howells | 21279cf | 2009-10-15 10:14:35 +0100 | [diff] [blame] | 847 | *_dest_keyring = key_get(rka->dest_keyring); |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | return -ENOKEY; |
| 852 | } |
| 853 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 854 | /* |
| 855 | * change the request_key authorisation key on the current process |
| 856 | */ |
| 857 | static int keyctl_change_reqkey_auth(struct key *key) |
| 858 | { |
| 859 | struct cred *new; |
| 860 | |
| 861 | new = prepare_creds(); |
| 862 | if (!new) |
| 863 | return -ENOMEM; |
| 864 | |
| 865 | key_put(new->request_key_auth); |
| 866 | new->request_key_auth = key_get(key); |
| 867 | |
| 868 | return commit_creds(new); |
| 869 | } |
| 870 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | /* |
| 872 | * instantiate the key with the specified payload, and, if one is given, link |
| 873 | * the key into the keyring |
| 874 | */ |
| 875 | long keyctl_instantiate_key(key_serial_t id, |
| 876 | const void __user *_payload, |
| 877 | size_t plen, |
| 878 | key_serial_t ringid) |
| 879 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 880 | const struct cred *cred = current_cred(); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 881 | struct request_key_auth *rka; |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 882 | struct key *instkey, *dest_keyring; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | void *payload; |
| 884 | long ret; |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 885 | bool vm = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 887 | kenter("%d,,%zu,%d", id, plen, ringid); |
| 888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | ret = -EINVAL; |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 890 | if (plen > 1024 * 1024 - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | goto error; |
| 892 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 893 | /* the appropriate instantiation authorisation key must have been |
| 894 | * assumed before calling this */ |
| 895 | ret = -EPERM; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 896 | instkey = cred->request_key_auth; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 897 | if (!instkey) |
| 898 | goto error; |
| 899 | |
| 900 | rka = instkey->payload.data; |
| 901 | if (rka->target_key->serial != id) |
| 902 | goto error; |
| 903 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | /* pull the payload in if one was supplied */ |
| 905 | payload = NULL; |
| 906 | |
| 907 | if (_payload) { |
| 908 | ret = -ENOMEM; |
| 909 | payload = kmalloc(plen, GFP_KERNEL); |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 910 | if (!payload) { |
| 911 | if (plen <= PAGE_SIZE) |
| 912 | goto error; |
| 913 | vm = true; |
| 914 | payload = vmalloc(plen); |
| 915 | if (!payload) |
| 916 | goto error; |
| 917 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | |
| 919 | ret = -EFAULT; |
| 920 | if (copy_from_user(payload, _payload, plen) != 0) |
| 921 | goto error2; |
| 922 | } |
| 923 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 924 | /* find the destination keyring amongst those belonging to the |
| 925 | * requesting task */ |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 926 | ret = get_instantiation_keyring(ringid, rka, &dest_keyring); |
| 927 | if (ret < 0) |
| 928 | goto error2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | |
| 930 | /* instantiate the key and link it into a keyring */ |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 931 | ret = key_instantiate_and_link(rka->target_key, payload, plen, |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 932 | dest_keyring, instkey); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 934 | key_put(dest_keyring); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 935 | |
| 936 | /* discard the assumed authority if it's just been disabled by |
| 937 | * instantiation of the key */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 938 | if (ret == 0) |
| 939 | keyctl_change_reqkey_auth(NULL); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 940 | |
| 941 | error2: |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 942 | if (!vm) |
| 943 | kfree(payload); |
| 944 | else |
| 945 | vfree(payload); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 946 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame^] | 948 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | /* |
| 951 | * negatively instantiate the key with the given timeout (in seconds), and, if |
| 952 | * one is given, link the key into the keyring |
| 953 | */ |
| 954 | long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid) |
| 955 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 956 | const struct cred *cred = current_cred(); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 957 | struct request_key_auth *rka; |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 958 | struct key *instkey, *dest_keyring; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | long ret; |
| 960 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 961 | kenter("%d,%u,%d", id, timeout, ringid); |
| 962 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 963 | /* the appropriate instantiation authorisation key must have been |
| 964 | * assumed before calling this */ |
| 965 | ret = -EPERM; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 966 | instkey = cred->request_key_auth; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 967 | if (!instkey) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 970 | rka = instkey->payload.data; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 971 | if (rka->target_key->serial != id) |
| 972 | goto error; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 973 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | /* find the destination keyring if present (which must also be |
| 975 | * writable) */ |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 976 | ret = get_instantiation_keyring(ringid, rka, &dest_keyring); |
| 977 | if (ret < 0) |
| 978 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | |
| 980 | /* instantiate the key and link it into a keyring */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 981 | ret = key_negate_and_link(rka->target_key, timeout, |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 982 | dest_keyring, instkey); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 984 | key_put(dest_keyring); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 985 | |
| 986 | /* discard the assumed authority if it's just been disabled by |
| 987 | * instantiation of the key */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 988 | if (ret == 0) |
| 989 | keyctl_change_reqkey_auth(NULL); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 990 | |
| 991 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame^] | 993 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | /* |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 996 | * set the default keyring in which request_key() will cache keys |
| 997 | * - return the old setting |
| 998 | */ |
| 999 | long keyctl_set_reqkey_keyring(int reqkey_defl) |
| 1000 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1001 | struct cred *new; |
| 1002 | int ret, old_setting; |
| 1003 | |
| 1004 | old_setting = current_cred_xxx(jit_keyring); |
| 1005 | |
| 1006 | if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE) |
| 1007 | return old_setting; |
| 1008 | |
| 1009 | new = prepare_creds(); |
| 1010 | if (!new) |
| 1011 | return -ENOMEM; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1012 | |
| 1013 | switch (reqkey_defl) { |
| 1014 | case KEY_REQKEY_DEFL_THREAD_KEYRING: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1015 | ret = install_thread_keyring_to_cred(new); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1016 | if (ret < 0) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1017 | goto error; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1018 | goto set; |
| 1019 | |
| 1020 | case KEY_REQKEY_DEFL_PROCESS_KEYRING: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1021 | ret = install_process_keyring_to_cred(new); |
| 1022 | if (ret < 0) { |
| 1023 | if (ret != -EEXIST) |
| 1024 | goto error; |
| 1025 | ret = 0; |
| 1026 | } |
| 1027 | goto set; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1028 | |
| 1029 | case KEY_REQKEY_DEFL_DEFAULT: |
| 1030 | case KEY_REQKEY_DEFL_SESSION_KEYRING: |
| 1031 | case KEY_REQKEY_DEFL_USER_KEYRING: |
| 1032 | case KEY_REQKEY_DEFL_USER_SESSION_KEYRING: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1033 | case KEY_REQKEY_DEFL_REQUESTOR_KEYRING: |
| 1034 | goto set; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1035 | |
| 1036 | case KEY_REQKEY_DEFL_NO_CHANGE: |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1037 | case KEY_REQKEY_DEFL_GROUP_KEYRING: |
| 1038 | default: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1039 | ret = -EINVAL; |
| 1040 | goto error; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1043 | set: |
| 1044 | new->jit_keyring = reqkey_defl; |
| 1045 | commit_creds(new); |
| 1046 | return old_setting; |
| 1047 | error: |
| 1048 | abort_creds(new); |
Dan Carpenter | 4303ef1 | 2010-06-11 17:30:05 +0100 | [diff] [blame] | 1049 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame^] | 1050 | } |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1051 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1052 | /* |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1053 | * set or clear the timeout for a key |
| 1054 | */ |
| 1055 | long keyctl_set_timeout(key_serial_t id, unsigned timeout) |
| 1056 | { |
| 1057 | struct timespec now; |
David Howells | 9156235 | 2010-06-11 17:31:05 +0100 | [diff] [blame] | 1058 | struct key *key, *instkey; |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1059 | key_ref_t key_ref; |
| 1060 | time_t expiry; |
| 1061 | long ret; |
| 1062 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 1063 | key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, |
| 1064 | KEY_SETATTR); |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1065 | if (IS_ERR(key_ref)) { |
David Howells | 9156235 | 2010-06-11 17:31:05 +0100 | [diff] [blame] | 1066 | /* setting the timeout on a key under construction is permitted |
| 1067 | * if we have the authorisation token handy */ |
| 1068 | if (PTR_ERR(key_ref) == -EACCES) { |
| 1069 | instkey = key_get_instantiation_authkey(id); |
| 1070 | if (!IS_ERR(instkey)) { |
| 1071 | key_put(instkey); |
| 1072 | key_ref = lookup_user_key(id, |
| 1073 | KEY_LOOKUP_PARTIAL, |
| 1074 | 0); |
| 1075 | if (!IS_ERR(key_ref)) |
| 1076 | goto okay; |
| 1077 | } |
| 1078 | } |
| 1079 | |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1080 | ret = PTR_ERR(key_ref); |
| 1081 | goto error; |
| 1082 | } |
| 1083 | |
David Howells | 9156235 | 2010-06-11 17:31:05 +0100 | [diff] [blame] | 1084 | okay: |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1085 | key = key_ref_to_ptr(key_ref); |
| 1086 | |
| 1087 | /* make the changes with the locks held to prevent races */ |
| 1088 | down_write(&key->sem); |
| 1089 | |
| 1090 | expiry = 0; |
| 1091 | if (timeout > 0) { |
| 1092 | now = current_kernel_time(); |
| 1093 | expiry = now.tv_sec + timeout; |
| 1094 | } |
| 1095 | |
| 1096 | key->expiry = expiry; |
David Howells | c08ef80 | 2009-09-14 17:26:13 +0100 | [diff] [blame] | 1097 | key_schedule_gc(key->expiry + key_gc_delay); |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1098 | |
| 1099 | up_write(&key->sem); |
| 1100 | key_put(key); |
| 1101 | |
| 1102 | ret = 0; |
| 1103 | error: |
| 1104 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame^] | 1105 | } |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1106 | |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1107 | /* |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1108 | * assume the authority to instantiate the specified key |
| 1109 | */ |
| 1110 | long keyctl_assume_authority(key_serial_t id) |
| 1111 | { |
| 1112 | struct key *authkey; |
| 1113 | long ret; |
| 1114 | |
| 1115 | /* special key IDs aren't permitted */ |
| 1116 | ret = -EINVAL; |
| 1117 | if (id < 0) |
| 1118 | goto error; |
| 1119 | |
| 1120 | /* we divest ourselves of authority if given an ID of 0 */ |
| 1121 | if (id == 0) { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1122 | ret = keyctl_change_reqkey_auth(NULL); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1123 | goto error; |
| 1124 | } |
| 1125 | |
| 1126 | /* attempt to assume the authority temporarily granted to us whilst we |
| 1127 | * instantiate the specified key |
| 1128 | * - the authorisation key must be in the current task's keyrings |
| 1129 | * somewhere |
| 1130 | */ |
| 1131 | authkey = key_get_instantiation_authkey(id); |
| 1132 | if (IS_ERR(authkey)) { |
| 1133 | ret = PTR_ERR(authkey); |
| 1134 | goto error; |
| 1135 | } |
| 1136 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1137 | ret = keyctl_change_reqkey_auth(authkey); |
| 1138 | if (ret < 0) |
| 1139 | goto error; |
| 1140 | key_put(authkey); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1141 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1142 | ret = authkey->serial; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1143 | error: |
| 1144 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame^] | 1145 | } |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1146 | |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 1147 | /* |
| 1148 | * get the security label of a key |
| 1149 | * - the key must grant us view permission |
| 1150 | * - if there's a buffer, we place up to buflen bytes of data into it |
| 1151 | * - unless there's an error, we return the amount of information available, |
| 1152 | * irrespective of how much we may have copied (including the terminal NUL) |
| 1153 | * - implements keyctl(KEYCTL_GET_SECURITY) |
| 1154 | */ |
| 1155 | long keyctl_get_security(key_serial_t keyid, |
| 1156 | char __user *buffer, |
| 1157 | size_t buflen) |
| 1158 | { |
| 1159 | struct key *key, *instkey; |
| 1160 | key_ref_t key_ref; |
| 1161 | char *context; |
| 1162 | long ret; |
| 1163 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 1164 | key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW); |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 1165 | if (IS_ERR(key_ref)) { |
| 1166 | if (PTR_ERR(key_ref) != -EACCES) |
| 1167 | return PTR_ERR(key_ref); |
| 1168 | |
| 1169 | /* viewing a key under construction is also permitted if we |
| 1170 | * have the authorisation token handy */ |
| 1171 | instkey = key_get_instantiation_authkey(keyid); |
| 1172 | if (IS_ERR(instkey)) |
Roel Kluin | fa1cc7b | 2009-12-15 15:05:12 -0800 | [diff] [blame] | 1173 | return PTR_ERR(instkey); |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 1174 | key_put(instkey); |
| 1175 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 1176 | key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0); |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 1177 | if (IS_ERR(key_ref)) |
| 1178 | return PTR_ERR(key_ref); |
| 1179 | } |
| 1180 | |
| 1181 | key = key_ref_to_ptr(key_ref); |
| 1182 | ret = security_key_getsecurity(key, &context); |
| 1183 | if (ret == 0) { |
| 1184 | /* if no information was returned, give userspace an empty |
| 1185 | * string */ |
| 1186 | ret = 1; |
| 1187 | if (buffer && buflen > 0 && |
| 1188 | copy_to_user(buffer, "", 1) != 0) |
| 1189 | ret = -EFAULT; |
| 1190 | } else if (ret > 0) { |
| 1191 | /* return as much data as there's room for */ |
| 1192 | if (buffer && buflen > 0) { |
| 1193 | if (buflen > ret) |
| 1194 | buflen = ret; |
| 1195 | |
| 1196 | if (copy_to_user(buffer, context, buflen) != 0) |
| 1197 | ret = -EFAULT; |
| 1198 | } |
| 1199 | |
| 1200 | kfree(context); |
| 1201 | } |
| 1202 | |
| 1203 | key_ref_put(key_ref); |
| 1204 | return ret; |
| 1205 | } |
| 1206 | |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1207 | /* |
| 1208 | * attempt to install the calling process's session keyring on the process's |
| 1209 | * parent process |
| 1210 | * - the keyring must exist and must grant us LINK permission |
| 1211 | * - implements keyctl(KEYCTL_SESSION_TO_PARENT) |
| 1212 | */ |
| 1213 | long keyctl_session_to_parent(void) |
| 1214 | { |
Geert Uytterhoeven | a00ae4d | 2009-12-13 20:21:34 +0100 | [diff] [blame] | 1215 | #ifdef TIF_NOTIFY_RESUME |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1216 | struct task_struct *me, *parent; |
| 1217 | const struct cred *mycred, *pcred; |
| 1218 | struct cred *cred, *oldcred; |
| 1219 | key_ref_t keyring_r; |
| 1220 | int ret; |
| 1221 | |
| 1222 | keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_LINK); |
| 1223 | if (IS_ERR(keyring_r)) |
| 1224 | return PTR_ERR(keyring_r); |
| 1225 | |
| 1226 | /* our parent is going to need a new cred struct, a new tgcred struct |
| 1227 | * and new security data, so we allocate them here to prevent ENOMEM in |
| 1228 | * our parent */ |
| 1229 | ret = -ENOMEM; |
| 1230 | cred = cred_alloc_blank(); |
| 1231 | if (!cred) |
| 1232 | goto error_keyring; |
| 1233 | |
| 1234 | cred->tgcred->session_keyring = key_ref_to_ptr(keyring_r); |
| 1235 | keyring_r = NULL; |
| 1236 | |
| 1237 | me = current; |
David Howells | 9d1ac65 | 2010-09-10 09:59:46 +0100 | [diff] [blame] | 1238 | rcu_read_lock(); |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1239 | write_lock_irq(&tasklist_lock); |
| 1240 | |
| 1241 | parent = me->real_parent; |
| 1242 | ret = -EPERM; |
| 1243 | |
| 1244 | /* the parent mustn't be init and mustn't be a kernel thread */ |
| 1245 | if (parent->pid <= 1 || !parent->mm) |
| 1246 | goto not_permitted; |
| 1247 | |
| 1248 | /* the parent must be single threaded */ |
Oleg Nesterov | dd98acf | 2010-05-26 14:43:23 -0700 | [diff] [blame] | 1249 | if (!thread_group_empty(parent)) |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1250 | goto not_permitted; |
| 1251 | |
| 1252 | /* the parent and the child must have different session keyrings or |
| 1253 | * there's no point */ |
| 1254 | mycred = current_cred(); |
| 1255 | pcred = __task_cred(parent); |
| 1256 | if (mycred == pcred || |
| 1257 | mycred->tgcred->session_keyring == pcred->tgcred->session_keyring) |
| 1258 | goto already_same; |
| 1259 | |
| 1260 | /* the parent must have the same effective ownership and mustn't be |
| 1261 | * SUID/SGID */ |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 1262 | if (pcred->uid != mycred->euid || |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1263 | pcred->euid != mycred->euid || |
| 1264 | pcred->suid != mycred->euid || |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 1265 | pcred->gid != mycred->egid || |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1266 | pcred->egid != mycred->egid || |
| 1267 | pcred->sgid != mycred->egid) |
| 1268 | goto not_permitted; |
| 1269 | |
| 1270 | /* the keyrings must have the same UID */ |
David Howells | 3d96406 | 2010-09-10 09:59:51 +0100 | [diff] [blame] | 1271 | if ((pcred->tgcred->session_keyring && |
| 1272 | pcred->tgcred->session_keyring->uid != mycred->euid) || |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1273 | mycred->tgcred->session_keyring->uid != mycred->euid) |
| 1274 | goto not_permitted; |
| 1275 | |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1276 | /* if there's an already pending keyring replacement, then we replace |
| 1277 | * that */ |
| 1278 | oldcred = parent->replacement_session_keyring; |
| 1279 | |
| 1280 | /* the replacement session keyring is applied just prior to userspace |
| 1281 | * restarting */ |
| 1282 | parent->replacement_session_keyring = cred; |
| 1283 | cred = NULL; |
| 1284 | set_ti_thread_flag(task_thread_info(parent), TIF_NOTIFY_RESUME); |
| 1285 | |
| 1286 | write_unlock_irq(&tasklist_lock); |
David Howells | 9d1ac65 | 2010-09-10 09:59:46 +0100 | [diff] [blame] | 1287 | rcu_read_unlock(); |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1288 | if (oldcred) |
| 1289 | put_cred(oldcred); |
| 1290 | return 0; |
| 1291 | |
| 1292 | already_same: |
| 1293 | ret = 0; |
| 1294 | not_permitted: |
Marc Dionne | 5c84342 | 2009-09-14 12:46:23 +0100 | [diff] [blame] | 1295 | write_unlock_irq(&tasklist_lock); |
David Howells | 9d1ac65 | 2010-09-10 09:59:46 +0100 | [diff] [blame] | 1296 | rcu_read_unlock(); |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1297 | put_cred(cred); |
| 1298 | return ret; |
| 1299 | |
| 1300 | error_keyring: |
| 1301 | key_ref_put(keyring_r); |
| 1302 | return ret; |
Geert Uytterhoeven | a00ae4d | 2009-12-13 20:21:34 +0100 | [diff] [blame] | 1303 | |
| 1304 | #else /* !TIF_NOTIFY_RESUME */ |
| 1305 | /* |
| 1306 | * To be removed when TIF_NOTIFY_RESUME has been implemented on |
| 1307 | * m68k/xtensa |
| 1308 | */ |
| 1309 | #warning TIF_NOTIFY_RESUME not implemented |
| 1310 | return -EOPNOTSUPP; |
| 1311 | #endif /* !TIF_NOTIFY_RESUME */ |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1312 | } |
| 1313 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1314 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | * the key control system call |
| 1316 | */ |
Heiko Carstens | 938bb9f | 2009-01-14 14:14:30 +0100 | [diff] [blame] | 1317 | SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3, |
| 1318 | unsigned long, arg4, unsigned long, arg5) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | { |
| 1320 | switch (option) { |
| 1321 | case KEYCTL_GET_KEYRING_ID: |
| 1322 | return keyctl_get_keyring_ID((key_serial_t) arg2, |
| 1323 | (int) arg3); |
| 1324 | |
| 1325 | case KEYCTL_JOIN_SESSION_KEYRING: |
| 1326 | return keyctl_join_session_keyring((const char __user *) arg2); |
| 1327 | |
| 1328 | case KEYCTL_UPDATE: |
| 1329 | return keyctl_update_key((key_serial_t) arg2, |
| 1330 | (const void __user *) arg3, |
| 1331 | (size_t) arg4); |
| 1332 | |
| 1333 | case KEYCTL_REVOKE: |
| 1334 | return keyctl_revoke_key((key_serial_t) arg2); |
| 1335 | |
| 1336 | case KEYCTL_DESCRIBE: |
| 1337 | return keyctl_describe_key((key_serial_t) arg2, |
| 1338 | (char __user *) arg3, |
| 1339 | (unsigned) arg4); |
| 1340 | |
| 1341 | case KEYCTL_CLEAR: |
| 1342 | return keyctl_keyring_clear((key_serial_t) arg2); |
| 1343 | |
| 1344 | case KEYCTL_LINK: |
| 1345 | return keyctl_keyring_link((key_serial_t) arg2, |
| 1346 | (key_serial_t) arg3); |
| 1347 | |
| 1348 | case KEYCTL_UNLINK: |
| 1349 | return keyctl_keyring_unlink((key_serial_t) arg2, |
| 1350 | (key_serial_t) arg3); |
| 1351 | |
| 1352 | case KEYCTL_SEARCH: |
| 1353 | return keyctl_keyring_search((key_serial_t) arg2, |
| 1354 | (const char __user *) arg3, |
| 1355 | (const char __user *) arg4, |
| 1356 | (key_serial_t) arg5); |
| 1357 | |
| 1358 | case KEYCTL_READ: |
| 1359 | return keyctl_read_key((key_serial_t) arg2, |
| 1360 | (char __user *) arg3, |
| 1361 | (size_t) arg4); |
| 1362 | |
| 1363 | case KEYCTL_CHOWN: |
| 1364 | return keyctl_chown_key((key_serial_t) arg2, |
| 1365 | (uid_t) arg3, |
| 1366 | (gid_t) arg4); |
| 1367 | |
| 1368 | case KEYCTL_SETPERM: |
| 1369 | return keyctl_setperm_key((key_serial_t) arg2, |
| 1370 | (key_perm_t) arg3); |
| 1371 | |
| 1372 | case KEYCTL_INSTANTIATE: |
| 1373 | return keyctl_instantiate_key((key_serial_t) arg2, |
| 1374 | (const void __user *) arg3, |
| 1375 | (size_t) arg4, |
| 1376 | (key_serial_t) arg5); |
| 1377 | |
| 1378 | case KEYCTL_NEGATE: |
| 1379 | return keyctl_negate_key((key_serial_t) arg2, |
| 1380 | (unsigned) arg3, |
| 1381 | (key_serial_t) arg4); |
| 1382 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1383 | case KEYCTL_SET_REQKEY_KEYRING: |
| 1384 | return keyctl_set_reqkey_keyring(arg2); |
| 1385 | |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1386 | case KEYCTL_SET_TIMEOUT: |
| 1387 | return keyctl_set_timeout((key_serial_t) arg2, |
| 1388 | (unsigned) arg3); |
| 1389 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1390 | case KEYCTL_ASSUME_AUTHORITY: |
| 1391 | return keyctl_assume_authority((key_serial_t) arg2); |
| 1392 | |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 1393 | case KEYCTL_GET_SECURITY: |
| 1394 | return keyctl_get_security((key_serial_t) arg2, |
James Morris | 90bd49a | 2008-12-29 14:35:35 +1100 | [diff] [blame] | 1395 | (char __user *) arg3, |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 1396 | (size_t) arg4); |
| 1397 | |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1398 | case KEYCTL_SESSION_TO_PARENT: |
| 1399 | return keyctl_session_to_parent(); |
| 1400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | default: |
| 1402 | return -EOPNOTSUPP; |
| 1403 | } |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame^] | 1404 | } |