Blame man7/keyrings.7

Packit 7cfc04
.\"
Packit 7cfc04
.\" Copyright (C) 2014 Red Hat, Inc. All Rights Reserved.
Packit 7cfc04
.\" Written by David Howells (dhowells@redhat.com)
Packit 7cfc04
.\" and Copyright (C) 2016 Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(GPLv2+_SW_ONEPARA)
Packit 7cfc04
.\" This program is free software; you can redistribute it and/or
Packit 7cfc04
.\" modify it under the terms of the GNU General Public License
Packit 7cfc04
.\" as published by the Free Software Foundation; either version
Packit 7cfc04
.\" 2 of the License, or (at your option) any later version.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.TH KEYRINGS 7 2017-09-15 Linux "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
keyrings \- in-kernel key management and retention facility
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The Linux key-management facility
Packit 7cfc04
is primarily a way for various kernel components
Packit 7cfc04
to retain or cache security data,
Packit 7cfc04
authentication keys, encryption keys, and other data in the kernel.
Packit 7cfc04
.PP
Packit 7cfc04
System call interfaces are provided so that user-space programs can manage
Packit 7cfc04
those objects and also use the facility for their own purposes; see
Packit 7cfc04
.BR add_key (2),
Packit 7cfc04
.BR request_key (2),
Packit 7cfc04
and
Packit 7cfc04
.BR keyctl (2).
Packit 7cfc04
.PP
Packit 7cfc04
A library and some user-space utilities are provided to allow access to the
Packit 7cfc04
facility.
Packit 7cfc04
See
Packit 7cfc04
.BR keyctl (1),
Packit 7cfc04
.BR keyctl (3),
Packit 7cfc04
and
Packit 7cfc04
.BR keyutils (7)
Packit 7cfc04
for more information.
Packit 7cfc04
.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Packit 7cfc04
.SS Keys
Packit 7cfc04
A key has the following attributes:
Packit 7cfc04
.TP
Packit 7cfc04
Serial number (ID)
Packit 7cfc04
This is a unique integer handle by which a key is referred to in system calls.
Packit 7cfc04
The serial number is sometimes synonymously referred as the key ID.
Packit 7cfc04
Programmatically, key serial numbers are represented using the type
Packit 7cfc04
.IR key_serial_t .
Packit 7cfc04
.TP
Packit 7cfc04
Type
Packit 7cfc04
A key's type defines what sort of data can be held in the key,
Packit 7cfc04
how the proposed content of the key will be parsed,
Packit 7cfc04
and how the payload will be used.
Packit 7cfc04
.IP
Packit 7cfc04
There are a number of general-purpose types available, plus some specialist
Packit 7cfc04
types defined by specific kernel components.
Packit 7cfc04
.TP
Packit 7cfc04
Description (name)
Packit 7cfc04
The key description is a printable string that is used as the search term
Packit 7cfc04
for the key (in conjunction with the key type) as well as a display name.
Packit 7cfc04
During searches, the description may be partially matched or exactly matched.
Packit 7cfc04
.TP
Packit 7cfc04
Payload (data)
Packit 7cfc04
The payload is the actual content of a key.
Packit 7cfc04
This is usually set when a key is created,
Packit 7cfc04
but it is possible for the kernel to upcall to user space to finish the
Packit 7cfc04
instantiation of a key if that key wasn't already known to the kernel
Packit 7cfc04
when it was requested.
Packit 7cfc04
For further details, see
Packit 7cfc04
.BR request_key (2).
Packit 7cfc04
.IP
Packit 7cfc04
A key's payload can be read and updated if the key type supports it and if
Packit 7cfc04
suitable permission is granted to the caller.
Packit 7cfc04
.TP
Packit 7cfc04
Access rights
Packit 7cfc04
Much as files do,
Packit 7cfc04
each key has an owning user ID, an owning group ID, and a security label.
Packit 7cfc04
Each key also has a set of permissions,
Packit 7cfc04
though there are more than for a normal UNIX file,
Packit 7cfc04
and there is an additional category\(empossessor\(embeyond the usual user,
Packit 7cfc04
group, and other (see
Packit 7cfc04
.IR Possession ,
Packit 7cfc04
below).
Packit 7cfc04
.IP
Packit 7cfc04
Note that keys are quota controlled, since they require unswappable kernel
Packit 7cfc04
memory.
Packit 7cfc04
The owning user ID specifies whose quota is to be debited.
Packit 7cfc04
.TP
Packit 7cfc04
Expiration time
Packit 7cfc04
Each key can have an expiration time set.
Packit 7cfc04
When that time is reached,
Packit 7cfc04
the key is marked as being expired and accesses to it fail with the error
Packit 7cfc04
.BR EKEYEXPIRED .
Packit 7cfc04
If not deleted, updated, or replaced, then, after a set amount of time,
Packit 7cfc04
an expired key is automatically removed (garbage collected)
Packit 7cfc04
along with all links to it,
Packit 7cfc04
and attempts to access the key fail with the error
Packit 7cfc04
.BR ENOKEY .
Packit 7cfc04
.TP
Packit 7cfc04
Reference count
Packit 7cfc04
Each key has a reference count.
Packit 7cfc04
Keys are referenced by keyrings, by currently active users,
Packit 7cfc04
and by a process's credentials.
Packit 7cfc04
When the reference count reaches zero,
Packit 7cfc04
the key is scheduled for garbage collection.
Packit 7cfc04
.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Packit 7cfc04
.SS Key types
Packit 7cfc04
The kernel provides several basic types of key:
Packit 7cfc04
.TP
Packit 7cfc04
.I """keyring"""
Packit 7cfc04
'\" Note that keyrings use different fields in struct key in order to store
Packit 7cfc04
'\" their data - index_key instead of type/description and name_link/keys
Packit 7cfc04
'\" instead of payload.
Packit 7cfc04
Keyrings are special keys which store a set of links
Packit 7cfc04
to other keys (including other keyrings),
Packit 7cfc04
analogous to a directory holding links to files.
Packit 7cfc04
The main purpose of a keyring is to prevent other keys from
Packit 7cfc04
being garbage collected because nothing refers to them.
Packit 7cfc04
.IP
Packit 7cfc04
Keyrings with descriptions (names)
Packit 7cfc04
that begin with a period (\(aq.\(aq) are reserved to the implementation.
Packit 7cfc04
.TP
Packit 7cfc04
.I """user"""
Packit 7cfc04
This is a general-purpose key type.
Packit 7cfc04
The key is kept entirely within kernel memory.
Packit 7cfc04
The payload may be read and updated by user-space applications.
Packit 7cfc04
.IP
Packit 7cfc04
The payload for keys of this type is a blob of arbitrary data
Packit 7cfc04
of up to 32,767 bytes.
Packit 7cfc04
.IP
Packit 7cfc04
The description may be any valid string, though it is preferred that it
Packit 7cfc04
start with a colon-delimited prefix representing the service
Packit 7cfc04
to which the key is of interest
Packit 7cfc04
(for instance
Packit 7cfc04
.IR """afs:mykey""" ).
Packit 7cfc04
.TP
Packit 7cfc04
.IR """logon""" " (since Linux 3.3)"
Packit 7cfc04
.\" commit 9f6ed2ca257fa8650b876377833e6f14e272848b
Packit 7cfc04
This key type is essentially the same as
Packit 7cfc04
.IR """user""" ,
Packit 7cfc04
but it does not provide reading (i.e., the
Packit 7cfc04
.BR keyctl (2)
Packit 7cfc04
.BR KEYCTL_READ
Packit 7cfc04
operation),
Packit 7cfc04
meaning that the key payload is never visible from user space.
Packit 7cfc04
This is suitable for storing username-password pairs
Packit 7cfc04
that should not be readable from user space.
Packit 7cfc04
.IP
Packit 7cfc04
The description of a
Packit 7cfc04
.IR """logon"""
Packit 7cfc04
key
Packit 7cfc04
.I must\
Packit 7cfc04
start with a non-empty colon-delimited prefix whose purpose
Packit 7cfc04
is to identify the service to which the key belongs.
Packit 7cfc04
(Note that this differs from keys of the
Packit 7cfc04
.IR """user"""
Packit 7cfc04
type, where the inclusion of a prefix is recommended but is not enforced.)
Packit 7cfc04
.TP
Packit 7cfc04
.IR """big_key""" " (since Linux 3.13)"
Packit 7cfc04
.\" commit ab3c3587f8cda9083209a61dbe3a4407d3cada10
Packit 7cfc04
This key type is similar to the
Packit 7cfc04
.I """user"""
Packit 7cfc04
key type, but it may hold a payload of up to 1\ MiB in size.
Packit 7cfc04
This key type is useful for purposes such as holding Kerberos ticket caches.
Packit 7cfc04
.IP
Packit 7cfc04
The payload data may be stored in a tmpfs filesystem,
Packit 7cfc04
rather than in kernel memory,
Packit 7cfc04
if the data size exceeds the overhead of storing the data in the filesystem.
Packit 7cfc04
(Storing the data in a filesystem requires filesystem structures
Packit 7cfc04
to be allocated in the kernel.
Packit 7cfc04
The size of these structures determines the size threshold
Packit 7cfc04
above which the tmpfs storage method is used.)
Packit 7cfc04
Since Linux 4.8,
Packit 7cfc04
.\" commit 13100a72f40f5748a04017e0ab3df4cf27c809ef
Packit 7cfc04
the payload data is encrypted when stored in tmpfs,
Packit 7cfc04
thereby preventing it from being written unencrypted into swap space.
Packit 7cfc04
.PP
Packit 7cfc04
There are more specialized key types available also,
Packit 7cfc04
but they aren't discussed here
Packit 7cfc04
because they aren't intended for normal user-space use.
Packit 7cfc04
.PP
Packit 7cfc04
Key type names
Packit 7cfc04
that begin with a period (\(aq.\(aq) are reserved to the implementation.
Packit 7cfc04
.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Packit 7cfc04
.SS Keyrings
Packit 7cfc04
As previously mentioned, keyrings are a special type of key that contain
Packit 7cfc04
links to other keys (which may include other keyrings).
Packit 7cfc04
Keys may be linked to by multiple keyrings.
Packit 7cfc04
Keyrings may be considered as analogous to UNIX directories
Packit 7cfc04
where each directory contains a set of hard links to files.
Packit 7cfc04
.PP
Packit 7cfc04
Various operations (system calls) may be applied only to keyrings:
Packit 7cfc04
.IP Adding
Packit 7cfc04
A key may be added to a keyring by system calls that create keys.
Packit 7cfc04
This prevents the new key from being immediately deleted
Packit 7cfc04
when the system call releases its last reference to the key.
Packit 7cfc04
.IP Linking
Packit 7cfc04
A link may be added to a keyring pointing to a key that is already known,
Packit 7cfc04
provided this does not create a self-referential cycle.
Packit 7cfc04
.IP Unlinking
Packit 7cfc04
A link may be removed from a keyring.
Packit 7cfc04
When the last link to a key is removed,
Packit 7cfc04
that key will be scheduled for deletion by the garbage collector.
Packit 7cfc04
.IP Clearing
Packit 7cfc04
All the links may be removed from a keyring.
Packit 7cfc04
.IP Searching
Packit 7cfc04
A keyring may be considered the root of a tree or subtree in which keyrings
Packit 7cfc04
form the branches and non-keyrings the leaves.
Packit 7cfc04
This tree may be searched for a key matching
Packit 7cfc04
a particular type and description.
Packit 7cfc04
.PP
Packit 7cfc04
See
Packit 7cfc04
.BR keyctl_clear (3),
Packit 7cfc04
.BR keyctl_link (3),
Packit 7cfc04
.BR keyctl_search (3),
Packit 7cfc04
and
Packit 7cfc04
.BR keyctl_unlink (3)
Packit 7cfc04
for more information.
Packit 7cfc04
.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Packit 7cfc04
.SS Anchoring keys
Packit 7cfc04
To prevent a key from being garbage collected,
Packit 7cfc04
it must anchored to keep its reference count elevated
Packit 7cfc04
when it is not in active use by the kernel.
Packit 7cfc04
.PP
Packit 7cfc04
Keyrings are used to anchor other keys:
Packit 7cfc04
each link is a reference on a key.
Packit 7cfc04
Note that keyrings themselves are just keys and
Packit 7cfc04
are also subject to the same anchoring requirement to prevent
Packit 7cfc04
them being garbage collected.
Packit 7cfc04
.PP
Packit 7cfc04
The kernel makes available a number of anchor keyrings.
Packit 7cfc04
Note that some of these keyrings will be created only when first accessed.
Packit 7cfc04
.TP
Packit 7cfc04
Process keyrings
Packit 7cfc04
Process credentials themselves reference keyrings with specific semantics.
Packit 7cfc04
These keyrings are pinned as long as the set of credentials exists,
Packit 7cfc04
which is usually as long as the process exists.
Packit 7cfc04
.IP
Packit 7cfc04
There are three keyrings with different inheritance/sharing rules:
Packit 7cfc04
the
Packit 7cfc04
.BR session-keyring (7)
Packit 7cfc04
(inherited and shared by all child processes),
Packit 7cfc04
the
Packit 7cfc04
.BR process-keyring (7)
Packit 7cfc04
(shared by all threads in a process) and
Packit 7cfc04
the
Packit 7cfc04
.BR thread-keyring (7)
Packit 7cfc04
(specific to a particular thread).
Packit 7cfc04
.IP
Packit 7cfc04
As an alternative to using the actual keyring IDs,
Packit 7cfc04
in calls to
Packit 7cfc04
.BR add_key (2),
Packit 7cfc04
.BR keyctl (2),
Packit 7cfc04
and
Packit 7cfc04
.BR request_key (2),
Packit 7cfc04
the special keyring values
Packit 7cfc04
.BR KEY_SPEC_SESSION_KEYRING ,
Packit 7cfc04
.BR KEY_SPEC_PROCESS_KEYRING ,
Packit 7cfc04
and
Packit 7cfc04
.BR KEY_SPEC_THREAD_KEYRING
Packit 7cfc04
can be used to refer to the caller's own instances of these keyrings.
Packit 7cfc04
.TP
Packit 7cfc04
User keyrings
Packit 7cfc04
Each UID known to the kernel has a record that contains two keyrings: the
Packit 7cfc04
.BR user-keyring (7)
Packit 7cfc04
and the
Packit 7cfc04
.BR user-session-keyring (7).
Packit 7cfc04
These exist for as long as the UID record in the kernel exists.
Packit 7cfc04
.IP
Packit 7cfc04
As an alternative to using the actual keyring IDs,
Packit 7cfc04
in calls to
Packit 7cfc04
.BR add_key (2),
Packit 7cfc04
.BR keyctl (2),
Packit 7cfc04
and
Packit 7cfc04
.BR request_key (2),
Packit 7cfc04
the special keyring values
Packit 7cfc04
.BR KEY_SPEC_USER_KEYRING
Packit 7cfc04
and
Packit 7cfc04
.BR KEY_SPEC_USER_SESSION_KEYRING
Packit 7cfc04
can be used to refer to the caller's own instances of these keyrings.
Packit 7cfc04
.IP
Packit 7cfc04
A link to the user keyring is placed in a new session keyring by
Packit 7cfc04
.BR pam_keyinit (8)
Packit 7cfc04
when a new login session is initiated.
Packit 7cfc04
.TP
Packit 7cfc04
Persistent keyrings
Packit 7cfc04
There is a
Packit 7cfc04
.BR persistent-keyring (7)
Packit 7cfc04
available to each UID known to the system.
Packit 7cfc04
It may persist beyond the life of the UID record previously mentioned,
Packit 7cfc04
but has an expiration time set such that it is automatically cleaned up
Packit 7cfc04
after a set time.
Packit 7cfc04
The persistent keyring permits, for example,
Packit 7cfc04
.BR cron (8)
Packit 7cfc04
scripts to use credentials that are left in the persistent keyring after
Packit 7cfc04
the user logs out.
Packit 7cfc04
.IP
Packit 7cfc04
Note that the expiration time of the persistent keyring
Packit 7cfc04
is reset every time the persistent key is requested.
Packit 7cfc04
.TP
Packit 7cfc04
Special keyrings
Packit 7cfc04
There are special keyrings owned by the kernel that can anchor keys
Packit 7cfc04
for special purposes.
Packit 7cfc04
An example of this is the \fIsystem keyring\fR used for holding
Packit 7cfc04
encryption keys for module signature verification.
Packit 7cfc04
.IP
Packit 7cfc04
These special keyrings  are usually closed to direct alteration
Packit 7cfc04
by user space.
Packit 7cfc04
.PP
Packit 7cfc04
An originally planned "group keyring",
Packit 7cfc04
for storing keys associated with each GID known to the kernel,
Packit 7cfc04
is not so far implemented, is unlikely to be implemented.
Packit 7cfc04
Nevertheless, the constant
Packit 7cfc04
.BR KEY_SPEC_GROUP_KEYRING
Packit 7cfc04
has been defined for this keyring.
Packit 7cfc04
.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Packit 7cfc04
.SS Possession
Packit 7cfc04
The concept of possession is important to understanding the keyrings
Packit 7cfc04
security model.
Packit 7cfc04
Whether a thread possesses a key is determined by the following rules:
Packit 7cfc04
.IP (1) 4
Packit 7cfc04
Any key or keyring that does not grant
Packit 7cfc04
.I search
Packit 7cfc04
permission to the caller is ignored in all the following rules.
Packit 7cfc04
.IP (2)
Packit 7cfc04
A thread possesses its
Packit 7cfc04
.BR session-keyring (7),
Packit 7cfc04
.BR process-keyring (7),
Packit 7cfc04
and
Packit 7cfc04
.BR thread-keyring (7)
Packit 7cfc04
directly because those keyrings are referred to by its credentials.
Packit 7cfc04
.IP (3)
Packit 7cfc04
If a keyring is possessed, then any key it links to is also possessed.
Packit 7cfc04
.IP (4)
Packit 7cfc04
If any key a keyring links to is itself a keyring, then rule (3) applies
Packit 7cfc04
recursively.
Packit 7cfc04
.IP (5)
Packit 7cfc04
If a process is upcalled from the kernel to instantiate a key (see
Packit 7cfc04
.BR request_key (2)),
Packit 7cfc04
then it also possesses the requester's keyrings as in
Packit 7cfc04
rule (1) as if it were the requester.
Packit 7cfc04
.PP
Packit 7cfc04
Note that possession is not a fundamental property of a key,
Packit 7cfc04
but must rather be calculated each time the key is needed.
Packit 7cfc04
.PP
Packit 7cfc04
Possession is designed to allow set-user-ID programs run from, say
Packit 7cfc04
a user's shell to access the user's keys.
Packit 7cfc04
Granting permissions to the key possessor while denying them
Packit 7cfc04
to the key owner and group allows the prevention of access to keys
Packit 7cfc04
on the basis of UID and GID matches.
Packit 7cfc04
.PP
Packit 7cfc04
When it creates the session keyring,
Packit 7cfc04
.BR pam_keyinit (8)
Packit 7cfc04
adds a link to the
Packit 7cfc04
.BR user-keyring (7),
Packit 7cfc04
thus making the user keyring and anything it contains possessed by default.
Packit 7cfc04
.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Packit 7cfc04
.SS Access rights
Packit 7cfc04
Each key has the following security-related attributes:
Packit 7cfc04
.IP * 3
Packit 7cfc04
The owning user ID
Packit 7cfc04
.IP *
Packit 7cfc04
The ID of a group that is permitted to access the key
Packit 7cfc04
.IP *
Packit 7cfc04
A security label
Packit 7cfc04
.IP *
Packit 7cfc04
A permissions mask
Packit 7cfc04
.PP
Packit 7cfc04
The permissions mask contains four sets of rights.
Packit 7cfc04
The first three sets are mutually exclusive.
Packit 7cfc04
One and only one will be in force for a particular access check.
Packit 7cfc04
In order of descending priority, these three sets are:
Packit 7cfc04
.IP \fIuser\fR
Packit 7cfc04
The set specifies the rights granted
Packit 7cfc04
if the key's user ID matches the caller's filesystem user ID.
Packit 7cfc04
.IP \fIgroup\fR
Packit 7cfc04
The set specifies the rights granted
Packit 7cfc04
if the user ID didn't match and the key's group ID matches the caller's
Packit 7cfc04
filesystem GID or one of the caller's supplementary group IDs.
Packit 7cfc04
.IP \fIother\fR
Packit 7cfc04
The set specifies the rights granted
Packit 7cfc04
if neither the key's user ID nor group ID matched.
Packit 7cfc04
.PP
Packit 7cfc04
The fourth set of rights is:
Packit 7cfc04
.IP \fIpossessor\fR
Packit 7cfc04
The set specifies the rights granted
Packit 7cfc04
if a key is determined to be possessed by the caller.
Packit 7cfc04
.PP
Packit 7cfc04
The complete set of rights for a key is the union of whichever
Packit 7cfc04
of the first three sets is applicable plus the fourth set
Packit 7cfc04
if the key is possessed.
Packit 7cfc04
.PP
Packit 7cfc04
The set of rights that may be granted in each of the four masks
Packit 7cfc04
is as follows:
Packit 7cfc04
.TP
Packit 7cfc04
.I view
Packit 7cfc04
The attributes of the key may be read.
Packit 7cfc04
This includes the type,
Packit 7cfc04
description, and access rights (excluding the security label).
Packit 7cfc04
.TP
Packit 7cfc04
.I read
Packit 7cfc04
For a key: the payload of the key may be read.
Packit 7cfc04
For a keyring: the list of serial numbers (keys) to
Packit 7cfc04
which the keyring has links may be read.
Packit 7cfc04
.TP
Packit 7cfc04
.I write
Packit 7cfc04
The payload of the key may be updated and the key may be revoked.
Packit 7cfc04
For a keyring, links may be added to or removed from the keyring,
Packit 7cfc04
and the keyring may be cleared completely (all links are removed),
Packit 7cfc04
.TP
Packit 7cfc04
.I search
Packit 7cfc04
For a key (or a keyring): the key may be found by a search.
Packit 7cfc04
For a keyring: keys and keyrings that are linked to by the
Packit 7cfc04
keyring may be searched.
Packit 7cfc04
.TP
Packit 7cfc04
.I link
Packit 7cfc04
Links may be created from keyrings to the key.
Packit 7cfc04
The initial link to a key that is established when the key is created
Packit 7cfc04
doesn't require this permission.
Packit 7cfc04
.TP
Packit 7cfc04
.I setattr
Packit 7cfc04
The ownership details and security label of the key may be changed,
Packit 7cfc04
the key's expiration time may be set, and the key may be revoked.
Packit 7cfc04
.PP
Packit 7cfc04
In addition to access rights, any active Linux Security Module (LSM) may
Packit 7cfc04
prevent access to a key if its policy so dictates.
Packit 7cfc04
A key may be given a
Packit 7cfc04
security label or other attribute by the LSM;
Packit 7cfc04
this label is retrievable via
Packit 7cfc04
.BR keyctl_get_security (3).
Packit 7cfc04
.PP
Packit 7cfc04
See
Packit 7cfc04
.BR keyctl_chown (3),
Packit 7cfc04
.BR keyctl_describe (3),
Packit 7cfc04
.BR keyctl_get_security (3),
Packit 7cfc04
.BR keyctl_setperm (3),
Packit 7cfc04
and
Packit 7cfc04
.BR selinux (8)
Packit 7cfc04
for more information.
Packit 7cfc04
.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Packit 7cfc04
.SS Searching for keys
Packit 7cfc04
One of the key features of the Linux key-management facility
Packit 7cfc04
is the ability to find a key that a process is retaining.
Packit 7cfc04
The
Packit 7cfc04
.BR request_key (2)
Packit 7cfc04
system call is the primary point of
Packit 7cfc04
access for user-space applications to find a key.
Packit 7cfc04
(Internally, the kernel has something similar available
Packit 7cfc04
for use by internal components that make use of keys.)
Packit 7cfc04
.PP
Packit 7cfc04
The search algorithm works as follows:
Packit 7cfc04
.IP (1) 4
Packit 7cfc04
The process keyrings are searched in the following order: the thread
Packit 7cfc04
.BR thread-keyring (7)
Packit 7cfc04
if it exists, the
Packit 7cfc04
.BR process-keyring (7)
Packit 7cfc04
if it exists, and then either the
Packit 7cfc04
.BR session-keyring (7)
Packit 7cfc04
if it exists or the
Packit 7cfc04
.BR user-session-keyring (7)
Packit 7cfc04
if that exists.
Packit 7cfc04
.IP (2)
Packit 7cfc04
If the caller was a process that was invoked by the
Packit 7cfc04
.BR request_key (2)
Packit 7cfc04
upcall mechanism, then the keyrings of the original caller of
Packit 7cfc04
.BR request_key (2)
Packit 7cfc04
will be searched as well.
Packit 7cfc04
.IP (3)
Packit 7cfc04
The search of a keyring tree is in breadth-first order:
Packit 7cfc04
each keyring is searched first for a match,
Packit 7cfc04
then the keyrings referred to by that keyring are searched.
Packit 7cfc04
.IP (4)
Packit 7cfc04
If a matching key is found that is valid,
Packit 7cfc04
then the search terminates and that key is returned.
Packit 7cfc04
.IP (5)
Packit 7cfc04
If a matching key is found that has an error state attached,
Packit 7cfc04
that error state is noted and the search continues.
Packit 7cfc04
.IP (6)
Packit 7cfc04
If no valid matching key is found,
Packit 7cfc04
then the first noted error state is returned; otherwise, an
Packit 7cfc04
.B ENOKEY
Packit 7cfc04
error is returned.
Packit 7cfc04
.PP
Packit 7cfc04
It is also possible to search a specific keyring, in which case only steps
Packit 7cfc04
(3) to (6) apply.
Packit 7cfc04
.PP
Packit 7cfc04
See
Packit 7cfc04
.BR request_key (2)
Packit 7cfc04
and
Packit 7cfc04
.BR keyctl_search (3)
Packit 7cfc04
for more information.
Packit 7cfc04
.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Packit 7cfc04
.SS On-demand key creation
Packit 7cfc04
If a key cannot be found,
Packit 7cfc04
.BR request_key (2)
Packit 7cfc04
will, if given a
Packit 7cfc04
.I callout_info
Packit 7cfc04
argument, create a new key and then upcall to user space to
Packit 7cfc04
instantiate the key.
Packit 7cfc04
This allows keys to be created on an as-needed basis.
Packit 7cfc04
.PP
Packit 7cfc04
Typically,
Packit 7cfc04
this will involve the kernel creating a new process that executes the
Packit 7cfc04
.BR request-key (8)
Packit 7cfc04
program, which will then execute the appropriate handler based on its
Packit 7cfc04
configuration.
Packit 7cfc04
.PP
Packit 7cfc04
The handler is passed a special authorization key that allows it
Packit 7cfc04
and only it to instantiate the new key.
Packit 7cfc04
This is also used to permit searches performed by the
Packit 7cfc04
handler program to also search the requester's keyrings.
Packit 7cfc04
.PP
Packit 7cfc04
See
Packit 7cfc04
.BR request_key (2),
Packit 7cfc04
.BR keyctl_assume_authority (3),
Packit 7cfc04
.BR keyctl_instantiate (3),
Packit 7cfc04
.BR keyctl_negate (3),
Packit 7cfc04
.BR keyctl_reject (3),
Packit 7cfc04
.BR request-key (8),
Packit 7cfc04
and
Packit 7cfc04
.BR request-key.conf (5)
Packit 7cfc04
for more information.
Packit 7cfc04
.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Packit 7cfc04
.SS /proc files
Packit 7cfc04
The kernel provides various
Packit 7cfc04
.I /proc
Packit 7cfc04
files that expose information about keys or define limits on key usage.
Packit 7cfc04
.TP
Packit 7cfc04
.IR /proc/keys " (since Linux 2.6.10)"
Packit 7cfc04
This file exposes a list of the keys for which the reading thread has
Packit 7cfc04
.I view
Packit 7cfc04
permission, providing various information about each key.
Packit 7cfc04
The thread need not possess the key for it to be visible in this file.
Packit 7cfc04
.\" David Howells, Dec 2016 linux-man@:
Packit 7cfc04
.\" This [The thread need not possess the key for it to be visible in
Packit 7cfc04
.\" this file.] is correct.  See proc_keys_show() in security/keys/proc.c:
Packit 7cfc04
.\"
Packit 7cfc04
.\"	rc = key_task_permission(key_ref, ctx.cred, KEY_NEED_VIEW);
Packit 7cfc04
.\"	if (rc < 0)
Packit 7cfc04
.\"		return 0;
Packit 7cfc04
.\"
Packit 7cfc04
.\"Possibly it shouldn't be, but for now it is.
Packit 7cfc04
.\"
Packit 7cfc04
.IP
Packit 7cfc04
The only keys included in the list are those that grant
Packit 7cfc04
.I view
Packit 7cfc04
permission to the reading process
Packit 7cfc04
(regardless of whether or not it possesses them).
Packit 7cfc04
LSM security checks are still performed,
Packit 7cfc04
and may filter out further keys that the process is not authorized to view.
Packit 7cfc04
.IP
Packit 7cfc04
An example of the data that one might see in this file
Packit 7cfc04
(with the columns numbered for easy reference below)
Packit 7cfc04
is the following:
Packit 7cfc04
.IP
Packit 7cfc04
.in 0n
Packit 7cfc04
.EX
Packit 7cfc04
  (1)     (2)     (3)(4)    (5)     (6)   (7)   (8)        (9)
Packit 7cfc04
009a2028 I--Q---   1 perm 3f010000  1000  1000 user     krb_ccache:primary: 12
Packit 7cfc04
1806c4ba I--Q---   1 perm 3f010000  1000  1000 keyring  _pid: 2
Packit 7cfc04
25d3a08f I--Q---   1 perm 1f3f0000  1000 65534 keyring  _uid_ses.1000: 1
Packit 7cfc04
28576bd8 I--Q---   3 perm 3f010000  1000  1000 keyring  _krb: 1
Packit 7cfc04
2c546d21 I--Q--- 190 perm 3f030000  1000  1000 keyring  _ses: 2
Packit 7cfc04
30a4e0be I------   4   2d 1f030000  1000 65534 keyring  _persistent.1000: 1
Packit 7cfc04
32100fab I--Q---   4 perm 1f3f0000  1000 65534 keyring  _uid.1000: 2
Packit 7cfc04
32a387ea I--Q---   1 perm 3f010000  1000  1000 keyring  _pid: 2
Packit 7cfc04
3ce56aea I--Q---   5 perm 3f030000  1000  1000 keyring  _ses: 1
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
The fields shown in each line of this file are as follows:
Packit 7cfc04
.RS
Packit 7cfc04
.TP
Packit 7cfc04
ID (1)
Packit 7cfc04
The ID (serial number) of the key, expressed in hexadecimal.
Packit 7cfc04
.TP
Packit 7cfc04
Flags (2)
Packit 7cfc04
A set of flags describing the state of the key:
Packit 7cfc04
.RS
Packit 7cfc04
.IP I 4
Packit 7cfc04
.\" KEY_FLAG_INSTANTIATED
Packit 7cfc04
The key has been instantiated.
Packit 7cfc04
.IP R
Packit 7cfc04
.\" KEY_FLAG_REVOKED
Packit 7cfc04
The key has been revoked.
Packit 7cfc04
.IP D
Packit 7cfc04
.\" KEY_FLAG_DEAD
Packit 7cfc04
The key is dead (i.e., the key type has been unregistered).
Packit 7cfc04
.\" unregister_key_type() in the kernel source
Packit 7cfc04
(A key may be briefly in this state during garbage collection.)
Packit 7cfc04
.IP Q
Packit 7cfc04
.\" KEY_FLAG_IN_QUOTA
Packit 7cfc04
The key contributes to the user's quota.
Packit 7cfc04
.IP U
Packit 7cfc04
.\" KEY_FLAG_USER_CONSTRUCT
Packit 7cfc04
The key is under construction via a callback to user space;
Packit 7cfc04
see
Packit 7cfc04
.BR request-key (2).
Packit 7cfc04
.IP N
Packit 7cfc04
.\" KEY_FLAG_NEGATIVE
Packit 7cfc04
The key is negatively instantiated.
Packit 7cfc04
.IP i
Packit 7cfc04
.\" KEY_FLAG_INVALIDATED
Packit 7cfc04
The key has been invalidated.
Packit 7cfc04
.RE
Packit 7cfc04
.TP
Packit 7cfc04
Usage (3)
Packit 7cfc04
This is a count of the number of kernel credential
Packit 7cfc04
structures that are pinning the key
Packit 7cfc04
(approximately: the number of threads and open file references
Packit 7cfc04
that refer to this key).
Packit 7cfc04
.TP
Packit 7cfc04
Timeout (4)
Packit 7cfc04
The amount of time until the key will expire,
Packit 7cfc04
expressed in human-readable form (weeks, days, hours, minutes, and seconds).
Packit 7cfc04
The string
Packit 7cfc04
.I perm
Packit 7cfc04
here means that the key is permanent (no timeout).
Packit 7cfc04
The string
Packit 7cfc04
.I expd
Packit 7cfc04
means that the key has already expired,
Packit 7cfc04
but has not yet been garbage collected.
Packit 7cfc04
.TP
Packit 7cfc04
Permissions (5)
Packit 7cfc04
The key permissions, expressed as four hexadecimal bytes containing,
Packit 7cfc04
from left to right, the possessor, user, group, and other permissions.
Packit 7cfc04
Within each byte, the permission bits are as follows:
Packit 7cfc04
.IP
Packit 7cfc04
.PD 0
Packit 7cfc04
.RS 12
Packit 7cfc04
.TP
Packit 7cfc04
0x01
Packit 7cfc04
.I view
Packit 7cfc04
.TP
Packit 7cfc04
Ox02
Packit 7cfc04
.I read
Packit 7cfc04
.TP
Packit 7cfc04
0x04
Packit 7cfc04
.I write
Packit 7cfc04
.TP
Packit 7cfc04
0x08
Packit 7cfc04
.I search
Packit 7cfc04
.TP
Packit 7cfc04
0x10
Packit 7cfc04
.I link
Packit 7cfc04
.TP
Packit 7cfc04
0x20
Packit 7cfc04
.I setattr
Packit 7cfc04
.RE
Packit 7cfc04
.PD
Packit 7cfc04
.TP
Packit 7cfc04
UID (6)
Packit 7cfc04
The user ID of the key owner.
Packit 7cfc04
.TP
Packit 7cfc04
GID (7)
Packit 7cfc04
The group ID of the key.
Packit 7cfc04
The value \-1 here means that the key has no group ID;
Packit 7cfc04
this can occur in certain circumstances for keys created by the kernel.
Packit 7cfc04
.TP
Packit 7cfc04
Type (8)
Packit 7cfc04
The key type (user, keyring, etc.)
Packit 7cfc04
.TP
Packit 7cfc04
Description (9)
Packit 7cfc04
The key description (name).
Packit 7cfc04
This field contains descriptive information about the key.
Packit 7cfc04
For most key types, it has the form
Packit 7cfc04
.IP
Packit 7cfc04
     name[: extra\-info]
Packit 7cfc04
.IP
Packit 7cfc04
The
Packit 7cfc04
.I name
Packit 7cfc04
subfield is the key's description (name).
Packit 7cfc04
The optional
Packit 7cfc04
.I extra\-info
Packit 7cfc04
field provides some further information about the key.
Packit 7cfc04
The information that appears here depends on the key type, as follows:
Packit 7cfc04
.RS
Packit 7cfc04
.TP 4
Packit 7cfc04
.IR """user""" " and " """logon"""
Packit 7cfc04
The size in bytes of the key payload (expressed in decimal).
Packit 7cfc04
.TP
Packit 7cfc04
.IR """keyring"""
Packit 7cfc04
The number of keys linked to the keyring,
Packit 7cfc04
or the string
Packit 7cfc04
.IR empty
Packit 7cfc04
if there are no keys linked to the keyring.
Packit 7cfc04
.TP
Packit 7cfc04
.IR """big_key"""
Packit 7cfc04
The payload size in bytes, followed either by the string
Packit 7cfc04
.IR [file] ,
Packit 7cfc04
if the key payload exceeds the threshold that means that the
Packit 7cfc04
payload is stored in a (swappable)
Packit 7cfc04
.BR tmpfs (5)
Packit 7cfc04
filesystem,
Packit 7cfc04
or otherwise the string
Packit 7cfc04
.IR [buff] ,
Packit 7cfc04
indicating that the key is small enough to reside in kernel memory.
Packit 7cfc04
.RE
Packit 7cfc04
.IP
Packit 7cfc04
For the
Packit 7cfc04
.IR """.request_key_auth"""
Packit 7cfc04
key type
Packit 7cfc04
(authorization key; see
Packit 7cfc04
.BR request_key (2)),
Packit 7cfc04
the description field has the form shown in the following example:
Packit 7cfc04
.IP
Packit 7cfc04
    key:c9a9b19 pid:28880 ci:10
Packit 7cfc04
.IP
Packit 7cfc04
The three subfields are as follows:
Packit 7cfc04
.RS
Packit 7cfc04
.TP 5
Packit 7cfc04
.I key
Packit 7cfc04
The hexadecimal ID of the key being instantiated in the requesting program.
Packit 7cfc04
.TP
Packit 7cfc04
.I pid
Packit 7cfc04
The PID of the requesting program.
Packit 7cfc04
.TP
Packit 7cfc04
.I ci
Packit 7cfc04
The length of the callout data with which the requested key should
Packit 7cfc04
be instantiated
Packit 7cfc04
(i.e., the length of the payload associated with the authorization key).
Packit 7cfc04
.RE
Packit 7cfc04
.RE
Packit 7cfc04
.TP
Packit 7cfc04
.IR /proc/key-users " (since Linux 2.6.10)"
Packit 7cfc04
This file lists various information for each user ID that
Packit 7cfc04
has at least one key on the system.
Packit 7cfc04
An example of the data that one might see in this file is the following:
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
   0:    10 9/9 2/1000000 22/25000000
Packit 7cfc04
  42:     9 9/9 8/200 106/20000
Packit 7cfc04
1000:    11 11/11 10/200 271/20000
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
The fields shown in each line are as follows:
Packit 7cfc04
.RS
Packit 7cfc04
.TP
Packit 7cfc04
.I uid
Packit 7cfc04
The user ID.
Packit 7cfc04
.TP
Packit 7cfc04
.I usage
Packit 7cfc04
This is a kernel-internal usage count for the kernel structure
Packit 7cfc04
used to record key users.
Packit 7cfc04
.TP
Packit 7cfc04
.IR nkeys / nikeys
Packit 7cfc04
The total number of keys owned by the user,
Packit 7cfc04
and the number of those keys that have been instantiated.
Packit 7cfc04
.TP
Packit 7cfc04
.IR qnkeys / maxkeys
Packit 7cfc04
The number of keys owned by the user,
Packit 7cfc04
and the maximum number of keys that the user may own.
Packit 7cfc04
.TP
Packit 7cfc04
.IR qnbytes / maxbytes
Packit 7cfc04
The number of bytes consumed in payloads of the keys owned by this user,
Packit 7cfc04
and the upper limit on the number of bytes in key payloads for that user.
Packit 7cfc04
.RE
Packit 7cfc04
.TP
Packit 7cfc04
.IR /proc/sys/kernel/keys/gc_delay " (since Linux 2.6.32)"
Packit 7cfc04
.\" commit 5d135440faf7db8d566de0c6fab36b16cf9cfc3b
Packit 7cfc04
The value in this file specifies the interval, in seconds,
Packit 7cfc04
after which revoked and expired keys will be garbage collected.
Packit 7cfc04
The purpose of having such an interval is so that there is a window
Packit 7cfc04
of time where user space can see an error (respectively
Packit 7cfc04
.BR EKEYREVOKED
Packit 7cfc04
and
Packit 7cfc04
.BR EKEYEXPIRED )
Packit 7cfc04
that indicates what happened to the key.
Packit 7cfc04
.IP
Packit 7cfc04
The default value in this file is 300 (i.e., 5 minutes).
Packit 7cfc04
.TP
Packit 7cfc04
.IR /proc/sys/kernel/keys/persistent_keyring_expiry " (since Linux 3.13)"
Packit 7cfc04
.\" commit f36f8c75ae2e7d4da34f4c908cebdb4aa42c977e
Packit 7cfc04
This file defines an interval, in seconds,
Packit 7cfc04
to which the persistent keyring's expiration timer is reset
Packit 7cfc04
each time the keyring is accessed (via
Packit 7cfc04
.BR keyctl_get_persistent (3)
Packit 7cfc04
or the
Packit 7cfc04
.BR keyctl (2)
Packit 7cfc04
.B KEYCTL_GET_PERSISTENT
Packit 7cfc04
operation.)
Packit 7cfc04
.IP
Packit 7cfc04
The default value in this file is 259200 (i.e., 3 days).
Packit 7cfc04
.PP
Packit 7cfc04
The following files (which are writable by privileged processes)
Packit 7cfc04
are used to enforce quotas on the number of keys
Packit 7cfc04
and number of bytes of data that can be stored in key payloads:
Packit 7cfc04
.TP
Packit 7cfc04
.IR /proc/sys/kernel/keys/maxbytes " (since Linux 2.6.26)"
Packit 7cfc04
.\" commit 0b77f5bfb45c13e1e5142374f9d6ca75292252a4
Packit 7cfc04
.\" Previously: KEYQUOTA_MAX_BYTES      10000
Packit 7cfc04
This is the maximum number of bytes of data that a nonroot user
Packit 7cfc04
can hold in the payloads of the keys owned by the user.
Packit 7cfc04
.IP
Packit 7cfc04
The default value in this file is 20,000.
Packit 7cfc04
.TP
Packit 7cfc04
.IR /proc/sys/kernel/keys/maxkeys " (since Linux 2.6.26)"
Packit 7cfc04
.\" commit 0b77f5bfb45c13e1e5142374f9d6ca75292252a4
Packit 7cfc04
.\" Previously: KEYQUOTA_MAX_KEYS       100
Packit 7cfc04
This is the maximum number of keys that a nonroot user may own.
Packit 7cfc04
.IP
Packit 7cfc04
The default value in this file is 200.
Packit 7cfc04
.TP
Packit 7cfc04
.IR /proc/sys/kernel/keys/root_maxbytes " (since Linux 2.6.26)"
Packit 7cfc04
This is the maximum number of bytes of data that the root user
Packit 7cfc04
(UID 0 in the root user namespace)
Packit 7cfc04
can hold in the payloads of the keys owned by root.
Packit 7cfc04
.IP
Packit 7cfc04
.\"738c5d190f6540539a04baf36ce21d46b5da04bd
Packit 7cfc04
The default value in this file is 25,000,000 (20,000 before Linux 3.17).
Packit 7cfc04
.\" commit 0b77f5bfb45c13e1e5142374f9d6ca75292252a4
Packit 7cfc04
.TP
Packit 7cfc04
.IR /proc/sys/kernel/keys/root_maxkeys " (since Linux 2.6.26)"
Packit 7cfc04
.\" commit 0b77f5bfb45c13e1e5142374f9d6ca75292252a4
Packit 7cfc04
This is the maximum number of keys that the root user
Packit 7cfc04
(UID 0 in the root user namespace)
Packit 7cfc04
may own.
Packit 7cfc04
.IP
Packit 7cfc04
.\"738c5d190f6540539a04baf36ce21d46b5da04bd
Packit 7cfc04
The default value in this file is 1,000,000 (200 before Linux 3.17).
Packit 7cfc04
.PP
Packit 7cfc04
With respect to keyrings,
Packit 7cfc04
note that each link in a keyring consumes 4 bytes of the keyring payload.
Packit 7cfc04
.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Packit 7cfc04
.SS Users
Packit 7cfc04
The Linux key-management facility has a number of users and usages,
Packit 7cfc04
but is not limited to those that already exist.
Packit 7cfc04
.PP
Packit 7cfc04
In-kernel users of this facility include:
Packit 7cfc04
.TP
Packit 7cfc04
Network filesystems - DNS
Packit 7cfc04
The kernel uses the upcall mechanism provided by the keys to upcall to
Packit 7cfc04
user space to do DNS lookups and then to cache the results.
Packit 7cfc04
.TP
Packit 7cfc04
AF_RXRPC and kAFS - Authentication
Packit 7cfc04
The AF_RXRPC network protocol and the in-kernel AFS filesystem
Packit 7cfc04
use keys to store the ticket needed to do secured or encrypted traffic.
Packit 7cfc04
These are then looked up by
Packit 7cfc04
network operations on AF_RXRPC and filesystem operations on kAFS.
Packit 7cfc04
.TP
Packit 7cfc04
NFS - User ID mapping
Packit 7cfc04
The NFS filesystem uses keys to store mappings of
Packit 7cfc04
foreign user IDs to local user IDs.
Packit 7cfc04
.TP
Packit 7cfc04
CIFS - Password
Packit 7cfc04
The CIFS filesystem uses keys to store passwords for accessing remote shares.
Packit 7cfc04
.TP
Packit 7cfc04
Module verification
Packit 7cfc04
The kernel build process can be made to cryptographically sign modules.
Packit 7cfc04
That signature is then checked when a module is loaded.
Packit 7cfc04
.PP
Packit 7cfc04
User-space users of this facility include:
Packit 7cfc04
.TP
Packit 7cfc04
Kerberos key storage
Packit 7cfc04
The MIT Kerberos 5 facility (libkrb5) can use keys to store authentication
Packit 7cfc04
tokens which can be made to be automatically cleaned up a set time after
Packit 7cfc04
the user last uses them,
Packit 7cfc04
but until then permits them to hang around after the user
Packit 7cfc04
has logged out so that
Packit 7cfc04
.BR cron (8)
Packit 7cfc04
scripts can use them.
Packit 7cfc04
.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.ad l
Packit 7cfc04
.nh
Packit 7cfc04
.BR keyctl (1),
Packit 7cfc04
.BR add_key (2),
Packit 7cfc04
.BR keyctl (2),
Packit 7cfc04
.BR request_key (2),
Packit 7cfc04
.BR keyctl (3),
Packit 7cfc04
.BR keyutils (7),
Packit 7cfc04
.BR persistent\-keyring (7),
Packit 7cfc04
.BR process\-keyring (7),
Packit 7cfc04
.BR session\-keyring (7),
Packit 7cfc04
.BR thread\-keyring (7),
Packit 7cfc04
.BR user\-keyring (7),
Packit 7cfc04
.BR user\-session\-keyring (7),
Packit 7cfc04
.BR pam_keyinit (8),
Packit 7cfc04
.BR request-key (8)
Packit 7cfc04
.PP
Packit 7cfc04
The kernel source files
Packit 7cfc04
.IR Documentation/crypto/asymmetric-keys.txt
Packit 7cfc04
and under
Packit 7cfc04
.IR Documentation/security/keys
Packit 7cfc04
(or, before Linux 4.13, in the file
Packit 7cfc04
.IR Documentation/security/keys.txt ).
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.