ima_api.c 7.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * Copyright (C) 2008 IBM Corporation
 *
 * Author: Mimi Zohar <zohar@us.ibm.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, version 2 of the
 * License.
 *
 * File: ima_api.c
M
Mimi Zohar 已提交
12 13
 *	Implements must_appraise_or_measure, collect_measurement,
 *	appraise_measurement, store_measurement and store_template.
14 15
 */
#include <linux/module.h>
16
#include <linux/slab.h>
M
Mimi Zohar 已提交
17 18 19 20
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/xattr.h>
#include <linux/evm.h>
21
#include "ima.h"
M
Mimi Zohar 已提交
22

M
Mimi Zohar 已提交
23
static const char *IMA_TEMPLATE_NAME = "ima";
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

/*
 * ima_store_template - store ima template measurements
 *
 * Calculate the hash of a template entry, add the template entry
 * to an ordered list of measurement entries maintained inside the kernel,
 * and also update the aggregate integrity value (maintained inside the
 * configured TPM PCR) over the hashes of the current list of measurement
 * entries.
 *
 * Applications retrieve the current kernel-held measurement list through
 * the securityfs entries in /sys/kernel/security/ima. The signed aggregate
 * TPM PCR (called quote) can be retrieved using a TPM user space library
 * and is used to validate the measurement list.
 *
 * Returns 0 on success, error code otherwise
 */
int ima_store_template(struct ima_template_entry *entry,
		       int violation, struct inode *inode)
{
	const char *op = "add_template_measure";
	const char *audit_cause = "hashing_error";
	int result;
47
	struct ima_digest_data hash;
48 49 50 51 52 53

	memset(entry->digest, 0, sizeof(entry->digest));
	entry->template_name = IMA_TEMPLATE_NAME;
	entry->template_len = sizeof(entry->template);

	if (!violation) {
54
		result = ima_calc_buffer_hash(&entry->template,
55
					      entry->template_len, &hash);
56 57 58 59 60 61
		if (result < 0) {
			integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode,
					    entry->template_name, op,
					    audit_cause, result, 0);
			return result;
		}
62
		memcpy(entry->digest, hash.digest, hash.length);
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
	}
	result = ima_add_template_entry(entry, violation, op, inode);
	return result;
}

/*
 * ima_add_violation - add violation to measurement list.
 *
 * Violations are flagged in the measurement list with zero hash values.
 * By extending the PCR with 0xFF's instead of with zeroes, the PCR
 * value is invalidated.
 */
void ima_add_violation(struct inode *inode, const unsigned char *filename,
		       const char *op, const char *cause)
{
	struct ima_template_entry *entry;
	int violation = 1;
	int result;

	/* can overflow, only indicator */
	atomic_long_inc(&ima_htable.violations);

	entry = kmalloc(sizeof(*entry), GFP_KERNEL);
	if (!entry) {
		result = -ENOMEM;
		goto err_out;
	}
	memset(&entry->template, 0, sizeof(entry->template));
	strncpy(entry->template.file_name, filename, IMA_EVENT_NAME_LEN_MAX);
	result = ima_store_template(entry, violation, inode);
	if (result < 0)
		kfree(entry);
err_out:
	integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
			    op, cause, result, 0);
}

/**
101
 * ima_get_action - appraise & measure decision based on policy.
102 103
 * @inode: pointer to inode to measure
 * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXECUTE)
M
Mimi Zohar 已提交
104
 * @function: calling function (FILE_CHECK, BPRM_CHECK, MMAP_CHECK, MODULE_CHECK)
105 106 107 108
 *
 * The policy is defined in terms of keypairs:
 * 		subj=, obj=, type=, func=, mask=, fsmagic=
 *	subj,obj, and type: are LSM specific.
M
Mimi Zohar 已提交
109
 * 	func: FILE_CHECK | BPRM_CHECK | MMAP_CHECK | MODULE_CHECK
110 111 112
 * 	mask: contains the permission mask
 *	fsmagic: hex value
 *
M
Mimi Zohar 已提交
113 114 115
 * Returns IMA_MEASURE, IMA_APPRAISE mask.
 *
 */
116
int ima_get_action(struct inode *inode, int mask, int function)
117
{
P
Peter Moody 已提交
118
	int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE;
M
Mimi Zohar 已提交
119 120 121 122 123 124

	if (!ima_appraise)
		flags &= ~IMA_APPRAISE;

	return ima_match_policy(inode, function, mask, flags);
}
125

M
Mimi Zohar 已提交
126 127 128
int ima_must_measure(struct inode *inode, int mask, int function)
{
	return ima_match_policy(inode, function, mask, IMA_MEASURE);
129 130 131 132 133 134 135 136 137 138 139 140
}

/*
 * ima_collect_measurement - collect file measurement
 *
 * Calculate the file hash, if it doesn't already exist,
 * storing the measurement and i_version in the iint.
 *
 * Must be called with iint->mutex held.
 *
 * Return 0 on success, error code otherwise
 */
