blob: a0fe5041310a790a276c792757cba0231cb25cce [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 Zohar523979a2009-02-11 11:12:28 -050024static const char *IMA_TEMPLATE_NAME = "ima";
Mimi Zohar3323eec2009-02-04 09:06:58 -050025
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,
Roberto Sassu9803d412013-06-07 12:16:27 +020043 int violation, struct inode *inode,
44 const unsigned char *filename)
Mimi Zohar3323eec2009-02-04 09:06:58 -050045{
46 const char *op = "add_template_measure";
47 const char *audit_cause = "hashing_error";
48 int result;
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +030049 struct {
50 struct ima_digest_data hdr;
Mimi Zohar140d8022013-03-11 20:29:47 -040051 char digest[TPM_DIGEST_SIZE];
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +030052 } hash;
Mimi Zohar3323eec2009-02-04 09:06:58 -050053
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) {
Dmitry Kasatkinea593992013-06-07 12:16:24 +020059 /* this function uses default algo */
60 hash.hdr.algo = HASH_ALGO_SHA1;
Dmitry Kasatkin50af5542012-05-14 14:13:56 +030061 result = ima_calc_buffer_hash(&entry->template,
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +030062 entry->template_len, &hash.hdr);
Mimi Zohar3323eec2009-02-04 09:06:58 -050063 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 }
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +030069 memcpy(entry->digest, hash.hdr.digest, hash.hdr.length);
Mimi Zohar3323eec2009-02-04 09:06:58 -050070 }
Roberto Sassu9803d412013-06-07 12:16:27 +020071 result = ima_add_template_entry(entry, violation, op, inode, filename);
Mimi Zohar3323eec2009-02-04 09:06:58 -050072 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 */
Roberto Sassu7d802a22013-06-07 12:16:26 +020082void ima_add_violation(struct file *file, const unsigned char *filename,
Mimi Zohar3323eec2009-02-04 09:06:58 -050083 const char *op, const char *cause)
84{
85 struct ima_template_entry *entry;
Roberto Sassu7d802a22013-06-07 12:16:26 +020086 struct inode *inode = file->f_dentry->d_inode;
Mimi Zohar3323eec2009-02-04 09:06:58 -050087 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);
Roberto Sassu9803d412013-06-07 12:16:27 +0200100 result = ima_store_template(entry, violation, inode, filename);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500101 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/**
Dmitry Kasatkind9d300c2012-06-27 11:26:14 +0300109 * ima_get_action - appraise & measure decision based on policy.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500110 * @inode: pointer to inode to measure
111 * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXECUTE)
Mimi Zohar16cac492012-12-13 11:15:04 -0500112 * @function: calling function (FILE_CHECK, BPRM_CHECK, MMAP_CHECK, MODULE_CHECK)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500113 *
114 * The policy is defined in terms of keypairs:
115 * subj=, obj=, type=, func=, mask=, fsmagic=
116 * subj,obj, and type: are LSM specific.
Mimi Zohar16cac492012-12-13 11:15:04 -0500117 * func: FILE_CHECK | BPRM_CHECK | MMAP_CHECK | MODULE_CHECK
Mimi Zohar3323eec2009-02-04 09:06:58 -0500118 * mask: contains the permission mask
119 * fsmagic: hex value
120 *
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500121 * Returns IMA_MEASURE, IMA_APPRAISE mask.
122 *
123 */
Dmitry Kasatkind9d300c2012-06-27 11:26:14 +0300124int ima_get_action(struct inode *inode, int mask, int function)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500125{
Peter Moodye7c568e2012-06-14 10:04:36 -0700126 int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500127
128 if (!ima_appraise)
129 flags &= ~IMA_APPRAISE;
130
131 return ima_match_policy(inode, function, mask, flags);
132}
133
Mimi Zohar1adace92011-02-22 10:19:43 -0500134int ima_must_measure(struct inode *inode, int mask, int function)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500135{
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500136 return ima_match_policy(inode, function, mask, IMA_MEASURE);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500137}
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 */
Mimi Zoharf381c272011-03-09 14:13:22 -0500149int ima_collect_measurement(struct integrity_iint_cache *iint,
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300150 struct file *file,
151 struct evm_ima_xattr_data **xattr_value,
152 int *xattr_len)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500153{
Al Viro496ad9a2013-01-23 17:07:38 -0500154 struct inode *inode = file_inode(file);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500155 const char *filename = file->f_dentry->d_name.name;
156 int result = 0;
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300157 struct {
158 struct ima_digest_data hdr;
159 char digest[IMA_MAX_DIGEST_SIZE];
160 } hash;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500161
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300162 if (xattr_value)
163 *xattr_len = ima_read_xattr(file->f_dentry, xattr_value);
164
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500165 if (!(iint->flags & IMA_COLLECTED)) {
Al Viro496ad9a2013-01-23 17:07:38 -0500166 u64 i_version = file_inode(file)->i_version;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500167
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300168 /* use default hash algorithm */
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300169 hash.hdr.algo = ima_hash_algo;
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300170
171 if (xattr_value)
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300172 ima_get_hash_algo(*xattr_value, *xattr_len, &hash.hdr);
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300173
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300174 result = ima_calc_file_hash(file, &hash.hdr);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500175 if (!result) {
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300176 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;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500186 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500187 }
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500188 if (result)
189 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
190 filename, "collect_data", "failed",
191 result, 0);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500192 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 */
Mimi Zoharf381c272011-03-09 14:13:22 -0500210void ima_store_measurement(struct integrity_iint_cache *iint,
211 struct file *file, const unsigned char *filename)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500212{
213 const char *op = "add_template_measure";
214 const char *audit_cause = "ENOMEM";
215 int result = -ENOMEM;
Al Viro496ad9a2013-01-23 17:07:38 -0500216 struct inode *inode = file_inode(file);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500217 struct ima_template_entry *entry;
218 int violation = 0;
219
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500220 if (iint->flags & IMA_MEASURED)
221 return;
222
Mimi Zohar3323eec2009-02-04 09:06:58 -0500223 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));
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300230 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;
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300235
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300236 hash.hdr.algo = ima_hash_algo;
237 result = ima_calc_file_hash(file, &hash.hdr);
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300238 if (result)
239 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
240 filename, "collect_data", "failed",
241 result, 0);
242 else
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300243 memcpy(entry->template.digest, hash.hdr.digest,
244 hash.hdr.length);
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300245 } else
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300246 memcpy(entry->template.digest, iint->ima_hash->digest,
247 iint->ima_hash->length);
Mimi Zohar08e1b762012-06-20 09:32:55 -0400248 strcpy(entry->template.file_name,
249 (strlen(filename) > IMA_EVENT_NAME_LEN_MAX) ?
250 file->f_dentry->d_name.name : filename);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500251
Roberto Sassu9803d412013-06-07 12:16:27 +0200252 result = ima_store_template(entry, violation, inode, filename);
Roberto Sassu45fae742011-12-19 15:57:27 +0100253 if (!result || result == -EEXIST)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500254 iint->flags |= IMA_MEASURED;
Roberto Sassu45fae742011-12-19 15:57:27 +0100255 if (result < 0)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500256 kfree(entry);
257}
Peter Moodye7c568e2012-06-14 10:04:36 -0700258
259void ima_audit_measurement(struct integrity_iint_cache *iint,
260 const unsigned char *filename)
261{
262 struct audit_buffer *ab;
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300263 char hash[(iint->ima_hash->length * 2) + 1];
Peter Moodye7c568e2012-06-14 10:04:36 -0700264 int i;
265
266 if (iint->flags & IMA_AUDITED)
267 return;
268
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300269 for (i = 0; i < iint->ima_hash->length; i++)
270 hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]);
Peter Moodye7c568e2012-06-14 10:04:36 -0700271 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}
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300288
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}