]> bbs.cooldavid.org Git - net-next-2.6.git/blame - security/keys/keyctl.c
keys: allow the callout data to be passed as a blob rather than a string
[net-next-2.6.git] / security / keys / keyctl.c
CommitLineData
1da177e4
LT
1/* keyctl.c: userspace keyctl operations
2 *
3e30148c 3 * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved.
1da177e4
LT
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/sched.h>
15#include <linux/slab.h>
16#include <linux/syscalls.h>
17#include <linux/keyctl.h>
18#include <linux/fs.h>
c59ede7b 19#include <linux/capability.h>
0cb409d9 20#include <linux/string.h>
1da177e4 21#include <linux/err.h>
38bbca6b 22#include <linux/vmalloc.h>
1da177e4
LT
23#include <asm/uaccess.h>
24#include "internal.h"
25
0cb409d9
DA
26static 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
1da177e4
LT
48/*****************************************************************************/
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{
664cceb0 62 key_ref_t keyring_ref, key_ref;
1da177e4
LT
63 char type[32], *description;
64 void *payload;
0cb409d9 65 long ret;
38bbca6b 66 bool vm;
1da177e4
LT
67
68 ret = -EINVAL;
38bbca6b 69 if (plen > 1024 * 1024 - 1)
1da177e4
LT
70 goto error;
71
72 /* draw all the data into kernel space */
0cb409d9 73 ret = key_get_type_from_user(type, _type, sizeof(type));
1da177e4
LT
74 if (ret < 0)
75 goto error;
1da177e4 76
0cb409d9
DA
77 description = strndup_user(_description, PAGE_SIZE);
78 if (IS_ERR(description)) {
79 ret = PTR_ERR(description);
1da177e4 80 goto error;
0cb409d9 81 }
1da177e4
LT
82
83 /* pull the payload in if one was supplied */
84 payload = NULL;
85
38bbca6b 86 vm = false;
1da177e4
LT
87 if (_payload) {
88 ret = -ENOMEM;
89 payload = kmalloc(plen, GFP_KERNEL);
38bbca6b
DH
90 if (!payload) {
91 if (plen <= PAGE_SIZE)
92 goto error2;
93 vm = true;
94 payload = vmalloc(plen);
95 if (!payload)
96 goto error2;
97 }
1da177e4
LT
98
99 ret = -EFAULT;
100 if (copy_from_user(payload, _payload, plen) != 0)
101 goto error3;
102 }
103
104 /* find the target keyring (which must be writable) */
664cceb0
DH
105 keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
106 if (IS_ERR(keyring_ref)) {
107 ret = PTR_ERR(keyring_ref);
1da177e4
LT
108 goto error3;
109 }
110
111 /* create or update the requested key and add it to the target
112 * keyring */
664cceb0 113 key_ref = key_create_or_update(keyring_ref, type, description,
7e047ef5 114 payload, plen, KEY_ALLOC_IN_QUOTA);
664cceb0
DH
115 if (!IS_ERR(key_ref)) {
116 ret = key_ref_to_ptr(key_ref)->serial;
117 key_ref_put(key_ref);
1da177e4
LT
118 }
119 else {
664cceb0 120 ret = PTR_ERR(key_ref);
1da177e4
LT
121 }
122
664cceb0 123 key_ref_put(keyring_ref);
1da177e4 124 error3:
38bbca6b
DH
125 if (!vm)
126 kfree(payload);
127 else
128 vfree(payload);
1da177e4
LT
129 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;
664cceb0
DH
153 struct key *key;
154 key_ref_t dest_ref;
4a38e122 155 size_t callout_len;
1da177e4 156 char type[32], *description, *callout_info;
0cb409d9 157 long ret;
1da177e4
LT
158
159 /* pull the type into kernel space */
0cb409d9 160 ret = key_get_type_from_user(type, _type, sizeof(type));
1da177e4
LT
161 if (ret < 0)
162 goto error;
1260f801 163
1da177e4 164 /* pull the description into kernel space */
0cb409d9
DA
165 description = strndup_user(_description, PAGE_SIZE);
166 if (IS_ERR(description)) {
167 ret = PTR_ERR(description);
1da177e4 168 goto error;
0cb409d9 169 }
1da177e4
LT
170
171 /* pull the callout info into kernel space */
172 callout_info = NULL;
4a38e122 173 callout_len = 0;
1da177e4 174 if (_callout_info) {
0cb409d9
DA
175 callout_info = strndup_user(_callout_info, PAGE_SIZE);
176 if (IS_ERR(callout_info)) {
177 ret = PTR_ERR(callout_info);
1da177e4 178 goto error2;
0cb409d9 179 }
4a38e122 180 callout_len = strlen(callout_info);
1da177e4
LT
181 }
182
183 /* get the destination keyring if specified */
664cceb0 184 dest_ref = NULL;
1da177e4 185 if (destringid) {
664cceb0
DH
186 dest_ref = lookup_user_key(NULL, destringid, 1, 0, KEY_WRITE);
187 if (IS_ERR(dest_ref)) {
188 ret = PTR_ERR(dest_ref);
1da177e4
LT
189 goto error3;
190 }
191 }
192
193 /* find the key type */
194 ktype = key_type_lookup(type);
195 if (IS_ERR(ktype)) {
196 ret = PTR_ERR(ktype);
197 goto error4;
198 }
199
200 /* do the search */
4a38e122
DH
201 key = request_key_and_link(ktype, description, callout_info,
202 callout_len, NULL, key_ref_to_ptr(dest_ref),
7e047ef5 203 KEY_ALLOC_IN_QUOTA);
1da177e4
LT
204 if (IS_ERR(key)) {
205 ret = PTR_ERR(key);
206 goto error5;
207 }
208
1da177e4
LT
209 ret = key->serial;
210
3e30148c 211 key_put(key);
1da177e4
LT
212 error5:
213 key_type_put(ktype);
214 error4:
664cceb0 215 key_ref_put(dest_ref);
1da177e4
LT
216 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{
664cceb0 233 key_ref_t key_ref;
1da177e4
LT
234 long ret;
235
664cceb0
DH
236 key_ref = lookup_user_key(NULL, id, create, 0, KEY_SEARCH);
237 if (IS_ERR(key_ref)) {
238 ret = PTR_ERR(key_ref);
1da177e4
LT
239 goto error;
240 }
241
664cceb0
DH
242 ret = key_ref_to_ptr(key_ref)->serial;
243 key_ref_put(key_ref);
1da177e4
LT
244 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;
0cb409d9 257 long ret;
1da177e4
LT
258
259 /* fetch the name from userspace */
260 name = NULL;
261 if (_name) {
0cb409d9
DA
262 name = strndup_user(_name, PAGE_SIZE);
263 if (IS_ERR(name)) {
264 ret = PTR_ERR(name);
1da177e4 265 goto error;
0cb409d9 266 }
1da177e4
LT
267 }
268
269 /* join the session */
270 ret = join_session_keyring(name);
271
1da177e4
LT
272 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{
664cceb0 287 key_ref_t key_ref;
1da177e4
LT
288 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) */
664cceb0
DH
309 key_ref = lookup_user_key(NULL, id, 0, 0, KEY_WRITE);
310 if (IS_ERR(key_ref)) {
311 ret = PTR_ERR(key_ref);
1da177e4
LT
312 goto error2;
313 }
314
315 /* update the key */
664cceb0 316 ret = key_update(key_ref, payload, plen);
1da177e4 317
664cceb0 318 key_ref_put(key_ref);
1da177e4
LT
319 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{
664cceb0 334 key_ref_t key_ref;
1da177e4
LT
335 long ret;
336
664cceb0
DH
337 key_ref = lookup_user_key(NULL, id, 0, 0, KEY_WRITE);
338 if (IS_ERR(key_ref)) {
339 ret = PTR_ERR(key_ref);
1da177e4
LT
340 goto error;
341 }
342
664cceb0 343 key_revoke(key_ref_to_ptr(key_ref));
1da177e4
LT
344 ret = 0;
345
664cceb0 346 key_ref_put(key_ref);
1da177e4 347 error:
1260f801 348 return ret;
1da177e4
LT
349
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{
664cceb0 360 key_ref_t keyring_ref;
1da177e4
LT
361 long ret;
362
664cceb0
DH
363 keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
364 if (IS_ERR(keyring_ref)) {
365 ret = PTR_ERR(keyring_ref);
1da177e4
LT
366 goto error;
367 }
368
664cceb0 369 ret = keyring_clear(key_ref_to_ptr(keyring_ref));
1da177e4 370
664cceb0 371 key_ref_put(keyring_ref);
1da177e4
LT
372 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{
664cceb0 386 key_ref_t keyring_ref, key_ref;
1da177e4
LT
387 long ret;
388
664cceb0
DH
389 keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
390 if (IS_ERR(keyring_ref)) {
391 ret = PTR_ERR(keyring_ref);
1da177e4
LT
392 goto error;
393 }
394
664cceb0
DH
395 key_ref = lookup_user_key(NULL, id, 1, 0, KEY_LINK);
396 if (IS_ERR(key_ref)) {
397 ret = PTR_ERR(key_ref);
1da177e4
LT
398 goto error2;
399 }
400
664cceb0 401 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
1da177e4 402
664cceb0 403 key_ref_put(key_ref);
1da177e4 404 error2:
664cceb0 405 key_ref_put(keyring_ref);
1da177e4
LT
406 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{
664cceb0 420 key_ref_t keyring_ref, key_ref;
1da177e4
LT
421 long ret;
422
664cceb0
DH
423 keyring_ref = lookup_user_key(NULL, ringid, 0, 0, KEY_WRITE);
424 if (IS_ERR(keyring_ref)) {
425 ret = PTR_ERR(keyring_ref);
1da177e4
LT
426 goto error;
427 }
428
664cceb0
DH
429 key_ref = lookup_user_key(NULL, id, 0, 0, 0);
430 if (IS_ERR(key_ref)) {
431 ret = PTR_ERR(key_ref);
1da177e4
LT
432 goto error2;
433 }
434
664cceb0 435 ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
1da177e4 436
664cceb0 437 key_ref_put(key_ref);
1da177e4 438 error2:
664cceb0 439 key_ref_put(keyring_ref);
1da177e4
LT
440 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{
3e30148c 460 struct key *key, *instkey;
664cceb0 461 key_ref_t key_ref;
1da177e4
LT
462 char *tmpbuf;
463 long ret;
464
664cceb0
DH
465 key_ref = lookup_user_key(NULL, keyid, 0, 1, KEY_VIEW);
466 if (IS_ERR(key_ref)) {
3e30148c
DH
467 /* viewing a key under construction is permitted if we have the
468 * authorisation token handy */
664cceb0 469 if (PTR_ERR(key_ref) == -EACCES) {
3e30148c
DH
470 instkey = key_get_instantiation_authkey(keyid);
471 if (!IS_ERR(instkey)) {
472 key_put(instkey);
664cceb0
DH
473 key_ref = lookup_user_key(NULL, keyid,
474 0, 1, 0);
475 if (!IS_ERR(key_ref))
3e30148c
DH
476 goto okay;
477 }
478 }
479
664cceb0 480 ret = PTR_ERR(key_ref);
1da177e4
LT
481 goto error;
482 }
483
3e30148c 484okay:
1da177e4
LT
485 /* 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
664cceb0
DH
491 key = key_ref_to_ptr(key_ref);
492
1da177e4 493 ret = snprintf(tmpbuf, PAGE_SIZE - 1,
664cceb0
DH
494 "%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 : ""
1da177e4
LT
501 );
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:
664cceb0 520 key_ref_put(key_ref);
1da177e4
LT
521 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;
664cceb0 542 key_ref_t keyring_ref, key_ref, dest_ref;
1da177e4 543 char type[32], *description;
0cb409d9 544 long ret;
1da177e4
LT
545
546 /* pull the type and description into kernel space */
0cb409d9 547 ret = key_get_type_from_user(type, _type, sizeof(type));
1da177e4
LT
548 if (ret < 0)
549 goto error;
1da177e4 550
0cb409d9
DA
551 description = strndup_user(_description, PAGE_SIZE);
552 if (IS_ERR(description)) {
553 ret = PTR_ERR(description);
1da177e4 554 goto error;
0cb409d9 555 }
1da177e4
LT
556
557 /* get the keyring at which to begin the search */
664cceb0
DH
558 keyring_ref = lookup_user_key(NULL, ringid, 0, 0, KEY_SEARCH);
559 if (IS_ERR(keyring_ref)) {
560 ret = PTR_ERR(keyring_ref);
1da177e4
LT
561 goto error2;
562 }
563
564 /* get the destination keyring if specified */
664cceb0 565 dest_ref = NULL;
1da177e4 566 if (destringid) {
664cceb0
DH
567 dest_ref = lookup_user_key(NULL, destringid, 1, 0, KEY_WRITE);
568 if (IS_ERR(dest_ref)) {
569 ret = PTR_ERR(dest_ref);
1da177e4
LT
570 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 */
664cceb0
DH
582 key_ref = keyring_search(keyring_ref, ktype, description);
583 if (IS_ERR(key_ref)) {
584 ret = PTR_ERR(key_ref);
1da177e4
LT
585
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 */
664cceb0 593 if (dest_ref) {
29db9190
DH
594 ret = key_permission(key_ref, KEY_LINK);
595 if (ret < 0)
1da177e4
LT
596 goto error6;
597
664cceb0 598 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
1da177e4
LT
599 if (ret < 0)
600 goto error6;
601 }
602
664cceb0 603 ret = key_ref_to_ptr(key_ref)->serial;
1da177e4
LT
604
605 error6:
664cceb0 606 key_ref_put(key_ref);
1da177e4
LT
607 error5:
608 key_type_put(ktype);
609 error4:
664cceb0 610 key_ref_put(dest_ref);
1da177e4 611 error3:
664cceb0 612 key_ref_put(keyring_ref);
1da177e4
LT
613 error2:
614 kfree(description);
615 error:
616 return ret;
617
618} /* end keyctl_keyring_search() */
619
1da177e4
LT
620/*****************************************************************************/
621/*
622 * 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{
664cceb0
DH
632 struct key *key;
633 key_ref_t key_ref;
1da177e4
LT
634 long ret;
635
636 /* find the key first */
664cceb0
DH
637 key_ref = lookup_user_key(NULL, keyid, 0, 0, 0);
638 if (IS_ERR(key_ref)) {
639 ret = -ENOKEY;
640 goto error;
1da177e4
LT
641 }
642
664cceb0
DH
643 key = key_ref_to_ptr(key_ref);
644
645 /* see if we can read it directly */
29db9190
DH
646 ret = key_permission(key_ref, KEY_READ);
647 if (ret == 0)
664cceb0 648 goto can_read_key;
29db9190
DH
649 if (ret != -EACCES)
650 goto error;
664cceb0
DH
651
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;
658 goto error2;
659 }
1da177e4
LT
660
661 /* the key is probably readable - now try to read it */
1da177e4
LT
662 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{
5801649d 691 struct key_user *newowner, *zapowner = NULL;
1da177e4 692 struct key *key;
664cceb0 693 key_ref_t key_ref;
1da177e4
LT
694 long ret;
695
696 ret = 0;
697 if (uid == (uid_t) -1 && gid == (gid_t) -1)
698 goto error;
699
29db9190 700 key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
664cceb0
DH
701 if (IS_ERR(key_ref)) {
702 ret = PTR_ERR(key_ref);
1da177e4
LT
703 goto error;
704 }
705
664cceb0
DH
706 key = key_ref_to_ptr(key_ref);
707
1da177e4
LT
708 /* make the changes with the locks held to prevent chown/chown races */
709 ret = -EACCES;
710 down_write(&key->sem);
1da177e4
LT
711
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)
5801649d 715 goto error_put;
1da177e4
LT
716
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))
5801649d 720 goto error_put;
1da177e4
LT
721 }
722
5801649d 723 /* change the UID */
1da177e4 724 if (uid != (uid_t) -1 && uid != key->uid) {
5801649d
FT
725 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;
1da177e4
LT
759 }
760
761 /* change the GID */
762 if (gid != (gid_t) -1)
763 key->gid = gid;
764
765 ret = 0;
766
5801649d 767error_put:
1da177e4
LT
768 up_write(&key->sem);
769 key_put(key);
5801649d
FT
770 if (zapowner)
771 key_user_put(zapowner);
772error:
1da177e4
LT
773 return ret;
774
5801649d
FT
775quota_overrun:
776 spin_unlock(&newowner->lock);
777 zapowner = newowner;
778 ret = -EDQUOT;
779 goto error_put;
780
1da177e4
LT
781} /* 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;
664cceb0 792 key_ref_t key_ref;
1da177e4
LT
793 long ret;
794
795 ret = -EINVAL;
664cceb0 796 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
1da177e4
LT
797 goto error;
798
29db9190 799 key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
664cceb0
DH
800 if (IS_ERR(key_ref)) {
801 ret = PTR_ERR(key_ref);
1da177e4
LT
802 goto error;
803 }
804
664cceb0
DH
805 key = key_ref_to_ptr(key_ref);
806
76d8aeab 807 /* make the changes with the locks held to prevent chown/chmod races */
1da177e4
LT
808 ret = -EACCES;
809 down_write(&key->sem);
1da177e4 810
76d8aeab
DH
811 /* 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 }
1da177e4 816
1da177e4
LT
817 up_write(&key->sem);
818 key_put(key);
76d8aeab 819error:
1da177e4
LT
820 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{
3e30148c 834 struct request_key_auth *rka;
664cceb0
DH
835 struct key *instkey;
836 key_ref_t keyring_ref;
1da177e4
LT
837 void *payload;
838 long ret;
38bbca6b 839 bool vm = false;
1da177e4
LT
840
841 ret = -EINVAL;
38bbca6b 842 if (plen > 1024 * 1024 - 1)
1da177e4
LT
843 goto error;
844
b5f545c8
DH
845 /* 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
1da177e4
LT
856 /* pull the payload in if one was supplied */
857 payload = NULL;
858
859 if (_payload) {
860 ret = -ENOMEM;
861 payload = kmalloc(plen, GFP_KERNEL);
38bbca6b
DH
862 if (!payload) {
863 if (plen <= PAGE_SIZE)
864 goto error;
865 vm = true;
866 payload = vmalloc(plen);
867 if (!payload)
868 goto error;
869 }
1da177e4
LT
870
871 ret = -EFAULT;
872 if (copy_from_user(payload, _payload, plen) != 0)
873 goto error2;
874 }
875
3e30148c
DH
876 /* find the destination keyring amongst those belonging to the
877 * requesting task */
664cceb0 878 keyring_ref = NULL;
1da177e4 879 if (ringid) {
664cceb0
DH
880 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);
b5f545c8 884 goto error2;
1da177e4
LT
885 }
886 }
887
888 /* instantiate the key and link it into a keyring */
3e30148c 889 ret = key_instantiate_and_link(rka->target_key, payload, plen,
664cceb0 890 key_ref_to_ptr(keyring_ref), instkey);
1da177e4 891
664cceb0 892 key_ref_put(keyring_ref);
b5f545c8
DH
893
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:
38bbca6b
DH
902 if (!vm)
903 kfree(payload);
904 else
905 vfree(payload);
b5f545c8 906error:
1da177e4
LT
907 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{
3e30148c 918 struct request_key_auth *rka;
664cceb0
DH
919 struct key *instkey;
920 key_ref_t keyring_ref;
1da177e4
LT
921 long ret;
922
b5f545c8
DH
923 /* 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)
1da177e4 928 goto error;
1da177e4 929
3e30148c 930 rka = instkey->payload.data;
b5f545c8
DH
931 if (rka->target_key->serial != id)
932 goto error;
3e30148c 933
1da177e4
LT
934 /* find the destination keyring if present (which must also be
935 * writable) */
664cceb0 936 keyring_ref = NULL;
1da177e4 937 if (ringid) {
664cceb0
DH
938 keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
939 if (IS_ERR(keyring_ref)) {
940 ret = PTR_ERR(keyring_ref);
b5f545c8 941 goto error;
1da177e4
LT
942 }
943 }
944
945 /* instantiate the key and link it into a keyring */
664cceb0
DH
946 ret = key_negate_and_link(rka->target_key, timeout,
947 key_ref_to_ptr(keyring_ref), instkey);
1da177e4 948
664cceb0 949 key_ref_put(keyring_ref);
b5f545c8
DH
950
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:
1da177e4
LT
959 return ret;
960
961} /* end keyctl_negate_key() */
962
3e30148c
DH
963/*****************************************************************************/
964/*
965 * 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
017679c4
DH
1001/*****************************************************************************/
1002/*
1003 * 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
b5f545c8
DH
1041/*****************************************************************************/
1042/*
1043 * 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
1da177e4
LT
1083/*****************************************************************************/
1084/*
1085 * 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
3e30148c
DH
1153 case KEYCTL_SET_REQKEY_KEYRING:
1154 return keyctl_set_reqkey_keyring(arg2);
1155
017679c4
DH
1156 case KEYCTL_SET_TIMEOUT:
1157 return keyctl_set_timeout((key_serial_t) arg2,
1158 (unsigned) arg3);
1159
b5f545c8
DH
1160 case KEYCTL_ASSUME_AUTHORITY:
1161 return keyctl_assume_authority((key_serial_t) arg2);
1162
1da177e4
LT
1163 default:
1164 return -EOPNOTSUPP;
1165 }
1166
1167} /* end sys_keyctl() */