blob: 1dccafef7494b6e2d4986de522edb7cf63b2680f [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
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 Zohar3323eec2009-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 Zohar3323eec2009-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 Zohar3323eec2009-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
Eric Parise0d5bd22009-12-04 15:48:00 -050088/*
Mimi Zohar8eb988c2010-01-20 15:35:41 -050089 * ima_counts_get - increment file counts
90 *
91 * Maintain read/write counters for all files, but only
92 * invalidate the PCR for measured files:
93 * - Opening a file for write when already open for read,
94 * results in a time of measure, time of use (ToMToU) error.
95 * - Opening a file for read when already open for write,
96 * could result in a file measurement error.
97 *
98 */
99void ima_counts_get(struct file *file)
100{
101 struct dentry *dentry = file->f_path.dentry;
102 struct inode *inode = dentry->d_inode;
103 fmode_t mode = file->f_mode;
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500104 int rc;
Eric Parisad16ad02010-10-25 14:41:45 -0400105 bool send_tomtou = false, send_writers = false;
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500106
Eric Parisa178d202010-10-25 14:41:59 -0400107 if (!S_ISREG(inode->i_mode))
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500108 return;
Eric Parisa178d202010-10-25 14:41:59 -0400109
Eric Parisad16ad02010-10-25 14:41:45 -0400110 spin_lock(&inode->i_lock);
111
Mimi Zohare9505982010-08-31 09:38:51 -0400112 if (!ima_initialized)
113 goto out;
Eric Parisad16ad02010-10-25 14:41:45 -0400114
Eric Parisa178d202010-10-25 14:41:59 -0400115 rc = ima_must_measure(NULL, inode, MAY_READ, FILE_CHECK);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500116 if (rc < 0)
117 goto out;
118
119 if (mode & FMODE_WRITE) {
Eric Parisa178d202010-10-25 14:41:59 -0400120 if (inode->i_readcount)
Eric Parisad16ad02010-10-25 14:41:45 -0400121 send_tomtou = true;
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500122 goto out;
123 }
Eric Parisad16ad02010-10-25 14:41:45 -0400124
125 if (atomic_read(&inode->i_writecount) > 0)
126 send_writers = true;
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500127out:
Eric Parisa178d202010-10-25 14:41:59 -0400128 /* remember the vfs deals with i_writecount */
129 if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
130 inode->i_readcount++;
Eric Parisad16ad02010-10-25 14:41:45 -0400131 spin_unlock(&inode->i_lock);
Eric Parisad16ad02010-10-25 14:41:45 -0400132
133 if (send_tomtou)
134 ima_add_violation(inode, dentry->d_name.name, "invalid_pcr",
135 "ToMToU");
136 if (send_writers)
137 ima_add_violation(inode, dentry->d_name.name, "invalid_pcr",
138 "open_writers");
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500139}
140
141/*
Eric Parise0d5bd22009-12-04 15:48:00 -0500142 * Decrement ima counts
143 */
Eric Parisbc7d2a32010-10-25 14:42:05 -0400144static void ima_dec_counts(struct inode *inode, struct file *file)
Eric Parise0d5bd22009-12-04 15:48:00 -0500145{
Al Viro1429b3e2009-12-16 06:38:01 -0500146 mode_t mode = file->f_mode;
Eric Parisbc7d2a32010-10-25 14:42:05 -0400147
148 assert_spin_locked(&inode->i_lock);
149
150 if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
151 if (unlikely(inode->i_readcount == 0)) {
152 if (!ima_limit_imbalance(file)) {
153 printk(KERN_INFO "%s: open/free imbalance (r:%u)\n",
154 __func__, inode->i_readcount);
155 dump_stack();
156 }
157 return;
158 }
159 inode->i_readcount--;
160 }
161}
162
163static void ima_check_last_writer(struct ima_iint_cache *iint,
164 struct inode *inode,
165 struct file *file)
166{
167 mode_t mode = file->f_mode;
Eric Paris497f3232010-10-25 14:41:32 -0400168
Eric Parise0d5bd22009-12-04 15:48:00 -0500169 BUG_ON(!mutex_is_locked(&iint->mutex));
Eric Parisad16ad02010-10-25 14:41:45 -0400170 assert_spin_locked(&inode->i_lock);
Eric Parise0d5bd22009-12-04 15:48:00 -0500171
Eric Parisbc7d2a32010-10-25 14:42:05 -0400172 if (mode & FMODE_WRITE &&
173 atomic_read(&inode->i_writecount) == 1 &&
174 iint->version != inode->i_version)
175 iint->flags &= ~IMA_MEASURED;
176}
Eric Parise0d5bd22009-12-04 15:48:00 -0500177
Eric Parisbc7d2a32010-10-25 14:42:05 -0400178static void ima_file_free_iint(struct ima_iint_cache *iint, struct inode *inode,
179 struct file *file)
180{
181 mutex_lock(&iint->mutex);
182 spin_lock(&inode->i_lock);
183
184 ima_dec_counts(inode, file);
185 ima_check_last_writer(iint, inode, file);
186
187 spin_unlock(&inode->i_lock);
188 mutex_unlock(&iint->mutex);
Eric Parisbc7d2a32010-10-25 14:42:05 -0400189}
190
191static void ima_file_free_noiint(struct inode *inode, struct file *file)
192{
193 spin_lock(&inode->i_lock);
194
195 ima_dec_counts(inode, file);
196
197 spin_unlock(&inode->i_lock);
Eric Parise0d5bd22009-12-04 15:48:00 -0500198}
199
Mimi Zohar3323eec2009-02-04 09:06:58 -0500200/**
201 * ima_file_free - called on __fput()
202 * @file: pointer to file structure being freed
203 *
204 * Flag files that changed, based on i_version;
Eric Parisbc7d2a32010-10-25 14:42:05 -0400205 * and decrement the i_readcount.
Mimi Zohar3323eec2009-02-04 09:06:58 -0500206 */
207void ima_file_free(struct file *file)
208{
209 struct inode *inode = file->f_dentry->d_inode;
210 struct ima_iint_cache *iint;
211
Mimi Zohare9505982010-08-31 09:38:51 -0400212 if (!iint_initialized || !S_ISREG(inode->i_mode))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500213 return;
Eric Paris64c62f02010-10-25 14:42:12 -0400214 iint = ima_iint_find(inode);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500215
Eric Parisbc7d2a32010-10-25 14:42:05 -0400216 if (iint)
217 ima_file_free_iint(iint, inode, file);
218 else
219 ima_file_free_noiint(inode, file);
Eric Parisad16ad02010-10-25 14:41:45 -0400220
Mimi Zohar3323eec2009-02-04 09:06:58 -0500221}
222
Mimi Zohar3323eec2009-02-04 09:06:58 -0500223static int process_measurement(struct file *file, const unsigned char *filename,
224 int mask, int function)
225{
226 struct inode *inode = file->f_dentry->d_inode;
227 struct ima_iint_cache *iint;
Mimi Zohare9505982010-08-31 09:38:51 -0400228 int rc = 0;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500229
230 if (!ima_initialized || !S_ISREG(inode->i_mode))
231 return 0;
Eric Parisbc7d2a32010-10-25 14:42:05 -0400232
233 rc = ima_must_measure(NULL, inode, mask, function);
234 if (rc != 0)
235 return rc;
236retry:
Eric Paris64c62f02010-10-25 14:42:12 -0400237 iint = ima_iint_find(inode);
Eric Parisbc7d2a32010-10-25 14:42:05 -0400238 if (!iint) {
239 rc = ima_inode_alloc(inode);
240 if (!rc || rc == -EEXIST)
241 goto retry;
242 return rc;
243 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500244
245 mutex_lock(&iint->mutex);
Eric Parisbc7d2a32010-10-25 14:42:05 -0400246
Mimi Zohar3323eec2009-02-04 09:06:58 -0500247 rc = ima_must_measure(iint, inode, mask, function);
248 if (rc != 0)
249 goto out;
250
251 rc = ima_collect_measurement(iint, file);
252 if (!rc)
253 ima_store_measurement(iint, file, filename);
254out:
255 mutex_unlock(&iint->mutex);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500256 return rc;
257}
258
259/**
260 * ima_file_mmap - based on policy, collect/store measurement.
261 * @file: pointer to the file to be measured (May be NULL)
262 * @prot: contains the protection that will be applied by the kernel.
263 *
264 * Measure files being mmapped executable based on the ima_must_measure()
265 * policy decision.
266 *
267 * Return 0 on success, an error code on failure.
268 * (Based on the results of appraise_measurement().)
269 */
270int ima_file_mmap(struct file *file, unsigned long prot)
271{
272 int rc;
273
274 if (!file)
275 return 0;
276 if (prot & PROT_EXEC)
277 rc = process_measurement(file, file->f_dentry->d_name.name,
278 MAY_EXEC, FILE_MMAP);
279 return 0;
280}
281
282/**
283 * ima_bprm_check - based on policy, collect/store measurement.
284 * @bprm: contains the linux_binprm structure
285 *
286 * The OS protects against an executable file, already open for write,
287 * from being executed in deny_write_access() and an executable file,
288 * already open for execute, from being modified in get_write_access().
289 * So we can be certain that what we verify and measure here is actually
290 * what is being executed.
291 *
292 * Return 0 on success, an error code on failure.
293 * (Based on the results of appraise_measurement().)
294 */
295int ima_bprm_check(struct linux_binprm *bprm)
296{
297 int rc;
298
299 rc = process_measurement(bprm->file, bprm->filename,
300 MAY_EXEC, BPRM_CHECK);
301 return 0;
302}
303
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500304/**
305 * ima_path_check - based on policy, collect/store measurement.
306 * @file: pointer to the file to be measured
307 * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
308 *
309 * Measure files based on the ima_must_measure() policy decision.
310 *
311 * Always return 0 and audit dentry_open failures.
312 * (Return code will be based upon measurement appraisal.)
313 */
Mimi Zohar9bbb6ca2010-01-26 17:02:40 -0500314int ima_file_check(struct file *file, int mask)
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500315{
316 int rc;
317
318 rc = process_measurement(file, file->f_dentry->d_name.name,
319 mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
Mimi Zohar1e93d002010-01-26 17:02:41 -0500320 FILE_CHECK);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500321 return 0;
322}
Mimi Zohar9bbb6ca2010-01-26 17:02:40 -0500323EXPORT_SYMBOL_GPL(ima_file_check);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500324
Mimi Zohar3323eec2009-02-04 09:06:58 -0500325static int __init init_ima(void)
326{
327 int error;
328
Mimi Zohar3323eec2009-02-04 09:06:58 -0500329 error = ima_init();
330 ima_initialized = 1;
331 return error;
332}
333
Mimi Zoharbab73932009-02-04 09:06:59 -0500334static void __exit cleanup_ima(void)
335{
336 ima_cleanup();
337}
338
Mimi Zohar3323eec2009-02-04 09:06:58 -0500339late_initcall(init_ima); /* Start IMA after the TPM is available */
340
341MODULE_DESCRIPTION("Integrity Measurement Architecture");
342MODULE_LICENSE("GPL");