blob: b85ace218395b9af99e7a2fc8f568352ced3372d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* keyctl.c: userspace keyctl operations
2 *
David Howells3e301482005-06-23 22:00:56 -07003 * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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.Dunlapc59ede72006-01-11 12:17:46 -080019#include <linux/capability.h>
Davi Arnaut0cb409d2006-03-24 03:18:43 -080020#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/err.h>
David Howells38bbca62008-04-29 01:01:19 -070022#include <linux/vmalloc.h>
David Howells70a5bb72008-04-29 01:01:26 -070023#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/uaccess.h>
25#include "internal.h"
26
Davi Arnaut0cb409d2006-03-24 03:18:43 -080027static 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)
36 return -EFAULT;
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 Torvalds1da177e2005-04-16 15:20:36 -070049/*****************************************************************************/
50/*
51 * extract the description of a new key from userspace and either add it as a
52 * new key to the specified keyring or update a matching key in that keyring
53 * - the keyring must be writable
54 * - returns the new key's serial number
55 * - implements add_key()
56 */
Heiko Carstens1e7bfb22009-01-14 14:14:29 +010057SYSCALL_DEFINE5(add_key, const char __user *, _type,
58 const char __user *, _description,
59 const void __user *, _payload,
60 size_t, plen,
61 key_serial_t, ringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
David Howells664cceb2005-09-28 17:03:15 +010063 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 char type[32], *description;
65 void *payload;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080066 long ret;
David Howells38bbca62008-04-29 01:01:19 -070067 bool vm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 ret = -EINVAL;
David Howells38bbca62008-04-29 01:01:19 -070070 if (plen > 1024 * 1024 - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 goto error;
72
73 /* draw all the data into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -080074 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (ret < 0)
76 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Davi Arnaut0cb409d2006-03-24 03:18:43 -080078 description = strndup_user(_description, PAGE_SIZE);
79 if (IS_ERR(description)) {
80 ret = PTR_ERR(description);
David Howells3e301482005-06-23 22:00:56 -070081 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 /* pull the payload in if one was supplied */
85 payload = NULL;
86
David Howells38bbca62008-04-29 01:01:19 -070087 vm = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (_payload) {
89 ret = -ENOMEM;
90 payload = kmalloc(plen, GFP_KERNEL);
David Howells38bbca62008-04-29 01:01:19 -070091 if (!payload) {
92 if (plen <= PAGE_SIZE)
93 goto error2;
94 vm = true;
95 payload = vmalloc(plen);
96 if (!payload)
97 goto error2;
98 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 ret = -EFAULT;
101 if (copy_from_user(payload, _payload, plen) != 0)
102 goto error3;
103 }
104
105 /* find the target keyring (which must be writable) */
David Howells55931222009-09-02 09:13:45 +0100106 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100107 if (IS_ERR(keyring_ref)) {
108 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 goto error3;
110 }
111
112 /* create or update the requested key and add it to the target
113 * keyring */
David Howells664cceb2005-09-28 17:03:15 +0100114 key_ref = key_create_or_update(keyring_ref, type, description,
Arun Raghavan6b79ccb2008-04-29 01:01:28 -0700115 payload, plen, KEY_PERM_UNDEF,
116 KEY_ALLOC_IN_QUOTA);
David Howells664cceb2005-09-28 17:03:15 +0100117 if (!IS_ERR(key_ref)) {
118 ret = key_ref_to_ptr(key_ref)->serial;
119 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121 else {
David Howells664cceb2005-09-28 17:03:15 +0100122 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124
David Howells664cceb2005-09-28 17:03:15 +0100125 key_ref_put(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 error3:
David Howells38bbca62008-04-29 01:01:19 -0700127 if (!vm)
128 kfree(payload);
129 else
130 vfree(payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 error2:
132 kfree(description);
133 error:
134 return ret;
135
136} /* end sys_add_key() */
137
138/*****************************************************************************/
139/*
140 * search the process keyrings for a matching key
141 * - nested keyrings may also be searched if they have Search permission
142 * - if a key is found, it will be attached to the destination keyring if
143 * there's one specified
144 * - /sbin/request-key will be invoked if _callout_info is non-NULL
145 * - the _callout_info string will be passed to /sbin/request-key
146 * - if the _callout_info string is empty, it will be rendered as "-"
147 * - implements request_key()
148 */
Heiko Carstens1e7bfb22009-01-14 14:14:29 +0100149SYSCALL_DEFINE4(request_key, const char __user *, _type,
150 const char __user *, _description,
151 const char __user *, _callout_info,
152 key_serial_t, destringid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 struct key_type *ktype;
David Howells664cceb2005-09-28 17:03:15 +0100155 struct key *key;
156 key_ref_t dest_ref;
David Howells4a38e122008-04-29 01:01:24 -0700157 size_t callout_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 char type[32], *description, *callout_info;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800159 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 /* pull the type into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800162 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (ret < 0)
164 goto error;
David Howells1260f802005-08-04 11:50:01 +0100165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /* pull the description into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800167 description = strndup_user(_description, PAGE_SIZE);
168 if (IS_ERR(description)) {
169 ret = PTR_ERR(description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 /* pull the callout info into kernel space */
174 callout_info = NULL;
David Howells4a38e122008-04-29 01:01:24 -0700175 callout_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (_callout_info) {
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800177 callout_info = strndup_user(_callout_info, PAGE_SIZE);
178 if (IS_ERR(callout_info)) {
179 ret = PTR_ERR(callout_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 goto error2;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800181 }
David Howells4a38e122008-04-29 01:01:24 -0700182 callout_len = strlen(callout_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184
185 /* get the destination keyring if specified */
David Howells664cceb2005-09-28 17:03:15 +0100186 dest_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (destringid) {
David Howells55931222009-09-02 09:13:45 +0100188 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
189 KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100190 if (IS_ERR(dest_ref)) {
191 ret = PTR_ERR(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 goto error3;
193 }
194 }
195
196 /* find the key type */
197 ktype = key_type_lookup(type);
198 if (IS_ERR(ktype)) {
199 ret = PTR_ERR(ktype);
200 goto error4;
201 }
202
203 /* do the search */
David Howells4a38e122008-04-29 01:01:24 -0700204 key = request_key_and_link(ktype, description, callout_info,
205 callout_len, NULL, key_ref_to_ptr(dest_ref),
David Howells7e047ef2006-06-26 00:24:50 -0700206 KEY_ALLOC_IN_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (IS_ERR(key)) {
208 ret = PTR_ERR(key);
209 goto error5;
210 }
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 ret = key->serial;
213
David Howells3e301482005-06-23 22:00:56 -0700214 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 error5:
216 key_type_put(ktype);
217 error4:
David Howells664cceb2005-09-28 17:03:15 +0100218 key_ref_put(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 error3:
220 kfree(callout_info);
221 error2:
222 kfree(description);
223 error:
224 return ret;
225
226} /* end sys_request_key() */
227
228/*****************************************************************************/
229/*
230 * get the ID of the specified process keyring
231 * - the keyring must have search permission to be found
232 * - implements keyctl(KEYCTL_GET_KEYRING_ID)
233 */
234long keyctl_get_keyring_ID(key_serial_t id, int create)
235{
David Howells664cceb2005-09-28 17:03:15 +0100236 key_ref_t key_ref;
David Howells55931222009-09-02 09:13:45 +0100237 unsigned long lflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 long ret;
239
David Howells55931222009-09-02 09:13:45 +0100240 lflags = create ? KEY_LOOKUP_CREATE : 0;
241 key_ref = lookup_user_key(id, lflags, KEY_SEARCH);
David Howells664cceb2005-09-28 17:03:15 +0100242 if (IS_ERR(key_ref)) {
243 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 goto error;
245 }
246
David Howells664cceb2005-09-28 17:03:15 +0100247 ret = key_ref_to_ptr(key_ref)->serial;
248 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 error:
250 return ret;
251
252} /* end keyctl_get_keyring_ID() */
253
254/*****************************************************************************/
255/*
256 * join the session keyring
257 * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING)
258 */
259long keyctl_join_session_keyring(const char __user *_name)
260{
261 char *name;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800262 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /* fetch the name from userspace */
265 name = NULL;
266 if (_name) {
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800267 name = strndup_user(_name, PAGE_SIZE);
268 if (IS_ERR(name)) {
269 ret = PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273
274 /* join the session */
275 ret = join_session_keyring(name);
Vegard Nossum0d54ee12009-01-17 17:45:45 +0100276 kfree(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 error:
279 return ret;
280
281} /* end keyctl_join_session_keyring() */
282
283/*****************************************************************************/
284/*
285 * update a key's data payload
286 * - the key must be writable
287 * - implements keyctl(KEYCTL_UPDATE)
288 */
289long keyctl_update_key(key_serial_t id,
290 const void __user *_payload,
291 size_t plen)
292{
David Howells664cceb2005-09-28 17:03:15 +0100293 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 void *payload;
295 long ret;
296
297 ret = -EINVAL;
298 if (plen > PAGE_SIZE)
299 goto error;
300
301 /* pull the payload in if one was supplied */
302 payload = NULL;
303 if (_payload) {
304 ret = -ENOMEM;
305 payload = kmalloc(plen, GFP_KERNEL);
306 if (!payload)
307 goto error;
308
309 ret = -EFAULT;
310 if (copy_from_user(payload, _payload, plen) != 0)
311 goto error2;
312 }
313
314 /* find the target key (which must be writable) */
David Howells55931222009-09-02 09:13:45 +0100315 key_ref = lookup_user_key(id, 0, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100316 if (IS_ERR(key_ref)) {
317 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 goto error2;
319 }
320
321 /* update the key */
David Howells664cceb2005-09-28 17:03:15 +0100322 ret = key_update(key_ref, payload, plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
David Howells664cceb2005-09-28 17:03:15 +0100324 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 error2:
326 kfree(payload);
327 error:
328 return ret;
329
330} /* end keyctl_update_key() */
331
332/*****************************************************************************/
333/*
334 * revoke a key
335 * - the key must be writable
336 * - implements keyctl(KEYCTL_REVOKE)
337 */
338long keyctl_revoke_key(key_serial_t id)
339{
David Howells664cceb2005-09-28 17:03:15 +0100340 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 long ret;
342
David Howells55931222009-09-02 09:13:45 +0100343 key_ref = lookup_user_key(id, 0, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100344 if (IS_ERR(key_ref)) {
345 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 goto error;
347 }
348
David Howells664cceb2005-09-28 17:03:15 +0100349 key_revoke(key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 ret = 0;
351
David Howells664cceb2005-09-28 17:03:15 +0100352 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 error:
David Howells1260f802005-08-04 11:50:01 +0100354 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356} /* end keyctl_revoke_key() */
357
358/*****************************************************************************/
359/*
360 * clear the specified process keyring
361 * - the keyring must be writable
362 * - implements keyctl(KEYCTL_CLEAR)
363 */
364long keyctl_keyring_clear(key_serial_t ringid)
365{
David Howells664cceb2005-09-28 17:03:15 +0100366 key_ref_t keyring_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 long ret;
368
David Howells55931222009-09-02 09:13:45 +0100369 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100370 if (IS_ERR(keyring_ref)) {
371 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 goto error;
373 }
374
David Howells664cceb2005-09-28 17:03:15 +0100375 ret = keyring_clear(key_ref_to_ptr(keyring_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
David Howells664cceb2005-09-28 17:03:15 +0100377 key_ref_put(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 error:
379 return ret;
380
381} /* end keyctl_keyring_clear() */
382
383/*****************************************************************************/
384/*
385 * link a key into a keyring
386 * - the keyring must be writable
387 * - the key must be linkable
388 * - implements keyctl(KEYCTL_LINK)
389 */
390long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
391{
David Howells664cceb2005-09-28 17:03:15 +0100392 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 long ret;
394
David Howells55931222009-09-02 09:13:45 +0100395 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100396 if (IS_ERR(keyring_ref)) {
397 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 goto error;
399 }
400
David Howells55931222009-09-02 09:13:45 +0100401 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_LINK);
David Howells664cceb2005-09-28 17:03:15 +0100402 if (IS_ERR(key_ref)) {
403 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 goto error2;
405 }
406
David Howells664cceb2005-09-28 17:03:15 +0100407 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
David Howells664cceb2005-09-28 17:03:15 +0100409 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 error2:
David Howells664cceb2005-09-28 17:03:15 +0100411 key_ref_put(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 error:
413 return ret;
414
415} /* end keyctl_keyring_link() */
416
417/*****************************************************************************/
418/*
419 * unlink the first attachment of a key from a keyring
420 * - the keyring must be writable
421 * - we don't need any permissions on the key
422 * - implements keyctl(KEYCTL_UNLINK)
423 */
424long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
425{
David Howells664cceb2005-09-28 17:03:15 +0100426 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 long ret;
428
David Howells55931222009-09-02 09:13:45 +0100429 keyring_ref = lookup_user_key(ringid, 0, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100430 if (IS_ERR(keyring_ref)) {
431 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 goto error;
433 }
434
David Howells55931222009-09-02 09:13:45 +0100435 key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
David Howells664cceb2005-09-28 17:03:15 +0100436 if (IS_ERR(key_ref)) {
437 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 goto error2;
439 }
440
David Howells664cceb2005-09-28 17:03:15 +0100441 ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
David Howells664cceb2005-09-28 17:03:15 +0100443 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 error2:
David Howells664cceb2005-09-28 17:03:15 +0100445 key_ref_put(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 error:
447 return ret;
448
449} /* end keyctl_keyring_unlink() */
450
451/*****************************************************************************/
452/*
453 * describe a user key
454 * - the key must have view permission
455 * - if there's a buffer, we place up to buflen bytes of data into it
456 * - unless there's an error, we return the amount of description available,
457 * irrespective of how much we may have copied
458 * - the description is formatted thus:
459 * type;uid;gid;perm;description<NUL>
460 * - implements keyctl(KEYCTL_DESCRIBE)
461 */
462long keyctl_describe_key(key_serial_t keyid,
463 char __user *buffer,
464 size_t buflen)
465{
David Howells3e301482005-06-23 22:00:56 -0700466 struct key *key, *instkey;
David Howells664cceb2005-09-28 17:03:15 +0100467 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 char *tmpbuf;
469 long ret;
470
David Howells55931222009-09-02 09:13:45 +0100471 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
David Howells664cceb2005-09-28 17:03:15 +0100472 if (IS_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700473 /* viewing a key under construction is permitted if we have the
474 * authorisation token handy */
David Howells664cceb2005-09-28 17:03:15 +0100475 if (PTR_ERR(key_ref) == -EACCES) {
David Howells3e301482005-06-23 22:00:56 -0700476 instkey = key_get_instantiation_authkey(keyid);
477 if (!IS_ERR(instkey)) {
478 key_put(instkey);
David Howells8bbf49762008-11-14 10:39:14 +1100479 key_ref = lookup_user_key(keyid,
David Howells55931222009-09-02 09:13:45 +0100480 KEY_LOOKUP_PARTIAL,
481 0);
David Howells664cceb2005-09-28 17:03:15 +0100482 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700483 goto okay;
484 }
485 }
486
David Howells664cceb2005-09-28 17:03:15 +0100487 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 goto error;
489 }
490
David Howells3e301482005-06-23 22:00:56 -0700491okay:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /* calculate how much description we're going to return */
493 ret = -ENOMEM;
494 tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
495 if (!tmpbuf)
496 goto error2;
497
David Howells664cceb2005-09-28 17:03:15 +0100498 key = key_ref_to_ptr(key_ref);
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 ret = snprintf(tmpbuf, PAGE_SIZE - 1,
David Howells664cceb2005-09-28 17:03:15 +0100501 "%s;%d;%d;%08x;%s",
502 key_ref_to_ptr(key_ref)->type->name,
503 key_ref_to_ptr(key_ref)->uid,
504 key_ref_to_ptr(key_ref)->gid,
505 key_ref_to_ptr(key_ref)->perm,
506 key_ref_to_ptr(key_ref)->description ?
507 key_ref_to_ptr(key_ref)->description : ""
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 );
509
510 /* include a NUL char at the end of the data */
511 if (ret > PAGE_SIZE - 1)
512 ret = PAGE_SIZE - 1;
513 tmpbuf[ret] = 0;
514 ret++;
515
516 /* consider returning the data */
517 if (buffer && buflen > 0) {
518 if (buflen > ret)
519 buflen = ret;
520
521 if (copy_to_user(buffer, tmpbuf, buflen) != 0)
522 ret = -EFAULT;
523 }
524
525 kfree(tmpbuf);
526 error2:
David Howells664cceb2005-09-28 17:03:15 +0100527 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 error:
529 return ret;
530
531} /* end keyctl_describe_key() */
532
533/*****************************************************************************/
534/*
535 * search the specified keyring for a matching key
536 * - the start keyring must be searchable
537 * - nested keyrings may also be searched if they are searchable
538 * - only keys with search permission may be found
539 * - if a key is found, it will be attached to the destination keyring if
540 * there's one specified
541 * - implements keyctl(KEYCTL_SEARCH)
542 */
543long keyctl_keyring_search(key_serial_t ringid,
544 const char __user *_type,
545 const char __user *_description,
546 key_serial_t destringid)
547{
548 struct key_type *ktype;
David Howells664cceb2005-09-28 17:03:15 +0100549 key_ref_t keyring_ref, key_ref, dest_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 char type[32], *description;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800551 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 /* pull the type and description into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800554 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (ret < 0)
556 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800558 description = strndup_user(_description, PAGE_SIZE);
559 if (IS_ERR(description)) {
560 ret = PTR_ERR(description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /* get the keyring at which to begin the search */
David Howells55931222009-09-02 09:13:45 +0100565 keyring_ref = lookup_user_key(ringid, 0, KEY_SEARCH);
David Howells664cceb2005-09-28 17:03:15 +0100566 if (IS_ERR(keyring_ref)) {
567 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 goto error2;
569 }
570
571 /* get the destination keyring if specified */
David Howells664cceb2005-09-28 17:03:15 +0100572 dest_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (destringid) {
David Howells55931222009-09-02 09:13:45 +0100574 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
575 KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100576 if (IS_ERR(dest_ref)) {
577 ret = PTR_ERR(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 goto error3;
579 }
580 }
581
582 /* find the key type */
583 ktype = key_type_lookup(type);
584 if (IS_ERR(ktype)) {
585 ret = PTR_ERR(ktype);
586 goto error4;
587 }
588
589 /* do the search */
David Howells664cceb2005-09-28 17:03:15 +0100590 key_ref = keyring_search(keyring_ref, ktype, description);
591 if (IS_ERR(key_ref)) {
592 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /* treat lack or presence of a negative key the same */
595 if (ret == -EAGAIN)
596 ret = -ENOKEY;
597 goto error5;
598 }
599
600 /* link the resulting key to the destination keyring if we can */
David Howells664cceb2005-09-28 17:03:15 +0100601 if (dest_ref) {
David Howells29db9192005-10-30 15:02:44 -0800602 ret = key_permission(key_ref, KEY_LINK);
603 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 goto error6;
605
David Howells664cceb2005-09-28 17:03:15 +0100606 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (ret < 0)
608 goto error6;
609 }
610
David Howells664cceb2005-09-28 17:03:15 +0100611 ret = key_ref_to_ptr(key_ref)->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 error6:
David Howells664cceb2005-09-28 17:03:15 +0100614 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 error5:
616 key_type_put(ktype);
617 error4:
David Howells664cceb2005-09-28 17:03:15 +0100618 key_ref_put(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 error3:
David Howells664cceb2005-09-28 17:03:15 +0100620 key_ref_put(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 error2:
622 kfree(description);
623 error:
624 return ret;
625
626} /* end keyctl_keyring_search() */
627
628/*****************************************************************************/
629/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 * read a user key's payload
631 * - the keyring must be readable or the key must be searchable from the
632 * process's keyrings
633 * - if there's a buffer, we place up to buflen bytes of data into it
634 * - unless there's an error, we return the amount of data in the key,
635 * irrespective of how much we may have copied
636 * - implements keyctl(KEYCTL_READ)
637 */
638long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
639{
David Howells664cceb2005-09-28 17:03:15 +0100640 struct key *key;
641 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 long ret;
643
644 /* find the key first */
David Howells55931222009-09-02 09:13:45 +0100645 key_ref = lookup_user_key(keyid, 0, 0);
David Howells664cceb2005-09-28 17:03:15 +0100646 if (IS_ERR(key_ref)) {
647 ret = -ENOKEY;
648 goto error;
649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
David Howells664cceb2005-09-28 17:03:15 +0100651 key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
David Howells664cceb2005-09-28 17:03:15 +0100653 /* see if we can read it directly */
David Howells29db9192005-10-30 15:02:44 -0800654 ret = key_permission(key_ref, KEY_READ);
655 if (ret == 0)
David Howells664cceb2005-09-28 17:03:15 +0100656 goto can_read_key;
David Howells29db9192005-10-30 15:02:44 -0800657 if (ret != -EACCES)
658 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100659
660 /* we can't; see if it's searchable from this process's keyrings
661 * - we automatically take account of the fact that it may be
662 * dangling off an instantiation key
663 */
664 if (!is_key_possessed(key_ref)) {
665 ret = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 goto error2;
667 }
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 /* the key is probably readable - now try to read it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 can_read_key:
671 ret = key_validate(key);
672 if (ret == 0) {
673 ret = -EOPNOTSUPP;
674 if (key->type->read) {
675 /* read the data with the semaphore held (since we
676 * might sleep) */
677 down_read(&key->sem);
678 ret = key->type->read(key, buffer, buflen);
679 up_read(&key->sem);
680 }
681 }
682
683 error2:
684 key_put(key);
685 error:
686 return ret;
687
688} /* end keyctl_read_key() */
689
690/*****************************************************************************/
691/*
692 * change the ownership of a key
693 * - the keyring owned by the changer
694 * - if the uid or gid is -1, then that parameter is not changed
695 * - implements keyctl(KEYCTL_CHOWN)
696 */
697long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
698{
Fredrik Tolf58016492006-06-26 00:24:51 -0700699 struct key_user *newowner, *zapowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100701 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 long ret;
703
704 ret = 0;
705 if (uid == (uid_t) -1 && gid == (gid_t) -1)
706 goto error;
707
David Howells55931222009-09-02 09:13:45 +0100708 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
709 KEY_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100710 if (IS_ERR(key_ref)) {
711 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 goto error;
713 }
714
David Howells664cceb2005-09-28 17:03:15 +0100715 key = key_ref_to_ptr(key_ref);
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /* make the changes with the locks held to prevent chown/chown races */
718 ret = -EACCES;
719 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 if (!capable(CAP_SYS_ADMIN)) {
722 /* only the sysadmin can chown a key to some other UID */
723 if (uid != (uid_t) -1 && key->uid != uid)
Fredrik Tolf58016492006-06-26 00:24:51 -0700724 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 /* only the sysadmin can set the key's GID to a group other
727 * than one of those that the current process subscribes to */
728 if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
Fredrik Tolf58016492006-06-26 00:24:51 -0700729 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731
Fredrik Tolf58016492006-06-26 00:24:51 -0700732 /* change the UID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (uid != (uid_t) -1 && uid != key->uid) {
Fredrik Tolf58016492006-06-26 00:24:51 -0700734 ret = -ENOMEM;
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -0600735 newowner = key_user_lookup(uid, current_user_ns());
Fredrik Tolf58016492006-06-26 00:24:51 -0700736 if (!newowner)
737 goto error_put;
738
739 /* transfer the quota burden to the new user */
740 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
David Howells0b77f5b2008-04-29 01:01:32 -0700741 unsigned maxkeys = (uid == 0) ?
742 key_quota_root_maxkeys : key_quota_maxkeys;
743 unsigned maxbytes = (uid == 0) ?
744 key_quota_root_maxbytes : key_quota_maxbytes;
745
Fredrik Tolf58016492006-06-26 00:24:51 -0700746 spin_lock(&newowner->lock);
David Howells0b77f5b2008-04-29 01:01:32 -0700747 if (newowner->qnkeys + 1 >= maxkeys ||
748 newowner->qnbytes + key->quotalen >= maxbytes ||
749 newowner->qnbytes + key->quotalen <
750 newowner->qnbytes)
Fredrik Tolf58016492006-06-26 00:24:51 -0700751 goto quota_overrun;
752
753 newowner->qnkeys++;
754 newowner->qnbytes += key->quotalen;
755 spin_unlock(&newowner->lock);
756
757 spin_lock(&key->user->lock);
758 key->user->qnkeys--;
759 key->user->qnbytes -= key->quotalen;
760 spin_unlock(&key->user->lock);
761 }
762
763 atomic_dec(&key->user->nkeys);
764 atomic_inc(&newowner->nkeys);
765
766 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
767 atomic_dec(&key->user->nikeys);
768 atomic_inc(&newowner->nikeys);
769 }
770
771 zapowner = key->user;
772 key->user = newowner;
773 key->uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775
776 /* change the GID */
777 if (gid != (gid_t) -1)
778 key->gid = gid;
779
780 ret = 0;
781
Fredrik Tolf58016492006-06-26 00:24:51 -0700782error_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 up_write(&key->sem);
784 key_put(key);
Fredrik Tolf58016492006-06-26 00:24:51 -0700785 if (zapowner)
786 key_user_put(zapowner);
787error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return ret;
789
Fredrik Tolf58016492006-06-26 00:24:51 -0700790quota_overrun:
791 spin_unlock(&newowner->lock);
792 zapowner = newowner;
793 ret = -EDQUOT;
794 goto error_put;
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796} /* end keyctl_chown_key() */
797
798/*****************************************************************************/
799/*
800 * change the permission mask on a key
801 * - the keyring owned by the changer
802 * - implements keyctl(KEYCTL_SETPERM)
803 */
804long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
805{
806 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100807 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 long ret;
809
810 ret = -EINVAL;
David Howells664cceb2005-09-28 17:03:15 +0100811 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 goto error;
813
David Howells55931222009-09-02 09:13:45 +0100814 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
815 KEY_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100816 if (IS_ERR(key_ref)) {
817 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 goto error;
819 }
820
David Howells664cceb2005-09-28 17:03:15 +0100821 key = key_ref_to_ptr(key_ref);
822
David Howells76d8aea2005-06-23 22:00:49 -0700823 /* make the changes with the locks held to prevent chown/chmod races */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 ret = -EACCES;
825 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
David Howells76d8aea2005-06-23 22:00:49 -0700827 /* if we're not the sysadmin, we can only change a key that we own */
David Howells47d804b2008-11-14 10:39:11 +1100828 if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) {
David Howells76d8aea2005-06-23 22:00:49 -0700829 key->perm = perm;
830 ret = 0;
831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 up_write(&key->sem);
834 key_put(key);
David Howells76d8aea2005-06-23 22:00:49 -0700835error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return ret;
837
838} /* end keyctl_setperm_key() */
839
David Howells8bbf49762008-11-14 10:39:14 +1100840/*
841 * get the destination keyring for instantiation
842 */
843static long get_instantiation_keyring(key_serial_t ringid,
844 struct request_key_auth *rka,
845 struct key **_dest_keyring)
846{
847 key_ref_t dkref;
848
David Howellseca1bf52008-12-29 00:41:51 +0000849 *_dest_keyring = NULL;
850
David Howells8bbf49762008-11-14 10:39:14 +1100851 /* just return a NULL pointer if we weren't asked to make a link */
David Howellseca1bf52008-12-29 00:41:51 +0000852 if (ringid == 0)
David Howells8bbf49762008-11-14 10:39:14 +1100853 return 0;
David Howells8bbf49762008-11-14 10:39:14 +1100854
855 /* if a specific keyring is nominated by ID, then use that */
856 if (ringid > 0) {
David Howells55931222009-09-02 09:13:45 +0100857 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
David Howells8bbf49762008-11-14 10:39:14 +1100858 if (IS_ERR(dkref))
859 return PTR_ERR(dkref);
860 *_dest_keyring = key_ref_to_ptr(dkref);
861 return 0;
862 }
863
864 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
865 return -EINVAL;
866
867 /* otherwise specify the destination keyring recorded in the
868 * authorisation key (any KEY_SPEC_*_KEYRING) */
869 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
870 *_dest_keyring = rka->dest_keyring;
871 return 0;
872 }
873
874 return -ENOKEY;
875}
876
David Howellsd84f4f92008-11-14 10:39:23 +1100877/*
878 * change the request_key authorisation key on the current process
879 */
880static int keyctl_change_reqkey_auth(struct key *key)
881{
882 struct cred *new;
883
884 new = prepare_creds();
885 if (!new)
886 return -ENOMEM;
887
888 key_put(new->request_key_auth);
889 new->request_key_auth = key_get(key);
890
891 return commit_creds(new);
892}
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894/*****************************************************************************/
895/*
896 * instantiate the key with the specified payload, and, if one is given, link
897 * the key into the keyring
898 */
899long keyctl_instantiate_key(key_serial_t id,
900 const void __user *_payload,
901 size_t plen,
902 key_serial_t ringid)
903{
David Howellsd84f4f92008-11-14 10:39:23 +1100904 const struct cred *cred = current_cred();
David Howells3e301482005-06-23 22:00:56 -0700905 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +1100906 struct key *instkey, *dest_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 void *payload;
908 long ret;
David Howells38bbca62008-04-29 01:01:19 -0700909 bool vm = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
David Howellsd84f4f92008-11-14 10:39:23 +1100911 kenter("%d,,%zu,%d", id, plen, ringid);
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 ret = -EINVAL;
David Howells38bbca62008-04-29 01:01:19 -0700914 if (plen > 1024 * 1024 - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 goto error;
916
David Howellsb5f545c2006-01-08 01:02:47 -0800917 /* the appropriate instantiation authorisation key must have been
918 * assumed before calling this */
919 ret = -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +1100920 instkey = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -0800921 if (!instkey)
922 goto error;
923
924 rka = instkey->payload.data;
925 if (rka->target_key->serial != id)
926 goto error;
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 /* pull the payload in if one was supplied */
929 payload = NULL;
930
931 if (_payload) {
932 ret = -ENOMEM;
933 payload = kmalloc(plen, GFP_KERNEL);
David Howells38bbca62008-04-29 01:01:19 -0700934 if (!payload) {
935 if (plen <= PAGE_SIZE)
936 goto error;
937 vm = true;
938 payload = vmalloc(plen);
939 if (!payload)
940 goto error;
941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 ret = -EFAULT;
944 if (copy_from_user(payload, _payload, plen) != 0)
945 goto error2;
946 }
947
David Howells3e301482005-06-23 22:00:56 -0700948 /* find the destination keyring amongst those belonging to the
949 * requesting task */
David Howells8bbf49762008-11-14 10:39:14 +1100950 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
951 if (ret < 0)
952 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 /* instantiate the key and link it into a keyring */
David Howells3e301482005-06-23 22:00:56 -0700955 ret = key_instantiate_and_link(rka->target_key, payload, plen,
David Howells8bbf49762008-11-14 10:39:14 +1100956 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
David Howells8bbf49762008-11-14 10:39:14 +1100958 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -0800959
960 /* discard the assumed authority if it's just been disabled by
961 * instantiation of the key */
David Howellsd84f4f92008-11-14 10:39:23 +1100962 if (ret == 0)
963 keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -0800964
965error2:
David Howells38bbca62008-04-29 01:01:19 -0700966 if (!vm)
967 kfree(payload);
968 else
969 vfree(payload);
David Howellsb5f545c2006-01-08 01:02:47 -0800970error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return ret;
972
973} /* end keyctl_instantiate_key() */
974
975/*****************************************************************************/
976/*
977 * negatively instantiate the key with the given timeout (in seconds), and, if
978 * one is given, link the key into the keyring
979 */
980long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
981{
David Howellsd84f4f92008-11-14 10:39:23 +1100982 const struct cred *cred = current_cred();
David Howells3e301482005-06-23 22:00:56 -0700983 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +1100984 struct key *instkey, *dest_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 long ret;
986
David Howellsd84f4f92008-11-14 10:39:23 +1100987 kenter("%d,%u,%d", id, timeout, ringid);
988
David Howellsb5f545c2006-01-08 01:02:47 -0800989 /* the appropriate instantiation authorisation key must have been
990 * assumed before calling this */
991 ret = -EPERM;
David Howellsd84f4f92008-11-14 10:39:23 +1100992 instkey = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -0800993 if (!instkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
David Howells3e301482005-06-23 22:00:56 -0700996 rka = instkey->payload.data;
David Howellsb5f545c2006-01-08 01:02:47 -0800997 if (rka->target_key->serial != id)
998 goto error;
David Howells3e301482005-06-23 22:00:56 -0700999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 /* find the destination keyring if present (which must also be
1001 * writable) */
David Howells8bbf49762008-11-14 10:39:14 +11001002 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1003 if (ret < 0)
1004 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 /* instantiate the key and link it into a keyring */
David Howells664cceb2005-09-28 17:03:15 +01001007 ret = key_negate_and_link(rka->target_key, timeout,
David Howells8bbf49762008-11-14 10:39:14 +11001008 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
David Howells8bbf49762008-11-14 10:39:14 +11001010 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -08001011
1012 /* discard the assumed authority if it's just been disabled by
1013 * instantiation of the key */
David Howellsd84f4f92008-11-14 10:39:23 +11001014 if (ret == 0)
1015 keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001016
1017error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return ret;
1019
1020} /* end keyctl_negate_key() */
1021
1022/*****************************************************************************/
1023/*
David Howells3e301482005-06-23 22:00:56 -07001024 * set the default keyring in which request_key() will cache keys
1025 * - return the old setting
1026 */
1027long keyctl_set_reqkey_keyring(int reqkey_defl)
1028{
David Howellsd84f4f92008-11-14 10:39:23 +11001029 struct cred *new;
1030 int ret, old_setting;
1031
1032 old_setting = current_cred_xxx(jit_keyring);
1033
1034 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1035 return old_setting;
1036
1037 new = prepare_creds();
1038 if (!new)
1039 return -ENOMEM;
David Howells3e301482005-06-23 22:00:56 -07001040
1041 switch (reqkey_defl) {
1042 case KEY_REQKEY_DEFL_THREAD_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001043 ret = install_thread_keyring_to_cred(new);
David Howells3e301482005-06-23 22:00:56 -07001044 if (ret < 0)
David Howellsd84f4f92008-11-14 10:39:23 +11001045 goto error;
David Howells3e301482005-06-23 22:00:56 -07001046 goto set;
1047
1048 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001049 ret = install_process_keyring_to_cred(new);
1050 if (ret < 0) {
1051 if (ret != -EEXIST)
1052 goto error;
1053 ret = 0;
1054 }
1055 goto set;
David Howells3e301482005-06-23 22:00:56 -07001056
1057 case KEY_REQKEY_DEFL_DEFAULT:
1058 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1059 case KEY_REQKEY_DEFL_USER_KEYRING:
1060 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
David Howellsd84f4f92008-11-14 10:39:23 +11001061 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1062 goto set;
David Howells3e301482005-06-23 22:00:56 -07001063
1064 case KEY_REQKEY_DEFL_NO_CHANGE:
David Howells3e301482005-06-23 22:00:56 -07001065 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1066 default:
David Howellsd84f4f92008-11-14 10:39:23 +11001067 ret = -EINVAL;
1068 goto error;
David Howells3e301482005-06-23 22:00:56 -07001069 }
1070
David Howellsd84f4f92008-11-14 10:39:23 +11001071set:
1072 new->jit_keyring = reqkey_defl;
1073 commit_creds(new);
1074 return old_setting;
1075error:
1076 abort_creds(new);
1077 return -EINVAL;
1078
David Howells3e301482005-06-23 22:00:56 -07001079} /* end keyctl_set_reqkey_keyring() */
1080
1081/*****************************************************************************/
1082/*
David Howells017679c2006-01-08 01:02:43 -08001083 * set or clear the timeout for a key
1084 */
1085long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1086{
1087 struct timespec now;
1088 struct key *key;
1089 key_ref_t key_ref;
1090 time_t expiry;
1091 long ret;
1092
David Howells55931222009-09-02 09:13:45 +01001093 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1094 KEY_SETATTR);
David Howells017679c2006-01-08 01:02:43 -08001095 if (IS_ERR(key_ref)) {
1096 ret = PTR_ERR(key_ref);
1097 goto error;
1098 }
1099
1100 key = key_ref_to_ptr(key_ref);
1101
1102 /* make the changes with the locks held to prevent races */
1103 down_write(&key->sem);
1104
1105 expiry = 0;
1106 if (timeout > 0) {
1107 now = current_kernel_time();
1108 expiry = now.tv_sec + timeout;
1109 }
1110
1111 key->expiry = expiry;
1112
1113 up_write(&key->sem);
1114 key_put(key);
1115
1116 ret = 0;
1117error:
1118 return ret;
1119
1120} /* end keyctl_set_timeout() */
1121
1122/*****************************************************************************/
1123/*
David Howellsb5f545c2006-01-08 01:02:47 -08001124 * assume the authority to instantiate the specified key
1125 */
1126long keyctl_assume_authority(key_serial_t id)
1127{
1128 struct key *authkey;
1129 long ret;
1130
1131 /* special key IDs aren't permitted */
1132 ret = -EINVAL;
1133 if (id < 0)
1134 goto error;
1135
1136 /* we divest ourselves of authority if given an ID of 0 */
1137 if (id == 0) {
David Howellsd84f4f92008-11-14 10:39:23 +11001138 ret = keyctl_change_reqkey_auth(NULL);
David Howellsb5f545c2006-01-08 01:02:47 -08001139 goto error;
1140 }
1141
1142 /* attempt to assume the authority temporarily granted to us whilst we
1143 * instantiate the specified key
1144 * - the authorisation key must be in the current task's keyrings
1145 * somewhere
1146 */
1147 authkey = key_get_instantiation_authkey(id);
1148 if (IS_ERR(authkey)) {
1149 ret = PTR_ERR(authkey);
1150 goto error;
1151 }
1152
David Howellsd84f4f92008-11-14 10:39:23 +11001153 ret = keyctl_change_reqkey_auth(authkey);
1154 if (ret < 0)
1155 goto error;
1156 key_put(authkey);
David Howellsb5f545c2006-01-08 01:02:47 -08001157
David Howellsd84f4f92008-11-14 10:39:23 +11001158 ret = authkey->serial;
David Howellsb5f545c2006-01-08 01:02:47 -08001159error:
1160 return ret;
1161
1162} /* end keyctl_assume_authority() */
1163
David Howells70a5bb72008-04-29 01:01:26 -07001164/*
1165 * get the security label of a key
1166 * - the key must grant us view permission
1167 * - if there's a buffer, we place up to buflen bytes of data into it
1168 * - unless there's an error, we return the amount of information available,
1169 * irrespective of how much we may have copied (including the terminal NUL)
1170 * - implements keyctl(KEYCTL_GET_SECURITY)
1171 */
1172long keyctl_get_security(key_serial_t keyid,
1173 char __user *buffer,
1174 size_t buflen)
1175{
1176 struct key *key, *instkey;
1177 key_ref_t key_ref;
1178 char *context;
1179 long ret;
1180
David Howells55931222009-09-02 09:13:45 +01001181 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
David Howells70a5bb72008-04-29 01:01:26 -07001182 if (IS_ERR(key_ref)) {
1183 if (PTR_ERR(key_ref) != -EACCES)
1184 return PTR_ERR(key_ref);
1185
1186 /* viewing a key under construction is also permitted if we
1187 * have the authorisation token handy */
1188 instkey = key_get_instantiation_authkey(keyid);
1189 if (IS_ERR(instkey))
1190 return PTR_ERR(key_ref);
1191 key_put(instkey);
1192
David Howells55931222009-09-02 09:13:45 +01001193 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
David Howells70a5bb72008-04-29 01:01:26 -07001194 if (IS_ERR(key_ref))
1195 return PTR_ERR(key_ref);
1196 }
1197
1198 key = key_ref_to_ptr(key_ref);
1199 ret = security_key_getsecurity(key, &context);
1200 if (ret == 0) {
1201 /* if no information was returned, give userspace an empty
1202 * string */
1203 ret = 1;
1204 if (buffer && buflen > 0 &&
1205 copy_to_user(buffer, "", 1) != 0)
1206 ret = -EFAULT;
1207 } else if (ret > 0) {
1208 /* return as much data as there's room for */
1209 if (buffer && buflen > 0) {
1210 if (buflen > ret)
1211 buflen = ret;
1212
1213 if (copy_to_user(buffer, context, buflen) != 0)
1214 ret = -EFAULT;
1215 }
1216
1217 kfree(context);
1218 }
1219
1220 key_ref_put(key_ref);
1221 return ret;
1222}
1223
David Howellsb5f545c2006-01-08 01:02:47 -08001224/*****************************************************************************/
1225/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 * the key control system call
1227 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001228SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1229 unsigned long, arg4, unsigned long, arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 switch (option) {
1232 case KEYCTL_GET_KEYRING_ID:
1233 return keyctl_get_keyring_ID((key_serial_t) arg2,
1234 (int) arg3);
1235
1236 case KEYCTL_JOIN_SESSION_KEYRING:
1237 return keyctl_join_session_keyring((const char __user *) arg2);
1238
1239 case KEYCTL_UPDATE:
1240 return keyctl_update_key((key_serial_t) arg2,
1241 (const void __user *) arg3,
1242 (size_t) arg4);
1243
1244 case KEYCTL_REVOKE:
1245 return keyctl_revoke_key((key_serial_t) arg2);
1246
1247 case KEYCTL_DESCRIBE:
1248 return keyctl_describe_key((key_serial_t) arg2,
1249 (char __user *) arg3,
1250 (unsigned) arg4);
1251
1252 case KEYCTL_CLEAR:
1253 return keyctl_keyring_clear((key_serial_t) arg2);
1254
1255 case KEYCTL_LINK:
1256 return keyctl_keyring_link((key_serial_t) arg2,
1257 (key_serial_t) arg3);
1258
1259 case KEYCTL_UNLINK:
1260 return keyctl_keyring_unlink((key_serial_t) arg2,
1261 (key_serial_t) arg3);
1262
1263 case KEYCTL_SEARCH:
1264 return keyctl_keyring_search((key_serial_t) arg2,
1265 (const char __user *) arg3,
1266 (const char __user *) arg4,
1267 (key_serial_t) arg5);
1268
1269 case KEYCTL_READ:
1270 return keyctl_read_key((key_serial_t) arg2,
1271 (char __user *) arg3,
1272 (size_t) arg4);
1273
1274 case KEYCTL_CHOWN:
1275 return keyctl_chown_key((key_serial_t) arg2,
1276 (uid_t) arg3,
1277 (gid_t) arg4);
1278
1279 case KEYCTL_SETPERM:
1280 return keyctl_setperm_key((key_serial_t) arg2,
1281 (key_perm_t) arg3);
1282
1283 case KEYCTL_INSTANTIATE:
1284 return keyctl_instantiate_key((key_serial_t) arg2,
1285 (const void __user *) arg3,
1286 (size_t) arg4,
1287 (key_serial_t) arg5);
1288
1289 case KEYCTL_NEGATE:
1290 return keyctl_negate_key((key_serial_t) arg2,
1291 (unsigned) arg3,
1292 (key_serial_t) arg4);
1293
David Howells3e301482005-06-23 22:00:56 -07001294 case KEYCTL_SET_REQKEY_KEYRING:
1295 return keyctl_set_reqkey_keyring(arg2);
1296
David Howells017679c2006-01-08 01:02:43 -08001297 case KEYCTL_SET_TIMEOUT:
1298 return keyctl_set_timeout((key_serial_t) arg2,
1299 (unsigned) arg3);
1300
David Howellsb5f545c2006-01-08 01:02:47 -08001301 case KEYCTL_ASSUME_AUTHORITY:
1302 return keyctl_assume_authority((key_serial_t) arg2);
1303
David Howells70a5bb72008-04-29 01:01:26 -07001304 case KEYCTL_GET_SECURITY:
1305 return keyctl_get_security((key_serial_t) arg2,
James Morris90bd49a2008-12-29 14:35:35 +11001306 (char __user *) arg3,
David Howells70a5bb72008-04-29 01:01:26 -07001307 (size_t) arg4);
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 default:
1310 return -EOPNOTSUPP;
1311 }
1312
1313} /* end sys_keyctl() */