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