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 580ed0ac5c7SDavid Howells * Move a key from one keyring to another:: 581ed0ac5c7SDavid Howells 582ed0ac5c7SDavid Howells long keyctl(KEYCTL_MOVE, 583ed0ac5c7SDavid Howells key_serial_t id, 584ed0ac5c7SDavid Howells key_serial_t from_ring_id, 585ed0ac5c7SDavid Howells key_serial_t to_ring_id, 586ed0ac5c7SDavid Howells unsigned int flags); 587ed0ac5c7SDavid Howells 588ed0ac5c7SDavid Howells Move the key specified by "id" from the keyring specified by 589ed0ac5c7SDavid Howells "from_ring_id" to the keyring specified by "to_ring_id". If the two 590ed0ac5c7SDavid Howells keyrings are the same, nothing is done. 591ed0ac5c7SDavid Howells 592ed0ac5c7SDavid Howells "flags" can have KEYCTL_MOVE_EXCL set in it to cause the operation to fail 593ed0ac5c7SDavid Howells with EEXIST if a matching key exists in the destination keyring, otherwise 594ed0ac5c7SDavid Howells such a key will be replaced. 595ed0ac5c7SDavid Howells 596ed0ac5c7SDavid Howells A process must have link permission on the key for this function to be 597ed0ac5c7SDavid Howells successful and write permission on both keyrings. Any errors that can 598ed0ac5c7SDavid Howells occur from KEYCTL_LINK also apply on the destination keyring here. 599ed0ac5c7SDavid Howells 600ed0ac5c7SDavid Howells 601b68101a1SKees Cook * Unlink a key or keyring from another keyring:: 602b68101a1SKees Cook 603b68101a1SKees Cook long keyctl(KEYCTL_UNLINK, key_serial_t keyring, key_serial_t key); 604b68101a1SKees Cook 605b68101a1SKees Cook This function looks through the keyring for the first link to the 606b68101a1SKees Cook specified key, and removes it if found. Subsequent links to that key are 607b68101a1SKees Cook ignored. The process must have write permission on the keyring. 608b68101a1SKees Cook 609b68101a1SKees Cook If the keyring is not a keyring, error ENOTDIR will result; and if the key 610b68101a1SKees Cook is not present, error ENOENT will be the result. 611b68101a1SKees Cook 612b68101a1SKees Cook 613b68101a1SKees Cook * Search a keyring tree for a key:: 614b68101a1SKees Cook 615b68101a1SKees Cook key_serial_t keyctl(KEYCTL_SEARCH, key_serial_t keyring, 616b68101a1SKees Cook const char *type, const char *description, 617b68101a1SKees Cook key_serial_t dest_keyring); 618b68101a1SKees Cook 619b68101a1SKees Cook This searches the keyring tree headed by the specified keyring until a key 620b68101a1SKees Cook is found that matches the type and description criteria. Each keyring is 621b68101a1SKees Cook checked for keys before recursion into its children occurs. 622b68101a1SKees Cook 623b68101a1SKees Cook The process must have search permission on the top level keyring, or else 624b68101a1SKees Cook error EACCES will result. Only keyrings that the process has search 625b68101a1SKees Cook permission on will be recursed into, and only keys and keyrings for which 626b68101a1SKees Cook a process has search permission can be matched. If the specified keyring 627b68101a1SKees Cook is not a keyring, ENOTDIR will result. 628b68101a1SKees Cook 629b68101a1SKees Cook If the search succeeds, the function will attempt to link the found key 630b68101a1SKees Cook into the destination keyring if one is supplied (non-zero ID). All the 631b68101a1SKees Cook constraints applicable to KEYCTL_LINK apply in this case too. 632b68101a1SKees Cook 633b68101a1SKees Cook Error ENOKEY, EKEYREVOKED or EKEYEXPIRED will be returned if the search 634b68101a1SKees Cook fails. On success, the resulting key ID will be returned. 635b68101a1SKees Cook 636b68101a1SKees Cook 637b68101a1SKees Cook * Read the payload data from a key:: 638b68101a1SKees Cook 639b68101a1SKees Cook long keyctl(KEYCTL_READ, key_serial_t keyring, char *buffer, 640b68101a1SKees Cook size_t buflen); 641b68101a1SKees Cook 642b68101a1SKees Cook This function attempts to read the payload data from the specified key 643b68101a1SKees Cook into the buffer. The process must have read permission on the key to 644b68101a1SKees Cook succeed. 645b68101a1SKees Cook 646b68101a1SKees Cook The returned data will be processed for presentation by the key type. For 647b68101a1SKees Cook instance, a keyring will return an array of key_serial_t entries 648b68101a1SKees Cook representing the IDs of all the keys to which it is subscribed. The user 649b68101a1SKees Cook defined key type will return its data as is. If a key type does not 650b68101a1SKees Cook implement this function, error EOPNOTSUPP will result. 651b68101a1SKees Cook 652be543dd6SEric Biggers If the specified buffer is too small, then the size of the buffer required 653be543dd6SEric Biggers will be returned. Note that in this case, the contents of the buffer may 654be543dd6SEric Biggers have been overwritten in some undefined way. 655b68101a1SKees Cook 656be543dd6SEric Biggers Otherwise, on success, the function will return the amount of data copied 657be543dd6SEric Biggers into the buffer. 658b68101a1SKees Cook 659b68101a1SKees Cook * Instantiate a partially constructed key:: 660b68101a1SKees Cook 661b68101a1SKees Cook long keyctl(KEYCTL_INSTANTIATE, key_serial_t key, 662b68101a1SKees Cook const void *payload, size_t plen, 663b68101a1SKees Cook key_serial_t keyring); 664b68101a1SKees Cook long keyctl(KEYCTL_INSTANTIATE_IOV, key_serial_t key, 665b68101a1SKees Cook const struct iovec *payload_iov, unsigned ioc, 666b68101a1SKees Cook key_serial_t keyring); 667b68101a1SKees Cook 668b68101a1SKees Cook If the kernel calls back to userspace to complete the instantiation of a 669b68101a1SKees Cook key, userspace should use this call to supply data for the key before the 670b68101a1SKees Cook invoked process returns, or else the key will be marked negative 671b68101a1SKees Cook automatically. 672b68101a1SKees Cook 673b68101a1SKees Cook The process must have write access on the key to be able to instantiate 674b68101a1SKees Cook it, and the key must be uninstantiated. 675b68101a1SKees Cook 676b68101a1SKees Cook If a keyring is specified (non-zero), the key will also be linked into 677b68101a1SKees Cook that keyring, however all the constraints applying in KEYCTL_LINK apply in 678b68101a1SKees Cook this case too. 679b68101a1SKees Cook 680b68101a1SKees Cook The payload and plen arguments describe the payload data as for add_key(). 681b68101a1SKees Cook 682b68101a1SKees Cook The payload_iov and ioc arguments describe the payload data in an iovec 683b68101a1SKees Cook array instead of a single buffer. 684b68101a1SKees Cook 685b68101a1SKees Cook 686b68101a1SKees Cook * Negatively instantiate a partially constructed key:: 687b68101a1SKees Cook 688b68101a1SKees Cook long keyctl(KEYCTL_NEGATE, key_serial_t key, 689b68101a1SKees Cook unsigned timeout, key_serial_t keyring); 690b68101a1SKees Cook long keyctl(KEYCTL_REJECT, key_serial_t key, 691b68101a1SKees Cook unsigned timeout, unsigned error, key_serial_t keyring); 692b68101a1SKees Cook 693b68101a1SKees Cook If the kernel calls back to userspace to complete the instantiation of a 694b68101a1SKees Cook key, userspace should use this call mark the key as negative before the 695b68101a1SKees Cook invoked process returns if it is unable to fulfill the request. 696b68101a1SKees Cook 697b68101a1SKees Cook The process must have write access on the key to be able to instantiate 698b68101a1SKees Cook it, and the key must be uninstantiated. 699b68101a1SKees Cook 700b68101a1SKees Cook If a keyring is specified (non-zero), the key will also be linked into 701b68101a1SKees Cook that keyring, however all the constraints applying in KEYCTL_LINK apply in 702b68101a1SKees Cook this case too. 703b68101a1SKees Cook 704b68101a1SKees Cook If the key is rejected, future searches for it will return the specified 705b68101a1SKees Cook error code until the rejected key expires. Negating the key is the same 706b68101a1SKees Cook as rejecting the key with ENOKEY as the error code. 707b68101a1SKees Cook 708b68101a1SKees Cook 709b68101a1SKees Cook * Set the default request-key destination keyring:: 710b68101a1SKees Cook 711b68101a1SKees Cook long keyctl(KEYCTL_SET_REQKEY_KEYRING, int reqkey_defl); 712b68101a1SKees Cook 713b68101a1SKees Cook This sets the default keyring to which implicitly requested keys will be 714b68101a1SKees Cook attached for this thread. reqkey_defl should be one of these constants:: 715b68101a1SKees Cook 716b68101a1SKees Cook CONSTANT VALUE NEW DEFAULT KEYRING 717b68101a1SKees Cook ====================================== ====== ======================= 718b68101a1SKees Cook KEY_REQKEY_DEFL_NO_CHANGE -1 No change 719b68101a1SKees Cook KEY_REQKEY_DEFL_DEFAULT 0 Default[1] 720b68101a1SKees Cook KEY_REQKEY_DEFL_THREAD_KEYRING 1 Thread keyring 721b68101a1SKees Cook KEY_REQKEY_DEFL_PROCESS_KEYRING 2 Process keyring 722b68101a1SKees Cook KEY_REQKEY_DEFL_SESSION_KEYRING 3 Session keyring 723b68101a1SKees Cook KEY_REQKEY_DEFL_USER_KEYRING 4 User keyring 724b68101a1SKees Cook KEY_REQKEY_DEFL_USER_SESSION_KEYRING 5 User session keyring 725b68101a1SKees Cook KEY_REQKEY_DEFL_GROUP_KEYRING 6 Group keyring 726b68101a1SKees Cook 727b68101a1SKees Cook The old default will be returned if successful and error EINVAL will be 728b68101a1SKees Cook returned if reqkey_defl is not one of the above values. 729b68101a1SKees Cook 730b68101a1SKees Cook The default keyring can be overridden by the keyring indicated to the 731b68101a1SKees Cook request_key() system call. 732b68101a1SKees Cook 733b68101a1SKees Cook Note that this setting is inherited across fork/exec. 734b68101a1SKees Cook 735b68101a1SKees Cook [1] The default is: the thread keyring if there is one, otherwise 736b68101a1SKees Cook the process keyring if there is one, otherwise the session keyring if 737b68101a1SKees Cook there is one, otherwise the user default session keyring. 738b68101a1SKees Cook 739b68101a1SKees Cook 740b68101a1SKees Cook * Set the timeout on a key:: 741b68101a1SKees Cook 742b68101a1SKees Cook long keyctl(KEYCTL_SET_TIMEOUT, key_serial_t key, unsigned timeout); 743b68101a1SKees Cook 744b68101a1SKees Cook This sets or clears the timeout on a key. The timeout can be 0 to clear 745b68101a1SKees Cook the timeout or a number of seconds to set the expiry time that far into 746b68101a1SKees Cook the future. 747b68101a1SKees Cook 748b68101a1SKees Cook The process must have attribute modification access on a key to set its 749b68101a1SKees Cook timeout. Timeouts may not be set with this function on negative, revoked 750b68101a1SKees Cook or expired keys. 751b68101a1SKees Cook 752b68101a1SKees Cook 753b68101a1SKees Cook * Assume the authority granted to instantiate a key:: 754b68101a1SKees Cook 755b68101a1SKees Cook long keyctl(KEYCTL_ASSUME_AUTHORITY, key_serial_t key); 756b68101a1SKees Cook 757b68101a1SKees Cook This assumes or divests the authority required to instantiate the 758b68101a1SKees Cook specified key. Authority can only be assumed if the thread has the 759b68101a1SKees Cook authorisation key associated with the specified key in its keyrings 760b68101a1SKees Cook somewhere. 761b68101a1SKees Cook 762b68101a1SKees Cook Once authority is assumed, searches for keys will also search the 763b68101a1SKees Cook requester's keyrings using the requester's security label, UID, GID and 764b68101a1SKees Cook groups. 765b68101a1SKees Cook 766b68101a1SKees Cook If the requested authority is unavailable, error EPERM will be returned, 767b68101a1SKees Cook likewise if the authority has been revoked because the target key is 768b68101a1SKees Cook already instantiated. 769b68101a1SKees Cook 770b68101a1SKees Cook If the specified key is 0, then any assumed authority will be divested. 771b68101a1SKees Cook 772b68101a1SKees Cook The assumed authoritative key is inherited across fork and exec. 773b68101a1SKees Cook 774b68101a1SKees Cook 775b68101a1SKees Cook * Get the LSM security context attached to a key:: 776b68101a1SKees Cook 777b68101a1SKees Cook long keyctl(KEYCTL_GET_SECURITY, key_serial_t key, char *buffer, 778b68101a1SKees Cook size_t buflen) 779b68101a1SKees Cook 780b68101a1SKees Cook This function returns a string that represents the LSM security context 781b68101a1SKees Cook attached to a key in the buffer provided. 782b68101a1SKees Cook 783b68101a1SKees Cook Unless there's an error, it always returns the amount of data it could 784b68101a1SKees Cook produce, even if that's too big for the buffer, but it won't copy more 785b68101a1SKees Cook than requested to userspace. If the buffer pointer is NULL then no copy 786b68101a1SKees Cook will take place. 787b68101a1SKees Cook 788b68101a1SKees Cook A NUL character is included at the end of the string if the buffer is 789b68101a1SKees Cook sufficiently big. This is included in the returned count. If no LSM is 790b68101a1SKees Cook in force then an empty string will be returned. 791b68101a1SKees Cook 792b68101a1SKees Cook A process must have view permission on the key for this function to be 793b68101a1SKees Cook successful. 794b68101a1SKees Cook 795b68101a1SKees Cook 796b68101a1SKees Cook * Install the calling process's session keyring on its parent:: 797b68101a1SKees Cook 798b68101a1SKees Cook long keyctl(KEYCTL_SESSION_TO_PARENT); 799b68101a1SKees Cook 800b68101a1SKees Cook This functions attempts to install the calling process's session keyring 801b68101a1SKees Cook on to the calling process's parent, replacing the parent's current session 802b68101a1SKees Cook keyring. 803b68101a1SKees Cook 804b68101a1SKees Cook The calling process must have the same ownership as its parent, the 805b68101a1SKees Cook keyring must have the same ownership as the calling process, the calling 806b68101a1SKees Cook process must have LINK permission on the keyring and the active LSM module 807b68101a1SKees Cook mustn't deny permission, otherwise error EPERM will be returned. 808b68101a1SKees Cook 809b68101a1SKees Cook Error ENOMEM will be returned if there was insufficient memory to complete 810b68101a1SKees Cook the operation, otherwise 0 will be returned to indicate success. 811b68101a1SKees Cook 812b68101a1SKees Cook The keyring will be replaced next time the parent process leaves the 813b68101a1SKees Cook kernel and resumes executing userspace. 814b68101a1SKees Cook 815b68101a1SKees Cook 816b68101a1SKees Cook * Invalidate a key:: 817b68101a1SKees Cook 818b68101a1SKees Cook long keyctl(KEYCTL_INVALIDATE, key_serial_t key); 819b68101a1SKees Cook 820b68101a1SKees Cook This function marks a key as being invalidated and then wakes up the 821b68101a1SKees Cook garbage collector. The garbage collector immediately removes invalidated 822b68101a1SKees Cook keys from all keyrings and deletes the key when its reference count 823b68101a1SKees Cook reaches zero. 824b68101a1SKees Cook 825b68101a1SKees Cook Keys that are marked invalidated become invisible to normal key operations 826b68101a1SKees Cook immediately, though they are still visible in /proc/keys until deleted 827b68101a1SKees Cook (they're marked with an 'i' flag). 828b68101a1SKees Cook 829b68101a1SKees Cook A process must have search permission on the key for this function to be 830b68101a1SKees Cook successful. 831b68101a1SKees Cook 832b68101a1SKees Cook * Compute a Diffie-Hellman shared secret or public key:: 833b68101a1SKees Cook 834b68101a1SKees Cook long keyctl(KEYCTL_DH_COMPUTE, struct keyctl_dh_params *params, 835b68101a1SKees Cook char *buffer, size_t buflen, struct keyctl_kdf_params *kdf); 836b68101a1SKees Cook 837b68101a1SKees Cook The params struct contains serial numbers for three keys:: 838b68101a1SKees Cook 839b68101a1SKees Cook - The prime, p, known to both parties 840b68101a1SKees Cook - The local private key 841b68101a1SKees Cook - The base integer, which is either a shared generator or the 842b68101a1SKees Cook remote public key 843b68101a1SKees Cook 844b68101a1SKees Cook The value computed is:: 845b68101a1SKees Cook 846b68101a1SKees Cook result = base ^ private (mod prime) 847b68101a1SKees Cook 848b68101a1SKees Cook If the base is the shared generator, the result is the local 849b68101a1SKees Cook public key. If the base is the remote public key, the result is 850b68101a1SKees Cook the shared secret. 851b68101a1SKees Cook 852b68101a1SKees Cook If the parameter kdf is NULL, the following applies: 853b68101a1SKees Cook 854b68101a1SKees Cook - The buffer length must be at least the length of the prime, or zero. 855b68101a1SKees Cook 856b68101a1SKees Cook - If the buffer length is nonzero, the length of the result is 857b68101a1SKees Cook returned when it is successfully calculated and copied in to the 858b68101a1SKees Cook buffer. When the buffer length is zero, the minimum required 859b68101a1SKees Cook buffer length is returned. 860b68101a1SKees Cook 861b68101a1SKees Cook The kdf parameter allows the caller to apply a key derivation function 862b68101a1SKees Cook (KDF) on the Diffie-Hellman computation where only the result 863b68101a1SKees Cook of the KDF is returned to the caller. The KDF is characterized with 864b68101a1SKees Cook struct keyctl_kdf_params as follows: 865b68101a1SKees Cook 866b68101a1SKees Cook - ``char *hashname`` specifies the NUL terminated string identifying 867b68101a1SKees Cook the hash used from the kernel crypto API and applied for the KDF 868b68101a1SKees Cook operation. The KDF implemenation complies with SP800-56A as well 869b68101a1SKees Cook as with SP800-108 (the counter KDF). 870b68101a1SKees Cook 871b68101a1SKees Cook - ``char *otherinfo`` specifies the OtherInfo data as documented in 872b68101a1SKees Cook SP800-56A section 5.8.1.2. The length of the buffer is given with 873b68101a1SKees Cook otherinfolen. The format of OtherInfo is defined by the caller. 874b68101a1SKees Cook The otherinfo pointer may be NULL if no OtherInfo shall be used. 875b68101a1SKees Cook 876b68101a1SKees Cook This function will return error EOPNOTSUPP if the key type is not 877b68101a1SKees Cook supported, error ENOKEY if the key could not be found, or error 878b68101a1SKees Cook EACCES if the key is not readable by the caller. In addition, the 879b68101a1SKees Cook function will return EMSGSIZE when the parameter kdf is non-NULL 880b68101a1SKees Cook and either the buffer length or the OtherInfo length exceeds the 881b68101a1SKees Cook allowed length. 882b68101a1SKees Cook 88300d60fd3SDavid Howells 884b68101a1SKees Cook * Restrict keyring linkage:: 885b68101a1SKees Cook 886b68101a1SKees Cook long keyctl(KEYCTL_RESTRICT_KEYRING, key_serial_t keyring, 887b68101a1SKees Cook const char *type, const char *restriction); 888b68101a1SKees Cook 889b68101a1SKees Cook An existing keyring can restrict linkage of additional keys by evaluating 890b68101a1SKees Cook the contents of the key according to a restriction scheme. 891b68101a1SKees Cook 892b68101a1SKees Cook "keyring" is the key ID for an existing keyring to apply a restriction 893b68101a1SKees Cook to. It may be empty or may already have keys linked. Existing linked keys 894b68101a1SKees Cook will remain in the keyring even if the new restriction would reject them. 895b68101a1SKees Cook 896b68101a1SKees Cook "type" is a registered key type. 897b68101a1SKees Cook 898b68101a1SKees Cook "restriction" is a string describing how key linkage is to be restricted. 899b68101a1SKees Cook The format varies depending on the key type, and the string is passed to 900b68101a1SKees Cook the lookup_restriction() function for the requested type. It may specify 901b68101a1SKees Cook a method and relevant data for the restriction such as signature 902b68101a1SKees Cook verification or constraints on key payload. If the requested key type is 903b68101a1SKees Cook later unregistered, no keys may be added to the keyring after the key type 904b68101a1SKees Cook is removed. 905b68101a1SKees Cook 906b68101a1SKees Cook To apply a keyring restriction the process must have Set Attribute 907b68101a1SKees Cook permission and the keyring must not be previously restricted. 908b68101a1SKees Cook 9097228b66aSMat Martineau One application of restricted keyrings is to verify X.509 certificate 9107228b66aSMat Martineau chains or individual certificate signatures using the asymmetric key type. 9117228b66aSMat Martineau See Documentation/crypto/asymmetric-keys.txt for specific restrictions 9127228b66aSMat Martineau applicable to the asymmetric key type. 9137228b66aSMat Martineau 9147228b66aSMat Martineau 91500d60fd3SDavid Howells * Query an asymmetric key:: 91600d60fd3SDavid Howells 91700d60fd3SDavid Howells long keyctl(KEYCTL_PKEY_QUERY, 91800d60fd3SDavid Howells key_serial_t key_id, unsigned long reserved, 91900d60fd3SDavid Howells struct keyctl_pkey_query *info); 92000d60fd3SDavid Howells 92100d60fd3SDavid Howells Get information about an asymmetric key. The information is returned in 92200d60fd3SDavid Howells the keyctl_pkey_query struct:: 92300d60fd3SDavid Howells 92400d60fd3SDavid Howells __u32 supported_ops; 92500d60fd3SDavid Howells __u32 key_size; 92600d60fd3SDavid Howells __u16 max_data_size; 92700d60fd3SDavid Howells __u16 max_sig_size; 92800d60fd3SDavid Howells __u16 max_enc_size; 92900d60fd3SDavid Howells __u16 max_dec_size; 93000d60fd3SDavid Howells __u32 __spare[10]; 93100d60fd3SDavid Howells 93200d60fd3SDavid Howells ``supported_ops`` contains a bit mask of flags indicating which ops are 93300d60fd3SDavid Howells supported. This is constructed from a bitwise-OR of:: 93400d60fd3SDavid Howells 93500d60fd3SDavid Howells KEYCTL_SUPPORTS_{ENCRYPT,DECRYPT,SIGN,VERIFY} 93600d60fd3SDavid Howells 93700d60fd3SDavid Howells ``key_size`` indicated the size of the key in bits. 93800d60fd3SDavid Howells 93900d60fd3SDavid Howells ``max_*_size`` indicate the maximum sizes in bytes of a blob of data to be 94000d60fd3SDavid Howells signed, a signature blob, a blob to be encrypted and a blob to be 94100d60fd3SDavid Howells decrypted. 94200d60fd3SDavid Howells 94300d60fd3SDavid Howells ``__spare[]`` must be set to 0. This is intended for future use to hand 94400d60fd3SDavid Howells over one or more passphrases needed unlock a key. 94500d60fd3SDavid Howells 94600d60fd3SDavid Howells If successful, 0 is returned. If the key is not an asymmetric key, 94700d60fd3SDavid Howells EOPNOTSUPP is returned. 94800d60fd3SDavid Howells 94900d60fd3SDavid Howells 95000d60fd3SDavid Howells * Encrypt, decrypt, sign or verify a blob using an asymmetric key:: 95100d60fd3SDavid Howells 95200d60fd3SDavid Howells long keyctl(KEYCTL_PKEY_ENCRYPT, 95300d60fd3SDavid Howells const struct keyctl_pkey_params *params, 95400d60fd3SDavid Howells const char *info, 95500d60fd3SDavid Howells const void *in, 95600d60fd3SDavid Howells void *out); 95700d60fd3SDavid Howells 95800d60fd3SDavid Howells long keyctl(KEYCTL_PKEY_DECRYPT, 95900d60fd3SDavid Howells const struct keyctl_pkey_params *params, 96000d60fd3SDavid Howells const char *info, 96100d60fd3SDavid Howells const void *in, 96200d60fd3SDavid Howells void *out); 96300d60fd3SDavid Howells 96400d60fd3SDavid Howells long keyctl(KEYCTL_PKEY_SIGN, 96500d60fd3SDavid Howells const struct keyctl_pkey_params *params, 96600d60fd3SDavid Howells const char *info, 96700d60fd3SDavid Howells const void *in, 96800d60fd3SDavid Howells void *out); 96900d60fd3SDavid Howells 97000d60fd3SDavid Howells long keyctl(KEYCTL_PKEY_VERIFY, 97100d60fd3SDavid Howells const struct keyctl_pkey_params *params, 97200d60fd3SDavid Howells const char *info, 97300d60fd3SDavid Howells const void *in, 97400d60fd3SDavid Howells const void *in2); 97500d60fd3SDavid Howells 97600d60fd3SDavid Howells Use an asymmetric key to perform a public-key cryptographic operation a 97700d60fd3SDavid Howells blob of data. For encryption and verification, the asymmetric key may 97800d60fd3SDavid Howells only need the public parts to be available, but for decryption and signing 97900d60fd3SDavid Howells the private parts are required also. 98000d60fd3SDavid Howells 98100d60fd3SDavid Howells The parameter block pointed to by params contains a number of integer 98200d60fd3SDavid Howells values:: 98300d60fd3SDavid Howells 98400d60fd3SDavid Howells __s32 key_id; 98500d60fd3SDavid Howells __u32 in_len; 98600d60fd3SDavid Howells __u32 out_len; 98700d60fd3SDavid Howells __u32 in2_len; 98800d60fd3SDavid Howells 98900d60fd3SDavid Howells ``key_id`` is the ID of the asymmetric key to be used. ``in_len`` and 99000d60fd3SDavid Howells ``in2_len`` indicate the amount of data in the in and in2 buffers and 99100d60fd3SDavid Howells ``out_len`` indicates the size of the out buffer as appropriate for the 99200d60fd3SDavid Howells above operations. 99300d60fd3SDavid Howells 99400d60fd3SDavid Howells For a given operation, the in and out buffers are used as follows:: 99500d60fd3SDavid Howells 99600d60fd3SDavid Howells Operation ID in,in_len out,out_len in2,in2_len 99700d60fd3SDavid Howells ======================= =============== =============== =============== 99800d60fd3SDavid Howells KEYCTL_PKEY_ENCRYPT Raw data Encrypted data - 99900d60fd3SDavid Howells KEYCTL_PKEY_DECRYPT Encrypted data Raw data - 100000d60fd3SDavid Howells KEYCTL_PKEY_SIGN Raw data Signature - 100100d60fd3SDavid Howells KEYCTL_PKEY_VERIFY Raw data - Signature 100200d60fd3SDavid Howells 100300d60fd3SDavid Howells ``info`` is a string of key=value pairs that supply supplementary 100400d60fd3SDavid Howells information. These include: 100500d60fd3SDavid Howells 100600d60fd3SDavid Howells ``enc=<encoding>`` The encoding of the encrypted/signature blob. This 100700d60fd3SDavid Howells can be "pkcs1" for RSASSA-PKCS1-v1.5 or 100800d60fd3SDavid Howells RSAES-PKCS1-v1.5; "pss" for "RSASSA-PSS"; "oaep" for 100900d60fd3SDavid Howells "RSAES-OAEP". If omitted or is "raw", the raw output 101000d60fd3SDavid Howells of the encryption function is specified. 101100d60fd3SDavid Howells 101200d60fd3SDavid Howells ``hash=<algo>`` If the data buffer contains the output of a hash 101300d60fd3SDavid Howells function and the encoding includes some indication of 101400d60fd3SDavid Howells which hash function was used, the hash function can be 101500d60fd3SDavid Howells specified with this, eg. "hash=sha256". 101600d60fd3SDavid Howells 101700d60fd3SDavid Howells The ``__spare[]`` space in the parameter block must be set to 0. This is 101800d60fd3SDavid Howells intended, amongst other things, to allow the passing of passphrases 101900d60fd3SDavid Howells required to unlock a key. 102000d60fd3SDavid Howells 102100d60fd3SDavid Howells If successful, encrypt, decrypt and sign all return the amount of data 102200d60fd3SDavid Howells written into the output buffer. Verification returns 0 on success. 102300d60fd3SDavid Howells 102400d60fd3SDavid Howells 1025b68101a1SKees CookKernel Services 1026b68101a1SKees Cook=============== 1027b68101a1SKees Cook 1028b68101a1SKees CookThe kernel services for key management are fairly simple to deal with. They can 1029b68101a1SKees Cookbe broken down into two areas: keys and key types. 1030b68101a1SKees Cook 1031b68101a1SKees CookDealing with keys is fairly straightforward. Firstly, the kernel service 1032b68101a1SKees Cookregisters its type, then it searches for a key of that type. It should retain 1033b68101a1SKees Cookthe key as long as it has need of it, and then it should release it. For a 1034b68101a1SKees Cookfilesystem or device file, a search would probably be performed during the open 1035b68101a1SKees Cookcall, and the key released upon close. How to deal with conflicting keys due to 1036b68101a1SKees Cooktwo different users opening the same file is left to the filesystem author to 1037b68101a1SKees Cooksolve. 1038b68101a1SKees Cook 1039b68101a1SKees CookTo access the key manager, the following header must be #included:: 1040b68101a1SKees Cook 1041b68101a1SKees Cook <linux/key.h> 1042b68101a1SKees Cook 1043b68101a1SKees CookSpecific key types should have a header file under include/keys/ that should be 1044b68101a1SKees Cookused to access that type. For keys of type "user", for example, that would be:: 1045b68101a1SKees Cook 1046b68101a1SKees Cook <keys/user-type.h> 1047b68101a1SKees Cook 1048b68101a1SKees CookNote that there are two different types of pointers to keys that may be 1049b68101a1SKees Cookencountered: 1050b68101a1SKees Cook 1051b68101a1SKees Cook * struct key * 1052b68101a1SKees Cook 1053b68101a1SKees Cook This simply points to the key structure itself. Key structures will be at 1054b68101a1SKees Cook least four-byte aligned. 1055b68101a1SKees Cook 1056b68101a1SKees Cook * key_ref_t 1057b68101a1SKees Cook 1058b68101a1SKees Cook This is equivalent to a ``struct key *``, but the least significant bit is set 1059b68101a1SKees Cook if the caller "possesses" the key. By "possession" it is meant that the 1060b68101a1SKees Cook calling processes has a searchable link to the key from one of its 1061b68101a1SKees Cook keyrings. There are three functions for dealing with these:: 1062b68101a1SKees Cook 1063b68101a1SKees Cook key_ref_t make_key_ref(const struct key *key, bool possession); 1064b68101a1SKees Cook 1065b68101a1SKees Cook struct key *key_ref_to_ptr(const key_ref_t key_ref); 1066b68101a1SKees Cook 1067b68101a1SKees Cook bool is_key_possessed(const key_ref_t key_ref); 1068b68101a1SKees Cook 1069b68101a1SKees Cook The first function constructs a key reference from a key pointer and 1070b68101a1SKees Cook possession information (which must be true or false). 1071b68101a1SKees Cook 1072b68101a1SKees Cook The second function retrieves the key pointer from a reference and the 1073b68101a1SKees Cook third retrieves the possession flag. 1074b68101a1SKees Cook 1075b68101a1SKees CookWhen accessing a key's payload contents, certain precautions must be taken to 1076b68101a1SKees Cookprevent access vs modification races. See the section "Notes on accessing 1077b68101a1SKees Cookpayload contents" for more information. 1078b68101a1SKees Cook 1079b68101a1SKees Cook * To search for a key, call:: 1080b68101a1SKees Cook 1081b68101a1SKees Cook struct key *request_key(const struct key_type *type, 1082b68101a1SKees Cook const char *description, 1083b68101a1SKees Cook const char *callout_info); 1084b68101a1SKees Cook 1085b68101a1SKees Cook This is used to request a key or keyring with a description that matches 1086b68101a1SKees Cook the description specified according to the key type's match_preparse() 1087b68101a1SKees Cook method. This permits approximate matching to occur. If callout_string is 1088b68101a1SKees Cook not NULL, then /sbin/request-key will be invoked in an attempt to obtain 1089b68101a1SKees Cook the key from userspace. In that case, callout_string will be passed as an 1090b68101a1SKees Cook argument to the program. 1091b68101a1SKees Cook 1092b68101a1SKees Cook Should the function fail error ENOKEY, EKEYEXPIRED or EKEYREVOKED will be 1093b68101a1SKees Cook returned. 1094b68101a1SKees Cook 1095b68101a1SKees Cook If successful, the key will have been attached to the default keyring for 1096b68101a1SKees Cook implicitly obtained request-key keys, as set by KEYCTL_SET_REQKEY_KEYRING. 1097b68101a1SKees Cook 1098adf31eebSJosh Holland See also Documentation/security/keys/request-key.rst. 1099b68101a1SKees Cook 1100b68101a1SKees Cook 1101b68101a1SKees Cook * To search for a key, passing auxiliary data to the upcaller, call:: 1102b68101a1SKees Cook 1103b68101a1SKees Cook struct key *request_key_with_auxdata(const struct key_type *type, 1104b68101a1SKees Cook const char *description, 1105b68101a1SKees Cook const void *callout_info, 1106b68101a1SKees Cook size_t callout_len, 1107b68101a1SKees Cook void *aux); 1108b68101a1SKees Cook 1109b68101a1SKees Cook This is identical to request_key(), except that the auxiliary data is 1110b68101a1SKees Cook passed to the key_type->request_key() op if it exists, and the callout_info 1111b68101a1SKees Cook is a blob of length callout_len, if given (the length may be 0). 1112b68101a1SKees Cook 1113b68101a1SKees Cook 1114b68101a1SKees Cook * A key can be requested asynchronously by calling one of:: 1115b68101a1SKees Cook 1116b68101a1SKees Cook struct key *request_key_async(const struct key_type *type, 1117b68101a1SKees Cook const char *description, 1118b68101a1SKees Cook const void *callout_info, 1119b68101a1SKees Cook size_t callout_len); 1120b68101a1SKees Cook 1121b68101a1SKees Cook or:: 1122b68101a1SKees Cook 1123b68101a1SKees Cook struct key *request_key_async_with_auxdata(const struct key_type *type, 1124b68101a1SKees Cook const char *description, 1125b68101a1SKees Cook const char *callout_info, 1126b68101a1SKees Cook size_t callout_len, 1127b68101a1SKees Cook void *aux); 1128b68101a1SKees Cook 1129b68101a1SKees Cook which are asynchronous equivalents of request_key() and 1130b68101a1SKees Cook request_key_with_auxdata() respectively. 1131b68101a1SKees Cook 1132b68101a1SKees Cook These two functions return with the key potentially still under 1133b68101a1SKees Cook construction. To wait for construction completion, the following should be 1134b68101a1SKees Cook called:: 1135b68101a1SKees Cook 1136b68101a1SKees Cook int wait_for_key_construction(struct key *key, bool intr); 1137b68101a1SKees Cook 1138b68101a1SKees Cook The function will wait for the key to finish being constructed and then 1139b68101a1SKees Cook invokes key_validate() to return an appropriate value to indicate the state 1140b68101a1SKees Cook of the key (0 indicates the key is usable). 1141b68101a1SKees Cook 1142b68101a1SKees Cook If intr is true, then the wait can be interrupted by a signal, in which 1143b68101a1SKees Cook case error ERESTARTSYS will be returned. 1144b68101a1SKees Cook 1145b68101a1SKees Cook 1146b68101a1SKees Cook * When it is no longer required, the key should be released using:: 1147b68101a1SKees Cook 1148b68101a1SKees Cook void key_put(struct key *key); 1149b68101a1SKees Cook 1150b68101a1SKees Cook Or:: 1151b68101a1SKees Cook 1152b68101a1SKees Cook void key_ref_put(key_ref_t key_ref); 1153b68101a1SKees Cook 1154b68101a1SKees Cook These can be called from interrupt context. If CONFIG_KEYS is not set then 1155b68101a1SKees Cook the argument will not be parsed. 1156b68101a1SKees Cook 1157b68101a1SKees Cook 1158b68101a1SKees Cook * Extra references can be made to a key by calling one of the following 1159b68101a1SKees Cook functions:: 1160b68101a1SKees Cook 1161b68101a1SKees Cook struct key *__key_get(struct key *key); 1162b68101a1SKees Cook struct key *key_get(struct key *key); 1163b68101a1SKees Cook 1164b68101a1SKees Cook Keys so references will need to be disposed of by calling key_put() when 1165b68101a1SKees Cook they've been finished with. The key pointer passed in will be returned. 1166b68101a1SKees Cook 1167b68101a1SKees Cook In the case of key_get(), if the pointer is NULL or CONFIG_KEYS is not set 1168b68101a1SKees Cook then the key will not be dereferenced and no increment will take place. 1169b68101a1SKees Cook 1170b68101a1SKees Cook 1171b68101a1SKees Cook * A key's serial number can be obtained by calling:: 1172b68101a1SKees Cook 1173b68101a1SKees Cook key_serial_t key_serial(struct key *key); 1174b68101a1SKees Cook 1175b68101a1SKees Cook If key is NULL or if CONFIG_KEYS is not set then 0 will be returned (in the 1176b68101a1SKees Cook latter case without parsing the argument). 1177b68101a1SKees Cook 1178b68101a1SKees Cook 1179b68101a1SKees Cook * If a keyring was found in the search, this can be further searched by:: 1180b68101a1SKees Cook 1181b68101a1SKees Cook key_ref_t keyring_search(key_ref_t keyring_ref, 1182b68101a1SKees Cook const struct key_type *type, 1183b68101a1SKees Cook const char *description) 1184b68101a1SKees Cook 1185b68101a1SKees Cook This searches the keyring tree specified for a matching key. Error ENOKEY 1186b68101a1SKees Cook is returned upon failure (use IS_ERR/PTR_ERR to determine). If successful, 1187b68101a1SKees Cook the returned key will need to be released. 1188b68101a1SKees Cook 1189b68101a1SKees Cook The possession attribute from the keyring reference is used to control 1190b68101a1SKees Cook access through the permissions mask and is propagated to the returned key 1191b68101a1SKees Cook reference pointer if successful. 1192b68101a1SKees Cook 1193b68101a1SKees Cook 1194b68101a1SKees Cook * A keyring can be created by:: 1195b68101a1SKees Cook 1196b68101a1SKees Cook struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid, 1197b68101a1SKees Cook const struct cred *cred, 1198b68101a1SKees Cook key_perm_t perm, 1199b68101a1SKees Cook struct key_restriction *restrict_link, 1200b68101a1SKees Cook unsigned long flags, 1201b68101a1SKees Cook struct key *dest); 1202b68101a1SKees Cook 1203b68101a1SKees Cook This creates a keyring with the given attributes and returns it. If dest 1204b68101a1SKees Cook is not NULL, the new keyring will be linked into the keyring to which it 1205b68101a1SKees Cook points. No permission checks are made upon the destination keyring. 1206b68101a1SKees Cook 1207b68101a1SKees Cook Error EDQUOT can be returned if the keyring would overload the quota (pass 1208b68101a1SKees Cook KEY_ALLOC_NOT_IN_QUOTA in flags if the keyring shouldn't be accounted 1209b68101a1SKees Cook towards the user's quota). Error ENOMEM can also be returned. 1210b68101a1SKees Cook 1211b68101a1SKees Cook If restrict_link is not NULL, it should point to a structure that contains 1212b68101a1SKees Cook the function that will be called each time an attempt is made to link a 1213b68101a1SKees Cook key into the new keyring. The structure may also contain a key pointer 1214b68101a1SKees Cook and an associated key type. The function is called to check whether a key 1215b68101a1SKees Cook may be added into the keyring or not. The key type is used by the garbage 1216b68101a1SKees Cook collector to clean up function or data pointers in this structure if the 1217b68101a1SKees Cook given key type is unregistered. Callers of key_create_or_update() within 1218b68101a1SKees Cook the kernel can pass KEY_ALLOC_BYPASS_RESTRICTION to suppress the check. 1219b68101a1SKees Cook An example of using this is to manage rings of cryptographic keys that are 1220b68101a1SKees Cook set up when the kernel boots where userspace is also permitted to add keys 1221b68101a1SKees Cook - provided they can be verified by a key the kernel already has. 1222b68101a1SKees Cook 1223b68101a1SKees Cook When called, the restriction function will be passed the keyring being 1224b68101a1SKees Cook added to, the key type, the payload of the key being added, and data to be 1225b68101a1SKees Cook used in the restriction check. Note that when a new key is being created, 1226b68101a1SKees Cook this is called between payload preparsing and actual key creation. The 1227b68101a1SKees Cook function should return 0 to allow the link or an error to reject it. 1228b68101a1SKees Cook 1229b68101a1SKees Cook A convenience function, restrict_link_reject, exists to always return 1230b68101a1SKees Cook -EPERM to in this case. 1231b68101a1SKees Cook 1232b68101a1SKees Cook 1233b68101a1SKees Cook * To check the validity of a key, this function can be called:: 1234b68101a1SKees Cook 1235b68101a1SKees Cook int validate_key(struct key *key); 1236b68101a1SKees Cook 1237b68101a1SKees Cook This checks that the key in question hasn't expired or and hasn't been 1238b68101a1SKees Cook revoked. Should the key be invalid, error EKEYEXPIRED or EKEYREVOKED will 1239b68101a1SKees Cook be returned. If the key is NULL or if CONFIG_KEYS is not set then 0 will be 1240b68101a1SKees Cook returned (in the latter case without parsing the argument). 1241b68101a1SKees Cook 1242b68101a1SKees Cook 1243b68101a1SKees Cook * To register a key type, the following function should be called:: 1244b68101a1SKees Cook 1245b68101a1SKees Cook int register_key_type(struct key_type *type); 1246b68101a1SKees Cook 1247b68101a1SKees Cook This will return error EEXIST if a type of the same name is already 1248b68101a1SKees Cook present. 1249b68101a1SKees Cook 1250b68101a1SKees Cook 1251b68101a1SKees Cook * To unregister a key type, call:: 1252b68101a1SKees Cook 1253b68101a1SKees Cook void unregister_key_type(struct key_type *type); 1254b68101a1SKees Cook 1255b68101a1SKees Cook 1256b68101a1SKees CookUnder some circumstances, it may be desirable to deal with a bundle of keys. 1257b68101a1SKees CookThe facility provides access to the keyring type for managing such a bundle:: 1258b68101a1SKees Cook 1259b68101a1SKees Cook struct key_type key_type_keyring; 1260b68101a1SKees Cook 1261b68101a1SKees CookThis can be used with a function such as request_key() to find a specific 1262b68101a1SKees Cookkeyring in a process's keyrings. A keyring thus found can then be searched 1263b68101a1SKees Cookwith keyring_search(). Note that it is not possible to use request_key() to 1264b68101a1SKees Cooksearch a specific keyring, so using keyrings in this way is of limited utility. 1265b68101a1SKees Cook 1266b68101a1SKees Cook 1267b68101a1SKees CookNotes On Accessing Payload Contents 1268b68101a1SKees Cook=================================== 1269b68101a1SKees Cook 1270b68101a1SKees CookThe simplest payload is just data stored in key->payload directly. In this 1271b68101a1SKees Cookcase, there's no need to indulge in RCU or locking when accessing the payload. 1272b68101a1SKees Cook 1273b68101a1SKees CookMore complex payload contents must be allocated and pointers to them set in the 1274b68101a1SKees Cookkey->payload.data[] array. One of the following ways must be selected to 1275b68101a1SKees Cookaccess the data: 1276b68101a1SKees Cook 1277b68101a1SKees Cook 1) Unmodifiable key type. 1278b68101a1SKees Cook 1279b68101a1SKees Cook If the key type does not have a modify method, then the key's payload can 1280b68101a1SKees Cook be accessed without any form of locking, provided that it's known to be 1281b68101a1SKees Cook instantiated (uninstantiated keys cannot be "found"). 1282b68101a1SKees Cook 1283b68101a1SKees Cook 2) The key's semaphore. 1284b68101a1SKees Cook 1285b68101a1SKees Cook The semaphore could be used to govern access to the payload and to control 1286b68101a1SKees Cook the payload pointer. It must be write-locked for modifications and would 1287b68101a1SKees Cook have to be read-locked for general access. The disadvantage of doing this 1288b68101a1SKees Cook is that the accessor may be required to sleep. 1289b68101a1SKees Cook 1290b68101a1SKees Cook 3) RCU. 1291b68101a1SKees Cook 1292b68101a1SKees Cook RCU must be used when the semaphore isn't already held; if the semaphore 1293b68101a1SKees Cook is held then the contents can't change under you unexpectedly as the 1294b68101a1SKees Cook semaphore must still be used to serialise modifications to the key. The 1295b68101a1SKees Cook key management code takes care of this for the key type. 1296b68101a1SKees Cook 1297b68101a1SKees Cook However, this means using:: 1298b68101a1SKees Cook 1299b68101a1SKees Cook rcu_read_lock() ... rcu_dereference() ... rcu_read_unlock() 1300b68101a1SKees Cook 1301b68101a1SKees Cook to read the pointer, and:: 1302b68101a1SKees Cook 1303b68101a1SKees Cook rcu_dereference() ... rcu_assign_pointer() ... call_rcu() 1304b68101a1SKees Cook 1305b68101a1SKees Cook to set the pointer and dispose of the old contents after a grace period. 1306b68101a1SKees Cook Note that only the key type should ever modify a key's payload. 1307b68101a1SKees Cook 1308b68101a1SKees Cook Furthermore, an RCU controlled payload must hold a struct rcu_head for the 1309b68101a1SKees Cook use of call_rcu() and, if the payload is of variable size, the length of 1310b68101a1SKees Cook the payload. key->datalen cannot be relied upon to be consistent with the 1311b68101a1SKees Cook payload just dereferenced if the key's semaphore is not held. 1312b68101a1SKees Cook 1313b68101a1SKees Cook Note that key->payload.data[0] has a shadow that is marked for __rcu 1314b68101a1SKees Cook usage. This is called key->payload.rcu_data0. The following accessors 1315b68101a1SKees Cook wrap the RCU calls to this element: 1316b68101a1SKees Cook 1317b68101a1SKees Cook a) Set or change the first payload pointer:: 1318b68101a1SKees Cook 1319b68101a1SKees Cook rcu_assign_keypointer(struct key *key, void *data); 1320b68101a1SKees Cook 1321b68101a1SKees Cook b) Read the first payload pointer with the key semaphore held:: 1322b68101a1SKees Cook 1323b68101a1SKees Cook [const] void *dereference_key_locked([const] struct key *key); 1324b68101a1SKees Cook 1325b68101a1SKees Cook Note that the return value will inherit its constness from the key 1326b68101a1SKees Cook parameter. Static analysis will give an error if it things the lock 1327b68101a1SKees Cook isn't held. 1328b68101a1SKees Cook 1329b68101a1SKees Cook c) Read the first payload pointer with the RCU read lock held:: 1330b68101a1SKees Cook 1331b68101a1SKees Cook const void *dereference_key_rcu(const struct key *key); 1332b68101a1SKees Cook 1333b68101a1SKees Cook 1334b68101a1SKees CookDefining a Key Type 1335b68101a1SKees Cook=================== 1336b68101a1SKees Cook 1337b68101a1SKees CookA kernel service may want to define its own key type. For instance, an AFS 1338b68101a1SKees Cookfilesystem might want to define a Kerberos 5 ticket key type. To do this, it 1339b68101a1SKees Cookauthor fills in a key_type struct and registers it with the system. 1340b68101a1SKees Cook 1341b68101a1SKees CookSource files that implement key types should include the following header file:: 1342b68101a1SKees Cook 1343b68101a1SKees Cook <linux/key-type.h> 1344b68101a1SKees Cook 1345b68101a1SKees CookThe structure has a number of fields, some of which are mandatory: 1346b68101a1SKees Cook 1347b68101a1SKees Cook * ``const char *name`` 1348b68101a1SKees Cook 1349b68101a1SKees Cook The name of the key type. This is used to translate a key type name 1350b68101a1SKees Cook supplied by userspace into a pointer to the structure. 1351b68101a1SKees Cook 1352b68101a1SKees Cook 1353b68101a1SKees Cook * ``size_t def_datalen`` 1354b68101a1SKees Cook 1355b68101a1SKees Cook This is optional - it supplies the default payload data length as 1356b68101a1SKees Cook contributed to the quota. If the key type's payload is always or almost 1357b68101a1SKees Cook always the same size, then this is a more efficient way to do things. 1358b68101a1SKees Cook 1359b68101a1SKees Cook The data length (and quota) on a particular key can always be changed 1360b68101a1SKees Cook during instantiation or update by calling:: 1361b68101a1SKees Cook 1362b68101a1SKees Cook int key_payload_reserve(struct key *key, size_t datalen); 1363b68101a1SKees Cook 1364b68101a1SKees Cook With the revised data length. Error EDQUOT will be returned if this is not 1365b68101a1SKees Cook viable. 1366b68101a1SKees Cook 1367b68101a1SKees Cook 1368b68101a1SKees Cook * ``int (*vet_description)(const char *description);`` 1369b68101a1SKees Cook 1370b68101a1SKees Cook This optional method is called to vet a key description. If the key type 1371b68101a1SKees Cook doesn't approve of the key description, it may return an error, otherwise 1372b68101a1SKees Cook it should return 0. 1373b68101a1SKees Cook 1374b68101a1SKees Cook 1375b68101a1SKees Cook * ``int (*preparse)(struct key_preparsed_payload *prep);`` 1376b68101a1SKees Cook 1377b68101a1SKees Cook This optional method permits the key type to attempt to parse payload 1378b68101a1SKees Cook before a key is created (add key) or the key semaphore is taken (update or 1379b68101a1SKees Cook instantiate key). The structure pointed to by prep looks like:: 1380b68101a1SKees Cook 1381b68101a1SKees Cook struct key_preparsed_payload { 1382b68101a1SKees Cook char *description; 1383b68101a1SKees Cook union key_payload payload; 1384b68101a1SKees Cook const void *data; 1385b68101a1SKees Cook size_t datalen; 1386b68101a1SKees Cook size_t quotalen; 1387b68101a1SKees Cook time_t expiry; 1388b68101a1SKees Cook }; 1389b68101a1SKees Cook 1390b68101a1SKees Cook Before calling the method, the caller will fill in data and datalen with 1391b68101a1SKees Cook the payload blob parameters; quotalen will be filled in with the default 1392b68101a1SKees Cook quota size from the key type; expiry will be set to TIME_T_MAX and the 1393b68101a1SKees Cook rest will be cleared. 1394b68101a1SKees Cook 1395b68101a1SKees Cook If a description can be proposed from the payload contents, that should be 1396b68101a1SKees Cook attached as a string to the description field. This will be used for the 1397b68101a1SKees Cook key description if the caller of add_key() passes NULL or "". 1398b68101a1SKees Cook 1399b68101a1SKees Cook The method can attach anything it likes to payload. This is merely passed 1400b68101a1SKees Cook along to the instantiate() or update() operations. If set, the expiry 1401b68101a1SKees Cook time will be applied to the key if it is instantiated from this data. 1402b68101a1SKees Cook 1403b68101a1SKees Cook The method should return 0 if successful or a negative error code 1404b68101a1SKees Cook otherwise. 1405b68101a1SKees Cook 1406b68101a1SKees Cook 1407b68101a1SKees Cook * ``void (*free_preparse)(struct key_preparsed_payload *prep);`` 1408b68101a1SKees Cook 1409b68101a1SKees Cook This method is only required if the preparse() method is provided, 1410b68101a1SKees Cook otherwise it is unused. It cleans up anything attached to the description 1411b68101a1SKees Cook and payload fields of the key_preparsed_payload struct as filled in by the 1412b68101a1SKees Cook preparse() method. It will always be called after preparse() returns 1413b68101a1SKees Cook successfully, even if instantiate() or update() succeed. 1414b68101a1SKees Cook 1415b68101a1SKees Cook 1416b68101a1SKees Cook * ``int (*instantiate)(struct key *key, struct key_preparsed_payload *prep);`` 1417b68101a1SKees Cook 1418b68101a1SKees Cook This method is called to attach a payload to a key during construction. 1419b68101a1SKees Cook The payload attached need not bear any relation to the data passed to this 1420b68101a1SKees Cook function. 1421b68101a1SKees Cook 1422b68101a1SKees Cook The prep->data and prep->datalen fields will define the original payload 1423b68101a1SKees Cook blob. If preparse() was supplied then other fields may be filled in also. 1424b68101a1SKees Cook 1425b68101a1SKees Cook If the amount of data attached to the key differs from the size in 1426b68101a1SKees Cook keytype->def_datalen, then key_payload_reserve() should be called. 1427b68101a1SKees Cook 1428b68101a1SKees Cook This method does not have to lock the key in order to attach a payload. 1429b68101a1SKees Cook The fact that KEY_FLAG_INSTANTIATED is not set in key->flags prevents 1430b68101a1SKees Cook anything else from gaining access to the key. 1431b68101a1SKees Cook 1432b68101a1SKees Cook It is safe to sleep in this method. 1433b68101a1SKees Cook 1434b68101a1SKees Cook generic_key_instantiate() is provided to simply copy the data from 1435b68101a1SKees Cook prep->payload.data[] to key->payload.data[], with RCU-safe assignment on 1436b68101a1SKees Cook the first element. It will then clear prep->payload.data[] so that the 1437b68101a1SKees Cook free_preparse method doesn't release the data. 1438b68101a1SKees Cook 1439b68101a1SKees Cook 1440b68101a1SKees Cook * ``int (*update)(struct key *key, const void *data, size_t datalen);`` 1441b68101a1SKees Cook 1442b68101a1SKees Cook If this type of key can be updated, then this method should be provided. 1443b68101a1SKees Cook It is called to update a key's payload from the blob of data provided. 1444b68101a1SKees Cook 1445b68101a1SKees Cook The prep->data and prep->datalen fields will define the original payload 1446b68101a1SKees Cook blob. If preparse() was supplied then other fields may be filled in also. 1447b68101a1SKees Cook 1448b68101a1SKees Cook key_payload_reserve() should be called if the data length might change 1449b68101a1SKees Cook before any changes are actually made. Note that if this succeeds, the type 1450b68101a1SKees Cook is committed to changing the key because it's already been altered, so all 1451b68101a1SKees Cook memory allocation must be done first. 1452b68101a1SKees Cook 1453b68101a1SKees Cook The key will have its semaphore write-locked before this method is called, 1454b68101a1SKees Cook but this only deters other writers; any changes to the key's payload must 1455b68101a1SKees Cook be made under RCU conditions, and call_rcu() must be used to dispose of 1456b68101a1SKees Cook the old payload. 1457b68101a1SKees Cook 1458b68101a1SKees Cook key_payload_reserve() should be called before the changes are made, but 1459b68101a1SKees Cook after all allocations and other potentially failing function calls are 1460b68101a1SKees Cook made. 1461b68101a1SKees Cook 1462b68101a1SKees Cook It is safe to sleep in this method. 1463b68101a1SKees Cook 1464b68101a1SKees Cook 1465b68101a1SKees Cook * ``int (*match_preparse)(struct key_match_data *match_data);`` 1466b68101a1SKees Cook 1467b68101a1SKees Cook This method is optional. It is called when a key search is about to be 1468b68101a1SKees Cook performed. It is given the following structure:: 1469b68101a1SKees Cook 1470b68101a1SKees Cook struct key_match_data { 1471b68101a1SKees Cook bool (*cmp)(const struct key *key, 1472b68101a1SKees Cook const struct key_match_data *match_data); 1473b68101a1SKees Cook const void *raw_data; 1474b68101a1SKees Cook void *preparsed; 1475b68101a1SKees Cook unsigned lookup_type; 1476b68101a1SKees Cook }; 1477b68101a1SKees Cook 1478b68101a1SKees Cook On entry, raw_data will be pointing to the criteria to be used in matching 1479b68101a1SKees Cook a key by the caller and should not be modified. ``(*cmp)()`` will be pointing 1480b68101a1SKees Cook to the default matcher function (which does an exact description match 1481b68101a1SKees Cook against raw_data) and lookup_type will be set to indicate a direct lookup. 1482b68101a1SKees Cook 1483b68101a1SKees Cook The following lookup_type values are available: 1484b68101a1SKees Cook 1485b68101a1SKees Cook * KEYRING_SEARCH_LOOKUP_DIRECT - A direct lookup hashes the type and 1486b68101a1SKees Cook description to narrow down the search to a small number of keys. 1487b68101a1SKees Cook 1488b68101a1SKees Cook * KEYRING_SEARCH_LOOKUP_ITERATE - An iterative lookup walks all the 1489b68101a1SKees Cook keys in the keyring until one is matched. This must be used for any 1490b68101a1SKees Cook search that's not doing a simple direct match on the key description. 1491b68101a1SKees Cook 1492b68101a1SKees Cook The method may set cmp to point to a function of its choice that does some 1493b68101a1SKees Cook other form of match, may set lookup_type to KEYRING_SEARCH_LOOKUP_ITERATE 1494b68101a1SKees Cook and may attach something to the preparsed pointer for use by ``(*cmp)()``. 1495b68101a1SKees Cook ``(*cmp)()`` should return true if a key matches and false otherwise. 1496b68101a1SKees Cook 1497b68101a1SKees Cook If preparsed is set, it may be necessary to use the match_free() method to 1498b68101a1SKees Cook clean it up. 1499b68101a1SKees Cook 1500b68101a1SKees Cook The method should return 0 if successful or a negative error code 1501b68101a1SKees Cook otherwise. 1502b68101a1SKees Cook 1503b68101a1SKees Cook It is permitted to sleep in this method, but ``(*cmp)()`` may not sleep as 1504b68101a1SKees Cook locks will be held over it. 1505b68101a1SKees Cook 1506b68101a1SKees Cook If match_preparse() is not provided, keys of this type will be matched 1507b68101a1SKees Cook exactly by their description. 1508b68101a1SKees Cook 1509b68101a1SKees Cook 1510b68101a1SKees Cook * ``void (*match_free)(struct key_match_data *match_data);`` 1511b68101a1SKees Cook 1512b68101a1SKees Cook This method is optional. If given, it called to clean up 1513b68101a1SKees Cook match_data->preparsed after a successful call to match_preparse(). 1514b68101a1SKees Cook 1515b68101a1SKees Cook 1516b68101a1SKees Cook * ``void (*revoke)(struct key *key);`` 1517b68101a1SKees Cook 1518b68101a1SKees Cook This method is optional. It is called to discard part of the payload 1519b68101a1SKees Cook data upon a key being revoked. The caller will have the key semaphore 1520b68101a1SKees Cook write-locked. 1521b68101a1SKees Cook 1522b68101a1SKees Cook It is safe to sleep in this method, though care should be taken to avoid 1523b68101a1SKees Cook a deadlock against the key semaphore. 1524b68101a1SKees Cook 1525b68101a1SKees Cook 1526b68101a1SKees Cook * ``void (*destroy)(struct key *key);`` 1527b68101a1SKees Cook 1528b68101a1SKees Cook This method is optional. It is called to discard the payload data on a key 1529b68101a1SKees Cook when it is being destroyed. 1530b68101a1SKees Cook 1531b68101a1SKees Cook This method does not need to lock the key to access the payload; it can 1532b68101a1SKees Cook consider the key as being inaccessible at this time. Note that the key's 1533b68101a1SKees Cook type may have been changed before this function is called. 1534b68101a1SKees Cook 1535b68101a1SKees Cook It is not safe to sleep in this method; the caller may hold spinlocks. 1536b68101a1SKees Cook 1537b68101a1SKees Cook 1538b68101a1SKees Cook * ``void (*describe)(const struct key *key, struct seq_file *p);`` 1539b68101a1SKees Cook 1540b68101a1SKees Cook This method is optional. It is called during /proc/keys reading to 1541b68101a1SKees Cook summarise a key's description and payload in text form. 1542b68101a1SKees Cook 1543b68101a1SKees Cook This method will be called with the RCU read lock held. rcu_dereference() 1544b68101a1SKees Cook should be used to read the payload pointer if the payload is to be 1545b68101a1SKees Cook accessed. key->datalen cannot be trusted to stay consistent with the 1546b68101a1SKees Cook contents of the payload. 1547b68101a1SKees Cook 1548b68101a1SKees Cook The description will not change, though the key's state may. 1549b68101a1SKees Cook 1550b68101a1SKees Cook It is not safe to sleep in this method; the RCU read lock is held by the 1551b68101a1SKees Cook caller. 1552b68101a1SKees Cook 1553b68101a1SKees Cook 1554b68101a1SKees Cook * ``long (*read)(const struct key *key, char __user *buffer, size_t buflen);`` 1555b68101a1SKees Cook 1556b68101a1SKees Cook This method is optional. It is called by KEYCTL_READ to translate the 1557b68101a1SKees Cook key's payload into something a blob of data for userspace to deal with. 1558b68101a1SKees Cook Ideally, the blob should be in the same format as that passed in to the 1559b68101a1SKees Cook instantiate and update methods. 1560b68101a1SKees Cook 1561b68101a1SKees Cook If successful, the blob size that could be produced should be returned 1562b68101a1SKees Cook rather than the size copied. 1563b68101a1SKees Cook 1564b68101a1SKees Cook This method will be called with the key's semaphore read-locked. This will 1565b68101a1SKees Cook prevent the key's payload changing. It is not necessary to use RCU locking 1566b68101a1SKees Cook when accessing the key's payload. It is safe to sleep in this method, such 1567b68101a1SKees Cook as might happen when the userspace buffer is accessed. 1568b68101a1SKees Cook 1569b68101a1SKees Cook 1570b68101a1SKees Cook * ``int (*request_key)(struct key_construction *cons, const char *op, void *aux);`` 1571b68101a1SKees Cook 1572b68101a1SKees Cook This method is optional. If provided, request_key() and friends will 1573b68101a1SKees Cook invoke this function rather than upcalling to /sbin/request-key to operate 1574b68101a1SKees Cook upon a key of this type. 1575b68101a1SKees Cook 1576b68101a1SKees Cook The aux parameter is as passed to request_key_async_with_auxdata() and 1577b68101a1SKees Cook similar or is NULL otherwise. Also passed are the construction record for 1578b68101a1SKees Cook the key to be operated upon and the operation type (currently only 1579b68101a1SKees Cook "create"). 1580b68101a1SKees Cook 1581b68101a1SKees Cook This method is permitted to return before the upcall is complete, but the 1582b68101a1SKees Cook following function must be called under all circumstances to complete the 1583b68101a1SKees Cook instantiation process, whether or not it succeeds, whether or not there's 1584b68101a1SKees Cook an error:: 1585b68101a1SKees Cook 1586b68101a1SKees Cook void complete_request_key(struct key_construction *cons, int error); 1587b68101a1SKees Cook 1588b68101a1SKees Cook The error parameter should be 0 on success, -ve on error. The 1589b68101a1SKees Cook construction record is destroyed by this action and the authorisation key 1590b68101a1SKees Cook will be revoked. If an error is indicated, the key under construction 1591b68101a1SKees Cook will be negatively instantiated if it wasn't already instantiated. 1592b68101a1SKees Cook 1593b68101a1SKees Cook If this method returns an error, that error will be returned to the 1594b68101a1SKees Cook caller of request_key*(). complete_request_key() must be called prior to 1595b68101a1SKees Cook returning. 1596b68101a1SKees Cook 1597b68101a1SKees Cook The key under construction and the authorisation key can be found in the 1598b68101a1SKees Cook key_construction struct pointed to by cons: 1599b68101a1SKees Cook 1600b68101a1SKees Cook * ``struct key *key;`` 1601b68101a1SKees Cook 1602b68101a1SKees Cook The key under construction. 1603b68101a1SKees Cook 1604b68101a1SKees Cook * ``struct key *authkey;`` 1605b68101a1SKees Cook 1606b68101a1SKees Cook The authorisation key. 1607b68101a1SKees Cook 1608b68101a1SKees Cook 1609b68101a1SKees Cook * ``struct key_restriction *(*lookup_restriction)(const char *params);`` 1610b68101a1SKees Cook 1611b68101a1SKees Cook This optional method is used to enable userspace configuration of keyring 1612b68101a1SKees Cook restrictions. The restriction parameter string (not including the key type 1613b68101a1SKees Cook name) is passed in, and this method returns a pointer to a key_restriction 1614b68101a1SKees Cook structure containing the relevant functions and data to evaluate each 1615b68101a1SKees Cook attempted key link operation. If there is no match, -EINVAL is returned. 1616b68101a1SKees Cook 1617b68101a1SKees Cook 161870025f84SDavid Howells * ``int (*asym_eds_op)(struct kernel_pkey_params *params, 161970025f84SDavid Howells const void *in, void *out);`` 162070025f84SDavid Howells ``int (*asym_verify_signature)(struct kernel_pkey_params *params, 162170025f84SDavid Howells const void *in, const void *in2);`` 162270025f84SDavid Howells 162370025f84SDavid Howells These methods are optional. If provided the first allows a key to be 162470025f84SDavid Howells used to encrypt, decrypt or sign a blob of data, and the second allows a 162570025f84SDavid Howells key to verify a signature. 162670025f84SDavid Howells 162770025f84SDavid Howells In all cases, the following information is provided in the params block:: 162870025f84SDavid Howells 162970025f84SDavid Howells struct kernel_pkey_params { 163070025f84SDavid Howells struct key *key; 163170025f84SDavid Howells const char *encoding; 163270025f84SDavid Howells const char *hash_algo; 163370025f84SDavid Howells char *info; 163470025f84SDavid Howells __u32 in_len; 163570025f84SDavid Howells union { 163670025f84SDavid Howells __u32 out_len; 163770025f84SDavid Howells __u32 in2_len; 163870025f84SDavid Howells }; 163970025f84SDavid Howells enum kernel_pkey_operation op : 8; 164070025f84SDavid Howells }; 164170025f84SDavid Howells 164270025f84SDavid Howells This includes the key to be used; a string indicating the encoding to use 164370025f84SDavid Howells (for instance, "pkcs1" may be used with an RSA key to indicate 164470025f84SDavid Howells RSASSA-PKCS1-v1.5 or RSAES-PKCS1-v1.5 encoding or "raw" if no encoding); 164570025f84SDavid Howells the name of the hash algorithm used to generate the data for a signature 164670025f84SDavid Howells (if appropriate); the sizes of the input and output (or second input) 164770025f84SDavid Howells buffers; and the ID of the operation to be performed. 164870025f84SDavid Howells 164970025f84SDavid Howells For a given operation ID, the input and output buffers are used as 165070025f84SDavid Howells follows:: 165170025f84SDavid Howells 165270025f84SDavid Howells Operation ID in,in_len out,out_len in2,in2_len 165370025f84SDavid Howells ======================= =============== =============== =============== 165470025f84SDavid Howells kernel_pkey_encrypt Raw data Encrypted data - 165570025f84SDavid Howells kernel_pkey_decrypt Encrypted data Raw data - 165670025f84SDavid Howells kernel_pkey_sign Raw data Signature - 165770025f84SDavid Howells kernel_pkey_verify Raw data - Signature 165870025f84SDavid Howells 165970025f84SDavid Howells asym_eds_op() deals with encryption, decryption and signature creation as 166070025f84SDavid Howells specified by params->op. Note that params->op is also set for 166170025f84SDavid Howells asym_verify_signature(). 166270025f84SDavid Howells 166370025f84SDavid Howells Encrypting and signature creation both take raw data in the input buffer 166470025f84SDavid Howells and return the encrypted result in the output buffer. Padding may have 166570025f84SDavid Howells been added if an encoding was set. In the case of signature creation, 166670025f84SDavid Howells depending on the encoding, the padding created may need to indicate the 166770025f84SDavid Howells digest algorithm - the name of which should be supplied in hash_algo. 166870025f84SDavid Howells 166970025f84SDavid Howells Decryption takes encrypted data in the input buffer and returns the raw 167070025f84SDavid Howells data in the output buffer. Padding will get checked and stripped off if 167170025f84SDavid Howells an encoding was set. 167270025f84SDavid Howells 167370025f84SDavid Howells Verification takes raw data in the input buffer and the signature in the 167470025f84SDavid Howells second input buffer and checks that the one matches the other. Padding 167570025f84SDavid Howells will be validated. Depending on the encoding, the digest algorithm used 167670025f84SDavid Howells to generate the raw data may need to be indicated in hash_algo. 167770025f84SDavid Howells 167870025f84SDavid Howells If successful, asym_eds_op() should return the number of bytes written 167970025f84SDavid Howells into the output buffer. asym_verify_signature() should return 0. 168070025f84SDavid Howells 168170025f84SDavid Howells A variety of errors may be returned, including EOPNOTSUPP if the operation 168270025f84SDavid Howells is not supported; EKEYREJECTED if verification fails; ENOPKG if the 168370025f84SDavid Howells required crypto isn't available. 168470025f84SDavid Howells 168570025f84SDavid Howells 168670025f84SDavid Howells * ``int (*asym_query)(const struct kernel_pkey_params *params, 168770025f84SDavid Howells struct kernel_pkey_query *info);`` 168870025f84SDavid Howells 168970025f84SDavid Howells This method is optional. If provided it allows information about the 169070025f84SDavid Howells public or asymmetric key held in the key to be determined. 169170025f84SDavid Howells 169270025f84SDavid Howells The parameter block is as for asym_eds_op() and co. but in_len and out_len 169370025f84SDavid Howells are unused. The encoding and hash_algo fields should be used to reduce 169470025f84SDavid Howells the returned buffer/data sizes as appropriate. 169570025f84SDavid Howells 169670025f84SDavid Howells If successful, the following information is filled in:: 169770025f84SDavid Howells 169870025f84SDavid Howells struct kernel_pkey_query { 169970025f84SDavid Howells __u32 supported_ops; 170070025f84SDavid Howells __u32 key_size; 170170025f84SDavid Howells __u16 max_data_size; 170270025f84SDavid Howells __u16 max_sig_size; 170370025f84SDavid Howells __u16 max_enc_size; 170470025f84SDavid Howells __u16 max_dec_size; 170570025f84SDavid Howells }; 170670025f84SDavid Howells 170770025f84SDavid Howells The supported_ops field will contain a bitmask indicating what operations 170870025f84SDavid Howells are supported by the key, including encryption of a blob, decryption of a 170970025f84SDavid Howells blob, signing a blob and verifying the signature on a blob. The following 171070025f84SDavid Howells constants are defined for this:: 171170025f84SDavid Howells 171270025f84SDavid Howells KEYCTL_SUPPORTS_{ENCRYPT,DECRYPT,SIGN,VERIFY} 171370025f84SDavid Howells 171470025f84SDavid Howells The key_size field is the size of the key in bits. max_data_size and 171570025f84SDavid Howells max_sig_size are the maximum raw data and signature sizes for creation and 171670025f84SDavid Howells verification of a signature; max_enc_size and max_dec_size are the maximum 171770025f84SDavid Howells raw data and signature sizes for encryption and decryption. The 171870025f84SDavid Howells max_*_size fields are measured in bytes. 171970025f84SDavid Howells 172070025f84SDavid Howells If successful, 0 will be returned. If the key doesn't support this, 172170025f84SDavid Howells EOPNOTSUPP will be returned. 172270025f84SDavid Howells 172370025f84SDavid Howells 1724b68101a1SKees CookRequest-Key Callback Service 1725b68101a1SKees Cook============================ 1726b68101a1SKees Cook 1727b68101a1SKees CookTo create a new key, the kernel will attempt to execute the following command 1728b68101a1SKees Cookline:: 1729b68101a1SKees Cook 1730b68101a1SKees Cook /sbin/request-key create <key> <uid> <gid> \ 1731b68101a1SKees Cook <threadring> <processring> <sessionring> <callout_info> 1732b68101a1SKees Cook 1733b68101a1SKees Cook<key> is the key being constructed, and the three keyrings are the process 1734b68101a1SKees Cookkeyrings from the process that caused the search to be issued. These are 1735b68101a1SKees Cookincluded for two reasons: 1736b68101a1SKees Cook 1737b68101a1SKees Cook 1 There may be an authentication token in one of the keyrings that is 1738b68101a1SKees Cook required to obtain the key, eg: a Kerberos Ticket-Granting Ticket. 1739b68101a1SKees Cook 1740b68101a1SKees Cook 2 The new key should probably be cached in one of these rings. 1741b68101a1SKees Cook 1742b68101a1SKees CookThis program should set it UID and GID to those specified before attempting to 1743b68101a1SKees Cookaccess any more keys. It may then look around for a user specific process to 1744b68101a1SKees Cookhand the request off to (perhaps a path held in placed in another key by, for 1745b68101a1SKees Cookexample, the KDE desktop manager). 1746b68101a1SKees Cook 1747b68101a1SKees CookThe program (or whatever it calls) should finish construction of the key by 1748b68101a1SKees Cookcalling KEYCTL_INSTANTIATE or KEYCTL_INSTANTIATE_IOV, which also permits it to 1749b68101a1SKees Cookcache the key in one of the keyrings (probably the session ring) before 1750b68101a1SKees Cookreturning. Alternatively, the key can be marked as negative with KEYCTL_NEGATE 1751b68101a1SKees Cookor KEYCTL_REJECT; this also permits the key to be cached in one of the 1752b68101a1SKees Cookkeyrings. 1753b68101a1SKees Cook 1754b68101a1SKees CookIf it returns with the key remaining in the unconstructed state, the key will 1755b68101a1SKees Cookbe marked as being negative, it will be added to the session keyring, and an 1756b68101a1SKees Cookerror will be returned to the key requestor. 1757b68101a1SKees Cook 1758b68101a1SKees CookSupplementary information may be provided from whoever or whatever invoked this 1759b68101a1SKees Cookservice. This will be passed as the <callout_info> parameter. If no such 1760b68101a1SKees Cookinformation was made available, then "-" will be passed as this parameter 1761b68101a1SKees Cookinstead. 1762b68101a1SKees Cook 1763b68101a1SKees Cook 1764b68101a1SKees CookSimilarly, the kernel may attempt to update an expired or a soon to expire key 1765b68101a1SKees Cookby executing:: 1766b68101a1SKees Cook 1767b68101a1SKees Cook /sbin/request-key update <key> <uid> <gid> \ 1768b68101a1SKees Cook <threadring> <processring> <sessionring> 1769b68101a1SKees Cook 1770b68101a1SKees CookIn this case, the program isn't required to actually attach the key to a ring; 1771b68101a1SKees Cookthe rings are provided for reference. 1772b68101a1SKees Cook 1773b68101a1SKees Cook 1774b68101a1SKees CookGarbage Collection 1775b68101a1SKees Cook================== 1776b68101a1SKees Cook 1777b68101a1SKees CookDead keys (for which the type has been removed) will be automatically unlinked 1778b68101a1SKees Cookfrom those keyrings that point to them and deleted as soon as possible by a 1779b68101a1SKees Cookbackground garbage collector. 1780b68101a1SKees Cook 1781b68101a1SKees CookSimilarly, revoked and expired keys will be garbage collected, but only after a 1782b68101a1SKees Cookcertain amount of time has passed. This time is set as a number of seconds in:: 1783b68101a1SKees Cook 1784b68101a1SKees Cook /proc/sys/kernel/keys/gc_delay 1785