key.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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/spinlock.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. rwlock_t lock; /* examination vs change lock */
  68. struct rw_semaphore sem; /* change vs change sem */
  69. struct key_user *user; /* owner of this key */
  70. time_t expiry; /* time at which key expires (or 0) */
  71. uid_t uid;
  72. gid_t gid;
  73. key_perm_t perm; /* access permissions */
  74. unsigned short quotalen; /* length added to quota */
  75. unsigned short datalen; /* payload data length */
  76. unsigned short flags; /* status flags (change with lock writelocked) */
  77. #define KEY_FLAG_INSTANTIATED 0x00000001 /* set if key has been instantiated */
  78. #define KEY_FLAG_DEAD 0x00000002 /* set if key type has been deleted */
  79. #define KEY_FLAG_REVOKED 0x00000004 /* set if key had been revoked */
  80. #define KEY_FLAG_IN_QUOTA 0x00000008 /* set if key consumes quota */
  81. #define KEY_FLAG_USER_CONSTRUCT 0x00000010 /* set if key is being constructed in userspace */
  82. #define KEY_FLAG_NEGATIVE 0x00000020 /* set if key is negative */
  83. #ifdef KEY_DEBUGGING
  84. unsigned magic;
  85. #define KEY_DEBUG_MAGIC 0x18273645u
  86. #define KEY_DEBUG_MAGIC_X 0xf8e9dacbu
  87. #endif
  88. /* the description string
  89. * - this is used to match a key against search criteria
  90. * - this should be a printable string
  91. * - eg: for krb5 AFS, this might be "afs@REDHAT.COM"
  92. */
  93. char *description;
  94. /* type specific data
  95. * - this is used by the keyring type to index the name
  96. */
  97. union {
  98. struct list_head link;
  99. } type_data;
  100. /* key data
  101. * - this is used to hold the data actually used in cryptography or
  102. * whatever
  103. */
  104. union {
  105. unsigned long value;
  106. void *data;
  107. struct keyring_list *subscriptions;
  108. } payload;
  109. };
  110. /*****************************************************************************/
  111. /*
  112. * kernel managed key type definition
  113. */
  114. struct key_type {
  115. /* name of the type */
  116. const char *name;
  117. /* default payload length for quota precalculation (optional)
  118. * - this can be used instead of calling key_payload_reserve(), that
  119. * function only needs to be called if the real datalen is different
  120. */
  121. size_t def_datalen;
  122. /* instantiate a key of this type
  123. * - this method should call key_payload_reserve() to determine if the
  124. * user's quota will hold the payload
  125. */
  126. int (*instantiate)(struct key *key, const void *data, size_t datalen);
  127. /* duplicate a key of this type (optional)
  128. * - the source key will be locked against change
  129. * - the new description will be attached
  130. * - the quota will have been adjusted automatically from
  131. * source->quotalen
  132. */
  133. int (*duplicate)(struct key *key, const struct key *source);
  134. /* update a key of this type (optional)
  135. * - this method should call key_payload_reserve() to recalculate the
  136. * quota consumption
  137. * - the key must be locked against read when modifying
  138. */
  139. int (*update)(struct key *key, const void *data, size_t datalen);
  140. /* match a key against a description */
  141. int (*match)(const struct key *key, const void *desc);
  142. /* clear the data from a key (optional) */
  143. void (*destroy)(struct key *key);
  144. /* describe a key */
  145. void (*describe)(const struct key *key, struct seq_file *p);
  146. /* read a key's data (optional)
  147. * - permission checks will be done by the caller
  148. * - the key's semaphore will be readlocked by the caller
  149. * - should return the amount of data that could be read, no matter how
  150. * much is copied into the buffer
  151. * - shouldn't do the copy if the buffer is NULL
  152. */
  153. long (*read)(const struct key *key, char __user *buffer, size_t buflen);
  154. /* internal fields */
  155. struct list_head link; /* link in types list */
  156. };
  157. extern struct key_type key_type_keyring;
  158. extern int register_key_type(struct key_type *ktype);
  159. extern void unregister_key_type(struct key_type *ktype);
  160. extern struct key *key_alloc(struct key_type *type,
  161. const char *desc,
  162. uid_t uid, gid_t gid, key_perm_t perm,
  163. int not_in_quota);
  164. extern int key_payload_reserve(struct key *key, size_t datalen);
  165. extern int key_instantiate_and_link(struct key *key,
  166. const void *data,
  167. size_t datalen,
  168. struct key *keyring);
  169. extern int key_negate_and_link(struct key *key,
  170. unsigned timeout,
  171. struct key *keyring);
  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. extern struct key *request_key(struct key_type *type,
  181. const char *description,
  182. const char *callout_info);
  183. extern int key_validate(struct key *key);
  184. extern struct key *key_create_or_update(struct key *keyring,
  185. const char *type,
  186. const char *description,
  187. const void *payload,
  188. size_t plen,
  189. int not_in_quota);
  190. extern int key_update(struct key *key,
  191. const void *payload,
  192. size_t plen);
  193. extern int key_link(struct key *keyring,
  194. struct key *key);
  195. extern int key_unlink(struct key *keyring,
  196. struct key *key);
  197. extern struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
  198. int not_in_quota, struct key *dest);
  199. extern int keyring_clear(struct key *keyring);
  200. extern struct key *keyring_search(struct key *keyring,
  201. struct key_type *type,
  202. const char *description);
  203. extern struct key *search_process_keyrings(struct key_type *type,
  204. const char *description);
  205. extern int keyring_add_key(struct key *keyring,
  206. struct key *key);
  207. extern struct key *key_lookup(key_serial_t id);
  208. #define key_serial(key) ((key) ? (key)->serial : 0)
  209. /*
  210. * the userspace interface
  211. */
  212. extern struct key root_user_keyring, root_session_keyring;
  213. extern int alloc_uid_keyring(struct user_struct *user);
  214. extern void switch_uid_keyring(struct user_struct *new_user);
  215. extern int copy_keys(unsigned long clone_flags, struct task_struct *tsk);
  216. extern int copy_thread_group_keys(struct task_struct *tsk);
  217. extern void exit_keys(struct task_struct *tsk);
  218. extern void exit_thread_group_keys(struct signal_struct *tg);
  219. extern int suid_keys(struct task_struct *tsk);
  220. extern int exec_keys(struct task_struct *tsk);
  221. extern void key_fsuid_changed(struct task_struct *tsk);
  222. extern void key_fsgid_changed(struct task_struct *tsk);
  223. extern void key_init(void);
  224. #else /* CONFIG_KEYS */
  225. #define key_validate(k) 0
  226. #define key_serial(k) 0
  227. #define key_get(k) NULL
  228. #define key_put(k) do { } while(0)
  229. #define alloc_uid_keyring(u) 0
  230. #define switch_uid_keyring(u) do { } while(0)
  231. #define copy_keys(f,t) 0
  232. #define copy_thread_group_keys(t) 0
  233. #define exit_keys(t) do { } while(0)
  234. #define exit_thread_group_keys(tg) do { } while(0)
  235. #define suid_keys(t) do { } while(0)
  236. #define exec_keys(t) do { } while(0)
  237. #define key_fsuid_changed(t) do { } while(0)
  238. #define key_fsgid_changed(t) do { } while(0)
  239. #define key_init() do { } while(0)
  240. #endif /* CONFIG_KEYS */
  241. #endif /* __KERNEL__ */
  242. #endif /* _LINUX_KEY_H */