Mimi Zohar | 3323eec9 | 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 | 3323eec9 | 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> |
James Morris | d5813a5 | 2011-08-30 10:19:50 +1000 | [diff] [blame] | 25 | #include <linux/ima.h> |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 26 | |
| 27 | #include "ima.h" |
| 28 | |
| 29 | int ima_initialized; |
| 30 | |
| 31 | char *ima_hash = "sha1"; |
| 32 | static int __init hash_setup(char *str) |
| 33 | { |
Mimi Zohar | 07ff7a0 | 2009-05-05 13:13:10 -0400 | [diff] [blame] | 34 | if (strncmp(str, "md5", 3) == 0) |
| 35 | ima_hash = "md5"; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 36 | return 1; |
| 37 | } |
| 38 | __setup("ima_hash=", hash_setup); |
| 39 | |
Eric Paris | e0d5bd2 | 2009-12-04 15:48:00 -0500 | [diff] [blame] | 40 | /* |
Mimi Zohar | 890275b5 | 2010-11-02 10:13:07 -0400 | [diff] [blame] | 41 | * ima_rdwr_violation_check |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 42 | * |
Mimi Zohar | 890275b5 | 2010-11-02 10:13:07 -0400 | [diff] [blame] | 43 | * Only invalidate the PCR for measured files: |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 44 | * - Opening a file for write when already open for read, |
| 45 | * results in a time of measure, time of use (ToMToU) error. |
| 46 | * - Opening a file for read when already open for write, |
| 47 | * could result in a file measurement error. |
| 48 | * |
| 49 | */ |
Mimi Zohar | 890275b5 | 2010-11-02 10:13:07 -0400 | [diff] [blame] | 50 | static void ima_rdwr_violation_check(struct file *file) |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 51 | { |
| 52 | struct dentry *dentry = file->f_path.dentry; |
| 53 | struct inode *inode = dentry->d_inode; |
| 54 | fmode_t mode = file->f_mode; |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 55 | int rc; |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 56 | bool send_tomtou = false, send_writers = false; |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 57 | |
Mimi Zohar | 890275b5 | 2010-11-02 10:13:07 -0400 | [diff] [blame] | 58 | if (!S_ISREG(inode->i_mode) || !ima_initialized) |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 59 | return; |
Eric Paris | a178d20 | 2010-10-25 14:41:59 -0400 | [diff] [blame] | 60 | |
Mimi Zohar | 890275b5 | 2010-11-02 10:13:07 -0400 | [diff] [blame] | 61 | mutex_lock(&inode->i_mutex); /* file metadata: permissions, xattr */ |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 62 | |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 63 | if (mode & FMODE_WRITE) { |
Mimi Zohar | a68a27b | 2010-11-02 10:10:56 -0400 | [diff] [blame] | 64 | if (atomic_read(&inode->i_readcount) && IS_IMA(inode)) |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 65 | send_tomtou = true; |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 66 | goto out; |
| 67 | } |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 68 | |
Mimi Zohar | 1adace9 | 2011-02-22 10:19:43 -0500 | [diff] [blame] | 69 | rc = ima_must_measure(inode, MAY_READ, FILE_CHECK); |
Eric Paris | bade72d | 2010-10-25 14:42:25 -0400 | [diff] [blame] | 70 | if (rc < 0) |
| 71 | goto out; |
| 72 | |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 73 | if (atomic_read(&inode->i_writecount) > 0) |
| 74 | send_writers = true; |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 75 | out: |
Mimi Zohar | 890275b5 | 2010-11-02 10:13:07 -0400 | [diff] [blame] | 76 | mutex_unlock(&inode->i_mutex); |
Eric Paris | ad16ad0 | 2010-10-25 14:41:45 -0400 | [diff] [blame] | 77 | |
| 78 | if (send_tomtou) |
| 79 | ima_add_violation(inode, dentry->d_name.name, "invalid_pcr", |
| 80 | "ToMToU"); |
| 81 | if (send_writers) |
| 82 | ima_add_violation(inode, dentry->d_name.name, "invalid_pcr", |
| 83 | "open_writers"); |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 84 | } |
| 85 | |
Mimi Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 86 | static void ima_check_last_writer(struct integrity_iint_cache *iint, |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 87 | struct inode *inode, |
| 88 | struct file *file) |
| 89 | { |
Al Viro | 4b2a2c6 | 2011-07-26 04:30:35 -0400 | [diff] [blame] | 90 | fmode_t mode = file->f_mode; |
Eric Paris | 497f323 | 2010-10-25 14:41:32 -0400 | [diff] [blame] | 91 | |
Mimi Zohar | 854fdd5 | 2010-11-02 10:14:22 -0400 | [diff] [blame] | 92 | mutex_lock(&iint->mutex); |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 93 | if (mode & FMODE_WRITE && |
| 94 | atomic_read(&inode->i_writecount) == 1 && |
| 95 | iint->version != inode->i_version) |
| 96 | iint->flags &= ~IMA_MEASURED; |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 97 | mutex_unlock(&iint->mutex); |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 98 | } |
| 99 | |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 100 | /** |
| 101 | * ima_file_free - called on __fput() |
| 102 | * @file: pointer to file structure being freed |
| 103 | * |
Mimi Zohar | 890275b5 | 2010-11-02 10:13:07 -0400 | [diff] [blame] | 104 | * Flag files that changed, based on i_version |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 105 | */ |
| 106 | void ima_file_free(struct file *file) |
| 107 | { |
| 108 | struct inode *inode = file->f_dentry->d_inode; |
Mimi Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 109 | struct integrity_iint_cache *iint; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 110 | |
Mimi Zohar | e950598 | 2010-08-31 09:38:51 -0400 | [diff] [blame] | 111 | if (!iint_initialized || !S_ISREG(inode->i_mode)) |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 112 | return; |
Eric Paris | 196f518 | 2010-10-25 14:42:19 -0400 | [diff] [blame] | 113 | |
Mimi Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 114 | iint = integrity_iint_find(inode); |
Mimi Zohar | 854fdd5 | 2010-11-02 10:14:22 -0400 | [diff] [blame] | 115 | if (!iint) |
| 116 | return; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 117 | |
Mimi Zohar | 854fdd5 | 2010-11-02 10:14:22 -0400 | [diff] [blame] | 118 | ima_check_last_writer(iint, inode, file); |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 119 | } |
| 120 | |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 121 | static int process_measurement(struct file *file, const unsigned char *filename, |
| 122 | int mask, int function) |
| 123 | { |
| 124 | struct inode *inode = file->f_dentry->d_inode; |
Mimi Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 125 | struct integrity_iint_cache *iint; |
Mimi Zohar | e950598 | 2010-08-31 09:38:51 -0400 | [diff] [blame] | 126 | int rc = 0; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 127 | |
| 128 | if (!ima_initialized || !S_ISREG(inode->i_mode)) |
| 129 | return 0; |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 130 | |
Mimi Zohar | 1adace9 | 2011-02-22 10:19:43 -0500 | [diff] [blame] | 131 | rc = ima_must_measure(inode, mask, function); |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 132 | if (rc != 0) |
| 133 | return rc; |
| 134 | retry: |
Mimi Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 135 | iint = integrity_iint_find(inode); |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 136 | if (!iint) { |
Mimi Zohar | f381c27 | 2011-03-09 14:13:22 -0500 | [diff] [blame] | 137 | rc = integrity_inode_alloc(inode); |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 138 | if (!rc || rc == -EEXIST) |
| 139 | goto retry; |
| 140 | return rc; |
| 141 | } |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 142 | |
| 143 | mutex_lock(&iint->mutex); |
Eric Paris | bc7d2a3 | 2010-10-25 14:42:05 -0400 | [diff] [blame] | 144 | |
Mimi Zohar | 1adace9 | 2011-02-22 10:19:43 -0500 | [diff] [blame] | 145 | rc = iint->flags & IMA_MEASURED ? 1 : 0; |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 146 | if (rc != 0) |
| 147 | goto out; |
| 148 | |
| 149 | rc = ima_collect_measurement(iint, file); |
| 150 | if (!rc) |
| 151 | ima_store_measurement(iint, file, filename); |
| 152 | out: |
| 153 | mutex_unlock(&iint->mutex); |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 154 | return rc; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * ima_file_mmap - based on policy, collect/store measurement. |
| 159 | * @file: pointer to the file to be measured (May be NULL) |
| 160 | * @prot: contains the protection that will be applied by the kernel. |
| 161 | * |
| 162 | * Measure files being mmapped executable based on the ima_must_measure() |
| 163 | * policy decision. |
| 164 | * |
| 165 | * Return 0 on success, an error code on failure. |
| 166 | * (Based on the results of appraise_measurement().) |
| 167 | */ |
| 168 | int ima_file_mmap(struct file *file, unsigned long prot) |
| 169 | { |
| 170 | int rc; |
| 171 | |
| 172 | if (!file) |
| 173 | return 0; |
| 174 | if (prot & PROT_EXEC) |
| 175 | rc = process_measurement(file, file->f_dentry->d_name.name, |
| 176 | MAY_EXEC, FILE_MMAP); |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * ima_bprm_check - based on policy, collect/store measurement. |
| 182 | * @bprm: contains the linux_binprm structure |
| 183 | * |
| 184 | * The OS protects against an executable file, already open for write, |
| 185 | * from being executed in deny_write_access() and an executable file, |
| 186 | * already open for execute, from being modified in get_write_access(). |
| 187 | * So we can be certain that what we verify and measure here is actually |
| 188 | * what is being executed. |
| 189 | * |
| 190 | * Return 0 on success, an error code on failure. |
| 191 | * (Based on the results of appraise_measurement().) |
| 192 | */ |
| 193 | int ima_bprm_check(struct linux_binprm *bprm) |
| 194 | { |
| 195 | int rc; |
| 196 | |
| 197 | rc = process_measurement(bprm->file, bprm->filename, |
| 198 | MAY_EXEC, BPRM_CHECK); |
| 199 | return 0; |
| 200 | } |
| 201 | |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 202 | /** |
| 203 | * ima_path_check - based on policy, collect/store measurement. |
| 204 | * @file: pointer to the file to be measured |
| 205 | * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE |
| 206 | * |
| 207 | * Measure files based on the ima_must_measure() policy decision. |
| 208 | * |
| 209 | * Always return 0 and audit dentry_open failures. |
| 210 | * (Return code will be based upon measurement appraisal.) |
| 211 | */ |
Mimi Zohar | 9bbb6ca | 2010-01-26 17:02:40 -0500 | [diff] [blame] | 212 | int ima_file_check(struct file *file, int mask) |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 213 | { |
| 214 | int rc; |
| 215 | |
Mimi Zohar | 890275b5 | 2010-11-02 10:13:07 -0400 | [diff] [blame] | 216 | ima_rdwr_violation_check(file); |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 217 | rc = process_measurement(file, file->f_dentry->d_name.name, |
| 218 | mask & (MAY_READ | MAY_WRITE | MAY_EXEC), |
Mimi Zohar | 1e93d00 | 2010-01-26 17:02:41 -0500 | [diff] [blame] | 219 | FILE_CHECK); |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 220 | return 0; |
| 221 | } |
Mimi Zohar | 9bbb6ca | 2010-01-26 17:02:40 -0500 | [diff] [blame] | 222 | EXPORT_SYMBOL_GPL(ima_file_check); |
Mimi Zohar | 8eb988c | 2010-01-20 15:35:41 -0500 | [diff] [blame] | 223 | |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 224 | static int __init init_ima(void) |
| 225 | { |
| 226 | int error; |
| 227 | |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 228 | error = ima_init(); |
| 229 | ima_initialized = 1; |
| 230 | return error; |
| 231 | } |
| 232 | |
Mimi Zohar | bab7393 | 2009-02-04 09:06:59 -0500 | [diff] [blame] | 233 | static void __exit cleanup_ima(void) |
| 234 | { |
| 235 | ima_cleanup(); |
| 236 | } |
| 237 | |
Mimi Zohar | 3323eec9 | 2009-02-04 09:06:58 -0500 | [diff] [blame] | 238 | late_initcall(init_ima); /* Start IMA after the TPM is available */ |
| 239 | |
| 240 | MODULE_DESCRIPTION("Integrity Measurement Architecture"); |
| 241 | MODULE_LICENSE("GPL"); |