1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2005,2006,2007,2008 IBM Corporation
6 * Reiner Sailer <sailer@watson.ibm.com>
7 * Mimi Zohar <zohar@us.ibm.com>
10 * internal Integrity Measurement Architecture (IMA) definitions
16 #include <linux/types.h>
17 #include <linux/crypto.h>
19 #include <linux/security.h>
20 #include <linux/hash.h>
21 #include <linux/tpm.h>
22 #include <linux/audit.h>
23 #include <crypto/hash_info.h>
25 #include "../integrity.h"
27 #ifdef CONFIG_HAVE_IMA_KEXEC
31 enum ima_show_type { IMA_SHOW_BINARY, IMA_SHOW_BINARY_NO_FIELD_LEN,
32 IMA_SHOW_BINARY_OLD_STRING_FMT, IMA_SHOW_ASCII };
33 enum tpm_pcrs { TPM_PCR0 = 0, TPM_PCR8 = 8, TPM_PCR10 = 10 };
35 /* digest size for IMA, fits SHA1 or MD5 */
36 #define IMA_DIGEST_SIZE SHA1_DIGEST_SIZE
37 #define IMA_EVENT_NAME_LEN_MAX 255
39 #define IMA_HASH_BITS 10
40 #define IMA_MEASURE_HTABLE_SIZE (1 << IMA_HASH_BITS)
42 #define IMA_TEMPLATE_FIELD_ID_MAX_LEN 16
43 #define IMA_TEMPLATE_NUM_FIELDS_MAX 15
45 #define IMA_TEMPLATE_IMA_NAME "ima"
46 #define IMA_TEMPLATE_IMA_FMT "d|n"
48 #define NR_BANKS(chip) ((chip != NULL) ? chip->nr_allocated_banks : 0)
50 /* current content of the policy */
51 extern int ima_policy_flag;
53 /* set during initialization */
54 extern int ima_hash_algo;
55 extern int ima_sha1_idx __ro_after_init;
56 extern int ima_hash_algo_idx __ro_after_init;
57 extern int ima_extra_slots __ro_after_init;
58 extern int ima_appraise;
59 extern struct tpm_chip *ima_tpm_chip;
60 extern const char boot_aggregate_name[];
62 /* IMA event related data */
63 struct ima_event_data {
64 struct integrity_iint_cache *iint;
66 const unsigned char *filename;
67 struct evm_ima_xattr_data *xattr_value;
69 const struct modsig *modsig;
70 const char *violation;
75 /* IMA template field data definition */
76 struct ima_field_data {
81 /* IMA template field definition */
82 struct ima_template_field {
83 const char field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN];
84 int (*field_init)(struct ima_event_data *event_data,
85 struct ima_field_data *field_data);
86 void (*field_show)(struct seq_file *m, enum ima_show_type show,
87 struct ima_field_data *field_data);
90 /* IMA template descriptor definition */
91 struct ima_template_desc {
92 struct list_head list;
96 const struct ima_template_field **fields;
99 struct ima_template_entry {
101 struct tpm_digest *digests;
102 struct ima_template_desc *template_desc; /* template descriptor */
103 u32 template_data_len;
104 struct ima_field_data template_data[]; /* template related data */
107 struct ima_queue_entry {
108 struct hlist_node hnext; /* place in hash collision list */
109 struct list_head later; /* place in ima_measurements list */
110 struct ima_template_entry *entry;
112 extern struct list_head ima_measurements; /* list of all measurements */
114 /* Some details preceding the binary serialized measurement list */
115 struct ima_kexec_hdr {
123 extern const int read_idmap[];
125 #ifdef CONFIG_HAVE_IMA_KEXEC
126 void ima_load_kexec_buffer(void);
128 static inline void ima_load_kexec_buffer(void) {}
129 #endif /* CONFIG_HAVE_IMA_KEXEC */
132 * The default binary_runtime_measurements list format is defined as the
133 * platform native format. The canonical format is defined as little-endian.
135 extern bool ima_canonical_fmt;
137 /* Internal IMA function definitions */
139 int ima_fs_init(void);
140 int ima_add_template_entry(struct ima_template_entry *entry, int violation,
141 const char *op, struct inode *inode,
142 const unsigned char *filename);
143 int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash);
144 int ima_calc_buffer_hash(const void *buf, loff_t len,
145 struct ima_digest_data *hash);
146 int ima_calc_field_array_hash(struct ima_field_data *field_data,
147 struct ima_template_entry *entry);
148 int ima_calc_boot_aggregate(struct ima_digest_data *hash);
149 void ima_add_violation(struct file *file, const unsigned char *filename,
150 struct integrity_iint_cache *iint,
151 const char *op, const char *cause);
152 int ima_init_crypto(void);
153 void ima_putc(struct seq_file *m, void *data, int datalen);
154 void ima_print_digest(struct seq_file *m, u8 *digest, u32 size);
155 int template_desc_init_fields(const char *template_fmt,
156 const struct ima_template_field ***fields,
158 struct ima_template_desc *ima_template_desc_current(void);
159 struct ima_template_desc *ima_template_desc_buf(void);
160 struct ima_template_desc *lookup_template_desc(const char *name);
161 bool ima_template_has_modsig(const struct ima_template_desc *ima_template);
162 int ima_restore_measurement_entry(struct ima_template_entry *entry);
163 int ima_restore_measurement_list(loff_t bufsize, void *buf);
164 int ima_measurements_show(struct seq_file *m, void *v);
165 unsigned long ima_get_binary_runtime_size(void);
166 int ima_init_template(void);
167 void ima_init_template_list(void);
168 int __init ima_init_digests(void);
169 int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
173 * used to protect h_table and sha_table
175 extern spinlock_t ima_queue_lock;
178 atomic_long_t len; /* number of stored measurements in the list */
179 atomic_long_t violations;
180 struct hlist_head queue[IMA_MEASURE_HTABLE_SIZE];
182 extern struct ima_h_table ima_htable;
184 static inline unsigned int ima_hash_key(u8 *digest)
186 /* there is no point in taking a hash of part of a digest */
187 return (digest[0] | digest[1] << 8) % IMA_MEASURE_HTABLE_SIZE;
190 #define __ima_hooks(hook) \
192 hook(FILE_CHECK, file) \
193 hook(MMAP_CHECK, mmap) \
194 hook(BPRM_CHECK, bprm) \
195 hook(CREDS_CHECK, creds) \
196 hook(POST_SETATTR, post_setattr) \
197 hook(MODULE_CHECK, module) \
198 hook(FIRMWARE_CHECK, firmware) \
199 hook(KEXEC_KERNEL_CHECK, kexec_kernel) \
200 hook(KEXEC_INITRAMFS_CHECK, kexec_initramfs) \
201 hook(POLICY_CHECK, policy) \
202 hook(KEXEC_CMDLINE, kexec_cmdline) \
203 hook(KEY_CHECK, key) \
204 hook(CRITICAL_DATA, critical_data) \
205 hook(MAX_CHECK, none)
207 #define __ima_hook_enumify(ENUM, str) ENUM,
208 #define __ima_stringify(arg) (#arg)
209 #define __ima_hook_measuring_stringify(ENUM, str) \
210 (__ima_stringify(measuring_ ##str)),
213 __ima_hooks(__ima_hook_enumify)
216 static const char * const ima_hooks_measure_str[] = {
217 __ima_hooks(__ima_hook_measuring_stringify)
220 static inline const char *func_measure_str(enum ima_hooks func)
222 if (func >= MAX_CHECK)
223 return ima_hooks_measure_str[NONE];
225 return ima_hooks_measure_str[func];
228 extern const char *const func_tokens[];
232 #ifdef CONFIG_IMA_QUEUE_EARLY_BOOT_KEYS
234 * To track keys that need to be measured.
236 struct ima_key_entry {
237 struct list_head list;
242 void ima_init_key_queue(void);
243 bool ima_should_queue_key(void);
244 bool ima_queue_key(struct key *keyring, const void *payload,
246 void ima_process_queued_keys(void);
248 static inline void ima_init_key_queue(void) {}
249 static inline bool ima_should_queue_key(void) { return false; }
250 static inline bool ima_queue_key(struct key *keyring,
252 size_t payload_len) { return false; }
253 static inline void ima_process_queued_keys(void) {}
254 #endif /* CONFIG_IMA_QUEUE_EARLY_BOOT_KEYS */
256 /* LIM API function definitions */
257 int ima_get_action(struct user_namespace *mnt_userns, struct inode *inode,
258 const struct cred *cred, u32 secid, int mask,
259 enum ima_hooks func, int *pcr,
260 struct ima_template_desc **template_desc,
261 const char *func_data);
262 int ima_must_measure(struct inode *inode, int mask, enum ima_hooks func);
263 int ima_collect_measurement(struct integrity_iint_cache *iint,
264 struct file *file, void *buf, loff_t size,
265 enum hash_algo algo, struct modsig *modsig);
266 void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,
267 const unsigned char *filename,
268 struct evm_ima_xattr_data *xattr_value,
269 int xattr_len, const struct modsig *modsig, int pcr,
270 struct ima_template_desc *template_desc);
271 void process_buffer_measurement(struct user_namespace *mnt_userns,
272 struct inode *inode, const void *buf, int size,
273 const char *eventname, enum ima_hooks func,
274 int pcr, const char *func_data,
276 void ima_audit_measurement(struct integrity_iint_cache *iint,
277 const unsigned char *filename);
278 int ima_alloc_init_template(struct ima_event_data *event_data,
279 struct ima_template_entry **entry,
280 struct ima_template_desc *template_desc);
281 int ima_store_template(struct ima_template_entry *entry, int violation,
283 const unsigned char *filename, int pcr);
284 void ima_free_template_entry(struct ima_template_entry *entry);
285 const char *ima_d_path(const struct path *path, char **pathbuf, char *filename);
287 /* IMA policy related functions */
288 int ima_match_policy(struct user_namespace *mnt_userns, struct inode *inode,
289 const struct cred *cred, u32 secid, enum ima_hooks func,
290 int mask, int flags, int *pcr,
291 struct ima_template_desc **template_desc,
292 const char *func_data);
293 void ima_init_policy(void);
294 void ima_update_policy(void);
295 void ima_update_policy_flag(void);
296 ssize_t ima_parse_add_rule(char *);
297 void ima_delete_rules(void);
298 int ima_check_policy(void);
299 void *ima_policy_start(struct seq_file *m, loff_t *pos);
300 void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos);
301 void ima_policy_stop(struct seq_file *m, void *v);
302 int ima_policy_show(struct seq_file *m, void *v);
304 /* Appraise integrity measurements */
305 #define IMA_APPRAISE_ENFORCE 0x01
306 #define IMA_APPRAISE_FIX 0x02
307 #define IMA_APPRAISE_LOG 0x04
308 #define IMA_APPRAISE_MODULES 0x08
309 #define IMA_APPRAISE_FIRMWARE 0x10
310 #define IMA_APPRAISE_POLICY 0x20
311 #define IMA_APPRAISE_KEXEC 0x40
313 #ifdef CONFIG_IMA_APPRAISE
314 int ima_check_blacklist(struct integrity_iint_cache *iint,
315 const struct modsig *modsig, int pcr);
316 int ima_appraise_measurement(enum ima_hooks func,
317 struct integrity_iint_cache *iint,
318 struct file *file, const unsigned char *filename,
319 struct evm_ima_xattr_data *xattr_value,
320 int xattr_len, const struct modsig *modsig);
321 int ima_must_appraise(struct user_namespace *mnt_userns, struct inode *inode,
322 int mask, enum ima_hooks func);
323 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file);
324 enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
325 enum ima_hooks func);
326 enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
328 int ima_read_xattr(struct dentry *dentry,
329 struct evm_ima_xattr_data **xattr_value);
332 static inline int ima_check_blacklist(struct integrity_iint_cache *iint,
333 const struct modsig *modsig, int pcr)
338 static inline int ima_appraise_measurement(enum ima_hooks func,
339 struct integrity_iint_cache *iint,
341 const unsigned char *filename,
342 struct evm_ima_xattr_data *xattr_value,
344 const struct modsig *modsig)
346 return INTEGRITY_UNKNOWN;
349 static inline int ima_must_appraise(struct user_namespace *mnt_userns,
350 struct inode *inode, int mask,
356 static inline void ima_update_xattr(struct integrity_iint_cache *iint,
361 static inline enum integrity_status ima_get_cache_status(struct integrity_iint_cache
365 return INTEGRITY_UNKNOWN;
368 static inline enum hash_algo
369 ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len)
371 return ima_hash_algo;
374 static inline int ima_read_xattr(struct dentry *dentry,
375 struct evm_ima_xattr_data **xattr_value)
380 #endif /* CONFIG_IMA_APPRAISE */
382 #ifdef CONFIG_IMA_APPRAISE_MODSIG
383 int ima_read_modsig(enum ima_hooks func, const void *buf, loff_t buf_len,
384 struct modsig **modsig);
385 void ima_collect_modsig(struct modsig *modsig, const void *buf, loff_t size);
386 int ima_get_modsig_digest(const struct modsig *modsig, enum hash_algo *algo,
387 const u8 **digest, u32 *digest_size);
388 int ima_get_raw_modsig(const struct modsig *modsig, const void **data,
390 void ima_free_modsig(struct modsig *modsig);
392 static inline int ima_read_modsig(enum ima_hooks func, const void *buf,
393 loff_t buf_len, struct modsig **modsig)
398 static inline void ima_collect_modsig(struct modsig *modsig, const void *buf,
403 static inline int ima_get_modsig_digest(const struct modsig *modsig,
404 enum hash_algo *algo, const u8 **digest,
410 static inline int ima_get_raw_modsig(const struct modsig *modsig,
411 const void **data, u32 *data_len)
416 static inline void ima_free_modsig(struct modsig *modsig)
419 #endif /* CONFIG_IMA_APPRAISE_MODSIG */
421 /* LSM based policy rules require audit */
422 #ifdef CONFIG_IMA_LSM_RULES
424 #define ima_filter_rule_init security_audit_rule_init
425 #define ima_filter_rule_free security_audit_rule_free
426 #define ima_filter_rule_match security_audit_rule_match
430 static inline int ima_filter_rule_init(u32 field, u32 op, char *rulestr,
436 static inline void ima_filter_rule_free(void *lsmrule)
440 static inline int ima_filter_rule_match(u32 secid, u32 field, u32 op,
445 #endif /* CONFIG_IMA_LSM_RULES */
447 #ifdef CONFIG_IMA_READ_POLICY
448 #define POLICY_FILE_FLAGS (S_IWUSR | S_IRUSR)
450 #define POLICY_FILE_FLAGS S_IWUSR
451 #endif /* CONFIG_IMA_READ_POLICY */
453 #endif /* __LINUX_IMA_H */