xref: /linux/security/integrity/ima/ima.h (revision cd3cec0a02c7338ce2901c574f3935b8f6984aab)
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 integrity_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 extern const int read_idmap[];
123 
124 #ifdef CONFIG_HAVE_IMA_KEXEC
125 void ima_load_kexec_buffer(void);
126 #else
127 static inline void ima_load_kexec_buffer(void) {}
128 #endif /* CONFIG_HAVE_IMA_KEXEC */
129 
130 #ifdef CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS
131 void ima_post_key_create_or_update(struct key *keyring, struct key *key,
132 				   const void *payload, size_t plen,
133 				   unsigned long flags, bool create);
134 #endif
135 
136 /*
137  * The default binary_runtime_measurements list format is defined as the
138  * platform native format.  The canonical format is defined as little-endian.
139  */
140 extern bool ima_canonical_fmt;
141 
142 /* Internal IMA function definitions */
143 int ima_init(void);
144 int ima_fs_init(void);
145 int ima_add_template_entry(struct ima_template_entry *entry, int violation,
146 			   const char *op, struct inode *inode,
147 			   const unsigned char *filename);
148 int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash);
149 int ima_calc_buffer_hash(const void *buf, loff_t len,
150 			 struct ima_digest_data *hash);
151 int ima_calc_field_array_hash(struct ima_field_data *field_data,
152 			      struct ima_template_entry *entry);
153 int ima_calc_boot_aggregate(struct ima_digest_data *hash);
154 void ima_add_violation(struct file *file, const unsigned char *filename,
155 		       struct integrity_iint_cache *iint,
156 		       const char *op, const char *cause);
157 int ima_init_crypto(void);
158 void ima_putc(struct seq_file *m, void *data, int datalen);
159 void ima_print_digest(struct seq_file *m, u8 *digest, u32 size);
160 int template_desc_init_fields(const char *template_fmt,
161 			      const struct ima_template_field ***fields,
162 			      int *num_fields);
163 struct ima_template_desc *ima_template_desc_current(void);
164 struct ima_template_desc *ima_template_desc_buf(void);
165 struct ima_template_desc *lookup_template_desc(const char *name);
166 bool ima_template_has_modsig(const struct ima_template_desc *ima_template);
167 int ima_restore_measurement_entry(struct ima_template_entry *entry);
168 int ima_restore_measurement_list(loff_t bufsize, void *buf);
169 int ima_measurements_show(struct seq_file *m, void *v);
170 unsigned long ima_get_binary_runtime_size(void);
171 int ima_init_template(void);
172 void ima_init_template_list(void);
173 int __init ima_init_digests(void);
174 int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event,
175 			  void *lsm_data);
176 
177 /*
178  * used to protect h_table and sha_table
179  */
180 extern spinlock_t ima_queue_lock;
181 
182 struct ima_h_table {
183 	atomic_long_t len;	/* number of stored measurements in the list */
184 	atomic_long_t violations;
185 	struct hlist_head queue[IMA_MEASURE_HTABLE_SIZE];
186 };
187 extern struct ima_h_table ima_htable;
188 
189 static inline unsigned int ima_hash_key(u8 *digest)
190 {
191 	/* there is no point in taking a hash of part of a digest */
192 	return (digest[0] | digest[1] << 8) % IMA_MEASURE_HTABLE_SIZE;
193 }
194 
195 #define __ima_hooks(hook)				\
196 	hook(NONE, none)				\
197 	hook(FILE_CHECK, file)				\
198 	hook(MMAP_CHECK, mmap)				\
199 	hook(MMAP_CHECK_REQPROT, mmap_reqprot)		\
200 	hook(BPRM_CHECK, bprm)				\
201 	hook(CREDS_CHECK, creds)			\
202 	hook(POST_SETATTR, post_setattr)		\
203 	hook(MODULE_CHECK, module)			\
204 	hook(FIRMWARE_CHECK, firmware)			\
205 	hook(KEXEC_KERNEL_CHECK, kexec_kernel)		\
206 	hook(KEXEC_INITRAMFS_CHECK, kexec_initramfs)	\
207 	hook(POLICY_CHECK, policy)			\
208 	hook(KEXEC_CMDLINE, kexec_cmdline)		\
209 	hook(KEY_CHECK, key)				\
210 	hook(CRITICAL_DATA, critical_data)		\
211 	hook(SETXATTR_CHECK, setxattr_check)		\
212 	hook(MAX_CHECK, none)
213 
214 #define __ima_hook_enumify(ENUM, str)	ENUM,
215 #define __ima_stringify(arg) (#arg)
216 #define __ima_hook_measuring_stringify(ENUM, str) \
217 		(__ima_stringify(measuring_ ##str)),
218 
219 enum ima_hooks {
220 	__ima_hooks(__ima_hook_enumify)
221 };
222 
223 static const char * const ima_hooks_measure_str[] = {
224 	__ima_hooks(__ima_hook_measuring_stringify)
225 };
226 
227 static inline const char *func_measure_str(enum ima_hooks func)
228 {
229 	if (func >= MAX_CHECK)
230 		return ima_hooks_measure_str[NONE];
231 
232 	return ima_hooks_measure_str[func];
233 }
234 
235 extern const char *const func_tokens[];
236 
237 struct modsig;
238 
239 #ifdef CONFIG_IMA_QUEUE_EARLY_BOOT_KEYS
240 /*
241  * To track keys that need to be measured.
242  */
243 struct ima_key_entry {
244 	struct list_head list;
245 	void *payload;
246 	size_t payload_len;
247 	char *keyring_name;
248 };
249 void ima_init_key_queue(void);
250 bool ima_should_queue_key(void);
251 bool ima_queue_key(struct key *keyring, const void *payload,
252 		   size_t payload_len);
253 void ima_process_queued_keys(void);
254 #else
255 static inline void ima_init_key_queue(void) {}
256 static inline bool ima_should_queue_key(void) { return false; }
257 static inline bool ima_queue_key(struct key *keyring,
258 				 const void *payload,
259 				 size_t payload_len) { return false; }
260 static inline void ima_process_queued_keys(void) {}
261 #endif /* CONFIG_IMA_QUEUE_EARLY_BOOT_KEYS */
262 
263 /* LIM API function definitions */
264 int ima_get_action(struct mnt_idmap *idmap, struct inode *inode,
265 		   const struct cred *cred, u32 secid, int mask,
266 		   enum ima_hooks func, int *pcr,
267 		   struct ima_template_desc **template_desc,
268 		   const char *func_data, unsigned int *allowed_algos);
269 int ima_must_measure(struct inode *inode, int mask, enum ima_hooks func);
270 int ima_collect_measurement(struct integrity_iint_cache *iint,
271 			    struct file *file, void *buf, loff_t size,
272 			    enum hash_algo algo, struct modsig *modsig);
273 void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,
274 			   const unsigned char *filename,
275 			   struct evm_ima_xattr_data *xattr_value,
276 			   int xattr_len, const struct modsig *modsig, int pcr,
277 			   struct ima_template_desc *template_desc);
278 int process_buffer_measurement(struct mnt_idmap *idmap,
279 			       struct inode *inode, const void *buf, int size,
280 			       const char *eventname, enum ima_hooks func,
281 			       int pcr, const char *func_data,
282 			       bool buf_hash, u8 *digest, size_t digest_len);
283 void ima_audit_measurement(struct integrity_iint_cache *iint,
284 			   const unsigned char *filename);
285 int ima_alloc_init_template(struct ima_event_data *event_data,
286 			    struct ima_template_entry **entry,
287 			    struct ima_template_desc *template_desc);
288 int ima_store_template(struct ima_template_entry *entry, int violation,
289 		       struct inode *inode,
290 		       const unsigned char *filename, int pcr);
291 void ima_free_template_entry(struct ima_template_entry *entry);
292 const char *ima_d_path(const struct path *path, char **pathbuf, char *filename);
293 
294 /* IMA policy related functions */
295 int ima_match_policy(struct mnt_idmap *idmap, struct inode *inode,
296 		     const struct cred *cred, u32 secid, enum ima_hooks func,
297 		     int mask, int flags, int *pcr,
298 		     struct ima_template_desc **template_desc,
299 		     const char *func_data, unsigned int *allowed_algos);
300 void ima_init_policy(void);
301 void ima_update_policy(void);
302 void ima_update_policy_flags(void);
303 ssize_t ima_parse_add_rule(char *);
304 void ima_delete_rules(void);
305 int ima_check_policy(void);
306 void *ima_policy_start(struct seq_file *m, loff_t *pos);
307 void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos);
308 void ima_policy_stop(struct seq_file *m, void *v);
309 int ima_policy_show(struct seq_file *m, void *v);
310 
311 /* Appraise integrity measurements */
312 #define IMA_APPRAISE_ENFORCE	0x01
313 #define IMA_APPRAISE_FIX	0x02
314 #define IMA_APPRAISE_LOG	0x04
315 #define IMA_APPRAISE_MODULES	0x08
316 #define IMA_APPRAISE_FIRMWARE	0x10
317 #define IMA_APPRAISE_POLICY	0x20
318 #define IMA_APPRAISE_KEXEC	0x40
319 
320 #ifdef CONFIG_IMA_APPRAISE
321 int ima_check_blacklist(struct integrity_iint_cache *iint,
322 			const struct modsig *modsig, int pcr);
323 int ima_appraise_measurement(enum ima_hooks func,
324 			     struct integrity_iint_cache *iint,
325 			     struct file *file, const unsigned char *filename,
326 			     struct evm_ima_xattr_data *xattr_value,
327 			     int xattr_len, const struct modsig *modsig);
328 int ima_must_appraise(struct mnt_idmap *idmap, struct inode *inode,
329 		      int mask, enum ima_hooks func);
330 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file);
331 enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
332 					   enum ima_hooks func);
333 enum hash_algo ima_get_hash_algo(const struct evm_ima_xattr_data *xattr_value,
334 				 int xattr_len);
335 int ima_read_xattr(struct dentry *dentry,
336 		   struct evm_ima_xattr_data **xattr_value, int xattr_len);
337 
338 #else
339 static inline int ima_check_blacklist(struct integrity_iint_cache *iint,
340 				      const struct modsig *modsig, int pcr)
341 {
342 	return 0;
343 }
344 
345 static inline int ima_appraise_measurement(enum ima_hooks func,
346 					   struct integrity_iint_cache *iint,
347 					   struct file *file,
348 					   const unsigned char *filename,
349 					   struct evm_ima_xattr_data *xattr_value,
350 					   int xattr_len,
351 					   const struct modsig *modsig)
352 {
353 	return INTEGRITY_UNKNOWN;
354 }
355 
356 static inline int ima_must_appraise(struct mnt_idmap *idmap,
357 				    struct inode *inode, int mask,
358 				    enum ima_hooks func)
359 {
360 	return 0;
361 }
362 
363 static inline void ima_update_xattr(struct integrity_iint_cache *iint,
364 				    struct file *file)
365 {
366 }
367 
368 static inline enum integrity_status ima_get_cache_status(struct integrity_iint_cache
369 							 *iint,
370 							 enum ima_hooks func)
371 {
372 	return INTEGRITY_UNKNOWN;
373 }
374 
375 static inline enum hash_algo
376 ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len)
377 {
378 	return ima_hash_algo;
379 }
380 
381 static inline int ima_read_xattr(struct dentry *dentry,
382 				 struct evm_ima_xattr_data **xattr_value,
383 				 int xattr_len)
384 {
385 	return 0;
386 }
387 
388 #endif /* CONFIG_IMA_APPRAISE */
389 
390 #ifdef CONFIG_IMA_APPRAISE_MODSIG
391 int ima_read_modsig(enum ima_hooks func, const void *buf, loff_t buf_len,
392 		    struct modsig **modsig);
393 void ima_collect_modsig(struct modsig *modsig, const void *buf, loff_t size);
394 int ima_get_modsig_digest(const struct modsig *modsig, enum hash_algo *algo,
395 			  const u8 **digest, u32 *digest_size);
396 int ima_get_raw_modsig(const struct modsig *modsig, const void **data,
397 		       u32 *data_len);
398 void ima_free_modsig(struct modsig *modsig);
399 #else
400 static inline int ima_read_modsig(enum ima_hooks func, const void *buf,
401 				  loff_t buf_len, struct modsig **modsig)
402 {
403 	return -EOPNOTSUPP;
404 }
405 
406 static inline void ima_collect_modsig(struct modsig *modsig, const void *buf,
407 				      loff_t size)
408 {
409 }
410 
411 static inline int ima_get_modsig_digest(const struct modsig *modsig,
412 					enum hash_algo *algo, const u8 **digest,
413 					u32 *digest_size)
414 {
415 	return -EOPNOTSUPP;
416 }
417 
418 static inline int ima_get_raw_modsig(const struct modsig *modsig,
419 				     const void **data, u32 *data_len)
420 {
421 	return -EOPNOTSUPP;
422 }
423 
424 static inline void ima_free_modsig(struct modsig *modsig)
425 {
426 }
427 #endif /* CONFIG_IMA_APPRAISE_MODSIG */
428 
429 /* LSM based policy rules require audit */
430 #ifdef CONFIG_IMA_LSM_RULES
431 
432 #define ima_filter_rule_init security_audit_rule_init
433 #define ima_filter_rule_free security_audit_rule_free
434 #define ima_filter_rule_match security_audit_rule_match
435 
436 #else
437 
438 static inline int ima_filter_rule_init(u32 field, u32 op, char *rulestr,
439 				       void **lsmrule)
440 {
441 	return -EINVAL;
442 }
443 
444 static inline void ima_filter_rule_free(void *lsmrule)
445 {
446 }
447 
448 static inline int ima_filter_rule_match(u32 secid, u32 field, u32 op,
449 					void *lsmrule)
450 {
451 	return -EINVAL;
452 }
453 #endif /* CONFIG_IMA_LSM_RULES */
454 
455 #ifdef	CONFIG_IMA_READ_POLICY
456 #define	POLICY_FILE_FLAGS	(S_IWUSR | S_IRUSR)
457 #else
458 #define	POLICY_FILE_FLAGS	S_IWUSR
459 #endif /* CONFIG_IMA_READ_POLICY */
460 
461 #endif /* __LINUX_IMA_H */
462