man > keyrings(7)

πŸ“› NAME

keyrings - in-kernel key management and retention facility

πŸš€ Quick Reference

Use CaseCommandDescription
Add a key to session keyringkeyctl add user mykey "data" @sCreate a user key and attach to the current session keyring
Request a keykeyctl request user mykeySearch for a key, invoking upcall if not found
Search a keyringkeyctl search @s user mykeyFind a key by type and description in a keyring
Link a key to a keyringkeyctl link @u @sLink a key (or keyring) into another keyring
Unlink a keykeyctl unlink @u @sRemove a link from a keyring
Set expiration timekeyctl timeout <key-ID> 3600Set the key to expire in 1 hour
List keys visible to youcat /proc/keysShow all keys with view permission
Show key quotascat /proc/key-usersDisplay per-UID key usage and limits

πŸ“– DESCRIPTION

The Linux key-management facility is primarily a way for various kernel components to retain or cache security data, authentication keys, encryption keys, and other data in the kernel. System call interfaces are provided so that user-space programs can manage those objects and also use the facility for their own purposes; see add_key(2), request_key(2), and keyctl(2). A library and some user-space utilities are provided to allow access to the facility. See keyctl(1), keyctl(3), and keyutils(7) for more information.

πŸ”‘ Keys

A key has the following attributes:

πŸ—‚οΈ Key types

The kernel provides several basic types of key: There are more specialized key types available also, but they aren't discussed here because they aren't intended for normal user-space use. Key type names that begin with a period ('.') are reserved to the implementation.

πŸ’ Keyrings

As previously mentioned, keyrings are a special type of key that contain links to other keys (which may include other keyrings). Keys may be linked to by multiple keyrings. Keyrings may be considered as analogous to UNIX directories where each directory contains a set of hard links to files. Various operations (system calls) may be applied only to keyrings: See keyctl_clear(3), keyctl_link(3), keyctl_search(3), and keyctl_unlink(3) for more information.

βš“ Anchoring keys

To prevent a key from being garbage collected, it must be anchored to keep its reference count elevated when it is not in active use by the kernel. Keyrings are used to anchor other keys: each link is a reference on a key. Note that keyrings themselves are just keys and are also subject to the same anchoring requirement to prevent them being garbage collected. The kernel makes available a number of anchor keyrings. Note that some of these keyrings will be created only when first accessed.

🏭 Process keyrings

Process credentials themselves reference keyrings with specific semantics. These keyrings are pinned as long as the set of credentials exists, which is usually as long as the process exists. There are three keyrings with different inheritance/sharing rules: the ses‐ sion-keyring(7) (inherited and shared by all child processes), the process-keyring(7) (shared by all threads in a process) and the thread-keyring(7) (specific to a particu‐ lar thread). As an alternative to using the actual keyring IDs, in calls to add_key(2), keyctl(2), and request_key(2), the special keyring values KEY_SPEC_SESSION_KEYRING, KEY_SPEC_PROCESS_KEYRING, and KEY_SPEC_THREAD_KEYRING can be used to refer to the caller's own instances of these keyrings.

πŸ‘€ User keyrings

Each UID known to the kernel has a record that contains two keyrings: the user-keyring(7) and the user-session-keyring(7). These exist for as long as the UID record in the kernel exists. As an alternative to using the actual keyring IDs, in calls to add_key(2), keyctl(2), and request_key(2), the special keyring values KEY_SPEC_USER_KEYRING and KEY_SPEC_USER_SESSION_KEYRING can be used to refer to the caller's own instances of these keyrings. A link to the user keyring is placed in a new session keyring by pam_keyinit(8) when a new login session is initiated.

πŸ’Ύ Persistent keyrings

There is a persistent-keyring(7) available to each UID known to the system. It may persist beyond the life of the UID record previously mentioned, but has an expiration time set such that it is automatically cleaned up after a set time. The persistent keyring permits, for example, cron(8) scripts to use credentials that are left in the persistent keyring after the user logs out. Note that the expiration time of the persistent keyring is reset every time the per‐ sistent key is requested.

πŸ›‘οΈ Special keyrings

There are special keyrings owned by the kernel that can anchor keys for special pur‐ poses. An example of this is the system keyring used for holding encryption keys for module signature verification. These special keyrings are usually closed to direct alteration by user space. An originally planned "group keyring", for storing keys associated with each GID known to the kernel, is not so far implemented, is unlikely to be implemented. Nevertheless, the constant KEY_SPEC_GROUP_KEYRING has been defined for this keyring.

