]>
Commit | Line | Data |
---|---|---|
3323eec9 MZ |
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 | |
e0d5bd2a | 16 | * implements the IMA hooks: ima_bprm_check, ima_file_mmap, |
9bbb6cad | 17 | * and ima_file_check. |
3323eec9 MZ |
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> | |
5a0e3ad6 | 24 | #include <linux/slab.h> |
3323eec9 MZ |
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 | { | |
07ff7a0b MZ |
33 | if (strncmp(str, "md5", 3) == 0) |
34 | ima_hash = "md5"; | |
3323eec9 MZ |
35 | return 1; |
36 | } | |
37 | __setup("ima_hash=", hash_setup); | |
38 | ||
d1625436 MZ |
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 | ||
e0d5bd2a EP |
88 | /* |
89 | * Update the counts given an fmode_t | |
90 | */ | |
91 | static void ima_inc_counts(struct ima_iint_cache *iint, fmode_t mode) | |
92 | { | |
ad16ad00 | 93 | assert_spin_locked(&iint->inode->i_lock); |
e0d5bd2a | 94 | |
e0d5bd2a EP |
95 | if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) |
96 | iint->readcount++; | |
e0d5bd2a EP |
97 | } |
98 | ||
8eb988c7 MZ |
99 | /* |
100 | * ima_counts_get - increment file counts | |
101 | * | |
102 | * Maintain read/write counters for all files, but only | |
103 | * invalidate the PCR for measured files: | |
104 | * - Opening a file for write when already open for read, | |
105 | * results in a time of measure, time of use (ToMToU) error. | |
106 | * - Opening a file for read when already open for write, | |
107 | * could result in a file measurement error. | |
108 | * | |
109 | */ | |
110 | void ima_counts_get(struct file *file) | |
111 | { | |
112 | struct dentry *dentry = file->f_path.dentry; | |
113 | struct inode *inode = dentry->d_inode; | |
114 | fmode_t mode = file->f_mode; | |
115 | struct ima_iint_cache *iint; | |
116 | int rc; | |
ad16ad00 | 117 | bool send_tomtou = false, send_writers = false; |
8eb988c7 | 118 | |
e950598d | 119 | if (!iint_initialized || !S_ISREG(inode->i_mode)) |
8eb988c7 MZ |
120 | return; |
121 | iint = ima_iint_find_get(inode); | |
122 | if (!iint) | |
123 | return; | |
124 | mutex_lock(&iint->mutex); | |
ad16ad00 EP |
125 | spin_lock(&inode->i_lock); |
126 | ||
e950598d MZ |
127 | if (!ima_initialized) |
128 | goto out; | |
ad16ad00 | 129 | |
1e93d005 | 130 | rc = ima_must_measure(iint, inode, MAY_READ, FILE_CHECK); |
8eb988c7 MZ |
131 | if (rc < 0) |
132 | goto out; | |
133 | ||
134 | if (mode & FMODE_WRITE) { | |
ad16ad00 EP |
135 | if (iint->readcount) |
136 | send_tomtou = true; | |
8eb988c7 MZ |
137 | goto out; |
138 | } | |
ad16ad00 EP |
139 | |
140 | if (atomic_read(&inode->i_writecount) > 0) | |
141 | send_writers = true; | |
8eb988c7 MZ |
142 | out: |
143 | ima_inc_counts(iint, file->f_mode); | |
ad16ad00 | 144 | spin_unlock(&inode->i_lock); |
8eb988c7 | 145 | mutex_unlock(&iint->mutex); |
8eb988c7 | 146 | kref_put(&iint->refcount, iint_free); |
ad16ad00 EP |
147 | |
148 | if (send_tomtou) | |
149 | ima_add_violation(inode, dentry->d_name.name, "invalid_pcr", | |
150 | "ToMToU"); | |
151 | if (send_writers) | |
152 | ima_add_violation(inode, dentry->d_name.name, "invalid_pcr", | |
153 | "open_writers"); | |
8eb988c7 MZ |
154 | } |
155 | ||
e0d5bd2a EP |
156 | /* |
157 | * Decrement ima counts | |
158 | */ | |
159 | static void ima_dec_counts(struct ima_iint_cache *iint, struct inode *inode, | |
1429b3ec | 160 | struct file *file) |
e0d5bd2a | 161 | { |
1429b3ec | 162 | mode_t mode = file->f_mode; |
497f3233 EP |
163 | bool dump = false; |
164 | ||
e0d5bd2a | 165 | BUG_ON(!mutex_is_locked(&iint->mutex)); |
ad16ad00 | 166 | assert_spin_locked(&inode->i_lock); |
e0d5bd2a | 167 | |
497f3233 EP |
168 | if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) { |
169 | if (unlikely(iint->readcount == 0)) | |
170 | dump = true; | |
e0d5bd2a | 171 | iint->readcount--; |
497f3233 | 172 | } |
e0d5bd2a | 173 | if (mode & FMODE_WRITE) { |
b9593d30 | 174 | if (atomic_read(&inode->i_writecount) <= 0) |
497f3233 | 175 | dump = true; |
b9593d30 EP |
176 | if (atomic_read(&inode->i_writecount) == 1 && |
177 | iint->version != inode->i_version) | |
178 | iint->flags &= ~IMA_MEASURED; | |
e0d5bd2a EP |
179 | } |
180 | ||
497f3233 | 181 | if (dump && !ima_limit_imbalance(file)) { |
b9593d30 EP |
182 | printk(KERN_INFO "%s: open/free imbalance (r:%u)\n", |
183 | __func__, iint->readcount); | |
e0d5bd2a EP |
184 | dump_stack(); |
185 | } | |
186 | } | |
187 | ||
3323eec9 MZ |
188 | /** |
189 | * ima_file_free - called on __fput() | |
190 | * @file: pointer to file structure being freed | |
191 | * | |
192 | * Flag files that changed, based on i_version; | |
193 | * and decrement the iint readcount/writecount. | |
194 | */ | |
195 | void ima_file_free(struct file *file) | |
196 | { | |
197 | struct inode *inode = file->f_dentry->d_inode; | |
198 | struct ima_iint_cache *iint; | |
199 | ||
e950598d | 200 | if (!iint_initialized || !S_ISREG(inode->i_mode)) |
3323eec9 MZ |
201 | return; |
202 | iint = ima_iint_find_get(inode); | |
203 | if (!iint) | |
204 | return; | |
205 | ||
206 | mutex_lock(&iint->mutex); | |
ad16ad00 EP |
207 | spin_lock(&inode->i_lock); |
208 | ||
1429b3ec | 209 | ima_dec_counts(iint, inode, file); |
ad16ad00 EP |
210 | |
211 | spin_unlock(&inode->i_lock); | |
3323eec9 MZ |
212 | mutex_unlock(&iint->mutex); |
213 | kref_put(&iint->refcount, iint_free); | |
214 | } | |
215 | ||
3323eec9 MZ |
216 | static int process_measurement(struct file *file, const unsigned char *filename, |
217 | int mask, int function) | |
218 | { | |
219 | struct inode *inode = file->f_dentry->d_inode; | |
220 | struct ima_iint_cache *iint; | |
e950598d | 221 | int rc = 0; |
3323eec9 MZ |
222 | |
223 | if (!ima_initialized || !S_ISREG(inode->i_mode)) | |
224 | return 0; | |
9353384e | 225 | iint = ima_iint_find_get(inode); |
3323eec9 MZ |
226 | if (!iint) |
227 | return -ENOMEM; | |
228 | ||
229 | mutex_lock(&iint->mutex); | |
230 | rc = ima_must_measure(iint, inode, mask, function); | |
231 | if (rc != 0) | |
232 | goto out; | |
233 | ||
234 | rc = ima_collect_measurement(iint, file); | |
235 | if (!rc) | |
236 | ima_store_measurement(iint, file, filename); | |
237 | out: | |
238 | mutex_unlock(&iint->mutex); | |
239 | kref_put(&iint->refcount, iint_free); | |
240 | return rc; | |
241 | } | |
242 | ||
243 | /** | |
244 | * ima_file_mmap - based on policy, collect/store measurement. | |
245 | * @file: pointer to the file to be measured (May be NULL) | |
246 | * @prot: contains the protection that will be applied by the kernel. | |
247 | * | |
248 | * Measure files being mmapped executable based on the ima_must_measure() | |
249 | * policy decision. | |
250 | * | |
251 | * Return 0 on success, an error code on failure. | |
252 | * (Based on the results of appraise_measurement().) | |
253 | */ | |
254 | int ima_file_mmap(struct file *file, unsigned long prot) | |
255 | { | |
256 | int rc; | |
257 | ||
258 | if (!file) | |
259 | return 0; | |
260 | if (prot & PROT_EXEC) | |
261 | rc = process_measurement(file, file->f_dentry->d_name.name, | |
262 | MAY_EXEC, FILE_MMAP); | |
263 | return 0; | |
264 | } | |
265 | ||
266 | /** | |
267 | * ima_bprm_check - based on policy, collect/store measurement. | |
268 | * @bprm: contains the linux_binprm structure | |
269 | * | |
270 | * The OS protects against an executable file, already open for write, | |
271 | * from being executed in deny_write_access() and an executable file, | |
272 | * already open for execute, from being modified in get_write_access(). | |
273 | * So we can be certain that what we verify and measure here is actually | |
274 | * what is being executed. | |
275 | * | |
276 | * Return 0 on success, an error code on failure. | |
277 | * (Based on the results of appraise_measurement().) | |
278 | */ | |
279 | int ima_bprm_check(struct linux_binprm *bprm) | |
280 | { | |
281 | int rc; | |
282 | ||
283 | rc = process_measurement(bprm->file, bprm->filename, | |
284 | MAY_EXEC, BPRM_CHECK); | |
285 | return 0; | |
286 | } | |
287 | ||
8eb988c7 MZ |
288 | /** |
289 | * ima_path_check - based on policy, collect/store measurement. | |
290 | * @file: pointer to the file to be measured | |
291 | * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE | |
292 | * | |
293 | * Measure files based on the ima_must_measure() policy decision. | |
294 | * | |
295 | * Always return 0 and audit dentry_open failures. | |
296 | * (Return code will be based upon measurement appraisal.) | |
297 | */ | |
9bbb6cad | 298 | int ima_file_check(struct file *file, int mask) |
8eb988c7 MZ |
299 | { |
300 | int rc; | |
301 | ||
302 | rc = process_measurement(file, file->f_dentry->d_name.name, | |
303 | mask & (MAY_READ | MAY_WRITE | MAY_EXEC), | |
1e93d005 | 304 | FILE_CHECK); |
8eb988c7 MZ |
305 | return 0; |
306 | } | |
9bbb6cad | 307 | EXPORT_SYMBOL_GPL(ima_file_check); |
8eb988c7 | 308 | |
3323eec9 MZ |
309 | static int __init init_ima(void) |
310 | { | |
311 | int error; | |
312 | ||
3323eec9 MZ |
313 | error = ima_init(); |
314 | ima_initialized = 1; | |
315 | return error; | |
316 | } | |
317 | ||
bab73937 MZ |
318 | static void __exit cleanup_ima(void) |
319 | { | |
320 | ima_cleanup(); | |
321 | } | |
322 | ||
3323eec9 MZ |
323 | late_initcall(init_ima); /* Start IMA after the TPM is available */ |
324 | ||
325 | MODULE_DESCRIPTION("Integrity Measurement Architecture"); | |
326 | MODULE_LICENSE("GPL"); |