141 142
int ima_collect_measurement(struct integrity_iint_cache *iint,
			    struct file *file)
143
{
A
Al Viro 已提交
144
	struct inode *inode = file_inode(file);
M
Mimi Zohar 已提交
145 146
	const char *filename = file->f_dentry->d_name.name;
	int result = 0;
147

M
Mimi Zohar 已提交
148
	if (!(iint->flags & IMA_COLLECTED)) {
A
Al Viro 已提交
149
		u64 i_version = file_inode(file)->i_version;
150

151 152 153
		/* use default hash algorithm */
		iint->ima_hash.algo = ima_hash_algo;
		result = ima_calc_file_hash(file, &iint->ima_hash);
M
Mimi Zohar 已提交
154
		if (!result) {
155
			iint->version = i_version;
M
Mimi Zohar 已提交
156 157
			iint->flags |= IMA_COLLECTED;
		}
158
	}
M
Mimi Zohar 已提交
159 160 161 162
	if (result)
		integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
				    filename, "collect_data", "failed",
				    result, 0);
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
	return result;
}

/*
 * ima_store_measurement - store file measurement
 *
 * Create an "ima" template and then store the template by calling
 * ima_store_template.
 *
 * We only get here if the inode has not already been measured,
 * but the measurement could already exist:
 * 	- multiple copies of the same file on either the same or
 *	  different filesystems.
 *	- the inode was previously flushed as well as the iint info,
 *	  containing the hashing info.
 *
 * Must be called with iint->mutex held.
 */
181 182
void ima_store_measurement(struct integrity_iint_cache *iint,
			   struct file *file, const unsigned char *filename)
183 184 185 186
{
	const char *op = "add_template_measure";
	const char *audit_cause = "ENOMEM";
	int result = -ENOMEM;
A
Al Viro 已提交
187
	struct inode *inode = file_inode(file);
188 189 190
	struct ima_template_entry *entry;
	int violation = 0;

M
Mimi Zohar 已提交
191 192 193
	if (iint->flags & IMA_MEASURED)
		return;

194 195 196 197 198 199 200
	entry = kmalloc(sizeof(*entry), GFP_KERNEL);
	if (!entry) {
		integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
				    op, audit_cause, result, 0);
		return;
	}
	memset(&entry->template, 0, sizeof(entry->template));
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
	if (iint->ima_hash.algo != ima_hash_algo) {
		struct ima_digest_data hash;

		hash.algo = ima_hash_algo;
		result = ima_calc_file_hash(file, &hash);
		if (result)
			integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
					    filename, "collect_data", "failed",
					    result, 0);
		else
			memcpy(entry->template.digest, hash.digest,
			       hash.length);
	} else
		memcpy(entry->template.digest, iint->ima_hash.digest,
		       iint->ima_hash.length);
216 217 218
	strcpy(entry->template.file_name,
	       (strlen(filename) > IMA_EVENT_NAME_LEN_MAX) ?
	       file->f_dentry->d_name.name : filename);
219 220

	result = ima_store_template(entry, violation, inode);
221
	if (!result || result == -EEXIST)
222
		iint->flags |= IMA_MEASURED;
223
	if (result < 0)
224 225
		kfree(entry);
}
P
Peter Moody 已提交
226 227 228 229 230

void ima_audit_measurement(struct integrity_iint_cache *iint,
			   const unsigned char *filename)
{
	struct audit_buffer *ab;
231
	char hash[(iint->ima_hash.length * 2) + 1];
P
Peter Moody 已提交
232 233 234 235 236
	int i;

	if (iint->flags & IMA_AUDITED)
		return;

237 238
	for (i = 0; i < iint->ima_hash.length; i++)
		hex_byte_pack(hash + (i * 2), iint->ima_hash.digest[i]);
P
Peter Moody 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
	hash[i * 2] = '\0';

	ab = audit_log_start(current->audit_context, GFP_KERNEL,
			     AUDIT_INTEGRITY_RULE);
	if (!ab)
		return;

	audit_log_format(ab, "file=");
	audit_log_untrustedstring(ab, filename);
	audit_log_format(ab, " hash=");
	audit_log_untrustedstring(ab, hash);

	audit_log_task_info(ab, current);
	audit_log_end(ab);

	iint->flags |= IMA_AUDITED;
}
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

const char *ima_d_path(struct path *path, char **pathbuf)
{
	char *pathname = NULL;

	/* We will allow 11 spaces for ' (deleted)' to be appended */
	*pathbuf = kmalloc(PATH_MAX + 11, GFP_KERNEL);
	if (*pathbuf) {
		pathname = d_path(path, *pathbuf, PATH_MAX + 11);
		if (IS_ERR(pathname)) {
			kfree(*pathbuf);
			*pathbuf = NULL;
			pathname = NULL;
		}
	}
	return pathname;
}