Skip to content

Commit

Permalink
Merge branch 'ima-buffer-measurement-changes-v4' into next-integrity
Browse files Browse the repository at this point in the history
Based on cover letter "ima: Provide more info about buffer measurement":

Providing more information about buffer measurements requires
modification of the existing functions ima_measure_critical_data()
and process_buffer_measurement().  Unlike for file measurements,
there is no integrity_iint_cache structure for storing and retrieving
buffer measurements.

With these changes, kernel subsystems relying on IMA to measure
critical data don't have to duplicate the function to calculate the
buffer digest but, instead, can get directly the one that IMA would
insert, depending on the policy, in the new measurement list entry.
  • Loading branch information
Mimi Zohar committed Jul 23, 2021
2 parents a32ad90 + ca3c9bd commit e37be53
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 38 deletions.
23 changes: 17 additions & 6 deletions include/linux/ima.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
#include <linux/fs.h>
#include <linux/security.h>
#include <linux/kexec.h>
#include <crypto/hash_info.h>
struct linux_binprm;

#ifdef CONFIG_IMA
extern enum hash_algo ima_get_current_hash_algo(void);
extern int ima_bprm_check(struct linux_binprm *bprm);
extern int ima_file_check(struct file *file, int mask);
extern void ima_post_create_tmpfile(struct user_namespace *mnt_userns,
Expand All @@ -33,10 +35,10 @@ extern void ima_post_path_mknod(struct user_namespace *mnt_userns,
extern int ima_file_hash(struct file *file, char *buf, size_t buf_size);
extern int ima_inode_hash(struct inode *inode, char *buf, size_t buf_size);
extern void ima_kexec_cmdline(int kernel_fd, const void *buf, int size);
extern void ima_measure_critical_data(const char *event_label,
const char *event_name,
const void *buf, size_t buf_len,
bool hash);
extern int ima_measure_critical_data(const char *event_label,
const char *event_name,
const void *buf, size_t buf_len,
bool hash, u8 *digest, size_t digest_len);

#ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
extern void ima_appraise_parse_cmdline(void);
Expand Down Expand Up @@ -64,6 +66,11 @@ static inline const char * const *arch_get_ima_policy(void)
#endif

#else
static inline enum hash_algo ima_get_current_hash_algo(void)
{
return HASH_ALGO__LAST;
}

static inline int ima_bprm_check(struct linux_binprm *bprm)
{
return 0;
Expand Down Expand Up @@ -137,10 +144,14 @@ static inline int ima_inode_hash(struct inode *inode, char *buf, size_t buf_size

static inline void ima_kexec_cmdline(int kernel_fd, const void *buf, int size) {}

static inline void ima_measure_critical_data(const char *event_label,
static inline int ima_measure_critical_data(const char *event_label,
const char *event_name,
const void *buf, size_t buf_len,
bool hash) {}
bool hash, u8 *digest,
size_t digest_len)
{
return -ENOENT;
}

#endif /* CONFIG_IMA */

Expand Down
10 changes: 5 additions & 5 deletions security/integrity/ima/ima.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,11 @@ void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,
struct evm_ima_xattr_data *xattr_value,
int xattr_len, const struct modsig *modsig, int pcr,
struct ima_template_desc *template_desc);
void process_buffer_measurement(struct user_namespace *mnt_userns,
struct inode *inode, const void *buf, int size,
const char *eventname, enum ima_hooks func,
int pcr, const char *func_data,
bool buf_hash);
int process_buffer_measurement(struct user_namespace *mnt_userns,
struct inode *inode, const void *buf, int size,
const char *eventname, enum ima_hooks func,
int pcr, const char *func_data,
bool buf_hash, u8 *digest, size_t digest_len);
void ima_audit_measurement(struct integrity_iint_cache *iint,
const unsigned char *filename);
int ima_alloc_init_template(struct ima_event_data *event_data,
Expand Down
2 changes: 1 addition & 1 deletion security/integrity/ima/ima_appraise.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ int ima_check_blacklist(struct integrity_iint_cache *iint,
if ((rc == -EPERM) && (iint->flags & IMA_MEASURE))
process_buffer_measurement(&init_user_ns, NULL, digest, digestsize,
"blacklisted-hash", NONE,
pcr, NULL, false);
pcr, NULL, false, NULL, 0);
}

return rc;
Expand Down
2 changes: 1 addition & 1 deletion security/integrity/ima/ima_asymmetric_keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ void ima_post_key_create_or_update(struct key *keyring, struct key *key,
*/
process_buffer_measurement(&init_user_ns, NULL, payload, payload_len,
keyring->description, KEY_CHECK, 0,
keyring->description, false);
keyring->description, false, NULL, 0);
}
3 changes: 2 additions & 1 deletion security/integrity/ima/ima_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ int __init ima_init(void)
ima_init_key_queue();

ima_measure_critical_data("kernel_info", "kernel_version",
UTS_RELEASE, strlen(UTS_RELEASE), false);
UTS_RELEASE, strlen(UTS_RELEASE), false,
NULL, 0);

return rc;
}
69 changes: 48 additions & 21 deletions security/integrity/ima/ima_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int ima_appraise = IMA_APPRAISE_ENFORCE;
int ima_appraise;
#endif

