ima: add audit log support for larger hashes
[linux/fpc-iii.git] / security / integrity / ima / ima_api.c
blob5fcc80695d8776c69164251dfd31caa703a2c461
1 /*
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
9 * License.
11 * File: ima_api.c
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>
18 #include <linux/fs.h>
19 #include <linux/xattr.h>
20 #include <linux/evm.h>
21 #include <crypto/hash_info.h>
22 #include "ima.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();
32 int i, result = 0;
34 *entry = kzalloc(sizeof(**entry) + template_desc->num_fields *
35 sizeof(struct ima_field_data), GFP_NOFS);
36 if (!*entry)
37 return -ENOMEM;
39 for (i = 0; i < template_desc->num_fields; i++) {
40 struct ima_template_field *field = template_desc->fields[i];
41 u32 len;
43 result = field->field_init(iint, file, filename,
44 &((*entry)->template_data[i]));
45 if (result != 0)
46 goto out;
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;
53 return 0;
54 out:
55 kfree(*entry);
56 *entry = NULL;
57 return result;
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
67 * entries.
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;
83 int result;
84 struct {
85 struct ima_digest_data hdr;
86 char digest[TPM_DIGEST_SIZE];
87 } hash;
89 if (!violation) {
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);
96 if (result < 0) {
97 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode,
98 template_name, op,
99 audit_cause, result, 0);
100 return result;
102 memcpy(entry->digest, hash.hdr.digest, hash.hdr.length);
104 result = ima_add_template_entry(entry, violation, op, inode, filename);
105 return result;
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;
120 int violation = 1;
121 int result;
123 /* can overflow, only indicator */
124 atomic_long_inc(&ima_htable.violations);
126 result = ima_alloc_init_template(NULL, file, filename, &entry);
127 if (result < 0) {
128 result = -ENOMEM;
129 goto err_out;
131 result = ima_store_template(entry, violation, inode, filename);
132 if (result < 0)
133 kfree(entry);
134 err_out:
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
150 * fsmagic: hex value
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;
159 if (!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,
181 struct file *file,
182 struct evm_ima_xattr_data **xattr_value,
183 int *xattr_len)
185 struct inode *inode = file_inode(file);
186 const char *filename = file->f_dentry->d_name.name;
187 int result = 0;
188 struct {
189 struct ima_digest_data hdr;
190 char digest[IMA_MAX_DIGEST_SIZE];
191 } hash;
193 if (xattr_value)
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;
202 if (xattr_value)
203 ima_get_hash_algo(*xattr_value, *xattr_len, &hash.hdr);
205 result = ima_calc_file_hash(file, &hash.hdr);
206 if (!result) {
207 int length = sizeof(hash.hdr) + hash.hdr.length;
208 void *tmpbuf = krealloc(iint->ima_hash, length,
209 GFP_NOFS);
210 if (tmpbuf) {
211 iint->ima_hash = tmpbuf;
212 memcpy(iint->ima_hash, &hash, length);
213 iint->version = i_version;
214 iint->flags |= IMA_COLLECTED;
215 } else
216 result = -ENOMEM;
219 if (result)
220 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
221 filename, "collect_data", "failed",
222 result, 0);
223 return result;
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;
249 int violation = 0;
251 if (iint->flags & IMA_MEASURED)
252 return;
254 result = ima_alloc_init_template(iint, file, filename, &entry);
255 if (result < 0) {
256 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
257 op, audit_cause, result, 0);
258 return;
261 result = ima_store_template(entry, violation, inode, filename);
262 if (!result || result == -EEXIST)
263 iint->flags |= IMA_MEASURED;
264 if (result < 0)
265 kfree(entry);
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];
273 const char *algo_name = hash_algo_name[iint->ima_hash->algo];
274 char algo_hash[sizeof(hash) + strlen(algo_name) + 2];
275 int i;
277 if (iint->flags & IMA_AUDITED)
278 return;
280 for (i = 0; i < iint->ima_hash->length; i++)
281 hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]);
282 hash[i * 2] = '\0';
284 ab = audit_log_start(current->audit_context, GFP_KERNEL,
285 AUDIT_INTEGRITY_RULE);
286 if (!ab)
287 return;
289 audit_log_format(ab, "file=");
290 audit_log_untrustedstring(ab, filename);
291 audit_log_format(ab, " hash=");
292 snprintf(algo_hash, sizeof(algo_hash), "%s:%s", algo_name, hash);
293 audit_log_untrustedstring(ab, algo_hash);
295 audit_log_task_info(ab, current);
296 audit_log_end(ab);
298 iint->flags |= IMA_AUDITED;
301 const char *ima_d_path(struct path *path, char **pathbuf)
303 char *pathname = NULL;
305 /* We will allow 11 spaces for ' (deleted)' to be appended */
306 *pathbuf = kmalloc(PATH_MAX + 11, GFP_KERNEL);
307 if (*pathbuf) {
308 pathname = d_path(path, *pathbuf, PATH_MAX + 11);
309 if (IS_ERR(pathname)) {
310 kfree(*pathbuf);
311 *pathbuf = NULL;
312 pathname = NULL;
315 return pathname;