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 ima_template_entry **entry)
31 struct ima_template_desc *template_desc = ima_template_desc_current();
34 *entry = kzalloc(sizeof(**entry) + template_desc->num_fields *
35 sizeof(struct ima_field_data), GFP_NOFS);
39 for (i = 0; i < template_desc->num_fields; i++) {
40 struct ima_template_field *field = template_desc->fields[i];
43 result = field->field_init(iint, file, filename,
44 &((*entry)->template_data[i]));
48 len = (*entry)->template_data[i].len;
49 (*entry)->template_data_len += sizeof(len);
50 (*entry)->template_data_len += len;
52 (*entry)->template_desc = template_desc;
61 * ima_store_template - store ima template measurements
63 * Calculate the hash of a template entry, add the template entry
64 * to an ordered list of measurement entries maintained inside the kernel,
65 * and also update the aggregate integrity value (maintained inside the
66 * configured TPM PCR) over the hashes of the current list of measurement
69 * Applications retrieve the current kernel-held measurement list through
70 * the securityfs entries in /sys/kernel/security/ima. The signed aggregate
71 * TPM PCR (called quote) can be retrieved using a TPM user space library
72 * and is used to validate the measurement list.
74 * Returns 0 on success, error code otherwise
76 int ima_store_template(struct ima_template_entry *entry,
77 int violation, struct inode *inode,
78 const unsigned char *filename)
80 const char *op = "add_template_measure";
81 const char *audit_cause = "hashing_error";
82 char *template_name = entry->template_desc->name;
85 struct ima_digest_data hdr;
86 char digest[TPM_DIGEST_SIZE];
90 int num_fields = entry->template_desc->num_fields;
92 /* this function uses default algo */
93 hash.hdr.algo = HASH_ALGO_SHA1;
94 result = ima_calc_field_array_hash(&entry->template_data[0],
95 num_fields, &hash.hdr);
97 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode,
99 audit_cause, result, 0);
102 memcpy(entry->digest, hash.hdr.digest, hash.hdr.length);
104 result = ima_add_template_entry(entry, violation, op, inode, filename);
109 * ima_add_violation - add violation to measurement list.
111 * Violations are flagged in the measurement list with zero hash values.
112 * By extending the PCR with 0xFF's instead of with zeroes, the PCR
113 * value is invalidated.
115 void ima_add_violation(struct file *file, const unsigned char *filename,
116 const char *op, const char *cause)
118 struct ima_template_entry *entry;
119 struct inode *inode = file->f_dentry->d_inode;
123 /* can overflow, only indicator */
124 atomic_long_inc(&ima_htable.violations);
126 result = ima_alloc_init_template(NULL, file, filename, &entry);
131 result = ima_store_template(entry, violation, inode, filename);
135 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
136 op, cause, result, 0);
140 * ima_get_action - appraise & measure decision based on policy.
141 * @inode: pointer to inode to measure
142 * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXECUTE)
143 * @function: calling function (FILE_CHECK, BPRM_CHECK, MMAP_CHECK, MODULE_CHECK)
145 * The policy is defined in terms of keypairs:
146 * subj=, obj=, type=, func=, mask=, fsmagic=
147 * subj,obj, and type: are LSM specific.
148 * func: FILE_CHECK | BPRM_CHECK | MMAP_CHECK | MODULE_CHECK
149 * mask: contains the permission mask
152 * Returns IMA_MEASURE, IMA_APPRAISE mask.
155 int ima_get_action(struct inode *inode, int mask, int function)
157 int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE;
160 flags &= ~IMA_APPRAISE;
162 return ima_match_policy(inode, function, mask, flags);
165 int ima_must_measure(struct inode *inode, int mask, int function)
167 return ima_match_policy(inode, function, mask, IMA_MEASURE);
171 * ima_collect_measurement - collect file measurement
173 * Calculate the file hash, if it doesn't already exist,
174 * storing the measurement and i_version in the iint.
176 * Must be called with iint->mutex held.
178 * Return 0 on success, error code otherwise
180 int ima_collect_measurement(struct integrity_iint_cache *iint,
182 struct evm_ima_xattr_data **xattr_value,
185 struct inode *inode = file_inode(file);
186 const char *filename = file->f_dentry->d_name.name;
189 struct ima_digest_data hdr;
190 char digest[IMA_MAX_DIGEST_SIZE];
194 *xattr_len = ima_read_xattr(file->f_dentry, xattr_value);
196 if (!(iint->flags & IMA_COLLECTED)) {
197 u64 i_version = file_inode(file)->i_version;
199 /* use default hash algorithm */
200 hash.hdr.algo = ima_hash_algo;
203 ima_get_hash_algo(*xattr_value, *xattr_len, &hash.hdr);
205 result = ima_calc_file_hash(file, &hash.hdr);
207 int length = sizeof(hash.hdr) + hash.hdr.length;
208 void *tmpbuf = krealloc(iint->ima_hash, length,
211 iint->ima_hash = tmpbuf;
212 memcpy(iint->ima_hash, &hash, length);
213 iint->version = i_version;
214 iint->flags |= IMA_COLLECTED;
220 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
221 filename, "collect_data", "failed",
227 * ima_store_measurement - store file measurement
229 * Create an "ima" template and then store the template by calling
230 * ima_store_template.
232 * We only get here if the inode has not already been measured,
233 * but the measurement could already exist:
234 * - multiple copies of the same file on either the same or
235 * different filesystems.
236 * - the inode was previously flushed as well as the iint info,
237 * containing the hashing info.
239 * Must be called with iint->mutex held.
241 void ima_store_measurement(struct integrity_iint_cache *iint,
242 struct file *file, const unsigned char *filename)
244 const char *op = "add_template_measure";
245 const char *audit_cause = "ENOMEM";
246 int result = -ENOMEM;
247 struct inode *inode = file_inode(file);
248 struct ima_template_entry *entry;
251 if (iint->flags & IMA_MEASURED)
254 result = ima_alloc_init_template(iint, file, filename, &entry);
256 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
257 op, audit_cause, result, 0);
261 result = ima_store_template(entry, violation, inode, filename);
262 if (!result || result == -EEXIST)
263 iint->flags |= IMA_MEASURED;
268 void ima_audit_measurement(struct integrity_iint_cache *iint,
269 const unsigned char *filename)
271 struct audit_buffer *ab;
272 char hash[(iint->ima_hash->length * 2) + 1];
275 if (iint->flags & IMA_AUDITED)
278 for (i = 0; i < iint->ima_hash->length; i++)
279 hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]);
282 ab = audit_log_start(current->audit_context, GFP_KERNEL,
283 AUDIT_INTEGRITY_RULE);
287 audit_log_format(ab, "file=");
288 audit_log_untrustedstring(ab, filename);
289 audit_log_format(ab, " hash=");
290 audit_log_untrustedstring(ab, hash);
292 audit_log_task_info(ab, current);
295 iint->flags |= IMA_AUDITED;
298 const char *ima_d_path(struct path *path, char **pathbuf)
300 char *pathname = NULL;
302 /* We will allow 11 spaces for ' (deleted)' to be appended */
303 *pathbuf = kmalloc(PATH_MAX + 11, GFP_KERNEL);
305 pathname = d_path(path, *pathbuf, PATH_MAX + 11);
306 if (IS_ERR(pathname)) {