IMA: Handle dentry_open failures
[linux-2.6/mini2440.git] / security / integrity / ima / ima_main.c
blob1987424623c249aae66c709b2a01d83babaec74d
1 /*
2 * Copyright (C) 2005,2006,2007,2008 IBM Corporation
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>
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.
15 * File: ima_main.c
16 * implements the IMA hooks: ima_bprm_check, ima_file_mmap,
17 * and ima_path_check.
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>
25 #include "ima.h"
27 int ima_initialized;
29 char *ima_hash = "sha1";
30 static int __init hash_setup(char *str)
32 if (strncmp(str, "md5", 3) == 0)
33 ima_hash = "md5";
34 return 1;
36 __setup("ima_hash=", hash_setup);
38 /**
39 * ima_file_free - called on __fput()
40 * @file: pointer to file structure being freed
42 * Flag files that changed, based on i_version;
43 * and decrement the iint readcount/writecount.
45 void ima_file_free(struct file *file)
47 struct inode *inode = file->f_dentry->d_inode;
48 struct ima_iint_cache *iint;
50 if (!ima_initialized || !S_ISREG(inode->i_mode))
51 return;
52 iint = ima_iint_find_get(inode);
53 if (!iint)
54 return;
56 mutex_lock(&iint->mutex);
57 if (iint->opencount <= 0) {
58 printk(KERN_INFO
59 "%s: %s open/free imbalance (r:%ld w:%ld o:%ld f:%ld)\n",
60 __FUNCTION__, file->f_dentry->d_name.name,
61 iint->readcount, iint->writecount,
62 iint->opencount, atomic_long_read(&file->f_count));
63 if (!(iint->flags & IMA_IINT_DUMP_STACK)) {
64 dump_stack();
65 iint->flags |= IMA_IINT_DUMP_STACK;
68 iint->opencount--;
70 if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
71 iint->readcount--;
73 if (file->f_mode & FMODE_WRITE) {
74 iint->writecount--;
75 if (iint->writecount == 0) {
76 if (iint->version != inode->i_version)
77 iint->flags &= ~IMA_MEASURED;
80 mutex_unlock(&iint->mutex);
81 kref_put(&iint->refcount, iint_free);
84 /* ima_read_write_check - reflect possible reading/writing errors in the PCR.
86 * When opening a file for read, if the file is already open for write,
87 * the file could change, resulting in a file measurement error.
89 * Opening a file for write, if the file is already open for read, results
90 * in a time of measure, time of use (ToMToU) error.
92 * In either case invalidate the PCR.
94 enum iint_pcr_error { TOMTOU, OPEN_WRITERS };
95 static void ima_read_write_check(enum iint_pcr_error error,
96 struct ima_iint_cache *iint,
97 struct inode *inode,
98 const unsigned char *filename)
100 switch (error) {
101 case TOMTOU:
102 if (iint->readcount > 0)
103 ima_add_violation(inode, filename, "invalid_pcr",
104 "ToMToU");
105 break;
106 case OPEN_WRITERS:
107 if (iint->writecount > 0)
108 ima_add_violation(inode, filename, "invalid_pcr",
109 "open_writers");
110 break;
114 static int get_path_measurement(struct ima_iint_cache *iint, struct file *file,
115 const unsigned char *filename)
117 int rc = 0;
119 iint->opencount++;
120 iint->readcount++;
122 rc = ima_collect_measurement(iint, file);
123 if (!rc)
124 ima_store_measurement(iint, file, filename);
125 return rc;
129 * ima_path_check - based on policy, collect/store measurement.
130 * @path: contains a pointer to the path to be measured
131 * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
133 * Measure the file being open for readonly, based on the
134 * ima_must_measure() policy decision.
136 * Keep read/write counters for all files, but only
137 * invalidate the PCR for measured files:
138 * - Opening a file for write when already open for read,
139 * results in a time of measure, time of use (ToMToU) error.
140 * - Opening a file for read when already open for write,
141 * could result in a file measurement error.
143 * Return 0 on success, an error code on failure.
144 * (Based on the results of appraise_measurement().)
146 int ima_path_check(struct path *path, int mask)
148 struct inode *inode = path->dentry->d_inode;
149 struct ima_iint_cache *iint;
150 struct file *file = NULL;
151 int rc;
153 if (!ima_initialized || !S_ISREG(inode->i_mode))
154 return 0;
155 iint = ima_iint_find_insert_get(inode);
156 if (!iint)
157 return 0;
159 mutex_lock(&iint->mutex);
160 iint->opencount++;
161 if ((mask & MAY_WRITE) || (mask == 0))
162 iint->writecount++;
163 else if (mask & (MAY_READ | MAY_EXEC))
164 iint->readcount++;
166 rc = ima_must_measure(iint, inode, MAY_READ, PATH_CHECK);
167 if (rc < 0)
168 goto out;
170 if ((mask & MAY_WRITE) || (mask == 0))
171 ima_read_write_check(TOMTOU, iint, inode,
172 path->dentry->d_name.name);
174 if ((mask & (MAY_WRITE | MAY_READ | MAY_EXEC)) != MAY_READ)
175 goto out;
177 ima_read_write_check(OPEN_WRITERS, iint, inode,
178 path->dentry->d_name.name);
179 if (!(iint->flags & IMA_MEASURED)) {
180 struct dentry *dentry = dget(path->dentry);
181 struct vfsmount *mnt = mntget(path->mnt);
183 file = dentry_open(dentry, mnt, O_RDONLY, current_cred());
184 if (IS_ERR(file)) {
185 pr_info("%s dentry_open failed\n", dentry->d_name.name);
186 rc = PTR_ERR(file);
187 file = NULL;
188 goto out;
190 rc = get_path_measurement(iint, file, dentry->d_name.name);
192 out:
193 mutex_unlock(&iint->mutex);
194 if (file)
195 fput(file);
196 kref_put(&iint->refcount, iint_free);
197 return 0;
200 static int process_measurement(struct file *file, const unsigned char *filename,
201 int mask, int function)
203 struct inode *inode = file->f_dentry->d_inode;
204 struct ima_iint_cache *iint;
205 int rc;
207 if (!ima_initialized || !S_ISREG(inode->i_mode))
208 return 0;
209 iint = ima_iint_find_insert_get(inode);
210 if (!iint)
211 return -ENOMEM;
213 mutex_lock(&iint->mutex);
214 rc = ima_must_measure(iint, inode, mask, function);
215 if (rc != 0)
216 goto out;
218 rc = ima_collect_measurement(iint, file);
219 if (!rc)
220 ima_store_measurement(iint, file, filename);
221 out:
222 mutex_unlock(&iint->mutex);
223 kref_put(&iint->refcount, iint_free);
224 return rc;
227 static void opencount_get(struct file *file)
229 struct inode *inode = file->f_dentry->d_inode;
230 struct ima_iint_cache *iint;
232 if (!ima_initialized || !S_ISREG(inode->i_mode))
233 return;
234 iint = ima_iint_find_insert_get(inode);
235 if (!iint)
236 return;
237 mutex_lock(&iint->mutex);
238 iint->opencount++;
239 mutex_unlock(&iint->mutex);
243 * ima_file_mmap - based on policy, collect/store measurement.
244 * @file: pointer to the file to be measured (May be NULL)
245 * @prot: contains the protection that will be applied by the kernel.
247 * Measure files being mmapped executable based on the ima_must_measure()
248 * policy decision.
250 * Return 0 on success, an error code on failure.
251 * (Based on the results of appraise_measurement().)
253 int ima_file_mmap(struct file *file, unsigned long prot)
255 int rc;
257 if (!file)
258 return 0;
259 if (prot & PROT_EXEC)
260 rc = process_measurement(file, file->f_dentry->d_name.name,
261 MAY_EXEC, FILE_MMAP);
262 return 0;
266 * ima_shm_check - IPC shm and shmat create/fput a file
268 * Maintain the opencount for these files to prevent unnecessary
269 * imbalance messages.
271 void ima_shm_check(struct file *file)
273 opencount_get(file);
274 return;
278 * ima_bprm_check - based on policy, collect/store measurement.
279 * @bprm: contains the linux_binprm structure
281 * The OS protects against an executable file, already open for write,
282 * from being executed in deny_write_access() and an executable file,
283 * already open for execute, from being modified in get_write_access().
284 * So we can be certain that what we verify and measure here is actually
285 * what is being executed.
287 * Return 0 on success, an error code on failure.
288 * (Based on the results of appraise_measurement().)
290 int ima_bprm_check(struct linux_binprm *bprm)
292 int rc;
294 rc = process_measurement(bprm->file, bprm->filename,
295 MAY_EXEC, BPRM_CHECK);
296 return 0;
299 static int __init init_ima(void)
301 int error;
303 ima_iintcache_init();
304 error = ima_init();
305 ima_initialized = 1;
306 return error;
309 static void __exit cleanup_ima(void)
311 ima_cleanup();
314 late_initcall(init_ima); /* Start IMA after the TPM is available */
316 MODULE_DESCRIPTION("Integrity Measurement Architecture");
317 MODULE_LICENSE("GPL");