Kernel Key Retention Service — The Linux Kernel documentation (2024)

This service allows cryptographic keys, authentication tokens, cross-domainuser mappings, and similar to be cached in the kernel for the use offilesystems and other kernel services.

Keyrings are permitted; these are a special type of key that can hold links toother keys. Processes each have three standard keyring subscriptions that akernel service can search for relevant keys.

The key service can be configured on by enabling:

"Security options"/"Enable access key retention support" (CONFIG_KEYS)

This document has the following sections:

  • Key Overview

  • Key Service Overview

  • Key Access Permissions

  • SELinux Support

  • New ProcFS Files

  • Userspace System Call Interface

  • Kernel Services

  • Notes On Accessing Payload Contents

  • Defining a Key Type

  • Request-Key Callback Service

  • Garbage Collection

Key Overview

In this context, keys represent units of cryptographic data, authenticationtokens, keyrings, etc.. These are represented in the kernel by struct key.

Each key has a number of attributes:

  • A serial number.

  • A type.

  • A description (for matching a key in a search).

  • Access control information.

  • An expiry time.

  • A payload.

  • State.

  • Each key is issued a serial number of type key_serial_t that is unique forthe lifetime of that key. All serial numbers are positive non-zero 32-bitintegers.

    Userspace programs can use a key's serial numbers as a way to gain accessto it, subject to permission checking.

  • Each key is of a defined "type". Types must be registered inside thekernel by a kernel service (such as a filesystem) before keys of that typecan be added or used. Userspace programs cannot define new types directly.

    Key types are represented in the kernel by struct key_type. This defines anumber of operations that can be performed on a key of that type.

    Should a type be removed from the system, all the keys of that type willbe invalidated.

  • Each key has a description. This should be a printable string. The keytype provides an operation to perform a match between the description on akey and a criterion string.

  • Each key has an owner user ID, a group ID and a permissions mask. Theseare used to control what a process may do to a key from userspace, andwhether a kernel service will be able to find the key.

  • Each key can be set to expire at a specific time by the key type'sinstantiation function. Keys can also be immortal.

  • Each key can have a payload. This is a quantity of data that represent theactual "key". In the case of a keyring, this is a list of keys to whichthe keyring links; in the case of a user-defined key, it's an arbitraryblob of data.

    Having a payload is not required; and the payload can, in fact, just be avalue stored in the struct key itself.

    When a key is instantiated, the key type's instantiation function iscalled with a blob of data, and that then creates the key's payload insome way.

    Similarly, when userspace wants to read back the contents of the key, ifpermitted, another key type operation will be called to convert the key'sattached payload back into a blob of data.

  • Each key can be in one of a number of basic states:

    • Uninstantiated. The key exists, but does not have any data attached.Keys being requested from userspace will be in this state.

    • Instantiated. This is the normal state. The key is fully formed, andhas data attached.

    • Negative. This is a relatively short-lived state. The key acts as anote saying that a previous call out to userspace failed, and acts asa throttle on key lookups. A negative key can be updated to a normalstate.

    • Expired. Keys can have lifetimes set. If their lifetime is exceeded,they traverse to this state. An expired key can be updated back to anormal state.

    • Revoked. A key is put in this state by userspace action. It can't befound or operated upon (apart from by unlinking it).

    • Dead. The key's type was unregistered, and so the key is now useless.

Keys in the last three states are subject to garbage collection. See thesection on "Garbage collection".

Key Service Overview

The key service provides a number of features besides keys:

  • The key service defines three special key types:

    (+) "keyring"

    Keyrings are special keys that contain a list of other keys. Keyringlists can be modified using various system calls. Keyrings should notbe given a payload when created.

    (+) "user"

    A key of this type has a description and a payload that are arbitraryblobs of data. These can be created, updated and read by userspace,and aren't intended for use by kernel services.

    (+) "logon"

    Like a "user" key, a "logon" key has a payload that is an arbitraryblob of data. It is intended as a place to store secrets which areaccessible to the kernel but not to userspace programs.

    The description can be arbitrary, but must be prefixed with a non-zerolength string that describes the key "subclass". The subclass isseparated from the rest of the description by a ':'. "logon" keys canbe created and updated from userspace, but the payload is onlyreadable from kernel space.

  • Each process subscribes to three keyrings: a thread-specific keyring, aprocess-specific keyring, and a session-specific keyring.

    The thread-specific keyring is discarded from the child when any sort ofclone, fork, vfork or execve occurs. A new keyring is created only whenrequired.

    The process-specific keyring is replaced with an empty one in the child onclone, fork, vfork unless CLONE_THREAD is supplied, in which case it isshared. execve also discards the process's process keyring and creates anew one.

    The session-specific keyring is persistent across clone, fork, vfork andexecve, even when the latter executes a set-UID or set-GID binary. Aprocess can, however, replace its current session keyring with a new oneby using PR_JOIN_SESSION_KEYRING. It is permitted to request an anonymousnew one, or to attempt to create or join one of a specific name.

    The ownership of the thread keyring changes when the real UID and GID ofthe thread changes.

  • Each user ID resident in the system holds two special keyrings: a userspecific keyring and a default user session keyring. The default sessionkeyring is initialised with a link to the user-specific keyring.

    When a process changes its real UID, if it used to have no session key, itwill be subscribed to the default session key for the new UID.

    If a process attempts to access its session key when it doesn't have one,it will be subscribed to the default for its current UID.

  • Each user has two quotas against which the keys they own are tracked. Onelimits the total number of keys and keyrings, the other limits the totalamount of description and payload space that can be consumed.

    The user can view information on this and other statistics through procfsfiles. The root user may also alter the quota limits through sysctl files(see the section "New procfs files").

    Process-specific and thread-specific keyrings are not counted towards auser's quota.

    If a system call that modifies a key or keyring in some way would put theuser over quota, the operation is refused and error EDQUOT is returned.

  • There's a system call interface by which userspace programs can create andmanipulate keys and keyrings.

  • There's a kernel interface by which services can register types and searchfor keys.

  • There's a way for the a search done from the kernel to call back touserspace to request a key that can't be found in a process's keyrings.

  • An optional filesystem is available through which the key database can beviewed and manipulated.

Key Access Permissions

Keys have an owner user ID, a group access ID, and a permissions mask. The maskhas up to eight bits each for possessor, user, group and other access. Onlysix of each set of eight bits are defined. These permissions granted are:

  • View

    This permits a key or keyring's attributes to be viewed - including keytype and description.

  • Read

    This permits a key's payload to be viewed or a keyring's list of linkedkeys.

  • Write

    This permits a key's payload to be instantiated or updated, or it allows alink to be added to or removed from a keyring.

  • Search

    This permits keyrings to be searched and keys to be found. Searches canonly recurse into nested keyrings that have search permission set.

  • Link

    This permits a key or keyring to be linked to. To create a link from akeyring to a key, a process must have Write permission on the keyring andLink permission on the key.

  • Set Attribute

    This permits a key's UID, GID and permissions mask to be changed.

For changing the ownership, group ID or permissions mask, being the owner ofthe key or having the sysadmin capability is sufficient.

SELinux Support

The security class "key" has been added to SELinux so that mandatory accesscontrols can be applied to keys created within various contexts. This supportis preliminary, and is likely to change quite significantly in the near future.Currently, all of the basic permissions explained above are provided in SELinuxas well; SELinux is simply invoked after all basic permission checks have beenperformed.

The value of the file /proc/self/attr/keycreate influences the labeling ofnewly-created keys. If the contents of that file correspond to an SELinuxsecurity context, then the key will be assigned that context. Otherwise, thekey will be assigned the current context of the task that invoked the keycreation request. Tasks must be granted explicit permission to assign aparticular context to newly-created keys, using the "create" permission in thekey security class.

