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 60028db3e2SLinus Torvalds * Each key has an owner user ID, a group ID and a permissions mask. These 61028db3e2SLinus Torvalds are used to control what a process may do to a key from userspace, and 62028db3e2SLinus Torvalds 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 201028db3e2SLinus TorvaldsKeys have an owner user ID, a group access ID, and a permissions mask. The mask 202028db3e2SLinus Torvaldshas up to eight bits each for possessor, user, group and other access. Only 203028db3e2SLinus Torvaldssix of each set of eight bits are defined. These permissions granted are: 204b68101a1SKees Cook 205028db3e2SLinus Torvalds * View 206b68101a1SKees Cook 207028db3e2SLinus Torvalds This permits a key or keyring's attributes to be viewed - including key 208028db3e2SLinus Torvalds type and description. 209b68101a1SKees Cook 210028db3e2SLinus Torvalds * 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 215028db3e2SLinus Torvalds * Write 216b68101a1SKees Cook 217028db3e2SLinus Torvalds This permits a key's payload to be instantiated or updated, or it allows a 218028db3e2SLinus Torvalds link to be added to or removed from a keyring. 219b68101a1SKees Cook 220028db3e2SLinus Torvalds * 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 225028db3e2SLinus Torvalds * Link 226b68101a1SKees Cook 227b68101a1SKees Cook This permits a key or keyring to be linked to. To create a link from a 228028db3e2SLinus Torvalds keyring to a key, a process must have Write permission on the keyring and 229028db3e2SLinus Torvalds Link permission on the key. 230b68101a1SKees Cook 231028db3e2SLinus Torvalds * 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 436504b69ebSDavid Howells To link a key into the destination keyring the key must grant link 437504b69ebSDavid Howells permission on the key to the caller and the keyring must grant write 438504b69ebSDavid Howells permission. 439504b69ebSDavid Howells 440adf31eebSJosh Holland See also Documentation/security/keys/request-key.rst. 441b68101a1SKees Cook 442b68101a1SKees Cook 443b68101a1SKees CookThe keyctl syscall functions are: 444b68101a1SKees Cook 445b68101a1SKees Cook * Map a special key ID to a real key ID for this process:: 446b68101a1SKees Cook 447b68101a1SKees Cook key_serial_t keyctl(KEYCTL_GET_KEYRING_ID, key_serial_t id, 448b68101a1SKees Cook int create); 449b68101a1SKees Cook 450b68101a1SKees Cook The special key specified by "id" is looked up (with the key being created 451b68101a1SKees Cook if necessary) and the ID of the key or keyring thus found is returned if 452b68101a1SKees Cook it exists. 453b68101a1SKees Cook 454b68101a1SKees Cook If the key does not yet exist, the key will be created if "create" is 455b68101a1SKees Cook non-zero; and the error ENOKEY will be returned if "create" is zero. 456b68101a1SKees Cook 457b68101a1SKees Cook 458b68101a1SKees Cook * Replace the session keyring this process subscribes to with a new one:: 459b68101a1SKees Cook 460b68101a1SKees Cook key_serial_t keyctl(KEYCTL_JOIN_SESSION_KEYRING, const char *name); 461b68101a1SKees Cook 462b68101a1SKees Cook If name is NULL, an anonymous keyring is created attached to the process 463b68101a1SKees Cook as its session keyring, displacing the old session keyring. 464b68101a1SKees Cook 465b68101a1SKees Cook If name is not NULL, if a keyring of that name exists, the process 466b68101a1SKees Cook attempts to attach it as the session keyring, returning an error if that 467b68101a1SKees Cook is not permitted; otherwise a new keyring of that name is created and 468b68101a1SKees Cook attached as the session keyring. 469b68101a1SKees Cook 470b68101a1SKees Cook To attach to a named keyring, the keyring must have search permission for 471b68101a1SKees Cook the process's ownership. 472b68101a1SKees Cook 473b68101a1SKees Cook The ID of the new session keyring is returned if successful. 474b68101a1SKees Cook 475b68101a1SKees Cook 476b68101a1SKees Cook * Update the specified key:: 477b68101a1SKees Cook 478b68101a1SKees Cook long keyctl(KEYCTL_UPDATE, key_serial_t key, const void *payload, 479b68101a1SKees Cook size_t plen); 480b68101a1SKees Cook 481b68101a1SKees Cook This will try to update the specified key with the given payload, or it 482b68101a1SKees Cook will return error EOPNOTSUPP if that function is not supported by the key 483b68101a1SKees Cook type. The process must also have permission to write to the key to be able 484b68101a1SKees Cook to update it. 485b68101a1SKees Cook 486b68101a1SKees Cook The payload is of length plen, and may be absent or empty as for 487b68101a1SKees Cook add_key(). 488b68101a1SKees Cook 489b68101a1SKees Cook 490b68101a1SKees Cook * Revoke a key:: 491b68101a1SKees Cook 492b68101a1SKees Cook long keyctl(KEYCTL_REVOKE, key_serial_t key); 493b68101a1SKees Cook 494b68101a1SKees Cook This makes a key unavailable for further operations. Further attempts to 495b68101a1SKees Cook use the key will be met with error EKEYREVOKED, and the key will no longer 496b68101a1SKees Cook be findable. 497b68101a1SKees Cook 498b68101a1SKees Cook 499b68101a1SKees Cook * Change the ownership of a key:: 500b68101a1SKees Cook 501b68101a1SKees Cook long keyctl(KEYCTL_CHOWN, key_serial_t key, uid_t uid, gid_t gid); 502b68101a1SKees Cook 503b68101a1SKees Cook This function permits a key's owner and group ID to be changed. Either one 504b68101a1SKees Cook of uid or gid can be set to -1 to suppress that change. 505b68101a1SKees Cook 506b68101a1SKees Cook Only the superuser can change a key's owner to something other than the 507b68101a1SKees Cook key's current owner. Similarly, only the superuser can change a key's 508b68101a1SKees Cook group ID to something other than the calling process's group ID or one of 509b68101a1SKees Cook its group list members. 510b68101a1SKees Cook 511b68101a1SKees Cook 512b68101a1SKees Cook * Change the permissions mask on a key:: 513b68101a1SKees Cook 514b68101a1SKees Cook long keyctl(KEYCTL_SETPERM, key_serial_t key, key_perm_t perm); 515b68101a1SKees Cook 516b68101a1SKees Cook This function permits the owner of a key or the superuser to change the 517b68101a1SKees Cook permissions mask on a key. 518b68101a1SKees Cook 519b68101a1SKees Cook Only bits the available bits are permitted; if any other bits are set, 520b68101a1SKees Cook error EINVAL will be returned. 521b68101a1SKees Cook 522b68101a1SKees Cook 523b68101a1SKees Cook * Describe a key:: 524b68101a1SKees Cook 525b68101a1SKees Cook long keyctl(KEYCTL_DESCRIBE, key_serial_t key, char *buffer, 526b68101a1SKees Cook size_t buflen); 527b68101a1SKees Cook 528b68101a1SKees Cook This function returns a summary of the key's attributes (but not its 529b68101a1SKees Cook payload data) as a string in the buffer provided. 530b68101a1SKees Cook 531b68101a1SKees Cook Unless there's an error, it always returns the amount of data it could 532b68101a1SKees Cook produce, even if that's too big for the buffer, but it won't copy more 533b68101a1SKees Cook than requested to userspace. If the buffer pointer is NULL then no copy 534b68101a1SKees Cook will take place. 535b68101a1SKees Cook 536b68101a1SKees Cook A process must have view permission on the key for this function to be 537b68101a1SKees Cook successful. 538b68101a1SKees Cook 539b68101a1SKees Cook If successful, a string is placed in the buffer in the following format:: 540b68101a1SKees Cook 541b68101a1SKees Cook <type>;<uid>;<gid>;<perm>;<description> 542b68101a1SKees Cook 543b68101a1SKees Cook Where type and description are strings, uid and gid are decimal, and perm 544b68101a1SKees Cook is hexadecimal. A NUL character is included at the end of the string if 545b68101a1SKees Cook the buffer is sufficiently big. 546b68101a1SKees Cook 547b68101a1SKees Cook This can be parsed with:: 548b68101a1SKees Cook 549b68101a1SKees Cook sscanf(buffer, "%[^;];%d;%d;%o;%s", type, &uid, &gid, &mode, desc); 550b68101a1SKees Cook 551b68101a1SKees Cook 552b68101a1SKees Cook * Clear out a keyring:: 553b68101a1SKees Cook 554b68101a1SKees Cook long keyctl(KEYCTL_CLEAR, key_serial_t keyring); 555b68101a1SKees Cook 556b68101a1SKees Cook This function clears the list of keys attached to a keyring. The calling 557b68101a1SKees Cook process must have write permission on the keyring, and it must be a 558b68101a1SKees Cook keyring (or else error ENOTDIR will result). 559b68101a1SKees Cook 560b68101a1SKees Cook This function can also be used to clear special kernel keyrings if they 561b68101a1SKees Cook are appropriately marked if the user has CAP_SYS_ADMIN capability. The 562b68101a1SKees Cook DNS resolver cache keyring is an example of this. 563b68101a1SKees Cook 564b68101a1SKees Cook 565b68101a1SKees Cook * Link a key into a keyring:: 566b68101a1SKees Cook 567b68101a1SKees Cook long keyctl(KEYCTL_LINK, key_serial_t keyring, key_serial_t key); 568b68101a1SKees Cook 569b68101a1SKees Cook This function creates a link from the keyring to the key. The process must 570b68101a1SKees Cook have write permission on the keyring and must have link permission on the 571b68101a1SKees Cook key. 572b68101a1SKees Cook 573b68101a1SKees Cook Should the keyring not be a keyring, error ENOTDIR will result; and if the 574b68101a1SKees Cook keyring is full, error ENFILE will result. 575b68101a1SKees Cook 576b68101a1SKees Cook The link procedure checks the nesting of the keyrings, returning ELOOP if 577b68101a1SKees Cook it appears too deep or EDEADLK if the link would introduce a cycle. 578b68101a1SKees Cook 579b68101a1SKees Cook Any links within the keyring to keys that match the new key in terms of 580b68101a1SKees Cook type and description will be discarded from the keyring as the new one is 581b68101a1SKees Cook added. 582b68101a1SKees Cook 583b68101a1SKees Cook 584ed0ac5c7SDavid Howells * Move a key from one keyring to another:: 585ed0ac5c7SDavid Howells 586ed0ac5c7SDavid Howells long keyctl(KEYCTL_MOVE, 587ed0ac5c7SDavid Howells key_serial_t id, 588ed0ac5c7SDavid Howells key_serial_t from_ring_id, 589ed0ac5c7SDavid Howells key_serial_t to_ring_id, 590ed0ac5c7SDavid Howells unsigned int flags); 591ed0ac5c7SDavid Howells 592ed0ac5c7SDavid Howells Move the key specified by "id" from the keyring specified by 593ed0ac5c7SDavid Howells "from_ring_id" to the keyring specified by "to_ring_id". If the two 594ed0ac5c7SDavid Howells keyrings are the same, nothing is done. 595ed0ac5c7SDavid Howells 596ed0ac5c7SDavid Howells "flags" can have KEYCTL_MOVE_EXCL set in it to cause the operation to fail 597ed0ac5c7SDavid Howells with EEXIST if a matching key exists in the destination keyring, otherwise 598ed0ac5c7SDavid Howells such a key will be replaced. 599ed0ac5c7SDavid Howells 600ed0ac5c7SDavid Howells A process must have link permission on the key for this function to be 601ed0ac5c7SDavid Howells successful and write permission on both keyrings. Any errors that can 602ed0ac5c7SDavid Howells occur from KEYCTL_LINK also apply on the destination keyring here. 603ed0ac5c7SDavid Howells 604ed0ac5c7SDavid Howells 605b68101a1SKees Cook * Unlink a key or keyring from another keyring:: 606b68101a1SKees Cook 607b68101a1SKees Cook long keyctl(KEYCTL_UNLINK, key_serial_t keyring, key_serial_t key); 608b68101a1SKees Cook 609b68101a1SKees Cook This function looks through the keyring for the first link to the 610b68101a1SKees Cook specified key, and removes it if found. Subsequent links to that key are 611b68101a1SKees Cook ignored. The process must have write permission on the keyring. 612b68101a1SKees Cook 613b68101a1SKees Cook If the keyring is not a keyring, error ENOTDIR will result; and if the key 614b68101a1SKees Cook is not present, error ENOENT will be the result. 615b68101a1SKees Cook 616b68101a1SKees Cook 617b68101a1SKees Cook * Search a keyring tree for a key:: 618b68101a1SKees Cook 619b68101a1SKees Cook key_serial_t keyctl(KEYCTL_SEARCH, key_serial_t keyring, 620b68101a1SKees Cook const char *type, const char *description, 621b68101a1SKees Cook key_serial_t dest_keyring); 622b68101a1SKees Cook 623b68101a1SKees Cook This searches the keyring tree headed by the specified keyring until a key 624b68101a1SKees Cook is found that matches the type and description criteria. Each keyring is 625b68101a1SKees Cook checked for keys before recursion into its children occurs. 626b68101a1SKees Cook 627b68101a1SKees Cook The process must have search permission on the top level keyring, or else 628b68101a1SKees Cook error EACCES will result. Only keyrings that the process has search 629b68101a1SKees Cook permission on will be recursed into, and only keys and keyrings for which 630b68101a1SKees Cook a process has search permission can be matched. If the specified keyring 631b68101a1SKees Cook is not a keyring, ENOTDIR will result. 632b68101a1SKees Cook 633b68101a1SKees Cook If the search succeeds, the function will attempt to link the found key 634b68101a1SKees Cook into the destination keyring if one is supplied (non-zero ID). All the 635b68101a1SKees Cook constraints applicable to KEYCTL_LINK apply in this case too. 636b68101a1SKees Cook 637b68101a1SKees Cook Error ENOKEY, EKEYREVOKED or EKEYEXPIRED will be returned if the search 638b68101a1SKees Cook fails. On success, the resulting key ID will be returned. 639b68101a1SKees Cook 640b68101a1SKees Cook 641b68101a1SKees Cook * Read the payload data from a key:: 642b68101a1SKees Cook 643b68101a1SKees Cook long keyctl(KEYCTL_READ, key_serial_t keyring, char *buffer, 644b68101a1SKees Cook size_t buflen); 645b68101a1SKees Cook 646b68101a1SKees Cook This function attempts to read the payload data from the specified key 647b68101a1SKees Cook into the buffer. The process must have read permission on the key to 648b68101a1SKees Cook succeed. 649b68101a1SKees Cook 650b68101a1SKees Cook The returned data will be processed for presentation by the key type. For 651b68101a1SKees Cook instance, a keyring will return an array of key_serial_t entries 652b68101a1SKees Cook representing the IDs of all the keys to which it is subscribed. The user 653b68101a1SKees Cook defined key type will return its data as is. If a key type does not 654b68101a1SKees Cook implement this function, error EOPNOTSUPP will result. 655b68101a1SKees Cook 656be543dd6SEric Biggers If the specified buffer is too small, then the size of the buffer required 657be543dd6SEric Biggers will be returned. Note that in this case, the contents of the buffer may 658be543dd6SEric Biggers have been overwritten in some undefined way. 659b68101a1SKees Cook 660be543dd6SEric Biggers Otherwise, on success, the function will return the amount of data copied 661be543dd6SEric Biggers into the buffer. 662b68101a1SKees Cook 663b68101a1SKees Cook * Instantiate a partially constructed key:: 664b68101a1SKees Cook 665b68101a1SKees Cook long keyctl(KEYCTL_INSTANTIATE, key_serial_t key, 666b68101a1SKees Cook const void *payload, size_t plen, 667b68101a1SKees Cook key_serial_t keyring); 668b68101a1SKees Cook long keyctl(KEYCTL_INSTANTIATE_IOV, key_serial_t key, 669b68101a1SKees Cook const struct iovec *payload_iov, unsigned ioc, 670b68101a1SKees Cook key_serial_t keyring); 671b68101a1SKees Cook 672b68101a1SKees Cook If the kernel calls back to userspace to complete the instantiation of a 673b68101a1SKees Cook key, userspace should use this call to supply data for the key before the 674b68101a1SKees Cook invoked process returns, or else the key will be marked negative 675b68101a1SKees Cook automatically. 676b68101a1SKees Cook 677b68101a1SKees Cook The process must have write access on the key to be able to instantiate 678b68101a1SKees Cook it, and the key must be uninstantiated. 679b68101a1SKees Cook 680b68101a1SKees Cook If a keyring is specified (non-zero), the key will also be linked into 681b68101a1SKees Cook that keyring, however all the constraints applying in KEYCTL_LINK apply in 682b68101a1SKees Cook this case too. 683b68101a1SKees Cook 684b68101a1SKees Cook The payload and plen arguments describe the payload data as for add_key(). 685b68101a1SKees Cook 686b68101a1SKees Cook The payload_iov and ioc arguments describe the payload data in an iovec 687b68101a1SKees Cook array instead of a single buffer. 688b68101a1SKees Cook 689b68101a1SKees Cook 690b68101a1SKees Cook * Negatively instantiate a partially constructed key:: 691b68101a1SKees Cook 692b68101a1SKees Cook long keyctl(KEYCTL_NEGATE, key_serial_t key, 693b68101a1SKees Cook unsigned timeout, key_serial_t keyring); 694b68101a1SKees Cook long keyctl(KEYCTL_REJECT, key_serial_t key, 695b68101a1SKees Cook unsigned timeout, unsigned error, key_serial_t keyring); 696b68101a1SKees Cook 697b68101a1SKees Cook If the kernel calls back to userspace to complete the instantiation of a 698b68101a1SKees Cook key, userspace should use this call mark the key as negative before the 699b68101a1SKees Cook invoked process returns if it is unable to fulfill the request. 700b68101a1SKees Cook 701b68101a1SKees Cook The process must have write access on the key to be able to instantiate 702b68101a1SKees Cook it, and the key must be uninstantiated. 703b68101a1SKees Cook 704b68101a1SKees Cook If a keyring is specified (non-zero), the key will also be linked into 705b68101a1SKees Cook that keyring, however all the constraints applying in KEYCTL_LINK apply in 706b68101a1SKees Cook this case too. 707b68101a1SKees Cook 708b68101a1SKees Cook If the key is rejected, future searches for it will return the specified 709b68101a1SKees Cook error code until the rejected key expires. Negating the key is the same 710b68101a1SKees Cook as rejecting the key with ENOKEY as the error code. 711b68101a1SKees Cook 712b68101a1SKees Cook 713b68101a1SKees Cook * Set the default request-key destination keyring:: 714b68101a1SKees Cook 715b68101a1SKees Cook long keyctl(KEYCTL_SET_REQKEY_KEYRING, int reqkey_defl); 716b68101a1SKees Cook 717b68101a1SKees Cook This sets the default keyring to which implicitly requested keys will be 718b68101a1SKees Cook attached for this thread. reqkey_defl should be one of these constants:: 719b68101a1SKees Cook 720b68101a1SKees Cook CONSTANT VALUE NEW DEFAULT KEYRING 721b68101a1SKees Cook ====================================== ====== ======================= 722b68101a1SKees Cook KEY_REQKEY_DEFL_NO_CHANGE -1 No change 723b68101a1SKees Cook KEY_REQKEY_DEFL_DEFAULT 0 Default[1] 724b68101a1SKees Cook KEY_REQKEY_DEFL_THREAD_KEYRING 1 Thread keyring 725b68101a1SKees Cook KEY_REQKEY_DEFL_PROCESS_KEYRING 2 Process keyring 726b68101a1SKees Cook KEY_REQKEY_DEFL_SESSION_KEYRING 3 Session keyring 727b68101a1SKees Cook KEY_REQKEY_DEFL_USER_KEYRING 4 User keyring 728b68101a1SKees Cook KEY_REQKEY_DEFL_USER_SESSION_KEYRING 5 User session keyring 729b68101a1SKees Cook KEY_REQKEY_DEFL_GROUP_KEYRING 6 Group keyring 730b68101a1SKees Cook 731b68101a1SKees Cook The old default will be returned if successful and error EINVAL will be 732b68101a1SKees Cook returned if reqkey_defl is not one of the above values. 733b68101a1SKees Cook 734b68101a1SKees Cook The default keyring can be overridden by the keyring indicated to the 735b68101a1SKees Cook request_key() system call. 736b68101a1SKees Cook 737b68101a1SKees Cook Note that this setting is inherited across fork/exec. 738b68101a1SKees Cook 739b68101a1SKees Cook [1] The default is: the thread keyring if there is one, otherwise 740b68101a1SKees Cook the process keyring if there is one, otherwise the session keyring if 741b68101a1SKees Cook there is one, otherwise the user default session keyring. 742b68101a1SKees Cook 743b68101a1SKees Cook 744b68101a1SKees Cook * Set the timeout on a key:: 745b68101a1SKees Cook 746b68101a1SKees Cook long keyctl(KEYCTL_SET_TIMEOUT, key_serial_t key, unsigned timeout); 747b68101a1SKees Cook 748b68101a1SKees Cook This sets or clears the timeout on a key. The timeout can be 0 to clear 749b68101a1SKees Cook the timeout or a number of seconds to set the expiry time that far into 750b68101a1SKees Cook the future. 751b68101a1SKees Cook 752b68101a1SKees Cook The process must have attribute modification access on a key to set its 753b68101a1SKees Cook timeout. Timeouts may not be set with this function on negative, revoked 754b68101a1SKees Cook or expired keys. 755b68101a1SKees Cook 756b68101a1SKees Cook 757b68101a1SKees Cook * Assume the authority granted to instantiate a key:: 758b68101a1SKees Cook 759b68101a1SKees Cook long keyctl(KEYCTL_ASSUME_AUTHORITY, key_serial_t key); 760b68101a1SKees Cook 761b68101a1SKees Cook This assumes or divests the authority required to instantiate the 762b68101a1SKees Cook specified key. Authority can only be assumed if the thread has the 763b68101a1SKees Cook authorisation key associated with the specified key in its keyrings 764b68101a1SKees Cook somewhere. 765b68101a1SKees Cook 766b68101a1SKees Cook Once authority is assumed, searches for keys will also search the 767b68101a1SKees Cook requester's keyrings using the requester's security label, UID, GID and 768b68101a1SKees Cook groups. 769b68101a1SKees Cook 770b68101a1SKees Cook If the requested authority is unavailable, error EPERM will be returned, 771b68101a1SKees Cook likewise if the authority has been revoked because the target key is 772b68101a1SKees Cook already instantiated. 773b68101a1SKees Cook 774b68101a1SKees Cook If the specified key is 0, then any assumed authority will be divested. 775b68101a1SKees Cook 776b68101a1SKees Cook The assumed authoritative key is inherited across fork and exec. 777b68101a1SKees Cook 778b68101a1SKees Cook 779b68101a1SKees Cook * Get the LSM security context attached to a key:: 780b68101a1SKees Cook 781b68101a1SKees Cook long keyctl(KEYCTL_GET_SECURITY, key_serial_t key, char *buffer, 782b68101a1SKees Cook size_t buflen) 783b68101a1SKees Cook 784b68101a1SKees Cook This function returns a string that represents the LSM security context 785b68101a1SKees Cook attached to a key in the buffer provided. 786b68101a1SKees Cook 787b68101a1SKees Cook Unless there's an error, it always returns the amount of data it could 788b68101a1SKees Cook produce, even if that's too big for the buffer, but it won't copy more 789b68101a1SKees Cook than requested to userspace. If the buffer pointer is NULL then no copy 790b68101a1SKees Cook will take place. 791b68101a1SKees Cook 792b68101a1SKees Cook A NUL character is included at the end of the string if the buffer is 793b68101a1SKees Cook sufficiently big. This is included in the returned count. If no LSM is 794b68101a1SKees Cook in force then an empty string will be returned. 795b68101a1SKees Cook 796b68101a1SKees Cook A process must have view permission on the key for this function to be 797b68101a1SKees Cook successful. 798b68101a1SKees Cook 799b68101a1SKees Cook 800b68101a1SKees Cook * Install the calling process's session keyring on its parent:: 801b68101a1SKees Cook 802b68101a1SKees Cook long keyctl(KEYCTL_SESSION_TO_PARENT); 803b68101a1SKees Cook 804b68101a1SKees Cook This functions attempts to install the calling process's session keyring 805b68101a1SKees Cook on to the calling process's parent, replacing the parent's current session 806b68101a1SKees Cook keyring. 807b68101a1SKees Cook 808b68101a1SKees Cook The calling process must have the same ownership as its parent, the 809b68101a1SKees Cook keyring must have the same ownership as the calling process, the calling 810b68101a1SKees Cook process must have LINK permission on the keyring and the active LSM module 811b68101a1SKees Cook mustn't deny permission, otherwise error EPERM will be returned. 812b68101a1SKees Cook 813b68101a1SKees Cook Error ENOMEM will be returned if there was insufficient memory to complete 814b68101a1SKees Cook the operation, otherwise 0 will be returned to indicate success. 815b68101a1SKees Cook 816b68101a1SKees Cook The keyring will be replaced next time the parent process leaves the 817b68101a1SKees Cook kernel and resumes executing userspace. 818b68101a1SKees Cook 819b68101a1SKees Cook 820b68101a1SKees Cook * Invalidate a key:: 821b68101a1SKees Cook 822b68101a1SKees Cook long keyctl(KEYCTL_INVALIDATE, key_serial_t key); 823b68101a1SKees Cook 824b68101a1SKees Cook This function marks a key as being invalidated and then wakes up the 825b68101a1SKees Cook garbage collector. The garbage collector immediately removes invalidated 826b68101a1SKees Cook keys from all keyrings and deletes the key when its reference count 827b68101a1SKees Cook reaches zero. 828b68101a1SKees Cook 829b68101a1SKees Cook Keys that are marked invalidated become invisible to normal key operations 830b68101a1SKees Cook immediately, though they are still visible in /proc/keys until deleted 831b68101a1SKees Cook (they're marked with an 'i' flag). 832b68101a1SKees Cook 833b68101a1SKees Cook A process must have search permission on the key for this function to be 834b68101a1SKees Cook successful. 835b68101a1SKees Cook 836b68101a1SKees Cook * Compute a Diffie-Hellman shared secret or public key:: 837b68101a1SKees Cook 838b68101a1SKees Cook long keyctl(KEYCTL_DH_COMPUTE, struct keyctl_dh_params *params, 839b68101a1SKees Cook char *buffer, size_t buflen, struct keyctl_kdf_params *kdf); 840b68101a1SKees Cook 841b68101a1SKees Cook The params struct contains serial numbers for three keys:: 842b68101a1SKees Cook 843b68101a1SKees Cook - The prime, p, known to both parties 844b68101a1SKees Cook - The local private key 845b68101a1SKees Cook - The base integer, which is either a shared generator or the 846b68101a1SKees Cook remote public key 847b68101a1SKees Cook 848b68101a1SKees Cook The value computed is:: 849b68101a1SKees Cook 850b68101a1SKees Cook result = base ^ private (mod prime) 851b68101a1SKees Cook 852b68101a1SKees Cook If the base is the shared generator, the result is the local 853b68101a1SKees Cook public key. If the base is the remote public key, the result is 854b68101a1SKees Cook the shared secret. 855b68101a1SKees Cook 856b68101a1SKees Cook If the parameter kdf is NULL, the following applies: 857b68101a1SKees Cook 858b68101a1SKees Cook - The buffer length must be at least the length of the prime, or zero. 859b68101a1SKees Cook 860b68101a1SKees Cook - If the buffer length is nonzero, the length of the result is 861b68101a1SKees Cook returned when it is successfully calculated and copied in to the 862b68101a1SKees Cook buffer. When the buffer length is zero, the minimum required 863b68101a1SKees Cook buffer length is returned. 864b68101a1SKees Cook 865b68101a1SKees Cook The kdf parameter allows the caller to apply a key derivation function 866b68101a1SKees Cook (KDF) on the Diffie-Hellman computation where only the result 867b68101a1SKees Cook of the KDF is returned to the caller. The KDF is characterized with 868b68101a1SKees Cook struct keyctl_kdf_params as follows: 869b68101a1SKees Cook 870b68101a1SKees Cook - ``char *hashname`` specifies the NUL terminated string identifying 871b68101a1SKees Cook the hash used from the kernel crypto API and applied for the KDF 872b68101a1SKees Cook operation. The KDF implemenation complies with SP800-56A as well 873b68101a1SKees Cook as with SP800-108 (the counter KDF). 874b68101a1SKees Cook 875b68101a1SKees Cook - ``char *otherinfo`` specifies the OtherInfo data as documented in 876b68101a1SKees Cook SP800-56A section 5.8.1.2. The length of the buffer is given with 877b68101a1SKees Cook otherinfolen. The format of OtherInfo is defined by the caller. 878b68101a1SKees Cook The otherinfo pointer may be NULL if no OtherInfo shall be used. 879b68101a1SKees Cook 880b68101a1SKees Cook This function will return error EOPNOTSUPP if the key type is not 881b68101a1SKees Cook supported, error ENOKEY if the key could not be found, or error 882b68101a1SKees Cook EACCES if the key is not readable by the caller. In addition, the 883b68101a1SKees Cook function will return EMSGSIZE when the parameter kdf is non-NULL 884b68101a1SKees Cook and either the buffer length or the OtherInfo length exceeds the 885b68101a1SKees Cook allowed length. 886b68101a1SKees Cook 88700d60fd3SDavid Howells 888b68101a1SKees Cook * Restrict keyring linkage:: 889b68101a1SKees Cook 890b68101a1SKees Cook long keyctl(KEYCTL_RESTRICT_KEYRING, key_serial_t keyring, 891b68101a1SKees Cook const char *type, const char *restriction); 892b68101a1SKees Cook 893b68101a1SKees Cook An existing keyring can restrict linkage of additional keys by evaluating 894b68101a1SKees Cook the contents of the key according to a restriction scheme. 895b68101a1SKees Cook 896b68101a1SKees Cook "keyring" is the key ID for an existing keyring to apply a restriction 897b68101a1SKees Cook to. It may be empty or may already have keys linked. Existing linked keys 898b68101a1SKees Cook will remain in the keyring even if the new restriction would reject them. 899b68101a1SKees Cook 900b68101a1SKees Cook "type" is a registered key type. 901b68101a1SKees Cook 902b68101a1SKees Cook "restriction" is a string describing how key linkage is to be restricted. 903b68101a1SKees Cook The format varies depending on the key type, and the string is passed to 904b68101a1SKees Cook the lookup_restriction() function for the requested type. It may specify 905b68101a1SKees Cook a method and relevant data for the restriction such as signature 906b68101a1SKees Cook verification or constraints on key payload. If the requested key type is 907b68101a1SKees Cook later unregistered, no keys may be added to the keyring after the key type 908b68101a1SKees Cook is removed. 909b68101a1SKees Cook 910b68101a1SKees Cook To apply a keyring restriction the process must have Set Attribute 911b68101a1SKees Cook permission and the keyring must not be previously restricted. 912b68101a1SKees Cook 9137228b66aSMat Martineau One application of restricted keyrings is to verify X.509 certificate 9147228b66aSMat Martineau chains or individual certificate signatures using the asymmetric key type. 9157228b66aSMat Martineau See Documentation/crypto/asymmetric-keys.txt for specific restrictions 9167228b66aSMat Martineau applicable to the asymmetric key type. 9177228b66aSMat Martineau 9187228b66aSMat Martineau 91900d60fd3SDavid Howells * Query an asymmetric key:: 92000d60fd3SDavid Howells 92100d60fd3SDavid Howells long keyctl(KEYCTL_PKEY_QUERY, 92200d60fd3SDavid Howells key_serial_t key_id, unsigned long reserved, 92300d60fd3SDavid Howells struct keyctl_pkey_query *info); 92400d60fd3SDavid Howells 92500d60fd3SDavid Howells Get information about an asymmetric key. The information is returned in 92600d60fd3SDavid Howells the keyctl_pkey_query struct:: 92700d60fd3SDavid Howells 92800d60fd3SDavid Howells __u32 supported_ops; 92900d60fd3SDavid Howells __u32 key_size; 93000d60fd3SDavid Howells __u16 max_data_size; 93100d60fd3SDavid Howells __u16 max_sig_size; 93200d60fd3SDavid Howells __u16 max_enc_size; 93300d60fd3SDavid Howells __u16 max_dec_size; 93400d60fd3SDavid Howells __u32 __spare[10]; 93500d60fd3SDavid Howells 93600d60fd3SDavid Howells ``supported_ops`` contains a bit mask of flags indicating which ops are 93700d60fd3SDavid Howells supported. This is constructed from a bitwise-OR of:: 93800d60fd3SDavid Howells 93900d60fd3SDavid Howells KEYCTL_SUPPORTS_{ENCRYPT,DECRYPT,SIGN,VERIFY} 94000d60fd3SDavid Howells 94100d60fd3SDavid Howells ``key_size`` indicated the size of the key in bits. 94200d60fd3SDavid Howells 94300d60fd3SDavid Howells ``max_*_size`` indicate the maximum sizes in bytes of a blob of data to be 94400d60fd3SDavid Howells signed, a signature blob, a blob to be encrypted and a blob to be 94500d60fd3SDavid Howells decrypted. 94600d60fd3SDavid Howells 94700d60fd3SDavid Howells ``__spare[]`` must be set to 0. This is intended for future use to hand 94800d60fd3SDavid Howells over one or more passphrases needed unlock a key. 94900d60fd3SDavid Howells 95000d60fd3SDavid Howells If successful, 0 is returned. If the key is not an asymmetric key, 95100d60fd3SDavid Howells EOPNOTSUPP is returned. 95200d60fd3SDavid Howells 95300d60fd3SDavid Howells 95400d60fd3SDavid Howells * Encrypt, decrypt, sign or verify a blob using an asymmetric key:: 95500d60fd3SDavid Howells 95600d60fd3SDavid Howells long keyctl(KEYCTL_PKEY_ENCRYPT, 95700d60fd3SDavid Howells const struct keyctl_pkey_params *params, 95800d60fd3SDavid Howells const char *info, 95900d60fd3SDavid Howells const void *in, 96000d60fd3SDavid Howells void *out); 96100d60fd3SDavid Howells 96200d60fd3SDavid Howells long keyctl(KEYCTL_PKEY_DECRYPT, 96300d60fd3SDavid Howells const struct keyctl_pkey_params *params, 96400d60fd3SDavid Howells const char *info, 96500d60fd3SDavid Howells const void *in, 96600d60fd3SDavid Howells void *out); 96700d60fd3SDavid Howells 96800d60fd3SDavid Howells long keyctl(KEYCTL_PKEY_SIGN, 96900d60fd3SDavid Howells const struct keyctl_pkey_params *params, 97000d60fd3SDavid Howells const char *info, 97100d60fd3SDavid Howells const void *in, 97200d60fd3SDavid Howells void *out); 97300d60fd3SDavid Howells 97400d60fd3SDavid Howells long keyctl(KEYCTL_PKEY_VERIFY, 97500d60fd3SDavid Howells const struct keyctl_pkey_params *params, 97600d60fd3SDavid Howells const char *info, 97700d60fd3SDavid Howells const void *in, 97800d60fd3SDavid Howells const void *in2); 97900d60fd3SDavid Howells 98000d60fd3SDavid Howells Use an asymmetric key to perform a public-key cryptographic operation a 98100d60fd3SDavid Howells blob of data. For encryption and verification, the asymmetric key may 98200d60fd3SDavid Howells only need the public parts to be available, but for decryption and signing 98300d60fd3SDavid Howells the private parts are required also. 98400d60fd3SDavid Howells 98500d60fd3SDavid Howells The parameter block pointed to by params contains a number of integer 98600d60fd3SDavid Howells values:: 98700d60fd3SDavid Howells 98800d60fd3SDavid Howells __s32 key_id; 98900d60fd3SDavid Howells __u32 in_len; 99000d60fd3SDavid Howells __u32 out_len; 99100d60fd3SDavid Howells __u32 in2_len; 99200d60fd3SDavid Howells 99300d60fd3SDavid Howells ``key_id`` is the ID of the asymmetric key to be used. ``in_len`` and 99400d60fd3SDavid Howells ``in2_len`` indicate the amount of data in the in and in2 buffers and 99500d60fd3SDavid Howells ``out_len`` indicates the size of the out buffer as appropriate for the 99600d60fd3SDavid Howells above operations. 99700d60fd3SDavid Howells 99800d60fd3SDavid Howells For a given operation, the in and out buffers are used as follows:: 99900d60fd3SDavid Howells 100000d60fd3SDavid Howells Operation ID in,in_len out,out_len in2,in2_len 100100d60fd3SDavid Howells ======================= =============== =============== =============== 100200d60fd3SDavid Howells KEYCTL_PKEY_ENCRYPT Raw data Encrypted data - 100300d60fd3SDavid Howells KEYCTL_PKEY_DECRYPT Encrypted data Raw data - 100400d60fd3SDavid Howells KEYCTL_PKEY_SIGN Raw data Signature - 100500d60fd3SDavid Howells KEYCTL_PKEY_VERIFY Raw data - Signature 100600d60fd3SDavid Howells 100700d60fd3SDavid Howells ``info`` is a string of key=value pairs that supply supplementary 100800d60fd3SDavid Howells information. These include: 100900d60fd3SDavid Howells 101000d60fd3SDavid Howells ``enc=<encoding>`` The encoding of the encrypted/signature blob. This 101100d60fd3SDavid Howells can be "pkcs1" for RSASSA-PKCS1-v1.5 or 101200d60fd3SDavid Howells RSAES-PKCS1-v1.5; "pss" for "RSASSA-PSS"; "oaep" for 101300d60fd3SDavid Howells "RSAES-OAEP". If omitted or is "raw", the raw output 101400d60fd3SDavid Howells of the encryption function is specified. 101500d60fd3SDavid Howells 101600d60fd3SDavid Howells ``hash=<algo>`` If the data buffer contains the output of a hash 101700d60fd3SDavid Howells function and the encoding includes some indication of 101800d60fd3SDavid Howells which hash function was used, the hash function can be 101900d60fd3SDavid Howells specified with this, eg. "hash=sha256". 102000d60fd3SDavid Howells 102100d60fd3SDavid Howells The ``__spare[]`` space in the parameter block must be set to 0. This is 102200d60fd3SDavid Howells intended, amongst other things, to allow the passing of passphrases 102300d60fd3SDavid Howells required to unlock a key. 102400d60fd3SDavid Howells 102500d60fd3SDavid Howells If successful, encrypt, decrypt and sign all return the amount of data 102600d60fd3SDavid Howells written into the output buffer. Verification returns 0 on success. 102700d60fd3SDavid Howells 102800d60fd3SDavid Howells 1029f7e47677SDavid Howells * Watch a key or keyring for changes:: 1030f7e47677SDavid Howells 1031f7e47677SDavid Howells long keyctl(KEYCTL_WATCH_KEY, key_serial_t key, int queue_fd, 1032f7e47677SDavid Howells const struct watch_notification_filter *filter); 1033f7e47677SDavid Howells 1034f7e47677SDavid Howells This will set or remove a watch for changes on the specified key or 1035f7e47677SDavid Howells keyring. 1036f7e47677SDavid Howells 1037f7e47677SDavid Howells "key" is the ID of the key to be watched. 1038f7e47677SDavid Howells 1039f7e47677SDavid Howells "queue_fd" is a file descriptor referring to an open "/dev/watch_queue" 1040f7e47677SDavid Howells which manages the buffer into which notifications will be delivered. 1041f7e47677SDavid Howells 1042f7e47677SDavid Howells "filter" is either NULL to remove a watch or a filter specification to 1043f7e47677SDavid Howells indicate what events are required from the key. 1044f7e47677SDavid Howells 1045f7e47677SDavid Howells See Documentation/watch_queue.rst for more information. 1046f7e47677SDavid Howells 1047f7e47677SDavid Howells Note that only one watch may be emplaced for any particular { key, 1048f7e47677SDavid Howells queue_fd } combination. 1049f7e47677SDavid Howells 1050f7e47677SDavid Howells Notification records look like:: 1051f7e47677SDavid Howells 1052f7e47677SDavid Howells struct key_notification { 1053f7e47677SDavid Howells struct watch_notification watch; 1054f7e47677SDavid Howells __u32 key_id; 1055f7e47677SDavid Howells __u32 aux; 1056f7e47677SDavid Howells }; 1057f7e47677SDavid Howells 1058f7e47677SDavid Howells In this, watch::type will be "WATCH_TYPE_KEY_NOTIFY" and subtype will be 1059f7e47677SDavid Howells one of:: 1060f7e47677SDavid Howells 1061f7e47677SDavid Howells NOTIFY_KEY_INSTANTIATED 1062f7e47677SDavid Howells NOTIFY_KEY_UPDATED 1063f7e47677SDavid Howells NOTIFY_KEY_LINKED 1064f7e47677SDavid Howells NOTIFY_KEY_UNLINKED 1065f7e47677SDavid Howells NOTIFY_KEY_CLEARED 1066f7e47677SDavid Howells NOTIFY_KEY_REVOKED 1067f7e47677SDavid Howells NOTIFY_KEY_INVALIDATED 1068f7e47677SDavid Howells NOTIFY_KEY_SETATTR 1069f7e47677SDavid Howells 1070f7e47677SDavid Howells Where these indicate a key being instantiated/rejected, updated, a link 1071f7e47677SDavid Howells being made in a keyring, a link being removed from a keyring, a keyring 1072f7e47677SDavid Howells being cleared, a key being revoked, a key being invalidated or a key 1073f7e47677SDavid Howells having one of its attributes changed (user, group, perm, timeout, 1074f7e47677SDavid Howells restriction). 1075f7e47677SDavid Howells 1076f7e47677SDavid Howells If a watched key is deleted, a basic watch_notification will be issued 1077f7e47677SDavid Howells with "type" set to WATCH_TYPE_META and "subtype" set to 1078f7e47677SDavid Howells watch_meta_removal_notification. The watchpoint ID will be set in the 1079f7e47677SDavid Howells "info" field. 1080f7e47677SDavid Howells 1081f7e47677SDavid Howells This needs to be configured by enabling: 1082f7e47677SDavid Howells 1083f7e47677SDavid Howells "Provide key/keyring change notifications" (KEY_NOTIFICATIONS) 1084f7e47677SDavid Howells 1085f7e47677SDavid Howells 1086b68101a1SKees CookKernel Services 1087b68101a1SKees Cook=============== 1088b68101a1SKees Cook 1089b68101a1SKees CookThe kernel services for key management are fairly simple to deal with. They can 1090b68101a1SKees Cookbe broken down into two areas: keys and key types. 1091b68101a1SKees Cook 1092b68101a1SKees CookDealing with keys is fairly straightforward. Firstly, the kernel service 1093b68101a1SKees Cookregisters its type, then it searches for a key of that type. It should retain 1094b68101a1SKees Cookthe key as long as it has need of it, and then it should release it. For a 1095b68101a1SKees Cookfilesystem or device file, a search would probably be performed during the open 1096b68101a1SKees Cookcall, and the key released upon close. How to deal with conflicting keys due to 1097b68101a1SKees Cooktwo different users opening the same file is left to the filesystem author to 1098b68101a1SKees Cooksolve. 1099b68101a1SKees Cook 1100b68101a1SKees CookTo access the key manager, the following header must be #included:: 1101b68101a1SKees Cook 1102b68101a1SKees Cook <linux/key.h> 1103b68101a1SKees Cook 1104b68101a1SKees CookSpecific key types should have a header file under include/keys/ that should be 1105b68101a1SKees Cookused to access that type. For keys of type "user", for example, that would be:: 1106b68101a1SKees Cook 1107b68101a1SKees Cook <keys/user-type.h> 1108b68101a1SKees Cook 1109b68101a1SKees CookNote that there are two different types of pointers to keys that may be 1110b68101a1SKees Cookencountered: 1111b68101a1SKees Cook 1112b68101a1SKees Cook * struct key * 1113b68101a1SKees Cook 1114b68101a1SKees Cook This simply points to the key structure itself. Key structures will be at 1115b68101a1SKees Cook least four-byte aligned. 1116b68101a1SKees Cook 1117b68101a1SKees Cook * key_ref_t 1118b68101a1SKees Cook 1119b68101a1SKees Cook This is equivalent to a ``struct key *``, but the least significant bit is set 1120b68101a1SKees Cook if the caller "possesses" the key. By "possession" it is meant that the 1121b68101a1SKees Cook calling processes has a searchable link to the key from one of its 1122b68101a1SKees Cook keyrings. There are three functions for dealing with these:: 1123b68101a1SKees Cook 1124b68101a1SKees Cook key_ref_t make_key_ref(const struct key *key, bool possession); 1125b68101a1SKees Cook 1126b68101a1SKees Cook struct key *key_ref_to_ptr(const key_ref_t key_ref); 1127b68101a1SKees Cook 1128b68101a1SKees Cook bool is_key_possessed(const key_ref_t key_ref); 1129b68101a1SKees Cook 1130b68101a1SKees Cook The first function constructs a key reference from a key pointer and 1131b68101a1SKees Cook possession information (which must be true or false). 1132b68101a1SKees Cook 1133b68101a1SKees Cook The second function retrieves the key pointer from a reference and the 1134b68101a1SKees Cook third retrieves the possession flag. 1135b68101a1SKees Cook 1136b68101a1SKees CookWhen accessing a key's payload contents, certain precautions must be taken to 1137b68101a1SKees Cookprevent access vs modification races. See the section "Notes on accessing 1138b68101a1SKees Cookpayload contents" for more information. 1139b68101a1SKees Cook 1140b68101a1SKees Cook * To search for a key, call:: 1141b68101a1SKees Cook 1142b68101a1SKees Cook struct key *request_key(const struct key_type *type, 1143b68101a1SKees Cook const char *description, 1144028db3e2SLinus Torvalds const char *callout_info); 1145b68101a1SKees Cook 1146b68101a1SKees Cook This is used to request a key or keyring with a description that matches 1147b68101a1SKees Cook the description specified according to the key type's match_preparse() 1148b68101a1SKees Cook method. This permits approximate matching to occur. If callout_string is 1149b68101a1SKees Cook not NULL, then /sbin/request-key will be invoked in an attempt to obtain 1150b68101a1SKees Cook the key from userspace. In that case, callout_string will be passed as an 1151b68101a1SKees Cook argument to the program. 1152b68101a1SKees Cook 1153b68101a1SKees Cook Should the function fail error ENOKEY, EKEYEXPIRED or EKEYREVOKED will be 1154b68101a1SKees Cook returned. 1155b68101a1SKees Cook 1156b68101a1SKees Cook If successful, the key will have been attached to the default keyring for 1157b68101a1SKees Cook implicitly obtained request-key keys, as set by KEYCTL_SET_REQKEY_KEYRING. 1158b68101a1SKees Cook 1159adf31eebSJosh Holland See also Documentation/security/keys/request-key.rst. 1160b68101a1SKees Cook 1161b68101a1SKees Cook 1162402613f3SDaniel W. S. Almeida * To search for a key in a specific domain, call:: 1163a58946c1SDavid Howells 1164a58946c1SDavid Howells struct key *request_key_tag(const struct key_type *type, 1165a58946c1SDavid Howells const char *description, 1166a58946c1SDavid Howells struct key_tag *domain_tag, 1167028db3e2SLinus Torvalds const char *callout_info); 1168a58946c1SDavid Howells 1169a58946c1SDavid Howells This is identical to request_key(), except that a domain tag may be 1170a58946c1SDavid Howells specifies that causes search algorithm to only match keys matching that 1171a58946c1SDavid Howells tag. The domain_tag may be NULL, specifying a global domain that is 1172a58946c1SDavid Howells separate from any nominated domain. 1173a58946c1SDavid Howells 1174a58946c1SDavid Howells 1175b68101a1SKees Cook * To search for a key, passing auxiliary data to the upcaller, call:: 1176b68101a1SKees Cook 1177b68101a1SKees Cook struct key *request_key_with_auxdata(const struct key_type *type, 1178b68101a1SKees Cook const char *description, 1179a58946c1SDavid Howells struct key_tag *domain_tag, 1180b68101a1SKees Cook const void *callout_info, 1181b68101a1SKees Cook size_t callout_len, 1182028db3e2SLinus Torvalds void *aux); 1183b68101a1SKees Cook 1184a58946c1SDavid Howells This is identical to request_key_tag(), except that the auxiliary data is 1185a58946c1SDavid Howells passed to the key_type->request_key() op if it exists, and the 1186a58946c1SDavid Howells callout_info is a blob of length callout_len, if given (the length may be 1187a58946c1SDavid Howells 0). 1188b68101a1SKees Cook 1189b68101a1SKees Cook 1190896f1950SDavid Howells * To search for a key under RCU conditions, call:: 1191896f1950SDavid Howells 1192896f1950SDavid Howells struct key *request_key_rcu(const struct key_type *type, 1193a58946c1SDavid Howells const char *description, 1194a58946c1SDavid Howells struct key_tag *domain_tag); 1195896f1950SDavid Howells 1196a58946c1SDavid Howells which is similar to request_key_tag() except that it does not check for 1197a58946c1SDavid Howells keys that are under construction and it will not call out to userspace to 1198896f1950SDavid Howells construct a key if it can't find a match. 1199896f1950SDavid Howells 1200896f1950SDavid Howells 1201b68101a1SKees Cook * When it is no longer required, the key should be released using:: 1202b68101a1SKees Cook 1203b68101a1SKees Cook void key_put(struct key *key); 1204b68101a1SKees Cook 1205b68101a1SKees Cook Or:: 1206b68101a1SKees Cook 1207b68101a1SKees Cook void key_ref_put(key_ref_t key_ref); 1208b68101a1SKees Cook 1209b68101a1SKees Cook These can be called from interrupt context. If CONFIG_KEYS is not set then 1210b68101a1SKees Cook the argument will not be parsed. 1211b68101a1SKees Cook 1212b68101a1SKees Cook 1213b68101a1SKees Cook * Extra references can be made to a key by calling one of the following 1214b68101a1SKees Cook functions:: 1215b68101a1SKees Cook 1216b68101a1SKees Cook struct key *__key_get(struct key *key); 1217b68101a1SKees Cook struct key *key_get(struct key *key); 1218b68101a1SKees Cook 1219b68101a1SKees Cook Keys so references will need to be disposed of by calling key_put() when 1220b68101a1SKees Cook they've been finished with. The key pointer passed in will be returned. 1221b68101a1SKees Cook 1222b68101a1SKees Cook In the case of key_get(), if the pointer is NULL or CONFIG_KEYS is not set 1223b68101a1SKees Cook then the key will not be dereferenced and no increment will take place. 1224b68101a1SKees Cook 1225b68101a1SKees Cook 1226b68101a1SKees Cook * A key's serial number can be obtained by calling:: 1227b68101a1SKees Cook 1228b68101a1SKees Cook key_serial_t key_serial(struct key *key); 1229b68101a1SKees Cook 1230b68101a1SKees Cook If key is NULL or if CONFIG_KEYS is not set then 0 will be returned (in the 1231b68101a1SKees Cook latter case without parsing the argument). 1232b68101a1SKees Cook 1233b68101a1SKees Cook 1234b68101a1SKees Cook * If a keyring was found in the search, this can be further searched by:: 1235b68101a1SKees Cook 1236b68101a1SKees Cook key_ref_t keyring_search(key_ref_t keyring_ref, 1237b68101a1SKees Cook const struct key_type *type, 1238dcf49dbcSDavid Howells const char *description, 1239dcf49dbcSDavid Howells bool recurse) 1240b68101a1SKees Cook 1241dcf49dbcSDavid Howells This searches the specified keyring only (recurse == false) or keyring tree 1242dcf49dbcSDavid Howells (recurse == true) specified for a matching key. Error ENOKEY is returned 1243dcf49dbcSDavid Howells upon failure (use IS_ERR/PTR_ERR to determine). If successful, the returned 1244dcf49dbcSDavid Howells key will need to be released. 1245b68101a1SKees Cook 1246b68101a1SKees Cook The possession attribute from the keyring reference is used to control 1247b68101a1SKees Cook access through the permissions mask and is propagated to the returned key 1248b68101a1SKees Cook reference pointer if successful. 1249b68101a1SKees Cook 1250b68101a1SKees Cook 1251b68101a1SKees Cook * A keyring can be created by:: 1252b68101a1SKees Cook 1253b68101a1SKees Cook struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid, 1254b68101a1SKees Cook const struct cred *cred, 1255028db3e2SLinus Torvalds key_perm_t perm, 1256b68101a1SKees Cook struct key_restriction *restrict_link, 1257b68101a1SKees Cook unsigned long flags, 1258b68101a1SKees Cook struct key *dest); 1259b68101a1SKees Cook 1260b68101a1SKees Cook This creates a keyring with the given attributes and returns it. If dest 1261b68101a1SKees Cook is not NULL, the new keyring will be linked into the keyring to which it 1262b68101a1SKees Cook points. No permission checks are made upon the destination keyring. 1263b68101a1SKees Cook 1264b68101a1SKees Cook Error EDQUOT can be returned if the keyring would overload the quota (pass 1265b68101a1SKees Cook KEY_ALLOC_NOT_IN_QUOTA in flags if the keyring shouldn't be accounted 1266b68101a1SKees Cook towards the user's quota). Error ENOMEM can also be returned. 1267b68101a1SKees Cook 1268b68101a1SKees Cook If restrict_link is not NULL, it should point to a structure that contains 1269b68101a1SKees Cook the function that will be called each time an attempt is made to link a 1270b68101a1SKees Cook key into the new keyring. The structure may also contain a key pointer 1271b68101a1SKees Cook and an associated key type. The function is called to check whether a key 1272b68101a1SKees Cook may be added into the keyring or not. The key type is used by the garbage 1273b68101a1SKees Cook collector to clean up function or data pointers in this structure if the 1274b68101a1SKees Cook given key type is unregistered. Callers of key_create_or_update() within 1275b68101a1SKees Cook the kernel can pass KEY_ALLOC_BYPASS_RESTRICTION to suppress the check. 1276b68101a1SKees Cook An example of using this is to manage rings of cryptographic keys that are 1277b68101a1SKees Cook set up when the kernel boots where userspace is also permitted to add keys 1278b68101a1SKees Cook - provided they can be verified by a key the kernel already has. 1279b68101a1SKees Cook 1280b68101a1SKees Cook When called, the restriction function will be passed the keyring being 1281b68101a1SKees Cook added to, the key type, the payload of the key being added, and data to be 1282b68101a1SKees Cook used in the restriction check. Note that when a new key is being created, 1283b68101a1SKees Cook this is called between payload preparsing and actual key creation. The 1284b68101a1SKees Cook function should return 0 to allow the link or an error to reject it. 1285b68101a1SKees Cook 1286b68101a1SKees Cook A convenience function, restrict_link_reject, exists to always return 1287b68101a1SKees Cook -EPERM to in this case. 1288b68101a1SKees Cook 1289b68101a1SKees Cook 1290b68101a1SKees Cook * To check the validity of a key, this function can be called:: 1291b68101a1SKees Cook 1292b68101a1SKees Cook int validate_key(struct key *key); 1293b68101a1SKees Cook 1294b68101a1SKees Cook This checks that the key in question hasn't expired or and hasn't been 1295b68101a1SKees Cook revoked. Should the key be invalid, error EKEYEXPIRED or EKEYREVOKED will 1296b68101a1SKees Cook be returned. If the key is NULL or if CONFIG_KEYS is not set then 0 will be 1297b68101a1SKees Cook returned (in the latter case without parsing the argument). 1298b68101a1SKees Cook 1299b68101a1SKees Cook 1300b68101a1SKees Cook * To register a key type, the following function should be called:: 1301b68101a1SKees Cook 1302b68101a1SKees Cook int register_key_type(struct key_type *type); 1303b68101a1SKees Cook 1304b68101a1SKees Cook This will return error EEXIST if a type of the same name is already 1305b68101a1SKees Cook present. 1306b68101a1SKees Cook 1307b68101a1SKees Cook 1308b68101a1SKees Cook * To unregister a key type, call:: 1309b68101a1SKees Cook 1310b68101a1SKees Cook void unregister_key_type(struct key_type *type); 1311b68101a1SKees Cook 1312b68101a1SKees Cook 1313b68101a1SKees CookUnder some circumstances, it may be desirable to deal with a bundle of keys. 1314b68101a1SKees CookThe facility provides access to the keyring type for managing such a bundle:: 1315b68101a1SKees Cook 1316b68101a1SKees Cook struct key_type key_type_keyring; 1317b68101a1SKees Cook 1318b68101a1SKees CookThis can be used with a function such as request_key() to find a specific 1319b68101a1SKees Cookkeyring in a process's keyrings. A keyring thus found can then be searched 1320b68101a1SKees Cookwith keyring_search(). Note that it is not possible to use request_key() to 1321b68101a1SKees Cooksearch a specific keyring, so using keyrings in this way is of limited utility. 1322b68101a1SKees Cook 1323b68101a1SKees Cook 1324b68101a1SKees CookNotes On Accessing Payload Contents 1325b68101a1SKees Cook=================================== 1326b68101a1SKees Cook 1327b68101a1SKees CookThe simplest payload is just data stored in key->payload directly. In this 1328b68101a1SKees Cookcase, there's no need to indulge in RCU or locking when accessing the payload. 1329b68101a1SKees Cook 1330b68101a1SKees CookMore complex payload contents must be allocated and pointers to them set in the 1331b68101a1SKees Cookkey->payload.data[] array. One of the following ways must be selected to 1332b68101a1SKees Cookaccess the data: 1333b68101a1SKees Cook 1334b68101a1SKees Cook 1) Unmodifiable key type. 1335b68101a1SKees Cook 1336b68101a1SKees Cook If the key type does not have a modify method, then the key's payload can 1337b68101a1SKees Cook be accessed without any form of locking, provided that it's known to be 1338b68101a1SKees Cook instantiated (uninstantiated keys cannot be "found"). 1339b68101a1SKees Cook 1340b68101a1SKees Cook 2) The key's semaphore. 1341b68101a1SKees Cook 1342b68101a1SKees Cook The semaphore could be used to govern access to the payload and to control 1343b68101a1SKees Cook the payload pointer. It must be write-locked for modifications and would 1344b68101a1SKees Cook have to be read-locked for general access. The disadvantage of doing this 1345b68101a1SKees Cook is that the accessor may be required to sleep. 1346b68101a1SKees Cook 1347b68101a1SKees Cook 3) RCU. 1348b68101a1SKees Cook 1349b68101a1SKees Cook RCU must be used when the semaphore isn't already held; if the semaphore 1350b68101a1SKees Cook is held then the contents can't change under you unexpectedly as the 1351b68101a1SKees Cook semaphore must still be used to serialise modifications to the key. The 1352b68101a1SKees Cook key management code takes care of this for the key type. 1353b68101a1SKees Cook 1354b68101a1SKees Cook However, this means using:: 1355b68101a1SKees Cook 1356b68101a1SKees Cook rcu_read_lock() ... rcu_dereference() ... rcu_read_unlock() 1357b68101a1SKees Cook 1358b68101a1SKees Cook to read the pointer, and:: 1359b68101a1SKees Cook 1360b68101a1SKees Cook rcu_dereference() ... rcu_assign_pointer() ... call_rcu() 1361b68101a1SKees Cook 1362b68101a1SKees Cook to set the pointer and dispose of the old contents after a grace period. 1363b68101a1SKees Cook Note that only the key type should ever modify a key's payload. 1364b68101a1SKees Cook 1365b68101a1SKees Cook Furthermore, an RCU controlled payload must hold a struct rcu_head for the 1366b68101a1SKees Cook use of call_rcu() and, if the payload is of variable size, the length of 1367b68101a1SKees Cook the payload. key->datalen cannot be relied upon to be consistent with the 1368b68101a1SKees Cook payload just dereferenced if the key's semaphore is not held. 1369b68101a1SKees Cook 1370b68101a1SKees Cook Note that key->payload.data[0] has a shadow that is marked for __rcu 1371b68101a1SKees Cook usage. This is called key->payload.rcu_data0. The following accessors 1372b68101a1SKees Cook wrap the RCU calls to this element: 1373b68101a1SKees Cook 1374b68101a1SKees Cook a) Set or change the first payload pointer:: 1375b68101a1SKees Cook 1376b68101a1SKees Cook rcu_assign_keypointer(struct key *key, void *data); 1377b68101a1SKees Cook 1378b68101a1SKees Cook b) Read the first payload pointer with the key semaphore held:: 1379b68101a1SKees Cook 1380b68101a1SKees Cook [const] void *dereference_key_locked([const] struct key *key); 1381b68101a1SKees Cook 1382b68101a1SKees Cook Note that the return value will inherit its constness from the key 1383b68101a1SKees Cook parameter. Static analysis will give an error if it things the lock 1384b68101a1SKees Cook isn't held. 1385b68101a1SKees Cook 1386b68101a1SKees Cook c) Read the first payload pointer with the RCU read lock held:: 1387b68101a1SKees Cook 1388b68101a1SKees Cook const void *dereference_key_rcu(const struct key *key); 1389b68101a1SKees Cook 1390b68101a1SKees Cook 1391b68101a1SKees CookDefining a Key Type 1392b68101a1SKees Cook=================== 1393b68101a1SKees Cook 1394b68101a1SKees CookA kernel service may want to define its own key type. For instance, an AFS 1395b68101a1SKees Cookfilesystem might want to define a Kerberos 5 ticket key type. To do this, it 1396b68101a1SKees Cookauthor fills in a key_type struct and registers it with the system. 1397b68101a1SKees Cook 1398b68101a1SKees CookSource files that implement key types should include the following header file:: 1399b68101a1SKees Cook 1400b68101a1SKees Cook <linux/key-type.h> 1401b68101a1SKees Cook 1402b68101a1SKees CookThe structure has a number of fields, some of which are mandatory: 1403b68101a1SKees Cook 1404b68101a1SKees Cook * ``const char *name`` 1405b68101a1SKees Cook 1406b68101a1SKees Cook The name of the key type. This is used to translate a key type name 1407b68101a1SKees Cook supplied by userspace into a pointer to the structure. 1408b68101a1SKees Cook 1409b68101a1SKees Cook 1410b68101a1SKees Cook * ``size_t def_datalen`` 1411b68101a1SKees Cook 1412b68101a1SKees Cook This is optional - it supplies the default payload data length as 1413b68101a1SKees Cook contributed to the quota. If the key type's payload is always or almost 1414b68101a1SKees Cook always the same size, then this is a more efficient way to do things. 1415b68101a1SKees Cook 1416b68101a1SKees Cook The data length (and quota) on a particular key can always be changed 1417b68101a1SKees Cook during instantiation or update by calling:: 1418b68101a1SKees Cook 1419b68101a1SKees Cook int key_payload_reserve(struct key *key, size_t datalen); 1420b68101a1SKees Cook 1421b68101a1SKees Cook With the revised data length. Error EDQUOT will be returned if this is not 1422b68101a1SKees Cook viable. 1423b68101a1SKees Cook 1424b68101a1SKees Cook 1425b68101a1SKees Cook * ``int (*vet_description)(const char *description);`` 1426b68101a1SKees Cook 1427b68101a1SKees Cook This optional method is called to vet a key description. If the key type 1428b68101a1SKees Cook doesn't approve of the key description, it may return an error, otherwise 1429b68101a1SKees Cook it should return 0. 1430b68101a1SKees Cook 1431b68101a1SKees Cook 1432b68101a1SKees Cook * ``int (*preparse)(struct key_preparsed_payload *prep);`` 1433b68101a1SKees Cook 1434b68101a1SKees Cook This optional method permits the key type to attempt to parse payload 1435b68101a1SKees Cook before a key is created (add key) or the key semaphore is taken (update or 1436b68101a1SKees Cook instantiate key). The structure pointed to by prep looks like:: 1437b68101a1SKees Cook 1438b68101a1SKees Cook struct key_preparsed_payload { 1439b68101a1SKees Cook char *description; 1440b68101a1SKees Cook union key_payload payload; 1441b68101a1SKees Cook const void *data; 1442b68101a1SKees Cook size_t datalen; 1443b68101a1SKees Cook size_t quotalen; 1444b68101a1SKees Cook time_t expiry; 1445b68101a1SKees Cook }; 1446b68101a1SKees Cook 1447b68101a1SKees Cook Before calling the method, the caller will fill in data and datalen with 1448b68101a1SKees Cook the payload blob parameters; quotalen will be filled in with the default 1449b68101a1SKees Cook quota size from the key type; expiry will be set to TIME_T_MAX and the 1450b68101a1SKees Cook rest will be cleared. 1451b68101a1SKees Cook 1452b68101a1SKees Cook If a description can be proposed from the payload contents, that should be 1453b68101a1SKees Cook attached as a string to the description field. This will be used for the 1454b68101a1SKees Cook key description if the caller of add_key() passes NULL or "". 1455b68101a1SKees Cook 1456b68101a1SKees Cook The method can attach anything it likes to payload. This is merely passed 1457b68101a1SKees Cook along to the instantiate() or update() operations. If set, the expiry 1458b68101a1SKees Cook time will be applied to the key if it is instantiated from this data. 1459b68101a1SKees Cook 1460b68101a1SKees Cook The method should return 0 if successful or a negative error code 1461b68101a1SKees Cook otherwise. 1462b68101a1SKees Cook 1463b68101a1SKees Cook 1464b68101a1SKees Cook * ``void (*free_preparse)(struct key_preparsed_payload *prep);`` 1465b68101a1SKees Cook 1466b68101a1SKees Cook This method is only required if the preparse() method is provided, 1467b68101a1SKees Cook otherwise it is unused. It cleans up anything attached to the description 1468b68101a1SKees Cook and payload fields of the key_preparsed_payload struct as filled in by the 1469b68101a1SKees Cook preparse() method. It will always be called after preparse() returns 1470b68101a1SKees Cook successfully, even if instantiate() or update() succeed. 1471b68101a1SKees Cook 1472b68101a1SKees Cook 1473b68101a1SKees Cook * ``int (*instantiate)(struct key *key, struct key_preparsed_payload *prep);`` 1474b68101a1SKees Cook 1475b68101a1SKees Cook This method is called to attach a payload to a key during construction. 1476b68101a1SKees Cook The payload attached need not bear any relation to the data passed to this 1477b68101a1SKees Cook function. 1478b68101a1SKees Cook 1479b68101a1SKees Cook The prep->data and prep->datalen fields will define the original payload 1480b68101a1SKees Cook blob. If preparse() was supplied then other fields may be filled in also. 1481b68101a1SKees Cook 1482b68101a1SKees Cook If the amount of data attached to the key differs from the size in 1483b68101a1SKees Cook keytype->def_datalen, then key_payload_reserve() should be called. 1484b68101a1SKees Cook 1485b68101a1SKees Cook This method does not have to lock the key in order to attach a payload. 1486b68101a1SKees Cook The fact that KEY_FLAG_INSTANTIATED is not set in key->flags prevents 1487b68101a1SKees Cook anything else from gaining access to the key. 1488b68101a1SKees Cook 1489b68101a1SKees Cook It is safe to sleep in this method. 1490b68101a1SKees Cook 1491b68101a1SKees Cook generic_key_instantiate() is provided to simply copy the data from 1492b68101a1SKees Cook prep->payload.data[] to key->payload.data[], with RCU-safe assignment on 1493b68101a1SKees Cook the first element. It will then clear prep->payload.data[] so that the 1494b68101a1SKees Cook free_preparse method doesn't release the data. 1495b68101a1SKees Cook 1496b68101a1SKees Cook 1497b68101a1SKees Cook * ``int (*update)(struct key *key, const void *data, size_t datalen);`` 1498b68101a1SKees Cook 1499b68101a1SKees Cook If this type of key can be updated, then this method should be provided. 1500b68101a1SKees Cook It is called to update a key's payload from the blob of data provided. 1501b68101a1SKees Cook 1502b68101a1SKees Cook The prep->data and prep->datalen fields will define the original payload 1503b68101a1SKees Cook blob. If preparse() was supplied then other fields may be filled in also. 1504b68101a1SKees Cook 1505b68101a1SKees Cook key_payload_reserve() should be called if the data length might change 1506b68101a1SKees Cook before any changes are actually made. Note that if this succeeds, the type 1507b68101a1SKees Cook is committed to changing the key because it's already been altered, so all 1508b68101a1SKees Cook memory allocation must be done first. 1509b68101a1SKees Cook 1510b68101a1SKees Cook The key will have its semaphore write-locked before this method is called, 1511b68101a1SKees Cook but this only deters other writers; any changes to the key's payload must 1512b68101a1SKees Cook be made under RCU conditions, and call_rcu() must be used to dispose of 1513b68101a1SKees Cook the old payload. 1514b68101a1SKees Cook 1515b68101a1SKees Cook key_payload_reserve() should be called before the changes are made, but 1516b68101a1SKees Cook after all allocations and other potentially failing function calls are 1517b68101a1SKees Cook made. 1518b68101a1SKees Cook 1519b68101a1SKees Cook It is safe to sleep in this method. 1520b68101a1SKees Cook 1521b68101a1SKees Cook 1522b68101a1SKees Cook * ``int (*match_preparse)(struct key_match_data *match_data);`` 1523b68101a1SKees Cook 1524b68101a1SKees Cook This method is optional. It is called when a key search is about to be 1525b68101a1SKees Cook performed. It is given the following structure:: 1526b68101a1SKees Cook 1527b68101a1SKees Cook struct key_match_data { 1528b68101a1SKees Cook bool (*cmp)(const struct key *key, 1529b68101a1SKees Cook const struct key_match_data *match_data); 1530b68101a1SKees Cook const void *raw_data; 1531b68101a1SKees Cook void *preparsed; 1532b68101a1SKees Cook unsigned lookup_type; 1533b68101a1SKees Cook }; 1534b68101a1SKees Cook 1535b68101a1SKees Cook On entry, raw_data will be pointing to the criteria to be used in matching 1536b68101a1SKees Cook a key by the caller and should not be modified. ``(*cmp)()`` will be pointing 1537b68101a1SKees Cook to the default matcher function (which does an exact description match 1538b68101a1SKees Cook against raw_data) and lookup_type will be set to indicate a direct lookup. 1539b68101a1SKees Cook 1540b68101a1SKees Cook The following lookup_type values are available: 1541b68101a1SKees Cook 1542b68101a1SKees Cook * KEYRING_SEARCH_LOOKUP_DIRECT - A direct lookup hashes the type and 1543b68101a1SKees Cook description to narrow down the search to a small number of keys. 1544b68101a1SKees Cook 1545b68101a1SKees Cook * KEYRING_SEARCH_LOOKUP_ITERATE - An iterative lookup walks all the 1546b68101a1SKees Cook keys in the keyring until one is matched. This must be used for any 1547b68101a1SKees Cook search that's not doing a simple direct match on the key description. 1548b68101a1SKees Cook 1549b68101a1SKees Cook The method may set cmp to point to a function of its choice that does some 1550b68101a1SKees Cook other form of match, may set lookup_type to KEYRING_SEARCH_LOOKUP_ITERATE 1551b68101a1SKees Cook and may attach something to the preparsed pointer for use by ``(*cmp)()``. 1552b68101a1SKees Cook ``(*cmp)()`` should return true if a key matches and false otherwise. 1553b68101a1SKees Cook 1554b68101a1SKees Cook If preparsed is set, it may be necessary to use the match_free() method to 1555b68101a1SKees Cook clean it up. 1556b68101a1SKees Cook 1557b68101a1SKees Cook The method should return 0 if successful or a negative error code 1558b68101a1SKees Cook otherwise. 1559b68101a1SKees Cook 1560b68101a1SKees Cook It is permitted to sleep in this method, but ``(*cmp)()`` may not sleep as 1561b68101a1SKees Cook locks will be held over it. 1562b68101a1SKees Cook 1563b68101a1SKees Cook If match_preparse() is not provided, keys of this type will be matched 1564b68101a1SKees Cook exactly by their description. 1565b68101a1SKees Cook 1566b68101a1SKees Cook 1567b68101a1SKees Cook * ``void (*match_free)(struct key_match_data *match_data);`` 1568b68101a1SKees Cook 1569b68101a1SKees Cook This method is optional. If given, it called to clean up 1570b68101a1SKees Cook match_data->preparsed after a successful call to match_preparse(). 1571b68101a1SKees Cook 1572b68101a1SKees Cook 1573b68101a1SKees Cook * ``void (*revoke)(struct key *key);`` 1574b68101a1SKees Cook 1575b68101a1SKees Cook This method is optional. It is called to discard part of the payload 1576b68101a1SKees Cook data upon a key being revoked. The caller will have the key semaphore 1577b68101a1SKees Cook write-locked. 1578b68101a1SKees Cook 1579b68101a1SKees Cook It is safe to sleep in this method, though care should be taken to avoid 1580b68101a1SKees Cook a deadlock against the key semaphore. 1581b68101a1SKees Cook 1582b68101a1SKees Cook 1583b68101a1SKees Cook * ``void (*destroy)(struct key *key);`` 1584b68101a1SKees Cook 1585b68101a1SKees Cook This method is optional. It is called to discard the payload data on a key 1586b68101a1SKees Cook when it is being destroyed. 1587b68101a1SKees Cook 1588b68101a1SKees Cook This method does not need to lock the key to access the payload; it can 1589b68101a1SKees Cook consider the key as being inaccessible at this time. Note that the key's 1590b68101a1SKees Cook type may have been changed before this function is called. 1591b68101a1SKees Cook 1592b68101a1SKees Cook It is not safe to sleep in this method; the caller may hold spinlocks. 1593b68101a1SKees Cook 1594b68101a1SKees Cook 1595b68101a1SKees Cook * ``void (*describe)(const struct key *key, struct seq_file *p);`` 1596b68101a1SKees Cook 1597b68101a1SKees Cook This method is optional. It is called during /proc/keys reading to 1598b68101a1SKees Cook summarise a key's description and payload in text form. 1599b68101a1SKees Cook 1600b68101a1SKees Cook This method will be called with the RCU read lock held. rcu_dereference() 1601b68101a1SKees Cook should be used to read the payload pointer if the payload is to be 1602b68101a1SKees Cook accessed. key->datalen cannot be trusted to stay consistent with the 1603b68101a1SKees Cook contents of the payload. 1604b68101a1SKees Cook 1605b68101a1SKees Cook The description will not change, though the key's state may. 1606b68101a1SKees Cook 1607b68101a1SKees Cook It is not safe to sleep in this method; the RCU read lock is held by the 1608b68101a1SKees Cook caller. 1609b68101a1SKees Cook 1610b68101a1SKees Cook 1611b68101a1SKees Cook * ``long (*read)(const struct key *key, char __user *buffer, size_t buflen);`` 1612b68101a1SKees Cook 1613b68101a1SKees Cook This method is optional. It is called by KEYCTL_READ to translate the 1614b68101a1SKees Cook key's payload into something a blob of data for userspace to deal with. 1615b68101a1SKees Cook Ideally, the blob should be in the same format as that passed in to the 1616b68101a1SKees Cook instantiate and update methods. 1617b68101a1SKees Cook 1618b68101a1SKees Cook If successful, the blob size that could be produced should be returned 1619b68101a1SKees Cook rather than the size copied. 1620b68101a1SKees Cook 1621b68101a1SKees Cook This method will be called with the key's semaphore read-locked. This will 1622b68101a1SKees Cook prevent the key's payload changing. It is not necessary to use RCU locking 1623b68101a1SKees Cook when accessing the key's payload. It is safe to sleep in this method, such 1624b68101a1SKees Cook as might happen when the userspace buffer is accessed. 1625b68101a1SKees Cook 1626b68101a1SKees Cook 1627b68101a1SKees Cook * ``int (*request_key)(struct key_construction *cons, const char *op, void *aux);`` 1628b68101a1SKees Cook 1629b68101a1SKees Cook This method is optional. If provided, request_key() and friends will 1630b68101a1SKees Cook invoke this function rather than upcalling to /sbin/request-key to operate 1631b68101a1SKees Cook upon a key of this type. 1632b68101a1SKees Cook 1633b68101a1SKees Cook The aux parameter is as passed to request_key_async_with_auxdata() and 1634b68101a1SKees Cook similar or is NULL otherwise. Also passed are the construction record for 1635b68101a1SKees Cook the key to be operated upon and the operation type (currently only 1636b68101a1SKees Cook "create"). 1637b68101a1SKees Cook 1638b68101a1SKees Cook This method is permitted to return before the upcall is complete, but the 1639b68101a1SKees Cook following function must be called under all circumstances to complete the 1640b68101a1SKees Cook instantiation process, whether or not it succeeds, whether or not there's 1641b68101a1SKees Cook an error:: 1642b68101a1SKees Cook 1643b68101a1SKees Cook void complete_request_key(struct key_construction *cons, int error); 1644b68101a1SKees Cook 1645b68101a1SKees Cook The error parameter should be 0 on success, -ve on error. The 1646b68101a1SKees Cook construction record is destroyed by this action and the authorisation key 1647b68101a1SKees Cook will be revoked. If an error is indicated, the key under construction 1648b68101a1SKees Cook will be negatively instantiated if it wasn't already instantiated. 1649b68101a1SKees Cook 1650b68101a1SKees Cook If this method returns an error, that error will be returned to the 1651b68101a1SKees Cook caller of request_key*(). complete_request_key() must be called prior to 1652b68101a1SKees Cook returning. 1653b68101a1SKees Cook 1654b68101a1SKees Cook The key under construction and the authorisation key can be found in the 1655b68101a1SKees Cook key_construction struct pointed to by cons: 1656b68101a1SKees Cook 1657b68101a1SKees Cook * ``struct key *key;`` 1658b68101a1SKees Cook 1659b68101a1SKees Cook The key under construction. 1660b68101a1SKees Cook 1661b68101a1SKees Cook * ``struct key *authkey;`` 1662b68101a1SKees Cook 1663b68101a1SKees Cook The authorisation key. 1664b68101a1SKees Cook 1665b68101a1SKees Cook 1666b68101a1SKees Cook * ``struct key_restriction *(*lookup_restriction)(const char *params);`` 1667b68101a1SKees Cook 1668b68101a1SKees Cook This optional method is used to enable userspace configuration of keyring 1669b68101a1SKees Cook restrictions. The restriction parameter string (not including the key type 1670b68101a1SKees Cook name) is passed in, and this method returns a pointer to a key_restriction 1671b68101a1SKees Cook structure containing the relevant functions and data to evaluate each 1672b68101a1SKees Cook attempted key link operation. If there is no match, -EINVAL is returned. 1673b68101a1SKees Cook 1674b68101a1SKees Cook 167543415f13SMauro Carvalho Chehab * ``asym_eds_op`` and ``asym_verify_signature``:: 167643415f13SMauro Carvalho Chehab 167743415f13SMauro Carvalho Chehab int (*asym_eds_op)(struct kernel_pkey_params *params, 167843415f13SMauro Carvalho Chehab const void *in, void *out); 167943415f13SMauro Carvalho Chehab int (*asym_verify_signature)(struct kernel_pkey_params *params, 168043415f13SMauro Carvalho Chehab const void *in, const void *in2); 168170025f84SDavid Howells 168270025f84SDavid Howells These methods are optional. If provided the first allows a key to be 168370025f84SDavid Howells used to encrypt, decrypt or sign a blob of data, and the second allows a 168470025f84SDavid Howells key to verify a signature. 168570025f84SDavid Howells 168670025f84SDavid Howells In all cases, the following information is provided in the params block:: 168770025f84SDavid Howells 168870025f84SDavid Howells struct kernel_pkey_params { 168970025f84SDavid Howells struct key *key; 169070025f84SDavid Howells const char *encoding; 169170025f84SDavid Howells const char *hash_algo; 169270025f84SDavid Howells char *info; 169370025f84SDavid Howells __u32 in_len; 169470025f84SDavid Howells union { 169570025f84SDavid Howells __u32 out_len; 169670025f84SDavid Howells __u32 in2_len; 169770025f84SDavid Howells }; 169870025f84SDavid Howells enum kernel_pkey_operation op : 8; 169970025f84SDavid Howells }; 170070025f84SDavid Howells 170170025f84SDavid Howells This includes the key to be used; a string indicating the encoding to use 170270025f84SDavid Howells (for instance, "pkcs1" may be used with an RSA key to indicate 170370025f84SDavid Howells RSASSA-PKCS1-v1.5 or RSAES-PKCS1-v1.5 encoding or "raw" if no encoding); 170470025f84SDavid Howells the name of the hash algorithm used to generate the data for a signature 170570025f84SDavid Howells (if appropriate); the sizes of the input and output (or second input) 170670025f84SDavid Howells buffers; and the ID of the operation to be performed. 170770025f84SDavid Howells 170870025f84SDavid Howells For a given operation ID, the input and output buffers are used as 170970025f84SDavid Howells follows:: 171070025f84SDavid Howells 171170025f84SDavid Howells Operation ID in,in_len out,out_len in2,in2_len 171270025f84SDavid Howells ======================= =============== =============== =============== 171370025f84SDavid Howells kernel_pkey_encrypt Raw data Encrypted data - 171470025f84SDavid Howells kernel_pkey_decrypt Encrypted data Raw data - 171570025f84SDavid Howells kernel_pkey_sign Raw data Signature - 171670025f84SDavid Howells kernel_pkey_verify Raw data - Signature 171770025f84SDavid Howells 171870025f84SDavid Howells asym_eds_op() deals with encryption, decryption and signature creation as 171970025f84SDavid Howells specified by params->op. Note that params->op is also set for 172070025f84SDavid Howells asym_verify_signature(). 172170025f84SDavid Howells 172270025f84SDavid Howells Encrypting and signature creation both take raw data in the input buffer 172370025f84SDavid Howells and return the encrypted result in the output buffer. Padding may have 172470025f84SDavid Howells been added if an encoding was set. In the case of signature creation, 172570025f84SDavid Howells depending on the encoding, the padding created may need to indicate the 172670025f84SDavid Howells digest algorithm - the name of which should be supplied in hash_algo. 172770025f84SDavid Howells 172870025f84SDavid Howells Decryption takes encrypted data in the input buffer and returns the raw 172970025f84SDavid Howells data in the output buffer. Padding will get checked and stripped off if 173070025f84SDavid Howells an encoding was set. 173170025f84SDavid Howells 173270025f84SDavid Howells Verification takes raw data in the input buffer and the signature in the 173370025f84SDavid Howells second input buffer and checks that the one matches the other. Padding 173470025f84SDavid Howells will be validated. Depending on the encoding, the digest algorithm used 173570025f84SDavid Howells to generate the raw data may need to be indicated in hash_algo. 173670025f84SDavid Howells 173770025f84SDavid Howells If successful, asym_eds_op() should return the number of bytes written 173870025f84SDavid Howells into the output buffer. asym_verify_signature() should return 0. 173970025f84SDavid Howells 174070025f84SDavid Howells A variety of errors may be returned, including EOPNOTSUPP if the operation 174170025f84SDavid Howells is not supported; EKEYREJECTED if verification fails; ENOPKG if the 174270025f84SDavid Howells required crypto isn't available. 174370025f84SDavid Howells 174470025f84SDavid Howells 174543415f13SMauro Carvalho Chehab * ``asym_query``:: 174643415f13SMauro Carvalho Chehab 174743415f13SMauro Carvalho Chehab int (*asym_query)(const struct kernel_pkey_params *params, 174843415f13SMauro Carvalho Chehab struct kernel_pkey_query *info); 174970025f84SDavid Howells 175070025f84SDavid Howells This method is optional. If provided it allows information about the 175170025f84SDavid Howells public or asymmetric key held in the key to be determined. 175270025f84SDavid Howells 175370025f84SDavid Howells The parameter block is as for asym_eds_op() and co. but in_len and out_len 175470025f84SDavid Howells are unused. The encoding and hash_algo fields should be used to reduce 175570025f84SDavid Howells the returned buffer/data sizes as appropriate. 175670025f84SDavid Howells 175770025f84SDavid Howells If successful, the following information is filled in:: 175870025f84SDavid Howells 175970025f84SDavid Howells struct kernel_pkey_query { 176070025f84SDavid Howells __u32 supported_ops; 176170025f84SDavid Howells __u32 key_size; 176270025f84SDavid Howells __u16 max_data_size; 176370025f84SDavid Howells __u16 max_sig_size; 176470025f84SDavid Howells __u16 max_enc_size; 176570025f84SDavid Howells __u16 max_dec_size; 176670025f84SDavid Howells }; 176770025f84SDavid Howells 176870025f84SDavid Howells The supported_ops field will contain a bitmask indicating what operations 176970025f84SDavid Howells are supported by the key, including encryption of a blob, decryption of a 177070025f84SDavid Howells blob, signing a blob and verifying the signature on a blob. The following 177170025f84SDavid Howells constants are defined for this:: 177270025f84SDavid Howells 177370025f84SDavid Howells KEYCTL_SUPPORTS_{ENCRYPT,DECRYPT,SIGN,VERIFY} 177470025f84SDavid Howells 177570025f84SDavid Howells The key_size field is the size of the key in bits. max_data_size and 177670025f84SDavid Howells max_sig_size are the maximum raw data and signature sizes for creation and 177770025f84SDavid Howells verification of a signature; max_enc_size and max_dec_size are the maximum 177870025f84SDavid Howells raw data and signature sizes for encryption and decryption. The 177970025f84SDavid Howells max_*_size fields are measured in bytes. 178070025f84SDavid Howells 178170025f84SDavid Howells If successful, 0 will be returned. If the key doesn't support this, 178270025f84SDavid Howells EOPNOTSUPP will be returned. 178370025f84SDavid Howells 178470025f84SDavid Howells 1785b68101a1SKees CookRequest-Key Callback Service 1786b68101a1SKees Cook============================ 1787b68101a1SKees Cook 1788b68101a1SKees CookTo create a new key, the kernel will attempt to execute the following command 1789b68101a1SKees Cookline:: 1790b68101a1SKees Cook 1791b68101a1SKees Cook /sbin/request-key create <key> <uid> <gid> \ 1792b68101a1SKees Cook <threadring> <processring> <sessionring> <callout_info> 1793b68101a1SKees Cook 1794b68101a1SKees Cook<key> is the key being constructed, and the three keyrings are the process 1795b68101a1SKees Cookkeyrings from the process that caused the search to be issued. These are 1796b68101a1SKees Cookincluded for two reasons: 1797b68101a1SKees Cook 1798b68101a1SKees Cook 1 There may be an authentication token in one of the keyrings that is 1799b68101a1SKees Cook required to obtain the key, eg: a Kerberos Ticket-Granting Ticket. 1800b68101a1SKees Cook 1801b68101a1SKees Cook 2 The new key should probably be cached in one of these rings. 1802b68101a1SKees Cook 1803b68101a1SKees CookThis program should set it UID and GID to those specified before attempting to 1804b68101a1SKees Cookaccess any more keys. It may then look around for a user specific process to 1805b68101a1SKees Cookhand the request off to (perhaps a path held in placed in another key by, for 1806b68101a1SKees Cookexample, the KDE desktop manager). 1807b68101a1SKees Cook 1808b68101a1SKees CookThe program (or whatever it calls) should finish construction of the key by 1809b68101a1SKees Cookcalling KEYCTL_INSTANTIATE or KEYCTL_INSTANTIATE_IOV, which also permits it to 1810b68101a1SKees Cookcache the key in one of the keyrings (probably the session ring) before 1811b68101a1SKees Cookreturning. Alternatively, the key can be marked as negative with KEYCTL_NEGATE 1812b68101a1SKees Cookor KEYCTL_REJECT; this also permits the key to be cached in one of the 1813b68101a1SKees Cookkeyrings. 1814b68101a1SKees Cook 1815b68101a1SKees CookIf it returns with the key remaining in the unconstructed state, the key will 1816b68101a1SKees Cookbe marked as being negative, it will be added to the session keyring, and an 1817b68101a1SKees Cookerror will be returned to the key requestor. 1818b68101a1SKees Cook 1819b68101a1SKees CookSupplementary information may be provided from whoever or whatever invoked this 1820b68101a1SKees Cookservice. This will be passed as the <callout_info> parameter. If no such 1821b68101a1SKees Cookinformation was made available, then "-" will be passed as this parameter 1822b68101a1SKees Cookinstead. 1823b68101a1SKees Cook 1824b68101a1SKees Cook 1825b68101a1SKees CookSimilarly, the kernel may attempt to update an expired or a soon to expire key 1826b68101a1SKees Cookby executing:: 1827b68101a1SKees Cook 1828b68101a1SKees Cook /sbin/request-key update <key> <uid> <gid> \ 1829b68101a1SKees Cook <threadring> <processring> <sessionring> 1830b68101a1SKees Cook 1831b68101a1SKees CookIn this case, the program isn't required to actually attach the key to a ring; 1832b68101a1SKees Cookthe rings are provided for reference. 1833b68101a1SKees Cook 1834b68101a1SKees Cook 1835b68101a1SKees CookGarbage Collection 1836b68101a1SKees Cook================== 1837b68101a1SKees Cook 1838b68101a1SKees CookDead keys (for which the type has been removed) will be automatically unlinked 1839b68101a1SKees Cookfrom those keyrings that point to them and deleted as soon as possible by a 1840b68101a1SKees Cookbackground garbage collector. 1841b68101a1SKees Cook 1842b68101a1SKees CookSimilarly, revoked and expired keys will be garbage collected, but only after a 1843b68101a1SKees Cookcertain amount of time has passed. This time is set as a number of seconds in:: 1844b68101a1SKees Cook 1845b68101a1SKees Cook /proc/sys/kernel/keys/gc_delay 1846