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