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