🀲 Possession

The concept of possession is important to understanding the keyrings security model. Whether a thread possesses a key is determined by the following rules:
  1. Any key or keyring that does not grant search permission to the caller is ignored in all the following rules.
  2. A thread possesses its session-keyring(7), process-keyring(7), and thread-keyring(7) di‐ rectly because those keyrings are referred to by its credentials.
  3. If a keyring is possessed, then any key it links to is also possessed.
  4. If any key a keyring links to is itself a keyring, then rule (3) applies recursively.
  5. If a process is upcalled from the kernel to instantiate a key (see request_key(2)), then it also possesses the requester's keyrings as in rule (1) as if it were the requester.
Note that possession is not a fundamental property of a key, but must rather be calculated each time the key is needed. Possession is designed to allow set-user-ID programs run from, say a user's shell to access the user's keys. Granting permissions to the key possessor while denying them to the key owner and group allows the prevention of access to keys on the basis of UID and GID matches. When it creates the session keyring, pam_keyinit(8) adds a link to the user-keyring(7), thus making the user keyring and anything it contains possessed by default.

πŸ”‘ Access rights

Each key has the following security-related attributes: The permissions mask contains four sets of rights. The first three sets are mutually exclu‐ sive. One and only one will be in force for a particular access check. In order of descend‐ ing priority, these three sets are: The fourth set of rights is: The complete set of rights for a key is the union of whichever of the first three sets is ap‐ plicable plus the fourth set if the key is possessed. The set of rights that may be granted in each of the four masks is as follows: In addition to access rights, any active Linux Security Module (LSM) may prevent access to a key if its policy so dictates. A key may be given a security label or other attribute by the LSM; this label is retrievable via keyctl_get_security(3). See keyctl_chown(3), keyctl_describe(3), keyctl_get_security(3), keyctl_setperm(3), and selinux(8) for more information.

πŸ” Searching for keys

One of the key features of the Linux key-management facility is the ability to find a key that a process is retaining. The request_key(2) system call is the primary point of access for user-space applications to find a key. (Internally, the kernel has something similar available for use by internal components that make use of keys.) The search algorithm works as follows:
  1. The process keyrings are searched in the following order: the thread thread-keyring(7) if it exists, the process-keyring(7) if it exists, and then either the session-keyring(7) if it exists or the user-session-keyring(7) if that exists.
  2. If the caller was a process that was invoked by the request_key(2) upcall mechanism, then the keyrings of the original caller of request_key(2) will be searched as well.
  3. The search of a keyring tree is in breadth-first order: each keyring is searched first for a match, then the keyrings referred to by that keyring are searched.
  4. If a matching key is found that is valid, then the search terminates and that key is re‐ turned.
  5. If a matching key is found that has an error state attached, that error state is noted and the search continues.
  6. If no valid matching key is found, then the first noted error state is returned; other‐ wise, an ENOKEY error is returned.
It is also possible to search a specific keyring, in which case only steps (3) to (6) apply. See request_key(2) and keyctl_search(3) for more information.

⚑ On-demand key creation

If a key cannot be found, request_key(2) will, if given a callout_info argument, create a new key and then upcall to user space to instantiate the key. This allows keys to be created on an as-needed basis. Typically, this will involve the kernel creating a new process that executes the re‐ quest-key(8) program, which will then execute the appropriate handler based on its configura‐ tion. The handler is passed a special authorization key that allows it and only it to instantiate the new key. This is also used to permit searches performed by the handler program to also search the requester's keyrings. See request_key(2), keyctl_assume_authority(3), keyctl_instantiate(3), keyctl_negate(3), keyctl_reject(3), request-key(8), and request-key.conf(5) for more information.

πŸ“ /proc files

The kernel provides various /proc files that expose information about keys or define limits on key usage.

πŸ“‹ /proc/keys (since Linux 2.6.10)

