xref: /linux/security/integrity/ima/ima.h (revision ca853314e78b0a65c20b6a889a23c31f918d4aa2)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2005,2006,2007,2008 IBM Corporation
4  *
5  * Authors:
6  * Reiner Sailer <sailer@watson.ibm.com>
7  * Mimi Zohar <zohar@us.ibm.com>
8  *
9  * File: ima.h
10  *	internal Integrity Measurement Architecture (IMA) definitions
11  */
12 
13 #ifndef __LINUX_IMA_H
14 #define __LINUX_IMA_H
15 
16 #include <linux/types.h>
17 #include <linux/crypto.h>
18 #include <linux/fs.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>
24 
25 #include "../integrity.h"
26 
27 #ifdef CONFIG_HAVE_IMA_KEXEC
28 #include <asm/ima.h>
29 #endif
30 
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 };
34 
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
38 
39 #define IMA_HASH_BITS 10
40 #define IMA_MEASURE_HTABLE_SIZE (1 << IMA_HASH_BITS)
41 
42 #define IMA_TEMPLATE_FIELD_ID_MAX_LEN	16
43 #define IMA_TEMPLATE_NUM_FIELDS_MAX	15
44 
45 #define IMA_TEMPLATE_IMA_NAME "ima"
46 #define IMA_TEMPLATE_IMA_FMT "d|n"
47 
48 #define NR_BANKS(chip) ((chip != NULL) ? chip->nr_allocated_banks : 0)
49 
50 /* current content of the policy */
51 extern int ima_policy_flag;
52 
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[];
61 
62 /* IMA event related data */
63 struct ima_event_data {
64 	struct integrity_iint_cache *iint;
65 	struct file *file;
66 	const unsigned char *filename;
67 	struct evm_ima_xattr_data *xattr_value;
68 	int xattr_len;
69 	const struct modsig *modsig;
70 	const char *violation;
71 	const void *buf;
72 	int buf_len;
73 };
74 
75 /* IMA template field data definition */
76 struct ima_field_data {
77 	u8 *data;
78 	u32 len;
79 };
80 
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);
88 };
89 
90 /* IMA template descriptor definition */
91 struct ima_template_desc {
92 	struct list_head list;
93 	char *name;
94 	char *fmt;
95 	int num_fields;
96 	const struct ima_template_field **fields;
97 };
98 
99 struct ima_template_entry {
100 	int pcr;
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 */
105 };
106 
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;
111 };
112 extern struct list_head ima_measurements;	/* list of all measurements */
113 
114 /* Some details preceding the binary serialized measurement list */
115 struct ima_kexec_hdr {
116 	u16 version;
117 	u16 _reserved0;
118 	u32 _reserved1;
119 	u64 buffer_size;
120 	u64 count;
121 };
122 
123 extern const int read_idmap[];
124 
125 #ifdef CONFIG_HAVE_IMA_KEXEC
126 void ima_load_kexec_buffer(void);
127 #else
128 static inline void ima_load_kexec_buffer(void) {}
129 #endif /* CONFIG_HAVE_IMA_KEXEC */
130 
131 /*
132  * The default binary_runtime_measurements list format is defined as the
133  * platform native format.  The canonical format is defined as little-endian.
134  */
135 extern bool ima_canonical_fmt;
136 
137 /* Internal IMA function definitions */
138 int ima_init(void);
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,
157 			      int *num_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,
170 			  void *lsm_data);
171 
172 /*
173  * used to protect h_table and sha_table
174  */
175 extern spinlock_t ima_queue_lock;
176 
177 struct ima_h_table {
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];
181 };
182 extern struct ima_h_table ima_htable;
183 
184 static inline unsigned int ima_hash_key(u8 *digest)
185 {
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;
188 }
189 
190 #define __ima_hooks(hook)				\
191 	hook(NONE, none)				\
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(MAX_CHECK, none)
205 
206 #define __ima_hook_enumify(ENUM, str)	ENUM,
207 #define __ima_stringify(arg) (#arg)
208 #define __ima_hook_measuring_stringify(ENUM, str) \
209 		(__ima_stringify(measuring_ ##str)),
210 
211 enum ima_hooks {
212 	__ima_hooks(__ima_hook_enumify)
213 };
214 
215 static const char * const ima_hooks_measure_str[] = {
216 	__ima_hooks(__ima_hook_measuring_stringify)
217 };
218 
219 static inline const char *func_measure_str(enum ima_hooks func)
220 {
221 	if (func >= MAX_CHECK)
222 		return ima_hooks_measure_str[NONE];
223 
224 	return ima_hooks_measure_str[func];
225 }
226 
227 extern const char *const func_tokens[];
228 
229 struct modsig;
230 
231 #ifdef CONFIG_IMA_QUEUE_EARLY_BOOT_KEYS
232 /*
233  * To track keys that need to be measured.
234  */
235 struct ima_key_entry {
236 	struct list_head list;
237 	void *payload;
238 	size_t payload_len;
239 	char *keyring_name;
240 };
241 void ima_init_key_queue(void);
242 bool ima_should_queue_key(void);
243 bool ima_queue_key(struct key *keyring, const void *payload,
244 		   size_t payload_len);
245 void ima_process_queued_keys(void);
246 #else
247 static inline void ima_init_key_queue(void) {}
248 static inline bool ima_should_queue_key(void) { return false; }
249 static inline bool ima_queue_key(struct key *keyring,
250 				 const void *payload,
251 				 size_t payload_len) { return false; }
252 static inline void ima_process_queued_keys(void) {}
253 #endif /* CONFIG_IMA_QUEUE_EARLY_BOOT_KEYS */
254 
255 /* LIM API function definitions */
256 int ima_get_action(struct inode *inode, const struct cred *cred, u32 secid,
257 		   int mask, enum ima_hooks func, int *pcr,
258 		   struct ima_template_desc **template_desc,
259 		   const char *keyring);
260 int ima_must_measure(struct inode *inode, int mask, enum ima_hooks func);
261 int ima_collect_measurement(struct integrity_iint_cache *iint,
262 			    struct file *file, void *buf, loff_t size,
263 			    enum hash_algo algo, struct modsig *modsig);
264 void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,
265 			   const unsigned char *filename,
266 			   struct evm_ima_xattr_data *xattr_value,
267 			   int xattr_len, const struct modsig *modsig, int pcr,
268 			   struct ima_template_desc *template_desc);
269 void process_buffer_measurement(struct inode *inode, const void *buf, int size,
270 				const char *eventname, enum ima_hooks func,
271 				int pcr, const char *keyring);
272 void ima_audit_measurement(struct integrity_iint_cache *iint,
273 			   const unsigned char *filename);
274 int ima_alloc_init_template(struct ima_event_data *event_data,
275 			    struct ima_template_entry **entry,
276 			    struct ima_template_desc *template_desc);
277 int ima_store_template(struct ima_template_entry *entry, int violation,
278 		       struct inode *inode,
279 		       const unsigned char *filename, int pcr);
280 void ima_free_template_entry(struct ima_template_entry *entry);
281 const char *ima_d_path(const struct path *path, char **pathbuf, char *filename);
282 
283 /* IMA policy related functions */
284 int ima_match_policy(struct inode *inode, const struct cred *cred, u32 secid,
285 		     enum ima_hooks func, int mask, int flags, int *pcr,
286 		     struct ima_template_desc **template_desc,
287 		     const char *keyring);
288 void ima_init_policy(void);
289 void ima_update_policy(void);
290 void ima_update_policy_flag(void);
291 ssize_t ima_parse_add_rule(char *);
292 void ima_delete_rules(void);
293 int ima_check_policy(void);
294 void *ima_policy_start(struct seq_file *m, loff_t *pos);
295 void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos);
296 void ima_policy_stop(struct seq_file *m, void *v);
297 int ima_policy_show(struct seq_file *m, void *v);
298 
299 /* Appraise integrity measurements */
300 #define IMA_APPRAISE_ENFORCE	0x01
301 #define IMA_APPRAISE_FIX	0x02
302 #define IMA_APPRAISE_LOG	0x04
303 #define IMA_APPRAISE_MODULES	0x08
304 #define IMA_APPRAISE_FIRMWARE	0x10
305 #define IMA_APPRAISE_POLICY	0x20
306 #define IMA_APPRAISE_KEXEC	0x40
307 
308 #ifdef CONFIG_IMA_APPRAISE
309 int ima_check_blacklist(struct integrity_iint_cache *iint,
310 			const struct modsig *modsig, int pcr);
311 int ima_appraise_measurement(enum ima_hooks func,
312 			     struct integrity_iint_cache *iint,
313 			     struct file *file, const unsigned char *filename,
314 			     struct evm_ima_xattr_data *xattr_value,
315 			     int xattr_len, const struct modsig *modsig);
316 int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func);
317 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file);
318 enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
319 					   enum ima_hooks func);
320 enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
321 				 int xattr_len);
322 int ima_read_xattr(struct dentry *dentry,
323 		   struct evm_ima_xattr_data **xattr_value);
324 
325 #else
326 static inline int ima_check_blacklist(struct integrity_iint_cache *iint,
327 				      const struct modsig *modsig, int pcr)
328 {
329 	return 0;
330 }
331 
332 static inline int ima_appraise_measurement(enum ima_hooks func,
333 					   struct integrity_iint_cache *iint,
334 					   struct file *file,
335 					   const unsigned char *filename,
336 					   struct evm_ima_xattr_data *xattr_value,
337 					   int xattr_len,
338 					   const struct modsig *modsig)
339 {
340 	return INTEGRITY_UNKNOWN;
341 }
342 
343 static inline int ima_must_appraise(struct inode *inode, int mask,
344 				    enum ima_hooks func)
345 {
346 	return 0;
347 }
348 
349 static inline void ima_update_xattr(struct integrity_iint_cache *iint,
350 				    struct file *file)
351 {
352 }
353 
354 static inline enum integrity_status ima_get_cache_status(struct integrity_iint_cache
355 							 *iint,
356 							 enum ima_hooks func)
357 {
358 	return INTEGRITY_UNKNOWN;
359 }
360 
361 static inline enum hash_algo
362 ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len)
363 {
364 	return ima_hash_algo;
365 }
366 
367 static inline int ima_read_xattr(struct dentry *dentry,
368 				 struct evm_ima_xattr_data **xattr_value)
369 {
370 	return 0;
371 }
372 
373 #endif /* CONFIG_IMA_APPRAISE */
374 
375 #ifdef CONFIG_IMA_APPRAISE_MODSIG
376 int ima_read_modsig(enum ima_hooks func, const void *buf, loff_t buf_len,
377 		    struct modsig **modsig);
378 void ima_collect_modsig(struct modsig *modsig, const void *buf, loff_t size);
379 int ima_get_modsig_digest(const struct modsig *modsig, enum hash_algo *algo,
380 			  const u8 **digest, u32 *digest_size);
381 int ima_get_raw_modsig(const struct modsig *modsig, const void **data,
382 		       u32 *data_len);
383 void ima_free_modsig(struct modsig *modsig);
384 #else
385 static inline int ima_read_modsig(enum ima_hooks func, const void *buf,
386 				  loff_t buf_len, struct modsig **modsig)
387 {
388 	return -EOPNOTSUPP;
389 }
390 
391 static inline void ima_collect_modsig(struct modsig *modsig, const void *buf,
392 				      loff_t size)
393 {
394 }
395 
396 static inline int ima_get_modsig_digest(const struct modsig *modsig,
397 					enum hash_algo *algo, const u8 **digest,
398 					u32 *digest_size)
399 {
400 	return -EOPNOTSUPP;
401 }
402 
403 static inline int ima_get_raw_modsig(const struct modsig *modsig,
404 				     const void **data, u32 *data_len)
405 {
406 	return -EOPNOTSUPP;
407 }
408 
409 static inline void ima_free_modsig(struct modsig *modsig)
410 {
411 }
412 #endif /* CONFIG_IMA_APPRAISE_MODSIG */
413 
414 /* LSM based policy rules require audit */
415 #ifdef CONFIG_IMA_LSM_RULES
416 
417 #define ima_filter_rule_init security_audit_rule_init
418 #define ima_filter_rule_free security_audit_rule_free
419 #define ima_filter_rule_match security_audit_rule_match
420 
421 #else
422 
423 static inline int ima_filter_rule_init(u32 field, u32 op, char *rulestr,
424 				       void **lsmrule)
425 {
426 	return -EINVAL;
427 }
428 
429 static inline void ima_filter_rule_free(void *lsmrule)
430 {
431 }
432 
433 static inline int ima_filter_rule_match(u32 secid, u32 field, u32 op,
434 					void *lsmrule)
435 {
436 	return -EINVAL;
437 }
438 #endif /* CONFIG_IMA_LSM_RULES */
439 
440 #ifdef	CONFIG_IMA_READ_POLICY
441 #define	POLICY_FILE_FLAGS	(S_IWUSR | S_IRUSR)
442 #else
443 #define	POLICY_FILE_FLAGS	S_IWUSR
444 #endif /* CONFIG_IMA_READ_POLICY */
445 
446 #endif /* __LINUX_IMA_H */
447