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 .\" SPDX-License-Identifier: GPL-2.0-or-later
7 .TH keyrings 7 (date) "Linux man-pages (unreleased)"
9 keyrings \- in-kernel key management and retention facility
11 The Linux key-management facility
12 is primarily a way for various kernel components
13 to retain or cache security data,
14 authentication keys, encryption keys, and other data in the kernel.
16 System call interfaces are provided so that user-space programs can manage
17 those objects and also use the facility for their own purposes; see
23 A library and some user-space utilities are provided to allow access to the
31 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
33 A key has the following attributes:
36 This is a unique integer handle by which a key is referred to in system calls.
37 The serial number is sometimes synonymously referred as the key ID.
38 Programmatically, key serial numbers are represented using the type
42 A key's type defines what sort of data can be held in the key,
43 how the proposed content of the key will be parsed,
44 and how the payload will be used.
46 There are a number of general-purpose types available, plus some specialist
47 types defined by specific kernel components.
50 The key description is a printable string that is used as the search term
51 for the key (in conjunction with the key type) as well as a display name.
52 During searches, the description may be partially matched or exactly matched.
55 The payload is the actual content of a key.
56 This is usually set when a key is created,
57 but it is possible for the kernel to upcall to user space to finish the
58 instantiation of a key if that key wasn't already known to the kernel
59 when it was requested.
60 For further details, see
63 A key's payload can be read and updated if the key type supports it and if
64 suitable permission is granted to the caller.
68 each key has an owning user ID, an owning group ID, and a security label.
69 Each key also has a set of permissions,
70 though there are more than for a normal UNIX file,
71 and there is an additional category\[em]possessor\[em]beyond the usual user,
76 Note that keys are quota controlled, since they require unswappable kernel
78 The owning user ID specifies whose quota is to be debited.
81 Each key can have an expiration time set.
82 When that time is reached,
83 the key is marked as being expired and accesses to it fail with the error
85 If not deleted, updated, or replaced, then, after a set amount of time,
86 an expired key is automatically removed (garbage collected)
87 along with all links to it,
88 and attempts to access the key fail with the error
92 Each key has a reference count.
93 Keys are referenced by keyrings, by currently active users,
94 and by a process's credentials.
95 When the reference count reaches zero,
96 the key is scheduled for garbage collection.
97 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
99 The kernel provides several basic types of key:
102 .\" Note that keyrings use different fields in struct key in order to store
103 .\" their data - index_key instead of type/description and name_link/keys
104 .\" instead of payload.
105 Keyrings are special keys which store a set of links
106 to other keys (including other keyrings),
107 analogous to a directory holding links to files.
108 The main purpose of a keyring is to prevent other keys from
109 being garbage collected because nothing refers to them.
111 Keyrings with descriptions (names)
112 that begin with a period (\[aq].\[aq]) are reserved to the implementation.
115 This is a general-purpose key type.
116 The key is kept entirely within kernel memory.
117 The payload may be read and updated by user-space applications.
119 The payload for keys of this type is a blob of arbitrary data
120 of up to 32,767 bytes.
122 The description may be any valid string, though it is preferred that it
123 start with a colon-delimited prefix representing the service
124 to which the key is of interest
126 .IR """afs:mykey""" ).
128 .IR """logon""" " (since Linux 3.3)"
129 .\" commit 9f6ed2ca257fa8650b876377833e6f14e272848b
130 This key type is essentially the same as
132 but it does not provide reading (i.e., the
136 meaning that the key payload is never visible from user space.
137 This is suitable for storing username-password pairs
138 that should not be readable from user space.
144 start with a non-empty colon-delimited prefix whose purpose
145 is to identify the service to which the key belongs.
146 (Note that this differs from keys of the
148 type, where the inclusion of a prefix is recommended but is not enforced.)
150 .IR """big_key""" " (since Linux 3.13)"
151 .\" commit ab3c3587f8cda9083209a61dbe3a4407d3cada10
152 This key type is similar to the
154 key type, but it may hold a payload of up to 1\ MiB in size.
155 This key type is useful for purposes such as holding Kerberos ticket caches.
157 The payload data may be stored in a tmpfs filesystem,
158 rather than in kernel memory,
159 if the data size exceeds the overhead of storing the data in the filesystem.
160 (Storing the data in a filesystem requires filesystem structures
161 to be allocated in the kernel.
162 The size of these structures determines the size threshold
163 above which the tmpfs storage method is used.)
165 .\" commit 13100a72f40f5748a04017e0ab3df4cf27c809ef
166 the payload data is encrypted when stored in tmpfs,
167 thereby preventing it from being written unencrypted into swap space.
169 There are more specialized key types available also,
170 but they aren't discussed here
171 because they aren't intended for normal user-space use.
174 that begin with a period (\[aq].\[aq]) are reserved to the implementation.
175 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
177 As previously mentioned, keyrings are a special type of key that contain
178 links to other keys (which may include other keyrings).
179 Keys may be linked to by multiple keyrings.
180 Keyrings may be considered as analogous to UNIX directories
181 where each directory contains a set of hard links to files.
183 Various operations (system calls) may be applied only to keyrings:
186 A key may be added to a keyring by system calls that create keys.
187 This prevents the new key from being immediately deleted
188 when the system call releases its last reference to the key.
191 A link may be added to a keyring pointing to a key that is already known,
192 provided this does not create a self-referential cycle.
195 A link may be removed from a keyring.
196 When the last link to a key is removed,
197 that key will be scheduled for deletion by the garbage collector.
200 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 .B 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 .B KEY_SPEC_USER_KEYRING
275 .B 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 .B 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:
371 The set specifies the rights granted
372 if the key's user ID matches the caller's filesystem user ID.
375 The set specifies the rights granted
376 if the user ID didn't match and the key's group ID matches the caller's
377 filesystem GID or one of the caller's supplementary group IDs.
380 The set specifies the rights granted
381 if neither the key's user ID nor group ID matched.
383 The fourth set of rights is:
386 The set specifies the rights granted
387 if a key is determined to be possessed by the caller.
389 The complete set of rights for a key is the union of whichever
390 of the first three sets is applicable plus the fourth set
391 if the key is possessed.
393 The set of rights that may be granted in each of the four masks
397 The attributes of the key may be read.
398 This includes the type,
399 description, and access rights (excluding the security label).
402 For a key: the payload of the key may be read.
403 For a keyring: the list of serial numbers (keys) to
404 which the keyring has links may be read.
407 The payload of the key may be updated and the key may be revoked.
408 For a keyring, links may be added to or removed from the keyring,
409 and the keyring may be cleared completely (all links are removed),
412 For a key (or a keyring): the key may be found by a search.
413 For a keyring: keys and keyrings that are linked to by the
414 keyring may be searched.
417 Links may be created from keyrings to the key.
418 The initial link to a key that is established when the key is created
419 doesn't require this permission.
422 The ownership details and security label of the key may be changed,
423 the key's expiration time may be set, and the key may be revoked.
425 In addition to access rights, any active Linux Security Module (LSM) may
426 prevent access to a key if its policy so dictates.
428 security label or other attribute by the LSM;
429 this label is retrievable via
430 .BR keyctl_get_security (3).
433 .BR keyctl_chown (3),
434 .BR keyctl_describe (3),
435 .BR keyctl_get_security (3),
436 .BR keyctl_setperm (3),
439 for more information.
440 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
441 .SS Searching for keys
442 One of the key features of the Linux key-management facility
443 is the ability to find a key that a process is retaining.
446 system call is the primary point of
447 access for user-space applications to find a key.
448 (Internally, the kernel has something similar available
449 for use by internal components that make use of keys.)
451 The search algorithm works as follows:
453 The process keyrings are searched in the following order: the
454 .BR thread\-keyring (7)
456 .BR process\-keyring (7)
457 if it exists, and then either the
458 .BR session\-keyring (7)
460 .BR user\-session\-keyring (7)
463 If the caller was a process that was invoked by the
465 upcall mechanism, then the keyrings of the original caller of
467 will be searched as well.
469 The search of a keyring tree is in breadth-first order:
470 each keyring is searched first for a match,
471 then the keyrings referred to by that keyring are searched.
473 If a matching key is found that is valid,
474 then the search terminates and that key is returned.
476 If a matching key is found that has an error state attached,
477 that error state is noted and the search continues.
479 If no valid matching key is found,
480 then the first noted error state is returned; otherwise, an
484 It is also possible to search a specific keyring, in which case only steps
490 .BR keyctl_search (3)
491 for more information.
492 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
493 .SS On-demand key creation
494 If a key cannot be found,
498 argument, create a new key and then upcall to user space to
500 This allows keys to be created on an as-needed basis.
503 this will involve the kernel creating a new process that executes the
505 program, which will then execute the appropriate handler based on its
508 The handler is passed a special authorization key that allows it
509 and only it to instantiate the new key.
510 This is also used to permit searches performed by the
511 handler program to also search the requester's keyrings.
515 .BR keyctl_assume_authority (3),
516 .BR keyctl_instantiate (3),
517 .BR keyctl_negate (3),
518 .BR keyctl_reject (3),
519 .BR request\-key (8),
521 .BR request\-key.conf (5)
522 for more information.
523 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
525 The kernel provides various
527 files that expose information about keys or define limits on key usage.
529 .IR /proc/keys " (since Linux 2.6.10)"
530 This file exposes a list of the keys for which the reading thread has
532 permission, providing various information about each key.
533 The thread need not possess the key for it to be visible in this file.
534 .\" David Howells, Dec 2016 linux-man@:
535 .\" This [The thread need not possess the key for it to be visible in
536 .\" this file.] is correct. See proc_keys_show() in security/keys/proc.c:
538 .\" rc = key_task_permission(key_ref, ctx.cred, KEY_NEED_VIEW);
542 .\"Possibly it shouldn't be, but for now it is.
545 The only keys included in the list are those that grant
547 permission to the reading process
548 (regardless of whether or not it possesses them).
549 LSM security checks are still performed,
550 and may filter out further keys that the process is not authorized to view.
552 An example of the data that one might see in this file
553 (with the columns numbered for easy reference below)
557 (1) (2) (3)(4) (5) (6) (7) (8) (9)
558 009a2028 I\-\-Q\-\-\- 1 perm 3f010000 1000 1000 user krb_ccache:primary: 12
559 1806c4ba I\-\-Q\-\-\- 1 perm 3f010000 1000 1000 keyring _pid: 2
560 25d3a08f I\-\-Q\-\-\- 1 perm 1f3f0000 1000 65534 keyring _uid_ses.1000: 1
561 28576bd8 I\-\-Q\-\-\- 3 perm 3f010000 1000 1000 keyring _krb: 1
562 2c546d21 I\-\-Q\-\-\- 190 perm 3f030000 1000 1000 keyring _ses: 2
563 30a4e0be I\-\-\-\-\-\- 4 2d 1f030000 1000 65534 keyring _persistent.1000: 1
564 32100fab I\-\-Q\-\-\- 4 perm 1f3f0000 1000 65534 keyring _uid.1000: 2
565 32a387ea I\-\-Q\-\-\- 1 perm 3f010000 1000 1000 keyring _pid: 2
566 3ce56aea I\-\-Q\-\-\- 5 perm 3f030000 1000 1000 keyring _ses: 1
569 The fields shown in each line of this file are as follows:
573 The ID (serial number) of the key, expressed in hexadecimal.
576 A set of flags describing the state of the key:
580 .\" KEY_FLAG_INSTANTIATED
581 The key has been instantiated.
585 The key has been revoked.
589 The key is dead (i.e., the key type has been unregistered).
590 .\" unregister_key_type() in the kernel source
591 (A key may be briefly in this state during garbage collection.)
594 .\" KEY_FLAG_IN_QUOTA
595 The key contributes to the user's quota.
598 .\" KEY_FLAG_USER_CONSTRUCT
599 The key is under construction via a callback to user space;
601 .BR request\-key (2).
604 .\" KEY_FLAG_NEGATIVE
605 The key is negatively instantiated.
608 .\" KEY_FLAG_INVALIDATED
609 The key has been invalidated.
613 This is a count of the number of kernel credential
614 structures that are pinning the key
615 (approximately: the number of threads and open file references
616 that refer to this key).
619 The amount of time until the key will expire,
620 expressed in human-readable form (weeks, days, hours, minutes, and seconds).
623 here means that the key is permanent (no timeout).
626 means that the key has already expired,
627 but has not yet been garbage collected.
630 The key permissions, expressed as four hexadecimal bytes containing,
631 from left to right, the possessor, user, group, and other permissions.
632 Within each byte, the permission bits are as follows:
658 The user ID of the key owner.
661 The group ID of the key.
662 The value \-1 here means that the key has no group ID;
663 this can occur in certain circumstances for keys created by the kernel.
666 The key type (user, keyring, etc.)
669 The key description (name).
670 This field contains descriptive information about the key.
671 For most key types, it has the form
681 subfield is the key's description (name).
684 field provides some further information about the key.
685 The information that appears here depends on the key type, as follows:
688 .IR """user""" " and " """logon"""
689 The size in bytes of the key payload (expressed in decimal).
692 The number of keys linked to the keyring,
695 if there are no keys linked to the keyring.
698 The payload size in bytes, followed either by the string
700 if the key payload exceeds the threshold that means that the
701 payload is stored in a (swappable)
704 or otherwise the string
706 indicating that the key is small enough to reside in kernel memory.
710 .I """.request_key_auth"""
712 (authorization key; see
713 .BR request_key (2)),
714 the description field has the form shown in the following example:
718 key:c9a9b19 pid:28880 ci:10
722 The three subfields are as follows:
726 The hexadecimal ID of the key being instantiated in the requesting program.
729 The PID of the requesting program.
732 The length of the callout data with which the requested key should
734 (i.e., the length of the payload associated with the authorization key).
738 .IR /proc/key\-users " (since Linux 2.6.10)"
739 This file lists various information for each user ID that
740 has at least one key on the system.
741 An example of the data that one might see in this file is the following:
745 0: 10 9/9 2/1000000 22/25000000
746 42: 9 9/9 8/200 106/20000
747 1000: 11 11/11 10/200 271/20000
751 The fields shown in each line are as follows:
758 This is a kernel-internal usage count for the kernel structure
759 used to record key users.
762 The total number of keys owned by the user,
763 and the number of those keys that have been instantiated.
766 The number of keys owned by the user,
767 and the maximum number of keys that the user may own.
769 .IR qnbytes / maxbytes
770 The number of bytes consumed in payloads of the keys owned by this user,
771 and the upper limit on the number of bytes in key payloads for that user.
774 .IR /proc/sys/kernel/keys/gc_delay " (since Linux 2.6.32)"
775 .\" commit 5d135440faf7db8d566de0c6fab36b16cf9cfc3b
776 The value in this file specifies the interval, in seconds,
777 after which revoked and expired keys will be garbage collected.
778 The purpose of having such an interval is so that there is a window
779 of time where user space can see an error (respectively
783 that indicates what happened to the key.
785 The default value in this file is 300 (i.e., 5 minutes).
787 .IR /proc/sys/kernel/keys/persistent_keyring_expiry " (since Linux 3.13)"
788 .\" commit f36f8c75ae2e7d4da34f4c908cebdb4aa42c977e
789 This file defines an interval, in seconds,
790 to which the persistent keyring's expiration timer is reset
791 each time the keyring is accessed (via
792 .BR keyctl_get_persistent (3)
795 .B KEYCTL_GET_PERSISTENT
798 The default value in this file is 259200 (i.e., 3 days).
800 The following files (which are writable by privileged processes)
801 are used to enforce quotas on the number of keys
802 and number of bytes of data that can be stored in key payloads:
804 .IR /proc/sys/kernel/keys/maxbytes " (since Linux 2.6.26)"
805 .\" commit 0b77f5bfb45c13e1e5142374f9d6ca75292252a4
806 .\" Previously: KEYQUOTA_MAX_BYTES 10000
807 This is the maximum number of bytes of data that a nonroot user
808 can hold in the payloads of the keys owned by the user.
810 The default value in this file is 20,000.
812 .IR /proc/sys/kernel/keys/maxkeys " (since Linux 2.6.26)"
813 .\" commit 0b77f5bfb45c13e1e5142374f9d6ca75292252a4
814 .\" Previously: KEYQUOTA_MAX_KEYS 100
815 This is the maximum number of keys that a nonroot user may own.
817 The default value in this file is 200.
819 .IR /proc/sys/kernel/keys/root_maxbytes " (since Linux 2.6.26)"
820 This is the maximum number of bytes of data that the root user
821 (UID 0 in the root user namespace)
822 can hold in the payloads of the keys owned by root.
824 .\"738c5d190f6540539a04baf36ce21d46b5da04bd
825 The default value in this file is 25,000,000 (20,000 before Linux 3.17).
826 .\" commit 0b77f5bfb45c13e1e5142374f9d6ca75292252a4
828 .IR /proc/sys/kernel/keys/root_maxkeys " (since Linux 2.6.26)"
829 .\" commit 0b77f5bfb45c13e1e5142374f9d6ca75292252a4
830 This is the maximum number of keys that the root user
831 (UID 0 in the root user namespace)
834 .\"738c5d190f6540539a04baf36ce21d46b5da04bd
835 The default value in this file is 1,000,000 (200 before Linux 3.17).
837 With respect to keyrings,
838 note that each link in a keyring consumes 4 bytes of the keyring payload.
839 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
841 The Linux key-management facility has a number of users and usages,
842 but is not limited to those that already exist.
844 In-kernel users of this facility include:
846 Network filesystems - DNS
847 The kernel uses the upcall mechanism provided by the keys to upcall to
848 user space to do DNS lookups and then to cache the results.
850 AF_RXRPC and kAFS - Authentication
851 The AF_RXRPC network protocol and the in-kernel AFS filesystem
852 use keys to store the ticket needed to do secured or encrypted traffic.
853 These are then looked up by
854 network operations on AF_RXRPC and filesystem operations on kAFS.
856 NFS - User ID mapping
857 The NFS filesystem uses keys to store mappings of
858 foreign user IDs to local user IDs.
861 The CIFS filesystem uses keys to store passwords for accessing remote shares.
864 The kernel build process can be made to cryptographically sign modules.
865 That signature is then checked when a module is loaded.
867 User-space users of this facility include:
870 The MIT Kerberos 5 facility (libkrb5) can use keys to store authentication
871 tokens which can be made to be automatically cleaned up a set time after
872 the user last uses them,
873 but until then permits them to hang around after the user
874 has logged out so that
876 scripts can use them.
877 .\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
887 .BR persistent\-keyring (7),
888 .BR process\-keyring (7),
889 .BR session\-keyring (7),
890 .BR thread\-keyring (7),
891 .BR user\-keyring (7),
892 .BR user\-session\-keyring (7),
896 The kernel source files
897 .I Documentation/crypto/asymmetric\-keys.txt
899 .I Documentation/security/keys
900 (or, before Linux 4.13, in the file
901 .IR Documentation/security/keys.txt ).