]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/keyctl.2
ioctl_console.2, ioctl_getfsmap.2, ioctl_iflags.2, ioctl_list.2, ioctl_ns.2, kcmp...
[thirdparty/man-pages.git] / man2 / keyctl.2
1 .\" Copyright (C) 2016 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (C) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
3 .\" A very few fragments remain from an earlier version of this page
4 .\" written by David Howells (dhowells@redhat.com)
5 .\"
6 .\" %%%LICENSE_START(VERBATIM)
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
10 .\"
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
15 .\"
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date. The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein. The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
22 .\" professionally.
23 .\"
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" %%%LICENSE_END
27 .\"
28 .TH KEYCTL 2 2017-03-13 Linux "Linux Key Management Calls"
29 .SH NAME
30 keyctl \- manipulate the kernel's key management facility
31 .SH SYNOPSIS
32 .nf
33 .B #include <sys/types.h>
34 .B #include <keyutils.h>
35 .PP
36 .BI "long keyctl(int " operation ", ...)"
37
38 .B "/* For direct call via syscall(2): */"
39 .B #include <asm/unistd.h>
40 .B #include <linux/keyctl.h>
41 .B #include <unistd.h>
42 .PP
43 .BI "long syscall(__NR_keyctl, int " operation ", __kernel_ulong_t " arg2 ,
44 .BI " __kernel_ulong_t " arg3 ", __kernel_ulong_t " arg4 ,
45 .BI " __kernel_ulong_t " arg5 );
46 .fi
47 .PP
48 No glibc wrapper is provided for this system call; see NOTES.
49 .SH DESCRIPTION
50 .BR keyctl ()
51 allows user-space programs to perform key manipulation.
52 .PP
53 The operation performed by
54 .BR keyctl ()
55 is determined by the value of the
56 .I operation
57 argument.
58 Each of these operations is wrapped by the
59 .I libkeyutils
60 library (provided by the
61 .I keyutils
62 package) into individual functions (noted below)
63 to permit the compiler to check types.
64 .PP
65 The permitted values for
66 .I operation
67 are:
68 .TP
69 .BR KEYCTL_GET_KEYRING_ID " (since Linux 2.6.10)"
70 Map a special key ID to a real key ID for this process.
71 .IP
72 This operation looks up the special key whose ID is provided in
73 .I arg2
74 (cast to
75 .IR key_serial_t ).
76 If the special key is found,
77 the ID of the corresponding real key is returned as the function result.
78 The following values may be specified in
79 .IR arg2 :
80 .RS
81 .TP
82 .B KEY_SPEC_THREAD_KEYRING
83 This specifies the calling thread's thread-specific keyring.
84 See
85 .BR thread-keyring (7).
86 .TP
87 .B KEY_SPEC_PROCESS_KEYRING
88 This specifies the caller's process-specific keyring.
89 See
90 .BR process-keyring (7).
91 .TP
92 .B KEY_SPEC_SESSION_KEYRING
93 This specifies the caller's session-specific keyring.
94 See
95 .BR session-keyring (7).
96 .TP
97 .B KEY_SPEC_USER_KEYRING
98 This specifies the caller's UID-specific keyring.
99 See
100 .BR user-keyring (7).
101 .TP
102 .B KEY_SPEC_USER_SESSION_KEYRING
103 This specifies the caller's UID-session keyring.
104 See
105 .BR user-session-keyring (7).
106 .TP
107 .BR KEY_SPEC_REQKEY_AUTH_KEY " (since Linux 2.6.16)"
108 .\" commit b5f545c880a2a47947ba2118b2509644ab7a2969
109 This specifies the authorization key created by
110 .BR request_key (2)
111 and passed to the process it spawns to generate a key.
112 This key is available only in a
113 .BR request-key (8)-style
114 program that was passed an authorization key by the kernel and
115 ceases to be available once the requested key has been instantiated; see
116 .BR request_key (2).
117 .TP
118 .BR KEY_SPEC_REQUESTOR_KEYRING " (since Linux 2.6.29)"
119 .\" commit 8bbf4976b59fc9fc2861e79cab7beb3f6d647640
120 This specifies the key ID for the
121 .BR request_key (2)
122 destination keyring.
123 This keyring is available only in a
124 .BR request-key (8)-style
125 program that was passed an authorization key by the kernel and
126 ceases to be available once the requested key has been instantiated; see
127 .BR request_key (2).
128 .RE
129 .IP
130 The behavior if the key specified in
131 .I arg2
132 does not exist depends on the value of
133 .I arg3
134 (cast to
135 .IR int ).
136 If
137 .I arg3
138 contains a non-zero value, then\(emif it is appropriate to do so
139 (e.g., when looking up the user, user-session, or session key)\(ema new key
140 is created and its real key ID returned as the function result.
141 .\" The keyctl_get_keyring_ID.3 page says that a new key
142 .\" "will be created *if it is appropriate to do so**. What is the
143 .\" determiner for appropriate?
144 .\" David Howells: Some special keys such as KEY_SPEC_REQKEY_AUTH_KEY
145 .\" wouldn't get created but user/user-session/session keyring would
146 .\" be created.
147 Otherwise, the operation fails with the error
148 .BR ENOKEY .
149 .IP
150 If a valid key ID is specified in
151 .IR arg2 ,
152 and the key exists, then this operation simply returns the key ID.
153 If the key does not exist, the call fails with error
154 .BR ENOKEY .
155 .IP
156 The caller must have
157 .I search
158 permission on a keyring in order for it to be found.
159 .IP
160 The arguments
161 .IR arg4
162 and
163 .IR arg5
164 are ignored.
165 .IP
166 This operation is exposed by
167 .I libkeyutils
168 via the function
169 .BR keyctl_get_keyring_ID (3).
170 .TP
171 .BR KEYCTL_JOIN_SESSION_KEYRING " (since Linux 2.6.10)"
172 Replace the session keyring this process subscribes to with
173 a new session keyring.
174 .\" This may be useful in conjunction with some sort of
175 .\" session management framework that is employed by the application.
176 .IP
177 If
178 .I arg2
179 is NULL,
180 an anonymous keyring with the description "_ses" is created
181 and the process is subscribed to that keyring as its session keyring,
182 displacing the previous session keyring.
183 .IP
184 Otherwise,
185 .I arg2
186 (cast to
187 .IR "char\ *" )
188 is treated as the description (name) of a keyring,
189 and the behavior is as follows:
190 .RS
191 .IP * 3
192 If a keyring with a matching description exists,
193 the process will attempt to subscribe to that keyring
194 as its session keyring if possible;
195 if that is not possible, an error is returned.
196 In order to subscribe to the keyring,
197 the caller must have
198 .I search
199 permission on the keyring.
200 .IP *
201 If a keyring with a matching description does not exist,
202 then a new keyring with the specified description is created,
203 and the process is subscribed to that keyring as its session keyring.
204 .RE
205 .IP
206 The arguments
207 .IR arg3 ,
208 .IR arg4 ,
209 and
210 .IR arg5
211 are ignored.
212 .IP
213 This operation is exposed by
214 .I libkeyutils
215 via the function
216 .BR keyctl_join_session_keyring (3).
217 .TP
218 .BR KEYCTL_UPDATE " (since Linux 2.6.10)"
219 Update a key's data payload.
220 .IP
221 The
222 .I arg2
223 argument (cast to
224 .IR key_serial_t )
225 specifies the ID of the key to be updated.
226 The
227 .I arg3
228 argument (cast to
229 .IR "void\ *" )
230 points to the new payload and
231 .I arg4
232 (cast to
233 .IR size_t )
234 contains the new payload size in bytes.
235 .IP
236 The caller must have
237 .I write
238 permission on the key specified and the key type must support updating.
239 .IP
240 A negatively instantiated key (see the description of
241 .BR KEYCTL_REJECT )
242 can be positively instantiated with this operation.
243 .IP
244 The
245 .I arg5
246 argument is ignored.
247 .IP
248 This operation is exposed by
249 .I libkeyutils
250 via the function
251 .BR keyctl_update (3).
252 .TP
253 .BR KEYCTL_REVOKE " (since Linux 2.6.10)"
254 Revoke the key with the ID provided in
255 .I arg2
256 (cast to
257 .IR key_serial_t ).
258 The key is scheduled for garbage collection;
259 it will no longer be findable,
260 and will be unavailable for further operations.
261 Further attempts to use the key will fail with the error
262 .BR EKEYREVOKED .
263 .IP
264 The caller must have
265 .IR write
266 or
267 .IR setattr
268 permission on the key.
269 .\" Keys with the KEY_FLAG_KEEP bit set cause an EPERM
270 .\" error for KEYCTL_REVOKE. Does this need to be documented?
271 .\" David Howells: No significance for user space.
272 .IP
273 The arguments
274 .IR arg3 ,
275 .IR arg4 ,
276 and
277 .IR arg5
278 are ignored.
279 .IP
280 This operation is exposed by
281 .I libkeyutils
282 via the function
283 .BR keyctl_revoke (3).
284 .TP
285 .BR KEYCTL_CHOWN " (since Linux 2.6.10)"
286 Change the ownership (user and group ID) of a key.
287 .IP
288 The
289 .I arg2
290 argument (cast to
291 .IR key_serial_t )
292 contains the key ID.
293 The
294 .I arg3
295 argument (cast to
296 .IR uid_t )
297 contains the new user ID (or \-1 in case the user ID shouldn't be changed).
298 The
299 .I arg4
300 argument (cast to
301 .IR gid_t )
302 contains the new group ID (or \-1 in case the group ID shouldn't be changed).
303 .IP
304 The key must grant the caller
305 .I setattr
306 permission.
307 .IP
308 For the UID to be changed, or for the GID to be changed to a group
309 the caller is not a member of, the caller must have the
310 .B CAP_SYS_ADMIN
311 capability (see
312 .BR capabilities (7)).
313 .IP
314 If the UID is to be changed, the new user must have sufficient
315 quota to accept the key.
316 The quota deduction will be removed from the old user
317 to the new user should the UID be changed.
318 .IP
319 The
320 .I arg5
321 argument is ignored.
322 .IP
323 This operation is exposed by
324 .I libkeyutils
325 via the function
326 .BR keyctl_chown (3).
327 .TP
328 .BR KEYCTL_SETPERM " (since Linux 2.6.10)"
329 Change the permissions of the key with the ID provided in the
330 .I arg2
331 argument (cast to
332 .IR key_serial_t )
333 to the permissions provided in the
334 .I arg3
335 argument (cast to
336 .IR key_perm_t ).
337 .IP
338 If the caller doesn't have the
339 .B CAP_SYS_ADMIN
340 capability, it can change permissions only for the keys it owns.
341 (More precisely: the caller's filesystem UID must match the UID of the key.)
342 .IP
343 The key must grant
344 .I setattr
345 permission to the caller
346 .IR regardless
347 of the caller's capabilities.
348 .\" FIXME Above, is it really intended that a privileged process can't
349 .\" override the lack of the 'setattr' permission?
350 .IP
351 The permissions in
352 .IR arg3
353 specify masks of available operations
354 for each of the following user categories:
355 .RS
356 .TP
357 .IR possessor " (since Linux 2.6.14)"
358 .\" commit 664cceb0093b755739e56572b836a99104ee8a75
359 This is the permission granted to a process that possesses the key
360 (has it attached searchably to one of the process's keyrings);
361 see
362 .BR keyrings (7).
363 .TP
364 .IR user
365 This is the permission granted to a process
366 whose filesystem UID matches the UID of the key.
367 .TP
368 .IR group
369 This is the permission granted to a process
370 whose filesystem GID or any of its supplementary GIDs
371 matches the GID of the key.
372 .TP
373 .IR other
374 This is the permission granted to other processes
375 that do not match the
376 .IR user
377 and
378 .IR group
379 categories.
380 .RE
381 .IP
382 The
383 .IR user ,
384 .IR group ,
385 and
386 .IR other
387 categories are exclusive: if a process matches the
388 .IR user
389 category, it will not receive permissions granted in the
390 .IR group
391 category; if a process matches the
392 .I user
393 or
394 .IR group
395 category, then it will not receive permissions granted in the
396 .IR other
397 category.
398 .IP
399 The
400 .I possessor
401 category grants permissions that are cumulative with the grants from the
402 .IR user ,
403 .IR group ,
404 or
405 .IR other
406 category.
407 .IP
408 Each permission mask is eight bits in size,
409 with only six bits currently used.
410 The available permissions are:
411 .RS
412 .TP
413 .IR view
414 This permission allows reading attributes of a key.
415 .IP
416 This permission is required for the
417 .BR KEYCTL_DESCRIBE
418 operation.
419 .IP
420 The permission bits for each category are
421 .BR KEY_POS_VIEW ,
422 .BR KEY_USR_VIEW ,
423 .BR KEY_GRP_VIEW ,
424 and
425 .BR KEY_OTH_VIEW .
426 .TP
427 .IR read
428 This permission allows reading a key's payload.
429 .IP
430 This permission is required for the
431 .BR KEYCTL_READ
432 operation.
433 .IP
434 The permission bits for each category are
435 .BR KEY_POS_READ ,
436 .BR KEY_USR_READ ,
437 .BR KEY_GRP_READ ,
438 and
439 .BR KEY_OTH_READ .
440 .TP
441 .IR write
442 This permission allows update or instantiation of a key's payload.
443 For a keyring, it allows keys to be linked and unlinked from the keyring,
444 .IP
445 This permission is required for the
446 .BR KEYCTL_UPDATE ,
447 .BR KEYCTL_REVOKE ,
448 .BR KEYCTL_CLEAR ,
449 .BR KEYCTL_LINK ,
450 and
451 .BR KEYCTL_UNLINK
452 operations.
453 .IP
454 The permission bits for each category are
455 .BR KEY_POS_WRITE ,
456 .BR KEY_USR_WRITE ,
457 .BR KEY_GRP_WRITE ,
458 and
459 .BR KEY_OTH_WRITE .
460 .TP
461 .IR search
462 This permission allows keyrings to be searched and keys to be found.
463 Searches can recurse only into nested keyrings that have
464 .I search
465 permission set.
466 .IP
467 This permission is required for the
468 .BR KEYCTL_GET_KEYRING_ID ,
469 .BR KEYCTL_JOIN_SESSION_KEYRING ,
470 .BR KEYCTL_SEARCH ,
471 and
472 .BR KEYCTL_INVALIDATE
473 operations.
474 .IP
475 The permission bits for each category are
476 .BR KEY_POS_SEARCH ,
477 .BR KEY_USR_SEARCH ,
478 .BR KEY_GRP_SEARCH ,
479 and
480 .BR KEY_OTH_SEARCH .
481 .TP
482 .IR link
483 This permission allows a key or keyring to be linked to.
484 .IP
485 This permission is required for the
486 .BR KEYCTL_LINK
487 and
488 .BR KEYCTL_SESSION_TO_PARENT
489 operations.
490 .IP
491 The permission bits for each category are
492 .BR KEY_POS_LINK ,
493 .BR KEY_USR_LINK ,
494 .BR KEY_GRP_LINK ,
495 and
496 .BR KEY_OTH_LINK .
497 .TP
498 .IR setattr " (since Linux 2.6.15)."
499 This permission allows a key's UID, GID, and permissions mask to be changed.
500 .IP
501 This permission is required for the
502 .BR KEYCTL_REVOKE ,
503 .BR KEYCTL_CHOWN ,
504 and
505 .BR KEYCTL_SETPERM
506 operations.
507 .IP
508 The permission bits for each category are
509 .BR KEY_POS_SETATTR ,
510 .BR KEY_USR_SETATTR ,
511 .BR KEY_GRP_SETATTR ,
512 and
513 .BR KEY_OTH_SETATTR .
514 .RE
515 .IP
516 As a convenience, the following macros are defined as masks for
517 all of the permission bits in each of the user categories:
518 .BR KEY_POS_ALL ,
519 .BR KEY_USR_ALL,
520 .BR KEY_GRP_ALL ,
521 and
522 .BR KEY_OTH_ALL .
523 .IP
524 The
525 .IR arg4 " and " arg5
526 arguments are ignored.
527 .IP
528 This operation is exposed by
529 .I libkeyutils
530 via the function
531 .BR keyctl_setperm (3).
532 .TP
533 .BR KEYCTL_DESCRIBE " (since Linux 2.6.10)"
534 Obtain a string describing the attributes of a specified key.
535 .IP
536 The ID of the key to be described is specified in
537 .I arg2
538 (cast to
539 .IR key_serial_t ).
540 The descriptive string is returned in the buffer pointed to by
541 .I arg3
542 (cast to
543 .IR "char\ *" );
544 .I arg4
545 (cast to
546 .IR size_t )
547 specifies the size of that buffer in bytes.
548 .IP
549 The key must grant the caller
550 .I view
551 permission.
552 .IP
553 The returned string is null-terminated and
554 contains the following information about the key:
555 .IP
556 .in +4n
557 .IR type ; uid ; gid ; perm ; description
558 .in
559 .IP
560 In the above,
561 .IR type
562 and
563 .IR description
564 are strings,
565 .IR uid
566 and
567 .IR gid
568 are decimal strings, and
569 .I perm
570 is a hexadecimal permissions mask.
571 The descriptive string is written with the following format:
572 .IP
573 %s;%d;%d;%08x;%s
574 .IP
575 .BR "Note: the intention is that the descriptive string should"
576 .BR "be extensible in future kernel versions".
577 In particular, the
578 .IR description
579 field will not contain semicolons;
580 .\" FIXME But, the kernel does not enforce the requirement
581 .\" that the key description contains no semicolons!
582 .\" So, user space has no guarantee here??
583 .\" Either something more needs to be said here,
584 .\" or a kernel fix is required.
585 it should be parsed by working backwards from the end of the string
586 to find the last semicolon.
587 This allows future semicolon-delimited fields to be inserted
588 in the descriptive string in the future.
589 .IP
590 Writing to the buffer is attempted only when
591 .IR arg3
592 is non-NULL and the specified buffer size
593 is large enough to accept the descriptive string
594 (including the terminating null byte).
595 '\" Function commentary says it copies up to buflen bytes, but see the
596 '\" (buffer && buflen >= ret) condition in keyctl_describe_key() in
597 '\" security/keyctl.c
598 In order to determine whether the buffer size was too small,
599 check to see if the return value of the operation is greater than
600 .IR arg4 .
601 .IP
602 The
603 .I arg5
604 argument is ignored.
605 .IP
606 This operation is exposed by
607 .I libkeyutils
608 via the function
609 .BR keyctl_describe (3).
610 .TP
611 .B KEYCTL_CLEAR
612 Clear the contents of (i.e., unlink all keys from) a keyring.
613 .IP
614 The ID of the key
615 (which must be of keyring type)
616 .\" or the error ENOTDIR results
617 is provided in
618 .I arg2
619 (cast to
620 .IR key_serial_t ).
621 .\" According to Documentation/security/keys.txt:
622 .\" This function can also be used to clear special kernel keyrings if they
623 .\" are appropriately marked if the user has CAP_SYS_ADMIN capability. The
624 .\" DNS resolver cache keyring is an example of this.
625 .IP
626 The caller must have
627 .I write
628 permission on the keyring.
629 .IP
630 The arguments
631 .IR arg3 ,
632 .IR arg4 ,
633 and
634 .IR arg5
635 are ignored.
636 .IP
637 This operation is exposed by
638 .I libkeyutils
639 via the function
640 .BR keyctl_clear (3).
641 .TP
642 .BR KEYCTL_LINK " (since Linux 2.6.10)"
643 Create a link from a keyring to a key.
644 .IP
645 The key to be linked is specified in
646 .IR arg2
647 (cast to
648 .IR key_serial_t );
649 the keyring is specified in
650 .IR arg3
651 (cast to
652 .IR key_serial_t ).
653 .IP
654 If a key with the same type and description is already linked in the keyring,
655 then that key is displaced from the keyring.
656 .IP
657 Before creating the link,
658 the kernel checks the nesting of the keyrings and returns appropriate errors
659 if the link would produce a cycle
660 or if the nesting of keyrings would be too deep
661 (The limit on the nesting of keyrings is determined by the kernel constant
662 .BR KEYRING_SEARCH_MAX_DEPTH ,
663 defined with the value 6, and is necessary to prevent overflows
664 on the kernel stack when recursively searching keyrings).
665 .IP
666 The caller must have
667 .I link
668 permission on the key being added and
669 .I write
670 permission on the keyring.
671 .IP
672 The arguments
673 .IR arg4
674 and
675 .IR arg5
676 are ignored.
677 .IP
678 This operation is exposed by
679 .I libkeyutils
680 via the function
681 .BR keyctl_link (3).
682 .TP
683 .BR KEYCTL_UNLINK " (since Linux 2.6.10)"
684 Unlink a key from a keyring.
685 .IP
686 The ID of the key to be unlinked is specified in
687 .I arg2
688 (cast to
689 .IR key_serial_t );
690 the ID of the keyring from which it is to be unlinked is specified in
691 .I arg3
692 (cast to
693 .IR key_serial_t ).
694 .IP
695 If the key is not currently linked into the keyring, an error results.
696 .IP
697 The caller must have
698 .I write
699 permission on the keyring from which the key is being removed.
700 .IP
701 If the last link to a key is removed,
702 then that key will be scheduled for destruction.
703 .IP
704 The arguments
705 .IR arg4
706 and
707 .IR arg5
708 are ignored.
709 .IP
710 This operation is exposed by
711 .I libkeyutils
712 via the function
713 .BR keyctl_unlink (3).
714 .TP
715 .BR KEYCTL_SEARCH " (since Linux 2.6.10)"
716 Search for a key in a keyring tree,
717 returning its ID and optionally linking it to a specified keyring.
718 .IP
719 The tree to be searched is specified by passing
720 the ID of the head keyring in
721 .IR arg2
722 (cast to
723 .IR key_serial_t ).
724 The search is performed breadth-first and recursively.
725 .IP
726 The
727 .I arg3
728 and
729 .I arg4
730 arguments specify the key to be searched for:
731 .I arg3
732 (cast as
733 .IR "char\ *" )
734 contains the key type
735 (a null-terminated character string up to 32 bytes in size,
736 including the terminating null byte), and
737 .I arg4
738 (cast as
739 .IR "char\ *" )
740 contains the description of the key
741 (a null-terminated character string up to 4096 bytes in size,
742 including the terminating null byte).
743 .IP
744 The source keyring must grant
745 .I search
746 permission to the caller.
747 When performing the recursive search, only keyrings that grant the caller
748 .I search
749 permission will be searched.
750 Only keys with for which the caller has
751 .I search
752 permission can be found.
753 .IP
754 If the key is found, its ID is returned as the function result.
755 .IP
756 If the key is found and
757 .I arg5
758 (cast to
759 .IR key_serial_t )
760 is nonzero, then, subject to the same constraints and rules as
761 .BR KEYCTL_LINK ,
762 the key is linked into the keyring whose ID is specified in
763 .IR arg5 .
764 If the destination keyring specified in
765 .I arg5
766 already contains a link to a key that has the same type and description,
767 then that link will be displaced by a link to
768 the key found by this operation.
769 .IP
770 Instead of valid existing keyring IDs, the source
771 .RI ( arg2 )
772 and destination
773 .RI ( arg5 )
774 keyrings can be one of the special keyring IDs listed under
775 .BR KEYCTL_GET_KEYRING_ID .
776 .IP
777 This operation is exposed by
778 .I libkeyutils
779 via the function
780 .BR keyctl_search (3).
781 .TP
782 .BR KEYCTL_READ " (since Linux 2.6.10)"
783 Read the payload data of a key.
784 .IP
785 The ID of the key whose payload is to be read is specified in
786 .I arg2
787 (cast to
788 .IR key_serial_t ).
789 This can be the ID of an existing key,
790 or any of the special key IDs listed for
791 .BR KEYCTL_GET_KEYRING_ID .
792 .\" including KEY_SPEC_REQKEY_AUTH_KEY
793 .IP
794 The payload is placed in the buffer pointed by
795 .I arg3
796 (cast to
797 .IR "char\ *" );
798 the size of that buffer must be specified in
799 .I arg4
800 (cast to
801 .IR size_t ).
802 .IP
803 The returned data will be processed for presentation
804 according to the key type.
805 For example, a keyring will return an array of
806 .I key_serial_t
807 entries representing the IDs of all the keys that are linked to it.
808 The
809 .IR "user"
810 key type will return its data as is.
811 If a key type does not implement this function,
812 the operation fails with the error
813 .BR EOPNOTSUPP .
814 .IP
815 If
816 .I arg3
817 is not NULL,
818 as much of the payload data as will fit is copied into the buffer.
819 On a successful return,
820 the return value is always the total size of the payload data.
821 To determine whether the buffer was of sufficient size,
822 check to see that the return value is less than or equal to
823 the value supplied in
824 .IR arg4 .
825 .IP
826 The key must either grant the caller
827 .I read
828 permission, or grant the caller
829 .I search
830 permission when searched for from the process keyrings
831 (i.e., the key is possessed).
832 .IP
833 The
834 .I arg5
835 argument is ignored.
836 .IP
837 This operation is exposed by
838 .I libkeyutils
839 via the function
840 .BR keyctl_read (3).
841 .TP
842 .BR KEYCTL_INSTANTIATE " (since Linux 2.6.10)"
843 (Positively) instantiate an uninstantiated key with a specified payload.
844 .IP
845 The ID of the key to be instantiated is provided in
846 .I arg2
847 (cast to
848 .IR key_serial_t ).
849 .IP
850 The key payload is specified in the buffer pointed to by
851 .I arg3
852 (cast to
853 .IR "void\ *");
854 the size of that buffer is specified in
855 .I arg4
856 (cast to
857 .IR size_t ).
858 .IP
859 The payload may be a NULL pointer and the buffer size may be 0
860 if this is supported by the key type (e.g., it is a keyring).
861 .IP
862 The operation may be fail if the payload data is in the wrong format
863 or is otherwise invalid.
864 .IP
865 If
866 .I arg5
867 (cast to
868 .IR key_serial_t )
869 is nonzero, then, subject to the same constraints and rules as
870 .BR KEYCTL_LINK ,
871 the instantiated key is linked into the keyring whose ID specified in
872 .IR arg5 .
873 .IP
874 The caller must have the appropriate authorization key,
875 and once the uninstantiated key has been instantiated,
876 the authorization key is revoked.
877 In other words, this operation is available only from a
878 .BR request-key (8)-style
879 program.
880 See
881 .BR request_key (2)
882 for an explanation of uninstantiated keys and key instantiation.
883 .IP
884 This operation is exposed by
885 .I libkeyutils
886 via the function
887 .BR keyctl_instantiate (3).
888 .TP
889 .BR KEYCTL_NEGATE " (since Linux 2.6.10)"
890 Negatively instantiate an uninstantiated key.
891 .IP
892 This operation is equivalent to the call:
893 .IP
894 keyctl(KEYCTL_REJECT, arg2, arg3, ENOKEY, arg4);
895 .IP
896 The
897 .I arg5
898 argument is ignored.
899 .IP
900 This operation is exposed by
901 .I libkeyutils
902 via the function
903 .BR keyctl_negate (3).
904 .TP
905 .BR KEYCTL_SET_REQKEY_KEYRING " (since Linux 2.6.13)"
906 Set the default keyring to which implicitly requested keys
907 will be linked for this thread, and return the previous setting.
908 Implicit key requests are those made by internal kernel components,
909 .\" I.e., calls to the kernel's internal request_key() interface,
910 .\" which is distinct from the request_key(2) system call (which
911 .\" ultimately employs the kernel-internal interface).
912 such as can occur when, for example, opening files
913 on an AFS or NFS filesystem.
914 Setting the default keyring also has an effect when requesting
915 a key from user space; see
916 .BR request_key (2)
917 for details.
918 .IP
919 The
920 .I arg2
921 argument (cast to
922 .IR int )
923 should contain one of the following values,
924 to specify the new default keyring:
925 .RS
926 .TP
927 .BR KEY_REQKEY_DEFL_NO_CHANGE
928 Don't change the default keyring.
929 This can be used to discover the current default keyring
930 (without changing it).
931 .TP
932 .BR KEY_REQKEY_DEFL_DEFAULT
933 This selects the default behaviour,
934 which is to use the thread-specific keyring if there is one,
935 otherwise the process-specific keyring if there is one,
936 otherwise the session keyring if there is one,
937 otherwise the UID-specific session keyring,
938 otherwise the user-specific keyring.
939 .TP
940 .BR KEY_REQKEY_DEFL_THREAD_KEYRING
941 Use the thread-specific keyring
942 .RB ( thread-keyring (7))
943 as the new default keyring.
944 .TP
945 .BR KEY_REQKEY_DEFL_PROCESS_KEYRING
946 Use the process-specific keyring
947 .RB ( process-keyring (7))
948 as the new default keyring.
949 .TP
950 .BR KEY_REQKEY_DEFL_SESSION_KEYRING
951 Use the session-specific keyring
952 .RB ( session-keyring (7))
953 as the new default keyring.
954 .TP
955 .BR KEY_REQKEY_DEFL_USER_KEYRING
956 Use the UID-specific keyring
957 .RB ( user-keyring (7))
958 as the new default keyring.
959 .TP
960 .BR KEY_REQKEY_DEFL_USER_SESSION_KEYRING
961 Use the UID-specific session keyring
962 .RB ( user-session-keyring (7))
963 as the new default keyring.
964 .TP
965 .BR KEY_REQKEY_DEFL_REQUESTOR_KEYRING " (since Linux 2.6.29)"
966 '\" 8bbf4976b59fc9fc2861e79cab7beb3f6d647640
967 Use the requestor keyring.
968 .\" FIXME The preceding explanation needs to be expanded.
969 .\" Is the following correct:
970 .\"
971 .\" The requestor keyring is the dest_keyring that
972 .\" was supplied to a call to request_key(2)?
973 .\"
974 .\" David Howells said: to be checked
975 .RE
976 .IP
977 All other values are invalid.
978 .\" (including the still-unsupported KEY_REQKEY_DEFL_GROUP_KEYRING)
979 .IP
980 The arguments
981 .IR arg3 ,
982 .IR arg4 ,
983 and
984 .IR arg5
985 are ignored.
986 .IP
987 The setting controlled by this operation is inherited by the child of
988 .BR fork (2)
989 and preserved across
990 .BR execve (2).
991 .IP
992 This operation is exposed by
993 .I libkeyutils
994 via the function
995 .BR keyctl_set_reqkey_keyring (3).
996 .TP
997 .BR KEYCTL_SET_TIMEOUT " (since Linux 2.6.16)"
998 Set a timeout on a key.
999 .IP
1000 The ID of the key is specified in
1001 .I arg2
1002 (cast to
1003 .IR key_serial_t ).
1004 The timeout value, in seconds from the current time,
1005 is specified in
1006 .I arg3
1007 (cast to
1008 .IR "unsigned int" ).
1009 The timeout is measured against the realtime clock.
1010 .IP
1011 Specifying the timeout value as 0 clears any existing timeout on the key.
1012 .IP
1013 The
1014 .I /proc/keys
1015 file displays the remaining time until each key will expire.
1016 (This is the only method of discovering the timeout on a key.)
1017 .IP
1018 The caller must either have the
1019 .I setattr
1020 permission on the key
1021 or hold an instantiation authorization token for the key (see
1022 .BR request_key (2)).
1023 .IP
1024 The key and any links to the key will be
1025 automatically garbage collected after the timeout expires.
1026 Subsequent attempts to access the key will then fail with the error
1027 .BR EKEYEXPIRED .
1028 .IP
1029 This operation cannot be used to set timeouts on revoked, expired,
1030 or negatively instantiated keys.
1031 .IP
1032 The arguments
1033 .IR arg4
1034 and
1035 .IR arg5
1036 are ignored.
1037 .IP
1038 This operation is exposed by
1039 .I libkeyutils
1040 via the function
1041 .BR keyctl_set_timeout (3).
1042 .TP
1043 .BR KEYCTL_ASSUME_AUTHORITY " (since Linux 2.6.16)"
1044 Assume (or divest) the authority for the calling thread
1045 to instantiate a key.
1046 .IP
1047 The
1048 .I arg2
1049 argument (cast to
1050 .IR key_serial_t )
1051 specifies either a nonzero key ID to assume authority,
1052 or the value 0 to divest authority.
1053 .IP
1054 If
1055 .I arg2
1056 is nonzero, then it specifies the ID of an uninstantiated key for which
1057 authority is to be assumed.
1058 That key can then be instantiated using one of
1059 .BR KEYCTL_INSTANTIATE ,
1060 .BR KEYCTL_INSTANTIATE_IOV ,
1061 .BR KEYCTL_REJECT ,
1062 or
1063 .BR KEYCTL_NEGATE .
1064 Once the key has been instantiated,
1065 the thread is automatically divested of authority to instantiate the key.
1066 .IP
1067 Authority over a key can be assumed only if the calling thread has present
1068 in its keyrings the authorization key that is
1069 associated with the specified key.
1070 (In other words, the
1071 .BR KEYCTL_ASSUME_AUTHORITY
1072 operation is available only from a
1073 .BR request-key (8)-style
1074 program; see
1075 .BR request_key (2)
1076 for an explanation of how this operation is used.)
1077 The caller must have
1078 .I search
1079 permission on the authorization key.
1080 .IP
1081 If the specified key has a matching authorization key,
1082 then the ID of that key is returned.
1083 The authorization key can be read
1084 .RB ( KEYCTL_READ )
1085 to obtain the callout information passed to
1086 .BR request_key (2).
1087 .IP
1088 If the ID given in
1089 .I arg2
1090 is 0, then the currently assumed authority is cleared (divested),
1091 and the value 0 is returned.
1092 .IP
1093 The
1094 .BR KEYCTL_ASSUME_AUTHORITY
1095 mechanism allows a program such as
1096 .BR request-key (8)
1097 to assume the necessary authority to instantiate a new uninstantiated key
1098 that was created as a consequence of a call to
1099 .BR request_key (2).
1100 For further information, see
1101 .BR request_key (2)
1102 and the kernel source file
1103 .IR Documentation/security/keys-request-key.txt .
1104 .IP
1105 The arguments
1106 .IR arg3 ,
1107 .IR arg4 ,
1108 and
1109 .IR arg5
1110 are ignored.
1111 .IP
1112 This operation is exposed by
1113 .I libkeyutils
1114 via the function
1115 .BR keyctl_assume_authority (3).
1116 .TP
1117 .BR KEYCTL_GET_SECURITY " (since Linux 2.6.26)"
1118 .\" commit 70a5bb72b55e82fbfbf1e22cae6975fac58a1e2d
1119 Get the LSM (Linux Security Module) security label of the specified key.
1120 .IP
1121 The ID of the key whose security label is to be fetched is specified in
1122 .I arg2
1123 (cast to
1124 .IR key_serial_t ).
1125 The security label (terminated by a null byte)
1126 will be placed in the buffer pointed to by
1127 .I arg3
1128 argument (cast to
1129 .IR "char\ *" );
1130 the size of the buffer must be provided in
1131 .I arg4
1132 (cast to
1133 .IR size_t ).
1134 .IP
1135 If
1136 .I arg3
1137 is specified as NULL or the buffer size specified in
1138 .IR arg4
1139 is too small, the full size of the security label string
1140 (including the terminating null byte)
1141 is returned as the function result,
1142 and nothing is copied to the buffer.
1143 .IP
1144 The caller must have
1145 .I view
1146 permission on the specified key.
1147 .IP
1148 The returned security label string will be rendered in a form appropriate
1149 to the LSM in force.
1150 For example, with SELinux, it may look like:
1151 .IP
1152 unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
1153 .IP
1154 If no LSM is currently in force,
1155 then an empty string is placed in the buffer.
1156 .IP
1157 The
1158 .I arg5
1159 argument is ignored.
1160 .IP
1161 This operation is exposed by
1162 .I libkeyutils
1163 via the functions
1164 .BR keyctl_get_security (3)
1165 and
1166 .BR keyctl_get_security_alloc (3).
1167 .TP
1168 .BR KEYCTL_SESSION_TO_PARENT " (since Linux 2.6.32)"
1169 .\" commit ee18d64c1f632043a02e6f5ba5e045bb26a5465f
1170 Replace the session keyring to which the
1171 .I parent
1172 of the calling process
1173 subscribes with the session keyring of the calling process.
1174 .\" What is the use case for KEYCTL_SESSION_TO_PARENT?
1175 .\" David Howells: the Process Authentication Groups people requested this,
1176 .\" but then didn't use it; maybe there are no users.
1177 .IP
1178 The keyring will be replaced in the parent process at the point
1179 where the parent next transitions from kernel space to user space.
1180 .IP
1181 The keyring must exist and must grant the caller
1182 .I link
1183 permission.
1184 The parent process must be single-threaded and have
1185 the same effective ownership as this process
1186 and must not be set-user-ID or set-group-ID.
1187 The UID of the parent process's existing session keyring (f it has one),
1188 as well as the UID of the caller's session keyring
1189 much match the caller's effective UID.
1190 .IP
1191 The fact that it is the parent process that is affected by this operation
1192 allows a program such as the shell to start a child process that
1193 uses this operation to change the shell's session keyring.
1194 (This is what the
1195 .BR keyctl (1)
1196 .B new_session
1197 command does.)
1198 .IP
1199 The arguments
1200 .IR arg2 ,
1201 .IR arg3 ,
1202 .IR arg4 ,
1203 and
1204 .IR arg5
1205 are ignored.
1206 .IP
1207 This operation is exposed by
1208 .I libkeyutils
1209 via the function
1210 .BR keyctl_session_to_parent (3).
1211 .TP
1212 .BR KEYCTL_REJECT " (since Linux 2.6.39)"
1213 .\" commit fdd1b94581782a2ddf9124414e5b7a5f48ce2f9c
1214 Mark a key as negatively instantiated and set an expiration timer
1215 on the key.
1216 This operation provides a superset of the functionality of the earlier
1217 .BR KEYCTL_NEGATE
1218 operation.
1219 .IP
1220 The ID of the key that is to be negatively instantiated is specified in
1221 .I arg2
1222 (cast to
1223 .IR key_serial_t ).
1224 The
1225 .I arg3
1226 (cast to
1227 .IR "unsigned int" )
1228 argument specifies the lifetime of the key, in seconds.
1229 The
1230 .I arg4
1231 argument (cast to
1232 .IR "unsigned int" )
1233 specifies the error to be returned when a search hits this key;
1234 typically, this is one of
1235 .BR EKEYREJECTED ,
1236 .BR EKEYREVOKED ,
1237 or
1238 .BR EKEYEXPIRED .
1239 .IP
1240 If
1241 .I arg5
1242 (cast to
1243 .IR key_serial_t )
1244 is nonzero, then, subject to the same constraints and rules as
1245 .BR KEYCTL_LINK ,
1246 the negatively instantiated key is linked into the keyring
1247 whose ID is specified in
1248 .IR arg5 .
1249 .IP
1250 The caller must have the appropriate authorization key.
1251 In other words, this operation is available only from a
1252 .BR request-key (8)-style
1253 program.
1254 See
1255 .BR request_key (2).
1256 .IP
1257 The caller must have the appropriate authorization key,
1258 and once the uninstantiated key has been instantiated,
1259 the authorization key is revoked.
1260 In other words, this operation is available only from a
1261 .BR request-key (8)-style
1262 program.
1263 See
1264 .BR request_key (2)
1265 for an explanation of uninstantiated keys and key instantiation.
1266 .IP
1267 This operation is exposed by
1268 .I libkeyutils
1269 via the function
1270 .BR keyctl_reject (3).
1271 .TP
1272 .BR KEYCTL_INSTANTIATE_IOV " (since Linux 2.6.39)"
1273 .\" commit ee009e4a0d4555ed522a631bae9896399674f063
1274 Instantiate an uninstantiated key with a payload specified
1275 via a vector of buffers.
1276 .IP
1277 This operation is the same as
1278 .BR KEYCTL_INSTANTIATE ,
1279 but the payload data is specified as an array of
1280 .IR iovec
1281 structures:
1282 .IP
1283 .in +4n
1284 .nf
1285 struct iovec {
1286 void *iov_base; /* Starting address of buffer */
1287 size_t iov_len; /* Size of buffer (in bytes) */
1288 };
1289 .fi
1290 .in
1291 .IP
1292 The pointer to the payload vector is specified in
1293 .IR arg3
1294 (cast as
1295 .IR "const struct iovec\ *" ).
1296 The number of items in the vector is specified in
1297 .IR arg4
1298 (cast as
1299 .IR "unsigned int" ).
1300 .IP
1301 The
1302 .I arg2
1303 (key ID)
1304 and
1305 .I arg5
1306 (keyring ID)
1307 are interpreted as for
1308 .BR KEYCTL_INSTANTIATE .
1309 .IP
1310 This operation is exposed by
1311 .I libkeyutils
1312 via the function
1313 .BR keyctl_instantiate_iov (3).
1314 .TP
1315 .BR KEYCTL_INVALIDATE " (since Linux 3.5)"
1316 .\" commit fd75815f727f157a05f4c96b5294a4617c0557da
1317 Mark a key as invalid.
1318 .IP
1319 The ID of the key to be invalidated is specified in
1320 .I arg2
1321 (cast to
1322 .IR key_serial_t ).
1323 .IP
1324 To invalidate a key,
1325 the caller must have
1326 .I search
1327 permission on the key.
1328 .\" CAP_SYS_ADMIN is permitted to invalidate certain special keys
1329 .IP
1330 This operation marks the key as invalid
1331 and schedules immediate garbage collection.
1332 The garbage collector removes the invalidated key from all keyrings and
1333 deletes the key when its reference count reaches zero.
1334 After this operation,
1335 the key will be ignored by all searches,
1336 even if it is not yet deleted.
1337 .IP
1338 Keys that are marked invalid become invisible to normal key operations
1339 immediately, though they are still visible in
1340 .I /proc/keys
1341 (marked with an 'i' flag)
1342 until they are actually removed.
1343 .IP
1344 The arguments
1345 .IR arg3 ,
1346 .IR arg4 ,
1347 and
1348 .IR arg5
1349 are ignored.
1350 .IP
1351 This operation is exposed by
1352 .I libkeyutils
1353 via the function
1354 .BR keyctl_invalidate (3).
1355 .TP
1356 .BR KEYCTL_GET_PERSISTENT " (since Linux 3.13)"
1357 .\" commit f36f8c75ae2e7d4da34f4c908cebdb4aa42c977e
1358 Get the persistent keyring
1359 .RB ( persistent-keyring (7))
1360 for a specified user and link it to a specified keyring.
1361 .IP
1362 The user ID is specified in
1363 .I arg2
1364 (cast to
1365 .IR uid_t ).
1366 If the value \-1 is specified, the caller's real user ID is used.
1367 The ID of the destination keyring is specified in
1368 .I arg3
1369 (cast to
1370 .IR key_serial_t ).
1371 .IP
1372 The caller must have the
1373 .BR CAP_SETUID
1374 capability in its user namespace in order to fetch the persistent keyring
1375 for a user ID that does not match either the real or effective user ID
1376 of the caller.
1377 .IP
1378 If the call is successful,
1379 a link to the persistent keyring is added to the keyring
1380 whose ID was specified in
1381 .IR arg3 .
1382 .IP
1383 The caller must have
1384 .I write
1385 permission on the keyring.
1386 .IP
1387 The persistent keyring will be created by the kernel
1388 if it does not yet exist.
1389 .IP
1390 Each time the
1391 .B KEYCTL_GET_PERSISTENT
1392 operation is performed, the persistent keyring will
1393 have its expiration timeout reset to the value in:
1394 .IP
1395 /proc/sys/kernel/keys/persistent_keyring_expiry
1396 .IP
1397 Should the timeout be reached,
1398 the persistent keyring will be removed and
1399 everything it pins can then be garbage collected.
1400 .IP
1401 Persistent keyrings were added to Linux in kernel version 3.13.
1402 .IP
1403 The arguments
1404 .IR arg4
1405 and
1406 .IR arg5
1407 are ignored.
1408 .IP
1409 This operation is exposed by
1410 .I libkeyutils
1411 via the function
1412 .BR keyctl_get_persistent (3).
1413 .TP
1414 .BR KEYCTL_DH_COMPUTE " (since Linux 4.7)"
1415 .\" commit ddbb41148724367394d0880c516bfaeed127b52e
1416 Compute a Diffie-Hellman shared secret or public key.
1417 .IP
1418 The
1419 .I arg2
1420 argument is a pointer to a set of parameters containing
1421 serial numbers for three
1422 .IR """user"""
1423 keys used in the Diffie-Hellman calculation,
1424 packaged in a structure of the following form:
1425 .IP
1426 .in +4n
1427 .nf
1428 struct keyctl_dh_params {
1429 int32_t private; /* The local private key */
1430 int32_t prime; /* The prime, known to both parties */
1431 int32_t base; /* The base integer: either a shared
1432 generator or the remote public key */
1433 };
1434 .fi
1435 .in
1436 .IP
1437 Each of the three keys specified in this structure must grant the caller
1438 .I read
1439 permission.
1440 The payloads of these keys are used to calculate the Diffie-Hellman
1441 result as:
1442 .IP
1443 base ^ private mod prime
1444 .IP
1445 If the base is the shared generator, the result is the local public key.
1446 If the base is the remote public key, the result is the shared secret.
1447 .IP
1448 The
1449 .I arg3
1450 argument (cast to
1451 .IR "char\ *" )
1452 points to a buffer where the result of the calculation is placed.
1453 The size of that buffer is specified in
1454 .I arg4
1455 (cast to
1456 .IR size_t ).
1457 .IP
1458 The buffer must be large enough to accommodate the output data,
1459 otherwise an error is returned.
1460 If
1461 .I arg4
1462 is specified zero,
1463 in which case the buffer is not used and
1464 the operation returns the minimum required buffer size
1465 (i.e., the length of the prime).
1466 .IP
1467 Diffie-Hellman computations can be performed in user space,
1468 but require a multiple-precision integer (MPI) library.
1469 Moving the implementation into the kernel gives access to
1470 the kernel MPI implementation,
1471 and allows access to secure or acceleration hardware.
1472 .IP
1473 Adding support for DH computation to the
1474 .BR keyctl()
1475 system call was considered a good fit due to the DH algorithm's use
1476 for deriving shared keys;
1477 it also allows the type of the key to determine
1478 which DH implementation (software or hardware) is appropriate.
1479 .IP
1480 The
1481 .I arg5
1482 argument is reserved and must be 0.
1483 .SH RETURN VALUE
1484 For a successful call, the return value depends on the operation:
1485 .TP
1486 .B KEYCTL_GET_KEYRING_ID
1487 The ID of the requested keyring.
1488 .TP
1489 .B KEYCTL_JOIN_SESSION_KEYRING
1490 The ID of the joined session keyring.
1491 .TP
1492 .B KEYCTL_DESCRIBE
1493 The size of the description (including the terminating null byte),
1494 irrespective of the provided buffer size.
1495 .TP
1496 .B KEYCTL_SEARCH
1497 The ID of the key that was found.
1498 .TP
1499 .B KEYCTL_READ
1500 The amount of data that is available in the key,
1501 irrespective of the provided buffer size.
1502 .TP
1503 .B KEYCTL_SET_REQKEY_KEYRING
1504 The ID of the previous default keyring
1505 to which implicitly requested keys were linked
1506 (one of
1507 .BR KEY_REQKEY_DEFL_USER_* ).
1508 .TP
1509 .B KEYCTL_ASSUME_AUTHORITY
1510 Either 0, if the ID given was 0,
1511 or the ID of the authorization key matching the specified key,
1512 if a non-zero key ID was provided.
1513 .TP
1514 .B KEYCTL_GET_SECURITY
1515 The size of the LSM security label string
1516 (including the terminating null byte),
1517 irrespective of the provided buffer size.
1518 .TP
1519 .B KEYCTL_GET_PERSISTENT
1520 The ID of the persistent keyring.
1521 .TP
1522 .B KEYCTL_DH_COMPUTE
1523 The number of bytes copied to the buffer, or, if
1524 .I arg4
1525 is 0, the required buffer size.
1526 .TP
1527 All other operations
1528 Zero.
1529 .PP
1530 On error, \-1 is returned, and
1531 .I errno
1532 is set appropriately to indicate the error.
1533 .SH ERRORS
1534 .TP
1535 .B EACCES
1536 The requested operation wasn't permitted.
1537 .TP
1538 .B EDEADLK
1539 .I operation
1540 is
1541 .BR KEYCTL_LINK
1542 and the requested link would result in a cycle.
1543 .TP
1544 .B EDQUOT
1545 The key quota for the caller's user would be exceeded by creating a key or
1546 linking it to the keyring.
1547 .TP
1548 .B EINVAL
1549 .I operation
1550 was
1551 .B KEYCTL_SETPERM
1552 and an invalid permission bit was specified in
1553 .IR arg3 .
1554 .TP
1555 .B EINVAL
1556 .I operation
1557 was
1558 .BR KEYCTL_SEARCH
1559 and the size of the description in
1560 .IR arg4
1561 (including the terminating null byte) exceeded 4096 bytes.
1562 size of the string (including the terminating null byte) specified in
1563 .I arg3
1564 (the key type)
1565 or
1566 .I arg4
1567 (the key description)
1568 exceeded the limit (32 bytes and 4096 bytes respectively).
1569 .TP
1570 .B EINVAL
1571 .I operation
1572 was
1573 .B KEYCTL_DH_COMPUTE
1574 and the buffer size provided is not enough to hold the result.
1575 Provide 0 as a buffer size in order to obtain the minimum buffer size.
1576 .TP
1577 .B EKEYEXPIRED
1578 An expired key was found or specified.
1579 .TP
1580 .B EKEYREJECTED
1581 A rejected key was found or specified.
1582 .TP
1583 .B EKEYREVOKED
1584 A revoked key was found or specified.
1585 .TP
1586 .B ELOOP
1587 .I operation
1588 is
1589 .BR KEYCTL_LINK
1590 and the requested link would cause the maximum nesting depth
1591 for keyrings to be exceeded.
1592 .TP
1593 .BR ENFILE " (Linux kernels before 3.13)"
1594 .IR operation
1595 is
1596 .BR KEYCTL_LINK
1597 and the keyring is full.
1598 (Before Linux 3.13,
1599 .\" commit b2a4df200d570b2c33a57e1ebfa5896e4bc81b69
1600 the available space for storing keyring links was limited to
1601 a single page of memory; since Linux 3.13, there is no fixed limit.)
1602 .TP
1603 .B ENOENT
1604 .I operation
1605 is
1606 .B KEYCTL_UNLINK
1607 and the key to be unlinked isn't linked to the keyring.
1608 .TP
1609 .B ENOKEY
1610 No matching key was found or an invalid key was specified.
1611 .TP
1612 .B ENOKEY
1613 The value
1614 .B KEYCTL_GET_KEYRING_ID
1615 was specified in
1616 .IR operation ,
1617 the key specified in
1618 .I arg2
1619 did not exist, and
1620 .I arg3
1621 was zero (meaning don't create the key if it didn't exist).
1622 .TP
1623 .B ENOTDIR
1624 A key of keyring type was expected but the ID of a key with
1625 a different type was provided.
1626 .TP
1627 .B EOPNOTSUPP
1628 .I operation
1629 is
1630 .B KEYCTL_READ
1631 and the key type does not support reading
1632 (e.g., the type is
1633 .IR """login""" ).
1634 .TP
1635 .B EOPNOTSUPP
1636 .I operation
1637 is
1638 .B KEYCTL_UPDATE
1639 and the key type does not support updating.
1640 .TP
1641 .B EPERM
1642 .I operation
1643 was
1644 .BR KEYCTL_GET_PERSISTENT ,
1645 .I arg2
1646 specified a UID other than the calling thread's real or effective UID,
1647 and the caller did not have the
1648 .B CAP_SETUID
1649 capability.
1650 .TP
1651 .B EPERM
1652 .I operation
1653 was
1654 .BR KEYCTL_SESSION_TO_PARENT
1655 and either:
1656 all of the UIDs (GIDs) of the parent process do not match
1657 the effective UID (GID) of the calling process;
1658 the UID of the parent's existing session keyring or
1659 the UID of the caller's session keyring did not match
1660 the effective UID of the caller;
1661 the parent process is not single-thread;
1662 or the parent process is
1663 .BR init (1)
1664 or a kernel thread.
1665 .SH VERSIONS
1666 This system call first appeared in Linux 2.6.10.
1667 .SH CONFORMING TO
1668 This system call is a nonstandard Linux extension.
1669 .SH NOTES
1670 No wrapper for this system call is provided in glibc.
1671 A wrapper is provided in the
1672 .IR libkeyutils
1673 library.
1674 When employing the wrapper in that library, link with
1675 .IR \-lkeyutils .
1676 However, rather than using this system call directly,
1677 you probably want to use the various library functions
1678 mentioned in the descriptions of individual operations above.
1679 .SH EXAMPLE
1680 The program below provide subset of the functionality of the
1681 .BR request-key (8)
1682 program provided by the
1683 .I keyutils
1684 package.
1685 For informational purposes,
1686 the program records various information in a log file.
1687 .PP
1688 As described in
1689 .BR request_key (2),
1690 the
1691 .BR request-key (8)
1692 program is invoked with command-line arguments that
1693 describe a key that is to be instantiated.
1694 The example program fetches and logs these arguments.
1695 The program assumes authority to instantiate the requested key,
1696 and then instantiates that key.
1697 .PP
1698 The following shell session demonstrates the use of this program.
1699 In the session,
1700 we compile the program and then use it to temporarily replace the standard
1701 .BR request-key (8)
1702 program.
1703 (Note that temporarily disabling the standard
1704 .BR request-key (8)
1705 program may not be safe on some systems.)
1706 While our example program is installed,
1707 we use the example program shown in
1708 .BR request_key (2)
1709 to request a key.
1710 .PP
1711 .in +4n
1712 .nf
1713 $ \fBcc \-o key_instantiate key_instantiate.c \-lkeyutils\fP
1714 $ \fBsudo mv /sbin/request\-key /sbin/request\-key.backup\fP
1715 $ \fBsudo cp key_instantiate /sbin/request\-key\fP
1716 $ \fB./t_request_key user mykey somepayloaddata\fP
1717 Key ID is 20d035bf
1718 $ \fBsudo mv /sbin/request\-key.backup /sbin/request\-key\fP
1719 .fi
1720 .in
1721 .PP
1722 Looking at the log file created by this program,
1723 we can see the command-line arguments supplied to our example program:
1724 .PP
1725 .in +4n
1726 .nf
1727 $ \fBcat /tmp/key_instantiate.log \fP
1728 Time: Mon Nov 7 13:06:47 2016
1729
1730 Command line arguments:
1731 argv[0]: /sbin/request-key
1732 operation: create
1733 key_to_instantiate: 20d035bf
1734 UID: 1000
1735 GID: 1000
1736 thread_keyring: 0
1737 process_keyring: 0
1738 session_keyring: 256e6a6
1739
1740 Key description: user;1000;1000;3f010000;mykey
1741 Auth key payload: somepayloaddata
1742 Destination keyring: 256e6a6
1743 Auth key description: .request_key_auth;1000;1000;0b010000;20d035bf
1744 .fi
1745 .in
1746 .PP
1747 The last few lines of the above output show that the example program
1748 was able to fetch:
1749 .IP * 3
1750 the description of the key to be instantiated,
1751 which included the name of the key
1752 .RI ( mykey );
1753 .IP *
1754 the payload of the authorization key, which consisted of the data
1755 .RI ( somepayloaddata )
1756 passed to
1757 .BR request_key (2);
1758 .IP *
1759 the destination keyring that was specified in the call to
1760 .BR request_key (2);
1761 and
1762 .IP *
1763 the description of the authorization key,
1764 where we can see that the name of the authorization key matches
1765 the ID of the key that is to be instantiated
1766 .RI ( 20d035bf ).
1767 .PP
1768 The example program in
1769 .BR request_key (2)
1770 specified the destination keyring as
1771 .BR KEY_SPEC_SESSION_KEYRING .
1772 By examining the contents of
1773 .IR /proc/keys ,
1774 we can see that this was translated to the ID of the destination keyring
1775 .RI ( 0256e6a6 )
1776 shown in the log output above;
1777 we can also see the newly created key with the name
1778 .IR mykey
1779 and ID
1780 .IR 20d035bf .
1781 .PP
1782 .in +4n
1783 .nf
1784 $ \fBcat /proc/keys | egrep \(aqmykey|256e6a6\(aq\fP
1785 0256e6a6 I\-\-Q\-\-\- 194 perm 3f030000 1000 1000 keyring _ses: 3
1786 20d035bf I\-\-Q\-\-\- 1 perm 3f010000 1000 1000 user mykey: 16
1787 .fi
1788 .in
1789 .SS Program source
1790 \&
1791 .EX
1792 /* key_instantiate.c */
1793
1794 #include <sys/types.h>
1795 #include <keyutils.h>
1796 #include <time.h>
1797 #include <fcntl.h>
1798 #include <stdio.h>
1799 #include <stdlib.h>
1800 #include <unistd.h>
1801 #include <string.h>
1802 #include <errno.h>
1803
1804 #ifndef KEY_SPEC_REQUESTOR_KEYRING
1805 #define KEY_SPEC_REQUESTOR_KEYRING \-8
1806 #endif
1807
1808 int
1809 main(int argc, char *argv[])
1810 {
1811 FILE *fp;
1812 time_t t;
1813 char *operation;
1814 key_serial_t key_to_instantiate, dest_keyring;
1815 key_serial_t thread_keyring, process_keyring, session_keyring;
1816 uid_t uid;
1817 gid_t gid;
1818 char dbuf[256];
1819 char auth_key_payload[256];
1820 int akp_size; /* Size of auth_key_payload */
1821
1822 fp = fopen("/tmp/key_instantiate.log", "w");
1823 if (fp == NULL)
1824 exit(EXIT_FAILURE);
1825
1826 setbuf(fp, NULL);
1827
1828 t = time(NULL);
1829 fprintf(fp, "Time: %s\\n", ctime(&t));
1830
1831 /*
1832 * The kernel passes a fixed set of arguments to the program
1833 * that it execs; fetch them.
1834 */
1835 operation = argv[1];
1836 key_to_instantiate = atoi(argv[2]);
1837 uid = atoi(argv[3]);
1838 gid = atoi(argv[4]);
1839 thread_keyring = atoi(argv[5]);
1840 process_keyring = atoi(argv[6]);
1841 session_keyring = atoi(argv[7]);
1842
1843 fprintf(fp, "Command line arguments:\\n");
1844 fprintf(fp, " argv[0]: %s\\n", argv[0]);
1845 fprintf(fp, " operation: %s\\n", operation);
1846 fprintf(fp, " key_to_instantiate: %lx\\n",
1847 (long) key_to_instantiate);
1848 fprintf(fp, " UID: %ld\\n", (long) uid);
1849 fprintf(fp, " GID: %ld\\n", (long) gid);
1850 fprintf(fp, " thread_keyring: %lx\\n", (long) thread_keyring);
1851 fprintf(fp, " process_keyring: %lx\\n", (long) process_keyring);
1852 fprintf(fp, " session_keyring: %lx\\n", (long) session_keyring);
1853 fprintf(fp, "\\n");
1854
1855 /*
1856 * Assume the authority to instantiate the key named in argv[2]
1857 */
1858 if (keyctl(KEYCTL_ASSUME_AUTHORITY, key_to_instantiate) == \-1) {
1859 fprintf(fp, "KEYCTL_ASSUME_AUTHORITY failed: %s\\n",
1860 strerror(errno));
1861 exit(EXIT_FAILURE);
1862 }
1863
1864 /*
1865 * Fetch the description of the key that is to be instantiated
1866 */
1867 if (keyctl(KEYCTL_DESCRIBE, key_to_instantiate,
1868 dbuf, sizeof(dbuf)) == \-1) {
1869 fprintf(fp, "KEYCTL_DESCRIBE failed: %s\\n", strerror(errno));
1870 exit(EXIT_FAILURE);
1871 }
1872
1873 fprintf(fp, "Key description: %s\\n", dbuf);
1874
1875 /*
1876 * Fetch the payload of the authorization key, which is
1877 * actually the callout data given to request_key()
1878 */
1879 akp_size = keyctl(KEYCTL_READ, KEY_SPEC_REQKEY_AUTH_KEY,
1880 auth_key_payload, sizeof(auth_key_payload));
1881 if (akp_size == \-1) {
1882 fprintf(fp, "KEYCTL_READ failed: %s\\n", strerror(errno));
1883 exit(EXIT_FAILURE);
1884 }
1885
1886 auth_key_payload[akp_size] = \(aq\\0\(aq;
1887 fprintf(fp, "Auth key payload: %s\\n", auth_key_payload);
1888
1889 /*
1890 * For interest, get the ID of the authorization key and
1891 * display it.
1892 */
1893 auth_key = keyctl(KEYCTL_GET_KEYRING_ID,
1894 KEY_SPEC_REQKEY_AUTH_KEY);
1895 if (auth_key == \-1) {
1896 fprintf(fp, "KEYCTL_GET_KEYRING_ID failed: %s\\n",
1897 strerror(errno));
1898 exit(EXIT_FAILURE);
1899 }
1900
1901 fprintf(fp, "Auth key ID: %lx\\n", (long) auth_key);
1902
1903 /*
1904 * Fetch key ID for the request_key(2) destination keyring.
1905 */
1906 dest_keyring = keyctl(KEYCTL_GET_KEYRING_ID,
1907 KEY_SPEC_REQUESTOR_KEYRING);
1908 if (dest_keyring == \-1) {
1909 fprintf(fp, "KEYCTL_GET_KEYRING_ID failed: %s\\n",
1910 strerror(errno));
1911 exit(EXIT_FAILURE);
1912 }
1913
1914 fprintf(fp, "Destination keyring: %lx\\n", (long) dest_keyring);
1915
1916 /*
1917 * Fetch the description of the authorization key. This
1918 * allows us to see the key type, UID, GID, permissions,
1919 * and description (name) of the key. Among other things,
1920 * we will see that the name of the key is a hexadecimal
1921 * string representing the ID of the key to be instantiated.
1922 */
1923 if (keyctl(KEYCTL_DESCRIBE, KEY_SPEC_REQKEY_AUTH_KEY,
1924 dbuf, sizeof(dbuf)) == \-1) {
1925 fprintf(fp, "KEYCTL_DESCRIBE failed: %s\\n", strerror(errno));
1926 exit(EXIT_FAILURE);
1927 }
1928
1929 fprintf(fp, "Auth key description: %s\\n", dbuf);
1930
1931 /*
1932 * Instantiate the key using the callout data that was supplied
1933 * in the payload of the authorization key.
1934 */
1935 if (keyctl(KEYCTL_INSTANTIATE, key_to_instantiate,
1936 auth_key_payload, akp_size + 1, dest_keyring) == \-1) {
1937 fprintf(fp, "KEYCTL_INSTANTIATE failed: %s\\n",
1938 strerror(errno));
1939 exit(EXIT_FAILURE);
1940 }
1941
1942 exit(EXIT_SUCCESS);
1943 }
1944 .EE
1945 .SH SEE ALSO
1946 .ad l
1947 .nh
1948 .BR keyctl (1),
1949 .BR add_key (2),
1950 .BR request_key (2),
1951 .\" .BR find_key_by_type_and_name (3)
1952 .\" There is a man page, but this function seems not to exist
1953 .BR keyctl (3),
1954 .BR keyctl_assume_authority (3),
1955 .BR keyctl_chown (3),
1956 .BR keyctl_clear (3),
1957 .BR keyctl_describe (3),
1958 .BR keyctl_describe_alloc (3),
1959 .BR keyctl_get_keyring_ID (3),
1960 .BR keyctl_get_persistent (3),
1961 .BR keyctl_get_security (3),
1962 .BR keyctl_get_security_alloc (3),
1963 .BR keyctl_instantiate (3),
1964 .BR keyctl_instantiate_iov (3),
1965 .BR keyctl_invalidate (3),
1966 .BR keyctl_join_session_keyring (3),
1967 .BR keyctl_link (3),
1968 .BR keyctl_negate (3),
1969 .BR keyctl_read (3),
1970 .BR keyctl_read_alloc (3),
1971 .BR keyctl_reject (3),
1972 .BR keyctl_revoke (3),
1973 .BR keyctl_search (3),
1974 .BR keyctl_session_to_parent (3),
1975 .BR keyctl_set_reqkey_keyring (3),
1976 .BR keyctl_set_timeout (3),
1977 .BR keyctl_setperm (3),
1978 .BR keyctl_unlink (3),
1979 .BR keyctl_update (3),
1980 .BR recursive_key_scan (3),
1981 .BR recursive_session_key_scan (3),
1982 .BR capabilities (7),
1983 .BR credentials (7),
1984 .BR keyrings (7),
1985 .BR keyutils (7),
1986 .BR persistent\-keyring (7),
1987 .BR process\-keyring (7),
1988 .BR session\-keyring (7),
1989 .BR thread\-keyring (7),
1990 .BR user\-keyring (7),
1991 .BR user\-namespaces (7),
1992 .BR user\-session\-keyring (7),
1993 .BR request\-key (8)
1994 .PP
1995 The kernel source files
1996 .IR Documentation/security/keys.txt
1997 and
1998 .IR Documentation/security/keys\-request\-key.txt .