blob: 5865ea2a2777fe6d96dde77364bc56cc7c54f61f [file] [log] [blame]
Mimi Zohar3323eec2009-02-04 09:06:58 -05001/*
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
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050012 * Implements must_appraise_or_measure, collect_measurement,
13 * appraise_measurement, store_measurement and store_template.
Mimi Zohar3323eec2009-02-04 09:06:58 -050014 */
15#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050017#include <linux/file.h>
18#include <linux/fs.h>
19#include <linux/xattr.h>
20#include <linux/evm.h>
Dmitry Kasatkinea593992013-06-07 12:16:24 +020021#include <crypto/hash_info.h>
Mimi Zohar3323eec2009-02-04 09:06:58 -050022#include "ima.h"
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050023
Mimi Zohar3323eec2009-02-04 09:06:58 -050024/*
Roberto Sassua7ed7c62013-12-02 19:40:34 +010025 * ima_free_template_entry - free an existing template entry
26 */
27void ima_free_template_entry(struct ima_template_entry *entry)
28{
29 int i;
30
31 for (i = 0; i < entry->template_desc->num_fields; i++)
32 kfree(entry->template_data[i].data);
33
34 kfree(entry);
35}
36
37/*
Roberto Sassu7bc5f442013-06-07 12:16:28 +020038 * ima_alloc_init_template - create and initialize a new template entry
39 */
Roberto Sassu23b57412015-04-11 17:09:50 +020040int ima_alloc_init_template(struct ima_event_data *event_data,
41 struct ima_template_entry **entry)
Roberto Sassu7bc5f442013-06-07 12:16:28 +020042{
Roberto Sassua71dc652013-06-07 12:16:33 +020043 struct ima_template_desc *template_desc = ima_template_desc_current();
44 int i, result = 0;
Roberto Sassu7bc5f442013-06-07 12:16:28 +020045
Roberto Sassua71dc652013-06-07 12:16:33 +020046 *entry = kzalloc(sizeof(**entry) + template_desc->num_fields *
47 sizeof(struct ima_field_data), GFP_NOFS);
48 if (!*entry)
Roberto Sassu7bc5f442013-06-07 12:16:28 +020049 return -ENOMEM;
50
Roberto Sassua7ed7c62013-12-02 19:40:34 +010051 (*entry)->template_desc = template_desc;
Roberto Sassua71dc652013-06-07 12:16:33 +020052 for (i = 0; i < template_desc->num_fields; i++) {
53 struct ima_template_field *field = template_desc->fields[i];
54 u32 len;
Roberto Sassu7bc5f442013-06-07 12:16:28 +020055
Roberto Sassu23b57412015-04-11 17:09:50 +020056 result = field->field_init(event_data,
Roberto Sassua71dc652013-06-07 12:16:33 +020057 &((*entry)->template_data[i]));
58 if (result != 0)
59 goto out;
Roberto Sassu7bc5f442013-06-07 12:16:28 +020060
Roberto Sassua71dc652013-06-07 12:16:33 +020061 len = (*entry)->template_data[i].len;
62 (*entry)->template_data_len += sizeof(len);
63 (*entry)->template_data_len += len;
64 }
Roberto Sassu7bc5f442013-06-07 12:16:28 +020065 return 0;
Roberto Sassua71dc652013-06-07 12:16:33 +020066out:
Roberto Sassua7ed7c62013-12-02 19:40:34 +010067 ima_free_template_entry(*entry);
Roberto Sassua71dc652013-06-07 12:16:33 +020068 *entry = NULL;
Roberto Sassu7bc5f442013-06-07 12:16:28 +020069 return result;
70}
71
72/*
Mimi Zohar3323eec2009-02-04 09:06:58 -050073 * ima_store_template - store ima template measurements
74 *
75 * Calculate the hash of a template entry, add the template entry
76 * to an ordered list of measurement entries maintained inside the kernel,
77 * and also update the aggregate integrity value (maintained inside the
78 * configured TPM PCR) over the hashes of the current list of measurement
79 * entries.
80 *
81 * Applications retrieve the current kernel-held measurement list through
82 * the securityfs entries in /sys/kernel/security/ima. The signed aggregate
83 * TPM PCR (called quote) can be retrieved using a TPM user space library
84 * and is used to validate the measurement list.
85 *
86 * Returns 0 on success, error code otherwise
87 */
88int ima_store_template(struct ima_template_entry *entry,
Roberto Sassu9803d412013-06-07 12:16:27 +020089 int violation, struct inode *inode,
90 const unsigned char *filename)
Mimi Zohar3323eec2009-02-04 09:06:58 -050091{
Mimi Zohar52a13282013-12-11 14:44:04 -050092 static const char op[] = "add_template_measure";
93 static const char audit_cause[] = "hashing_error";
Roberto Sassua71dc652013-06-07 12:16:33 +020094 char *template_name = entry->template_desc->name;
Mimi Zohar3323eec2009-02-04 09:06:58 -050095 int result;
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +030096 struct {
97 struct ima_digest_data hdr;
Mimi Zohar140d8022013-03-11 20:29:47 -040098 char digest[TPM_DIGEST_SIZE];
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +030099 } hash;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500100
Mimi Zohar3323eec2009-02-04 09:06:58 -0500101 if (!violation) {
Roberto Sassua71dc652013-06-07 12:16:33 +0200102 int num_fields = entry->template_desc->num_fields;
103
Dmitry Kasatkinea593992013-06-07 12:16:24 +0200104 /* this function uses default algo */
105 hash.hdr.algo = HASH_ALGO_SHA1;
Roberto Sassua71dc652013-06-07 12:16:33 +0200106 result = ima_calc_field_array_hash(&entry->template_data[0],
Roberto Sassub6f8f162013-11-08 19:21:39 +0100107 entry->template_desc,
Roberto Sassua71dc652013-06-07 12:16:33 +0200108 num_fields, &hash.hdr);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500109 if (result < 0) {
110 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode,
Roberto Sassua71dc652013-06-07 12:16:33 +0200111 template_name, op,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500112 audit_cause, result, 0);
113 return result;
114 }
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300115 memcpy(entry->digest, hash.hdr.digest, hash.hdr.length);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500116 }
Roberto Sassu9803d412013-06-07 12:16:27 +0200117 result = ima_add_template_entry(entry, violation, op, inode, filename);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500118 return result;
119}
120
121/*
122 * ima_add_violation - add violation to measurement list.
123 *
124 * Violations are flagged in the measurement list with zero hash values.
125 * By extending the PCR with 0xFF's instead of with zeroes, the PCR
126 * value is invalidated.
127 */
Roberto Sassu7d802a22013-06-07 12:16:26 +0200128void ima_add_violation(struct file *file, const unsigned char *filename,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500129 const char *op, const char *cause)
130{
131 struct ima_template_entry *entry;
Libo Chen31d4b762013-12-11 14:11:28 +0800132 struct inode *inode = file_inode(file);
Roberto Sassu23b57412015-04-11 17:09:50 +0200133 struct ima_event_data event_data = {NULL, file, filename, NULL, 0};
Mimi Zohar3323eec2009-02-04 09:06:58 -0500134 int violation = 1;
135 int result;
136
137 /* can overflow, only indicator */
138 atomic_long_inc(&ima_htable.violations);
139
Roberto Sassu23b57412015-04-11 17:09:50 +0200140 result = ima_alloc_init_template(&event_data, &entry);
Roberto Sassu7bc5f442013-06-07 12:16:28 +0200141 if (result < 0) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500142 result = -ENOMEM;
143 goto err_out;
144 }
Roberto Sassu9803d412013-06-07 12:16:27 +0200145 result = ima_store_template(entry, violation, inode, filename);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500146 if (result < 0)
Roberto Sassua7ed7c62013-12-02 19:40:34 +0100147 ima_free_template_entry(entry);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500148err_out:
149 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
150 op, cause, result, 0);
151}
152
153/**
Dmitry Kasatkind9d300c2012-06-27 11:26:14 +0300154 * ima_get_action - appraise & measure decision based on policy.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500155 * @inode: pointer to inode to measure
156 * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXECUTE)
Mimi Zohar16cac492012-12-13 11:15:04 -0500157 * @function: calling function (FILE_CHECK, BPRM_CHECK, MMAP_CHECK, MODULE_CHECK)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500158 *
159 * The policy is defined in terms of keypairs:
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200160 * subj=, obj=, type=, func=, mask=, fsmagic=
Mimi Zohar3323eec2009-02-04 09:06:58 -0500161 * subj,obj, and type: are LSM specific.
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200162 * func: FILE_CHECK | BPRM_CHECK | MMAP_CHECK | MODULE_CHECK
163 * mask: contains the permission mask
Mimi Zohar3323eec2009-02-04 09:06:58 -0500164 * fsmagic: hex value
165 *
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500166 * Returns IMA_MEASURE, IMA_APPRAISE mask.
167 *
168 */
Dmitry Kasatkind9d300c2012-06-27 11:26:14 +0300169int ima_get_action(struct inode *inode, int mask, int function)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500170{
Peter Moodye7c568e2012-06-14 10:04:36 -0700171 int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500172
Dmitry Kasatkinfd5f4e902014-11-05 17:01:14 +0200173 flags &= ima_policy_flag;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500174
175 return ima_match_policy(inode, function, mask, flags);
176}
177
Mimi Zohar3323eec2009-02-04 09:06:58 -0500178/*
179 * ima_collect_measurement - collect file measurement
180 *
181 * Calculate the file hash, if it doesn't already exist,
182 * storing the measurement and i_version in the iint.
183 *
184 * Must be called with iint->mutex held.
185 *
186 * Return 0 on success, error code otherwise
187 */
Mimi Zoharf381c272011-03-09 14:13:22 -0500188int ima_collect_measurement(struct integrity_iint_cache *iint,
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300189 struct file *file,
190 struct evm_ima_xattr_data **xattr_value,
191 int *xattr_len)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500192{
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400193 const char *audit_cause = "failed";
Al Viro496ad9a2013-01-23 17:07:38 -0500194 struct inode *inode = file_inode(file);
Al Virob5830432014-10-31 01:22:04 -0400195 const char *filename = file->f_path.dentry->d_name.name;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500196 int result = 0;
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300197 struct {
198 struct ima_digest_data hdr;
199 char digest[IMA_MAX_DIGEST_SIZE];
200 } hash;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500201
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300202 if (xattr_value)
Al Virob5830432014-10-31 01:22:04 -0400203 *xattr_len = ima_read_xattr(file->f_path.dentry, xattr_value);
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300204
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500205 if (!(iint->flags & IMA_COLLECTED)) {
Al Viro496ad9a2013-01-23 17:07:38 -0500206 u64 i_version = file_inode(file)->i_version;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500207
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400208 if (file->f_flags & O_DIRECT) {
209 audit_cause = "failed(directio)";
210 result = -EACCES;
211 goto out;
212 }
213
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300214 /* use default hash algorithm */
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300215 hash.hdr.algo = ima_hash_algo;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300216
217 if (xattr_value)
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300218 ima_get_hash_algo(*xattr_value, *xattr_len, &hash.hdr);
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300219
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300220 result = ima_calc_file_hash(file, &hash.hdr);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500221 if (!result) {
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300222 int length = sizeof(hash.hdr) + hash.hdr.length;
223 void *tmpbuf = krealloc(iint->ima_hash, length,
224 GFP_NOFS);
225 if (tmpbuf) {
226 iint->ima_hash = tmpbuf;
227 memcpy(iint->ima_hash, &hash, length);
228 iint->version = i_version;
229 iint->flags |= IMA_COLLECTED;
230 } else
231 result = -ENOMEM;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500232 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500233 }
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400234out:
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500235 if (result)
236 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400237 filename, "collect_data", audit_cause,
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500238 result, 0);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500239 return result;
240}
241
242/*
243 * ima_store_measurement - store file measurement
244 *
245 * Create an "ima" template and then store the template by calling
246 * ima_store_template.
247 *
248 * We only get here if the inode has not already been measured,
249 * but the measurement could already exist:
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200250 * - multiple copies of the same file on either the same or
Mimi Zohar3323eec2009-02-04 09:06:58 -0500251 * different filesystems.
252 * - the inode was previously flushed as well as the iint info,
253 * containing the hashing info.
254 *
255 * Must be called with iint->mutex held.
256 */
Mimi Zoharf381c272011-03-09 14:13:22 -0500257void ima_store_measurement(struct integrity_iint_cache *iint,
Mimi Zoharbcbc9b02013-07-23 11:15:00 -0400258 struct file *file, const unsigned char *filename,
259 struct evm_ima_xattr_data *xattr_value,
260 int xattr_len)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500261{
Mimi Zohar52a13282013-12-11 14:44:04 -0500262 static const char op[] = "add_template_measure";
263 static const char audit_cause[] = "ENOMEM";
Mimi Zohar3323eec2009-02-04 09:06:58 -0500264 int result = -ENOMEM;
Al Viro496ad9a2013-01-23 17:07:38 -0500265 struct inode *inode = file_inode(file);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500266 struct ima_template_entry *entry;
Roberto Sassu23b57412015-04-11 17:09:50 +0200267 struct ima_event_data event_data = {iint, file, filename,
268 xattr_value, xattr_len};
Mimi Zohar3323eec2009-02-04 09:06:58 -0500269 int violation = 0;
270
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500271 if (iint->flags & IMA_MEASURED)
272 return;
273
Roberto Sassu23b57412015-04-11 17:09:50 +0200274 result = ima_alloc_init_template(&event_data, &entry);
Roberto Sassu7bc5f442013-06-07 12:16:28 +0200275 if (result < 0) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500276 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
277 op, audit_cause, result, 0);
278 return;
279 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500280
Roberto Sassu9803d412013-06-07 12:16:27 +0200281 result = ima_store_template(entry, violation, inode, filename);
Roberto Sassu45fae742011-12-19 15:57:27 +0100282 if (!result || result == -EEXIST)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500283 iint->flags |= IMA_MEASURED;
Roberto Sassu45fae742011-12-19 15:57:27 +0100284 if (result < 0)
Roberto Sassua7ed7c62013-12-02 19:40:34 +0100285 ima_free_template_entry(entry);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500286}
Peter Moodye7c568e2012-06-14 10:04:36 -0700287
288void ima_audit_measurement(struct integrity_iint_cache *iint,
289 const unsigned char *filename)
290{
291 struct audit_buffer *ab;
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300292 char hash[(iint->ima_hash->length * 2) + 1];
Mimi Zohar5278aa52013-06-07 12:16:38 +0200293 const char *algo_name = hash_algo_name[iint->ima_hash->algo];
294 char algo_hash[sizeof(hash) + strlen(algo_name) + 2];
Peter Moodye7c568e2012-06-14 10:04:36 -0700295 int i;
296
297 if (iint->flags & IMA_AUDITED)
298 return;
299
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300300 for (i = 0; i < iint->ima_hash->length; i++)
301 hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]);
Peter Moodye7c568e2012-06-14 10:04:36 -0700302 hash[i * 2] = '\0';
303
304 ab = audit_log_start(current->audit_context, GFP_KERNEL,
305 AUDIT_INTEGRITY_RULE);
306 if (!ab)
307 return;
308
309 audit_log_format(ab, "file=");
310 audit_log_untrustedstring(ab, filename);
311 audit_log_format(ab, " hash=");
Mimi Zohar5278aa52013-06-07 12:16:38 +0200312 snprintf(algo_hash, sizeof(algo_hash), "%s:%s", algo_name, hash);
313 audit_log_untrustedstring(ab, algo_hash);
Peter Moodye7c568e2012-06-14 10:04:36 -0700314
315 audit_log_task_info(ab, current);
316 audit_log_end(ab);
317
318 iint->flags |= IMA_AUDITED;
319}
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300320
321const char *ima_d_path(struct path *path, char **pathbuf)
322{
323 char *pathname = NULL;
324
Dmitry Kasatkin456f5fd2014-10-01 21:43:10 +0300325 *pathbuf = __getname();
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300326 if (*pathbuf) {
Dmitry Kasatkin17f4bad2014-08-19 16:48:39 +0300327 pathname = d_absolute_path(path, *pathbuf, PATH_MAX);
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300328 if (IS_ERR(pathname)) {
Dmitry Kasatkin456f5fd2014-10-01 21:43:10 +0300329 __putname(*pathbuf);
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300330 *pathbuf = NULL;
331 pathname = NULL;
332 }
333 }
Dmitry Kasatkin61997c42013-11-13 22:23:20 +0200334 return pathname ?: (const char *)path->dentry->d_name.name;
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300335}