This allows other parts of the kernel (perhaps a stacked LSM allowing system monitoring, eg. the proposed KRSI LSM [1]) to retrieve the hash of a given file from IMA if it's present in the iint cache. It's true that the existence of the hash means that it's also in the audit logs or in /sys/kernel/security/ima/ascii_runtime_measurements, but it can be difficult to pull that information out for every subsequent exec. This is especially true if a given host has been up for a long time and the file was first measured a long time ago. It should be kept in mind that this function gives access to cached entries which can be removed, for instance on security_inode_free(). This is based on Peter Moody's patch: https://sourceforge.net/p/linux-ima/mailman/message/33036180/ [1] https://lkml.org/lkml/2019/9/10/393
Unified · Split
| @@ -23,6 +23,7 @@ extern int ima_read_file(struct file *file, enum kernel_read_file_id id); · 22 unmodified lines | |||
| 23 | extern int ima_post_read_file(struct file *file, void *buf, loff_t size, | 23 | extern int ima_post_read_file(struct file *file, void *buf, loff_t size, |
| 24 | enum kernel_read_file_id id); | 24 | enum kernel_read_file_id id); |
| 25 | extern void ima_post_path_mknod(struct dentry *dentry); | 25 | extern void ima_post_path_mknod(struct dentry *dentry); |
| 26 | extern int ima_file_hash(struct file *file, char *buf, size_t buf_size); | ||
| 26 | extern void ima_kexec_cmdline(const void *buf, int size); | 27 | extern void ima_kexec_cmdline(const void *buf, int size); |
| 27 | 28 | ||
| 28 | #ifdef CONFIG_IMA_KEXEC | 29 | #ifdef CONFIG_IMA_KEXEC |
| @@ -91,6 +92,11 @@ static inline void ima_post_path_mknod(struct dentry *dentry) · 62 unmodified lines | |||
| 91 | return; | 92 | return; |
| 92 | } | 93 | } |
| 93 | 94 | ||
| 95 | static inline int ima_file_hash(struct file *file, char *buf, size_t buf_size) | ||
| 96 | { | ||
| 97 | return -EOPNOTSUPP; | ||
| 98 | } | ||
| 99 | |||
| 94 | static inline void ima_kexec_cmdline(const void *buf, int size) {} | 100 | static inline void ima_kexec_cmdline(const void *buf, int size) {} |
| 95 | #endif /* CONFIG_IMA */ | 101 | #endif /* CONFIG_IMA */ |
| 96 | 102 | ||
| @@ -445,6 +445,55 @@ int ima_file_check(struct file *file, int mask) · 444 unmodified lines | |||
| 445 | } | 445 | } |
| 446 | EXPORT_SYMBOL_GPL(ima_file_check); | 446 | EXPORT_SYMBOL_GPL(ima_file_check); |
| 447 | 447 | ||
| 448 | /** | ||
| 449 | * ima_file_hash - return the stored measurement if a file has been hashed and | ||
| 450 | * is in the iint cache. | ||
| 451 | * @file: pointer to the file | ||
| 452 | * @buf: buffer in which to store the hash | ||
| 453 | * @buf_size: length of the buffer | ||
| 454 | * | ||
| 455 | * On success, return the hash algorithm (as defined in the enum hash_algo). | ||
| 456 | * If buf is not NULL, this function also outputs the hash into buf. | ||
| 457 | * If the hash is larger than buf_size, then only buf_size bytes will be copied. | ||
| 458 | * It generally just makes sense to pass a buffer capable of holding the largest | ||
| 459 | * possible hash: IMA_MAX_DIGEST_SIZE. | ||
| 460 | * The file hash returned is based on the entire file, including the appended | ||
| 461 | * signature. | ||
| 462 | * | ||
| 463 | * If IMA is disabled or if no measurement is available, return -EOPNOTSUPP. | ||
| 464 | * If the parameters are incorrect, return -EINVAL. | ||
| 465 | */ | ||
| 466 | int ima_file_hash(struct file *file, char *buf, size_t buf_size) | ||
| 467 | { | ||
| 468 | struct inode *inode; | ||
| 469 | struct integrity_iint_cache *iint; | ||
| 470 | int hash_algo; | ||
| 471 | |||
| 472 | if (!file) | ||
| 473 | return -EINVAL; | ||
| 474 | |||
| 475 | if (!ima_policy_flag) | ||
| 476 | return -EOPNOTSUPP; | ||
| 477 | |||
| 478 | inode = file_inode(file); | ||
| 479 | iint = integrity_iint_find(inode); | ||
| 480 | if (!iint) | ||
| 481 | return -EOPNOTSUPP; | ||
| 482 | |||
| 483 | mutex_lock(&iint->mutex); | ||
| 484 | if (buf) { | ||
| 485 | size_t copied_size; | ||
| 486 | |||
| 487 | copied_size = min_t(size_t, iint->ima_hash->length, buf_size); | ||
| 488 | memcpy(buf, iint->ima_hash->digest, copied_size); | ||
| 489 | } | ||
| 490 | hash_algo = iint->ima_hash->algo; | ||
| 491 | mutex_unlock(&iint->mutex); | ||
| 492 | |||
| 493 | return hash_algo; | ||
| 494 | } | ||
| 495 | EXPORT_SYMBOL_GPL(ima_file_hash); | ||
| 496 | |||
| 448 | /** | 497 | /** |
| 449 | * ima_post_create_tmpfile - mark newly created tmpfile as new | 498 | * ima_post_create_tmpfile - mark newly created tmpfile as new |
| 450 | * @file : newly created tmpfile | 499 | * @file : newly created tmpfile |