Merge tag 'v3.10.108' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / security / keys / keyctl.c
CommitLineData
973c9f4f 1/* Userspace key control operations
1da177e4 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>
59e6b9c1 17#include <linux/key.h>
1da177e4
LT
18#include <linux/keyctl.h>
19#include <linux/fs.h>
c59ede7b 20#include <linux/capability.h>
0cb409d9 21#include <linux/string.h>
1da177e4 22#include <linux/err.h>
38bbca6b 23#include <linux/vmalloc.h>
70a5bb72 24#include <linux/security.h>
a27bb332 25#include <linux/uio.h>
1da177e4
LT
26#include <asm/uaccess.h>
27#include "internal.h"
28
0cb409d9
DA
29static int key_get_type_from_user(char *type,
30 const char __user *_type,
31 unsigned len)
32{
33 int ret;
34
35 ret = strncpy_from_user(type, _type, len);
0cb409d9 36 if (ret < 0)
4303ef19 37 return ret;
0cb409d9
DA
38 if (ret == 0 || ret >= len)
39 return -EINVAL;
0cb409d9
DA
40 if (type[0] == '.')
41 return -EPERM;
0cb409d9 42 type[len - 1] = '\0';
0cb409d9
DA
43 return 0;
44}
45
1da177e4 46/*
973c9f4f
DH
47 * Extract the description of a new key from userspace and either add it as a
48 * new key to the specified keyring or update a matching key in that keyring.
49 *
cf7f601c
DH
50 * If the description is NULL or an empty string, the key type is asked to
51 * generate one from the payload.
52 *
973c9f4f
DH
53 * The keyring must be writable so that we can attach the key to it.
54 *
55 * If successful, the new key's serial number is returned, otherwise an error
56 * code is returned.
1da177e4 57 */
1e7bfb21
HC
58SYSCALL_DEFINE5(add_key, const char __user *, _type,
59 const char __user *, _description,
60 const void __user *, _payload,
61 size_t, plen,
62 key_serial_t, ringid)
1da177e4 63{
664cceb0 64 key_ref_t keyring_ref, key_ref;
1da177e4
LT
65 char type[32], *description;
66 void *payload;
0cb409d9 67 long ret;
38bbca6b 68 bool vm;
1da177e4
LT
69
70 ret = -EINVAL;
38bbca6b 71 if (plen > 1024 * 1024 - 1)
1da177e4
LT
72 goto error;
73
74 /* draw all the data into kernel space */
0cb409d9 75 ret = key_get_type_from_user(type, _type, sizeof(type));
1da177e4
LT
76 if (ret < 0)
77 goto error;
1da177e4 78
cf7f601c
DH
79 description = NULL;
80 if (_description) {
81 description = strndup_user(_description, PAGE_SIZE);
82 if (IS_ERR(description)) {
83 ret = PTR_ERR(description);
84 goto error;
85 }
86 if (!*description) {
87 kfree(description);
88 description = NULL;
89 }
0cb409d9 90 }
1da177e4
LT
91
92 /* pull the payload in if one was supplied */
93 payload = NULL;
94
38bbca6b 95 vm = false;
f862c943 96 if (plen) {
1da177e4 97 ret = -ENOMEM;
4f1c28d2 98 payload = kmalloc(plen, GFP_KERNEL | __GFP_NOWARN);
38bbca6b
DH
99 if (!payload) {
100 if (plen <= PAGE_SIZE)
101 goto error2;
102 vm = true;
103 payload = vmalloc(plen);
104 if (!payload)
105 goto error2;
106 }
1da177e4
LT
107
108 ret = -EFAULT;
109 if (copy_from_user(payload, _payload, plen) != 0)
110 goto error3;
111 }
112
113 /* find the target keyring (which must be writable) */
5593122e 114 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
664cceb0
DH
115 if (IS_ERR(keyring_ref)) {
116 ret = PTR_ERR(keyring_ref);
1da177e4
LT
117 goto error3;
118 }
119
120 /* create or update the requested key and add it to the target
121 * keyring */
664cceb0 122 key_ref = key_create_or_update(keyring_ref, type, description,
6b79ccb5
AR
123 payload, plen, KEY_PERM_UNDEF,
124 KEY_ALLOC_IN_QUOTA);
664cceb0
DH
125 if (!IS_ERR(key_ref)) {
126 ret = key_ref_to_ptr(key_ref)->serial;
127 key_ref_put(key_ref);
1da177e4
LT
128 }
129 else {
664cceb0 130 ret = PTR_ERR(key_ref);
1da177e4
LT
131 }
132
664cceb0 133 key_ref_put(keyring_ref);
1da177e4 134 error3:
38bbca6b
DH
135 if (!vm)
136 kfree(payload);
137 else
138 vfree(payload);
1da177e4
LT
139 error2:
140 kfree(description);
141 error:
142 return ret;
a8b17ed0 143}
1da177e4 144
1da177e4 145/*
973c9f4f
DH
146 * Search the process keyrings and keyring trees linked from those for a
147 * matching key. Keyrings must have appropriate Search permission to be
148 * searched.
149 *
150 * If a key is found, it will be attached to the destination keyring if there's
151 * one specified and the serial number of the key will be returned.
152 *
153 * If no key is found, /sbin/request-key will be invoked if _callout_info is
154 * non-NULL in an attempt to create a key. The _callout_info string will be
155 * passed to /sbin/request-key to aid with completing the request. If the
156 * _callout_info string is "" then it will be changed to "-".
1da177e4 157 */
1e7bfb21
HC
158SYSCALL_DEFINE4(request_key, const char __user *, _type,
159 const char __user *, _description,
160 const char __user *, _callout_info,
161 key_serial_t, destringid)
1da177e4
LT
162{
163 struct key_type *ktype;
664cceb0
DH
164 struct key *key;
165 key_ref_t dest_ref;
4a38e122 166 size_t callout_len;
1da177e4 167 char type[32], *description, *callout_info;
0cb409d9 168 long ret;
1da177e4
LT
169
170 /* pull the type into kernel space */
0cb409d9 171 ret = key_get_type_from_user(type, _type, sizeof(type));
1da177e4
LT
172 if (ret < 0)
173 goto error;
1260f801 174
1da177e4 175 /* pull the description into kernel space */
0cb409d9
DA
176 description = strndup_user(_description, PAGE_SIZE);
177 if (IS_ERR(description)) {
178 ret = PTR_ERR(description);
1da177e4 179 goto error;
0cb409d9 180 }
1da177e4
LT
181
182 /* pull the callout info into kernel space */
183 callout_info = NULL;
4a38e122 184 callout_len = 0;
1da177e4 185 if (_callout_info) {
0cb409d9
DA
186 callout_info = strndup_user(_callout_info, PAGE_SIZE);
187 if (IS_ERR(callout_info)) {
188 ret = PTR_ERR(callout_info);
1da177e4 189 goto error2;
0cb409d9 190 }
4a38e122 191 callout_len = strlen(callout_info);
1da177e4
LT
192 }
193
194 /* get the destination keyring if specified */
664cceb0 195 dest_ref = NULL;
1da177e4 196 if (destringid) {
5593122e
DH
197 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
198 KEY_WRITE);
664cceb0
DH
199 if (IS_ERR(dest_ref)) {
200 ret = PTR_ERR(dest_ref);
1da177e4
LT
201 goto error3;
202 }
203 }
204
205 /* find the key type */
206 ktype = key_type_lookup(type);
207 if (IS_ERR(ktype)) {
208 ret = PTR_ERR(ktype);
209 goto error4;
210 }
211
212 /* do the search */
4a38e122
DH
213 key = request_key_and_link(ktype, description, callout_info,
214 callout_len, NULL, key_ref_to_ptr(dest_ref),
7e047ef5 215 KEY_ALLOC_IN_QUOTA);
1da177e4
LT
216 if (IS_ERR(key)) {
217 ret = PTR_ERR(key);
218 goto error5;
219 }
220
4aab1e89
DH
221 /* wait for the key to finish being constructed */
222 ret = wait_for_key_construction(key, 1);
223 if (ret < 0)
224 goto error6;
225
1da177e4
LT
226 ret = key->serial;
227
4aab1e89 228error6:
3e30148c 229 key_put(key);
c5b60b5e 230error5:
1da177e4 231 key_type_put(ktype);
c5b60b5e 232error4:
664cceb0 233 key_ref_put(dest_ref);
c5b60b5e 234error3:
1da177e4 235 kfree(callout_info);
c5b60b5e 236error2:
1da177e4 237 kfree(description);
c5b60b5e 238error:
1da177e4 239 return ret;
a8b17ed0 240}
1da177e4 241
1da177e4 242/*
973c9f4f
DH
243 * Get the ID of the specified process keyring.
244 *
245 * The requested keyring must have search permission to be found.
246 *
247 * If successful, the ID of the requested keyring will be returned.
1da177e4
LT
248 */
249long keyctl_get_keyring_ID(key_serial_t id, int create)
250{
664cceb0 251 key_ref_t key_ref;
5593122e 252 unsigned long lflags;
1da177e4
LT
253 long ret;
254
5593122e
DH
255 lflags = create ? KEY_LOOKUP_CREATE : 0;
256 key_ref = lookup_user_key(id, lflags, KEY_SEARCH);
664cceb0
DH
257 if (IS_ERR(key_ref)) {
258 ret = PTR_ERR(key_ref);
1da177e4
LT
259 goto error;
260 }
261
664cceb0
DH
262 ret = key_ref_to_ptr(key_ref)->serial;
263 key_ref_put(key_ref);
c5b60b5e 264error:
1da177e4 265 return ret;
973c9f4f 266}
1da177e4 267
1da177e4 268/*
973c9f4f
DH
269 * Join a (named) session keyring.
270 *
271 * Create and join an anonymous session keyring or join a named session
272 * keyring, creating it if necessary. A named session keyring must have Search
273 * permission for it to be joined. Session keyrings without this permit will
8e728d26
DH
274 * be skipped over. It is not permitted for userspace to create or join
275 * keyrings whose name begin with a dot.
973c9f4f
DH
276 *
277 * If successful, the ID of the joined session keyring will be returned.
1da177e4
LT
278 */
279long keyctl_join_session_keyring(const char __user *_name)
280{
281 char *name;
0cb409d9 282 long ret;
1da177e4
LT
283
284 /* fetch the name from userspace */
285 name = NULL;
286 if (_name) {
0cb409d9
DA
287 name = strndup_user(_name, PAGE_SIZE);
288 if (IS_ERR(name)) {
289 ret = PTR_ERR(name);
1da177e4 290 goto error;
0cb409d9 291 }
8e728d26
DH
292
293 ret = -EPERM;
294 if (name[0] == '.')
295 goto error_name;
1da177e4
LT
296 }
297
298 /* join the session */
299 ret = join_session_keyring(name);
8e728d26 300error_name:
0d54ee1c 301 kfree(name);
c5b60b5e 302error:
1da177e4 303 return ret;
a8b17ed0 304}
1da177e4 305
1da177e4 306/*
973c9f4f
DH
307 * Update a key's data payload from the given data.
308 *
309 * The key must grant the caller Write permission and the key type must support
310 * updating for this to work. A negative key can be positively instantiated
311 * with this call.
312 *
313 * If successful, 0 will be returned. If the key type does not support
314 * updating, then -EOPNOTSUPP will be returned.
1da177e4
LT
315 */
316long keyctl_update_key(key_serial_t id,
317 const void __user *_payload,
318 size_t plen)
319{
664cceb0 320 key_ref_t key_ref;
1da177e4
LT
321 void *payload;
322 long ret;
323
324 ret = -EINVAL;
325 if (plen > PAGE_SIZE)
326 goto error;
327
328 /* pull the payload in if one was supplied */
329 payload = NULL;
f862c943 330 if (plen) {
1da177e4
LT
331 ret = -ENOMEM;
332 payload = kmalloc(plen, GFP_KERNEL);
333 if (!payload)
334 goto error;
335
336 ret = -EFAULT;
337 if (copy_from_user(payload, _payload, plen) != 0)
338 goto error2;
339 }
340
341 /* find the target key (which must be writable) */
5593122e 342 key_ref = lookup_user_key(id, 0, KEY_WRITE);
664cceb0
DH
343 if (IS_ERR(key_ref)) {
344 ret = PTR_ERR(key_ref);
1da177e4
LT
345 goto error2;
346 }
347
348 /* update the key */
664cceb0 349 ret = key_update(key_ref, payload, plen);
1da177e4 350
664cceb0 351 key_ref_put(key_ref);
c5b60b5e 352error2:
1da177e4 353 kfree(payload);
c5b60b5e 354error:
1da177e4 355 return ret;
a8b17ed0 356}
1da177e4 357
1da177e4 358/*
973c9f4f
DH
359 * Revoke a key.
360 *
361 * The key must be grant the caller Write or Setattr permission for this to
362 * work. The key type should give up its quota claim when revoked. The key
363 * and any links to the key will be automatically garbage collected after a
364 * certain amount of time (/proc/sys/kernel/keys/gc_delay).
365 *
366 * If successful, 0 is returned.
1da177e4
LT
367 */
368long keyctl_revoke_key(key_serial_t id)
369{
664cceb0 370 key_ref_t key_ref;
1da177e4
LT
371 long ret;
372
5593122e 373 key_ref = lookup_user_key(id, 0, KEY_WRITE);
664cceb0
DH
374 if (IS_ERR(key_ref)) {
375 ret = PTR_ERR(key_ref);
0c2c9a3f
DH
376 if (ret != -EACCES)
377 goto error;
378 key_ref = lookup_user_key(id, 0, KEY_SETATTR);
379 if (IS_ERR(key_ref)) {
380 ret = PTR_ERR(key_ref);
381 goto error;
382 }
1da177e4
LT
383 }
384
664cceb0 385 key_revoke(key_ref_to_ptr(key_ref));
1da177e4
LT
386 ret = 0;
387
664cceb0 388 key_ref_put(key_ref);
c5b60b5e 389error:
1260f801 390 return ret;
a8b17ed0 391}
1da177e4 392
fd75815f
DH
393/*
394 * Invalidate a key.
395 *
396 * The key must be grant the caller Invalidate permission for this to work.
397 * The key and any links to the key will be automatically garbage collected
398 * immediately.
399 *
400 * If successful, 0 is returned.
401 */
402long keyctl_invalidate_key(key_serial_t id)
403{
404 key_ref_t key_ref;
405 long ret;
406
407 kenter("%d", id);
408
409 key_ref = lookup_user_key(id, 0, KEY_SEARCH);
410 if (IS_ERR(key_ref)) {
411 ret = PTR_ERR(key_ref);
412 goto error;
413 }
414
415 key_invalidate(key_ref_to_ptr(key_ref));
416 ret = 0;
417
418 key_ref_put(key_ref);
419error:
420 kleave(" = %ld", ret);
421 return ret;
422}
423
1da177e4 424/*
973c9f4f
DH
425 * Clear the specified keyring, creating an empty process keyring if one of the
426 * special keyring IDs is used.
427 *
428 * The keyring must grant the caller Write permission for this to work. If
429 * successful, 0 will be returned.
1da177e4
LT
430 */
431long keyctl_keyring_clear(key_serial_t ringid)
432{
664cceb0 433 key_ref_t keyring_ref;
1da177e4
LT
434 long ret;
435
5593122e 436 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
664cceb0
DH
437 if (IS_ERR(keyring_ref)) {
438 ret = PTR_ERR(keyring_ref);
700920eb
DH
439
440 /* Root is permitted to invalidate certain special keyrings */
441 if (capable(CAP_SYS_ADMIN)) {
442 keyring_ref = lookup_user_key(ringid, 0, 0);
443 if (IS_ERR(keyring_ref))
444 goto error;
445 if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
446 &key_ref_to_ptr(keyring_ref)->flags))
447 goto clear;
448 goto error_put;
449 }
450
1da177e4
LT
451 goto error;
452 }
453
700920eb 454clear:
664cceb0 455 ret = keyring_clear(key_ref_to_ptr(keyring_ref));
700920eb 456error_put:
664cceb0 457 key_ref_put(keyring_ref);
c5b60b5e 458error:
1da177e4 459 return ret;
a8b17ed0 460}
1da177e4 461
1da177e4 462/*
973c9f4f
DH
463 * Create a link from a keyring to a key if there's no matching key in the
464 * keyring, otherwise replace the link to the matching key with a link to the
465 * new key.
466 *
467 * The key must grant the caller Link permission and the the keyring must grant
468 * the caller Write permission. Furthermore, if an additional link is created,
469 * the keyring's quota will be extended.
470 *
471 * If successful, 0 will be returned.
1da177e4
LT
472 */
473long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
474{
664cceb0 475 key_ref_t keyring_ref, key_ref;
1da177e4
LT
476 long ret;
477
5593122e 478 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
664cceb0
DH
479 if (IS_ERR(keyring_ref)) {
480 ret = PTR_ERR(keyring_ref);
1da177e4
LT
481 goto error;
482 }
483
5593122e 484 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_LINK);
664cceb0
DH
485 if (IS_ERR(key_ref)) {
486 ret = PTR_ERR(key_ref);
1da177e4
LT
487 goto error2;
488 }
489
664cceb0 490 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
1da177e4 491
664cceb0 492 key_ref_put(key_ref);
c5b60b5e 493error2:
664cceb0 494 key_ref_put(keyring_ref);
c5b60b5e 495error:
1da177e4 496 return ret;
a8b17ed0 497}
1da177e4 498
1da177e4 499/*
973c9f4f
DH
500 * Unlink a key from a keyring.
501 *
502 * The keyring must grant the caller Write permission for this to work; the key
503 * itself need not grant the caller anything. If the last link to a key is
504 * removed then that key will be scheduled for destruction.
505 *
506 * If successful, 0 will be returned.
1da177e4
LT
507 */
508long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
509{
664cceb0 510 key_ref_t keyring_ref, key_ref;
1da177e4
LT
511 long ret;
512
5593122e 513 keyring_ref = lookup_user_key(ringid, 0, KEY_WRITE);
664cceb0
DH
514 if (IS_ERR(keyring_ref)) {
515 ret = PTR_ERR(keyring_ref);
1da177e4
LT
516 goto error;
517 }
518
5593122e 519 key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
664cceb0
DH
520 if (IS_ERR(key_ref)) {
521 ret = PTR_ERR(key_ref);
1da177e4
LT
522 goto error2;
523 }
524
664cceb0 525 ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
1da177e4 526
664cceb0 527 key_ref_put(key_ref);
c5b60b5e 528error2:
664cceb0 529 key_ref_put(keyring_ref);
c5b60b5e 530error:
1da177e4 531 return ret;
a8b17ed0 532}
1da177e4 533
1da177e4 534/*
973c9f4f
DH
535 * Return a description of a key to userspace.
536 *
537 * The key must grant the caller View permission for this to work.
538 *
539 * If there's a buffer, we place up to buflen bytes of data into it formatted
540 * in the following way:
541 *
1da177e4 542 * type;uid;gid;perm;description<NUL>
973c9f4f
DH
543 *
544 * If successful, we return the amount of description available, irrespective
545 * of how much we may have copied into the buffer.
1da177e4
LT
546 */
547long keyctl_describe_key(key_serial_t keyid,
548 char __user *buffer,
549 size_t buflen)
550{
3e30148c 551 struct key *key, *instkey;
664cceb0 552 key_ref_t key_ref;
1da177e4
LT
553 char *tmpbuf;
554 long ret;
555
5593122e 556 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
664cceb0 557 if (IS_ERR(key_ref)) {
3e30148c
DH
558 /* viewing a key under construction is permitted if we have the
559 * authorisation token handy */
664cceb0 560 if (PTR_ERR(key_ref) == -EACCES) {
3e30148c
DH
561 instkey = key_get_instantiation_authkey(keyid);
562 if (!IS_ERR(instkey)) {
563 key_put(instkey);
8bbf4976 564 key_ref = lookup_user_key(keyid,
5593122e
DH
565 KEY_LOOKUP_PARTIAL,
566 0);
664cceb0 567 if (!IS_ERR(key_ref))
3e30148c
DH
568 goto okay;
569 }
570 }
571
664cceb0 572 ret = PTR_ERR(key_ref);
1da177e4
LT
573 goto error;
574 }
575
3e30148c 576okay:
1da177e4
LT
577 /* calculate how much description we're going to return */
578 ret = -ENOMEM;
579 tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
580 if (!tmpbuf)
581 goto error2;
582
664cceb0
DH
583 key = key_ref_to_ptr(key_ref);
584
1da177e4 585 ret = snprintf(tmpbuf, PAGE_SIZE - 1,
664cceb0 586 "%s;%d;%d;%08x;%s",
94fd8405 587 key->type->name,
9a56c2db
EB
588 from_kuid_munged(current_user_ns(), key->uid),
589 from_kgid_munged(current_user_ns(), key->gid),
94fd8405
DH
590 key->perm,
591 key->description ?: "");
1da177e4
LT
592
593 /* include a NUL char at the end of the data */
594 if (ret > PAGE_SIZE - 1)
595 ret = PAGE_SIZE - 1;
596 tmpbuf[ret] = 0;
597 ret++;
598
599 /* consider returning the data */
600 if (buffer && buflen > 0) {
601 if (buflen > ret)
602 buflen = ret;
603
604 if (copy_to_user(buffer, tmpbuf, buflen) != 0)
605 ret = -EFAULT;
606 }
607
608 kfree(tmpbuf);
c5b60b5e 609error2:
664cceb0 610 key_ref_put(key_ref);
c5b60b5e 611error:
1da177e4 612 return ret;
a8b17ed0 613}
1da177e4 614
1da177e4 615/*
973c9f4f
DH
616 * Search the specified keyring and any keyrings it links to for a matching
617 * key. Only keyrings that grant the caller Search permission will be searched
618 * (this includes the starting keyring). Only keys with Search permission can
619 * be found.
620 *
621 * If successful, the found key will be linked to the destination keyring if
622 * supplied and the key has Link permission, and the found key ID will be
623 * returned.
1da177e4
LT
624 */
625long keyctl_keyring_search(key_serial_t ringid,
626 const char __user *_type,
627 const char __user *_description,
628 key_serial_t destringid)
629{
630 struct key_type *ktype;
664cceb0 631 key_ref_t keyring_ref, key_ref, dest_ref;
1da177e4 632 char type[32], *description;
0cb409d9 633 long ret;
1da177e4
LT
634
635 /* pull the type and description into kernel space */
0cb409d9 636 ret = key_get_type_from_user(type, _type, sizeof(type));
1da177e4
LT
637 if (ret < 0)
638 goto error;
1da177e4 639
0cb409d9
DA
640 description = strndup_user(_description, PAGE_SIZE);
641 if (IS_ERR(description)) {
642 ret = PTR_ERR(description);
1da177e4 643 goto error;
0cb409d9 644 }
1da177e4
LT
645
646 /* get the keyring at which to begin the search */
5593122e 647 keyring_ref = lookup_user_key(ringid, 0, KEY_SEARCH);
664cceb0
DH
648 if (IS_ERR(keyring_ref)) {
649 ret = PTR_ERR(keyring_ref);
1da177e4
LT
650 goto error2;
651 }
652
653 /* get the destination keyring if specified */
664cceb0 654 dest_ref = NULL;
1da177e4 655 if (destringid) {
5593122e
DH
656 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
657 KEY_WRITE);
664cceb0
DH
658 if (IS_ERR(dest_ref)) {
659 ret = PTR_ERR(dest_ref);
1da177e4
LT
660 goto error3;
661 }
662 }
663
664 /* find the key type */
665 ktype = key_type_lookup(type);
666 if (IS_ERR(ktype)) {
667 ret = PTR_ERR(ktype);
668 goto error4;
669 }
670
671 /* do the search */
664cceb0
DH
672 key_ref = keyring_search(keyring_ref, ktype, description);
673 if (IS_ERR(key_ref)) {
674 ret = PTR_ERR(key_ref);
1da177e4
LT
675
676 /* treat lack or presence of a negative key the same */
677 if (ret == -EAGAIN)
678 ret = -ENOKEY;
679 goto error5;
680 }
681
682 /* link the resulting key to the destination keyring if we can */
664cceb0 683 if (dest_ref) {
29db9190
DH
684 ret = key_permission(key_ref, KEY_LINK);
685 if (ret < 0)
1da177e4
LT
686 goto error6;
687
664cceb0 688 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
1da177e4
LT
689 if (ret < 0)
690 goto error6;
691 }
692
664cceb0 693 ret = key_ref_to_ptr(key_ref)->serial;
1da177e4 694
c5b60b5e 695error6:
664cceb0 696 key_ref_put(key_ref);
c5b60b5e 697error5:
1da177e4 698 key_type_put(ktype);
c5b60b5e 699error4:
664cceb0 700 key_ref_put(dest_ref);
c5b60b5e 701error3:
664cceb0 702 key_ref_put(keyring_ref);
c5b60b5e 703error2:
1da177e4 704 kfree(description);
c5b60b5e 705error:
1da177e4 706 return ret;
a8b17ed0 707}
1da177e4 708
1da177e4 709/*
973c9f4f
DH
710 * Read a key's payload.
711 *
712 * The key must either grant the caller Read permission, or it must grant the
713 * caller Search permission when searched for from the process keyrings.
714 *
715 * If successful, we place up to buflen bytes of data into the buffer, if one
716 * is provided, and return the amount of data that is available in the key,
717 * irrespective of how much we copied into the buffer.
1da177e4
LT
718 */
719long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
720{
664cceb0
DH
721 struct key *key;
722 key_ref_t key_ref;
1da177e4
LT
723 long ret;
724
725 /* find the key first */
5593122e 726 key_ref = lookup_user_key(keyid, 0, 0);
664cceb0
DH
727 if (IS_ERR(key_ref)) {
728 ret = -ENOKEY;
729 goto error;
1da177e4
LT
730 }
731
664cceb0
DH
732 key = key_ref_to_ptr(key_ref);
733
734 /* see if we can read it directly */
29db9190
DH
735 ret = key_permission(key_ref, KEY_READ);
736 if (ret == 0)
664cceb0 737 goto can_read_key;
29db9190
DH
738 if (ret != -EACCES)
739 goto error;
664cceb0
DH
740
741 /* we can't; see if it's searchable from this process's keyrings
742 * - we automatically take account of the fact that it may be
743 * dangling off an instantiation key
744 */
745 if (!is_key_possessed(key_ref)) {
746 ret = -EACCES;
747 goto error2;
748 }
1da177e4
LT
749
750 /* the key is probably readable - now try to read it */
c5b60b5e 751can_read_key:
afd8f582
DH
752 ret = -EOPNOTSUPP;
753 if (key->type->read) {
754 /* Read the data with the semaphore held (since we might sleep)
755 * to protect against the key being updated or revoked.
756 */
757 down_read(&key->sem);
758 ret = key_validate(key);
759 if (ret == 0)
1da177e4 760 ret = key->type->read(key, buffer, buflen);
afd8f582 761 up_read(&key->sem);
1da177e4
LT
762 }
763
c5b60b5e 764error2:
1da177e4 765 key_put(key);
c5b60b5e 766error:
1da177e4 767 return ret;
a8b17ed0 768}
1da177e4 769
1da177e4 770/*
973c9f4f
DH
771 * Change the ownership of a key
772 *
773 * The key must grant the caller Setattr permission for this to work, though
774 * the key need not be fully instantiated yet. For the UID to be changed, or
775 * for the GID to be changed to a group the caller is not a member of, the
776 * caller must have sysadmin capability. If either uid or gid is -1 then that
777 * attribute is not changed.
778 *
779 * If the UID is to be changed, the new user must have sufficient quota to
780 * accept the key. The quota deduction will be removed from the old user to
781 * the new user should the attribute be changed.
782 *
783 * If successful, 0 will be returned.
1da177e4 784 */
9a56c2db 785long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
1da177e4 786{
5801649d 787 struct key_user *newowner, *zapowner = NULL;
1da177e4 788 struct key *key;
664cceb0 789 key_ref_t key_ref;
1da177e4 790 long ret;
9a56c2db
EB
791 kuid_t uid;
792 kgid_t gid;
793
794 uid = make_kuid(current_user_ns(), user);
795 gid = make_kgid(current_user_ns(), group);
796 ret = -EINVAL;
797 if ((user != (uid_t) -1) && !uid_valid(uid))
798 goto error;
799 if ((group != (gid_t) -1) && !gid_valid(gid))
800 goto error;
1da177e4
LT
801
802 ret = 0;
9a56c2db 803 if (user == (uid_t) -1 && group == (gid_t) -1)
1da177e4
LT
804 goto error;
805
5593122e
DH
806 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
807 KEY_SETATTR);
664cceb0
DH
808 if (IS_ERR(key_ref)) {
809 ret = PTR_ERR(key_ref);
1da177e4
LT
810 goto error;
811 }
812
664cceb0
DH
813 key = key_ref_to_ptr(key_ref);
814
1da177e4
LT
815 /* make the changes with the locks held to prevent chown/chown races */
816 ret = -EACCES;
817 down_write(&key->sem);
1da177e4
LT
818
819 if (!capable(CAP_SYS_ADMIN)) {
820 /* only the sysadmin can chown a key to some other UID */
9a56c2db 821 if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
5801649d 822 goto error_put;
1da177e4
LT
823
824 /* only the sysadmin can set the key's GID to a group other
825 * than one of those that the current process subscribes to */
9a56c2db 826 if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
5801649d 827 goto error_put;
1da177e4
LT
828 }
829
5801649d 830 /* change the UID */
9a56c2db 831 if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) {
5801649d 832 ret = -ENOMEM;
9a56c2db 833 newowner = key_user_lookup(uid);
5801649d
FT
834 if (!newowner)
835 goto error_put;
836
837 /* transfer the quota burden to the new user */
838 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
9a56c2db 839 unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
0b77f5bf 840 key_quota_root_maxkeys : key_quota_maxkeys;
9a56c2db 841 unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
0b77f5bf
DH
842 key_quota_root_maxbytes : key_quota_maxbytes;
843
5801649d 844 spin_lock(&newowner->lock);
0b77f5bf
DH
845 if (newowner->qnkeys + 1 >= maxkeys ||
846 newowner->qnbytes + key->quotalen >= maxbytes ||
847 newowner->qnbytes + key->quotalen <
848 newowner->qnbytes)
5801649d
FT
849 goto quota_overrun;
850
851 newowner->qnkeys++;
852 newowner->qnbytes += key->quotalen;
853 spin_unlock(&newowner->lock);
854
855 spin_lock(&key->user->lock);
856 key->user->qnkeys--;
857 key->user->qnbytes -= key->quotalen;
858 spin_unlock(&key->user->lock);
859 }
860
861 atomic_dec(&key->user->nkeys);
862 atomic_inc(&newowner->nkeys);
863
864 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
865 atomic_dec(&key->user->nikeys);
866 atomic_inc(&newowner->nikeys);
867 }
868
869 zapowner = key->user;
870 key->user = newowner;
871 key->uid = uid;
1da177e4
LT
872 }
873
874 /* change the GID */
9a56c2db 875 if (group != (gid_t) -1)
1da177e4
LT
876 key->gid = gid;
877
878 ret = 0;
879
5801649d 880error_put:
1da177e4
LT
881 up_write(&key->sem);
882 key_put(key);
5801649d
FT
883 if (zapowner)
884 key_user_put(zapowner);
885error:
1da177e4
LT
886 return ret;
887
5801649d
FT
888quota_overrun:
889 spin_unlock(&newowner->lock);
890 zapowner = newowner;
891 ret = -EDQUOT;
892 goto error_put;
a8b17ed0 893}
5801649d 894
1da177e4 895/*
973c9f4f
DH
896 * Change the permission mask on a key.
897 *
898 * The key must grant the caller Setattr permission for this to work, though
899 * the key need not be fully instantiated yet. If the caller does not have
900 * sysadmin capability, it may only change the permission on keys that it owns.
1da177e4
LT
901 */
902long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
903{
904 struct key *key;
664cceb0 905 key_ref_t key_ref;
1da177e4
LT
906 long ret;
907
908 ret = -EINVAL;
664cceb0 909 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
1da177e4
LT
910 goto error;
911
5593122e
DH
912 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
913 KEY_SETATTR);
664cceb0
DH
914 if (IS_ERR(key_ref)) {
915 ret = PTR_ERR(key_ref);
1da177e4
LT
916 goto error;
917 }
918
664cceb0
DH
919 key = key_ref_to_ptr(key_ref);
920
76d8aeab 921 /* make the changes with the locks held to prevent chown/chmod races */
1da177e4
LT
922 ret = -EACCES;
923 down_write(&key->sem);
1da177e4 924
76d8aeab 925 /* if we're not the sysadmin, we can only change a key that we own */
9a56c2db 926 if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
76d8aeab
DH
927 key->perm = perm;
928 ret = 0;
929 }
1da177e4 930
1da177e4
LT
931 up_write(&key->sem);
932 key_put(key);
76d8aeab 933error:
1da177e4 934 return ret;
a8b17ed0 935}
1da177e4 936
8bbf4976 937/*
973c9f4f
DH
938 * Get the destination keyring for instantiation and check that the caller has
939 * Write permission on it.
8bbf4976
DH
940 */
941static long get_instantiation_keyring(key_serial_t ringid,
942 struct request_key_auth *rka,
943 struct key **_dest_keyring)
944{
945 key_ref_t dkref;
946
eca1bf5b
DH
947 *_dest_keyring = NULL;
948
8bbf4976 949 /* just return a NULL pointer if we weren't asked to make a link */
eca1bf5b 950 if (ringid == 0)
8bbf4976 951 return 0;
8bbf4976
DH
952
953 /* if a specific keyring is nominated by ID, then use that */
954 if (ringid > 0) {
5593122e 955 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
8bbf4976
DH
956 if (IS_ERR(dkref))
957 return PTR_ERR(dkref);
958 *_dest_keyring = key_ref_to_ptr(dkref);
959 return 0;
960 }
961
962 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
963 return -EINVAL;
964
965 /* otherwise specify the destination keyring recorded in the
966 * authorisation key (any KEY_SPEC_*_KEYRING) */
967 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
21279cfa 968 *_dest_keyring = key_get(rka->dest_keyring);
8bbf4976
DH
969 return 0;
970 }
971
972 return -ENOKEY;
973}
974
d84f4f99 975/*
973c9f4f 976 * Change the request_key authorisation key on the current process.
d84f4f99
DH
977 */
978static int keyctl_change_reqkey_auth(struct key *key)
979{
980 struct cred *new;
981
982 new = prepare_creds();
983 if (!new)
984 return -ENOMEM;
985
986 key_put(new->request_key_auth);
987 new->request_key_auth = key_get(key);
988
989 return commit_creds(new);
990}
991
ee009e4a
DH
992/*
993 * Copy the iovec data from userspace
994 */
995static long copy_from_user_iovec(void *buffer, const struct iovec *iov,
996 unsigned ioc)
997{
998 for (; ioc > 0; ioc--) {
999 if (copy_from_user(buffer, iov->iov_base, iov->iov_len) != 0)
1000 return -EFAULT;
1001 buffer += iov->iov_len;
1002 iov++;
1003 }
1004 return 0;
1005}
1006
1da177e4 1007/*
973c9f4f
DH
1008 * Instantiate a key with the specified payload and link the key into the
1009 * destination keyring if one is given.
1010 *
1011 * The caller must have the appropriate instantiation permit set for this to
1012 * work (see keyctl_assume_authority). No other permissions are required.
1013 *
1014 * If successful, 0 will be returned.
1da177e4 1015 */
ee009e4a
DH
1016long keyctl_instantiate_key_common(key_serial_t id,
1017 const struct iovec *payload_iov,
1018 unsigned ioc,
1019 size_t plen,
1020 key_serial_t ringid)
1da177e4 1021{
d84f4f99 1022 const struct cred *cred = current_cred();
3e30148c 1023 struct request_key_auth *rka;
8bbf4976 1024 struct key *instkey, *dest_keyring;
1da177e4
LT
1025 void *payload;
1026 long ret;
38bbca6b 1027 bool vm = false;
1da177e4 1028
d84f4f99
DH
1029 kenter("%d,,%zu,%d", id, plen, ringid);
1030
1da177e4 1031 ret = -EINVAL;
38bbca6b 1032 if (plen > 1024 * 1024 - 1)
1da177e4
LT
1033 goto error;
1034
b5f545c8
DH
1035 /* the appropriate instantiation authorisation key must have been
1036 * assumed before calling this */
1037 ret = -EPERM;
d84f4f99 1038 instkey = cred->request_key_auth;
b5f545c8
DH
1039 if (!instkey)
1040 goto error;
1041
1042 rka = instkey->payload.data;
1043 if (rka->target_key->serial != id)
1044 goto error;
1045
1da177e4
LT
1046 /* pull the payload in if one was supplied */
1047 payload = NULL;
1048
ee009e4a 1049 if (payload_iov) {
1da177e4
LT
1050 ret = -ENOMEM;
1051 payload = kmalloc(plen, GFP_KERNEL);
38bbca6b
DH
1052 if (!payload) {
1053 if (plen <= PAGE_SIZE)
1054 goto error;
1055 vm = true;
1056 payload = vmalloc(plen);
1057 if (!payload)
1058 goto error;
1059 }
1da177e4 1060
ee009e4a
DH
1061 ret = copy_from_user_iovec(payload, payload_iov, ioc);
1062 if (ret < 0)
1da177e4
LT
1063 goto error2;
1064 }
1065
3e30148c
DH
1066 /* find the destination keyring amongst those belonging to the
1067 * requesting task */
8bbf4976
DH
1068 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1069 if (ret < 0)
1070 goto error2;
1da177e4
LT
1071
1072 /* instantiate the key and link it into a keyring */
3e30148c 1073 ret = key_instantiate_and_link(rka->target_key, payload, plen,
8bbf4976 1074 dest_keyring, instkey);
1da177e4 1075
8bbf4976 1076 key_put(dest_keyring);
b5f545c8
DH
1077
1078 /* discard the assumed authority if it's just been disabled by
1079 * instantiation of the key */
d84f4f99
DH
1080 if (ret == 0)
1081 keyctl_change_reqkey_auth(NULL);
b5f545c8
DH
1082
1083error2:
38bbca6b
DH
1084 if (!vm)
1085 kfree(payload);
1086 else
1087 vfree(payload);
b5f545c8 1088error:
1da177e4 1089 return ret;
a8b17ed0 1090}
1da177e4 1091
ee009e4a
DH
1092/*
1093 * Instantiate a key with the specified payload and link the key into the
1094 * destination keyring if one is given.
1095 *
1096 * The caller must have the appropriate instantiation permit set for this to
1097 * work (see keyctl_assume_authority). No other permissions are required.
1098 *
1099 * If successful, 0 will be returned.
1100 */
1101long keyctl_instantiate_key(key_serial_t id,
1102 const void __user *_payload,
1103 size_t plen,
1104 key_serial_t ringid)
1105{
1106 if (_payload && plen) {
1107 struct iovec iov[1] = {
1108 [0].iov_base = (void __user *)_payload,
1109 [0].iov_len = plen
1110 };
1111
1112 return keyctl_instantiate_key_common(id, iov, 1, plen, ringid);
1113 }
1114
1115 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1116}
1117
1118/*
1119 * Instantiate a key with the specified multipart payload and link the key into
1120 * the destination keyring if one is given.
1121 *
1122 * The caller must have the appropriate instantiation permit set for this to
1123 * work (see keyctl_assume_authority). No other permissions are required.
1124 *
1125 * If successful, 0 will be returned.
1126 */
1127long keyctl_instantiate_key_iov(key_serial_t id,
1128 const struct iovec __user *_payload_iov,
1129 unsigned ioc,
1130 key_serial_t ringid)
1131{
1132 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1133 long ret;
1134
423b9788 1135 if (!_payload_iov || !ioc)
ee009e4a
DH
1136 goto no_payload;
1137
1138 ret = rw_copy_check_uvector(WRITE, _payload_iov, ioc,
ac34ebb3 1139 ARRAY_SIZE(iovstack), iovstack, &iov);
ee009e4a 1140 if (ret < 0)
a84a9219 1141 goto err;
ee009e4a
DH
1142 if (ret == 0)
1143 goto no_payload_free;
1144
1145 ret = keyctl_instantiate_key_common(id, iov, ioc, ret, ringid);
a84a9219 1146err:
ee009e4a
DH
1147 if (iov != iovstack)
1148 kfree(iov);
1149 return ret;
1150
1151no_payload_free:
1152 if (iov != iovstack)
1153 kfree(iov);
1154no_payload:
1155 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1156}
1157
1da177e4 1158/*
973c9f4f
DH
1159 * Negatively instantiate the key with the given timeout (in seconds) and link
1160 * the key into the destination keyring if one is given.
1161 *
1162 * The caller must have the appropriate instantiation permit set for this to
1163 * work (see keyctl_assume_authority). No other permissions are required.
1164 *
1165 * The key and any links to the key will be automatically garbage collected
1166 * after the timeout expires.
1167 *
1168 * Negative keys are used to rate limit repeated request_key() calls by causing
1169 * them to return -ENOKEY until the negative key expires.
1170 *
1171 * If successful, 0 will be returned.
1da177e4
LT
1172 */
1173long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
fdd1b945
DH
1174{
1175 return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1176}
1177
1178/*
1179 * Negatively instantiate the key with the given timeout (in seconds) and error
1180 * code and link the key into the destination keyring if one is given.
1181 *
1182 * The caller must have the appropriate instantiation permit set for this to
1183 * work (see keyctl_assume_authority). No other permissions are required.
1184 *
1185 * The key and any links to the key will be automatically garbage collected
1186 * after the timeout expires.
1187 *
1188 * Negative keys are used to rate limit repeated request_key() calls by causing
1189 * them to return the specified error code until the negative key expires.
1190 *
1191 * If successful, 0 will be returned.
1192 */
1193long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1194 key_serial_t ringid)
1da177e4 1195{
d84f4f99 1196 const struct cred *cred = current_cred();
3e30148c 1197 struct request_key_auth *rka;
8bbf4976 1198 struct key *instkey, *dest_keyring;
1da177e4
LT
1199 long ret;
1200
fdd1b945
DH
1201 kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1202
1203 /* must be a valid error code and mustn't be a kernel special */
1204 if (error <= 0 ||
1205 error >= MAX_ERRNO ||
1206 error == ERESTARTSYS ||
1207 error == ERESTARTNOINTR ||
1208 error == ERESTARTNOHAND ||
1209 error == ERESTART_RESTARTBLOCK)
1210 return -EINVAL;
d84f4f99 1211
b5f545c8
DH
1212 /* the appropriate instantiation authorisation key must have been
1213 * assumed before calling this */
1214 ret = -EPERM;
d84f4f99 1215 instkey = cred->request_key_auth;
b5f545c8 1216 if (!instkey)
1da177e4 1217 goto error;
1da177e4 1218
3e30148c 1219 rka = instkey->payload.data;
b5f545c8
DH
1220 if (rka->target_key->serial != id)
1221 goto error;
3e30148c 1222
1da177e4
LT
1223 /* find the destination keyring if present (which must also be
1224 * writable) */
8bbf4976
DH
1225 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1226 if (ret < 0)
1227 goto error;
1da177e4
LT
1228
1229 /* instantiate the key and link it into a keyring */
fdd1b945 1230 ret = key_reject_and_link(rka->target_key, timeout, error,
8bbf4976 1231 dest_keyring, instkey);
1da177e4 1232
8bbf4976 1233 key_put(dest_keyring);
b5f545c8
DH
1234
1235 /* discard the assumed authority if it's just been disabled by
1236 * instantiation of the key */
d84f4f99
DH
1237 if (ret == 0)
1238 keyctl_change_reqkey_auth(NULL);
b5f545c8
DH
1239
1240error:
1da177e4 1241 return ret;
a8b17ed0 1242}
1da177e4 1243
3e30148c 1244/*
973c9f4f
DH
1245 * Read or set the default keyring in which request_key() will cache keys and
1246 * return the old setting.
1247 *
d19182e2
EB
1248 * If a thread or process keyring is specified then it will be created if it
1249 * doesn't yet exist. The old setting will be returned if successful.
3e30148c
DH
1250 */
1251long keyctl_set_reqkey_keyring(int reqkey_defl)
1252{
d84f4f99
DH
1253 struct cred *new;
1254 int ret, old_setting;
1255
1256 old_setting = current_cred_xxx(jit_keyring);
1257
1258 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1259 return old_setting;
1260
1261 new = prepare_creds();
1262 if (!new)
1263 return -ENOMEM;
3e30148c
DH
1264
1265 switch (reqkey_defl) {
1266 case KEY_REQKEY_DEFL_THREAD_KEYRING:
d84f4f99 1267 ret = install_thread_keyring_to_cred(new);
3e30148c 1268 if (ret < 0)
d84f4f99 1269 goto error;
3e30148c
DH
1270 goto set;
1271
1272 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
d84f4f99 1273 ret = install_process_keyring_to_cred(new);
d19182e2
EB
1274 if (ret < 0)
1275 goto error;
d84f4f99 1276 goto set;
3e30148c
DH
1277
1278 case KEY_REQKEY_DEFL_DEFAULT:
1279 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1280 case KEY_REQKEY_DEFL_USER_KEYRING:
1281 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
d84f4f99
DH
1282 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1283 goto set;
3e30148c
DH
1284
1285 case KEY_REQKEY_DEFL_NO_CHANGE:
3e30148c
DH
1286 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1287 default:
d84f4f99
DH
1288 ret = -EINVAL;
1289 goto error;
3e30148c
DH
1290 }
1291
d84f4f99
DH
1292set:
1293 new->jit_keyring = reqkey_defl;
1294 commit_creds(new);
1295 return old_setting;
1296error:
1297 abort_creds(new);
4303ef19 1298 return ret;
a8b17ed0 1299}
d84f4f99 1300
017679c4 1301/*
973c9f4f
DH
1302 * Set or clear the timeout on a key.
1303 *
1304 * Either the key must grant the caller Setattr permission or else the caller
1305 * must hold an instantiation authorisation token for the key.
1306 *
1307 * The timeout is either 0 to clear the timeout, or a number of seconds from
1308 * the current time. The key and any links to the key will be automatically
1309 * garbage collected after the timeout expires.
1310 *
1311 * If successful, 0 is returned.
017679c4
DH
1312 */
1313long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1314{
9156235b 1315 struct key *key, *instkey;
017679c4 1316 key_ref_t key_ref;
017679c4
DH
1317 long ret;
1318
5593122e
DH
1319 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1320 KEY_SETATTR);
017679c4 1321 if (IS_ERR(key_ref)) {
9156235b
DH
1322 /* setting the timeout on a key under construction is permitted
1323 * if we have the authorisation token handy */
1324 if (PTR_ERR(key_ref) == -EACCES) {
1325 instkey = key_get_instantiation_authkey(id);
1326 if (!IS_ERR(instkey)) {
1327 key_put(instkey);
1328 key_ref = lookup_user_key(id,
1329 KEY_LOOKUP_PARTIAL,
1330 0);
1331 if (!IS_ERR(key_ref))
1332 goto okay;
1333 }
1334 }
1335
017679c4
DH
1336 ret = PTR_ERR(key_ref);
1337 goto error;
1338 }
1339
9156235b 1340okay:
017679c4 1341 key = key_ref_to_ptr(key_ref);
59e6b9c1 1342 key_set_timeout(key, timeout);
017679c4
DH
1343 key_put(key);
1344
1345 ret = 0;
1346error:
1347 return ret;
a8b17ed0 1348}
017679c4 1349
b5f545c8 1350/*
973c9f4f
DH
1351 * Assume (or clear) the authority to instantiate the specified key.
1352 *
1353 * This sets the authoritative token currently in force for key instantiation.
1354 * This must be done for a key to be instantiated. It has the effect of making
1355 * available all the keys from the caller of the request_key() that created a
1356 * key to request_key() calls made by the caller of this function.
1357 *
1358 * The caller must have the instantiation key in their process keyrings with a
1359 * Search permission grant available to the caller.
1360 *
1361 * If the ID given is 0, then the setting will be cleared and 0 returned.
1362 *
1363 * If the ID given has a matching an authorisation key, then that key will be
1364 * set and its ID will be returned. The authorisation key can be read to get
1365 * the callout information passed to request_key().
b5f545c8
DH
1366 */
1367long keyctl_assume_authority(key_serial_t id)
1368{
1369 struct key *authkey;
1370 long ret;
1371
1372 /* special key IDs aren't permitted */
1373 ret = -EINVAL;
1374 if (id < 0)
1375 goto error;
1376
1377 /* we divest ourselves of authority if given an ID of 0 */
1378 if (id == 0) {
d84f4f99 1379 ret = keyctl_change_reqkey_auth(NULL);
b5f545c8
DH
1380 goto error;
1381 }
1382
1383 /* attempt to assume the authority temporarily granted to us whilst we
1384 * instantiate the specified key
1385 * - the authorisation key must be in the current task's keyrings
1386 * somewhere
1387 */
1388 authkey = key_get_instantiation_authkey(id);
1389 if (IS_ERR(authkey)) {
1390 ret = PTR_ERR(authkey);
1391 goto error;
1392 }
1393
d84f4f99
DH
1394 ret = keyctl_change_reqkey_auth(authkey);
1395 if (ret < 0)
1396 goto error;
1397 key_put(authkey);
b5f545c8 1398
d84f4f99 1399 ret = authkey->serial;
b5f545c8
DH
1400error:
1401 return ret;
a8b17ed0 1402}
b5f545c8 1403
70a5bb72 1404/*
973c9f4f
DH
1405 * Get a key's the LSM security label.
1406 *
1407 * The key must grant the caller View permission for this to work.
1408 *
1409 * If there's a buffer, then up to buflen bytes of data will be placed into it.
1410 *
1411 * If successful, the amount of information available will be returned,
1412 * irrespective of how much was copied (including the terminal NUL).
70a5bb72
DH
1413 */
1414long keyctl_get_security(key_serial_t keyid,
1415 char __user *buffer,
1416 size_t buflen)
1417{
1418 struct key *key, *instkey;
1419 key_ref_t key_ref;
1420 char *context;
1421 long ret;
1422
5593122e 1423 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
70a5bb72
DH
1424 if (IS_ERR(key_ref)) {
1425 if (PTR_ERR(key_ref) != -EACCES)
1426 return PTR_ERR(key_ref);
1427
1428 /* viewing a key under construction is also permitted if we
1429 * have the authorisation token handy */
1430 instkey = key_get_instantiation_authkey(keyid);
1431 if (IS_ERR(instkey))
fa1cc7b5 1432 return PTR_ERR(instkey);
70a5bb72
DH
1433 key_put(instkey);
1434
5593122e 1435 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
70a5bb72
DH
1436 if (IS_ERR(key_ref))
1437 return PTR_ERR(key_ref);
1438 }
1439
1440 key = key_ref_to_ptr(key_ref);
1441 ret = security_key_getsecurity(key, &context);
1442 if (ret == 0) {
1443 /* if no information was returned, give userspace an empty
1444 * string */
1445 ret = 1;
1446 if (buffer && buflen > 0 &&
1447 copy_to_user(buffer, "", 1) != 0)
1448 ret = -EFAULT;
1449 } else if (ret > 0) {
1450 /* return as much data as there's room for */
1451 if (buffer && buflen > 0) {
1452 if (buflen > ret)
1453 buflen = ret;
1454
1455 if (copy_to_user(buffer, context, buflen) != 0)
1456 ret = -EFAULT;
1457 }
1458
1459 kfree(context);
1460 }
1461
1462 key_ref_put(key_ref);
1463 return ret;
1464}
1465
ee18d64c 1466/*
973c9f4f
DH
1467 * Attempt to install the calling process's session keyring on the process's
1468 * parent process.
1469 *
1470 * The keyring must exist and must grant the caller LINK permission, and the
1471 * parent process must be single-threaded and must have the same effective
1472 * ownership as this process and mustn't be SUID/SGID.
1473 *
1474 * The keyring will be emplaced on the parent when it next resumes userspace.
1475 *
1476 * If successful, 0 will be returned.
ee18d64c
DH
1477 */
1478long keyctl_session_to_parent(void)
1479{
1480 struct task_struct *me, *parent;
1481 const struct cred *mycred, *pcred;
67d12145 1482 struct callback_head *newwork, *oldwork;
ee18d64c 1483 key_ref_t keyring_r;
413cd3d9 1484 struct cred *cred;
ee18d64c
DH
1485 int ret;
1486
1487 keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_LINK);
1488 if (IS_ERR(keyring_r))
1489 return PTR_ERR(keyring_r);
1490
413cd3d9 1491 ret = -ENOMEM;
413cd3d9 1492
ee18d64c
DH
1493 /* our parent is going to need a new cred struct, a new tgcred struct
1494 * and new security data, so we allocate them here to prevent ENOMEM in
1495 * our parent */
ee18d64c
DH
1496 cred = cred_alloc_blank();
1497 if (!cred)
67d12145
AV
1498 goto error_keyring;
1499 newwork = &cred->rcu;
ee18d64c 1500
3a50597d
DH
1501 cred->session_keyring = key_ref_to_ptr(keyring_r);
1502 keyring_r = NULL;
67d12145 1503 init_task_work(newwork, key_change_session_keyring);
ee18d64c
DH
1504
1505 me = current;
9d1ac65a 1506 rcu_read_lock();
ee18d64c
DH
1507 write_lock_irq(&tasklist_lock);
1508
ee18d64c 1509 ret = -EPERM;
413cd3d9
ON
1510 oldwork = NULL;
1511 parent = me->real_parent;
ee18d64c
DH
1512
1513 /* the parent mustn't be init and mustn't be a kernel thread */
1514 if (parent->pid <= 1 || !parent->mm)
413cd3d9 1515 goto unlock;
ee18d64c
DH
1516
1517 /* the parent must be single threaded */
dd98acf7 1518 if (!thread_group_empty(parent))
413cd3d9 1519 goto unlock;
ee18d64c
DH
1520
1521 /* the parent and the child must have different session keyrings or
1522 * there's no point */
1523 mycred = current_cred();
1524 pcred = __task_cred(parent);
1525 if (mycred == pcred ||
3a50597d 1526 mycred->session_keyring == pcred->session_keyring) {
413cd3d9
ON
1527 ret = 0;
1528 goto unlock;
1529 }
ee18d64c
DH
1530
1531 /* the parent must have the same effective ownership and mustn't be
1532 * SUID/SGID */
9a56c2db
EB
1533 if (!uid_eq(pcred->uid, mycred->euid) ||
1534 !uid_eq(pcred->euid, mycred->euid) ||
1535 !uid_eq(pcred->suid, mycred->euid) ||
1536 !gid_eq(pcred->gid, mycred->egid) ||
1537 !gid_eq(pcred->egid, mycred->egid) ||
1538 !gid_eq(pcred->sgid, mycred->egid))
413cd3d9 1539 goto unlock;
ee18d64c
DH
1540
1541 /* the keyrings must have the same UID */
3a50597d 1542 if ((pcred->session_keyring &&
2a74dbb9
LT
1543 !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
1544 !uid_eq(mycred->session_keyring->uid, mycred->euid))
413cd3d9 1545 goto unlock;
ee18d64c 1546
413cd3d9
ON
1547 /* cancel an already pending keyring replacement */
1548 oldwork = task_work_cancel(parent, key_change_session_keyring);
ee18d64c
DH
1549
1550 /* the replacement session keyring is applied just prior to userspace
1551 * restarting */
67d12145 1552 ret = task_work_add(parent, newwork, true);
413cd3d9
ON
1553 if (!ret)
1554 newwork = NULL;
1555unlock:
ee18d64c 1556 write_unlock_irq(&tasklist_lock);
9d1ac65a 1557 rcu_read_unlock();
67d12145
AV
1558 if (oldwork)
1559 put_cred(container_of(oldwork, struct cred, rcu));
1560 if (newwork)
1561 put_cred(cred);
ee18d64c
DH
1562 return ret;
1563
1564error_keyring:
1565 key_ref_put(keyring_r);
1566 return ret;
1567}
1568
1da177e4 1569/*
973c9f4f 1570 * The key control system call
1da177e4 1571 */
938bb9f5
HC
1572SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1573 unsigned long, arg4, unsigned long, arg5)
1da177e4
LT
1574{
1575 switch (option) {
1576 case KEYCTL_GET_KEYRING_ID:
1577 return keyctl_get_keyring_ID((key_serial_t) arg2,
1578 (int) arg3);
1579
1580 case KEYCTL_JOIN_SESSION_KEYRING:
1581 return keyctl_join_session_keyring((const char __user *) arg2);
1582
1583 case KEYCTL_UPDATE:
1584 return keyctl_update_key((key_serial_t) arg2,
1585 (const void __user *) arg3,
1586 (size_t) arg4);
1587
1588 case KEYCTL_REVOKE:
1589 return keyctl_revoke_key((key_serial_t) arg2);
1590
1591 case KEYCTL_DESCRIBE:
1592 return keyctl_describe_key((key_serial_t) arg2,
1593 (char __user *) arg3,
1594 (unsigned) arg4);
1595
1596 case KEYCTL_CLEAR:
1597 return keyctl_keyring_clear((key_serial_t) arg2);
1598
1599 case KEYCTL_LINK:
1600 return keyctl_keyring_link((key_serial_t) arg2,
1601 (key_serial_t) arg3);
1602
1603 case KEYCTL_UNLINK:
1604 return keyctl_keyring_unlink((key_serial_t) arg2,
1605 (key_serial_t) arg3);
1606
1607 case KEYCTL_SEARCH:
1608 return keyctl_keyring_search((key_serial_t) arg2,
1609 (const char __user *) arg3,
1610 (const char __user *) arg4,
1611 (key_serial_t) arg5);
1612
1613 case KEYCTL_READ:
1614 return keyctl_read_key((key_serial_t) arg2,
1615 (char __user *) arg3,
1616 (size_t) arg4);
1617
1618 case KEYCTL_CHOWN:
1619 return keyctl_chown_key((key_serial_t) arg2,
1620 (uid_t) arg3,
1621 (gid_t) arg4);
1622
1623 case KEYCTL_SETPERM:
1624 return keyctl_setperm_key((key_serial_t) arg2,
1625 (key_perm_t) arg3);
1626
1627 case KEYCTL_INSTANTIATE:
1628 return keyctl_instantiate_key((key_serial_t) arg2,
1629 (const void __user *) arg3,
1630 (size_t) arg4,
1631 (key_serial_t) arg5);
1632
1633 case KEYCTL_NEGATE:
1634 return keyctl_negate_key((key_serial_t) arg2,
1635 (unsigned) arg3,
1636 (key_serial_t) arg4);
1637
3e30148c
DH
1638 case KEYCTL_SET_REQKEY_KEYRING:
1639 return keyctl_set_reqkey_keyring(arg2);
1640
017679c4
DH
1641 case KEYCTL_SET_TIMEOUT:
1642 return keyctl_set_timeout((key_serial_t) arg2,
1643 (unsigned) arg3);
1644
b5f545c8
DH
1645 case KEYCTL_ASSUME_AUTHORITY:
1646 return keyctl_assume_authority((key_serial_t) arg2);
1647
70a5bb72
DH
1648 case KEYCTL_GET_SECURITY:
1649 return keyctl_get_security((key_serial_t) arg2,
90bd49ab 1650 (char __user *) arg3,
70a5bb72
DH
1651 (size_t) arg4);
1652
ee18d64c
DH
1653 case KEYCTL_SESSION_TO_PARENT:
1654 return keyctl_session_to_parent();
1655
fdd1b945
DH
1656 case KEYCTL_REJECT:
1657 return keyctl_reject_key((key_serial_t) arg2,
1658 (unsigned) arg3,
1659 (unsigned) arg4,
1660 (key_serial_t) arg5);
1661
ee009e4a
DH
1662 case KEYCTL_INSTANTIATE_IOV:
1663 return keyctl_instantiate_key_iov(
1664 (key_serial_t) arg2,
1665 (const struct iovec __user *) arg3,
1666 (unsigned) arg4,
1667 (key_serial_t) arg5);
1668
fd75815f
DH
1669 case KEYCTL_INVALIDATE:
1670 return keyctl_invalidate_key((key_serial_t) arg2);
1671
1da177e4
LT
1672 default:
1673 return -EOPNOTSUPP;
1674 }
a8b17ed0 1675}