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> |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 21 | #include "ima.h" |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 22 | |
Mimi Zohar | 523979a | 2009-02-11 11:12:28 -0500 | [diff] [blame] | 23 | static const char *IMA_TEMPLATE_NAME = "ima"; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | * ima_store_template - store ima template measurements |
| 27 | * |
| 28 | * Calculate the hash of a template entry, add the template entry |
| 29 | * to an ordered list of measurement entries maintained inside the kernel, |
| 30 | * and also update the aggregate integrity value (maintained inside the |
| 31 | * configured TPM PCR) over the hashes of the current list of measurement |
| 32 | * entries. |
| 33 | * |
| 34 | * Applications retrieve the current kernel-held measurement list through |
| 35 | * the securityfs entries in /sys/kernel/security/ima. The signed aggregate |
| 36 | * TPM PCR (called quote) can be retrieved using a TPM user space library |
| 37 | * and is used to validate the measurement list. |
| 38 | * |
| 39 | * Returns 0 on success, error code otherwise |
| 40 | */ |
| 41 | int ima_store_template(struct ima_template_entry *entry, |
| 42 | int violation, struct inode *inode) |
| 43 | { |
| 44 | const char *op = "add_template_measure"; |
| 45 | const char *audit_cause = "hashing_error"; |
| 46 | int result; |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 47 | struct { |
| 48 | struct ima_digest_data hdr; |
Mimi Zohar | 140d802 | 2013-03-11 20:29:47 -0400 | [diff] [blame^] | 49 | char digest[TPM_DIGEST_SIZE]; |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 50 | } hash; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 51 | |
| 52 | memset(entry->digest, 0, sizeof(entry->digest)); |
| 53 | entry->template_name = IMA_TEMPLATE_NAME; |
| 54 | entry->template_len = sizeof(entry->template); |
| 55 | |
| 56 | if (!violation) { |
Dmitry Kasatkin | 50af554 | 2012-05-14 14:13:56 +0300 | [diff] [blame] | 57 | result = ima_calc_buffer_hash(&entry->template, |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 58 | entry->template_len, &hash.hdr); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 59 | if (result < 0) { |
| 60 | integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, |
| 61 | entry->template_name, op, |
| 62 | audit_cause, result, 0); |
| 63 | return result; |
| 64 | } |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 65 | memcpy(entry->digest, hash.hdr.digest, hash.hdr.length); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 66 | } |
| 67 | result = ima_add_template_entry(entry, violation, op, inode); |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * ima_add_violation - add violation to measurement list. |
| 73 | * |
| 74 | * Violations are flagged in the measurement list with zero hash values. |
| 75 | * By extending the PCR with 0xFF's instead of with zeroes, the PCR |
| 76 | * value is invalidated. |
| 77 | */ |
| 78 | void ima_add_violation(struct inode *inode, const unsigned char *filename, |
| 79 | const char *op, const char *cause) |
| 80 | { |
| 81 | struct ima_template_entry *entry; |
| 82 | int violation = 1; |
| 83 | int result; |
| 84 | |
| 85 | /* can overflow, only indicator */ |
| 86 | atomic_long_inc(&ima_htable.violations); |
| 87 | |
| 88 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 89 | if (!entry) { |
| 90 | result = -ENOMEM; |
| 91 | goto err_out; |
| 92 | } |
| 93 | memset(&entry->template, 0, sizeof(entry->template)); |
| 94 | strncpy(entry->template.file_name, filename, IMA_EVENT_NAME_LEN_MAX); |
| 95 | result = ima_store_template(entry, violation, inode); |
| 96 | if (result < 0) |
| 97 | kfree(entry); |
| 98 | err_out: |
| 99 | integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename, |
| 100 | op, cause, result, 0); |
| 101 | } |
| 102 | |
| 103 | /** |
Dmitry Kasatkin | d9d300c | 2012-06-27 11:26:14 +0300 | [diff] [blame] | 104 | * ima_get_action - appraise & measure decision based on policy. |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 105 | * @inode: pointer to inode to measure |
| 106 | * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXECUTE) |
Mimi Zohar | 16cac49 | 2012-12-13 11:15:04 -0500 | [diff] [blame] | 107 | * @function: calling function (FILE_CHECK, BPRM_CHECK, MMAP_CHECK, MODULE_CHECK) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 108 | * |
| 109 | * The policy is defined in terms of keypairs: |
| 110 | * subj=, obj=, type=, func=, mask=, fsmagic= |
| 111 | * subj,obj, and type: are LSM specific. |
Mimi Zohar | 16cac49 | 2012-12-13 11:15:04 -0500 | [diff] [blame] | 112 | * func: FILE_CHECK | BPRM_CHECK | MMAP_CHECK | MODULE_CHECK |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 113 | * mask: contains the permission mask |
| 114 | * fsmagic: hex value |
| 115 | * |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 116 | * Returns IMA_MEASURE, IMA_APPRAISE mask. |
| 117 | * |
| 118 | */ |
Dmitry Kasatkin | d9d300c | 2012-06-27 11:26:14 +0300 | [diff] [blame] | 119 | int ima_get_action(struct inode *inode, int mask, int function) |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 120 | { |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 121 | int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 122 | |
| 123 | if (!ima_appraise) |
| 124 | flags &= ~IMA_APPRAISE; |
| 125 | |
| 126 | return ima_match_policy(inode, function, mask, flags); |
| 127 | } |
| 128 | |
Mimi Zohar | 1adace9 | 2011-02-22 10:19:43 -0500 | [diff] [blame] | 129 | int ima_must_measure(struct inode *inode, int mask, int function) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 130 | { |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 131 | return ima_match_policy(inode, function, mask, IMA_MEASURE); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* |
| 135 | * ima_collect_measurement - collect file measurement |
| 136 | * |
| 137 | * Calculate the file hash, if it doesn't already exist, |
| 138 | * storing the measurement and i_version in the iint. |
| 139 | * |
| 140 | * Must be called with iint->mutex held. |
| 141 | * |
| 142 | * Return 0 on success, error code otherwise |
| 143 | */ |
Mimi Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 144 | int ima_collect_measurement(struct integrity_iint_cache *iint, |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 145 | struct file *file, |
| 146 | struct evm_ima_xattr_data **xattr_value, |
| 147 | int *xattr_len) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 148 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 149 | struct inode *inode = file_inode(file); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 150 | const char *filename = file->f_dentry->d_name.name; |
| 151 | int result = 0; |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 152 | struct { |
| 153 | struct ima_digest_data hdr; |
| 154 | char digest[IMA_MAX_DIGEST_SIZE]; |
| 155 | } hash; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 156 | |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 157 | if (xattr_value) |
| 158 | *xattr_len = ima_read_xattr(file->f_dentry, xattr_value); |
| 159 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 160 | if (!(iint->flags & IMA_COLLECTED)) { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 161 | u64 i_version = file_inode(file)->i_version; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 162 | |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 163 | /* use default hash algorithm */ |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 164 | hash.hdr.algo = ima_hash_algo; |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 165 | |
| 166 | if (xattr_value) |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 167 | ima_get_hash_algo(*xattr_value, *xattr_len, &hash.hdr); |
Dmitry Kasatkin | d3634d0 | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 168 | |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 169 | result = ima_calc_file_hash(file, &hash.hdr); |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 170 | if (!result) { |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 171 | int length = sizeof(hash.hdr) + hash.hdr.length; |
| 172 | void *tmpbuf = krealloc(iint->ima_hash, length, |
| 173 | GFP_NOFS); |
| 174 | if (tmpbuf) { |
| 175 | iint->ima_hash = tmpbuf; |
| 176 | memcpy(iint->ima_hash, &hash, length); |
| 177 | iint->version = i_version; |
| 178 | iint->flags |= IMA_COLLECTED; |
| 179 | } else |
| 180 | result = -ENOMEM; |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 181 | } |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 182 | } |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 183 | if (result) |
| 184 | integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, |
| 185 | filename, "collect_data", "failed", |
| 186 | result, 0); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 187 | return result; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * ima_store_measurement - store file measurement |
| 192 | * |
| 193 | * Create an "ima" template and then store the template by calling |
| 194 | * ima_store_template. |
| 195 | * |
| 196 | * We only get here if the inode has not already been measured, |
| 197 | * but the measurement could already exist: |
| 198 | * - multiple copies of the same file on either the same or |
| 199 | * different filesystems. |
| 200 | * - the inode was previously flushed as well as the iint info, |
| 201 | * containing the hashing info. |
| 202 | * |
| 203 | * Must be called with iint->mutex held. |
| 204 | */ |
Mimi Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 205 | void ima_store_measurement(struct integrity_iint_cache *iint, |
| 206 | struct file *file, const unsigned char *filename) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 207 | { |
| 208 | const char *op = "add_template_measure"; |
| 209 | const char *audit_cause = "ENOMEM"; |
| 210 | int result = -ENOMEM; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 211 | struct inode *inode = file_inode(file); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 212 | struct ima_template_entry *entry; |
| 213 | int violation = 0; |
| 214 | |
Mimi Zohar | 2fe5d6d | 2012-02-13 10:15:05 -0500 | [diff] [blame] | 215 | if (iint->flags & IMA_MEASURED) |
| 216 | return; |
| 217 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 218 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 219 | if (!entry) { |
| 220 | integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename, |
| 221 | op, audit_cause, result, 0); |
| 222 | return; |
| 223 | } |
| 224 | memset(&entry->template, 0, sizeof(entry->template)); |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 225 | if (iint->ima_hash->algo != ima_hash_algo) { |
| 226 | struct { |
| 227 | struct ima_digest_data hdr; |
| 228 | char digest[IMA_MAX_DIGEST_SIZE]; |
| 229 | } hash; |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 230 | |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 231 | hash.hdr.algo = ima_hash_algo; |
| 232 | result = ima_calc_file_hash(file, &hash.hdr); |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 233 | if (result) |
| 234 | integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, |
| 235 | filename, "collect_data", "failed", |
| 236 | result, 0); |
| 237 | else |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 238 | memcpy(entry->template.digest, hash.hdr.digest, |
| 239 | hash.hdr.length); |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 240 | } else |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 241 | memcpy(entry->template.digest, iint->ima_hash->digest, |
| 242 | iint->ima_hash->length); |
Mimi Zohar | 08e1b76 | 2012-06-20 09:32:55 -0400 | [diff] [blame] | 243 | strcpy(entry->template.file_name, |
| 244 | (strlen(filename) > IMA_EVENT_NAME_LEN_MAX) ? |
| 245 | file->f_dentry->d_name.name : filename); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 246 | |
| 247 | result = ima_store_template(entry, violation, inode); |
Roberto Sassu | 45fae74 | 2011-12-19 15:57:27 +0100 | [diff] [blame] | 248 | if (!result || result == -EEXIST) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 249 | iint->flags |= IMA_MEASURED; |
Roberto Sassu | 45fae74 | 2011-12-19 15:57:27 +0100 | [diff] [blame] | 250 | if (result < 0) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 251 | kfree(entry); |
| 252 | } |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 253 | |
| 254 | void ima_audit_measurement(struct integrity_iint_cache *iint, |
| 255 | const unsigned char *filename) |
| 256 | { |
| 257 | struct audit_buffer *ab; |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 258 | char hash[(iint->ima_hash->length * 2) + 1]; |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 259 | int i; |
| 260 | |
| 261 | if (iint->flags & IMA_AUDITED) |
| 262 | return; |
| 263 | |
Dmitry Kasatkin | a35c3fb | 2013-04-25 10:44:04 +0300 | [diff] [blame] | 264 | for (i = 0; i < iint->ima_hash->length; i++) |
| 265 | hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]); |
Peter Moody | e7c568e | 2012-06-14 10:04:36 -0700 | [diff] [blame] | 266 | hash[i * 2] = '\0'; |
| 267 | |
| 268 | ab = audit_log_start(current->audit_context, GFP_KERNEL, |
| 269 | AUDIT_INTEGRITY_RULE); |
| 270 | if (!ab) |
| 271 | return; |
| 272 | |
| 273 | audit_log_format(ab, "file="); |
| 274 | audit_log_untrustedstring(ab, filename); |
| 275 | audit_log_format(ab, " hash="); |
| 276 | audit_log_untrustedstring(ab, hash); |
| 277 | |
| 278 | audit_log_task_info(ab, current); |
| 279 | audit_log_end(ab); |
| 280 | |
| 281 | iint->flags |= IMA_AUDITED; |
| 282 | } |
Dmitry Kasatkin | ea1046d | 2012-09-04 00:40:17 +0300 | [diff] [blame] | 283 | |
| 284 | const char *ima_d_path(struct path *path, char **pathbuf) |
| 285 | { |
| 286 | char *pathname = NULL; |
| 287 | |
| 288 | /* We will allow 11 spaces for ' (deleted)' to be appended */ |
| 289 | *pathbuf = kmalloc(PATH_MAX + 11, GFP_KERNEL); |
| 290 | if (*pathbuf) { |
| 291 | pathname = d_path(path, *pathbuf, PATH_MAX + 11); |
| 292 | if (IS_ERR(pathname)) { |
| 293 | kfree(*pathbuf); |
| 294 | *pathbuf = NULL; |
| 295 | pathname = NULL; |
| 296 | } |
| 297 | } |
| 298 | return pathname; |
| 299 | } |