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