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