key.h 16 KB

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