blob: 1698bf90ee84a76e3f5cb0671e936fdba4efadb3 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/uaccess.h>
24#include "internal.h"
25
Davi Arnaut0cb409d2006-03-24 03:18:43 -080026static int key_get_type_from_user(char *type,
27 const char __user *_type,
28 unsigned len)
29{
30 int ret;
31
32 ret = strncpy_from_user(type, _type, len);
33
34 if (ret < 0)
35 return -EFAULT;
36
37 if (ret == 0 || ret >= len)
38 return -EINVAL;
39
40 if (type[0] == '.')
41 return -EPERM;
42
43 type[len - 1] = '\0';
44
45 return 0;
46}
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*****************************************************************************/
49/*
50 * extract the description of a new key from userspace and either add it as a
51 * new key to the specified keyring or update a matching key in that keyring
52 * - the keyring must be writable
53 * - returns the new key's serial number
54 * - implements add_key()
55 */
56asmlinkage long sys_add_key(const char __user *_type,
57 const char __user *_description,
58 const void __user *_payload,
59 size_t plen,
60 key_serial_t ringid)
61{
David Howells664cceb2005-09-28 17:03:15 +010062 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 char type[32], *description;
64 void *payload;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080065 long ret;
David Howells38bbca62008-04-29 01:01:19 -070066 bool vm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 ret = -EINVAL;
David Howells38bbca62008-04-29 01:01:19 -070069 if (plen > 1024 * 1024 - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 goto error;
71
72 /* draw all the data into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -080073 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (ret < 0)
75 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Davi Arnaut0cb409d2006-03-24 03:18:43 -080077 description = strndup_user(_description, PAGE_SIZE);
78 if (IS_ERR(description)) {
79 ret = PTR_ERR(description);
David Howells3e301482005-06-23 22:00:56 -070080 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -080081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 /* pull the payload in if one was supplied */
84 payload = NULL;
85
David Howells38bbca62008-04-29 01:01:19 -070086 vm = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (_payload) {
88 ret = -ENOMEM;
89 payload = kmalloc(plen, GFP_KERNEL);
David Howells38bbca62008-04-29 01:01:19 -070090 if (!payload) {
91 if (plen <= PAGE_SIZE)
92 goto error2;
93 vm = true;
94 payload = vmalloc(plen);
95 if (!payload)
96 goto error2;
97 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 ret = -EFAULT;
100 if (copy_from_user(payload, _payload, plen) != 0)
101 goto error3;
102 }
103
104 /* find the target keyring (which must be writable) */
David Howells664cceb2005-09-28 17:03:15 +0100105 keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
106 if (IS_ERR(keyring_ref)) {
107 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 goto error3;
109 }
110
111 /* create or update the requested key and add it to the target
112 * keyring */
David Howells664cceb2005-09-28 17:03:15 +0100113 key_ref = key_create_or_update(keyring_ref, type, description,
David Howells7e047ef2006-06-26 00:24:50 -0700114 payload, plen, KEY_ALLOC_IN_QUOTA);
David Howells664cceb2005-09-28 17:03:15 +0100115 if (!IS_ERR(key_ref)) {
116 ret = key_ref_to_ptr(key_ref)->serial;
117 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119 else {
David Howells664cceb2005-09-28 17:03:15 +0100120 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122
David Howells664cceb2005-09-28 17:03:15 +0100123 key_ref_put(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 error3:
David Howells38bbca62008-04-29 01:01:19 -0700125 if (!vm)
126 kfree(payload);
127 else
128 vfree(payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 error2:
130 kfree(description);
131 error:
132 return ret;
133
134} /* end sys_add_key() */
135
136/*****************************************************************************/
137/*
138 * search the process keyrings for a matching key
139 * - nested keyrings may also be searched if they have Search permission
140 * - if a key is found, it will be attached to the destination keyring if
141 * there's one specified
142 * - /sbin/request-key will be invoked if _callout_info is non-NULL
143 * - the _callout_info string will be passed to /sbin/request-key
144 * - if the _callout_info string is empty, it will be rendered as "-"
145 * - implements request_key()
146 */
147asmlinkage long sys_request_key(const char __user *_type,
148 const char __user *_description,
149 const char __user *_callout_info,
150 key_serial_t destringid)
151{
152 struct key_type *ktype;
David Howells664cceb2005-09-28 17:03:15 +0100153 struct key *key;
154 key_ref_t dest_ref;
David Howells4a38e122008-04-29 01:01:24 -0700155 size_t callout_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 char type[32], *description, *callout_info;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800157 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 /* pull the type into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800160 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (ret < 0)
162 goto error;
David Howells1260f802005-08-04 11:50:01 +0100163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /* pull the description into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800165 description = strndup_user(_description, PAGE_SIZE);
166 if (IS_ERR(description)) {
167 ret = PTR_ERR(description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 /* pull the callout info into kernel space */
172 callout_info = NULL;
David Howells4a38e122008-04-29 01:01:24 -0700173 callout_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (_callout_info) {
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800175 callout_info = strndup_user(_callout_info, PAGE_SIZE);
176 if (IS_ERR(callout_info)) {
177 ret = PTR_ERR(callout_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 goto error2;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800179 }
David Howells4a38e122008-04-29 01:01:24 -0700180 callout_len = strlen(callout_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182
183 /* get the destination keyring if specified */
David Howells664cceb2005-09-28 17:03:15 +0100184 dest_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (destringid) {
David Howells664cceb2005-09-28 17:03:15 +0100186 dest_ref = lookup_user_key(NULL, destringid, 1, 0, KEY_WRITE);
187 if (IS_ERR(dest_ref)) {
188 ret = PTR_ERR(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 goto error3;
190 }
191 }
192
193 /* find the key type */
194 ktype = key_type_lookup(type);
195 if (IS_ERR(ktype)) {
196 ret = PTR_ERR(ktype);
197 goto error4;
198 }
199
200 /* do the search */
David Howells4a38e122008-04-29 01:01:24 -0700201 key = request_key_and_link(ktype, description, callout_info,
202 callout_len, NULL, key_ref_to_ptr(dest_ref),
David Howells7e047ef2006-06-26 00:24:50 -0700203 KEY_ALLOC_IN_QUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (IS_ERR(key)) {
205 ret = PTR_ERR(key);
206 goto error5;
207 }
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 ret = key->serial;
210
David Howells3e301482005-06-23 22:00:56 -0700211 key_put(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 error5:
213 key_type_put(ktype);
214 error4:
David Howells664cceb2005-09-28 17:03:15 +0100215 key_ref_put(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 error3:
217 kfree(callout_info);
218 error2:
219 kfree(description);
220 error:
221 return ret;
222
223} /* end sys_request_key() */
224
225/*****************************************************************************/
226/*
227 * get the ID of the specified process keyring
228 * - the keyring must have search permission to be found
229 * - implements keyctl(KEYCTL_GET_KEYRING_ID)
230 */
231long keyctl_get_keyring_ID(key_serial_t id, int create)
232{
David Howells664cceb2005-09-28 17:03:15 +0100233 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 long ret;
235
David Howells664cceb2005-09-28 17:03:15 +0100236 key_ref = lookup_user_key(NULL, id, create, 0, KEY_SEARCH);
237 if (IS_ERR(key_ref)) {
238 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 goto error;
240 }
241
David Howells664cceb2005-09-28 17:03:15 +0100242 ret = key_ref_to_ptr(key_ref)->serial;
243 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 error:
245 return ret;
246
247} /* end keyctl_get_keyring_ID() */
248
249/*****************************************************************************/
250/*
251 * join the session keyring
252 * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING)
253 */
254long keyctl_join_session_keyring(const char __user *_name)
255{
256 char *name;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800257 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 /* fetch the name from userspace */
260 name = NULL;
261 if (_name) {
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800262 name = strndup_user(_name, PAGE_SIZE);
263 if (IS_ERR(name)) {
264 ret = PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268
269 /* join the session */
270 ret = join_session_keyring(name);
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 error:
273 return ret;
274
275} /* end keyctl_join_session_keyring() */
276
277/*****************************************************************************/
278/*
279 * update a key's data payload
280 * - the key must be writable
281 * - implements keyctl(KEYCTL_UPDATE)
282 */
283long keyctl_update_key(key_serial_t id,
284 const void __user *_payload,
285 size_t plen)
286{
David Howells664cceb2005-09-28 17:03:15 +0100287 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 void *payload;
289 long ret;
290
291 ret = -EINVAL;
292 if (plen > PAGE_SIZE)
293 goto error;
294
295 /* pull the payload in if one was supplied */
296 payload = NULL;
297 if (_payload) {
298 ret = -ENOMEM;
299 payload = kmalloc(plen, GFP_KERNEL);
300 if (!payload)
301 goto error;
302
303 ret = -EFAULT;
304 if (copy_from_user(payload, _payload, plen) != 0)
305 goto error2;
306 }
307
308 /* find the target key (which must be writable) */
David Howells664cceb2005-09-28 17:03:15 +0100309 key_ref = lookup_user_key(NULL, id, 0, 0, KEY_WRITE);
310 if (IS_ERR(key_ref)) {
311 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 goto error2;
313 }
314
315 /* update the key */
David Howells664cceb2005-09-28 17:03:15 +0100316 ret = key_update(key_ref, payload, plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
David Howells664cceb2005-09-28 17:03:15 +0100318 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 error2:
320 kfree(payload);
321 error:
322 return ret;
323
324} /* end keyctl_update_key() */
325
326/*****************************************************************************/
327/*
328 * revoke a key
329 * - the key must be writable
330 * - implements keyctl(KEYCTL_REVOKE)
331 */
332long keyctl_revoke_key(key_serial_t id)
333{
David Howells664cceb2005-09-28 17:03:15 +0100334 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 long ret;
336
David Howells664cceb2005-09-28 17:03:15 +0100337 key_ref = lookup_user_key(NULL, id, 0, 0, KEY_WRITE);
338 if (IS_ERR(key_ref)) {
339 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 goto error;
341 }
342
David Howells664cceb2005-09-28 17:03:15 +0100343 key_revoke(key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 ret = 0;
345
David Howells664cceb2005-09-28 17:03:15 +0100346 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 error:
David Howells1260f802005-08-04 11:50:01 +0100348 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350} /* end keyctl_revoke_key() */
351
352/*****************************************************************************/
353/*
354 * clear the specified process keyring
355 * - the keyring must be writable
356 * - implements keyctl(KEYCTL_CLEAR)
357 */
358long keyctl_keyring_clear(key_serial_t ringid)
359{
David Howells664cceb2005-09-28 17:03:15 +0100360 key_ref_t keyring_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 long ret;
362
David Howells664cceb2005-09-28 17:03:15 +0100363 keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
364 if (IS_ERR(keyring_ref)) {
365 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 goto error;
367 }
368
David Howells664cceb2005-09-28 17:03:15 +0100369 ret = keyring_clear(key_ref_to_ptr(keyring_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
David Howells664cceb2005-09-28 17:03:15 +0100371 key_ref_put(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 error:
373 return ret;
374
375} /* end keyctl_keyring_clear() */
376
377/*****************************************************************************/
378/*
379 * link a key into a keyring
380 * - the keyring must be writable
381 * - the key must be linkable
382 * - implements keyctl(KEYCTL_LINK)
383 */
384long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
385{
David Howells664cceb2005-09-28 17:03:15 +0100386 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 long ret;
388
David Howells664cceb2005-09-28 17:03:15 +0100389 keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
390 if (IS_ERR(keyring_ref)) {
391 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 goto error;
393 }
394
David Howells664cceb2005-09-28 17:03:15 +0100395 key_ref = lookup_user_key(NULL, id, 1, 0, KEY_LINK);
396 if (IS_ERR(key_ref)) {
397 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 goto error2;
399 }
400
David Howells664cceb2005-09-28 17:03:15 +0100401 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
David Howells664cceb2005-09-28 17:03:15 +0100403 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 error2:
David Howells664cceb2005-09-28 17:03:15 +0100405 key_ref_put(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 error:
407 return ret;
408
409} /* end keyctl_keyring_link() */
410
411/*****************************************************************************/
412/*
413 * unlink the first attachment of a key from a keyring
414 * - the keyring must be writable
415 * - we don't need any permissions on the key
416 * - implements keyctl(KEYCTL_UNLINK)
417 */
418long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
419{
David Howells664cceb2005-09-28 17:03:15 +0100420 key_ref_t keyring_ref, key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 long ret;
422
David Howells664cceb2005-09-28 17:03:15 +0100423 keyring_ref = lookup_user_key(NULL, ringid, 0, 0, KEY_WRITE);
424 if (IS_ERR(keyring_ref)) {
425 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 goto error;
427 }
428
David Howells664cceb2005-09-28 17:03:15 +0100429 key_ref = lookup_user_key(NULL, id, 0, 0, 0);
430 if (IS_ERR(key_ref)) {
431 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 goto error2;
433 }
434
David Howells664cceb2005-09-28 17:03:15 +0100435 ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
David Howells664cceb2005-09-28 17:03:15 +0100437 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 error2:
David Howells664cceb2005-09-28 17:03:15 +0100439 key_ref_put(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 error:
441 return ret;
442
443} /* end keyctl_keyring_unlink() */
444
445/*****************************************************************************/
446/*
447 * describe a user key
448 * - the key must have view permission
449 * - if there's a buffer, we place up to buflen bytes of data into it
450 * - unless there's an error, we return the amount of description available,
451 * irrespective of how much we may have copied
452 * - the description is formatted thus:
453 * type;uid;gid;perm;description<NUL>
454 * - implements keyctl(KEYCTL_DESCRIBE)
455 */
456long keyctl_describe_key(key_serial_t keyid,
457 char __user *buffer,
458 size_t buflen)
459{
David Howells3e301482005-06-23 22:00:56 -0700460 struct key *key, *instkey;
David Howells664cceb2005-09-28 17:03:15 +0100461 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 char *tmpbuf;
463 long ret;
464
David Howells664cceb2005-09-28 17:03:15 +0100465 key_ref = lookup_user_key(NULL, keyid, 0, 1, KEY_VIEW);
466 if (IS_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700467 /* viewing a key under construction is permitted if we have the
468 * authorisation token handy */
David Howells664cceb2005-09-28 17:03:15 +0100469 if (PTR_ERR(key_ref) == -EACCES) {
David Howells3e301482005-06-23 22:00:56 -0700470 instkey = key_get_instantiation_authkey(keyid);
471 if (!IS_ERR(instkey)) {
472 key_put(instkey);
David Howells664cceb2005-09-28 17:03:15 +0100473 key_ref = lookup_user_key(NULL, keyid,
474 0, 1, 0);
475 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700476 goto okay;
477 }
478 }
479
David Howells664cceb2005-09-28 17:03:15 +0100480 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 goto error;
482 }
483
David Howells3e301482005-06-23 22:00:56 -0700484okay:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 /* calculate how much description we're going to return */
486 ret = -ENOMEM;
487 tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
488 if (!tmpbuf)
489 goto error2;
490
David Howells664cceb2005-09-28 17:03:15 +0100491 key = key_ref_to_ptr(key_ref);
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 ret = snprintf(tmpbuf, PAGE_SIZE - 1,
David Howells664cceb2005-09-28 17:03:15 +0100494 "%s;%d;%d;%08x;%s",
495 key_ref_to_ptr(key_ref)->type->name,
496 key_ref_to_ptr(key_ref)->uid,
497 key_ref_to_ptr(key_ref)->gid,
498 key_ref_to_ptr(key_ref)->perm,
499 key_ref_to_ptr(key_ref)->description ?
500 key_ref_to_ptr(key_ref)->description : ""
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 );
502
503 /* include a NUL char at the end of the data */
504 if (ret > PAGE_SIZE - 1)
505 ret = PAGE_SIZE - 1;
506 tmpbuf[ret] = 0;
507 ret++;
508
509 /* consider returning the data */
510 if (buffer && buflen > 0) {
511 if (buflen > ret)
512 buflen = ret;
513
514 if (copy_to_user(buffer, tmpbuf, buflen) != 0)
515 ret = -EFAULT;
516 }
517
518 kfree(tmpbuf);
519 error2:
David Howells664cceb2005-09-28 17:03:15 +0100520 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 error:
522 return ret;
523
524} /* end keyctl_describe_key() */
525
526/*****************************************************************************/
527/*
528 * search the specified keyring for a matching key
529 * - the start keyring must be searchable
530 * - nested keyrings may also be searched if they are searchable
531 * - only keys with search permission may be found
532 * - if a key is found, it will be attached to the destination keyring if
533 * there's one specified
534 * - implements keyctl(KEYCTL_SEARCH)
535 */
536long keyctl_keyring_search(key_serial_t ringid,
537 const char __user *_type,
538 const char __user *_description,
539 key_serial_t destringid)
540{
541 struct key_type *ktype;
David Howells664cceb2005-09-28 17:03:15 +0100542 key_ref_t keyring_ref, key_ref, dest_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 char type[32], *description;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800544 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 /* pull the type and description into kernel space */
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800547 ret = key_get_type_from_user(type, _type, sizeof(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (ret < 0)
549 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800551 description = strndup_user(_description, PAGE_SIZE);
552 if (IS_ERR(description)) {
553 ret = PTR_ERR(description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 goto error;
Davi Arnaut0cb409d2006-03-24 03:18:43 -0800555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 /* get the keyring at which to begin the search */
David Howells664cceb2005-09-28 17:03:15 +0100558 keyring_ref = lookup_user_key(NULL, ringid, 0, 0, KEY_SEARCH);
559 if (IS_ERR(keyring_ref)) {
560 ret = PTR_ERR(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 goto error2;
562 }
563
564 /* get the destination keyring if specified */
David Howells664cceb2005-09-28 17:03:15 +0100565 dest_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (destringid) {
David Howells664cceb2005-09-28 17:03:15 +0100567 dest_ref = lookup_user_key(NULL, destringid, 1, 0, KEY_WRITE);
568 if (IS_ERR(dest_ref)) {
569 ret = PTR_ERR(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 goto error3;
571 }
572 }
573
574 /* find the key type */
575 ktype = key_type_lookup(type);
576 if (IS_ERR(ktype)) {
577 ret = PTR_ERR(ktype);
578 goto error4;
579 }
580
581 /* do the search */
David Howells664cceb2005-09-28 17:03:15 +0100582 key_ref = keyring_search(keyring_ref, ktype, description);
583 if (IS_ERR(key_ref)) {
584 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 /* treat lack or presence of a negative key the same */
587 if (ret == -EAGAIN)
588 ret = -ENOKEY;
589 goto error5;
590 }
591
592 /* link the resulting key to the destination keyring if we can */
David Howells664cceb2005-09-28 17:03:15 +0100593 if (dest_ref) {
David Howells29db9192005-10-30 15:02:44 -0800594 ret = key_permission(key_ref, KEY_LINK);
595 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 goto error6;
597
David Howells664cceb2005-09-28 17:03:15 +0100598 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (ret < 0)
600 goto error6;
601 }
602
David Howells664cceb2005-09-28 17:03:15 +0100603 ret = key_ref_to_ptr(key_ref)->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 error6:
David Howells664cceb2005-09-28 17:03:15 +0100606 key_ref_put(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 error5:
608 key_type_put(ktype);
609 error4:
David Howells664cceb2005-09-28 17:03:15 +0100610 key_ref_put(dest_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 error3:
David Howells664cceb2005-09-28 17:03:15 +0100612 key_ref_put(keyring_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 error2:
614 kfree(description);
615 error:
616 return ret;
617
618} /* end keyctl_keyring_search() */
619
620/*****************************************************************************/
621/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 * read a user key's payload
623 * - the keyring must be readable or the key must be searchable from the
624 * process's keyrings
625 * - if there's a buffer, we place up to buflen bytes of data into it
626 * - unless there's an error, we return the amount of data in the key,
627 * irrespective of how much we may have copied
628 * - implements keyctl(KEYCTL_READ)
629 */
630long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
631{
David Howells664cceb2005-09-28 17:03:15 +0100632 struct key *key;
633 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 long ret;
635
636 /* find the key first */
David Howells664cceb2005-09-28 17:03:15 +0100637 key_ref = lookup_user_key(NULL, keyid, 0, 0, 0);
638 if (IS_ERR(key_ref)) {
639 ret = -ENOKEY;
640 goto error;
641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
David Howells664cceb2005-09-28 17:03:15 +0100643 key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
David Howells664cceb2005-09-28 17:03:15 +0100645 /* see if we can read it directly */
David Howells29db9192005-10-30 15:02:44 -0800646 ret = key_permission(key_ref, KEY_READ);
647 if (ret == 0)
David Howells664cceb2005-09-28 17:03:15 +0100648 goto can_read_key;
David Howells29db9192005-10-30 15:02:44 -0800649 if (ret != -EACCES)
650 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100651
652 /* we can't; see if it's searchable from this process's keyrings
653 * - we automatically take account of the fact that it may be
654 * dangling off an instantiation key
655 */
656 if (!is_key_possessed(key_ref)) {
657 ret = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 goto error2;
659 }
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /* the key is probably readable - now try to read it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 can_read_key:
663 ret = key_validate(key);
664 if (ret == 0) {
665 ret = -EOPNOTSUPP;
666 if (key->type->read) {
667 /* read the data with the semaphore held (since we
668 * might sleep) */
669 down_read(&key->sem);
670 ret = key->type->read(key, buffer, buflen);
671 up_read(&key->sem);
672 }
673 }
674
675 error2:
676 key_put(key);
677 error:
678 return ret;
679
680} /* end keyctl_read_key() */
681
682/*****************************************************************************/
683/*
684 * change the ownership of a key
685 * - the keyring owned by the changer
686 * - if the uid or gid is -1, then that parameter is not changed
687 * - implements keyctl(KEYCTL_CHOWN)
688 */
689long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
690{
Fredrik Tolf58016492006-06-26 00:24:51 -0700691 struct key_user *newowner, *zapowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100693 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 long ret;
695
696 ret = 0;
697 if (uid == (uid_t) -1 && gid == (gid_t) -1)
698 goto error;
699
David Howells29db9192005-10-30 15:02:44 -0800700 key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100701 if (IS_ERR(key_ref)) {
702 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 goto error;
704 }
705
David Howells664cceb2005-09-28 17:03:15 +0100706 key = key_ref_to_ptr(key_ref);
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 /* make the changes with the locks held to prevent chown/chown races */
709 ret = -EACCES;
710 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 if (!capable(CAP_SYS_ADMIN)) {
713 /* only the sysadmin can chown a key to some other UID */
714 if (uid != (uid_t) -1 && key->uid != uid)
Fredrik Tolf58016492006-06-26 00:24:51 -0700715 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 /* only the sysadmin can set the key's GID to a group other
718 * than one of those that the current process subscribes to */
719 if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
Fredrik Tolf58016492006-06-26 00:24:51 -0700720 goto error_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
722
Fredrik Tolf58016492006-06-26 00:24:51 -0700723 /* change the UID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (uid != (uid_t) -1 && uid != key->uid) {
Fredrik Tolf58016492006-06-26 00:24:51 -0700725 ret = -ENOMEM;
726 newowner = key_user_lookup(uid);
727 if (!newowner)
728 goto error_put;
729
730 /* transfer the quota burden to the new user */
731 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
732 spin_lock(&newowner->lock);
733 if (newowner->qnkeys + 1 >= KEYQUOTA_MAX_KEYS ||
734 newowner->qnbytes + key->quotalen >=
735 KEYQUOTA_MAX_BYTES)
736 goto quota_overrun;
737
738 newowner->qnkeys++;
739 newowner->qnbytes += key->quotalen;
740 spin_unlock(&newowner->lock);
741
742 spin_lock(&key->user->lock);
743 key->user->qnkeys--;
744 key->user->qnbytes -= key->quotalen;
745 spin_unlock(&key->user->lock);
746 }
747
748 atomic_dec(&key->user->nkeys);
749 atomic_inc(&newowner->nkeys);
750
751 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
752 atomic_dec(&key->user->nikeys);
753 atomic_inc(&newowner->nikeys);
754 }
755
756 zapowner = key->user;
757 key->user = newowner;
758 key->uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
761 /* change the GID */
762 if (gid != (gid_t) -1)
763 key->gid = gid;
764
765 ret = 0;
766
Fredrik Tolf58016492006-06-26 00:24:51 -0700767error_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 up_write(&key->sem);
769 key_put(key);
Fredrik Tolf58016492006-06-26 00:24:51 -0700770 if (zapowner)
771 key_user_put(zapowner);
772error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return ret;
774
Fredrik Tolf58016492006-06-26 00:24:51 -0700775quota_overrun:
776 spin_unlock(&newowner->lock);
777 zapowner = newowner;
778 ret = -EDQUOT;
779 goto error_put;
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781} /* end keyctl_chown_key() */
782
783/*****************************************************************************/
784/*
785 * change the permission mask on a key
786 * - the keyring owned by the changer
787 * - implements keyctl(KEYCTL_SETPERM)
788 */
789long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
790{
791 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100792 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 long ret;
794
795 ret = -EINVAL;
David Howells664cceb2005-09-28 17:03:15 +0100796 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 goto error;
798
David Howells29db9192005-10-30 15:02:44 -0800799 key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
David Howells664cceb2005-09-28 17:03:15 +0100800 if (IS_ERR(key_ref)) {
801 ret = PTR_ERR(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 goto error;
803 }
804
David Howells664cceb2005-09-28 17:03:15 +0100805 key = key_ref_to_ptr(key_ref);
806
David Howells76d8aea2005-06-23 22:00:49 -0700807 /* make the changes with the locks held to prevent chown/chmod races */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 ret = -EACCES;
809 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
David Howells76d8aea2005-06-23 22:00:49 -0700811 /* if we're not the sysadmin, we can only change a key that we own */
812 if (capable(CAP_SYS_ADMIN) || key->uid == current->fsuid) {
813 key->perm = perm;
814 ret = 0;
815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 up_write(&key->sem);
818 key_put(key);
David Howells76d8aea2005-06-23 22:00:49 -0700819error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return ret;
821
822} /* end keyctl_setperm_key() */
823
824/*****************************************************************************/
825/*
826 * instantiate the key with the specified payload, and, if one is given, link
827 * the key into the keyring
828 */
829long keyctl_instantiate_key(key_serial_t id,
830 const void __user *_payload,
831 size_t plen,
832 key_serial_t ringid)
833{
David Howells3e301482005-06-23 22:00:56 -0700834 struct request_key_auth *rka;
David Howells664cceb2005-09-28 17:03:15 +0100835 struct key *instkey;
836 key_ref_t keyring_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 void *payload;
838 long ret;
David Howells38bbca62008-04-29 01:01:19 -0700839 bool vm = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 ret = -EINVAL;
David Howells38bbca62008-04-29 01:01:19 -0700842 if (plen > 1024 * 1024 - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 goto error;
844
David Howellsb5f545c2006-01-08 01:02:47 -0800845 /* the appropriate instantiation authorisation key must have been
846 * assumed before calling this */
847 ret = -EPERM;
848 instkey = current->request_key_auth;
849 if (!instkey)
850 goto error;
851
852 rka = instkey->payload.data;
853 if (rka->target_key->serial != id)
854 goto error;
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 /* pull the payload in if one was supplied */
857 payload = NULL;
858
859 if (_payload) {
860 ret = -ENOMEM;
861 payload = kmalloc(plen, GFP_KERNEL);
David Howells38bbca62008-04-29 01:01:19 -0700862 if (!payload) {
863 if (plen <= PAGE_SIZE)
864 goto error;
865 vm = true;
866 payload = vmalloc(plen);
867 if (!payload)
868 goto error;
869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 ret = -EFAULT;
872 if (copy_from_user(payload, _payload, plen) != 0)
873 goto error2;
874 }
875
David Howells3e301482005-06-23 22:00:56 -0700876 /* find the destination keyring amongst those belonging to the
877 * requesting task */
David Howells664cceb2005-09-28 17:03:15 +0100878 keyring_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (ringid) {
David Howells664cceb2005-09-28 17:03:15 +0100880 keyring_ref = lookup_user_key(rka->context, ringid, 1, 0,
881 KEY_WRITE);
882 if (IS_ERR(keyring_ref)) {
883 ret = PTR_ERR(keyring_ref);
David Howellsb5f545c2006-01-08 01:02:47 -0800884 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
886 }
887
888 /* instantiate the key and link it into a keyring */
David Howells3e301482005-06-23 22:00:56 -0700889 ret = key_instantiate_and_link(rka->target_key, payload, plen,
David Howells664cceb2005-09-28 17:03:15 +0100890 key_ref_to_ptr(keyring_ref), instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
David Howells664cceb2005-09-28 17:03:15 +0100892 key_ref_put(keyring_ref);
David Howellsb5f545c2006-01-08 01:02:47 -0800893
894 /* discard the assumed authority if it's just been disabled by
895 * instantiation of the key */
896 if (ret == 0) {
897 key_put(current->request_key_auth);
898 current->request_key_auth = NULL;
899 }
900
901error2:
David Howells38bbca62008-04-29 01:01:19 -0700902 if (!vm)
903 kfree(payload);
904 else
905 vfree(payload);
David Howellsb5f545c2006-01-08 01:02:47 -0800906error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return ret;
908
909} /* end keyctl_instantiate_key() */
910
911/*****************************************************************************/
912/*
913 * negatively instantiate the key with the given timeout (in seconds), and, if
914 * one is given, link the key into the keyring
915 */
916long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
917{
David Howells3e301482005-06-23 22:00:56 -0700918 struct request_key_auth *rka;
David Howells664cceb2005-09-28 17:03:15 +0100919 struct key *instkey;
920 key_ref_t keyring_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 long ret;
922
David Howellsb5f545c2006-01-08 01:02:47 -0800923 /* the appropriate instantiation authorisation key must have been
924 * assumed before calling this */
925 ret = -EPERM;
926 instkey = current->request_key_auth;
927 if (!instkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
David Howells3e301482005-06-23 22:00:56 -0700930 rka = instkey->payload.data;
David Howellsb5f545c2006-01-08 01:02:47 -0800931 if (rka->target_key->serial != id)
932 goto error;
David Howells3e301482005-06-23 22:00:56 -0700933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 /* find the destination keyring if present (which must also be
935 * writable) */
David Howells664cceb2005-09-28 17:03:15 +0100936 keyring_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (ringid) {
David Howells664cceb2005-09-28 17:03:15 +0100938 keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
939 if (IS_ERR(keyring_ref)) {
940 ret = PTR_ERR(keyring_ref);
David Howellsb5f545c2006-01-08 01:02:47 -0800941 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943 }
944
945 /* instantiate the key and link it into a keyring */
David Howells664cceb2005-09-28 17:03:15 +0100946 ret = key_negate_and_link(rka->target_key, timeout,
947 key_ref_to_ptr(keyring_ref), instkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
David Howells664cceb2005-09-28 17:03:15 +0100949 key_ref_put(keyring_ref);
David Howellsb5f545c2006-01-08 01:02:47 -0800950
951 /* discard the assumed authority if it's just been disabled by
952 * instantiation of the key */
953 if (ret == 0) {
954 key_put(current->request_key_auth);
955 current->request_key_auth = NULL;
956 }
957
958error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return ret;
960
961} /* end keyctl_negate_key() */
962
963/*****************************************************************************/
964/*
David Howells3e301482005-06-23 22:00:56 -0700965 * set the default keyring in which request_key() will cache keys
966 * - return the old setting
967 */
968long keyctl_set_reqkey_keyring(int reqkey_defl)
969{
970 int ret;
971
972 switch (reqkey_defl) {
973 case KEY_REQKEY_DEFL_THREAD_KEYRING:
974 ret = install_thread_keyring(current);
975 if (ret < 0)
976 return ret;
977 goto set;
978
979 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
980 ret = install_process_keyring(current);
981 if (ret < 0)
982 return ret;
983
984 case KEY_REQKEY_DEFL_DEFAULT:
985 case KEY_REQKEY_DEFL_SESSION_KEYRING:
986 case KEY_REQKEY_DEFL_USER_KEYRING:
987 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
988 set:
989 current->jit_keyring = reqkey_defl;
990
991 case KEY_REQKEY_DEFL_NO_CHANGE:
992 return current->jit_keyring;
993
994 case KEY_REQKEY_DEFL_GROUP_KEYRING:
995 default:
996 return -EINVAL;
997 }
998
999} /* end keyctl_set_reqkey_keyring() */
1000
1001/*****************************************************************************/
1002/*
David Howells017679c2006-01-08 01:02:43 -08001003 * set or clear the timeout for a key
1004 */
1005long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1006{
1007 struct timespec now;
1008 struct key *key;
1009 key_ref_t key_ref;
1010 time_t expiry;
1011 long ret;
1012
1013 key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
1014 if (IS_ERR(key_ref)) {
1015 ret = PTR_ERR(key_ref);
1016 goto error;
1017 }
1018
1019 key = key_ref_to_ptr(key_ref);
1020
1021 /* make the changes with the locks held to prevent races */
1022 down_write(&key->sem);
1023
1024 expiry = 0;
1025 if (timeout > 0) {
1026 now = current_kernel_time();
1027 expiry = now.tv_sec + timeout;
1028 }
1029
1030 key->expiry = expiry;
1031
1032 up_write(&key->sem);
1033 key_put(key);
1034
1035 ret = 0;
1036error:
1037 return ret;
1038
1039} /* end keyctl_set_timeout() */
1040
1041/*****************************************************************************/
1042/*
David Howellsb5f545c2006-01-08 01:02:47 -08001043 * assume the authority to instantiate the specified key
1044 */
1045long keyctl_assume_authority(key_serial_t id)
1046{
1047 struct key *authkey;
1048 long ret;
1049
1050 /* special key IDs aren't permitted */
1051 ret = -EINVAL;
1052 if (id < 0)
1053 goto error;
1054
1055 /* we divest ourselves of authority if given an ID of 0 */
1056 if (id == 0) {
1057 key_put(current->request_key_auth);
1058 current->request_key_auth = NULL;
1059 ret = 0;
1060 goto error;
1061 }
1062
1063 /* attempt to assume the authority temporarily granted to us whilst we
1064 * instantiate the specified key
1065 * - the authorisation key must be in the current task's keyrings
1066 * somewhere
1067 */
1068 authkey = key_get_instantiation_authkey(id);
1069 if (IS_ERR(authkey)) {
1070 ret = PTR_ERR(authkey);
1071 goto error;
1072 }
1073
1074 key_put(current->request_key_auth);
1075 current->request_key_auth = authkey;
1076 ret = authkey->serial;
1077
1078error:
1079 return ret;
1080
1081} /* end keyctl_assume_authority() */
1082
1083/*****************************************************************************/
1084/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 * the key control system call
1086 */
1087asmlinkage long sys_keyctl(int option, unsigned long arg2, unsigned long arg3,
1088 unsigned long arg4, unsigned long arg5)
1089{
1090 switch (option) {
1091 case KEYCTL_GET_KEYRING_ID:
1092 return keyctl_get_keyring_ID((key_serial_t) arg2,
1093 (int) arg3);
1094
1095 case KEYCTL_JOIN_SESSION_KEYRING:
1096 return keyctl_join_session_keyring((const char __user *) arg2);
1097
1098 case KEYCTL_UPDATE:
1099 return keyctl_update_key((key_serial_t) arg2,
1100 (const void __user *) arg3,
1101 (size_t) arg4);
1102
1103 case KEYCTL_REVOKE:
1104 return keyctl_revoke_key((key_serial_t) arg2);
1105
1106 case KEYCTL_DESCRIBE:
1107 return keyctl_describe_key((key_serial_t) arg2,
1108 (char __user *) arg3,
1109 (unsigned) arg4);
1110
1111 case KEYCTL_CLEAR:
1112 return keyctl_keyring_clear((key_serial_t) arg2);
1113
1114 case KEYCTL_LINK:
1115 return keyctl_keyring_link((key_serial_t) arg2,
1116 (key_serial_t) arg3);
1117
1118 case KEYCTL_UNLINK:
1119 return keyctl_keyring_unlink((key_serial_t) arg2,
1120 (key_serial_t) arg3);
1121
1122 case KEYCTL_SEARCH:
1123 return keyctl_keyring_search((key_serial_t) arg2,
1124 (const char __user *) arg3,
1125 (const char __user *) arg4,
1126 (key_serial_t) arg5);
1127
1128 case KEYCTL_READ:
1129 return keyctl_read_key((key_serial_t) arg2,
1130 (char __user *) arg3,
1131 (size_t) arg4);
1132
1133 case KEYCTL_CHOWN:
1134 return keyctl_chown_key((key_serial_t) arg2,
1135 (uid_t) arg3,
1136 (gid_t) arg4);
1137
1138 case KEYCTL_SETPERM:
1139 return keyctl_setperm_key((key_serial_t) arg2,
1140 (key_perm_t) arg3);
1141
1142 case KEYCTL_INSTANTIATE:
1143 return keyctl_instantiate_key((key_serial_t) arg2,
1144 (const void __user *) arg3,
1145 (size_t) arg4,
1146 (key_serial_t) arg5);
1147
1148 case KEYCTL_NEGATE:
1149 return keyctl_negate_key((key_serial_t) arg2,
1150 (unsigned) arg3,
1151 (key_serial_t) arg4);
1152
David Howells3e301482005-06-23 22:00:56 -07001153 case KEYCTL_SET_REQKEY_KEYRING:
1154 return keyctl_set_reqkey_keyring(arg2);
1155
David Howells017679c2006-01-08 01:02:43 -08001156 case KEYCTL_SET_TIMEOUT:
1157 return keyctl_set_timeout((key_serial_t) arg2,
1158 (unsigned) arg3);
1159
David Howellsb5f545c2006-01-08 01:02:47 -08001160 case KEYCTL_ASSUME_AUTHORITY:
1161 return keyctl_assume_authority((key_serial_t) arg2);
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 default:
1164 return -EOPNOTSUPP;
1165 }
1166
1167} /* end sys_keyctl() */