ima.h 7.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright (C) 2005,2006,2007,2008 IBM Corporation
 *
 * Authors:
 * Reiner Sailer <sailer@watson.ibm.com>
 * Mimi Zohar <zohar@us.ibm.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, version 2 of the
 * License.
 *
 * File: ima.h
 *	internal Integrity Measurement Architecture (IMA) definitions
 */

#ifndef __LINUX_IMA_H
#define __LINUX_IMA_H

#include <linux/types.h>
#include <linux/crypto.h>
#include <linux/security.h>
#include <linux/hash.h>
#include <linux/tpm.h>
#include <linux/audit.h>

27 28
#include "../integrity.h"

29
enum ima_show_type { IMA_SHOW_BINARY, IMA_SHOW_BINARY_NO_FIELD_LEN,
30
		     IMA_SHOW_BINARY_OLD_STRING_FMT, IMA_SHOW_ASCII };
31 32 33
enum tpm_pcrs { TPM_PCR0 = 0, TPM_PCR8 = 8 };

/* digest size for IMA, fits SHA1 or MD5 */
34
#define IMA_DIGEST_SIZE		SHA1_DIGEST_SIZE
35 36 37 38 39
#define IMA_EVENT_NAME_LEN_MAX	255

#define IMA_HASH_BITS 9
#define IMA_MEASURE_HTABLE_SIZE (1 << IMA_HASH_BITS)

40 41 42
#define IMA_TEMPLATE_FIELD_ID_MAX_LEN	16
#define IMA_TEMPLATE_NUM_FIELDS_MAX	15

43 44 45
#define IMA_TEMPLATE_IMA_NAME "ima"
#define IMA_TEMPLATE_IMA_FMT "d|n"

46 47 48
/* current content of the policy */
extern int ima_policy_flag;

49 50 51
/* set during initialization */
extern int ima_initialized;
extern int ima_used_chip;
52
extern int ima_hash_algo;
M
Mimi Zohar 已提交
53
extern int ima_appraise;
54

55 56 57 58 59 60 61 62 63 64 65
/* IMA template field data definition */
struct ima_field_data {
	u8 *data;
	u32 len;
};

/* IMA template field definition */
struct ima_template_field {
	const char field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN];
	int (*field_init) (struct integrity_iint_cache *iint, struct file *file,
			   const unsigned char *filename,
66 67
			   struct evm_ima_xattr_data *xattr_value,
			   int xattr_len, struct ima_field_data *field_data);
68 69 70 71 72 73 74 75 76 77 78 79
	void (*field_show) (struct seq_file *m, enum ima_show_type show,
			    struct ima_field_data *field_data);
};

/* IMA template descriptor definition */
struct ima_template_desc {
	char *name;
	char *fmt;
	int num_fields;
	struct ima_template_field **fields;
};

80
struct ima_template_entry {
81
	u8 digest[TPM_DIGEST_SIZE];	/* sha1 or md5 measurement hash */
82 83 84
	struct ima_template_desc *template_desc; /* template descriptor */
	u32 template_data_len;
	struct ima_field_data template_data[0];	/* template related data */
85 86 87 88 89 90 91 92 93 94 95
};

struct ima_queue_entry {
	struct hlist_node hnext;	/* place in hash collision list */
	struct list_head later;		/* place in ima_measurements list */
	struct ima_template_entry *entry;
};
extern struct list_head ima_measurements;	/* list of all measurements */

/* Internal IMA function definitions */
int ima_init(void);
M
Mimi Zohar 已提交
96
int ima_fs_init(void);
97
int ima_add_template_entry(struct ima_template_entry *entry, int violation,
98 99
			   const char *op, struct inode *inode,
			   const unsigned char *filename);
100
int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash);
101 102
int ima_calc_field_array_hash(struct ima_field_data *field_data,
			      struct ima_template_desc *desc, int num_fields,
103
			      struct ima_digest_data *hash);
104
int __init ima_calc_boot_aggregate(struct ima_digest_data *hash);
105
void ima_add_violation(struct file *file, const unsigned char *filename,
106
		       const char *op, const char *cause);
107
int ima_init_crypto(void);
108 109
void ima_putc(struct seq_file *m, void *data, int datalen);
void ima_print_digest(struct seq_file *m, u8 *digest, int size);
110 111
struct ima_template_desc *ima_template_desc_current(void);
int ima_init_template(void);
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

/*
 * used to protect h_table and sha_table
 */
extern spinlock_t ima_queue_lock;

struct ima_h_table {
	atomic_long_t len;	/* number of stored measurements in the list */
	atomic_long_t violations;
	struct hlist_head queue[IMA_MEASURE_HTABLE_SIZE];
};
extern struct ima_h_table ima_htable;

static inline unsigned long ima_hash_key(u8 *digest)
{
	return hash_long(*digest, IMA_HASH_BITS);
}

