key.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. #ifdef __KERNEL__
  24. /* key handle serial number */
  25. typedef int32_t key_serial_t;
  26. /* key handle permissions mask */
  27. typedef uint32_t key_perm_t;
  28. struct key;
  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. struct seq_file;
  61. struct user_struct;
  62. struct signal_struct;
  63. struct cred;
  64. struct key_type;
  65. struct key_owner;
  66. struct keyring_list;
  67. struct keyring_name;
  68. /*****************************************************************************/
  69. /*
  70. * key reference with possession attribute handling
  71. *
  72. * NOTE! key_ref_t is a typedef'd pointer to a type that is not actually
  73. * defined. This is because we abuse the bottom bit of the reference to carry a
  74. * flag to indicate whether the calling process possesses that key in one of
  75. * its keyrings.
  76. *
  77. * the key_ref_t has been made a separate type so that the compiler can reject
  78. * attempts to dereference it without proper conversion.
  79. *
  80. * the three functions are used to assemble and disassemble references
  81. */
  82. typedef struct __key_reference_with_attributes *key_ref_t;
  83. static inline key_ref_t make_key_ref(const struct key *key,
  84. unsigned long possession)
  85. {
  86. return (key_ref_t) ((unsigned long) key | possession);
  87. }
  88. static inline struct key *key_ref_to_ptr(const key_ref_t key_ref)
  89. {
  90. return (struct key *) ((unsigned long) key_ref & ~1UL);
  91. }
  92. static inline unsigned long is_key_possessed(const key_ref_t key_ref)
  93. {
  94. return (unsigned long) key_ref & 1UL;
  95. }
  96. /*****************************************************************************/
  97. /*
  98. * authentication token / access credential / keyring
  99. * - types of key include:
  100. * - keyrings
  101. * - disk encryption IDs
  102. * - Kerberos TGTs and tickets
  103. */
  104. struct key {
  105. atomic_t usage; /* number of references */
  106. key_serial_t serial; /* key serial number */
  107. struct rb_node serial_node;
  108. struct key_type *type; /* type of key */
  109. struct rw_semaphore sem; /* change vs change sem */
  110. struct key_user *user; /* owner of this key */
  111. void *security; /* security data for this key */
  112. union {
  113. time_t expiry; /* time at which key expires (or 0) */
  114. time_t revoked_at; /* time at which key was revoked */
  115. };
  116. uid_t uid;
  117. gid_t gid;
  118. key_perm_t perm; /* access permissions */
  119. unsigned short quotalen; /* length added to quota */
  120. unsigned short datalen; /* payload data length
  121. * - may not match RCU dereferenced payload
  122. * - payload should contain own length
  123. */
  124. #ifdef KEY_DEBUGGING
  125. unsigned magic;
  126. #define KEY_DEBUG_MAGIC 0x18273645u
  127. #define KEY_DEBUG_MAGIC_X 0xf8e9dacbu
  128. #endif
  129. unsigned long flags; /* status flags (change with bitops) */
  130. #define KEY_FLAG_INSTANTIATED 0 /* set if key has been instantiated */
  131. #define KEY_FLAG_DEAD 1 /* set if key type has been deleted */
  132. #define KEY_FLAG_REVOKED 2 /* set if key had been revoked */
  133. #define KEY_FLAG_IN_QUOTA 3 /* set if key consumes quota */
  134. #define KEY_FLAG_USER_CONSTRUCT 4 /* set if key is being constructed in userspace */
  135. #define KEY_FLAG_NEGATIVE 5 /* set if key is negative */
  136. #define KEY_FLAG_ROOT_CAN_CLEAR 6 /* set if key can be cleared by root without permission */
  137. /* the description string
  138. * - this is used to match a key against search criteria
  139. * - this should be a printable string
  140. * - eg: for krb5 AFS, this might be "afs@REDHAT.COM"
  141. */
  142. char *description;
  143. /* type specific data
  144. * - this is used by the keyring type to index the name
  145. */
  146. union {
  147. struct list_head link;
  148. unsigned long x[2];
  149. void *p[2];
  150. int reject_error;
  151. } type_data;
  152. /* key data
  153. * - this is used to hold the data actually used in cryptography or
  154. * whatever
  155. */
  156. union {
  157. unsigned long value;
  158. void __rcu *rcudata;
  159. void *data;
  160. struct keyring_list __rcu *subscriptions;
  161. } payload;
  162. };
  163. extern struct key *key_alloc(struct key_type *type,
  164. const char *desc,
  165. uid_t uid, gid_t gid,
  166. const struct cred *cred,
  167. key_perm_t perm,
  168. unsigned long flags);
  169. #define KEY_ALLOC_IN_QUOTA 0x0000 /* add to quota, reject if would overrun */
  170. #define KEY_ALLOC_QUOTA_OVERRUN 0x0001 /* add to quota, permit even if overrun */
  171. #define KEY_ALLOC_NOT_IN_QUOTA 0x0002 /* not in quota */
  172. extern void key_revoke(struct key *key);
  173. extern void key_put(struct key *key);
  174. static inline struct key *key_get(struct key *key)
  175. {
  176. if (key)
  177. atomic_inc(&key->usage);
  178. return key;
  179. }
  180. static inline void key_ref_put(key_ref_t key_ref)
  181. {
  182. key_put(key_ref_to_ptr(key_ref));
  183. }
  184. extern struct key *request_key(struct key_type *type,
  185. const char *description,
  186. const char *callout_info);
  187. extern struct key *request_key_with_auxdata(struct key_type *type,
  188. const char *description,
  189. const void *callout_info,
  190. size_t callout_len,
  191. void *aux);
  192. extern struct key *request_key_async(struct key_type *type,
  193. const char *description,
  194. const void *callout_info,
  195. size_t callout_len);
  196. extern struct key *request_key_async_with_auxdata(struct key_type *type,
  197. const char *description,
  198. const void *callout_info,
  199. size_t callout_len,
  200. void *aux);
  201. extern int wait_for_key_construction(struct key *key, bool intr);
  202. extern int key_validate(struct key *key);
  203. extern key_ref_t key_create_or_update(key_ref_t keyring,
  204. const char *type,
  205. const char *description,
  206. const void *payload,
  207. size_t plen,
  208. key_perm_t perm,
  209. unsigned long flags);
  210. extern int key_update(key_ref_t key,
  211. const void *payload,
  212. size_t plen);
  213. extern int key_link(struct key *keyring,
  214. struct key *key);
  215. extern int key_unlink(struct key *keyring,
  216. struct key *key);
  217. extern struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
  218. const struct cred *cred,
  219. unsigned long flags,
  220. struct key *dest);
  221. extern int keyring_clear(struct key *keyring);
  222. extern key_ref_t keyring_search(key_ref_t keyring,
  223. struct key_type *type,
  224. const char *description);
  225. extern int keyring_add_key(struct key *keyring,
  226. struct key *key);
  227. extern struct key *key_lookup(key_serial_t id);
  228. static inline key_serial_t key_serial(const struct key *key)
  229. {
  230. return key ? key->serial : 0;
  231. }
  232. extern void key_set_timeout(struct key *, unsigned);
  233. /**
  234. * key_is_instantiated - Determine if a key has been positively instantiated
  235. * @key: The key to check.
  236. *
  237. * Return true if the specified key has been positively instantiated, false
  238. * otherwise.
  239. */
  240. static inline bool key_is_instantiated(const struct key *key)
  241. {
  242. return test_bit(KEY_FLAG_INSTANTIATED, &key->flags) &&
  243. !test_bit(KEY_FLAG_NEGATIVE, &key->flags);
  244. }
  245. #define rcu_dereference_key(KEY) \
  246. (rcu_dereference_protected((KEY)->payload.rcudata, \
  247. rwsem_is_locked(&((struct key *)(KEY))->sem)))
  248. #define rcu_assign_keypointer(KEY, PAYLOAD) \
  249. (rcu_assign_pointer((KEY)->payload.rcudata, PAYLOAD))
  250. #ifdef CONFIG_SYSCTL
  251. extern ctl_table key_sysctls[];
  252. #endif
  253. extern void key_replace_session_keyring(void);
  254. /*
  255. * the userspace interface
  256. */
  257. extern int install_thread_keyring_to_cred(struct cred *cred);
  258. extern void key_fsuid_changed(struct task_struct *tsk);
  259. extern void key_fsgid_changed(struct task_struct *tsk);
  260. extern void key_init(void);
  261. #else /* CONFIG_KEYS */
  262. #define key_validate(k) 0
  263. #define key_serial(k) 0
  264. #define key_get(k) ({ NULL; })
  265. #define key_revoke(k) do { } while(0)
  266. #define key_put(k) do { } while(0)
  267. #define key_ref_put(k) do { } while(0)
  268. #define make_key_ref(k, p) NULL
  269. #define key_ref_to_ptr(k) NULL
  270. #define is_key_possessed(k) 0
  271. #define key_fsuid_changed(t) do { } while(0)
  272. #define key_fsgid_changed(t) do { } while(0)
  273. #define key_init() do { } while(0)
  274. #define key_replace_session_keyring() do { } while(0)
  275. #endif /* CONFIG_KEYS */
  276. #endif /* __KERNEL__ */
  277. #endif /* _LINUX_KEY_H */