blob: 35c129cbb7e9bf9357d963c73e134f98b1a1c1dc [file] [log] [blame]
Thomas Gleixnerb886d832019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Mimi Zohar3323eec2009-02-04 09:06:58 -05002/*
3 * Copyright (C) 2008 IBM Corporation
4 *
5 * Author: Mimi Zohar <zohar@us.ibm.com>
6 *
Mimi Zohar3323eec2009-02-04 09:06:58 -05007 * File: ima_api.c
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -05008 * Implements must_appraise_or_measure, collect_measurement,
9 * appraise_measurement, store_measurement and store_template.
Mimi Zohar3323eec2009-02-04 09:06:58 -050010 */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050012#include <linux/file.h>
13#include <linux/fs.h>
14#include <linux/xattr.h>
15#include <linux/evm.h>
Jeff Layton3b370b22017-12-11 06:35:21 -050016#include <linux/iversion.h>
Dmitry Kasatkin1525b062014-10-30 12:39:39 +020017
Mimi Zohar3323eec2009-02-04 09:06:58 -050018#include "ima.h"
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050019
Mimi Zohar3323eec2009-02-04 09:06:58 -050020/*
Roberto Sassua7ed7c62013-12-02 19:40:34 +010021 * ima_free_template_entry - free an existing template entry
22 */
23void ima_free_template_entry(struct ima_template_entry *entry)
24{
25 int i;
26
27 for (i = 0; i < entry->template_desc->num_fields; i++)
28 kfree(entry->template_data[i].data);
29
30 kfree(entry);
31}
32
33/*
Roberto Sassu7bc5f442013-06-07 12:16:28 +020034 * ima_alloc_init_template - create and initialize a new template entry
35 */
Roberto Sassu23b57412015-04-11 17:09:50 +020036int ima_alloc_init_template(struct ima_event_data *event_data,
37 struct ima_template_entry **entry)
Roberto Sassu7bc5f442013-06-07 12:16:28 +020038{
Roberto Sassua71dc652013-06-07 12:16:33 +020039 struct ima_template_desc *template_desc = ima_template_desc_current();
40 int i, result = 0;
Roberto Sassu7bc5f442013-06-07 12:16:28 +020041
Roberto Sassua71dc652013-06-07 12:16:33 +020042 *entry = kzalloc(sizeof(**entry) + template_desc->num_fields *
43 sizeof(struct ima_field_data), GFP_NOFS);
44 if (!*entry)
Roberto Sassu7bc5f442013-06-07 12:16:28 +020045 return -ENOMEM;
46
Roberto Sassua7ed7c62013-12-02 19:40:34 +010047 (*entry)->template_desc = template_desc;
Roberto Sassua71dc652013-06-07 12:16:33 +020048 for (i = 0; i < template_desc->num_fields; i++) {
Eric Biggersb2724d52018-09-07 13:22:23 -070049 const struct ima_template_field *field =
50 template_desc->fields[i];
Roberto Sassua71dc652013-06-07 12:16:33 +020051 u32 len;
Roberto Sassu7bc5f442013-06-07 12:16:28 +020052
Roberto Sassu23b57412015-04-11 17:09:50 +020053 result = field->field_init(event_data,
Roberto Sassua71dc652013-06-07 12:16:33 +020054 &((*entry)->template_data[i]));
55 if (result != 0)
56 goto out;
Roberto Sassu7bc5f442013-06-07 12:16:28 +020057
Roberto Sassua71dc652013-06-07 12:16:33 +020058 len = (*entry)->template_data[i].len;
59 (*entry)->template_data_len += sizeof(len);
60 (*entry)->template_data_len += len;
61 }
Roberto Sassu7bc5f442013-06-07 12:16:28 +020062 return 0;
Roberto Sassua71dc652013-06-07 12:16:33 +020063out:
Roberto Sassua7ed7c62013-12-02 19:40:34 +010064 ima_free_template_entry(*entry);
Roberto Sassua71dc652013-06-07 12:16:33 +020065 *entry = NULL;
Roberto Sassu7bc5f442013-06-07 12:16:28 +020066 return result;
67}
68
69/*
Mimi Zohar3323eec2009-02-04 09:06:58 -050070 * ima_store_template - store ima template measurements
71 *
72 * Calculate the hash of a template entry, add the template entry
73 * to an ordered list of measurement entries maintained inside the kernel,
74 * and also update the aggregate integrity value (maintained inside the
75 * configured TPM PCR) over the hashes of the current list of measurement
76 * entries.
77 *
78 * Applications retrieve the current kernel-held measurement list through
79 * the securityfs entries in /sys/kernel/security/ima. The signed aggregate
80 * TPM PCR (called quote) can be retrieved using a TPM user space library
81 * and is used to validate the measurement list.
82 *
83 * Returns 0 on success, error code otherwise
84 */
85int ima_store_template(struct ima_template_entry *entry,
Roberto Sassu9803d412013-06-07 12:16:27 +020086 int violation, struct inode *inode,
Eric Richter14b1da82016-06-01 13:14:03 -050087 const unsigned char *filename, int pcr)
Mimi Zohar3323eec2009-02-04 09:06:58 -050088{
Mimi Zohar52a13282013-12-11 14:44:04 -050089 static const char op[] = "add_template_measure";
90 static const char audit_cause[] = "hashing_error";
Roberto Sassua71dc652013-06-07 12:16:33 +020091 char *template_name = entry->template_desc->name;
Mimi Zohar3323eec2009-02-04 09:06:58 -050092 int result;
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +030093 struct {
94 struct ima_digest_data hdr;
Mimi Zohar140d8022013-03-11 20:29:47 -040095 char digest[TPM_DIGEST_SIZE];
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +030096 } hash;
Mimi Zohar3323eec2009-02-04 09:06:58 -050097
Mimi Zohar3323eec2009-02-04 09:06:58 -050098 if (!violation) {
Roberto Sassua71dc652013-06-07 12:16:33 +020099 int num_fields = entry->template_desc->num_fields;
100
Dmitry Kasatkinea593992013-06-07 12:16:24 +0200101 /* this function uses default algo */
102 hash.hdr.algo = HASH_ALGO_SHA1;
Roberto Sassua71dc652013-06-07 12:16:33 +0200103 result = ima_calc_field_array_hash(&entry->template_data[0],
Roberto Sassub6f8f162013-11-08 19:21:39 +0100104 entry->template_desc,
Roberto Sassua71dc652013-06-07 12:16:33 +0200105 num_fields, &hash.hdr);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500106 if (result < 0) {
107 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode,
Roberto Sassua71dc652013-06-07 12:16:33 +0200108 template_name, op,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500109 audit_cause, result, 0);
110 return result;
111 }
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300112 memcpy(entry->digest, hash.hdr.digest, hash.hdr.length);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500113 }
Eric Richter14b1da82016-06-01 13:14:03 -0500114 entry->pcr = pcr;
Roberto Sassu9803d412013-06-07 12:16:27 +0200115 result = ima_add_template_entry(entry, violation, op, inode, filename);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500116 return result;
117}
118
119/*
120 * ima_add_violation - add violation to measurement list.
121 *
122 * Violations are flagged in the measurement list with zero hash values.
123 * By extending the PCR with 0xFF's instead of with zeroes, the PCR
124 * value is invalidated.
125 */
Roberto Sassu7d802a22013-06-07 12:16:26 +0200126void ima_add_violation(struct file *file, const unsigned char *filename,
Roberto Sassu8d94eb92015-04-11 17:12:39 +0200127 struct integrity_iint_cache *iint,
Mimi Zohar3323eec2009-02-04 09:06:58 -0500128 const char *op, const char *cause)
129{
130 struct ima_template_entry *entry;
Libo Chen31d4b762013-12-11 14:11:28 +0800131 struct inode *inode = file_inode(file);
Roberto Sassu8d94eb92015-04-11 17:12:39 +0200132 struct ima_event_data event_data = {iint, file, filename, NULL, 0,
133 cause};
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 }
Eric Richter14b1da82016-06-01 13:14:03 -0500145 result = ima_store_template(entry, violation, inode,
146 filename, CONFIG_IMA_MEASURE_PCR_IDX);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500147 if (result < 0)
Roberto Sassua7ed7c62013-12-02 19:40:34 +0100148 ima_free_template_entry(entry);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500149err_out:
150 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
151 op, cause, result, 0);
152}
153
154/**
Dmitry Kasatkind9d300c2012-06-27 11:26:14 +0300155 * ima_get_action - appraise & measure decision based on policy.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500156 * @inode: pointer to inode to measure
Matthew Garrettd906c102018-01-08 13:36:20 -0800157 * @cred: pointer to credentials structure to validate
158 * @secid: secid of the task being validated
Lans Zhang20f482a2017-01-06 12:38:11 +0800159 * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXEC,
160 * MAY_APPEND)
Mimi Zohar4ad87a32016-01-14 20:59:14 -0500161 * @func: caller identifier
Eric Richter725de7f2016-06-01 13:14:02 -0500162 * @pcr: pointer filled in if matched measure policy sets pcr=
Mimi Zohar3323eec2009-02-04 09:06:58 -0500163 *
164 * The policy is defined in terms of keypairs:
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200165 * subj=, obj=, type=, func=, mask=, fsmagic=
Mimi Zohar3323eec2009-02-04 09:06:58 -0500166 * subj,obj, and type: are LSM specific.
Matthew Garrettd906c102018-01-08 13:36:20 -0800167 * func: FILE_CHECK | BPRM_CHECK | CREDS_CHECK | MMAP_CHECK | MODULE_CHECK
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200168 * mask: contains the permission mask
Mimi Zohar3323eec2009-02-04 09:06:58 -0500169 * fsmagic: hex value
170 *
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500171 * Returns IMA_MEASURE, IMA_APPRAISE mask.
172 *
173 */
Matthew Garrettd906c102018-01-08 13:36:20 -0800174int ima_get_action(struct inode *inode, const struct cred *cred, u32 secid,
175 int mask, enum ima_hooks func, int *pcr)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500176{
Mimi Zoharda1b0022016-09-29 10:04:52 -0400177 int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE | IMA_HASH;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500178
Dmitry Kasatkinfd5f4e902014-11-05 17:01:14 +0200179 flags &= ima_policy_flag;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500180
Matthew Garrettd906c102018-01-08 13:36:20 -0800181 return ima_match_policy(inode, cred, secid, func, mask, flags, pcr);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500182}
183
Mimi Zohar3323eec2009-02-04 09:06:58 -0500184/*
185 * ima_collect_measurement - collect file measurement
186 *
187 * Calculate the file hash, if it doesn't already exist,
188 * storing the measurement and i_version in the iint.
189 *
190 * Must be called with iint->mutex held.
191 *
192 * Return 0 on success, error code otherwise
193 */
Mimi Zoharf381c272011-03-09 14:13:22 -0500194int ima_collect_measurement(struct integrity_iint_cache *iint,
Mimi Zoharcf222212016-01-14 17:57:47 -0500195 struct file *file, void *buf, loff_t size,
196 enum hash_algo algo)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500197{
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400198 const char *audit_cause = "failed";
Al Viro496ad9a2013-01-23 17:07:38 -0500199 struct inode *inode = file_inode(file);
Al Virob5830432014-10-31 01:22:04 -0400200 const char *filename = file->f_path.dentry->d_name.name;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500201 int result = 0;
Mimi Zoharf3cc6b22017-06-17 23:56:23 -0400202 int length;
203 void *tmpbuf;
204 u64 i_version;
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300205 struct {
206 struct ima_digest_data hdr;
207 char digest[IMA_MAX_DIGEST_SIZE];
208 } hash;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500209
Mimi Zoharf3cc6b22017-06-17 23:56:23 -0400210 if (iint->flags & IMA_COLLECTED)
211 goto out;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500212
Mimi Zoharf3cc6b22017-06-17 23:56:23 -0400213 /*
214 * Dectecting file change is based on i_version. On filesystems
215 * which do not support i_version, support is limited to an initial
216 * measurement/appraisal/audit.
217 */
Jeff Layton3b370b22017-12-11 06:35:21 -0500218 i_version = inode_query_iversion(inode);
Mimi Zoharf3cc6b22017-06-17 23:56:23 -0400219 hash.hdr.algo = algo;
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400220
Mimi Zoharf3cc6b22017-06-17 23:56:23 -0400221 /* Initialize hash digest to 0's in case of failure */
222 memset(&hash.digest, 0, sizeof(hash.digest));
Dmitry Kasatkind3634d02013-04-25 10:44:04 +0300223
Mimi Zoharf3cc6b22017-06-17 23:56:23 -0400224 if (buf)
225 result = ima_calc_buffer_hash(buf, size, &hash.hdr);
226 else
227 result = ima_calc_file_hash(file, &hash.hdr);
228
229 if (result && result != -EBADF && result != -EINVAL)
230 goto out;
231
232 length = sizeof(hash.hdr) + hash.hdr.length;
233 tmpbuf = krealloc(iint->ima_hash, length, GFP_NOFS);
234 if (!tmpbuf) {
235 result = -ENOMEM;
236 goto out;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500237 }
Mimi Zoharf3cc6b22017-06-17 23:56:23 -0400238
239 iint->ima_hash = tmpbuf;
240 memcpy(iint->ima_hash, &hash, length);
241 iint->version = i_version;
242
243 /* Possibly temporary failure due to type of read (eg. O_DIRECT) */
244 if (!result)
245 iint->flags |= IMA_COLLECTED;
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400246out:
Mimi Zoharf3cc6b22017-06-17 23:56:23 -0400247 if (result) {
248 if (file->f_flags & O_DIRECT)
249 audit_cause = "failed(directio)";
250
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500251 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
Mimi Zoharf9b2a732014-05-12 09:28:11 -0400252 filename, "collect_data", audit_cause,
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500253 result, 0);
Mimi Zoharf3cc6b22017-06-17 23:56:23 -0400254 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500255 return result;
256}
257
258/*
259 * ima_store_measurement - store file measurement
260 *
261 * Create an "ima" template and then store the template by calling
262 * ima_store_template.
263 *
264 * We only get here if the inode has not already been measured,
265 * but the measurement could already exist:
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +0200266 * - multiple copies of the same file on either the same or
Mimi Zohar3323eec2009-02-04 09:06:58 -0500267 * different filesystems.
268 * - the inode was previously flushed as well as the iint info,
269 * containing the hashing info.
270 *
271 * Must be called with iint->mutex held.
272 */
Mimi Zoharf381c272011-03-09 14:13:22 -0500273void ima_store_measurement(struct integrity_iint_cache *iint,
Mimi Zoharbcbc9b0c2013-07-23 11:15:00 -0400274 struct file *file, const unsigned char *filename,
275 struct evm_ima_xattr_data *xattr_value,
Eric Richter14b1da82016-06-01 13:14:03 -0500276 int xattr_len, int pcr)
Mimi Zohar3323eec2009-02-04 09:06:58 -0500277{
Mimi Zohar52a13282013-12-11 14:44:04 -0500278 static const char op[] = "add_template_measure";
279 static const char audit_cause[] = "ENOMEM";
Mimi Zohar3323eec2009-02-04 09:06:58 -0500280 int result = -ENOMEM;
Al Viro496ad9a2013-01-23 17:07:38 -0500281 struct inode *inode = file_inode(file);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500282 struct ima_template_entry *entry;
Roberto Sassu8d94eb92015-04-11 17:12:39 +0200283 struct ima_event_data event_data = {iint, file, filename, xattr_value,
284 xattr_len, NULL};
Mimi Zohar3323eec2009-02-04 09:06:58 -0500285 int violation = 0;
286
Eric Richtera4226382016-06-01 13:14:06 -0500287 if (iint->measured_pcrs & (0x1 << pcr))
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500288 return;
289
Roberto Sassu23b57412015-04-11 17:09:50 +0200290 result = ima_alloc_init_template(&event_data, &entry);
Roberto Sassu7bc5f442013-06-07 12:16:28 +0200291 if (result < 0) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500292 integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
293 op, audit_cause, result, 0);
294 return;
295 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500296
Eric Richter14b1da82016-06-01 13:14:03 -0500297 result = ima_store_template(entry, violation, inode, filename, pcr);
Mimi Zoharf3cc6b22017-06-17 23:56:23 -0400298 if ((!result || result == -EEXIST) && !(file->f_flags & O_DIRECT)) {
Mimi Zohar3323eec2009-02-04 09:06:58 -0500299 iint->flags |= IMA_MEASURED;
Eric Richtera4226382016-06-01 13:14:06 -0500300 iint->measured_pcrs |= (0x1 << pcr);
301 }
Roberto Sassu45fae742011-12-19 15:57:27 +0100302 if (result < 0)
Roberto Sassua7ed7c62013-12-02 19:40:34 +0100303 ima_free_template_entry(entry);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500304}
Peter Moodye7c568e2012-06-14 10:04:36 -0700305
306void ima_audit_measurement(struct integrity_iint_cache *iint,
307 const unsigned char *filename)
308{
309 struct audit_buffer *ab;
Tycho Andersene456ef82018-03-08 16:08:36 -0700310 char *hash;
Mimi Zohar5278aa52013-06-07 12:16:38 +0200311 const char *algo_name = hash_algo_name[iint->ima_hash->algo];
Peter Moodye7c568e2012-06-14 10:04:36 -0700312 int i;
313
314 if (iint->flags & IMA_AUDITED)
315 return;
316
Tycho Andersene456ef82018-03-08 16:08:36 -0700317 hash = kzalloc((iint->ima_hash->length * 2) + 1, GFP_KERNEL);
318 if (!hash)
319 return;
320
Dmitry Kasatkina35c3fb2013-04-25 10:44:04 +0300321 for (i = 0; i < iint->ima_hash->length; i++)
322 hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]);
Peter Moodye7c568e2012-06-14 10:04:36 -0700323 hash[i * 2] = '\0';
324
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -0400325 ab = audit_log_start(audit_context(), GFP_KERNEL,
Peter Moodye7c568e2012-06-14 10:04:36 -0700326 AUDIT_INTEGRITY_RULE);
327 if (!ab)
Tycho Andersene456ef82018-03-08 16:08:36 -0700328 goto out;
Peter Moodye7c568e2012-06-14 10:04:36 -0700329
330 audit_log_format(ab, "file=");
331 audit_log_untrustedstring(ab, filename);
Tycho Andersene456ef82018-03-08 16:08:36 -0700332 audit_log_format(ab, " hash=\"%s:%s\"", algo_name, hash);
Peter Moodye7c568e2012-06-14 10:04:36 -0700333
Paul Moore2a1fe212018-11-26 18:40:07 -0500334 audit_log_task_info(ab);
Peter Moodye7c568e2012-06-14 10:04:36 -0700335 audit_log_end(ab);
336
337 iint->flags |= IMA_AUDITED;
Tycho Andersene456ef82018-03-08 16:08:36 -0700338out:
339 kfree(hash);
340 return;
Peter Moodye7c568e2012-06-14 10:04:36 -0700341}
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300342
Mimi Zoharbc15ed62017-01-17 06:45:41 -0500343/*
344 * ima_d_path - return a pointer to the full pathname
345 *
346 * Attempt to return a pointer to the full pathname for use in the
347 * IMA measurement list, IMA audit records, and auditing logs.
348 *
349 * On failure, return a pointer to a copy of the filename, not dname.
350 * Returning a pointer to dname, could result in using the pointer
351 * after the memory has been freed.
352 */
353const char *ima_d_path(const struct path *path, char **pathbuf, char *namebuf)
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300354{
355 char *pathname = NULL;
356
Dmitry Kasatkin456f5fd2014-10-01 21:43:10 +0300357 *pathbuf = __getname();
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300358 if (*pathbuf) {
Dmitry Kasatkin17f4bad2014-08-19 16:48:39 +0300359 pathname = d_absolute_path(path, *pathbuf, PATH_MAX);
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300360 if (IS_ERR(pathname)) {
Dmitry Kasatkin456f5fd2014-10-01 21:43:10 +0300361 __putname(*pathbuf);
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300362 *pathbuf = NULL;
363 pathname = NULL;
364 }
365 }
Mimi Zoharbc15ed62017-01-17 06:45:41 -0500366
367 if (!pathname) {
368 strlcpy(namebuf, path->dentry->d_name.name, NAME_MAX);
369 pathname = namebuf;
370 }
371
372 return pathname;
Dmitry Kasatkinea1046d2012-09-04 00:40:17 +0300373}