This file exposes a list of the keys for which the reading thread has view permission, providing various information about each key. The thread need not possess the key for it to be visible in this file. The only keys included in the list are those that grant view permission to the reading process (regardless of whether or not it possesses them). LSM security checks are still performed, and may filter out further keys that the process is not authorized to view. An example of the data that one might see in this file (with the columns numbered for easy reference below) is the following:
(1)     (2)     (3)(4)    (5)     (6)   (7)   (8)        (9)
009a2028 I--Q---   1 perm 3f010000  1000  1000 user     krb_ccache:primary: 12
1806c4ba I--Q---   1 perm 3f010000  1000  1000 keyring  _pid: 2
25d3a08f I--Q---   1 perm 1f3f0000  1000 65534 keyring  _uid_ses.1000: 1
28576bd8 I--Q---   3 perm 3f010000  1000  1000 keyring  _krb: 1
2c546d21 I--Q--- 190 perm 3f030000  1000  1000 keyring  _ses: 2
30a4e0be I------   4   2d 1f030000  1000 65534 keyring  _persistent.1000: 1
32100fab I--Q---   4 perm 1f3f0000  1000 65534 keyring  _uid.1000: 2
32a387ea I--Q---   1 perm 3f010000  1000  1000 keyring  _pid: 2
3ce56aea I--Q---   5 perm 3f030000  1000  1000 keyring  _ses: 1
The fields shown in each line of this file are as follows:

πŸ‘₯ /proc/key-users (since Linux 2.6.10)

This file lists various information for each user ID that has at least one key on the system. An example of the data that one might see in this file is the following:
0:    10 9/9 2/1000000 22/25000000
42:     9 9/9 8/200 106/20000
1000:    11 11/11 10/200 271/20000
The fields shown in each line are as follows:

🧹 /proc/sys/kernel/keys/gc_delay (since Linux 2.6.32)

The value in this file specifies the interval, in seconds, after which revoked and ex‐ pired keys will be garbage collected. The purpose of having such an interval is so that there is a window of time where user space can see an error (respectively EKEYRE‐ VOKED and EKEYEXPIRED) that indicates what happened to the key. The default value in this file is 300 (i.e., 5 minutes).

⏳ /proc/sys/kernel/keys/persistent_keyring_expiry (since Linux 3.13)

This file defines an interval, in seconds, to which the persistent keyring's expira‐ tion timer is reset each time the keyring is accessed (via keyctl_get_persistent(3) or the keyctl(2) KEYCTL_GET_PERSISTENT operation.) The default value in this file is 259200 (i.e., 3 days). The following files (which are writable by privileged processes) are used to enforce quotas on the number of keys and number of bytes of data that can be stored in key payloads:

πŸ“¦ /proc/sys/kernel/keys/maxbytes (since Linux 2.6.26)

This is the maximum number of bytes of data that a nonroot user can hold in the payloads of the keys owned by the user. The default value in this file is 20,000.

πŸ”’ /proc/sys/kernel/keys/maxkeys (since Linux 2.6.26)

This is the maximum number of keys that a nonroot user may own. The default value in this file is 200.

πŸ‘‘ /proc/sys/kernel/keys/root_maxbytes (since Linux 2.6.26)

This is the maximum number of bytes of data that the root user (UID 0 in the root user namespace) can hold in the payloads of the keys owned by root. The default value in this file is 25,000,000 (20,000 before Linux 3.17).

πŸ‘‘πŸ”’ /proc/sys/kernel/keys/root_maxkeys (since Linux 2.6.26)

This is the maximum number of keys that the root user (UID 0 in the root user namespace) may own. The default value in this file is 1,000,000 (200 before Linux 3.17). With respect to keyrings, note that each link in a keyring consumes 4 bytes of the keyring payload.

πŸ‘₯ Users

The Linux key-management facility has a number of users and usages, but is not limited to those that already exist.

🐧 In-kernel users

πŸ–₯️ User-space users

πŸ”— SEE ALSO

keyctl(1), add_key(2), keyctl(2), request_key(2), keyctl(3), keyutils(7), persistent-keyring(7), process-keyring(7), session-keyring(7), thread-keyring(7), user-keyring(7), user-session-keyring(7), pam_keyinit(8), request-key(8) The kernel source files Documentation/crypto/asymmetric-keys.txt and under Documentation/security/keys (or, before Linux 4.13, in the file Documentation/security/keys.txt).

πŸ“œ COLOPHON

This page is part of release 5.10 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/.
keyrings(7)
πŸ“› NAME πŸš€ Quick Reference πŸ“– DESCRIPTION
πŸ”‘ Keys πŸ—‚οΈ Key types πŸ’ Keyrings βš“ Anchoring keys 🀲 Possession πŸ”‘ Access rights πŸ” Searching for keys ⚑ On-demand key creation πŸ“ /proc files πŸ‘₯ Users
πŸ”— SEE ALSO πŸ“œ COLOPHON

Generated by phpman v4.9.27 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-18 20:44 @2600:1f28:365:80b0:b91e:58eb:6587:15c8
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
Valid XHTML 1.0 Transitional!Valid CSS!
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format

^_top_^