ima.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright (C) 2005,2006,2007,2008 IBM Corporation
  3. *
  4. * Authors:
  5. * Reiner Sailer <sailer@watson.ibm.com>
  6. * Mimi Zohar <zohar@us.ibm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation, version 2 of the
  11. * License.
  12. *
  13. * File: ima.h
  14. * internal Integrity Measurement Architecture (IMA) definitions
  15. */
  16. #ifndef __LINUX_IMA_H
  17. #define __LINUX_IMA_H
  18. #include <linux/types.h>
  19. #include <linux/crypto.h>
  20. #include <linux/fs.h>
  21. #include <linux/security.h>
  22. #include <linux/hash.h>
  23. #include <linux/tpm.h>
  24. #include <linux/audit.h>
  25. #include <crypto/hash_info.h>
  26. #include "../integrity.h"
  27. #ifdef CONFIG_HAVE_IMA_KEXEC
  28. #include <asm/ima.h>
  29. #endif
  30. enum ima_show_type { IMA_SHOW_BINARY, IMA_SHOW_BINARY_NO_FIELD_LEN,
  31. IMA_SHOW_BINARY_OLD_STRING_FMT, IMA_SHOW_ASCII };
  32. enum tpm_pcrs { TPM_PCR0 = 0, TPM_PCR8 = 8 };
  33. /* digest size for IMA, fits SHA1 or MD5 */
  34. #define IMA_DIGEST_SIZE SHA1_DIGEST_SIZE
  35. #define IMA_EVENT_NAME_LEN_MAX 255
  36. #define IMA_HASH_BITS 10
  37. #define IMA_MEASURE_HTABLE_SIZE (1 << IMA_HASH_BITS)
  38. #define IMA_TEMPLATE_FIELD_ID_MAX_LEN 16
  39. #define IMA_TEMPLATE_NUM_FIELDS_MAX 15
  40. #define IMA_TEMPLATE_IMA_NAME "ima"
  41. #define IMA_TEMPLATE_IMA_FMT "d|n"
  42. /* current content of the policy */
  43. extern int ima_policy_flag;
  44. /* set during initialization */
  45. extern int ima_hash_algo;
  46. extern int ima_appraise;
  47. extern struct tpm_chip *ima_tpm_chip;
  48. extern const char boot_aggregate_name[];
  49. /* IMA event related data */
  50. struct ima_event_data {
  51. struct integrity_iint_cache *iint;
  52. struct file *file;
  53. const unsigned char *filename;
  54. struct evm_ima_xattr_data *xattr_value;
  55. int xattr_len;
  56. const char *violation;
  57. };
  58. /* IMA template field data definition */
  59. struct ima_field_data {
  60. u8 *data;
  61. u32 len;
  62. };
  63. /* IMA template field definition */
  64. struct ima_template_field {
  65. const char field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN];
  66. int (*field_init)(struct ima_event_data *event_data,
  67. struct ima_field_data *field_data);
  68. void (*field_show)(struct seq_file *m, enum ima_show_type show,
  69. struct ima_field_data *field_data);
  70. };
  71. /* IMA template descriptor definition */
  72. struct ima_template_desc {
  73. struct list_head list;
  74. char *name;
  75. char *fmt;
  76. int num_fields;
  77. struct ima_template_field **fields;
  78. };
  79. struct ima_template_entry {
  80. int pcr;
  81. u8 digest[TPM_DIGEST_SIZE]; /* sha1 or md5 measurement hash */
  82. struct ima_template_desc *template_desc; /* template descriptor */
  83. u32 template_data_len;
  84. struct ima_field_data template_data[0]; /* template related data */
  85. };
  86. struct ima_queue_entry {
  87. struct hlist_node hnext; /* place in hash collision list */
  88. struct list_head later; /* place in ima_measurements list */
  89. struct ima_template_entry *entry;
  90. };
  91. extern struct list_head ima_measurements; /* list of all measurements */
  92. /* Some details preceding the binary serialized measurement list */
  93. struct ima_kexec_hdr {
  94. u16 version;
  95. u16 _reserved0;
  96. u32 _reserved1;
  97. u64 buffer_size;
  98. u64 count;
  99. };
  100. #ifdef CONFIG_HAVE_IMA_KEXEC
  101. void ima_load_kexec_buffer(void);
  102. #else
  103. static inline void ima_load_kexec_buffer(void) {}
  104. #endif /* CONFIG_HAVE_IMA_KEXEC */
  105. /*
  106. * The default binary_runtime_measurements list format is defined as the
  107. * platform native format. The canonical format is defined as little-endian.
  108. */
  109. extern bool ima_canonical_fmt;
  110. /* Internal IMA function definitions */
  111. int ima_init(void);
  112. int ima_fs_init(void);
  113. int ima_add_template_entry(struct ima_template_entry *entry, int violation,
  114. const char *op, struct inode *inode,
  115. const unsigned char *filename);
  116. int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash);
  117. int ima_calc_buffer_hash(const void *buf, loff_t len,
  118. struct ima_digest_data *hash);
  119. int ima_calc_field_array_hash(struct ima_field_data *field_data,
  120. struct ima_template_desc *desc, int num_fields,
  121. struct ima_digest_data *hash);
  122. int ima_calc_boot_aggregate(struct ima_digest_data *hash);
  123. void ima_add_violation(struct file *file, const unsigned char *filename,
  124. struct integrity_iint_cache *iint,
  125. const char *op, const char *cause);
  126. int ima_init_crypto(void);
  127. void ima_putc(struct seq_file *m, void *data, int datalen);
  128. void ima_print_digest(struct seq_file *m, u8 *digest, u32 size);
  129. struct ima_template_desc *ima_template_desc_current(void);
  130. int ima_restore_measurement_entry(struct ima_template_entry *entry);
  131. int ima_restore_measurement_list(loff_t bufsize, void *buf);
  132. int ima_measurements_show(struct seq_file *m, void *v);
  133. unsigned long ima_get_binary_runtime_size(void);
  134. int ima_init_template(void);
  135. void ima_init_template_list(void);
  136. /*
  137. * used to protect h_table and sha_table
  138. */
  139. extern spinlock_t ima_queue_lock;
  140. struct ima_h_table {
  141. atomic_long_t len; /* number of stored measurements in the list */
  142. atomic_long_t violations;
  143. struct hlist_head queue[IMA_MEASURE_HTABLE_SIZE];
  144. };
  145. extern struct ima_h_table ima_htable;
  146. static inline unsigned int ima_hash_key(u8 *digest)
  147. {
  148. /* there is no point in taking a hash of part of a digest */
  149. return (digest[0] | digest[1] << 8) % IMA_MEASURE_HTABLE_SIZE;
  150. }
  151. #define __ima_hooks(hook) \
  152. hook(NONE) \
  153. hook(FILE_CHECK) \
  154. hook(MMAP_CHECK) \
  155. hook(BPRM_CHECK) \
  156. hook(CREDS_CHECK) \
  157. hook(POST_SETATTR) \
  158. hook(MODULE_CHECK) \
  159. hook(FIRMWARE_CHECK) \
  160. hook(KEXEC_KERNEL_CHECK) \
  161. hook(KEXEC_INITRAMFS_CHECK) \
  162. hook(POLICY_CHECK) \
  163. hook(MAX_CHECK)
  164. #define __ima_hook_enumify(ENUM) ENUM,
  165. enum ima_hooks {
  166. __ima_hooks(__ima_hook_enumify)
  167. };
  168. /* LIM API function definitions */
  169. int ima_get_action(struct inode *inode, const struct cred *cred, u32 secid,
  170. int mask, enum ima_hooks func, int *pcr);
  171. int ima_must_measure(struct inode *inode, int mask, enum ima_hooks func);
  172. int ima_collect_measurement(struct integrity_iint_cache *iint,
  173. struct file *file, void *buf, loff_t size,
  174. enum hash_algo algo);
  175. void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,
  176. const unsigned char *filename,
  177. struct evm_ima_xattr_data *xattr_value,
  178. int xattr_len, int pcr);
  179. void ima_audit_measurement(struct integrity_iint_cache *iint,
  180. const unsigned char *filename);
  181. int ima_alloc_init_template(struct ima_event_data *event_data,
  182. struct ima_template_entry **entry);
  183. int ima_store_template(struct ima_template_entry *entry, int violation,
  184. struct inode *inode,
  185. const unsigned char *filename, int pcr);
  186. void ima_free_template_entry(struct ima_template_entry *entry);
  187. const char *ima_d_path(const struct path *path, char **pathbuf, char *filename);
  188. /* IMA policy related functions */
  189. int ima_match_policy(struct inode *inode, const struct cred *cred, u32 secid,
  190. enum ima_hooks func, int mask, int flags, int *pcr);
  191. void ima_init_policy(void);
  192. void ima_update_policy(void);
  193. void ima_update_policy_flag(void);
  194. ssize_t ima_parse_add_rule(char *);
  195. void ima_delete_rules(void);
  196. int ima_check_policy(void);
  197. void *ima_policy_start(struct seq_file *m, loff_t *pos);
  198. void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos);
  199. void ima_policy_stop(struct seq_file *m, void *v);
  200. int ima_policy_show(struct seq_file *m, void *v);
  201. /* Appraise integrity measurements */
  202. #define IMA_APPRAISE_ENFORCE 0x01
  203. #define IMA_APPRAISE_FIX 0x02
  204. #define IMA_APPRAISE_LOG 0x04
  205. #define IMA_APPRAISE_MODULES 0x08
  206. #define IMA_APPRAISE_FIRMWARE 0x10
  207. #define IMA_APPRAISE_POLICY 0x20
  208. #define IMA_APPRAISE_KEXEC 0x40
  209. #ifdef CONFIG_IMA_APPRAISE
  210. int ima_appraise_measurement(enum ima_hooks func,
  211. struct integrity_iint_cache *iint,
  212. struct file *file, const unsigned char *filename,
  213. struct evm_ima_xattr_data *xattr_value,
  214. int xattr_len);
  215. int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func);
  216. void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file);
  217. enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
  218. enum ima_hooks func);
  219. enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
  220. int xattr_len);
  221. int ima_read_xattr(struct dentry *dentry,
  222. struct evm_ima_xattr_data **xattr_value);
  223. #else
  224. static inline int ima_appraise_measurement(enum ima_hooks func,
  225. struct integrity_iint_cache *iint,
  226. struct file *file,
  227. const unsigned char *filename,
  228. struct evm_ima_xattr_data *xattr_value,
  229. int xattr_len)
  230. {
  231. return INTEGRITY_UNKNOWN;
  232. }
  233. static inline int ima_must_appraise(struct inode *inode, int mask,
  234. enum ima_hooks func)
  235. {
  236. return 0;
  237. }
  238. static inline void ima_update_xattr(struct integrity_iint_cache *iint,
  239. struct file *file)
  240. {
  241. }
  242. static inline enum integrity_status ima_get_cache_status(struct integrity_iint_cache
  243. *iint,
  244. enum ima_hooks func)
  245. {
  246. return INTEGRITY_UNKNOWN;
  247. }
  248. static inline enum hash_algo
  249. ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len)
  250. {
  251. return ima_hash_algo;
  252. }
  253. static inline int ima_read_xattr(struct dentry *dentry,
  254. struct evm_ima_xattr_data **xattr_value)
  255. {
  256. return 0;
  257. }
  258. #endif /* CONFIG_IMA_APPRAISE */
  259. /* LSM based policy rules require audit */
  260. #ifdef CONFIG_IMA_LSM_RULES
  261. #define security_filter_rule_init security_audit_rule_init
  262. #define security_filter_rule_match security_audit_rule_match
  263. #else
  264. static inline int security_filter_rule_init(u32 field, u32 op, char *rulestr,
  265. void **lsmrule)
  266. {
  267. return -EINVAL;
  268. }
  269. static inline int security_filter_rule_match(u32 secid, u32 field, u32 op,
  270. void *lsmrule,
  271. struct audit_context *actx)
  272. {
  273. return -EINVAL;
  274. }
  275. #endif /* CONFIG_IMA_LSM_RULES */
  276. #ifdef CONFIG_IMA_READ_POLICY
  277. #define POLICY_FILE_FLAGS (S_IWUSR | S_IRUSR)
  278. #else
  279. #define POLICY_FILE_FLAGS S_IWUSR
  280. #endif /* CONFIG_IMA_READ_POLICY */
  281. #endif /* __LINUX_IMA_H */