The default keyrings associated with users will be labeled with the defaultcontext of the user if and only if the login programs have been instrumented toproperly initialize keycreate during the login process. Otherwise, they willbe labeled with the context of the login program itself.

Note, however, that the default keyrings associated with the root user arelabeled with the default kernel context, since they are created early in theboot process, before root has a chance to log in.

The keyrings associated with new threads are each labeled with the context oftheir associated thread, and both session and process keyrings are handledsimilarly.

New ProcFS Files

Two files have been added to procfs by which an administrator can find outabout the status of the key service:

  • /proc/keys

    This lists the keys that are currently viewable by the task reading thefile, giving information about their type, description and permissions.It is not possible to view the payload of the key this way, though someinformation about it may be given.

    The only keys included in the list are those that grant View permission tothe reading process whether or not it possesses them. Note that LSMsecurity checks are still performed, and may further filter out keys thatthe current process is not authorised to view.

    The contents of the file look like this:

    SERIAL FLAGS USAGE EXPY PERM UID GID TYPE DESCRIPTION: SUMMARY00000001 I----- 39 perm 1f3f0000 0 0 keyring _uid_ses.0: 1/400000002 I----- 2 perm 1f3f0000 0 0 keyring _uid.0: empty00000007 I----- 1 perm 1f3f0000 0 0 keyring _pid.1: empty0000018d I----- 1 perm 1f3f0000 0 0 keyring _pid.412: empty000004d2 I--Q-- 1 perm 1f3f0000 32 -1 keyring _uid.32: 1/4000004d3 I--Q-- 3 perm 1f3f0000 32 -1 keyring _uid_ses.32: empty00000892 I--QU- 1 perm 1f000000 0 0 user metal:copper: 000000893 I--Q-N 1 35s 1f3f0000 0 0 user metal:silver: 000000894 I--Q-- 1 10h 003f0000 0 0 user metal:gold: 0

    The flags are:

    I InstantiatedR RevokedD DeadQ Contributes to user's quotaU Under construction by callback to userspaceN Negative key
  • /proc/key-users

    This file lists the tracking data for each user that has at least one keyon the system. Such data includes quota information and statistics:

    [root@andromeda root]# cat /proc/key-users0: 46 45/45 1/100 13/1000029: 2 2/2 2/100 40/1000032: 2 2/2 2/100 40/1000038: 2 2/2 2/100 40/10000

    The format of each line is:

    <UID>: User ID to which this applies<usage> Structure refcount<inst>/<keys> Total number of keys and number instantiated<keys>/<max> Key count quota<bytes>/<max> Key size quota

Four new sysctl files have been added also for the purpose of controlling thequota limits on keys:

  • /proc/sys/kernel/keys/root_maxkeys/proc/sys/kernel/keys/root_maxbytes

    These files hold the maximum number of keys that root may have and themaximum total number of bytes of data that root may have stored in thosekeys.

  • /proc/sys/kernel/keys/maxkeys/proc/sys/kernel/keys/maxbytes

    These files hold the maximum number of keys that each non-root user mayhave and the maximum total number of bytes of data that each of thoseusers may have stored in their keys.

Root may alter these by writing each new limit as a decimal number string tothe appropriate file.

Userspace System Call Interface

Userspace can manipulate keys directly through three new syscalls: add_key,request_key and keyctl. The latter provides a number of functions formanipulating keys.

When referring to a key directly, userspace programs should use the key'sserial number (a positive 32-bit integer). However, there are some specialvalues available for referring to special keys and keyrings that relate to theprocess making the call:

CONSTANT VALUE KEY REFERENCED============================== ====== ===========================KEY_SPEC_THREAD_KEYRING -1 thread-specific keyringKEY_SPEC_PROCESS_KEYRING -2 process-specific keyringKEY_SPEC_SESSION_KEYRING -3 session-specific keyringKEY_SPEC_USER_KEYRING -4 UID-specific keyringKEY_SPEC_USER_SESSION_KEYRING -5 UID-session keyringKEY_SPEC_GROUP_KEYRING -6 GID-specific keyringKEY_SPEC_REQKEY_AUTH_KEY -7 assumed request_key() authorisation key

The main syscalls are:

  • Create a new key of given type, description and payload and add it to thenominated keyring:

    key_serial_t add_key(const char *type, const char *desc, const void *payload, size_t plen, key_serial_t keyring);

    If a key of the same type and description as that proposed already existsin the keyring, this will try to update it with the given payload, or itwill return error EEXIST if that function is not supported by the keytype. The process must also have permission to write to the key to be ableto update it. The new key will have all user permissions granted and nogroup or third party permissions.

    Otherwise, this will attempt to create a new key of the specified type anddescription, and to instantiate it with the supplied payload and attach itto the keyring. In this case, an error will be generated if the processdoes not have permission to write to the keyring.

    If the key type supports it, if the description is NULL or an emptystring, the key type will try and generate a description from the contentof the payload.

    The payload is optional, and the pointer can be NULL if not required bythe type. The payload is plen in size, and plen can be zero for an emptypayload.

    A new keyring can be generated by setting type "keyring", the keyring nameas the description (or NULL) and setting the payload to NULL.

    User defined keys can be created by specifying type "user". It isrecommended that a user defined key's description by prefixed with a typeID and a colon, such as "krb5tgt:" for a Kerberos 5 ticket grantingticket.

    Any other type must have been registered with the kernel in advance by akernel service such as a filesystem.

    The ID of the new or updated key is returned if successful.

  • Search the process's keyrings for a key, potentially calling out touserspace to create it:

    key_serial_t request_key(const char *type, const char *description, const char *callout_info, key_serial_t dest_keyring);

    This function searches all the process's keyrings in the order thread,process, session for a matching key. This works very much likeKEYCTL_SEARCH, including the optional attachment of the discovered key toa keyring.

    If a key cannot be found, and if callout_info is not NULL, then/sbin/request-key will be invoked in an attempt to obtain a key. Thecallout_info string will be passed as an argument to the program.

    To link a key into the destination keyring the key must grant linkpermission on the key to the caller and the keyring must grant writepermission.

    See also Key Request Service.

