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, |
| 17 | * and ima_path_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> |
| 24 | |
| 25 | #include "ima.h" |
| 26 | |
| 27 | int ima_initialized; |
| 28 | |
| 29 | char *ima_hash = "sha1"; |
| 30 | static int __init hash_setup(char *str) |
| 31 | { |
Mimi Zohar | 07ff7a0 | 2009-05-05 13:13:10 -0400 | [diff] [blame] | 32 | if (strncmp(str, "md5", 3) == 0) |
| 33 | ima_hash = "md5"; |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 34 | return 1; |
| 35 | } |
| 36 | __setup("ima_hash=", hash_setup); |
| 37 | |
Eric Paris | e0d5bd2 | 2009-12-04 15:48:00 -0500 | [diff] [blame^] | 38 | /* |
| 39 | * Update the counts given an fmode_t |
| 40 | */ |
| 41 | static void ima_inc_counts(struct ima_iint_cache *iint, fmode_t mode) |
| 42 | { |
| 43 | BUG_ON(!mutex_is_locked(&iint->mutex)); |
| 44 | |
| 45 | iint->opencount++; |
| 46 | if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) |
| 47 | iint->readcount++; |
| 48 | if (mode & FMODE_WRITE) |
| 49 | iint->writecount++; |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * Update the counts given open flags instead of fmode |
| 54 | */ |
| 55 | static void ima_inc_counts_flags(struct ima_iint_cache *iint, int flags) |
| 56 | { |
| 57 | ima_inc_counts(iint, (__force fmode_t)((flags+1) & O_ACCMODE)); |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * Decrement ima counts |
| 62 | */ |
| 63 | static void ima_dec_counts(struct ima_iint_cache *iint, struct inode *inode, |
| 64 | fmode_t mode) |
| 65 | { |
| 66 | BUG_ON(!mutex_is_locked(&iint->mutex)); |
| 67 | |
| 68 | iint->opencount--; |
| 69 | if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) |
| 70 | iint->readcount--; |
| 71 | if (mode & FMODE_WRITE) { |
| 72 | iint->writecount--; |
| 73 | if (iint->writecount == 0) { |
| 74 | if (iint->version != inode->i_version) |
| 75 | iint->flags &= ~IMA_MEASURED; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if ((iint->opencount < 0) || |
| 80 | (iint->readcount < 0) || |
| 81 | (iint->writecount < 0)) { |
| 82 | static int dumped; |
| 83 | |
| 84 | if (dumped) |
| 85 | return; |
| 86 | dumped = 1; |
| 87 | |
| 88 | printk(KERN_INFO "%s: open/free imbalance (r:%ld w:%ld o:%ld)\n", |
| 89 | __FUNCTION__, iint->readcount, iint->writecount, |
| 90 | iint->opencount); |
| 91 | dump_stack(); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static void ima_dec_counts_flags(struct ima_iint_cache *iint, |
| 96 | struct inode *inode, int flags) |
| 97 | { |
| 98 | ima_dec_counts(iint, inode, (__force fmode_t)((flags+1) & O_ACCMODE)); |
| 99 | } |
| 100 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 101 | /** |
| 102 | * ima_file_free - called on __fput() |
| 103 | * @file: pointer to file structure being freed |
| 104 | * |
| 105 | * Flag files that changed, based on i_version; |
| 106 | * and decrement the iint readcount/writecount. |
| 107 | */ |
| 108 | void ima_file_free(struct file *file) |
| 109 | { |
| 110 | struct inode *inode = file->f_dentry->d_inode; |
| 111 | struct ima_iint_cache *iint; |
| 112 | |
| 113 | if (!ima_initialized || !S_ISREG(inode->i_mode)) |
| 114 | return; |
| 115 | iint = ima_iint_find_get(inode); |
| 116 | if (!iint) |
| 117 | return; |
| 118 | |
| 119 | mutex_lock(&iint->mutex); |
Eric Paris | e0d5bd2 | 2009-12-04 15:48:00 -0500 | [diff] [blame^] | 120 | ima_dec_counts(iint, inode, file->f_mode); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 121 | mutex_unlock(&iint->mutex); |
| 122 | kref_put(&iint->refcount, iint_free); |
| 123 | } |
| 124 | |
| 125 | /* ima_read_write_check - reflect possible reading/writing errors in the PCR. |
| 126 | * |
| 127 | * When opening a file for read, if the file is already open for write, |
| 128 | * the file could change, resulting in a file measurement error. |
| 129 | * |
| 130 | * Opening a file for write, if the file is already open for read, results |
| 131 | * in a time of measure, time of use (ToMToU) error. |
| 132 | * |
| 133 | * In either case invalidate the PCR. |
| 134 | */ |
| 135 | enum iint_pcr_error { TOMTOU, OPEN_WRITERS }; |
| 136 | static void ima_read_write_check(enum iint_pcr_error error, |
| 137 | struct ima_iint_cache *iint, |
| 138 | struct inode *inode, |
| 139 | const unsigned char *filename) |
| 140 | { |
| 141 | switch (error) { |
| 142 | case TOMTOU: |
| 143 | if (iint->readcount > 0) |
| 144 | ima_add_violation(inode, filename, "invalid_pcr", |
| 145 | "ToMToU"); |
| 146 | break; |
| 147 | case OPEN_WRITERS: |
| 148 | if (iint->writecount > 0) |
| 149 | ima_add_violation(inode, filename, "invalid_pcr", |
| 150 | "open_writers"); |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | static int get_path_measurement(struct ima_iint_cache *iint, struct file *file, |
| 156 | const unsigned char *filename) |
| 157 | { |
| 158 | int rc = 0; |
| 159 | |
Eric Paris | e0d5bd2 | 2009-12-04 15:48:00 -0500 | [diff] [blame^] | 160 | ima_inc_counts(iint, file->f_mode); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 161 | |
| 162 | rc = ima_collect_measurement(iint, file); |
| 163 | if (!rc) |
| 164 | ima_store_measurement(iint, file, filename); |
| 165 | return rc; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * ima_path_check - based on policy, collect/store measurement. |
| 170 | * @path: contains a pointer to the path to be measured |
| 171 | * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE |
| 172 | * |
| 173 | * Measure the file being open for readonly, based on the |
| 174 | * ima_must_measure() policy decision. |
| 175 | * |
| 176 | * Keep read/write counters for all files, but only |
| 177 | * invalidate the PCR for measured files: |
| 178 | * - Opening a file for write when already open for read, |
| 179 | * results in a time of measure, time of use (ToMToU) error. |
| 180 | * - Opening a file for read when already open for write, |
| 181 | * could result in a file measurement error. |
| 182 | * |
Mimi Zohar | 04288f4 | 2009-06-04 13:53:10 -0400 | [diff] [blame] | 183 | * Always return 0 and audit dentry_open failures. |
| 184 | * (Return code will be based upon measurement appraisal.) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 185 | */ |
Mimi Zohar | b9fc745 | 2009-05-19 13:25:57 -0400 | [diff] [blame] | 186 | int ima_path_check(struct path *path, int mask, int update_counts) |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 187 | { |
| 188 | struct inode *inode = path->dentry->d_inode; |
| 189 | struct ima_iint_cache *iint; |
| 190 | struct file *file = NULL; |
| 191 | int rc; |
| 192 | |
| 193 | if (!ima_initialized || !S_ISREG(inode->i_mode)) |
| 194 | return 0; |
Eric Paris | 9353384 | 2009-12-04 15:47:52 -0500 | [diff] [blame] | 195 | iint = ima_iint_find_get(inode); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 196 | if (!iint) |
| 197 | return 0; |
| 198 | |
| 199 | mutex_lock(&iint->mutex); |
Mimi Zohar | b9fc745 | 2009-05-19 13:25:57 -0400 | [diff] [blame] | 200 | if (update_counts) |
Eric Paris | e0d5bd2 | 2009-12-04 15:48:00 -0500 | [diff] [blame^] | 201 | ima_inc_counts_flags(iint, mask); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 202 | |
| 203 | rc = ima_must_measure(iint, inode, MAY_READ, PATH_CHECK); |
| 204 | if (rc < 0) |
| 205 | goto out; |
| 206 | |
| 207 | if ((mask & MAY_WRITE) || (mask == 0)) |
| 208 | ima_read_write_check(TOMTOU, iint, inode, |
| 209 | path->dentry->d_name.name); |
| 210 | |
| 211 | if ((mask & (MAY_WRITE | MAY_READ | MAY_EXEC)) != MAY_READ) |
| 212 | goto out; |
| 213 | |
| 214 | ima_read_write_check(OPEN_WRITERS, iint, inode, |
| 215 | path->dentry->d_name.name); |
| 216 | if (!(iint->flags & IMA_MEASURED)) { |
| 217 | struct dentry *dentry = dget(path->dentry); |
| 218 | struct vfsmount *mnt = mntget(path->mnt); |
| 219 | |
Eric Paris | 1a62e95 | 2009-05-11 13:59:22 -0400 | [diff] [blame] | 220 | file = dentry_open(dentry, mnt, O_RDONLY | O_LARGEFILE, |
| 221 | current_cred()); |
Eric Paris | f06dd16 | 2009-05-11 13:59:16 -0400 | [diff] [blame] | 222 | if (IS_ERR(file)) { |
Mimi Zohar | 04288f4 | 2009-06-04 13:53:10 -0400 | [diff] [blame] | 223 | int audit_info = 0; |
| 224 | |
| 225 | integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, |
| 226 | dentry->d_name.name, |
| 227 | "add_measurement", |
| 228 | "dentry_open failed", |
| 229 | 1, audit_info); |
Eric Paris | f06dd16 | 2009-05-11 13:59:16 -0400 | [diff] [blame] | 230 | file = NULL; |
| 231 | goto out; |
| 232 | } |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 233 | rc = get_path_measurement(iint, file, dentry->d_name.name); |
| 234 | } |
| 235 | out: |
| 236 | mutex_unlock(&iint->mutex); |
| 237 | if (file) |
| 238 | fput(file); |
| 239 | kref_put(&iint->refcount, iint_free); |
| 240 | return 0; |
| 241 | } |
Mimi Zohar | b9fc745 | 2009-05-19 13:25:57 -0400 | [diff] [blame] | 242 | EXPORT_SYMBOL_GPL(ima_path_check); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 243 | |
| 244 | static int process_measurement(struct file *file, const unsigned char *filename, |
| 245 | int mask, int function) |
| 246 | { |
| 247 | struct inode *inode = file->f_dentry->d_inode; |
| 248 | struct ima_iint_cache *iint; |
| 249 | int rc; |
| 250 | |
| 251 | if (!ima_initialized || !S_ISREG(inode->i_mode)) |
| 252 | return 0; |
Eric Paris | 9353384 | 2009-12-04 15:47:52 -0500 | [diff] [blame] | 253 | iint = ima_iint_find_get(inode); |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 254 | if (!iint) |
| 255 | return -ENOMEM; |
| 256 | |
| 257 | mutex_lock(&iint->mutex); |
| 258 | rc = ima_must_measure(iint, inode, mask, function); |
| 259 | if (rc != 0) |
| 260 | goto out; |
| 261 | |
| 262 | rc = ima_collect_measurement(iint, file); |
| 263 | if (!rc) |
| 264 | ima_store_measurement(iint, file, filename); |
| 265 | out: |
| 266 | mutex_unlock(&iint->mutex); |
| 267 | kref_put(&iint->refcount, iint_free); |
| 268 | return rc; |
| 269 | } |
| 270 | |
Mimi Zohar | b9fc745 | 2009-05-19 13:25:57 -0400 | [diff] [blame] | 271 | /* |
Mimi Zohar | 94e5d71 | 2009-06-26 14:05:27 -0400 | [diff] [blame] | 272 | * ima_counts_put - decrement file counts |
| 273 | * |
| 274 | * File counts are incremented in ima_path_check. On file open |
| 275 | * error, such as ETXTBSY, decrement the counts to prevent |
| 276 | * unnecessary imbalance messages. |
| 277 | */ |
| 278 | void ima_counts_put(struct path *path, int mask) |
| 279 | { |
| 280 | struct inode *inode = path->dentry->d_inode; |
| 281 | struct ima_iint_cache *iint; |
| 282 | |
Mimi Zohar | acd0c93 | 2009-09-04 13:08:46 -0400 | [diff] [blame] | 283 | /* The inode may already have been freed, freeing the iint |
| 284 | * with it. Verify the inode is not NULL before dereferencing |
| 285 | * it. |
| 286 | */ |
| 287 | if (!ima_initialized || !inode || !S_ISREG(inode->i_mode)) |
Mimi Zohar | 94e5d71 | 2009-06-26 14:05:27 -0400 | [diff] [blame] | 288 | return; |
Eric Paris | 9353384 | 2009-12-04 15:47:52 -0500 | [diff] [blame] | 289 | iint = ima_iint_find_get(inode); |
Mimi Zohar | 94e5d71 | 2009-06-26 14:05:27 -0400 | [diff] [blame] | 290 | if (!iint) |
| 291 | return; |
| 292 | |
| 293 | mutex_lock(&iint->mutex); |
Eric Paris | e0d5bd2 | 2009-12-04 15:48:00 -0500 | [diff] [blame^] | 294 | ima_dec_counts_flags(iint, inode, mask); |
Mimi Zohar | 94e5d71 | 2009-06-26 14:05:27 -0400 | [diff] [blame] | 295 | mutex_unlock(&iint->mutex); |
Eric Paris | 53a7197 | 2009-08-26 14:56:48 -0400 | [diff] [blame] | 296 | |
| 297 | kref_put(&iint->refcount, iint_free); |
Mimi Zohar | 94e5d71 | 2009-06-26 14:05:27 -0400 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /* |
| 301 | * ima_counts_get - increment file counts |
Mimi Zohar | b9fc745 | 2009-05-19 13:25:57 -0400 | [diff] [blame] | 302 | * |
| 303 | * - for IPC shm and shmat file. |
| 304 | * - for nfsd exported files. |
| 305 | * |
| 306 | * Increment the counts for these files to prevent unnecessary |
| 307 | * imbalance messages. |
| 308 | */ |
| 309 | void ima_counts_get(struct file *file) |
Mimi Zohar | 1df9f0a | 2009-02-04 09:07:02 -0500 | [diff] [blame] | 310 | { |
| 311 | struct inode *inode = file->f_dentry->d_inode; |
| 312 | struct ima_iint_cache *iint; |
| 313 | |
| 314 | if (!ima_initialized || !S_ISREG(inode->i_mode)) |
| 315 | return; |
Eric Paris | 9353384 | 2009-12-04 15:47:52 -0500 | [diff] [blame] | 316 | iint = ima_iint_find_get(inode); |
Mimi Zohar | 1df9f0a | 2009-02-04 09:07:02 -0500 | [diff] [blame] | 317 | if (!iint) |
| 318 | return; |
| 319 | mutex_lock(&iint->mutex); |
Eric Paris | e0d5bd2 | 2009-12-04 15:48:00 -0500 | [diff] [blame^] | 320 | ima_inc_counts(iint, file->f_mode); |
Mimi Zohar | 1df9f0a | 2009-02-04 09:07:02 -0500 | [diff] [blame] | 321 | mutex_unlock(&iint->mutex); |
Eric Paris | 53a7197 | 2009-08-26 14:56:48 -0400 | [diff] [blame] | 322 | |
| 323 | kref_put(&iint->refcount, iint_free); |
Mimi Zohar | 1df9f0a | 2009-02-04 09:07:02 -0500 | [diff] [blame] | 324 | } |
Mimi Zohar | b9fc745 | 2009-05-19 13:25:57 -0400 | [diff] [blame] | 325 | EXPORT_SYMBOL_GPL(ima_counts_get); |
Mimi Zohar | 1df9f0a | 2009-02-04 09:07:02 -0500 | [diff] [blame] | 326 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 327 | /** |
| 328 | * ima_file_mmap - based on policy, collect/store measurement. |
| 329 | * @file: pointer to the file to be measured (May be NULL) |
| 330 | * @prot: contains the protection that will be applied by the kernel. |
| 331 | * |
| 332 | * Measure files being mmapped executable based on the ima_must_measure() |
| 333 | * policy decision. |
| 334 | * |
| 335 | * Return 0 on success, an error code on failure. |
| 336 | * (Based on the results of appraise_measurement().) |
| 337 | */ |
| 338 | int ima_file_mmap(struct file *file, unsigned long prot) |
| 339 | { |
| 340 | int rc; |
| 341 | |
| 342 | if (!file) |
| 343 | return 0; |
| 344 | if (prot & PROT_EXEC) |
| 345 | rc = process_measurement(file, file->f_dentry->d_name.name, |
| 346 | MAY_EXEC, FILE_MMAP); |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * ima_bprm_check - based on policy, collect/store measurement. |
| 352 | * @bprm: contains the linux_binprm structure |
| 353 | * |
| 354 | * The OS protects against an executable file, already open for write, |
| 355 | * from being executed in deny_write_access() and an executable file, |
| 356 | * already open for execute, from being modified in get_write_access(). |
| 357 | * So we can be certain that what we verify and measure here is actually |
| 358 | * what is being executed. |
| 359 | * |
| 360 | * Return 0 on success, an error code on failure. |
| 361 | * (Based on the results of appraise_measurement().) |
| 362 | */ |
| 363 | int ima_bprm_check(struct linux_binprm *bprm) |
| 364 | { |
| 365 | int rc; |
| 366 | |
| 367 | rc = process_measurement(bprm->file, bprm->filename, |
| 368 | MAY_EXEC, BPRM_CHECK); |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static int __init init_ima(void) |
| 373 | { |
| 374 | int error; |
| 375 | |
| 376 | ima_iintcache_init(); |
| 377 | error = ima_init(); |
| 378 | ima_initialized = 1; |
| 379 | return error; |
| 380 | } |
| 381 | |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 382 | static void __exit cleanup_ima(void) |
| 383 | { |
| 384 | ima_cleanup(); |
| 385 | } |
| 386 | |
Mimi Zohar | 3323eec | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 387 | late_initcall(init_ima); /* Start IMA after the TPM is available */ |
| 388 | |
| 389 | MODULE_DESCRIPTION("Integrity Measurement Architecture"); |
| 390 | MODULE_LICENSE("GPL"); |