blob: 69b4856af4daf60469537b7dc16792fd38f45a7e [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 Zohar890275b2010-11-02 10:13:07 -040089 * ima_rdwr_violation_check
Mimi Zohar8eb988c2010-01-20 15:35:41 -050090 *
Mimi Zohar890275b2010-11-02 10:13:07 -040091 * Only invalidate the PCR for measured files:
Mimi Zohar8eb988c2010-01-20 15:35:41 -050092 * - Opening a file for write when already open for read,
93 * results in a time of measure, time of use (ToMToU) error.
94 * - Opening a file for read when already open for write,
95 * could result in a file measurement error.
96 *
97 */
Mimi Zohar890275b2010-11-02 10:13:07 -040098static void ima_rdwr_violation_check(struct file *file)
Mimi Zohar8eb988c2010-01-20 15:35:41 -050099{
100 struct dentry *dentry = file->f_path.dentry;
101 struct inode *inode = dentry->d_inode;
102 fmode_t mode = file->f_mode;
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500103 int rc;
Eric Parisad16ad02010-10-25 14:41:45 -0400104 bool send_tomtou = false, send_writers = false;
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500105
Mimi Zohar890275b2010-11-02 10:13:07 -0400106 if (!S_ISREG(inode->i_mode) || !ima_initialized)
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500107 return;
Eric Parisa178d202010-10-25 14:41:59 -0400108
Mimi Zohar890275b2010-11-02 10:13:07 -0400109 mutex_lock(&inode->i_mutex); /* file metadata: permissions, xattr */
Eric Parisad16ad02010-10-25 14:41:45 -0400110
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500111 if (mode & FMODE_WRITE) {
Mimi Zohara68a27b2010-11-02 10:10:56 -0400112 if (atomic_read(&inode->i_readcount) && IS_IMA(inode))
Eric Parisad16ad02010-10-25 14:41:45 -0400113 send_tomtou = true;
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500114 goto out;
115 }
Eric Parisad16ad02010-10-25 14:41:45 -0400116
Eric Parisbade72d2010-10-25 14:42:25 -0400117 rc = ima_must_measure(NULL, inode, MAY_READ, FILE_CHECK);
118 if (rc < 0)
119 goto out;
120
Eric Parisad16ad02010-10-25 14:41:45 -0400121 if (atomic_read(&inode->i_writecount) > 0)
122 send_writers = true;
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500123out:
Mimi Zohar890275b2010-11-02 10:13:07 -0400124 mutex_unlock(&inode->i_mutex);
Eric Parisad16ad02010-10-25 14:41:45 -0400125
126 if (send_tomtou)
127 ima_add_violation(inode, dentry->d_name.name, "invalid_pcr",
128 "ToMToU");
129 if (send_writers)
130 ima_add_violation(inode, dentry->d_name.name, "invalid_pcr",
131 "open_writers");
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500132}
133
134/*
Eric Parise0d5bd22009-12-04 15:48:00 -0500135 * Decrement ima counts
136 */
Eric Parisbc7d2a32010-10-25 14:42:05 -0400137static void ima_dec_counts(struct inode *inode, struct file *file)
Eric Parise0d5bd22009-12-04 15:48:00 -0500138{
Al Viro1429b3e2009-12-16 06:38:01 -0500139 mode_t mode = file->f_mode;
Eric Parisbc7d2a32010-10-25 14:42:05 -0400140
141 assert_spin_locked(&inode->i_lock);
142
143 if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
Mimi Zohara68a27b2010-11-02 10:10:56 -0400144 if (unlikely(atomic_read(&inode->i_readcount) == 0)) {
Eric Parisbc7d2a32010-10-25 14:42:05 -0400145 if (!ima_limit_imbalance(file)) {
146 printk(KERN_INFO "%s: open/free imbalance (r:%u)\n",
Mimi Zohara68a27b2010-11-02 10:10:56 -0400147 __func__,
148 atomic_read(&inode->i_readcount));
Eric Parisbc7d2a32010-10-25 14:42:05 -0400149 dump_stack();
150 }
151 return;
152 }
Eric Parisbc7d2a32010-10-25 14:42:05 -0400153 }
154}
155
156static void ima_check_last_writer(struct ima_iint_cache *iint,
157 struct inode *inode,
158 struct file *file)
159{
160 mode_t mode = file->f_mode;
Eric Paris497f3232010-10-25 14:41:32 -0400161
Eric Parise0d5bd22009-12-04 15:48:00 -0500162 BUG_ON(!mutex_is_locked(&iint->mutex));
Eric Parisad16ad02010-10-25 14:41:45 -0400163 assert_spin_locked(&inode->i_lock);
Eric Parise0d5bd22009-12-04 15:48:00 -0500164
Eric Parisbc7d2a32010-10-25 14:42:05 -0400165 if (mode & FMODE_WRITE &&
166 atomic_read(&inode->i_writecount) == 1 &&
167 iint->version != inode->i_version)
168 iint->flags &= ~IMA_MEASURED;
169}
Eric Parise0d5bd22009-12-04 15:48:00 -0500170
Eric Parisbc7d2a32010-10-25 14:42:05 -0400171static void ima_file_free_iint(struct ima_iint_cache *iint, struct inode *inode,
172 struct file *file)
173{
174 mutex_lock(&iint->mutex);
175 spin_lock(&inode->i_lock);
176
177 ima_dec_counts(inode, file);
178 ima_check_last_writer(iint, inode, file);
179
180 spin_unlock(&inode->i_lock);
181 mutex_unlock(&iint->mutex);
Eric Parisbc7d2a32010-10-25 14:42:05 -0400182}
183
184static void ima_file_free_noiint(struct inode *inode, struct file *file)
185{
186 spin_lock(&inode->i_lock);
187
188 ima_dec_counts(inode, file);
189
190 spin_unlock(&inode->i_lock);
Eric Parise0d5bd22009-12-04 15:48:00 -0500191}
192
Mimi Zohar3323eec2009-02-04 09:06:58 -0500193/**
194 * ima_file_free - called on __fput()
195 * @file: pointer to file structure being freed
196 *
Mimi Zohar890275b2010-11-02 10:13:07 -0400197 * Flag files that changed, based on i_version
Mimi Zohar3323eec2009-02-04 09:06:58 -0500198 */
199void ima_file_free(struct file *file)
200{
201 struct inode *inode = file->f_dentry->d_inode;
202 struct ima_iint_cache *iint;
203
Mimi Zohare9505982010-08-31 09:38:51 -0400204 if (!iint_initialized || !S_ISREG(inode->i_mode))
Mimi Zohar3323eec2009-02-04 09:06:58 -0500205 return;
Eric Paris196f5182010-10-25 14:42:19 -0400206
Eric Paris64c62f02010-10-25 14:42:12 -0400207 iint = ima_iint_find(inode);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500208
Eric Parisbc7d2a32010-10-25 14:42:05 -0400209 if (iint)
210 ima_file_free_iint(iint, inode, file);
211 else
212 ima_file_free_noiint(inode, file);
Eric Parisad16ad02010-10-25 14:41:45 -0400213
Mimi Zohar3323eec2009-02-04 09:06:58 -0500214}
215
Mimi Zohar3323eec2009-02-04 09:06:58 -0500216static int process_measurement(struct file *file, const unsigned char *filename,
217 int mask, int function)
218{
219 struct inode *inode = file->f_dentry->d_inode;
220 struct ima_iint_cache *iint;
Mimi Zohare9505982010-08-31 09:38:51 -0400221 int rc = 0;
Mimi Zohar3323eec2009-02-04 09:06:58 -0500222
223 if (!ima_initialized || !S_ISREG(inode->i_mode))
224 return 0;
Eric Parisbc7d2a32010-10-25 14:42:05 -0400225
226 rc = ima_must_measure(NULL, inode, mask, function);
227 if (rc != 0)
228 return rc;
229retry:
Eric Paris64c62f02010-10-25 14:42:12 -0400230 iint = ima_iint_find(inode);
Eric Parisbc7d2a32010-10-25 14:42:05 -0400231 if (!iint) {
232 rc = ima_inode_alloc(inode);
233 if (!rc || rc == -EEXIST)
234 goto retry;
235 return rc;
236 }
Mimi Zohar3323eec2009-02-04 09:06:58 -0500237
238 mutex_lock(&iint->mutex);
Eric Parisbc7d2a32010-10-25 14:42:05 -0400239
Mimi Zohar3323eec2009-02-04 09:06:58 -0500240 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);
Mimi Zohar3323eec2009-02-04 09:06:58 -0500249 return rc;
250}
251
252/**
253 * ima_file_mmap - based on policy, collect/store measurement.
254 * @file: pointer to the file to be measured (May be NULL)
255 * @prot: contains the protection that will be applied by the kernel.
256 *
257 * Measure files being mmapped executable based on the ima_must_measure()
258 * policy decision.
259 *
260 * Return 0 on success, an error code on failure.
261 * (Based on the results of appraise_measurement().)
262 */
263int ima_file_mmap(struct file *file, unsigned long prot)
264{
265 int rc;
266
267 if (!file)
268 return 0;
269 if (prot & PROT_EXEC)
270 rc = process_measurement(file, file->f_dentry->d_name.name,
271 MAY_EXEC, FILE_MMAP);
272 return 0;
273}
274
275/**
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
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500297/**
298 * ima_path_check - based on policy, collect/store measurement.
299 * @file: pointer to the file to be measured
300 * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
301 *
302 * Measure files based on the ima_must_measure() policy decision.
303 *
304 * Always return 0 and audit dentry_open failures.
305 * (Return code will be based upon measurement appraisal.)
306 */
Mimi Zohar9bbb6ca2010-01-26 17:02:40 -0500307int ima_file_check(struct file *file, int mask)
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500308{
309 int rc;
310
Mimi Zohar890275b2010-11-02 10:13:07 -0400311 ima_rdwr_violation_check(file);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500312 rc = process_measurement(file, file->f_dentry->d_name.name,
313 mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
Mimi Zohar1e93d002010-01-26 17:02:41 -0500314 FILE_CHECK);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500315 return 0;
316}
Mimi Zohar9bbb6ca2010-01-26 17:02:40 -0500317EXPORT_SYMBOL_GPL(ima_file_check);
Mimi Zohar8eb988c2010-01-20 15:35:41 -0500318
Mimi Zohar3323eec2009-02-04 09:06:58 -0500319static int __init init_ima(void)
320{
321 int error;
322
Mimi Zohar3323eec2009-02-04 09:06:58 -0500323 error = ima_init();
324 ima_initialized = 1;
325 return error;
326}
327
Mimi Zoharbab73932009-02-04 09:06:59 -0500328static void __exit cleanup_ima(void)
329{
330 ima_cleanup();
331}
332
Mimi Zohar3323eec2009-02-04 09:06:58 -0500333late_initcall(init_ima); /* Start IMA after the TPM is available */
334
335MODULE_DESCRIPTION("Integrity Measurement Architecture");
336MODULE_LICENSE("GPL");