The keyctl syscall functions are:

  • Map a special key ID to a real key ID for this process:

    key_serial_t keyctl(KEYCTL_GET_KEYRING_ID, key_serial_t id, int create);

    The special key specified by "id" is looked up (with the key being createdif necessary) and the ID of the key or keyring thus found is returned ifit exists.

    If the key does not yet exist, the key will be created if "create" isnon-zero; and the error ENOKEY will be returned if "create" is zero.

  • Replace the session keyring this process subscribes to with a new one:

    key_serial_t keyctl(KEYCTL_JOIN_SESSION_KEYRING, const char *name);

    If name is NULL, an anonymous keyring is created attached to the processas its session keyring, displacing the old session keyring.

    If name is not NULL, if a keyring of that name exists, the processattempts to attach it as the session keyring, returning an error if thatis not permitted; otherwise a new keyring of that name is created andattached as the session keyring.

    To attach to a named keyring, the keyring must have search permission forthe process's ownership.

    The ID of the new session keyring is returned if successful.

  • Update the specified key:

    long keyctl(KEYCTL_UPDATE, key_serial_t key, const void *payload, size_t plen);

    This will try to update the specified key with the given payload, or itwill return error EOPNOTSUPP if that function is not supported by the keytype. The process must also have permission to write to the key to be ableto update it.

    The payload is of length plen, and may be absent or empty as foradd_key().

  • Revoke a key:

    long keyctl(KEYCTL_REVOKE, key_serial_t key);

    This makes a key unavailable for further operations. Further attempts touse the key will be met with error EKEYREVOKED, and the key will no longerbe findable.

  • Change the ownership of a key:

    long keyctl(KEYCTL_CHOWN, key_serial_t key, uid_t uid, gid_t gid);

    This function permits a key's owner and group ID to be changed. Either oneof uid or gid can be set to -1 to suppress that change.

    Only the superuser can change a key's owner to something other than thekey's current owner. Similarly, only the superuser can change a key'sgroup ID to something other than the calling process's group ID or one ofits group list members.

  • Change the permissions mask on a key:

    long keyctl(KEYCTL_SETPERM, key_serial_t key, key_perm_t perm);

    This function permits the owner of a key or the superuser to change thepermissions mask on a key.

    Only bits the available bits are permitted; if any other bits are set,error EINVAL will be returned.

  • Describe a key:

    long keyctl(KEYCTL_DESCRIBE, key_serial_t key, char *buffer, size_t buflen);

    This function returns a summary of the key's attributes (but not itspayload data) as a string in the buffer provided.

    Unless there's an error, it always returns the amount of data it couldproduce, even if that's too big for the buffer, but it won't copy morethan requested to userspace. If the buffer pointer is NULL then no copywill take place.

    A process must have view permission on the key for this function to besuccessful.

    If successful, a string is placed in the buffer in the following format:

    <type>;<uid>;<gid>;<perm>;<description>

    Where type and description are strings, uid and gid are decimal, and permis hexadecimal. A NUL character is included at the end of the string ifthe buffer is sufficiently big.

    This can be parsed with:

    sscanf(buffer, "%[^;];%d;%d;%o;%s", type, &uid, &gid, &mode, desc);
  • Clear out a keyring:

    long keyctl(KEYCTL_CLEAR, key_serial_t keyring);

    This function clears the list of keys attached to a keyring. The callingprocess must have write permission on the keyring, and it must be akeyring (or else error ENOTDIR will result).

    This function can also be used to clear special kernel keyrings if theyare appropriately marked if the user has CAP_SYS_ADMIN capability. TheDNS resolver cache keyring is an example of this.

  • Link a key into a keyring:

    long keyctl(KEYCTL_LINK, key_serial_t keyring, key_serial_t key);

    This function creates a link from the keyring to the key. The process musthave write permission on the keyring and must have link permission on thekey.

    Should the keyring not be a keyring, error ENOTDIR will result; and if thekeyring is full, error ENFILE will result.

    The link procedure checks the nesting of the keyrings, returning ELOOP ifit appears too deep or EDEADLK if the link would introduce a cycle.

    Any links within the keyring to keys that match the new key in terms oftype and description will be discarded from the keyring as the new one isadded.

  • Move a key from one keyring to another:

    long keyctl(KEYCTL_MOVE, key_serial_t id, key_serial_t from_ring_id, key_serial_t to_ring_id, unsigned int flags);

    Move the key specified by "id" from the keyring specified by"from_ring_id" to the keyring specified by "to_ring_id". If the twokeyrings are the same, nothing is done.

    "flags" can have KEYCTL_MOVE_EXCL set in it to cause the operation to failwith EEXIST if a matching key exists in the destination keyring, otherwisesuch a key will be replaced.

    A process must have link permission on the key for this function to besuccessful and write permission on both keyrings. Any errors that canoccur from KEYCTL_LINK also apply on the destination keyring here.

  • Unlink a key or keyring from another keyring:

    long keyctl(KEYCTL_UNLINK, key_serial_t keyring, key_serial_t key);

    This function looks through the keyring for the first link to thespecified key, and removes it if found. Subsequent links to that key areignored. The process must have write permission on the keyring.

    If the keyring is not a keyring, error ENOTDIR will result; and if the keyis not present, error ENOENT will be the result.

  • Search a keyring tree for a key:

    key_serial_t keyctl(KEYCTL_SEARCH, key_serial_t keyring, const char *type, const char *description, key_serial_t dest_keyring);

    This searches the keyring tree headed by the specified keyring until a keyis found that matches the type and description criteria. Each keyring ischecked for keys before recursion into its children occurs.

    The process must have search permission on the top level keyring, or elseerror EACCES will result. Only keyrings that the process has searchpermission on will be recursed into, and only keys and keyrings for whicha process has search permission can be matched. If the specified keyringis not a keyring, ENOTDIR will result.

    If the search succeeds, the function will attempt to link the found keyinto the destination keyring if one is supplied (non-zero ID). All theconstraints applicable to KEYCTL_LINK apply in this case too.

    Error ENOKEY, EKEYREVOKED or EKEYEXPIRED will be returned if the searchfails. On success, the resulting key ID will be returned.

  • Read the payload data from a key:

    long keyctl(KEYCTL_READ, key_serial_t keyring, char *buffer, size_t buflen);

    This function attempts to read the payload data from the specified keyinto the buffer. The process must have read permission on the key tosucceed.

    The returned data will be processed for presentation by the key type. Forinstance, a keyring will return an array of key_serial_t entriesrepresenting the IDs of all the keys to which it is subscribed. The userdefined key type will return its data as is. If a key type does notimplement this function, error EOPNOTSUPP will result.

    If the specified buffer is too small, then the size of the buffer requiredwill be returned. Note that in this case, the contents of the buffer mayhave been overwritten in some undefined way.

    Otherwise, on success, the function will return the amount of data copiedinto the buffer.

  • Instantiate a partially constructed key:

    long keyctl(KEYCTL_INSTANTIATE, key_serial_t key, const void *payload, size_t plen, key_serial_t keyring);long keyctl(KEYCTL_INSTANTIATE_IOV, key_serial_t key, const struct iovec *payload_iov, unsigned ioc, key_serial_t keyring);

    If the kernel calls back to userspace to complete the instantiation of akey, userspace should use this call to supply data for the key before theinvoked process returns, or else the key will be marked negativeautomatically.

    The process must have write access on the key to be able to instantiateit, and the key must be uninstantiated.

    If a keyring is specified (non-zero), the key will also be linked intothat keyring, however all the constraints applying in KEYCTL_LINK apply inthis case too.

    The payload and plen arguments describe the payload data as for add_key().

    The payload_iov and ioc arguments describe the payload data in an iovecarray instead of a single buffer.

  • Negatively instantiate a partially constructed key:

    long keyctl(KEYCTL_NEGATE, key_serial_t key, unsigned timeout, key_serial_t keyring);long keyctl(KEYCTL_REJECT, key_serial_t key, unsigned timeout, unsigned error, key_serial_t keyring);

    If the kernel calls back to userspace to complete the instantiation of akey, userspace should use this call mark the key as negative before theinvoked process returns if it is unable to fulfill the request.

    The process must have write access on the key to be able to instantiateit, and the key must be uninstantiated.

    If a keyring is specified (non-zero), the key will also be linked intothat keyring, however all the constraints applying in KEYCTL_LINK apply inthis case too.

    If the key is rejected, future searches for it will return the specifiederror code until the rejected key expires. Negating the key is the sameas rejecting the key with ENOKEY as the error code.

  • Set the default request-key destination keyring:

    long keyctl(KEYCTL_SET_REQKEY_KEYRING, int reqkey_defl);

    This sets the default keyring to which implicitly requested keys will beattached for this thread. reqkey_defl should be one of these constants:

    CONSTANT VALUE NEW DEFAULT KEYRING====================================== ====== =======================KEY_REQKEY_DEFL_NO_CHANGE -1 No changeKEY_REQKEY_DEFL_DEFAULT 0 Default[1]KEY_REQKEY_DEFL_THREAD_KEYRING 1 Thread keyringKEY_REQKEY_DEFL_PROCESS_KEYRING 2 Process keyringKEY_REQKEY_DEFL_SESSION_KEYRING 3 Session keyringKEY_REQKEY_DEFL_USER_KEYRING 4 User keyringKEY_REQKEY_DEFL_USER_SESSION_KEYRING 5 User session keyringKEY_REQKEY_DEFL_GROUP_KEYRING 6 Group keyring

    The old default will be returned if successful and error EINVAL will bereturned if reqkey_defl is not one of the above values.

    The default keyring can be overridden by the keyring indicated to therequest_key() system call.

    Note that this setting is inherited across fork/exec.

    [1] The default is: the thread keyring if there is one, otherwisethe process keyring if there is one, otherwise the session keyring ifthere is one, otherwise the user default session keyring.

  • Set the timeout on a key:

    long keyctl(KEYCTL_SET_TIMEOUT, key_serial_t key, unsigned timeout);

    This sets or clears the timeout on a key. The timeout can be 0 to clearthe timeout or a number of seconds to set the expiry time that far intothe future.

    The process must have attribute modification access on a key to set itstimeout. Timeouts may not be set with this function on negative, revokedor expired keys.

  • Assume the authority granted to instantiate a key:

    long keyctl(KEYCTL_ASSUME_AUTHORITY, key_serial_t key);

    This assumes or divests the authority required to instantiate thespecified key. Authority can only be assumed if the thread has theauthorisation key associated with the specified key in its keyringssomewhere.

    Once authority is assumed, searches for keys will also search therequester's keyrings using the requester's security label, UID, GID andgroups.

    If the requested authority is unavailable, error EPERM will be returned,likewise if the authority has been revoked because the target key isalready instantiated.

    If the specified key is 0, then any assumed authority will be divested.

    The assumed authoritative key is inherited across fork and exec.

  • Get the LSM security context attached to a key:

    long keyctl(KEYCTL_GET_SECURITY, key_serial_t key, char *buffer, size_t buflen)

    This function returns a string that represents the LSM security contextattached to a key in the buffer provided.

    Unless there's an error, it always returns the amount of data it couldproduce, even if that's too big for the buffer, but it won't copy morethan requested to userspace. If the buffer pointer is NULL then no copywill take place.

    A NUL character is included at the end of the string if the buffer issufficiently big. This is included in the returned count. If no LSM isin force then an empty string will be returned.

    A process must have view permission on the key for this function to besuccessful.

  • Install the calling process's session keyring on its parent:

    long keyctl(KEYCTL_SESSION_TO_PARENT);

    This functions attempts to install the calling process's session keyringon to the calling process's parent, replacing the parent's current sessionkeyring.

    The calling process must have the same ownership as its parent, thekeyring must have the same ownership as the calling process, the callingprocess must have LINK permission on the keyring and the active LSM modulemustn't deny permission, otherwise error EPERM will be returned.

    Error ENOMEM will be returned if there was insufficient memory to completethe operation, otherwise 0 will be returned to indicate success.

    The keyring will be replaced next time the parent process leaves thekernel and resumes executing userspace.

  • Invalidate a key:

    long keyctl(KEYCTL_INVALIDATE, key_serial_t key);

    This function marks a key as being invalidated and then wakes up thegarbage collector. The garbage collector immediately removes invalidatedkeys from all keyrings and deletes the key when its reference countreaches zero.

    Keys that are marked invalidated become invisible to normal key operationsimmediately, though they are still visible in /proc/keys until deleted(they're marked with an 'i' flag).

    A process must have search permission on the key for this function to besuccessful.

  • Compute a Diffie-Hellman shared secret or public key:

    long keyctl(KEYCTL_DH_COMPUTE, struct keyctl_dh_params *params, char *buffer, size_t buflen, struct keyctl_kdf_params *kdf);

    The params struct contains serial numbers for three keys:

    - The prime, p, known to both parties- The local private key- The base integer, which is either a shared generator or the remote public key

    The value computed is:

    result = base ^ private (mod prime)

    If the base is the shared generator, the result is the localpublic key. If the base is the remote public key, the result isthe shared secret.

    If the parameter kdf is NULL, the following applies:

    • The buffer length must be at least the length of the prime, or zero.

    • If the buffer length is nonzero, the length of the result isreturned when it is successfully calculated and copied in to thebuffer. When the buffer length is zero, the minimum requiredbuffer length is returned.

    The kdf parameter allows the caller to apply a key derivation function(KDF) on the Diffie-Hellman computation where only the resultof the KDF is returned to the caller. The KDF is characterized withstruct keyctl_kdf_params as follows:

    • char *hashname specifies the NUL terminated string identifyingthe hash used from the kernel crypto API and applied for the KDFoperation. The KDF implementation complies with SP800-56A as wellas with SP800-108 (the counter KDF).

    • char *otherinfo specifies the OtherInfo data as documented inSP800-56A section 5.8.1.2. The length of the buffer is given withotherinfolen. The format of OtherInfo is defined by the caller.The otherinfo pointer may be NULL if no OtherInfo shall be used.

    This function will return error EOPNOTSUPP if the key type is notsupported, error ENOKEY if the key could not be found, or errorEACCES if the key is not readable by the caller. In addition, thefunction will return EMSGSIZE when the parameter kdf is non-NULLand either the buffer length or the OtherInfo length exceeds theallowed length.

  • Restrict keyring linkage:

    long keyctl(KEYCTL_RESTRICT_KEYRING, key_serial_t keyring, const char *type, const char *restriction);

    An existing keyring can restrict linkage of additional keys by evaluatingthe contents of the key according to a restriction scheme.

    "keyring" is the key ID for an existing keyring to apply a restrictionto. It may be empty or may already have keys linked. Existing linked keyswill remain in the keyring even if the new restriction would reject them.

    "type" is a registered key type.

    "restriction" is a string describing how key linkage is to be restricted.The format varies depending on the key type, and the string is passed tothe lookup_restriction() function for the requested type. It may specifya method and relevant data for the restriction such as signatureverification or constraints on key payload. If the requested key type islater unregistered, no keys may be added to the keyring after the key typeis removed.

    To apply a keyring restriction the process must have Set Attributepermission and the keyring must not be previously restricted.

    One application of restricted keyrings is to verify X.509 certificatechains or individual certificate signatures using the asymmetric key type.See Asymmetric / Public-key Cryptography Key Type for specific restrictionsapplicable to the asymmetric key type.

  • Query an asymmetric key:

    long keyctl(KEYCTL_PKEY_QUERY, key_serial_t key_id, unsigned long reserved, const char *params, struct keyctl_pkey_query *info);

    Get information about an asymmetric key. Specific algorithms andencodings may be queried by using the params argument. This is astring containing a space- or tab-separated string of key-value pairs.Currently supported keys include enc and hash. The informationis returned in the keyctl_pkey_query struct:

    __u32 supported_ops;__u32 key_size;__u16 max_data_size;__u16 max_sig_size;__u16 max_enc_size;__u16 max_dec_size;__u32 __spare[10];

    supported_ops contains a bit mask of flags indicating which ops aresupported. This is constructed from a bitwise-OR of:

    KEYCTL_SUPPORTS_{ENCRYPT,DECRYPT,SIGN,VERIFY}

    key_size indicated the size of the key in bits.

    max_*_size indicate the maximum sizes in bytes of a blob of data to besigned, a signature blob, a blob to be encrypted and a blob to bedecrypted.

    __spare[] must be set to 0. This is intended for future use to handover one or more passphrases needed unlock a key.

    If successful, 0 is returned. If the key is not an asymmetric key,EOPNOTSUPP is returned.

  • Encrypt, decrypt, sign or verify a blob using an asymmetric key:

    long keyctl(KEYCTL_PKEY_ENCRYPT, const struct keyctl_pkey_params *params, const char *info, const void *in, void *out);long keyctl(KEYCTL_PKEY_DECRYPT, const struct keyctl_pkey_params *params, const char *info, const void *in, void *out);long keyctl(KEYCTL_PKEY_SIGN, const struct keyctl_pkey_params *params, const char *info, const void *in, void *out);long keyctl(KEYCTL_PKEY_VERIFY, const struct keyctl_pkey_params *params, const char *info, const void *in, const void *in2);

    Use an asymmetric key to perform a public-key cryptographic operation ablob of data. For encryption and verification, the asymmetric key mayonly need the public parts to be available, but for decryption and signingthe private parts are required also.

    The parameter block pointed to by params contains a number of integervalues:

    __s32 key_id;__u32 in_len;__u32 out_len;__u32 in2_len;

    key_id is the ID of the asymmetric key to be used. in_len andin2_len indicate the amount of data in the in and in2 buffers andout_len indicates the size of the out buffer as appropriate for theabove operations.

    For a given operation, the in and out buffers are used as follows:

    Operation ID in,in_len out,out_len in2,in2_len======================= =============== =============== ===============KEYCTL_PKEY_ENCRYPT Raw data Encrypted data -KEYCTL_PKEY_DECRYPT Encrypted data Raw data -KEYCTL_PKEY_SIGN Raw data Signature -KEYCTL_PKEY_VERIFY Raw data - Signature

    info is a string of key=value pairs that supply supplementaryinformation. These include:

    enc=<encoding> The encoding of the encrypted/signature blob. This

    can be "pkcs1" for RSASSA-PKCS1-v1.5 orRSAES-PKCS1-v1.5; "pss" for "RSASSA-PSS"; "oaep" for"RSAES-OAEP". If omitted or is "raw", the raw outputof the encryption function is specified.

    hash=<algo> If the data buffer contains the output of a hash

    function and the encoding includes some indication ofwhich hash function was used, the hash function can bespecified with this, eg. "hash=sha256".

    The __spare[] space in the parameter block must be set to 0. This isintended, amongst other things, to allow the passing of passphrasesrequired to unlock a key.

    If successful, encrypt, decrypt and sign all return the amount of datawritten into the output buffer. Verification returns 0 on success.

  • Watch a key or keyring for changes:

    long keyctl(KEYCTL_WATCH_KEY, key_serial_t key, int queue_fd, const struct watch_notification_filter *filter);

    This will set or remove a watch for changes on the specified key orkeyring.

    "key" is the ID of the key to be watched.

    "queue_fd" is a file descriptor referring to an open pipe whichmanages the buffer into which notifications will be delivered.

    "filter" is either NULL to remove a watch or a filter specification toindicate what events are required from the key.

    See General notification mechanism for more information.

    Note that only one watch may be emplaced for any particular { key,queue_fd } combination.

    Notification records look like:

    struct key_notification { struct watch_notification watch; __u32 key_id; __u32 aux;};

    In this, watch::type will be "WATCH_TYPE_KEY_NOTIFY" and subtype will beone of:

    NOTIFY_KEY_INSTANTIATEDNOTIFY_KEY_UPDATEDNOTIFY_KEY_LINKEDNOTIFY_KEY_UNLINKEDNOTIFY_KEY_CLEAREDNOTIFY_KEY_REVOKEDNOTIFY_KEY_INVALIDATEDNOTIFY_KEY_SETATTR

    Where these indicate a key being instantiated/rejected, updated, a linkbeing made in a keyring, a link being removed from a keyring, a keyringbeing cleared, a key being revoked, a key being invalidated or a keyhaving one of its attributes changed (user, group, perm, timeout,restriction).

    If a watched key is deleted, a basic watch_notification will be issuedwith "type" set to WATCH_TYPE_META and "subtype" set towatch_meta_removal_notification. The watchpoint ID will be set in the"info" field.

    This needs to be configured by enabling:

    "Provide key/keyring change notifications" (KEY_NOTIFICATIONS)

Kernel Services

The kernel services for key management are fairly simple to deal with. They canbe broken down into two areas: keys and key types.

Dealing with keys is fairly straightforward. Firstly, the kernel serviceregisters its type, then it searches for a key of that type. It should retainthe key as long as it has need of it, and then it should release it. For afilesystem or device file, a search would probably be performed during the opencall, and the key released upon close. How to deal with conflicting keys due totwo different users opening the same file is left to the filesystem author tosolve.

To access the key manager, the following header must be #included:

<linux/key.h>

Specific key types should have a header file under include/keys/ that should beused to access that type. For keys of type "user", for example, that would be:

<keys/user-type.h>

Note that there are two different types of pointers to keys that may beencountered:

  • struct key *

    This simply points to the key structure itself. Key structures will be atleast four-byte aligned.

  • key_ref_t

    This is equivalent to a struct key *, but the least significant bit is setif the caller "possesses" the key. By "possession" it is meant that thecalling processes has a searchable link to the key from one of itskeyrings. There are three functions for dealing with these:

    key_ref_t make_key_ref(const struct key *key, bool possession);struct key *key_ref_to_ptr(const key_ref_t key_ref);bool is_key_possessed(const key_ref_t key_ref);

    The first function constructs a key reference from a key pointer andpossession information (which must be true or false).

    The second function retrieves the key pointer from a reference and thethird retrieves the possession flag.

When accessing a key's payload contents, certain precautions must be taken toprevent access vs modification races. See the section "Notes on accessingpayload contents" for more information.

  • To search for a key, call:

    struct key *request_key(const struct key_type *type, const char *description, const char *callout_info);

    This is used to request a key or keyring with a description that matchesthe description specified according to the key type's match_preparse()method. This permits approximate matching to occur. If callout_string isnot NULL, then /sbin/request-key will be invoked in an attempt to obtainthe key from userspace. In that case, callout_string will be passed as anargument to the program.

    Should the function fail error ENOKEY, EKEYEXPIRED or EKEYREVOKED will bereturned.

    If successful, the key will have been attached to the default keyring forimplicitly obtained request-key keys, as set by KEYCTL_SET_REQKEY_KEYRING.

    See also Key Request Service.

  • To search for a key in a specific domain, call:

    struct key *request_key_tag(const struct key_type *type, const char *description, struct key_tag *domain_tag, const char *callout_info);

    This is identical to request_key(), except that a domain tag may bespecifies that causes search algorithm to only match keys matching thattag. The domain_tag may be NULL, specifying a global domain that isseparate from any nominated domain.

  • To search for a key, passing auxiliary data to the upcaller, call:

    struct key *request_key_with_auxdata(const struct key_type *type, const char *description, struct key_tag *domain_tag, const void *callout_info, size_t callout_len, void *aux);

    This is identical to request_key_tag(), except that the auxiliary data ispassed to the key_type->request_key() op if it exists, and thecallout_info is a blob of length callout_len, if given (the length may be0).

  • To search for a key under RCU conditions, call:

    struct key *request_key_rcu(const struct key_type *type, const char *description, struct key_tag *domain_tag);

    which is similar to request_key_tag() except that it does not check forkeys that are under construction and it will not call out to userspace toconstruct a key if it can't find a match.

  • When it is no longer required, the key should be released using:

    void key_put(struct key *key);

    Or:

    void key_ref_put(key_ref_t key_ref);

    These can be called from interrupt context. If CONFIG_KEYS is not set thenthe argument will not be parsed.

  • Extra references can be made to a key by calling one of the followingfunctions:

    struct key *__key_get(struct key *key);struct key *key_get(struct key *key);

    Keys so references will need to be disposed of by calling key_put() whenthey've been finished with. The key pointer passed in will be returned.

    In the case of key_get(), if the pointer is NULL or CONFIG_KEYS is not setthen the key will not be dereferenced and no increment will take place.

  • A key's serial number can be obtained by calling:

    key_serial_t key_serial(struct key *key);

    If key is NULL or if CONFIG_KEYS is not set then 0 will be returned (in thelatter case without parsing the argument).

  • If a keyring was found in the search, this can be further searched by:

    key_ref_t keyring_search(key_ref_t keyring_ref, const struct key_type *type, const char *description, bool recurse)

    This searches the specified keyring only (recurse == false) or keyring tree(recurse == true) specified for a matching key. Error ENOKEY is returnedupon failure (use IS_ERR/PTR_ERR to determine). If successful, the returnedkey will need to be released.

    The possession attribute from the keyring reference is used to controlaccess through the permissions mask and is propagated to the returned keyreference pointer if successful.

  • A keyring can be created by:

    struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid, const struct cred *cred, key_perm_t perm, struct key_restriction *restrict_link, unsigned long flags, struct key *dest);

    This creates a keyring with the given attributes and returns it. If destis not NULL, the new keyring will be linked into the keyring to which itpoints. No permission checks are made upon the destination keyring.

    Error EDQUOT can be returned if the keyring would overload the quota (passKEY_ALLOC_NOT_IN_QUOTA in flags if the keyring shouldn't be accountedtowards the user's quota). Error ENOMEM can also be returned.

    If restrict_link is not NULL, it should point to a structure that containsthe function that will be called each time an attempt is made to link akey into the new keyring. The structure may also contain a key pointerand an associated key type. The function is called to check whether a keymay be added into the keyring or not. The key type is used by the garbagecollector to clean up function or data pointers in this structure if thegiven key type is unregistered. Callers of key_create_or_update() withinthe kernel can pass KEY_ALLOC_BYPASS_RESTRICTION to suppress the check.An example of using this is to manage rings of cryptographic keys that areset up when the kernel boots where userspace is also permitted to add keys- provided they can be verified by a key the kernel already has.

    When called, the restriction function will be passed the keyring beingadded to, the key type, the payload of the key being added, and data to beused in the restriction check. Note that when a new key is being created,this is called between payload preparsing and actual key creation. Thefunction should return 0 to allow the link or an error to reject it.

    A convenience function, restrict_link_reject, exists to always return-EPERM to in this case.

  • To check the validity of a key, this function can be called:

    int validate_key(struct key *key);

    This checks that the key in question hasn't expired or and hasn't beenrevoked. Should the key be invalid, error EKEYEXPIRED or EKEYREVOKED willbe returned. If the key is NULL or if CONFIG_KEYS is not set then 0 will bereturned (in the latter case without parsing the argument).

  • To register a key type, the following function should be called:

    int register_key_type(struct key_type *type);

    This will return error EEXIST if a type of the same name is alreadypresent.

  • To unregister a key type, call:

    void unregister_key_type(struct key_type *type);

