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