ima: switch to new template management mechanism
[deliverable/linux.git] / security / integrity / ima / ima_api.c
CommitLineData
3323eec9
MZ
1/*
2 * Copyright (C) 2008 IBM Corporation
3 *
4 * Author: Mimi Zohar <zohar@us.ibm.com>
5 *
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.
10 *
11 * File: ima_api.c
2fe5d6de
MZ
12 * Implements must_appraise_or_measure, collect_measurement,
13 * appraise_measurement, store_measurement and store_template.
3323eec9
MZ
14 */
15#include <linux/module.h>
5a0e3ad6 16#include <linux/slab.h>
2fe5d6de
MZ
17#include <linux/file.h>
18#include <linux/fs.h>
19#include <linux/xattr.h>
20#include <linux/evm.h>
ea593993 21#include <crypto/hash_info.h>
3323eec9 22#include "ima.h"
2fe5d6de 23
7bc5f447
RS
24/*
25 * ima_alloc_init_template - create and initialize a new template entry
26 */
27int ima_alloc_init_template(struct integrity_iint_cache *iint,
28 struct file *file, const unsigned char *filename,
29 struct ima_template_entry **entry)
30{
a71dc65d
RS
31 struct ima_template_desc *template_desc = ima_template_desc_current();
32 int i, result = 0;
7bc5f447 33
a71dc65d
RS
34 *entry = kzalloc(sizeof(**entry) + template_desc->num_fields *
35 sizeof(struct ima_field_data), GFP_NOFS);
36 if (!*entry)
7bc5f447
RS
37 return -ENOMEM;
38
a71dc65d
RS
39 for (i = 0; i < template_desc->num_fields; i++) {
40 struct ima_template_field *field = template_desc->fields[i];
41 u32 len;
7bc5f447 42
a71dc65d
RS
43 result = field->field_init(iint, file, filename,
44 &((*entry)->template_data[i]));
45 if (result != 0)
46 goto out;
7bc5f447 47
a71dc65d
RS
48 len = (*entry)->template_data[i].len;
49 (*entry)->template_data_len += sizeof(len);
50 (*entry)->template_data_len += len;
51 }
52 (*entry)->template_desc = template_desc;
7bc5f447 53 return 0;
a71dc65d
RS
54out:
55 kfree(*entry);
56 *entry = NULL;
7bc5f447
RS
57 return result;
58}
59
3323eec9
MZ
60/*
61 * ima_store_template - store ima template measurements
62 *
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.
68 *
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.
73 *
74 * Returns 0 on success, error code otherwise
75 */
76int ima_store_template(struct ima_template_entry *entry,
9803d413
RS
77 int violation, struct inode *inode,
78 const unsigned char *filename)
3323eec9
MZ
79{
80 const char *op = "add_template_measure";
81 const char *audit_cause = "hashing_error";
a71dc65d 82 char *template_name = entry->template_desc->name;
3323eec9 83 int result;
a35c3fb6
DK
84 struct {
85 struct ima_digest_data hdr;
140d8022 86 char digest[TPM_DIGEST_SIZE];
a35c3fb6 87 } hash;
3323eec9 88
3323eec9 89 if (!violation) {
a71dc65d
RS
90 int num_fields = entry->template_desc->num_fields;
91
ea593993
DK
92 /* this function uses default algo */
93 hash.hdr.algo = HASH_ALGO_SHA1;
a71dc65d
RS
94 result = ima_calc_field_array_hash(&entry->template_data[0],
95 num_fields, &hash.hdr);
3323eec9
MZ
96 if (result < 0) {
97 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode,
a71dc65d 98 template_name, op,
3323eec9
MZ
99 audit_cause, result, 0);
100 return result;
101 }
a35c3fb6 102 memcpy(entry->digest, hash.hdr.digest, hash.hdr.length);
3323eec9 103 }
9803d413 104 result = ima_add_template_entry(entry, violation, op, inode, filename);
3323eec9
MZ
105 return result;
106}
107
108/*
109 * ima_add_violation - add violation to measurement list.
110 *
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.
114 */
7d802a22 115void ima_add_violation(struct file *file, const unsigned char *filename,
3323eec9
MZ
116 const char *op, const char *cause)
117{
118 struct ima_template_entry *entry;
7d802a22 119 struct inode *inode = file->f_dentry->d_inode;
3323eec9
MZ
120 int violation = 1;
121 int result;
122
123 /* can overflow, only indicator */
124 atomic_long_inc(&ima_htable.violations);
125
7bc5f447
RS
126 result = ima_alloc_init_template(NULL, file, filename, &entry);
127 if (result < 0) {
3323eec9
MZ
128 result = -ENOMEM;
129 goto err_out;
130 }
9803d413 131 result = ima_store_template(entry, violation, inode, filename);
3323eec9
MZ
132 if (result < 0)
133 kfree(entry);
134err_out:
135 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
136 op, cause, result, 0);
137}
138
139/**
d9d300cd 140 * ima_get_action - appraise & measure decision based on policy.
3323eec9
MZ
141 * @inode: pointer to inode to measure
142 * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXECUTE)
16cac49f 143 * @function: calling function (FILE_CHECK, BPRM_CHECK, MMAP_CHECK, MODULE_CHECK)
3323eec9
MZ
144 *
145 * The policy is defined in terms of keypairs:
146 * subj=, obj=, type=, func=, mask=, fsmagic=
147 * subj,obj, and type: are LSM specific.
16cac49f 148 * func: FILE_CHECK | BPRM_CHECK | MMAP_CHECK | MODULE_CHECK
3323eec9
MZ
149 * mask: contains the permission mask
150 * fsmagic: hex value
151 *
2fe5d6de
MZ
152 * Returns IMA_MEASURE, IMA_APPRAISE mask.
153 *
154 */
d9d300cd 155int ima_get_action(struct inode *inode, int mask, int function)
3323eec9 156{
e7c568e0 157 int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE;
2fe5d6de
MZ
158
159 if (!ima_appraise)
160 flags &= ~IMA_APPRAISE;
161
162 return ima_match_policy(inode, function, mask, flags);
163}
3323eec9 164
2fe5d6de
MZ
165int ima_must_measure(struct inode *inode, int mask, int function)
166{
167 return ima_match_policy(inode, function, mask, IMA_MEASURE);
3323eec9
MZ
168}
169
170/*
171 * ima_collect_measurement - collect file measurement
172 *
173 * Calculate the file hash, if it doesn't already exist,
174 * storing the measurement and i_version in the iint.
175 *
176 * Must be called with iint->mutex held.
177 *
178 * Return 0 on success, error code otherwise
179 */
f381c272 180int ima_collect_measurement(struct integrity_iint_cache *iint,
d3634d0f
DK
181 struct file *file,
182 struct evm_ima_xattr_data **xattr_value,
183 int *xattr_len)
3323eec9 184{
496ad9aa 185 struct inode *inode = file_inode(file);
2fe5d6de
MZ
186 const char *filename = file->f_dentry->d_name.name;
187 int result = 0;
a35c3fb6
DK
188 struct {
189 struct ima_digest_data hdr;
190 char digest[IMA_MAX_DIGEST_SIZE];
191 } hash;
3323eec9 192
d3634d0f
DK
193 if (xattr_value)
194 *xattr_len = ima_read_xattr(file->f_dentry, xattr_value);
195
2fe5d6de 196 if (!(iint->flags & IMA_COLLECTED)) {
496ad9aa 197 u64 i_version = file_inode(file)->i_version;
3323eec9 198
c7c8bb23 199 /* use default hash algorithm */
a35c3fb6 200 hash.hdr.algo = ima_hash_algo;
d3634d0f
DK
201
202 if (xattr_value)
a35c3fb6 203 ima_get_hash_algo(*xattr_value, *xattr_len, &hash.hdr);
d3634d0f 204
a35c3fb6 205 result = ima_calc_file_hash(file, &hash.hdr);
2fe5d6de 206 if (!result) {
a35c3fb6
DK
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;
2fe5d6de 217 }
3323eec9 218 }
2fe5d6de
MZ
219 if (result)
220 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
221 filename, "collect_data", "failed",
222 result, 0);
3323eec9
MZ
223 return result;
224}
225
226/*
227 * ima_store_measurement - store file measurement
228 *
229 * Create an "ima" template and then store the template by calling
230 * ima_store_template.
231 *
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.
238 *
239 * Must be called with iint->mutex held.
240 */
f381c272
MZ
241void ima_store_measurement(struct integrity_iint_cache *iint,
242 struct file *file, const unsigned char *filename)
3323eec9
MZ
243{
244 const char *op = "add_template_measure";
245 const char *audit_cause = "ENOMEM";
246 int result = -ENOMEM;
496ad9aa 247 struct inode *inode = file_inode(file);
3323eec9
MZ
248 struct ima_template_entry *entry;
249 int violation = 0;
250
2fe5d6de
MZ
251 if (iint->flags & IMA_MEASURED)
252 return;
253
7bc5f447
RS
254 result = ima_alloc_init_template(iint, file, filename, &entry);
255 if (result < 0) {
3323eec9
MZ
256 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
257 op, audit_cause, result, 0);
258 return;
259 }
3323eec9 260
9803d413 261 result = ima_store_template(entry, violation, inode, filename);
45fae749 262 if (!result || result == -EEXIST)
3323eec9 263 iint->flags |= IMA_MEASURED;
45fae749 264 if (result < 0)
3323eec9
MZ
265 kfree(entry);
266}
e7c568e0
PM
267
268void ima_audit_measurement(struct integrity_iint_cache *iint,
269 const unsigned char *filename)
270{
271 struct audit_buffer *ab;
a35c3fb6 272 char hash[(iint->ima_hash->length * 2) + 1];
e7c568e0
PM
273 int i;
274
275 if (iint->flags & IMA_AUDITED)
276 return;
277
a35c3fb6
DK
278 for (i = 0; i < iint->ima_hash->length; i++)
279 hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]);
e7c568e0
PM
280 hash[i * 2] = '\0';
281
282 ab = audit_log_start(current->audit_context, GFP_KERNEL,
283 AUDIT_INTEGRITY_RULE);
284 if (!ab)
285 return;
286
287 audit_log_format(ab, "file=");
288 audit_log_untrustedstring(ab, filename);
289 audit_log_format(ab, " hash=");
290 audit_log_untrustedstring(ab, hash);
291
292 audit_log_task_info(ab, current);
293 audit_log_end(ab);
294
295 iint->flags |= IMA_AUDITED;
296}
ea1046d4
DK
297
298const char *ima_d_path(struct path *path, char **pathbuf)
299{
300 char *pathname = NULL;
301
302 /* We will allow 11 spaces for ' (deleted)' to be appended */
303 *pathbuf = kmalloc(PATH_MAX + 11, GFP_KERNEL);
304 if (*pathbuf) {
305 pathname = d_path(path, *pathbuf, PATH_MAX + 11);
306 if (IS_ERR(pathname)) {
307 kfree(*pathbuf);
308 *pathbuf = NULL;
309 pathname = NULL;
310 }
311 }
312 return pathname;
313}
This page took 0.266055 seconds and 5 git commands to generate.