Skip to content

Commit 6beea7a

Browse files
Florent Revestmimizohar
authored andcommitted
ima: add the ability to query the cached hash of a given file
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 Signed-off-by: Florent Revest <[email protected]> Reviewed-by: KP Singh <[email protected]> Signed-off-by: Mimi Zohar <[email protected]>
1 parent 5350ceb commit 6beea7a

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

include/linux/ima.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern int ima_read_file(struct file *file, enum kernel_read_file_id id);
2323
extern int ima_post_read_file(struct file *file, void *buf, loff_t size,
2424
enum kernel_read_file_id id);
2525
extern void ima_post_path_mknod(struct dentry *dentry);
26+
extern int ima_file_hash(struct file *file, char *buf, size_t buf_size);
2627
extern void ima_kexec_cmdline(const void *buf, int size);
2728

2829
#ifdef CONFIG_IMA_KEXEC
@@ -91,6 +92,11 @@ static inline void ima_post_path_mknod(struct dentry *dentry)
9192
return;
9293
}
9394

95+
static inline int ima_file_hash(struct file *file, char *buf, size_t buf_size)
96+
{
97+
return -EOPNOTSUPP;
98+
}
99+
94100
static inline void ima_kexec_cmdline(const void *buf, int size) {}
95101
#endif /* CONFIG_IMA */
96102

security/integrity/ima/ima_main.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,55 @@ int ima_file_check(struct file *file, int mask)
445445
}
446446
EXPORT_SYMBOL_GPL(ima_file_check);
447447

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+
448497
/**
449498
* ima_post_create_tmpfile - mark newly created tmpfile as new
450499
* @file : newly created tmpfile

0 commit comments

Comments
 (0)