2 * Copyright (C) 2008 IBM Corporation
4 * Author: Mimi Zohar <zohar@us.ibm.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, version 2 of the
12 * Implements must_appraise_or_measure, collect_measurement,
13 * appraise_measurement, store_measurement and store_template.
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/file.h>
19 #include <linux/xattr.h>
20 #include <linux/evm.h>
21 #include <crypto/hash_info.h>
25 * ima_alloc_init_template - create and initialize a new template entry
27 int ima_alloc_init_template(struct integrity_iint_cache *iint,
28 struct file *file, const unsigned char *filename,
29 struct evm_ima_xattr_data *xattr_value,
30 int xattr_len, struct ima_template_entry **entry)
32 struct ima_template_desc *template_desc = ima_template_desc_current();
35 *entry = kzalloc(sizeof(**entry) + template_desc->num_fields *
36 sizeof(struct ima_field_data), GFP_NOFS);
40 for (i = 0; i < template_desc->num_fields; i++) {
41 struct ima_template_field *field = template_desc->fields[i];
44 result = field->field_init(iint, file, filename,
45 xattr_value, xattr_len,
46 &((*entry)->template_data[i]));
50 len = (*entry)->template_data[i].len;
51 (*entry)->template_data_len += sizeof(len);
52 (*entry)->template_data_len += len;
54 (*entry)->template_desc = template_desc;
63 * ima_store_template - store ima template measurements
65 * Calculate the hash of a template entry, add the template entry
66 * to an ordered list of measurement entries maintained inside the kernel,
67 * and also update the aggregate integrity value (maintained inside the
68 * configured TPM PCR) over the hashes of the current list of measurement
71 * Applications retrieve the current kernel-held measurement list through
72 * the securityfs entries in /sys/kernel/security/ima. The signed aggregate
73 * TPM PCR (called quote) can be retrieved using a TPM user space library
74 * and is used to validate the measurement list.
76 * Returns 0 on success, error code otherwise
78 int ima_store_template(struct ima_template_entry *entry,
79 int violation, struct inode *inode,
80 const unsigned char *filename)
82 const char *op = "add_template_measure";
83 const char *audit_cause = "hashing_error";
84 char *template_name = entry->template_desc->name;
87 struct ima_digest_data hdr;
88 char digest[TPM_DIGEST_SIZE];
92 int num_fields = entry->template_desc->num_fields;
94 /* this function uses default algo */
95 hash.hdr.algo = HASH_ALGO_SHA1;
96 result = ima_calc_field_array_hash(&entry->template_data[0],
98 num_fields, &hash.hdr);
100 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode,
102 audit_cause, result, 0);
105 memcpy(entry->digest, hash.hdr.digest, hash.hdr.length);
107 result = ima_add_template_entry(entry, violation, op, inode, filename);
112 * ima_add_violation - add violation to measurement list.
114 * Violations are flagged in the measurement list with zero hash values.
115 * By extending the PCR with 0xFF's instead of with zeroes, the PCR
116 * value is invalidated.
118 void ima_add_violation(struct file *file, const unsigned char *filename,
119 const char *op, const char *cause)
121 struct ima_template_entry *entry;
122 struct inode *inode = file->f_dentry->d_inode;
126 /* can overflow, only indicator */
127 atomic_long_inc(&ima_htable.violations);
129 result = ima_alloc_init_template(NULL, file, filename,
135 result = ima_store_template(entry, violation, inode, filename);
139 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
140 op, cause, result, 0);
144 * ima_get_action - appraise & measure decision based on policy.
145 * @inode: pointer to inode to measure
146 * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXECUTE)
147 * @function: calling function (FILE_CHECK, BPRM_CHECK, MMAP_CHECK, MODULE_CHECK)
149 * The policy is defined in terms of keypairs:
150 * subj=, obj=, type=, func=, mask=, fsmagic=
151 * subj,obj, and type: are LSM specific.
152 * func: FILE_CHECK | BPRM_CHECK | MMAP_CHECK | MODULE_CHECK
153 * mask: contains the permission mask
156 * Returns IMA_MEASURE, IMA_APPRAISE mask.
159 int ima_get_action(struct inode *inode, int mask, int function)
161 int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE;
164 flags &= ~IMA_APPRAISE;
166 return ima_match_policy(inode, function, mask, flags);
169 int ima_must_measure(struct inode *inode, int mask, int function)
171 return ima_match_policy(inode, function, mask, IMA_MEASURE);
175 * ima_collect_measurement - collect file measurement
177 * Calculate the file hash, if it doesn't already exist,
178 * storing the measurement and i_version in the iint.
180 * Must be called with iint->mutex held.
182 * Return 0 on success, error code otherwise
184 int ima_collect_measurement(struct integrity_iint_cache *iint,
186 struct evm_ima_xattr_data **xattr_value,
189 struct inode *inode = file_inode(file);
190 const char *filename = file->f_dentry->d_name.name;
193 struct ima_digest_data hdr;
194 char digest[IMA_MAX_DIGEST_SIZE];
198 *xattr_len = ima_read_xattr(file->f_dentry, xattr_value);
200 if (!(iint->flags & IMA_COLLECTED)) {
201 u64 i_version = file_inode(file)->i_version;
203 /* use default hash algorithm */
204 hash.hdr.algo = ima_hash_algo;
207 ima_get_hash_algo(*xattr_value, *xattr_len, &hash.hdr);
209 result = ima_calc_file_hash(file, &hash.hdr);
211 int length = sizeof(hash.hdr) + hash.hdr.length;
212 void *tmpbuf = krealloc(iint->ima_hash, length,
215 iint->ima_hash = tmpbuf;
216 memcpy(iint->ima_hash, &hash, length);
217 iint->version = i_version;
218 iint->flags |= IMA_COLLECTED;
224 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
225 filename, "collect_data", "failed",
231 * ima_store_measurement - store file measurement
233 * Create an "ima" template and then store the template by calling
234 * ima_store_template.
236 * We only get here if the inode has not already been measured,
237 * but the measurement could already exist:
238 * - multiple copies of the same file on either the same or
239 * different filesystems.
240 * - the inode was previously flushed as well as the iint info,
241 * containing the hashing info.
243 * Must be called with iint->mutex held.
245 void ima_store_measurement(struct integrity_iint_cache *iint,
246 struct file *file, const unsigned char *filename,
247 struct evm_ima_xattr_data *xattr_value,
250 const char *op = "add_template_measure";
251 const char *audit_cause = "ENOMEM";
252 int result = -ENOMEM;
253 struct inode *inode = file_inode(file);
254 struct ima_template_entry *entry;
257 if (iint->flags & IMA_MEASURED)
260 result = ima_alloc_init_template(iint, file, filename,
261 xattr_value, xattr_len, &entry);
263 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
264 op, audit_cause, result, 0);
268 result = ima_store_template(entry, violation, inode, filename);
269 if (!result || result == -EEXIST)
270 iint->flags |= IMA_MEASURED;
275 void ima_audit_measurement(struct integrity_iint_cache *iint,
276 const unsigned char *filename)
278 struct audit_buffer *ab;
279 char hash[(iint->ima_hash->length * 2) + 1];
280 const char *algo_name = hash_algo_name[iint->ima_hash->algo];
281 char algo_hash[sizeof(hash) + strlen(algo_name) + 2];
284 if (iint->flags & IMA_AUDITED)
287 for (i = 0; i < iint->ima_hash->length; i++)
288 hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]);
291 ab = audit_log_start(current->audit_context, GFP_KERNEL,
292 AUDIT_INTEGRITY_RULE);
296 audit_log_format(ab, "file=");
297 audit_log_untrustedstring(ab, filename);
298 audit_log_format(ab, " hash=");
299 snprintf(algo_hash, sizeof(algo_hash), "%s:%s", algo_name, hash);
300 audit_log_untrustedstring(ab, algo_hash);
302 audit_log_task_info(ab, current);
305 iint->flags |= IMA_AUDITED;
308 const char *ima_d_path(struct path *path, char **pathbuf)
310 char *pathname = NULL;
312 /* We will allow 11 spaces for ' (deleted)' to be appended */
313 *pathbuf = kmalloc(PATH_MAX + 11, GFP_KERNEL);
315 pathname = d_path(path, *pathbuf, PATH_MAX + 11);
316 if (IS_ERR(pathname)) {