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 /* set during initialization */ 50 extern int ima_hash_algo; 51 extern int ima_sha1_idx __ro_after_init; 52 extern int ima_hash_algo_idx __ro_after_init; 53 extern int ima_extra_slots __ro_after_init; 54 extern int ima_appraise; 55 extern struct tpm_chip *ima_tpm_chip; 56 extern const char boot_aggregate_name[]; 57 58 /* IMA event related data */ 59 struct ima_event_data { 60 struct integrity_iint_cache *iint; 61 struct file *file; 62 const unsigned char *filename; 63 struct evm_ima_xattr_data *xattr_value; 64 int xattr_len; 65 const struct modsig *modsig; 66 const char *violation; 67 const void *buf; 68 int buf_len; 69 }; 70 71 /* IMA template field data definition */ 72 struct ima_field_data { 73 u8 *data; 74 u32 len; 75 }; 76 77 /* IMA template field definition */ 78 struct ima_template_field { 79 const char field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN]; 80 int (*field_init)(struct ima_event_data *event_data, 81 struct ima_field_data *field_data); 82 void (*field_show)(struct seq_file *m, enum ima_show_type show, 83 struct ima_field_data *field_data); 84 }; 85 86 /* IMA template descriptor definition */ 87 struct ima_template_desc { 88 struct list_head list; 89 char *name; 90 char *fmt; 91 int num_fields; 92 const struct ima_template_field **fields; 93 }; 94 95 struct ima_template_entry { 96 int pcr; 97 struct tpm_digest *digests; 98 struct ima_template_desc *template_desc; /* template descriptor */ 99 u32 template_data_len; 100 struct ima_field_data template_data[]; /* template related data */ 101 }; 102 103 struct ima_queue_entry { 104 struct hlist_node hnext; /* place in hash collision list */ 105 struct list_head later; /* place in ima_measurements list */ 106 struct ima_template_entry *entry; 107 }; 108 extern struct list_head ima_measurements; /* list of all measurements */ 109 110 /* Some details preceding the binary serialized measurement list */ 111 struct ima_kexec_hdr { 112 u16 version; 113 u16 _reserved0; 114 u32 _reserved1; 115 u64 buffer_size; 116 u64 count; 117 }; 118 119 extern const int read_idmap[]; 120 121 #ifdef CONFIG_HAVE_IMA_KEXEC 122 void ima_load_kexec_buffer(void); 123 #else 124 static inline void ima_load_kexec_buffer(void) {} 125 #endif /* CONFIG_HAVE_IMA_KEXEC */ 126 127 /* 128 * The default binary_runtime_measurements list format is defined as the 129 * platform native format. The canonical format is defined as little-endian. 130 */ 131 extern bool ima_canonical_fmt; 132 133 /* Internal IMA function definitions */ 134 int ima_init(void); 135 int ima_fs_init(void); 136 int ima_add_template_entry(struct ima_template_entry *entry, int violation, 137 const char *op, struct inode *inode, 138 const unsigned char *filename); 139 int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash); 140 int ima_calc_buffer_hash(const void *buf, loff_t len, 141 struct ima_digest_data *hash); 142 int ima_calc_field_array_hash(struct ima_field_data *field_data, 143 struct ima_template_entry *entry); 144 int ima_calc_boot_aggregate(struct ima_digest_data *hash); 145 void ima_add_violation(struct file *file, const unsigned char *filename, 146 struct integrity_iint_cache *iint, 147 const char *op, const char *cause); 148 int ima_init_crypto(void); 149 void ima_putc(struct seq_file *m, void *data, int datalen); 150 void ima_print_digest(struct seq_file *m, u8 *digest, u32 size); 151 int template_desc_init_fields(const char *template_fmt, 152 const struct ima_template_field ***fields, 153 int *num_fields); 154 struct ima_template_desc *ima_template_desc_current(void); 155 struct ima_template_desc *ima_template_desc_buf(void); 156 struct ima_template_desc *lookup_template_desc(const char *name); 157 bool ima_template_has_modsig(const struct ima_template_desc *ima_template); 158 int ima_restore_measurement_entry(struct ima_template_entry *entry); 159 int ima_restore_measurement_list(loff_t bufsize, void *buf); 160 int ima_measurements_show(struct seq_file *m, void *v); 161 unsigned long ima_get_binary_runtime_size(void); 162 int ima_init_template(void); 163 void ima_init_template_list(void); 164 int __init ima_init_digests(void); 165 int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event, 166 void *lsm_data); 167 168 /* 169 * used to protect h_table and sha_table 170 */ 171 extern spinlock_t ima_queue_lock; 172 173 struct ima_h_table { 174 atomic_long_t len; /* number of stored measurements in the list */ 175 atomic_long_t violations; 176 struct hlist_head queue[IMA_MEASURE_HTABLE_SIZE]; 177 }; 178 extern struct ima_h_table ima_htable; 179 180 static inline unsigned int ima_hash_key(u8 *digest) 181 { 182 /* there is no point in taking a hash of part of a digest */ 183 return (digest[0] | digest[1] << 8) % IMA_MEASURE_HTABLE_SIZE; 184 } 185 186 #define __ima_hooks(hook) \ 187 hook(NONE, none) \ 188 hook(FILE_CHECK, file) \ 189 hook(MMAP_CHECK, mmap) \ 190 hook(BPRM_CHECK, bprm) \ 191 hook(CREDS_CHECK, creds) \ 192 hook(POST_SETATTR, post_setattr) \ 193 hook(MODULE_CHECK, module) \ 194 hook(FIRMWARE_CHECK, firmware) \ 195 hook(KEXEC_KERNEL_CHECK, kexec_kernel) \ 196 hook(KEXEC_INITRAMFS_CHECK, kexec_initramfs) \ 197 hook(POLICY_CHECK, policy) \ 198 hook(KEXEC_CMDLINE, kexec_cmdline) \ 199 hook(KEY_CHECK, key) \ 200 hook(CRITICAL_DATA, critical_data) \ 201 hook(MAX_CHECK, none) 202 203 #define __ima_hook_enumify(ENUM, str) ENUM, 204 #define __ima_stringify(arg) (#arg) 205 #define __ima_hook_measuring_stringify(ENUM, str) \ 206 (__ima_stringify(measuring_ ##str)), 207 208 enum ima_hooks { 209 __ima_hooks(__ima_hook_enumify) 210 }; 211 212 static const char * const ima_hooks_measure_str[] = { 213 __ima_hooks(__ima_hook_measuring_stringify) 214 }; 215 216 static inline const char *func_measure_str(enum ima_hooks func) 217 { 218 if (func >= MAX_CHECK) 219 return ima_hooks_measure_str[NONE]; 220 221 return ima_hooks_measure_str[func]; 222 } 223 224 extern const char *const func_tokens[]; 225 226 struct modsig; 227 228 #ifdef CONFIG_IMA_QUEUE_EARLY_BOOT_KEYS 229 /* 230 * To track keys that need to be measured. 231 */ 232 struct ima_key_entry { 233 struct list_head list; 234 void *payload; 235 size_t payload_len; 236 char *keyring_name; 237 }; 238 void ima_init_key_queue(void); 239 bool ima_should_queue_key(void); 240 bool ima_queue_key(struct key *keyring, const void *payload, 241 size_t payload_len); 242 void ima_process_queued_keys(void); 243 #else 244 static inline void ima_init_key_queue(void) {} 245 static inline bool ima_should_queue_key(void) { return false; } 246 static inline bool ima_queue_key(struct key *keyring, 247 const void *payload, 248 size_t payload_len) { return false; } 249 static inline void ima_process_queued_keys(void) {} 250 #endif /* CONFIG_IMA_QUEUE_EARLY_BOOT_KEYS */ 251 252 /* LIM API function definitions */ 253 int ima_get_action(struct user_namespace *mnt_userns, struct inode *inode, 254 const struct cred *cred, u32 secid, int mask, 255 enum ima_hooks func, int *pcr, 256 struct ima_template_desc **template_desc, 257 const char *func_data); 258 int ima_must_measure(struct inode *inode, int mask, enum ima_hooks func); 259 int ima_collect_measurement(struct integrity_iint_cache *iint, 260 struct file *file, void *buf, loff_t size, 261 enum hash_algo algo, struct modsig *modsig); 262 void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file, 263 const unsigned char *filename, 264 struct evm_ima_xattr_data *xattr_value, 265 int xattr_len, const struct modsig *modsig, int pcr, 266 struct ima_template_desc *template_desc); 267 void process_buffer_measurement(struct user_namespace *mnt_userns, 268 struct inode *inode, const void *buf, int size, 269 const char *eventname, enum ima_hooks func, 270 int pcr, const char *func_data, 271 bool buf_hash); 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 user_namespace *mnt_userns, struct inode *inode, 285 const struct cred *cred, u32 secid, enum ima_hooks func, 286 int mask, int flags, int *pcr, 287 struct ima_template_desc **template_desc, 288 const char *func_data); 289 void ima_init_policy(void); 290 void ima_update_policy(void); 291 void ima_update_policy_flag(void); 292 ssize_t ima_parse_add_rule(char *); 293 void ima_delete_rules(void); 294 int ima_check_policy(void); 295 void *ima_policy_start(struct seq_file *m, loff_t *pos); 296 void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos); 297 void ima_policy_stop(struct seq_file *m, void *v); 298 int ima_policy_show(struct seq_file *m, void *v); 299 300 /* Appraise integrity measurements */ 301 #define IMA_APPRAISE_ENFORCE 0x01 302 #define IMA_APPRAISE_FIX 0x02 303 #define IMA_APPRAISE_LOG 0x04 304 #define IMA_APPRAISE_MODULES 0x08 305 #define IMA_APPRAISE_FIRMWARE 0x10 306 #define IMA_APPRAISE_POLICY 0x20 307 #define IMA_APPRAISE_KEXEC 0x40 308 309 #ifdef CONFIG_IMA_APPRAISE 310 int ima_check_blacklist(struct integrity_iint_cache *iint, 311 const struct modsig *modsig, int pcr); 312 int ima_appraise_measurement(enum ima_hooks func, 313 struct integrity_iint_cache *iint, 314 struct file *file, const unsigned char *filename, 315 struct evm_ima_xattr_data *xattr_value, 316 int xattr_len, const struct modsig *modsig); 317 int ima_must_appraise(struct user_namespace *mnt_userns, struct inode *inode, 318 int mask, enum ima_hooks func); 319 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file); 320 enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint, 321 enum ima_hooks func); 322 enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, 323 int xattr_len); 324 int ima_read_xattr(struct dentry *dentry, 325 struct evm_ima_xattr_data **xattr_value); 326 327 #else 328 static inline int ima_check_blacklist(struct integrity_iint_cache *iint, 329 const struct modsig *modsig, int pcr) 330 { 331 return 0; 332 } 333 334 static inline int ima_appraise_measurement(enum ima_hooks func, 335 struct integrity_iint_cache *iint, 336 struct file *file, 337 const unsigned char *filename, 338 struct evm_ima_xattr_data *xattr_value, 339 int xattr_len, 340 const struct modsig *modsig) 341 { 342 return INTEGRITY_UNKNOWN; 343 } 344 345 static inline int ima_must_appraise(struct user_namespace *mnt_userns, 346 struct inode *inode, int mask, 347 enum ima_hooks func) 348 { 349 return 0; 350 } 351 352 static inline void ima_update_xattr(struct integrity_iint_cache *iint, 353 struct file *file) 354 { 355 } 356 357 static inline enum integrity_status ima_get_cache_status(struct integrity_iint_cache 358 *iint, 359 enum ima_hooks func) 360 { 361 return INTEGRITY_UNKNOWN; 362 } 363 364 static inline enum hash_algo 365 ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value, int xattr_len) 366 { 367 return ima_hash_algo; 368 } 369 370 static inline int ima_read_xattr(struct dentry *dentry, 371 struct evm_ima_xattr_data **xattr_value) 372 { 373 return 0; 374 } 375 376 #endif /* CONFIG_IMA_APPRAISE */ 377 378 #ifdef CONFIG_IMA_APPRAISE_MODSIG 379 int ima_read_modsig(enum ima_hooks func, const void *buf, loff_t buf_len, 380 struct modsig **modsig); 381 void ima_collect_modsig(struct modsig *modsig, const void *buf, loff_t size); 382 int ima_get_modsig_digest(const struct modsig *modsig, enum hash_algo *algo, 383 const u8 **digest, u32 *digest_size); 384 int ima_get_raw_modsig(const struct modsig *modsig, const void **data, 385 u32 *data_len); 386 void ima_free_modsig(struct modsig *modsig); 387 #else 388 static inline int ima_read_modsig(enum ima_hooks func, const void *buf, 389 loff_t buf_len, struct modsig **modsig) 390 { 391 return -EOPNOTSUPP; 392 } 393 394 static inline void ima_collect_modsig(struct modsig *modsig, const void *buf, 395 loff_t size) 396 { 397 } 398 399 static inline int ima_get_modsig_digest(const struct modsig *modsig, 400 enum hash_algo *algo, const u8 **digest, 401 u32 *digest_size) 402 { 403 return -EOPNOTSUPP; 404 } 405 406 static inline int ima_get_raw_modsig(const struct modsig *modsig, 407 const void **data, u32 *data_len) 408 { 409 return -EOPNOTSUPP; 410 } 411 412 static inline void ima_free_modsig(struct modsig *modsig) 413 { 414 } 415 #endif /* CONFIG_IMA_APPRAISE_MODSIG */ 416 417 /* LSM based policy rules require audit */ 418 #ifdef CONFIG_IMA_LSM_RULES 419 420 #define ima_filter_rule_init security_audit_rule_init 421 #define ima_filter_rule_free security_audit_rule_free 422 #define ima_filter_rule_match security_audit_rule_match 423 424 #else 425 426 static inline int ima_filter_rule_init(u32 field, u32 op, char *rulestr, 427 void **lsmrule) 428 { 429 return -EINVAL; 430 } 431 432 static inline void ima_filter_rule_free(void *lsmrule) 433 { 434 } 435 436 static inline int ima_filter_rule_match(u32 secid, u32 field, u32 op, 437 void *lsmrule) 438 { 439 return -EINVAL; 440 } 441 #endif /* CONFIG_IMA_LSM_RULES */ 442 443 #ifdef CONFIG_IMA_READ_POLICY 444 #define POLICY_FILE_FLAGS (S_IWUSR | S_IRUSR) 445 #else 446 #define POLICY_FILE_FLAGS S_IWUSR 447 #endif /* CONFIG_IMA_READ_POLICY */ 448 449 #endif /* __LINUX_IMA_H */ 450