Under some circ*mstances, it may be desirable to deal with a bundle of keys.The facility provides access to the keyring type for managing such a bundle:

struct key_type key_type_keyring;

This can be used with a function such as request_key() to find a specifickeyring in a process's keyrings. A keyring thus found can then be searchedwith keyring_search(). Note that it is not possible to use request_key() tosearch a specific keyring, so using keyrings in this way is of limited utility.

Notes On Accessing Payload Contents

The simplest payload is just data stored in key->payload directly. In thiscase, there's no need to indulge in RCU or locking when accessing the payload.

More complex payload contents must be allocated and pointers to them set in thekey->payload.data[] array. One of the following ways must be selected toaccess the data:

  1. Unmodifiable key type.

    If the key type does not have a modify method, then the key's payload canbe accessed without any form of locking, provided that it's known to beinstantiated (uninstantiated keys cannot be "found").

  2. The key's semaphore.

    The semaphore could be used to govern access to the payload and to controlthe payload pointer. It must be write-locked for modifications and wouldhave to be read-locked for general access. The disadvantage of doing thisis that the accessor may be required to sleep.

  3. RCU.

    RCU must be used when the semaphore isn't already held; if the semaphoreis held then the contents can't change under you unexpectedly as thesemaphore must still be used to serialise modifications to the key. Thekey management code takes care of this for the key type.

    However, this means using:

    rcu_read_lock() ... rcu_dereference() ... rcu_read_unlock()

    to read the pointer, and:

    rcu_dereference() ... rcu_assign_pointer() ... call_rcu()

    to set the pointer and dispose of the old contents after a grace period.Note that only the key type should ever modify a key's payload.

    Furthermore, an RCU controlled payload must hold a struct rcu_head for theuse of call_rcu() and, if the payload is of variable size, the length ofthe payload. key->datalen cannot be relied upon to be consistent with thepayload just dereferenced if the key's semaphore is not held.

    Note that key->payload.data[0] has a shadow that is marked for __rcuusage. This is called key->payload.rcu_data0. The following accessorswrap the RCU calls to this element:

    1. Set or change the first payload pointer:

      rcu_assign_keypointer(struct key *key, void *data);
    2. Read the first payload pointer with the key semaphore held:

       [const] void *dereference_key_locked([const] struct key *key);Note that the return value will inherit its constness from the keyparameter. Static analysis will give an error if it things the lockisn't held.
    3. Read the first payload pointer with the RCU read lock held:

      const void *dereference_key_rcu(const struct key *key);

