This source file includes following definitions.
- key_get_type_from_user
- SYSCALL_DEFINE5
- SYSCALL_DEFINE4
- keyctl_get_keyring_ID
- keyctl_join_session_keyring
- keyctl_update_key
- keyctl_revoke_key
- keyctl_invalidate_key
- keyctl_keyring_clear
- keyctl_keyring_link
- keyctl_keyring_unlink
- keyctl_keyring_move
- keyctl_describe_key
- keyctl_keyring_search
- __keyctl_read_key
- keyctl_read_key
- keyctl_chown_key
- keyctl_setperm_key
- get_instantiation_keyring
- keyctl_change_reqkey_auth
- keyctl_instantiate_key_common
- keyctl_instantiate_key
- keyctl_instantiate_key_iov
- keyctl_negate_key
- keyctl_reject_key
- keyctl_set_reqkey_keyring
- keyctl_set_timeout
- keyctl_assume_authority
- keyctl_get_security
- keyctl_session_to_parent
- keyctl_restrict_keyring
- keyctl_capabilities
- SYSCALL_DEFINE5
1
2
3
4
5
6
7
8 #include <linux/init.h>
9 #include <linux/sched.h>
10 #include <linux/sched/task.h>
11 #include <linux/slab.h>
12 #include <linux/syscalls.h>
13 #include <linux/key.h>
14 #include <linux/keyctl.h>
15 #include <linux/fs.h>
16 #include <linux/capability.h>
17 #include <linux/cred.h>
18 #include <linux/string.h>
19 #include <linux/err.h>
20 #include <linux/vmalloc.h>
21 #include <linux/security.h>
22 #include <linux/uio.h>
23 #include <linux/uaccess.h>
24 #include <keys/request_key_auth-type.h>
25 #include "internal.h"
26
27 #define KEY_MAX_DESC_SIZE 4096
28
29 static const unsigned char keyrings_capabilities[2] = {
30 [0] = (KEYCTL_CAPS0_CAPABILITIES |
31 (IS_ENABLED(CONFIG_PERSISTENT_KEYRINGS) ? KEYCTL_CAPS0_PERSISTENT_KEYRINGS : 0) |
32 (IS_ENABLED(CONFIG_KEY_DH_OPERATIONS) ? KEYCTL_CAPS0_DIFFIE_HELLMAN : 0) |
33 (IS_ENABLED(CONFIG_ASYMMETRIC_KEY_TYPE) ? KEYCTL_CAPS0_PUBLIC_KEY : 0) |
34 (IS_ENABLED(CONFIG_BIG_KEYS) ? KEYCTL_CAPS0_BIG_KEY : 0) |
35 KEYCTL_CAPS0_INVALIDATE |
36 KEYCTL_CAPS0_RESTRICT_KEYRING |
37 KEYCTL_CAPS0_MOVE
38 ),
39 [1] = (KEYCTL_CAPS1_NS_KEYRING_NAME |
40 KEYCTL_CAPS1_NS_KEY_TAG),
41 };
42
43 static int key_get_type_from_user(char *type,
44 const char __user *_type,
45 unsigned len)
46 {
47 int ret;
48
49 ret = strncpy_from_user(type, _type, len);
50 if (ret < 0)
51 return ret;
52 if (ret == 0 || ret >= len)
53 return -EINVAL;
54 if (type[0] == '.')
55 return -EPERM;
56 type[len - 1] = '\0';
57 return 0;
58 }
59
60
61
62
63
64
65
66
67
68
69
70
71
72 SYSCALL_DEFINE5(add_key, const char __user *, _type,
73 const char __user *, _description,
74 const void __user *, _payload,
75 size_t, plen,
76 key_serial_t, ringid)
77 {
78 key_ref_t keyring_ref, key_ref;
79 char type[32], *description;
80 void *payload;
81 long ret;
82
83 ret = -EINVAL;
84 if (plen > 1024 * 1024 - 1)
85 goto error;
86
87
88 ret = key_get_type_from_user(type, _type, sizeof(type));
89 if (ret < 0)
90 goto error;
91
92 description = NULL;
93 if (_description) {
94 description = strndup_user(_description, KEY_MAX_DESC_SIZE);
95 if (IS_ERR(description)) {
96 ret = PTR_ERR(description);
97 goto error;
98 }
99 if (!*description) {
100 kfree(description);
101 description = NULL;
102 } else if ((description[0] == '.') &&
103 (strncmp(type, "keyring", 7) == 0)) {
104 ret = -EPERM;
105 goto error2;
106 }
107 }
108
109
110 payload = NULL;
111
112 if (plen) {
113 ret = -ENOMEM;
114 payload = kvmalloc(plen, GFP_KERNEL);
115 if (!payload)
116 goto error2;
117
118 ret = -EFAULT;
119 if (copy_from_user(payload, _payload, plen) != 0)
120 goto error3;
121 }
122
123
124 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
125 if (IS_ERR(keyring_ref)) {
126 ret = PTR_ERR(keyring_ref);
127 goto error3;
128 }
129
130
131
132 key_ref = key_create_or_update(keyring_ref, type, description,
133 payload, plen, KEY_PERM_UNDEF,
134 KEY_ALLOC_IN_QUOTA);
135 if (!IS_ERR(key_ref)) {
136 ret = key_ref_to_ptr(key_ref)->serial;
137 key_ref_put(key_ref);
138 }
139 else {
140 ret = PTR_ERR(key_ref);
141 }
142
143 key_ref_put(keyring_ref);
144 error3:
145 kvfree_sensitive(payload, plen);
146 error2:
147 kfree(description);
148 error:
149 return ret;
150 }
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 SYSCALL_DEFINE4(request_key, const char __user *, _type,
166 const char __user *, _description,
167 const char __user *, _callout_info,
168 key_serial_t, destringid)
169 {
170 struct key_type *ktype;
171 struct key *key;
172 key_ref_t dest_ref;
173 size_t callout_len;
174 char type[32], *description, *callout_info;
175 long ret;
176
177
178 ret = key_get_type_from_user(type, _type, sizeof(type));
179 if (ret < 0)
180 goto error;
181
182
183 description = strndup_user(_description, KEY_MAX_DESC_SIZE);
184 if (IS_ERR(description)) {
185 ret = PTR_ERR(description);
186 goto error;
187 }
188
189
190 callout_info = NULL;
191 callout_len = 0;
192 if (_callout_info) {
193 callout_info = strndup_user(_callout_info, PAGE_SIZE);
194 if (IS_ERR(callout_info)) {
195 ret = PTR_ERR(callout_info);
196 goto error2;
197 }
198 callout_len = strlen(callout_info);
199 }
200
201
202 dest_ref = NULL;
203 if (destringid) {
204 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
205 KEY_NEED_WRITE);
206 if (IS_ERR(dest_ref)) {
207 ret = PTR_ERR(dest_ref);
208 goto error3;
209 }
210 }
211
212
213 ktype = key_type_lookup(type);
214 if (IS_ERR(ktype)) {
215 ret = PTR_ERR(ktype);
216 goto error4;
217 }
218
219
220 key = request_key_and_link(ktype, description, NULL, callout_info,
221 callout_len, NULL, key_ref_to_ptr(dest_ref),
222 KEY_ALLOC_IN_QUOTA);
223 if (IS_ERR(key)) {
224 ret = PTR_ERR(key);
225 goto error5;
226 }
227
228
229 ret = wait_for_key_construction(key, 1);
230 if (ret < 0)
231 goto error6;
232
233 ret = key->serial;
234
235 error6:
236 key_put(key);
237 error5:
238 key_type_put(ktype);
239 error4:
240 key_ref_put(dest_ref);
241 error3:
242 kfree(callout_info);
243 error2:
244 kfree(description);
245 error:
246 return ret;
247 }
248
249
250
251
252
253
254
255
256 long keyctl_get_keyring_ID(key_serial_t id, int create)
257 {
258 key_ref_t key_ref;
259 unsigned long lflags;
260 long ret;
261
262 lflags = create ? KEY_LOOKUP_CREATE : 0;
263 key_ref = lookup_user_key(id, lflags, KEY_NEED_SEARCH);
264 if (IS_ERR(key_ref)) {
265 ret = PTR_ERR(key_ref);
266 goto error;
267 }
268
269 ret = key_ref_to_ptr(key_ref)->serial;
270 key_ref_put(key_ref);
271 error:
272 return ret;
273 }
274
275
276
277
278
279
280
281
282
283
284
285
286 long keyctl_join_session_keyring(const char __user *_name)
287 {
288 char *name;
289 long ret;
290
291
292 name = NULL;
293 if (_name) {
294 name = strndup_user(_name, KEY_MAX_DESC_SIZE);
295 if (IS_ERR(name)) {
296 ret = PTR_ERR(name);
297 goto error;
298 }
299
300 ret = -EPERM;
301 if (name[0] == '.')
302 goto error_name;
303 }
304
305
306 ret = join_session_keyring(name);
307 error_name:
308 kfree(name);
309 error:
310 return ret;
311 }
312
313
314
315
316
317
318
319
320
321
322
323 long keyctl_update_key(key_serial_t id,
324 const void __user *_payload,
325 size_t plen)
326 {
327 key_ref_t key_ref;
328 void *payload;
329 long ret;
330
331 ret = -EINVAL;
332 if (plen > PAGE_SIZE)
333 goto error;
334
335
336 payload = NULL;
337 if (plen) {
338 ret = -ENOMEM;
339 payload = kvmalloc(plen, GFP_KERNEL);
340 if (!payload)
341 goto error;
342
343 ret = -EFAULT;
344 if (copy_from_user(payload, _payload, plen) != 0)
345 goto error2;
346 }
347
348
349 key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
350 if (IS_ERR(key_ref)) {
351 ret = PTR_ERR(key_ref);
352 goto error2;
353 }
354
355
356 ret = key_update(key_ref, payload, plen);
357
358 key_ref_put(key_ref);
359 error2:
360 kvfree_sensitive(payload, plen);
361 error:
362 return ret;
363 }
364
365
366
367
368
369
370
371
372
373
374
375
376
377 long keyctl_revoke_key(key_serial_t id)
378 {
379 key_ref_t key_ref;
380 struct key *key;
381 long ret;
382
383 key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
384 if (IS_ERR(key_ref)) {
385 ret = PTR_ERR(key_ref);
386 if (ret != -EACCES)
387 goto error;
388 key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
389 if (IS_ERR(key_ref)) {
390 ret = PTR_ERR(key_ref);
391 goto error;
392 }
393 }
394
395 key = key_ref_to_ptr(key_ref);
396 ret = 0;
397 if (test_bit(KEY_FLAG_KEEP, &key->flags))
398 ret = -EPERM;
399 else
400 key_revoke(key);
401
402 key_ref_put(key_ref);
403 error:
404 return ret;
405 }
406
407
408
409
410
411
412
413
414
415
416
417
418 long keyctl_invalidate_key(key_serial_t id)
419 {
420 key_ref_t key_ref;
421 struct key *key;
422 long ret;
423
424 kenter("%d", id);
425
426 key_ref = lookup_user_key(id, 0, KEY_NEED_SEARCH);
427 if (IS_ERR(key_ref)) {
428 ret = PTR_ERR(key_ref);
429
430
431 if (capable(CAP_SYS_ADMIN)) {
432 key_ref = lookup_user_key(id, 0, 0);
433 if (IS_ERR(key_ref))
434 goto error;
435 if (test_bit(KEY_FLAG_ROOT_CAN_INVAL,
436 &key_ref_to_ptr(key_ref)->flags))
437 goto invalidate;
438 goto error_put;
439 }
440
441 goto error;
442 }
443
444 invalidate:
445 key = key_ref_to_ptr(key_ref);
446 ret = 0;
447 if (test_bit(KEY_FLAG_KEEP, &key->flags))
448 ret = -EPERM;
449 else
450 key_invalidate(key);
451 error_put:
452 key_ref_put(key_ref);
453 error:
454 kleave(" = %ld", ret);
455 return ret;
456 }
457
458
459
460
461
462
463
464
465 long keyctl_keyring_clear(key_serial_t ringid)
466 {
467 key_ref_t keyring_ref;
468 struct key *keyring;
469 long ret;
470
471 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
472 if (IS_ERR(keyring_ref)) {
473 ret = PTR_ERR(keyring_ref);
474
475
476 if (capable(CAP_SYS_ADMIN)) {
477 keyring_ref = lookup_user_key(ringid, 0, 0);
478 if (IS_ERR(keyring_ref))
479 goto error;
480 if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
481 &key_ref_to_ptr(keyring_ref)->flags))
482 goto clear;
483 goto error_put;
484 }
485
486 goto error;
487 }
488
489 clear:
490 keyring = key_ref_to_ptr(keyring_ref);
491 if (test_bit(KEY_FLAG_KEEP, &keyring->flags))
492 ret = -EPERM;
493 else
494 ret = keyring_clear(keyring);
495 error_put:
496 key_ref_put(keyring_ref);
497 error:
498 return ret;
499 }
500
501
502
503
504
505
506
507
508
509
510
511
512 long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
513 {
514 key_ref_t keyring_ref, key_ref;
515 long ret;
516
517 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
518 if (IS_ERR(keyring_ref)) {
519 ret = PTR_ERR(keyring_ref);
520 goto error;
521 }
522
523 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
524 if (IS_ERR(key_ref)) {
525 ret = PTR_ERR(key_ref);
526 goto error2;
527 }
528
529 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
530
531 key_ref_put(key_ref);
532 error2:
533 key_ref_put(keyring_ref);
534 error:
535 return ret;
536 }
537
538
539
540
541
542
543
544
545
546
547
548
549 long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
550 {
551 key_ref_t keyring_ref, key_ref;
552 struct key *keyring, *key;
553 long ret;
554
555 keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE);
556 if (IS_ERR(keyring_ref)) {
557 ret = PTR_ERR(keyring_ref);
558 goto error;
559 }
560
561 key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
562 if (IS_ERR(key_ref)) {
563 ret = PTR_ERR(key_ref);
564 goto error2;
565 }
566
567 keyring = key_ref_to_ptr(keyring_ref);
568 key = key_ref_to_ptr(key_ref);
569 if (test_bit(KEY_FLAG_KEEP, &keyring->flags) &&
570 test_bit(KEY_FLAG_KEEP, &key->flags))
571 ret = -EPERM;
572 else
573 ret = key_unlink(keyring, key);
574
575 key_ref_put(key_ref);
576 error2:
577 key_ref_put(keyring_ref);
578 error:
579 return ret;
580 }
581
582
583
584
585
586
587
588
589
590
591
592 long keyctl_keyring_move(key_serial_t id, key_serial_t from_ringid,
593 key_serial_t to_ringid, unsigned int flags)
594 {
595 key_ref_t key_ref, from_ref, to_ref;
596 long ret;
597
598 if (flags & ~KEYCTL_MOVE_EXCL)
599 return -EINVAL;
600
601 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
602 if (IS_ERR(key_ref))
603 return PTR_ERR(key_ref);
604
605 from_ref = lookup_user_key(from_ringid, 0, KEY_NEED_WRITE);
606 if (IS_ERR(from_ref)) {
607 ret = PTR_ERR(from_ref);
608 goto error2;
609 }
610
611 to_ref = lookup_user_key(to_ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
612 if (IS_ERR(to_ref)) {
613 ret = PTR_ERR(to_ref);
614 goto error3;
615 }
616
617 ret = key_move(key_ref_to_ptr(key_ref), key_ref_to_ptr(from_ref),
618 key_ref_to_ptr(to_ref), flags);
619
620 key_ref_put(to_ref);
621 error3:
622 key_ref_put(from_ref);
623 error2:
624 key_ref_put(key_ref);
625 return ret;
626 }
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641 long keyctl_describe_key(key_serial_t keyid,
642 char __user *buffer,
643 size_t buflen)
644 {
645 struct key *key, *instkey;
646 key_ref_t key_ref;
647 char *infobuf;
648 long ret;
649 int desclen, infolen;
650
651 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
652 if (IS_ERR(key_ref)) {
653
654
655 if (PTR_ERR(key_ref) == -EACCES) {
656 instkey = key_get_instantiation_authkey(keyid);
657 if (!IS_ERR(instkey)) {
658 key_put(instkey);
659 key_ref = lookup_user_key(keyid,
660 KEY_LOOKUP_PARTIAL,
661 0);
662 if (!IS_ERR(key_ref))
663 goto okay;
664 }
665 }
666
667 ret = PTR_ERR(key_ref);
668 goto error;
669 }
670
671 okay:
672 key = key_ref_to_ptr(key_ref);
673 desclen = strlen(key->description);
674
675
676 ret = -ENOMEM;
677 infobuf = kasprintf(GFP_KERNEL,
678 "%s;%d;%d;%08x;",
679 key->type->name,
680 from_kuid_munged(current_user_ns(), key->uid),
681 from_kgid_munged(current_user_ns(), key->gid),
682 key->perm);
683 if (!infobuf)
684 goto error2;
685 infolen = strlen(infobuf);
686 ret = infolen + desclen + 1;
687
688
689 if (buffer && buflen >= ret) {
690 if (copy_to_user(buffer, infobuf, infolen) != 0 ||
691 copy_to_user(buffer + infolen, key->description,
692 desclen + 1) != 0)
693 ret = -EFAULT;
694 }
695
696 kfree(infobuf);
697 error2:
698 key_ref_put(key_ref);
699 error:
700 return ret;
701 }
702
703
704
705
706
707
708
709
710
711
712
713 long keyctl_keyring_search(key_serial_t ringid,
714 const char __user *_type,
715 const char __user *_description,
716 key_serial_t destringid)
717 {
718 struct key_type *ktype;
719 key_ref_t keyring_ref, key_ref, dest_ref;
720 char type[32], *description;
721 long ret;
722
723
724 ret = key_get_type_from_user(type, _type, sizeof(type));
725 if (ret < 0)
726 goto error;
727
728 description = strndup_user(_description, KEY_MAX_DESC_SIZE);
729 if (IS_ERR(description)) {
730 ret = PTR_ERR(description);
731 goto error;
732 }
733
734
735 keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH);
736 if (IS_ERR(keyring_ref)) {
737 ret = PTR_ERR(keyring_ref);
738 goto error2;
739 }
740
741
742 dest_ref = NULL;
743 if (destringid) {
744 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
745 KEY_NEED_WRITE);
746 if (IS_ERR(dest_ref)) {
747 ret = PTR_ERR(dest_ref);
748 goto error3;
749 }
750 }
751
752
753 ktype = key_type_lookup(type);
754 if (IS_ERR(ktype)) {
755 ret = PTR_ERR(ktype);
756 goto error4;
757 }
758
759
760 key_ref = keyring_search(keyring_ref, ktype, description, true);
761 if (IS_ERR(key_ref)) {
762 ret = PTR_ERR(key_ref);
763
764
765 if (ret == -EAGAIN)
766 ret = -ENOKEY;
767 goto error5;
768 }
769
770
771 if (dest_ref) {
772 ret = key_permission(key_ref, KEY_NEED_LINK);
773 if (ret < 0)
774 goto error6;
775
776 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
777 if (ret < 0)
778 goto error6;
779 }
780
781 ret = key_ref_to_ptr(key_ref)->serial;
782
783 error6:
784 key_ref_put(key_ref);
785 error5:
786 key_type_put(ktype);
787 error4:
788 key_ref_put(dest_ref);
789 error3:
790 key_ref_put(keyring_ref);
791 error2:
792 kfree(description);
793 error:
794 return ret;
795 }
796
797
798
799
800 static long __keyctl_read_key(struct key *key, char *buffer, size_t buflen)
801 {
802 long ret;
803
804 down_read(&key->sem);
805 ret = key_validate(key);
806 if (ret == 0)
807 ret = key->type->read(key, buffer, buflen);
808 up_read(&key->sem);
809 return ret;
810 }
811
812
813
814
815
816
817
818
819
820
821
822 long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
823 {
824 struct key *key;
825 key_ref_t key_ref;
826 long ret;
827 char *key_data = NULL;
828 size_t key_data_len;
829
830
831 key_ref = lookup_user_key(keyid, 0, 0);
832 if (IS_ERR(key_ref)) {
833 ret = -ENOKEY;
834 goto out;
835 }
836
837 key = key_ref_to_ptr(key_ref);
838
839 ret = key_read_state(key);
840 if (ret < 0)
841 goto key_put_out;
842
843
844 ret = key_permission(key_ref, KEY_NEED_READ);
845 if (ret == 0)
846 goto can_read_key;
847 if (ret != -EACCES)
848 goto key_put_out;
849
850
851
852
853
854 if (!is_key_possessed(key_ref)) {
855 ret = -EACCES;
856 goto key_put_out;
857 }
858
859
860 can_read_key:
861 if (!key->type->read) {
862 ret = -EOPNOTSUPP;
863 goto key_put_out;
864 }
865
866 if (!buffer || !buflen) {
867
868 ret = __keyctl_read_key(key, NULL, 0);
869 goto key_put_out;
870 }
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887 key_data_len = (buflen <= PAGE_SIZE) ? buflen : 0;
888 for (;;) {
889 if (key_data_len) {
890 key_data = kvmalloc(key_data_len, GFP_KERNEL);
891 if (!key_data) {
892 ret = -ENOMEM;
893 goto key_put_out;
894 }
895 }
896
897 ret = __keyctl_read_key(key, key_data, key_data_len);
898
899
900
901
902
903 if (ret <= 0 || ret > buflen)
904 break;
905
906
907
908
909
910
911
912 if (ret > key_data_len) {
913 if (unlikely(key_data))
914 kvfree_sensitive(key_data, key_data_len);
915 key_data_len = ret;
916 continue;
917 }
918
919 if (copy_to_user(buffer, key_data, ret))
920 ret = -EFAULT;
921 break;
922 }
923 kvfree_sensitive(key_data, key_data_len);
924
925 key_put_out:
926 key_put(key);
927 out:
928 return ret;
929 }
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946 long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
947 {
948 struct key_user *newowner, *zapowner = NULL;
949 struct key *key;
950 key_ref_t key_ref;
951 long ret;
952 kuid_t uid;
953 kgid_t gid;
954
955 uid = make_kuid(current_user_ns(), user);
956 gid = make_kgid(current_user_ns(), group);
957 ret = -EINVAL;
958 if ((user != (uid_t) -1) && !uid_valid(uid))
959 goto error;
960 if ((group != (gid_t) -1) && !gid_valid(gid))
961 goto error;
962
963 ret = 0;
964 if (user == (uid_t) -1 && group == (gid_t) -1)
965 goto error;
966
967 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
968 KEY_NEED_SETATTR);
969 if (IS_ERR(key_ref)) {
970 ret = PTR_ERR(key_ref);
971 goto error;
972 }
973
974 key = key_ref_to_ptr(key_ref);
975
976
977 ret = -EACCES;
978 down_write(&key->sem);
979
980 if (!capable(CAP_SYS_ADMIN)) {
981
982 if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
983 goto error_put;
984
985
986
987 if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
988 goto error_put;
989 }
990
991
992 if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) {
993 ret = -ENOMEM;
994 newowner = key_user_lookup(uid);
995 if (!newowner)
996 goto error_put;
997
998
999 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
1000 unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
1001 key_quota_root_maxkeys : key_quota_maxkeys;
1002 unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
1003 key_quota_root_maxbytes : key_quota_maxbytes;
1004
1005 spin_lock(&newowner->lock);
1006 if (newowner->qnkeys + 1 > maxkeys ||
1007 newowner->qnbytes + key->quotalen > maxbytes ||
1008 newowner->qnbytes + key->quotalen <
1009 newowner->qnbytes)
1010 goto quota_overrun;
1011
1012 newowner->qnkeys++;
1013 newowner->qnbytes += key->quotalen;
1014 spin_unlock(&newowner->lock);
1015
1016 spin_lock(&key->user->lock);
1017 key->user->qnkeys--;
1018 key->user->qnbytes -= key->quotalen;
1019 spin_unlock(&key->user->lock);
1020 }
1021
1022 atomic_dec(&key->user->nkeys);
1023 atomic_inc(&newowner->nkeys);
1024
1025 if (key->state != KEY_IS_UNINSTANTIATED) {
1026 atomic_dec(&key->user->nikeys);
1027 atomic_inc(&newowner->nikeys);
1028 }
1029
1030 zapowner = key->user;
1031 key->user = newowner;
1032 key->uid = uid;
1033 }
1034
1035
1036 if (group != (gid_t) -1)
1037 key->gid = gid;
1038
1039 ret = 0;
1040
1041 error_put:
1042 up_write(&key->sem);
1043 key_put(key);
1044 if (zapowner)
1045 key_user_put(zapowner);
1046 error:
1047 return ret;
1048
1049 quota_overrun:
1050 spin_unlock(&newowner->lock);
1051 zapowner = newowner;
1052 ret = -EDQUOT;
1053 goto error_put;
1054 }
1055
1056
1057
1058
1059
1060
1061
1062
1063 long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
1064 {
1065 struct key *key;
1066 key_ref_t key_ref;
1067 long ret;
1068
1069 ret = -EINVAL;
1070 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
1071 goto error;
1072
1073 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1074 KEY_NEED_SETATTR);
1075 if (IS_ERR(key_ref)) {
1076 ret = PTR_ERR(key_ref);
1077 goto error;
1078 }
1079
1080 key = key_ref_to_ptr(key_ref);
1081
1082
1083 ret = -EACCES;
1084 down_write(&key->sem);
1085
1086
1087 if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
1088 key->perm = perm;
1089 ret = 0;
1090 }
1091
1092 up_write(&key->sem);
1093 key_put(key);
1094 error:
1095 return ret;
1096 }
1097
1098
1099
1100
1101
1102 static long get_instantiation_keyring(key_serial_t ringid,
1103 struct request_key_auth *rka,
1104 struct key **_dest_keyring)
1105 {
1106 key_ref_t dkref;
1107
1108 *_dest_keyring = NULL;
1109
1110
1111 if (ringid == 0)
1112 return 0;
1113
1114
1115 if (ringid > 0) {
1116 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
1117 if (IS_ERR(dkref))
1118 return PTR_ERR(dkref);
1119 *_dest_keyring = key_ref_to_ptr(dkref);
1120 return 0;
1121 }
1122
1123 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
1124 return -EINVAL;
1125
1126
1127
1128 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
1129 *_dest_keyring = key_get(rka->dest_keyring);
1130 return 0;
1131 }
1132
1133 return -ENOKEY;
1134 }
1135
1136
1137
1138
1139 static int keyctl_change_reqkey_auth(struct key *key)
1140 {
1141 struct cred *new;
1142
1143 new = prepare_creds();
1144 if (!new)
1145 return -ENOMEM;
1146
1147 key_put(new->request_key_auth);
1148 new->request_key_auth = key_get(key);
1149
1150 return commit_creds(new);
1151 }
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162 long keyctl_instantiate_key_common(key_serial_t id,
1163 struct iov_iter *from,
1164 key_serial_t ringid)
1165 {
1166 const struct cred *cred = current_cred();
1167 struct request_key_auth *rka;
1168 struct key *instkey, *dest_keyring;
1169 size_t plen = from ? iov_iter_count(from) : 0;
1170 void *payload;
1171 long ret;
1172
1173 kenter("%d,,%zu,%d", id, plen, ringid);
1174
1175 if (!plen)
1176 from = NULL;
1177
1178 ret = -EINVAL;
1179 if (plen > 1024 * 1024 - 1)
1180 goto error;
1181
1182
1183
1184 ret = -EPERM;
1185 instkey = cred->request_key_auth;
1186 if (!instkey)
1187 goto error;
1188
1189 rka = instkey->payload.data[0];
1190 if (rka->target_key->serial != id)
1191 goto error;
1192
1193
1194 payload = NULL;
1195
1196 if (from) {
1197 ret = -ENOMEM;
1198 payload = kvmalloc(plen, GFP_KERNEL);
1199 if (!payload)
1200 goto error;
1201
1202 ret = -EFAULT;
1203 if (!copy_from_iter_full(payload, plen, from))
1204 goto error2;
1205 }
1206
1207
1208
1209 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1210 if (ret < 0)
1211 goto error2;
1212
1213
1214 ret = key_instantiate_and_link(rka->target_key, payload, plen,
1215 dest_keyring, instkey);
1216
1217 key_put(dest_keyring);
1218
1219
1220
1221 if (ret == 0)
1222 keyctl_change_reqkey_auth(NULL);
1223
1224 error2:
1225 kvfree_sensitive(payload, plen);
1226 error:
1227 return ret;
1228 }
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239 long keyctl_instantiate_key(key_serial_t id,
1240 const void __user *_payload,
1241 size_t plen,
1242 key_serial_t ringid)
1243 {
1244 if (_payload && plen) {
1245 struct iovec iov;
1246 struct iov_iter from;
1247 int ret;
1248
1249 ret = import_single_range(WRITE, (void __user *)_payload, plen,
1250 &iov, &from);
1251 if (unlikely(ret))
1252 return ret;
1253
1254 return keyctl_instantiate_key_common(id, &from, ringid);
1255 }
1256
1257 return keyctl_instantiate_key_common(id, NULL, ringid);
1258 }
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269 long keyctl_instantiate_key_iov(key_serial_t id,
1270 const struct iovec __user *_payload_iov,
1271 unsigned ioc,
1272 key_serial_t ringid)
1273 {
1274 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1275 struct iov_iter from;
1276 long ret;
1277
1278 if (!_payload_iov)
1279 ioc = 0;
1280
1281 ret = import_iovec(WRITE, _payload_iov, ioc,
1282 ARRAY_SIZE(iovstack), &iov, &from);
1283 if (ret < 0)
1284 return ret;
1285 ret = keyctl_instantiate_key_common(id, &from, ringid);
1286 kfree(iov);
1287 return ret;
1288 }
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305 long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
1306 {
1307 return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1308 }
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325 long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1326 key_serial_t ringid)
1327 {
1328 const struct cred *cred = current_cred();
1329 struct request_key_auth *rka;
1330 struct key *instkey, *dest_keyring;
1331 long ret;
1332
1333 kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1334
1335
1336 if (error <= 0 ||
1337 error >= MAX_ERRNO ||
1338 error == ERESTARTSYS ||
1339 error == ERESTARTNOINTR ||
1340 error == ERESTARTNOHAND ||
1341 error == ERESTART_RESTARTBLOCK)
1342 return -EINVAL;
1343
1344
1345
1346 ret = -EPERM;
1347 instkey = cred->request_key_auth;
1348 if (!instkey)
1349 goto error;
1350
1351 rka = instkey->payload.data[0];
1352 if (rka->target_key->serial != id)
1353 goto error;
1354
1355
1356
1357 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1358 if (ret < 0)
1359 goto error;
1360
1361
1362 ret = key_reject_and_link(rka->target_key, timeout, error,
1363 dest_keyring, instkey);
1364
1365 key_put(dest_keyring);
1366
1367
1368
1369 if (ret == 0)
1370 keyctl_change_reqkey_auth(NULL);
1371
1372 error:
1373 return ret;
1374 }
1375
1376
1377
1378
1379
1380
1381
1382
1383 long keyctl_set_reqkey_keyring(int reqkey_defl)
1384 {
1385 struct cred *new;
1386 int ret, old_setting;
1387
1388 old_setting = current_cred_xxx(jit_keyring);
1389
1390 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1391 return old_setting;
1392
1393 new = prepare_creds();
1394 if (!new)
1395 return -ENOMEM;
1396
1397 switch (reqkey_defl) {
1398 case KEY_REQKEY_DEFL_THREAD_KEYRING:
1399 ret = install_thread_keyring_to_cred(new);
1400 if (ret < 0)
1401 goto error;
1402 goto set;
1403
1404 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
1405 ret = install_process_keyring_to_cred(new);
1406 if (ret < 0)
1407 goto error;
1408 goto set;
1409
1410 case KEY_REQKEY_DEFL_DEFAULT:
1411 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1412 case KEY_REQKEY_DEFL_USER_KEYRING:
1413 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1414 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1415 goto set;
1416
1417 case KEY_REQKEY_DEFL_NO_CHANGE:
1418 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1419 default:
1420 ret = -EINVAL;
1421 goto error;
1422 }
1423
1424 set:
1425 new->jit_keyring = reqkey_defl;
1426 commit_creds(new);
1427 return old_setting;
1428 error:
1429 abort_creds(new);
1430 return ret;
1431 }
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447 long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1448 {
1449 struct key *key, *instkey;
1450 key_ref_t key_ref;
1451 long ret;
1452
1453 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1454 KEY_NEED_SETATTR);
1455 if (IS_ERR(key_ref)) {
1456
1457
1458 if (PTR_ERR(key_ref) == -EACCES) {
1459 instkey = key_get_instantiation_authkey(id);
1460 if (!IS_ERR(instkey)) {
1461 key_put(instkey);
1462 key_ref = lookup_user_key(id,
1463 KEY_LOOKUP_PARTIAL,
1464 0);
1465 if (!IS_ERR(key_ref))
1466 goto okay;
1467 }
1468 }
1469
1470 ret = PTR_ERR(key_ref);
1471 goto error;
1472 }
1473
1474 okay:
1475 key = key_ref_to_ptr(key_ref);
1476 ret = 0;
1477 if (test_bit(KEY_FLAG_KEEP, &key->flags))
1478 ret = -EPERM;
1479 else
1480 key_set_timeout(key, timeout);
1481 key_put(key);
1482
1483 error:
1484 return ret;
1485 }
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504 long keyctl_assume_authority(key_serial_t id)
1505 {
1506 struct key *authkey;
1507 long ret;
1508
1509
1510 ret = -EINVAL;
1511 if (id < 0)
1512 goto error;
1513
1514
1515 if (id == 0) {
1516 ret = keyctl_change_reqkey_auth(NULL);
1517 goto error;
1518 }
1519
1520
1521
1522
1523
1524
1525 authkey = key_get_instantiation_authkey(id);
1526 if (IS_ERR(authkey)) {
1527 ret = PTR_ERR(authkey);
1528 goto error;
1529 }
1530
1531 ret = keyctl_change_reqkey_auth(authkey);
1532 if (ret == 0)
1533 ret = authkey->serial;
1534 key_put(authkey);
1535 error:
1536 return ret;
1537 }
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549 long keyctl_get_security(key_serial_t keyid,
1550 char __user *buffer,
1551 size_t buflen)
1552 {
1553 struct key *key, *instkey;
1554 key_ref_t key_ref;
1555 char *context;
1556 long ret;
1557
1558 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
1559 if (IS_ERR(key_ref)) {
1560 if (PTR_ERR(key_ref) != -EACCES)
1561 return PTR_ERR(key_ref);
1562
1563
1564
1565 instkey = key_get_instantiation_authkey(keyid);
1566 if (IS_ERR(instkey))
1567 return PTR_ERR(instkey);
1568 key_put(instkey);
1569
1570 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
1571 if (IS_ERR(key_ref))
1572 return PTR_ERR(key_ref);
1573 }
1574
1575 key = key_ref_to_ptr(key_ref);
1576 ret = security_key_getsecurity(key, &context);
1577 if (ret == 0) {
1578
1579
1580 ret = 1;
1581 if (buffer && buflen > 0 &&
1582 copy_to_user(buffer, "", 1) != 0)
1583 ret = -EFAULT;
1584 } else if (ret > 0) {
1585
1586 if (buffer && buflen > 0) {
1587 if (buflen > ret)
1588 buflen = ret;
1589
1590 if (copy_to_user(buffer, context, buflen) != 0)
1591 ret = -EFAULT;
1592 }
1593
1594 kfree(context);
1595 }
1596
1597 key_ref_put(key_ref);
1598 return ret;
1599 }
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613 long keyctl_session_to_parent(void)
1614 {
1615 struct task_struct *me, *parent;
1616 const struct cred *mycred, *pcred;
1617 struct callback_head *newwork, *oldwork;
1618 key_ref_t keyring_r;
1619 struct cred *cred;
1620 int ret;
1621
1622 keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK);
1623 if (IS_ERR(keyring_r))
1624 return PTR_ERR(keyring_r);
1625
1626 ret = -ENOMEM;
1627
1628
1629
1630
1631 cred = cred_alloc_blank();
1632 if (!cred)
1633 goto error_keyring;
1634 newwork = &cred->rcu;
1635
1636 cred->session_keyring = key_ref_to_ptr(keyring_r);
1637 keyring_r = NULL;
1638 init_task_work(newwork, key_change_session_keyring);
1639
1640 me = current;
1641 rcu_read_lock();
1642 write_lock_irq(&tasklist_lock);
1643
1644 ret = -EPERM;
1645 oldwork = NULL;
1646 parent = rcu_dereference_protected(me->real_parent,
1647 lockdep_is_held(&tasklist_lock));
1648
1649
1650 if (parent->pid <= 1 || !parent->mm)
1651 goto unlock;
1652
1653
1654 if (!thread_group_empty(parent))
1655 goto unlock;
1656
1657
1658
1659 mycred = current_cred();
1660 pcred = __task_cred(parent);
1661 if (mycred == pcred ||
1662 mycred->session_keyring == pcred->session_keyring) {
1663 ret = 0;
1664 goto unlock;
1665 }
1666
1667
1668
1669 if (!uid_eq(pcred->uid, mycred->euid) ||
1670 !uid_eq(pcred->euid, mycred->euid) ||
1671 !uid_eq(pcred->suid, mycred->euid) ||
1672 !gid_eq(pcred->gid, mycred->egid) ||
1673 !gid_eq(pcred->egid, mycred->egid) ||
1674 !gid_eq(pcred->sgid, mycred->egid))
1675 goto unlock;
1676
1677
1678 if ((pcred->session_keyring &&
1679 !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
1680 !uid_eq(mycred->session_keyring->uid, mycred->euid))
1681 goto unlock;
1682
1683
1684 oldwork = task_work_cancel(parent, key_change_session_keyring);
1685
1686
1687
1688 ret = task_work_add(parent, newwork, true);
1689 if (!ret)
1690 newwork = NULL;
1691 unlock:
1692 write_unlock_irq(&tasklist_lock);
1693 rcu_read_unlock();
1694 if (oldwork)
1695 put_cred(container_of(oldwork, struct cred, rcu));
1696 if (newwork)
1697 put_cred(cred);
1698 return ret;
1699
1700 error_keyring:
1701 key_ref_put(keyring_r);
1702 return ret;
1703 }
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716 long keyctl_restrict_keyring(key_serial_t id, const char __user *_type,
1717 const char __user *_restriction)
1718 {
1719 key_ref_t key_ref;
1720 char type[32];
1721 char *restriction = NULL;
1722 long ret;
1723
1724 key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
1725 if (IS_ERR(key_ref))
1726 return PTR_ERR(key_ref);
1727
1728 ret = -EINVAL;
1729 if (_type) {
1730 if (!_restriction)
1731 goto error;
1732
1733 ret = key_get_type_from_user(type, _type, sizeof(type));
1734 if (ret < 0)
1735 goto error;
1736
1737 restriction = strndup_user(_restriction, PAGE_SIZE);
1738 if (IS_ERR(restriction)) {
1739 ret = PTR_ERR(restriction);
1740 goto error;
1741 }
1742 } else {
1743 if (_restriction)
1744 goto error;
1745 }
1746
1747 ret = keyring_restrict(key_ref, _type ? type : NULL, restriction);
1748 kfree(restriction);
1749 error:
1750 key_ref_put(key_ref);
1751 return ret;
1752 }
1753
1754
1755
1756
1757 long keyctl_capabilities(unsigned char __user *_buffer, size_t buflen)
1758 {
1759 size_t size = buflen;
1760
1761 if (size > 0) {
1762 if (size > sizeof(keyrings_capabilities))
1763 size = sizeof(keyrings_capabilities);
1764 if (copy_to_user(_buffer, keyrings_capabilities, size) != 0)
1765 return -EFAULT;
1766 if (size < buflen &&
1767 clear_user(_buffer + size, buflen - size) != 0)
1768 return -EFAULT;
1769 }
1770
1771 return sizeof(keyrings_capabilities);
1772 }
1773
1774
1775
1776
1777 SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1778 unsigned long, arg4, unsigned long, arg5)
1779 {
1780 switch (option) {
1781 case KEYCTL_GET_KEYRING_ID:
1782 return keyctl_get_keyring_ID((key_serial_t) arg2,
1783 (int) arg3);
1784
1785 case KEYCTL_JOIN_SESSION_KEYRING:
1786 return keyctl_join_session_keyring((const char __user *) arg2);
1787
1788 case KEYCTL_UPDATE:
1789 return keyctl_update_key((key_serial_t) arg2,
1790 (const void __user *) arg3,
1791 (size_t) arg4);
1792
1793 case KEYCTL_REVOKE:
1794 return keyctl_revoke_key((key_serial_t) arg2);
1795
1796 case KEYCTL_DESCRIBE:
1797 return keyctl_describe_key((key_serial_t) arg2,
1798 (char __user *) arg3,
1799 (unsigned) arg4);
1800
1801 case KEYCTL_CLEAR:
1802 return keyctl_keyring_clear((key_serial_t) arg2);
1803
1804 case KEYCTL_LINK:
1805 return keyctl_keyring_link((key_serial_t) arg2,
1806 (key_serial_t) arg3);
1807
1808 case KEYCTL_UNLINK:
1809 return keyctl_keyring_unlink((key_serial_t) arg2,
1810 (key_serial_t) arg3);
1811
1812 case KEYCTL_SEARCH:
1813 return keyctl_keyring_search((key_serial_t) arg2,
1814 (const char __user *) arg3,
1815 (const char __user *) arg4,
1816 (key_serial_t) arg5);
1817
1818 case KEYCTL_READ:
1819 return keyctl_read_key((key_serial_t) arg2,
1820 (char __user *) arg3,
1821 (size_t) arg4);
1822
1823 case KEYCTL_CHOWN:
1824 return keyctl_chown_key((key_serial_t) arg2,
1825 (uid_t) arg3,
1826 (gid_t) arg4);
1827
1828 case KEYCTL_SETPERM:
1829 return keyctl_setperm_key((key_serial_t) arg2,
1830 (key_perm_t) arg3);
1831
1832 case KEYCTL_INSTANTIATE:
1833 return keyctl_instantiate_key((key_serial_t) arg2,
1834 (const void __user *) arg3,
1835 (size_t) arg4,
1836 (key_serial_t) arg5);
1837
1838 case KEYCTL_NEGATE:
1839 return keyctl_negate_key((key_serial_t) arg2,
1840 (unsigned) arg3,
1841 (key_serial_t) arg4);
1842
1843 case KEYCTL_SET_REQKEY_KEYRING:
1844 return keyctl_set_reqkey_keyring(arg2);
1845
1846 case KEYCTL_SET_TIMEOUT:
1847 return keyctl_set_timeout((key_serial_t) arg2,
1848 (unsigned) arg3);
1849
1850 case KEYCTL_ASSUME_AUTHORITY:
1851 return keyctl_assume_authority((key_serial_t) arg2);
1852
1853 case KEYCTL_GET_SECURITY:
1854 return keyctl_get_security((key_serial_t) arg2,
1855 (char __user *) arg3,
1856 (size_t) arg4);
1857
1858 case KEYCTL_SESSION_TO_PARENT:
1859 return keyctl_session_to_parent();
1860
1861 case KEYCTL_REJECT:
1862 return keyctl_reject_key((key_serial_t) arg2,
1863 (unsigned) arg3,
1864 (unsigned) arg4,
1865 (key_serial_t) arg5);
1866
1867 case KEYCTL_INSTANTIATE_IOV:
1868 return keyctl_instantiate_key_iov(
1869 (key_serial_t) arg2,
1870 (const struct iovec __user *) arg3,
1871 (unsigned) arg4,
1872 (key_serial_t) arg5);
1873
1874 case KEYCTL_INVALIDATE:
1875 return keyctl_invalidate_key((key_serial_t) arg2);
1876
1877 case KEYCTL_GET_PERSISTENT:
1878 return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3);
1879
1880 case KEYCTL_DH_COMPUTE:
1881 return keyctl_dh_compute((struct keyctl_dh_params __user *) arg2,
1882 (char __user *) arg3, (size_t) arg4,
1883 (struct keyctl_kdf_params __user *) arg5);
1884
1885 case KEYCTL_RESTRICT_KEYRING:
1886 return keyctl_restrict_keyring((key_serial_t) arg2,
1887 (const char __user *) arg3,
1888 (const char __user *) arg4);
1889
1890 case KEYCTL_PKEY_QUERY:
1891 if (arg3 != 0)
1892 return -EINVAL;
1893 return keyctl_pkey_query((key_serial_t)arg2,
1894 (const char __user *)arg4,
1895 (struct keyctl_pkey_query __user *)arg5);
1896
1897 case KEYCTL_PKEY_ENCRYPT:
1898 case KEYCTL_PKEY_DECRYPT:
1899 case KEYCTL_PKEY_SIGN:
1900 return keyctl_pkey_e_d_s(
1901 option,
1902 (const struct keyctl_pkey_params __user *)arg2,
1903 (const char __user *)arg3,
1904 (const void __user *)arg4,
1905 (void __user *)arg5);
1906
1907 case KEYCTL_PKEY_VERIFY:
1908 return keyctl_pkey_verify(
1909 (const struct keyctl_pkey_params __user *)arg2,
1910 (const char __user *)arg3,
1911 (const void __user *)arg4,
1912 (const void __user *)arg5);
1913
1914 case KEYCTL_MOVE:
1915 return keyctl_keyring_move((key_serial_t)arg2,
1916 (key_serial_t)arg3,
1917 (key_serial_t)arg4,
1918 (unsigned int)arg5);
1919
1920 case KEYCTL_CAPABILITIES:
1921 return keyctl_capabilities((unsigned char __user *)arg2, (size_t)arg3);
1922
1923 default:
1924 return -EOPNOTSUPP;
1925 }
1926 }