key.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. #ifdef __KERNEL__
  25. #include <linux/uidgid.h>
  26. /* key handle serial number */
  27. typedef int32_t key_serial_t;
  28. /* key handle permissions mask */
  29. typedef uint32_t key_perm_t;
  30. struct key;
  31. #ifdef CONFIG_KEYS
  32. #undef KEY_DEBUGGING
  33. #define KEY_POS_VIEW 0x01000000 /* possessor can view a key's attributes */
  34. #define KEY_POS_READ 0x02000000 /* possessor can read key payload / view keyring */
  35. #define KEY_POS_WRITE 0x04000000 /* possessor can update key payload / add link to keyring */
  36. #define KEY_POS_SEARCH 0x08000000 /* possessor can find a key in search / search a keyring */
  37. #define KEY_POS_LINK 0x10000000 /* possessor can create a link to a key/keyring */
  38. #define KEY_POS_SETATTR 0x20000000 /* possessor can set key attributes */
  39. #define KEY_POS_ALL 0x3f000000
  40. #define KEY_USR_VIEW 0x00010000 /* user permissions... */
  41. #define KEY_USR_READ 0x00020000
  42. #define KEY_USR_WRITE 0x00040000
  43. #define KEY_USR_SEARCH 0x00080000
  44. #define KEY_USR_LINK 0x00100000
  45. #define KEY_USR_SETATTR 0x00200000
  46. #define KEY_USR_ALL 0x003f0000
  47. #define KEY_GRP_VIEW 0x00000100 /* group permissions... */
  48. #define KEY_GRP_READ 0x00000200
  49. #define KEY_GRP_WRITE 0x00000400
  50. #define KEY_GRP_SEARCH 0x00000800
  51. #define KEY_GRP_LINK 0x00001000
  52. #define KEY_GRP_SETATTR 0x00002000
  53. #define KEY_GRP_ALL 0x00003f00
  54. #define KEY_OTH_VIEW 0x00000001 /* third party permissions... */
  55. #define KEY_OTH_READ 0x00000002
  56. #define KEY_OTH_WRITE 0x00000004
  57. #define KEY_OTH_SEARCH 0x00000008
  58. #define KEY_OTH_LINK 0x00000010
  59. #define KEY_OTH_SETATTR 0x00000020
  60. #define KEY_OTH_ALL 0x0000003f
  61. #define KEY_PERM_UNDEF 0xffffffff
  62. struct seq_file;
  63. struct user_struct;
  64. struct signal_struct;
  65. struct cred;
  66. struct key_type;
  67. struct key_owner;
  68. struct keyring_list;
  69. struct keyring_name;
  70. struct keyring_index_key {
  71. struct key_type *type;
  72. const char *description;
  73. size_t desc_len;
  74. };
  75. /*****************************************************************************/
  76. /*
  77. * key reference with possession attribute handling
  78. *
  79. * NOTE! key_ref_t is a typedef'd pointer to a type that is not actually
  80. * defined. This is because we abuse the bottom bit of the reference to carry a
  81. * flag to indicate whether the calling process possesses that key in one of
  82. * its keyrings.
  83. *
  84. * the key_ref_t has been made a separate type so that the compiler can reject
  85. * attempts to dereference it without proper conversion.
  86. *
  87. * the three functions are used to assemble and disassemble references
  88. */
  89. typedef struct __key_reference_with_attributes *key_ref_t;
  90. static inline key_ref_t make_key_ref(const struct key *key,
  91. bool possession)
  92. {
  93. return (key_ref_t) ((unsigned long) key | possession);
  94. }
  95. static inline struct key *key_ref_to_ptr(const key_ref_t key_ref)
  96. {
  97. return (struct key *) ((unsigned long) key_ref & ~1UL);
  98. }
  99. static inline bool is_key_possessed(const key_ref_t key_ref)
  100. {
  101. return (unsigned long) key_ref & 1UL;
  102. }
  103. /*****************************************************************************/
  104. /*
  105. * authentication token / access credential / keyring
  106. * - types of key include:
  107. * - keyrings
  108. * - disk encryption IDs
  109. * - Kerberos TGTs and tickets
  110. */
  111. struct key {
  112. atomic_t usage; /* number of references */
  113. key_serial_t serial; /* key serial number */
  114. union {
  115. struct list_head graveyard_link;
  116. struct rb_node serial_node;
  117. };
  118. struct rw_semaphore sem; /* change vs change sem */
  119. struct key_user *user; /* owner of this key */
  120. void *security; /* security data for this key */
  121. union {
  122. time_t expiry; /* time at which key expires (or 0) */
  123. time_t revoked_at; /* time at which key was revoked */
  124. };
  125. time_t last_used_at; /* last time used for LRU keyring discard */
  126. kuid_t uid;
  127. kgid_t gid;
  128. key_perm_t perm; /* access permissions */
  129. unsigned short quotalen; /* length added to quota */
  130. unsigned short datalen; /* payload data length
  131. * - may not match RCU dereferenced payload
  132. * - payload should contain own length
  133. */
  134. #ifdef KEY_DEBUGGING
  135. unsigned magic;
  136. #define KEY_DEBUG_MAGIC 0x18273645u
  137. #define KEY_DEBUG_MAGIC_X 0xf8e9dacbu
  138. #endif
  139. unsigned long flags; /* status flags (change with bitops) */
  140. #define KEY_FLAG_INSTANTIATED 0 /* set if key has been instantiated */
  141. #define KEY_FLAG_DEAD 1 /* set if key type has been deleted */
  142. #define KEY_FLAG_REVOKED 2 /* set if key had been revoked */
  143. #define KEY_FLAG_IN_QUOTA 3 /* set if key consumes quota */
  144. #define KEY_FLAG_USER_CONSTRUCT 4 /* set if key is being constructed in userspace */
  145. #define KEY_FLAG_NEGATIVE 5 /* set if key is negative */
  146. #define KEY_FLAG_ROOT_CAN_CLEAR 6 /* set if key can be cleared by root without permission */
  147. #define KEY_FLAG_INVALIDATED 7 /* set if key has been invalidated */
  148. #define KEY_FLAG_TRUSTED 8 /* set if key is trusted */
  149. #define KEY_FLAG_TRUSTED_ONLY 9 /* set if keyring only accepts links to trusted keys */
  150. /* the key type and key description string
  151. * - the desc is used to match a key against search criteria
  152. * - it should be a printable string
  153. * - eg: for krb5 AFS, this might be "afs@REDHAT.COM"
  154. */
  155. union {
  156. struct keyring_index_key index_key;
  157. struct {
  158. struct key_type *type; /* type of key */
  159. char *description;
  160. };
  161. };
  162. /* type specific data
  163. * - this is used by the keyring type to index the name
  164. */
  165. union {
  166. struct list_head link;
  167. unsigned long x[2];
  168. void *p[2];
  169. int reject_error;
  170. } type_data;
  171. /* key data
  172. * - this is used to hold the data actually used in cryptography or
  173. * whatever
  174. */
  175. union {
  176. union {
  177. unsigned long value;
  178. void __rcu *rcudata;
  179. void *data;
  180. void *data2[2];
  181. } payload;
  182. struct assoc_array keys;
  183. };
  184. };
  185. extern struct key *key_alloc(struct key_type *type,
  186. const char *desc,
  187. kuid_t uid, kgid_t gid,
  188. const struct cred *cred,
  189. key_perm_t perm,
  190. unsigned long flags);
  191. #define KEY_ALLOC_IN_QUOTA 0x0000 /* add to quota, reject if would overrun */
  192. #define KEY_ALLOC_QUOTA_OVERRUN 0x0001 /* add to quota, permit even if overrun */
  193. #define KEY_ALLOC_NOT_IN_QUOTA 0x0002 /* not in quota */
  194. #define KEY_ALLOC_TRUSTED 0x0004 /* Key should be flagged as trusted */
  195. extern void key_revoke(struct key *key);
  196. extern void key_invalidate(struct key *key);
  197. extern void key_put(struct key *key);
  198. static inline struct key *__key_get(struct key *key)
  199. {
  200. atomic_inc(&key->usage);
  201. return key;
  202. }
  203. static inline struct key *key_get(struct key *key)
  204. {
  205. return key ? __key_get(key) : key;
  206. }
  207. static inline void key_ref_put(key_ref_t key_ref)
  208. {
  209. key_put(key_ref_to_ptr(key_ref));
  210. }
  211. extern struct key *request_key(struct key_type *type,
  212. const char *description,
  213. const char *callout_info);
  214. extern struct key *request_key_with_auxdata(struct key_type *type,
  215. const char *description,
  216. const void *callout_info,
  217. size_t callout_len,
  218. void *aux);
  219. extern struct key *request_key_async(struct key_type *type,
  220. const char *description,
  221. const void *callout_info,
  222. size_t callout_len);
  223. extern struct key *request_key_async_with_auxdata(struct key_type *type,
  224. const char *description,
  225. const void *callout_info,
  226. size_t callout_len,
  227. void *aux);
  228. extern int wait_for_key_construction(struct key *key, bool intr);
  229. extern int key_validate(const struct key *key);
  230. extern key_ref_t key_create_or_update(key_ref_t keyring,
  231. const char *type,
  232. const char *description,
  233. const void *payload,
  234. size_t plen,
  235. key_perm_t perm,
  236. unsigned long flags);
  237. extern int key_update(key_ref_t key,
  238. const void *payload,
  239. size_t plen);
  240. extern int key_link(struct key *keyring,
  241. struct key *key);
  242. extern int key_unlink(struct key *keyring,
  243. struct key *key);
  244. extern struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,
  245. const struct cred *cred,
  246. key_perm_t perm,
  247. unsigned long flags,
  248. struct key *dest);
  249. extern int keyring_clear(struct key *keyring);
  250. extern key_ref_t keyring_search(key_ref_t keyring,
  251. struct key_type *type,
  252. const char *description);
  253. extern int keyring_add_key(struct key *keyring,
  254. struct key *key);
  255. extern struct key *key_lookup(key_serial_t id);
  256. static inline key_serial_t key_serial(const struct key *key)
  257. {
  258. return key ? key->serial : 0;
  259. }
  260. extern void key_set_timeout(struct key *, unsigned);
  261. /**
  262. * key_is_instantiated - Determine if a key has been positively instantiated
  263. * @key: The key to check.
  264. *
  265. * Return true if the specified key has been positively instantiated, false
  266. * otherwise.
  267. */
  268. static inline bool key_is_instantiated(const struct key *key)
  269. {
  270. return test_bit(KEY_FLAG_INSTANTIATED, &key->flags) &&
  271. !test_bit(KEY_FLAG_NEGATIVE, &key->flags);
  272. }
  273. #define rcu_dereference_key(KEY) \
  274. (rcu_dereference_protected((KEY)->payload.rcudata, \
  275. rwsem_is_locked(&((struct key *)(KEY))->sem)))
  276. #define rcu_assign_keypointer(KEY, PAYLOAD) \
  277. do { \
  278. rcu_assign_pointer((KEY)->payload.rcudata, (PAYLOAD)); \
  279. } while (0)
  280. #ifdef CONFIG_SYSCTL
  281. extern ctl_table key_sysctls[];
  282. #endif
  283. /*
  284. * the userspace interface
  285. */
  286. extern int install_thread_keyring_to_cred(struct cred *cred);
  287. extern void key_fsuid_changed(struct task_struct *tsk);
  288. extern void key_fsgid_changed(struct task_struct *tsk);
  289. extern void key_init(void);
  290. #else /* CONFIG_KEYS */
  291. #define key_validate(k) 0
  292. #define key_serial(k) 0
  293. #define key_get(k) ({ NULL; })
  294. #define key_revoke(k) do { } while(0)
  295. #define key_invalidate(k) do { } while(0)
  296. #define key_put(k) do { } while(0)
  297. #define key_ref_put(k) do { } while(0)
  298. #define make_key_ref(k, p) NULL
  299. #define key_ref_to_ptr(k) NULL
  300. #define is_key_possessed(k) 0
  301. #define key_fsuid_changed(t) do { } while(0)
  302. #define key_fsgid_changed(t) do { } while(0)
  303. #define key_init() do { } while(0)
  304. #endif /* CONFIG_KEYS */
  305. #endif /* __KERNEL__ */
  306. #endif /* _LINUX_KEY_H */