xref: /linux/security/integrity/ima/ima.h (revision 71dfa617ea9f18e4585fe78364217cd32b1fc382)
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 enum ima_show_type { IMA_SHOW_BINARY, IMA_SHOW_BINARY_NO_FIELD_LEN,
28 		     IMA_SHOW_BINARY_OLD_STRING_FMT, IMA_SHOW_ASCII };
29 enum tpm_pcrs { TPM_PCR0 = 0, TPM_PCR8 = 8, TPM_PCR10 = 10 };
30 
31 /* digest size for IMA, fits SHA1 or MD5 */
32 #define IMA_DIGEST_SIZE		SHA1_DIGEST_SIZE
33 #define IMA_EVENT_NAME_LEN_MAX	255
34 
35 #define IMA_HASH_BITS 10
36 #define IMA_MEASURE_HTABLE_SIZE (1 << IMA_HASH_BITS)
37 
38 #define IMA_TEMPLATE_FIELD_ID_MAX_LEN	16
39 #define IMA_TEMPLATE_NUM_FIELDS_MAX	15
40 
41 #define IMA_TEMPLATE_IMA_NAME "ima"
42 #define IMA_TEMPLATE_IMA_FMT "d|n"
43 
44 #define NR_BANKS(chip) ((chip != NULL) ? chip->nr_allocated_banks : 0)
45 
46 /* current content of the policy */
47 extern int ima_policy_flag;
48 
49 /* bitset of digests algorithms allowed in the setxattr hook */
50 extern atomic_t ima_setxattr_allowed_hash_algorithms;
51 
52 /* set during initialization */
53 extern int ima_hash_algo __ro_after_init;
54 extern int ima_sha1_idx __ro_after_init;
55 extern int ima_hash_algo_idx __ro_after_init;
56 extern int ima_extra_slots __ro_after_init;
57 extern int ima_appraise;
58 extern struct tpm_chip *ima_tpm_chip;
59 extern const char boot_aggregate_name[];
60 
61 /* IMA event related data */
62 struct ima_event_data {
63 	struct ima_iint_cache *iint;
64 	struct file *file;
65 	const unsigned char *filename;
66 	struct evm_ima_xattr_data *xattr_value;
67 	int xattr_len;
68 	const struct modsig *modsig;
69 	const char *violation;
70 	const void *buf;
71 	int buf_len;
72 };
73 
74 /* IMA template field data definition */
75 struct ima_field_data {
76 	u8 *data;
77 	u32 len;
78 };
79 
80 /* IMA template field definition */
81 struct ima_template_field {
82 	const char field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN];
83 	int (*field_init)(struct ima_event_data *event_data,
84 			  struct ima_field_data *field_data);
85 	void (*field_show)(struct seq_file *m, enum ima_show_type show,
86 			   struct ima_field_data *field_data);
87 };
88 
89 /* IMA template descriptor definition */
90 struct ima_template_desc {
91 	struct list_head list;
92 	char *name;
93 	char *fmt;
94 	int num_fields;
95 	const struct ima_template_field **fields;
96 };
97 
98 struct ima_template_entry {
99 	int pcr;
100 	struct tpm_digest *digests;
101 	struct ima_template_desc *template_desc; /* template descriptor */
102 	u32 template_data_len;
103 	struct ima_field_data template_data[];	/* template related data */
104 };
105 
106 struct ima_queue_entry {
107 	struct hlist_node hnext;	/* place in hash collision list */
108 	struct list_head later;		/* place in ima_measurements list */
109 	struct ima_template_entry *entry;
110 };
111 extern struct list_head ima_measurements;	/* list of all measurements */
112 
113 /* Some details preceding the binary serialized measurement list */
114 struct ima_kexec_hdr {
115 	u16 version;
116 	u16 _reserved0;
117 	u32 _reserved1;
118 	u64 buffer_size;
119 	u64 count;
120 };
121 
122 /* IMA iint action cache flags */
123 #define IMA_MEASURE		0x00000001
124 #define IMA_MEASURED		0x00000002
125 #define IMA_APPRAISE		0x00000004
126 #define IMA_APPRAISED		0x00000008
127 /*#define IMA_COLLECT		0x00000010  do not use this flag */
128 #define IMA_COLLECTED		0x00000020
129 #define IMA_AUDIT		0x00000040
130 #define IMA_AUDITED		0x00000080
131 #define IMA_HASH		0x00000100
132 #define IMA_HASHED		0x00000200
133 
134 /* IMA iint policy rule cache flags */
135 #define IMA_NONACTION_FLAGS	0xff000000
136 #define IMA_DIGSIG_REQUIRED	0x01000000
137 #define IMA_PERMIT_DIRECTIO	0x02000000
138 #define IMA_NEW_FILE		0x04000000
139 #define IMA_FAIL_UNVERIFIABLE_SIGS	0x10000000
140 #define IMA_MODSIG_ALLOWED	0x20000000
141 #define IMA_CHECK_BLACKLIST	0x40000000
142 #define IMA_VERITY_REQUIRED	0x80000000
143 
144 #define IMA_DO_MASK		(IMA_MEASURE | IMA_APPRAISE | IMA_AUDIT | \
145 				 IMA_HASH | IMA_APPRAISE_SUBMASK)
146 #define IMA_DONE_MASK		(IMA_MEASURED | IMA_APPRAISED | IMA_AUDITED | \
147 				 IMA_HASHED | IMA_COLLECTED | \
148 				 IMA_APPRAISED_SUBMASK)
149 
150 /* IMA iint subaction appraise cache flags */
151 #define IMA_FILE_APPRAISE	0x00001000
152 #define IMA_FILE_APPRAISED	0x00002000
153 #define IMA_MMAP_APPRAISE	0x00004000
154 #define IMA_MMAP_APPRAISED	0x00008000
155 #define IMA_BPRM_APPRAISE	0x00010000
156 #define IMA_BPRM_APPRAISED	0x00020000
157 #define IMA_READ_APPRAISE	0x00040000
158 #define IMA_READ_APPRAISED	0x00080000
159 #define IMA_CREDS_APPRAISE	0x00100000
160 #define IMA_CREDS_APPRAISED	0x00200000
161 #define IMA_APPRAISE_SUBMASK	(IMA_FILE_APPRAISE | IMA_MMAP_APPRAISE | \
162 				 IMA_BPRM_APPRAISE | IMA_READ_APPRAISE | \
163 				 IMA_CREDS_APPRAISE)
164 #define IMA_APPRAISED_SUBMASK	(IMA_FILE_APPRAISED | IMA_MMAP_APPRAISED | \
165 				 IMA_BPRM_APPRAISED | IMA_READ_APPRAISED | \
166 				 IMA_CREDS_APPRAISED)
167 
168 /* IMA iint cache atomic_flags */
169 #define IMA_CHANGE_XATTR	0
170 #define IMA_UPDATE_XATTR	1
171 #define IMA_CHANGE_ATTR		2
172 #define IMA_DIGSIG		3
173 #define IMA_MUST_MEASURE	4
174 
175 /* IMA integrity metadata associated with an inode */
176 struct ima_iint_cache {
177 	struct mutex mutex;	/* protects: version, flags, digest */
178 	u64 version;		/* track inode changes */
179 	unsigned long flags;
180 	unsigned long measured_pcrs;
181 	unsigned long atomic_flags;
182 	unsigned long real_ino;
183 	dev_t real_dev;
184 	enum integrity_status ima_file_status:4;
185 	enum integrity_status ima_mmap_status:4;
186 	enum integrity_status ima_bprm_status:4;
187 	enum integrity_status ima_read_status:4;
188 	enum integrity_status ima_creds_status:4;
189 	struct ima_digest_data *ima_hash;
190 };
191 
192 extern struct lsm_blob_sizes ima_blob_sizes;
193 
194 static inline struct ima_iint_cache *
195 ima_inode_get_iint(const struct inode *inode)
196 {
197 	struct ima_iint_cache **iint_sec;
198 
199 	if (unlikely(!inode->i_security))
200 		return NULL;
201 
202 	iint_sec = inode->i_security + ima_blob_sizes.lbs_inode;
203 	return *iint_sec;
204 }
205 
206 static inline void ima_inode_set_iint(const struct inode *inode,
207 				      struct ima_iint_cache *iint)
208 {
209 	struct ima_iint_cache **iint_sec;
210 
211 	if (unlikely(!inode->i_security))
212 		return;
213 
214 	iint_sec = inode->i_security + ima_blob_sizes.lbs_inode;
215 	*iint_sec = iint;
216 }
217 
218 struct ima_iint_cache *ima_iint_find(struct inode *inode);
219 struct ima_iint_cache *ima_inode_get(struct inode *inode);
220 void ima_inode_free(struct inode *inode);
221 void __init ima_iintcache_init(void);
222 
223 extern const int read_idmap[];
224 
225 #ifdef CONFIG_HAVE_IMA_KEXEC
226 void ima_load_kexec_buffer(void);
227 #else
228 static inline void ima_load_kexec_buffer(void) {}
229 #endif /* CONFIG_HAVE_IMA_KEXEC */
230 
231 #ifdef CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS
232 void ima_post_key_create_or_update(struct key *keyring, struct key *key,
233 				   const void *payload, size_t plen,
234 				   unsigned long flags, bool create);
235 #endif
236 
237 /*
238  * The default binary_runtime_measurements list format is defined as the
239  * platform native format.  The canonical format is defined as little-endian.
240  */
241 extern bool ima_canonical_fmt;
242 
243 /* Internal IMA function definitions */
244 int ima_init(void);
245 int ima_fs_init(void);
246 int ima_add_template_entry(struct ima_template_entry *entry, int violation,
247 			   const char *op, struct inode *inode,
248 			   const unsigned char *filename);
249 int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash);
250 int ima_calc_buffer_hash(const void *buf, loff_t len,
251 			 struct ima_digest_data *hash);
252 int ima_calc_field_array_hash(struct ima_field_data *field_data,
253 			      struct ima_template_entry *entry);
254 int ima_calc_boot_aggregate(struct ima_digest_data *hash);
255 void ima_add_violation(struct file *file, const unsigned char *filename,
256 		       struct ima_iint_cache *iint, const char *op,
257 		       const char *cause);
258 int ima_init_crypto(void);
259 void ima_putc(struct seq_file *m, void *data, int datalen);
260 void ima_print_digest(struct seq_file *m, u8 *digest, u32 size);
261 int template_desc_init_fields(const char *template_fmt,
262 			      const struct ima_template_field ***fields,
263 			      int *num_fields);
264 struct ima_template_desc *ima_template_desc_current(void);
265 struct ima_template_desc *ima_template_desc_buf(void);
266 struct ima_template_desc *lookup_template_desc(const char *name);
267 bool ima_template_has_modsig(const struct ima_template_desc *ima_template);
268 int ima_restore_measurement_entry(struct ima_template_entry *entry);
269 int ima_restore_measurement_list(loff_t bufsize, void *buf);
270 int ima_measurements_show(struct seq_file *m, void *v);
271 unsigned long ima_get_binary_runtime_size(void);
272 int ima_init_template(void);
273 void ima_init_template_list(void);
274 int __init ima_init_digests(void);
275 int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
276 			  void *lsm_data);
277 
278 /*
279  * used to protect h_table and sha_table
280  */
281 extern spinlock_t ima_queue_lock;
282 
283 struct ima_h_table {
284 	atomic_long_t len;	/* number of stored measurements in the list */
285 	atomic_long_t violations;
286 	struct hlist_head queue[IMA_MEASURE_HTABLE_SIZE];
287 };
288 extern struct ima_h_table ima_htable;
289 
290 static inline unsigned int ima_hash_key(u8 *digest)
291 {
292 	/* there is no point in taking a hash of part of a digest */
293 	return (digest[0] | digest[1] << 8) % IMA_MEASURE_HTABLE_SIZE;
294 }
295 
296 #define __ima_hooks(hook)				\
297 	hook(NONE, none)				\
298 	hook(FILE_CHECK, file)				\
299 	hook(MMAP_CHECK, mmap)				\
300 	hook(MMAP_CHECK_REQPROT, mmap_reqprot)		\
301 	hook(BPRM_CHECK, bprm)				\
302 	hook(CREDS_CHECK, creds)			\
303 	hook(POST_SETATTR, post_setattr)		\
304 	hook(MODULE_CHECK, module)			\
305 	hook(FIRMWARE_CHECK, firmware)			\
306 	hook(KEXEC_KERNEL_CHECK, kexec_kernel)		\
307 	hook(KEXEC_INITRAMFS_CHECK, kexec_initramfs)	\
308 	hook(POLICY_CHECK, policy)			\
309 	hook(KEXEC_CMDLINE, kexec_cmdline)		\
310 	hook(KEY_CHECK, key)				\
311 	hook(CRITICAL_DATA, critical_data)		\
312 	hook(SETXATTR_CHECK, setxattr_check)		\
313 	hook(MAX_CHECK, none)
314 
315 #define __ima_hook_enumify(ENUM, str)	ENUM,
316 #define __ima_stringify(arg) (#arg)
317 #define __ima_hook_measuring_stringify(ENUM, str) \
318 		(__ima_stringify(measuring_ ##str)),
319 
320 enum ima_hooks {
321 	__ima_hooks(__ima_hook_enumify)
322 };
323 
324 static const char * const ima_hooks_measure_str[] = {
325 	__ima_hooks(__ima_hook_measuring_stringify)
326 };
327 
328 static inline const char *func_measure_str(enum ima_hooks func)
329 {
330 	if (func >= MAX_CHECK)
331 		return ima_hooks_measure_str[NONE];
332 
333 	return ima_hooks_measure_str[func];
334 }
335 
336 extern const char *const func_tokens[];
337 
338 struct modsig;
339 
340 #ifdef CONFIG_IMA_QUEUE_EARLY_BOOT_KEYS
341 /*
342  * To track keys that need to be measured.
343  */
344 struct ima_key_entry {
345 	struct list_head list;
346 	void *payload;
347 	size_t payload_len;
348 	char *keyring_name;
349 };
350 void ima_init_key_queue(void);
351 bool ima_should_queue_key(void);
352 bool ima_queue_key(struct key *keyring, const void *payload,
353 		   size_t payload_len);
354 void ima_process_queued_keys(void);
355 #else
356 static inline void ima_init_key_queue(void) {}
357 static inline bool ima_should_queue_key(void) { return false; }
358 static inline bool ima_queue_key(struct key *keyring,
359 				 const void *payload,
360 				 size_t payload_len) { return false; }
361 static inline void ima_process_queued_keys(void) {}
362 #endif /* CONFIG_IMA_QUEUE_EARLY_BOOT_KEYS */
363 
364 /* LIM API function definitions */
365 int ima_get_action(struct mnt_idmap *idmap, struct inode *inode,
366 		   const struct cred *cred, u32 secid, int mask,
367 		   enum ima_hooks func, int *pcr,
368 		   struct ima_template_desc **template_desc,
369 		   const char *func_data, unsigned int *allowed_algos);
370 int ima_must_measure(struct inode *inode, int mask, enum ima_hooks func);
371 int ima_collect_measurement(struct ima_iint_cache *iint, struct file *file,
372 			    void *buf, loff_t size, enum hash_algo algo,
373 			    struct modsig *modsig);
374 void ima_store_measurement(struct ima_iint_cache *iint, struct file *file,
375 			   const unsigned char *filename,
376 			   struct evm_ima_xattr_data *xattr_value,
377 			   int xattr_len, const struct modsig *modsig, int pcr,
378 			   struct ima_template_desc *template_desc);
379 int process_buffer_measurement(struct mnt_idmap *idmap,
380 			       struct inode *inode, const void *buf, int size,
381 			       const char *eventname, enum ima_hooks func,
382 			       int pcr, const char *func_data,
383 			       bool buf_hash, u8 *digest, size_t digest_len);
384 void ima_audit_measurement(struct ima_iint_cache *iint,
385 			   const unsigned char *filename);
386 int ima_alloc_init_template(struct ima_event_data *event_data,
387 			    struct ima_template_entry **entry,
388 			    struct ima_template_desc *template_desc);
389 int ima_store_template(struct ima_template_entry *entry, int violation,
390 		       struct inode *inode,
391 		       const unsigned char *filename, int pcr);
392 void ima_free_template_entry(struct ima_template_entry *entry);
393 const char *ima_d_path(const struct path *path, char **pathbuf, char *filename);
394 
395 /* IMA policy related functions */
396 int ima_match_policy(struct mnt_idmap *idmap, struct inode *inode,
397 		     const struct cred *cred, u32 secid, enum ima_hooks func,
398 		     int mask, int flags, int *pcr,
399 		     struct ima_template_desc **template_desc,
400 		     const char *func_data, unsigned int *allowed_algos);
401 void ima_init_policy(void);
402 void ima_update_policy(void);
403 void ima_update_policy_flags(void);
404 ssize_t ima_parse_add_rule(char *);
405 void ima_delete_rules(void);
406 int ima_check_policy(void);
407 void *ima_policy_start(struct seq_file *m, loff_t *pos);
408 void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos);
409 void ima_policy_stop(struct seq_file *m, void *v);
410 int ima_policy_show(struct seq_file *m, void *v);
411 
412 /* Appraise integrity measurements */
413 #define IMA_APPRAISE_ENFORCE	0x01
414 #define IMA_APPRAISE_FIX	0x02
415 #define IMA_APPRAISE_LOG	0x04
416 #define IMA_APPRAISE_MODULES	0x08
417 #define IMA_APPRAISE_FIRMWARE	0x10
418 #define IMA_APPRAISE_POLICY	0x20
419 #define IMA_APPRAISE_KEXEC	0x40
420 
421 #ifdef CONFIG_IMA_APPRAISE
422 int ima_check_blacklist(struct ima_iint_cache *iint,
423 			const struct modsig *modsig, int pcr);
424 int ima_appraise_measurement(enum ima_hooks func, struct ima_iint_cache *iint,
425 			     struct file *file, const unsigned char *filename,
426 			     struct evm_ima_xattr_data *xattr_value,
427 			     int xattr_len, const struct modsig *modsig);
428 int ima_must_appraise(struct mnt_idmap *idmap, struct inode *inode,
429 		      int mask, enum ima_hooks func);
430 void ima_update_xattr(struct ima_iint_cache *iint, struct file *file);
431 enum integrity_status ima_get_cache_status(struct ima_iint_cache *iint,
432 					   enum ima_hooks func);
433 enum hash_algo ima_get_hash_algo(const struct evm_ima_xattr_data *xattr_value,
434 				 int xattr_len);
435 int ima_read_xattr(struct dentry *dentry,
436 		   struct evm_ima_xattr_data **xattr_value, int xattr_len);
437 void __init init_ima_appraise_lsm(const struct lsm_id *lsmid);
438 
439 #else
440 static inline int ima_check_blacklist(struct ima_iint_cache *iint,
441 				      const struct modsig *modsig, int pcr)
442 {
443 	return 0;
444 }
445 
446 static inline int ima_appraise_measurement(enum ima_hooks func,
447 					   struct ima_iint_cache *iint,
448 					   struct file *file,
449 					   const unsigned char *filename,
450 					   struct evm_ima_xattr_data *xattr_value,
451 					   int xattr_len,
452 					   const struct modsig *modsig)
453 {
454 	return INTEGRITY_UNKNOWN;
455 }
456 
457 static inline int ima_must_appraise(struct mnt_idmap *idmap,
458 				    struct inode *inode, int mask,
459 				    enum ima_hooks func)
460 {
461 	return 0;
462 }
463 
464 static inline void ima_update_xattr(struct ima_iint_cache *iint,
465 				    struct file *file)
466 {
467 }
468 
469 static inline enum integrity_status
470 ima_get_cache_status(struct ima_iint_cache *iint, enum ima_hooks func)
471 {
472 	return INTEGRITY_UNKNOWN;
473 }
474 
475 static inline enum hash_algo
476 ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len)
477 {
478 	return ima_hash_algo;
479 }
480 
481 static inline int ima_read_xattr(struct dentry *dentry,
482 				 struct evm_ima_xattr_data **xattr_value,
483 				 int xattr_len)
484 {
485 	return 0;
486 }
487 
488 static inline void __init init_ima_appraise_lsm(const struct lsm_id *lsmid)
489 {
490 }
491 
492 #endif /* CONFIG_IMA_APPRAISE */
493 
494 #ifdef CONFIG_IMA_APPRAISE_MODSIG
495 int ima_read_modsig(enum ima_hooks func, const void *buf, loff_t buf_len,
496 		    struct modsig **modsig);
497 void ima_collect_modsig(struct modsig *modsig, const void *buf, loff_t size);
498 int ima_get_modsig_digest(const struct modsig *modsig, enum hash_algo *algo,
499 			  const u8 **digest, u32 *digest_size);
500 int ima_get_raw_modsig(const struct modsig *modsig, const void **data,
501 		       u32 *data_len);
502 void ima_free_modsig(struct modsig *modsig);
503 #else
504 static inline int ima_read_modsig(enum ima_hooks func, const void *buf,
505 				  loff_t buf_len, struct modsig **modsig)
506 {
507 	return -EOPNOTSUPP;
508 }
509 
510 static inline void ima_collect_modsig(struct modsig *modsig, const void *buf,
511 				      loff_t size)
512 {
513 }
514 
515 static inline int ima_get_modsig_digest(const struct modsig *modsig,
516 					enum hash_algo *algo, const u8 **digest,
517 					u32 *digest_size)
518 {
519 	return -EOPNOTSUPP;
520 }
521 
522 static inline int ima_get_raw_modsig(const struct modsig *modsig,
523 				     const void **data, u32 *data_len)
524 {
525 	return -EOPNOTSUPP;
526 }
527 
528 static inline void ima_free_modsig(struct modsig *modsig)
529 {
530 }
531 #endif /* CONFIG_IMA_APPRAISE_MODSIG */
532 
533 /* LSM based policy rules require audit */
534 #ifdef CONFIG_IMA_LSM_RULES
535 
536 #define ima_filter_rule_init security_audit_rule_init
537 #define ima_filter_rule_free security_audit_rule_free
538 #define ima_filter_rule_match security_audit_rule_match
539 
540 #else
541 
542 static inline int ima_filter_rule_init(u32 field, u32 op, char *rulestr,
543 				       void **lsmrule)
544 {
545 	return -EINVAL;
546 }
547 
548 static inline void ima_filter_rule_free(void *lsmrule)
549 {
550 }
551 
552 static inline int ima_filter_rule_match(u32 secid, u32 field, u32 op,
553 					void *lsmrule)
554 {
555 	return -EINVAL;
556 }
557 #endif /* CONFIG_IMA_LSM_RULES */
558 
559 #ifdef	CONFIG_IMA_READ_POLICY
560 #define	POLICY_FILE_FLAGS	(S_IWUSR | S_IRUSR)
561 #else
562 #define	POLICY_FILE_FLAGS	S_IWUSR
563 #endif /* CONFIG_IMA_READ_POLICY */
564 
565 #endif /* __LINUX_IMA_H */
566