key.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /* Authentication token and access key management
  2. *
  3. * Copyright (C) 2004, 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. *
  12. * See Documentation/security/keys.txt for information on keys/keyrings.
  13. */
  14. #ifndef _LINUX_KEY_H
  15. #define _LINUX_KEY_H
  16. #include <linux/types.h>
  17. #include <linux/list.h>
  18. #include <linux/rbtree.h>
  19. #include <linux/rcupdate.h>
  20. #include <linux/sysctl.h>
  21. #include <linux/rwsem.h>
  22. #include <linux/atomic.h>
  23. #include <linux/assoc_array.h>
  24. #include <linux/refcount.h>
  25. #ifdef __KERNEL__
  26. #include <linux/uidgid.h>
  27. /* key handle serial number */
  28. typedef int32_t key_serial_t;
  29. /* key handle permissions mask */
  30. typedef uint32_t key_perm_t;
  31. struct key;
  32. #ifdef CONFIG_KEYS
  33. #undef KEY_DEBUGGING
  34. #define KEY_POS_VIEW 0x01000000 /* possessor can view a key's attributes */
  35. #define KEY_POS_READ 0x02000000 /* possessor can read key payload / view keyring */
  36. #define KEY_POS_WRITE 0x04000000 /* possessor can update key payload / add link to keyring */
  37. #define KEY_POS_SEARCH 0x08000000 /* possessor can find a key in search / search a keyring */
  38. #define KEY_POS_LINK 0x10000000 /* possessor can create a link to a key/keyring */
  39. #define KEY_POS_SETATTR 0x20000000 /* possessor can set key attributes */
  40. #define KEY_POS_ALL 0x3f000000
  41. #define KEY_USR_VIEW 0x00010000 /* user permissions... */
  42. #define KEY_USR_READ 0x00020000
  43. #define KEY_USR_WRITE 0x00040000
  44. #define KEY_USR_SEARCH 0x00080000
  45. #define KEY_USR_LINK 0x00100000
  46. #define KEY_USR_SETATTR 0x00200000
  47. #define KEY_USR_ALL 0x003f0000
  48. #define KEY_GRP_VIEW 0x00000100 /* group permissions... */
  49. #define KEY_GRP_READ 0x00000200
  50. #define KEY_GRP_WRITE 0x00000400
  51. #define KEY_GRP_SEARCH 0x00000800
  52. #define KEY_GRP_LINK 0x00001000
  53. #define KEY_GRP_SETATTR 0x00002000
  54. #define KEY_GRP_ALL 0x00003f00
  55. #define KEY_OTH_VIEW 0x00000001 /* third party permissions... */
  56. #define KEY_OTH_READ 0x00000002
  57. #define KEY_OTH_WRITE 0x00000004
  58. #define KEY_OTH_SEARCH 0x00000008
  59. #define KEY_OTH_LINK 0x00000010
  60. #define KEY_OTH_SETATTR 0x00000020
  61. #define KEY_OTH_ALL 0x0000003f
  62. #define KEY_PERM_UNDEF 0xffffffff
  63. struct seq_file;
  64. struct user_struct;
  65. struct signal_struct;
  66. struct cred;
  67. struct key_type;
  68. struct key_owner;
  69. struct keyring_list;
  70. struct keyring_name;
  71. struct keyring_index_key {
  72. struct key_type *type;
  73. const char *description;
  74. size_t desc_len;
  75. };
  76. union key_payload {
  77. void __rcu *rcu_data0;
  78. void *data[4];
  79. };
  80. /*****************************************************************************/
  81. /*
  82. * key reference with possession attribute handling
  83. *
  84. * NOTE! key_ref_t is a typedef'd pointer to a type that is not actually
  85. * defined. This is because we abuse the bottom bit of the reference to carry a
  86. * flag to indicate whether the calling process possesses that key in one of
  87. * its keyrings.
  88. *
  89. * the key_ref_t has been made a separate type so that the compiler can reject
  90. * attempts to dereference it without proper conversion.
  91. *
  92. * the three functions are used to assemble and disassemble references
  93. */
  94. typedef struct __key_reference_with_attributes *key_ref_t;
  95. static inline key_ref_t make_key_ref(const struct key *key,
  96. bool possession)
  97. {
  98. return (key_ref_t) ((unsigned long) key | possession);
  99. }
  100. static inline struct key *key_ref_to_ptr(const key_ref_t key_ref)
  101. {
  102. return (struct key *) ((unsigned long) key_ref & ~1UL);
  103. }
  104. static inline bool is_key_possessed(const key_ref_t key_ref)
  105. {
  106. return (unsigned long) key_ref & 1UL;
  107. }
  108. typedef int (*key_restrict_link_func_t)(struct key *dest_keyring,
  109. const struct key_type *type,
  110. const union key_payload *payload,
  111. struct key *restriction_key);
  112. struct key_restriction {
  113. key_restrict_link_func_t check;
  114. struct key *key;
  115. struct key_type *keytype;
  116. };
  117. /*****************************************************************************/
  118. /*
  119. * authentication token / access credential / keyring
  120. * - types of key include:
  121. * - keyrings
  122. * - disk encryption IDs
  123. * - Kerberos TGTs and tickets
  124. */
  125. struct key {
  126. refcount_t usage; /* number of references */
  127. key_serial_t serial; /* key serial number */
  128. union {
  129. struct list_head graveyard_link;
  130. struct rb_node serial_node;
  131. };
  132. struct rw_semaphore sem; /* change vs change sem */
  133. struct key_user *user; /* owner of this key */
  134. void *security; /* security data for this key */
  135. union {
  136. time_t expiry; /* time at which key expires (or 0) */
  137. time_t revoked_at; /* time at which key was revoked */
  138. };
  139. time_t last_used_at; /* last time used for LRU keyring discard */
  140. kuid_t uid;
  141. kgid_t gid;
  142. key_perm_t perm; /* access permissions */
  143. unsigned short quotalen; /* length added to quota */
  144. unsigned short datalen; /* payload data length
  145. * - may not match RCU dereferenced payload
  146. * - payload should contain own length
  147. */
  148. #ifdef KEY_DEBUGGING
  149. unsigned magic;
  150. #define KEY_DEBUG_MAGIC 0x18273645u
  151. #define KEY_DEBUG_MAGIC_X 0xf8e9dacbu
  152. #endif
  153. unsigned long flags; /* status flags (change with bitops) */
  154. #define KEY_FLAG_INSTANTIATED 0 /* set if key has been instantiated */
  155. #define KEY_FLAG_DEAD 1 /* set if key type has been deleted */
  156. #define KEY_FLAG_REVOKED 2 /* set if key had been revoked */
  157. #define KEY_FLAG_IN_QUOTA 3 /* set if key consumes quota */
  158. #define KEY_FLAG_USER_CONSTRUCT 4 /* set if key is being constructed in userspace */
  159. #define KEY_FLAG_NEGATIVE 5 /* set if key is negative */
  160. #define KEY_FLAG_ROOT_CAN_CLEAR 6 /* set if key can be cleared by root without permission */
  161. #define KEY_FLAG_INVALIDATED 7 /* set if key has been invalidated */
  162. #define KEY_FLAG_BUILTIN 8 /* set if key is built in to the kernel */
  163. #define KEY_FLAG_ROOT_CAN_INVAL 9 /* set if key can be invalidated by root without permission */
  164. #define KEY_FLAG_KEEP 10 /* set if key should not be removed */
  165. /* the key type and key description string
  166. * - the desc is used to match a key against search criteria
  167. * - it should be a printable string
  168. * - eg: for krb5 AFS, this might be "afs@REDHAT.COM"
  169. */
  170. union {
  171. struct keyring_index_key index_key;
  172. struct {
  173. struct key_type *type; /* type of key */
  174. char *description;
  175. };
  176. };
  177. /* key data
  178. * - this is used to hold the data actually used in cryptography or
  179. * whatever
  180. */
  181. union {
  182. union key_payload payload;
  183. struct {
  184. /* Keyring bits */
  185. struct list_head name_link;
  186. struct assoc_array keys;
  187. };
  188. int reject_error;
  189. };
  190. /* This is set on a keyring to restrict the addition of a link to a key
  191. * to it. If this structure isn't provided then it is assumed that the
  192. * keyring is open to any addition. It is ignored for non-keyring
  193. * keys.
  194. *
  195. * This is intended for use with rings of trusted keys whereby addition
  196. * to the keyring needs to be controlled. KEY_ALLOC_BYPASS_RESTRICTION
  197. * overrides this, allowing the kernel to add extra keys without
  198. * restriction.
  199. */
  200. struct key_restriction *restrict_link;
  201. };
  202. extern struct key *key_alloc(struct key_type *type,
  203. const char *desc,
  204. kuid_t uid, kgid_t gid,
  205. const struct cred *cred,
  206. key_perm_t perm,
  207. unsigned long flags,
  208. struct key_restriction *restrict_link);
  209. #define KEY_ALLOC_IN_QUOTA 0x0000 /* add to quota, reject if would overrun */
  210. #define KEY_ALLOC_QUOTA_OVERRUN 0x0001 /* add to quota, permit even if overrun */
  211. #define KEY_ALLOC_NOT_IN_QUOTA 0x0002 /* not in quota */
  212. #define KEY_ALLOC_BUILT_IN 0x0004 /* Key is built into kernel */
  213. #define KEY_ALLOC_BYPASS_RESTRICTION 0x0008 /* Override the check on restricted keyrings */
  214. extern void key_revoke(struct key *key);
  215. extern void key_invalidate(struct key *key);
  216. extern void key_put(struct key *key);
  217. static inline struct key *__key_get(struct key *key)
  218. {
  219. refcount_inc(&key->usage);
  220. return key;
  221. }
  222. static inline struct key *key_get(struct key *key)
  223. {
  224. return key ? __key_get(key) : key;
  225. }
  226. static inline void key_ref_put(key_ref_t key_ref)
  227. {
  228. key_put(key_ref_to_ptr(key_ref));
  229. }
  230. extern struct key *request_key(struct key_type *type,
  231. const char *description,
  232. const char *callout_info);
  233. extern struct key *request_key_with_auxdata(struct key_type *type,
  234. const char *description,
  235. const void *callout_info,
  236. size_t callout_len,
  237. void *aux);
  238. extern struct key *request_key_async(struct key_type *type,
  239. const char *description,
  240. const void *callout_info,
  241. size_t callout_len);
  242. extern struct key *request_key_async_with_auxdata(struct key_type *type,
  243. const char *description,
  244. const void *callout_info,
  245. size_t callout_len,
  246. void *aux);
  247. extern int wait_for_key_construction(struct key *key, bool intr);
  248. extern int key_validate(const struct key *key);
  249. extern key_ref_t key_create_or_update(key_ref_t keyring,
  250. const char *type,
  251. const char *description,
  252. const void *payload,
  253. size_t plen,
  254. key_perm_t perm,
  255. unsigned long flags);
  256. extern int key_update(key_ref_t key,
  257. const void *payload,
  258. size_t plen);
  259. extern int key_link(struct key *keyring,
  260. struct key *key);
  261. extern int key_unlink(struct key *keyring,
  262. struct key *key);
  263. extern struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,
  264. const struct cred *cred,
  265. key_perm_t perm,
  266. unsigned long flags,
  267. struct key_restriction *restrict_link,
  268. struct key *dest);
  269. extern int restrict_link_reject(struct key *keyring,
  270. const struct key_type *type,
  271. const union key_payload *payload,
  272. struct key *restriction_key);
  273. extern int keyring_clear(struct key *keyring);
  274. extern key_ref_t keyring_search(key_ref_t keyring,
  275. struct key_type *type,
  276. const char *description);
  277. extern int keyring_add_key(struct key *keyring,
  278. struct key *key);
  279. extern struct key *key_lookup(key_serial_t id);
  280. static inline key_serial_t key_serial(const struct key *key)
  281. {
  282. return key ? key->serial : 0;
  283. }
  284. extern void key_set_timeout(struct key *, unsigned);
  285. /*
  286. * The permissions required on a key that we're looking up.
  287. */
  288. #define KEY_NEED_VIEW 0x01 /* Require permission to view attributes */
  289. #define KEY_NEED_READ 0x02 /* Require permission to read content */
  290. #define KEY_NEED_WRITE 0x04 /* Require permission to update / modify */
  291. #define KEY_NEED_SEARCH 0x08 /* Require permission to search (keyring) or find (key) */
  292. #define KEY_NEED_LINK 0x10 /* Require permission to link */
  293. #define KEY_NEED_SETATTR 0x20 /* Require permission to change attributes */
  294. #define KEY_NEED_ALL 0x3f /* All the above permissions */
  295. /**
  296. * key_is_instantiated - Determine if a key has been positively instantiated
  297. * @key: The key to check.
  298. *
  299. * Return true if the specified key has been positively instantiated, false
  300. * otherwise.
  301. */
  302. static inline bool key_is_instantiated(const struct key *key)
  303. {
  304. return test_bit(KEY_FLAG_INSTANTIATED, &key->flags) &&
  305. !test_bit(KEY_FLAG_NEGATIVE, &key->flags);
  306. }
  307. #define dereference_key_rcu(KEY) \
  308. (rcu_dereference((KEY)->payload.rcu_data0))
  309. #define dereference_key_locked(KEY) \
  310. (rcu_dereference_protected((KEY)->payload.rcu_data0, \
  311. rwsem_is_locked(&((struct key *)(KEY))->sem)))
  312. #define rcu_assign_keypointer(KEY, PAYLOAD) \
  313. do { \
  314. rcu_assign_pointer((KEY)->payload.rcu_data0, (PAYLOAD)); \
  315. } while (0)
  316. #ifdef CONFIG_SYSCTL
  317. extern struct ctl_table key_sysctls[];
  318. #endif
  319. /*
  320. * the userspace interface
  321. */
  322. extern int install_thread_keyring_to_cred(struct cred *cred);
  323. extern void key_fsuid_changed(struct task_struct *tsk);
  324. extern void key_fsgid_changed(struct task_struct *tsk);
  325. extern void key_init(void);
  326. #else /* CONFIG_KEYS */
  327. #define key_validate(k) 0
  328. #define key_serial(k) 0
  329. #define key_get(k) ({ NULL; })
  330. #define key_revoke(k) do { } while(0)
  331. #define key_invalidate(k) do { } while(0)
  332. #define key_put(k) do { } while(0)
  333. #define key_ref_put(k) do { } while(0)
  334. #define make_key_ref(k, p) NULL
  335. #define key_ref_to_ptr(k) NULL
  336. #define is_key_possessed(k) 0
  337. #define key_fsuid_changed(t) do { } while(0)
  338. #define key_fsgid_changed(t) do { } while(0)
  339. #define key_init() do { } while(0)
  340. #endif /* CONFIG_KEYS */
  341. #endif /* __KERNEL__ */
  342. #endif /* _LINUX_KEY_H */