ima: pass the filename argument up to ima_add_template_entry()
[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
523979ad 24static const char *IMA_TEMPLATE_NAME = "ima";
3323eec9
MZ
25
26/*
27 * ima_store_template - store ima template measurements
28 *
29 * Calculate the hash of a template entry, add the template entry
30 * to an ordered list of measurement entries maintained inside the kernel,
31 * and also update the aggregate integrity value (maintained inside the
32 * configured TPM PCR) over the hashes of the current list of measurement
33 * entries.
34 *
35 * Applications retrieve the current kernel-held measurement list through
36 * the securityfs entries in /sys/kernel/security/ima. The signed aggregate
37 * TPM PCR (called quote) can be retrieved using a TPM user space library
38 * and is used to validate the measurement list.
39 *
40 * Returns 0 on success, error code otherwise
41 */
42int ima_store_template(struct ima_template_entry *entry,
9803d413
RS
43 int violation, struct inode *inode,
44 const unsigned char *filename)
3323eec9
MZ
45{
46 const char *op = "add_template_measure";
47 const char *audit_cause = "hashing_error";
48 int result;
a35c3fb6
DK
49 struct {
50 struct ima_digest_data hdr;
140d8022 51 char digest[TPM_DIGEST_SIZE];
a35c3fb6 52 } hash;
3323eec9
MZ
53
54 memset(entry->digest, 0, sizeof(entry->digest));
55 entry->template_name = IMA_TEMPLATE_NAME;
56 entry->template_len = sizeof(entry->template);
57
58 if (!violation) {
ea593993
DK
59 /* this function uses default algo */
60 hash.hdr.algo = HASH_ALGO_SHA1;
50af5544 61 result = ima_calc_buffer_hash(&entry->template,
a35c3fb6 62 entry->template_len, &hash.hdr);
3323eec9
MZ
63 if (result < 0) {
64 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode,
65 entry->template_name, op,
66 audit_cause, result, 0);
67 return result;
68 }
a35c3fb6 69 memcpy(entry->digest, hash.hdr.digest, hash.hdr.length);
3323eec9 70 }
9803d413 71 result = ima_add_template_entry(entry, violation, op, inode, filename);
3323eec9
MZ
72 return result;
73}
74
75/*
76 * ima_add_violation - add violation to measurement list.
77 *
78 * Violations are flagged in the measurement list with zero hash values.
79 * By extending the PCR with 0xFF's instead of with zeroes, the PCR
80 * value is invalidated.
81 */
7d802a22 82void ima_add_violation(struct file *file, const unsigned char *filename,
3323eec9
MZ
83 const char *op, const char *cause)
84{
85 struct ima_template_entry *entry;
7d802a22 86 struct inode *inode = file->f_dentry->d_inode;
3323eec9
MZ
87 int violation = 1;
88 int result;
89
90 /* can overflow, only indicator */
91 atomic_long_inc(&ima_htable.violations);
92
93 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
94 if (!entry) {
95 result = -ENOMEM;
96 goto err_out;
97 }
98 memset(&entry->template, 0, sizeof(entry->template));
99 strncpy(entry->template.file_name, filename, IMA_EVENT_NAME_LEN_MAX);
9803d413 100 result = ima_store_template(entry, violation, inode, filename);
3323eec9
MZ
101 if (result < 0)
102 kfree(entry);
103err_out:
104 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
105 op, cause, result, 0);
106}
107
108/**
d9d300cd 109 * ima_get_action - appraise & measure decision based on policy.
3323eec9
MZ
110 * @inode: pointer to inode to measure
111 * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXECUTE)
16cac49f 112 * @function: calling function (FILE_CHECK, BPRM_CHECK, MMAP_CHECK, MODULE_CHECK)
3323eec9
MZ
113 *
114 * The policy is defined in terms of keypairs:
115 * subj=, obj=, type=, func=, mask=, fsmagic=
116 * subj,obj, and type: are LSM specific.
16cac49f 117 * func: FILE_CHECK | BPRM_CHECK | MMAP_CHECK | MODULE_CHECK
3323eec9
MZ
118 * mask: contains the permission mask
119 * fsmagic: hex value
120 *
2fe5d6de
MZ
121 * Returns IMA_MEASURE, IMA_APPRAISE mask.
122 *
123 */
d9d300cd 124int ima_get_action(struct inode *inode, int mask, int function)
3323eec9 125{
e7c568e0 126 int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE;
2fe5d6de
MZ
127
128 if (!ima_appraise)
129 flags &= ~IMA_APPRAISE;
130
131 return ima_match_policy(inode, function, mask, flags);
132}
3323eec9 133
2fe5d6de
MZ
134int ima_must_measure(struct inode *inode, int mask, int function)
135{
136 return ima_match_policy(inode, function, mask, IMA_MEASURE);
3323eec9
MZ
137}
138
139/*
140 * ima_collect_measurement - collect file measurement
141 *
142 * Calculate the file hash, if it doesn't already exist,
143 * storing the measurement and i_version in the iint.
144 *
145 * Must be called with iint->mutex held.
146 *
147 * Return 0 on success, error code otherwise
148 */
f381c272 149int ima_collect_measurement(struct integrity_iint_cache *iint,
d3634d0f
DK
150 struct file *file,
151 struct evm_ima_xattr_data **xattr_value,
152 int *xattr_len)
3323eec9 153{
496ad9aa 154 struct inode *inode = file_inode(file);
2fe5d6de
MZ
155 const char *filename = file->f_dentry->d_name.name;
156 int result = 0;
a35c3fb6
DK
157 struct {
158 struct ima_digest_data hdr;
159 char digest[IMA_MAX_DIGEST_SIZE];
160 } hash;
3323eec9 161
d3634d0f
DK
162 if (xattr_value)
163 *xattr_len = ima_read_xattr(file->f_dentry, xattr_value);
164
2fe5d6de 165 if (!(iint->flags & IMA_COLLECTED)) {
496ad9aa 166 u64 i_version = file_inode(file)->i_version;
3323eec9 167
c7c8bb23 168 /* use default hash algorithm */
a35c3fb6 169 hash.hdr.algo = ima_hash_algo;
d3634d0f
DK
170
171 if (xattr_value)
a35c3fb6 172 ima_get_hash_algo(*xattr_value, *xattr_len, &hash.hdr);
d3634d0f 173
a35c3fb6 174 result = ima_calc_file_hash(file, &hash.hdr);
2fe5d6de 175 if (!result) {
a35c3fb6
DK
176 int length = sizeof(hash.hdr) + hash.hdr.length;
177 void *tmpbuf = krealloc(iint->ima_hash, length,
178 GFP_NOFS);
179 if (tmpbuf) {
180 iint->ima_hash = tmpbuf;
181 memcpy(iint->ima_hash, &hash, length);
182 iint->version = i_version;
183 iint->flags |= IMA_COLLECTED;
184 } else
185 result = -ENOMEM;
2fe5d6de 186 }
3323eec9 187 }
2fe5d6de
MZ
188 if (result)
189 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
190 filename, "collect_data", "failed",
191 result, 0);
3323eec9
MZ
192 return result;
193}
194
195/*
196 * ima_store_measurement - store file measurement
197 *
198 * Create an "ima" template and then store the template by calling
199 * ima_store_template.
200 *
201 * We only get here if the inode has not already been measured,
202 * but the measurement could already exist:
203 * - multiple copies of the same file on either the same or
204 * different filesystems.
205 * - the inode was previously flushed as well as the iint info,
206 * containing the hashing info.
207 *
208 * Must be called with iint->mutex held.
209 */
f381c272
MZ
210void ima_store_measurement(struct integrity_iint_cache *iint,
211 struct file *file, const unsigned char *filename)
3323eec9
MZ
212{
213 const char *op = "add_template_measure";
214 const char *audit_cause = "ENOMEM";
215 int result = -ENOMEM;
496ad9aa 216 struct inode *inode = file_inode(file);
3323eec9
MZ
217 struct ima_template_entry *entry;
218 int violation = 0;
219
2fe5d6de
MZ
220 if (iint->flags & IMA_MEASURED)
221 return;
222
3323eec9
MZ
223 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
224 if (!entry) {
225 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
226 op, audit_cause, result, 0);
227 return;
228 }
229 memset(&entry->template, 0, sizeof(entry->template));
a35c3fb6
DK
230 if (iint->ima_hash->algo != ima_hash_algo) {
231 struct {
232 struct ima_digest_data hdr;
233 char digest[IMA_MAX_DIGEST_SIZE];
234 } hash;
c7c8bb23 235
a35c3fb6
DK
236 hash.hdr.algo = ima_hash_algo;
237 result = ima_calc_file_hash(file, &hash.hdr);
c7c8bb23
DK
238 if (result)
239 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
240 filename, "collect_data", "failed",
241 result, 0);
242 else
a35c3fb6
DK
243 memcpy(entry->template.digest, hash.hdr.digest,
244 hash.hdr.length);
c7c8bb23 245 } else
a35c3fb6
DK
246 memcpy(entry->template.digest, iint->ima_hash->digest,
247 iint->ima_hash->length);
08e1b76a
MZ
248 strcpy(entry->template.file_name,
249 (strlen(filename) > IMA_EVENT_NAME_LEN_MAX) ?
250 file->f_dentry->d_name.name : filename);
3323eec9 251
9803d413 252 result = ima_store_template(entry, violation, inode, filename);
45fae749 253 if (!result || result == -EEXIST)
3323eec9 254 iint->flags |= IMA_MEASURED;
45fae749 255 if (result < 0)
3323eec9
MZ
256 kfree(entry);
257}
e7c568e0
PM
258
259void ima_audit_measurement(struct integrity_iint_cache *iint,
260 const unsigned char *filename)
261{
262 struct audit_buffer *ab;
a35c3fb6 263 char hash[(iint->ima_hash->length * 2) + 1];
e7c568e0
PM
264 int i;
265
266 if (iint->flags & IMA_AUDITED)
267 return;
268
a35c3fb6
DK
269 for (i = 0; i < iint->ima_hash->length; i++)
270 hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]);
e7c568e0
PM
271 hash[i * 2] = '\0';
272
273 ab = audit_log_start(current->audit_context, GFP_KERNEL,
274 AUDIT_INTEGRITY_RULE);
275 if (!ab)
276 return;
277
278 audit_log_format(ab, "file=");
279 audit_log_untrustedstring(ab, filename);
280 audit_log_format(ab, " hash=");
281 audit_log_untrustedstring(ab, hash);
282
283 audit_log_task_info(ab, current);
284 audit_log_end(ab);
285
286 iint->flags |= IMA_AUDITED;
287}
ea1046d4
DK
288
289const char *ima_d_path(struct path *path, char **pathbuf)
290{
291 char *pathname = NULL;
292
293 /* We will allow 11 spaces for ' (deleted)' to be appended */
294 *pathbuf = kmalloc(PATH_MAX + 11, GFP_KERNEL);
295 if (*pathbuf) {
296 pathname = d_path(path, *pathbuf, PATH_MAX + 11);
297 if (IS_ERR(pathname)) {
298 kfree(*pathbuf);
299 *pathbuf = NULL;
300 pathname = NULL;
301 }
302 }
303 return pathname;
304}
This page took 0.224047 seconds and 5 git commands to generate.