key.h 8.7 KB

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