key.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. union {
  108. struct list_head graveyard_link;
  109. struct rb_node serial_node;
  110. };
  111. struct key_type *type; /* type of key */
  112. struct rw_semaphore sem; /* change vs change sem */
  113. struct key_user *user; /* owner of this key */
  114. void *security; /* security data for this key */
  115. union {
  116. time_t expiry; /* time at which key expires (or 0) */
  117. time_t revoked_at; /* time at which key was revoked */
  118. };
  119. time_t last_used_at; /* last time used for LRU keyring discard */
  120. uid_t uid;
  121. gid_t gid;
  122. key_perm_t perm; /* access permissions */
  123. unsigned short quotalen; /* length added to quota */
  124. unsigned short datalen; /* payload data length
  125. * - may not match RCU dereferenced payload
  126. * - payload should contain own length
  127. */
  128. #ifdef KEY_DEBUGGING
  129. unsigned magic;
  130. #define KEY_DEBUG_MAGIC 0x18273645u
  131. #define KEY_DEBUG_MAGIC_X 0xf8e9dacbu
  132. #endif
  133. unsigned long flags; /* status flags (change with bitops) */
  134. #define KEY_FLAG_INSTANTIATED 0 /* set if key has been instantiated */
  135. #define KEY_FLAG_DEAD 1 /* set if key type has been deleted */
  136. #define KEY_FLAG_REVOKED 2 /* set if key had been revoked */
  137. #define KEY_FLAG_IN_QUOTA 3 /* set if key consumes quota */
  138. #define KEY_FLAG_USER_CONSTRUCT 4 /* set if key is being constructed in userspace */
  139. #define KEY_FLAG_NEGATIVE 5 /* set if key is negative */
  140. #define KEY_FLAG_ROOT_CAN_CLEAR 6 /* set if key can be cleared by root without permission */
  141. #define KEY_FLAG_INVALIDATED 7 /* set if key has been invalidated */
  142. /* the description string
  143. * - this is used to match a key against search criteria
  144. * - this should be a printable string
  145. * - eg: for krb5 AFS, this might be "afs@REDHAT.COM"
  146. */
  147. char *description;
  148. /* type specific data
  149. * - this is used by the keyring type to index the name
  150. */
  151. union {
  152. struct list_head link;
  153. unsigned long x[2];
  154. void *p[2];
  155. int reject_error;
  156. } type_data;
  157. /* key data
  158. * - this is used to hold the data actually used in cryptography or
  159. * whatever
  160. */
  161. union {
  162. unsigned long value;
  163. void __rcu *rcudata;
  164. void *data;
  165. struct keyring_list __rcu *subscriptions;
  166. } payload;
  167. };
  168. extern struct key *key_alloc(struct key_type *type,
  169. const char *desc,
  170. uid_t uid, gid_t gid,
  171. const struct cred *cred,
  172. key_perm_t perm,
  173. unsigned long flags);
  174. #define KEY_ALLOC_IN_QUOTA 0x0000 /* add to quota, reject if would overrun */
  175. #define KEY_ALLOC_QUOTA_OVERRUN 0x0001 /* add to quota, permit even if overrun */
  176. #define KEY_ALLOC_NOT_IN_QUOTA 0x0002 /* not in quota */
  177. extern void key_revoke(struct key *key);
  178. extern void key_invalidate(struct key *key);
  179. extern void key_put(struct key *key);
  180. static inline struct key *key_get(struct key *key)
  181. {
  182. if (key)
  183. atomic_inc(&key->usage);
  184. return key;
  185. }
  186. static inline void key_ref_put(key_ref_t key_ref)
  187. {
  188. key_put(key_ref_to_ptr(key_ref));
  189. }
  190. extern struct key *request_key(struct key_type *type,
  191. const char *description,
  192. const char *callout_info);
  193. extern struct key *request_key_with_auxdata(struct key_type *type,
  194. const char *description,
  195. const void *callout_info,
  196. size_t callout_len,
  197. void *aux);
  198. extern struct key *request_key_async(struct key_type *type,
  199. const char *description,
  200. const void *callout_info,
  201. size_t callout_len);
  202. extern struct key *request_key_async_with_auxdata(struct key_type *type,
  203. const char *description,
  204. const void *callout_info,
  205. size_t callout_len,
  206. void *aux);
  207. extern int wait_for_key_construction(struct key *key, bool intr);
  208. extern int key_validate(const struct key *key);
  209. extern key_ref_t key_create_or_update(key_ref_t keyring,
  210. const char *type,
  211. const char *description,
  212. const void *payload,
  213. size_t plen,
  214. key_perm_t perm,
  215. unsigned long flags);
  216. extern int key_update(key_ref_t key,
  217. const void *payload,
  218. size_t plen);
  219. extern int key_link(struct key *keyring,
  220. struct key *key);
  221. extern int key_unlink(struct key *keyring,
  222. struct key *key);
  223. extern struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
  224. const struct cred *cred,
  225. key_perm_t perm,
  226. unsigned long flags,
  227. struct key *dest);
  228. extern int keyring_clear(struct key *keyring);
  229. extern key_ref_t keyring_search(key_ref_t keyring,
  230. struct key_type *type,
  231. const char *description);
  232. extern int keyring_add_key(struct key *keyring,
  233. struct key *key);
  234. extern struct key *key_lookup(key_serial_t id);
  235. static inline key_serial_t key_serial(const struct key *key)
  236. {
  237. return key ? key->serial : 0;
  238. }
  239. extern void key_set_timeout(struct key *, unsigned);
  240. /**
  241. * key_is_instantiated - Determine if a key has been positively instantiated
  242. * @key: The key to check.
  243. *
  244. * Return true if the specified key has been positively instantiated, false
  245. * otherwise.
  246. */
  247. static inline bool key_is_instantiated(const struct key *key)
  248. {
  249. return test_bit(KEY_FLAG_INSTANTIATED, &key->flags) &&
  250. !test_bit(KEY_FLAG_NEGATIVE, &key->flags);
  251. }
  252. #define rcu_dereference_key(KEY) \
  253. (rcu_dereference_protected((KEY)->payload.rcudata, \
  254. rwsem_is_locked(&((struct key *)(KEY))->sem)))
  255. #define rcu_assign_keypointer(KEY, PAYLOAD) \
  256. do { \
  257. rcu_assign_pointer((KEY)->payload.rcudata, (PAYLOAD)); \
  258. } while (0)
  259. #ifdef CONFIG_SYSCTL
  260. extern ctl_table key_sysctls[];
  261. #endif
  262. /*
  263. * the userspace interface
  264. */
  265. extern int install_thread_keyring_to_cred(struct cred *cred);
  266. extern void key_fsuid_changed(struct task_struct *tsk);
  267. extern void key_fsgid_changed(struct task_struct *tsk);
  268. extern void key_init(void);
  269. #else /* CONFIG_KEYS */
  270. #define key_validate(k) 0
  271. #define key_serial(k) 0
  272. #define key_get(k) ({ NULL; })
  273. #define key_revoke(k) do { } while(0)
  274. #define key_invalidate(k) do { } while(0)
  275. #define key_put(k) do { } while(0)
  276. #define key_ref_put(k) do { } while(0)
  277. #define make_key_ref(k, p) NULL
  278. #define key_ref_to_ptr(k) NULL
  279. #define is_key_possessed(k) 0
  280. #define key_fsuid_changed(t) do { } while(0)
  281. #define key_fsgid_changed(t) do { } while(0)
  282. #define key_init() do { } while(0)
  283. #endif /* CONFIG_KEYS */
  284. #endif /* __KERNEL__ */
  285. #endif /* _LINUX_KEY_H */