/* LIM API function definitions */
131
int ima_get_action(struct inode *inode, int mask, int function);
132
int ima_must_measure(struct inode *inode, int mask, int function);
133
int ima_collect_measurement(struct integrity_iint_cache *iint,
134 135 136
			    struct file *file,
			    struct evm_ima_xattr_data **xattr_value,
			    int *xattr_len);
137
void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,
138 139 140
			   const unsigned char *filename,
			   struct evm_ima_xattr_data *xattr_value,
			   int xattr_len);
P
Peter Moody 已提交
141 142
void ima_audit_measurement(struct integrity_iint_cache *iint,
			   const unsigned char *filename);
143 144
int ima_alloc_init_template(struct integrity_iint_cache *iint,
			    struct file *file, const unsigned char *filename,
145 146
			    struct evm_ima_xattr_data *xattr_value,
			    int xattr_len, struct ima_template_entry **entry);
147
int ima_store_template(struct ima_template_entry *entry, int violation,
148
		       struct inode *inode, const unsigned char *filename);
149
void ima_free_template_entry(struct ima_template_entry *entry);
150
const char *ima_d_path(struct path *path, char **pathbuf);
151 152

/* IMA policy related functions */
153
enum ima_hooks { FILE_CHECK = 1, MMAP_CHECK, BPRM_CHECK, MODULE_CHECK, FIRMWARE_CHECK, POST_SETATTR };
154

M
Mimi Zohar 已提交
155 156
int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
		     int flags);
157 158
void ima_init_policy(void);
void ima_update_policy(void);
159
void ima_update_policy_flag(void);
160
ssize_t ima_parse_add_rule(char *);
M
Mimi Zohar 已提交
161 162
void ima_delete_rules(void);

M
Mimi Zohar 已提交
163 164 165
/* Appraise integrity measurements */
#define IMA_APPRAISE_ENFORCE	0x01
#define IMA_APPRAISE_FIX	0x02
166 167 168
#define IMA_APPRAISE_LOG	0x04
#define IMA_APPRAISE_MODULES	0x08
#define IMA_APPRAISE_FIRMWARE	0x10
M
Mimi Zohar 已提交
169 170

#ifdef CONFIG_IMA_APPRAISE
171
int ima_appraise_measurement(int func, struct integrity_iint_cache *iint,
172 173
			     struct file *file, const unsigned char *filename,
			     struct evm_ima_xattr_data *xattr_value,
174
			     int xattr_len, int opened);
D
Dmitry Kasatkin 已提交
175
int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func);
M
Mimi Zohar 已提交
176
void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file);
177 178
enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
					   int func);
179 180 181 182
void ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len,
		       struct ima_digest_data *hash);
int ima_read_xattr(struct dentry *dentry,
		   struct evm_ima_xattr_data **xattr_value);
M
Mimi Zohar 已提交
183 184

#else
185 186
static inline int ima_appraise_measurement(int func,
					   struct integrity_iint_cache *iint,
M
Mimi Zohar 已提交
187
					   struct file *file,
188 189
					   const unsigned char *filename,
					   struct evm_ima_xattr_data *xattr_value,
190
					   int xattr_len, int opened)
M
Mimi Zohar 已提交
191 192 193 194
{
	return INTEGRITY_UNKNOWN;
}

D
Dmitry Kasatkin 已提交
195 196
static inline int ima_must_appraise(struct inode *inode, int mask,
				    enum ima_hooks func)
M
Mimi Zohar 已提交
197 198 199 200 201 202 203 204
{
	return 0;
}

static inline void ima_update_xattr(struct integrity_iint_cache *iint,
				    struct file *file)
{
}
205 206 207 208 209 210

static inline enum integrity_status ima_get_cache_status(struct integrity_iint_cache
							 *iint, int func)
{
	return INTEGRITY_UNKNOWN;
}
211 212 213 214 215 216 217 218 219 220 221 222 223

static inline void ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
				     int xattr_len,
				     struct ima_digest_data *hash)
{
}

static inline int ima_read_xattr(struct dentry *dentry,
				 struct evm_ima_xattr_data **xattr_value)
{
	return 0;
}

M
Mimi Zohar 已提交
224 225
#endif

M
Mimi Zohar 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
/* LSM based policy rules require audit */
#ifdef CONFIG_IMA_LSM_RULES

#define security_filter_rule_init security_audit_rule_init
#define security_filter_rule_match security_audit_rule_match

#else

static inline int security_filter_rule_init(u32 field, u32 op, char *rulestr,
					    void **lsmrule)
{
	return -EINVAL;
}

static inline int security_filter_rule_match(u32 secid, u32 field, u32 op,
					     void *lsmrule,
					     struct audit_context *actx)
{
	return -EINVAL;
}
#endif /* CONFIG_IMA_LSM_RULES */
247 248 249 250 251 252 253 254 255 256 257 258

#ifdef CONFIG_IMA_TRUSTED_KEYRING
static inline int ima_init_keyring(const unsigned int id)
{
	return integrity_init_keyring(id);
}
#else
static inline int ima_init_keyring(const unsigned int id)
{
	return 0;
}
#endif /* CONFIG_IMA_TRUSTED_KEYRING */
259
#endif