~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/security/keys/keyctl.c

Version: ~ [ linux-6.0-rc6 ] ~ [ linux-5.19.10 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.69 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.144 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.214 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.259 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.294 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.329 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp