blob: 122f17fc7fc1f967074c16aca77b401cb3bf982e [file] [log] [blame]
Mimi Zohar3323eec2009-02-04 09:06:58 -05001/*
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
16 * implements the IMA hooks: ima_bprm_check, ima_file_mmap,
17 * and ima_path_check.
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>
24
25#include "ima.h"
26
27int ima_initialized;
28
29char *ima_hash = "sha1";
30static int __init hash_setup(char *str)
31{
Mimi Zohar07ff7a02009-05-05 13:13:10 -040032 if (strncmp(str, "md5", 3) == 0)
33 ima_hash = "md5";
Mimi Zohar3323eec2009-02-04 09:06:58 -050034 return 1;
35}
36__setup("ima_hash=", hash_setup);
37
38/**
39 * ima_file_free - called on __fput()
40 * @file: pointer to file structure being freed
41 *
42 * Flag files that changed, based on i_version;
43 * and decrement the iint readcount/writecount.
44 */
45void ima_file_free(struct file *file)
46{
47 struct inode *inode = file->f_dentry->d_inode;
48 struct ima_iint_cache *iint;
49
50 if (!ima_initialized || !S_ISREG(inode->i_mode))
51 return;
52 iint = ima_iint_find_get(inode);
53 if (!iint)
54 return;
55
56 mutex_lock(&iint->mutex);
Mimi Zohar1df9f0a2009-02-04 09:07:02 -050057 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;
66 }
67 }
68 iint->opencount--;
69
Mimi Zohar3323eec2009-02-04 09:06:58 -050070 if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
71 iint->readcount--;
72
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;
78 }
79 }
80 mutex_unlock(&iint->mutex);
81 kref_put(&iint->refcount, iint_free);
82}
83
84/* ima_read_write_check - reflect possible reading/writing errors in the PCR.
85 *
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.
88 *
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.
91 *
92 * In either case invalidate the PCR.
93 */
94enum iint_pcr_error { TOMTOU, OPEN_WRITERS };
95static 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)
99{
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;
111 }
112}
113
114static int get_path_measurement(struct ima_iint_cache *iint, struct file *file,
115 const unsigned char *filename)
116{
117 int rc = 0;
118
119 if (IS_ERR(file)) {
120 pr_info("%s dentry_open failed\n", filename);
121 return rc;
122 }
Mimi Zohar1df9f0a2009-02-04 09:07:02 -0500123 iint->opencount++;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500124 iint->readcount++;
125
126 rc = ima_collect_measurement(iint, file);
127 if (!rc)
128 ima_store_measurement(iint, file, filename);
129 return rc;
130}
131
132/**
133 * ima_path_check - based on policy, collect/store measurement.
134 * @path: contains a pointer to the path to be measured
135 * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
136 *
137 * Measure the file being open for readonly, based on the
138 * ima_must_measure() policy decision.
139 *
140 * Keep read/write counters for all files, but only
141 * invalidate the PCR for measured files:
142 * - Opening a file for write when already open for read,
143 * results in a time of measure, time of use (ToMToU) error.
144 * - Opening a file for read when already open for write,
145 * could result in a file measurement error.
146 *
147 * Return 0 on success, an error code on failure.
148 * (Based on the results of appraise_measurement().)
149 */
150int ima_path_check(struct path *path, int mask)
151{
152 struct inode *inode = path->dentry->d_inode;
153 struct ima_iint_cache *iint;
154 struct file *file = NULL;
155 int rc;
156
157 if (!ima_initialized || !S_ISREG(inode->i_mode))
158 return 0;
159 iint = ima_iint_find_insert_get(inode);
160 if (!iint)
161 return 0;
162
163 mutex_lock(&iint->mutex);
Mimi Zohar1df9f0a2009-02-04 09:07:02 -0500164 iint->opencount++;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500165 if ((mask & MAY_WRITE) || (mask == 0))
166 iint->writecount++;
167 else if (mask & (MAY_READ | MAY_EXEC))
168 iint->readcount++;
169
170 rc = ima_must_measure(iint, inode, MAY_READ, PATH_CHECK);
171 if (rc < 0)
172 goto out;
173
174 if ((mask & MAY_WRITE) || (mask == 0))
175 ima_read_write_check(TOMTOU, iint, inode,
176 path->dentry->d_name.name);
177
178 if ((mask & (MAY_WRITE | MAY_READ | MAY_EXEC)) != MAY_READ)
179 goto out;
180
181 ima_read_write_check(OPEN_WRITERS, iint, inode,
182 path->dentry->d_name.name);
183 if (!(iint->flags & IMA_MEASURED)) {
184 struct dentry *dentry = dget(path->dentry);
185 struct vfsmount *mnt = mntget(path->mnt);
186
187 file = dentry_open(dentry, mnt, O_RDONLY, current->cred);
188 rc = get_path_measurement(iint, file, dentry->d_name.name);
189 }
190out:
191 mutex_unlock(&iint->mutex);
192 if (file)
193 fput(file);
194 kref_put(&iint->refcount, iint_free);
195 return 0;
196}
197
198static int process_measurement(struct file *file, const unsigned char *filename,
199 int mask, int function)
200{
201 struct inode *inode = file->f_dentry->d_inode;
202 struct ima_iint_cache *iint;
203 int rc;
204
205 if (!ima_initialized || !S_ISREG(inode->i_mode))
206 return 0;
207 iint = ima_iint_find_insert_get(inode);
208 if (!iint)
209 return -ENOMEM;
210
211 mutex_lock(&iint->mutex);
212 rc = ima_must_measure(iint, inode, mask, function);
213 if (rc != 0)
214 goto out;
215
216 rc = ima_collect_measurement(iint, file);
217 if (!rc)
218 ima_store_measurement(iint, file, filename);
219out:
220 mutex_unlock(&iint->mutex);
221 kref_put(&iint->refcount, iint_free);
222 return rc;
223}
224
Mimi Zohar1df9f0a2009-02-04 09:07:02 -0500225static void opencount_get(struct file *file)
226{
227 struct inode *inode = file->f_dentry->d_inode;
228 struct ima_iint_cache *iint;
229
230 if (!ima_initialized || !S_ISREG(inode->i_mode))
231 return;
232 iint = ima_iint_find_insert_get(inode);
233 if (!iint)
234 return;
235 mutex_lock(&iint->mutex);
236 iint->opencount++;
237 mutex_unlock(&iint->mutex);
238}
239
Mimi Zohar3323eec2009-02-04 09:06:58 -0500240/**
241 * ima_file_mmap - based on policy, collect/store measurement.
242 * @file: pointer to the file to be measured (May be NULL)
243 * @prot: contains the protection that will be applied by the kernel.
244 *
245 * Measure files being mmapped executable based on the ima_must_measure()
246 * policy decision.
247 *
248 * Return 0 on success, an error code on failure.
249 * (Based on the results of appraise_measurement().)
250 */
251int ima_file_mmap(struct file *file, unsigned long prot)
252{
253 int rc;
254
255 if (!file)
256 return 0;
257 if (prot & PROT_EXEC)
258 rc = process_measurement(file, file->f_dentry->d_name.name,
259 MAY_EXEC, FILE_MMAP);
260 return 0;
261}
262
Mimi Zohar1df9f0a2009-02-04 09:07:02 -0500263/*
264 * ima_shm_check - IPC shm and shmat create/fput a file
265 *
266 * Maintain the opencount for these files to prevent unnecessary
267 * imbalance messages.
268 */
269void ima_shm_check(struct file *file)
270{
271 opencount_get(file);
272 return;
273}
274
Mimi Zohar3323eec2009-02-04 09:06:58 -0500275/**
276 * ima_bprm_check - based on policy, collect/store measurement.
277 * @bprm: contains the linux_binprm structure
278 *
279 * The OS protects against an executable file, already open for write,
280 * from being executed in deny_write_access() and an executable file,
281 * already open for execute, from being modified in get_write_access().
282 * So we can be certain that what we verify and measure here is actually
283 * what is being executed.
284 *
285 * Return 0 on success, an error code on failure.
286 * (Based on the results of appraise_measurement().)
287 */
288int ima_bprm_check(struct linux_binprm *bprm)
289{
290 int rc;
291
292 rc = process_measurement(bprm->file, bprm->filename,
293 MAY_EXEC, BPRM_CHECK);
294 return 0;
295}
296
297static int __init init_ima(void)
298{
299 int error;
300
301 ima_iintcache_init();
302 error = ima_init();
303 ima_initialized = 1;
304 return error;
305}
306
Mimi Zoharbab73932009-02-04 09:06:59 -0500307static void __exit cleanup_ima(void)
308{
309 ima_cleanup();
310}
311
Mimi Zohar3323eec2009-02-04 09:06:58 -0500312late_initcall(init_ima); /* Start IMA after the TPM is available */
313
314MODULE_DESCRIPTION("Integrity Measurement Architecture");
315MODULE_LICENSE("GPL");