1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* Authentication token and access key management 3 * 4 * Copyright (C) 2004, 2007 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 * 7 * See Documentation/security/keys/core.rst for information on keys/keyrings. 8 */ 9 10 #ifndef _LINUX_KEY_H 11 #define _LINUX_KEY_H 12 13 #include <linux/types.h> 14 #include <linux/list.h> 15 #include <linux/rbtree.h> 16 #include <linux/rcupdate.h> 17 #include <linux/sysctl.h> 18 #include <linux/rwsem.h> 19 #include <linux/atomic.h> 20 #include <linux/assoc_array.h> 21 #include <linux/refcount.h> 22 #include <linux/time64.h> 23 24 #ifdef __KERNEL__ 25 #include <linux/uidgid.h> 26 27 /* key handle serial number */ 28 typedef int32_t key_serial_t; 29 30 /* key handle permissions mask */ 31 typedef uint32_t key_perm_t; 32 33 struct key; 34 struct net; 35 36 #ifdef CONFIG_KEYS 37 38 #undef KEY_DEBUGGING 39 40 /* Maximum length of a key description, including the trailing NUL */ 41 #define KEY_MAX_DESC_SIZE 4096 42 43 #define KEY_POS_VIEW 0x01000000 /* possessor can view a key's attributes */ 44 #define KEY_POS_READ 0x02000000 /* possessor can read key payload / view keyring */ 45 #define KEY_POS_WRITE 0x04000000 /* possessor can update key payload / add link to keyring */ 46 #define KEY_POS_SEARCH 0x08000000 /* possessor can find a key in search / search a keyring */ 47 #define KEY_POS_LINK 0x10000000 /* possessor can create a link to a key/keyring */ 48 #define KEY_POS_SETATTR 0x20000000 /* possessor can set key attributes */ 49 #define KEY_POS_ALL 0x3f000000 50 51 #define KEY_USR_VIEW 0x00010000 /* user permissions... */ 52 #define KEY_USR_READ 0x00020000 53 #define KEY_USR_WRITE 0x00040000 54 #define KEY_USR_SEARCH 0x00080000 55 #define KEY_USR_LINK 0x00100000 56 #define KEY_USR_SETATTR 0x00200000 57 #define KEY_USR_ALL 0x003f0000 58 59 #define KEY_GRP_VIEW 0x00000100 /* group permissions... */ 60 #define KEY_GRP_READ 0x00000200 61 #define KEY_GRP_WRITE 0x00000400 62 #define KEY_GRP_SEARCH 0x00000800 63 #define KEY_GRP_LINK 0x00001000 64 #define KEY_GRP_SETATTR 0x00002000 65 #define KEY_GRP_ALL 0x00003f00 66 67 #define KEY_OTH_VIEW 0x00000001 /* third party permissions... */ 68 #define KEY_OTH_READ 0x00000002 69 #define KEY_OTH_WRITE 0x00000004 70 #define KEY_OTH_SEARCH 0x00000008 71 #define KEY_OTH_LINK 0x00000010 72 #define KEY_OTH_SETATTR 0x00000020 73 #define KEY_OTH_ALL 0x0000003f 74 75 #define KEY_PERM_UNDEF 0xffffffff 76 77 /* 78 * The permissions required on a key that we're looking up. 79 */ 80 enum key_need_perm { 81 KEY_NEED_UNSPECIFIED, /* Needed permission unspecified */ 82 KEY_NEED_VIEW, /* Require permission to view attributes */ 83 KEY_NEED_READ, /* Require permission to read content */ 84 KEY_NEED_WRITE, /* Require permission to update / modify */ 85 KEY_NEED_SEARCH, /* Require permission to search (keyring) or find (key) */ 86 KEY_NEED_LINK, /* Require permission to link */ 87 KEY_NEED_SETATTR, /* Require permission to change attributes */ 88 KEY_NEED_UNLINK, /* Require permission to unlink key */ 89 KEY_SYSADMIN_OVERRIDE, /* Special: override by CAP_SYS_ADMIN */ 90 KEY_AUTHTOKEN_OVERRIDE, /* Special: override by possession of auth token */ 91 KEY_DEFER_PERM_CHECK, /* Special: permission check is deferred */ 92 }; 93 94 enum key_lookup_flag { 95 KEY_LOOKUP_CREATE = 0x01, 96 KEY_LOOKUP_PARTIAL = 0x02, 97 KEY_LOOKUP_ALL = (KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL), 98 }; 99 100 struct seq_file; 101 struct user_struct; 102 struct signal_struct; 103 struct cred; 104 105 struct key_type; 106 struct key_owner; 107 struct key_tag; 108 struct keyring_list; 109 struct keyring_name; 110 111 struct key_tag { 112 struct rcu_head rcu; 113 atomic64_t index_id; /* Opaque keyring index ID */ 114 refcount_t usage; 115 bool removed; /* T when subject removed */ 116 }; 117 118 struct keyring_index_key { 119 /* [!] If this structure is altered, the union in struct key must change too! */ 120 unsigned long hash; /* Hash value */ 121 union { 122 struct { 123 #ifdef __LITTLE_ENDIAN /* Put desc_len at the LSB of x */ 124 u16 desc_len; 125 char desc[sizeof(long) - 2]; /* First few chars of description */ 126 #else 127 char desc[sizeof(long) - 2]; /* First few chars of description */ 128 u16 desc_len; 129 #endif 130 }; 131 unsigned long x; 132 }; 133 struct key_type *type; 134 struct key_tag *domain_tag; /* Domain of operation */ 135 const char *description; 136 u64 type_id; /* Cached index IDs */ 137 u64 domain_id; 138 }; 139 140 union key_payload { 141 void __rcu *rcu_data0; 142 void *data[4]; 143 }; 144 145 /*****************************************************************************/ 146 /* 147 * key reference with possession attribute handling 148 * 149 * NOTE! key_ref_t is a typedef'd pointer to a type that is not actually 150 * defined. This is because we abuse the bottom bit of the reference to carry a 151 * flag to indicate whether the calling process possesses that key in one of 152 * its keyrings. 153 * 154 * the key_ref_t has been made a separate type so that the compiler can reject 155 * attempts to dereference it without proper conversion. 156 * 157 * the three functions are used to assemble and disassemble references 158 */ 159 typedef struct __key_reference_with_attributes *key_ref_t; 160 161 static inline key_ref_t make_key_ref(const struct key *key, 162 bool possession) 163 { 164 return (key_ref_t) ((unsigned long) key | possession); 165 } 166 167 static inline struct key *key_ref_to_ptr(const key_ref_t key_ref) 168 { 169 return (struct key *) ((unsigned long) key_ref & ~1UL); 170 } 171 172 static inline bool is_key_possessed(const key_ref_t key_ref) 173 { 174 return (unsigned long) key_ref & 1UL; 175 } 176 177 typedef int (*key_restrict_link_func_t)(struct key *dest_keyring, 178 const struct key_type *type, 179 const union key_payload *payload, 180 struct key *restriction_key); 181 182 struct key_restriction { 183 key_restrict_link_func_t check; 184 struct key *key; 185 struct key_type *keytype; 186 }; 187 188 enum key_state { 189 KEY_IS_UNINSTANTIATED, 190 KEY_IS_POSITIVE, /* Positively instantiated */ 191 }; 192 193 /*****************************************************************************/ 194 /* 195 * authentication token / access credential / keyring 196 * - types of key include: 197 * - keyrings 198 * - disk encryption IDs 199 * - Kerberos TGTs and tickets 200 */ 201 struct key { 202 refcount_t usage; /* number of references */ 203 key_serial_t serial; /* key serial number */ 204 union { 205 struct list_head graveyard_link; 206 struct rb_node serial_node; 207 }; 208 #ifdef CONFIG_KEY_NOTIFICATIONS 209 struct watch_list *watchers; /* Entities watching this key for changes */ 210 #endif 211 struct rw_semaphore sem; /* change vs change sem */ 212 struct key_user *user; /* owner of this key */ 213 void *security; /* security data for this key */ 214 union { 215 time64_t expiry; /* time at which key expires (or 0) */ 216 time64_t revoked_at; /* time at which key was revoked */ 217 }; 218 time64_t last_used_at; /* last time used for LRU keyring discard */ 219 kuid_t uid; 220 kgid_t gid; 221 key_perm_t perm; /* access permissions */ 222 unsigned int quotalen; /* length added to quota */ 223 unsigned int datalen; /* payload data length 224 * - may not match RCU dereferenced payload 225 * - payload should contain own length 226 */ 227 short state; /* Key state (+) or rejection error (-) */ 228 229 #ifdef KEY_DEBUGGING 230 unsigned magic; 231 #define KEY_DEBUG_MAGIC 0x18273645u 232 #endif 233 234 unsigned long flags; /* status flags (change with bitops) */ 235 #define KEY_FLAG_DEAD 0 /* set if key type has been deleted */ 236 #define KEY_FLAG_REVOKED 1 /* set if key had been revoked */ 237 #define KEY_FLAG_IN_QUOTA 2 /* set if key consumes quota */ 238 #define KEY_FLAG_USER_CONSTRUCT 3 /* set if key is being constructed in userspace */ 239 #define KEY_FLAG_ROOT_CAN_CLEAR 4 /* set if key can be cleared by root without permission */ 240 #define KEY_FLAG_INVALIDATED 5 /* set if key has been invalidated */ 241 #define KEY_FLAG_BUILTIN 6 /* set if key is built in to the kernel */ 242 #define KEY_FLAG_ROOT_CAN_INVAL 7 /* set if key can be invalidated by root without permission */ 243 #define KEY_FLAG_KEEP 8 /* set if key should not be removed */ 244 #define KEY_FLAG_UID_KEYRING 9 /* set if key is a user or user session keyring */ 245 #define KEY_FLAG_USER_ALIVE 10 /* set if final put has not happened on key yet */ 246 247 /* the key type and key description string 248 * - the desc is used to match a key against search criteria 249 * - it should be a printable string 250 * - eg: for krb5 AFS, this might be "afs@REDHAT.COM" 251 */ 252 union { 253 struct keyring_index_key index_key; 254 struct { 255 unsigned long hash; 256 unsigned long len_desc; 257 struct key_type *type; /* type of key */ 258 struct key_tag *domain_tag; /* Domain of operation */ 259 char *description; 260 u64 type_id; 261 u64 domain_id; 262 }; 263 }; 264 265 /* key data 266 * - this is used to hold the data actually used in cryptography or 267 * whatever 268 */ 269 union { 270 union key_payload payload; 271 struct { 272 /* Keyring bits */ 273 struct list_head name_link; 274 struct assoc_array keys; 275 }; 276 }; 277 278 /* This is set on a keyring to restrict the addition of a link to a key 279 * to it. If this structure isn't provided then it is assumed that the 280 * keyring is open to any addition. It is ignored for non-keyring 281 * keys. Only set this value using keyring_restrict(), keyring_alloc(), 282 * or key_alloc(). 283 * 284 * This is intended for use with rings of trusted keys whereby addition 285 * to the keyring needs to be controlled. KEY_ALLOC_BYPASS_RESTRICTION 286 * overrides this, allowing the kernel to add extra keys without 287 * restriction. 288 */ 289 struct key_restriction *restrict_link; 290 }; 291 292 extern struct key *key_alloc(struct key_type *type, 293 const char *desc, 294 kuid_t uid, kgid_t gid, 295 const struct cred *cred, 296 key_perm_t perm, 297 unsigned long flags, 298 struct key_restriction *restrict_link); 299 300 301 #define KEY_ALLOC_IN_QUOTA 0x0000 /* add to quota, reject if would overrun */ 302 #define KEY_ALLOC_QUOTA_OVERRUN 0x0001 /* add to quota, permit even if overrun */ 303 #define KEY_ALLOC_NOT_IN_QUOTA 0x0002 /* not in quota */ 304 #define KEY_ALLOC_BUILT_IN 0x0004 /* Key is built into kernel */ 305 #define KEY_ALLOC_BYPASS_RESTRICTION 0x0008 /* Override the check on restricted keyrings */ 306 #define KEY_ALLOC_UID_KEYRING 0x0010 /* allocating a user or user session keyring */ 307 #define KEY_ALLOC_SET_KEEP 0x0020 /* Set the KEEP flag on the key/keyring */ 308 309 extern void key_revoke(struct key *key); 310 extern void key_invalidate(struct key *key); 311 extern void key_put(struct key *key); 312 extern bool key_put_tag(struct key_tag *tag); 313 extern void key_remove_domain(struct key_tag *domain_tag); 314 315 static inline struct key *__key_get(struct key *key) 316 { 317 refcount_inc(&key->usage); 318 return key; 319 } 320 321 static inline struct key *key_get(struct key *key) 322 { 323 return key ? __key_get(key) : key; 324 } 325 326 static inline void key_ref_put(key_ref_t key_ref) 327 { 328 key_put(key_ref_to_ptr(key_ref)); 329 } 330 331 extern struct key *request_key_tag(struct key_type *type, 332 const char *description, 333 struct key_tag *domain_tag, 334 const char *callout_info); 335 336 extern struct key *request_key_rcu(struct key_type *type, 337 const char *description, 338 struct key_tag *domain_tag); 339 340 extern struct key *request_key_with_auxdata(struct key_type *type, 341 const char *description, 342 struct key_tag *domain_tag, 343 const void *callout_info, 344 size_t callout_len, 345 void *aux); 346 347 /** 348 * request_key - Request a key and wait for construction 349 * @type: Type of key. 350 * @description: The searchable description of the key. 351 * @callout_info: The data to pass to the instantiation upcall (or NULL). 352 * 353 * As for request_key_tag(), but with the default global domain tag. 354 */ 355 static inline struct key *request_key(struct key_type *type, 356 const char *description, 357 const char *callout_info) 358 { 359 return request_key_tag(type, description, NULL, callout_info); 360 } 361 362 #ifdef CONFIG_NET 363 /** 364 * request_key_net - Request a key for a net namespace and wait for construction 365 * @type: Type of key. 366 * @description: The searchable description of the key. 367 * @net: The network namespace that is the key's domain of operation. 368 * @callout_info: The data to pass to the instantiation upcall (or NULL). 369 * 370 * As for request_key() except that it does not add the returned key to a 371 * keyring if found, new keys are always allocated in the user's quota, the 372 * callout_info must be a NUL-terminated string and no auxiliary data can be 373 * passed. Only keys that operate the specified network namespace are used. 374 * 375 * Furthermore, it then works as wait_for_key_construction() to wait for the 376 * completion of keys undergoing construction with a non-interruptible wait. 377 */ 378 #define request_key_net(type, description, net, callout_info) \ 379 request_key_tag(type, description, net->key_domain, callout_info) 380 381 /** 382 * request_key_net_rcu - Request a key for a net namespace under RCU conditions 383 * @type: Type of key. 384 * @description: The searchable description of the key. 385 * @net: The network namespace that is the key's domain of operation. 386 * 387 * As for request_key_rcu() except that only keys that operate the specified 388 * network namespace are used. 389 */ 390 #define request_key_net_rcu(type, description, net) \ 391 request_key_rcu(type, description, net->key_domain) 392 #endif /* CONFIG_NET */ 393 394 extern int wait_for_key_construction(struct key *key, bool intr); 395 396 extern int key_validate(const struct key *key); 397 398 extern key_ref_t key_create(key_ref_t keyring, 399 const char *type, 400 const char *description, 401 const void *payload, 402 size_t plen, 403 key_perm_t perm, 404 unsigned long flags); 405 406 extern key_ref_t key_create_or_update(key_ref_t keyring, 407 const char *type, 408 const char *description, 409 const void *payload, 410 size_t plen, 411 key_perm_t perm, 412 unsigned long flags); 413 414 extern int key_update(key_ref_t key, 415 const void *payload, 416 size_t plen); 417 418 extern int key_link(struct key *keyring, 419 struct key *key); 420 421 extern int key_move(struct key *key, 422 struct key *from_keyring, 423 struct key *to_keyring, 424 unsigned int flags); 425 426 extern int key_unlink(struct key *keyring, 427 struct key *key); 428 429 extern struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid, 430 const struct cred *cred, 431 key_perm_t perm, 432 unsigned long flags, 433 struct key_restriction *restrict_link, 434 struct key *dest); 435 436 extern int restrict_link_reject(struct key *keyring, 437 const struct key_type *type, 438 const union key_payload *payload, 439 struct key *restriction_key); 440 441 extern int keyring_clear(struct key *keyring); 442 443 extern key_ref_t keyring_search(key_ref_t keyring, 444 struct key_type *type, 445 const char *description, 446 bool recurse); 447 448 extern int keyring_restrict(key_ref_t keyring, const char *type, 449 const char *restriction); 450 451 extern void key_register_bpf_keyring(struct key *keyring); 452 extern void key_register_dm_verity_keyring(struct key *keyring); 453 extern void key_register_fs_verity_keyring(struct key *keyring); 454 455 extern struct key *key_lookup(key_serial_t id); 456 457 static inline key_serial_t key_serial(const struct key *key) 458 { 459 return key ? key->serial : 0; 460 } 461 462 extern void key_set_timeout(struct key *, unsigned); 463 464 extern key_ref_t lookup_user_key(key_serial_t id, unsigned long flags, 465 enum key_need_perm need_perm); 466 extern void key_free_user_ns(struct user_namespace *); 467 468 static inline short key_read_state(const struct key *key) 469 { 470 /* Barrier versus mark_key_instantiated(). */ 471 return smp_load_acquire(&key->state); 472 } 473 474 /** 475 * key_is_positive - Determine if a key has been positively instantiated 476 * @key: The key to check. 477 * 478 * Return true if the specified key has been positively instantiated, false 479 * otherwise. 480 */ 481 static inline bool key_is_positive(const struct key *key) 482 { 483 return key_read_state(key) == KEY_IS_POSITIVE; 484 } 485 486 static inline bool key_is_negative(const struct key *key) 487 { 488 return key_read_state(key) < 0; 489 } 490 491 #define dereference_key_rcu(KEY) \ 492 (rcu_dereference((KEY)->payload.rcu_data0)) 493 494 #define dereference_key_locked(KEY) \ 495 (rcu_dereference_protected((KEY)->payload.rcu_data0, \ 496 rwsem_is_locked(&((struct key *)(KEY))->sem))) 497 498 #define rcu_assign_keypointer(KEY, PAYLOAD) \ 499 do { \ 500 rcu_assign_pointer((KEY)->payload.rcu_data0, (PAYLOAD)); \ 501 } while (0) 502 503 /* 504 * the userspace interface 505 */ 506 extern int install_thread_keyring_to_cred(struct cred *cred); 507 extern void key_fsuid_changed(struct cred *new_cred); 508 extern void key_fsgid_changed(struct cred *new_cred); 509 extern void key_init(void); 510 511 #else /* CONFIG_KEYS */ 512 513 #define key_validate(k) 0 514 #define key_serial(k) 0 515 #define key_get(k) ({ NULL; }) 516 #define key_revoke(k) do { } while(0) 517 #define key_invalidate(k) do { } while(0) 518 #define key_put(k) do { } while(0) 519 #define key_ref_put(k) do { } while(0) 520 #define make_key_ref(k, p) NULL 521 #define key_ref_to_ptr(k) NULL 522 #define is_key_possessed(k) 0 523 #define key_fsuid_changed(c) do { } while(0) 524 #define key_fsgid_changed(c) do { } while(0) 525 #define key_init() do { } while(0) 526 #define key_free_user_ns(ns) do { } while(0) 527 #define key_remove_domain(d) do { } while(0) 528 #define key_lookup(k) NULL 529 530 #endif /* CONFIG_KEYS */ 531 #endif /* __KERNEL__ */ 532 #endif /* _LINUX_KEY_H */ 533