Defining a Key Type

A kernel service may want to define its own key type. For instance, an AFSfilesystem might want to define a Kerberos 5 ticket key type. To do this, itauthor fills in a key_type struct and registers it with the system.

Source files that implement key types should include the following header file:

<linux/key-type.h>

The structure has a number of fields, some of which are mandatory:

  • const char *name

    The name of the key type. This is used to translate a key type namesupplied by userspace into a pointer to the structure.

  • size_t def_datalen

    This is optional - it supplies the default payload data length ascontributed to the quota. If the key type's payload is always or almostalways the same size, then this is a more efficient way to do things.

    The data length (and quota) on a particular key can always be changedduring instantiation or update by calling:

    int key_payload_reserve(struct key *key, size_t datalen);

    With the revised data length. Error EDQUOT will be returned if this is notviable.

  • int (*vet_description)(const char *description);

    This optional method is called to vet a key description. If the key typedoesn't approve of the key description, it may return an error, otherwiseit should return 0.

  • int (*preparse)(struct key_preparsed_payload *prep);

    This optional method permits the key type to attempt to parse payloadbefore a key is created (add key) or the key semaphore is taken (update orinstantiate key). The structure pointed to by prep looks like:

    struct key_preparsed_payload { char *description; union key_payload payload; const void *data; size_t datalen; size_t quotalen; time_t expiry;};

    Before calling the method, the caller will fill in data and datalen withthe payload blob parameters; quotalen will be filled in with the defaultquota size from the key type; expiry will be set to TIME_T_MAX and therest will be cleared.

    If a description can be proposed from the payload contents, that should beattached as a string to the description field. This will be used for thekey description if the caller of add_key() passes NULL or "".

    The method can attach anything it likes to payload. This is merely passedalong to the instantiate() or update() operations. If set, the expirytime will be applied to the key if it is instantiated from this data.

    The method should return 0 if successful or a negative error codeotherwise.

  • void (*free_preparse)(struct key_preparsed_payload *prep);

    This method is only required if the preparse() method is provided,otherwise it is unused. It cleans up anything attached to the descriptionand payload fields of the key_preparsed_payload struct as filled in by thepreparse() method. It will always be called after preparse() returnssuccessfully, even if instantiate() or update() succeed.

  • int (*instantiate)(struct key *key, struct key_preparsed_payload *prep);

    This method is called to attach a payload to a key during construction.The payload attached need not bear any relation to the data passed to thisfunction.

    The prep->data and prep->datalen fields will define the original payloadblob. If preparse() was supplied then other fields may be filled in also.

    If the amount of data attached to the key differs from the size inkeytype->def_datalen, then key_payload_reserve() should be called.

    This method does not have to lock the key in order to attach a payload.The fact that KEY_FLAG_INSTANTIATED is not set in key->flags preventsanything else from gaining access to the key.

    It is safe to sleep in this method.

    generic_key_instantiate() is provided to simply copy the data fromprep->payload.data[] to key->payload.data[], with RCU-safe assignment onthe first element. It will then clear prep->payload.data[] so that thefree_preparse method doesn't release the data.

  • int (*update)(struct key *key, const void *data, size_t datalen);

    If this type of key can be updated, then this method should be provided.It is called to update a key's payload from the blob of data provided.

    The prep->data and prep->datalen fields will define the original payloadblob. If preparse() was supplied then other fields may be filled in also.

    key_payload_reserve() should be called if the data length might changebefore any changes are actually made. Note that if this succeeds, the typeis committed to changing the key because it's already been altered, so allmemory allocation must be done first.

    The key will have its semaphore write-locked before this method is called,but this only deters other writers; any changes to the key's payload mustbe made under RCU conditions, and call_rcu() must be used to dispose ofthe old payload.

    key_payload_reserve() should be called before the changes are made, butafter all allocations and other potentially failing function calls aremade.

    It is safe to sleep in this method.

  • int (*match_preparse)(struct key_match_data *match_data);

    This method is optional. It is called when a key search is about to beperformed. It is given the following structure:

    struct key_match_data { bool (*cmp)(const struct key *key, const struct key_match_data *match_data); const void *raw_data; void *preparsed; unsigned lookup_type;};

    On entry, raw_data will be pointing to the criteria to be used in matchinga key by the caller and should not be modified. (*cmp)() will be pointingto the default matcher function (which does an exact description matchagainst raw_data) and lookup_type will be set to indicate a direct lookup.

    The following lookup_type values are available:

    • KEYRING_SEARCH_LOOKUP_DIRECT - A direct lookup hashes the type anddescription to narrow down the search to a small number of keys.

    • KEYRING_SEARCH_LOOKUP_ITERATE - An iterative lookup walks all thekeys in the keyring until one is matched. This must be used for anysearch that's not doing a simple direct match on the key description.

    The method may set cmp to point to a function of its choice that does someother form of match, may set lookup_type to KEYRING_SEARCH_LOOKUP_ITERATEand may attach something to the preparsed pointer for use by (*cmp)().(*cmp)() should return true if a key matches and false otherwise.

    If preparsed is set, it may be necessary to use the match_free() method toclean it up.

    The method should return 0 if successful or a negative error codeotherwise.

    It is permitted to sleep in this method, but (*cmp)() may not sleep aslocks will be held over it.

    If match_preparse() is not provided, keys of this type will be matchedexactly by their description.

  • void (*match_free)(struct key_match_data *match_data);

    This method is optional. If given, it called to clean upmatch_data->preparsed after a successful call to match_preparse().

  • void (*revoke)(struct key *key);

    This method is optional. It is called to discard part of the payloaddata upon a key being revoked. The caller will have the key semaphorewrite-locked.

    It is safe to sleep in this method, though care should be taken to avoida deadlock against the key semaphore.

  • void (*destroy)(struct key *key);

    This method is optional. It is called to discard the payload data on a keywhen it is being destroyed.

    This method does not need to lock the key to access the payload; it canconsider the key as being inaccessible at this time. Note that the key'stype may have been changed before this function is called.

    It is not safe to sleep in this method; the caller may hold spinlocks.

  • void (*describe)(const struct key *key, struct seq_file *p);

    This method is optional. It is called during /proc/keys reading tosummarise a key's description and payload in text form.

    This method will be called with the RCU read lock held. rcu_dereference()should be used to read the payload pointer if the payload is to beaccessed. key->datalen cannot be trusted to stay consistent with thecontents of the payload.

    The description will not change, though the key's state may.

    It is not safe to sleep in this method; the RCU read lock is held by thecaller.

  • long (*read)(const struct key *key, char __user *buffer, size_t buflen);

    This method is optional. It is called by KEYCTL_READ to translate thekey's payload into something a blob of data for userspace to deal with.Ideally, the blob should be in the same format as that passed in to theinstantiate and update methods.

    If successful, the blob size that could be produced should be returnedrather than the size copied.

    This method will be called with the key's semaphore read-locked. This willprevent the key's payload changing. It is not necessary to use RCU lockingwhen accessing the key's payload. It is safe to sleep in this method, suchas might happen when the userspace buffer is accessed.

  • int (*request_key)(struct key_construction *cons, const char *op, void *aux);

    This method is optional. If provided, request_key() and friends willinvoke this function rather than upcalling to /sbin/request-key to operateupon a key of this type.

    The aux parameter is as passed to request_key_async_with_auxdata() andsimilar or is NULL otherwise. Also passed are the construction record forthe key to be operated upon and the operation type (currently only"create").

    This method is permitted to return before the upcall is complete, but thefollowing function must be called under all circ*mstances to complete theinstantiation process, whether or not it succeeds, whether or not there'san error:

    void complete_request_key(struct key_construction *cons, int error);

    The error parameter should be 0 on success, -ve on error. Theconstruction record is destroyed by this action and the authorisation keywill be revoked. If an error is indicated, the key under constructionwill be negatively instantiated if it wasn't already instantiated.

    If this method returns an error, that error will be returned to thecaller of request_key*(). complete_request_key() must be called prior toreturning.

    The key under construction and the authorisation key can be found in thekey_construction struct pointed to by cons:

    • struct key *key;

      The key under construction.

    • struct key *authkey;

      The authorisation key.

  • struct key_restriction *(*lookup_restriction)(const char *params);

    This optional method is used to enable userspace configuration of keyringrestrictions. The restriction parameter string (not including the key typename) is passed in, and this method returns a pointer to a key_restrictionstructure containing the relevant functions and data to evaluate eachattempted key link operation. If there is no match, -EINVAL is returned.

  • asym_eds_op and asym_verify_signature:

    int (*asym_eds_op)(struct kernel_pkey_params *params, const void *in, void *out);int (*asym_verify_signature)(struct kernel_pkey_params *params, const void *in, const void *in2);

    These methods are optional. If provided the first allows a key to beused to encrypt, decrypt or sign a blob of data, and the second allows akey to verify a signature.

    In all cases, the following information is provided in the params block:

    struct kernel_pkey_params { struct key *key; const char *encoding; const char *hash_algo; char *info; __u32 in_len; union { __u32 out_len; __u32 in2_len; }; enum kernel_pkey_operation op : 8;};

    This includes the key to be used; a string indicating the encoding to use(for instance, "pkcs1" may be used with an RSA key to indicateRSASSA-PKCS1-v1.5 or RSAES-PKCS1-v1.5 encoding or "raw" if no encoding);the name of the hash algorithm used to generate the data for a signature(if appropriate); the sizes of the input and output (or second input)buffers; and the ID of the operation to be performed.

    For a given operation ID, the input and output buffers are used asfollows:

    Operation ID in,in_len out,out_len in2,in2_len======================= =============== =============== ===============kernel_pkey_encrypt Raw data Encrypted data -kernel_pkey_decrypt Encrypted data Raw data -kernel_pkey_sign Raw data Signature -kernel_pkey_verify Raw data - Signature

    asym_eds_op() deals with encryption, decryption and signature creation asspecified by params->op. Note that params->op is also set forasym_verify_signature().

    Encrypting and signature creation both take raw data in the input bufferand return the encrypted result in the output buffer. Padding may havebeen added if an encoding was set. In the case of signature creation,depending on the encoding, the padding created may need to indicate thedigest algorithm - the name of which should be supplied in hash_algo.

    Decryption takes encrypted data in the input buffer and returns the rawdata in the output buffer. Padding will get checked and stripped off ifan encoding was set.

    Verification takes raw data in the input buffer and the signature in thesecond input buffer and checks that the one matches the other. Paddingwill be validated. Depending on the encoding, the digest algorithm usedto generate the raw data may need to be indicated in hash_algo.

    If successful, asym_eds_op() should return the number of bytes writteninto the output buffer. asym_verify_signature() should return 0.

    A variety of errors may be returned, including EOPNOTSUPP if the operationis not supported; EKEYREJECTED if verification fails; ENOPKG if therequired crypto isn't available.

  • asym_query:

    int (*asym_query)(const struct kernel_pkey_params *params, struct kernel_pkey_query *info);

    This method is optional. If provided it allows information about thepublic or asymmetric key held in the key to be determined.

    The parameter block is as for asym_eds_op() and co. but in_len and out_lenare unused. The encoding and hash_algo fields should be used to reducethe returned buffer/data sizes as appropriate.

    If successful, the following information is filled in:

    struct kernel_pkey_query { __u32 supported_ops; __u32 key_size; __u16 max_data_size; __u16 max_sig_size; __u16 max_enc_size; __u16 max_dec_size;};

    The supported_ops field will contain a bitmask indicating what operationsare supported by the key, including encryption of a blob, decryption of ablob, signing a blob and verifying the signature on a blob. The followingconstants are defined for this:

    KEYCTL_SUPPORTS_{ENCRYPT,DECRYPT,SIGN,VERIFY}

    The key_size field is the size of the key in bits. max_data_size andmax_sig_size are the maximum raw data and signature sizes for creation andverification of a signature; max_enc_size and max_dec_size are the maximumraw data and signature sizes for encryption and decryption. Themax_*_size fields are measured in bytes.

    If successful, 0 will be returned. If the key doesn't support this,EOPNOTSUPP will be returned.