int ima_hash_algo = HASH_ALGO_SHA1;
int __ro_after_init ima_hash_algo = HASH_ALGO_SHA1;
static int hash_setup_done;

static struct notifier_block ima_lsm_policy_notifier = {
Expand Down Expand Up @@ -76,6 +76,11 @@ static int __init hash_setup(char *str)
}
__setup("ima_hash=", hash_setup);

enum hash_algo ima_get_current_hash_algo(void)
{
return ima_hash_algo;
}

/* Prevent mmap'ing a file execute that is already mmap'ed write */
static int mmap_violation_check(enum ima_hooks func, struct file *file,
char **pathbuf, const char **pathname,
Expand Down Expand Up @@ -822,7 +827,7 @@ int ima_post_load_data(char *buf, loff_t size,
return 0;
}

/*
/**
* process_buffer_measurement - Measure the buffer or the buffer data hash
* @mnt_userns: user namespace of the mount the inode was found from
* @inode: inode associated with the object being measured (NULL for KEY_CHECK)
Expand All @@ -833,14 +838,20 @@ int ima_post_load_data(char *buf, loff_t size,
* @pcr: pcr to extend the measurement
* @func_data: func specific data, may be NULL
* @buf_hash: measure buffer data hash
* @digest: buffer digest will be written to
* @digest_len: buffer length
*
* Based on policy, either the buffer data or buffer data hash is measured
*
* Return: 0 if the buffer has been successfully measured, 1 if the digest
* has been written to the passed location but not added to a measurement entry,
* a negative value otherwise.
*/
void process_buffer_measurement(struct user_namespace *mnt_userns,
struct inode *inode, const void *buf, int size,
const char *eventname, enum ima_hooks func,
int pcr, const char *func_data,
bool buf_hash)
int process_buffer_measurement(struct user_namespace *mnt_userns,
struct inode *inode, const void *buf, int size,
const char *eventname, enum ima_hooks func,
int pcr, const char *func_data,
bool buf_hash, u8 *digest, size_t digest_len)
{
int ret = 0;
const char *audit_cause = "ENOMEM";
Expand All @@ -861,8 +872,11 @@ void process_buffer_measurement(struct user_namespace *mnt_userns,
int action = 0;
u32 secid;

if (!ima_policy_flag)
return;
if (digest && digest_len < digest_hash_len)
return -EINVAL;

if (!ima_policy_flag && !digest)
return -ENOENT;

template = ima_template_desc_buf();
if (!template) {
Expand All @@ -883,8 +897,8 @@ void process_buffer_measurement(struct user_namespace *mnt_userns,
action = ima_get_action(mnt_userns, inode, current_cred(),
secid, 0, func, &pcr, &template,
func_data);
if (!(action & IMA_MEASURE))
return;
if (!(action & IMA_MEASURE) && !digest)
return -ENOENT;
}

if (!pcr)
Expand Down Expand Up @@ -914,6 +928,12 @@ void process_buffer_measurement(struct user_namespace *mnt_userns,
event_data.buf_len = digest_hash_len;
}

if (digest)
memcpy(digest, iint.ima_hash->digest, digest_hash_len);

if (!ima_policy_flag || (func && !(action & IMA_MEASURE)))
return 1;

ret = ima_alloc_init_template(&event_data, &entry, template);
if (ret < 0) {
audit_cause = "alloc_entry";
Expand All @@ -932,7 +952,7 @@ void process_buffer_measurement(struct user_namespace *mnt_userns,
func_measure_str(func),
audit_cause, ret, 0, ret);

return;
return ret;
}

/**
Expand All @@ -956,7 +976,7 @@ void ima_kexec_cmdline(int kernel_fd, const void *buf, int size)

process_buffer_measurement(file_mnt_user_ns(f.file), file_inode(f.file),
buf, size, "kexec-cmdline", KEXEC_CMDLINE, 0,
NULL, false);
NULL, false, NULL, 0);
fdput(f);
}

Expand All @@ -967,23 +987,30 @@ void ima_kexec_cmdline(int kernel_fd, const void *buf, int size)
* @buf: pointer to buffer data
* @buf_len: length of buffer data (in bytes)
* @hash: measure buffer data hash
* @digest: buffer digest will be written to
* @digest_len: buffer length
*
* Measure data critical to the integrity of the kernel into the IMA log
* and extend the pcr. Examples of critical data could be various data
* structures, policies, and states stored in kernel memory that can
* impact the integrity of the system.
*
* Return: 0 if the buffer has been successfully measured, 1 if the digest
* has been written to the passed location but not added to a measurement entry,
* a negative value otherwise.
*/
void ima_measure_critical_data(const char *event_label,
const char *event_name,
const void *buf, size_t buf_len,
bool hash)
int ima_measure_critical_data(const char *event_label,
const char *event_name,
const void *buf, size_t buf_len,
bool hash, u8 *digest, size_t digest_len)
{
if (!event_name || !event_label || !buf || !buf_len)
return;
return -ENOPARAM;

process_buffer_measurement(&init_user_ns, NULL, buf, buf_len, event_name,
CRITICAL_DATA, 0, event_label,
hash);
return process_buffer_measurement(&init_user_ns, NULL, buf, buf_len,
event_name, CRITICAL_DATA, 0,
event_label, hash, digest,
digest_len);
}

static int __init init_ima(void)
Expand Down
2 changes: 1 addition & 1 deletion security/integrity/ima/ima_queue_keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void ima_process_queued_keys(void)
entry->keyring_name,
KEY_CHECK, 0,
entry->keyring_name,
false);
false, NULL, 0);
list_del(&entry->list);
ima_free_key_entry(entry);
}
Expand Down
6 changes: 4 additions & 2 deletions security/selinux/ima.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ void selinux_ima_measure_state_locked(struct selinux_state *state)
}

ima_measure_critical_data("selinux", "selinux-state",
state_str, strlen(state_str), false);
state_str, strlen(state_str), false,
NULL, 0);

kfree(state_str);

Expand All @@ -103,7 +104,8 @@ void selinux_ima_measure_state_locked(struct selinux_state *state)
}

ima_measure_critical_data("selinux", "selinux-policy-hash",
policy, policy_len, true);
policy, policy_len, true,
NULL, 0);

vfree(policy);
}
Expand Down

0 comments on commit e37be53

Please sign in to comment.