blob: fcce331eca72da78f3c9cce7eca232b56d3d3922 [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 */
57asmlinkage long sys_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)
62{
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 Howells8bbf49762008-11-14 10:39:14 +1100106 keyring_ref = lookup_user_key(ringid, 1, 0, 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 */
149asmlinkage long sys_request_key(const char __user *_type,
150 const char __user *_description,
151 const char __user *_callout_info,
152 key_serial_t destringid)
153{
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 Howells8bbf49762008-11-14 10:39:14 +1100188 dest_ref = lookup_user_key(destringid, 1, 0, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100189 if (IS_ERR(dest_ref)) {
190 ret = PTR_ERR(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 goto error3;
192 }
193 }
194
195 /* find the key type */
196 ktype = key_type_lookup(type);
197 if (IS_ERR(ktype)) {
198 ret = PTR_ERR(ktype);
199 goto error4;
200 }
201
202 /* do the search */
David Howells4a38e122008-04-29 01:01:24 -0700203 key = request_key_and_link(ktype, description, callout_info,
204 callout_len, NULL, key_ref_to_ptr(dest_ref),
David Howells7e047ef2006-06-26 00:24:50 -0700205 KEY_ALLOC_IN_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (IS_ERR(key)) {
207 ret = PTR_ERR(key);
208 goto error5;
209 }
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 ret = key->serial;
212
David Howells3e301482005-06-23 22:00:56 -0700213 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 error5:
215 key_type_put(ktype);
216 error4:
David Howells664cceb2005-09-28 17:03:15 +0100217 key_ref_put(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 error3:
219 kfree(callout_info);
220 error2:
221 kfree(description);
222 error:
223 return ret;
224
225} /* end sys_request_key() */
226
227/*****************************************************************************/
228/*
229 * get the ID of the specified process keyring
230 * - the keyring must have search permission to be found
231 * - implements keyctl(KEYCTL_GET_KEYRING_ID)
232 */
233long keyctl_get_keyring_ID(key_serial_t id, int create)
234{
David Howells664cceb2005-09-28 17:03:15 +0100235 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 long ret;
237
David Howells8bbf49762008-11-14 10:39:14 +1100238 key_ref = lookup_user_key(id, create, 0, KEY_SEARCH);
David Howells664cceb2005-09-28 17:03:15 +0100239 if (IS_ERR(key_ref)) {
240 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goto error;
242 }
243
David Howells664cceb2005-09-28 17:03:15 +0100244 ret = key_ref_to_ptr(key_ref)->serial;
245 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 error:
247 return ret;
248
249} /* end keyctl_get_keyring_ID() */
250
251/*****************************************************************************/
252/*
253 * join the session keyring
254 * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING)
255 */
256long keyctl_join_session_keyring(const char __user *_name)
257{
258 char *name;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800259 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 /* fetch the name from userspace */
262 name = NULL;
263 if (_name) {
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800264 name = strndup_user(_name, PAGE_SIZE);
265 if (IS_ERR(name)) {
266 ret = PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
271 /* join the session */
272 ret = join_session_keyring(name);
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 error:
275 return ret;
276
277} /* end keyctl_join_session_keyring() */
278
279/*****************************************************************************/
280/*
281 * update a key's data payload
282 * - the key must be writable
283 * - implements keyctl(KEYCTL_UPDATE)
284 */
285long keyctl_update_key(key_serial_t id,
286 const void __user *_payload,
287 size_t plen)
288{
David Howells664cceb2005-09-28 17:03:15 +0100289 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 void *payload;
291 long ret;
292
293 ret = -EINVAL;
294 if (plen > PAGE_SIZE)
295 goto error;
296
297 /* pull the payload in if one was supplied */
298 payload = NULL;
299 if (_payload) {
300 ret = -ENOMEM;
301 payload = kmalloc(plen, GFP_KERNEL);
302 if (!payload)
303 goto error;
304
305 ret = -EFAULT;
306 if (copy_from_user(payload, _payload, plen) != 0)
307 goto error2;
308 }
309
310 /* find the target key (which must be writable) */
David Howells8bbf49762008-11-14 10:39:14 +1100311 key_ref = lookup_user_key(id, 0, 0, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100312 if (IS_ERR(key_ref)) {
313 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 goto error2;
315 }
316
317 /* update the key */
David Howells664cceb2005-09-28 17:03:15 +0100318 ret = key_update(key_ref, payload, plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
David Howells664cceb2005-09-28 17:03:15 +0100320 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 error2:
322 kfree(payload);
323 error:
324 return ret;
325
326} /* end keyctl_update_key() */
327
328/*****************************************************************************/
329/*
330 * revoke a key
331 * - the key must be writable
332 * - implements keyctl(KEYCTL_REVOKE)
333 */
334long keyctl_revoke_key(key_serial_t id)
335{
David Howells664cceb2005-09-28 17:03:15 +0100336 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 long ret;
338
David Howells8bbf49762008-11-14 10:39:14 +1100339 key_ref = lookup_user_key(id, 0, 0, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100340 if (IS_ERR(key_ref)) {
341 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 goto error;
343 }
344
David Howells664cceb2005-09-28 17:03:15 +0100345 key_revoke(key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 ret = 0;
347
David Howells664cceb2005-09-28 17:03:15 +0100348 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 error:
David Howells1260f802005-08-04 11:50:01 +0100350 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352} /* end keyctl_revoke_key() */
353
354/*****************************************************************************/
355/*
356 * clear the specified process keyring
357 * - the keyring must be writable
358 * - implements keyctl(KEYCTL_CLEAR)
359 */
360long keyctl_keyring_clear(key_serial_t ringid)
361{
David Howells664cceb2005-09-28 17:03:15 +0100362 key_ref_t keyring_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 long ret;
364
David Howells8bbf49762008-11-14 10:39:14 +1100365 keyring_ref = lookup_user_key(ringid, 1, 0, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100366 if (IS_ERR(keyring_ref)) {
367 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 goto error;
369 }
370
David Howells664cceb2005-09-28 17:03:15 +0100371 ret = keyring_clear(key_ref_to_ptr(keyring_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
David Howells664cceb2005-09-28 17:03:15 +0100373 key_ref_put(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 error:
375 return ret;
376
377} /* end keyctl_keyring_clear() */
378
379/*****************************************************************************/
380/*
381 * link a key into a keyring
382 * - the keyring must be writable
383 * - the key must be linkable
384 * - implements keyctl(KEYCTL_LINK)
385 */
386long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
387{
David Howells664cceb2005-09-28 17:03:15 +0100388 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 long ret;
390
David Howells8bbf49762008-11-14 10:39:14 +1100391 keyring_ref = lookup_user_key(ringid, 1, 0, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100392 if (IS_ERR(keyring_ref)) {
393 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 goto error;
395 }
396
David Howells8bbf49762008-11-14 10:39:14 +1100397 key_ref = lookup_user_key(id, 1, 0, KEY_LINK);
David Howells664cceb2005-09-28 17:03:15 +0100398 if (IS_ERR(key_ref)) {
399 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 goto error2;
401 }
402
David Howells664cceb2005-09-28 17:03:15 +0100403 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
David Howells664cceb2005-09-28 17:03:15 +0100405 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 error2:
David Howells664cceb2005-09-28 17:03:15 +0100407 key_ref_put(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 error:
409 return ret;
410
411} /* end keyctl_keyring_link() */
412
413/*****************************************************************************/
414/*
415 * unlink the first attachment of a key from a keyring
416 * - the keyring must be writable
417 * - we don't need any permissions on the key
418 * - implements keyctl(KEYCTL_UNLINK)
419 */
420long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
421{
David Howells664cceb2005-09-28 17:03:15 +0100422 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 long ret;
424
David Howells8bbf49762008-11-14 10:39:14 +1100425 keyring_ref = lookup_user_key(ringid, 0, 0, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100426 if (IS_ERR(keyring_ref)) {
427 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 goto error;
429 }
430
David Howells8bbf49762008-11-14 10:39:14 +1100431 key_ref = lookup_user_key(id, 0, 0, 0);
David Howells664cceb2005-09-28 17:03:15 +0100432 if (IS_ERR(key_ref)) {
433 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 goto error2;
435 }
436
David Howells664cceb2005-09-28 17:03:15 +0100437 ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
David Howells664cceb2005-09-28 17:03:15 +0100439 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 error2:
David Howells664cceb2005-09-28 17:03:15 +0100441 key_ref_put(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 error:
443 return ret;
444
445} /* end keyctl_keyring_unlink() */
446
447/*****************************************************************************/
448/*
449 * describe a user key
450 * - the key must have view permission
451 * - if there's a buffer, we place up to buflen bytes of data into it
452 * - unless there's an error, we return the amount of description available,
453 * irrespective of how much we may have copied
454 * - the description is formatted thus:
455 * type;uid;gid;perm;description<NUL>
456 * - implements keyctl(KEYCTL_DESCRIBE)
457 */
458long keyctl_describe_key(key_serial_t keyid,
459 char __user *buffer,
460 size_t buflen)
461{
David Howells3e301482005-06-23 22:00:56 -0700462 struct key *key, *instkey;
David Howells664cceb2005-09-28 17:03:15 +0100463 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 char *tmpbuf;
465 long ret;
466
David Howells8bbf49762008-11-14 10:39:14 +1100467 key_ref = lookup_user_key(keyid, 0, 1, KEY_VIEW);
David Howells664cceb2005-09-28 17:03:15 +0100468 if (IS_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700469 /* viewing a key under construction is permitted if we have the
470 * authorisation token handy */
David Howells664cceb2005-09-28 17:03:15 +0100471 if (PTR_ERR(key_ref) == -EACCES) {
David Howells3e301482005-06-23 22:00:56 -0700472 instkey = key_get_instantiation_authkey(keyid);
473 if (!IS_ERR(instkey)) {
474 key_put(instkey);
David Howells8bbf49762008-11-14 10:39:14 +1100475 key_ref = lookup_user_key(keyid,
David Howells664cceb2005-09-28 17:03:15 +0100476 0, 1, 0);
477 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700478 goto okay;
479 }
480 }
481
David Howells664cceb2005-09-28 17:03:15 +0100482 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 goto error;
484 }
485
David Howells3e301482005-06-23 22:00:56 -0700486okay:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 /* calculate how much description we're going to return */
488 ret = -ENOMEM;
489 tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
490 if (!tmpbuf)
491 goto error2;
492
David Howells664cceb2005-09-28 17:03:15 +0100493 key = key_ref_to_ptr(key_ref);
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 ret = snprintf(tmpbuf, PAGE_SIZE - 1,
David Howells664cceb2005-09-28 17:03:15 +0100496 "%s;%d;%d;%08x;%s",
497 key_ref_to_ptr(key_ref)->type->name,
498 key_ref_to_ptr(key_ref)->uid,
499 key_ref_to_ptr(key_ref)->gid,
500 key_ref_to_ptr(key_ref)->perm,
501 key_ref_to_ptr(key_ref)->description ?
502 key_ref_to_ptr(key_ref)->description : ""
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 );
504
505 /* include a NUL char at the end of the data */
506 if (ret > PAGE_SIZE - 1)
507 ret = PAGE_SIZE - 1;
508 tmpbuf[ret] = 0;
509 ret++;
510
511 /* consider returning the data */
512 if (buffer && buflen > 0) {
513 if (buflen > ret)
514 buflen = ret;
515
516 if (copy_to_user(buffer, tmpbuf, buflen) != 0)
517 ret = -EFAULT;
518 }
519
520 kfree(tmpbuf);
521 error2:
David Howells664cceb2005-09-28 17:03:15 +0100522 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 error:
524 return ret;
525
526} /* end keyctl_describe_key() */
527
528/*****************************************************************************/
529/*
530 * search the specified keyring for a matching key
531 * - the start keyring must be searchable
532 * - nested keyrings may also be searched if they are searchable
533 * - only keys with search permission may be found
534 * - if a key is found, it will be attached to the destination keyring if
535 * there's one specified
536 * - implements keyctl(KEYCTL_SEARCH)
537 */
538long keyctl_keyring_search(key_serial_t ringid,
539 const char __user *_type,
540 const char __user *_description,
541 key_serial_t destringid)
542{
543 struct key_type *ktype;
David Howells664cceb2005-09-28 17:03:15 +0100544 key_ref_t keyring_ref, key_ref, dest_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 char type[32], *description;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800546 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 /* pull the type and description into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800549 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (ret < 0)
551 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800553 description = strndup_user(_description, PAGE_SIZE);
554 if (IS_ERR(description)) {
555 ret = PTR_ERR(description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 /* get the keyring at which to begin the search */
David Howells8bbf49762008-11-14 10:39:14 +1100560 keyring_ref = lookup_user_key(ringid, 0, 0, KEY_SEARCH);
David Howells664cceb2005-09-28 17:03:15 +0100561 if (IS_ERR(keyring_ref)) {
562 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 goto error2;
564 }
565
566 /* get the destination keyring if specified */
David Howells664cceb2005-09-28 17:03:15 +0100567 dest_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (destringid) {
David Howells8bbf49762008-11-14 10:39:14 +1100569 dest_ref = lookup_user_key(destringid, 1, 0, KEY_WRITE);
David Howells664cceb2005-09-28 17:03:15 +0100570 if (IS_ERR(dest_ref)) {
571 ret = PTR_ERR(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 goto error3;
573 }
574 }
575
576 /* find the key type */
577 ktype = key_type_lookup(type);
578 if (IS_ERR(ktype)) {
579 ret = PTR_ERR(ktype);
580 goto error4;
581 }
582
583 /* do the search */
David Howells664cceb2005-09-28 17:03:15 +0100584 key_ref = keyring_search(keyring_ref, ktype, description);
585 if (IS_ERR(key_ref)) {
586 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 /* treat lack or presence of a negative key the same */
589 if (ret == -EAGAIN)
590 ret = -ENOKEY;
591 goto error5;
592 }
593
594 /* link the resulting key to the destination keyring if we can */
David Howells664cceb2005-09-28 17:03:15 +0100595 if (dest_ref) {
David Howells29db9192005-10-30 15:02:44 -0800596 ret = key_permission(key_ref, KEY_LINK);
597 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 goto error6;
599
David Howells664cceb2005-09-28 17:03:15 +0100600 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (ret < 0)
602 goto error6;
603 }
604
David Howells664cceb2005-09-28 17:03:15 +0100605 ret = key_ref_to_ptr(key_ref)->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 error6:
David Howells664cceb2005-09-28 17:03:15 +0100608 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 error5:
610 key_type_put(ktype);
611 error4:
David Howells664cceb2005-09-28 17:03:15 +0100612 key_ref_put(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 error3:
David Howells664cceb2005-09-28 17:03:15 +0100614 key_ref_put(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 error2:
616 kfree(description);
617 error:
618 return ret;
619
620} /* end keyctl_keyring_search() */
621
622/*****************************************************************************/
623/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 * read a user key's payload
625 * - the keyring must be readable or the key must be searchable from the
626 * process's keyrings
627 * - if there's a buffer, we place up to buflen bytes of data into it
628 * - unless there's an error, we return the amount of data in the key,
629 * irrespective of how much we may have copied
630 * - implements keyctl(KEYCTL_READ)
631 */
632long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
633{
David Howells664cceb2005-09-28 17:03:15 +0100634 struct key *key;
635 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 long ret;
637
638 /* find the key first */
David Howells8bbf49762008-11-14 10:39:14 +1100639 key_ref = lookup_user_key(keyid, 0, 0, 0);
David Howells664cceb2005-09-28 17:03:15 +0100640 if (IS_ERR(key_ref)) {
641 ret = -ENOKEY;
642 goto error;
643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
David Howells664cceb2005-09-28 17:03:15 +0100645 key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
David Howells664cceb2005-09-28 17:03:15 +0100647 /* see if we can read it directly */
David Howells29db9192005-10-30 15:02:44 -0800648 ret = key_permission(key_ref, KEY_READ);
649 if (ret == 0)
David Howells664cceb2005-09-28 17:03:15 +0100650 goto can_read_key;
David Howells29db9192005-10-30 15:02:44 -0800651 if (ret != -EACCES)
652 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100653
654 /* we can't; see if it's searchable from this process's keyrings
655 * - we automatically take account of the fact that it may be
656 * dangling off an instantiation key
657 */
658 if (!is_key_possessed(key_ref)) {
659 ret = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 goto error2;
661 }
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /* the key is probably readable - now try to read it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 can_read_key:
665 ret = key_validate(key);
666 if (ret == 0) {
667 ret = -EOPNOTSUPP;
668 if (key->type->read) {
669 /* read the data with the semaphore held (since we
670 * might sleep) */
671 down_read(&key->sem);
672 ret = key->type->read(key, buffer, buflen);
673 up_read(&key->sem);
674 }
675 }
676
677 error2:
678 key_put(key);
679 error:
680 return ret;
681
682} /* end keyctl_read_key() */
683
684/*****************************************************************************/
685/*
686 * change the ownership of a key
687 * - the keyring owned by the changer
688 * - if the uid or gid is -1, then that parameter is not changed
689 * - implements keyctl(KEYCTL_CHOWN)
690 */
691long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
692{
Fredrik Tolf58016492006-06-26 00:24:51 -0700693 struct key_user *newowner, *zapowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100695 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 long ret;
697
698 ret = 0;
699 if (uid == (uid_t) -1 && gid == (gid_t) -1)
700 goto error;
701
David Howells8bbf49762008-11-14 10:39:14 +1100702 key_ref = lookup_user_key(id, 1, 1, KEY_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100703 if (IS_ERR(key_ref)) {
704 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 goto error;
706 }
707
David Howells664cceb2005-09-28 17:03:15 +0100708 key = key_ref_to_ptr(key_ref);
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /* make the changes with the locks held to prevent chown/chown races */
711 ret = -EACCES;
712 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 if (!capable(CAP_SYS_ADMIN)) {
715 /* only the sysadmin can chown a key to some other UID */
716 if (uid != (uid_t) -1 && key->uid != uid)
Fredrik Tolf58016492006-06-26 00:24:51 -0700717 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 /* only the sysadmin can set the key's GID to a group other
720 * than one of those that the current process subscribes to */
721 if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
Fredrik Tolf58016492006-06-26 00:24:51 -0700722 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724
Fredrik Tolf58016492006-06-26 00:24:51 -0700725 /* change the UID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (uid != (uid_t) -1 && uid != key->uid) {
Fredrik Tolf58016492006-06-26 00:24:51 -0700727 ret = -ENOMEM;
728 newowner = key_user_lookup(uid);
729 if (!newowner)
730 goto error_put;
731
732 /* transfer the quota burden to the new user */
733 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
David Howells0b77f5b2008-04-29 01:01:32 -0700734 unsigned maxkeys = (uid == 0) ?
735 key_quota_root_maxkeys : key_quota_maxkeys;
736 unsigned maxbytes = (uid == 0) ?
737 key_quota_root_maxbytes : key_quota_maxbytes;
738
Fredrik Tolf58016492006-06-26 00:24:51 -0700739 spin_lock(&newowner->lock);
David Howells0b77f5b2008-04-29 01:01:32 -0700740 if (newowner->qnkeys + 1 >= maxkeys ||
741 newowner->qnbytes + key->quotalen >= maxbytes ||
742 newowner->qnbytes + key->quotalen <
743 newowner->qnbytes)
Fredrik Tolf58016492006-06-26 00:24:51 -0700744 goto quota_overrun;
745
746 newowner->qnkeys++;
747 newowner->qnbytes += key->quotalen;
748 spin_unlock(&newowner->lock);
749
750 spin_lock(&key->user->lock);
751 key->user->qnkeys--;
752 key->user->qnbytes -= key->quotalen;
753 spin_unlock(&key->user->lock);
754 }
755
756 atomic_dec(&key->user->nkeys);
757 atomic_inc(&newowner->nkeys);
758
759 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
760 atomic_dec(&key->user->nikeys);
761 atomic_inc(&newowner->nikeys);
762 }
763
764 zapowner = key->user;
765 key->user = newowner;
766 key->uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768
769 /* change the GID */
770 if (gid != (gid_t) -1)
771 key->gid = gid;
772
773 ret = 0;
774
Fredrik Tolf58016492006-06-26 00:24:51 -0700775error_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 up_write(&key->sem);
777 key_put(key);
Fredrik Tolf58016492006-06-26 00:24:51 -0700778 if (zapowner)
779 key_user_put(zapowner);
780error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return ret;
782
Fredrik Tolf58016492006-06-26 00:24:51 -0700783quota_overrun:
784 spin_unlock(&newowner->lock);
785 zapowner = newowner;
786 ret = -EDQUOT;
787 goto error_put;
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789} /* end keyctl_chown_key() */
790
791/*****************************************************************************/
792/*
793 * change the permission mask on a key
794 * - the keyring owned by the changer
795 * - implements keyctl(KEYCTL_SETPERM)
796 */
797long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
798{
799 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100800 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 long ret;
802
803 ret = -EINVAL;
David Howells664cceb2005-09-28 17:03:15 +0100804 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 goto error;
806
David Howells8bbf49762008-11-14 10:39:14 +1100807 key_ref = lookup_user_key(id, 1, 1, KEY_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100808 if (IS_ERR(key_ref)) {
809 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 goto error;
811 }
812
David Howells664cceb2005-09-28 17:03:15 +0100813 key = key_ref_to_ptr(key_ref);
814
David Howells76d8aea2005-06-23 22:00:49 -0700815 /* make the changes with the locks held to prevent chown/chmod races */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 ret = -EACCES;
817 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
David Howells76d8aea2005-06-23 22:00:49 -0700819 /* if we're not the sysadmin, we can only change a key that we own */
David Howells47d804b2008-11-14 10:39:11 +1100820 if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) {
David Howells76d8aea2005-06-23 22:00:49 -0700821 key->perm = perm;
822 ret = 0;
823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 up_write(&key->sem);
826 key_put(key);
David Howells76d8aea2005-06-23 22:00:49 -0700827error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return ret;
829
830} /* end keyctl_setperm_key() */
831
David Howells8bbf49762008-11-14 10:39:14 +1100832/*
833 * get the destination keyring for instantiation
834 */
835static long get_instantiation_keyring(key_serial_t ringid,
836 struct request_key_auth *rka,
837 struct key **_dest_keyring)
838{
839 key_ref_t dkref;
840
841 /* just return a NULL pointer if we weren't asked to make a link */
842 if (ringid == 0) {
843 *_dest_keyring = NULL;
844 return 0;
845 }
846
847 /* if a specific keyring is nominated by ID, then use that */
848 if (ringid > 0) {
849 dkref = lookup_user_key(ringid, 1, 0, KEY_WRITE);
850 if (IS_ERR(dkref))
851 return PTR_ERR(dkref);
852 *_dest_keyring = key_ref_to_ptr(dkref);
853 return 0;
854 }
855
856 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
857 return -EINVAL;
858
859 /* otherwise specify the destination keyring recorded in the
860 * authorisation key (any KEY_SPEC_*_KEYRING) */
861 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
862 *_dest_keyring = rka->dest_keyring;
863 return 0;
864 }
865
866 return -ENOKEY;
867}
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869/*****************************************************************************/
870/*
871 * instantiate the key with the specified payload, and, if one is given, link
872 * the key into the keyring
873 */
874long keyctl_instantiate_key(key_serial_t id,
875 const void __user *_payload,
876 size_t plen,
877 key_serial_t ringid)
878{
David Howells3e301482005-06-23 22:00:56 -0700879 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +1100880 struct key *instkey, *dest_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 void *payload;
882 long ret;
David Howells38bbca62008-04-29 01:01:19 -0700883 bool vm = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 ret = -EINVAL;
David Howells38bbca62008-04-29 01:01:19 -0700886 if (plen > 1024 * 1024 - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 goto error;
888
David Howellsb5f545c2006-01-08 01:02:47 -0800889 /* the appropriate instantiation authorisation key must have been
890 * assumed before calling this */
891 ret = -EPERM;
892 instkey = current->request_key_auth;
893 if (!instkey)
894 goto error;
895
896 rka = instkey->payload.data;
897 if (rka->target_key->serial != id)
898 goto error;
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 /* pull the payload in if one was supplied */
901 payload = NULL;
902
903 if (_payload) {
904 ret = -ENOMEM;
905 payload = kmalloc(plen, GFP_KERNEL);
David Howells38bbca62008-04-29 01:01:19 -0700906 if (!payload) {
907 if (plen <= PAGE_SIZE)
908 goto error;
909 vm = true;
910 payload = vmalloc(plen);
911 if (!payload)
912 goto error;
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 ret = -EFAULT;
916 if (copy_from_user(payload, _payload, plen) != 0)
917 goto error2;
918 }
919
David Howells3e301482005-06-23 22:00:56 -0700920 /* find the destination keyring amongst those belonging to the
921 * requesting task */
David Howells8bbf49762008-11-14 10:39:14 +1100922 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
923 if (ret < 0)
924 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 /* instantiate the key and link it into a keyring */
David Howells3e301482005-06-23 22:00:56 -0700927 ret = key_instantiate_and_link(rka->target_key, payload, plen,
David Howells8bbf49762008-11-14 10:39:14 +1100928 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
David Howells8bbf49762008-11-14 10:39:14 +1100930 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -0800931
932 /* discard the assumed authority if it's just been disabled by
933 * instantiation of the key */
934 if (ret == 0) {
935 key_put(current->request_key_auth);
936 current->request_key_auth = NULL;
937 }
938
939error2:
David Howells38bbca62008-04-29 01:01:19 -0700940 if (!vm)
941 kfree(payload);
942 else
943 vfree(payload);
David Howellsb5f545c2006-01-08 01:02:47 -0800944error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return ret;
946
947} /* end keyctl_instantiate_key() */
948
949/*****************************************************************************/
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 */
954long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
955{
David Howells3e301482005-06-23 22:00:56 -0700956 struct request_key_auth *rka;
David Howells8bbf49762008-11-14 10:39:14 +1100957 struct key *instkey, *dest_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 long ret;
959
David Howellsb5f545c2006-01-08 01:02:47 -0800960 /* the appropriate instantiation authorisation key must have been
961 * assumed before calling this */
962 ret = -EPERM;
963 instkey = current->request_key_auth;
964 if (!instkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
David Howells3e301482005-06-23 22:00:56 -0700967 rka = instkey->payload.data;
David Howellsb5f545c2006-01-08 01:02:47 -0800968 if (rka->target_key->serial != id)
969 goto error;
David Howells3e301482005-06-23 22:00:56 -0700970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /* find the destination keyring if present (which must also be
972 * writable) */
David Howells8bbf49762008-11-14 10:39:14 +1100973 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
974 if (ret < 0)
975 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 /* instantiate the key and link it into a keyring */
David Howells664cceb2005-09-28 17:03:15 +0100978 ret = key_negate_and_link(rka->target_key, timeout,
David Howells8bbf49762008-11-14 10:39:14 +1100979 dest_keyring, instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
David Howells8bbf49762008-11-14 10:39:14 +1100981 key_put(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -0800982
983 /* discard the assumed authority if it's just been disabled by
984 * instantiation of the key */
985 if (ret == 0) {
986 key_put(current->request_key_auth);
987 current->request_key_auth = NULL;
988 }
989
990error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return ret;
992
993} /* end keyctl_negate_key() */
994
995/*****************************************************************************/
996/*
David Howells3e301482005-06-23 22:00:56 -0700997 * set the default keyring in which request_key() will cache keys
998 * - return the old setting
999 */
1000long keyctl_set_reqkey_keyring(int reqkey_defl)
1001{
1002 int ret;
1003
1004 switch (reqkey_defl) {
1005 case KEY_REQKEY_DEFL_THREAD_KEYRING:
David Howells8bbf49762008-11-14 10:39:14 +11001006 ret = install_thread_keyring();
David Howells3e301482005-06-23 22:00:56 -07001007 if (ret < 0)
1008 return ret;
1009 goto set;
1010
1011 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
David Howells8bbf49762008-11-14 10:39:14 +11001012 ret = install_process_keyring();
David Howells3e301482005-06-23 22:00:56 -07001013 if (ret < 0)
1014 return ret;
1015
1016 case KEY_REQKEY_DEFL_DEFAULT:
1017 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1018 case KEY_REQKEY_DEFL_USER_KEYRING:
1019 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1020 set:
1021 current->jit_keyring = reqkey_defl;
1022
1023 case KEY_REQKEY_DEFL_NO_CHANGE:
1024 return current->jit_keyring;
1025
1026 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1027 default:
1028 return -EINVAL;
1029 }
1030
1031} /* end keyctl_set_reqkey_keyring() */
1032
1033/*****************************************************************************/
1034/*
David Howells017679c2006-01-08 01:02:43 -08001035 * set or clear the timeout for a key
1036 */
1037long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1038{
1039 struct timespec now;
1040 struct key *key;
1041 key_ref_t key_ref;
1042 time_t expiry;
1043 long ret;
1044
David Howells8bbf49762008-11-14 10:39:14 +11001045 key_ref = lookup_user_key(id, 1, 1, KEY_SETATTR);
David Howells017679c2006-01-08 01:02:43 -08001046 if (IS_ERR(key_ref)) {
1047 ret = PTR_ERR(key_ref);
1048 goto error;
1049 }
1050
1051 key = key_ref_to_ptr(key_ref);
1052
1053 /* make the changes with the locks held to prevent races */
1054 down_write(&key->sem);
1055
1056 expiry = 0;
1057 if (timeout > 0) {
1058 now = current_kernel_time();
1059 expiry = now.tv_sec + timeout;
1060 }
1061
1062 key->expiry = expiry;
1063
1064 up_write(&key->sem);
1065 key_put(key);
1066
1067 ret = 0;
1068error:
1069 return ret;
1070
1071} /* end keyctl_set_timeout() */
1072
1073/*****************************************************************************/
1074/*
David Howellsb5f545c2006-01-08 01:02:47 -08001075 * assume the authority to instantiate the specified key
1076 */
1077long keyctl_assume_authority(key_serial_t id)
1078{
1079 struct key *authkey;
1080 long ret;
1081
1082 /* special key IDs aren't permitted */
1083 ret = -EINVAL;
1084 if (id < 0)
1085 goto error;
1086
1087 /* we divest ourselves of authority if given an ID of 0 */
1088 if (id == 0) {
1089 key_put(current->request_key_auth);
1090 current->request_key_auth = NULL;
1091 ret = 0;
1092 goto error;
1093 }
1094
1095 /* attempt to assume the authority temporarily granted to us whilst we
1096 * instantiate the specified key
1097 * - the authorisation key must be in the current task's keyrings
1098 * somewhere
1099 */
1100 authkey = key_get_instantiation_authkey(id);
1101 if (IS_ERR(authkey)) {
1102 ret = PTR_ERR(authkey);
1103 goto error;
1104 }
1105
1106 key_put(current->request_key_auth);
1107 current->request_key_auth = authkey;
1108 ret = authkey->serial;
1109
1110error:
1111 return ret;
1112
1113} /* end keyctl_assume_authority() */
1114
David Howells70a5bb72008-04-29 01:01:26 -07001115/*
1116 * get the security label of a key
1117 * - the key must grant us view permission
1118 * - if there's a buffer, we place up to buflen bytes of data into it
1119 * - unless there's an error, we return the amount of information available,
1120 * irrespective of how much we may have copied (including the terminal NUL)
1121 * - implements keyctl(KEYCTL_GET_SECURITY)
1122 */
1123long keyctl_get_security(key_serial_t keyid,
1124 char __user *buffer,
1125 size_t buflen)
1126{
1127 struct key *key, *instkey;
1128 key_ref_t key_ref;
1129 char *context;
1130 long ret;
1131
David Howells8bbf49762008-11-14 10:39:14 +11001132 key_ref = lookup_user_key(keyid, 0, 1, KEY_VIEW);
David Howells70a5bb72008-04-29 01:01:26 -07001133 if (IS_ERR(key_ref)) {
1134 if (PTR_ERR(key_ref) != -EACCES)
1135 return PTR_ERR(key_ref);
1136
1137 /* viewing a key under construction is also permitted if we
1138 * have the authorisation token handy */
1139 instkey = key_get_instantiation_authkey(keyid);
1140 if (IS_ERR(instkey))
1141 return PTR_ERR(key_ref);
1142 key_put(instkey);
1143
David Howells8bbf49762008-11-14 10:39:14 +11001144 key_ref = lookup_user_key(keyid, 0, 1, 0);
David Howells70a5bb72008-04-29 01:01:26 -07001145 if (IS_ERR(key_ref))
1146 return PTR_ERR(key_ref);
1147 }
1148
1149 key = key_ref_to_ptr(key_ref);
1150 ret = security_key_getsecurity(key, &context);
1151 if (ret == 0) {
1152 /* if no information was returned, give userspace an empty
1153 * string */
1154 ret = 1;
1155 if (buffer && buflen > 0 &&
1156 copy_to_user(buffer, "", 1) != 0)
1157 ret = -EFAULT;
1158 } else if (ret > 0) {
1159 /* return as much data as there's room for */
1160 if (buffer && buflen > 0) {
1161 if (buflen > ret)
1162 buflen = ret;
1163
1164 if (copy_to_user(buffer, context, buflen) != 0)
1165 ret = -EFAULT;
1166 }
1167
1168 kfree(context);
1169 }
1170
1171 key_ref_put(key_ref);
1172 return ret;
1173}
1174
David Howellsb5f545c2006-01-08 01:02:47 -08001175/*****************************************************************************/
1176/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 * the key control system call
1178 */
1179asmlinkage long sys_keyctl(int option, unsigned long arg2, unsigned long arg3,
1180 unsigned long arg4, unsigned long arg5)
1181{
1182 switch (option) {
1183 case KEYCTL_GET_KEYRING_ID:
1184 return keyctl_get_keyring_ID((key_serial_t) arg2,
1185 (int) arg3);
1186
1187 case KEYCTL_JOIN_SESSION_KEYRING:
1188 return keyctl_join_session_keyring((const char __user *) arg2);
1189
1190 case KEYCTL_UPDATE:
1191 return keyctl_update_key((key_serial_t) arg2,
1192 (const void __user *) arg3,
1193 (size_t) arg4);
1194
1195 case KEYCTL_REVOKE:
1196 return keyctl_revoke_key((key_serial_t) arg2);
1197
1198 case KEYCTL_DESCRIBE:
1199 return keyctl_describe_key((key_serial_t) arg2,
1200 (char __user *) arg3,
1201 (unsigned) arg4);
1202
1203 case KEYCTL_CLEAR:
1204 return keyctl_keyring_clear((key_serial_t) arg2);
1205
1206 case KEYCTL_LINK:
1207 return keyctl_keyring_link((key_serial_t) arg2,
1208 (key_serial_t) arg3);
1209
1210 case KEYCTL_UNLINK:
1211 return keyctl_keyring_unlink((key_serial_t) arg2,
1212 (key_serial_t) arg3);
1213
1214 case KEYCTL_SEARCH:
1215 return keyctl_keyring_search((key_serial_t) arg2,
1216 (const char __user *) arg3,
1217 (const char __user *) arg4,
1218 (key_serial_t) arg5);
1219
1220 case KEYCTL_READ:
1221 return keyctl_read_key((key_serial_t) arg2,
1222 (char __user *) arg3,
1223 (size_t) arg4);
1224
1225 case KEYCTL_CHOWN:
1226 return keyctl_chown_key((key_serial_t) arg2,
1227 (uid_t) arg3,
1228 (gid_t) arg4);
1229
1230 case KEYCTL_SETPERM:
1231 return keyctl_setperm_key((key_serial_t) arg2,
1232 (key_perm_t) arg3);
1233
1234 case KEYCTL_INSTANTIATE:
1235 return keyctl_instantiate_key((key_serial_t) arg2,
1236 (const void __user *) arg3,
1237 (size_t) arg4,
1238 (key_serial_t) arg5);
1239
1240 case KEYCTL_NEGATE:
1241 return keyctl_negate_key((key_serial_t) arg2,
1242 (unsigned) arg3,
1243 (key_serial_t) arg4);
1244
David Howells3e301482005-06-23 22:00:56 -07001245 case KEYCTL_SET_REQKEY_KEYRING:
1246 return keyctl_set_reqkey_keyring(arg2);
1247
David Howells017679c2006-01-08 01:02:43 -08001248 case KEYCTL_SET_TIMEOUT:
1249 return keyctl_set_timeout((key_serial_t) arg2,
1250 (unsigned) arg3);
1251
David Howellsb5f545c2006-01-08 01:02:47 -08001252 case KEYCTL_ASSUME_AUTHORITY:
1253 return keyctl_assume_authority((key_serial_t) arg2);
1254
David Howells70a5bb72008-04-29 01:01:26 -07001255 case KEYCTL_GET_SECURITY:
1256 return keyctl_get_security((key_serial_t) arg2,
1257 (char *) arg3,
1258 (size_t) arg4);
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 default:
1261 return -EOPNOTSUPP;
1262 }
1263
1264} /* end sys_keyctl() */