Request-Key Callback Service

To create a new key, the kernel will attempt to execute the following commandline:

/sbin/request-key create <key> <uid> <gid> \ <threadring> <processring> <sessionring> <callout_info>

<key> is the key being constructed, and the three keyrings are the processkeyrings from the process that caused the search to be issued. These areincluded for two reasons:

1 There may be an authentication token in one of the keyrings that is

required to obtain the key, eg: a Kerberos Ticket-Granting Ticket.

2 The new key should probably be cached in one of these rings.

This program should set it UID and GID to those specified before attempting toaccess any more keys. It may then look around for a user specific process tohand the request off to (perhaps a path held in placed in another key by, forexample, the KDE desktop manager).

The program (or whatever it calls) should finish construction of the key bycalling KEYCTL_INSTANTIATE or KEYCTL_INSTANTIATE_IOV, which also permits it tocache the key in one of the keyrings (probably the session ring) beforereturning. Alternatively, the key can be marked as negative with KEYCTL_NEGATEor KEYCTL_REJECT; this also permits the key to be cached in one of thekeyrings.

If it returns with the key remaining in the unconstructed state, the key willbe marked as being negative, it will be added to the session keyring, and anerror will be returned to the key requestor.

Supplementary information may be provided from whoever or whatever invoked thisservice. This will be passed as the <callout_info> parameter. If no suchinformation was made available, then "-" will be passed as this parameterinstead.

