xref: /openbmc/linux/security/integrity/ima/ima.h (revision d26e1936)
13323eec9SMimi Zohar /*
23323eec9SMimi Zohar  * Copyright (C) 2005,2006,2007,2008 IBM Corporation
33323eec9SMimi Zohar  *
43323eec9SMimi Zohar  * Authors:
53323eec9SMimi Zohar  * Reiner Sailer <sailer@watson.ibm.com>
63323eec9SMimi Zohar  * Mimi Zohar <zohar@us.ibm.com>
73323eec9SMimi Zohar  *
83323eec9SMimi Zohar  * This program is free software; you can redistribute it and/or
93323eec9SMimi Zohar  * modify it under the terms of the GNU General Public License as
103323eec9SMimi Zohar  * published by the Free Software Foundation, version 2 of the
113323eec9SMimi Zohar  * License.
123323eec9SMimi Zohar  *
133323eec9SMimi Zohar  * File: ima.h
143323eec9SMimi Zohar  *	internal Integrity Measurement Architecture (IMA) definitions
153323eec9SMimi Zohar  */
163323eec9SMimi Zohar 
173323eec9SMimi Zohar #ifndef __LINUX_IMA_H
183323eec9SMimi Zohar #define __LINUX_IMA_H
193323eec9SMimi Zohar 
203323eec9SMimi Zohar #include <linux/types.h>
213323eec9SMimi Zohar #include <linux/crypto.h>
223323eec9SMimi Zohar #include <linux/security.h>
233323eec9SMimi Zohar #include <linux/hash.h>
243323eec9SMimi Zohar #include <linux/tpm.h>
253323eec9SMimi Zohar #include <linux/audit.h>
263323eec9SMimi Zohar 
27f381c272SMimi Zohar #include "../integrity.h"
28f381c272SMimi Zohar 
293323eec9SMimi Zohar enum ima_show_type { IMA_SHOW_BINARY, IMA_SHOW_ASCII };
303323eec9SMimi Zohar enum tpm_pcrs { TPM_PCR0 = 0, TPM_PCR8 = 8 };
313323eec9SMimi Zohar 
323323eec9SMimi Zohar /* digest size for IMA, fits SHA1 or MD5 */
33f381c272SMimi Zohar #define IMA_DIGEST_SIZE		SHA1_DIGEST_SIZE
343323eec9SMimi Zohar #define IMA_EVENT_NAME_LEN_MAX	255
353323eec9SMimi Zohar 
363323eec9SMimi Zohar #define IMA_HASH_BITS 9
373323eec9SMimi Zohar #define IMA_MEASURE_HTABLE_SIZE (1 << IMA_HASH_BITS)
383323eec9SMimi Zohar 
393323eec9SMimi Zohar /* set during initialization */
403323eec9SMimi Zohar extern int ima_initialized;
413323eec9SMimi Zohar extern int ima_used_chip;
423323eec9SMimi Zohar extern char *ima_hash;
432fe5d6deSMimi Zohar extern int ima_appraise;
443323eec9SMimi Zohar 
453323eec9SMimi Zohar /* IMA inode template definition */
463323eec9SMimi Zohar struct ima_template_data {
473323eec9SMimi Zohar 	u8 digest[IMA_DIGEST_SIZE];	/* sha1/md5 measurement hash */
483323eec9SMimi Zohar 	char file_name[IMA_EVENT_NAME_LEN_MAX + 1];	/* name + \0 */
493323eec9SMimi Zohar };
503323eec9SMimi Zohar 
513323eec9SMimi Zohar struct ima_template_entry {
523323eec9SMimi Zohar 	u8 digest[IMA_DIGEST_SIZE];	/* sha1 or md5 measurement hash */
53523979adSMimi Zohar 	const char *template_name;
543323eec9SMimi Zohar 	int template_len;
553323eec9SMimi Zohar 	struct ima_template_data template;
563323eec9SMimi Zohar };
573323eec9SMimi Zohar 
583323eec9SMimi Zohar struct ima_queue_entry {
593323eec9SMimi Zohar 	struct hlist_node hnext;	/* place in hash collision list */
603323eec9SMimi Zohar 	struct list_head later;		/* place in ima_measurements list */
613323eec9SMimi Zohar 	struct ima_template_entry *entry;
623323eec9SMimi Zohar };
633323eec9SMimi Zohar extern struct list_head ima_measurements;	/* list of all measurements */
643323eec9SMimi Zohar 
65417c6c8eSDmitry Kasatkin #ifdef CONFIG_IMA_AUDIT
663323eec9SMimi Zohar /* declarations */
673323eec9SMimi Zohar void integrity_audit_msg(int audit_msgno, struct inode *inode,
683323eec9SMimi Zohar 			 const unsigned char *fname, const char *op,
693323eec9SMimi Zohar 			 const char *cause, int result, int info);
70417c6c8eSDmitry Kasatkin #else
71417c6c8eSDmitry Kasatkin static inline void integrity_audit_msg(int audit_msgno, struct inode *inode,
72417c6c8eSDmitry Kasatkin 				       const unsigned char *fname,
73417c6c8eSDmitry Kasatkin 				       const char *op, const char *cause,
74417c6c8eSDmitry Kasatkin 				       int result, int info)
75417c6c8eSDmitry Kasatkin {
76417c6c8eSDmitry Kasatkin }
77417c6c8eSDmitry Kasatkin #endif
783323eec9SMimi Zohar 
793323eec9SMimi Zohar /* Internal IMA function definitions */
803323eec9SMimi Zohar int ima_init(void);
81bab73937SMimi Zohar void ima_cleanup(void);
82bab73937SMimi Zohar int ima_fs_init(void);
83bab73937SMimi Zohar void ima_fs_cleanup(void);
84a178d202SEric Paris int ima_inode_alloc(struct inode *inode);
853323eec9SMimi Zohar int ima_add_template_entry(struct ima_template_entry *entry, int violation,
863323eec9SMimi Zohar 			   const char *op, struct inode *inode);
873323eec9SMimi Zohar int ima_calc_hash(struct file *file, char *digest);
883323eec9SMimi Zohar int ima_calc_template_hash(int template_len, void *template, char *digest);
893323eec9SMimi Zohar int ima_calc_boot_aggregate(char *digest);
903323eec9SMimi Zohar void ima_add_violation(struct inode *inode, const unsigned char *filename,
913323eec9SMimi Zohar 		       const char *op, const char *cause);
923323eec9SMimi Zohar 
933323eec9SMimi Zohar /*
943323eec9SMimi Zohar  * used to protect h_table and sha_table
953323eec9SMimi Zohar  */
963323eec9SMimi Zohar extern spinlock_t ima_queue_lock;
973323eec9SMimi Zohar 
983323eec9SMimi Zohar struct ima_h_table {
993323eec9SMimi Zohar 	atomic_long_t len;	/* number of stored measurements in the list */
1003323eec9SMimi Zohar 	atomic_long_t violations;
1013323eec9SMimi Zohar 	struct hlist_head queue[IMA_MEASURE_HTABLE_SIZE];
1023323eec9SMimi Zohar };
1033323eec9SMimi Zohar extern struct ima_h_table ima_htable;
1043323eec9SMimi Zohar 
1053323eec9SMimi Zohar static inline unsigned long ima_hash_key(u8 *digest)
1063323eec9SMimi Zohar {
1073323eec9SMimi Zohar 	return hash_long(*digest, IMA_HASH_BITS);
1083323eec9SMimi Zohar }
1093323eec9SMimi Zohar 
1103323eec9SMimi Zohar /* LIM API function definitions */
111d9d300cdSDmitry Kasatkin int ima_get_action(struct inode *inode, int mask, int function);
1121adace9bSMimi Zohar int ima_must_measure(struct inode *inode, int mask, int function);
113f381c272SMimi Zohar int ima_collect_measurement(struct integrity_iint_cache *iint,
114f381c272SMimi Zohar 			    struct file *file);
115f381c272SMimi Zohar void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,
1163323eec9SMimi Zohar 			   const unsigned char *filename);
117e7c568e0SPeter Moody void ima_audit_measurement(struct integrity_iint_cache *iint,
118e7c568e0SPeter Moody 			   const unsigned char *filename);
1193323eec9SMimi Zohar int ima_store_template(struct ima_template_entry *entry, int violation,
1203323eec9SMimi Zohar 		       struct inode *inode);
121f381c272SMimi Zohar void ima_template_show(struct seq_file *m, void *e, enum ima_show_type show);
1223323eec9SMimi Zohar 
12385491641SEric Paris /* rbtree tree calls to lookup, insert, delete
1243323eec9SMimi Zohar  * integrity data associated with an inode.
1253323eec9SMimi Zohar  */
126f381c272SMimi Zohar struct integrity_iint_cache *integrity_iint_insert(struct inode *inode);
127f381c272SMimi Zohar struct integrity_iint_cache *integrity_iint_find(struct inode *inode);
1283323eec9SMimi Zohar 
1293323eec9SMimi Zohar /* IMA policy related functions */
1302fe5d6deSMimi Zohar enum ima_hooks { FILE_CHECK = 1, FILE_MMAP, BPRM_CHECK, POST_SETATTR };
1313323eec9SMimi Zohar 
1322fe5d6deSMimi Zohar int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
1332fe5d6deSMimi Zohar 		     int flags);
1343323eec9SMimi Zohar void ima_init_policy(void);
1353323eec9SMimi Zohar void ima_update_policy(void);
1366ccd0456SEric Paris ssize_t ima_parse_add_rule(char *);
1374af4662fSMimi Zohar void ima_delete_rules(void);
1384af4662fSMimi Zohar 
1392fe5d6deSMimi Zohar /* Appraise integrity measurements */
1402fe5d6deSMimi Zohar #define IMA_APPRAISE_ENFORCE	0x01
1412fe5d6deSMimi Zohar #define IMA_APPRAISE_FIX	0x02
1422fe5d6deSMimi Zohar 
1432fe5d6deSMimi Zohar #ifdef CONFIG_IMA_APPRAISE
1442fe5d6deSMimi Zohar int ima_appraise_measurement(struct integrity_iint_cache *iint,
1452fe5d6deSMimi Zohar 			     struct file *file, const unsigned char *filename);
146d26e1936SDmitry Kasatkin int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func);
1472fe5d6deSMimi Zohar void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file);
1482fe5d6deSMimi Zohar 
1492fe5d6deSMimi Zohar #else
1502fe5d6deSMimi Zohar static inline int ima_appraise_measurement(struct integrity_iint_cache *iint,
1512fe5d6deSMimi Zohar 					   struct file *file,
1522fe5d6deSMimi Zohar 					   const unsigned char *filename)
1532fe5d6deSMimi Zohar {
1542fe5d6deSMimi Zohar 	return INTEGRITY_UNKNOWN;
1552fe5d6deSMimi Zohar }
1562fe5d6deSMimi Zohar 
157d26e1936SDmitry Kasatkin static inline int ima_must_appraise(struct inode *inode, int mask,
158d26e1936SDmitry Kasatkin 				    enum ima_hooks func)
1592fe5d6deSMimi Zohar {
1602fe5d6deSMimi Zohar 	return 0;
1612fe5d6deSMimi Zohar }
1622fe5d6deSMimi Zohar 
1632fe5d6deSMimi Zohar static inline void ima_update_xattr(struct integrity_iint_cache *iint,
1642fe5d6deSMimi Zohar 				    struct file *file)
1652fe5d6deSMimi Zohar {
1662fe5d6deSMimi Zohar }
1672fe5d6deSMimi Zohar #endif
1682fe5d6deSMimi Zohar 
1694af4662fSMimi Zohar /* LSM based policy rules require audit */
1704af4662fSMimi Zohar #ifdef CONFIG_IMA_LSM_RULES
1714af4662fSMimi Zohar 
1724af4662fSMimi Zohar #define security_filter_rule_init security_audit_rule_init
1734af4662fSMimi Zohar #define security_filter_rule_match security_audit_rule_match
1744af4662fSMimi Zohar 
1754af4662fSMimi Zohar #else
1764af4662fSMimi Zohar 
1774af4662fSMimi Zohar static inline int security_filter_rule_init(u32 field, u32 op, char *rulestr,
1784af4662fSMimi Zohar 					    void **lsmrule)
1794af4662fSMimi Zohar {
1804af4662fSMimi Zohar 	return -EINVAL;
1814af4662fSMimi Zohar }
1824af4662fSMimi Zohar 
1834af4662fSMimi Zohar static inline int security_filter_rule_match(u32 secid, u32 field, u32 op,
1844af4662fSMimi Zohar 					     void *lsmrule,
1854af4662fSMimi Zohar 					     struct audit_context *actx)
1864af4662fSMimi Zohar {
1874af4662fSMimi Zohar 	return -EINVAL;
1884af4662fSMimi Zohar }
1894af4662fSMimi Zohar #endif /* CONFIG_IMA_LSM_RULES */
1903323eec9SMimi Zohar #endif
191