blob: 1d950fbb2aecbb21c62b28233dba7a707a116a6b [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,
Roberto Sassu8d94eb92015-04-11 17:12:39 +0200129 struct integrity_iint_cache *iint,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500130 const char *op, const char *cause)
131{
132 struct ima_template_entry *entry;
Libo Chen31d4b762013-12-11 14:11:28 +0800133 struct inode *inode = file_inode(file);
Roberto Sassu8d94eb92015-04-11 17:12:39 +0200134 struct ima_event_data event_data = {iint, file, filename, NULL, 0,
135 cause};
Mimi Zohar3323eec2009-02-04 09:06:58 -0500136 int violation = 1;
137 int result;
138
139 /* can overflow, only indicator */
140 atomic_long_inc(&ima_htable.violations);
141
Roberto Sassu23b57412015-04-11 17:09:50 +0200142 result = ima_alloc_init_template(&event_data, &entry);
Roberto Sassu7bc5f442013-06-07 12:16:28 +0200143 if (result < 0) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500144 result = -ENOMEM;
145 goto err_out;
146 }
Roberto Sassu9803d412013-06-07 12:16:27 +0200147 result = ima_store_template(entry, violation, inode, filename);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500148 if (result < 0)
Roberto Sassua7ed7c62013-12-02 19:40:34 +0100149 ima_free_template_entry(entry);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500150err_out:
151 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
152 op, cause, result, 0);
153}
154
155/**
Dmitry Kasatkind9d300c2012-06-27 11:26:14 +0300156 * ima_get_action - appraise & measure decision based on policy.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500157 * @inode: pointer to inode to measure
158 * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXECUTE)
Mimi Zohar16cac492012-12-13 11:15:04 -0500159 * @function: calling function (FILE_CHECK, BPRM_CHECK, MMAP_CHECK, MODULE_CHECK)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500160 *
161 * The policy is defined in terms of keypairs:
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200162 * subj=, obj=, type=, func=, mask=, fsmagic=
Mimi Zohar3323eec2009-02-04 09:06:58 -0500163 * subj,obj, and type: are LSM specific.
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200164 * func: FILE_CHECK | BPRM_CHECK | MMAP_CHECK | MODULE_CHECK
165 * mask: contains the permission mask
Mimi Zohar3323eec2009-02-04 09:06:58 -0500166 * fsmagic: hex value
167 *
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500168 * Returns IMA_MEASURE, IMA_APPRAISE mask.
169 *
170 */
Dmitry Kasatkind9d300c2012-06-27 11:26:14 +0300171int ima_get_action(struct inode *inode, int mask, int function)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500172{
Peter Moodye7c568e2012-06-14 10:04:36 -0700173 int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500174
Dmitry Kasatkinfd5f4e902014-11-05 17:01:14 +0200175 flags &= ima_policy_flag;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500176
177 return ima_match_policy(inode, function, mask, flags);
178}
179
Mimi Zohar3323eec2009-02-04 09:06:58 -0500180/*
181 * ima_collect_measurement - collect file measurement
182 *
183 * Calculate the file hash, if it doesn't already exist,
184 * storing the measurement and i_version in the iint.
185 *
186 * Must be called with iint->mutex held.
187 *
188 * Return 0 on success, error code otherwise
189 */
Mimi Zoharf381c272011-03-09 14:13:22 -0500190int ima_collect_measurement(struct integrity_iint_cache *iint,
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300191 struct file *file,
192 struct evm_ima_xattr_data **xattr_value,
193 int *xattr_len)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500194{
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400195 const char *audit_cause = "failed";
Al Viro496ad9a2013-01-23 17:07:38 -0500196 struct inode *inode = file_inode(file);
Al Virob5830432014-10-31 01:22:04 -0400197 const char *filename = file->f_path.dentry->d_name.name;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500198 int result = 0;
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300199 struct {
200 struct ima_digest_data hdr;
201 char digest[IMA_MAX_DIGEST_SIZE];
202 } hash;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500203
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300204 if (xattr_value)
Al Virob5830432014-10-31 01:22:04 -0400205 *xattr_len = ima_read_xattr(file->f_path.dentry, xattr_value);
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300206
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500207 if (!(iint->flags & IMA_COLLECTED)) {
Al Viro496ad9a2013-01-23 17:07:38 -0500208 u64 i_version = file_inode(file)->i_version;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500209
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400210 if (file->f_flags & O_DIRECT) {
211 audit_cause = "failed(directio)";
212 result = -EACCES;
213 goto out;
214 }
215
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300216 /* use default hash algorithm */
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300217 hash.hdr.algo = ima_hash_algo;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300218
219 if (xattr_value)
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300220 ima_get_hash_algo(*xattr_value, *xattr_len, &hash.hdr);
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300221
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300222 result = ima_calc_file_hash(file, &hash.hdr);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500223 if (!result) {
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300224 int length = sizeof(hash.hdr) + hash.hdr.length;
225 void *tmpbuf = krealloc(iint->ima_hash, length,
226 GFP_NOFS);
227 if (tmpbuf) {
228 iint->ima_hash = tmpbuf;
229 memcpy(iint->ima_hash, &hash, length);
230 iint->version = i_version;
231 iint->flags |= IMA_COLLECTED;
232 } else
233 result = -ENOMEM;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500234 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500235 }
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400236out:
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500237 if (result)
238 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400239 filename, "collect_data", audit_cause,
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500240 result, 0);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500241 return result;
242}
243
244/*
245 * ima_store_measurement - store file measurement
246 *
247 * Create an "ima" template and then store the template by calling
248 * ima_store_template.
249 *
250 * We only get here if the inode has not already been measured,
251 * but the measurement could already exist:
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200252 * - multiple copies of the same file on either the same or
Mimi Zohar3323eec2009-02-04 09:06:58 -0500253 * different filesystems.
254 * - the inode was previously flushed as well as the iint info,
255 * containing the hashing info.
256 *
257 * Must be called with iint->mutex held.
258 */
Mimi Zoharf381c272011-03-09 14:13:22 -0500259void ima_store_measurement(struct integrity_iint_cache *iint,
Mimi Zoharbcbc9b02013-07-23 11:15:00 -0400260 struct file *file, const unsigned char *filename,
261 struct evm_ima_xattr_data *xattr_value,
262 int xattr_len)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500263{
Mimi Zohar52a13282013-12-11 14:44:04 -0500264 static const char op[] = "add_template_measure";
265 static const char audit_cause[] = "ENOMEM";
Mimi Zohar3323eec2009-02-04 09:06:58 -0500266 int result = -ENOMEM;
Al Viro496ad9a2013-01-23 17:07:38 -0500267 struct inode *inode = file_inode(file);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500268 struct ima_template_entry *entry;
Roberto Sassu8d94eb92015-04-11 17:12:39 +0200269 struct ima_event_data event_data = {iint, file, filename, xattr_value,
270 xattr_len, NULL};
Mimi Zohar3323eec2009-02-04 09:06:58 -0500271 int violation = 0;
272
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500273 if (iint->flags & IMA_MEASURED)
274 return;
275
Roberto Sassu23b57412015-04-11 17:09:50 +0200276 result = ima_alloc_init_template(&event_data, &entry);
Roberto Sassu7bc5f442013-06-07 12:16:28 +0200277 if (result < 0) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500278 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
279 op, audit_cause, result, 0);
280 return;
281 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500282
Roberto Sassu9803d412013-06-07 12:16:27 +0200283 result = ima_store_template(entry, violation, inode, filename);
Roberto Sassu45fae742011-12-19 15:57:27 +0100284 if (!result || result == -EEXIST)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500285 iint->flags |= IMA_MEASURED;
Roberto Sassu45fae742011-12-19 15:57:27 +0100286 if (result < 0)
Roberto Sassua7ed7c62013-12-02 19:40:34 +0100287 ima_free_template_entry(entry);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500288}
Peter Moodye7c568e2012-06-14 10:04:36 -0700289
290void ima_audit_measurement(struct integrity_iint_cache *iint,
291 const unsigned char *filename)
292{
293 struct audit_buffer *ab;
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300294 char hash[(iint->ima_hash->length * 2) + 1];
Mimi Zohar5278aa52013-06-07 12:16:38 +0200295 const char *algo_name = hash_algo_name[iint->ima_hash->algo];
296 char algo_hash[sizeof(hash) + strlen(algo_name) + 2];
Peter Moodye7c568e2012-06-14 10:04:36 -0700297 int i;
298
299 if (iint->flags & IMA_AUDITED)
300 return;
301
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300302 for (i = 0; i < iint->ima_hash->length; i++)
303 hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]);
Peter Moodye7c568e2012-06-14 10:04:36 -0700304 hash[i * 2] = '\0';
305
306 ab = audit_log_start(current->audit_context, GFP_KERNEL,
307 AUDIT_INTEGRITY_RULE);
308 if (!ab)
309 return;
310
311 audit_log_format(ab, "file=");
312 audit_log_untrustedstring(ab, filename);
313 audit_log_format(ab, " hash=");
Mimi Zohar5278aa52013-06-07 12:16:38 +0200314 snprintf(algo_hash, sizeof(algo_hash), "%s:%s", algo_name, hash);
315 audit_log_untrustedstring(ab, algo_hash);
Peter Moodye7c568e2012-06-14 10:04:36 -0700316
317 audit_log_task_info(ab, current);
318 audit_log_end(ab);
319
320 iint->flags |= IMA_AUDITED;
321}
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300322
323const char *ima_d_path(struct path *path, char **pathbuf)
324{
325 char *pathname = NULL;
326
Dmitry Kasatkin456f5fd2014-10-01 21:43:10 +0300327 *pathbuf = __getname();
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300328 if (*pathbuf) {
Dmitry Kasatkin17f4bad2014-08-19 16:48:39 +0300329 pathname = d_absolute_path(path, *pathbuf, PATH_MAX);
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300330 if (IS_ERR(pathname)) {
Dmitry Kasatkin456f5fd2014-10-01 21:43:10 +0300331 __putname(*pathbuf);
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300332 *pathbuf = NULL;
333 pathname = NULL;
334 }
335 }
Dmitry Kasatkin61997c42013-11-13 22:23:20 +0200336 return pathname ?: (const char *)path->dentry->d_name.name;
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300337}