]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/keyrings.7
keyrings.7: Document /proc/sys/kernel/keys/gc_delay
[thirdparty/man-pages.git] / man7 / keyrings.7
CommitLineData
6b71fd9a
DH
1.\"
2.\" Copyright (C) 2014 Red Hat, Inc. All Rights Reserved.
3.\" Written by David Howells (dhowells@redhat.com)
4.\"
1ba9d9e5 5.\" %%%LICENSE_START(GPLv2+_SW_ONEPARA)
6b71fd9a
DH
6.\" This program is free software; you can redistribute it and/or
7.\" modify it under the terms of the GNU General Public Licence
8.\" as published by the Free Software Foundation; either version
9.\" 2 of the Licence, or (at your option) any later version.
1ba9d9e5 10.\" %%%LICENSE_END
6b71fd9a 11.\"
e7ca6b3c 12.TH KEYRINGS 7 2016-11-01 Linux "Linux Programmer's Manual"
6b71fd9a 13.SH NAME
8c5a425a 14keyrings \- in-kernel key management and retention facility
fe2d2f79 15.SH DESCRIPTION
e650d927
MK
16The Linux key-management facility
17is primarily a way for drivers to retain or cache security data,
c1f7a90f 18authentication keys, encryption keys, and other data in the kernel.
6b71fd9a 19.P
6d6d803e 20System call interfaces are provided so that user-space programs can manage those
6b71fd9a
DH
21objects and also use the facility for their own purposes.
22.P
6d6d803e 23A library and some user-space utilities are provided to allow access to the
a44454bc
MK
24facility.
25See
e650d927
MK
26.BR keyctl (1),
27.BR keyctl (3),
28and
6b71fd9a 29.BR keyutils (7)
f437df79 30for more information.
6b71fd9a 31.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
fe2d2f79 32.SS Keys
e650d927
MK
33A key has the following attributes:
34.TP
35Serial number
6b71fd9a
DH
36This is a unique integer handle by which a key is referred to in system call
37arguments.
e650d927
MK
38The serial number is sometimes synonymously referred the key ID.
39Programmatically, key serial numbers are represented using the type
40.IR key_serial_t .
41.TP
42Type
43A key's type defines what sort of data can be held in the key,
44how the proposed content of the key will be parsed,
45and how the payload will be used.
46
6b71fd9a
DH
47There are a number of general purpose types available, plus some specialist
48types defined by specific drivers.
e650d927
MK
49.TP
50Description (name)
51The key description is a printable string that is used as the search term
52for the key (in conjunction with the key type) as well as a display name.
53During searches, the description may be partially matched or exactly matched.
54.TP
55Payload
56The payload is the actual content of a key.
a44454bc 57This is usually set when a key is created,
6d6d803e 58but it is possible for the kernel to upcall to user space to finish the
a44454bc
MK
59instantiation of a key if that key wasn't already known to the kernel
60when it was requested.
e650d927
MK
61(Details can be found in
62.BR request_key (2).)
63
6b71fd9a
DH
64A key's payload can be read and updated if the key type supports it and if
65suitable permission is granted to the caller.
e650d927
MK
66.TP
67Access rights
630abd84
MK
68Much as files do,
69each key has an owning user ID, an owning group ID, and a security label.
a44454bc
MK
70files do.
71They also have a set of permissions,
72though there are more than for a normal UNIX file,
73and there is an additional category beyond the usual user,
e650d927
MK
74group, and other (see below).
75
6b71fd9a
DH
76Note that keys are quota controlled since they represent unswappable kernel
77memory and the owning user ID specifies whose quota is to be debited.
e650d927
MK
78.TP
79Expiration time
a44454bc
MK
80Each key can have an expiration time set.
81When that time is reached,
82the key is marked as being expired and accesses to it fail with
f437df79 83.BR EKEYEXPIRED .
e650d927
MK
84If not deleted, updated, or replaced, after a set amount of time,
85expired keys are automatically removed along with all links to them,
86and attempts to access the key will fail with the error
87.BR ENOKEY .
88.TP
89Reference count
a44454bc 90Each key has a reference count.
e650d927 91Keys are referenced by keyrings, by currently active users,
a44454bc
MK
92and by a process's credentials.
93When the reference count reaches zero,
94the key is scheduled for garbage collection.
6b71fd9a 95.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
fe2d2f79 96.SS Key types
6b71fd9a 97The facility provides several basic types of key:
e650d927
MK
98.TP
99.I """keyring"""
a44454bc
MK
100Keys of this type are special.
101The payload consists of a set of links to other
102keys, analogous to a directory holding links to files.
103The main purpose of a keyring is to prevent other keys from
104being garbage collected because nothing refers to them.
e650d927
MK
105.TP
106.I """user"""
a44454bc
MK
107This is a general purpose key type.
108It may be instantiated with an arbitrary blob of data of up to about 32KB.
109It is kept entirely within kernel memory.
6d6d803e 110It may be read and updated by user-space applications
e650d927
MK
111.TP
112.I """big_key"""
113This is similar to the
114.I """user"""
115key type, but it may hold a payload of up to 1MiB in size.
a44454bc
MK
116The data may be stored in the swap space rather than in kernel memory
117if the size exceeds the overhead of doing so
630abd84 118(a tmpfs file is used, which requires filesystem structures
a44454bc 119to be allocated in the kernel).
e650d927
MK
120.TP
121.I """logon"""
122This is similar to the
123.I """user"""
124key type, but the contents may not be read by user-space applications.
125.PP
6d6d803e
MK
126There are more specialized key types available also, but they're not discussed
127here as they're not intended for normal user-space use.
6b71fd9a 128.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
fe2d2f79 129.SS Keyrings
6b71fd9a 130As previously mentioned, keyrings are a special type of key that contain links
a44454bc
MK
131to other keys (which may include other keyrings).
132Keys may be linked to by multiple keyrings.
133Keyrings may be considered as analogous to UNIX directories
6b71fd9a
DH
134where each directory contains a set of hard links to files.
135.P
c1f7a90f 136Various operations (system calls) may be applied only to keyrings:
6b71fd9a 137.IP "\fBAdding\fR"
a44454bc
MK
138A key may be added to a keyring by system calls that create keys.
139This prevents the new key from being immediately deleted
140when the system call driver releases its last reference to the key.
6b71fd9a
DH
141.IP "\fBLinking\fR"
142A link may be added to a keyring pointing to a key that is already known,
143provided this does not create a self-referential cycle.
144.IP "\fBUnlinking\fR"
a44454bc
MK
145A link may be removed from a keyring.
146When the last link to a key is removed,
6b71fd9a
DH
147that key will be scheduled for deletion by the garbage collector.
148.IP "\fBClearing\fR"
149All the links may be removed from a keyring.
150.IP "\fBSearching\fR"
151A keyring may be considered the root of a tree or subtree in which keyrings
a44454bc
MK
152form the branches and non-keyrings the leaves.
153This tree may be searched for a leaf matching
154a particular type and description.
6b71fd9a 155.P
bf0dcc15 156See
6b71fd9a
DH
157.BR keyctl_clear (3),
158.BR keyctl_link (3),
e650d927 159.BR keyctl_search (3),
6b71fd9a
DH
160and
161.BR keyctl_unlink (3)
bf0dcc15 162for more information.
6b71fd9a 163.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
fe2d2f79 164.SS Anchoring keys
a44454bc
MK
165To prevent a key from being prematurely garbage collected,
166it must anchored to keep its reference count elevated
167when it is not in active use by the kernel.
6b71fd9a
DH
168.P
169\fBKeyrings\fR are used to anchor other keys - each link is a reference on a
170key - but whilst keyrings are available to link to keys, keyrings themselves
171are just keys and are also subject to the same anchoring necessity.
172.P
a44454bc 173The kernel makes available a number of anchor keyrings.
c1f7a90f 174Note that some of these keyrings will be created only when first accessed.
6b71fd9a
DH
175.IP "\fBProcess keyrings\fR"
176Process credentials themselves reference keyrings with specific semantics.
630abd84
MK
177These keyrings are pinned as long as the set of credentials exists,
178which is usually as long as the process exists.
6b71fd9a 179.IP
a44454bc 180There are three keyrings with different inheritance/sharing rules:
f437df79
MK
181The
182.BR session-keyring (7)
183(inherited and shared by all child processes),
184the
185.BR process-keyring (7)
186(shared by all threads in a process) and
187the
188.BR thread-keyring (7)
189(specific to a particular thread).
6b71fd9a
DH
190.IP "\fBUser keyrings\fR"
191Each UID known to the kernel has a record that contains two keyrings: The
f437df79
MK
192.BR user-keyring (7)
193and the
194.BR user-session-keyring (7).
a44454bc
MK
195These exist for as long as the UID record in the kernel exists.
196A link to the user keyring is placed in a new session keyring by
f437df79
MK
197.BR pam_keyinit (8)
198when a new login session is initiated.
6b71fd9a 199.IP "\fBPersistent keyrings\fR"
f437df79
MK
200There is a
201.BR persistent-keyring (7)
202available to each UID known to the system.
a44454bc
MK
203It may persist beyond the life of the UID record previously mentioned,
204but has an expiration time set such that it is automatically cleaned up
205after a set time.
206This, for example, permits cron scripts to use credentials left when the
6b71fd9a
DH
207user logs out.
208.IP
209Note that the expiration time is reset every time the persistent key is
210requested.
211.IP "\fBSpecial keyrings\fR"
a44454bc
MK
212There are special keyrings owned by the kernel that can anchor keys
213for special purposes.
214An example of this is the \fBsystem keyring\fR used for holding
6b71fd9a
DH
215encryption keys for module signature verification.
216.IP
e650d927
MK
217These special keyrings are usually closed to direct alteration
218by user space.
6b71fd9a 219.P
bf0dcc15 220See
6b71fd9a
DH
221.BR thread-keyring (7),
222.BR process-keyring (7),
223.BR session-keyring (7),
224.BR user-keyring (7),
225.BR user-session-keyring (7),
226and
227.BR persistent-keyring (7)
bf0dcc15 228for more information.
6b71fd9a 229.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
fe2d2f79 230.SS Possession
e650d927 231The concept of possession is important to understanding the keyrings
a44454bc
MK
232security model.
233Whether a thread possesses a key is determined by the following rules:
e650d927
MK
234.IP (1) 4
235Any key or keyring that does not grant
236.I search
237permission to the caller is ignored in all the following rules.
6b71fd9a 238.IP (2)
c1f7a90f 239A thread \fIpossesses\fR its \fBsession\fR, \fBprocess\fR, and \fBthread\fR
6b71fd9a
DH
240keyrings directly because those are pointed to by its credentials.
241.IP (3)
242If a keyring is possessed, then any key it links to is \fIalso\fR possessed.
243.IP (4)
244If any key a keyring links to is itself a keyring, then rule (3) applies
245\fIrecursively\fP.
246.IP (5)
247If a process is upcalled from the kernel to instantiate a key, then it also
e650d927 248possesses the \fIrequester's\fP keyrings as in rule (1) as if it were the
6b71fd9a
DH
249requester.
250.P
a44454bc 251Note that possession is not a fundamental property of a key,
e650d927 252but must rather be calculated each time the key is needed.
6b71fd9a 253.P
e650d927
MK
254Possession is designed to allow set-user-ID programs run from, say
255a user's shell to access the user's keys.
a44454bc 256It also allows the prevention of access to keys
6b71fd9a
DH
257just on the basis of UID and GID matches.
258.P
f437df79
MK
259When it creates the session keyring,
260.BR pam_keyinit (8)
e650d927
MK
261adds a link to the
262.BR user-keyring (7),
f437df79 263thus making the user keyring and anything it contains possessed by default.
6b71fd9a 264.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
fe2d2f79 265.SS Access rights
6b71fd9a 266Each key has the following security-related attributes:
e650d927
MK
267.IP * 3
268The owning user ID
269.IP *
270The ID of a group that is permitted to access the key
271.IP *
272A security label
273.IP *
274A permissions mask
6b71fd9a 275.P
a44454bc
MK
276The permissions mask contains four sets of rights.
277The first three sets are mutually exclusive.
e650d927
MK
278One and only one will be in force for a particular access check.
279In order of descending priority, these three sets are:
280.IP \fIuser\fR
281The set specifies the rights granted
282if the key's user ID matches the caller's filesystem user ID.
283.IP \fIgroup\fR
284The set specifies the rights granted
285if the user ID didn't match and the key's group ID matches the caller's
286filesystem GID or one of the caller's supplementary group IDs.
287.IP \fIother\fR
288The set specifies the rights granted
289if neither the key's user ID nor group ID matched.
6b71fd9a
DH
290.P
291The fourth set of rights is:
e650d927
MK
292.IP \fIpossessor\fR
293The set specifies the rights granted
294if a key is determined to be possessed by the caller.
6b71fd9a 295.P
e650d927
MK
296The complete set of rights for a key is the union of whichever
297of the first three sets is applicable plus the fourth set
a44454bc 298if the key is possessed.
6b71fd9a 299.P
e650d927
MK
300The set of rights that may be granted in each of the four masks
301is as follows:
302.TP
303.I view
304The attributes of the key may be read.
305This includes the type,
306description, and access rights (excluding the security label).
307.TP
308.I read
309For a key: the payload of the key may be read.
310For a keyring: the list of serial numbers (keys) to
311which the keyring has links may be read.
312.TP
313.I write
314The payload of the key may be updated.
315For a keyring, links may be added to or removed from the keyring,
316the keyring may be cleared completely (all links are removed),
317and the key may be revoked.
318.TP
319.I search
320For a key (or a keyring): the key may be found by a search.
321For a keyring: keys and keyrings that are linked to by the
322keyring may be searched.
323.TP
324.I link
325Links may be created from keyrings to the key.
326The initial link to a key that is established when the key is created
327doesn't require this permission.
328.TP
329.I setattr
330The ownership details and security label of the key may be changed,
331the key's expiration time may be set, and the key may be revoked.
332.P
333If any right is granted to a thread for a key,
334then that thread will see the key listed in
335.IR /proc/keys .
a44454bc 336If no rights at all are granted, then that thread
6b71fd9a
DH
337can't even tell that the key exists.
338.P
e650d927 339In addition to access rights, any active Linux Security Module (LSM) may
a44454bc
MK
340prevent access to a key if its policy so dictates.
341A key may be given a
6b71fd9a
DH
342security label or other attribute by the LSM which can be retrieved.
343.P
bf0dcc15 344See
6b71fd9a
DH
345.BR keyctl_chown (3),
346.BR keyctl_describe (3),
347.BR keyctl_get_security (3),
e650d927 348.BR keyctl_setperm (3),
6b71fd9a
DH
349and
350.BR selinux (8)
bf0dcc15 351for more information.
6b71fd9a 352.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
fe2d2f79 353.SS Searching for keys
e650d927
MK
354One of the key features of the Linux key-management facility
355is the ability to find a key that a process is retaining.
f437df79
MK
356The
357.BR request_key (2)
358system call is the primary point of
e650d927
MK
359access for user-space applications to find a key.
360(!nternally, the kernel has something similar available
361for use by internal components that make use of keys.)
6b71fd9a
DH
362.P
363The search algorithm works as follows:
e650d927 364.IP (1) 4
6b71fd9a 365The three process keyrings are searched in the following order: the thread
e650d927
MK
366.BR thread-keyring (7)
367if it exists, the
368.BR process-keyring (7)
369if it exists, and then either the
f437df79
MK
370.BR session-keyring (7)
371if it exists or the
372.BR user-session-keyring (7)
373if that exists.
6b71fd9a 374.IP (2)
c26b9d57
MK
375If the caller was a process that was invoked by the
376.BR request_key (2)
377upcall mechanism then the keyrings of the original caller of that
378.BR request_key (2)
6b71fd9a
DH
379will be searched as well.
380.IP (3)
e650d927
MK
381The search of the keyring tree is in preorder:
382each keyring is searched first for a match,
383then the keyrings referred to by that keyring are searched.
6b71fd9a 384.IP (4)
e650d927
MK
385If a matching key is found that is valid,
386then the search terminates and that key is returned.
6b71fd9a 387.IP (5)
e650d927
MK
388If a matching key is found that has an error state attached,
389that error state is noted and the search continues.
6b71fd9a 390.IP (6)
a44454bc 391If valid matching key is found,
e650d927
MK
392then the first noted error state is returned; otherwise, an
393.B ENOKEY
394error is returned.
6b71fd9a
DH
395.P
396It is also possible to search a specific keyring, in which case only steps (3)
397to (6) apply.
398.P
f437df79 399See
6b71fd9a
DH
400.BR request_key (2)
401and
402.BR keyctl_search (3)
f437df79 403for more information.
6b71fd9a 404.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
fe2d2f79 405.SS On-demand key creation
f437df79
MK
406If a key cannot be found,
407.BR request_key (2)
408will, if given a
409.I callout_info
6d6d803e 410argument, create a new key and then upcall to user space to
a44454bc
MK
411instantiate the key.
412This allows keys to be created on an as-needed basis.
6b71fd9a 413.P
e650d927 414Typically, this will involve the kernel forking and exec'ing the
f437df79 415.BR request-key (8)
6d6d803e 416program, which will then execute the appropriate handler based on its
6b71fd9a
DH
417configuration.
418.P
e650d927
MK
419The handler is passed a special authorization key that allows it
420and only it to instantiate the new key.
a44454bc 421This is also used to permit searches performed by the
6b71fd9a
DH
422handler program to also search the requester's keyrings.
423.P
bf0dcc15 424See
e650d927 425.BR request_key (2),
6b71fd9a
DH
426.BR keyctl_assume_authority (3),
427.BR keyctl_instantiate (3),
428.BR keyctl_negate (3),
429.BR keyctl_reject (3),
6b71fd9a
DH
430.BR request-key (8)
431and
432.BR request-key.conf (5)
bf0dcc15 433for more information.
5b0a63f6 434.SS /proc files
6b71fd9a 435.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
baf69887 436.\" FIXME document /proc files
c29d99da 437.TP
c29d99da
MK
438.IR /proc/sys/kernel/keys/gc_delay " (since Linux 2.6.32)"
439.\" commit 5d135440faf7db8d566de0c6fab36b16cf9cfc3b
ef805a05
MK
440The value in this file specifies the interval, in seconds,
441after which revoked and expired keys will be garbage collected.
442.\" FIXME What is the purpose of the GC delay?
c29d99da
MK
443
444The default value in this file is 300 (i.e., 5 minutes).
445.TP
446.IR /proc/sys/kernel/keys/persistent_keyring_expiry " (since Linux 3.13)"
447.\" commit f36f8c75ae2e7d4da34f4c908cebdb4aa42c977e
448This file defines an interval, in seconds,
449to which the persistent keyring's expiration timer is reset
450each time the keyring is accessed (via
451.BR keyctl_get_persistent (3)
452or the
453.BR keyctl (2)
454.B KEYCTL_GET_PERSISTENT
455operation.)
456
457The default value in this file is 259200 (i.e., 3 days).
5b0a63f6
MK
458.PP
459The following files (which are writable by privileged processies)
460are used to enforce quotas on the number of keys
461and number of bytes of data that can be stored in key payloads:
462.TP
463.IR /proc/sys/kernel/keys/maxbytes " (since Linux 2.6.26)"
464.\" commit 0b77f5bfb45c13e1e5142374f9d6ca75292252a4
465.\" Previously: KEYQUOTA_MAX_BYTES 10000
466This is the maximum number of bytes of data that a nonroot user
467can hold in the payloads of the keys owned by the user.
468
469The default value in this file is 20,000.
470.TP
471.IR /proc/sys/kernel/keys/maxkeys " (since Linux 2.6.26)"
472.\" commit 0b77f5bfb45c13e1e5142374f9d6ca75292252a4
473.\" Previously: KEYQUOTA_MAX_KEYS 100
474This is the maximum number of keys that a nonroot user may own.
475
476The default value in this file is 200.
477.TP
478.IR /proc/sys/kernel/keys/root_maxbytes " (since Linux 2.6.26)"
479This is the maximum number of bytes of data that the root user
480(UID 0 in the root user namespace)
481can hold in the payloads of the keys owned by root.
482
483The default value in this file is 25,000,000.
484.\" commit 0b77f5bfb45c13e1e5142374f9d6ca75292252a4
485.TP
486.IR /proc/sys/kernel/keys/root_maxkeys " (since Linux 2.6.26)"
487.\" commit 0b77f5bfb45c13e1e5142374f9d6ca75292252a4
488This is the maximum number of keys that the root user
489(UID 0 in the root user namespace)
490may own.
491
492The default value in this file is 1,000,000.
493.PP
494With respect to keyrings,
495note that each link in a keyring consumes 4 bytes of the keyring payload.
baf69887 496.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
fe2d2f79 497.SS Users
e650d927
MK
498The Linux key-management facility has a number of users and usages,
499but is not limited to those that already exist.
6b71fd9a
DH
500.P
501In-kernel users of this facility include:
502.IP "\fBNetwork filesystems - DNS\fR"
503The kernel uses the upcall mechanism provided by the keys to upcall to
6d6d803e 504user space to do DNS lookups and then to cache the results.
6b71fd9a 505.IP "\fBAF_RXRPC and kAFS - Authentication\fR"
e650d927
MK
506The AF_RXRPC network protocol and the in-kernel AFS filesystem
507use keys to store the ticket needed to do secured or encrypted traffic.
a44454bc 508These are then looked up by
6b71fd9a
DH
509network operations on AF_RXRPC and filesystem operations on kAFS.
510.IP "\fBNFS - User ID mapping\fR"
e650d927
MK
511The NFS filesystem uses keys to store mappings of
512foreign user IDs to local user IDs.
6b71fd9a
DH
513.IP "\fBCIFS - Password\fR"
514The CIFS filesystem uses keys to store passwords for accessing remote shares.
515.IP "\fBModule verification\fR"
a44454bc
MK
516The kernel build process can be made to cryptographically sign modules.
517That signature is then checked when a module is loaded.
6b71fd9a 518.P
6d6d803e 519User-space users of this facility include:
6b71fd9a
DH
520.IP "\fBKerberos key storage\fR"
521The MIT Kerberos 5 facility (libkrb5) can use keys to store authentication
522tokens which can be made to be automatically cleaned up a set time after the
e650d927
MK
523user last uses them,
524but until then permits them to hang around after the user
6b71fd9a
DH
525has logged out so that cron scripts can use them.
526.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
527.SH SEE ALSO
2aa9ab8b
MK
528.ad l
529.nh
6b71fd9a 530.BR keyutils (7),
2aa9ab8b
MK
531.BR persistent\-keyring (7),
532.BR process\-keyring (7),
533.BR session\-keyring (7),
534.BR thread\-keyring (7),
535.BR user\-keyring (7),
536.BR user\-session\-keyring (7),
537.BR pam_keyinit (8)