blob: f93641382e9f9483576578a3ba5f41286f4cc3ab [file] [log] [blame]
Mimi Zohar3323eec92009-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
Eric Parise0d5bd22009-12-04 15:48:00 -050016 * implements the IMA hooks: ima_bprm_check, ima_file_mmap,
Mimi Zohar9bbb6ca2010-01-26 17:02:40 -050017 * and ima_file_check.
Mimi Zohar3323eec92009-02-04 09:06:58 -050018 */
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Mimi Zohar3323eec92009-02-04 09:06:58 -050025
26#include "ima.h"
27
28int ima_initialized;
29
30char *ima_hash = "sha1";
31static int __init hash_setup(char *str)
32{
Mimi Zohar07ff7a02009-05-05 13:13:10 -040033 if (strncmp(str, "md5", 3) == 0)
34 ima_hash = "md5";
Mimi Zohar3323eec92009-02-04 09:06:58 -050035 return 1;
36}
37__setup("ima_hash=", hash_setup);
38
Mimi Zohard1625432009-12-04 15:48:40 -050039struct 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
Mimi Zohar8eb988c2010-01-20 15:35:41 -050088/* 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
Eric Parise0d5bd22009-12-04 15:48:00 -0500118/*
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
125 iint->opencount++;
126 if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
127 iint->readcount++;
128 if (mode & FMODE_WRITE)
129 iint->writecount++;
130}
131
132/*
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500133 * ima_counts_get - increment file counts
134 *
135 * Maintain read/write counters for all files, but only
136 * invalidate the PCR for measured files:
137 * - Opening a file for write when already open for read,
138 * results in a time of measure, time of use (ToMToU) error.
139 * - Opening a file for read when already open for write,
140 * could result in a file measurement error.
141 *
142 */
143void ima_counts_get(struct file *file)
144{
145 struct dentry *dentry = file->f_path.dentry;
146 struct inode *inode = dentry->d_inode;
147 fmode_t mode = file->f_mode;
148 struct ima_iint_cache *iint;
149 int rc;
150
151 if (!ima_initialized || !S_ISREG(inode->i_mode))
152 return;
153 iint = ima_iint_find_get(inode);
154 if (!iint)
155 return;
156 mutex_lock(&iint->mutex);
Mimi Zohar1e93d002010-01-26 17:02:41 -0500157 rc = ima_must_measure(iint, inode, MAY_READ, FILE_CHECK);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500158 if (rc < 0)
159 goto out;
160
161 if (mode & FMODE_WRITE) {
162 ima_read_write_check(TOMTOU, iint, inode, dentry->d_name.name);
163 goto out;
164 }
165 ima_read_write_check(OPEN_WRITERS, iint, inode, dentry->d_name.name);
166out:
167 ima_inc_counts(iint, file->f_mode);
168 mutex_unlock(&iint->mutex);
169
170 kref_put(&iint->refcount, iint_free);
171}
172
173/*
Eric Parise0d5bd22009-12-04 15:48:00 -0500174 * Decrement ima counts
175 */
176static void ima_dec_counts(struct ima_iint_cache *iint, struct inode *inode,
Al Viro1429b3e2009-12-16 06:38:01 -0500177 struct file *file)
Eric Parise0d5bd22009-12-04 15:48:00 -0500178{
Al Viro1429b3e2009-12-16 06:38:01 -0500179 mode_t mode = file->f_mode;
Eric Parise0d5bd22009-12-04 15:48:00 -0500180 BUG_ON(!mutex_is_locked(&iint->mutex));
181
182 iint->opencount--;
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
Mimi Zohard1625432009-12-04 15:48:40 -0500193 if (((iint->opencount < 0) ||
194 (iint->readcount < 0) ||
195 (iint->writecount < 0)) &&
196 !ima_limit_imbalance(file)) {
Eric Parise0d5bd22009-12-04 15:48:00 -0500197 printk(KERN_INFO "%s: open/free imbalance (r:%ld w:%ld o:%ld)\n",
H Hartley Sweetena19c5bb2010-03-09 17:59:59 -0600198 __func__, iint->readcount, iint->writecount,
Eric Parise0d5bd22009-12-04 15:48:00 -0500199 iint->opencount);
200 dump_stack();
201 }
202}
203
Mimi Zohar3323eec92009-02-04 09:06:58 -0500204/**
205 * ima_file_free - called on __fput()
206 * @file: pointer to file structure being freed
207 *
208 * Flag files that changed, based on i_version;
209 * and decrement the iint readcount/writecount.
210 */
211void ima_file_free(struct file *file)
212{
213 struct inode *inode = file->f_dentry->d_inode;
214 struct ima_iint_cache *iint;
215
216 if (!ima_initialized || !S_ISREG(inode->i_mode))
217 return;
218 iint = ima_iint_find_get(inode);
219 if (!iint)
220 return;
221
222 mutex_lock(&iint->mutex);
Al Viro1429b3e2009-12-16 06:38:01 -0500223 ima_dec_counts(iint, inode, file);
Mimi Zohar3323eec92009-02-04 09:06:58 -0500224 mutex_unlock(&iint->mutex);
225 kref_put(&iint->refcount, iint_free);
226}
227
Mimi Zohar3323eec92009-02-04 09:06:58 -0500228static int process_measurement(struct file *file, const unsigned char *filename,
229 int mask, int function)
230{
231 struct inode *inode = file->f_dentry->d_inode;
232 struct ima_iint_cache *iint;
233 int rc;
234
235 if (!ima_initialized || !S_ISREG(inode->i_mode))
236 return 0;
Eric Paris93533842009-12-04 15:47:52 -0500237 iint = ima_iint_find_get(inode);
Mimi Zohar3323eec92009-02-04 09:06:58 -0500238 if (!iint)
239 return -ENOMEM;
240
241 mutex_lock(&iint->mutex);
242 rc = ima_must_measure(iint, inode, mask, function);
243 if (rc != 0)
244 goto out;
245
246 rc = ima_collect_measurement(iint, file);
247 if (!rc)
248 ima_store_measurement(iint, file, filename);
249out:
250 mutex_unlock(&iint->mutex);
251 kref_put(&iint->refcount, iint_free);
252 return rc;
253}
254
255/**
256 * ima_file_mmap - based on policy, collect/store measurement.
257 * @file: pointer to the file to be measured (May be NULL)
258 * @prot: contains the protection that will be applied by the kernel.
259 *
260 * Measure files being mmapped executable based on the ima_must_measure()
261 * policy decision.
262 *
263 * Return 0 on success, an error code on failure.
264 * (Based on the results of appraise_measurement().)
265 */
266int ima_file_mmap(struct file *file, unsigned long prot)
267{
268 int rc;
269
270 if (!file)
271 return 0;
272 if (prot & PROT_EXEC)
273 rc = process_measurement(file, file->f_dentry->d_name.name,
274 MAY_EXEC, FILE_MMAP);
275 return 0;
276}
277
278/**
279 * ima_bprm_check - based on policy, collect/store measurement.
280 * @bprm: contains the linux_binprm structure
281 *
282 * The OS protects against an executable file, already open for write,
283 * from being executed in deny_write_access() and an executable file,
284 * already open for execute, from being modified in get_write_access().
285 * So we can be certain that what we verify and measure here is actually
286 * what is being executed.
287 *
288 * Return 0 on success, an error code on failure.
289 * (Based on the results of appraise_measurement().)
290 */
291int ima_bprm_check(struct linux_binprm *bprm)
292{
293 int rc;
294
295 rc = process_measurement(bprm->file, bprm->filename,
296 MAY_EXEC, BPRM_CHECK);
297 return 0;
298}
299
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500300/**
301 * ima_path_check - based on policy, collect/store measurement.
302 * @file: pointer to the file to be measured
303 * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
304 *
305 * Measure files based on the ima_must_measure() policy decision.
306 *
307 * Always return 0 and audit dentry_open failures.
308 * (Return code will be based upon measurement appraisal.)
309 */
Mimi Zohar9bbb6ca2010-01-26 17:02:40 -0500310int ima_file_check(struct file *file, int mask)
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500311{
312 int rc;
313
314 rc = process_measurement(file, file->f_dentry->d_name.name,
315 mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
Mimi Zohar1e93d002010-01-26 17:02:41 -0500316 FILE_CHECK);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500317 return 0;
318}
Mimi Zohar9bbb6ca2010-01-26 17:02:40 -0500319EXPORT_SYMBOL_GPL(ima_file_check);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500320
Mimi Zohar3323eec92009-02-04 09:06:58 -0500321static int __init init_ima(void)
322{
323 int error;
324
Mimi Zohar3323eec92009-02-04 09:06:58 -0500325 error = ima_init();
326 ima_initialized = 1;
327 return error;
328}
329
Mimi Zoharbab73932009-02-04 09:06:59 -0500330static void __exit cleanup_ima(void)
331{
332 ima_cleanup();
333}
334
Mimi Zohar3323eec92009-02-04 09:06:58 -0500335late_initcall(init_ima); /* Start IMA after the TPM is available */
336
337MODULE_DESCRIPTION("Integrity Measurement Architecture");
338MODULE_LICENSE("GPL");