Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 1 | /* |
| 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 Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 12 | * Implements must_appraise_or_measure, collect_measurement, |
| 13 | * appraise_measurement, store_measurement and store_template. |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 14 | */ |
| 15 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 17 | #include <linux/file.h> |
| 18 | #include <linux/fs.h> |
| 19 | #include <linux/xattr.h> |
| 20 | #include <linux/evm.h> |
Dmitry Kasatkin | ea59399 | 2013-06-07 12:16:24 +0200 | [diff] [blame] | 21 | #include <crypto/hash_info.h> |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 22 | #include "ima.h" |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 23 | |
Mimi Zohar | 523979a | 2009-02-11 11:12:28 -0500 | [diff] [blame] | 24 | static const char *IMA_TEMPLATE_NAME = "ima"; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 25 | |
| 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 | */ |
| 42 | int ima_store_template(struct ima_template_entry *entry, |
Roberto Sassu | 9803d41 | 2013-06-07 12:16:27 +0200 | [diff] [blame^] | 43 | int violation, struct inode *inode, |
| 44 | const unsigned char *filename) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 45 | { |
| 46 | const char *op = "add_template_measure"; |
| 47 | const char *audit_cause = "hashing_error"; |
| 48 | int result; |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 49 | struct { |
| 50 | struct ima_digest_data hdr; |
Mimi Zohar | 140d802 | 2013-03-11 20:29:47 -0400 | [diff] [blame] | 51 | char digest[TPM_DIGEST_SIZE]; |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 52 | } hash; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 53 | |
| 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 Kasatkin | ea59399 | 2013-06-07 12:16:24 +0200 | [diff] [blame] | 59 | /* this function uses default algo */ |
| 60 | hash.hdr.algo = HASH_ALGO_SHA1; |
Dmitry Kasatkin | 50af554 | 2012-05-14 14:13:56 +0300 | [diff] [blame] | 61 | result = ima_calc_buffer_hash(&entry->template, |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 62 | entry->template_len, &hash.hdr); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 63 | 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 Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 69 | memcpy(entry->digest, hash.hdr.digest, hash.hdr.length); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 70 | } |
Roberto Sassu | 9803d41 | 2013-06-07 12:16:27 +0200 | [diff] [blame^] | 71 | result = ima_add_template_entry(entry, violation, op, inode, filename); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 72 | 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 Sassu | 7d802a2 | 2013-06-07 12:16:26 +0200 | [diff] [blame] | 82 | void ima_add_violation(struct file *file, const unsigned char *filename, |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 83 | const char *op, const char *cause) |
| 84 | { |
| 85 | struct ima_template_entry *entry; |
Roberto Sassu | 7d802a2 | 2013-06-07 12:16:26 +0200 | [diff] [blame] | 86 | struct inode *inode = file->f_dentry->d_inode; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 87 | 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 Sassu | 9803d41 | 2013-06-07 12:16:27 +0200 | [diff] [blame^] | 100 | result = ima_store_template(entry, violation, inode, filename); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 101 | if (result < 0) |
| 102 | kfree(entry); |
| 103 | err_out: |
| 104 | integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename, |
| 105 | op, cause, result, 0); |
| 106 | } |
| 107 | |
| 108 | /** |
Dmitry Kasatkin | d9d300c | 2012-06-27 11:26:14 +0300 | [diff] [blame] | 109 | * ima_get_action - appraise & measure decision based on policy. |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 110 | * @inode: pointer to inode to measure |
| 111 | * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXECUTE) |
Mimi Zohar | 16cac49 | 2012-12-13 11:15:04 -0500 | [diff] [blame] | 112 | * @function: calling function (FILE_CHECK, BPRM_CHECK, MMAP_CHECK, MODULE_CHECK) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 113 | * |
| 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 Zohar | 16cac49 | 2012-12-13 11:15:04 -0500 | [diff] [blame] | 117 | * func: FILE_CHECK | BPRM_CHECK | MMAP_CHECK | MODULE_CHECK |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 118 | * mask: contains the permission mask |
| 119 | * fsmagic: hex value |
| 120 | * |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 121 | * Returns IMA_MEASURE, IMA_APPRAISE mask. |
| 122 | * |
| 123 | */ |
Dmitry Kasatkin | d9d300c | 2012-06-27 11:26:14 +0300 | [diff] [blame] | 124 | int ima_get_action(struct inode *inode, int mask, int function) |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 125 | { |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 126 | int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 127 | |
| 128 | if (!ima_appraise) |
| 129 | flags &= ~IMA_APPRAISE; |
| 130 | |
| 131 | return ima_match_policy(inode, function, mask, flags); |
| 132 | } |
| 133 | |
Mimi Zohar | 1adace9 | 2011-02-22 10:19:43 -0500 | [diff] [blame] | 134 | int ima_must_measure(struct inode *inode, int mask, int function) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 135 | { |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 136 | return ima_match_policy(inode, function, mask, IMA_MEASURE); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 137 | } |
| 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 Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 149 | int ima_collect_measurement(struct integrity_iint_cache *iint, |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 150 | struct file *file, |
| 151 | struct evm_ima_xattr_data **xattr_value, |
| 152 | int *xattr_len) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 153 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 154 | struct inode *inode = file_inode(file); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 155 | const char *filename = file->f_dentry->d_name.name; |
| 156 | int result = 0; |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 157 | struct { |
| 158 | struct ima_digest_data hdr; |
| 159 | char digest[IMA_MAX_DIGEST_SIZE]; |
| 160 | } hash; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 161 | |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 162 | if (xattr_value) |
| 163 | *xattr_len = ima_read_xattr(file->f_dentry, xattr_value); |
| 164 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 165 | if (!(iint->flags & IMA_COLLECTED)) { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 166 | u64 i_version = file_inode(file)->i_version; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 167 | |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 168 | /* use default hash algorithm */ |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 169 | hash.hdr.algo = ima_hash_algo; |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 170 | |
| 171 | if (xattr_value) |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 172 | ima_get_hash_algo(*xattr_value, *xattr_len, &hash.hdr); |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 173 | |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 174 | result = ima_calc_file_hash(file, &hash.hdr); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 175 | if (!result) { |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 176 | 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 Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 186 | } |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 187 | } |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 188 | if (result) |
| 189 | integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, |
| 190 | filename, "collect_data", "failed", |
| 191 | result, 0); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 192 | 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 Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 210 | void ima_store_measurement(struct integrity_iint_cache *iint, |
| 211 | struct file *file, const unsigned char *filename) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 212 | { |
| 213 | const char *op = "add_template_measure"; |
| 214 | const char *audit_cause = "ENOMEM"; |
| 215 | int result = -ENOMEM; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 216 | struct inode *inode = file_inode(file); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 217 | struct ima_template_entry *entry; |
| 218 | int violation = 0; |
| 219 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 220 | if (iint->flags & IMA_MEASURED) |
| 221 | return; |
| 222 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 223 | 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 Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 230 | 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 Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 235 | |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 236 | hash.hdr.algo = ima_hash_algo; |
| 237 | result = ima_calc_file_hash(file, &hash.hdr); |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 238 | if (result) |
| 239 | integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, |
| 240 | filename, "collect_data", "failed", |
| 241 | result, 0); |
| 242 | else |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 243 | memcpy(entry->template.digest, hash.hdr.digest, |
| 244 | hash.hdr.length); |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 245 | } else |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 246 | memcpy(entry->template.digest, iint->ima_hash->digest, |
| 247 | iint->ima_hash->length); |
Mimi Zohar | 08e1b76 | 2012-06-20 09:32:55 -0400 | [diff] [blame] | 248 | strcpy(entry->template.file_name, |
| 249 | (strlen(filename) > IMA_EVENT_NAME_LEN_MAX) ? |
| 250 | file->f_dentry->d_name.name : filename); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 251 | |
Roberto Sassu | 9803d41 | 2013-06-07 12:16:27 +0200 | [diff] [blame^] | 252 | result = ima_store_template(entry, violation, inode, filename); |
Roberto Sassu | 45fae74 | 2011-12-19 15:57:27 +0100 | [diff] [blame] | 253 | if (!result || result == -EEXIST) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 254 | iint->flags |= IMA_MEASURED; |
Roberto Sassu | 45fae74 | 2011-12-19 15:57:27 +0100 | [diff] [blame] | 255 | if (result < 0) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 256 | kfree(entry); |
| 257 | } |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 258 | |
| 259 | void ima_audit_measurement(struct integrity_iint_cache *iint, |
| 260 | const unsigned char *filename) |
| 261 | { |
| 262 | struct audit_buffer *ab; |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 263 | char hash[(iint->ima_hash->length * 2) + 1]; |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 264 | int i; |
| 265 | |
| 266 | if (iint->flags & IMA_AUDITED) |
| 267 | return; |
| 268 | |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 269 | for (i = 0; i < iint->ima_hash->length; i++) |
| 270 | hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]); |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 271 | 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 Kasatkin | ea1046d | 2012-09-04 00:40:17 +0300 | [diff] [blame] | 288 | |
| 289 | const 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 | } |