ima.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2005,2006,2007,2008 IBM Corporation
  4. *
  5. * Authors:
  6. * Reiner Sailer <[email protected]>
  7. * Mimi Zohar <[email protected]>
  8. *
  9. * File: ima.h
  10. * internal Integrity Measurement Architecture (IMA) definitions
  11. */
  12. #ifndef __LINUX_IMA_H
  13. #define __LINUX_IMA_H
  14. #include <linux/types.h>
  15. #include <linux/crypto.h>
  16. #include <linux/fs.h>
  17. #include <linux/security.h>
  18. #include <linux/hash.h>
  19. #include <linux/tpm.h>
  20. #include <linux/audit.h>
  21. #include <crypto/hash_info.h>
  22. #include "../integrity.h"
  23. enum ima_show_type { IMA_SHOW_BINARY, IMA_SHOW_BINARY_NO_FIELD_LEN,
  24. IMA_SHOW_BINARY_OLD_STRING_FMT, IMA_SHOW_ASCII };
  25. enum tpm_pcrs { TPM_PCR0 = 0, TPM_PCR8 = 8, TPM_PCR10 = 10 };
  26. /* digest size for IMA, fits SHA1 or MD5 */
  27. #define IMA_DIGEST_SIZE SHA1_DIGEST_SIZE
  28. #define IMA_EVENT_NAME_LEN_MAX 255
  29. #define IMA_HASH_BITS 10
  30. #define IMA_MEASURE_HTABLE_SIZE (1 << IMA_HASH_BITS)
  31. #define IMA_TEMPLATE_FIELD_ID_MAX_LEN 16
  32. #define IMA_TEMPLATE_NUM_FIELDS_MAX 15
  33. #define IMA_TEMPLATE_IMA_NAME "ima"
  34. #define IMA_TEMPLATE_IMA_FMT "d|n"
  35. #define NR_BANKS(chip) ((chip != NULL) ? chip->nr_allocated_banks : 0)
  36. /* current content of the policy */
  37. extern int ima_policy_flag;
  38. /* bitset of digests algorithms allowed in the setxattr hook */
  39. extern atomic_t ima_setxattr_allowed_hash_algorithms;
  40. /* set during initialization */
  41. extern int ima_hash_algo __ro_after_init;
  42. extern int ima_sha1_idx __ro_after_init;
  43. extern int ima_hash_algo_idx __ro_after_init;
  44. extern int ima_extra_slots __ro_after_init;
  45. extern int ima_appraise;
  46. extern struct tpm_chip *ima_tpm_chip;
  47. extern const char boot_aggregate_name[];
  48. /* IMA event related data */
  49. struct ima_event_data {
  50. struct integrity_iint_cache *iint;
  51. struct file *file;
  52. const unsigned char *filename;
  53. struct evm_ima_xattr_data *xattr_value;
  54. int xattr_len;
  55. const struct modsig *modsig;
  56. const char *violation;
  57. const void *buf;
  58. int buf_len;
  59. };
  60. /* IMA template field data definition */
  61. struct ima_field_data {
  62. u8 *data;
  63. u32 len;
  64. };
  65. /* IMA template field definition */
  66. struct ima_template_field {
  67. const char field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN];
  68. int (*field_init)(struct ima_event_data *event_data,
  69. struct ima_field_data *field_data);
  70. void (*field_show)(struct seq_file *m, enum ima_show_type show,
  71. struct ima_field_data *field_data);
  72. };
  73. /* IMA template descriptor definition */
  74. struct ima_template_desc {
  75. struct list_head list;
  76. char *name;
  77. char *fmt;
  78. int num_fields;
  79. const struct ima_template_field **fields;
  80. };
  81. struct ima_template_entry {
  82. int pcr;
  83. struct tpm_digest *digests;
  84. struct ima_template_desc *template_desc; /* template descriptor */
  85. u32 template_data_len;
  86. struct ima_field_data template_data[]; /* template related data */
  87. };
  88. struct ima_queue_entry {
  89. struct hlist_node hnext; /* place in hash collision list */
  90. struct list_head later; /* place in ima_measurements list */
  91. struct ima_template_entry *entry;
  92. };
  93. extern struct list_head ima_measurements; /* list of all measurements */
  94. /* Some details preceding the binary serialized measurement list */
  95. struct ima_kexec_hdr {
  96. u16 version;
  97. u16 _reserved0;
  98. u32 _reserved1;
  99. u64 buffer_size;
  100. u64 count;
  101. };
  102. extern const int read_idmap[];
  103. #ifdef CONFIG_HAVE_IMA_KEXEC
  104. void ima_load_kexec_buffer(void);
  105. #else
  106. static inline void ima_load_kexec_buffer(void) {}
  107. #endif /* CONFIG_HAVE_IMA_KEXEC */
  108. /*
  109. * The default binary_runtime_measurements list format is defined as the
  110. * platform native format. The canonical format is defined as little-endian.
  111. */
  112. extern bool ima_canonical_fmt;
  113. /* Internal IMA function definitions */
  114. int ima_init(void);
  115. int ima_fs_init(void);
  116. int ima_add_template_entry(struct ima_template_entry *entry, int violation,
  117. const char *op, struct inode *inode,
  118. const unsigned char *filename);
  119. int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash);
  120. int ima_calc_buffer_hash(const void *buf, loff_t len,
  121. struct ima_digest_data *hash);
  122. int ima_calc_field_array_hash(struct ima_field_data *field_data,
  123. struct ima_template_entry *entry);
  124. int ima_calc_boot_aggregate(struct ima_digest_data *hash);
  125. void ima_add_violation(struct file *file, const unsigned char *filename,
  126. struct integrity_iint_cache *iint,
  127. const char *op, const char *cause);
  128. int ima_init_crypto(void);
  129. void ima_putc(struct seq_file *m, void *data, int datalen);
  130. void ima_print_digest(struct seq_file *m, u8 *digest, u32 size);
  131. int template_desc_init_fields(const char *template_fmt,
  132. const struct ima_template_field ***fields,
  133. int *num_fields);
  134. struct ima_template_desc *ima_template_desc_current(void);
  135. struct ima_template_desc *ima_template_desc_buf(void);
  136. struct ima_template_desc *lookup_template_desc(const char *name);
  137. bool ima_template_has_modsig(const struct ima_template_desc *ima_template);
  138. int ima_restore_measurement_entry(struct ima_template_entry *entry);
  139. int ima_restore_measurement_list(loff_t bufsize, void *buf);
  140. int ima_measurements_show(struct seq_file *m, void *v);
  141. unsigned long ima_get_binary_runtime_size(void);
  142. int ima_init_template(void);
  143. void ima_init_template_list(void);
  144. int __init ima_init_digests(void);
  145. int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
  146. void *lsm_data);
  147. /*
  148. * used to protect h_table and sha_table
  149. */
  150. extern spinlock_t ima_queue_lock;
  151. struct ima_h_table {
  152. atomic_long_t len; /* number of stored measurements in the list */
  153. atomic_long_t violations;
  154. struct hlist_head queue[IMA_MEASURE_HTABLE_SIZE];
  155. };
  156. extern struct ima_h_table ima_htable;
  157. static inline unsigned int ima_hash_key(u8 *digest)
  158. {
  159. /* there is no point in taking a hash of part of a digest */
  160. return (digest[0] | digest[1] << 8) % IMA_MEASURE_HTABLE_SIZE;
  161. }
  162. #define __ima_hooks(hook) \
  163. hook(NONE, none) \
  164. hook(FILE_CHECK, file) \
  165. hook(MMAP_CHECK, mmap) \
  166. hook(BPRM_CHECK, bprm) \
  167. hook(CREDS_CHECK, creds) \
  168. hook(POST_SETATTR, post_setattr) \
  169. hook(MODULE_CHECK, module) \
  170. hook(FIRMWARE_CHECK, firmware) \
  171. hook(KEXEC_KERNEL_CHECK, kexec_kernel) \
  172. hook(KEXEC_INITRAMFS_CHECK, kexec_initramfs) \
  173. hook(POLICY_CHECK, policy) \
  174. hook(KEXEC_CMDLINE, kexec_cmdline) \
  175. hook(KEY_CHECK, key) \
  176. hook(CRITICAL_DATA, critical_data) \
  177. hook(SETXATTR_CHECK, setxattr_check) \
  178. hook(MAX_CHECK, none)
  179. #define __ima_hook_enumify(ENUM, str) ENUM,
  180. #define __ima_stringify(arg) (#arg)
  181. #define __ima_hook_measuring_stringify(ENUM, str) \
  182. (__ima_stringify(measuring_ ##str)),
  183. enum ima_hooks {
  184. __ima_hooks(__ima_hook_enumify)
  185. };
  186. static const char * const ima_hooks_measure_str[] = {
  187. __ima_hooks(__ima_hook_measuring_stringify)
  188. };
  189. static inline const char *func_measure_str(enum ima_hooks func)
  190. {
  191. if (func >= MAX_CHECK)
  192. return ima_hooks_measure_str[NONE];
  193. return ima_hooks_measure_str[func];
  194. }
  195. extern const char *const func_tokens[];
  196. struct modsig;
  197. #ifdef CONFIG_IMA_QUEUE_EARLY_BOOT_KEYS
  198. /*
  199. * To track keys that need to be measured.
  200. */
  201. struct ima_key_entry {
  202. struct list_head list;
  203. void *payload;
  204. size_t payload_len;
  205. char *keyring_name;
  206. };
  207. void ima_init_key_queue(void);
  208. bool ima_should_queue_key(void);
  209. bool ima_queue_key(struct key *keyring, const void *payload,
  210. size_t payload_len);
  211. void ima_process_queued_keys(void);
  212. #else
  213. static inline void ima_init_key_queue(void) {}
  214. static inline bool ima_should_queue_key(void) { return false; }
  215. static inline bool ima_queue_key(struct key *keyring,
  216. const void *payload,
  217. size_t payload_len) { return false; }
  218. static inline void ima_process_queued_keys(void) {}
  219. #endif /* CONFIG_IMA_QUEUE_EARLY_BOOT_KEYS */
  220. /* LIM API function definitions */
  221. int ima_get_action(struct user_namespace *mnt_userns, struct inode *inode,
  222. const struct cred *cred, u32 secid, int mask,
  223. enum ima_hooks func, int *pcr,
  224. struct ima_template_desc **template_desc,
  225. const char *func_data, unsigned int *allowed_algos);
  226. int ima_must_measure(struct inode *inode, int mask, enum ima_hooks func);
  227. int ima_collect_measurement(struct integrity_iint_cache *iint,
  228. struct file *file, void *buf, loff_t size,
  229. enum hash_algo algo, struct modsig *modsig);
  230. void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,
  231. const unsigned char *filename,
  232. struct evm_ima_xattr_data *xattr_value,
  233. int xattr_len, const struct modsig *modsig, int pcr,
  234. struct ima_template_desc *template_desc);
  235. int process_buffer_measurement(struct user_namespace *mnt_userns,
  236. struct inode *inode, const void *buf, int size,
  237. const char *eventname, enum ima_hooks func,
  238. int pcr, const char *func_data,
  239. bool buf_hash, u8 *digest, size_t digest_len);
  240. void ima_audit_measurement(struct integrity_iint_cache *iint,
  241. const unsigned char *filename);
  242. int ima_alloc_init_template(struct ima_event_data *event_data,
  243. struct ima_template_entry **entry,
  244. struct ima_template_desc *template_desc);
  245. int ima_store_template(struct ima_template_entry *entry, int violation,
  246. struct inode *inode,
  247. const unsigned char *filename, int pcr);
  248. void ima_free_template_entry(struct ima_template_entry *entry);
  249. const char *ima_d_path(const struct path *path, char **pathbuf, char *filename);
  250. /* IMA policy related functions */
  251. int ima_match_policy(struct user_namespace *mnt_userns, struct inode *inode,
  252. const struct cred *cred, u32 secid, enum ima_hooks func,
  253. int mask, int flags, int *pcr,
  254. struct ima_template_desc **template_desc,
  255. const char *func_data, unsigned int *allowed_algos);
  256. void ima_init_policy(void);
  257. void ima_update_policy(void);
  258. void ima_update_policy_flags(void);
  259. ssize_t ima_parse_add_rule(char *);
  260. void ima_delete_rules(void);
  261. int ima_check_policy(void);
  262. void *ima_policy_start(struct seq_file *m, loff_t *pos);
  263. void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos);
  264. void ima_policy_stop(struct seq_file *m, void *v);
  265. int ima_policy_show(struct seq_file *m, void *v);
  266. /* Appraise integrity measurements */
  267. #define IMA_APPRAISE_ENFORCE 0x01
  268. #define IMA_APPRAISE_FIX 0x02
  269. #define IMA_APPRAISE_LOG 0x04
  270. #define IMA_APPRAISE_MODULES 0x08
  271. #define IMA_APPRAISE_FIRMWARE 0x10
  272. #define IMA_APPRAISE_POLICY 0x20
  273. #define IMA_APPRAISE_KEXEC 0x40
  274. #ifdef CONFIG_IMA_APPRAISE
  275. int ima_check_blacklist(struct integrity_iint_cache *iint,
  276. const struct modsig *modsig, int pcr);
  277. int ima_appraise_measurement(enum ima_hooks func,
  278. struct integrity_iint_cache *iint,
  279. struct file *file, const unsigned char *filename,
  280. struct evm_ima_xattr_data *xattr_value,
  281. int xattr_len, const struct modsig *modsig);
  282. int ima_must_appraise(struct user_namespace *mnt_userns, struct inode *inode,
  283. int mask, enum ima_hooks func);
  284. void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file);
  285. enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
  286. enum ima_hooks func);
  287. enum hash_algo ima_get_hash_algo(const struct evm_ima_xattr_data *xattr_value,
  288. int xattr_len);
  289. int ima_read_xattr(struct dentry *dentry,
  290. struct evm_ima_xattr_data **xattr_value);
  291. #else
  292. static inline int ima_check_blacklist(struct integrity_iint_cache *iint,
  293. const struct modsig *modsig, int pcr)
  294. {
  295. return 0;
  296. }
  297. static inline int ima_appraise_measurement(enum ima_hooks func,
  298. struct integrity_iint_cache *iint,
  299. struct file *file,
  300. const unsigned char *filename,
  301. struct evm_ima_xattr_data *xattr_value,
  302. int xattr_len,
  303. const struct modsig *modsig)
  304. {
  305. return INTEGRITY_UNKNOWN;
  306. }
  307. static inline int ima_must_appraise(struct user_namespace *mnt_userns,
  308. struct inode *inode, int mask,
  309. enum ima_hooks func)
  310. {
  311. return 0;
  312. }
  313. static inline void ima_update_xattr(struct integrity_iint_cache *iint,
  314. struct file *file)
  315. {
  316. }
  317. static inline enum integrity_status ima_get_cache_status(struct integrity_iint_cache
  318. *iint,
  319. enum ima_hooks func)
  320. {
  321. return INTEGRITY_UNKNOWN;
  322. }
  323. static inline enum hash_algo
  324. ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len)
  325. {
  326. return ima_hash_algo;
  327. }
  328. static inline int ima_read_xattr(struct dentry *dentry,
  329. struct evm_ima_xattr_data **xattr_value)
  330. {
  331. return 0;
  332. }
  333. #endif /* CONFIG_IMA_APPRAISE */
  334. #ifdef CONFIG_IMA_APPRAISE_MODSIG
  335. int ima_read_modsig(enum ima_hooks func, const void *buf, loff_t buf_len,
  336. struct modsig **modsig);
  337. void ima_collect_modsig(struct modsig *modsig, const void *buf, loff_t size);
  338. int ima_get_modsig_digest(const struct modsig *modsig, enum hash_algo *algo,
  339. const u8 **digest, u32 *digest_size);
  340. int ima_get_raw_modsig(const struct modsig *modsig, const void **data,
  341. u32 *data_len);
  342. void ima_free_modsig(struct modsig *modsig);
  343. #else
  344. static inline int ima_read_modsig(enum ima_hooks func, const void *buf,
  345. loff_t buf_len, struct modsig **modsig)
  346. {
  347. return -EOPNOTSUPP;
  348. }
  349. static inline void ima_collect_modsig(struct modsig *modsig, const void *buf,
  350. loff_t size)
  351. {
  352. }
  353. static inline int ima_get_modsig_digest(const struct modsig *modsig,
  354. enum hash_algo *algo, const u8 **digest,
  355. u32 *digest_size)
  356. {
  357. return -EOPNOTSUPP;
  358. }
  359. static inline int ima_get_raw_modsig(const struct modsig *modsig,
  360. const void **data, u32 *data_len)
  361. {
  362. return -EOPNOTSUPP;
  363. }
  364. static inline void ima_free_modsig(struct modsig *modsig)
  365. {
  366. }
  367. #endif /* CONFIG_IMA_APPRAISE_MODSIG */
  368. /* LSM based policy rules require audit */
  369. #ifdef CONFIG_IMA_LSM_RULES
  370. #define ima_filter_rule_init security_audit_rule_init
  371. #define ima_filter_rule_free security_audit_rule_free
  372. #define ima_filter_rule_match security_audit_rule_match
  373. #else
  374. static inline int ima_filter_rule_init(u32 field, u32 op, char *rulestr,
  375. void **lsmrule)
  376. {
  377. return -EINVAL;
  378. }
  379. static inline void ima_filter_rule_free(void *lsmrule)
  380. {
  381. }
  382. static inline int ima_filter_rule_match(u32 secid, u32 field, u32 op,
  383. void *lsmrule)
  384. {
  385. return -EINVAL;
  386. }
  387. #endif /* CONFIG_IMA_LSM_RULES */
  388. #ifdef CONFIG_IMA_READ_POLICY
  389. #define POLICY_FILE_FLAGS (S_IWUSR | S_IRUSR)
  390. #else
  391. #define POLICY_FILE_FLAGS S_IWUSR
  392. #endif /* CONFIG_IMA_READ_POLICY */
  393. #endif /* __LINUX_IMA_H */