blob: 0e7540863fc299687877ae4591961b13b60fcaa9 [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 Sassu7bc5f442013-06-07 12:16:28 +020025 * 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,
Mimi Zoharbcbc9b0c2013-07-23 11:15:00 -040029 struct evm_ima_xattr_data *xattr_value,
30 int xattr_len, struct ima_template_entry **entry)
Roberto Sassu7bc5f442013-06-07 12:16:28 +020031{
Roberto Sassua71dc652013-06-07 12:16:33 +020032 struct ima_template_desc *template_desc = ima_template_desc_current();
33 int i, result = 0;
Roberto Sassu7bc5f442013-06-07 12:16:28 +020034
Roberto Sassua71dc652013-06-07 12:16:33 +020035 *entry = kzalloc(sizeof(**entry) + template_desc->num_fields *
36 sizeof(struct ima_field_data), GFP_NOFS);
37 if (!*entry)
Roberto Sassu7bc5f442013-06-07 12:16:28 +020038 return -ENOMEM;
39
Roberto Sassua71dc652013-06-07 12:16:33 +020040 for (i = 0; i < template_desc->num_fields; i++) {
41 struct ima_template_field *field = template_desc->fields[i];
42 u32 len;
Roberto Sassu7bc5f442013-06-07 12:16:28 +020043
Roberto Sassua71dc652013-06-07 12:16:33 +020044 result = field->field_init(iint, file, filename,
Mimi Zoharbcbc9b0c2013-07-23 11:15:00 -040045 xattr_value, xattr_len,
Roberto Sassua71dc652013-06-07 12:16:33 +020046 &((*entry)->template_data[i]));
47 if (result != 0)
48 goto out;
Roberto Sassu7bc5f442013-06-07 12:16:28 +020049
Roberto Sassua71dc652013-06-07 12:16:33 +020050 len = (*entry)->template_data[i].len;
51 (*entry)->template_data_len += sizeof(len);
52 (*entry)->template_data_len += len;
53 }
54 (*entry)->template_desc = template_desc;
Roberto Sassu7bc5f442013-06-07 12:16:28 +020055 return 0;
Roberto Sassua71dc652013-06-07 12:16:33 +020056out:
57 kfree(*entry);
58 *entry = NULL;
Roberto Sassu7bc5f442013-06-07 12:16:28 +020059 return result;
60}
61
62/*
Mimi Zohar3323eec2009-02-04 09:06:58 -050063 * ima_store_template - store ima template measurements
64 *
65 * Calculate the hash of a template entry, add the template entry
66 * to an ordered list of measurement entries maintained inside the kernel,
67 * and also update the aggregate integrity value (maintained inside the
68 * configured TPM PCR) over the hashes of the current list of measurement
69 * entries.
70 *
71 * Applications retrieve the current kernel-held measurement list through
72 * the securityfs entries in /sys/kernel/security/ima. The signed aggregate
73 * TPM PCR (called quote) can be retrieved using a TPM user space library
74 * and is used to validate the measurement list.
75 *
76 * Returns 0 on success, error code otherwise
77 */
78int ima_store_template(struct ima_template_entry *entry,
Roberto Sassu9803d412013-06-07 12:16:27 +020079 int violation, struct inode *inode,
80 const unsigned char *filename)
Mimi Zohar3323eec2009-02-04 09:06:58 -050081{
82 const char *op = "add_template_measure";
83 const char *audit_cause = "hashing_error";
Roberto Sassua71dc652013-06-07 12:16:33 +020084 char *template_name = entry->template_desc->name;
Mimi Zohar3323eec2009-02-04 09:06:58 -050085 int result;
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +030086 struct {
87 struct ima_digest_data hdr;
Mimi Zohar140d8022013-03-11 20:29:47 -040088 char digest[TPM_DIGEST_SIZE];
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +030089 } hash;
Mimi Zohar3323eec2009-02-04 09:06:58 -050090
Mimi Zohar3323eec2009-02-04 09:06:58 -050091 if (!violation) {
Roberto Sassua71dc652013-06-07 12:16:33 +020092 int num_fields = entry->template_desc->num_fields;
93
Dmitry Kasatkinea593992013-06-07 12:16:24 +020094 /* this function uses default algo */
95 hash.hdr.algo = HASH_ALGO_SHA1;
Roberto Sassua71dc652013-06-07 12:16:33 +020096 result = ima_calc_field_array_hash(&entry->template_data[0],
97 num_fields, &hash.hdr);
Mimi Zohar3323eec2009-02-04 09:06:58 -050098 if (result < 0) {
99 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode,
Roberto Sassua71dc652013-06-07 12:16:33 +0200100 template_name, op,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500101 audit_cause, result, 0);
102 return result;
103 }
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300104 memcpy(entry->digest, hash.hdr.digest, hash.hdr.length);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500105 }
Roberto Sassu9803d412013-06-07 12:16:27 +0200106 result = ima_add_template_entry(entry, violation, op, inode, filename);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500107 return result;
108}
109
110/*
111 * ima_add_violation - add violation to measurement list.
112 *
113 * Violations are flagged in the measurement list with zero hash values.
114 * By extending the PCR with 0xFF's instead of with zeroes, the PCR
115 * value is invalidated.
116 */
Roberto Sassu7d802a22013-06-07 12:16:26 +0200117void ima_add_violation(struct file *file, const unsigned char *filename,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500118 const char *op, const char *cause)
119{
120 struct ima_template_entry *entry;
Roberto Sassu7d802a22013-06-07 12:16:26 +0200121 struct inode *inode = file->f_dentry->d_inode;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500122 int violation = 1;
123 int result;
124
125 /* can overflow, only indicator */
126 atomic_long_inc(&ima_htable.violations);
127
Mimi Zoharbcbc9b0c2013-07-23 11:15:00 -0400128 result = ima_alloc_init_template(NULL, file, filename,
129 NULL, 0, &entry);
Roberto Sassu7bc5f442013-06-07 12:16:28 +0200130 if (result < 0) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500131 result = -ENOMEM;
132 goto err_out;
133 }
Roberto Sassu9803d412013-06-07 12:16:27 +0200134 result = ima_store_template(entry, violation, inode, filename);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500135 if (result < 0)
136 kfree(entry);
137err_out:
138 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
139 op, cause, result, 0);
140}
141
142/**
Dmitry Kasatkind9d300c2012-06-27 11:26:14 +0300143 * ima_get_action - appraise & measure decision based on policy.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500144 * @inode: pointer to inode to measure
145 * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXECUTE)
Mimi Zohar16cac492012-12-13 11:15:04 -0500146 * @function: calling function (FILE_CHECK, BPRM_CHECK, MMAP_CHECK, MODULE_CHECK)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500147 *
148 * The policy is defined in terms of keypairs:
149 * subj=, obj=, type=, func=, mask=, fsmagic=
150 * subj,obj, and type: are LSM specific.
Mimi Zohar16cac492012-12-13 11:15:04 -0500151 * func: FILE_CHECK | BPRM_CHECK | MMAP_CHECK | MODULE_CHECK
Mimi Zohar3323eec2009-02-04 09:06:58 -0500152 * mask: contains the permission mask
153 * fsmagic: hex value
154 *
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500155 * Returns IMA_MEASURE, IMA_APPRAISE mask.
156 *
157 */
Dmitry Kasatkind9d300c2012-06-27 11:26:14 +0300158int ima_get_action(struct inode *inode, int mask, int function)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500159{
Peter Moodye7c568e2012-06-14 10:04:36 -0700160 int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500161
162 if (!ima_appraise)
163 flags &= ~IMA_APPRAISE;
164
165 return ima_match_policy(inode, function, mask, flags);
166}
167
Mimi Zohar1adace92011-02-22 10:19:43 -0500168int ima_must_measure(struct inode *inode, int mask, int function)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500169{
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500170 return ima_match_policy(inode, function, mask, IMA_MEASURE);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500171}
172
173/*
174 * ima_collect_measurement - collect file measurement
175 *
176 * Calculate the file hash, if it doesn't already exist,
177 * storing the measurement and i_version in the iint.
178 *
179 * Must be called with iint->mutex held.
180 *
181 * Return 0 on success, error code otherwise
182 */
Mimi Zoharf381c272011-03-09 14:13:22 -0500183int ima_collect_measurement(struct integrity_iint_cache *iint,
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300184 struct file *file,
185 struct evm_ima_xattr_data **xattr_value,
186 int *xattr_len)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500187{
Al Viro496ad9a2013-01-23 17:07:38 -0500188 struct inode *inode = file_inode(file);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500189 const char *filename = file->f_dentry->d_name.name;
190 int result = 0;
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300191 struct {
192 struct ima_digest_data hdr;
193 char digest[IMA_MAX_DIGEST_SIZE];
194 } hash;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500195
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300196 if (xattr_value)
197 *xattr_len = ima_read_xattr(file->f_dentry, xattr_value);
198
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500199 if (!(iint->flags & IMA_COLLECTED)) {
Al Viro496ad9a2013-01-23 17:07:38 -0500200 u64 i_version = file_inode(file)->i_version;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500201
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300202 /* use default hash algorithm */
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300203 hash.hdr.algo = ima_hash_algo;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300204
205 if (xattr_value)
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300206 ima_get_hash_algo(*xattr_value, *xattr_len, &hash.hdr);
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300207
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300208 result = ima_calc_file_hash(file, &hash.hdr);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500209 if (!result) {
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300210 int length = sizeof(hash.hdr) + hash.hdr.length;
211 void *tmpbuf = krealloc(iint->ima_hash, length,
212 GFP_NOFS);
213 if (tmpbuf) {
214 iint->ima_hash = tmpbuf;
215 memcpy(iint->ima_hash, &hash, length);
216 iint->version = i_version;
217 iint->flags |= IMA_COLLECTED;
218 } else
219 result = -ENOMEM;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500220 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500221 }
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500222 if (result)
223 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
224 filename, "collect_data", "failed",
225 result, 0);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500226 return result;
227}
228
229/*
230 * ima_store_measurement - store file measurement
231 *
232 * Create an "ima" template and then store the template by calling
233 * ima_store_template.
234 *
235 * We only get here if the inode has not already been measured,
236 * but the measurement could already exist:
237 * - multiple copies of the same file on either the same or
238 * different filesystems.
239 * - the inode was previously flushed as well as the iint info,
240 * containing the hashing info.
241 *
242 * Must be called with iint->mutex held.
243 */
Mimi Zoharf381c272011-03-09 14:13:22 -0500244void ima_store_measurement(struct integrity_iint_cache *iint,
Mimi Zoharbcbc9b0c2013-07-23 11:15:00 -0400245 struct file *file, const unsigned char *filename,
246 struct evm_ima_xattr_data *xattr_value,
247 int xattr_len)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500248{
249 const char *op = "add_template_measure";
250 const char *audit_cause = "ENOMEM";
251 int result = -ENOMEM;
Al Viro496ad9a2013-01-23 17:07:38 -0500252 struct inode *inode = file_inode(file);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500253 struct ima_template_entry *entry;
254 int violation = 0;
255
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500256 if (iint->flags & IMA_MEASURED)
257 return;
258
Mimi Zoharbcbc9b0c2013-07-23 11:15:00 -0400259 result = ima_alloc_init_template(iint, file, filename,
260 xattr_value, xattr_len, &entry);
Roberto Sassu7bc5f442013-06-07 12:16:28 +0200261 if (result < 0) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500262 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
263 op, audit_cause, result, 0);
264 return;
265 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500266
Roberto Sassu9803d412013-06-07 12:16:27 +0200267 result = ima_store_template(entry, violation, inode, filename);
Roberto Sassu45fae742011-12-19 15:57:27 +0100268 if (!result || result == -EEXIST)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500269 iint->flags |= IMA_MEASURED;
Roberto Sassu45fae742011-12-19 15:57:27 +0100270 if (result < 0)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500271 kfree(entry);
272}
Peter Moodye7c568e2012-06-14 10:04:36 -0700273
274void ima_audit_measurement(struct integrity_iint_cache *iint,
275 const unsigned char *filename)
276{
277 struct audit_buffer *ab;
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300278 char hash[(iint->ima_hash->length * 2) + 1];
Mimi Zohar5278aa52013-06-07 12:16:38 +0200279 const char *algo_name = hash_algo_name[iint->ima_hash->algo];
280 char algo_hash[sizeof(hash) + strlen(algo_name) + 2];
Peter Moodye7c568e2012-06-14 10:04:36 -0700281 int i;
282
283 if (iint->flags & IMA_AUDITED)
284 return;
285
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300286 for (i = 0; i < iint->ima_hash->length; i++)
287 hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]);
Peter Moodye7c568e2012-06-14 10:04:36 -0700288 hash[i * 2] = '\0';
289
290 ab = audit_log_start(current->audit_context, GFP_KERNEL,
291 AUDIT_INTEGRITY_RULE);
292 if (!ab)
293 return;
294
295 audit_log_format(ab, "file=");
296 audit_log_untrustedstring(ab, filename);
297 audit_log_format(ab, " hash=");
Mimi Zohar5278aa52013-06-07 12:16:38 +0200298 snprintf(algo_hash, sizeof(algo_hash), "%s:%s", algo_name, hash);
299 audit_log_untrustedstring(ab, algo_hash);
Peter Moodye7c568e2012-06-14 10:04:36 -0700300
301 audit_log_task_info(ab, current);
302 audit_log_end(ab);
303
304 iint->flags |= IMA_AUDITED;
305}
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300306
307const char *ima_d_path(struct path *path, char **pathbuf)
308{
309 char *pathname = NULL;
310
311 /* We will allow 11 spaces for ' (deleted)' to be appended */
312 *pathbuf = kmalloc(PATH_MAX + 11, GFP_KERNEL);
313 if (*pathbuf) {
314 pathname = d_path(path, *pathbuf, PATH_MAX + 11);
315 if (IS_ERR(pathname)) {
316 kfree(*pathbuf);
317 *pathbuf = NULL;
318 pathname = NULL;
319 }
320 }
321 return pathname;
322}