Similarly, the kernel may attempt to update an expired or a soon to expire keyby executing:

/sbin/request-key update <key> <uid> <gid> \ <threadring> <processring> <sessionring>

In this case, the program isn't required to actually attach the key to a ring;the rings are provided for reference.

Garbage Collection

Dead keys (for which the type has been removed) will be automatically unlinkedfrom those keyrings that point to them and deleted as soon as possible by abackground garbage collector.

Similarly, revoked and expired keys will be garbage collected, but only after acertain amount of time has passed. This time is set as a number of seconds in:

/proc/sys/kernel/keys/gc_delay
Kernel Key Retention Service — The Linux Kernel  documentation (2024)

References

Top Articles
Safelite Auto Glass Review: Services And Cost (2024)
12 Top-Rated Attractions & Things to Do in Springfield, MO
Funny Roblox Id Codes 2023
Www.mytotalrewards/Rtx
San Angelo, Texas: eine Oase für Kunstliebhaber
Golden Abyss - Chapter 5 - Lunar_Angel
Www.paystubportal.com/7-11 Login
Steamy Afternoon With Handsome Fernando
fltimes.com | Finger Lakes Times
Detroit Lions 50 50
18443168434
Newgate Honda
Zürich Stadion Letzigrund detailed interactive seating plan with seat & row numbers | Sitzplan Saalplan with Sitzplatz & Reihen Nummerierung
978-0137606801
Nwi Arrests Lake County
Missed Connections Dayton Ohio
Justified Official Series Trailer
London Ups Store
Committees Of Correspondence | Encyclopedia.com
Jinx Chapter 24: Release Date, Spoilers & Where To Read - OtakuKart
How Much You Should Be Tipping For Beauty Services - American Beauty Institute
How to Create Your Very Own Crossword Puzzle
Apply for a credit card
Unforeseen Drama: The Tower of Terror’s Mysterious Closure at Walt Disney World
Ups Print Store Near Me
How Taraswrld Leaks Exposed the Dark Side of TikTok Fame
University Of Michigan Paging System
Random Bibleizer
10 Best Places to Go and Things to Know for a Trip to the Hickory M...
Receptionist Position Near Me
Gopher Carts Pensacola Beach
Duke University Transcript Request
Nikki Catsouras: The Tragic Story Behind The Face And Body Images
Kiddie Jungle Parma
Lincoln Financial Field, section 110, row 4, home of Philadelphia Eagles, Temple Owls, page 1
The Latest: Trump addresses apparent assassination attempt on X
In Branch Chase Atm Near Me
Appleton Post Crescent Today's Obituaries
Craigslist Red Wing Mn
American Bully Xxl Black Panther
Ktbs Payroll Login
Jail View Sumter
Thotsbook Com
Funkin' on the Heights
Caesars Rewards Loyalty Program Review [Previously Total Rewards]
Marcel Boom X
Www Pig11 Net
Ty Glass Sentenced
Michaelangelo's Monkey Junction
Game Akin To Bingo Nyt
Ranking 134 college football teams after Week 1, from Georgia to Temple
Latest Posts
Article information

Author: Eusebia Nader

Last Updated:

Views: 5636

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Eusebia Nader

Birthday: 1994-11-11

Address: Apt. 721 977 Ebert Meadows, Jereville, GA 73618-6603

Phone: +2316203969400

Job: International Farming Consultant

Hobby: Reading, Photography, Shooting, Singing, Magic, Kayaking, Mushroom hunting

Introduction: My name is Eusebia Nader, I am a encouraging, brainy, lively, nice, famous, healthy, clever person who loves writing and wants to share my knowledge and understanding with you.