1b68101a1SKees Cook============================
2b68101a1SKees CookKernel Key Retention Service
3b68101a1SKees Cook============================
4b68101a1SKees Cook
5b68101a1SKees CookThis service allows cryptographic keys, authentication tokens, cross-domain
6b68101a1SKees Cookuser mappings, and similar to be cached in the kernel for the use of
7b68101a1SKees Cookfilesystems and other kernel services.
8b68101a1SKees Cook
9b68101a1SKees CookKeyrings are permitted; these are a special type of key that can hold links to
10b68101a1SKees Cookother keys. Processes each have three standard keyring subscriptions that a
11b68101a1SKees Cookkernel service can search for relevant keys.
12b68101a1SKees Cook
13b68101a1SKees CookThe key service can be configured on by enabling:
14b68101a1SKees Cook
15b68101a1SKees Cook	"Security options"/"Enable access key retention support" (CONFIG_KEYS)
16b68101a1SKees Cook
17b68101a1SKees CookThis document has the following sections:
18b68101a1SKees Cook
1933c2f4ecSJosh Holland.. contents:: :local:
20b68101a1SKees Cook
21b68101a1SKees Cook
22b68101a1SKees CookKey Overview
23b68101a1SKees Cook============
24b68101a1SKees Cook
25b68101a1SKees CookIn this context, keys represent units of cryptographic data, authentication
26b68101a1SKees Cooktokens, keyrings, etc.. These are represented in the kernel by struct key.
27b68101a1SKees Cook
28b68101a1SKees CookEach key has a number of attributes:
29b68101a1SKees Cook
30b68101a1SKees Cook	- A serial number.
31b68101a1SKees Cook	- A type.
32b68101a1SKees Cook	- A description (for matching a key in a search).
33b68101a1SKees Cook	- Access control information.
34b68101a1SKees Cook	- An expiry time.
35b68101a1SKees Cook	- A payload.
36b68101a1SKees Cook	- State.
37b68101a1SKees Cook
38b68101a1SKees Cook
39b68101a1SKees Cook  *  Each key is issued a serial number of type key_serial_t that is unique for
40b68101a1SKees Cook     the lifetime of that key. All serial numbers are positive non-zero 32-bit
41b68101a1SKees Cook     integers.
42b68101a1SKees Cook
43b68101a1SKees Cook     Userspace programs can use a key's serial numbers as a way to gain access
44b68101a1SKees Cook     to it, subject to permission checking.
45b68101a1SKees Cook
46b68101a1SKees Cook  *  Each key is of a defined "type". Types must be registered inside the
47b68101a1SKees Cook     kernel by a kernel service (such as a filesystem) before keys of that type
48b68101a1SKees Cook     can be added or used. Userspace programs cannot define new types directly.
49b68101a1SKees Cook
50b68101a1SKees Cook     Key types are represented in the kernel by struct key_type. This defines a
51b68101a1SKees Cook     number of operations that can be performed on a key of that type.
52b68101a1SKees Cook
53b68101a1SKees Cook     Should a type be removed from the system, all the keys of that type will
54b68101a1SKees Cook     be invalidated.
55b68101a1SKees Cook
56b68101a1SKees Cook  *  Each key has a description. This should be a printable string. The key
57b68101a1SKees Cook     type provides an operation to perform a match between the description on a
58b68101a1SKees Cook     key and a criterion string.
59b68101a1SKees Cook
60b68101a1SKees Cook  *  Each key has an owner user ID, a group ID and a permissions mask. These
61b68101a1SKees Cook     are used to control what a process may do to a key from userspace, and
62b68101a1SKees Cook     whether a kernel service will be able to find the key.
63b68101a1SKees Cook
64b68101a1SKees Cook  *  Each key can be set to expire at a specific time by the key type's
65b68101a1SKees Cook     instantiation function. Keys can also be immortal.
66b68101a1SKees Cook
67b68101a1SKees Cook  *  Each key can have a payload. This is a quantity of data that represent the
68b68101a1SKees Cook     actual "key". In the case of a keyring, this is a list of keys to which
69b68101a1SKees Cook     the keyring links; in the case of a user-defined key, it's an arbitrary
70b68101a1SKees Cook     blob of data.
71b68101a1SKees Cook
72b68101a1SKees Cook     Having a payload is not required; and the payload can, in fact, just be a
73b68101a1SKees Cook     value stored in the struct key itself.
74b68101a1SKees Cook
75b68101a1SKees Cook     When a key is instantiated, the key type's instantiation function is
76b68101a1SKees Cook     called with a blob of data, and that then creates the key's payload in
77b68101a1SKees Cook     some way.
78b68101a1SKees Cook
79b68101a1SKees Cook     Similarly, when userspace wants to read back the contents of the key, if
80b68101a1SKees Cook     permitted, another key type operation will be called to convert the key's
81b68101a1SKees Cook     attached payload back into a blob of data.
82b68101a1SKees Cook
83b68101a1SKees Cook  *  Each key can be in one of a number of basic states:
84b68101a1SKees Cook
85b68101a1SKees Cook      *  Uninstantiated. The key exists, but does not have any data attached.
86b68101a1SKees Cook     	 Keys being requested from userspace will be in this state.
87b68101a1SKees Cook
88b68101a1SKees Cook      *  Instantiated. This is the normal state. The key is fully formed, and
89b68101a1SKees Cook	 has data attached.
90b68101a1SKees Cook
91b68101a1SKees Cook      *  Negative. This is a relatively short-lived state. The key acts as a
92b68101a1SKees Cook	 note saying that a previous call out to userspace failed, and acts as
93b68101a1SKees Cook	 a throttle on key lookups. A negative key can be updated to a normal
94b68101a1SKees Cook	 state.
95b68101a1SKees Cook
96b68101a1SKees Cook      *  Expired. Keys can have lifetimes set. If their lifetime is exceeded,
97b68101a1SKees Cook	 they traverse to this state. An expired key can be updated back to a
98b68101a1SKees Cook	 normal state.
99b68101a1SKees Cook
100b68101a1SKees Cook      *  Revoked. A key is put in this state by userspace action. It can't be
101b68101a1SKees Cook	 found or operated upon (apart from by unlinking it).
102b68101a1SKees Cook
103b68101a1SKees Cook      *  Dead. The key's type was unregistered, and so the key is now useless.
104b68101a1SKees Cook
105b68101a1SKees CookKeys in the last three states are subject to garbage collection.  See the
106b68101a1SKees Cooksection on "Garbage collection".
107b68101a1SKees Cook
108b68101a1SKees Cook
109b68101a1SKees CookKey Service Overview
110b68101a1SKees Cook====================
111b68101a1SKees Cook
112b68101a1SKees CookThe key service provides a number of features besides keys:
113b68101a1SKees Cook
114b68101a1SKees Cook  *  The key service defines three special key types:
115b68101a1SKees Cook
116b68101a1SKees Cook     (+) "keyring"
117b68101a1SKees Cook
118b68101a1SKees Cook	 Keyrings are special keys that contain a list of other keys. Keyring
119b68101a1SKees Cook	 lists can be modified using various system calls. Keyrings should not
120b68101a1SKees Cook	 be given a payload when created.
121b68101a1SKees Cook
122b68101a1SKees Cook     (+) "user"
123b68101a1SKees Cook
124b68101a1SKees Cook	 A key of this type has a description and a payload that are arbitrary
125b68101a1SKees Cook	 blobs of data. These can be created, updated and read by userspace,
126b68101a1SKees Cook	 and aren't intended for use by kernel services.
127b68101a1SKees Cook
128b68101a1SKees Cook     (+) "logon"
129b68101a1SKees Cook
130b68101a1SKees Cook	 Like a "user" key, a "logon" key has a payload that is an arbitrary
131b68101a1SKees Cook	 blob of data. It is intended as a place to store secrets which are
132b68101a1SKees Cook	 accessible to the kernel but not to userspace programs.
133b68101a1SKees Cook
134b68101a1SKees Cook	 The description can be arbitrary, but must be prefixed with a non-zero
135b68101a1SKees Cook	 length string that describes the key "subclass". The subclass is
136b68101a1SKees Cook	 separated from the rest of the description by a ':'. "logon" keys can
137b68101a1SKees Cook	 be created and updated from userspace, but the payload is only
138b68101a1SKees Cook	 readable from kernel space.
139b68101a1SKees Cook
140b68101a1SKees Cook  *  Each process subscribes to three keyrings: a thread-specific keyring, a
141b68101a1SKees Cook     process-specific keyring, and a session-specific keyring.
142b68101a1SKees Cook
143b68101a1SKees Cook     The thread-specific keyring is discarded from the child when any sort of
144b68101a1SKees Cook     clone, fork, vfork or execve occurs. A new keyring is created only when
145b68101a1SKees Cook     required.
146b68101a1SKees Cook
147b68101a1SKees Cook     The process-specific keyring is replaced with an empty one in the child on
148b68101a1SKees Cook     clone, fork, vfork unless CLONE_THREAD is supplied, in which case it is
149b68101a1SKees Cook     shared. execve also discards the process's process keyring and creates a
150b68101a1SKees Cook     new one.
151b68101a1SKees Cook
152b68101a1SKees Cook     The session-specific keyring is persistent across clone, fork, vfork and
153b68101a1SKees Cook     execve, even when the latter executes a set-UID or set-GID binary. A
154b68101a1SKees Cook     process can, however, replace its current session keyring with a new one
155b68101a1SKees Cook     by using PR_JOIN_SESSION_KEYRING. It is permitted to request an anonymous
156b68101a1SKees Cook     new one, or to attempt to create or join one of a specific name.
157b68101a1SKees Cook
158b68101a1SKees Cook     The ownership of the thread keyring changes when the real UID and GID of
159b68101a1SKees Cook     the thread changes.
160b68101a1SKees Cook
161b68101a1SKees Cook  *  Each user ID resident in the system holds two special keyrings: a user
162b68101a1SKees Cook     specific keyring and a default user session keyring. The default session
163b68101a1SKees Cook     keyring is initialised with a link to the user-specific keyring.
164b68101a1SKees Cook
165b68101a1SKees Cook     When a process changes its real UID, if it used to have no session key, it
166b68101a1SKees Cook     will be subscribed to the default session key for the new UID.
167b68101a1SKees Cook
168b68101a1SKees Cook     If a process attempts to access its session key when it doesn't have one,
169b68101a1SKees Cook     it will be subscribed to the default for its current UID.
170b68101a1SKees Cook
171b68101a1SKees Cook  *  Each user has two quotas against which the keys they own are tracked. One
172b68101a1SKees Cook     limits the total number of keys and keyrings, the other limits the total
173b68101a1SKees Cook     amount of description and payload space that can be consumed.
174b68101a1SKees Cook
175b68101a1SKees Cook     The user can view information on this and other statistics through procfs
176b68101a1SKees Cook     files.  The root user may also alter the quota limits through sysctl files
177b68101a1SKees Cook     (see the section "New procfs files").
178b68101a1SKees Cook
179b68101a1SKees Cook     Process-specific and thread-specific keyrings are not counted towards a
180b68101a1SKees Cook     user's quota.
181b68101a1SKees Cook
182b68101a1SKees Cook     If a system call that modifies a key or keyring in some way would put the
183b68101a1SKees Cook     user over quota, the operation is refused and error EDQUOT is returned.
184b68101a1SKees Cook
185b68101a1SKees Cook  *  There's a system call interface by which userspace programs can create and
186b68101a1SKees Cook     manipulate keys and keyrings.
187b68101a1SKees Cook
188b68101a1SKees Cook  *  There's a kernel interface by which services can register types and search
189b68101a1SKees Cook     for keys.
190b68101a1SKees Cook
191b68101a1SKees Cook  *  There's a way for the a search done from the kernel to call back to
192b68101a1SKees Cook     userspace to request a key that can't be found in a process's keyrings.
193b68101a1SKees Cook
194b68101a1SKees Cook  *  An optional filesystem is available through which the key database can be
195b68101a1SKees Cook     viewed and manipulated.
196b68101a1SKees Cook
197b68101a1SKees Cook
198b68101a1SKees CookKey Access Permissions
199b68101a1SKees Cook======================
200b68101a1SKees Cook
201b68101a1SKees CookKeys have an owner user ID, a group access ID, and a permissions mask. The mask
202b68101a1SKees Cookhas up to eight bits each for possessor, user, group and other access. Only
203b68101a1SKees Cooksix of each set of eight bits are defined. These permissions granted are:
204b68101a1SKees Cook
205b68101a1SKees Cook  *  View
206b68101a1SKees Cook
207b68101a1SKees Cook     This permits a key or keyring's attributes to be viewed - including key
208b68101a1SKees Cook     type and description.
209b68101a1SKees Cook
210b68101a1SKees Cook  *  Read
211b68101a1SKees Cook
212b68101a1SKees Cook     This permits a key's payload to be viewed or a keyring's list of linked
213b68101a1SKees Cook     keys.
214b68101a1SKees Cook
215b68101a1SKees Cook  *  Write
216b68101a1SKees Cook
217b68101a1SKees Cook     This permits a key's payload to be instantiated or updated, or it allows a
218b68101a1SKees Cook     link to be added to or removed from a keyring.
219b68101a1SKees Cook
220b68101a1SKees Cook  *  Search
221b68101a1SKees Cook
222b68101a1SKees Cook     This permits keyrings to be searched and keys to be found. Searches can
223b68101a1SKees Cook     only recurse into nested keyrings that have search permission set.
224b68101a1SKees Cook
225b68101a1SKees Cook  *  Link
226b68101a1SKees Cook
227b68101a1SKees Cook     This permits a key or keyring to be linked to. To create a link from a
228b68101a1SKees Cook     keyring to a key, a process must have Write permission on the keyring and
229b68101a1SKees Cook     Link permission on the key.
230b68101a1SKees Cook
231b68101a1SKees Cook  *  Set Attribute
232b68101a1SKees Cook
233b68101a1SKees Cook     This permits a key's UID, GID and permissions mask to be changed.
234b68101a1SKees Cook
235b68101a1SKees CookFor changing the ownership, group ID or permissions mask, being the owner of
236b68101a1SKees Cookthe key or having the sysadmin capability is sufficient.
237b68101a1SKees Cook
238b68101a1SKees Cook
239b68101a1SKees CookSELinux Support
240b68101a1SKees Cook===============
241b68101a1SKees Cook
242b68101a1SKees CookThe security class "key" has been added to SELinux so that mandatory access
243b68101a1SKees Cookcontrols can be applied to keys created within various contexts.  This support
244b68101a1SKees Cookis preliminary, and is likely to change quite significantly in the near future.
245b68101a1SKees CookCurrently, all of the basic permissions explained above are provided in SELinux
246b68101a1SKees Cookas well; SELinux is simply invoked after all basic permission checks have been
247b68101a1SKees Cookperformed.
248b68101a1SKees Cook
249b68101a1SKees CookThe value of the file /proc/self/attr/keycreate influences the labeling of
250b68101a1SKees Cooknewly-created keys.  If the contents of that file correspond to an SELinux
251b68101a1SKees Cooksecurity context, then the key will be assigned that context.  Otherwise, the
252b68101a1SKees Cookkey will be assigned the current context of the task that invoked the key
253b68101a1SKees Cookcreation request.  Tasks must be granted explicit permission to assign a
254b68101a1SKees Cookparticular context to newly-created keys, using the "create" permission in the
255b68101a1SKees Cookkey security class.
256b68101a1SKees Cook
257b68101a1SKees CookThe default keyrings associated with users will be labeled with the default
258b68101a1SKees Cookcontext of the user if and only if the login programs have been instrumented to
259b68101a1SKees Cookproperly initialize keycreate during the login process.  Otherwise, they will
260b68101a1SKees Cookbe labeled with the context of the login program itself.
261b68101a1SKees Cook
262b68101a1SKees CookNote, however, that the default keyrings associated with the root user are
263b68101a1SKees Cooklabeled with the default kernel context, since they are created early in the
264b68101a1SKees Cookboot process, before root has a chance to log in.
265b68101a1SKees Cook
266b68101a1SKees CookThe keyrings associated with new threads are each labeled with the context of
267b68101a1SKees Cooktheir associated thread, and both session and process keyrings are handled
268b68101a1SKees Cooksimilarly.
269b68101a1SKees Cook
270b68101a1SKees Cook
271b68101a1SKees CookNew ProcFS Files
272b68101a1SKees Cook================
273b68101a1SKees Cook
274b68101a1SKees CookTwo files have been added to procfs by which an administrator can find out
275b68101a1SKees Cookabout the status of the key service:
276b68101a1SKees Cook
277b68101a1SKees Cook  *  /proc/keys
278b68101a1SKees Cook
279b68101a1SKees Cook     This lists the keys that are currently viewable by the task reading the
280b68101a1SKees Cook     file, giving information about their type, description and permissions.
281b68101a1SKees Cook     It is not possible to view the payload of the key this way, though some
282b68101a1SKees Cook     information about it may be given.
283b68101a1SKees Cook
284b68101a1SKees Cook     The only keys included in the list are those that grant View permission to
285b68101a1SKees Cook     the reading process whether or not it possesses them.  Note that LSM
286b68101a1SKees Cook     security checks are still performed, and may further filter out keys that
287b68101a1SKees Cook     the current process is not authorised to view.
288b68101a1SKees Cook
289b68101a1SKees Cook     The contents of the file look like this::
290b68101a1SKees Cook
291b68101a1SKees Cook	SERIAL   FLAGS  USAGE EXPY PERM     UID   GID   TYPE      DESCRIPTION: SUMMARY
292b68101a1SKees Cook	00000001 I-----    39 perm 1f3f0000     0     0 keyring   _uid_ses.0: 1/4
293b68101a1SKees Cook	00000002 I-----     2 perm 1f3f0000     0     0 keyring   _uid.0: empty
294b68101a1SKees Cook	00000007 I-----     1 perm 1f3f0000     0     0 keyring   _pid.1: empty
295b68101a1SKees Cook	0000018d I-----     1 perm 1f3f0000     0     0 keyring   _pid.412: empty
296b68101a1SKees Cook	000004d2 I--Q--     1 perm 1f3f0000    32    -1 keyring   _uid.32: 1/4
297b68101a1SKees Cook	000004d3 I--Q--     3 perm 1f3f0000    32    -1 keyring   _uid_ses.32: empty
298b68101a1SKees Cook	00000892 I--QU-     1 perm 1f000000     0     0 user      metal:copper: 0
299b68101a1SKees Cook	00000893 I--Q-N     1  35s 1f3f0000     0     0 user      metal:silver: 0
300b68101a1SKees Cook	00000894 I--Q--     1  10h 003f0000     0     0 user      metal:gold: 0
301b68101a1SKees Cook
302b68101a1SKees Cook     The flags are::
303b68101a1SKees Cook
304b68101a1SKees Cook	I	Instantiated
305b68101a1SKees Cook	R	Revoked
306b68101a1SKees Cook	D	Dead
307b68101a1SKees Cook	Q	Contributes to user's quota
308b68101a1SKees Cook	U	Under construction by callback to userspace
309b68101a1SKees Cook	N	Negative key
310b68101a1SKees Cook
311b68101a1SKees Cook
312b68101a1SKees Cook  *  /proc/key-users
313b68101a1SKees Cook
314b68101a1SKees Cook     This file lists the tracking data for each user that has at least one key
315b68101a1SKees Cook     on the system.  Such data includes quota information and statistics::
316b68101a1SKees Cook
317b68101a1SKees Cook	[root@andromeda root]# cat /proc/key-users
318b68101a1SKees Cook	0:     46 45/45 1/100 13/10000
319b68101a1SKees Cook	29:     2 2/2 2/100 40/10000
320b68101a1SKees Cook	32:     2 2/2 2/100 40/10000
321b68101a1SKees Cook	38:     2 2/2 2/100 40/10000
322b68101a1SKees Cook
323b68101a1SKees Cook     The format of each line is::
324b68101a1SKees Cook
325b68101a1SKees Cook	<UID>:			User ID to which this applies
326b68101a1SKees Cook	<usage>			Structure refcount
327b68101a1SKees Cook	<inst>/<keys>		Total number of keys and number instantiated
328b68101a1SKees Cook	<keys>/<max>		Key count quota
329b68101a1SKees Cook	<bytes>/<max>		Key size quota
330b68101a1SKees Cook
331b68101a1SKees Cook
332b68101a1SKees CookFour new sysctl files have been added also for the purpose of controlling the
333b68101a1SKees Cookquota limits on keys:
334b68101a1SKees Cook
335b68101a1SKees Cook  *  /proc/sys/kernel/keys/root_maxkeys
336b68101a1SKees Cook     /proc/sys/kernel/keys/root_maxbytes
337b68101a1SKees Cook
338b68101a1SKees Cook     These files hold the maximum number of keys that root may have and the
339b68101a1SKees Cook     maximum total number of bytes of data that root may have stored in those
340b68101a1SKees Cook     keys.
341b68101a1SKees Cook
342b68101a1SKees Cook  *  /proc/sys/kernel/keys/maxkeys
343b68101a1SKees Cook     /proc/sys/kernel/keys/maxbytes
344b68101a1SKees Cook
345b68101a1SKees Cook     These files hold the maximum number of keys that each non-root user may
346b68101a1SKees Cook     have and the maximum total number of bytes of data that each of those
347b68101a1SKees Cook     users may have stored in their keys.
348b68101a1SKees Cook
349b68101a1SKees CookRoot may alter these by writing each new limit as a decimal number string to
350b68101a1SKees Cookthe appropriate file.
351b68101a1SKees Cook
352b68101a1SKees Cook
353b68101a1SKees CookUserspace System Call Interface
354b68101a1SKees Cook===============================
355b68101a1SKees Cook
356b68101a1SKees CookUserspace can manipulate keys directly through three new syscalls: add_key,
357b68101a1SKees Cookrequest_key and keyctl. The latter provides a number of functions for
358b68101a1SKees Cookmanipulating keys.
359b68101a1SKees Cook
360b68101a1SKees CookWhen referring to a key directly, userspace programs should use the key's
361b68101a1SKees Cookserial number (a positive 32-bit integer). However, there are some special
362b68101a1SKees Cookvalues available for referring to special keys and keyrings that relate to the
363b68101a1SKees Cookprocess making the call::
364b68101a1SKees Cook
365b68101a1SKees Cook	CONSTANT			VALUE	KEY REFERENCED
366b68101a1SKees Cook	==============================	======	===========================
367b68101a1SKees Cook	KEY_SPEC_THREAD_KEYRING		-1	thread-specific keyring
368b68101a1SKees Cook	KEY_SPEC_PROCESS_KEYRING	-2	process-specific keyring
369b68101a1SKees Cook	KEY_SPEC_SESSION_KEYRING	-3	session-specific keyring
370b68101a1SKees Cook	KEY_SPEC_USER_KEYRING		-4	UID-specific keyring
371b68101a1SKees Cook	KEY_SPEC_USER_SESSION_KEYRING	-5	UID-session keyring
372b68101a1SKees Cook	KEY_SPEC_GROUP_KEYRING		-6	GID-specific keyring
373b68101a1SKees Cook	KEY_SPEC_REQKEY_AUTH_KEY	-7	assumed request_key()
374b68101a1SKees Cook						  authorisation key
375b68101a1SKees Cook
376b68101a1SKees Cook
377b68101a1SKees CookThe main syscalls are:
378b68101a1SKees Cook
379b68101a1SKees Cook  *  Create a new key of given type, description and payload and add it to the
380b68101a1SKees Cook     nominated keyring::
381b68101a1SKees Cook
382b68101a1SKees Cook	key_serial_t add_key(const char *type, const char *desc,
383b68101a1SKees Cook			     const void *payload, size_t plen,
384b68101a1SKees Cook			     key_serial_t keyring);
385b68101a1SKees Cook
386b68101a1SKees Cook     If a key of the same type and description as that proposed already exists
387b68101a1SKees Cook     in the keyring, this will try to update it with the given payload, or it
388b68101a1SKees Cook     will return error EEXIST if that function is not supported by the key
389b68101a1SKees Cook     type. The process must also have permission to write to the key to be able
390b68101a1SKees Cook     to update it. The new key will have all user permissions granted and no
391b68101a1SKees Cook     group or third party permissions.
392b68101a1SKees Cook
393b68101a1SKees Cook     Otherwise, this will attempt to create a new key of the specified type and
394b68101a1SKees Cook     description, and to instantiate it with the supplied payload and attach it
395b68101a1SKees Cook     to the keyring. In this case, an error will be generated if the process
396b68101a1SKees Cook     does not have permission to write to the keyring.
397b68101a1SKees Cook
398b68101a1SKees Cook     If the key type supports it, if the description is NULL or an empty
399b68101a1SKees Cook     string, the key type will try and generate a description from the content
400b68101a1SKees Cook     of the payload.
401b68101a1SKees Cook
402b68101a1SKees Cook     The payload is optional, and the pointer can be NULL if not required by
403b68101a1SKees Cook     the type. The payload is plen in size, and plen can be zero for an empty
404b68101a1SKees Cook     payload.
405b68101a1SKees Cook
406b68101a1SKees Cook     A new keyring can be generated by setting type "keyring", the keyring name
407b68101a1SKees Cook     as the description (or NULL) and setting the payload to NULL.
408b68101a1SKees Cook
409b68101a1SKees Cook     User defined keys can be created by specifying type "user". It is
410b68101a1SKees Cook     recommended that a user defined key's description by prefixed with a type
411b68101a1SKees Cook     ID and a colon, such as "krb5tgt:" for a Kerberos 5 ticket granting
412b68101a1SKees Cook     ticket.
413b68101a1SKees Cook
414b68101a1SKees Cook     Any other type must have been registered with the kernel in advance by a
415b68101a1SKees Cook     kernel service such as a filesystem.
416b68101a1SKees Cook
417b68101a1SKees Cook     The ID of the new or updated key is returned if successful.
418b68101a1SKees Cook
419b68101a1SKees Cook
420b68101a1SKees Cook  *  Search the process's keyrings for a key, potentially calling out to
421b68101a1SKees Cook     userspace to create it::
422b68101a1SKees Cook
423b68101a1SKees Cook	key_serial_t request_key(const char *type, const char *description,
424b68101a1SKees Cook				 const char *callout_info,
425b68101a1SKees Cook				 key_serial_t dest_keyring);
426b68101a1SKees Cook
427b68101a1SKees Cook     This function searches all the process's keyrings in the order thread,
428b68101a1SKees Cook     process, session for a matching key. This works very much like
429b68101a1SKees Cook     KEYCTL_SEARCH, including the optional attachment of the discovered key to
430b68101a1SKees Cook     a keyring.
431b68101a1SKees Cook
432b68101a1SKees Cook     If a key cannot be found, and if callout_info is not NULL, then
433b68101a1SKees Cook     /sbin/request-key will be invoked in an attempt to obtain a key. The
434b68101a1SKees Cook     callout_info string will be passed as an argument to the program.
435b68101a1SKees Cook
436adf31eebSJosh Holland     See also Documentation/security/keys/request-key.rst.
437b68101a1SKees Cook
438b68101a1SKees Cook
439b68101a1SKees CookThe keyctl syscall functions are:
440b68101a1SKees Cook
441b68101a1SKees Cook  *  Map a special key ID to a real key ID for this process::
442b68101a1SKees Cook
443b68101a1SKees Cook	key_serial_t keyctl(KEYCTL_GET_KEYRING_ID, key_serial_t id,
444b68101a1SKees Cook			    int create);
445b68101a1SKees Cook
446b68101a1SKees Cook     The special key specified by "id" is looked up (with the key being created
447b68101a1SKees Cook     if necessary) and the ID of the key or keyring thus found is returned if
448b68101a1SKees Cook     it exists.
449b68101a1SKees Cook
450b68101a1SKees Cook     If the key does not yet exist, the key will be created if "create" is
451b68101a1SKees Cook     non-zero; and the error ENOKEY will be returned if "create" is zero.
452b68101a1SKees Cook
453b68101a1SKees Cook
454b68101a1SKees Cook  *  Replace the session keyring this process subscribes to with a new one::
455b68101a1SKees Cook
456b68101a1SKees Cook	key_serial_t keyctl(KEYCTL_JOIN_SESSION_KEYRING, const char *name);
457b68101a1SKees Cook
458b68101a1SKees Cook     If name is NULL, an anonymous keyring is created attached to the process
459b68101a1SKees Cook     as its session keyring, displacing the old session keyring.
460b68101a1SKees Cook
461b68101a1SKees Cook     If name is not NULL, if a keyring of that name exists, the process
462b68101a1SKees Cook     attempts to attach it as the session keyring, returning an error if that
463b68101a1SKees Cook     is not permitted; otherwise a new keyring of that name is created and
464b68101a1SKees Cook     attached as the session keyring.
465b68101a1SKees Cook
466b68101a1SKees Cook     To attach to a named keyring, the keyring must have search permission for
467b68101a1SKees Cook     the process's ownership.
468b68101a1SKees Cook
469b68101a1SKees Cook     The ID of the new session keyring is returned if successful.
470b68101a1SKees Cook
471b68101a1SKees Cook
472b68101a1SKees Cook  *  Update the specified key::
473b68101a1SKees Cook
474b68101a1SKees Cook	long keyctl(KEYCTL_UPDATE, key_serial_t key, const void *payload,
475b68101a1SKees Cook		    size_t plen);
476b68101a1SKees Cook
477b68101a1SKees Cook     This will try to update the specified key with the given payload, or it
478b68101a1SKees Cook     will return error EOPNOTSUPP if that function is not supported by the key
479b68101a1SKees Cook     type. The process must also have permission to write to the key to be able
480b68101a1SKees Cook     to update it.
481b68101a1SKees Cook
482b68101a1SKees Cook     The payload is of length plen, and may be absent or empty as for
483b68101a1SKees Cook     add_key().
484b68101a1SKees Cook
485b68101a1SKees Cook
486b68101a1SKees Cook  *  Revoke a key::
487b68101a1SKees Cook
488b68101a1SKees Cook	long keyctl(KEYCTL_REVOKE, key_serial_t key);
489b68101a1SKees Cook
490b68101a1SKees Cook     This makes a key unavailable for further operations. Further attempts to
491b68101a1SKees Cook     use the key will be met with error EKEYREVOKED, and the key will no longer
492b68101a1SKees Cook     be findable.
493b68101a1SKees Cook
494b68101a1SKees Cook
495b68101a1SKees Cook  *  Change the ownership of a key::
496b68101a1SKees Cook
497b68101a1SKees Cook	long keyctl(KEYCTL_CHOWN, key_serial_t key, uid_t uid, gid_t gid);
498b68101a1SKees Cook
499b68101a1SKees Cook     This function permits a key's owner and group ID to be changed. Either one
500b68101a1SKees Cook     of uid or gid can be set to -1 to suppress that change.
501b68101a1SKees Cook
502b68101a1SKees Cook     Only the superuser can change a key's owner to something other than the
503b68101a1SKees Cook     key's current owner. Similarly, only the superuser can change a key's
504b68101a1SKees Cook     group ID to something other than the calling process's group ID or one of
505b68101a1SKees Cook     its group list members.
506b68101a1SKees Cook
507b68101a1SKees Cook
508b68101a1SKees Cook  *  Change the permissions mask on a key::
509b68101a1SKees Cook
510b68101a1SKees Cook	long keyctl(KEYCTL_SETPERM, key_serial_t key, key_perm_t perm);
511b68101a1SKees Cook
512b68101a1SKees Cook     This function permits the owner of a key or the superuser to change the
513b68101a1SKees Cook     permissions mask on a key.
514b68101a1SKees Cook
515b68101a1SKees Cook     Only bits the available bits are permitted; if any other bits are set,
516b68101a1SKees Cook     error EINVAL will be returned.
517b68101a1SKees Cook
518b68101a1SKees Cook
519b68101a1SKees Cook  *  Describe a key::
520b68101a1SKees Cook
521b68101a1SKees Cook	long keyctl(KEYCTL_DESCRIBE, key_serial_t key, char *buffer,
522b68101a1SKees Cook		    size_t buflen);
523b68101a1SKees Cook
524b68101a1SKees Cook     This function returns a summary of the key's attributes (but not its
525b68101a1SKees Cook     payload data) as a string in the buffer provided.
526b68101a1SKees Cook
527b68101a1SKees Cook     Unless there's an error, it always returns the amount of data it could
528b68101a1SKees Cook     produce, even if that's too big for the buffer, but it won't copy more
529b68101a1SKees Cook     than requested to userspace. If the buffer pointer is NULL then no copy
530b68101a1SKees Cook     will take place.
531b68101a1SKees Cook
532b68101a1SKees Cook     A process must have view permission on the key for this function to be
533b68101a1SKees Cook     successful.
534b68101a1SKees Cook
535b68101a1SKees Cook     If successful, a string is placed in the buffer in the following format::
536b68101a1SKees Cook
537b68101a1SKees Cook	<type>;<uid>;<gid>;<perm>;<description>
538b68101a1SKees Cook
539b68101a1SKees Cook     Where type and description are strings, uid and gid are decimal, and perm
540b68101a1SKees Cook     is hexadecimal. A NUL character is included at the end of the string if
541b68101a1SKees Cook     the buffer is sufficiently big.
542b68101a1SKees Cook
543b68101a1SKees Cook     This can be parsed with::
544b68101a1SKees Cook
545b68101a1SKees Cook	sscanf(buffer, "%[^;];%d;%d;%o;%s", type, &uid, &gid, &mode, desc);
546b68101a1SKees Cook
547b68101a1SKees Cook
548b68101a1SKees Cook  *  Clear out a keyring::
549b68101a1SKees Cook
550b68101a1SKees Cook	long keyctl(KEYCTL_CLEAR, key_serial_t keyring);
551b68101a1SKees Cook
552b68101a1SKees Cook     This function clears the list of keys attached to a keyring. The calling
553b68101a1SKees Cook     process must have write permission on the keyring, and it must be a
554b68101a1SKees Cook     keyring (or else error ENOTDIR will result).
555b68101a1SKees Cook
556b68101a1SKees Cook     This function can also be used to clear special kernel keyrings if they
557b68101a1SKees Cook     are appropriately marked if the user has CAP_SYS_ADMIN capability.  The
558b68101a1SKees Cook     DNS resolver cache keyring is an example of this.
559b68101a1SKees Cook
560b68101a1SKees Cook
561b68101a1SKees Cook  *  Link a key into a keyring::
562b68101a1SKees Cook
563b68101a1SKees Cook	long keyctl(KEYCTL_LINK, key_serial_t keyring, key_serial_t key);
564b68101a1SKees Cook
565b68101a1SKees Cook     This function creates a link from the keyring to the key. The process must
566b68101a1SKees Cook     have write permission on the keyring and must have link permission on the
567b68101a1SKees Cook     key.
568b68101a1SKees Cook
569b68101a1SKees Cook     Should the keyring not be a keyring, error ENOTDIR will result; and if the
570b68101a1SKees Cook     keyring is full, error ENFILE will result.
571b68101a1SKees Cook
572b68101a1SKees Cook     The link procedure checks the nesting of the keyrings, returning ELOOP if
573b68101a1SKees Cook     it appears too deep or EDEADLK if the link would introduce a cycle.
574b68101a1SKees Cook
575b68101a1SKees Cook     Any links within the keyring to keys that match the new key in terms of
576b68101a1SKees Cook     type and description will be discarded from the keyring as the new one is
577b68101a1SKees Cook     added.
578b68101a1SKees Cook
579b68101a1SKees Cook
580b68101a1SKees Cook  *  Unlink a key or keyring from another keyring::
581b68101a1SKees Cook
582b68101a1SKees Cook	long keyctl(KEYCTL_UNLINK, key_serial_t keyring, key_serial_t key);
583b68101a1SKees Cook
584b68101a1SKees Cook     This function looks through the keyring for the first link to the
585b68101a1SKees Cook     specified key, and removes it if found. Subsequent links to that key are
586b68101a1SKees Cook     ignored. The process must have write permission on the keyring.
587b68101a1SKees Cook
588b68101a1SKees Cook     If the keyring is not a keyring, error ENOTDIR will result; and if the key
589b68101a1SKees Cook     is not present, error ENOENT will be the result.
590b68101a1SKees Cook
591b68101a1SKees Cook
592b68101a1SKees Cook  *  Search a keyring tree for a key::
593b68101a1SKees Cook
594b68101a1SKees Cook	key_serial_t keyctl(KEYCTL_SEARCH, key_serial_t keyring,
595b68101a1SKees Cook			    const char *type, const char *description,
596b68101a1SKees Cook			    key_serial_t dest_keyring);
597b68101a1SKees Cook
598b68101a1SKees Cook     This searches the keyring tree headed by the specified keyring until a key
599b68101a1SKees Cook     is found that matches the type and description criteria. Each keyring is
600b68101a1SKees Cook     checked for keys before recursion into its children occurs.
601b68101a1SKees Cook
602b68101a1SKees Cook     The process must have search permission on the top level keyring, or else
603b68101a1SKees Cook     error EACCES will result. Only keyrings that the process has search
604b68101a1SKees Cook     permission on will be recursed into, and only keys and keyrings for which
605b68101a1SKees Cook     a process has search permission can be matched. If the specified keyring
606b68101a1SKees Cook     is not a keyring, ENOTDIR will result.
607b68101a1SKees Cook
608b68101a1SKees Cook     If the search succeeds, the function will attempt to link the found key
609b68101a1SKees Cook     into the destination keyring if one is supplied (non-zero ID). All the
610b68101a1SKees Cook     constraints applicable to KEYCTL_LINK apply in this case too.
611b68101a1SKees Cook
612b68101a1SKees Cook     Error ENOKEY, EKEYREVOKED or EKEYEXPIRED will be returned if the search
613b68101a1SKees Cook     fails. On success, the resulting key ID will be returned.
614b68101a1SKees Cook
615b68101a1SKees Cook
616b68101a1SKees Cook  *  Read the payload data from a key::
617b68101a1SKees Cook
618b68101a1SKees Cook	long keyctl(KEYCTL_READ, key_serial_t keyring, char *buffer,
619b68101a1SKees Cook		    size_t buflen);
620b68101a1SKees Cook
621b68101a1SKees Cook     This function attempts to read the payload data from the specified key
622b68101a1SKees Cook     into the buffer. The process must have read permission on the key to
623b68101a1SKees Cook     succeed.
624b68101a1SKees Cook
625b68101a1SKees Cook     The returned data will be processed for presentation by the key type. For
626b68101a1SKees Cook     instance, a keyring will return an array of key_serial_t entries
627b68101a1SKees Cook     representing the IDs of all the keys to which it is subscribed. The user
628b68101a1SKees Cook     defined key type will return its data as is. If a key type does not
629b68101a1SKees Cook     implement this function, error EOPNOTSUPP will result.
630b68101a1SKees Cook
631be543dd6SEric Biggers     If the specified buffer is too small, then the size of the buffer required
632be543dd6SEric Biggers     will be returned.  Note that in this case, the contents of the buffer may
633be543dd6SEric Biggers     have been overwritten in some undefined way.
634b68101a1SKees Cook
635be543dd6SEric Biggers     Otherwise, on success, the function will return the amount of data copied
636be543dd6SEric Biggers     into the buffer.
637b68101a1SKees Cook
638b68101a1SKees Cook  *  Instantiate a partially constructed key::
639b68101a1SKees Cook
640b68101a1SKees Cook	long keyctl(KEYCTL_INSTANTIATE, key_serial_t key,
641b68101a1SKees Cook		    const void *payload, size_t plen,
642b68101a1SKees Cook		    key_serial_t keyring);
643b68101a1SKees Cook	long keyctl(KEYCTL_INSTANTIATE_IOV, key_serial_t key,
644b68101a1SKees Cook		    const struct iovec *payload_iov, unsigned ioc,
645b68101a1SKees Cook		    key_serial_t keyring);
646b68101a1SKees Cook
647b68101a1SKees Cook     If the kernel calls back to userspace to complete the instantiation of a
648b68101a1SKees Cook     key, userspace should use this call to supply data for the key before the
649b68101a1SKees Cook     invoked process returns, or else the key will be marked negative
650b68101a1SKees Cook     automatically.
651b68101a1SKees Cook
652b68101a1SKees Cook     The process must have write access on the key to be able to instantiate
653b68101a1SKees Cook     it, and the key must be uninstantiated.
654b68101a1SKees Cook
655b68101a1SKees Cook     If a keyring is specified (non-zero), the key will also be linked into
656b68101a1SKees Cook     that keyring, however all the constraints applying in KEYCTL_LINK apply in
657b68101a1SKees Cook     this case too.
658b68101a1SKees Cook
659b68101a1SKees Cook     The payload and plen arguments describe the payload data as for add_key().
660b68101a1SKees Cook
661b68101a1SKees Cook     The payload_iov and ioc arguments describe the payload data in an iovec
662b68101a1SKees Cook     array instead of a single buffer.
663b68101a1SKees Cook
664b68101a1SKees Cook
665b68101a1SKees Cook  *  Negatively instantiate a partially constructed key::
666b68101a1SKees Cook
667b68101a1SKees Cook	long keyctl(KEYCTL_NEGATE, key_serial_t key,
668b68101a1SKees Cook		    unsigned timeout, key_serial_t keyring);
669b68101a1SKees Cook	long keyctl(KEYCTL_REJECT, key_serial_t key,
670b68101a1SKees Cook		    unsigned timeout, unsigned error, key_serial_t keyring);
671b68101a1SKees Cook
672b68101a1SKees Cook     If the kernel calls back to userspace to complete the instantiation of a
673b68101a1SKees Cook     key, userspace should use this call mark the key as negative before the
674b68101a1SKees Cook     invoked process returns if it is unable to fulfill the request.
675b68101a1SKees Cook
676b68101a1SKees Cook     The process must have write access on the key to be able to instantiate
677b68101a1SKees Cook     it, and the key must be uninstantiated.
678b68101a1SKees Cook
679b68101a1SKees Cook     If a keyring is specified (non-zero), the key will also be linked into
680b68101a1SKees Cook     that keyring, however all the constraints applying in KEYCTL_LINK apply in
681b68101a1SKees Cook     this case too.
682b68101a1SKees Cook
683b68101a1SKees Cook     If the key is rejected, future searches for it will return the specified
684b68101a1SKees Cook     error code until the rejected key expires.  Negating the key is the same
685b68101a1SKees Cook     as rejecting the key with ENOKEY as the error code.
686b68101a1SKees Cook
687b68101a1SKees Cook
688b68101a1SKees Cook  *  Set the default request-key destination keyring::
689b68101a1SKees Cook
690b68101a1SKees Cook	long keyctl(KEYCTL_SET_REQKEY_KEYRING, int reqkey_defl);
691b68101a1SKees Cook
692b68101a1SKees Cook     This sets the default keyring to which implicitly requested keys will be
693b68101a1SKees Cook     attached for this thread. reqkey_defl should be one of these constants::
694b68101a1SKees Cook
695b68101a1SKees Cook	CONSTANT				VALUE	NEW DEFAULT KEYRING
696b68101a1SKees Cook	======================================	======	=======================
697b68101a1SKees Cook	KEY_REQKEY_DEFL_NO_CHANGE		-1	No change
698b68101a1SKees Cook	KEY_REQKEY_DEFL_DEFAULT			0	Default[1]
699b68101a1SKees Cook	KEY_REQKEY_DEFL_THREAD_KEYRING		1	Thread keyring
700b68101a1SKees Cook	KEY_REQKEY_DEFL_PROCESS_KEYRING		2	Process keyring
701b68101a1SKees Cook	KEY_REQKEY_DEFL_SESSION_KEYRING		3	Session keyring
702b68101a1SKees Cook	KEY_REQKEY_DEFL_USER_KEYRING		4	User keyring
703b68101a1SKees Cook	KEY_REQKEY_DEFL_USER_SESSION_KEYRING	5	User session keyring
704b68101a1SKees Cook	KEY_REQKEY_DEFL_GROUP_KEYRING		6	Group keyring
705b68101a1SKees Cook
706b68101a1SKees Cook     The old default will be returned if successful and error EINVAL will be
707b68101a1SKees Cook     returned if reqkey_defl is not one of the above values.
708b68101a1SKees Cook
709b68101a1SKees Cook     The default keyring can be overridden by the keyring indicated to the
710b68101a1SKees Cook     request_key() system call.
711b68101a1SKees Cook
712b68101a1SKees Cook     Note that this setting is inherited across fork/exec.
713b68101a1SKees Cook
714b68101a1SKees Cook     [1] The default is: the thread keyring if there is one, otherwise
715b68101a1SKees Cook     the process keyring if there is one, otherwise the session keyring if
716b68101a1SKees Cook     there is one, otherwise the user default session keyring.
717b68101a1SKees Cook
718b68101a1SKees Cook
719b68101a1SKees Cook  *  Set the timeout on a key::
720b68101a1SKees Cook
721b68101a1SKees Cook	long keyctl(KEYCTL_SET_TIMEOUT, key_serial_t key, unsigned timeout);
722b68101a1SKees Cook
723b68101a1SKees Cook     This sets or clears the timeout on a key. The timeout can be 0 to clear
724b68101a1SKees Cook     the timeout or a number of seconds to set the expiry time that far into
725b68101a1SKees Cook     the future.
726b68101a1SKees Cook
727b68101a1SKees Cook     The process must have attribute modification access on a key to set its
728b68101a1SKees Cook     timeout. Timeouts may not be set with this function on negative, revoked
729b68101a1SKees Cook     or expired keys.
730b68101a1SKees Cook
731b68101a1SKees Cook
732b68101a1SKees Cook  *  Assume the authority granted to instantiate a key::
733b68101a1SKees Cook
734b68101a1SKees Cook	long keyctl(KEYCTL_ASSUME_AUTHORITY, key_serial_t key);
735b68101a1SKees Cook
736b68101a1SKees Cook     This assumes or divests the authority required to instantiate the
737b68101a1SKees Cook     specified key. Authority can only be assumed if the thread has the
738b68101a1SKees Cook     authorisation key associated with the specified key in its keyrings
739b68101a1SKees Cook     somewhere.
740b68101a1SKees Cook
741b68101a1SKees Cook     Once authority is assumed, searches for keys will also search the
742b68101a1SKees Cook     requester's keyrings using the requester's security label, UID, GID and
743b68101a1SKees Cook     groups.
744b68101a1SKees Cook
745b68101a1SKees Cook     If the requested authority is unavailable, error EPERM will be returned,
746b68101a1SKees Cook     likewise if the authority has been revoked because the target key is
747b68101a1SKees Cook     already instantiated.
748b68101a1SKees Cook
749b68101a1SKees Cook     If the specified key is 0, then any assumed authority will be divested.
750b68101a1SKees Cook
751b68101a1SKees Cook     The assumed authoritative key is inherited across fork and exec.
752b68101a1SKees Cook
753b68101a1SKees Cook
754b68101a1SKees Cook  *  Get the LSM security context attached to a key::
755b68101a1SKees Cook
756b68101a1SKees Cook	long keyctl(KEYCTL_GET_SECURITY, key_serial_t key, char *buffer,
757b68101a1SKees Cook		    size_t buflen)
758b68101a1SKees Cook
759b68101a1SKees Cook     This function returns a string that represents the LSM security context
760b68101a1SKees Cook     attached to a key in the buffer provided.
761b68101a1SKees Cook
762b68101a1SKees Cook     Unless there's an error, it always returns the amount of data it could
763b68101a1SKees Cook     produce, even if that's too big for the buffer, but it won't copy more
764b68101a1SKees Cook     than requested to userspace. If the buffer pointer is NULL then no copy
765b68101a1SKees Cook     will take place.
766b68101a1SKees Cook
767b68101a1SKees Cook     A NUL character is included at the end of the string if the buffer is
768b68101a1SKees Cook     sufficiently big.  This is included in the returned count.  If no LSM is
769b68101a1SKees Cook     in force then an empty string will be returned.
770b68101a1SKees Cook
771b68101a1SKees Cook     A process must have view permission on the key for this function to be
772b68101a1SKees Cook     successful.
773b68101a1SKees Cook
774b68101a1SKees Cook
775b68101a1SKees Cook  *  Install the calling process's session keyring on its parent::
776b68101a1SKees Cook
777b68101a1SKees Cook	long keyctl(KEYCTL_SESSION_TO_PARENT);
778b68101a1SKees Cook
779b68101a1SKees Cook     This functions attempts to install the calling process's session keyring
780b68101a1SKees Cook     on to the calling process's parent, replacing the parent's current session
781b68101a1SKees Cook     keyring.
782b68101a1SKees Cook
783b68101a1SKees Cook     The calling process must have the same ownership as its parent, the
784b68101a1SKees Cook     keyring must have the same ownership as the calling process, the calling
785b68101a1SKees Cook     process must have LINK permission on the keyring and the active LSM module
786b68101a1SKees Cook     mustn't deny permission, otherwise error EPERM will be returned.
787b68101a1SKees Cook
788b68101a1SKees Cook     Error ENOMEM will be returned if there was insufficient memory to complete
789b68101a1SKees Cook     the operation, otherwise 0 will be returned to indicate success.
790b68101a1SKees Cook
791b68101a1SKees Cook     The keyring will be replaced next time the parent process leaves the
792b68101a1SKees Cook     kernel and resumes executing userspace.
793b68101a1SKees Cook
794b68101a1SKees Cook
795b68101a1SKees Cook  *  Invalidate a key::
796b68101a1SKees Cook
797b68101a1SKees Cook	long keyctl(KEYCTL_INVALIDATE, key_serial_t key);
798b68101a1SKees Cook
799b68101a1SKees Cook     This function marks a key as being invalidated and then wakes up the
800b68101a1SKees Cook     garbage collector.  The garbage collector immediately removes invalidated
801b68101a1SKees Cook     keys from all keyrings and deletes the key when its reference count
802b68101a1SKees Cook     reaches zero.
803b68101a1SKees Cook
804b68101a1SKees Cook     Keys that are marked invalidated become invisible to normal key operations
805b68101a1SKees Cook     immediately, though they are still visible in /proc/keys until deleted
806b68101a1SKees Cook     (they're marked with an 'i' flag).
807b68101a1SKees Cook
808b68101a1SKees Cook     A process must have search permission on the key for this function to be
809b68101a1SKees Cook     successful.
810b68101a1SKees Cook
811b68101a1SKees Cook  *  Compute a Diffie-Hellman shared secret or public key::
812b68101a1SKees Cook
813b68101a1SKees Cook	long keyctl(KEYCTL_DH_COMPUTE, struct keyctl_dh_params *params,
814b68101a1SKees Cook		    char *buffer, size_t buflen, struct keyctl_kdf_params *kdf);
815b68101a1SKees Cook
816b68101a1SKees Cook     The params struct contains serial numbers for three keys::
817b68101a1SKees Cook
818b68101a1SKees Cook	 - The prime, p, known to both parties
819b68101a1SKees Cook	 - The local private key
820b68101a1SKees Cook	 - The base integer, which is either a shared generator or the
821b68101a1SKees Cook	   remote public key
822b68101a1SKees Cook
823b68101a1SKees Cook     The value computed is::
824b68101a1SKees Cook
825b68101a1SKees Cook	result = base ^ private (mod prime)
826b68101a1SKees Cook
827b68101a1SKees Cook     If the base is the shared generator, the result is the local
828b68101a1SKees Cook     public key.  If the base is the remote public key, the result is
829b68101a1SKees Cook     the shared secret.
830b68101a1SKees Cook
831b68101a1SKees Cook     If the parameter kdf is NULL, the following applies:
832b68101a1SKees Cook
833b68101a1SKees Cook	 - The buffer length must be at least the length of the prime, or zero.
834b68101a1SKees Cook
835b68101a1SKees Cook	 - If the buffer length is nonzero, the length of the result is
836b68101a1SKees Cook	   returned when it is successfully calculated and copied in to the
837b68101a1SKees Cook	   buffer. When the buffer length is zero, the minimum required
838b68101a1SKees Cook	   buffer length is returned.
839b68101a1SKees Cook
840b68101a1SKees Cook     The kdf parameter allows the caller to apply a key derivation function
841b68101a1SKees Cook     (KDF) on the Diffie-Hellman computation where only the result
842b68101a1SKees Cook     of the KDF is returned to the caller. The KDF is characterized with
843b68101a1SKees Cook     struct keyctl_kdf_params as follows:
844b68101a1SKees Cook
845b68101a1SKees Cook	 - ``char *hashname`` specifies the NUL terminated string identifying
846b68101a1SKees Cook	   the hash used from the kernel crypto API and applied for the KDF
847b68101a1SKees Cook	   operation. The KDF implemenation complies with SP800-56A as well
848b68101a1SKees Cook	   as with SP800-108 (the counter KDF).
849b68101a1SKees Cook
850b68101a1SKees Cook	 - ``char *otherinfo`` specifies the OtherInfo data as documented in
851b68101a1SKees Cook	   SP800-56A section 5.8.1.2. The length of the buffer is given with
852b68101a1SKees Cook	   otherinfolen. The format of OtherInfo is defined by the caller.
853b68101a1SKees Cook	   The otherinfo pointer may be NULL if no OtherInfo shall be used.
854b68101a1SKees Cook
855b68101a1SKees Cook     This function will return error EOPNOTSUPP if the key type is not
856b68101a1SKees Cook     supported, error ENOKEY if the key could not be found, or error
857b68101a1SKees Cook     EACCES if the key is not readable by the caller. In addition, the
858b68101a1SKees Cook     function will return EMSGSIZE when the parameter kdf is non-NULL
859b68101a1SKees Cook     and either the buffer length or the OtherInfo length exceeds the
860b68101a1SKees Cook     allowed length.
861b68101a1SKees Cook
862b68101a1SKees Cook  *  Restrict keyring linkage::
863b68101a1SKees Cook
864b68101a1SKees Cook	long keyctl(KEYCTL_RESTRICT_KEYRING, key_serial_t keyring,
865b68101a1SKees Cook		    const char *type, const char *restriction);
866b68101a1SKees Cook
867b68101a1SKees Cook     An existing keyring can restrict linkage of additional keys by evaluating
868b68101a1SKees Cook     the contents of the key according to a restriction scheme.
869b68101a1SKees Cook
870b68101a1SKees Cook     "keyring" is the key ID for an existing keyring to apply a restriction
871b68101a1SKees Cook     to. It may be empty or may already have keys linked. Existing linked keys
872b68101a1SKees Cook     will remain in the keyring even if the new restriction would reject them.
873b68101a1SKees Cook
874b68101a1SKees Cook     "type" is a registered key type.
875b68101a1SKees Cook
876b68101a1SKees Cook     "restriction" is a string describing how key linkage is to be restricted.
877b68101a1SKees Cook     The format varies depending on the key type, and the string is passed to
878b68101a1SKees Cook     the lookup_restriction() function for the requested type.  It may specify
879b68101a1SKees Cook     a method and relevant data for the restriction such as signature
880b68101a1SKees Cook     verification or constraints on key payload. If the requested key type is
881b68101a1SKees Cook     later unregistered, no keys may be added to the keyring after the key type
882b68101a1SKees Cook     is removed.
883b68101a1SKees Cook
884b68101a1SKees Cook     To apply a keyring restriction the process must have Set Attribute
885b68101a1SKees Cook     permission and the keyring must not be previously restricted.
886b68101a1SKees Cook
8877228b66aSMat Martineau     One application of restricted keyrings is to verify X.509 certificate
8887228b66aSMat Martineau     chains or individual certificate signatures using the asymmetric key type.
8897228b66aSMat Martineau     See Documentation/crypto/asymmetric-keys.txt for specific restrictions
8907228b66aSMat Martineau     applicable to the asymmetric key type.
8917228b66aSMat Martineau
8927228b66aSMat Martineau
893b68101a1SKees CookKernel Services
894b68101a1SKees Cook===============
895b68101a1SKees Cook
896b68101a1SKees CookThe kernel services for key management are fairly simple to deal with. They can
897b68101a1SKees Cookbe broken down into two areas: keys and key types.
898b68101a1SKees Cook
899b68101a1SKees CookDealing with keys is fairly straightforward. Firstly, the kernel service
900b68101a1SKees Cookregisters its type, then it searches for a key of that type. It should retain
901b68101a1SKees Cookthe key as long as it has need of it, and then it should release it. For a
902b68101a1SKees Cookfilesystem or device file, a search would probably be performed during the open
903b68101a1SKees Cookcall, and the key released upon close. How to deal with conflicting keys due to
904b68101a1SKees Cooktwo different users opening the same file is left to the filesystem author to
905b68101a1SKees Cooksolve.
906b68101a1SKees Cook
907b68101a1SKees CookTo access the key manager, the following header must be #included::
908b68101a1SKees Cook
909b68101a1SKees Cook	<linux/key.h>
910b68101a1SKees Cook
911b68101a1SKees CookSpecific key types should have a header file under include/keys/ that should be
912b68101a1SKees Cookused to access that type.  For keys of type "user", for example, that would be::
913b68101a1SKees Cook
914b68101a1SKees Cook	<keys/user-type.h>
915b68101a1SKees Cook
916b68101a1SKees CookNote that there are two different types of pointers to keys that may be
917b68101a1SKees Cookencountered:
918b68101a1SKees Cook
919b68101a1SKees Cook  *  struct key *
920b68101a1SKees Cook
921b68101a1SKees Cook     This simply points to the key structure itself. Key structures will be at
922b68101a1SKees Cook     least four-byte aligned.
923b68101a1SKees Cook
924b68101a1SKees Cook  *  key_ref_t
925b68101a1SKees Cook
926b68101a1SKees Cook     This is equivalent to a ``struct key *``, but the least significant bit is set
927b68101a1SKees Cook     if the caller "possesses" the key. By "possession" it is meant that the
928b68101a1SKees Cook     calling processes has a searchable link to the key from one of its
929b68101a1SKees Cook     keyrings. There are three functions for dealing with these::
930b68101a1SKees Cook
931b68101a1SKees Cook	key_ref_t make_key_ref(const struct key *key, bool possession);
932b68101a1SKees Cook
933b68101a1SKees Cook	struct key *key_ref_to_ptr(const key_ref_t key_ref);
934b68101a1SKees Cook
935b68101a1SKees Cook	bool is_key_possessed(const key_ref_t key_ref);
936b68101a1SKees Cook
937b68101a1SKees Cook     The first function constructs a key reference from a key pointer and
938b68101a1SKees Cook     possession information (which must be true or false).
939b68101a1SKees Cook
940b68101a1SKees Cook     The second function retrieves the key pointer from a reference and the
941b68101a1SKees Cook     third retrieves the possession flag.
942b68101a1SKees Cook
943b68101a1SKees CookWhen accessing a key's payload contents, certain precautions must be taken to
944b68101a1SKees Cookprevent access vs modification races. See the section "Notes on accessing
945b68101a1SKees Cookpayload contents" for more information.
946b68101a1SKees Cook
947b68101a1SKees Cook *  To search for a key, call::
948b68101a1SKees Cook
949b68101a1SKees Cook	struct key *request_key(const struct key_type *type,
950b68101a1SKees Cook				const char *description,
951b68101a1SKees Cook				const char *callout_info);
952b68101a1SKees Cook
953b68101a1SKees Cook    This is used to request a key or keyring with a description that matches
954b68101a1SKees Cook    the description specified according to the key type's match_preparse()
955b68101a1SKees Cook    method. This permits approximate matching to occur. If callout_string is
956b68101a1SKees Cook    not NULL, then /sbin/request-key will be invoked in an attempt to obtain
957b68101a1SKees Cook    the key from userspace. In that case, callout_string will be passed as an
958b68101a1SKees Cook    argument to the program.
959b68101a1SKees Cook
960b68101a1SKees Cook    Should the function fail error ENOKEY, EKEYEXPIRED or EKEYREVOKED will be
961b68101a1SKees Cook    returned.
962b68101a1SKees Cook
963b68101a1SKees Cook    If successful, the key will have been attached to the default keyring for
964b68101a1SKees Cook    implicitly obtained request-key keys, as set by KEYCTL_SET_REQKEY_KEYRING.
965b68101a1SKees Cook
966adf31eebSJosh Holland    See also Documentation/security/keys/request-key.rst.
967b68101a1SKees Cook
968b68101a1SKees Cook
969b68101a1SKees Cook *  To search for a key, passing auxiliary data to the upcaller, call::
970b68101a1SKees Cook
971b68101a1SKees Cook	struct key *request_key_with_auxdata(const struct key_type *type,
972b68101a1SKees Cook					     const char *description,
973b68101a1SKees Cook					     const void *callout_info,
974b68101a1SKees Cook					     size_t callout_len,
975b68101a1SKees Cook					     void *aux);
976b68101a1SKees Cook
977b68101a1SKees Cook    This is identical to request_key(), except that the auxiliary data is
978b68101a1SKees Cook    passed to the key_type->request_key() op if it exists, and the callout_info
979b68101a1SKees Cook    is a blob of length callout_len, if given (the length may be 0).
980b68101a1SKees Cook
981b68101a1SKees Cook
982b68101a1SKees Cook *  A key can be requested asynchronously by calling one of::
983b68101a1SKees Cook
984b68101a1SKees Cook	struct key *request_key_async(const struct key_type *type,
985b68101a1SKees Cook				      const char *description,
986b68101a1SKees Cook				      const void *callout_info,
987b68101a1SKees Cook				      size_t callout_len);
988b68101a1SKees Cook
989b68101a1SKees Cook    or::
990b68101a1SKees Cook
991b68101a1SKees Cook	struct key *request_key_async_with_auxdata(const struct key_type *type,
992b68101a1SKees Cook						   const char *description,
993b68101a1SKees Cook						   const char *callout_info,
994b68101a1SKees Cook					     	   size_t callout_len,
995b68101a1SKees Cook					     	   void *aux);
996b68101a1SKees Cook
997b68101a1SKees Cook    which are asynchronous equivalents of request_key() and
998b68101a1SKees Cook    request_key_with_auxdata() respectively.
999b68101a1SKees Cook
1000b68101a1SKees Cook    These two functions return with the key potentially still under
1001b68101a1SKees Cook    construction.  To wait for construction completion, the following should be
1002b68101a1SKees Cook    called::
1003b68101a1SKees Cook
1004b68101a1SKees Cook	int wait_for_key_construction(struct key *key, bool intr);
1005b68101a1SKees Cook
1006b68101a1SKees Cook    The function will wait for the key to finish being constructed and then
1007b68101a1SKees Cook    invokes key_validate() to return an appropriate value to indicate the state
1008b68101a1SKees Cook    of the key (0 indicates the key is usable).
1009b68101a1SKees Cook
1010b68101a1SKees Cook    If intr is true, then the wait can be interrupted by a signal, in which
1011b68101a1SKees Cook    case error ERESTARTSYS will be returned.
1012b68101a1SKees Cook
1013b68101a1SKees Cook
1014b68101a1SKees Cook *  When it is no longer required, the key should be released using::
1015b68101a1SKees Cook
1016b68101a1SKees Cook	void key_put(struct key *key);
1017b68101a1SKees Cook
1018b68101a1SKees Cook    Or::
1019b68101a1SKees Cook
1020b68101a1SKees Cook	void key_ref_put(key_ref_t key_ref);
1021b68101a1SKees Cook
1022b68101a1SKees Cook    These can be called from interrupt context. If CONFIG_KEYS is not set then
1023b68101a1SKees Cook    the argument will not be parsed.
1024b68101a1SKees Cook
1025b68101a1SKees Cook
1026b68101a1SKees Cook *  Extra references can be made to a key by calling one of the following
1027b68101a1SKees Cook    functions::
1028b68101a1SKees Cook
1029b68101a1SKees Cook	struct key *__key_get(struct key *key);
1030b68101a1SKees Cook	struct key *key_get(struct key *key);
1031b68101a1SKees Cook
1032b68101a1SKees Cook    Keys so references will need to be disposed of by calling key_put() when
1033b68101a1SKees Cook    they've been finished with.  The key pointer passed in will be returned.
1034b68101a1SKees Cook
1035b68101a1SKees Cook    In the case of key_get(), if the pointer is NULL or CONFIG_KEYS is not set
1036b68101a1SKees Cook    then the key will not be dereferenced and no increment will take place.
1037b68101a1SKees Cook
1038b68101a1SKees Cook
1039b68101a1SKees Cook *  A key's serial number can be obtained by calling::
1040b68101a1SKees Cook
1041b68101a1SKees Cook	key_serial_t key_serial(struct key *key);
1042b68101a1SKees Cook
1043b68101a1SKees Cook    If key is NULL or if CONFIG_KEYS is not set then 0 will be returned (in the
1044b68101a1SKees Cook    latter case without parsing the argument).
1045b68101a1SKees Cook
1046b68101a1SKees Cook
1047b68101a1SKees Cook *  If a keyring was found in the search, this can be further searched by::
1048b68101a1SKees Cook
1049b68101a1SKees Cook	key_ref_t keyring_search(key_ref_t keyring_ref,
1050b68101a1SKees Cook				 const struct key_type *type,
1051b68101a1SKees Cook				 const char *description)
1052b68101a1SKees Cook
1053b68101a1SKees Cook    This searches the keyring tree specified for a matching key. Error ENOKEY
1054b68101a1SKees Cook    is returned upon failure (use IS_ERR/PTR_ERR to determine). If successful,
1055b68101a1SKees Cook    the returned key will need to be released.
1056b68101a1SKees Cook
1057b68101a1SKees Cook    The possession attribute from the keyring reference is used to control
1058b68101a1SKees Cook    access through the permissions mask and is propagated to the returned key
1059b68101a1SKees Cook    reference pointer if successful.
1060b68101a1SKees Cook
1061b68101a1SKees Cook
1062b68101a1SKees Cook *  A keyring can be created by::
1063b68101a1SKees Cook
1064b68101a1SKees Cook	struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
1065b68101a1SKees Cook				  const struct cred *cred,
1066b68101a1SKees Cook				  key_perm_t perm,
1067b68101a1SKees Cook				  struct key_restriction *restrict_link,
1068b68101a1SKees Cook				  unsigned long flags,
1069b68101a1SKees Cook				  struct key *dest);
1070b68101a1SKees Cook
1071b68101a1SKees Cook    This creates a keyring with the given attributes and returns it.  If dest
1072b68101a1SKees Cook    is not NULL, the new keyring will be linked into the keyring to which it
1073b68101a1SKees Cook    points.  No permission checks are made upon the destination keyring.
1074b68101a1SKees Cook
1075b68101a1SKees Cook    Error EDQUOT can be returned if the keyring would overload the quota (pass
1076b68101a1SKees Cook    KEY_ALLOC_NOT_IN_QUOTA in flags if the keyring shouldn't be accounted
1077b68101a1SKees Cook    towards the user's quota).  Error ENOMEM can also be returned.
1078b68101a1SKees Cook
1079b68101a1SKees Cook    If restrict_link is not NULL, it should point to a structure that contains
1080b68101a1SKees Cook    the function that will be called each time an attempt is made to link a
1081b68101a1SKees Cook    key into the new keyring.  The structure may also contain a key pointer
1082b68101a1SKees Cook    and an associated key type.  The function is called to check whether a key
1083b68101a1SKees Cook    may be added into the keyring or not.  The key type is used by the garbage
1084b68101a1SKees Cook    collector to clean up function or data pointers in this structure if the
1085b68101a1SKees Cook    given key type is unregistered.  Callers of key_create_or_update() within
1086b68101a1SKees Cook    the kernel can pass KEY_ALLOC_BYPASS_RESTRICTION to suppress the check.
1087b68101a1SKees Cook    An example of using this is to manage rings of cryptographic keys that are
1088b68101a1SKees Cook    set up when the kernel boots where userspace is also permitted to add keys
1089b68101a1SKees Cook    - provided they can be verified by a key the kernel already has.
1090b68101a1SKees Cook
1091b68101a1SKees Cook    When called, the restriction function will be passed the keyring being
1092b68101a1SKees Cook    added to, the key type, the payload of the key being added, and data to be
1093b68101a1SKees Cook    used in the restriction check.  Note that when a new key is being created,
1094b68101a1SKees Cook    this is called between payload preparsing and actual key creation.  The
1095b68101a1SKees Cook    function should return 0 to allow the link or an error to reject it.
1096b68101a1SKees Cook
1097b68101a1SKees Cook    A convenience function, restrict_link_reject, exists to always return
1098b68101a1SKees Cook    -EPERM to in this case.
1099b68101a1SKees Cook
1100b68101a1SKees Cook
1101b68101a1SKees Cook *  To check the validity of a key, this function can be called::
1102b68101a1SKees Cook
1103b68101a1SKees Cook	int validate_key(struct key *key);
1104b68101a1SKees Cook
1105b68101a1SKees Cook    This checks that the key in question hasn't expired or and hasn't been
1106b68101a1SKees Cook    revoked. Should the key be invalid, error EKEYEXPIRED or EKEYREVOKED will
1107b68101a1SKees Cook    be returned. If the key is NULL or if CONFIG_KEYS is not set then 0 will be
1108b68101a1SKees Cook    returned (in the latter case without parsing the argument).
1109b68101a1SKees Cook
1110b68101a1SKees Cook
1111b68101a1SKees Cook *  To register a key type, the following function should be called::
1112b68101a1SKees Cook
1113b68101a1SKees Cook	int register_key_type(struct key_type *type);
1114b68101a1SKees Cook
1115b68101a1SKees Cook    This will return error EEXIST if a type of the same name is already
1116b68101a1SKees Cook    present.
1117b68101a1SKees Cook
1118b68101a1SKees Cook
1119b68101a1SKees Cook *  To unregister a key type, call::
1120b68101a1SKees Cook
1121b68101a1SKees Cook	void unregister_key_type(struct key_type *type);
1122b68101a1SKees Cook
1123b68101a1SKees Cook
1124b68101a1SKees CookUnder some circumstances, it may be desirable to deal with a bundle of keys.
1125b68101a1SKees CookThe facility provides access to the keyring type for managing such a bundle::
1126b68101a1SKees Cook
1127b68101a1SKees Cook	struct key_type key_type_keyring;
1128b68101a1SKees Cook
1129b68101a1SKees CookThis can be used with a function such as request_key() to find a specific
1130b68101a1SKees Cookkeyring in a process's keyrings.  A keyring thus found can then be searched
1131b68101a1SKees Cookwith keyring_search().  Note that it is not possible to use request_key() to
1132b68101a1SKees Cooksearch a specific keyring, so using keyrings in this way is of limited utility.
1133b68101a1SKees Cook
1134b68101a1SKees Cook
1135b68101a1SKees CookNotes On Accessing Payload Contents
1136b68101a1SKees Cook===================================
1137b68101a1SKees Cook
1138b68101a1SKees CookThe simplest payload is just data stored in key->payload directly.  In this
1139b68101a1SKees Cookcase, there's no need to indulge in RCU or locking when accessing the payload.
1140b68101a1SKees Cook
1141b68101a1SKees CookMore complex payload contents must be allocated and pointers to them set in the
1142b68101a1SKees Cookkey->payload.data[] array.  One of the following ways must be selected to
1143b68101a1SKees Cookaccess the data:
1144b68101a1SKees Cook
1145b68101a1SKees Cook  1) Unmodifiable key type.
1146b68101a1SKees Cook
1147b68101a1SKees Cook     If the key type does not have a modify method, then the key's payload can
1148b68101a1SKees Cook     be accessed without any form of locking, provided that it's known to be
1149b68101a1SKees Cook     instantiated (uninstantiated keys cannot be "found").
1150b68101a1SKees Cook
1151b68101a1SKees Cook  2) The key's semaphore.
1152b68101a1SKees Cook
1153b68101a1SKees Cook     The semaphore could be used to govern access to the payload and to control
1154b68101a1SKees Cook     the payload pointer. It must be write-locked for modifications and would
1155b68101a1SKees Cook     have to be read-locked for general access. The disadvantage of doing this
1156b68101a1SKees Cook     is that the accessor may be required to sleep.
1157b68101a1SKees Cook
1158b68101a1SKees Cook  3) RCU.
1159b68101a1SKees Cook
1160b68101a1SKees Cook     RCU must be used when the semaphore isn't already held; if the semaphore
1161b68101a1SKees Cook     is held then the contents can't change under you unexpectedly as the
1162b68101a1SKees Cook     semaphore must still be used to serialise modifications to the key. The
1163b68101a1SKees Cook     key management code takes care of this for the key type.
1164b68101a1SKees Cook
1165b68101a1SKees Cook     However, this means using::
1166b68101a1SKees Cook
1167b68101a1SKees Cook	rcu_read_lock() ... rcu_dereference() ... rcu_read_unlock()
1168b68101a1SKees Cook
1169b68101a1SKees Cook     to read the pointer, and::
1170b68101a1SKees Cook
1171b68101a1SKees Cook	rcu_dereference() ... rcu_assign_pointer() ... call_rcu()
1172b68101a1SKees Cook
1173b68101a1SKees Cook     to set the pointer and dispose of the old contents after a grace period.
1174b68101a1SKees Cook     Note that only the key type should ever modify a key's payload.
1175b68101a1SKees Cook
1176b68101a1SKees Cook     Furthermore, an RCU controlled payload must hold a struct rcu_head for the
1177b68101a1SKees Cook     use of call_rcu() and, if the payload is of variable size, the length of
1178b68101a1SKees Cook     the payload. key->datalen cannot be relied upon to be consistent with the
1179b68101a1SKees Cook     payload just dereferenced if the key's semaphore is not held.
1180b68101a1SKees Cook
1181b68101a1SKees Cook     Note that key->payload.data[0] has a shadow that is marked for __rcu
1182b68101a1SKees Cook     usage.  This is called key->payload.rcu_data0.  The following accessors
1183b68101a1SKees Cook     wrap the RCU calls to this element:
1184b68101a1SKees Cook
1185b68101a1SKees Cook     a) Set or change the first payload pointer::
1186b68101a1SKees Cook
1187b68101a1SKees Cook		rcu_assign_keypointer(struct key *key, void *data);
1188b68101a1SKees Cook
1189b68101a1SKees Cook     b) Read the first payload pointer with the key semaphore held::
1190b68101a1SKees Cook
1191b68101a1SKees Cook		[const] void *dereference_key_locked([const] struct key *key);
1192b68101a1SKees Cook
1193b68101a1SKees Cook	 Note that the return value will inherit its constness from the key
1194b68101a1SKees Cook	 parameter.  Static analysis will give an error if it things the lock
1195b68101a1SKees Cook	 isn't held.
1196b68101a1SKees Cook
1197b68101a1SKees Cook     c) Read the first payload pointer with the RCU read lock held::
1198b68101a1SKees Cook
1199b68101a1SKees Cook		const void *dereference_key_rcu(const struct key *key);
1200b68101a1SKees Cook
1201b68101a1SKees Cook
1202b68101a1SKees CookDefining a Key Type
1203b68101a1SKees Cook===================
1204b68101a1SKees Cook
1205b68101a1SKees CookA kernel service may want to define its own key type. For instance, an AFS
1206b68101a1SKees Cookfilesystem might want to define a Kerberos 5 ticket key type. To do this, it
1207b68101a1SKees Cookauthor fills in a key_type struct and registers it with the system.
1208b68101a1SKees Cook
1209b68101a1SKees CookSource files that implement key types should include the following header file::
1210b68101a1SKees Cook
1211b68101a1SKees Cook	<linux/key-type.h>
1212b68101a1SKees Cook
1213b68101a1SKees CookThe structure has a number of fields, some of which are mandatory:
1214b68101a1SKees Cook
1215b68101a1SKees Cook  *  ``const char *name``
1216b68101a1SKees Cook
1217b68101a1SKees Cook     The name of the key type. This is used to translate a key type name
1218b68101a1SKees Cook     supplied by userspace into a pointer to the structure.
1219b68101a1SKees Cook
1220b68101a1SKees Cook
1221b68101a1SKees Cook  *  ``size_t def_datalen``
1222b68101a1SKees Cook
1223b68101a1SKees Cook     This is optional - it supplies the default payload data length as
1224b68101a1SKees Cook     contributed to the quota. If the key type's payload is always or almost
1225b68101a1SKees Cook     always the same size, then this is a more efficient way to do things.
1226b68101a1SKees Cook
1227b68101a1SKees Cook     The data length (and quota) on a particular key can always be changed
1228b68101a1SKees Cook     during instantiation or update by calling::
1229b68101a1SKees Cook
1230b68101a1SKees Cook	int key_payload_reserve(struct key *key, size_t datalen);
1231b68101a1SKees Cook
1232b68101a1SKees Cook     With the revised data length. Error EDQUOT will be returned if this is not
1233b68101a1SKees Cook     viable.
1234b68101a1SKees Cook
1235b68101a1SKees Cook
1236b68101a1SKees Cook  *  ``int (*vet_description)(const char *description);``
1237b68101a1SKees Cook
1238b68101a1SKees Cook     This optional method is called to vet a key description.  If the key type
1239b68101a1SKees Cook     doesn't approve of the key description, it may return an error, otherwise
1240b68101a1SKees Cook     it should return 0.
1241b68101a1SKees Cook
1242b68101a1SKees Cook
1243b68101a1SKees Cook  *  ``int (*preparse)(struct key_preparsed_payload *prep);``
1244b68101a1SKees Cook
1245b68101a1SKees Cook     This optional method permits the key type to attempt to parse payload
1246b68101a1SKees Cook     before a key is created (add key) or the key semaphore is taken (update or
1247b68101a1SKees Cook     instantiate key).  The structure pointed to by prep looks like::
1248b68101a1SKees Cook
1249b68101a1SKees Cook	struct key_preparsed_payload {
1250b68101a1SKees Cook		char		*description;
1251b68101a1SKees Cook		union key_payload payload;
1252b68101a1SKees Cook		const void	*data;
1253b68101a1SKees Cook		size_t		datalen;
1254b68101a1SKees Cook		size_t		quotalen;
1255b68101a1SKees Cook		time_t		expiry;
1256b68101a1SKees Cook	};
1257b68101a1SKees Cook
1258b68101a1SKees Cook     Before calling the method, the caller will fill in data and datalen with
1259b68101a1SKees Cook     the payload blob parameters; quotalen will be filled in with the default
1260b68101a1SKees Cook     quota size from the key type; expiry will be set to TIME_T_MAX and the
1261b68101a1SKees Cook     rest will be cleared.
1262b68101a1SKees Cook
1263b68101a1SKees Cook     If a description can be proposed from the payload contents, that should be
1264b68101a1SKees Cook     attached as a string to the description field.  This will be used for the
1265b68101a1SKees Cook     key description if the caller of add_key() passes NULL or "".
1266b68101a1SKees Cook
1267b68101a1SKees Cook     The method can attach anything it likes to payload.  This is merely passed
1268b68101a1SKees Cook     along to the instantiate() or update() operations.  If set, the expiry
1269b68101a1SKees Cook     time will be applied to the key if it is instantiated from this data.
1270b68101a1SKees Cook
1271b68101a1SKees Cook     The method should return 0 if successful or a negative error code
1272b68101a1SKees Cook     otherwise.
1273b68101a1SKees Cook
1274b68101a1SKees Cook
1275b68101a1SKees Cook  *  ``void (*free_preparse)(struct key_preparsed_payload *prep);``
1276b68101a1SKees Cook
1277b68101a1SKees Cook     This method is only required if the preparse() method is provided,
1278b68101a1SKees Cook     otherwise it is unused.  It cleans up anything attached to the description
1279b68101a1SKees Cook     and payload fields of the key_preparsed_payload struct as filled in by the
1280b68101a1SKees Cook     preparse() method.  It will always be called after preparse() returns
1281b68101a1SKees Cook     successfully, even if instantiate() or update() succeed.
1282b68101a1SKees Cook
1283b68101a1SKees Cook
1284b68101a1SKees Cook  *  ``int (*instantiate)(struct key *key, struct key_preparsed_payload *prep);``
1285b68101a1SKees Cook
1286b68101a1SKees Cook     This method is called to attach a payload to a key during construction.
1287b68101a1SKees Cook     The payload attached need not bear any relation to the data passed to this
1288b68101a1SKees Cook     function.
1289b68101a1SKees Cook
1290b68101a1SKees Cook     The prep->data and prep->datalen fields will define the original payload
1291b68101a1SKees Cook     blob.  If preparse() was supplied then other fields may be filled in also.
1292b68101a1SKees Cook
1293b68101a1SKees Cook     If the amount of data attached to the key differs from the size in
1294b68101a1SKees Cook     keytype->def_datalen, then key_payload_reserve() should be called.
1295b68101a1SKees Cook
1296b68101a1SKees Cook     This method does not have to lock the key in order to attach a payload.
1297b68101a1SKees Cook     The fact that KEY_FLAG_INSTANTIATED is not set in key->flags prevents
1298b68101a1SKees Cook     anything else from gaining access to the key.
1299b68101a1SKees Cook
1300b68101a1SKees Cook     It is safe to sleep in this method.
1301b68101a1SKees Cook
1302b68101a1SKees Cook     generic_key_instantiate() is provided to simply copy the data from
1303b68101a1SKees Cook     prep->payload.data[] to key->payload.data[], with RCU-safe assignment on
1304b68101a1SKees Cook     the first element.  It will then clear prep->payload.data[] so that the
1305b68101a1SKees Cook     free_preparse method doesn't release the data.
1306b68101a1SKees Cook
1307b68101a1SKees Cook
1308b68101a1SKees Cook  *  ``int (*update)(struct key *key, const void *data, size_t datalen);``
1309b68101a1SKees Cook
1310b68101a1SKees Cook     If this type of key can be updated, then this method should be provided.
1311b68101a1SKees Cook     It is called to update a key's payload from the blob of data provided.
1312b68101a1SKees Cook
1313b68101a1SKees Cook     The prep->data and prep->datalen fields will define the original payload
1314b68101a1SKees Cook     blob.  If preparse() was supplied then other fields may be filled in also.
1315b68101a1SKees Cook
1316b68101a1SKees Cook     key_payload_reserve() should be called if the data length might change
1317b68101a1SKees Cook     before any changes are actually made. Note that if this succeeds, the type
1318b68101a1SKees Cook     is committed to changing the key because it's already been altered, so all
1319b68101a1SKees Cook     memory allocation must be done first.
1320b68101a1SKees Cook
1321b68101a1SKees Cook     The key will have its semaphore write-locked before this method is called,
1322b68101a1SKees Cook     but this only deters other writers; any changes to the key's payload must
1323b68101a1SKees Cook     be made under RCU conditions, and call_rcu() must be used to dispose of
1324b68101a1SKees Cook     the old payload.
1325b68101a1SKees Cook
1326b68101a1SKees Cook     key_payload_reserve() should be called before the changes are made, but
1327b68101a1SKees Cook     after all allocations and other potentially failing function calls are
1328b68101a1SKees Cook     made.
1329b68101a1SKees Cook
1330b68101a1SKees Cook     It is safe to sleep in this method.
1331b68101a1SKees Cook
1332b68101a1SKees Cook
1333b68101a1SKees Cook  *  ``int (*match_preparse)(struct key_match_data *match_data);``
1334b68101a1SKees Cook
1335b68101a1SKees Cook     This method is optional.  It is called when a key search is about to be
1336b68101a1SKees Cook     performed.  It is given the following structure::
1337b68101a1SKees Cook
1338b68101a1SKees Cook	struct key_match_data {
1339b68101a1SKees Cook		bool (*cmp)(const struct key *key,
1340b68101a1SKees Cook			    const struct key_match_data *match_data);
1341b68101a1SKees Cook		const void	*raw_data;
1342b68101a1SKees Cook		void		*preparsed;
1343b68101a1SKees Cook		unsigned	lookup_type;
1344b68101a1SKees Cook	};
1345b68101a1SKees Cook
1346b68101a1SKees Cook     On entry, raw_data will be pointing to the criteria to be used in matching
1347b68101a1SKees Cook     a key by the caller and should not be modified.  ``(*cmp)()`` will be pointing
1348b68101a1SKees Cook     to the default matcher function (which does an exact description match
1349b68101a1SKees Cook     against raw_data) and lookup_type will be set to indicate a direct lookup.
1350b68101a1SKees Cook
1351b68101a1SKees Cook     The following lookup_type values are available:
1352b68101a1SKees Cook
1353b68101a1SKees Cook       *  KEYRING_SEARCH_LOOKUP_DIRECT - A direct lookup hashes the type and
1354b68101a1SKees Cook      	  description to narrow down the search to a small number of keys.
1355b68101a1SKees Cook
1356b68101a1SKees Cook       *  KEYRING_SEARCH_LOOKUP_ITERATE - An iterative lookup walks all the
1357b68101a1SKees Cook      	  keys in the keyring until one is matched.  This must be used for any
1358b68101a1SKees Cook      	  search that's not doing a simple direct match on the key description.
1359b68101a1SKees Cook
1360b68101a1SKees Cook     The method may set cmp to point to a function of its choice that does some
1361b68101a1SKees Cook     other form of match, may set lookup_type to KEYRING_SEARCH_LOOKUP_ITERATE
1362b68101a1SKees Cook     and may attach something to the preparsed pointer for use by ``(*cmp)()``.
1363b68101a1SKees Cook     ``(*cmp)()`` should return true if a key matches and false otherwise.
1364b68101a1SKees Cook
1365b68101a1SKees Cook     If preparsed is set, it may be necessary to use the match_free() method to
1366b68101a1SKees Cook     clean it up.
1367b68101a1SKees Cook
1368b68101a1SKees Cook     The method should return 0 if successful or a negative error code
1369b68101a1SKees Cook     otherwise.
1370b68101a1SKees Cook
1371b68101a1SKees Cook     It is permitted to sleep in this method, but ``(*cmp)()`` may not sleep as
1372b68101a1SKees Cook     locks will be held over it.
1373b68101a1SKees Cook
1374b68101a1SKees Cook     If match_preparse() is not provided, keys of this type will be matched
1375b68101a1SKees Cook     exactly by their description.
1376b68101a1SKees Cook
1377b68101a1SKees Cook
1378b68101a1SKees Cook  *  ``void (*match_free)(struct key_match_data *match_data);``
1379b68101a1SKees Cook
1380b68101a1SKees Cook     This method is optional.  If given, it called to clean up
1381b68101a1SKees Cook     match_data->preparsed after a successful call to match_preparse().
1382b68101a1SKees Cook
1383b68101a1SKees Cook
1384b68101a1SKees Cook  *  ``void (*revoke)(struct key *key);``
1385b68101a1SKees Cook
1386b68101a1SKees Cook     This method is optional.  It is called to discard part of the payload
1387b68101a1SKees Cook     data upon a key being revoked.  The caller will have the key semaphore
1388b68101a1SKees Cook     write-locked.
1389b68101a1SKees Cook
1390b68101a1SKees Cook     It is safe to sleep in this method, though care should be taken to avoid
1391b68101a1SKees Cook     a deadlock against the key semaphore.
1392b68101a1SKees Cook
1393b68101a1SKees Cook
1394b68101a1SKees Cook  *  ``void (*destroy)(struct key *key);``
1395b68101a1SKees Cook
1396b68101a1SKees Cook     This method is optional. It is called to discard the payload data on a key
1397b68101a1SKees Cook     when it is being destroyed.
1398b68101a1SKees Cook
1399b68101a1SKees Cook     This method does not need to lock the key to access the payload; it can
1400b68101a1SKees Cook     consider the key as being inaccessible at this time. Note that the key's
1401b68101a1SKees Cook     type may have been changed before this function is called.
1402b68101a1SKees Cook
1403b68101a1SKees Cook     It is not safe to sleep in this method; the caller may hold spinlocks.
1404b68101a1SKees Cook
1405b68101a1SKees Cook
1406b68101a1SKees Cook  *  ``void (*describe)(const struct key *key, struct seq_file *p);``
1407b68101a1SKees Cook
1408b68101a1SKees Cook     This method is optional. It is called during /proc/keys reading to
1409b68101a1SKees Cook     summarise a key's description and payload in text form.
1410b68101a1SKees Cook
1411b68101a1SKees Cook     This method will be called with the RCU read lock held. rcu_dereference()
1412b68101a1SKees Cook     should be used to read the payload pointer if the payload is to be
1413b68101a1SKees Cook     accessed. key->datalen cannot be trusted to stay consistent with the
1414b68101a1SKees Cook     contents of the payload.
1415b68101a1SKees Cook
1416b68101a1SKees Cook     The description will not change, though the key's state may.
1417b68101a1SKees Cook
1418b68101a1SKees Cook     It is not safe to sleep in this method; the RCU read lock is held by the
1419b68101a1SKees Cook     caller.
1420b68101a1SKees Cook
1421b68101a1SKees Cook
1422b68101a1SKees Cook  *  ``long (*read)(const struct key *key, char __user *buffer, size_t buflen);``
1423b68101a1SKees Cook
1424b68101a1SKees Cook     This method is optional. It is called by KEYCTL_READ to translate the
1425b68101a1SKees Cook     key's payload into something a blob of data for userspace to deal with.
1426b68101a1SKees Cook     Ideally, the blob should be in the same format as that passed in to the
1427b68101a1SKees Cook     instantiate and update methods.
1428b68101a1SKees Cook
1429b68101a1SKees Cook     If successful, the blob size that could be produced should be returned
1430b68101a1SKees Cook     rather than the size copied.
1431b68101a1SKees Cook
1432b68101a1SKees Cook     This method will be called with the key's semaphore read-locked. This will
1433b68101a1SKees Cook     prevent the key's payload changing. It is not necessary to use RCU locking
1434b68101a1SKees Cook     when accessing the key's payload. It is safe to sleep in this method, such
1435b68101a1SKees Cook     as might happen when the userspace buffer is accessed.
1436b68101a1SKees Cook
1437b68101a1SKees Cook
1438b68101a1SKees Cook  *  ``int (*request_key)(struct key_construction *cons, const char *op, void *aux);``
1439b68101a1SKees Cook
1440b68101a1SKees Cook     This method is optional.  If provided, request_key() and friends will
1441b68101a1SKees Cook     invoke this function rather than upcalling to /sbin/request-key to operate
1442b68101a1SKees Cook     upon a key of this type.
1443b68101a1SKees Cook
1444b68101a1SKees Cook     The aux parameter is as passed to request_key_async_with_auxdata() and
1445b68101a1SKees Cook     similar or is NULL otherwise.  Also passed are the construction record for
1446b68101a1SKees Cook     the key to be operated upon and the operation type (currently only
1447b68101a1SKees Cook     "create").
1448b68101a1SKees Cook
1449b68101a1SKees Cook     This method is permitted to return before the upcall is complete, but the
1450b68101a1SKees Cook     following function must be called under all circumstances to complete the
1451b68101a1SKees Cook     instantiation process, whether or not it succeeds, whether or not there's
1452b68101a1SKees Cook     an error::
1453b68101a1SKees Cook
1454b68101a1SKees Cook	void complete_request_key(struct key_construction *cons, int error);
1455b68101a1SKees Cook
1456b68101a1SKees Cook     The error parameter should be 0 on success, -ve on error.  The
1457b68101a1SKees Cook     construction record is destroyed by this action and the authorisation key
1458b68101a1SKees Cook     will be revoked.  If an error is indicated, the key under construction
1459b68101a1SKees Cook     will be negatively instantiated if it wasn't already instantiated.
1460b68101a1SKees Cook
1461b68101a1SKees Cook     If this method returns an error, that error will be returned to the
1462b68101a1SKees Cook     caller of request_key*().  complete_request_key() must be called prior to
1463b68101a1SKees Cook     returning.
1464b68101a1SKees Cook
1465b68101a1SKees Cook     The key under construction and the authorisation key can be found in the
1466b68101a1SKees Cook     key_construction struct pointed to by cons:
1467b68101a1SKees Cook
1468b68101a1SKees Cook      *  ``struct key *key;``
1469b68101a1SKees Cook
1470b68101a1SKees Cook     	 The key under construction.
1471b68101a1SKees Cook
1472b68101a1SKees Cook      *  ``struct key *authkey;``
1473b68101a1SKees Cook
1474b68101a1SKees Cook     	 The authorisation key.
1475b68101a1SKees Cook
1476b68101a1SKees Cook
1477b68101a1SKees Cook  *  ``struct key_restriction *(*lookup_restriction)(const char *params);``
1478b68101a1SKees Cook
1479b68101a1SKees Cook     This optional method is used to enable userspace configuration of keyring
1480b68101a1SKees Cook     restrictions. The restriction parameter string (not including the key type
1481b68101a1SKees Cook     name) is passed in, and this method returns a pointer to a key_restriction
1482b68101a1SKees Cook     structure containing the relevant functions and data to evaluate each
1483b68101a1SKees Cook     attempted key link operation. If there is no match, -EINVAL is returned.
1484b68101a1SKees Cook
1485b68101a1SKees Cook
1486b68101a1SKees CookRequest-Key Callback Service
1487b68101a1SKees Cook============================
1488b68101a1SKees Cook
1489b68101a1SKees CookTo create a new key, the kernel will attempt to execute the following command
1490b68101a1SKees Cookline::
1491b68101a1SKees Cook
1492b68101a1SKees Cook	/sbin/request-key create <key> <uid> <gid> \
1493b68101a1SKees Cook		<threadring> <processring> <sessionring> <callout_info>
1494b68101a1SKees Cook
1495b68101a1SKees Cook<key> is the key being constructed, and the three keyrings are the process
1496b68101a1SKees Cookkeyrings from the process that caused the search to be issued. These are
1497b68101a1SKees Cookincluded for two reasons:
1498b68101a1SKees Cook
1499b68101a1SKees Cook   1  There may be an authentication token in one of the keyrings that is
1500b68101a1SKees Cook      required to obtain the key, eg: a Kerberos Ticket-Granting Ticket.
1501b68101a1SKees Cook
1502b68101a1SKees Cook   2  The new key should probably be cached in one of these rings.
1503b68101a1SKees Cook
1504b68101a1SKees CookThis program should set it UID and GID to those specified before attempting to
1505b68101a1SKees Cookaccess any more keys. It may then look around for a user specific process to
1506b68101a1SKees Cookhand the request off to (perhaps a path held in placed in another key by, for
1507b68101a1SKees Cookexample, the KDE desktop manager).
1508b68101a1SKees Cook
1509b68101a1SKees CookThe program (or whatever it calls) should finish construction of the key by
1510b68101a1SKees Cookcalling KEYCTL_INSTANTIATE or KEYCTL_INSTANTIATE_IOV, which also permits it to
1511b68101a1SKees Cookcache the key in one of the keyrings (probably the session ring) before
1512b68101a1SKees Cookreturning.  Alternatively, the key can be marked as negative with KEYCTL_NEGATE
1513b68101a1SKees Cookor KEYCTL_REJECT; this also permits the key to be cached in one of the
1514b68101a1SKees Cookkeyrings.
1515b68101a1SKees Cook
1516b68101a1SKees CookIf it returns with the key remaining in the unconstructed state, the key will
1517b68101a1SKees Cookbe marked as being negative, it will be added to the session keyring, and an
1518b68101a1SKees Cookerror will be returned to the key requestor.
1519b68101a1SKees Cook
1520b68101a1SKees CookSupplementary information may be provided from whoever or whatever invoked this
1521b68101a1SKees Cookservice. This will be passed as the <callout_info> parameter. If no such
1522b68101a1SKees Cookinformation was made available, then "-" will be passed as this parameter
1523b68101a1SKees Cookinstead.
1524b68101a1SKees Cook
1525b68101a1SKees Cook
1526b68101a1SKees CookSimilarly, the kernel may attempt to update an expired or a soon to expire key
1527b68101a1SKees Cookby executing::
1528b68101a1SKees Cook
1529b68101a1SKees Cook	/sbin/request-key update <key> <uid> <gid> \
1530b68101a1SKees Cook		<threadring> <processring> <sessionring>
1531b68101a1SKees Cook
1532b68101a1SKees CookIn this case, the program isn't required to actually attach the key to a ring;
1533b68101a1SKees Cookthe rings are provided for reference.
1534b68101a1SKees Cook
1535b68101a1SKees Cook
1536b68101a1SKees CookGarbage Collection
1537b68101a1SKees Cook==================
1538b68101a1SKees Cook
1539b68101a1SKees CookDead keys (for which the type has been removed) will be automatically unlinked
1540b68101a1SKees Cookfrom those keyrings that point to them and deleted as soon as possible by a
1541b68101a1SKees Cookbackground garbage collector.
1542b68101a1SKees Cook
1543b68101a1SKees CookSimilarly, revoked and expired keys will be garbage collected, but only after a
1544b68101a1SKees Cookcertain amount of time has passed.  This time is set as a number of seconds in::
1545b68101a1SKees Cook
1546b68101a1SKees Cook	/proc/sys/kernel/keys/gc_delay
1547