key.h 9.9 KB

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