1 .\" Copyright (C) 2014 Red Hat, Inc. All Rights Reserved.
2 .\" Written by David Howells (dhowells@redhat.com)
3 .\" and Copyright (C) 2016 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\" %%%LICENSE_START(GPLv2+_SW_ONEPARA)
6 .\" This program is free software; you can redistribute it and/or
7 .\" modify it under the terms of the GNU General Public License
8 .\" as published by the Free Software Foundation; either version
9 .\" 2 of the License, or (at your option) any later version.
12 .TH KEYRINGS 7 2021-03-22 Linux "Linux Programmer's Manual"
14 keyrings \- in-kernel key management and retention facility
16 The Linux key-management facility
17 is primarily a way for various kernel components
18 to retain or cache security data,
19 authentication keys, encryption keys, and other data in the kernel.
21 System call interfaces are provided so that user-space programs can manage
22 those objects and also use the facility for their own purposes; see
28 A library and some user-space utilities are provided to allow access to the
36 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
38 A key has the following attributes:
41 This is a unique integer handle by which a key is referred to in system calls.
42 The serial number is sometimes synonymously referred as the key ID.
43 Programmatically, key serial numbers are represented using the type
47 A key's type defines what sort of data can be held in the key,
48 how the proposed content of the key will be parsed,
49 and how the payload will be used.
51 There are a number of general-purpose types available, plus some specialist
52 types defined by specific kernel components.
55 The key description is a printable string that is used as the search term
56 for the key (in conjunction with the key type) as well as a display name.
57 During searches, the description may be partially matched or exactly matched.
60 The payload is the actual content of a key.
61 This is usually set when a key is created,
62 but it is possible for the kernel to upcall to user space to finish the
63 instantiation of a key if that key wasn't already known to the kernel
64 when it was requested.
65 For further details, see
68 A key's payload can be read and updated if the key type supports it and if
69 suitable permission is granted to the caller.
73 each key has an owning user ID, an owning group ID, and a security label.
74 Each key also has a set of permissions,
75 though there are more than for a normal UNIX file,
76 and there is an additional category\(empossessor\(embeyond the usual user,
81 Note that keys are quota controlled, since they require unswappable kernel
83 The owning user ID specifies whose quota is to be debited.
86 Each key can have an expiration time set.
87 When that time is reached,
88 the key is marked as being expired and accesses to it fail with the error
90 If not deleted, updated, or replaced, then, after a set amount of time,
91 an expired key is automatically removed (garbage collected)
92 along with all links to it,
93 and attempts to access the key fail with the error
97 Each key has a reference count.
98 Keys are referenced by keyrings, by currently active users,
99 and by a process's credentials.
100 When the reference count reaches zero,
101 the key is scheduled for garbage collection.
102 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
104 The kernel provides several basic types of key:
107 .\" Note that keyrings use different fields in struct key in order to store
108 .\" their data - index_key instead of type/description and name_link/keys
109 .\" instead of payload.
110 Keyrings are special keys which store a set of links
111 to other keys (including other keyrings),
112 analogous to a directory holding links to files.
113 The main purpose of a keyring is to prevent other keys from
114 being garbage collected because nothing refers to them.
116 Keyrings with descriptions (names)
117 that begin with a period (\(aq.\(aq) are reserved to the implementation.
120 This is a general-purpose key type.
121 The key is kept entirely within kernel memory.
122 The payload may be read and updated by user-space applications.
124 The payload for keys of this type is a blob of arbitrary data
125 of up to 32,767 bytes.
127 The description may be any valid string, though it is preferred that it
128 start with a colon-delimited prefix representing the service
129 to which the key is of interest
131 .IR """afs:mykey""" ).
133 .IR """logon""" " (since Linux 3.3)"
134 .\" commit 9f6ed2ca257fa8650b876377833e6f14e272848b
135 This key type is essentially the same as
137 but it does not provide reading (i.e., the
141 meaning that the key payload is never visible from user space.
142 This is suitable for storing username-password pairs
143 that should not be readable from user space.
149 start with a non-empty colon-delimited prefix whose purpose
150 is to identify the service to which the key belongs.
151 (Note that this differs from keys of the
153 type, where the inclusion of a prefix is recommended but is not enforced.)
155 .IR """big_key""" " (since Linux 3.13)"
156 .\" commit ab3c3587f8cda9083209a61dbe3a4407d3cada10
157 This key type is similar to the
159 key type, but it may hold a payload of up to 1\ MiB in size.
160 This key type is useful for purposes such as holding Kerberos ticket caches.
162 The payload data may be stored in a tmpfs filesystem,
163 rather than in kernel memory,
164 if the data size exceeds the overhead of storing the data in the filesystem.
165 (Storing the data in a filesystem requires filesystem structures
166 to be allocated in the kernel.
167 The size of these structures determines the size threshold
168 above which the tmpfs storage method is used.)
170 .\" commit 13100a72f40f5748a04017e0ab3df4cf27c809ef
171 the payload data is encrypted when stored in tmpfs,
172 thereby preventing it from being written unencrypted into swap space.
174 There are more specialized key types available also,
175 but they aren't discussed here
176 because they aren't intended for normal user-space use.
179 that begin with a period (\(aq.\(aq) are reserved to the implementation.
180 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
182 As previously mentioned, keyrings are a special type of key that contain
183 links to other keys (which may include other keyrings).
184 Keys may be linked to by multiple keyrings.
185 Keyrings may be considered as analogous to UNIX directories
186 where each directory contains a set of hard links to files.
188 Various operations (system calls) may be applied only to keyrings:
190 A key may be added to a keyring by system calls that create keys.
191 This prevents the new key from being immediately deleted
192 when the system call releases its last reference to the key.
194 A link may be added to a keyring pointing to a key that is already known,
195 provided this does not create a self-referential cycle.
197 A link may be removed from a keyring.
198 When the last link to a key is removed,
199 that key will be scheduled for deletion by the garbage collector.
201 All the links may be removed from a keyring.
203 A keyring may be considered the root of a tree or subtree in which keyrings
204 form the branches and non-keyrings the leaves.
205 This tree may be searched for a key matching
206 a particular type and description.
209 .BR keyctl_clear (3),
211 .BR keyctl_search (3),
213 .BR keyctl_unlink (3)
214 for more information.
215 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
217 To prevent a key from being garbage collected,
218 it must be anchored to keep its reference count elevated
219 when it is not in active use by the kernel.
221 Keyrings are used to anchor other keys:
222 each link is a reference on a key.
223 Note that keyrings themselves are just keys and
224 are also subject to the same anchoring requirement to prevent
225 them being garbage collected.
227 The kernel makes available a number of anchor keyrings.
228 Note that some of these keyrings will be created only when first accessed.
231 Process credentials themselves reference keyrings with specific semantics.
232 These keyrings are pinned as long as the set of credentials exists,
233 which is usually as long as the process exists.
235 There are three keyrings with different inheritance/sharing rules:
237 .BR session\-keyring (7)
238 (inherited and shared by all child processes),
240 .BR process\-keyring (7)
241 (shared by all threads in a process) and
243 .BR thread\-keyring (7)
244 (specific to a particular thread).
246 As an alternative to using the actual keyring IDs,
252 the special keyring values
253 .BR KEY_SPEC_SESSION_KEYRING ,
254 .BR KEY_SPEC_PROCESS_KEYRING ,
256 .BR KEY_SPEC_THREAD_KEYRING
257 can be used to refer to the caller's own instances of these keyrings.
260 Each UID known to the kernel has a record that contains two keyrings: the
261 .BR user\-keyring (7)
263 .BR user\-session\-keyring (7).
264 These exist for as long as the UID record in the kernel exists.
266 As an alternative to using the actual keyring IDs,
272 the special keyring values
273 .BR KEY_SPEC_USER_KEYRING
275 .BR KEY_SPEC_USER_SESSION_KEYRING
276 can be used to refer to the caller's own instances of these keyrings.
278 A link to the user keyring is placed in a new session keyring by
280 when a new login session is initiated.
284 .BR persistent\-keyring (7)
285 available to each UID known to the system.
286 It may persist beyond the life of the UID record previously mentioned,
287 but has an expiration time set such that it is automatically cleaned up
289 The persistent keyring permits, for example,
291 scripts to use credentials that are left in the persistent keyring after
294 Note that the expiration time of the persistent keyring
295 is reset every time the persistent key is requested.
298 There are special keyrings owned by the kernel that can anchor keys
299 for special purposes.
300 An example of this is the \fIsystem keyring\fR used for holding
301 encryption keys for module signature verification.
303 These special keyrings are usually closed to direct alteration
306 An originally planned "group keyring",
307 for storing keys associated with each GID known to the kernel,
308 is not so far implemented, is unlikely to be implemented.
309 Nevertheless, the constant
310 .BR KEY_SPEC_GROUP_KEYRING
311 has been defined for this keyring.
312 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
314 The concept of possession is important to understanding the keyrings
316 Whether a thread possesses a key is determined by the following rules:
318 Any key or keyring that does not grant
320 permission to the caller is ignored in all the following rules.
322 A thread possesses its
323 .BR session\-keyring (7),
324 .BR process\-keyring (7),
326 .BR thread\-keyring (7)
327 directly because those keyrings are referred to by its credentials.
329 If a keyring is possessed, then any key it links to is also possessed.
331 If any key a keyring links to is itself a keyring, then rule (3) applies
334 If a process is upcalled from the kernel to instantiate a key (see
335 .BR request_key (2)),
336 then it also possesses the requester's keyrings as in
337 rule (1) as if it were the requester.
339 Note that possession is not a fundamental property of a key,
340 but must rather be calculated each time the key is needed.
342 Possession is designed to allow set-user-ID programs run from, say
343 a user's shell to access the user's keys.
344 Granting permissions to the key possessor while denying them
345 to the key owner and group allows the prevention of access to keys
346 on the basis of UID and GID matches.
348 When it creates the session keyring,
351 .BR user\-keyring (7),
352 thus making the user keyring and anything it contains possessed by default.
353 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
355 Each key has the following security-related attributes:
359 The ID of a group that is permitted to access the key
365 The permissions mask contains four sets of rights.
366 The first three sets are mutually exclusive.
367 One and only one will be in force for a particular access check.
368 In order of descending priority, these three sets are:
370 The set specifies the rights granted
371 if the key's user ID matches the caller's filesystem user ID.
373 The set specifies the rights granted
374 if the user ID didn't match and the key's group ID matches the caller's
375 filesystem GID or one of the caller's supplementary group IDs.
377 The set specifies the rights granted
378 if neither the key's user ID nor group ID matched.
380 The fourth set of rights is:
382 The set specifies the rights granted
383 if a key is determined to be possessed by the caller.
385 The complete set of rights for a key is the union of whichever
386 of the first three sets is applicable plus the fourth set
387 if the key is possessed.
389 The set of rights that may be granted in each of the four masks
393 The attributes of the key may be read.
394 This includes the type,
395 description, and access rights (excluding the security label).
398 For a key: the payload of the key may be read.
399 For a keyring: the list of serial numbers (keys) to
400 which the keyring has links may be read.
403 The payload of the key may be updated and the key may be revoked.
404 For a keyring, links may be added to or removed from the keyring,
405 and the keyring may be cleared completely (all links are removed),
408 For a key (or a keyring): the key may be found by a search.
409 For a keyring: keys and keyrings that are linked to by the
410 keyring may be searched.
413 Links may be created from keyrings to the key.
414 The initial link to a key that is established when the key is created
415 doesn't require this permission.
418 The ownership details and security label of the key may be changed,
419 the key's expiration time may be set, and the key may be revoked.
421 In addition to access rights, any active Linux Security Module (LSM) may
422 prevent access to a key if its policy so dictates.
424 security label or other attribute by the LSM;
425 this label is retrievable via
426 .BR keyctl_get_security (3).
429 .BR keyctl_chown (3),
430 .BR keyctl_describe (3),
431 .BR keyctl_get_security (3),
432 .BR keyctl_setperm (3),
435 for more information.
436 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
437 .SS Searching for keys
438 One of the key features of the Linux key-management facility
439 is the ability to find a key that a process is retaining.
442 system call is the primary point of
443 access for user-space applications to find a key.
444 (Internally, the kernel has something similar available
445 for use by internal components that make use of keys.)
447 The search algorithm works as follows:
449 The process keyrings are searched in the following order: the thread
450 .BR thread\-keyring (7)
452 .BR process\-keyring (7)
453 if it exists, and then either the
454 .BR session\-keyring (7)
456 .BR user\-session\-keyring (7)
459 If the caller was a process that was invoked by the
461 upcall mechanism, then the keyrings of the original caller of
463 will be searched as well.
465 The search of a keyring tree is in breadth-first order:
466 each keyring is searched first for a match,
467 then the keyrings referred to by that keyring are searched.
469 If a matching key is found that is valid,
470 then the search terminates and that key is returned.
472 If a matching key is found that has an error state attached,
473 that error state is noted and the search continues.
475 If no valid matching key is found,
476 then the first noted error state is returned; otherwise, an
480 It is also possible to search a specific keyring, in which case only steps
486 .BR keyctl_search (3)
487 for more information.
488 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
489 .SS On-demand key creation
490 If a key cannot be found,
494 argument, create a new key and then upcall to user space to
496 This allows keys to be created on an as-needed basis.
499 this will involve the kernel creating a new process that executes the
501 program, which will then execute the appropriate handler based on its
504 The handler is passed a special authorization key that allows it
505 and only it to instantiate the new key.
506 This is also used to permit searches performed by the
507 handler program to also search the requester's keyrings.
511 .BR keyctl_assume_authority (3),
512 .BR keyctl_instantiate (3),
513 .BR keyctl_negate (3),
514 .BR keyctl_reject (3),
515 .BR request\-key (8),
517 .BR request\-key.conf (5)
518 for more information.
519 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
521 The kernel provides various
523 files that expose information about keys or define limits on key usage.
525 .IR /proc/keys " (since Linux 2.6.10)"
526 This file exposes a list of the keys for which the reading thread has
528 permission, providing various information about each key.
529 The thread need not possess the key for it to be visible in this file.
530 .\" David Howells, Dec 2016 linux-man@:
531 .\" This [The thread need not possess the key for it to be visible in
532 .\" this file.] is correct. See proc_keys_show() in security/keys/proc.c:
534 .\" rc = key_task_permission(key_ref, ctx.cred, KEY_NEED_VIEW);
538 .\"Possibly it shouldn't be, but for now it is.
541 The only keys included in the list are those that grant
543 permission to the reading process
544 (regardless of whether or not it possesses them).
545 LSM security checks are still performed,
546 and may filter out further keys that the process is not authorized to view.
548 An example of the data that one might see in this file
549 (with the columns numbered for easy reference below)
553 (1) (2) (3)(4) (5) (6) (7) (8) (9)
554 009a2028 I\-\-Q\-\-\- 1 perm 3f010000 1000 1000 user krb_ccache:primary: 12
555 1806c4ba I\-\-Q\-\-\- 1 perm 3f010000 1000 1000 keyring _pid: 2
556 25d3a08f I\-\-Q\-\-\- 1 perm 1f3f0000 1000 65534 keyring _uid_ses.1000: 1
557 28576bd8 I\-\-Q\-\-\- 3 perm 3f010000 1000 1000 keyring _krb: 1
558 2c546d21 I\-\-Q\-\-\- 190 perm 3f030000 1000 1000 keyring _ses: 2
559 30a4e0be I\-\-\-\-\-\- 4 2d 1f030000 1000 65534 keyring _persistent.1000: 1
560 32100fab I\-\-Q\-\-\- 4 perm 1f3f0000 1000 65534 keyring _uid.1000: 2
561 32a387ea I\-\-Q\-\-\- 1 perm 3f010000 1000 1000 keyring _pid: 2
562 3ce56aea I\-\-Q\-\-\- 5 perm 3f030000 1000 1000 keyring _ses: 1
565 The fields shown in each line of this file are as follows:
569 The ID (serial number) of the key, expressed in hexadecimal.
572 A set of flags describing the state of the key:
575 .\" KEY_FLAG_INSTANTIATED
576 The key has been instantiated.
579 The key has been revoked.
582 The key is dead (i.e., the key type has been unregistered).
583 .\" unregister_key_type() in the kernel source
584 (A key may be briefly in this state during garbage collection.)
586 .\" KEY_FLAG_IN_QUOTA
587 The key contributes to the user's quota.
589 .\" KEY_FLAG_USER_CONSTRUCT
590 The key is under construction via a callback to user space;
592 .BR request\-key (2).
594 .\" KEY_FLAG_NEGATIVE
595 The key is negatively instantiated.
597 .\" KEY_FLAG_INVALIDATED
598 The key has been invalidated.
602 This is a count of the number of kernel credential
603 structures that are pinning the key
604 (approximately: the number of threads and open file references
605 that refer to this key).
608 The amount of time until the key will expire,
609 expressed in human-readable form (weeks, days, hours, minutes, and seconds).
612 here means that the key is permanent (no timeout).
615 means that the key has already expired,
616 but has not yet been garbage collected.
619 The key permissions, expressed as four hexadecimal bytes containing,
620 from left to right, the possessor, user, group, and other permissions.
621 Within each byte, the permission bits are as follows:
647 The user ID of the key owner.
650 The group ID of the key.
651 The value \-1 here means that the key has no group ID;
652 this can occur in certain circumstances for keys created by the kernel.
655 The key type (user, keyring, etc.)
658 The key description (name).
659 This field contains descriptive information about the key.
660 For most key types, it has the form
666 subfield is the key's description (name).
669 field provides some further information about the key.
670 The information that appears here depends on the key type, as follows:
673 .IR """user""" " and " """logon"""
674 The size in bytes of the key payload (expressed in decimal).
677 The number of keys linked to the keyring,
680 if there are no keys linked to the keyring.
683 The payload size in bytes, followed either by the string
685 if the key payload exceeds the threshold that means that the
686 payload is stored in a (swappable)
689 or otherwise the string
691 indicating that the key is small enough to reside in kernel memory.
695 .IR """.request_key_auth"""
697 (authorization key; see
698 .BR request_key (2)),
699 the description field has the form shown in the following example:
701 key:c9a9b19 pid:28880 ci:10
703 The three subfields are as follows:
707 The hexadecimal ID of the key being instantiated in the requesting program.
710 The PID of the requesting program.
713 The length of the callout data with which the requested key should
715 (i.e., the length of the payload associated with the authorization key).
719 .IR /proc/key\-users " (since Linux 2.6.10)"
720 This file lists various information for each user ID that
721 has at least one key on the system.
722 An example of the data that one might see in this file is the following:
726 0: 10 9/9 2/1000000 22/25000000
727 42: 9 9/9 8/200 106/20000
728 1000: 11 11/11 10/200 271/20000
732 The fields shown in each line are as follows:
739 This is a kernel-internal usage count for the kernel structure
740 used to record key users.
743 The total number of keys owned by the user,
744 and the number of those keys that have been instantiated.
747 The number of keys owned by the user,
748 and the maximum number of keys that the user may own.
750 .IR qnbytes / maxbytes
751 The number of bytes consumed in payloads of the keys owned by this user,
752 and the upper limit on the number of bytes in key payloads for that user.
755 .IR /proc/sys/kernel/keys/gc_delay " (since Linux 2.6.32)"
756 .\" commit 5d135440faf7db8d566de0c6fab36b16cf9cfc3b
757 The value in this file specifies the interval, in seconds,
758 after which revoked and expired keys will be garbage collected.
759 The purpose of having such an interval is so that there is a window
760 of time where user space can see an error (respectively
764 that indicates what happened to the key.
766 The default value in this file is 300 (i.e., 5 minutes).
768 .IR /proc/sys/kernel/keys/persistent_keyring_expiry " (since Linux 3.13)"
769 .\" commit f36f8c75ae2e7d4da34f4c908cebdb4aa42c977e
770 This file defines an interval, in seconds,
771 to which the persistent keyring's expiration timer is reset
772 each time the keyring is accessed (via
773 .BR keyctl_get_persistent (3)
776 .B KEYCTL_GET_PERSISTENT
779 The default value in this file is 259200 (i.e., 3 days).
781 The following files (which are writable by privileged processes)
782 are used to enforce quotas on the number of keys
783 and number of bytes of data that can be stored in key payloads:
785 .IR /proc/sys/kernel/keys/maxbytes " (since Linux 2.6.26)"
786 .\" commit 0b77f5bfb45c13e1e5142374f9d6ca75292252a4
787 .\" Previously: KEYQUOTA_MAX_BYTES 10000
788 This is the maximum number of bytes of data that a nonroot user
789 can hold in the payloads of the keys owned by the user.
791 The default value in this file is 20,000.
793 .IR /proc/sys/kernel/keys/maxkeys " (since Linux 2.6.26)"
794 .\" commit 0b77f5bfb45c13e1e5142374f9d6ca75292252a4
795 .\" Previously: KEYQUOTA_MAX_KEYS 100
796 This is the maximum number of keys that a nonroot user may own.
798 The default value in this file is 200.
800 .IR /proc/sys/kernel/keys/root_maxbytes " (since Linux 2.6.26)"
801 This is the maximum number of bytes of data that the root user
802 (UID 0 in the root user namespace)
803 can hold in the payloads of the keys owned by root.
805 .\"738c5d190f6540539a04baf36ce21d46b5da04bd
806 The default value in this file is 25,000,000 (20,000 before Linux 3.17).
807 .\" commit 0b77f5bfb45c13e1e5142374f9d6ca75292252a4
809 .IR /proc/sys/kernel/keys/root_maxkeys " (since Linux 2.6.26)"
810 .\" commit 0b77f5bfb45c13e1e5142374f9d6ca75292252a4
811 This is the maximum number of keys that the root user
812 (UID 0 in the root user namespace)
815 .\"738c5d190f6540539a04baf36ce21d46b5da04bd
816 The default value in this file is 1,000,000 (200 before Linux 3.17).
818 With respect to keyrings,
819 note that each link in a keyring consumes 4 bytes of the keyring payload.
820 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
822 The Linux key-management facility has a number of users and usages,
823 but is not limited to those that already exist.
825 In-kernel users of this facility include:
827 Network filesystems - DNS
828 The kernel uses the upcall mechanism provided by the keys to upcall to
829 user space to do DNS lookups and then to cache the results.
831 AF_RXRPC and kAFS - Authentication
832 The AF_RXRPC network protocol and the in-kernel AFS filesystem
833 use keys to store the ticket needed to do secured or encrypted traffic.
834 These are then looked up by
835 network operations on AF_RXRPC and filesystem operations on kAFS.
837 NFS - User ID mapping
838 The NFS filesystem uses keys to store mappings of
839 foreign user IDs to local user IDs.
842 The CIFS filesystem uses keys to store passwords for accessing remote shares.
845 The kernel build process can be made to cryptographically sign modules.
846 That signature is then checked when a module is loaded.
848 User-space users of this facility include:
851 The MIT Kerberos 5 facility (libkrb5) can use keys to store authentication
852 tokens which can be made to be automatically cleaned up a set time after
853 the user last uses them,
854 but until then permits them to hang around after the user
855 has logged out so that
857 scripts can use them.
858 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
868 .BR persistent\-keyring (7),
869 .BR process\-keyring (7),
870 .BR session\-keyring (7),
871 .BR thread\-keyring (7),
872 .BR user\-keyring (7),
873 .BR user\-session\-keyring (7),
877 The kernel source files
878 .IR Documentation/crypto/asymmetric\-keys.txt
880 .IR Documentation/security/keys
881 (or, before Linux 4.13, in the file
882 .IR Documentation/security/keys.txt ).