Thomas Gleixner | b886d83 | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2008 IBM Corporation |
| 4 | * |
| 5 | * Author: Mimi Zohar <zohar@us.ibm.com> |
| 6 | * |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 7 | * File: ima_api.c |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 8 | * Implements must_appraise_or_measure, collect_measurement, |
| 9 | * appraise_measurement, store_measurement and store_template. |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 10 | */ |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 12 | #include <linux/file.h> |
| 13 | #include <linux/fs.h> |
| 14 | #include <linux/xattr.h> |
| 15 | #include <linux/evm.h> |
Jeff Layton | 3b370b2 | 2017-12-11 06:35:21 -0500 | [diff] [blame] | 16 | #include <linux/iversion.h> |
Dmitry Kasatkin | 1525b06 | 2014-10-30 12:39:39 +0200 | [diff] [blame] | 17 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 18 | #include "ima.h" |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 19 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 20 | /* |
Roberto Sassu | a7ed7c6 | 2013-12-02 19:40:34 +0100 | [diff] [blame] | 21 | * ima_free_template_entry - free an existing template entry |
| 22 | */ |
| 23 | void 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 Sassu | 7bc5f44 | 2013-06-07 12:16:28 +0200 | [diff] [blame] | 34 | * ima_alloc_init_template - create and initialize a new template entry |
| 35 | */ |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 36 | int ima_alloc_init_template(struct ima_event_data *event_data, |
| 37 | struct ima_template_entry **entry) |
Roberto Sassu | 7bc5f44 | 2013-06-07 12:16:28 +0200 | [diff] [blame] | 38 | { |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 39 | struct ima_template_desc *template_desc = ima_template_desc_current(); |
| 40 | int i, result = 0; |
Roberto Sassu | 7bc5f44 | 2013-06-07 12:16:28 +0200 | [diff] [blame] | 41 | |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 42 | *entry = kzalloc(sizeof(**entry) + template_desc->num_fields * |
| 43 | sizeof(struct ima_field_data), GFP_NOFS); |
| 44 | if (!*entry) |
Roberto Sassu | 7bc5f44 | 2013-06-07 12:16:28 +0200 | [diff] [blame] | 45 | return -ENOMEM; |
| 46 | |
Roberto Sassu | a7ed7c6 | 2013-12-02 19:40:34 +0100 | [diff] [blame] | 47 | (*entry)->template_desc = template_desc; |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 48 | for (i = 0; i < template_desc->num_fields; i++) { |
Eric Biggers | b2724d5 | 2018-09-07 13:22:23 -0700 | [diff] [blame] | 49 | const struct ima_template_field *field = |
| 50 | template_desc->fields[i]; |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 51 | u32 len; |
Roberto Sassu | 7bc5f44 | 2013-06-07 12:16:28 +0200 | [diff] [blame] | 52 | |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 53 | result = field->field_init(event_data, |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 54 | &((*entry)->template_data[i])); |
| 55 | if (result != 0) |
| 56 | goto out; |
Roberto Sassu | 7bc5f44 | 2013-06-07 12:16:28 +0200 | [diff] [blame] | 57 | |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 58 | len = (*entry)->template_data[i].len; |
| 59 | (*entry)->template_data_len += sizeof(len); |
| 60 | (*entry)->template_data_len += len; |
| 61 | } |
Roberto Sassu | 7bc5f44 | 2013-06-07 12:16:28 +0200 | [diff] [blame] | 62 | return 0; |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 63 | out: |
Roberto Sassu | a7ed7c6 | 2013-12-02 19:40:34 +0100 | [diff] [blame] | 64 | ima_free_template_entry(*entry); |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 65 | *entry = NULL; |
Roberto Sassu | 7bc5f44 | 2013-06-07 12:16:28 +0200 | [diff] [blame] | 66 | return result; |
| 67 | } |
| 68 | |
| 69 | /* |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 70 | * 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 | */ |
| 85 | int ima_store_template(struct ima_template_entry *entry, |
Roberto Sassu | 9803d41 | 2013-06-07 12:16:27 +0200 | [diff] [blame] | 86 | int violation, struct inode *inode, |
Eric Richter | 14b1da8 | 2016-06-01 13:14:03 -0500 | [diff] [blame] | 87 | const unsigned char *filename, int pcr) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 88 | { |
Mimi Zohar | 52a1328 | 2013-12-11 14:44:04 -0500 | [diff] [blame] | 89 | static const char op[] = "add_template_measure"; |
| 90 | static const char audit_cause[] = "hashing_error"; |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 91 | char *template_name = entry->template_desc->name; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 92 | int result; |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 93 | struct { |
| 94 | struct ima_digest_data hdr; |
Mimi Zohar | 140d802 | 2013-03-11 20:29:47 -0400 | [diff] [blame] | 95 | char digest[TPM_DIGEST_SIZE]; |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 96 | } hash; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 97 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 98 | if (!violation) { |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 99 | int num_fields = entry->template_desc->num_fields; |
| 100 | |
Dmitry Kasatkin | ea59399 | 2013-06-07 12:16:24 +0200 | [diff] [blame] | 101 | /* this function uses default algo */ |
| 102 | hash.hdr.algo = HASH_ALGO_SHA1; |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 103 | result = ima_calc_field_array_hash(&entry->template_data[0], |
Roberto Sassu | b6f8f16 | 2013-11-08 19:21:39 +0100 | [diff] [blame] | 104 | entry->template_desc, |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 105 | num_fields, &hash.hdr); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 106 | if (result < 0) { |
| 107 | integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, |
Roberto Sassu | a71dc65 | 2013-06-07 12:16:33 +0200 | [diff] [blame] | 108 | template_name, op, |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 109 | audit_cause, result, 0); |
| 110 | return result; |
| 111 | } |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 112 | memcpy(entry->digest, hash.hdr.digest, hash.hdr.length); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 113 | } |
Eric Richter | 14b1da8 | 2016-06-01 13:14:03 -0500 | [diff] [blame] | 114 | entry->pcr = pcr; |
Roberto Sassu | 9803d41 | 2013-06-07 12:16:27 +0200 | [diff] [blame] | 115 | result = ima_add_template_entry(entry, violation, op, inode, filename); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 116 | 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 Sassu | 7d802a2 | 2013-06-07 12:16:26 +0200 | [diff] [blame] | 126 | void ima_add_violation(struct file *file, const unsigned char *filename, |
Roberto Sassu | 8d94eb9 | 2015-04-11 17:12:39 +0200 | [diff] [blame] | 127 | struct integrity_iint_cache *iint, |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 128 | const char *op, const char *cause) |
| 129 | { |
| 130 | struct ima_template_entry *entry; |
Libo Chen | 31d4b76 | 2013-12-11 14:11:28 +0800 | [diff] [blame] | 131 | struct inode *inode = file_inode(file); |
Roberto Sassu | 8d94eb9 | 2015-04-11 17:12:39 +0200 | [diff] [blame] | 132 | struct ima_event_data event_data = {iint, file, filename, NULL, 0, |
| 133 | cause}; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 134 | int violation = 1; |
| 135 | int result; |
| 136 | |
| 137 | /* can overflow, only indicator */ |
| 138 | atomic_long_inc(&ima_htable.violations); |
| 139 | |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 140 | result = ima_alloc_init_template(&event_data, &entry); |
Roberto Sassu | 7bc5f44 | 2013-06-07 12:16:28 +0200 | [diff] [blame] | 141 | if (result < 0) { |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 142 | result = -ENOMEM; |
| 143 | goto err_out; |
| 144 | } |
Eric Richter | 14b1da8 | 2016-06-01 13:14:03 -0500 | [diff] [blame] | 145 | result = ima_store_template(entry, violation, inode, |
| 146 | filename, CONFIG_IMA_MEASURE_PCR_IDX); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 147 | if (result < 0) |
Roberto Sassu | a7ed7c6 | 2013-12-02 19:40:34 +0100 | [diff] [blame] | 148 | ima_free_template_entry(entry); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 149 | err_out: |
| 150 | integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename, |
| 151 | op, cause, result, 0); |
| 152 | } |
| 153 | |
| 154 | /** |
Dmitry Kasatkin | d9d300c | 2012-06-27 11:26:14 +0300 | [diff] [blame] | 155 | * ima_get_action - appraise & measure decision based on policy. |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 156 | * @inode: pointer to inode to measure |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 157 | * @cred: pointer to credentials structure to validate |
| 158 | * @secid: secid of the task being validated |
Lans Zhang | 20f482a | 2017-01-06 12:38:11 +0800 | [diff] [blame] | 159 | * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXEC, |
| 160 | * MAY_APPEND) |
Mimi Zohar | 4ad87a3 | 2016-01-14 20:59:14 -0500 | [diff] [blame] | 161 | * @func: caller identifier |
Eric Richter | 725de7f | 2016-06-01 13:14:02 -0500 | [diff] [blame] | 162 | * @pcr: pointer filled in if matched measure policy sets pcr= |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 163 | * |
| 164 | * The policy is defined in terms of keypairs: |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 165 | * subj=, obj=, type=, func=, mask=, fsmagic= |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 166 | * subj,obj, and type: are LSM specific. |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 167 | * func: FILE_CHECK | BPRM_CHECK | CREDS_CHECK | MMAP_CHECK | MODULE_CHECK |
Dmitry Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 168 | * mask: contains the permission mask |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 169 | * fsmagic: hex value |
| 170 | * |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 171 | * Returns IMA_MEASURE, IMA_APPRAISE mask. |
| 172 | * |
| 173 | */ |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 174 | int ima_get_action(struct inode *inode, const struct cred *cred, u32 secid, |
| 175 | int mask, enum ima_hooks func, int *pcr) |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 176 | { |
Mimi Zohar | da1b002 | 2016-09-29 10:04:52 -0400 | [diff] [blame] | 177 | int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE | IMA_HASH; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 178 | |
Dmitry Kasatkin | fd5f4e90 | 2014-11-05 17:01:14 +0200 | [diff] [blame] | 179 | flags &= ima_policy_flag; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 180 | |
Matthew Garrett | d906c10 | 2018-01-08 13:36:20 -0800 | [diff] [blame] | 181 | return ima_match_policy(inode, cred, secid, func, mask, flags, pcr); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 182 | } |
| 183 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 184 | /* |
| 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 Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 194 | int ima_collect_measurement(struct integrity_iint_cache *iint, |
Mimi Zohar | cf22221 | 2016-01-14 17:57:47 -0500 | [diff] [blame] | 195 | struct file *file, void *buf, loff_t size, |
| 196 | enum hash_algo algo) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 197 | { |
Mimi Zohar | f9b2a73 | 2014-05-12 09:28:11 -0400 | [diff] [blame] | 198 | const char *audit_cause = "failed"; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 199 | struct inode *inode = file_inode(file); |
Al Viro | b583043 | 2014-10-31 01:22:04 -0400 | [diff] [blame] | 200 | const char *filename = file->f_path.dentry->d_name.name; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 201 | int result = 0; |
Mimi Zohar | f3cc6b2 | 2017-06-17 23:56:23 -0400 | [diff] [blame] | 202 | int length; |
| 203 | void *tmpbuf; |
| 204 | u64 i_version; |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 205 | struct { |
| 206 | struct ima_digest_data hdr; |
| 207 | char digest[IMA_MAX_DIGEST_SIZE]; |
| 208 | } hash; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 209 | |
Mimi Zohar | f3cc6b2 | 2017-06-17 23:56:23 -0400 | [diff] [blame] | 210 | if (iint->flags & IMA_COLLECTED) |
| 211 | goto out; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 212 | |
Mimi Zohar | f3cc6b2 | 2017-06-17 23:56:23 -0400 | [diff] [blame] | 213 | /* |
| 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 Layton | 3b370b2 | 2017-12-11 06:35:21 -0500 | [diff] [blame] | 218 | i_version = inode_query_iversion(inode); |
Mimi Zohar | f3cc6b2 | 2017-06-17 23:56:23 -0400 | [diff] [blame] | 219 | hash.hdr.algo = algo; |
Mimi Zohar | f9b2a73 | 2014-05-12 09:28:11 -0400 | [diff] [blame] | 220 | |
Mimi Zohar | f3cc6b2 | 2017-06-17 23:56:23 -0400 | [diff] [blame] | 221 | /* Initialize hash digest to 0's in case of failure */ |
| 222 | memset(&hash.digest, 0, sizeof(hash.digest)); |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 223 | |
Mimi Zohar | f3cc6b2 | 2017-06-17 23:56:23 -0400 | [diff] [blame] | 224 | 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 Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 237 | } |
Mimi Zohar | f3cc6b2 | 2017-06-17 23:56:23 -0400 | [diff] [blame] | 238 | |
| 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 Zohar | f9b2a73 | 2014-05-12 09:28:11 -0400 | [diff] [blame] | 246 | out: |
Mimi Zohar | f3cc6b2 | 2017-06-17 23:56:23 -0400 | [diff] [blame] | 247 | if (result) { |
| 248 | if (file->f_flags & O_DIRECT) |
| 249 | audit_cause = "failed(directio)"; |
| 250 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 251 | integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, |
Mimi Zohar | f9b2a73 | 2014-05-12 09:28:11 -0400 | [diff] [blame] | 252 | filename, "collect_data", audit_cause, |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 253 | result, 0); |
Mimi Zohar | f3cc6b2 | 2017-06-17 23:56:23 -0400 | [diff] [blame] | 254 | } |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 255 | 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 Kasatkin | 2bb930a | 2014-03-04 18:04:20 +0200 | [diff] [blame] | 266 | * - multiple copies of the same file on either the same or |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 267 | * 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 Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 273 | void ima_store_measurement(struct integrity_iint_cache *iint, |
Mimi Zohar | bcbc9b0c | 2013-07-23 11:15:00 -0400 | [diff] [blame] | 274 | struct file *file, const unsigned char *filename, |
| 275 | struct evm_ima_xattr_data *xattr_value, |
Eric Richter | 14b1da8 | 2016-06-01 13:14:03 -0500 | [diff] [blame] | 276 | int xattr_len, int pcr) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 277 | { |
Mimi Zohar | 52a1328 | 2013-12-11 14:44:04 -0500 | [diff] [blame] | 278 | static const char op[] = "add_template_measure"; |
| 279 | static const char audit_cause[] = "ENOMEM"; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 280 | int result = -ENOMEM; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 281 | struct inode *inode = file_inode(file); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 282 | struct ima_template_entry *entry; |
Roberto Sassu | 8d94eb9 | 2015-04-11 17:12:39 +0200 | [diff] [blame] | 283 | struct ima_event_data event_data = {iint, file, filename, xattr_value, |
| 284 | xattr_len, NULL}; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 285 | int violation = 0; |
| 286 | |
Eric Richter | a422638 | 2016-06-01 13:14:06 -0500 | [diff] [blame] | 287 | if (iint->measured_pcrs & (0x1 << pcr)) |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 288 | return; |
| 289 | |
Roberto Sassu | 23b5741 | 2015-04-11 17:09:50 +0200 | [diff] [blame] | 290 | result = ima_alloc_init_template(&event_data, &entry); |
Roberto Sassu | 7bc5f44 | 2013-06-07 12:16:28 +0200 | [diff] [blame] | 291 | if (result < 0) { |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 292 | integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename, |
| 293 | op, audit_cause, result, 0); |
| 294 | return; |
| 295 | } |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 296 | |
Eric Richter | 14b1da8 | 2016-06-01 13:14:03 -0500 | [diff] [blame] | 297 | result = ima_store_template(entry, violation, inode, filename, pcr); |
Mimi Zohar | f3cc6b2 | 2017-06-17 23:56:23 -0400 | [diff] [blame] | 298 | if ((!result || result == -EEXIST) && !(file->f_flags & O_DIRECT)) { |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 299 | iint->flags |= IMA_MEASURED; |
Eric Richter | a422638 | 2016-06-01 13:14:06 -0500 | [diff] [blame] | 300 | iint->measured_pcrs |= (0x1 << pcr); |
| 301 | } |
Roberto Sassu | 45fae74 | 2011-12-19 15:57:27 +0100 | [diff] [blame] | 302 | if (result < 0) |
Roberto Sassu | a7ed7c6 | 2013-12-02 19:40:34 +0100 | [diff] [blame] | 303 | ima_free_template_entry(entry); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 304 | } |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 305 | |
| 306 | void ima_audit_measurement(struct integrity_iint_cache *iint, |
| 307 | const unsigned char *filename) |
| 308 | { |
| 309 | struct audit_buffer *ab; |
Tycho Andersen | e456ef8 | 2018-03-08 16:08:36 -0700 | [diff] [blame] | 310 | char *hash; |
Mimi Zohar | 5278aa5 | 2013-06-07 12:16:38 +0200 | [diff] [blame] | 311 | const char *algo_name = hash_algo_name[iint->ima_hash->algo]; |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 312 | int i; |
| 313 | |
| 314 | if (iint->flags & IMA_AUDITED) |
| 315 | return; |
| 316 | |
Tycho Andersen | e456ef8 | 2018-03-08 16:08:36 -0700 | [diff] [blame] | 317 | hash = kzalloc((iint->ima_hash->length * 2) + 1, GFP_KERNEL); |
| 318 | if (!hash) |
| 319 | return; |
| 320 | |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 321 | for (i = 0; i < iint->ima_hash->length; i++) |
| 322 | hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]); |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 323 | hash[i * 2] = '\0'; |
| 324 | |
Richard Guy Briggs | cdfb6b3 | 2018-05-12 21:58:20 -0400 | [diff] [blame] | 325 | ab = audit_log_start(audit_context(), GFP_KERNEL, |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 326 | AUDIT_INTEGRITY_RULE); |
| 327 | if (!ab) |
Tycho Andersen | e456ef8 | 2018-03-08 16:08:36 -0700 | [diff] [blame] | 328 | goto out; |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 329 | |
| 330 | audit_log_format(ab, "file="); |
| 331 | audit_log_untrustedstring(ab, filename); |
Tycho Andersen | e456ef8 | 2018-03-08 16:08:36 -0700 | [diff] [blame] | 332 | audit_log_format(ab, " hash=\"%s:%s\"", algo_name, hash); |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 333 | |
Paul Moore | 2a1fe21 | 2018-11-26 18:40:07 -0500 | [diff] [blame] | 334 | audit_log_task_info(ab); |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 335 | audit_log_end(ab); |
| 336 | |
| 337 | iint->flags |= IMA_AUDITED; |
Tycho Andersen | e456ef8 | 2018-03-08 16:08:36 -0700 | [diff] [blame] | 338 | out: |
| 339 | kfree(hash); |
| 340 | return; |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 341 | } |
Dmitry Kasatkin | ea1046d | 2012-09-04 00:40:17 +0300 | [diff] [blame] | 342 | |
Mimi Zohar | bc15ed6 | 2017-01-17 06:45:41 -0500 | [diff] [blame] | 343 | /* |
| 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 | */ |
| 353 | const char *ima_d_path(const struct path *path, char **pathbuf, char *namebuf) |
Dmitry Kasatkin | ea1046d | 2012-09-04 00:40:17 +0300 | [diff] [blame] | 354 | { |
| 355 | char *pathname = NULL; |
| 356 | |
Dmitry Kasatkin | 456f5fd | 2014-10-01 21:43:10 +0300 | [diff] [blame] | 357 | *pathbuf = __getname(); |
Dmitry Kasatkin | ea1046d | 2012-09-04 00:40:17 +0300 | [diff] [blame] | 358 | if (*pathbuf) { |
Dmitry Kasatkin | 17f4bad | 2014-08-19 16:48:39 +0300 | [diff] [blame] | 359 | pathname = d_absolute_path(path, *pathbuf, PATH_MAX); |
Dmitry Kasatkin | ea1046d | 2012-09-04 00:40:17 +0300 | [diff] [blame] | 360 | if (IS_ERR(pathname)) { |
Dmitry Kasatkin | 456f5fd | 2014-10-01 21:43:10 +0300 | [diff] [blame] | 361 | __putname(*pathbuf); |
Dmitry Kasatkin | ea1046d | 2012-09-04 00:40:17 +0300 | [diff] [blame] | 362 | *pathbuf = NULL; |
| 363 | pathname = NULL; |
| 364 | } |
| 365 | } |
Mimi Zohar | bc15ed6 | 2017-01-17 06:45:41 -0500 | [diff] [blame] | 366 | |
| 367 | if (!pathname) { |
| 368 | strlcpy(namebuf, path->dentry->d_name.name, NAME_MAX); |
| 369 | pathname = namebuf; |
| 370 | } |
| 371 | |
| 372 | return pathname; |
Dmitry Kasatkin | ea1046d | 2012-09-04 00:40:17 +0300 | [diff] [blame] | 373 | } |