key.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* key.h: authentication token and access key management
  2. *
  3. * Copyright (C) 2004 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 <asm/atomic.h>
  21. #ifdef __KERNEL__
  22. /* key handle serial number */
  23. typedef int32_t key_serial_t;
  24. /* key handle permissions mask */
  25. typedef uint32_t key_perm_t;
  26. struct key;
  27. #ifdef CONFIG_KEYS
  28. #undef KEY_DEBUGGING
  29. #define KEY_USR_VIEW 0x00010000 /* user can view a key's attributes */
  30. #define KEY_USR_READ 0x00020000 /* user can read key payload / view keyring */
  31. #define KEY_USR_WRITE 0x00040000 /* user can update key payload / add link to keyring */
  32. #define KEY_USR_SEARCH 0x00080000 /* user can find a key in search / search a keyring */
  33. #define KEY_USR_LINK 0x00100000 /* user can create a link to a key/keyring */
  34. #define KEY_USR_ALL 0x001f0000
  35. #define KEY_GRP_VIEW 0x00000100 /* group permissions... */
  36. #define KEY_GRP_READ 0x00000200
  37. #define KEY_GRP_WRITE 0x00000400
  38. #define KEY_GRP_SEARCH 0x00000800
  39. #define KEY_GRP_LINK 0x00001000
  40. #define KEY_GRP_ALL 0x00001f00
  41. #define KEY_OTH_VIEW 0x00000001 /* third party permissions... */
  42. #define KEY_OTH_READ 0x00000002
  43. #define KEY_OTH_WRITE 0x00000004
  44. #define KEY_OTH_SEARCH 0x00000008
  45. #define KEY_OTH_LINK 0x00000010
  46. #define KEY_OTH_ALL 0x0000001f
  47. struct seq_file;
  48. struct user_struct;
  49. struct signal_struct;
  50. struct key_type;
  51. struct key_owner;
  52. struct keyring_list;
  53. struct keyring_name;
  54. /*****************************************************************************/
  55. /*
  56. * authentication token / access credential / keyring
  57. * - types of key include:
  58. * - keyrings
  59. * - disk encryption IDs
  60. * - Kerberos TGTs and tickets
  61. */
  62. struct key {
  63. atomic_t usage; /* number of references */
  64. key_serial_t serial; /* key serial number */
  65. struct rb_node serial_node;
  66. struct key_type *type; /* type of key */
  67. struct rw_semaphore sem; /* change vs change sem */
  68. struct key_user *user; /* owner of this key */
  69. time_t expiry; /* time at which key expires (or 0) */
  70. uid_t uid;
  71. gid_t gid;
  72. key_perm_t perm; /* access permissions */
  73. unsigned short quotalen; /* length added to quota */
  74. unsigned short datalen; /* payload data length
  75. * - may not match RCU dereferenced payload
  76. * - payload should contain own length
  77. */
  78. #ifdef KEY_DEBUGGING
  79. unsigned magic;
  80. #define KEY_DEBUG_MAGIC 0x18273645u
  81. #define KEY_DEBUG_MAGIC_X 0xf8e9dacbu
  82. #endif
  83. unsigned long flags; /* status flags (change with bitops) */
  84. #define KEY_FLAG_INSTANTIATED 0 /* set if key has been instantiated */
  85. #define KEY_FLAG_DEAD 1 /* set if key type has been deleted */
  86. #define KEY_FLAG_REVOKED 2 /* set if key had been revoked */
  87. #define KEY_FLAG_IN_QUOTA 3 /* set if key consumes quota */
  88. #define KEY_FLAG_USER_CONSTRUCT 4 /* set if key is being constructed in userspace */
  89. #define KEY_FLAG_NEGATIVE 5 /* set if key is negative */
  90. /* the description string
  91. * - this is used to match a key against search criteria
  92. * - this should be a printable string
  93. * - eg: for krb5 AFS, this might be "afs@REDHAT.COM"
  94. */
  95. char *description;
  96. /* type specific data
  97. * - this is used by the keyring type to index the name
  98. */
  99. union {
  100. struct list_head link;
  101. } type_data;
  102. /* key data
  103. * - this is used to hold the data actually used in cryptography or
  104. * whatever
  105. */
  106. union {
  107. unsigned long value;
  108. void *data;
  109. struct keyring_list *subscriptions;
  110. } payload;
  111. };
  112. /*****************************************************************************/
  113. /*
  114. * kernel managed key type definition
  115. */
  116. struct key_type {
  117. /* name of the type */
  118. const char *name;
  119. /* default payload length for quota precalculation (optional)
  120. * - this can be used instead of calling key_payload_reserve(), that
  121. * function only needs to be called if the real datalen is different
  122. */
  123. size_t def_datalen;
  124. /* instantiate a key of this type
  125. * - this method should call key_payload_reserve() to determine if the
  126. * user's quota will hold the payload
  127. */
  128. int (*instantiate)(struct key *key, const void *data, size_t datalen);
  129. /* duplicate a key of this type (optional)
  130. * - the source key will be locked against change
  131. * - the new description will be attached
  132. * - the quota will have been adjusted automatically from
  133. * source->quotalen
  134. */
  135. int (*duplicate)(struct key *key, const struct key *source);
  136. /* update a key of this type (optional)
  137. * - this method should call key_payload_reserve() to recalculate the
  138. * quota consumption
  139. * - the key must be locked against read when modifying
  140. */
  141. int (*update)(struct key *key, const void *data, size_t datalen);
  142. /* match a key against a description */
  143. int (*match)(const struct key *key, const void *desc);
  144. /* clear the data from a key (optional) */
  145. void (*destroy)(struct key *key);
  146. /* describe a key */
  147. void (*describe)(const struct key *key, struct seq_file *p);
  148. /* read a key's data (optional)
  149. * - permission checks will be done by the caller
  150. * - the key's semaphore will be readlocked by the caller
  151. * - should return the amount of data that could be read, no matter how
  152. * much is copied into the buffer
  153. * - shouldn't do the copy if the buffer is NULL
  154. */
  155. long (*read)(const struct key *key, char __user *buffer, size_t buflen);
  156. /* internal fields */
  157. struct list_head link; /* link in types list */
  158. };
  159. extern struct key_type key_type_keyring;
  160. extern int register_key_type(struct key_type *ktype);
  161. extern void unregister_key_type(struct key_type *ktype);
  162. extern struct key *key_alloc(struct key_type *type,
  163. const char *desc,
  164. uid_t uid, gid_t gid, key_perm_t perm,
  165. int not_in_quota);
  166. extern int key_payload_reserve(struct key *key, size_t datalen);
  167. extern int key_instantiate_and_link(struct key *key,
  168. const void *data,
  169. size_t datalen,
  170. struct key *keyring,
  171. struct key *instkey);
  172. extern int key_negate_and_link(struct key *key,
  173. unsigned timeout,
  174. struct key *keyring,
  175. struct key *instkey);
  176. extern void key_revoke(struct key *key);
  177. extern void key_put(struct key *key);
  178. static inline struct key *key_get(struct key *key)
  179. {
  180. if (key)
  181. atomic_inc(&key->usage);
  182. return key;
  183. }
  184. extern struct key *request_key(struct key_type *type,
  185. const char *description,
  186. const char *callout_info);
  187. extern int key_validate(struct key *key);
  188. extern struct key *key_create_or_update(struct key *keyring,
  189. const char *type,
  190. const char *description,
  191. const void *payload,
  192. size_t plen,
  193. int not_in_quota);
  194. extern int key_update(struct key *key,
  195. const void *payload,
  196. size_t plen);
  197. extern int key_link(struct key *keyring,
  198. struct key *key);
  199. extern int key_unlink(struct key *keyring,
  200. struct key *key);
  201. extern struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
  202. int not_in_quota, struct key *dest);
  203. extern int keyring_clear(struct key *keyring);
  204. extern struct key *keyring_search(struct key *keyring,
  205. struct key_type *type,
  206. const char *description);
  207. extern int keyring_add_key(struct key *keyring,
  208. struct key *key);
  209. extern struct key *key_lookup(key_serial_t id);
  210. extern void keyring_replace_payload(struct key *key, void *replacement);
  211. #define key_serial(key) ((key) ? (key)->serial : 0)
  212. /*
  213. * the userspace interface
  214. */
  215. extern struct key root_user_keyring, root_session_keyring;
  216. extern int alloc_uid_keyring(struct user_struct *user);
  217. extern void switch_uid_keyring(struct user_struct *new_user);
  218. extern int copy_keys(unsigned long clone_flags, struct task_struct *tsk);
  219. extern int copy_thread_group_keys(struct task_struct *tsk);
  220. extern void exit_keys(struct task_struct *tsk);
  221. extern void exit_thread_group_keys(struct signal_struct *tg);
  222. extern int suid_keys(struct task_struct *tsk);
  223. extern int exec_keys(struct task_struct *tsk);
  224. extern void key_fsuid_changed(struct task_struct *tsk);
  225. extern void key_fsgid_changed(struct task_struct *tsk);
  226. extern void key_init(void);
  227. #define __install_session_keyring(tsk, keyring) \
  228. ({ \
  229. struct key *old_session = tsk->signal->session_keyring; \
  230. tsk->signal->session_keyring = keyring; \
  231. old_session; \
  232. })
  233. #else /* CONFIG_KEYS */
  234. #define key_validate(k) 0
  235. #define key_serial(k) 0
  236. #define key_get(k) ({ NULL; })
  237. #define key_put(k) do { } while(0)
  238. #define alloc_uid_keyring(u) 0
  239. #define switch_uid_keyring(u) do { } while(0)
  240. #define __install_session_keyring(t, k) ({ NULL; })
  241. #define copy_keys(f,t) 0
  242. #define copy_thread_group_keys(t) 0
  243. #define exit_keys(t) do { } while(0)
  244. #define exit_thread_group_keys(tg) do { } while(0)
  245. #define suid_keys(t) do { } while(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 */