Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005,2006,2007,2008 IBM Corporation |
| 3 | * |
| 4 | * Authors: |
| 5 | * Reiner Sailer <sailer@watson.ibm.com> |
| 6 | * Serge Hallyn <serue@us.ibm.com> |
| 7 | * Kylene Hall <kylene@us.ibm.com> |
| 8 | * Mimi Zohar <zohar@us.ibm.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation, version 2 of the |
| 13 | * License. |
| 14 | * |
| 15 | * File: ima_main.c |
Eric Paris | e0d5bd2 | 2009-12-04 15:48:00 -0500 | [diff] [blame] | 16 | * implements the IMA hooks: ima_bprm_check, ima_file_mmap, |
Mimi Zohar | 9bbb6ca | 2010-01-26 17:02:40 -0500 | [diff] [blame] | 17 | * and ima_file_check. |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 18 | */ |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/file.h> |
| 21 | #include <linux/binfmts.h> |
| 22 | #include <linux/mount.h> |
| 23 | #include <linux/mman.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 25 | |
| 26 | #include "ima.h" |
| 27 | |
| 28 | int ima_initialized; |
| 29 | |
| 30 | char *ima_hash = "sha1"; |
| 31 | static int __init hash_setup(char *str) |
| 32 | { |
Mimi Zohar | 07ff7a0 | 2009-05-05 13:13:10 -0400 | [diff] [blame] | 33 | if (strncmp(str, "md5", 3) == 0) |
| 34 | ima_hash = "md5"; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 35 | return 1; |
| 36 | } |
| 37 | __setup("ima_hash=", hash_setup); |
| 38 | |
Mimi Zohar | d162543 | 2009-12-04 15:48:40 -0500 | [diff] [blame] | 39 | struct ima_imbalance { |
| 40 | struct hlist_node node; |
| 41 | unsigned long fsmagic; |
| 42 | }; |
| 43 | |
| 44 | /* |
| 45 | * ima_limit_imbalance - emit one imbalance message per filesystem type |
| 46 | * |
| 47 | * Maintain list of filesystem types that do not measure files properly. |
| 48 | * Return false if unknown, true if known. |
| 49 | */ |
| 50 | static bool ima_limit_imbalance(struct file *file) |
| 51 | { |
| 52 | static DEFINE_SPINLOCK(ima_imbalance_lock); |
| 53 | static HLIST_HEAD(ima_imbalance_list); |
| 54 | |
| 55 | struct super_block *sb = file->f_dentry->d_sb; |
| 56 | struct ima_imbalance *entry; |
| 57 | struct hlist_node *node; |
| 58 | bool found = false; |
| 59 | |
| 60 | rcu_read_lock(); |
| 61 | hlist_for_each_entry_rcu(entry, node, &ima_imbalance_list, node) { |
| 62 | if (entry->fsmagic == sb->s_magic) { |
| 63 | found = true; |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | rcu_read_unlock(); |
| 68 | if (found) |
| 69 | goto out; |
| 70 | |
| 71 | entry = kmalloc(sizeof(*entry), GFP_NOFS); |
| 72 | if (!entry) |
| 73 | goto out; |
| 74 | entry->fsmagic = sb->s_magic; |
| 75 | spin_lock(&ima_imbalance_lock); |
| 76 | /* |
| 77 | * we could have raced and something else might have added this fs |
| 78 | * to the list, but we don't really care |
| 79 | */ |
| 80 | hlist_add_head_rcu(&entry->node, &ima_imbalance_list); |
| 81 | spin_unlock(&ima_imbalance_lock); |
| 82 | printk(KERN_INFO "IMA: unmeasured files on fsmagic: %lX\n", |
| 83 | entry->fsmagic); |
| 84 | out: |
| 85 | return found; |
| 86 | } |
| 87 | |
Eric Paris | e0d5bd2 | 2009-12-04 15:48:00 -0500 | [diff] [blame] | 88 | /* |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 89 | * ima_counts_get - increment file counts |
| 90 | * |
| 91 | * Maintain read/write counters for all files, but only |
| 92 | * invalidate the PCR for measured files: |
| 93 | * - Opening a file for write when already open for read, |
| 94 | * results in a time of measure, time of use (ToMToU) error. |
| 95 | * - Opening a file for read when already open for write, |
| 96 | * could result in a file measurement error. |
| 97 | * |
| 98 | */ |
| 99 | void ima_counts_get(struct file *file) |
| 100 | { |
| 101 | struct dentry *dentry = file->f_path.dentry; |
| 102 | struct inode *inode = dentry->d_inode; |
| 103 | fmode_t mode = file->f_mode; |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 104 | int rc; |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 105 | bool send_tomtou = false, send_writers = false; |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 106 | |
Eric Paris | a178d20 | 2010-10-25 14:41:59 -0400 | [diff] [blame] | 107 | if (!S_ISREG(inode->i_mode)) |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 108 | return; |
Eric Paris | a178d20 | 2010-10-25 14:41:59 -0400 | [diff] [blame] | 109 | |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 110 | spin_lock(&inode->i_lock); |
| 111 | |
Mimi Zohar | e950598 | 2010-08-31 09:38:51 -0400 | [diff] [blame] | 112 | if (!ima_initialized) |
| 113 | goto out; |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 114 | |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 115 | if (mode & FMODE_WRITE) { |
Eric Paris | bade72d | 2010-10-25 14:42:25 -0400 | [diff] [blame] | 116 | if (inode->i_readcount && IS_IMA(inode)) |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 117 | send_tomtou = true; |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 118 | goto out; |
| 119 | } |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 120 | |
Eric Paris | bade72d | 2010-10-25 14:42:25 -0400 | [diff] [blame] | 121 | rc = ima_must_measure(NULL, inode, MAY_READ, FILE_CHECK); |
| 122 | if (rc < 0) |
| 123 | goto out; |
| 124 | |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 125 | if (atomic_read(&inode->i_writecount) > 0) |
| 126 | send_writers = true; |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 127 | out: |
Eric Paris | a178d20 | 2010-10-25 14:41:59 -0400 | [diff] [blame] | 128 | /* remember the vfs deals with i_writecount */ |
| 129 | if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) |
| 130 | inode->i_readcount++; |
Eric Paris | bade72d | 2010-10-25 14:42:25 -0400 | [diff] [blame] | 131 | |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 132 | spin_unlock(&inode->i_lock); |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 133 | |
| 134 | if (send_tomtou) |
| 135 | ima_add_violation(inode, dentry->d_name.name, "invalid_pcr", |
| 136 | "ToMToU"); |
| 137 | if (send_writers) |
| 138 | ima_add_violation(inode, dentry->d_name.name, "invalid_pcr", |
| 139 | "open_writers"); |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /* |
Eric Paris | e0d5bd2 | 2009-12-04 15:48:00 -0500 | [diff] [blame] | 143 | * Decrement ima counts |
| 144 | */ |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 145 | static void ima_dec_counts(struct inode *inode, struct file *file) |
Eric Paris | e0d5bd2 | 2009-12-04 15:48:00 -0500 | [diff] [blame] | 146 | { |
Al Viro | 1429b3e | 2009-12-16 06:38:01 -0500 | [diff] [blame] | 147 | mode_t mode = file->f_mode; |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 148 | |
| 149 | assert_spin_locked(&inode->i_lock); |
| 150 | |
| 151 | if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) { |
| 152 | if (unlikely(inode->i_readcount == 0)) { |
| 153 | if (!ima_limit_imbalance(file)) { |
| 154 | printk(KERN_INFO "%s: open/free imbalance (r:%u)\n", |
| 155 | __func__, inode->i_readcount); |
| 156 | dump_stack(); |
| 157 | } |
| 158 | return; |
| 159 | } |
| 160 | inode->i_readcount--; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | static void ima_check_last_writer(struct ima_iint_cache *iint, |
| 165 | struct inode *inode, |
| 166 | struct file *file) |
| 167 | { |
| 168 | mode_t mode = file->f_mode; |
Eric Paris | 497f323 | 2010-10-25 14:41:32 -0400 | [diff] [blame] | 169 | |
Eric Paris | e0d5bd2 | 2009-12-04 15:48:00 -0500 | [diff] [blame] | 170 | BUG_ON(!mutex_is_locked(&iint->mutex)); |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 171 | assert_spin_locked(&inode->i_lock); |
Eric Paris | e0d5bd2 | 2009-12-04 15:48:00 -0500 | [diff] [blame] | 172 | |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 173 | if (mode & FMODE_WRITE && |
| 174 | atomic_read(&inode->i_writecount) == 1 && |
| 175 | iint->version != inode->i_version) |
| 176 | iint->flags &= ~IMA_MEASURED; |
| 177 | } |
Eric Paris | e0d5bd2 | 2009-12-04 15:48:00 -0500 | [diff] [blame] | 178 | |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 179 | static void ima_file_free_iint(struct ima_iint_cache *iint, struct inode *inode, |
| 180 | struct file *file) |
| 181 | { |
| 182 | mutex_lock(&iint->mutex); |
| 183 | spin_lock(&inode->i_lock); |
| 184 | |
| 185 | ima_dec_counts(inode, file); |
| 186 | ima_check_last_writer(iint, inode, file); |
| 187 | |
| 188 | spin_unlock(&inode->i_lock); |
| 189 | mutex_unlock(&iint->mutex); |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static void ima_file_free_noiint(struct inode *inode, struct file *file) |
| 193 | { |
| 194 | spin_lock(&inode->i_lock); |
| 195 | |
| 196 | ima_dec_counts(inode, file); |
| 197 | |
| 198 | spin_unlock(&inode->i_lock); |
Eric Paris | e0d5bd2 | 2009-12-04 15:48:00 -0500 | [diff] [blame] | 199 | } |
| 200 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 201 | /** |
| 202 | * ima_file_free - called on __fput() |
| 203 | * @file: pointer to file structure being freed |
| 204 | * |
| 205 | * Flag files that changed, based on i_version; |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 206 | * and decrement the i_readcount. |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 207 | */ |
| 208 | void ima_file_free(struct file *file) |
| 209 | { |
| 210 | struct inode *inode = file->f_dentry->d_inode; |
| 211 | struct ima_iint_cache *iint; |
| 212 | |
Mimi Zohar | e950598 | 2010-08-31 09:38:51 -0400 | [diff] [blame] | 213 | if (!iint_initialized || !S_ISREG(inode->i_mode)) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 214 | return; |
Eric Paris | 196f518 | 2010-10-25 14:42:19 -0400 | [diff] [blame] | 215 | |
Eric Paris | 64c62f0 | 2010-10-25 14:42:12 -0400 | [diff] [blame] | 216 | iint = ima_iint_find(inode); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 217 | |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 218 | if (iint) |
| 219 | ima_file_free_iint(iint, inode, file); |
| 220 | else |
| 221 | ima_file_free_noiint(inode, file); |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 222 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 223 | } |
| 224 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 225 | static int process_measurement(struct file *file, const unsigned char *filename, |
| 226 | int mask, int function) |
| 227 | { |
| 228 | struct inode *inode = file->f_dentry->d_inode; |
| 229 | struct ima_iint_cache *iint; |
Mimi Zohar | e950598 | 2010-08-31 09:38:51 -0400 | [diff] [blame] | 230 | int rc = 0; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 231 | |
| 232 | if (!ima_initialized || !S_ISREG(inode->i_mode)) |
| 233 | return 0; |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 234 | |
| 235 | rc = ima_must_measure(NULL, inode, mask, function); |
| 236 | if (rc != 0) |
| 237 | return rc; |
| 238 | retry: |
Eric Paris | 64c62f0 | 2010-10-25 14:42:12 -0400 | [diff] [blame] | 239 | iint = ima_iint_find(inode); |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 240 | if (!iint) { |
| 241 | rc = ima_inode_alloc(inode); |
| 242 | if (!rc || rc == -EEXIST) |
| 243 | goto retry; |
| 244 | return rc; |
| 245 | } |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 246 | |
| 247 | mutex_lock(&iint->mutex); |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 248 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 249 | rc = ima_must_measure(iint, inode, mask, function); |
| 250 | if (rc != 0) |
| 251 | goto out; |
| 252 | |
| 253 | rc = ima_collect_measurement(iint, file); |
| 254 | if (!rc) |
| 255 | ima_store_measurement(iint, file, filename); |
| 256 | out: |
| 257 | mutex_unlock(&iint->mutex); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 258 | return rc; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * ima_file_mmap - based on policy, collect/store measurement. |
| 263 | * @file: pointer to the file to be measured (May be NULL) |
| 264 | * @prot: contains the protection that will be applied by the kernel. |
| 265 | * |
| 266 | * Measure files being mmapped executable based on the ima_must_measure() |
| 267 | * policy decision. |
| 268 | * |
| 269 | * Return 0 on success, an error code on failure. |
| 270 | * (Based on the results of appraise_measurement().) |
| 271 | */ |
| 272 | int ima_file_mmap(struct file *file, unsigned long prot) |
| 273 | { |
| 274 | int rc; |
| 275 | |
| 276 | if (!file) |
| 277 | return 0; |
| 278 | if (prot & PROT_EXEC) |
| 279 | rc = process_measurement(file, file->f_dentry->d_name.name, |
| 280 | MAY_EXEC, FILE_MMAP); |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * ima_bprm_check - based on policy, collect/store measurement. |
| 286 | * @bprm: contains the linux_binprm structure |
| 287 | * |
| 288 | * The OS protects against an executable file, already open for write, |
| 289 | * from being executed in deny_write_access() and an executable file, |
| 290 | * already open for execute, from being modified in get_write_access(). |
| 291 | * So we can be certain that what we verify and measure here is actually |
| 292 | * what is being executed. |
| 293 | * |
| 294 | * Return 0 on success, an error code on failure. |
| 295 | * (Based on the results of appraise_measurement().) |
| 296 | */ |
| 297 | int ima_bprm_check(struct linux_binprm *bprm) |
| 298 | { |
| 299 | int rc; |
| 300 | |
| 301 | rc = process_measurement(bprm->file, bprm->filename, |
| 302 | MAY_EXEC, BPRM_CHECK); |
| 303 | return 0; |
| 304 | } |
| 305 | |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 306 | /** |
| 307 | * ima_path_check - based on policy, collect/store measurement. |
| 308 | * @file: pointer to the file to be measured |
| 309 | * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE |
| 310 | * |
| 311 | * Measure files based on the ima_must_measure() policy decision. |
| 312 | * |
| 313 | * Always return 0 and audit dentry_open failures. |
| 314 | * (Return code will be based upon measurement appraisal.) |
| 315 | */ |
Mimi Zohar | 9bbb6ca | 2010-01-26 17:02:40 -0500 | [diff] [blame] | 316 | int ima_file_check(struct file *file, int mask) |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 317 | { |
| 318 | int rc; |
| 319 | |
| 320 | rc = process_measurement(file, file->f_dentry->d_name.name, |
| 321 | mask & (MAY_READ | MAY_WRITE | MAY_EXEC), |
Mimi Zohar | 1e93d00 | 2010-01-26 17:02:41 -0500 | [diff] [blame] | 322 | FILE_CHECK); |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 323 | return 0; |
| 324 | } |
Mimi Zohar | 9bbb6ca | 2010-01-26 17:02:40 -0500 | [diff] [blame] | 325 | EXPORT_SYMBOL_GPL(ima_file_check); |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 326 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 327 | static int __init init_ima(void) |
| 328 | { |
| 329 | int error; |
| 330 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 331 | error = ima_init(); |
| 332 | ima_initialized = 1; |
| 333 | return error; |
| 334 | } |
| 335 | |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 336 | static void __exit cleanup_ima(void) |
| 337 | { |
| 338 | ima_cleanup(); |
| 339 | } |
| 340 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 341 | late_initcall(init_ima); /* Start IMA after the TPM is available */ |
| 342 | |
| 343 | MODULE_DESCRIPTION("Integrity Measurement Architecture"); |
| 344 | MODULE_LICENSE("GPL"); |